mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
@@ -0,0 +1 @@
|
||||
../../../../stock_move_packaging_qty
|
||||
6
setup/stock_move_packaging_qty/setup.py
Normal file
6
setup/stock_move_packaging_qty/setup.py
Normal file
@@ -0,0 +1,6 @@
|
||||
import setuptools
|
||||
|
||||
setuptools.setup(
|
||||
setup_requires=['setuptools-odoo'],
|
||||
odoo_addon=True,
|
||||
)
|
||||
1
stock_move_packaging_qty/__init__.py
Normal file
1
stock_move_packaging_qty/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import models
|
||||
14
stock_move_packaging_qty/__manifest__.py
Normal file
14
stock_move_packaging_qty/__manifest__.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# Copyright 2021 ForgeFlow S.L. (https://www.forgeflow.com)
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
{
|
||||
"name": "Stock Packaging Qty",
|
||||
"summary": "Add packaging fields in the stock moves",
|
||||
"version": "13.0.1.1.0",
|
||||
"author": "ForgeFlow, Odoo Community Association (OCA)",
|
||||
"website": "https://github.com/OCA/stock-logistics-warehouse",
|
||||
"category": "Warehouse",
|
||||
"depends": ["stock"],
|
||||
"data": ["views/stock_picking_form_view.xml", "views/stock_move_tree_view.xml"],
|
||||
"license": "LGPL-3",
|
||||
"installable": True,
|
||||
}
|
||||
1
stock_move_packaging_qty/models/__init__.py
Normal file
1
stock_move_packaging_qty/models/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import stock_move
|
||||
112
stock_move_packaging_qty/models/stock_move.py
Normal file
112
stock_move_packaging_qty/models/stock_move.py
Normal file
@@ -0,0 +1,112 @@
|
||||
# Copyright 2020 Camptocamp SA
|
||||
# Copyright 2021 ForgeFlow, S.L.
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
from odoo.addons import decimal_precision as dp
|
||||
|
||||
|
||||
class StockPicking(models.Model):
|
||||
_inherit = "stock.move"
|
||||
|
||||
product_packaging = fields.Many2one(
|
||||
comodel_name="product.packaging",
|
||||
string="Package",
|
||||
default=False,
|
||||
check_company=True,
|
||||
)
|
||||
product_packaging_qty = fields.Float(
|
||||
string="Package quantity",
|
||||
compute="_compute_product_packaging_qty",
|
||||
inverse="_inverse_product_packaging_qty",
|
||||
digits=dp.get_precision("Product Unit of Measure"),
|
||||
)
|
||||
|
||||
@api.depends(
|
||||
"product_qty", "product_uom", "product_packaging", "product_packaging.qty"
|
||||
)
|
||||
def _compute_product_packaging_qty(self):
|
||||
for move in self:
|
||||
if (
|
||||
not move.product_packaging
|
||||
or move.product_qty == 0
|
||||
or move.product_packaging.qty == 0
|
||||
):
|
||||
move.product_packaging_qty = 0
|
||||
continue
|
||||
# Consider uom
|
||||
if move.product_id.uom_id != move.product_uom:
|
||||
product_qty = move.product_uom._compute_quantity(
|
||||
move.product_uom_qty, move.product_id.uom_id
|
||||
)
|
||||
else:
|
||||
product_qty = move.product_uom_qty
|
||||
move.product_packaging_qty = product_qty / move.product_packaging.qty
|
||||
|
||||
def _prepare_product_packaging_qty_values(self):
|
||||
return {
|
||||
"product_uom_qty": self.product_packaging.qty * self.product_packaging_qty,
|
||||
"product_uom": self.product_packaging.product_uom_id.id,
|
||||
}
|
||||
|
||||
def _inverse_product_packaging_qty(self):
|
||||
for move in self:
|
||||
if move.product_packaging_qty and not move.product_packaging:
|
||||
raise UserError(
|
||||
_(
|
||||
"You must define a package before setting a quantity "
|
||||
"of said package."
|
||||
)
|
||||
)
|
||||
if move.product_packaging and move.product_packaging.qty == 0:
|
||||
raise UserError(
|
||||
_("Please select a packaging with a quantity bigger than 0")
|
||||
)
|
||||
if move.product_packaging and move.product_packaging_qty:
|
||||
move.write(move._prepare_product_packaging_qty_values())
|
||||
|
||||
@api.onchange("product_packaging")
|
||||
def _onchange_product_packaging(self):
|
||||
if self.product_packaging:
|
||||
self.update(
|
||||
{
|
||||
"product_packaging_qty": 1,
|
||||
"product_uom_qty": self.product_packaging.qty,
|
||||
"product_uom": self.product_id.uom_id,
|
||||
}
|
||||
)
|
||||
else:
|
||||
self.update({"product_packaging_qty": 0})
|
||||
if self.product_packaging:
|
||||
return self._check_package()
|
||||
|
||||
@api.onchange("product_packaging_qty")
|
||||
def _onchange_product_packaging_qty(self):
|
||||
if self.product_packaging_qty and self.product_packaging:
|
||||
self.update(self._prepare_product_packaging_qty_values())
|
||||
|
||||
@api.onchange("product_uom_qty", "product_uom")
|
||||
def onchange_quantity(self):
|
||||
res = super().onchange_quantity()
|
||||
if not res:
|
||||
res = self._check_package()
|
||||
return res
|
||||
|
||||
def _check_package(self):
|
||||
default_uom = self.product_id.uom_id
|
||||
pack = self.product_packaging
|
||||
qty = self.product_qty
|
||||
q = default_uom._compute_quantity(pack.qty, self.product_uom)
|
||||
if qty and q and round(qty % q, 2):
|
||||
newqty = qty - (qty % q) + q
|
||||
return {
|
||||
"warning": {
|
||||
"title": _("Warning"),
|
||||
"message": _(
|
||||
"This product is packaged by %.2f %s. You should use %.2f %s."
|
||||
)
|
||||
% (pack.qty, default_uom.name, newqty, self.product_uom.name),
|
||||
},
|
||||
}
|
||||
return {}
|
||||
2
stock_move_packaging_qty/readme/CONTRIBUTORS.rst
Normal file
2
stock_move_packaging_qty/readme/CONTRIBUTORS.rst
Normal file
@@ -0,0 +1,2 @@
|
||||
* Mateu Griful <mateu.griful@forgeflow.com>
|
||||
* Lois Rilo <lois.rilo@forgeflow.com>
|
||||
1
stock_move_packaging_qty/readme/DESCRIPTION.rst
Normal file
1
stock_move_packaging_qty/readme/DESCRIPTION.rst
Normal file
@@ -0,0 +1 @@
|
||||
Add packaging fields in the stock moves
|
||||
1
stock_move_packaging_qty/tests/__init__.py
Normal file
1
stock_move_packaging_qty/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import test_stock_move_packaging_qty
|
||||
@@ -0,0 +1,46 @@
|
||||
# Copyright 2021 ForgeFlow S.L. (https://www.forgeflow.com)
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo.tests import SavepointCase
|
||||
|
||||
|
||||
class TestStockMovePackagingQty(SavepointCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
|
||||
cls.partner = cls.env.ref("base.res_partner_12")
|
||||
cls.product = cls.env.ref("product.product_product_9")
|
||||
cls.packaging = cls.env["product.packaging"].create(
|
||||
{"name": "Test packaging", "product_id": cls.product.id, "qty": 5.0}
|
||||
)
|
||||
cls.wh = cls.env["stock.warehouse"].create(
|
||||
{"name": "Base Warehouse", "code": "TESTWH"}
|
||||
)
|
||||
cls.categ_unit = cls.env.ref("uom.product_uom_categ_unit")
|
||||
cls.uom_unit = cls.env["uom.uom"].search(
|
||||
[("category_id", "=", cls.categ_unit.id), ("uom_type", "=", "reference")],
|
||||
limit=1,
|
||||
)
|
||||
cls.customer_location = cls.env.ref("stock.stock_location_customers")
|
||||
cls.supplier_location = cls.env.ref("stock.stock_location_suppliers")
|
||||
|
||||
def test_product_packaging_qty(self):
|
||||
move = self.env["stock.move"].create(
|
||||
{
|
||||
"name": "Move",
|
||||
"location_id": self.supplier_location.id,
|
||||
"location_dest_id": self.wh.lot_stock_id.id,
|
||||
"partner_id": self.partner.id,
|
||||
"product_id": self.product.id,
|
||||
"product_uom": self.product.uom_id.id,
|
||||
"product_uom_qty": 3.0,
|
||||
"price_unit": 10.0,
|
||||
}
|
||||
)
|
||||
move.write({"product_packaging": self.packaging.id})
|
||||
move._onchange_product_packaging()
|
||||
self.assertEqual(move.product_uom_qty, 5.0)
|
||||
self.assertEqual(move.product_packaging_qty, 1.0)
|
||||
move.write({"product_packaging_qty": 3.0})
|
||||
self.assertEqual(move.product_uom_qty, 15.0)
|
||||
16
stock_move_packaging_qty/views/stock_move_tree_view.xml
Normal file
16
stock_move_packaging_qty/views/stock_move_tree_view.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<odoo>
|
||||
<data>
|
||||
<record id="view_move_extra_tree" model="ir.ui.view">
|
||||
<field name="name">view.move.extra.tree</field>
|
||||
<field name="model">stock.move</field>
|
||||
<field name="inherit_id" ref="stock.view_move_tree" />
|
||||
<field name="arch" type="xml">
|
||||
<field name="product_id" position="after">
|
||||
<field name="product_packaging" />
|
||||
<field name="product_packaging_qty" />
|
||||
</field>
|
||||
</field>
|
||||
</record>
|
||||
</data>
|
||||
</odoo>
|
||||
19
stock_move_packaging_qty/views/stock_picking_form_view.xml
Normal file
19
stock_move_packaging_qty/views/stock_picking_form_view.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<odoo>
|
||||
<data>
|
||||
<record id="stock_picking_view_form_packaging" model="ir.ui.view">
|
||||
<field name="name">stock.picking.form</field>
|
||||
<field name="model">stock.picking</field>
|
||||
<field name="inherit_id" ref="stock.view_picking_form" />
|
||||
<field name="arch" type="xml">
|
||||
<xpath
|
||||
expr="//field[@name='move_ids_without_package']/tree/field[@name='product_uom_qty']"
|
||||
position="before"
|
||||
>
|
||||
<field name="product_packaging" />
|
||||
<field name="product_packaging_qty" />
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</data>
|
||||
</odoo>
|
||||
Reference in New Issue
Block a user