def check_record(d, parenttype=None, doctype_dl=None): """check for mandatory, select options, dates. these should ideally be in doclist""" from webnotes.utils.dateutils import parse_date if parenttype and not d.get('parent'): webnotes.msgprint(_("Parent is required."), raise_exception=1) if not doctype_dl: doctype_dl = webnotes.model.doctype.get(d.doctype) for key in d: docfield = doctype_dl.get_field(key) val = d[key] if docfield: if docfield.reqd and (val=='' or val==None): webnotes.msgprint("%s is mandatory." % docfield.label, raise_exception=1) if docfield.fieldtype=='Select' and val and docfield.options: if docfield.options.startswith('link:'): link_doctype = docfield.options.split(':')[1] if not webnotes.conn.exists(link_doctype, val): webnotes.msgprint("%s: %s must be a valid %s" % (docfield.label, val, link_doctype), raise_exception=1) elif docfield.options == "attach_files:": pass elif val not in docfield.options.split('\n'): webnotes.msgprint("%s must be one of: %s" % (docfield.label, ", ".join(filter(None, docfield.options.split("\n")))), raise_exception=1) if val and docfield.fieldtype=='Date': d[key] = parse_date(val) elif val and docfield.fieldtype in ["Int", "Check"]: d[key] = cint(val) elif val and docfield.fieldtype in ["Currency", "Float"]: d[key] = flt(val)
def check_record(d, parenttype): """check for mandatory, select options, dates. these should ideally be in doclist""" from webnotes.utils.dateutils import parse_date if parenttype and not d.get('parent'): raise Exception, "parent is required." for key in d: docfield = getdocfield(key) val = d[key] if docfield: if docfield.reqd and (val=='' or val==None): raise Exception, "%s is mandatory." % key if docfield.fieldtype=='Select' and val: if docfield.options and docfield.options.startswith('link:'): link_doctype = docfield.options.split(':')[1] if not webnotes.conn.exists(link_doctype, val): raise Exception, "%s must be a valid %s" % (key, link_doctype) elif not docfield.options: raise Exception, "Select options are missing for %s" elif val not in docfield.options.split('\n'): raise Exception, "%s must be one of: %s" % (key, ", ".join(filter(None, docfield.options.split("\n")))) if val and docfield.fieldtype=='Date': d[key] = parse_date(val) elif val and docfield.fieldtype in ["Int", "Check"]: d[key] = cint(val) elif val and docfield.fieldtype in ["Currency", "Float"]: d[key] = flt(val)
def check_record(d, parenttype=None, doctype_dl=None): """check for mandatory, select options, dates. these should ideally be in doclist""" from webnotes.utils.dateutils import parse_date if parenttype and not d.get('parent'): raise Exception, "parent is required." if not doctype_dl: doctype_dl = webnotes.model.doctype.get(d.doctype) for key in d: docfield = doctype_dl.get_field(key) val = d[key] if docfield: if docfield.reqd and (val=='' or val==None): raise Exception, "%s is mandatory." % key if docfield.fieldtype=='Select' and val and docfield.options: if docfield.options.startswith('link:'): link_doctype = docfield.options.split(':')[1] if not webnotes.conn.exists(link_doctype, val): raise Exception, "%s must be a valid %s" % (key, link_doctype) elif docfield.options == "attach_files:": pass elif val not in docfield.options.split('\n'): raise Exception, "%s must be one of: %s" % (key, ", ".join(filter(None, docfield.options.split("\n")))) if val and docfield.fieldtype=='Date': d[key] = parse_date(val) elif val and docfield.fieldtype in ["Int", "Check"]: d[key] = cint(val) elif val and docfield.fieldtype in ["Currency", "Float"]: d[key] = flt(val)
def check_record(d, parenttype): """check for mandatory, select options, dates. these should ideally be in doclist""" from webnotes.utils.dateutils import parse_date if parenttype and not d.get('parent'): raise Exception, "parent is required." for key in d: docfield = getdocfield(key) val = d[key] if docfield: if docfield.reqd and (val == '' or val == None): raise Exception, "%s is mandatory." % key if docfield.fieldtype == 'Select' and val: if docfield.options and docfield.options.startswith('link:'): link_doctype = docfield.options.split(':')[1] if not webnotes.conn.exists(link_doctype, val): raise Exception, "%s must be a valid %s" % ( key, link_doctype) elif not docfield.options: raise Exception, "Select options are missing for %s" elif val not in docfield.options.split('\n'): raise Exception, "%s must be one of: %s" % (key, ", ".join( filter(None, docfield.options.split("\n")))) if val and docfield.fieldtype == 'Date': d[key] = parse_date(val)
def import_vouchers(common_values, data, start_idx, import_type): from webnotes.model.doc import Document from webnotes.model.bean import Bean from accounts.utils import get_fiscal_year from webnotes.utils.dateutils import parse_date messages = [] def get_account_details(account): acc_details = webnotes.conn.sql("""select is_pl_account, master_name from tabAccount where name=%s""", account, as_dict=1) if not acc_details: webnotes.msgprint("%s is not an Account" % account, raise_exception=1) return acc_details[0] def apply_cost_center_and_against_invoice(detail, d): account = get_account_details(detail.account) if account.is_pl_account == "Yes": detail.cost_center = d.cost_center if account.master_name: map_fields([ "against_sales_invoice:against_invoice", "against_purhase_invoice:against_voucher", "against_journal_voucher:against_jv" ], d, detail.fields) webnotes.conn.commit() try: jv = Document("Journal Voucher") webnotes.conn.begin() for i in xrange(len(data)): jv = Document("Journal Voucher") d = data[i][0] if import_type == "Voucher Import: Two Accounts" and flt( d.get("amount")) == 0: webnotes.message_log = ["Amount not specified"] raise Exception elif import_type == "Voucher Import: Multiple Accounts" and \ (flt(d.get("total_debit")) == 0 or flt(d.get("total_credit")) == 0): webnotes.message_log = [ "Total Debit and Total Credit amount can not be zero" ] raise Exception else: d.posting_date = parse_date(d.posting_date) d.due_date = d.due_date and parse_date(d.due_date) or None 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 map_fields([ "voucher_type", "posting_date", "naming_series", "remarks:user_remark", "ref_number:cheque_no", "ref_date:cheque_date", "is_opening", "due_date", "company" ], d, jv.fields) jv.fiscal_year = get_fiscal_year(jv.posting_date)[0] details = [] if import_type == "Voucher Import: Two Accounts": map_fields(["amount:total_debit", "amount:total_credit"], d, jv.fields) detail1 = Document("Journal Voucher Detail") detail1.parent = True 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.parent = True detail2.parentfield = "entries" map_fields(["credit_account:account", "amount:credit"], d, detail2.fields) apply_cost_center_and_against_invoice(detail2, d) details = [detail1, detail2] elif import_type == "Voucher Import: Multiple Accounts": map_fields(["total_debit", "total_credit"], d, jv.fields) accounts = data[i][1] for acc in accounts: detail = Document("Journal Voucher Detail") detail.parent = True detail.parentfield = "entries" detail.account = acc detail.debit = flt(accounts[acc]) > 0 and flt( accounts[acc]) or 0 detail.credit = flt( accounts[acc]) < 0 and -1 * flt(accounts[acc]) or 0 apply_cost_center_and_against_invoice(detail, d) details.append(detail) if not details: webnotes.message_log = [ """No accounts found. If you entered accounts correctly, please check template once""" ] raise Exception doclist = Bean([jv] + details) # validate datatype from core.page.data_import_tool.data_import_tool import check_record for d in doclist: check_record(d.fields, d.parenttype) doclist.submit() messages.append("""<p style='color: green'>[row #%s] <a href=\"#Form/Journal Voucher/%s\">%s</a> imported</p>""" \ % ((start_idx + 1) + i, jv.name, jv.name)) webnotes.conn.commit() except Exception, e: webnotes.conn.rollback() err_msg = webnotes.message_log and "<br>".join( webnotes.message_log) or unicode(e) messages.append( """<p style='color: red'>[row #%s] %s failed: %s</p>""" % ((start_idx + 1) + i, jv.name or "", err_msg or "No message")) messages.append( "<p style='color: red'>All transactions rolled back</p>") webnotes.errprint(webnotes.getTraceback()) webnotes.message_log = []
def import_vouchers(common_values, data, start_idx, import_type): from webnotes.model.doc import Document from webnotes.model.bean import Bean from accounts.utils import get_fiscal_year from webnotes.utils.dateutils import parse_date messages = [] def get_account_details(account): acc_details = webnotes.conn.sql("""select is_pl_account, master_name from tabAccount where name=%s""", account, as_dict=1) if not acc_details: webnotes.msgprint("%s is not an Account" % account, raise_exception=1) return acc_details[0] def apply_cost_center_and_against_invoice(detail, d): account = get_account_details(detail.account) if account.is_pl_account=="Yes": detail.cost_center = d.cost_center if account.master_name: map_fields(["against_sales_invoice:against_invoice", "against_purhase_invoice:against_voucher", "against_journal_voucher:against_jv"], d, detail.fields) webnotes.conn.commit() try: jv = Document("Journal Voucher") webnotes.conn.begin() for i in xrange(len(data)): jv = Document("Journal Voucher") d = data[i][0] if import_type == "Voucher Import: Two Accounts" and flt(d.get("amount")) == 0: webnotes.message_log = ["Amount not specified"] raise Exception elif import_type == "Voucher Import: Multiple Accounts" and \ (flt(d.get("total_debit")) == 0 or flt(d.get("total_credit")) == 0): webnotes.message_log = ["Total Debit and Total Credit amount can not be zero"] raise Exception else: d.posting_date = parse_date(d.posting_date) d.due_date = d.due_date and parse_date(d.due_date) or None if d.ref_number: if not d.ref_date: webnotes.msgprint(_("Ref Date is Mandatory if Ref Number is specified"), raise_exception=1) d.ref_date = parse_date(d.ref_date) d.company = common_values.company map_fields(["voucher_type", "posting_date", "naming_series", "remarks:user_remark", "ref_number:cheque_no", "ref_date:cheque_date", "is_opening", "due_date", "company"], d, jv.fields) jv.fiscal_year = get_fiscal_year(jv.posting_date)[0] details = [] if import_type == "Voucher Import: Two Accounts": map_fields(["amount:total_debit", "amount:total_credit"], d, jv.fields) detail1 = Document("Journal Voucher Detail") detail1.parent = True 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.parent = True detail2.parentfield = "entries" map_fields(["credit_account:account","amount:credit"], d, detail2.fields) apply_cost_center_and_against_invoice(detail2, d) details = [detail1, detail2] elif import_type == "Voucher Import: Multiple Accounts": map_fields(["total_debit", "total_credit"], d, jv.fields) accounts = data[i][1] for acc in accounts: detail = Document("Journal Voucher Detail") detail.parent = True detail.parentfield = "entries" detail.account = acc detail.debit = flt(accounts[acc]) > 0 and flt(accounts[acc]) or 0 detail.credit = flt(accounts[acc]) < 0 and -1*flt(accounts[acc]) or 0 apply_cost_center_and_against_invoice(detail, d) details.append(detail) if not details: webnotes.message_log = ["""No accounts found. If you entered accounts correctly, please check template once"""] raise Exception doclist = Bean([jv]+details) # validate datatype from core.page.data_import_tool.data_import_tool import check_record for d in doclist: check_record(d.fields, d.parenttype) doclist.submit() messages.append("""<p style='color: green'>[row #%s] <a href=\"#Form/Journal Voucher/%s\">%s</a> imported</p>""" \ % ((start_idx + 1) + i, jv.name, jv.name)) webnotes.conn.commit() except Exception, e: webnotes.conn.rollback() 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>""" % ((start_idx + 1) + i, jv.name or "", err_msg or "No message")) messages.append("<p style='color: red'>All transactions rolled back</p>") webnotes.errprint(webnotes.getTraceback()) webnotes.message_log = []
def import_vouchers(common_values, data, start_idx, import_type): from webnotes.model.doc import Document from webnotes.model.doclist import DocList from webnotes.model.code import get_obj from accounts.utils import get_fiscal_year from webnotes.utils.dateutils import parse_date messages = [] def get_account_details(account): acc_details = webnotes.conn.sql("""select is_pl_account, master_name from tabAccount where name=%s""", account, as_dict=1) if not acc_details: webnotes.msgprint("%s is not an Account" % account, raise_exception=1) return acc_details[0] def apply_cost_center_and_against_invoice(detail, d): account = get_account_details(detail.account) if account.is_pl_account=="Yes": detail.cost_center = d.cost_center if account.master_name: map_fields(["against_sales_invoice:against_invoice", "against_purhase_invoice:against_voucher", "against_journal_voucher:against_jv"], d, detail.fields) webnotes.conn.commit() for i in xrange(len(data)): d = data[i][0] jv = webnotes.DictObj() try: d.posting_date = parse_date(d.posting_date) d.due_date = d.due_date and parse_date(d.due_date) or None 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") map_fields(["voucher_type", "posting_date", "naming_series", "remarks:user_remark", "ref_number:cheque_no", "ref_date:cheque_date", "is_opening", "amount:total_debit", "amount:total_credit", "due_date", "company"], d, jv.fields) jv.fiscal_year = get_fiscal_year(jv.posting_date)[0] details = [] if import_type == "Voucher Import: Two Accounts": detail1 = Document("Journal Voucher Detail") detail1.parent = True 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.parent = True detail2.parentfield = "entries" map_fields(["credit_account:account","amount:credit"], d, detail2.fields) apply_cost_center_and_against_invoice(detail2, d) details = [detail1, detail2] elif import_type == "Voucher Import: Multiple Accounts": accounts = data[i][1] for acc in accounts: detail = Document("Journal Voucher Detail") detail.parent = True detail.parentfield = "entries" detail.account = acc detail.debit = flt(accounts[acc]) > 0 and flt(accounts[acc]) or 0 detail.credit = flt(accounts[acc]) < 0 and -1*flt(accounts[acc]) or 0 apply_cost_center_and_against_invoice(detail, d) details.append(detail) if not details: messages.append("""<p style='color: red'>No accounts found. If you entered accounts correctly, please check template once</p>""") return webnotes.conn.begin() doclist = DocList([jv]+details) doclist.submit() webnotes.conn.commit() messages.append("""<p style='color: green'>[row #%s] <a href=\"#Form/Journal Voucher/%s\">%s</a> imported</p>""" \ % ((start_idx + 1) + i, jv.name, jv.name)) except Exception, e: webnotes.conn.rollback() 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>" \ % ((start_idx + 1) + i, jv.name or "", err_msg or "No message")) webnotes.errprint(webnotes.getTraceback()) webnotes.message_log = []