22# Copyright 2020 Tecnativa - Pedro M. Baeza
33# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
44
5- from odoo import _ , api , exceptions , fields , models
5+ from odoo import api , exceptions , fields , models
66from odoo .tools .misc import formatLang
77
88
@@ -71,18 +71,20 @@ def _check_global_discounts_sanity(self):
7171 return True
7272 taxes_keys = {}
7373 for line in self .order_line .filtered (
74- lambda _line : not _line .display_type and _line .product_id
74+ lambda _line : not _line .display_type
75+ and _line .product_id
76+ and not _line .product_id .bypass_global_discount
7577 ):
7678 if not line .tax_id :
7779 raise exceptions .UserError (
78- _ ("With global discounts, taxes in lines are required." )
80+ self . env . _ ("With global discounts, taxes in lines are required." )
7981 )
8082 for key in taxes_keys :
8183 if key == line .tax_id :
8284 break
8385 elif key & line .tax_id :
8486 raise exceptions .UserError (
85- _ ("Incompatible taxes found for global discounts." )
87+ self . env . _ ("Incompatible taxes found for global discounts." )
8688 )
8789 else :
8890 taxes_keys [line .tax_id ] = True
@@ -143,11 +145,12 @@ def _compute_tax_totals(self):
143145 for order in self :
144146 amount_discount_by_group = {}
145147 cumulative_discount_rate = 1.0
148+ currency = order .currency_id
146149 # Calculate cumulative discount rate
147150 for gbl_disc in order .global_discount_ids :
148151 discount_rate = gbl_disc .discount / 100
149152 cumulative_discount_rate *= 1 - discount_rate
150- amount_untaxed = 0.0
153+ base_amount = 0.0
151154 # Calculate the total discount amount and discount by tax group
152155 for line in order .order_line :
153156 if line .display_type or not line .product_id :
@@ -159,69 +162,74 @@ def _compute_tax_totals(self):
159162 )
160163 else :
161164 discounted_price_subtotal = line .price_subtotal
162- amount_untaxed += discounted_price_subtotal
163- # Calculate tax amounts for each tax group based on the
164- # discounted subtotal
165+ base_amount += discounted_price_subtotal
166+ # Calculate tax amounts for each tax group based on the discounted
167+ # subtotal
165168 for tax in line .tax_id :
166169 tax_group_id = tax .tax_group_id .id
167170 if tax_group_id not in amount_discount_by_group :
168171 amount_discount_by_group [tax_group_id ] = 0.0
169- # Calculate correct base amount for tax computation
170- base_amount = discounted_price_subtotal
171172 # Compute taxes on the correct base amount
172173 discounted_tax_vals = tax .compute_all (
173- base_amount ,
174- order . currency_id ,
174+ discounted_price_subtotal ,
175+ currency ,
175176 1.0 ,
176177 product = line .product_id ,
177178 partner = order .partner_shipping_id ,
178179 )
179- total_discounted_tax = sum (
180+ amount_discount_by_group [ tax_group_id ] + = sum (
180181 t .get ("amount" , 0.0 )
181182 for t in discounted_tax_vals .get ("taxes" , [])
182183 )
183- amount_discount_by_group [tax_group_id ] += total_discounted_tax
184184 # Calculate the final amount total
185- amount_total = amount_untaxed + sum (amount_discount_by_group .values ())
186- order .tax_totals ["amount_untaxed" ] = amount_untaxed
187- order .tax_totals ["amount_total" ] = amount_total
188- order .tax_totals ["formatted_amount_untaxed" ] = formatLang (
189- self .env , amount_untaxed , currency_obj = order .currency_id
185+ total_amount = base_amount + sum (amount_discount_by_group .values ())
186+ base_amount_currency = formatLang (
187+ self .env , base_amount , currency_obj = currency
190188 )
191- order .tax_totals ["formatted_amount_total" ] = formatLang (
192- self .env , amount_total , currency_obj = order .currency_id
189+ order .tax_totals .update (
190+ {
191+ "base_amount" : base_amount ,
192+ "total_amount" : total_amount ,
193+ "base_amount_currency" : base_amount ,
194+ "total_amount_currency" : total_amount ,
195+ }
193196 )
194- # Update groups by subtotal
195- for group in order .tax_totals ["groups_by_subtotal" ].values ():
196- for tax_group in group :
197- tax_group_id = tax_group ["tax_group_id" ]
198- discount_for_group = amount_discount_by_group .get (tax_group_id , 0.0 )
199- tax_group ["tax_group_amount" ] = discount_for_group
200- tax_group ["formatted_tax_group_amount" ] = formatLang (
201- self .env ,
202- tax_group ["tax_group_amount" ],
203- currency_obj = order .currency_id ,
204- )
205- # Update subtotals
206- for subtotal in order .tax_totals ["subtotals" ]:
207- subtotal ["amount" ] = amount_untaxed
208- subtotal ["formatted_amount" ] = formatLang (
209- self .env , amount_untaxed , currency_obj = order .currency_id
197+ # Update subtotals and groups by subtotal
198+ for group in order .tax_totals ["subtotals" ]:
199+ group .update (
200+ {
201+ "base_amount" : base_amount ,
202+ "base_amount_currency" : base_amount ,
203+ "amount" : base_amount ,
204+ "formatted_amount" : base_amount_currency ,
205+ }
210206 )
207+ for tax_group in group ["tax_groups" ]:
208+ discounted_tax_amount = amount_discount_by_group .get (
209+ tax_group ["id" ], 0.0
210+ )
211+ tax_group .update (
212+ {
213+ "base_amount" : base_amount ,
214+ "base_amount_currency" : base_amount ,
215+ "tax_amount" : discounted_tax_amount ,
216+ "tax_amount_currency" : discounted_tax_amount ,
217+ }
218+ )
211219 return res
212220
213221 @api .depends ("partner_id" , "company_id" )
214222 def _compute_global_discount_ids (self ):
215223 for order in self :
216- commercial = order .partner_id .commercial_partner_id
217- commercial_global_disc = commercial .customer_global_discount_ids
218- partner_global_disc = order .partner_id .customer_global_discount_ids
224+ commercial_global_disc = (
225+ order .partner_id .commercial_partner_id .customer_global_discount_ids
226+ )
227+ _discounts = (
228+ commercial_global_disc
229+ if commercial_global_disc
230+ else order .partner_id .customer_global_discount_ids
231+ )
219232 discounts = self .env ["global.discount" ]
220- _discounts = self .env ["global.discount" ]
221- if partner_global_disc :
222- _discounts = partner_global_disc
223- else :
224- _discounts = commercial_global_disc
225233 for discount in _discounts :
226234 if discount .company_id == order .company_id :
227235 discounts |= discount
0 commit comments