[IMP] account_exception: make it work on write and specific states

H11044
This commit is contained in:
Jorge Che
2022-09-30 15:38:53 +00:00
parent 8a09a73afa
commit 78fa2aec94
7 changed files with 58 additions and 13 deletions

View File

@@ -1,13 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="excep_no_phone" model="exception.rule">
<record id="except_no_phone" model="exception.rule">
<field name="name">No phone</field>
<field name="description">No phone number on customer</field>
<field name="sequence">50</field>
<field name="model">account.move</field>
<field name="code">if journal_entry.move_type == 'out_customer' and not journal_entry.partner_id.phone: failed=True</field>
<field name="code">failed = journal_entry.move_type == 'out_invoice' and not journal_entry.partner_id.phone</field>
<field name="active" eval="True"/>
</record>
</record>
<record id="except_no_phone_on_post" model="exception.rule">
<field name="name">No phone when posting</field>
<field name="description">No phone when posting</field>
<field name="sequence">50</field>
<field name="model">account.move</field>
<field name="code">failed = journal_entry._context.get('newState') == 'posted' and journal_entry.move_type == 'out_invoice' and not journal_entry.partner_id.phone</field>
<field name="active" eval="True"/>
</record>
</odoo>

View File

@@ -33,13 +33,23 @@ class AccountMove(models.Model):
def _reverse_field(self):
return 'journal_entry_ids'
def action_post(self):
self.ensure_one()
if self.detect_exceptions():
return self._popup_exceptions()
return super().action_post()
# TODO
@api.model
def _get_popup_action(self):
return self.env.ref('account_exception.action_account_move_exception_confirm')
def write(self, vals):
print(f'\n\nwrite()\ncontext={self._context}\n')
newState = vals.get('state', '')
if not vals.get('ignore_exception'):
for journal_entry in self:
if journal_entry.with_context(newState=newState).detect_exceptions():
return self._popup_exceptions()
return super().write(vals)
def detect_exceptions(self):
print(f'\n\ndetect_exceptions()\ncontext={self._context}\n')
res = False
if not self._context.get("detect_exceptions"):
self = self.with_context(detect_exceptions=True)
res = super(AccountMove, self).detect_exceptions()
return res

View File

@@ -0,0 +1 @@
from . import test_account_move_exception

View File

@@ -0,0 +1,24 @@
# from odoo.tests import common
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.tests import tagged
@tagged('post_install', '-at_install')
class TestAccountMoveException(AccountTestInvoicingCommon):
def test_10_validation_on_write(self):
self.env.user.groups_id += self.env.ref('analytic.group_analytic_accounting')
exception = self.env.ref('account_exception.excep_no_phone').sudo()
exception.active = True
invoice = self.init_invoice('out_invoice', products=self.product_a)
# must be exceptions when no phone and posting
invoice.partner_id.phone = False
invoice.action_post()
self.assertTrue(invoice.exception_ids)
# no exceptions when phone and posting
invoice.partner_id.phone = '123'
invoice.action_post()
self.assertTrue(invoice.exception_ids)
self.assertTrue(invoice.state='posted')

View File

@@ -33,8 +33,8 @@
groups="base_exception.group_exception_rule_manager"/>
</div>
</sheet>
<xpath expr="//field[@name='journal_id']/.." position="after">
<field name="ignore_exception" states="done" />
<xpath expr="//div[@name='journal_div']" position="after">
<field name="ignore_exception" />
</xpath>
</field>
</record>

View File

@@ -23,4 +23,5 @@ class AccountMoveExceptionConfirm(models.TransientModel):
def _action_ignore(self):
self.related_model_id.ignore_exception = True
super()._action_ignore()
return self.related_model_id.action_post()
# return self.related_model_id.action_post()
return True

View File

@@ -2,7 +2,7 @@
<odoo>
<record id="view_account_move_exception_confirm" model="ir.ui.view">
<field name="name">Stock Exceptions Rules</field>
<field name="name">Journal Entry Exception Rules</field>
<field name="model">account.move.exception.confirm</field>
<field name="inherit_id" ref="base_exception.view_exception_rule_confirm"/>
<field name="mode">primary</field>