2014-11-27 15:14:31 +00:00
|
|
|
import time
|
2014-12-15 17:46:43 +00:00
|
|
|
from calendar import monthrange
|
2014-11-27 15:14:31 +00:00
|
|
|
|
|
|
|
from netforce.model import Model, fields
|
|
|
|
|
|
|
|
class Period(Model):
|
|
|
|
_name="clinic.period"
|
|
|
|
_string="Period"
|
|
|
|
|
2014-12-16 09:53:36 +00:00
|
|
|
def _get_last_period(self,ids,context={}):
|
|
|
|
res={}
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
last_id=None
|
|
|
|
for line in obj.lines:
|
2015-06-20 10:01:37 +00:00
|
|
|
if line.state=='close':
|
2014-12-16 09:53:36 +00:00
|
|
|
last_id=line.id
|
|
|
|
res[obj.id]=last_id
|
|
|
|
return res
|
|
|
|
|
2014-11-27 15:14:31 +00:00
|
|
|
_fields={
|
|
|
|
"name": fields.Char("Name",required=True,search=True),
|
2014-12-15 17:46:43 +00:00
|
|
|
'date_start': fields.Date("From",required=True),
|
|
|
|
'date_stop': fields.Date("To",required=True),
|
2014-12-16 09:53:36 +00:00
|
|
|
'nmonth': fields.Integer("Count",required=True),
|
2014-11-27 15:14:31 +00:00
|
|
|
'lines': fields.One2Many("clinic.period.line","period_id", "Lines"),
|
2014-12-16 09:53:36 +00:00
|
|
|
'last_period_id': fields.Many2One("clinic.period.line","Last Period",function="_get_last_period"),
|
2014-11-27 15:14:31 +00:00
|
|
|
}
|
2015-06-20 10:01:37 +00:00
|
|
|
|
2014-11-27 15:14:31 +00:00
|
|
|
def _get_start(self,context={}):
|
|
|
|
year=time.strftime("%Y")
|
|
|
|
return '%s-01-01'%year
|
|
|
|
|
2014-12-15 17:46:43 +00:00
|
|
|
def _get_stop(self,context={}):
|
|
|
|
year=time.strftime("%Y")
|
|
|
|
return '%s-01-30'%year
|
|
|
|
|
2014-11-27 15:14:31 +00:00
|
|
|
_defaults={
|
|
|
|
'name': lambda *a: time.strftime("%Y"),
|
|
|
|
'date_start': _get_start,
|
2014-12-15 17:46:43 +00:00
|
|
|
'date_stop': _get_stop,
|
2014-11-27 15:14:31 +00:00
|
|
|
'nmonth': 12,
|
|
|
|
}
|
2015-06-20 10:01:37 +00:00
|
|
|
|
|
|
|
def close_period(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
for line in obj.lines:
|
|
|
|
line.write({
|
|
|
|
'state': 'close',
|
|
|
|
})
|
2014-11-27 15:14:31 +00:00
|
|
|
|
|
|
|
def gen_period(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
for line in obj.lines:
|
|
|
|
line.delete()
|
2014-12-15 17:46:43 +00:00
|
|
|
year,month,day=[int(x) for x in obj.date_start.split("-")]
|
|
|
|
tyear,tmonth,tday=[int(x) for x in obj.date_stop.split("-")]
|
2014-11-27 15:14:31 +00:00
|
|
|
lines=[]
|
2014-12-15 17:46:43 +00:00
|
|
|
def _zfill(n):
|
|
|
|
return str(n).zfill(2)
|
|
|
|
for m in range(1,obj.nmonth):
|
|
|
|
if tmonth >12:
|
|
|
|
tmonth=1
|
|
|
|
tyear+=1
|
|
|
|
weekday, ttotal_day=monthrange(int(tyear), int(tmonth))
|
|
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
|
|
if day > total_day:
|
|
|
|
day=total_day
|
|
|
|
if tday > ttotal_day:
|
|
|
|
tday=ttotal_day
|
2014-11-27 15:14:31 +00:00
|
|
|
lines.append(('create',{
|
2014-12-15 17:46:43 +00:00
|
|
|
'date_start': '%s-%s-%s'%(year,_zfill(month),_zfill(day)),
|
|
|
|
'date_stop': '%s-%s-%s'%(tyear,_zfill(tmonth),_zfill(tday)),
|
2014-11-27 15:14:31 +00:00
|
|
|
}))
|
2014-12-15 17:46:43 +00:00
|
|
|
if month==tmonth:
|
|
|
|
tmonth+=1
|
|
|
|
month=tmonth
|
|
|
|
year=tyear
|
|
|
|
tmonth+=1
|
2014-11-27 15:14:31 +00:00
|
|
|
obj.write({
|
|
|
|
'lines': lines,
|
|
|
|
})
|
2014-12-15 17:46:43 +00:00
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_period',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': obj.id,
|
|
|
|
},
|
|
|
|
'flash': 'Generate successfully',
|
|
|
|
}
|
2014-11-27 15:14:31 +00:00
|
|
|
|
|
|
|
Period.register()
|