diff --git a/ext/custom-addons/dp_sale_hide_discount/models/account.py b/ext/custom-addons/dp_sale_hide_discount/models/account.py index eaba963d..3999a302 100644 --- a/ext/custom-addons/dp_sale_hide_discount/models/account.py +++ b/ext/custom-addons/dp_sale_hide_discount/models/account.py @@ -19,19 +19,83 @@ # ############################################################################## from odoo import api, fields, models +from odoo.addons import decimal_precision as dp +from odoo.tools import float_round + + +class AcccountInvoice(models.Model): + _inherit = 'account.invoice' + + @api.model + def invoice_line_move_line_get(self): + res = [] + for line in self.invoice_line_ids: + if line.quantity == 0: + continue + tax_ids = [] + for tax in line.invoice_line_tax_ids: + tax_ids.append((4, tax.id, None)) + for child in tax.children_tax_ids: + if child.type_tax_use != 'none': + tax_ids.append((4, child.id, None)) + analytic_tag_ids = [(4, analytic_tag.id, None) for analytic_tag in line.analytic_tag_ids] + + move_line_dict = { + 'invl_id': line.id, + 'type': 'src', + 'name': line.name.split('\n')[0][:64], + 'price_unit': line.price_reduce, + 'quantity': line.quantity, + 'price': line.price_subtotal, + 'account_id': line.account_id.id, + 'product_id': line.product_id.id, + 'uom_id': line.uom_id.id, + 'account_analytic_id': line.account_analytic_id.id, + 'tax_ids': tax_ids, + 'invoice_id': self.id, + 'analytic_tag_ids': analytic_tag_ids + } + if line['account_analytic_id']: + move_line_dict['analytic_line_ids'] = [(0, 0, line._get_analytic_line())] + res.append(move_line_dict) + return res + + @api.multi + def get_taxes_values(self): + tax_grouped = {} + for line in self.invoice_line_ids: + taxes = \ + line.invoice_line_tax_ids.compute_all(line.price_reduce, self.currency_id, line.quantity, + line.product_id, + self.partner_id)['taxes'] + for tax in taxes: + val = self._prepare_tax_line_vals(line, tax) + key = self.env['account.tax'].browse(tax['id']).get_grouping_key(val) + + if key not in tax_grouped: + tax_grouped[key] = val + else: + tax_grouped[key]['amount'] += val['amount'] + tax_grouped[key]['base'] += val['base'] + return tax_grouped class AccountInvoiceLine(models.Model): _inherit = 'account.invoice.line' - unit_price_incl_discount = fields.Float(string='Preis/ME inkl. Rabatt', compute='_compute_unit_price_incl_discount') + price_reduce = fields.Float(string='REP', digits=dp.get_precision('Product Price'), default=0.0, + compute='_compute_price_reduce', required=True, store=True, + help='Rabattierter Einzelpreis, inkludiert bereits den Rabatt und ist auf 2 Stellen ' + 'kaufmännisch gerundet.') hide_discount = fields.Boolean(string='Rabatt verstecken') @api.multi - def _compute_unit_price_incl_discount(self): + @api.depends('price_unit', 'discount') + def _compute_price_reduce(self): for line in self: - unit_price_incl_discount = line.price_unit * (1 - (line.discount or 0.0) / 100.0) - line.unit_price_incl_discount = round(unit_price_incl_discount, 2) + line.price_reduce = float_round(line.price_unit * (1.0 - line.discount / 100.0), + precision_digits=self.env['decimal.precision'].precision_get( + 'Product Price')) @api.multi @api.depends('price_unit', 'discount', 'invoice_line_tax_ids', 'quantity', @@ -39,7 +103,18 @@ class AccountInvoiceLine(models.Model): 'invoice_id.date_invoice') def _compute_price(self): for line in self: - if line.hide_discount: - line.price_unit = line.unit_price_incl_discount - line.discount = 0 - return super(AccountInvoiceLine, self)._compute_price() + currency = line.invoice_id and line.invoice_id.currency_id or None + taxes = False + if line.invoice_line_tax_ids: + taxes = line.invoice_line_tax_ids.compute_all(line.price_reduce, currency, line.quantity, + product=line.product_id, + partner=line.invoice_id.partner_id) + line.price_subtotal = price_subtotal_signed = taxes[ + 'total_excluded'] if taxes else line.quantity * line.price_reduce + line.price_total = taxes['total_included'] if taxes else line.price_subtotal + if line.invoice_id.currency_id and line.invoice_id.currency_id != line.invoice_id.company_id.currency_id: + price_subtotal_signed = line.invoice_id.currency_id.with_context( + date=line.invoice_id.date_invoice).compute(price_subtotal_signed, + line.invoice_id.company_id.currency_id) + sign = line.invoice_id.type in ['in_refund', 'out_refund'] and -1 or 1 + line.price_subtotal_signed = price_subtotal_signed * sign diff --git a/ext/custom-addons/dp_sale_hide_discount/models/sale.py b/ext/custom-addons/dp_sale_hide_discount/models/sale.py index 858b92f5..b720e777 100644 --- a/ext/custom-addons/dp_sale_hide_discount/models/sale.py +++ b/ext/custom-addons/dp_sale_hide_discount/models/sale.py @@ -19,25 +19,52 @@ # ############################################################################## from odoo import api, fields, models, _ +from odoo.tools import float_round class SaleOrderLine(models.Model): _inherit = 'sale.order.line' - unit_price_incl_discount = fields.Float(string='Preis/ME inkl. Rabatt', compute='_compute_unit_price_incl_discount') hide_discount = fields.Boolean(string='Rabatt verstecken') @api.multi - def _compute_unit_price_incl_discount(self): - for line in self: - unit_price_incl_discount = line.price_unit * (1 - (line.discount or 0.0) / 100.0) - line.unit_price_incl_discount = round(unit_price_incl_discount, 2) - - @api.depends('product_uom_qty', 'discount', 'price_unit', 'tax_id', 'hide_discount') + @api.depends('product_uom_qty', 'discount', 'price_unit', 'tax_id') def _compute_amount(self): - super(SaleOrderLine, self)._compute_amount() for line in self: - if line.hide_discount: - line.update({ - 'price_unit': line.unit_price_incl_discount - }) + taxes = line.tax_id.compute_all(line.price_reduce, line.order_id.currency_id, line.product_uom_qty, + product=line.product_id, partner=line.order_id.partner_shipping_id) + line.update({ + 'price_tax': sum(t.get('amount', 0.0) for t in taxes.get('taxes', [])), + 'price_total': taxes['total_included'], + 'price_subtotal': taxes['total_excluded'], + }) + + @api.multi + def _get_tax_amount_by_group(self): + self.ensure_one() + res = {} + for line in self.order_line: + base_tax = 0 + for tax in line.tax_id: + group = tax.tax_group_id + res.setdefault(group, {'amount': 0.0, 'base': 0.0}) + # FORWARD-PORT UP TO SAAS-17 + taxes = tax.compute_all(line.price_reduce + base_tax, quantity=line.product_uom_qty, + product=line.product_id, partner=self.partner_shipping_id)['taxes'] + for t in taxes: + res[group]['amount'] += t['amount'] + res[group]['base'] += t['base'] + if tax.include_base_amount: + base_tax += tax.compute_all(line.price_reduce + base_tax, quantity=1, product=line.product_id, + partner=self.partner_shipping_id)['taxes'][0]['amount'] + res = sorted(res.items(), key=lambda l: l[0].sequence) + res = [(l[0].name, l[1]['amount'], l[1]['base'], len(res)) for l in res] + return res + + @api.multi + @api.depends('price_unit', 'discount') + def _get_price_reduce(self): + for line in self: + line.price_reduce = float_round(line.price_unit * (1.0 - line.discount / 100.0), + precision_digits=self.env['decimal.precision'].precision_get( + 'Product Price')) diff --git a/ext/custom-addons/dp_sale_hide_discount/views/account_views.xml b/ext/custom-addons/dp_sale_hide_discount/views/account_views.xml index b967d064..f109c446 100644 --- a/ext/custom-addons/dp_sale_hide_discount/views/account_views.xml +++ b/ext/custom-addons/dp_sale_hide_discount/views/account_views.xml @@ -7,6 +7,7 @@ + diff --git a/ext/custom-addons/dp_sale_hide_discount/views/sale_views.xml b/ext/custom-addons/dp_sale_hide_discount/views/sale_views.xml index f62dec0d..c42984d7 100644 --- a/ext/custom-addons/dp_sale_hide_discount/views/sale_views.xml +++ b/ext/custom-addons/dp_sale_hide_discount/views/sale_views.xml @@ -7,6 +7,7 @@ +