From e38c98818bdb38d01141c48ac3efbbb0f7ba7c0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Sat, 22 Jan 2022 11:58:28 +0100 Subject: [PATCH] [IMP]pms: matching autoreconcile festures: amount and partner --- pms/models/account_bank_statement.py | 9 ++++++- pms/models/account_move.py | 37 ++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/pms/models/account_bank_statement.py b/pms/models/account_bank_statement.py index 46d16f457..fd1f758d2 100644 --- a/pms/models/account_bank_statement.py +++ b/pms/models/account_bank_statement.py @@ -48,9 +48,16 @@ class AccountBankStatement(models.Model): statement_move_line = line.move_id.line_ids.filtered( lambda line: line.account_id.reconcile ) - payment_line = self.env["account.move.line"].browse( + payment_lines = self.env["account.move.line"].browse( to_reconcile_ids.ids )[0] + # We try to reconcile by amount + for record in payment_lines: + payment_line = ( + record if abs(record.balance) == line.amount else False + ) + if not payment_line: + payment_line = payment_lines[0] if payment_line and statement_move_line: statement_move_line.account_id = payment_line.account_id lines_to_reconcile = payment_line + statement_move_line diff --git a/pms/models/account_move.py b/pms/models/account_move.py index b92f77a42..4cc1ef2bb 100644 --- a/pms/models/account_move.py +++ b/pms/models/account_move.py @@ -1,5 +1,6 @@ # Copyright 2017 Dario Lodeiros # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) +import itertools as it import json from odoo import _, api, fields, models @@ -186,15 +187,33 @@ class AccountMove(models.Model): lambda line: line.account_id.user_type_id.type in ("receivable", "payable") ) - to_reconcile = ( + to_propose = ( self.env["account.move"] .browse(list(current_amounts.keys())) .line_ids.filtered( lambda line: line.account_id == pay_term_lines.account_id and line.folio_ids in move.folio_ids + and ( + line.move_id.partner_id == move.partner_id + or not line.move_id.partner_id + ) ) ) - (pay_term_lines + to_reconcile).reconcile() + to_reconcile = self.match_pays_by_amount( + payments=to_propose, invoice=move + ) + if to_reconcile: + (pay_term_lines + to_reconcile).reconcile() + # Set partner in payment + for record in to_reconcile: + if record.payment_id and not record.payment_id.partner_id: + record.payment_id.partner_id = move.partner_id + if ( + record.statement_line_id + and not record.statement_line_id.partner_id + ): + record.statement_line_id.partner_id = move.partner_id + return True def _post(self, soft=True): """ @@ -203,3 +222,17 @@ class AccountMove(models.Model): res = super(AccountMove, self)._post(soft) self._autoreconcile_folio_payments() return res + + def match_pays_by_amount(self, payments, invoice): + """ + Match payments by amount + """ + for i in range(len(payments)): + combinations = list(it.combinations(payments, i + 1)) + for combi in combinations: + # TODO: compare with currency differences + if sum(abs(item.balance) for item in combi) == invoice.amount_residual: + return payments.filtered( + lambda p: p.id in [item.id for item in combi] + ) + return []