import time from netforce.model import Model, fields, get_model from netforce.access import get_active_user from netforce.utils import get_data_path class Shop(Model): _name="clinic.shop" _string="Shop" def _get_all(self,ids,context={}): res={} st=get_model("clinic.setting").browse(1) shop_categs=[x.id for x in st.shop_categs] for obj in self.browse(ids): total=0 for line in obj.lines: amt=line.amount or 0 total+=amt res[obj.id]={ 'total': total, 'shop_categs': shop_categs, } return res _fields={ "number": fields.Char("Number",required=True,search=True), "ref": fields.Char("Ref",search=True), 'date': fields.Date("Date",search=True), 'patient_id': fields.Many2One('clinic.patient','Patient',search=True), 'department_id': fields.Many2One("clinic.department","Department"), 'branch_id': fields.Many2One("clinic.branch","Branch"), 'lines': fields.One2Many('clinic.shop.line','shop_id','Lines'), 'total': fields.Float("Total",function="_get_all",function_multi=True), 'user_id': fields.Many2One("base.user","Pharmacist"), 'state': fields.Selection([['draft','Draft'],['waiting_payment','Waiting Payment'],['paid','Paid']],'State'), "pickings": fields.One2Many("stock.picking","related_id","Pickings"), "invoices": fields.One2Many("account.invoice","related_id","Invoices"), "payments": fields.One2Many("account.payment","related_id","Payments"), 'dom_str': fields.Char("Dom Str"), 'shop_categs': fields.Many2Many("product.categ","Categs",function="_get_all",function_multi=True,store=True), "related_id": fields.Reference([["sale.order","Sales Order"],["purchase.order","Purchase Order"],["project","Project"],["job","Service Order"],["service.contract","Service Contract"]],"Related To"), } def _get_branch(self,context={}): b_ids=get_model('clinic.branch').search([]) if b_ids: return b_ids[0] def _get_department(self,context={}): dpt_ids=get_model('clinic.department').search([]) if dpt_ids: return dpt_ids[0] def _get_shop_categs(self,context={}): st=get_model("clinic.setting").browse(1) shop_categs=[x.id for x in st.shop_categs] return shop_categs def _get_related(self,context={}): related_id=None if context.get('refer_id'): related_id="clinic.hd.case,%s"%context.get("refer_id") return related_id def _get_patient(self,context={}): patient_id=None if context.get('refer_id'): refer_id=context.get("refer_id") hd_case=get_model("clinic.hd.case").browse(refer_id) patient_id=hd_case.patient_id.id return patient_id _defaults={ 'number': '/', 'date': lambda *a: time.strftime("%Y-%m-%d"), 'user_id': lambda *a: get_active_user(), 'branch_id': _get_branch, 'department_id': _get_department, 'state': 'draft', 'shop_categs': _get_shop_categs, 'related_id': _get_related, 'patient_id': _get_patient, } def update_all(self,context={}): data=context['data'] data['total']=0 for line in data['lines']: data['total']+=line['amount'] or 0 return data def onchange_line(self,context={}): data=context['data'] path=context['path'] line=get_data_path(data,path,parent=True) line['amount']=(line['qty'] or 0)*(line['price'] or 0) data=self.update_all(context=context) return data def onchange_product(self,context={}): data=context['data'] path=context['path'] line=get_data_path(data,path,parent=True) prod_id=line['product_id'] prod=get_model('product').browse(prod_id) line['uom_id']=prod.uom_id.id line['description']=prod.name or '' line['price']=prod.sale_price or 0 if not line.get('qty'): line['qty']=1 line['amount']=line['price']*line['qty'] data=self.update_all(context) return data def onchange_categ(self,context={}): data=context['data'] path=context['path'] line=get_data_path(data,path,parent=True) line['product_id']=None return data def create(self,vals,**kw): id=super().create(vals,**kw) self.function_store([id]) return id def write(self,ids,vals,**kw): super().write(ids,vals,**kw) self.function_store(ids) def pay(self,ids,context={}): obj=self.browse(ids)[0] active_id=obj.id action="clinic_shop" if obj.related_id: active_id=obj.related_id.id action="clinic_hd_case" return { 'next': { 'name': action, 'mode': 'form', 'active_id': active_id, }, 'flash': 'Pay Successfully', } Shop.register()