import time from calendar import monthrange from netforce.model import Model, fields class Period(Model): _name="clinic.period" _string="Period" def _get_last_period(self,ids,context={}): res={} for obj in self.browse(ids): last_id=None for line in obj.lines: if line.close: last_id=line.id res[obj.id]=last_id return res _fields={ "name": fields.Char("Name",required=True,search=True), 'date_start': fields.Date("From",required=True), 'date_stop': fields.Date("To",required=True), 'nmonth': fields.Integer("Count",required=True), 'lines': fields.One2Many("clinic.period.line","period_id", "Lines"), 'last_period_id': fields.Many2One("clinic.period.line","Last Period",function="_get_last_period"), } def _get_start(self,context={}): year=time.strftime("%Y") return '%s-01-01'%year def _get_stop(self,context={}): year=time.strftime("%Y") return '%s-01-30'%year _defaults={ 'name': lambda *a: time.strftime("%Y"), 'date_start': _get_start, 'date_stop': _get_stop, 'nmonth': 12, } def gen_period(self,ids,context={}): obj=self.browse(ids)[0] for line in obj.lines: line.delete() year,month,day=[int(x) for x in obj.date_start.split("-")] tyear,tmonth,tday=[int(x) for x in obj.date_stop.split("-")] lines=[] 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 lines.append(('create',{ 'date_start': '%s-%s-%s'%(year,_zfill(month),_zfill(day)), 'date_stop': '%s-%s-%s'%(tyear,_zfill(tmonth),_zfill(tday)), })) if month==tmonth: tmonth+=1 month=tmonth year=tyear tmonth+=1 obj.write({ 'lines': lines, }) return { 'next': { 'name': 'clinic_period', 'mode': 'form', 'active_id': obj.id, }, 'flash': 'Generate successfully', } Period.register()