clinic/netforce_clinic/models/compute_labor_cost.py

97 lines
3.3 KiB
Python
Raw Normal View History

2015-02-26 00:56:41 +00:00
import time
from calendar import monthrange
from netforce.model import Model,fields,get_model
class ComputeLaborCost(Model):
_name="clinic.compute.labor.cost"
_string="Compute Labor Cost"
_transient=True
_fields={
2015-06-21 16:05:47 +00:00
"period_id": fields.Many2One("clinic.period.line","Period"),
2015-02-26 00:56:41 +00:00
"date": fields.Date("Month"),
"date_from": fields.Date("From", required=True),
"date_to": fields.Date("To", required=True),
2015-03-05 11:10:56 +00:00
'branch_id': fields.Many2One("clinic.branch","Branch"),
'department_id': fields.Many2One("clinic.department","Department"),
2015-02-26 00:56:41 +00:00
}
def _get_date_from(self,context={}):
year,month=time.strftime("%Y-%m").split("-")
return '%s-%s-01'%(year,month)
def _get_date_to(self,context={}):
year,month,day=time.strftime("%Y-%m-%d").split("-")
weekday, total_day=monthrange(int(year), int(month))
return "%s-%s-%s"%(year,month,total_day)
2015-06-21 16:05:47 +00:00
def default_get(self,field_names=None,context={},**kw):
defaults=context.get("defaults",{})
date_from=defaults.get("date_from", self._get_date_from())
date_to=defaults.get("date_to", self._get_date_to())
yearnow=date_from.split("-")[0]
for period in get_model('clinic.period').search_browse([['name','=',yearnow]]):
for line in period.lines:
if line.state=='open':
period_id=line.id
date_from=line.date_start
date_to=line.date_stop
break
res={
'period_id': period_id,
'date': time.strftime("%Y-%m-%d"),
'date_from': date_from,
'date_to': date_to,
}
return res
2015-02-26 00:56:41 +00:00
def compute(self,ids,context={}):
obj=self.browse(ids)[0]
dom=[]
dom.append(['date','>=', obj.date_from])
dom.append(['date','<=', obj.date_to])
2015-03-05 11:10:56 +00:00
if obj.branch_id:
dom.append(['branch_id','=',obj.branch_id.id])
if obj.department_id:
dom.append(['department_id','=',obj.department_id.id])
2015-02-26 00:56:41 +00:00
for lcost in get_model("clinic.labor.cost").search_browse(dom):
print("compute %s ....", lcost.cycle_item_id.name)
2015-03-05 11:10:56 +00:00
#item=lcost.cycle_item_id
#item.to_draft()
#item.validate()
2015-02-26 00:56:41 +00:00
lcost.compute()
print("Done!")
return {
'next': {
'name': 'clinic_compute_labor_cost',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Compute Successfully',
}
def onchange_date(self,context={}):
data=context['data']
date=data['date']
year,month,day=date.split("-")
weekday, total_day=monthrange(int(year), int(month))
data['date_from']="%s-%s-01"%(year,month)
data['date_to']="%s-%s-%s"%(year,month,total_day)
return data
2015-04-07 04:43:32 +00:00
def onchange_branch(self,context={}):
data=context['data']
data['department_id']=None
return data
2015-06-21 16:05:47 +00:00
def onchange_period(self,context={}):
data=context['data']
period_id=data['period_id']
period=get_model('clinic.period.line').browse(period_id)
data['date_from']=period.date_start
data['date_to']=period.date_stop
return data
2015-02-26 00:56:41 +00:00
ComputeLaborCost.register()