Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] account_bank_reconciliation_cron #3

Open
wants to merge 2 commits into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions account_bank_reconciliation_cron/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
13 changes: 13 additions & 0 deletions account_bank_reconciliation_cron/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2017 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Account Bank Reconciliation Cron",
"summary": "Cron for automatically reconcile open bank statement lines",
"version": "14.0.1.0.0",
"license": "AGPL-3",
"author": "ForgeFlow, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-reconcile",
"depends": ["account_reconciliation_widget"],
"data": ["data/ir_cron.xml"],
}
11 changes: 11 additions & 0 deletions account_bank_reconciliation_cron/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<odoo noupdate="0">
<record id="ir_cron_automatic_reconciliation" model="ir.cron">
<field name="name">Automatically reconciliation for bank statement lines</field>
<field name="model_id" ref="model_account_bank_statement"/>
<field name="state">code</field>
<field name="code">model._cron_bank_automatic_reconciliation()</field>
<field name="interval_type">days</field>
<field name="interval_number">1</field>
<field name="numbercall">-1</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions account_bank_reconciliation_cron/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from. import account_bank_statement
48 changes: 48 additions & 0 deletions account_bank_reconciliation_cron/models/account_bank_statement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from odoo import models
from odoo.exceptions import UserError


class AccountBankStatement(models.Model):
_inherit = "account.bank.statement"

def _cron_bank_automatic_reconciliation(self):
bs_ids = self.search([('state', '=', 'posted')])
for bs in bs_ids:
for stl in bs.line_ids:
if not stl.is_reconciled:
counterpart_aml_dicts = []
amls = self.env['account.move.line'].search([
('reconciled', '=', False),
('name', 'ilike', stl.payment_ref),
('parent_state', '=', 'posted')
])
amls = amls.filtered(
lambda l: l.account_id.internal_type in ['receivable', 'payable'] or l.account_id.id in [bs.journal_id.payment_debit_account_id.id, bs.journal_id.payment_credit_account_id.id]
)
if len(amls) == 0:
continue
elif len(amls) == 1:
counterpart_aml_dicts.append({
'move_line': amls,
'name': "{}".format(amls.name),
'credit': abs(stl.amount) if stl.amount > 0 else 0.0,
'debit': abs(stl.amount) if stl.amount < 0 else 0.0,
'ref': amls.name,
})
else:
if sum(abs(amls.amount_residual)) == abs(stl.amount):
for aml in amls:
counterpart_aml_dicts.append({
'move_line': aml,
'name': "{}".format(aml.name),
'credit': abs(aml.amount_residual) if amls.amount_residual > 0 else 0.0,
'debit': abs(aml.amount_residual) if amls.amount_residual < 0 else 0.0,
'ref': aml.name,
})
if len(counterpart_aml_dicts) > 0:
stl.process_reconciliation(counterpart_aml_dicts=counterpart_aml_dicts)
if bs.all_lines_reconciled:
try:
bs.button_validate()
except UserError:
pass