clinic/netforce_clinic/models/shop.py

142 lines
4.9 KiB
Python
Raw Normal View History

2015-01-21 01:35:55 +00:00
import time
from netforce.model import Model, fields, get_model
from netforce.access import get_active_user
from netforce.utils import get_data_path
2015-01-23 10:28:08 +00:00
class Shop(Model):
_name="clinic.shop"
_string="Shop"
2015-01-21 01:35:55 +00:00
def _get_all(self,ids,context={}):
res={}
st=get_model("clinic.setting").browse(1)
shop_categs=[x.id for x in st.shop_categs]
2015-01-21 01:35:55 +00:00
for obj in self.browse(ids):
total=0
for line in obj.lines:
amt=line.amount or 0
total+=amt
2015-01-21 01:35:55 +00:00
res[obj.id]={
'total': total,
'shop_categs': shop_categs,
2015-01-21 01:35:55 +00:00
}
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),
2015-01-22 10:40:05 +00:00
'department_id': fields.Many2One("clinic.department","Department"),
'branch_id': fields.Many2One("clinic.branch","Branch"),
2015-01-23 10:28:08 +00:00
'lines': fields.One2Many('clinic.shop.line','shop_id','Lines'),
2015-01-21 01:35:55 +00:00
'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"),
2015-01-23 10:28:08 +00:00
"payments": fields.One2Many("account.payment","related_id","Payments"),
'dom_str': fields.Char("Dom Str"),
2015-01-29 16:38:22 +00:00
'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"),
2015-01-21 01:35:55 +00:00
}
2015-01-22 10:40:05 +00:00
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]
2015-01-29 16:38:22 +00:00
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
2015-01-22 10:40:05 +00:00
2015-01-21 01:35:55 +00:00
_defaults={
'number': '/',
2015-01-22 10:40:05 +00:00
'date': lambda *a: time.strftime("%Y-%m-%d"),
2015-01-21 01:35:55 +00:00
'user_id': lambda *a: get_active_user(),
2015-01-22 10:40:05 +00:00
'branch_id': _get_branch,
'department_id': _get_department,
2015-01-21 01:35:55 +00:00
'state': 'draft',
2015-01-29 16:38:22 +00:00
'shop_categs': _get_shop_categs,
'related_id': _get_related,
'patient_id': _get_patient,
2015-01-21 01:35:55 +00:00
}
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
2015-01-21 01:35:55 +00:00
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)
2015-01-21 01:35:55 +00:00
return data
2015-01-29 16:38:22 +00:00
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',
}
2015-01-23 10:28:08 +00:00
Shop.register()