2015-08-06 01:37:48 +00:00
|
|
|
import csv
|
|
|
|
import datetime
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
from netforce.model import Model, fields, get_model
|
|
|
|
from netforce.utils import get_file_path
|
|
|
|
|
|
|
|
class ConvBal(Model):
|
|
|
|
_inherit="conv.bal"
|
|
|
|
|
|
|
|
def create_sale_invoices(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
desc="Conversion balance %s"%obj.date
|
|
|
|
for inv in obj.sale_invoices:
|
|
|
|
vals={
|
|
|
|
"type": "out",
|
|
|
|
"inv_type": inv.amount_due>=0 and "invoice" or "credit",
|
|
|
|
"partner_id": inv.contact_id.id,
|
|
|
|
"date": inv.date,
|
|
|
|
"due_date": inv.due_date,
|
|
|
|
"number": inv.number,
|
|
|
|
"ref": inv.ref,
|
|
|
|
"memo": desc,
|
|
|
|
"lines": [],
|
|
|
|
"state": "waiting_payment",
|
|
|
|
"account_id": inv.account_id.id,
|
|
|
|
"reconcile_move_line_id": inv.move_line_id.id,
|
|
|
|
"currency_id": inv.account_id.currency_id.id,
|
|
|
|
"currency_rate": inv.amount_due/inv.amount_cur if inv.amount_cur else None,
|
2015-08-10 14:38:19 +00:00
|
|
|
"department_id": inv.department_id.id,
|
2015-08-06 01:37:48 +00:00
|
|
|
}
|
|
|
|
line_vals={
|
|
|
|
"description": desc,
|
|
|
|
"amount": abs(inv.amount_cur or inv.amount_due),
|
2015-08-10 14:38:19 +00:00
|
|
|
"track_id": inv.track_id.id,
|
2015-08-06 01:37:48 +00:00
|
|
|
}
|
|
|
|
vals["lines"].append(("create",line_vals))
|
|
|
|
res=get_model("account.invoice").search([["number","=",inv.number]])
|
|
|
|
if res:
|
|
|
|
inv2_id=res[0]
|
|
|
|
inv2=get_model("account.invoice").browse(inv2_id)
|
|
|
|
#ratchawat
|
|
|
|
inv2.write({
|
|
|
|
'lines': vals['lines'],
|
|
|
|
})
|
|
|
|
#if abs(inv2.amount_total)-abs(inv.amount_due)>0.001: # XXX
|
|
|
|
#raise Exception("Failed to update invoice %s: different amount"%inv.number)
|
2015-08-07 04:15:46 +00:00
|
|
|
if inv2.state=="draft":
|
|
|
|
raise Exception("Failed to update invoice %s: invalid state"%inv.number)
|
|
|
|
inv2.write({
|
|
|
|
"move_id": obj.move_id.id,
|
|
|
|
"reconcile_move_line_id": inv.move_line_id.id,
|
|
|
|
})
|
2015-08-06 01:37:48 +00:00
|
|
|
else:
|
|
|
|
get_model("account.invoice").create(vals)
|
|
|
|
|
2015-08-27 01:51:30 +00:00
|
|
|
def _import_sale_file(self,ids,context):
|
2015-08-06 01:37:48 +00:00
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
path=get_file_path(obj.file)
|
|
|
|
data=open(path).read()
|
|
|
|
rd=csv.reader(StringIO(data))
|
|
|
|
headers=next(rd)
|
|
|
|
headers=[h.strip() for h in headers]
|
|
|
|
del_ids=get_model("conv.sale.invoice").search([["conv_id","=",obj.id]])
|
|
|
|
get_model("conv.sale.invoice").delete(del_ids)
|
|
|
|
for row in rd:
|
2015-08-07 04:15:46 +00:00
|
|
|
#print("row",row)
|
2015-08-06 01:37:48 +00:00
|
|
|
row+="," #XXX append blank column for Amount Cur
|
|
|
|
line=dict(zip(headers,row))
|
2015-08-07 04:15:46 +00:00
|
|
|
#print("line",line)
|
2015-08-10 14:38:19 +00:00
|
|
|
track_id=None
|
|
|
|
department_id=None
|
|
|
|
department_name=line.get("Department")
|
|
|
|
if department_name:
|
|
|
|
for dpt in get_model("clinic.department").search_browse([['name','=',department_name]]):
|
|
|
|
department_id=dpt.id
|
|
|
|
track_id=dpt.branch_id.track_id.id
|
2015-08-06 01:37:48 +00:00
|
|
|
if not line.get("Number"):
|
|
|
|
continue
|
|
|
|
number=line["Number"].strip()
|
|
|
|
if not number:
|
|
|
|
continue
|
2015-08-10 14:38:19 +00:00
|
|
|
|
|
|
|
##XXX remove invoice
|
|
|
|
#for inv in get_model('account.invoice').search_browse([['number','=', number]]):
|
|
|
|
#print('number --> ', number)
|
|
|
|
#inv.to_draft()
|
|
|
|
#inv.delete()
|
|
|
|
#continue
|
|
|
|
|
2015-08-06 01:37:48 +00:00
|
|
|
ref=line["Reference"].strip()
|
|
|
|
contact_name=line["Contact"].strip()
|
|
|
|
res=get_model("partner").search([["name","=",contact_name]])
|
|
|
|
if not res:
|
|
|
|
raise Exception("Contact not found: '%s'"%contact_name)
|
|
|
|
contact_id=res[0]
|
|
|
|
date=datetime.datetime.strptime(line["Date"].strip(),obj.date_fmt).strftime("%Y-%m-%d")
|
|
|
|
due_date=datetime.datetime.strptime(line["Due Date"].strip(),obj.date_fmt).strftime("%Y-%m-%d")
|
|
|
|
amount_due=float(line["Amount Due"].strip().replace(",","") or 0)
|
|
|
|
acc_code=line["Account"].strip()
|
|
|
|
amount_cur=float(line["Amount Cur"].strip().replace(",","") or 0)
|
|
|
|
acc_codes=acc_code.split(",")
|
2015-08-07 04:15:46 +00:00
|
|
|
amts=[] #XXX
|
|
|
|
for m in line['Memo'].split(","):
|
|
|
|
amt=m.split(":")[1]
|
|
|
|
amt=amt.strip()
|
|
|
|
if amt:
|
|
|
|
amt=float(amt)
|
|
|
|
amts.append(amt)
|
2015-08-06 01:37:48 +00:00
|
|
|
if len(acc_codes) > 1:
|
|
|
|
count=0
|
2015-08-07 04:15:46 +00:00
|
|
|
print("*"*80)
|
2015-08-06 01:37:48 +00:00
|
|
|
for acc_code in acc_codes:
|
|
|
|
acc_code=acc_code.strip()
|
|
|
|
res=get_model("account.account").search([["code","=",acc_code]])
|
|
|
|
if not res:
|
|
|
|
raise Exception("Account code not found: %s"%acc_code)
|
|
|
|
acc_id=res[0]
|
|
|
|
amount_due=amts[count]
|
|
|
|
vals={
|
|
|
|
"conv_id": obj.id,
|
|
|
|
"number": number,
|
|
|
|
"ref": ref,
|
|
|
|
"contact_id": contact_id,
|
|
|
|
"date": date,
|
|
|
|
"due_date": due_date,
|
|
|
|
"amount_due": amount_due,
|
|
|
|
"account_id": acc_id,
|
2015-08-07 04:15:46 +00:00
|
|
|
"amount_cur": amount_due, #XXX
|
2015-08-10 14:38:19 +00:00
|
|
|
'track_id': track_id,
|
|
|
|
'department_id': department_id,
|
2015-08-06 01:37:48 +00:00
|
|
|
}
|
|
|
|
get_model("conv.sale.invoice").create(vals)
|
|
|
|
count+=1
|
2015-08-07 04:15:46 +00:00
|
|
|
elif len(amts) >= 1 and len(acc_codes)<=1:
|
|
|
|
acc_code=acc_codes[0].strip()
|
|
|
|
res=get_model("account.account").search([["code","=",acc_code]])
|
|
|
|
if not res:
|
|
|
|
raise Exception("Account code not found: %s"%acc_code)
|
|
|
|
acc_id=res[0]
|
|
|
|
for amt in amts:
|
|
|
|
amount_due=amt
|
|
|
|
vals={
|
|
|
|
"conv_id": obj.id,
|
|
|
|
"number": number,
|
|
|
|
"ref": ref,
|
|
|
|
"contact_id": contact_id,
|
|
|
|
"date": date,
|
|
|
|
"due_date": due_date,
|
|
|
|
"amount_due": amount_due,
|
|
|
|
"account_id": acc_id,
|
|
|
|
"amount_cur": amount_due, #XXX
|
2015-08-10 14:38:19 +00:00
|
|
|
'track_id': track_id,
|
|
|
|
'department_id': department_id,
|
2015-08-07 04:15:46 +00:00
|
|
|
}
|
|
|
|
get_model("conv.sale.invoice").create(vals)
|
2015-08-06 01:37:48 +00:00
|
|
|
else:
|
|
|
|
res=get_model("account.account").search([["code","=",acc_code]])
|
|
|
|
if not res:
|
|
|
|
raise Exception("Account code not found: %s"%acc_code)
|
|
|
|
acc_id=res[0]
|
|
|
|
vals={
|
|
|
|
"conv_id": obj.id,
|
|
|
|
"number": number,
|
|
|
|
"ref": ref,
|
|
|
|
"contact_id": contact_id,
|
|
|
|
"date": date,
|
|
|
|
"due_date": due_date,
|
|
|
|
"amount_due": amount_due,
|
|
|
|
"account_id": acc_id,
|
|
|
|
"amount_cur": amount_cur,
|
2015-08-10 14:38:19 +00:00
|
|
|
'track_id': track_id,
|
|
|
|
'department_id': department_id,
|
2015-08-06 01:37:48 +00:00
|
|
|
}
|
|
|
|
get_model("conv.sale.invoice").create(vals)
|
|
|
|
return {
|
|
|
|
"next": {
|
|
|
|
"name": "conv_bal",
|
|
|
|
"active_id": obj.id,
|
|
|
|
"view_xml": "conv_bal2",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConvBal.register()
|