Skip to content

Commit f345af4

Browse files
committed
[IMP] l10n_it_riba_oca: improve sbf riba flow and accounting entries
1 parent 71424a5 commit f345af4

19 files changed

Lines changed: 599 additions & 521 deletions

l10n_it_riba_oca/README.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ diverso.
5858
fine', è necessario specificare almeno il registro e il conto da
5959
utilizzare al momento dell'accettazione della distinta da parte della
6060
banca. Tale conto deve essere di tipo 'Crediti' (ad esempio "RiBa
61-
all'incasso", eventualmente da creare). Selezionando 'Salvo buon fine' è
62-
necessario impostare il tipo di incasso, immediato o a maturazione
63-
valuta: questo influisce sulla gestione degli insoluti perchè solo nel
64-
caso di incasso immediato vengono stornate le registrazioni di
65-
presentazione della RiBa.
61+
all'incasso", eventualmente da creare).
6662

6763
La configurazione relativa alla fase di accredito, verrà usata nel
6864
momento in cui la banca accredita l'importo della distinta. È possibile

l10n_it_riba_oca/models/account.py

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,6 @@ def unlink(self):
7171
class AccountMove(models.Model):
7272
_inherit = "account.move"
7373

74-
@api.depends(
75-
"past_due_move_line_ids.past_due_invoice_ids",
76-
"past_due_move_line_ids.full_reconcile_id",
77-
"past_due_move_line_ids.matched_debit_ids",
78-
"past_due_move_line_ids.matched_credit_ids",
79-
)
80-
def _compute_is_past_due(self):
81-
for invoice in self:
82-
invoice.is_past_due = False
83-
reconciled_past_due = 0
84-
for past_due_move_line in invoice.past_due_move_line_ids:
85-
if past_due_move_line.reconciled:
86-
reconciled_past_due += 1
87-
if len(invoice.past_due_move_line_ids) != reconciled_past_due:
88-
invoice.is_past_due = True
89-
9074
def _compute_open_amount(self):
9175
today = fields.Date.today()
9276
for invoice in self:
@@ -123,9 +107,6 @@ def _compute_open_amount(self):
123107
"Past Due Journal Items",
124108
)
125109

126-
is_past_due = fields.Boolean(
127-
"Is a past due invoice", compute="_compute_is_past_due", store=True
128-
)
129110
is_riba_payment = fields.Boolean(
130111
"Is RiBa Payment", related="invoice_payment_term_id.riba"
131112
)
@@ -303,7 +284,35 @@ def action_post(self):
303284
invoice._sync_dynamic_lines(
304285
container={"records": invoice, "self": invoice}
305286
)
306-
return super().action_post()
287+
res = super().action_post()
288+
289+
# Automatic reconciliation for RiBa credit moves
290+
# When a credit move is posted and there are related RiBa slips,
291+
# we need to reconcile the acceptance and credit move lines
292+
if self.riba_credited_ids:
293+
# Get the acceptance account from the RiBa configuration
294+
# Note: All RiBa slips should have the same configuration
295+
acceptance_account_id = self.riba_credited_ids[
296+
0
297+
].config_id.acceptance_account_id
298+
299+
# Find credit move lines with the acceptance account
300+
credit_lines = self.line_ids.filtered(
301+
lambda line: line.account_id == acceptance_account_id
302+
)
303+
304+
# Find acceptance move lines with the same account
305+
acceptance_lines = (
306+
self.riba_credited_ids.acceptance_move_ids.line_ids.filtered(
307+
lambda line: line.account_id == acceptance_account_id
308+
)
309+
)
310+
311+
# Reconcile the matching lines to complete the RiBa credit flow
312+
if credit_lines and acceptance_lines:
313+
lines = credit_lines | acceptance_lines
314+
lines.reconcile()
315+
return res
307316

308317
def button_draft(self):
309318
# ---- Delete Collection Fees Line of invoice when set Back to Draft
@@ -381,6 +390,7 @@ def action_riba_payment_date(self):
381390
class AccountMoveLine(models.Model):
382391
_inherit = "account.move.line"
383392

393+
slip_line_id = fields.Many2one("riba.slip.line", "RiBa Line", readonly=True)
384394
slip_line_ids = fields.One2many(
385395
"riba.slip.move.line", "move_line_id", "RiBa Detail"
386396
)
@@ -433,24 +443,33 @@ def fields_view_get(
433443
)
434444
return result
435445

436-
def get_riba_lines(self):
437-
riba_lines = self.env["riba.slip.line"]
438-
return riba_lines.search([("acceptance_move_id", "=", self.move_id.id)])
439-
440446
def update_paid_riba_lines(self):
441-
# set paid only if not past_due
442-
if not self.env.context.get("past_due_reconciliation"):
443-
riba_lines = self.get_riba_lines()
444-
for riba_line in riba_lines:
445-
# allowed transitions:
446-
# credited_to_paid and accepted_to_paid. See workflow
447-
if riba_line.state in ["confirmed", "credited"]:
448-
if riba_line.test_reconciled():
449-
riba_line.state = "paid"
450-
riba_line.slip_id.state = "paid"
447+
"""
448+
Update RiBa line status to 'paid' when move lines are reconciled.
449+
450+
This method is called during reconciliation to mark RiBa lines as paid
451+
when the related account move lines are reconciled, but only if:
452+
- We're not in a past_due_reconciliation context
453+
- The RiBa line is in a state that allows transition to 'paid'
454+
"""
455+
sl = self.slip_line_id
456+
# Only update if not processing past due reconciliation and line exists
457+
if not self.env.context.get("past_due_reconciliation") and sl.state in [
458+
"confirmed", # Accepted by bank but not yet credited
459+
"credited", # Already credited by bank
460+
]:
461+
# Mark the RiBa line as paid
462+
sl.state = "paid"
451463

452464
def reconcile(self):
465+
"""
466+
Override reconcile to update RiBa line states.
467+
468+
When account move lines are reconciled, we need to check if any
469+
of them are related to RiBa lines and update their status accordingly.
470+
"""
453471
res = super().reconcile()
472+
# Update RiBa line states for each reconciled line
454473
for line in self:
455474
line.update_paid_riba_lines()
456475
return res
@@ -491,15 +510,23 @@ def get_riba_lines(self):
491510
return riba_lines
492511

493512
def unreconcile_riba_lines(self, riba_lines):
513+
"""
514+
Reset RiBa line states when reconciliation is undone.
515+
516+
When account move lines are unreconciled, we need to revert RiBa lines
517+
to their previous state to maintain consistency in the RiBa workflow.
518+
"""
494519
for riba_line in riba_lines:
495-
# allowed transitions:
496-
# paid_to_cancel and past_due_to_cancel. See workflow
520+
# Only process lines that are in final states (paid or past_due)
497521
if riba_line.state in ["paid", "past_due"]:
498-
if not riba_line.test_reconciled():
522+
# Only revert if there's no specific payment recorded
523+
if not riba_line.payment_id:
524+
# If the RiBa slip has a credit move, revert to "credited" state
499525
if riba_line.slip_id.credit_move_id:
500526
riba_line.state = "credited"
501527
riba_line.slip_id.state = "credited"
502528
else:
529+
# Otherwise, revert to "confirmed" state (acceptance phase)
503530
riba_line.state = "confirmed"
504531
riba_line.slip_id.state = "accepted"
505532

0 commit comments

Comments
 (0)