diff --git a/stock_picking_product_kit_helper/__init__.py b/stock_picking_product_kit_helper/__init__.py new file mode 100644 index 000000000..9c982a787 --- /dev/null +++ b/stock_picking_product_kit_helper/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2019 Kitti U. - Ecosoft +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models diff --git a/stock_picking_product_kit_helper/__manifest__.py b/stock_picking_product_kit_helper/__manifest__.py new file mode 100644 index 000000000..b14f3ff39 --- /dev/null +++ b/stock_picking_product_kit_helper/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2019 Kitti U. - Ecosoft +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + 'name': 'Stock Picking Product Kit Helper', + 'summary': 'Set quanity in picking line based on product kit quantity', + 'version': '12.0.1.0.0', + 'category': 'Stock', + 'website': 'https://github.com/OCA/manufacture', + 'author': 'Ecosoft, Odoo Community Association (OCA)', + 'license': 'AGPL-3', + 'installable': True, + 'depends': [ + 'sale_mrp', + ], + 'data': [ + 'security/ir.model.access.csv', + 'views/stock_view.xml', + ], + 'maintainers': ['kittiu'] +} diff --git a/stock_picking_product_kit_helper/models/__init__.py b/stock_picking_product_kit_helper/models/__init__.py new file mode 100644 index 000000000..7945596c7 --- /dev/null +++ b/stock_picking_product_kit_helper/models/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2019 Kitti U. - Ecosoft +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import stock_picking diff --git a/stock_picking_product_kit_helper/models/stock_picking.py b/stock_picking_product_kit_helper/models/stock_picking.py new file mode 100644 index 000000000..1ad27466e --- /dev/null +++ b/stock_picking_product_kit_helper/models/stock_picking.py @@ -0,0 +1,133 @@ +# Copyright 2019 Kitti U. - Ecosoft +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models, fields, api, _ +from odoo.exceptions import ValidationError + + +class StockPicking(models.Model): + _inherit = 'stock.picking' + + product_kit_helper_ids = fields.One2many( + comodel_name='stock.picking.product.kit.helper', + string='Product Kit Helper Lines', + inverse_name='picking_id', + readonly=False, + states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, + ) + has_product_kit = fields.Boolean( + string='Has Product Kit', + compute='_compute_has_product_kit', + help="True if there is at least 1 product kit in the sales order", + ) + + @api.model + def _is_product_kit(self, product, company): + BOM = self.env['mrp.bom'].sudo() + bom = BOM._bom_find(product=product, + company_id=company.id) + return bom and bom.type == 'phantom' + + @api.multi + def _compute_has_product_kit(self): + for picking in self: + if any(self._is_product_kit(line.product_id, line.company_id) + for line in picking.move_lines.mapped('sale_line_id')): + picking.has_product_kit = True + + @api.multi + def show_product_kit(self): + """Find move_lines with product kit to create helper line.""" + self.ensure_one() + BOM = self.env['mrp.bom'].sudo() + helpers = [] + for sale_line in self.move_lines.mapped('sale_line_id'): + bom = BOM._bom_find(product=sale_line.product_id, + company_id=sale_line.company_id.id) + if bom and bom.type == 'phantom': # Create product kit line + helpers.append((0, 0, {'sale_line_id': sale_line.id, + 'product_id': sale_line.product_id.id, + 'product_uom_qty': 0.0, + })) + self.product_kit_helper_ids.unlink() + self.write({'product_kit_helper_ids': helpers}) + + @api.multi + def action_product_kit_helper(self): + """Assign product kit's quantity to stock move.""" + self.ensure_one() + if self.state in ('done', 'cancel'): + raise ValidationError( + _('Product Kit Helper is not allowed on current state')) + for helper in self.product_kit_helper_ids: + helper.action_explode_helper() + + +class StockPickingProductKitHelper(models.Model): + _name = 'stock.picking.product.kit.helper' + _description = """ + Product Kit Helper, allow user to specify quantity of product kit, + to explode as product quantity in operations tab + """ + + picking_id = fields.Many2one( + comodel_name='stock.picking', + string='Picking', + required=True, + index=True, + ondelete='cascade', + ) + sale_line_id = fields.Many2one( + comodel_name='sale.order.line', + string='Sales Order Line', + required=True, + ) + product_id = fields.Many2one( + comodel_name='product.product', + string='Product', + required=True, + readonly=True, + ) + product_uom_qty = fields.Float( + string='Quantity', + ) + product_uom = fields.Many2one( + comodel_name='uom.uom', + string='Unit of Measure', + related='sale_line_id.product_uom', + readonly=True, + ) + + @api.multi + def action_explode_helper(self): + """Explodes product kit quantity to detailed product in stock move.""" + self.ensure_one() + # Mock stock.move, in order to resue stock.move's action_explode + StockMove = self.env['stock.move'] + mock_loc = self.env['stock.location'].sudo().search([], limit=1) + mock_pt = self.env['stock.picking.type'].sudo().search([], limit=1) + mock_stock_move = StockMove.sudo().create({ + 'name': '/', + 'product_id': self.product_id.id, + 'product_uom': self.product_uom.id, + 'product_uom_qty': self.product_uom_qty, + 'picking_type_id': mock_pt.id, + 'location_id': mock_loc.id, + 'location_dest_id': mock_loc.id, + }) + # Reuse explode function and assign quantity_done in stock.move + mock_processed_moves = mock_stock_move.action_explode() + for mock_move in mock_processed_moves: + stock_move = StockMove.search([ + ('picking_id', '=', self.picking_id.id), + ('sale_line_id', '=', self.sale_line_id.id), + ('product_id', '=', mock_move.product_id.id)]) + if not stock_move: + continue + if len(stock_move) != 1: + raise ValidationError( + _('No matching detailed product %s for product kit %s') % + (mock_move.product_id.display_name, + self.product_id.display_name)) + stock_move.write({'quantity_done': mock_move.product_uom_qty}) + mock_processed_moves.sudo().unlink() diff --git a/stock_picking_product_kit_helper/readme/CONTRIBUTORS.rst b/stock_picking_product_kit_helper/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..033e67f43 --- /dev/null +++ b/stock_picking_product_kit_helper/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Kitti Upariphutthiphong diff --git a/stock_picking_product_kit_helper/readme/DESCRIPTION.rst b/stock_picking_product_kit_helper/readme/DESCRIPTION.rst new file mode 100644 index 000000000..2dfff1bdd --- /dev/null +++ b/stock_picking_product_kit_helper/readme/DESCRIPTION.rst @@ -0,0 +1,6 @@ +When sales order contain product kits (product with BOM of type kit), +the delivery order (stock.move) created by it will be exploded to multiple product lines. + +Normally, to partially deliver, user will calculate manually the quantity of each product lines to delivery. + +This module add new tab "Product Kit Help" to help in calculate quantity in product line with ease. diff --git a/stock_picking_product_kit_helper/readme/USAGE.rst b/stock_picking_product_kit_helper/readme/USAGE.rst new file mode 100644 index 000000000..843e025c6 --- /dev/null +++ b/stock_picking_product_kit_helper/readme/USAGE.rst @@ -0,0 +1,9 @@ +When origin sales order of the underlining delivery order contains at least 1 product kit, +the tab "Product Kit Helper" will appear. + +To use the helper, go to the "Product Kit Helper" tab, + +#. Click => Show Product Kit +#. Edit and fill in required quantity +#. Click Assign Operation Quantity +#. Check result in Operations tab diff --git a/stock_picking_product_kit_helper/security/ir.model.access.csv b/stock_picking_product_kit_helper/security/ir.model.access.csv new file mode 100644 index 000000000..3fb613bd4 --- /dev/null +++ b/stock_picking_product_kit_helper/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_stock_picking_product_kit_helper_user,stock.picking.product.kit.helper user,model_stock_picking_product_kit_helper,stock.group_stock_user,1,1,1,1 diff --git a/stock_picking_product_kit_helper/static/description/icon.png b/stock_picking_product_kit_helper/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/stock_picking_product_kit_helper/static/description/icon.png differ diff --git a/stock_picking_product_kit_helper/static/description/icon.svg b/stock_picking_product_kit_helper/static/description/icon.svg new file mode 100644 index 000000000..a7a26d093 --- /dev/null +++ b/stock_picking_product_kit_helper/static/description/icon.svg @@ -0,0 +1,79 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/stock_picking_product_kit_helper/tests/__init__.py b/stock_picking_product_kit_helper/tests/__init__.py new file mode 100644 index 000000000..aca871d3c --- /dev/null +++ b/stock_picking_product_kit_helper/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2019 Kitti U. - Ecosoft +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_stock_picking_product_kit_helper diff --git a/stock_picking_product_kit_helper/tests/test_stock_picking_product_kit_helper.py b/stock_picking_product_kit_helper/tests/test_stock_picking_product_kit_helper.py new file mode 100644 index 000000000..cfa50a48d --- /dev/null +++ b/stock_picking_product_kit_helper/tests/test_stock_picking_product_kit_helper.py @@ -0,0 +1,64 @@ +# Copyright 2019 Kitti U. - Ecosoft +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests import common, Form +from odoo.exceptions import ValidationError + + +class TestStockPickingProductKitHelper(common.TransactionCase): + + def setUp(self): + super(TestStockPickingProductKitHelper, self).setUp() + self.partner = self.env.ref('base.res_partner_2') + self.table_kit = self.env.ref('mrp.product_product_table_kit') + + def test_00_sale_product_kit_helper(self): + """Test sale order with product kit, I expect, + - Product is exploded on picking + - Use helper, will assign quantity to stock.move correctly + - After picking is done, do not allow to use helper + """ + # Create sales order of 10 table kit + order_form = Form(self.env['sale.order']) + order_form.partner_id = self.partner + with order_form.order_line.new() as line: + line.product_id = self.table_kit + line.product_uom_qty = 10 + order = order_form.save() + order.action_confirm() + # In the picking, product line is exploded. + picking = order.mapped('picking_ids') + self.assertEqual(len(picking), 1) + stock_moves = picking.move_lines + # 1 SO line exploded to 2 moves + moves = [{'product': x.product_id.name, 'qty': x.product_uom_qty} + for x in stock_moves] + self.assertEqual(moves, + [{'product': 'Wood Panel', 'qty': 10.0}, + {'product': 'Bolt', 'qty': 40.0}]) + self.assertTrue(picking.has_product_kit) + self.assertFalse(picking.product_kit_helper_ids) # Not show yet + picking.show_product_kit() + self.assertEqual(len(picking.product_kit_helper_ids), 1) + # Assign product set 4 qty and test that it apply to stock.move + picking.product_kit_helper_ids[0].write({'product_uom_qty': 4.0}) + picking.action_product_kit_helper() + moves = [{'product': x.product_id.name, 'qty': x.quantity_done} + for x in stock_moves] + self.assertEqual(moves, + [{'product': 'Wood Panel', 'qty': 4.0}, + {'product': 'Bolt', 'qty': 16.0}]) + # Assign again to 10 qty + picking.product_kit_helper_ids[0].write({'product_uom_qty': 10.0}) + picking.action_product_kit_helper() + moves = [{'product': x.product_id.name, 'qty': x.quantity_done} + for x in stock_moves] + self.assertEqual(moves, + [{'product': 'Wood Panel', 'qty': 10.0}, + {'product': 'Bolt', 'qty': 40.0}]) + # Validate Picking + picking.button_validate() + self.assertEqual(picking.state, 'done') + # After done state, block the helper + with self.assertRaises(ValidationError): + picking.action_product_kit_helper() diff --git a/stock_picking_product_kit_helper/views/stock_view.xml b/stock_picking_product_kit_helper/views/stock_view.xml new file mode 100644 index 000000000..525bdab06 --- /dev/null +++ b/stock_picking_product_kit_helper/views/stock_view.xml @@ -0,0 +1,41 @@ + + + + + + + stock.picking.form + stock.picking + + + + +

+ To deliver partial product kits, you can use this tab to help calculate + quantity and auto fill in "Done" column in Operations tab.
+

    +
  1. Click => Show Product Kit
  2. +
  3. Edit and fill in required quantity
  4. +
  5. Click Assign Operation Quantity
  6. +
  7. Check result in Operations tab
  8. +
+

+ +