import logging from odoo import fields _logger = logging.getLogger(__name__) def get_adp_receivable_aging(env, params): accounts = env['account.account'].search([ ('code', '=like', '1101%'), ('company_id', '=', env.company.id), ]) today = fields.Date.today() amls = env['account.move.line'].search([ ('account_id', 'in', accounts.ids), ('reconciled', '=', False), ('parent_state', '=', 'posted'), ]) buckets = {'current': 0, '1_30': 0, '31_60': 0, '61_90': 0, '90_plus': 0} for aml in amls: amt = abs(aml.amount_residual) if not aml.date_maturity or aml.date_maturity >= today: buckets['current'] += amt else: days = (today - aml.date_maturity).days if days <= 30: buckets['1_30'] += amt elif days <= 60: buckets['31_60'] += amt elif days <= 90: buckets['61_90'] += amt else: buckets['90_plus'] += amt return {'total': sum(buckets.values()), 'buckets': buckets} def match_adp_payment_to_invoice(env, params): move_line_ids = [int(x) for x in params['move_line_ids']] amls = env['account.move.line'].browse(move_line_ids).exists() if len(amls) < 2: return {'error': 'Need at least 2 existing journal items to reconcile'} amls.reconcile() return {'status': 'matched', 'move_line_ids': amls.ids} def verify_adp_split(env, params): invoice_id = int(params['invoice_id']) invoice = env['account.move'].browse(invoice_id) if not invoice.exists(): return {'error': 'Invoice not found'} lines = invoice.invoice_line_ids total = invoice.amount_untaxed return { 'invoice': invoice.name, 'total_untaxed': total, 'total_with_tax': invoice.amount_total, 'lines': [{'name': l.name, 'subtotal': l.price_subtotal, 'total': l.price_total} for l in lines], 'balanced': abs(sum(l.price_subtotal for l in lines) - total) < 0.01, } def find_adp_without_payment(env, params): adp_partner = env['res.partner'].search([('name', 'ilike', 'ADP')], limit=1) if not adp_partner: return {'status': 'info', 'message': 'No ADP partner found in the system.'} invoices = env['account.move'].search([ ('partner_id', '=', adp_partner.id), ('move_type', '=', 'out_invoice'), ('state', '=', 'posted'), ('payment_state', 'in', ('not_paid', 'partial')), ]) return { 'count': len(invoices), 'invoices': [{ 'id': inv.id, 'name': inv.name, 'amount': inv.amount_residual, 'date': str(inv.date), } for inv in invoices[:20]], } def get_adp_summary(env, params): date_from = params.get('date_from') date_to = params.get('date_to') accounts = env['account.account'].search([ ('code', '=like', '1101%'), ('company_id', '=', env.company.id), ]) domain = [ ('account_id', 'in', accounts.ids), ('parent_state', '=', 'posted'), ] if date_from: domain.append(('date', '>=', date_from)) if date_to: domain.append(('date', '<=', date_to)) lines = env['account.move.line'].search(domain) total_debit = sum(l.debit for l in lines) total_credit = sum(l.credit for l in lines) return { 'period': f'{date_from or "all"} to {date_to or "now"}', 'billed': total_debit, 'collected': total_credit, 'outstanding': total_debit - total_credit, } TOOLS = { 'get_adp_receivable_aging': get_adp_receivable_aging, 'match_adp_payment_to_invoice': match_adp_payment_to_invoice, 'verify_adp_split': verify_adp_split, 'find_adp_without_payment': find_adp_without_payment, 'get_adp_summary': get_adp_summary, }