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
@@ -109,7 +111,9 @@ def _compute_amounts(self):
109111 line .price_subtotal , discounts .copy ()
110112 )
111113 amount_discounted_untaxed += discounted_subtotal
112- discounted_tax = line .tax_id .compute_all (
114+ discounted_tax = line .tax_id .with_context (
115+ force_price_include = False
116+ ).compute_all (
113117 discounted_subtotal ,
114118 line .order_id .currency_id ,
115119 1.0 ,
@@ -143,11 +147,12 @@ def _compute_tax_totals(self):
143147 for order in self :
144148 amount_discount_by_group = {}
145149 cumulative_discount_rate = 1.0
150+ currency = order .currency_id
146151 # Calculate cumulative discount rate
147152 for gbl_disc in order .global_discount_ids :
148153 discount_rate = gbl_disc .discount / 100
149154 cumulative_discount_rate *= 1 - discount_rate
150- amount_untaxed = 0.0
155+ base_amount = 0.0
151156 # Calculate the total discount amount and discount by tax group
152157 for line in order .order_line :
153158 if line .display_type or not line .product_id :
@@ -159,69 +164,76 @@ def _compute_tax_totals(self):
159164 )
160165 else :
161166 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
167+ base_amount += discounted_price_subtotal
168+ # Calculate tax amounts for each tax group based on the discounted
169+ # subtotal
165170 for tax in line .tax_id :
166171 tax_group_id = tax .tax_group_id .id
167172 if tax_group_id not in amount_discount_by_group :
168173 amount_discount_by_group [tax_group_id ] = 0.0
169- # Calculate correct base amount for tax computation
170- base_amount = discounted_price_subtotal
171174 # Compute taxes on the correct base amount
172- discounted_tax_vals = tax .compute_all (
173- base_amount ,
174- order .currency_id ,
175+ discounted_tax_vals = tax .with_context (
176+ force_price_include = False
177+ ).compute_all (
178+ discounted_price_subtotal ,
179+ currency ,
175180 1.0 ,
176181 product = line .product_id ,
177182 partner = order .partner_shipping_id ,
178183 )
179- total_discounted_tax = sum (
184+ amount_discount_by_group [ tax_group_id ] + = sum (
180185 t .get ("amount" , 0.0 )
181186 for t in discounted_tax_vals .get ("taxes" , [])
182187 )
183- amount_discount_by_group [tax_group_id ] += total_discounted_tax
184188 # 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
189+ total_amount = base_amount + sum (amount_discount_by_group .values ())
190+ base_amount_currency = formatLang (
191+ self .env , base_amount , currency_obj = currency
190192 )
191- order .tax_totals ["formatted_amount_total" ] = formatLang (
192- self .env , amount_total , currency_obj = order .currency_id
193+ order .tax_totals .update (
194+ {
195+ "base_amount" : base_amount ,
196+ "total_amount" : total_amount ,
197+ "base_amount_currency" : base_amount ,
198+ "total_amount_currency" : total_amount ,
199+ }
193200 )
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
201+ # Update subtotals and groups by subtotal
202+ for group in order .tax_totals ["subtotals" ]:
203+ group .update (
204+ {
205+ "base_amount" : base_amount ,
206+ "base_amount_currency" : base_amount ,
207+ "amount" : base_amount ,
208+ "formatted_amount" : base_amount_currency ,
209+ }
210210 )
211+ for tax_group in group ["tax_groups" ]:
212+ discounted_tax_amount = amount_discount_by_group .get (
213+ tax_group ["id" ], 0.0
214+ )
215+ tax_group .update (
216+ {
217+ "base_amount" : base_amount ,
218+ "base_amount_currency" : base_amount ,
219+ "tax_amount" : discounted_tax_amount ,
220+ "tax_amount_currency" : discounted_tax_amount ,
221+ }
222+ )
211223 return res
212224
213225 @api .depends ("partner_id" , "company_id" )
214226 def _compute_global_discount_ids (self ):
215227 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
228+ commercial_global_disc = (
229+ order .partner_id .commercial_partner_id .customer_global_discount_ids
230+ )
231+ _discounts = (
232+ commercial_global_disc
233+ if commercial_global_disc
234+ else order .partner_id .customer_global_discount_ids
235+ )
219236 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
225237 for discount in _discounts :
226238 if discount .company_id == order .company_id :
227239 discounts |= discount
0 commit comments