changeset 5153:7139fadf96fe

Complete task #16213: Add a fix price in the options of the insurance policy plan line
author Luis Falcon <falcon@gnuhealth.org>
date Wed, 01 Jun 2022 21:07:05 +0100
parents ceddfbb042af
children e32aa03723b3 31ceaaddc3ab
files tryton/health_insurance/data/messages/messages.xml tryton/health_insurance/exceptions.py tryton/health_insurance/health_insurance.py tryton/health_insurance/view/gnuhealth_insurance_plan_product_policy.xml tryton/health_insurance/view/gnuhealth_insurance_plan_product_policy_tree.xml tryton/health_insurance/wizard/wizard_health_insurance.py
diffstat 6 files changed, 35 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/tryton/health_insurance/data/messages/messages.xml
+++ b/tryton/health_insurance/data/messages/messages.xml
@@ -18,5 +18,8 @@
         <record model="ir.message" id="msg_no_payment_term">
             <field name="text">SM-INSURANCE-0005: No payment term</field>
         </record>
+        <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>
     </data>
 </tryton>
--- a/tryton/health_insurance/exceptions.py
+++ b/tryton/health_insurance/exceptions.py
@@ -4,9 +4,11 @@
 from trytond.model.exceptions import ValidationError
 
 
-class DisctountPctOutOfRange(ValidationError):
+class DiscountPctOutOfRange(ValidationError):
     pass
 
+class NeedAPolicy(ValidationError):
+    pass
 
 class DiscountWithoutElement(ValidationError):
     pass
--- a/tryton/health_insurance/health_insurance.py
+++ b/tryton/health_insurance/health_insurance.py
@@ -24,7 +24,8 @@
 from trytond.pyson import Eval
 from trytond.i18n import gettext
 
-from .exceptions import (DisctountPctOutOfRange, DiscountWithoutElement)
+from .exceptions import (DiscountPctOutOfRange, NeedAPolicy,
+                         DiscountWithoutElement)
 
 
 __all__ = ['InsurancePlanProductPolicy', 'InsurancePlan', 'HealthService']
@@ -44,7 +45,11 @@
 
     discount = fields.Float(
         'Discount', digits=(3, 2),
-        help="Discount in Percentage", required=True)
+        help="Discount in Percentage. It has higher precedence than "
+             "the fixed price when both values coexist")
+
+    price = fields.Float(
+        'Price', help="Apply a fixed price for this product or category")
 
     @classmethod
     def validate(cls, policies):
@@ -54,9 +59,14 @@
             policy.validate_policy_elements()
 
     def validate_discount(self):
-        if (self.discount < 0 or self.discount > 100):
-            raise DisctountPctOutOfRange(
-                gettext('health_insurance.msg_pct_out_of_range')
+        if (self.discount):
+            if (self.discount < 0 or self.discount > 100):
+                raise DiscountPctOutOfRange(
+                    gettext('health_insurance.msg_pct_out_of_range')
+                    )
+        if (not self.discount and not self.price):
+            raise NeedAPolicy(
+                gettext('health_insurance.msg_need_a_policy')
                 )
 
     def validate_policy_elements(self):
--- a/tryton/health_insurance/view/gnuhealth_insurance_plan_product_policy.xml
+++ b/tryton/health_insurance/view/gnuhealth_insurance_plan_product_policy.xml
@@ -8,4 +8,6 @@
     <field name="product_category"/>
     <label name="discount"/>
     <field name="discount"/>
+    <label name="price"/>
+    <field name="price"/>
 </form>
--- a/tryton/health_insurance/view/gnuhealth_insurance_plan_product_policy_tree.xml
+++ b/tryton/health_insurance/view/gnuhealth_insurance_plan_product_policy_tree.xml
@@ -4,4 +4,5 @@
     <field name="product" expand="1"/>
     <field name="product_category" expand="1"/>
     <field name="discount" expand="1"/>
+    <field name="price" expand="1"/>
 </tree>
--- a/tryton/health_insurance/wizard/wizard_health_insurance.py
+++ b/tryton/health_insurance/wizard/wizard_health_insurance.py
@@ -46,6 +46,8 @@
             # mutually exclusive.
 
             discount = {}
+            # If the policy line contains both values (percentage and
+            # fixed price, the percentage value will take over.
             if insurance.plan_id.product_policy:
                 for policy in insurance.plan_id.product_policy:
                     # Check first for product
@@ -55,6 +57,11 @@
                             discount['type'] = 'pct'
                             return discount
 
+                        if policy.price:
+                            discount['value'] = policy.price
+                            discount['type'] = 'fixed'
+                            return discount
+
                 for policy in insurance.plan_id.product_policy:
                     # Then, if there's no product, check for the category
                     if (policy.product_category in product.categories):
@@ -178,7 +185,10 @@
                                         str_disc = str(discount['value']) + '%'
                                         desc = line.desc + " (Discnt " + \
                                             str(str_disc) + ")"
-
+                                    # Use the fixed price
+                                    else:
+                                        unit_price = discount['value']
+                                        desc = f"{line.desc} (policy plan)"
                     invoice_lines.append(('create', [{
                             'origin': str(line),
                             'product': line.product.id,