172 lines
5.3 KiB
Python
172 lines
5.3 KiB
Python
import logging
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
def calculate_hst_balance(env, params):
|
|
date_from = params.get('date_from')
|
|
date_to = params.get('date_to')
|
|
base_domain = [
|
|
('parent_state', '=', 'posted'),
|
|
('company_id', '=', env.company.id),
|
|
]
|
|
if date_from:
|
|
base_domain.append(('date', '>=', date_from))
|
|
if date_to:
|
|
base_domain.append(('date', '<=', date_to))
|
|
|
|
collected_accounts = env['account.account'].search([
|
|
('code', '=like', '2005%'), ('company_id', '=', env.company.id),
|
|
])
|
|
itc_accounts = env['account.account'].search([
|
|
('code', '=like', '2006%'), ('company_id', '=', env.company.id),
|
|
])
|
|
|
|
collected_lines = env['account.move.line'].search(
|
|
base_domain + [('account_id', 'in', collected_accounts.ids)]
|
|
)
|
|
itc_lines = env['account.move.line'].search(
|
|
base_domain + [('account_id', 'in', itc_accounts.ids)]
|
|
)
|
|
|
|
hst_collected = abs(sum(l.balance for l in collected_lines))
|
|
itcs = abs(sum(l.balance for l in itc_lines))
|
|
|
|
return {
|
|
'hst_collected': hst_collected,
|
|
'input_tax_credits': itcs,
|
|
'net_hst': hst_collected - itcs,
|
|
'status': 'owing' if (hst_collected - itcs) > 0 else 'refund',
|
|
'period': f'{date_from or "all"} to {date_to or "now"}',
|
|
}
|
|
|
|
|
|
def get_tax_report(env, params):
|
|
report_ref = params.get('report_ref', 'account.generic_tax_report')
|
|
try:
|
|
report = env.ref(report_ref)
|
|
except Exception:
|
|
return {'error': f'Report not found: {report_ref}'}
|
|
options = report.get_options({
|
|
'date': {
|
|
'date_from': params.get('date_from', ''),
|
|
'date_to': params.get('date_to', ''),
|
|
}
|
|
})
|
|
lines = report._get_lines(options)
|
|
return {
|
|
'report_name': report.name,
|
|
'lines': [{
|
|
'name': l.get('name', ''),
|
|
'columns': [c.get('no_format', c.get('name', '')) for c in l.get('columns', [])],
|
|
} for l in lines[:50]],
|
|
}
|
|
|
|
|
|
def find_missing_tax_invoices(env, params):
|
|
date_from = params.get('date_from')
|
|
date_to = params.get('date_to')
|
|
domain = [
|
|
('move_type', 'in', ('out_invoice', 'out_refund')),
|
|
('state', '=', 'posted'),
|
|
('company_id', '=', env.company.id),
|
|
]
|
|
if date_from:
|
|
domain.append(('date', '>=', date_from))
|
|
if date_to:
|
|
domain.append(('date', '<=', date_to))
|
|
|
|
invoices = env['account.move'].search(domain)
|
|
missing = invoices.filtered(
|
|
lambda inv: not any(line.tax_ids for line in inv.invoice_line_ids)
|
|
)
|
|
return {
|
|
'total_invoices': len(invoices),
|
|
'missing_tax_count': len(missing),
|
|
'invoices': [{
|
|
'id': inv.id,
|
|
'name': inv.name,
|
|
'partner': inv.partner_id.name if inv.partner_id else '',
|
|
'amount_total': inv.amount_total,
|
|
'date': str(inv.date),
|
|
} for inv in missing[:30]],
|
|
}
|
|
|
|
|
|
def find_missing_itc_bills(env, params):
|
|
date_from = params.get('date_from')
|
|
date_to = params.get('date_to')
|
|
domain = [
|
|
('move_type', 'in', ('in_invoice', 'in_refund')),
|
|
('state', '=', 'posted'),
|
|
('company_id', '=', env.company.id),
|
|
]
|
|
if date_from:
|
|
domain.append(('date', '>=', date_from))
|
|
if date_to:
|
|
domain.append(('date', '<=', date_to))
|
|
|
|
bills = env['account.move'].search(domain)
|
|
missing = bills.filtered(
|
|
lambda b: not any(line.tax_ids for line in b.invoice_line_ids)
|
|
)
|
|
return {
|
|
'total_bills': len(bills),
|
|
'missing_itc_count': len(missing),
|
|
'bills': [{
|
|
'id': b.id,
|
|
'name': b.name,
|
|
'partner': b.partner_id.name if b.partner_id else '',
|
|
'amount_total': b.amount_total,
|
|
'date': str(b.date),
|
|
} for b in missing[:30]],
|
|
}
|
|
|
|
|
|
def get_tax_return_status(env, params):
|
|
returns = env['account.return'].search([
|
|
('company_id', '=', env.company.id),
|
|
], order='date_start desc', limit=10)
|
|
return {
|
|
'returns': [{
|
|
'id': r.id,
|
|
'name': r.display_name,
|
|
'date_start': str(r.date_start) if hasattr(r, 'date_start') else '',
|
|
'date_end': str(r.date_end) if hasattr(r, 'date_end') else '',
|
|
'state': r.state if hasattr(r, 'state') else '',
|
|
} for r in returns],
|
|
}
|
|
|
|
|
|
def generate_tax_return(env, params):
|
|
try:
|
|
env['account.return']._generate_or_refresh_all_returns(
|
|
company=env.company
|
|
)
|
|
return {'status': 'generated', 'message': 'Tax returns refreshed successfully.'}
|
|
except Exception as e:
|
|
return {'error': str(e)}
|
|
|
|
|
|
def validate_tax_return(env, params):
|
|
return_id = int(params['return_id'])
|
|
tax_return = env['account.return'].browse(return_id)
|
|
if not tax_return.exists():
|
|
return {'error': 'Tax return not found'}
|
|
try:
|
|
tax_return.action_validate()
|
|
return {'status': 'validated', 'return_id': return_id}
|
|
except Exception as e:
|
|
return {'error': str(e)}
|
|
|
|
|
|
TOOLS = {
|
|
'calculate_hst_balance': calculate_hst_balance,
|
|
'get_tax_report': get_tax_report,
|
|
'find_missing_tax_invoices': find_missing_tax_invoices,
|
|
'find_missing_itc_bills': find_missing_itc_bills,
|
|
'get_tax_return_status': get_tax_return_status,
|
|
'generate_tax_return': generate_tax_return,
|
|
'validate_tax_return': validate_tax_return,
|
|
}
|