import time from netforce.model import Model, fields, get_model from netforce.access import get_active_company class HDCaseExpense(Model): _name="clinic.hd.case.expense" _string="Expense" _multi_company=True def _get_patient_conflict(self,ids,context={}): res={} for obj in self.browse(ids): hd_case=obj.hd_case_id patient=hd_case.patient_id conflict=False if obj.patient_id.id!=patient.id: conflict=True res[obj.id]=conflict return res def _get_store(self,ids,context={}): res={} for obj in self.browse(ids): pt=obj.patient_id name='%s,%s,%s,%s'%(obj.date, pt.hn_no or '', pt.card_no or '', pt.name) res[obj.id]=name return res _fields={ 'name': fields.Char("Name", function="_get_store",store=True), 'date': fields.Date("Date",required=True,search=True), 'patient_id': fields.Many2One("clinic.patient","Patient",required=True,search=True), 'hd_case_id': fields.Many2One("clinic.hd.case","HD Case",required=True,search=True), 'payment_id': fields.Many2One("account.payment","Payment",search=True), "invoices": fields.One2Many("account.invoice","clinic_expense_id","Invoices"), 'fee_amt': fields.Float("Fee"), 'mdc_amt': fields.Float("Medicine"), # EPOadm 'srv_amt': fields.Float("Service"), # Allow 'fee': fields.Boolean("Fee"), 'medicine': fields.Boolean("Medicine"), 'service': fields.Boolean("Service"), 'state': fields.Selection([['draft','Draft'],['waiting_matching','Waiting Matching'],['match','Match'],['unmatch','Unmatch'],['completed','Completed']],'State'), 'note': fields.Text("Note"), 'pt_conflict': fields.Boolean("Patient Conclict",function="_get_patient_conflict"), 'company_id': fields.Many2One("company","Company"), 'match_id': fields.Many2One("clinic.report.payment.matching","Match"), 'invno': fields.Char("Invoice No"), 'ok': fields.Boolean("OK"), } _defaults={ 'state': 'draft', 'date': time.strftime("%Y-%m-%d"), "company_id": lambda *a: get_active_company(), } _order="date desc" def create(self,vals,**kw): new_id=super().create(vals,**kw) hd_case_id=vals['hd_case_id'] hd_case=get_model("clinic.hd.case").browse(hd_case_id) for inv in hd_case.invoices: inv.write({ 'clinic_expense_id': new_id, }) self.function_store([new_id]) return new_id def write(self,ids,vals,**kw): self.function_store(ids) super().write(ids,vals,**kw) def complete(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'completed', }) def to_draft(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'draft', 'ok': False, }) def delete(self,ids,context={}): for obj in self.browse(ids): if obj.state!='draft': continue #XXX migration raise Exception("Status is not draft!") super().delete(ids,context) def do_match(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'match', 'ok':True, }) def do_unmatch(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'unmatch', 'ok': False, }) def cancel(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'cancelled', }) HDCaseExpense.register()