From 41941487da8b50207ee6ca042526366a39a335b6 Mon Sep 17 00:00:00 2001 From: Ahmed Aly Date: Fri, 30 Mar 2018 17:53:51 +0200 Subject: [PATCH] =?UTF-8?q?Fall=205402:=20CR1=20-=20H=C3=A4ndlerrabatt=20+?= =?UTF-8?q?=20intrastat=5Fid=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ext/custom-addons/dp_custom/models/account.py | 29 +++++------ ext/custom-addons/dp_custom/models/sale.py | 50 +++++++++++++++++++ 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/ext/custom-addons/dp_custom/models/account.py b/ext/custom-addons/dp_custom/models/account.py index 305c26ce..25634ffe 100644 --- a/ext/custom-addons/dp_custom/models/account.py +++ b/ext/custom-addons/dp_custom/models/account.py @@ -25,15 +25,17 @@ from odoo.exceptions import UserError class AccountInvoiceLine(models.Model): _inherit = 'account.invoice.line' - intrastat_id = fields.Many2one(comodel_name='report.intrastat.code', string='Intrastat Code', - compute="_compute_intrastat_id", inverse='_inverse_intrastat_id') + intrastat_id = fields.Many2one(comodel_name='report.intrastat.code', string='Intrastat Code') lot_id = fields.Many2one(comodel_name='stock.production.lot', string='Lot') weight = fields.Float(string='Gewicht', compute='_compute_weight') @api.model def create(self, vals): - if vals.get('product_id', False) and not vals.get('intrastat_id', False): - vals.update(intrastat_id=self.env['product.template'].browse(vals['product_id']).intrastat_id.id) + if vals.get('intrastat_id', None) is None: + if vals.get('lot_id', False): + vals.update(intrastat_id=self.env['stock.production.lot'].browse(vals['lot_id']).intrastat_id.id) + elif vals.get('product_id', False): + vals.update(intrastat_id=self.env['product.template'].browse(vals['product_id']).intrastat_id.id) return super(AccountInvoiceLine, self).create(vals) @api.multi @@ -50,16 +52,15 @@ class AccountInvoiceLine(models.Model): for record in self: record.weight = record.lot_id.weight or record.product_id.weight - def _compute_intrastat_id(self): - for record in self: - record.intrastat_id = record.lot_id.intrastat_id.id or record.product_id.intrastat_id.id - - def _inverse_intrastat_id(self): - for record in self: - if record.lot_id: - record.lot_id.intrastat_id = record.intrastat_id.id - else: - raise UserError(_('Der Intrastrat Code kann nur gesetzt werden wenn ein Lot angegeben wurde.')) + def write(self, vals): + res = super(AccountInvoiceLine, self).write(vals) + if vals.get('intrastat_id', False) and vals.get('lot_id', False): + self.env['stock.production.lot'].browse([vals.get('lot_id', False)]).write({ + 'intrastat_id': vals.get('intrastat_id') + }) + elif vals.get('intrastat_id', False) and not vals.get('lot_id', False): + raise UserError(_('Der Intrastrat Code kann nur gesetzt werden wenn ein Lot angegeben wurde.')) + return res class AccountPaymentTerm(models.Model): diff --git a/ext/custom-addons/dp_custom/models/sale.py b/ext/custom-addons/dp_custom/models/sale.py index 52f1050e..2e26420f 100644 --- a/ext/custom-addons/dp_custom/models/sale.py +++ b/ext/custom-addons/dp_custom/models/sale.py @@ -397,3 +397,53 @@ class SaleOrderLine(models.Model): res['lot_id'] = self.lot_id.id return res + + @api.multi + def invoice_line_create(self, invoice_id, qty): + """ + Overwritten and added a logic to create an extra line for discounts from a retailer + :param invoice_id: + :param qty: + :return: + """ + invoice_lines = self.env['account.invoice.line'] + precision = self.env['decimal.precision'].precision_get('Product Unit of Measure') + for line in self: + if not float_is_zero(qty, precision_digits=precision): + vals = line._prepare_invoice_line(qty=qty) + vals.update({'invoice_id': invoice_id, 'sale_line_ids': [(6, 0, [line.id])]}) + invoice_lines |= self.env['account.invoice.line'].create(vals) + if line.order_id.partner_invoice_id.retailer: + discount = line.calc_discount() + discount = int(round(discount)) + if discount > 0: + vals.update({ + 'price_unit': -invoice_lines.price_subtotal * (discount / 100), + 'uom_id': self.env.ref('product.product_uom_unit').id, + 'name': 'Händlerrabatt {}%'.format(discount), + 'intrastat_id': False + }) + invoice_lines |= self.env['account.invoice.line'].create(vals) + return invoice_lines + + @api.multi + def calc_discount(self): + discount = 0.0 + context_partner = dict(self.env.context, partner_id=self.order_id.partner_id.id, date=self.order_id.date_order) + pricelist_context = dict(context_partner, uom=self.product_uom.id) + + price, rule_id = self.order_id.pricelist_id.with_context(pricelist_context).get_product_price_rule( + self.product_id, self.product_uom_qty or 1.0, self.order_id.partner_id) + new_list_price, currency_id = self.with_context(context_partner)._get_real_price_currency(self.product_id, + rule_id, + self.product_uom_qty, + self.product_uom, + self.order_id.pricelist_id.id) + + if new_list_price != 0: + if self.order_id.pricelist_id.currency_id.id != currency_id: + # we need new_list_price in the same currency as price, which is in the SO's pricelist's currency + new_list_price = self.env['res.currency'].browse(currency_id).with_context(context_partner).compute( + new_list_price, self.order_id.pricelist_id.currency_id) + discount = (new_list_price - price) / new_list_price * 100 + return discount