import time from netforce.model import Model, fields, get_model class PatientMove(Model): _name="clinic.patient.move" _fields={ 'create_date': fields.DateTime("Create Date", required=True), 'date': fields.Date("Date", search=True, required=True), 'patient_id': fields.Many2One('clinic.patient','Patient', search=True, required=True), 'patient_type_id': fields.Many2One('clinic.patient.type','Patient Type', search=True, required=True), 'location_from_id': fields.Many2One("clinic.department","From Department", search=True, required=True), 'location_to_id': fields.Many2One("clinic.department","To Department", search=True, required=True), "state": fields.Selection([['new','New'],["move","Move"],['dispose','Dispose']],'State'), } _defaults={ 'create_date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"), 'date': lambda *a: time.strftime("%Y-%m-%d"), "state": "new", } _order="date desc" _constraints=["check_location_status"] def check_location_status(self, ids, context={}): for obj in self.browse(ids): if obj.state=='move' and obj.location_from_id.id == obj.location_to_id.id: raise Exception("Can not move patient the same location!!") elif obj.state in ('new','dispose') and obj.location_from_id.id != obj.location_to_id.id: raise Exception("New/Dispose patient should be the same location!!") def onchange_patient(self, context={}): data=context['data'] patient_id=data['patient_id'] patient=get_model("clinic.patient").browse(patient_id) data['patient_type_id']=patient.type_id.id data['location_from_id']=patient.branch_id.id return data PatientMove.register()