2015-08-21 08:15:53 +00:00
|
|
|
import time
|
|
|
|
from calendar import monthrange
|
|
|
|
|
|
|
|
from netforce.model import Model, fields, get_model
|
2015-08-23 05:39:02 +00:00
|
|
|
from netforce.utils import get_file_path
|
|
|
|
|
|
|
|
from . import utils
|
2015-08-21 08:15:53 +00:00
|
|
|
|
|
|
|
class PaymentMatching(Model):
|
|
|
|
_name="clinic.payment.matching"
|
|
|
|
_string="Payment Matching"
|
|
|
|
_transient=True
|
|
|
|
|
|
|
|
|
|
|
|
_fields={
|
|
|
|
'name': fields.Char("Name"),
|
|
|
|
"date": fields.Date("Month"),
|
|
|
|
"date_from": fields.Date("From", required=True),
|
|
|
|
"date_to": fields.Date("To", required=True),
|
|
|
|
'file_id': fields.Many2One('document','File',domain=[['categ_id.code','=','MP']]),
|
|
|
|
"period_id": fields.Many2One("clinic.period.line","Period"),
|
|
|
|
'department_id': fields.Many2One("clinic.department","Department"),
|
|
|
|
'branch_id': fields.Many2One("clinic.branch","Branch"),
|
|
|
|
"inv_state": fields.Selection([("draft","Draft"),("waiting_approval","Waiting Approval"),("waiting_payment","Waiting Payment"),("paid","Paid"),("voided","Voided")],"Status"),
|
|
|
|
"view_type": fields.Selection([("invoice","Invoice"),("file","File")],"View Type"),
|
2015-08-23 05:39:02 +00:00
|
|
|
'patient_type_id': fields.Many2One("clinic.patient.type","Patient Type",required=True),
|
|
|
|
'pcode': fields.Char("Code",required=True),
|
|
|
|
'hcode_id': fields.Many2One("clinic.hospital","HCode"),
|
2015-08-21 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def _get_date_from(self,context={}):
|
|
|
|
year,month,day=time.strftime("%Y-%m-%d").split("-")
|
|
|
|
return '%s-%s-%s'%(year,month,day)
|
|
|
|
|
|
|
|
def _get_date_to(self,context={}):
|
|
|
|
year,month,day=time.strftime("%Y-%m-%d").split("-")
|
|
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
|
|
return "%s-%s-%s"%(year,month,day)
|
|
|
|
|
|
|
|
def default_get(self,field_names=None,context={},**kw):
|
|
|
|
defaults=context.get("defaults",{})
|
|
|
|
date_from=defaults.get("date_from", self._get_date_from())
|
|
|
|
date_to=defaults.get("date_to", self._get_date_to())
|
|
|
|
yearnow=date_from.split("-")[0]
|
|
|
|
for period in get_model('clinic.period').search_browse([['name','=',yearnow]]):
|
|
|
|
for line in period.lines:
|
|
|
|
if line.state=='open':
|
|
|
|
period_id=line.id
|
|
|
|
date_from=line.date_start
|
|
|
|
date_to=line.date_stop
|
|
|
|
break
|
2015-08-23 05:39:02 +00:00
|
|
|
tids=get_model('clinic.patient.type').search([['default','=',True]])
|
|
|
|
patient_type_id=None
|
|
|
|
if tids:
|
|
|
|
patient_type_id=tids[0]
|
2015-08-21 08:15:53 +00:00
|
|
|
res={
|
|
|
|
'period_id': period_id,
|
|
|
|
'date': time.strftime("%Y-%m-%d"),
|
|
|
|
'date_from': date_from,
|
|
|
|
'date_to': date_to,
|
|
|
|
'inv_state': 'waiting_payment',
|
|
|
|
'view_type': 'invoice',
|
2015-08-23 05:39:02 +00:00
|
|
|
'patient_type_id': patient_type_id,
|
2015-08-21 08:15:53 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
|
|
|
|
def onchange_period(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
period_id=data['period_id']
|
|
|
|
period=get_model('clinic.period.line').browse(period_id)
|
|
|
|
data['date_from']=period.date_start
|
|
|
|
data['date_to']=period.date_stop
|
|
|
|
return data
|
|
|
|
|
2015-08-23 05:39:02 +00:00
|
|
|
def onchange_ptype(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
type_id=data['patient_type_id']
|
|
|
|
if type_id:
|
|
|
|
t=get_model('clinic.patient.type').browse(type_id)
|
|
|
|
data['pcode']=t.code or ""
|
|
|
|
return data
|
|
|
|
|
|
|
|
def get_line(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
if not obj.file_id.file:
|
|
|
|
raise Exception("File not found!")
|
|
|
|
fname=obj.file_id.file
|
|
|
|
if obj.pcode=='SSO':
|
|
|
|
hcode=obj.hcode_id.code or ""
|
|
|
|
if not hcode:
|
|
|
|
raise Exception("Wrong HCode")
|
|
|
|
try:
|
|
|
|
n,sf=fname.split(".")
|
|
|
|
except Exception as e:
|
|
|
|
print("ERROR: wrong file ", e)
|
|
|
|
|
|
|
|
if sf not in ('xls','xlsx'):
|
|
|
|
raise Exception("File should be xls or xlsx")
|
|
|
|
fpath=get_file_path(fname)
|
|
|
|
lines=utils.read_excel(fpath,show_datetime=True)
|
|
|
|
|
|
|
|
elif obj.pcode=='UC':
|
|
|
|
fpath=get_file_path(fname)
|
|
|
|
node='HDBills'
|
|
|
|
lines=utils.read_xml(fpath,node=node)
|
|
|
|
hcode=fname.split("_")[0]
|
|
|
|
else:
|
|
|
|
raise Exception("Type %s is not support"%obj.pcode or "")
|
|
|
|
|
|
|
|
if not lines:
|
|
|
|
raise Exception("No data to match")
|
|
|
|
return lines
|
|
|
|
|
2015-08-21 08:15:53 +00:00
|
|
|
def get_report_data(self,ids,context={}):
|
|
|
|
defaults=self.default_get(context=context)
|
|
|
|
print('defaults ', defaults)
|
|
|
|
date_from=defaults.get('date_from')
|
|
|
|
date_to=defaults.get('date_to')
|
|
|
|
branch_id=None
|
|
|
|
department_id=None
|
|
|
|
inv_state=defaults.get('inv_state')
|
|
|
|
view_type=defaults.get('view_type')
|
|
|
|
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
|
|
|
|
inv_state=obj.inv_state
|
|
|
|
view_type=obj.view_type
|
|
|
|
lines=[]
|
|
|
|
dom=[
|
|
|
|
['date','>=',date_from],
|
|
|
|
['date','<=',date_to],
|
|
|
|
['state','=',inv_state],
|
|
|
|
]
|
|
|
|
if branch_id and not department_id:
|
|
|
|
dom.append(['department_id.branch_id','=',branch_id])
|
|
|
|
elif department_id:
|
|
|
|
dom.append(['department_id','=',department_id])
|
|
|
|
if view_type=='invoice':
|
|
|
|
field_names=['date','number','amount_due']
|
|
|
|
for inv in get_model('account.invoice').search_read(dom,field_names):
|
|
|
|
vals={}
|
|
|
|
for field_name in field_names:
|
|
|
|
vals[field_name]=inv[field_name],
|
|
|
|
lines.append(vals)
|
|
|
|
data={
|
|
|
|
'lines': lines,
|
|
|
|
'date_from': date_from,
|
|
|
|
'date_to': date_to,
|
|
|
|
'total_inv': len(lines),
|
|
|
|
'view_type': view_type,
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
|
|
|
|
PaymentMatching.register()
|