@@ -193,6 +193,27 @@ def month_check(self, invoice_date_due, all_date_due):
193193 return True
194194 return False
195195
196+ def maturity_check (self , invoice_date_due , all_date_due ):
197+ """
198+ Check if expenses should be applied based on exact maturity date.
199+ Used for 'one_a_maturity' policy.
200+
201+ :param invoice_date_due: due date of current invoice
202+ :param all_date_due: list of existing due dates for partner
203+ :return: True if invoice_date_due already exists in all_date_due
204+
205+ Example:
206+ - Invoice 1: Oct -> Dec (60 days) -> expenses YES
207+ - Invoice 2: Nov -> Dec (30 days) -> expenses NO (Dec already exists)
208+ - Invoice with 30/60 days: 2 different dates -> 2 expenses
209+ """
210+ self .ensure_one ()
211+ if self .partner_id .riba_policy_expenses == "one_a_maturity" :
212+ for d in all_date_due :
213+ if invoice_date_due == d :
214+ return True
215+ return False
216+
196217 def _post (self , soft = True ):
197218 inv_riba_no_bank = self .filtered (
198219 lambda x : x .is_riba_payment
@@ -236,18 +257,20 @@ def action_post(self):
236257 _ ("Set a Service for Collection Fees in Company Config." )
237258 )
238259 # ---- Apply Collection Fees on invoice only on first due date of the month
239- # ---- Get Date of first due date
260+ # ---- Get all due dates with collection fees already applied
240261 move_line = self .env ["account.move.line" ].search (
241262 [
242263 ("partner_id" , "=" , invoice .partner_id .id ),
243264 ("move_id.invoice_payment_term_id.riba" , "=" , True ),
244- ("date_maturity " , "> =" , fields . Date . context_today ( invoice ) ),
265+ ("move_id.state " , "=" , "posted" ),
245266 ]
246267 )
247- if not any (
248- line .due_cost_line for line in move_line .mapped ("move_id.line_ids" )
249- ):
250- move_line = self .env ["account.move.line" ]
268+ # ---- Keep only lines from invoices that already have collection fees
269+ move_line = move_line .filtered (
270+ lambda line : any (
271+ inv_line .due_cost_line for inv_line in line .move_id .invoice_line_ids
272+ )
273+ )
251274 # ---- Filtered recordset with date_maturity
252275 move_line = move_line .filtered (lambda line : line .date_maturity is not False )
253276 # ---- Sorted
@@ -265,7 +288,18 @@ def action_post(self):
265288 untaxed_amount_currency = invoice .amount_untaxed ,
266289 )
267290 for pay_date in pterm_list :
268- if not invoice .month_check (pay_date ["date" ], previous_date_due ):
291+ # Check if expenses should be applied based on policy
292+ should_skip_expense = False
293+ if invoice .partner_id .riba_policy_expenses == "one_a_maturity" :
294+ should_skip_expense = invoice .maturity_check (
295+ pay_date ["date" ], previous_date_due
296+ )
297+ else :
298+ should_skip_expense = invoice .month_check (
299+ pay_date ["date" ], previous_date_due
300+ )
301+
302+ if not should_skip_expense :
269303 # ---- Get Line values for service product
270304 service_prod = invoice .company_id .due_cost_service_id
271305 account = service_prod .product_tmpl_id .get_product_accounts (
0 commit comments