Skip to content

Commit

Permalink
[IMP] stock_product_pack: black, isort, prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-p-marques authored and Usdoo Wedoo committed Sep 18, 2021
1 parent 5718726 commit d4e925e
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 112 deletions.
32 changes: 13 additions & 19 deletions stock_product_pack/__manifest__.py
Expand Up @@ -2,23 +2,17 @@
# Copyright 2019 Tecnativa - Ernesto Tejeda
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
'name': 'Stock product Pack',
'version': '12.0.1.0.1',
'category': 'Warehouse',
'summary': 'This module allows you to get the right available quantities '
'of the packs',
'website': 'https://github.com/OCA/product-pack',
'author': 'NaN·tic, '
'ADHOC SA, '
'Tecnativa, '
'Odoo Community Association (OCA)',
'maintainers': ['ernestotejeda'],
'license': 'AGPL-3',
'depends': [
'product_pack',
'stock',
],
'installable': True,
'auto_install': True,
'application': False,
"name": "Stock product Pack",
"version": "12.0.1.0.1",
"category": "Warehouse",
"summary": "This module allows you to get the right available quantities "
"of the packs",
"website": "https://github.com/OCA/product-pack",
"author": "NaN·tic, " "ADHOC SA, " "Tecnativa, " "Odoo Community Association (OCA)",
"maintainers": ["ernestotejeda"],
"license": "AGPL-3",
"depends": ["product_pack", "stock",],
"installable": True,
"auto_install": True,
"application": False,
}
42 changes: 24 additions & 18 deletions stock_product_pack/models/product_product.py
@@ -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
185 changes: 110 additions & 75 deletions stock_product_pack/tests/test_stock_product_pack.py
@@ -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)

0 comments on commit d4e925e

Please sign in to comment.