changeset 5159:d35dc2652483

health_insurance: Fix bug #62596: Traceback if there is no Account Receivable defined neither on party or default acct config
author Luis Falcon <falcon@gnuhealth.org>
date Wed, 08 Jun 2022 12:52:34 +0100
parents 5a01f3ef2acd
children 180cc7a9d84b
files tryton/health_insurance/data/messages/messages.xml tryton/health_insurance/exceptions.py tryton/health_insurance/wizard/wizard_health_insurance.py
diffstat 3 files changed, 24 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/tryton/health_insurance/data/messages/messages.xml
+++ b/tryton/health_insurance/data/messages/messages.xml
@@ -21,5 +21,8 @@
         <record model="ir.message" id="msg_need_a_policy">
             <field name="text">SM-INSURANCE-0006: You need to specify either a percentage or a fixed price for this policy line. If both are present, the percentage will be applied</field>
         </record>
+        <record model="ir.message" id="msg_no_account_receivable">
+            <field name="text">SM-INSURANCE-0007: No Account Receivable defined in party or in default configuration</field>
+        </record>
     </data>
 </tryton>
--- a/tryton/health_insurance/exceptions.py
+++ b/tryton/health_insurance/exceptions.py
@@ -24,3 +24,6 @@
 
 class NoPaymentTerm(UserError):
     pass
+
+class NoAccountReceivable(UserError):
+    pass
--- a/tryton/health_insurance/wizard/wizard_health_insurance.py
+++ b/tryton/health_insurance/wizard/wizard_health_insurance.py
@@ -26,7 +26,8 @@
 from trytond.transaction import Transaction
 from trytond.pool import Pool
 from trytond.i18n import gettext
-from ..exceptions import (ServiceInvoiced, NoInvoiceAddress, NoPaymentTerm)
+from ..exceptions import (
+    ServiceInvoiced, NoInvoiceAddress, NoPaymentTerm, NoAccountReceivable)
 
 __all__ = ['CreateServiceInvoice']
 
@@ -78,6 +79,8 @@
         Invoice = pool.get('account.invoice')
         Party = pool.get('party.party')
         Journal = pool.get('account.journal')
+        AcctConfig = pool.get('account.configuration')
+        acct_config = AcctConfig(1)
 
         currency_id = Transaction().context.get('currency')
 
@@ -100,9 +103,22 @@
             invoice_data['party'] = party.id
             invoice_data['type'] = 'out'
             invoice_data['invoice_date'] = datetime.date.today()
-            invoice_data['account'] = party.account_receivable.id
             invoice_data['company'] = service.company.id
 
+            """ Look for the AR account in the following order:
+                * Party
+                * Default AR in accounting config
+                * Raise an error if there is no AR account
+            """
+            if (party.account_receivable):
+                invoice_data['account'] = party.account_receivable.id
+            elif (acct_config.default_account_receivable):
+                invoice_data['account'] = \
+                    acct_config.default_account_receivable.id
+            else:
+                raise NoAccountReceivable(
+                    gettext('health_insurance.msg_no_account_receivable'))
+
             ctx = {}
             sale_price_list = None
             if hasattr(party, 'sale_price_list'):