flectra/addons/purchase_mrp/models/purchase_mrp.py

42 lines
1.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2018-01-16 11:34:37 +01:00
# Part of Odoo, Flectra. See LICENSE file for full copyright and licensing details.
2018-01-16 11:34:37 +01:00
from flectra import fields, models
from flectra.tools import float_compare
class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
qty_received = fields.Float(compute='_compute_qty_received', string="Received Qty", store=True, compute_sudo=True)
def _compute_qty_received(self):
super(PurchaseOrderLine, self)._compute_qty_received()
for line in self.filtered(lambda x: x.move_ids and x.product_id.id not in x.move_ids.mapped('product_id').ids):
bom = self.env['mrp.bom']._bom_find(product=line.product_id, company_id=line.company_id.id)
if bom and bom.type == 'phantom':
line.qty_received = line._get_bom_delivered(bom=bom)
def _get_bom_delivered(self, bom=False):
self.ensure_one()
# In the case of a kit, we need to check if all components are shipped. Since the BOM might
# have changed, we don't compute the quantities but verify the move state.
if bom:
bom_delivered = all([move.state == 'done' for move in self.move_ids])
if bom_delivered:
return self.product_qty
else:
return 0.0
class StockMove(models.Model):
_inherit = 'stock.move'
def _prepare_phantom_move_values(self, bom_line, quantity):
vals = super(StockMove, self)._prepare_phantom_move_values(bom_line, quantity)
if self.purchase_line_id:
vals['purchase_line_id'] = self.purchase_line_id.id
return vals