2014-11-27 15:14:31 +00:00
|
|
|
import time
|
2014-12-15 17:46:43 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
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"
|
|
|
|
|
|
|
|
_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),
|
|
|
|
#'duration': fields.Integer("Day in Period",required=True),
|
|
|
|
'nmonth': fields.Integer("Total Copy",required=True),
|
2014-11-27 15:14:31 +00:00
|
|
|
'lines': fields.One2Many("clinic.period.line","period_id", "Lines"),
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|