feat: add validation for fields

This commit is contained in:
Mangesh-Khairnar 2019-05-14 18:25:57 +05:30
parent 4df61b1495
commit 6175eb2cf9

View File

@ -6,13 +6,13 @@ from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from frappe.utils import cint,cstr, today
from frappe import _
import re
import datetime
from collections import OrderedDict
from frappe.core.doctype.file.file import download_file
def create_bank_remittance_txt(name):
payment_order = frappe.get_doc("Payment Order", name)
payment_order = frappe.get_cached_doc("Payment Order", name)
no_of_records = len(payment_order.get("references"))
total_amount = sum(entry.get("amount") for entry in payment_order.get("references"))
@ -60,18 +60,19 @@ def get_header_row(doc, client_code):
''' Returns header row and generated file name '''
file_name = generate_file_name(doc.name, doc.company_bank_account, doc.posting_date)
header = ["H"]
header.append(cstr(client_code)[:20])
header.append(validate_field_size(client_code, "Client Code", 20))
header += [''] * 3
header.append(cstr(file_name)[:20])
header.append(validate_field_size(file_name, "File Name", 20))
return "~".join(header), file_name
def get_batch_row(doc, no_of_records, total_amount, product_code):
batch = ["B"]
batch.append(cstr(no_of_records)[:5])
batch.append(cstr(format(total_amount, '0.2f'))[:17])
batch.append(validate_field_size(no_of_records, "No Of Records", 5))
batch.append(validate_amount(format(total_amount, '0.2f'), 17))
batch.append(sanitize_data(doc.name, '_')[:20])
batch.append(format_date(doc.posting_date))
batch.append(product_code[:20])
batch.append(validate_field_size(product_code,"Product Code", 20))
print(batch)
return "~".join(batch)
def get_detail_row(ref_doc, payment_entry, company_email):
@ -96,32 +97,32 @@ def get_detail_row(ref_doc, payment_entry, company_email):
record_identifier='D',
payment_ref_no=sanitize_data(ref_doc.payment_entry),
payment_type=cstr(payment_entry.mode_of_payment)[:10],
amount=str(format(ref_doc.amount, '.2f'))[:13],
amount=str(validate_amount(format(ref_doc.amount, '.2f'),13)),
payment_date=payment_date,
instrument_date=payment_date,
instrument_number='',
dr_account_no_client=str(company_bank_acc_no)[:20],
dr_account_no_client=str(validate_field_size(company_bank_acc_no, "Company Bank Account", 20)),
dr_description='',
dr_ref_no='',
cr_ref_no='',
bank_code_indicator='M',
beneficiary_code='',
beneficiary_name=sanitize_data(payment_entry.party, ' ')[:160],
beneficiary_bank=sanitize_data(supplier_bank_details.bank)[:10],
beneficiary_branch_code=cstr(supplier_bank_details.branch_code),
beneficiary_acc_no=supplier_bank_details.bank_account_no,
beneficiary_name=sanitize_data(validate_information(payment_entry, "party", 160), ' '),
beneficiary_bank=sanitize_data(validate_information(supplier_bank_details, "bank", 10)),
beneficiary_branch_code=cstr(validate_information(supplier_bank_details, "branch_code", 11)),
beneficiary_acc_no=validate_information(supplier_bank_details, "bank_account_no", 20),
location='',
print_location='',
beneficiary_address_1=sanitize_data(cstr(supplier_billing_address.address_line1), ' ')[:50],
beneficiary_address_2=sanitize_data(cstr(supplier_billing_address.address_line2), ' ')[:50],
beneficiary_address_1=validate_field_size(sanitize_data(cstr(supplier_billing_address.address_line1), ' '), " Beneficiary Address 1", 50),
beneficiary_address_2=validate_field_size(sanitize_data(cstr(supplier_billing_address.address_line2), ' '), " Beneficiary Address 2", 50),
beneficiary_address_3='',
beneficiary_address_4='',
beneficiary_address_5='',
beneficiary_city=supplier_billing_address.city[:20],
beneficiary_zipcode=cstr(supplier_billing_address.pincode)[:6],
beneficiary_state=supplier_billing_address.state[:20],
beneficiary_city=validate_field_size(cstr(supplier_billing_address.city), "Beneficiary City", 20),
beneficiary_zipcode=validate_field_size(cstr(supplier_billing_address.pincode), "Pin Code", 6),
beneficiary_state=validate_field_size(cstr(supplier_billing_address.state), "Beneficiary State", 20),
beneficiary_email=cstr(email)[:255],
beneficiary_mobile=cstr(supplier_billing_address.phone),
beneficiary_mobile=validate_field_size(cstr(supplier_billing_address.phone), "Beneficiary Mobile", 10),
payment_details_1='',
payment_details_2='',
payment_details_3='',
@ -153,15 +154,38 @@ def get_advice_rows(payment_entry):
def get_trailer_row(no_of_records, total_amount):
''' Returns trailer row '''
trailer = ["T"]
trailer.append(cstr(no_of_records)[:5])
trailer.append(cstr(format(total_amount, '.2f'))[:17])
trailer.append(validate_field_size(no_of_records, "No of Records", 5))
trailer.append(validate_amount(format(total_amount, "0.2f"), 17))
return "~".join(trailer)
def sanitize_data(val, replace_str=''):
''' Remove all the non-alphanumeric characters from string '''
pattern = pattern = re.compile('[\W_]+')
pattern = re.compile('[\W_]+')
return pattern.sub(replace_str, val)
def format_date(val):
''' Convert a datetime object to DD/MM/YYYY format '''
return val.strftime("%d/%m/%Y")
return val.strftime("%d/%m/%Y")
def validate_amount(val, max_int_size):
''' Validate amount to be within the allowed limits '''
int_size = len(str(val).split('.')[0])
if int_size > max_int_size:
frappe.throw(_("Amount for a single transaction is more than maximum allowed amount, create a separate payment order by splitting the transactions"))
return val
def validate_information(obj, attr, max_size):
''' Checks if the information is not set in the system and is within the size '''
if hasattr(obj, attr):
return validate_field_size(getattr(obj, attr), frappe.unscrub(attr), max_size)
else:
frappe.throw(_("%s is mandatory for generating remittance payments, set the field and try again" % attr))
def validate_field_size(val, label, max_size):
''' check the size of the val '''
if len(cstr(val)) > max_size:
frappe.throw("%s field is limited to size %d" % (label, max_size))
return cstr(val)