From 2d3e6d7383aaa3c1589f220ab3a97c715d73e00a Mon Sep 17 00:00:00 2001 From: Pierre Masereel Date: Wed, 25 Oct 2017 17:37:28 +0200 Subject: [PATCH] [IMP] purchase: edit received quantity on purchase_line when type service When we purchase products of type services,the received quantity is always set to the ordered quantity. We want to be able to manually enter a received quantity on service products to have a better trace of what have been received or not. We show the picking stat button olnly if there are picking We show the columns qty_received, qty_invoiced and invoice_status depending on the parent state, and not depending on the context passed in the menu item. --- addons/purchase/models/purchase.py | 34 +++++++++------------- addons/purchase/models/stock.py | 12 ++++++++ addons/purchase/views/purchase_views.xml | 14 ++++----- addons/purchase_mrp/models/purchase_mrp.py | 6 ++-- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index 49528b244d8..bb9f8d2346a 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -550,25 +550,6 @@ class PurchaseOrderLine(models.Model): qty -= inv_line.uom_id._compute_quantity(inv_line.quantity, line.product_uom) line.qty_invoiced = qty - @api.depends('order_id.state', 'move_ids.state', 'move_ids.product_uom_qty') - def _compute_qty_received(self): - for line in self: - if line.order_id.state not in ['purchase', 'done']: - line.qty_received = 0.0 - continue - 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.location_dest_id.usage == "supplier": - if move.to_refund: - total -= move.product_uom._compute_quantity(move.product_uom_qty, line.product_uom) - else: - total += move.product_uom._compute_quantity(move.product_uom_qty, line.product_uom) - line.qty_received = total - @api.model def create(self, values): line = super(PurchaseOrderLine, self).create(values) @@ -606,6 +587,7 @@ class PurchaseOrderLine(models.Model): product_image = fields.Binary( 'Product Image', related="product_id.image", help="Non-stored related field to allow portal user to see the image of the product he has ordered") + product_type = fields.Selection(related='product_id.type') move_ids = fields.One2many('stock.move', 'purchase_line_id', string='Reservation', readonly=True, ondelete='set null', copy=False) price_unit = fields.Float(string='Unit Price', required=True, digits=dp.get_precision('Product Price')) @@ -623,7 +605,7 @@ class PurchaseOrderLine(models.Model): # Replace by invoiced Qty qty_invoiced = fields.Float(compute='_compute_qty_invoiced', string="Billed Qty", digits=dp.get_precision('Product Unit of Measure'), store=True) - qty_received = fields.Float(compute='_compute_qty_received', string="Received Qty", digits=dp.get_precision('Product Unit of Measure'), store=True) + qty_received = fields.Float(string="Received Qty", digits=dp.get_precision('Product Unit of Measure')) partner_id = fields.Many2one('res.partner', related='order_id.partner_id', string='Partner', readonly=True, store=True) currency_id = fields.Many2one(related='order_id.currency_id', store=True, string='Currency', readonly=True) @@ -632,6 +614,18 @@ class PurchaseOrderLine(models.Model): orderpoint_id = fields.Many2one('stock.warehouse.orderpoint', 'Orderpoint') move_dest_ids = fields.One2many('stock.move', 'created_purchase_line_id', 'Downstream Moves') + def _update_received_qty(self): + for line in self: + total = 0.0 + for move in line.move_ids: + if move.state == 'done': + if move.location_dest_id.usage == "supplier": + if move.to_refund: + total -= move.product_uom._compute_quantity(move.product_uom_qty, line.product_uom) + else: + total += move.product_uom._compute_quantity(move.product_uom_qty, line.product_uom) + line.qty_received = total + @api.multi def _create_or_update_picking(self): for line in self: diff --git a/addons/purchase/models/stock.py b/addons/purchase/models/stock.py index 4edd90aafbb..70f40aea873 100644 --- a/addons/purchase/models/stock.py +++ b/addons/purchase/models/stock.py @@ -71,6 +71,18 @@ class StockMove(models.Model): vals['purchase_line_id'] = self.purchase_line_id.id return vals + def _action_done(self): + res = super(StockMove, self)._action_done() + self.mapped('purchase_line_id').sudo()._update_received_qty() + return res + + def write(self, vals): + res = super(StockMove, self).write(vals) + if 'product_uom_qty' in vals: + self.filtered(lambda m: m.state == 'done' and m.purchase_line_id).mapped( + 'purchase_line_id').sudo()._update_received_qty() + return res + class StockWarehouse(models.Model): _inherit = 'stock.warehouse' diff --git a/addons/purchase/views/purchase_views.xml b/addons/purchase/views/purchase_views.xml index eb4e49232ea..0894438082f 100644 --- a/addons/purchase/views/purchase_views.xml +++ b/addons/purchase/views/purchase_views.xml @@ -172,7 +172,7 @@ @@ -209,6 +209,7 @@ + @@ -217,8 +218,8 @@ - - + + @@ -398,7 +399,7 @@ - + @@ -415,8 +416,6 @@ Requests for Quotation ir.actions.act_window purchase.order - {'search_default_todo':1, 'show_purchase': False} - [('state','in',('draft','sent','bid','cancel', 'confirmed'))] tree,kanban,form,pivot,graph,calendar @@ -441,8 +440,7 @@ ir.actions.act_window purchase.order tree,kanban,form,pivot,graph,calendar - {'search_default_todo':1, 'show_purchase': True} - [('state','not in',('draft','sent','bid', 'confirmed'))] + [('state','in',('purchase', 'done'))]

diff --git a/addons/purchase_mrp/models/purchase_mrp.py b/addons/purchase_mrp/models/purchase_mrp.py index 25bfef78e85..0ad3c148760 100644 --- a/addons/purchase_mrp/models/purchase_mrp.py +++ b/addons/purchase_mrp/models/purchase_mrp.py @@ -8,10 +8,8 @@ from odoo.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) - - def _compute_qty_received(self): - super(PurchaseOrderLine, self)._compute_qty_received() + def _update_received_qty(self): + super(PurchaseOrderLine, self)._update_received_qty() 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':