Skip to content

Commit

Permalink
# This is a combination of 3 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

Add module stock_quant_package_product_packaging

# The commit message #2 will be skipped:

# Add tests

# The commit message #3 will be skipped:

# Display single_product fields in debug mode
  • Loading branch information
grindtildeath committed Mar 23, 2020
1 parent 8949b92 commit e2c68b7
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 0 deletions.
1 change: 1 addition & 0 deletions stock_quant_package_product_packaging/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
22 changes: 22 additions & 0 deletions stock_quant_package_product_packaging/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
{
"name": "Stock Quant Package Product Packaging",
"summary": "Use product packagings on packages",
"version": "12.0.1.0.0",
"development_status": "Alpha",
"category": "Warehouse Management",
"website": "https://github.com/OCA/stock-logistics-workflow",
"author": "Camptocamp, Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": [
"stock",
],
"data": [
"views/stock_quant_package.xml",
],
"demo": [
],
}
2 changes: 2 additions & 0 deletions stock_quant_package_product_packaging/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import stock_quant_package
from . import stock_move_line
13 changes: 13 additions & 0 deletions stock_quant_package_product_packaging/models/stock_move_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import models


class StockMoveLine(models.Model):
_inherit = 'stock.move.line'

def _action_done(self):
res = super()._action_done()
for line in self.filtered(lambda l: l.result_package_id):
line.result_package_id.auto_assign_packaging()
return res
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import api, fields, models


class StockQuantPackage(models.Model):

_inherit = "stock.quant.package"

# This is not the same thing as 'packaging_id':
# * packaging_id is the "Package type", packaging which have
# no 'product_id' and used for the delivery (postal 2kg, ...)
# * product_packaging_id is the actual Product Packaging (usually
# using a GTIN) used for the internal logistics/reception. It
# has a product_id
product_packaging_id = fields.Many2one(
'product.packaging',
'Product Packaging',
index=True,
help="Packaging of the product, used for internal logistics"
"transfers, put-away rules, ..."
)
single_product_id = fields.Many2one(
'product.product', compute='_compute_single_product'
)
single_product_qty = fields.Float(compute='_compute_single_product')

@api.depends('quant_ids', 'quant_ids.product_id')
def _compute_single_product(self):
for pack in self:
pack_products = pack.quant_ids.mapped('product_id')
if len(pack_products) == 1:
pack.single_product_id = pack_products.id
# TODO handle uom
pack.single_product_qty = sum(pack.quant_ids.mapped('quantity'))
else:
pack.single_product_id = False
pack.single_product_qty = 0

def auto_assign_packaging(self):
for pack in self:
if (
not pack.product_packaging_id
and pack.single_product_id
and pack.single_product_qty
):
packaging = self.env['product.packaging'].search(
[
('product_id', '=', pack.single_product_id.id),
('qty', '=', pack.single_product_qty)
], limit=1
)
if packaging:
pack.write({'product_packaging_id': packaging.id})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Akim Juillerat <[email protected]>
7 changes: 7 additions & 0 deletions stock_quant_package_product_packaging/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This module allows to define on a Product Package (`stock.quant.package`), a
Packaging Type (`product.packaging`) that is linked to a product, if said
package only contains Quants (`stock.quants`) from this product, and the sum of
the quants quantities is equal to the Packaging quantity.

If such a packaging exists, it will be automatically assigned to a package after
the move is set to done.
1 change: 1 addition & 0 deletions stock_quant_package_product_packaging/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_stock_quant_package_product_packaging
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo.tests import SavepointCase


class TestStockQuantPackageProductPackaging(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.receipt_picking_type = cls.env.ref("stock.picking_type_in")
# show_reserved must be set here because it changes the behaviour of
# put_in_pack operation:
# if show_reserved: qty_done must be set on stock.picking.move_line_ids
# if not show_reserved: qty_done must be set on
# stock.picking.move_line_nosuggest_ids
cls.receipt_picking_type.show_reserved = True
cls.product = cls.env.ref("product.product_delivery_02")
cls.packaging = cls.env["product.packaging"].create(
{"name": "10 pack", "product_id": cls.product.id, "qty": 10}
)

def test_auto_assign_packaging(self):
location_dest = self.receipt_picking_type.default_location_dest_id
picking = self.env["stock.picking"].create(
{
"picking_type_id": self.receipt_picking_type.id,
"location_id": self.env.ref("stock.stock_location_suppliers").id,
"location_dest_id": location_dest.id,
}
)
picking.onchange_picking_type()
picking.write(
{
"move_lines": [
(
0,
0,
{
"name": "TEST",
"product_id": self.product.id,
"product_uom_qty": 30.0,
"product_uom": self.product.uom_id.id,
"location_id": picking.location_id.id,
"location_dest_id": picking.location_dest_id.id,
},
)
]
}
)
picking.action_confirm()
picking.move_line_ids.qty_done = 10.0
first_package = picking.put_in_pack()
picking.move_line_ids.filtered(
lambda ml: not ml.result_package_id
).qty_done = 20.0
second_package = picking.put_in_pack()
picking.button_validate()
self.assertEqual(first_package.single_product_id, self.product)
self.assertEqual(first_package.single_product_qty, 10.0)
self.assertEqual(second_package.single_product_id, self.product)
self.assertEqual(second_package.single_product_qty, 20.0)
self.assertEqual(first_package.product_packaging_id, self.packaging)
self.assertFalse(second_package.product_packaging_id)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="view_quant_package_form_inherit" model="ir.ui.view">
<field name="name">stock.quant.package.form.inherit</field>
<field name="model">stock.quant.package</field>
<field name="inherit_id" ref="stock.view_quant_package_form"/>
<field name="arch" type="xml">
<field name="company_id" position="before">
<field name="single_product_id" groups="base.group_no_one" />
<field name="single_product_qty" groups="base.group_no_one" />
<field name="product_packaging_id"
domain="[('product_id', '=', single_product_id), ('qty', '=', single_product_qty)]"/>
</field>
</field>
</record>
</odoo>

0 comments on commit e2c68b7

Please sign in to comment.