1- from odoo import _ , api , fields , models
1+ from odoo import api , fields , models
22from odoo .exceptions import UserError , ValidationError
33from odoo .tools .float_utils import float_compare , float_round
44
@@ -15,7 +15,9 @@ class SaleInvoicePlan(models.Model):
1515 readonly = True ,
1616 ondelete = "cascade" ,
1717 )
18- analytic_account_id = fields .Many2one (related = "sale_id.analytic_account_id" )
18+ analytic_account_id = fields .Many2many (
19+ related = "sale_id.order_line.distribution_analytic_account_ids"
20+ )
1921 partner_id = fields .Many2one (
2022 comodel_name = "res.partner" ,
2123 string = "Customer" ,
@@ -104,7 +106,7 @@ def _compute_amount(self):
104106 # For last line, amount is the left over
105107 if rec .last :
106108 installments = rec .sale_id .invoice_plan_ids .filtered (
107- lambda l : l .invoice_type == "installment"
109+ lambda plan : plan .invoice_type == "installment"
108110 )
109111 prev_amount = sum ((installments - rec ).mapped ("amount" ))
110112 rec .amount = amount_untaxed - prev_amount
@@ -117,7 +119,7 @@ def _inverse_amount(self):
117119 if rec .sale_id .amount_untaxed != 0 :
118120 if rec .last :
119121 installments = rec .sale_id .invoice_plan_ids .filtered (
120- lambda l : l .invoice_type == "installment"
122+ lambda invoice_plan : invoice_plan .invoice_type == "installment"
121123 )
122124 prev_percent = sum ((installments - rec ).mapped ("percent" ))
123125 rec .percent = 100 - prev_percent
@@ -142,23 +144,17 @@ def _compute_to_invoice(self):
142144 def _get_amount_invoice (self , invoices ):
143145 """Hook function"""
144146 amount_invoiced = sum (invoices .mapped ("amount_untaxed" ))
145- deposit_product_id = (
146- self .env ["ir.config_parameter" ]
147- .sudo ()
148- .get_param ("sale.default_deposit_product_id" )
147+ lines = invoices .mapped ("invoice_line_ids" ).filtered (
148+ lambda invoice_line : invoice_line .product_id .id
149149 )
150- if deposit_product_id :
151- lines = invoices .mapped ("invoice_line_ids" ).filtered (
152- lambda l : l .product_id .id != int (deposit_product_id )
153- )
154- amount_invoiced = sum (lines .mapped ("price_subtotal" ))
150+ amount_invoiced = sum (lines .mapped ("price_subtotal" ))
155151 return amount_invoiced
156152
157153 @api .depends ("invoice_move_ids.state" )
158154 def _compute_invoiced (self ):
159155 for rec in self :
160156 invoiced = rec .invoice_move_ids .filtered (
161- lambda l : l .state in ("draft" , "posted" )
157+ lambda move_line : move_line .state in ("draft" , "posted" )
162158 )
163159 rec .invoiced = True if invoiced else False
164160 rec .amount_invoiced = (
@@ -178,7 +174,9 @@ def _compute_new_invoice_quantity(self, invoice_move):
178174 return
179175 percent = self .percent
180176 move = invoice_move .with_context (check_move_validity = False )
181- for line in move .invoice_line_ids :
177+ for line in move .invoice_line_ids .filtered (
178+ lambda line : line .display_type not in ("line_section" , "line_note" )
179+ ):
182180 self ._update_new_quantity (line , percent )
183181 move .line_ids .filtered (
184182 lambda x : x .display_type
@@ -188,7 +186,7 @@ def _compute_new_invoice_quantity(self, invoice_move):
188186 def _update_new_quantity (self , line , percent ):
189187 """Hook function"""
190188 if not len (line .sale_line_ids ) >= 0 :
191- raise UserError (_ ("No matched order line for invoice line" ))
189+ raise UserError (self . env . _ ("No matched order line for invoice line" ))
192190 order_line = fields .first (line .sale_line_ids )
193191 if order_line .is_downpayment : # based on 1 unit
194192 line .write ({"quantity" : - percent / 100 })
@@ -197,9 +195,14 @@ def _update_new_quantity(self, line, percent):
197195 prec = order_line .product_uom .rounding
198196 if plan_qty :
199197 plan_qty = float_round (plan_qty , precision_rounding = prec )
200- if float_compare (abs (plan_qty ), abs (line .quantity ), prec ) == 1 :
198+ if (
199+ float_compare (
200+ abs (plan_qty ), abs (line .quantity ), precision_rounding = prec
201+ )
202+ == 1
203+ ):
201204 raise ValidationError (
202- _ (
205+ self . env . _ (
203206 "Plan quantity: %(plan_qty)s, exceed invoiceable quantity: "
204207 "%(invoiceable_qty)s"
205208 "\n Product should be delivered before invoice"
@@ -218,7 +221,7 @@ def unlink(self):
218221 if lines :
219222 installments = [str (x ) for x in lines .mapped ("installment" )]
220223 raise UserError (
221- _ (
224+ self . env . _ (
222225 "Installment %s: already used and not allowed to delete.\n "
223226 "Please discard changes."
224227 )
0 commit comments