Skip to content

Commit

Permalink
[FIX] account_edi: Simplify payment EDI flow
Browse files Browse the repository at this point in the history
Since recent changes on account_edi, the Mexican payment flow is quite broken.
Since the payment flow has been made only for the Mexican localization,
this commit is simplifying a lot the flow to only fit the need of this localization
and resolves the introduced bugs.

X-original-commit: 454ab2a
  • Loading branch information
smetl committed Mar 3, 2024
1 parent c2feb99 commit 57b4eec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
5 changes: 1 addition & 4 deletions addons/account_edi/models/account_edi_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ def _postprocess_post_edi_results(documents, edi_result):
reconciled_lines = move.line_ids.filtered(lambda line: line.account_id.account_type in ('asset_receivable', 'liability_payable'))
reconciled_amls = reconciled_lines.mapped('matched_debit_ids.debit_move_id') \
| reconciled_lines.mapped('matched_credit_ids.credit_move_id')
reconciled_amls\
.filtered(lambda x: x.move_id.payment_id or x.move_id.statement_line_id)\
.move_id\
._update_payments_edi_documents()
reconciled_amls.move_id._update_payments_edi_documents()
else:
document.write({
'error': move_result.get('error', False),
Expand Down
38 changes: 10 additions & 28 deletions addons/account_edi/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,19 @@ def _prepare_edi_tax_details(self, filter_to_apply=None, filter_invl_to_apply=No
def _update_payments_edi_documents(self):
''' Update the edi documents linked to the current journal entries. These journal entries must be linked to an
account.payment of an account.bank.statement.line. This additional method is needed because the payment flow is
not the same as the invoice one. Indeed, the edi documents must be updated when the reconciliation with some
invoices is changing.
not the same as the invoice one. Indeed, the edi documents must be created when the payment is fully reconciled
with invoices.
'''
payments = self.filtered(lambda move: move.payment_id or move.statement_line_id)
edi_document_vals_list = []
to_remove = self.env['account.edi.document']
for payment in self:
for payment in payments:
edi_formats = payment._get_reconciled_invoices().journal_id.edi_format_ids | payment.edi_document_ids.edi_format_id
for edi_format in edi_formats:
# Only recreate document when cancelled before.
existing_edi_document = payment.edi_document_ids.filtered(lambda x: x.edi_format_id == edi_format)
if existing_edi_document.state == 'sent':
continue
move_applicability = edi_format._get_move_applicability(payment)

if move_applicability:
Expand Down Expand Up @@ -449,31 +453,9 @@ class AccountMoveLine(models.Model):
# Export Electronic Document
####################################################

def _reconcile_pre_hook(self):
def _reconcile_post_hook(self, data):
# EXTENDS 'account'
# In some countries, the payments must be sent to the government under some condition. One of them could be
# there is at least one reconciled invoice to the payment. Then, we need to update the state of the edi
# documents during the reconciliation.
results = super()._reconcile_pre_hook()
results = super()._reconcile_post_hook(data)
all_lines = self + self.matched_debit_ids.debit_move_id + self.matched_credit_ids.credit_move_id
results['edi_payments'] = all_lines.move_id\
.filtered(lambda move: move.payment_id or move.statement_line_id)
results['edi_invoices_per_payment_before'] = {
pay: pay._get_reconciled_invoices()
for pay in results['edi_payments']
}
all_lines.move_id._update_payments_edi_documents()
return results

def _reconcile_post_hook(self, data):
# EXTENDS 'account'
super()._reconcile_post_hook(data)
payments = data['edi_payments']

changed_payments = self.env['account.move']
for payment in payments:
amls = payment.line_ids.filtered(lambda x: x.account_id.account_type == 'asset_receivable')
if all(amls.mapped('reconciled')):
matched_invoices = payment._get_reconciled_invoices()
if all(inv.edi_state == 'sent' for inv in matched_invoices):
changed_payments |= payment
changed_payments._update_payments_edi_documents()

0 comments on commit 57b4eec

Please sign in to comment.