256 lines
8.4 KiB
Python
256 lines
8.4 KiB
Python
import time
|
|
import xlrd
|
|
from datetime import datetime
|
|
|
|
from netforce.model import Model, fields, get_model
|
|
from netforce.access import get_active_company
|
|
from netforce.access import get_active_user, set_active_user
|
|
from netforce.utils import get_file_path
|
|
from netforce.utils import get_data_path
|
|
|
|
class JE(Model):
|
|
_name="clinic.je"
|
|
_string="Import Journal Entry"
|
|
|
|
_fields={
|
|
"number": fields.Char("Number"),
|
|
"name": fields.Char("Description"),
|
|
"date_import": fields.DateTime("Date Import"),
|
|
"type": fields.Selection([("mg","Medical Government"),("sc","Social Security"),("nhso","NHSO (30฿)")],"Type"),
|
|
'file': fields.File("File"),
|
|
'payment_id': fields.Many2One("account.payment","Payment"),
|
|
'lines': fields.One2Many("clinic.input.line","je_id", "Input Line"),
|
|
'input_data': fields.One2Many("clinic.input.data","je_id", "Input Data"),
|
|
'sheets': fields.One2Many("clinic.file.sheet","je_id", "Sheets"),
|
|
'columns': fields.One2Many("clinic.file.column","je_id", "Columns"),
|
|
"state": fields.Selection([("draft","Draft"),('sheet_loaded','Sheet Loaded'),("fail","Fail"),("done","Done")],"Status",required=True),
|
|
'description': fields.Text("Description")
|
|
}
|
|
|
|
def _get_number(self,context={}):
|
|
while 1:
|
|
seq_id=get_model("sequence").find_sequence(name="Clinic Import JE")
|
|
num=get_model("sequence").get_next_number(seq_id,context=context)
|
|
if not num:
|
|
return None
|
|
user_id=get_active_user()
|
|
set_active_user(1)
|
|
res=self.search([["number","=",num]])
|
|
set_active_user(user_id)
|
|
if not res:
|
|
return num
|
|
get_model("sequence").increment_number(seq_id,context=context)
|
|
|
|
def _get_name(self,context={}):
|
|
timenow=time.strftime("%Y-%m-%d")
|
|
return 'Import Journal Entry - %s'%timenow
|
|
|
|
_defaults={
|
|
'date_import': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"),
|
|
'number': _get_number,
|
|
'name': _get_name,
|
|
'state': 'draft',
|
|
}
|
|
|
|
def load_sheet(self,ids,context={}):
|
|
obj=self.browse(ids[0])
|
|
fname=obj.file
|
|
if not fname:
|
|
raise Exception("Please select file!")
|
|
fpath=get_file_path(fname)
|
|
suffix=fpath.split(".")[-1]
|
|
if suffix not in ('xls', 'xlsx'):
|
|
raise Exception("ERROR : file support only xls, xlsx")
|
|
|
|
wb=xlrd.open_workbook(fpath)
|
|
sheets=wb.sheet_names()
|
|
index=0
|
|
# XXX clear old sheet
|
|
for sheet in obj.sheets:
|
|
sheet.delete()
|
|
|
|
vals={
|
|
'sheets': [],
|
|
}
|
|
|
|
for sheet in sheets:
|
|
line={
|
|
'index': index,
|
|
'name': sheet,
|
|
}
|
|
vals["sheets"].append(("create",line))
|
|
vals['state']='sheet_loaded'
|
|
obj.write(vals)
|
|
return {
|
|
'next': {
|
|
'name': "clinic_je",
|
|
'mode': 'form',
|
|
'active_id': obj.id,
|
|
},
|
|
'flash': "Load sheet successfully",
|
|
}
|
|
|
|
def load_data(self,ids,context={}):
|
|
obj=self.browse(ids[0])
|
|
if not obj.file:
|
|
raise Exception("Please select some file!")
|
|
|
|
if not obj.sheets:
|
|
raise Exception("Please load sheet!")
|
|
indexes=[]
|
|
for sheet in obj.sheets:
|
|
if sheet.select=='yes':
|
|
indexes.append((sheet.index,sheet.name))
|
|
if not indexes:
|
|
raise Exception("Please select some sheet!")
|
|
if len(indexes) > 1:
|
|
raise Exception("Only one sheet can select!")
|
|
sheet_name=indexes[0][1]
|
|
fname=obj.file
|
|
fpath=get_file_path(fname)
|
|
wb=xlrd.open_workbook(fpath)
|
|
ws=wb.sheet_by_name(sheet_name)
|
|
num_rows = ws.nrows - 1
|
|
curr_row = -1
|
|
input_data=[]
|
|
while curr_row < num_rows:
|
|
curr_row += 1
|
|
# skip header
|
|
if curr_row <=1:
|
|
continue
|
|
#row=ws.row(curr_row)
|
|
# XXX
|
|
doc_date=ws.cell_value(curr_row, 0)
|
|
doc_date=datetime(*xlrd.xldate_as_tuple(doc_date, wb.datemode))
|
|
doc_date=doc_date.strftime("%Y-%m-%d")
|
|
data={
|
|
'doc_date':doc_date,
|
|
'name':ws.cell_value(curr_row, 1),
|
|
'hn':ws.cell_value(curr_row, 2),
|
|
'amount':ws.cell_value(curr_row, 3),
|
|
}
|
|
input_data.append(('create',data))
|
|
data_ids=[data.id for data in obj.input_data]
|
|
get_model("clinic.input.data").delete(data_ids)
|
|
obj.write({
|
|
'input_data': input_data,
|
|
'description': 'see preview',
|
|
})
|
|
|
|
|
|
def clear_data(self,ids,context={}):
|
|
obj=self.browse(ids[0])
|
|
data_ids=[data.id for data in obj.input_data]
|
|
get_model("clinic.input.data").delete(data_ids)
|
|
|
|
def find_invoice(self,ids,context={}):
|
|
obj=self.browse(ids[0])
|
|
partner_id=None
|
|
setting=get_model("clinic.setting").browse(1)
|
|
if obj.type=='sc':
|
|
partner_id=setting.sc_partner_id.id
|
|
elif obj.type=='mg':
|
|
partner_id=setting.mg_partner_id.id
|
|
elif obj.type=='nhso':
|
|
partner_id=setting.nhso_partner_id.id
|
|
if not partner_id:
|
|
raise Exception("No partner")
|
|
|
|
# XXX
|
|
if obj.lines:
|
|
for line in obj.lines:
|
|
line.delete()
|
|
dom=[]
|
|
dom.append(['state','=','waiting_payment'])
|
|
dom.append(['partner_id','=',partner_id])
|
|
lines=[]
|
|
for invoice in get_model("account.invoice").search_browse(dom):
|
|
print(invoice.id, " ", invoice.number, " ", invoice.amount_total),
|
|
line={
|
|
'invoice_id': invoice.id,
|
|
'amount': invoice.amount_total,
|
|
}
|
|
lines.append(('create',line))
|
|
obj.write({
|
|
'lines': lines,
|
|
})
|
|
return {
|
|
'next': {
|
|
'name': 'clinic_je',
|
|
'mode': 'form',
|
|
'active_id': obj.id,
|
|
},
|
|
'flash':'Load invoice OK!',
|
|
}
|
|
|
|
def post_payment(self,ids,context={}):
|
|
obj=self.browse(ids[0])
|
|
if not obj.lines:
|
|
raise Exception("No invoice to post")
|
|
|
|
def onchange_sheet(self,context={}):
|
|
data=context["data"]
|
|
path=context["path"]
|
|
line=get_data_path(data,path,parent=True)
|
|
for sheet in data['sheets']:
|
|
if sheet['name']!=line['name']:
|
|
sheet['select']=None
|
|
return data
|
|
|
|
def make_payment(self,ids,context={}):
|
|
obj=self.browse(ids[0])
|
|
if obj.payment_id:
|
|
if obj.lines:
|
|
if obj.payment_id.invoice_lines:
|
|
return
|
|
vals={
|
|
'invoice_lines':[],
|
|
}
|
|
for line in obj.lines:
|
|
line={
|
|
'invoice_id': line.invoice_id.id,
|
|
'amount': line.amount,
|
|
}
|
|
vals['invoice_lines'].append(('create',line))
|
|
obj.payment_id.write(vals)
|
|
print("add invoice to payment")
|
|
return
|
|
company_id=get_active_company()
|
|
timenow=time.strftime("%Y-%m-%d")
|
|
setting=get_model("clinic.setting").browse(1)
|
|
account_id=None
|
|
partner_id=None
|
|
if obj.type=='sc':
|
|
account_id=setting.ar_sc_id.id
|
|
partner_id=setting.sc_partner_id.id
|
|
elif obj.type=='mg':
|
|
account_id=setting.ar_mg_id.id
|
|
partner_id=setting.mg_partner_id.id
|
|
elif obj.type=='nhso':
|
|
account_id=setting.ar_nhso_id.id
|
|
partner_id=setting.nhso_partner_id.id
|
|
vals={
|
|
"partner_id": partner_id,
|
|
"company_id": company_id,
|
|
"type": "in",
|
|
"pay_type": "invoice",
|
|
"date": timenow,
|
|
"account_id": account_id,
|
|
'lines': [], # TODO find invoice matching
|
|
}
|
|
payment_id=get_model("account.payment").create(vals,context={'type': 'in'})
|
|
obj.write({
|
|
'payment_id': payment_id,
|
|
})
|
|
|
|
return {
|
|
'next': {
|
|
'name': 'clinic_je',
|
|
'mode': 'form',
|
|
'active_id': obj.id,
|
|
},
|
|
'flash': "Make payment ok !",
|
|
}
|
|
|
|
JE.register()
|