Debian Codegraph @

addons/l10n_cl/models/account_move_line.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3 from odoo import models
4 from odoo.tools.float_utils import float_repr
5
6
7 class AccountMoveLine(models.Model):
8 _inherit = 'account.move.line'
9
10 def _l10n_cl_prices_and_taxes(self):
11 """ this method is preserved here to allow compatibility with old templates,
12 Nevertheless it will be deprecated in future versions, since it had been replaced by
13 the method _l10n_cl_get_line_amounts, which is the same method used to calculate
14 the values for the XML (DTE) file
15 """
16 self.ensure_one()
17 invoice = self.move_id
18 included_taxes = self.tax_ids.filtered(lambda x: x.l10n_cl_sii_code == 14) if self.move_id._l10n_cl_include_sii() else self.tax_ids
19 if not included_taxes:
20 price_unit = self.tax_ids.compute_all(
21 self.price_unit,
22 currency=invoice.currency_id,
23 product=self.product_id,
24 partner=invoice.partner_id,
25 rounding_method='round_globally',
26 )
27 price_unit = price_unit['total_excluded']
28 price_subtotal = self.price_subtotal
29 else:
30 price_unit = included_taxes.compute_all(
31 self.price_unit, invoice.currency_id, 1.0, self.product_id, invoice.partner_id)['total_included']
32 price = self.price_unit * (1 - (self.discount or 0.0) / 100.0)
33 price_subtotal = included_taxes.compute_all(
34 price, invoice.currency_id, self.quantity, self.product_id, invoice.partner_id)['total_included']
35 price_net = price_unit * (1 - (self.discount or 0.0) / 100.0)
36 return {
37 'price_unit': price_unit,
38 'price_subtotal': price_subtotal,
39 'price_net': price_net
40 }
41
42 def _l10n_cl_get_line_amounts(self):
43 """
44 This method is used to calculate the amount and taxes of the lines required in the Chilean localization
45 electronic documents.
46 """
47 # If in this fix we should check for boletas, we have the following cases, and how this affects the xml
48 # for facturas and boletas:
49
50 # 1. local invoice in same currency tax not included in price
51 # 2. local invoice in same currency tax included in price (there is difference of -1 peso in amount_untaxed
52 # and +1 peso in vat tax amount. The lines are OK
53 # 3. local invoice in different currency tax not included in price
54 # 4. local invoice in different currency tax include in price -> this is the most problematic case because
55 # 5. foreign invoice in different currency (without tax)
56 if self.display_type != 'product':
57 return {
58 'price_subtotal': 0,
59 }
60 line_sign = self.price_subtotal / abs(self.price_subtotal) if self.price_subtotal else 0
61 domestic_invoice_other_currency = self.move_id.currency_id != self.move_id.company_id.currency_id and not \
62 self.move_id.l10n_latam_document_type_id._is_doc_type_export()
63 export = self.move_id.l10n_latam_document_type_id._is_doc_type_export()
64 if not export:
65 # This is to manage case 1, 2, 3 and 4
66 # cases 1 and 2: domestic invoice in same currency and cases 3 and 4 with other currency
67 main_currency = self.move_id.company_id.currency_id
68 main_currency_field = 'balance'
69 second_currency_field = 'price_subtotal'
70 second_currency = self.currency_id
71 main_currency_rate = 1
72 second_currency_rate = 1 / self.move_id.invoice_currency_rate if self.move_id.invoice_currency_rate else 1
73 inverse_rate = second_currency_rate if domestic_invoice_other_currency else main_currency_rate
74 else:
75 # This is to manage case 5 (export docs)
76 main_currency = self.currency_id
77 second_currency = self.move_id.company_id.currency_id
78 main_currency_field = 'price_subtotal'
79 second_currency_field = 'balance'
80 inverse_rate = 1 / self.move_id.invoice_currency_rate if self.move_id.invoice_currency_rate else 1
81 price_subtotal = abs(self[main_currency_field]) * line_sign
82 if self.quantity and self.discount != 100.0:
83 price_unit = (price_subtotal / abs(self.quantity)) / (1 - self.discount / 100)
84 if self.move_id.l10n_latam_document_type_id._is_doc_type_electronic_ticket():
85 price_item_document = (self.price_total / abs(self.quantity)) / (1 - self.discount / 100)
86 price_line_document = self.price_total
87 else:
88 price_item_document = price_unit
89 price_line_document = price_subtotal
90 else:
91 price_item_document = price_line_document = 0.0
92 price_unit = self.price_unit
93
94 if self.discount == 100:
95 price_before_discount = price_unit * self.quantity
96 else:
97 price_before_discount = price_subtotal / (1 - self.discount / 100)
98 discount_amount = price_before_discount * self.discount / 100
99 values = {
100 'decimal_places': main_currency.decimal_places,
101 'price_item': round(price_unit, 6),
102 'price_item_document': round(price_item_document, 2),
103 'price_line_document': price_line_document,
104 'total_discount': main_currency.round(discount_amount),
105 'price_subtotal': main_currency.round(price_subtotal),
106 'exempt': bool(not self.tax_ids),
107 'main_currency': main_currency,
108 }
109 if domestic_invoice_other_currency or export:
110 price_subtotal_second = abs(self[second_currency_field]) * line_sign
111 if self.quantity and self.discount != 100.0:
112 price_unit_second = (price_subtotal_second / abs(self.quantity)) / (1 - self.discount / 100)
113 else:
114 price_unit_second = self.price_unit
115 discount_amount_second = price_unit_second * self.quantity - price_subtotal_second
116 values['second_currency'] = {
117 'price': second_currency.round(price_unit_second),
118 'currency_name': self.move_id._format_length(second_currency.name, 3),
119 'conversion_rate': round(inverse_rate, 4),
120 'amount_discount': second_currency.round(discount_amount_second),
121 'total_amount': second_currency.round(price_subtotal_second),
122 'round_currency': second_currency.decimal_places,
123 }
124
125 values['line_description'] = '%s (%s: %s @ %s)' % (
126 self.name,
127 values['second_currency']['currency_name'],
128 float_repr(values['second_currency']['price'], values['second_currency']['round_currency']),
129 self.move_id._float_repr_float_round(values['second_currency']['conversion_rate'], values['second_currency']['round_currency']),
130 ) if values.get('second_currency') and not self.l10n_latam_document_type_id._is_doc_type_export() else self.name
131 return values