59 lines
2.2 KiB
Python
59 lines
2.2 KiB
Python
import logging
|
|
|
|
from odoo import models, api
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class AccountMoveAuditHook(models.Model):
|
|
_inherit = 'account.move'
|
|
|
|
def action_post(self):
|
|
res = super().action_post()
|
|
|
|
ICP = self.env['ir.config_parameter'].sudo()
|
|
if ICP.get_param('fusion_accounting.enable_post_audit', 'False') != 'True':
|
|
return res
|
|
|
|
for move in self:
|
|
try:
|
|
self._fusion_audit_posted_entry(move)
|
|
except Exception as e:
|
|
_logger.warning("Fusion post-audit hook failed for %s: %s", move.name, e)
|
|
|
|
return res
|
|
|
|
def _fusion_audit_posted_entry(self, move):
|
|
issues = []
|
|
|
|
total_debit = sum(l.debit for l in move.line_ids)
|
|
total_credit = sum(l.credit for l in move.line_ids)
|
|
if abs(total_debit - total_credit) > 0.01:
|
|
issues.append(f'Unbalanced: debit={total_debit:.2f}, credit={total_credit:.2f}')
|
|
|
|
for line in move.line_ids:
|
|
if not line.account_id:
|
|
issues.append(f'Line missing account: {line.name}')
|
|
# M6: Only flag missing tax when the product has taxes configured
|
|
# (avoids false positives for HST-exempt healthcare services)
|
|
if (line.product_id and not line.tax_ids
|
|
and move.move_type in ('out_invoice', 'out_refund', 'in_invoice', 'in_refund')):
|
|
# Check if the product has default taxes configured
|
|
product_taxes = line.product_id.taxes_id if move.move_type in ('out_invoice', 'out_refund') else line.product_id.supplier_taxes_id
|
|
if product_taxes:
|
|
issues.append(f'Missing tax on product line: {line.product_id.name} (product has taxes configured but line has none)')
|
|
|
|
if not move.line_ids:
|
|
issues.append('Entry has no lines')
|
|
|
|
if issues:
|
|
body_parts = ['<strong>Fusion AI Auto-Audit</strong><ul>']
|
|
for issue in issues:
|
|
body_parts.append(f'<li>{issue}</li>')
|
|
body_parts.append('</ul>')
|
|
move.message_post(
|
|
body=''.join(body_parts),
|
|
message_type='comment',
|
|
subtype_xmlid='mail.mt_note',
|
|
)
|