From d7ed45018f81550330f4b65bd451b2d61dcccf93 Mon Sep 17 00:00:00 2001 From: Goffin Simon Date: Tue, 8 Dec 2015 12:08:17 +0100 Subject: [PATCH] [FIX] sale: _get_sale_order_line When making several expenses for the same analytic account, it can be possible that these expenses have the same product but different price unit(e.g.:this is the case for plane ticket with a vairiable price according to the time). Then the function "_get_sale_order_line" must check for all the existing confirmed SO lines if there is one which matches with the analytic account, the product and the price before creating a new SO line. opw:658383 --- addons/sale/sale_analytic.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/addons/sale/sale_analytic.py b/addons/sale/sale_analytic.py index 398c99e11e5..d324495df39 100644 --- a/addons/sale/sale_analytic.py +++ b/addons/sale/sale_analytic.py @@ -75,26 +75,29 @@ class AccountAnalyticLine(models.Model): def _get_sale_order_line(self, vals=None): result = dict(vals or {}) - sol = result.get('so_line', False) or self.so_line - if not sol and self.account_id and self.product_id and self.product_id.invoice_policy in ('cost', 'order'): - sol = self.env['sale.order.line'].search([ + so_line = result.get('so_line', False) or self.so_line + if not so_line and self.account_id and self.product_id and self.product_id.invoice_policy in ('cost', 'order'): + so_lines = self.env['sale.order.line'].search([ ('order_id.project_id', '=', self.account_id.id), ('state', '=', 'sale'), - ('product_id', '=', self.product_id.id)], - limit=1) - # Use the existing SO line only if the unit prices are the same - if sol and sol.price_unit == self._get_invoice_price(sol.order_id): - result.update({'so_line': sol.id}) + ('product_id', '=', self.product_id.id)]) + # Use the existing SO line only if the unit prices are the same, otherwise we create + # a new line + for line in so_lines: + if line.price_unit == self._get_invoice_price(line.order_id): + result.update({'so_line': line.id}) + so_line = line + break else: # This will trigger the creation of a new SO line - sol = False + so_line = False - if not sol and self.account_id and self.product_id and self.product_id.invoice_policy == 'cost': + if not so_line and self.account_id and self.product_id and self.product_id.invoice_policy == 'cost': order_line_vals = self._get_sale_order_line_vals() if order_line_vals: - sol = self.env['sale.order.line'].create(order_line_vals) - sol._compute_tax_id() - result.update({'so_line': sol.id}) + so_line = self.env['sale.order.line'].create(order_line_vals) + so_line._compute_tax_id() + result.update({'so_line': so_line.id}) return result