2018-01-16 06:58:15 +01:00
|
|
|
# -*- 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 06:58:15 +01:00
|
|
|
|
2018-01-16 11:34:37 +01:00
|
|
|
from flectra import fields, models
|
|
|
|
from flectra.tools import float_compare
|
2018-01-16 06:58:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PurchaseOrderLine(models.Model):
|
|
|
|
_inherit = 'purchase.order.line'
|
|
|
|
|
2018-07-23 10:46:49 +02:00
|
|
|
qty_received = fields.Float(compute='_compute_qty_received', string="Received Qty", store=True, compute_sudo=True)
|
2018-01-16 06:58:15 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|