forked from OCA/product-pack
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] stock_product_pack: black, isort, prettier
- Loading branch information
1 parent
5718726
commit d4e925e
Showing
3 changed files
with
147 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,45 @@ | ||
# Copyright 2019 Tecnativa - Ernesto Tejeda | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
from odoo import models | ||
import math | ||
|
||
from odoo import models | ||
|
||
|
||
class ProductProduct(models.Model): | ||
_inherit = 'product.product' | ||
_inherit = "product.product" | ||
|
||
def _compute_quantities_dict( | ||
self, lot_id, owner_id, package_id, | ||
from_date=False, to_date=False): | ||
self, lot_id, owner_id, package_id, from_date=False, to_date=False | ||
): | ||
res = super()._compute_quantities_dict( | ||
lot_id, owner_id, package_id, from_date=from_date, to_date=to_date) | ||
packs = self.filtered('pack_ok') | ||
lot_id, owner_id, package_id, from_date=from_date, to_date=to_date | ||
) | ||
packs = self.filtered("pack_ok") | ||
for product in packs.with_context(prefetch_fields=False): | ||
pack_qty_available = [] | ||
pack_virtual_available = [] | ||
subproducts = product.pack_line_ids.filtered( | ||
lambda p: p.product_id.type == 'product') | ||
lambda p: p.product_id.type == "product" | ||
) | ||
for subproduct in subproducts: | ||
subproduct_stock = subproduct.product_id | ||
sub_qty = subproduct.quantity | ||
if sub_qty: | ||
pack_qty_available.append(math.floor( | ||
subproduct_stock.qty_available / sub_qty)) | ||
pack_virtual_available.append(math.floor( | ||
subproduct_stock.virtual_available / sub_qty)) | ||
pack_qty_available.append( | ||
math.floor(subproduct_stock.qty_available / sub_qty) | ||
) | ||
pack_virtual_available.append( | ||
math.floor(subproduct_stock.virtual_available / sub_qty) | ||
) | ||
res[product.id] = { | ||
'qty_available': ( | ||
pack_qty_available and min(pack_qty_available) or False), | ||
'incoming_qty': 0, | ||
'outgoing_qty': 0, | ||
'virtual_available': ( | ||
pack_virtual_available and | ||
min(pack_virtual_available) or False), | ||
"qty_available": ( | ||
pack_qty_available and min(pack_qty_available) or False | ||
), | ||
"incoming_qty": 0, | ||
"outgoing_qty": 0, | ||
"virtual_available": ( | ||
pack_virtual_available and min(pack_virtual_available) or False | ||
), | ||
} | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,131 @@ | ||
# Copyright 2019 Tecnativa - Ernesto Tejeda | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
from odoo.tests import SavepointCase | ||
import logging | ||
|
||
from odoo.tests import SavepointCase | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class TestSaleProductPack(SavepointCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
category_all_id = cls.env.ref('product.product_category_all').id | ||
product_obj = cls.env['product.product'] | ||
cls.pack_dc = cls.env['product.product'].create({ | ||
'name': 'Pack', | ||
'type': 'product', | ||
'pack_ok': True, | ||
'pack_type': 'detailed', | ||
'pack_component_price': 'detailed', | ||
'categ_id': category_all_id, | ||
'pack_line_ids': [ | ||
(0, 0, { | ||
'product_id': product_obj.create({ | ||
'name': 'Component 1', | ||
'type': 'product', | ||
'categ_id': category_all_id, | ||
}).id, | ||
'quantity': 1, | ||
}), | ||
(0, 0, { | ||
'product_id': product_obj.create({ | ||
'name': 'Component 2', | ||
'type': 'product', | ||
'categ_id': category_all_id, | ||
}).id, | ||
'quantity': 1, | ||
}), | ||
(0, 0, { | ||
'product_id': product_obj.create({ | ||
'name': 'Component 3', | ||
'type': 'service', | ||
'categ_id': category_all_id, | ||
}).id, | ||
'quantity': 1, | ||
}), | ||
(0, 0, { | ||
'product_id': product_obj.create({ | ||
'name': 'Component 4', | ||
'type': 'consu', | ||
'categ_id': category_all_id, | ||
}).id, | ||
'quantity': 1, | ||
}), | ||
] | ||
}) | ||
category_all_id = cls.env.ref("product.product_category_all").id | ||
product_obj = cls.env["product.product"] | ||
cls.pack_dc = cls.env["product.product"].create( | ||
{ | ||
"name": "Pack", | ||
"type": "product", | ||
"pack_ok": True, | ||
"pack_type": "detailed", | ||
"pack_component_price": "detailed", | ||
"categ_id": category_all_id, | ||
"pack_line_ids": [ | ||
( | ||
0, | ||
0, | ||
{ | ||
"product_id": product_obj.create( | ||
{ | ||
"name": "Component 1", | ||
"type": "product", | ||
"categ_id": category_all_id, | ||
} | ||
).id, | ||
"quantity": 1, | ||
}, | ||
), | ||
( | ||
0, | ||
0, | ||
{ | ||
"product_id": product_obj.create( | ||
{ | ||
"name": "Component 2", | ||
"type": "product", | ||
"categ_id": category_all_id, | ||
} | ||
).id, | ||
"quantity": 1, | ||
}, | ||
), | ||
( | ||
0, | ||
0, | ||
{ | ||
"product_id": product_obj.create( | ||
{ | ||
"name": "Component 3", | ||
"type": "service", | ||
"categ_id": category_all_id, | ||
} | ||
).id, | ||
"quantity": 1, | ||
}, | ||
), | ||
( | ||
0, | ||
0, | ||
{ | ||
"product_id": product_obj.create( | ||
{ | ||
"name": "Component 4", | ||
"type": "consu", | ||
"categ_id": category_all_id, | ||
} | ||
).id, | ||
"quantity": 1, | ||
}, | ||
), | ||
], | ||
} | ||
) | ||
|
||
def test_compute_quantities_dict(self): | ||
location_id = self.env.ref('stock.stock_location_suppliers').id, | ||
location_dest_id = self.env.ref('stock.stock_location_stock').id, | ||
components = self.pack_dc.pack_line_ids.mapped('product_id') | ||
picking = self.env['stock.picking'].create({ | ||
'partner_id': self.env.ref('base.res_partner_4').id, | ||
'picking_type_id': self.env.ref('stock.picking_type_in').id, | ||
'location_id': location_id, | ||
'location_dest_id': location_dest_id, | ||
'move_lines': [ | ||
(0, 0, { | ||
'name': 'incoming_move_test_01', | ||
'product_id': components[0].id, | ||
'product_uom_qty': 5, | ||
'product_uom': components[0].uom_id.id, | ||
'location_id': location_id, | ||
'location_dest_id': location_dest_id, | ||
}), | ||
(0, 0, { | ||
'name': 'incoming_move_test_02', | ||
'product_id': components[1].id, | ||
'product_uom_qty': 7, | ||
'product_uom': components[1].uom_id.id, | ||
'location_id': location_id, | ||
'location_dest_id': location_dest_id, | ||
}), | ||
] | ||
}) | ||
location_id = (self.env.ref("stock.stock_location_suppliers").id,) | ||
location_dest_id = (self.env.ref("stock.stock_location_stock").id,) | ||
components = self.pack_dc.pack_line_ids.mapped("product_id") | ||
picking = self.env["stock.picking"].create( | ||
{ | ||
"partner_id": self.env.ref("base.res_partner_4").id, | ||
"picking_type_id": self.env.ref("stock.picking_type_in").id, | ||
"location_id": location_id, | ||
"location_dest_id": location_dest_id, | ||
"move_lines": [ | ||
( | ||
0, | ||
0, | ||
{ | ||
"name": "incoming_move_test_01", | ||
"product_id": components[0].id, | ||
"product_uom_qty": 5, | ||
"product_uom": components[0].uom_id.id, | ||
"location_id": location_id, | ||
"location_dest_id": location_dest_id, | ||
}, | ||
), | ||
( | ||
0, | ||
0, | ||
{ | ||
"name": "incoming_move_test_02", | ||
"product_id": components[1].id, | ||
"product_uom_qty": 7, | ||
"product_uom": components[1].uom_id.id, | ||
"location_id": location_id, | ||
"location_dest_id": location_dest_id, | ||
}, | ||
), | ||
], | ||
} | ||
) | ||
picking.action_confirm() | ||
self.assertEqual(self.pack_dc.virtual_available, 5) | ||
self.assertEqual(self.pack_dc.qty_available, 0) | ||
wizard_dict = picking.button_validate() | ||
wizard = self.env[wizard_dict['res_model']].browse( | ||
wizard_dict['res_id']) | ||
wizard = self.env[wizard_dict["res_model"]].browse(wizard_dict["res_id"]) | ||
wizard.process() | ||
self.assertEqual(self.pack_dc.virtual_available, 5) | ||
self.assertEqual(self.pack_dc.qty_available, 5) |