2019-05-03 15:51:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2018, Frappe and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import frappe
|
|
|
|
from frappe.model.document import Document
|
|
|
|
from frappe.utils import cint,cstr, today
|
|
|
|
from functools import reduce
|
|
|
|
import re
|
|
|
|
import datetime
|
|
|
|
from collections import OrderedDict
|
2019-05-05 15:15:21 +00:00
|
|
|
from frappe.core.doctype.file.file import download_file
|
2019-05-03 15:51:37 +00:00
|
|
|
|
|
|
|
def create_bank_remittance_txt(name):
|
|
|
|
payment_order = frappe.get_doc("Payment Order", name)
|
|
|
|
|
|
|
|
no_of_records = len(payment_order.get("references"))
|
|
|
|
total_amount = reduce(lambda x, y: x.get("amount") + y.get("amount"), payment_order.get("references"))
|
|
|
|
|
2019-05-05 17:06:32 +00:00
|
|
|
header, file_name = get_header_row(payment_order)
|
2019-05-03 15:51:37 +00:00
|
|
|
batch = get_batch_row(payment_order, no_of_records, total_amount)
|
|
|
|
|
|
|
|
detail = []
|
|
|
|
for ref_doc in payment_order.get("references"):
|
2019-05-05 15:15:21 +00:00
|
|
|
detail += get_detail_row(ref_doc, format_date(payment_order.posting_date))
|
2019-05-03 15:51:37 +00:00
|
|
|
|
|
|
|
trailer = get_trailer_row(no_of_records, total_amount)
|
|
|
|
detail_records = "\n".join(detail)
|
|
|
|
|
2019-05-05 17:06:32 +00:00
|
|
|
return "\n".join([header, batch , detail_records, trailer]), file_name
|
2019-05-03 15:51:37 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
2019-05-05 15:15:21 +00:00
|
|
|
def generate_report(name):
|
2019-05-05 17:06:32 +00:00
|
|
|
data, file_name = create_bank_remittance_txt(name)
|
|
|
|
|
2019-05-03 15:51:37 +00:00
|
|
|
f = frappe.get_doc({
|
|
|
|
'doctype': 'File',
|
|
|
|
'file_name': file_name+'.txt',
|
|
|
|
'content': data,
|
2019-05-05 15:15:21 +00:00
|
|
|
"attached_to_doctype": 'Payment Order',
|
|
|
|
"attached_to_name": name,
|
2019-05-03 15:51:37 +00:00
|
|
|
'is_private': True
|
|
|
|
})
|
|
|
|
f.save()
|
2019-05-05 15:15:21 +00:00
|
|
|
download_file(f.file_url)
|
2019-05-03 15:51:37 +00:00
|
|
|
|
2019-05-05 17:06:32 +00:00
|
|
|
def generate_file_name(name, date):
|
2019-05-03 15:51:37 +00:00
|
|
|
''' generate file name with format (account_code)_mmdd_(payment_order_no) '''
|
2019-05-05 17:06:32 +00:00
|
|
|
return date.strftime("%m%d")+sanitize_to_alphanumeric(name)
|
2019-05-03 15:51:37 +00:00
|
|
|
|
|
|
|
def get_header_row(doc):
|
|
|
|
client_code = "ELECTROLAB"
|
2019-05-05 17:06:32 +00:00
|
|
|
file_name = generate_file_name(doc.name, doc.posting_date)
|
2019-05-03 15:51:37 +00:00
|
|
|
header = ["H"]
|
|
|
|
header.append(cstr(client_code)[:20])
|
|
|
|
header += [''] * 3
|
|
|
|
header.append(cstr(file_name)[:20])
|
2019-05-05 17:06:32 +00:00
|
|
|
return "~".join(header), file_name
|
2019-05-03 15:51:37 +00:00
|
|
|
|
|
|
|
def get_batch_row(doc, no_of_records, total_amount):
|
|
|
|
product_code = "VENPAY"
|
|
|
|
batch = ["B"]
|
|
|
|
batch.append(cstr(no_of_records)[:5]) # 5
|
|
|
|
batch.append(cstr(total_amount)[:17]) #amt 17.2
|
|
|
|
batch.append(sanitize_to_alphanumeric(doc.name)[:20])
|
|
|
|
batch.append(format_date(doc.posting_date))
|
|
|
|
batch.append(product_code[:20])
|
|
|
|
return "~".join(batch)
|
|
|
|
|
|
|
|
def get_detail_row(ref_doc, payment_date):
|
|
|
|
payment_entry = frappe.get_cached_doc('Payment Entry', ref_doc.payment_entry)
|
|
|
|
supplier_bank_details = frappe.get_cached_doc('Bank Account', ref_doc.bank_account)
|
|
|
|
addr_link = frappe.db.get_value('Dynamic Link',
|
|
|
|
{
|
|
|
|
'link_doctype': 'Supplier',
|
|
|
|
'link_name': 'Sample Supplier',
|
|
|
|
'parenttype':'Address',
|
|
|
|
'parent': ('like', '%-Billing')
|
|
|
|
},'parent')
|
2019-05-05 17:06:32 +00:00
|
|
|
supplier_billing_address = frappe.get_cached_doc('Address', addr_link)
|
2019-05-03 15:51:37 +00:00
|
|
|
detail = OrderedDict(
|
|
|
|
record_identifier='D',
|
|
|
|
payment_ref_no=sanitize_to_alphanumeric(ref_doc.payment_entry),
|
2019-05-05 15:15:21 +00:00
|
|
|
payment_type=cstr(payment_entry.mode_of_payment)[:10],
|
2019-05-03 15:51:37 +00:00
|
|
|
amount=str(ref_doc.amount)[:13],
|
|
|
|
payment_date=payment_date,
|
|
|
|
instrument_date=payment_date,
|
|
|
|
instrument_number='',
|
|
|
|
dr_account_no_client=str(payment_entry.bank_account_no)[:20],
|
|
|
|
dr_description='',
|
|
|
|
dr_ref_no='',
|
|
|
|
cr_ref_no='',
|
|
|
|
bank_code_indicator='M',
|
|
|
|
beneficiary_code='',
|
|
|
|
beneficiary_name=sanitize_to_alphanumeric(payment_entry.party)[:160],
|
|
|
|
beneficiary_bank=sanitize_to_alphanumeric(supplier_bank_details.bank)[:10],
|
2019-05-05 15:15:21 +00:00
|
|
|
beneficiary_branch_code=cstr(supplier_bank_details.branch_code),
|
2019-05-03 15:51:37 +00:00
|
|
|
beneficiary_acc_no=supplier_bank_details.bank_account_no,
|
|
|
|
location=supplier_billing_address.city,
|
|
|
|
print_location=supplier_billing_address.city,
|
2019-05-05 15:15:21 +00:00
|
|
|
beneficiary_address_1=cstr(supplier_billing_address.address_line1)[:50],
|
|
|
|
beneficiary_address_2=cstr(supplier_billing_address.address_line2)[:50],
|
2019-05-03 15:51:37 +00:00
|
|
|
beneficiary_address_3='',
|
|
|
|
beneficiary_address_4='',
|
|
|
|
beneficiary_address_5='',
|
2019-05-05 15:15:21 +00:00
|
|
|
beneficiary_city=supplier_billing_address.city,
|
|
|
|
beneficiary_zipcode=cstr(supplier_billing_address.pincode),
|
2019-05-03 15:51:37 +00:00
|
|
|
beneficiary_state=supplier_billing_address.state,
|
2019-05-05 15:15:21 +00:00
|
|
|
beneficiary_email=supplier_billing_address.email_id,
|
2019-05-03 15:51:37 +00:00
|
|
|
beneficiary_mobile=supplier_billing_address.phone,
|
|
|
|
payment_details_1='',
|
|
|
|
payment_details_2='',
|
|
|
|
payment_details_3='',
|
|
|
|
payment_details_4='',
|
|
|
|
delivery_mode=''
|
|
|
|
)
|
2019-05-05 15:15:21 +00:00
|
|
|
detail_record = ["~".join(list(detail.values()))]
|
|
|
|
detail_record += get_advice_rows(payment_entry)
|
|
|
|
return detail_record
|
|
|
|
|
|
|
|
def get_advice_rows(payment_entry):
|
|
|
|
payment_entry_date = payment_entry.posting_date.strftime("%b%y%d%m").upper()
|
|
|
|
mode_of_payment = payment_entry.mode_of_payment
|
|
|
|
advice_rows = []
|
|
|
|
for record in payment_entry.references:
|
|
|
|
advice = ['E']
|
|
|
|
advice.append(cstr(mode_of_payment))
|
|
|
|
advice.append(cstr(record.total_amount))
|
|
|
|
advice.append('')
|
|
|
|
advice.append(cstr(record.outstanding_amount))
|
|
|
|
advice.append(sanitize_to_alphanumeric(record.reference_name))
|
|
|
|
advice.append(format_date(record.due_date))
|
|
|
|
advice.append(payment_entry_date)
|
|
|
|
advice += ['']*3
|
|
|
|
advice_rows.append("~".join(advice))
|
|
|
|
return advice_rows
|
2019-05-03 15:51:37 +00:00
|
|
|
|
|
|
|
def get_trailer_row(no_of_records, total_amount):
|
|
|
|
trailer = ["T"]
|
|
|
|
trailer.append(cstr(no_of_records)[:5]) # 5
|
|
|
|
trailer.append(cstr(total_amount)[:17]) # 17.2
|
|
|
|
return "~".join(trailer)
|
|
|
|
|
|
|
|
def sanitize_to_alphanumeric(val):
|
|
|
|
''' Remove all the non-alphanumeric characters from string '''
|
|
|
|
pattern = pattern = re.compile('[\W_]+')
|
|
|
|
return pattern.sub(' ', val)
|
|
|
|
|
|
|
|
def format_date(val):
|
|
|
|
''' Convert a datetime object to DD/MM/YYYY format '''
|
|
|
|
return val.strftime("%d/%m/%Y")
|