From 0923d1609ded4121f09a4a7ff9a9bba41dc2afb9 Mon Sep 17 00:00:00 2001 From: Goffin Simon Date: Wed, 3 Feb 2016 14:22:35 +0100 Subject: [PATCH] [FIX] purchase: _compute_qty_received Copied from _get_delivered_qty in addons/sale_mrp/sale_mrp.py When a product with a BOM is delivered, the delivered qty must be the produt qty set in the PO line. PS: There is no dependencies between mrp and purchase opw:668572 --- addons/purchase/purchase.py | 49 +++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/addons/purchase/purchase.py b/addons/purchase/purchase.py index 57e013e517e..ca2c98b6b79 100644 --- a/addons/purchase/purchase.py +++ b/addons/purchase/purchase.py @@ -447,7 +447,7 @@ class PurchaseOrderLine(models.Model): @api.depends('order_id.state', 'move_ids.state') def _compute_qty_received(self): - ProductUom = self.env['product.uom'] + productuom = self.env['product.uom'] for line in self: if line.order_id.state not in ['purchase', 'done']: line.qty_received = 0.0 @@ -455,15 +455,48 @@ class PurchaseOrderLine(models.Model): if line.product_id.type not in ['consu', 'product']: line.qty_received = line.product_qty continue - total = 0.0 - for move in line.move_ids: - if move.state == 'done': - if move.product_uom != line.product_uom: - total += ProductUom._compute_qty_obj(move.product_uom, move.product_uom_qty, line.product_uom) - else: - total += move.product_uom_qty + bom_delivered = self.sudo()._get_bom_delivered(line.sudo()) + if bom_delivered and any(bom_delivered.values()): + total = self.product_qty + elif bom_delivered: + total = 0.0 + else: + total = 0.0 + for move in line.move_ids: + if move.state == 'done': + if move.product_uom != line.product_uom: + total += productuom._compute_qty_obj(move.product_uom, move.product_uom_qty, line.product_uom) + else: + total += move.product_uom_qty line.qty_received = total + def _get_bom_delivered(self, line): + precision = self.env['decimal.precision'].precision_get('Product Unit of Measure') + bom_delivered = {} + # There is no dependencies between purchase and mrp + if 'mrp.bom' in self.env: + # In the case of a kit, we need to check if all components are shipped. We use a all or + # nothing policy. A product can have several BoMs, we don't know which one was used when the + # delivery was created. + for bom in line.product_id.product_tmpl_id.bom_ids: + if bom.type != 'phantom': + continue + bom_delivered[bom.id] = False + bom_exploded = self.env['mrp.bom']._bom_explode(bom, line.product_id, line.product_qty)[0] + for bom_line in bom_exploded: + qty = 0.0 + for move in line.move_ids: + if move.state == 'done' and move.product_id.id == bom_line.get('product_id', False): + qty += self.env['product.uom']._compute_qty_obj(move.product_uom, move.product_uom_qty, self.product_uom) + if float_compare(qty, bom_line['product_qty'], precision_digits=precision) < 0: + bom_delivered[bom.id] = False + break + else: + bom_delivered[bom.id] = True + return bom_delivered + + + name = fields.Text(string='Description', required=True) product_qty = fields.Float(string='Quantity', digits=dp.get_precision('Product Unit of Measure'), required=True) date_planned = fields.Datetime(string='Scheduled Date', required=True, select=True)