clinic/netforce_clinic/models/report_receipt_summary.py

176 lines
6.4 KiB
Python
Raw Permalink Normal View History

2016-08-18 06:33:09 +00:00
import time
from calendar import monthrange
from netforce.model import Model, fields, get_model
from netforce.access import get_active_company
class ReportReceiptSummary(Model):
_name='clinic.report.receipt.summary'
_transient=True
_fields={
"date": fields.Date("Month", required=True),
"date_from": fields.Date("From", required=True),
"date_to": fields.Date("To", required=True),
'branch_id': fields.Many2One("clinic.branch","Branch"),
'department_id': fields.Many2One("clinic.department","Departments"),
}
def default_get(self,field_names=None,context={},**kw):
defaults=context.get("defaults",{})
date=defaults.get('date',time.strftime("%Y-%m-%d"))
year,month=time.strftime("%Y-%m").split("-")
weekday, total_day=monthrange(int(year), int(month))
date_from=defaults.get('date_from','%s-%s-01'%(year,month))
date_to=defaults.get('date_to',"%s-%s-%s"%(year,month,total_day))
#date_from=defaults.get('date',date)
#date_to=defaults.get('date',date)
########### find branch
branch_id=defaults.get('branch_id',None)
if branch_id:
branch_id=int(branch_id)
department_id=defaults.get('department_id',None)
if department_id:
department_id=int(department_id)
select_dpt=get_model('select.company').get_select()
if select_dpt:
if not branch_id:
branch_id=select_dpt['branch_id']
if not department_id:
if select_dpt.get('department_ids'):
department_id=select_dpt['department_ids'][0]
else:
department_id=select_dpt['department_id']
res={
'date': date,
'date_from': date_from,
'date_to': date_to,
'branch_id': branch_id,
'department_id': department_id,
}
return res
def get_report_data(self,ids,context={}):
company_id=get_active_company()
company=get_model("company").browse(company_id)
################ default values ###############
defaults=self.default_get(context=context)
date=defaults.get('date',time.strftime("%Y-%m-%d"))
year=int(date[0:4])
crr_month=int(date[5:7])
weekday, crr_total_day=monthrange(year, crr_month)
date_from=defaults.get('date_from',date)
date_to=defaults.get('date_to',date)
branch_id=defaults.get("branch_id",None)
department_id=defaults.get("department_id",None)
################# when click run report #############
if ids:
obj=self.browse(ids)[0]
date_from=obj.date_from
date_to=obj.date_to
branch_id=obj.branch_id.id
department_id=obj.department_id.id
#################### condition to get shop rd ########
dom=[
['date', '>=', date_from],
['date', '<=', date_to],
]
if branch_id:
dom.append(['branch_id','=',branch_id])
branch=get_model("clinic.branch").browse(branch_id)
if department_id:
dom.append(['department_id','=',department_id])
department=get_model("clinic.department").browse(department_id)
lines=[]
total_amount=0
############### from HD Case ###################
for hdcase in get_model('clinic.hd.case').search_browse(dom):
patient=hdcase.patient_id
line_vals={
'patient_name': patient.name,
'patient_id': patient.id,
'is_rd_shop': False,
'id': hdcase.id,
}
for payment in hdcase.payments:
items=[]
for line in payment.lines:
items.append(line.description)
list_item='\n'.join([item for item in items if item])
line_vals.update({
'payment_id': payment.id,
'date': payment.date,
'number': payment.number,
'ref': payment.ref,
'list_item': list_item,
'amount_total': payment.amount_total,
'date_send': '', # ??
})
total_amount+=payment.amount_total
lines.append(line_vals)
############### from RD Shop ###################
for shop in get_model('clinic.shop').search_browse(dom):
patient=shop.patient_id
items=[]
for line in shop.lines:
prod=line.product_id
if prod:
items.append(prod.name)
else:
items.append(line.description)
list_item='\n'.join([item for item in items if item])
line_vals={
'is_rd_shop': True,
'patient_name': patient.name,
'patient_id': patient.id,
'date': shop.date,
'number': shop.number,
'id': shop.id,
'ref': shop.ref,
'list_item': list_item,
'amount_total': shop.sub_total,
'date_send': '', # ??
}
total_amount+=shop.sub_total
lines.append(line_vals)
data={
'title': 'Receipt Summary',
'date_from': date_from,
'date_to': date_to,
'parent_company_name': company.parent_id and company.parent_id.name or "",
'company_name': company.name,
'branch_name': branch.name,
'department_name': department.name,
'lines': sorted(lines, key=lambda line: line['date']),
'total_amount': total_amount,
}
return data
def onchange_branch(self,context={}):
data=context['data']
data['department_id']=None
return data
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
def onchange_datefrom(self,context={}):
data=context['data']
data['date_to']=data['date_from']
return data
ReportReceiptSummary.register()