changeset 2119:d48cc30f08f2

health_services : Extend prescription model to include the lines in the service document
author Luis Falcon <falcon@gnu.org>
date Tue, 27 Oct 2015 12:44:27 +0000
parents 76c627904c54
children 8636a781d095
files tryton/health_services/__init__.py tryton/health_services/health_services.py tryton/health_services/health_services_view.xml
diffstat 3 files changed, 69 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/tryton/health_services/__init__.py
+++ b/tryton/health_services/__init__.py
@@ -35,6 +35,7 @@
         CreateServiceInvoiceInit,
         Invoice,
         InvoiceLine,
+        PatientPrescriptionOrder,
         module='health_services', type_='model')
     Pool.register(
         CreateServiceInvoice,
--- a/tryton/health_services/health_services.py
+++ b/tryton/health_services/health_services.py
@@ -27,7 +27,8 @@
 from trytond.pool import Pool
 
 
-__all__ = ['GnuHealthSequences', 'HealthService', 'HealthServiceLine']
+__all__ = ['GnuHealthSequences', 'HealthService', 'HealthServiceLine',
+    'PatientPrescriptionOrder']
 
 
 class GnuHealthSequences(ModelSingleton, ModelSQL, ModelView):
@@ -149,3 +150,60 @@
                 #"This service has been invoiced.\n"
                 #"You can no longer modify service lines.")
         #return super(HealthServiceLine, cls).delete(lines)
+
+
+""" Add Prescription order charges to service model """
+
+class PatientPrescriptionOrder(ModelSQL, ModelView):
+    'Prescription Order'
+    __name__ = 'gnuhealth.prescription.order'
+
+    service = fields.Many2One(
+        'gnuhealth.health_service', 'Service',
+        domain=[('patient', '=', Eval('patient'))], depends=['patient'],
+        states = {'readonly': Equal(Eval('state'), 'done')},
+        help="Service document associated to this prescription")
+
+    @classmethod
+    def __setup__(cls):
+        cls._buttons.update({
+            'update_service': {
+                'readonly': Equal(Eval('state'), 'done'),
+            },
+            })
+
+
+    @classmethod
+    @ModelView.button
+    def update_service(cls, prescriptions):
+        pool = Pool()
+        HealthService = pool.get('gnuhealth.health_service')
+
+        hservice = []
+        prescription = prescriptions[0]
+
+        if not prescription.service:
+            cls.raise_user_error("Need to associate a service !")
+
+        service_data = {}
+        service_lines = []
+
+        # Add the prescription lines to the service document
+
+        for line in prescription.prescription_line:
+            service_lines.append(('create', [{
+                'product': line.medicament.name.id,
+                'desc': 'Prescription Line',
+                'qty': line.quantity
+                }]))
+
+            
+        hservice.append(prescription.service)
+        
+        description = "Services including " + \
+            prescription.prescription_id
+        
+        service_data ['desc'] =  description
+        service_data ['service_line'] = service_lines
+                
+        HealthService.write(hservice, service_data)
--- a/tryton/health_services/health_services_view.xml
+++ b/tryton/health_services/health_services_view.xml
@@ -99,5 +99,14 @@
         </record>
 
 
+<!-- Extend prescription view to include the service -->
+
+        <record model="ir.ui.view" id="view_prescription_form">
+            <field name="model">gnuhealth.prescription.order</field>
+            <field name="inherit" ref="health.gnuhealth_prescription_view" />
+            <field name="type">form</field>
+            <field name="name">gnuhealth_prescription</field>
+        </record> 
+
     </data>
 </tryton>