78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
import logging
|
|
|
|
AML = env['account.move.line'].sudo()
|
|
BSL = env['account.bank.statement.line'].sudo()
|
|
company_partner_id = env.company.partner_id.id
|
|
|
|
# No date limit this time — match oldest outstanding first
|
|
TRANSFER_PAIRS = [
|
|
(50, 51, 493), # Scotia Current -> Passport Visa
|
|
(53, 28, 493), # RBC Chequing -> RBC Visa
|
|
]
|
|
|
|
total_reconciled = 0
|
|
|
|
for source_jid, cc_jid, outstanding_acct_id in TRANSFER_PAIRS:
|
|
cc_lines = BSL.search([
|
|
('journal_id', '=', cc_jid),
|
|
('is_reconciled', '=', False),
|
|
('amount', '>', 0),
|
|
('company_id', '=', env.company.id),
|
|
])
|
|
# Filter to only transfer-like patterns (round amounts, payment from)
|
|
transfer_lines = cc_lines.filtered(
|
|
lambda l: (l.payment_ref or '').lower().startswith(('payment from', 'from'))
|
|
)
|
|
if not transfer_lines:
|
|
print(f'Journal {cc_jid}: no transfer lines found', flush=True)
|
|
continue
|
|
|
|
journal_name = transfer_lines[0].journal_id.name
|
|
print(f'Processing {journal_name}: {len(transfer_lines)} transfer lines (no date limit)', flush=True)
|
|
|
|
# Pre-load all outstanding entries for this account/partner, sorted by date
|
|
all_outstanding = AML.search([
|
|
('account_id', '=', outstanding_acct_id),
|
|
('partner_id', '=', company_partner_id),
|
|
('reconciled', '=', False),
|
|
('amount_residual', '>', 0),
|
|
], order='date asc')
|
|
|
|
# Index by amount for fast lookup
|
|
by_amount = {}
|
|
for o in all_outstanding:
|
|
by_amount.setdefault(o.amount_residual, []).append(o)
|
|
|
|
reconciled = 0
|
|
no_match = 0
|
|
used_ids = set()
|
|
|
|
# Sort lines by date (oldest first)
|
|
for line in sorted(transfer_lines, key=lambda l: l.move_id.date):
|
|
amount = line.amount
|
|
candidates = [c for c in by_amount.get(amount, []) if c.id not in used_ids]
|
|
|
|
if not candidates:
|
|
no_match += 1
|
|
continue
|
|
|
|
# Take the first (oldest) candidate
|
|
best = candidates[0]
|
|
|
|
try:
|
|
line.partner_id = company_partner_id
|
|
line.set_line_bank_statement_line(best.ids)
|
|
used_ids.add(best.id)
|
|
reconciled += 1
|
|
except Exception as e:
|
|
print(f' Error: line {line.id} (${amount}): {e}', flush=True)
|
|
|
|
if reconciled % 50 == 0 and reconciled > 0:
|
|
env.cr.commit()
|
|
|
|
env.cr.commit()
|
|
total_reconciled += reconciled
|
|
print(f'DONE {journal_name}: reconciled={reconciled}, no_match={no_match}', flush=True)
|
|
|
|
print(f'TOTAL: {total_reconciled} transfer lines reconciled', flush=True)
|