diff --git a/addons/purchase_requisition_stock/models/stock.py b/addons/purchase_requisition_stock/models/stock.py index 6d2a20d72ad..b1e8cb9fc76 100644 --- a/addons/purchase_requisition_stock/models/stock.py +++ b/addons/purchase_requisition_stock/models/stock.py @@ -60,7 +60,7 @@ class Orderpoint(models.Model): def _quantity_in_progress(self): res = super(Orderpoint, self)._quantity_in_progress() for op in self: - for pr in self.env['purchase.requisition'].search([('state','=','draft'),('origin','=',op.name)]): - for prline in pr.line_ids.filtered(lambda l: l.product_id.id == op.product_id.id): + for pr in self.env['purchase.requisition'].search([('state', '=', 'draft'), ('origin', '=', op.name)]): + for prline in pr.line_ids.filtered(lambda l: l.product_id.id == op.product_id.id and not l.move_dest_id): res[op.id] += prline.product_uom_id._compute_quantity(prline.product_qty, op.product_uom, round=False) return res diff --git a/addons/purchase_stock/models/purchase.py b/addons/purchase_stock/models/purchase.py index bd17d27850b..173a8352fe0 100644 --- a/addons/purchase_stock/models/purchase.py +++ b/addons/purchase_stock/models/purchase.py @@ -360,7 +360,7 @@ class PurchaseOrderLine(models.Model): 'date': self.order_id.date_order, 'date_expected': self.date_planned, 'location_id': self.order_id.partner_id.property_stock_supplier.id, - 'location_dest_id': self.orderpoint_id and self.orderpoint_id.location_id.id or self.order_id._get_destination_location(), + 'location_dest_id': (self.orderpoint_id and not self.move_dest_ids) and self.orderpoint_id.location_id.id or self.order_id._get_destination_location(), 'picking_id': picking.id, 'partner_id': self.order_id.dest_address_id.id, 'move_dest_ids': [(4, x) for x in self.move_dest_ids.ids], @@ -418,5 +418,9 @@ class PurchaseOrderLine(models.Model): args can be merged. If it returns an empty record then a new line will be created. """ - lines = self.filtered(lambda l: l.propagate_date == values['propagate_date'] and l.propagate_date_minimum_delta == values['propagate_date_minimum_delta'] and l.propagate_cancel == values['propagate_cancel'] and l.orderpoint_id == values['orderpoint_id']) + lines = self.filtered( + lambda l: l.propagate_date == values['propagate_date'] and + l.propagate_date_minimum_delta == values['propagate_date_minimum_delta'] and + l.propagate_cancel == values['propagate_cancel'] and + ((values['orderpoint_id'] and not values['move_dest_ids']) and l.orderpoint_id == values['orderpoint_id'] or True)) return lines and lines[0] or self.env['purchase.order.line'] diff --git a/addons/purchase_stock/models/stock.py b/addons/purchase_stock/models/stock.py index 3598a06645d..d1e2d946469 100644 --- a/addons/purchase_stock/models/stock.py +++ b/addons/purchase_stock/models/stock.py @@ -163,7 +163,7 @@ class Orderpoint(models.Model): def _quantity_in_progress(self): res = super(Orderpoint, self)._quantity_in_progress() - for poline in self.env['purchase.order.line'].search([('state','in',('draft','sent','to approve')),('orderpoint_id','in',self.ids)]): + for poline in self.env['purchase.order.line'].search([('state', 'in', ('draft', 'sent', 'to approve')), ('orderpoint_id', 'in', self.ids), ('move_dest_ids', '=', False)]): res[poline.orderpoint_id.id] += poline.product_uom._compute_quantity(poline.product_qty, poline.orderpoint_id.product_uom, round=False) return res diff --git a/addons/purchase_stock/models/stock_rule.py b/addons/purchase_stock/models/stock_rule.py index 174e930d52b..0a5a7b85c23 100644 --- a/addons/purchase_stock/models/stock_rule.py +++ b/addons/purchase_stock/models/stock_rule.py @@ -126,11 +126,20 @@ class StockRule(models.Model): @api.model def _get_procurements_to_merge_groupby(self, procurement): - return procurement.product_id, procurement.product_uom, procurement.values['propagate_date'], procurement.values['propagate_date_minimum_delta'], procurement.values['propagate_cancel'] + # Do not group procument from different orderpoint. 1. _quantity_in_progress + # directly depends from the orderpoint_id on the line. 2. The stock move + # generated from the order line has the orderpoint's location as + # destination location. In case of move_dest_ids those two points are not + # necessary anymore since those values are taken from destination moves. + return procurement.product_id, procurement.product_uom, procurement.values['propagate_date'],\ + procurement.values['propagate_date_minimum_delta'], procurement.values['propagate_cancel'],\ + (procurement.values.get('orderpoint_id') and not procurement.values.get('move_dest_ids')) and procurement.values['orderpoint_id'] @api.model def _get_procurements_to_merge_sorted(self, procurement): - return procurement.product_id.id, procurement.product_uom.id, procurement.values['propagate_date'], procurement.values['propagate_date_minimum_delta'], procurement.values['propagate_cancel'] + return procurement.product_id.id, procurement.product_uom.id, procurement.values['propagate_date'],\ + procurement.values['propagate_date_minimum_delta'], procurement.values['propagate_cancel'],\ + (procurement.values.get('orderpoint_id') and not procurement.values.get('move_dest_ids')) and procurement.values['orderpoint_id'] @api.model def _get_procurements_to_merge(self, procurements): diff --git a/addons/purchase_stock/tests/test_purchase_lead_time.py b/addons/purchase_stock/tests/test_purchase_lead_time.py index 07696540883..986b401a4a8 100644 --- a/addons/purchase_stock/tests/test_purchase_lead_time.py +++ b/addons/purchase_stock/tests/test_purchase_lead_time.py @@ -236,6 +236,6 @@ class TestPurchaseLeadTime(PurchaseTestCommon): po_line = self.env['purchase.order.line'].search([ ('product_id', '=', product_1.id), ]) - self.assertEqual(len(po_line), 2, 'the purchase order lines are not merged') + self.assertEqual(len(po_line), 2, 'the purchase order lines are merged') self.assertEqual(po_line[0].product_qty, 10, 'the purchase order line has a wrong quantity') self.assertEqual(po_line[1].product_qty, 5, 'the purchase order line has a wrong quantity') diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py index acc65cf7005..1c84c3ca3b3 100644 --- a/addons/stock/models/stock_move.py +++ b/addons/stock/models/stock_move.py @@ -174,6 +174,7 @@ class StockMove(models.Model): display_assign_serial = fields.Boolean(compute='_compute_display_assign_serial') next_serial = fields.Char('First SN') next_serial_count = fields.Integer('Number of SN') + orderpoint_id = fields.Many2one('stock.warehouse.orderpoint', 'Original Reordering Rule', check_company=True) @api.onchange('product_id', 'picking_type_id') def onchange_product(self): @@ -1055,6 +1056,7 @@ class StockMove(models.Model): 'route_ids': self.route_ids, 'warehouse_id': self.warehouse_id or self.picking_id.picking_type_id.warehouse_id or self.picking_type_id.warehouse_id, 'priority': self.priority, + 'orderpoint_id': self.orderpoint_id, } def _prepare_move_line_vals(self, quantity=None, reserved_quant=None): diff --git a/addons/stock/models/stock_rule.py b/addons/stock/models/stock_rule.py index 1ab9f7d1876..129266e640a 100644 --- a/addons/stock/models/stock_rule.py +++ b/addons/stock/models/stock_rule.py @@ -294,6 +294,7 @@ class StockRule(models.Model): 'description_picking': product_id._get_description(self.picking_type_id), 'priority': values.get('priority', "1"), 'delay_alert': self.delay_alert, + 'orderpoint_id': values.get('orderpoint_id') and values['orderpoint_id'].id, } for field in self._get_custom_move_fields(): if field in values: