[voucher import tool][fix] fixes for handling exception
This commit is contained in:
parent
7742e2baed
commit
58c3348ca6
@ -1,7 +1,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import webnotes
|
import webnotes
|
||||||
from webnotes import _
|
from webnotes import _
|
||||||
from webnotes.utils import flt, comma_and
|
from webnotes.utils import flt, comma_and, cstr
|
||||||
import webnotes.defaults
|
import webnotes.defaults
|
||||||
|
|
||||||
@webnotes.whitelist()
|
@webnotes.whitelist()
|
||||||
@ -31,13 +31,12 @@ def get_template():
|
|||||||
"3. Naming Series Options: %(naming_options)s"
|
"3. Naming Series Options: %(naming_options)s"
|
||||||
"4. Voucher Type Options: %(voucher_type)s"%(extra_note)s
|
"4. Voucher Type Options: %(voucher_type)s"%(extra_note)s
|
||||||
"-------Common Values-----------"
|
"-------Common Values-----------"
|
||||||
"Company:","%(default_company)s"
|
"Company:",
|
||||||
"--------Data----------"
|
"--------Data----------"
|
||||||
%(columns)s
|
%(columns)s
|
||||||
''' % {
|
''' % {
|
||||||
"template_type": template_type,
|
"template_type": template_type,
|
||||||
"user_fmt": webnotes.defaults.get_global_default('date_format'),
|
"user_fmt": webnotes.defaults.get_global_default('date_format'),
|
||||||
"default_company": webnotes.conn.get_default("company"),
|
|
||||||
"naming_options": naming_options.replace("\n", ", "),
|
"naming_options": naming_options.replace("\n", ", "),
|
||||||
"voucher_type": voucher_type.replace("\n", ", "),
|
"voucher_type": voucher_type.replace("\n", ", "),
|
||||||
"extra_note": extra_note,
|
"extra_note": extra_note,
|
||||||
@ -49,14 +48,28 @@ def get_template():
|
|||||||
|
|
||||||
@webnotes.whitelist()
|
@webnotes.whitelist()
|
||||||
def upload():
|
def upload():
|
||||||
from webnotes.utils.datautils import read_csv_content_from_uploaded_file
|
messages = []
|
||||||
rows = read_csv_content_from_uploaded_file()
|
try:
|
||||||
|
from webnotes.utils.datautils import read_csv_content_from_uploaded_file
|
||||||
common_values = get_common_values(rows)
|
rows = read_csv_content_from_uploaded_file()
|
||||||
company_abbr = webnotes.conn.get_value("Company", common_values.company, "abbr")
|
|
||||||
data, start_idx = get_data(rows, company_abbr)
|
|
||||||
|
|
||||||
|
common_values = get_common_values(rows)
|
||||||
|
if not common_values.company:
|
||||||
|
webnotes.msgprint(_("Company is missing in csv file"), raise_exception=1)
|
||||||
|
|
||||||
|
company_abbr = webnotes.conn.get_value("Company", common_values.company, "abbr")
|
||||||
|
data, start_idx = get_data(rows, company_abbr)
|
||||||
|
except Exception, e:
|
||||||
|
err_msg = webnotes.message_log and "<br>".join(webnotes.message_log) or cstr(e)
|
||||||
|
messages.append("""<p style='color: red'>%s</p>""" % (err_msg or "No message"))
|
||||||
|
webnotes.errprint(webnotes.getTraceback())
|
||||||
|
webnotes.message_log = []
|
||||||
|
return messages
|
||||||
|
|
||||||
return import_vouchers(common_values, data, start_idx, rows[0][0])
|
return import_vouchers(common_values, data, start_idx, rows[0][0])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def map_fields(field_list, source, target):
|
def map_fields(field_list, source, target):
|
||||||
for f in field_list:
|
for f in field_list:
|
||||||
@ -70,9 +83,8 @@ def import_vouchers(common_values, data, start_idx, import_type):
|
|||||||
from webnotes.model.bean import Bean
|
from webnotes.model.bean import Bean
|
||||||
from accounts.utils import get_fiscal_year
|
from accounts.utils import get_fiscal_year
|
||||||
from webnotes.utils.dateutils import parse_date
|
from webnotes.utils.dateutils import parse_date
|
||||||
|
|
||||||
messages = []
|
messages = []
|
||||||
|
|
||||||
def get_account_details(account):
|
def get_account_details(account):
|
||||||
acc_details = webnotes.conn.sql("""select is_pl_account,
|
acc_details = webnotes.conn.sql("""select is_pl_account,
|
||||||
master_name from tabAccount where name=%s""", account, as_dict=1)
|
master_name from tabAccount where name=%s""", account, as_dict=1)
|
||||||
@ -113,8 +125,9 @@ def import_vouchers(common_values, data, start_idx, import_type):
|
|||||||
|
|
||||||
if d.ref_number:
|
if d.ref_number:
|
||||||
if not d.ref_date:
|
if not d.ref_date:
|
||||||
raise webnotes.ValidationError, \
|
webnotes.msgprint(_("Ref Date is Mandatory if Ref Number is specified"),
|
||||||
"""Ref Date is Mandatory if Ref Number is specified"""
|
raise_exception=1)
|
||||||
|
|
||||||
d.ref_date = parse_date(d.ref_date)
|
d.ref_date = parse_date(d.ref_date)
|
||||||
|
|
||||||
d.company = common_values.company
|
d.company = common_values.company
|
||||||
@ -176,7 +189,7 @@ def import_vouchers(common_values, data, start_idx, import_type):
|
|||||||
webnotes.conn.commit()
|
webnotes.conn.commit()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
webnotes.conn.rollback()
|
webnotes.conn.rollback()
|
||||||
err_msg = webnotes.message_log and "<br>".join(webnotes.message_log) or unicode(e)
|
err_msg = webnotes.message_log and "<br>".join(webnotes.message_log) or cstr(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"))
|
||||||
messages.append("<p style='color: red'>All transactions rolled back</p>")
|
messages.append("<p style='color: red'>All transactions rolled back</p>")
|
||||||
@ -240,16 +253,20 @@ def get_data(rows, company_abbr):
|
|||||||
raise Exception, """Column No(s). %s %s empty. \
|
raise Exception, """Column No(s). %s %s empty. \
|
||||||
Please remove them and try again.""" % (comma_and(empty_columns),
|
Please remove them and try again.""" % (comma_and(empty_columns),
|
||||||
len(empty_columns)==1 and "is" or "are")
|
len(empty_columns)==1 and "is" or "are")
|
||||||
|
|
||||||
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)]
|
||||||
|
|
||||||
|
if not accounts:
|
||||||
|
webnotes.msgprint(_("""No Account found in csv file,
|
||||||
|
May be company abbreviation is not correct"""), raise_exception=1)
|
||||||
|
|
||||||
if accounts and (len(columns) != rows[i+1].index(accounts[0])):
|
if accounts and (len(columns) != rows[i+1].index(accounts[0])):
|
||||||
raise Exception, _("""All account columns should be after \
|
webnotes.msgprint(_("""All account columns should be after \
|
||||||
standard columns and on the right.
|
standard columns and on the right.
|
||||||
If you entered it properly, next probable reason \
|
If you entered it properly, next probable reason \
|
||||||
could be wrong account name.
|
could be wrong account name.
|
||||||
Please rectify it in the file and try again.""")
|
Please rectify it in the file and try again."""), raise_exception=1)
|
||||||
|
|
||||||
return data, start_row_idx
|
return data, start_row_idx
|
Loading…
x
Reference in New Issue
Block a user