From ecbb73c7acad22c33d9a2154a83c786258384bec Mon Sep 17 00:00:00 2001 From: "watcha.h" Date: Tue, 16 Dec 2014 00:46:43 +0700 Subject: [PATCH] gen period --- .../layouts/clinic_patient_type_form.xml | 2 +- .../layouts/clinic_period_form.xml | 4 +- netforce_clinic/models/period.py | 55 ++++++++++++++----- netforce_clinic/models/period_line.py | 21 +++++-- 4 files changed, 61 insertions(+), 21 deletions(-) diff --git a/netforce_clinic/layouts/clinic_patient_type_form.xml b/netforce_clinic/layouts/clinic_patient_type_form.xml index 1d77f04..9dcfc8d 100644 --- a/netforce_clinic/layouts/clinic_patient_type_form.xml +++ b/netforce_clinic/layouts/clinic_patient_type_form.xml @@ -1,7 +1,7 @@
diff --git a/netforce_clinic/layouts/clinic_period_form.xml b/netforce_clinic/layouts/clinic_period_form.xml index 379d896..855422a 100644 --- a/netforce_clinic/layouts/clinic_period_form.xml +++ b/netforce_clinic/layouts/clinic_period_form.xml @@ -7,12 +7,14 @@ - + + + diff --git a/netforce_clinic/models/period.py b/netforce_clinic/models/period.py index 108ee34..8829320 100644 --- a/netforce_clinic/models/period.py +++ b/netforce_clinic/models/period.py @@ -1,5 +1,6 @@ -from datetime import datetime, timedelta import time +from datetime import datetime, timedelta +from calendar import monthrange from netforce.model import Model, fields @@ -9,9 +10,10 @@ class Period(Model): _fields={ "name": fields.Char("Name",required=True,search=True), - 'date_start': fields.Date("Start Date",required=True), - 'duration': fields.Integer("Day in Period",required=True), - 'nmonth': fields.Integer("Number of Month",required=True), + '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), 'lines': fields.One2Many("clinic.period.line","period_id", "Lines"), } @@ -19,10 +21,14 @@ class Period(Model): 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, - 'duration': 30, + 'date_stop': _get_stop, 'nmonth': 12, } @@ -30,21 +36,40 @@ class Period(Model): obj=self.browse(ids)[0] for line in obj.lines: line.delete() - fmt="%Y-%m-%d" - start=obj.date_start + 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=[] - for i in range(obj.nmonth): - start=datetime.strptime(start,fmt) - stop=start+timedelta(days=obj.duration) + 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': start.strftime(fmt), - 'date_stop': stop.strftime(fmt), + 'date_start': '%s-%s-%s'%(year,_zfill(month),_zfill(day)), + 'date_stop': '%s-%s-%s'%(tyear,_zfill(tmonth),_zfill(tday)), })) - print(i+1, ' ', start.strftime(fmt), ' ', stop.strftime(fmt)) - stop=stop+timedelta(days=1) - start=stop.strftime(fmt) + 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() diff --git a/netforce_clinic/models/period_line.py b/netforce_clinic/models/period_line.py index 11bbb90..e14084c 100644 --- a/netforce_clinic/models/period_line.py +++ b/netforce_clinic/models/period_line.py @@ -1,20 +1,33 @@ +from datetime import datetime + from netforce.model import Model, fields class PeriodLine(Model): _name="clinic.period.line" _string="Period Line" + + def _get_total(self,ids,context={}): + res={} + fmt='%Y-%m-%d' + for obj in self.browse(ids): + start=datetime.strptime(obj.date_start,fmt) + stop=datetime.strptime(obj.date_stop,fmt) + days=(stop-start).days + res[obj.id]=days + return res _fields={ "period_id": fields.Many2One("clinic.period", "Period"), - #'date_start': fields.Date("Date Start"), - #'date_stop': fields.Date("Date Stop"), - 'date_start': fields.Char("Date Start"), - 'date_stop': fields.Char("Date Stop"), + 'date_start': fields.Date("Date Start"), + 'date_stop': fields.Date("Date Stop"), + 'day_total': fields.Integer("Total Day",function="_get_total"), 'state': fields.Selection([['draft','Draft'],['done','Done']],"State"), + 'close': fields.Boolean("Close"), } _defaults={ 'state': 'draft', + 'close': False, } PeriodLine.register()