error fixes in voucher import tool

This commit is contained in:
Nabin Hait 2012-11-07 17:38:39 +05:30
parent b444ba73e9
commit 4fecbc1b3d

View File

@ -89,77 +89,86 @@ def import_vouchers(common_values, data, start_idx, import_type):
"against_journal_voucher:against_jv"], d, detail.fields) "against_journal_voucher:against_jv"], d, detail.fields)
webnotes.conn.commit() webnotes.conn.commit()
for i in xrange(len(data)): try:
d = data[i][0] webnotes.conn.begin()
jv = webnotes.DictObj() for i in xrange(len(data)):
d = data[i][0]
try: if import_type == "Voucher Import: Two Accounts" and flt(d.get("amount")) == 0:
d.posting_date = parse_date(d.posting_date) webnotes.message_log = ["Amount not specified"]
d.due_date = d.due_date and parse_date(d.due_date) or None raise Exception
elif import_type == "Voucher Import: Multiple Accounts" and \
if d.ref_number: (flt(d.get("total_debit")) == 0 or flt(d.get("total_credit")) == 0):
if not d.ref_date: webnotes.message_log = ["Total Debit and Total Credit amount can not be zero"]
raise webnotes.ValidationError, \ raise Exception
"""Ref Date is Mandatory if Ref Number is specified""" else:
d.ref_date = parse_date(d.ref_date) d.posting_date = parse_date(d.posting_date)
d.due_date = d.due_date and parse_date(d.due_date) or None
d.company = common_values.company if d.ref_number:
if not d.ref_date:
raise webnotes.ValidationError, \
"""Ref Date is Mandatory if Ref Number is specified"""
d.ref_date = parse_date(d.ref_date)
d.company = common_values.company
jv = Document("Journal Voucher") jv = Document("Journal Voucher")
map_fields(["voucher_type", "posting_date", "naming_series", "remarks:user_remark", map_fields(["voucher_type", "posting_date", "naming_series",
"ref_number:cheque_no", "ref_date:cheque_date", "is_opening", "remarks:user_remark", "ref_number:cheque_no", "ref_date:cheque_date",
"amount:total_debit", "amount:total_credit", "due_date", "company"], d, jv.fields) "is_opening", "due_date", "company"], d, jv.fields)
jv.fiscal_year = get_fiscal_year(jv.posting_date)[0] jv.fiscal_year = get_fiscal_year(jv.posting_date)[0]
details = [] details = []
if import_type == "Voucher Import: Two Accounts": if import_type == "Voucher Import: Two Accounts":
detail1 = Document("Journal Voucher Detail") map_fields(["amount:total_debit", "amount:total_credit"], d, jv.fields)
detail1.parent = True
detail1.parentfield = "entries" detail1 = Document("Journal Voucher Detail")
map_fields(["debit_account:account","amount:debit"], d, detail1.fields) detail1.parent = True
apply_cost_center_and_against_invoice(detail1, d) detail1.parentfield = "entries"
map_fields(["debit_account:account","amount:debit"], d, detail1.fields)
apply_cost_center_and_against_invoice(detail1, d)
detail2 = Document("Journal Voucher Detail") detail2 = Document("Journal Voucher Detail")
detail2.parent = True detail2.parent = True
detail2.parentfield = "entries" detail2.parentfield = "entries"
map_fields(["credit_account:account","amount:credit"], d, detail2.fields) map_fields(["credit_account:account","amount:credit"], d, detail2.fields)
apply_cost_center_and_against_invoice(detail2, d) apply_cost_center_and_against_invoice(detail2, d)
details = [detail1, detail2] details = [detail1, detail2]
elif import_type == "Voucher Import: Multiple Accounts": elif import_type == "Voucher Import: Multiple Accounts":
accounts = data[i][1] map_fields(["total_debit", "total_credit"], d, jv.fields)
for acc in accounts: accounts = data[i][1]
detail = Document("Journal Voucher Detail") for acc in accounts:
detail.parent = True detail = Document("Journal Voucher Detail")
detail.parentfield = "entries" detail.parent = True
detail.account = acc detail.parentfield = "entries"
detail.debit = flt(accounts[acc]) > 0 and flt(accounts[acc]) or 0 detail.account = acc
detail.credit = flt(accounts[acc]) < 0 and -1*flt(accounts[acc]) or 0 detail.debit = flt(accounts[acc]) > 0 and flt(accounts[acc]) or 0
apply_cost_center_and_against_invoice(detail, d) detail.credit = flt(accounts[acc]) < 0 and -1*flt(accounts[acc]) or 0
details.append(detail) apply_cost_center_and_against_invoice(detail, d)
details.append(detail)
if not details: if not details:
messages.append("""<p style='color: red'>No accounts found. webnotes.message_log = ["""No accounts found.
If you entered accounts correctly, please check template once</p>""") If you entered accounts correctly, please check template once"""]
return raise Exception
webnotes.conn.begin()
doclist = DocList([jv]+details) doclist = DocList([jv]+details)
doclist.submit() doclist.submit()
webnotes.conn.commit()
messages.append("""<p style='color: green'>[row #%s] messages.append("""<p style='color: green'>[row #%s]
<a href=\"#Form/Journal Voucher/%s\">%s</a> imported</p>""" \ <a href=\"#Form/Journal Voucher/%s\">%s</a> imported</p>""" \
% ((start_idx + 1) + i, jv.name, jv.name)) % ((start_idx + 1) + i, jv.name, jv.name))
webnotes.conn.commit()
except Exception, e: except Exception, e:
webnotes.conn.rollback() webnotes.conn.rollback()
err_msg = webnotes.message_log and webnotes.message_log[0] or unicode(e) err_msg = webnotes.message_log and webnotes.message_log[0] or unicode(e)
messages.append("<p style='color: red'>[row #%s] %s failed: %s</p>" \ messages.append("""<p style='color: red'>[row #%s] %s failed: %s</p>"""
% ((start_idx + 1) + i, jv.name or "", err_msg or "No message")) % ((start_idx + 1) + i, jv.name or "", err_msg or "No message"))
webnotes.errprint(webnotes.getTraceback()) messages.append("<p style='color: red'>All transactions rolled back</p>")
webnotes.errprint(webnotes.getTraceback())
webnotes.message_log = [] webnotes.message_log = []
return messages return messages
@ -192,12 +201,18 @@ def get_data(rows, company_abbr):
d[columns[cidx]] = r[cidx] d[columns[cidx]] = r[cidx]
if accounts: if accounts:
total = 0 total_debit = total_credit = 0
for acc_idx in xrange(len(accounts)): for acc_idx in xrange(len(accounts)):
col_idx = len(columns) + acc_idx col_idx = len(columns) + acc_idx
acc_dict[accounts[acc_idx]] = r[col_idx] if flt(r[col_idx]) != 0:
if flt(r[col_idx]) > 0: total += flt(r[col_idx]) acc_dict[accounts[acc_idx]] = r[col_idx]
d['amount'] = total if flt(r[col_idx]) > 0:
total_debit += flt(r[col_idx])
else:
total_credit += abs(flt(r[col_idx]))
d['total_debit'] = total_debit
d['total_credit'] = total_credit
data.append([d, acc_dict]) data.append([d, acc_dict])
@ -205,7 +220,7 @@ def get_data(rows, company_abbr):
if r[0]=="--------Data----------": if r[0]=="--------Data----------":
start_row = i+2 start_row = i+2
columns = [c.replace(" ", "_").lower() for c in rows[i+1] columns = [c.replace(" ", "_").lower() for c in rows[i+1]
if not c.endswith(company_abbr)] if not c.endswith(" - " + company_abbr)]
accounts = [c for c in rows[i+1] if c.endswith(company_abbr)] accounts = [c for c in rows[i+1] if c.endswith(" - " + company_abbr)]
return data, start_row_idx return data, start_row_idx