Internal Reconciliation: for knock out booking and payment entries

This commit is contained in:
Nabin Hait 2011-09-26 18:56:02 +05:30
parent a28e366c15
commit 5f2046496b
7 changed files with 726 additions and 0 deletions

View File

@ -0,0 +1,21 @@
// Booking Entry Id
// --------------------
cur_frm.fields_dict.voucher_no.get_query = function(doc) {
if (!doc.account) msgprint("Please select Account first");
else {
return repl("select voucher_no, posting_date \
from `tabGL Entry` where ifnull(is_cancelled, 'No') = 'No'\
and account = '%(acc)s' \
and voucher_type = '%(dt)s' \
and voucher_no LIKE '%s' \
ORDER BY posting_date DESC, voucher_no DESC LIMIT 50 \
", {dt:session.rev_dt_labels[doc.voucher_type] || doc.voucher_type, acc:doc.account});
}
}
cur_frm.cscript.voucher_no =function(doc, cdt, cdn) {
get_server_fields('get_voucher_details', '', '', doc, cdt, cdn, 1)
}

View File

@ -0,0 +1,138 @@
# Please edit this list and import only required elements
import webnotes
from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
from webnotes.model import db_exists
from webnotes.model.doc import Document, addchild, removechild, getchildren, make_autoname, SuperDocType
from webnotes.model.doclist import getlist, copy_doclist
from webnotes.model.code import get_obj, get_server_obj, run_server_obj, updatedb, check_syntax
from webnotes import session, form, is_testing, msgprint, errprint
set = webnotes.conn.set
sql = webnotes.conn.sql
get_value = webnotes.conn.get_value
in_transaction = webnotes.conn.in_transaction
convert_to_lists = webnotes.conn.convert_to_lists
# -----------------------------------------------------------------------------------------
class DocType:
def __init__(self, doc, doclist):
self.doc = doc
self.doclist = doclist
self.acc_type = self.doc.account and sql("select debit_or_credit from `tabAccount` where name = %s", self.doc.account)[0][0].lower() or ''
self.dt = {
'Sales Invoice': 'Receivable Voucher',
'Purchase Invoice': 'Payable Voucher',
'Journal Voucher': 'Journal Voucher'
}
#--------------------------------------------------
def get_voucher_details(self):
tot_amt = sql("""
select sum(%s) from `tabGL Entry` where
voucher_type = %s and voucher_no = %s
and account = %s and ifnull(is_cancelled, 'No') = 'No'
"""% (self.acc_type, '%s', '%s', '%s'), (self.dt[self.doc.voucher_type], self.doc.voucher_no, self.doc.account))
outstanding = sql("""
select sum(%s) - sum(%s) from `tabGL Entry` where
against_voucher = %s and voucher_no != %s
and account = %s and ifnull(is_cancelled, 'No') = 'No'
""" % ((self.acc_type == 'debit' and 'credit' or 'debit'), self.acc_type, '%s', '%s', '%s'), (self.doc.voucher_no, self.doc.voucher_no, self.doc.account))
ret = {
'total_amount': flt(tot_amt[0][0]) or 0,
'pending_amt_to_reconcile': flt(tot_amt[0][0]) - flt(outstanding[0][0]) or 0
}
return ret
#--------------------------------------------------
def get_payment_entries(self):
"""
Get payment entries for the account and period
Payment entry will be decided based on account type (Dr/Cr)
"""
self.doc.clear_table(self.doclist, 'ir_payment_details')
gle = self.get_gl_entries()
self.create_payment_table(gle)
#--------------------------------------------------
def get_gl_entries(self):
self.validate_mandatory()
dc = self.acc_type == 'debit' and 'credit' or 'debit'
cond = self.doc.from_date and " and t1.posting_date >= '" + self.doc.from_date + "'" or ""
cond += self.doc.to_date and " and t1.posting_date <= '" + self.doc.to_date + "'"or ""
cond += self.doc.amt_greater_than and ' and t2.' + dc+' >= ' + self.doc.amt_greater_than or ''
cond += self.doc.amt_less_than and ' and t2.' + dc+' <= ' + self.doc.amt_less_than or ''
gle = sql("""
select t1.name as voucher_no, t1.posting_date, t1.total_debit as total_amt, sum(ifnull(t2.credit, 0)) - sum(ifnull(t2.debit, 0)) as amt_due, t1.remark, t2.against_account, t2.name as voucher_detail_no
from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2
where t1.name = t2.parent
and t1.docstatus = 1
and t2.account = %s
and ifnull(t2.against_voucher, '')='' and ifnull(t2.against_invoice, '')='' and ifnull(t2.against_jv, '')=''
and t2.%s > 0
%s
group by t1.name
"""% ('%s', dc, cond), self.doc.account, as_dict=1)
return gle
#--------------------------------------------------
def create_payment_table(self, gle):
for d in gle:
ch = addchild(self.doc, 'ir_payment_details', 'IR Payment Detail', 1, self.doclist)
ch.voucher_no = d.get('voucher_no')
ch.posting_date = d.get('posting_date')
ch.amt_due = self.acc_type == 'debit' and flt(d.get('amt_due')) or -1*flt(d.get('amt_due'))
ch.total_amt = flt(d.get('total_amt'))
ch.against_account = d.get('against_account')
ch.remarks = d.get('remark')
ch.amt_to_be_reconciled = flt(ch.amt_due)
ch.voucher_detail_no = d.get('voucher_detail_no')
#--------------------------------------------------
def validate_mandatory(self):
if not self.doc.account:
msgprint("Please select Account first", raise_exception=1)
#--------------------------------------------------
def reconcile(self):
"""
Links booking and payment voucher
1. cancel payment voucher
2. split into multiple rows if partially adjusted, assign against voucher
3. submit payment voucher
"""
lst = []
for d in getlist(self.doclist, 'ir_payment_details'):
if d.selected and flt(d.amt_to_be_reconciled) > 0:
args = {
'voucher_no' : d.voucher_no,
'voucher_detail_no' : d.voucher_detail_no,
'against_voucher_type' : self.dt[self.doc.voucher_type],
'against_voucher' : self.doc.voucher_no,
'account' : self.doc.account,
'is_advance' : 'No',
'dr_or_cr' : self.acc_type=='debit' and 'credit' or 'debit',
'unadjusted_amt' : flt(d.amt_due),
'allocated_amt' : flt(d.amt_to_be_reconciled)
}
lst.append(args)
if not sql("select name from `tab%s` where name = %s" %(self.dt[self.doc.voucher_type], '%s'), self.doc.voucher_no):
msgprint("Please select valid Voucher No to proceed", raise_exception=1)
if lst:
get_obj('GL Control').reconcile_against_document(lst)
msgprint("Successfully reconciled.")
else:
msgprint("No payment entries selected.", raise_exception=1)

View File

@ -0,0 +1,266 @@
# DocType, Internal Reconciliation
[
# These values are common in all dictionaries
{
'creation': '2011-08-30 11:45:50',
'docstatus': 0,
'modified': '2011-09-26 14:21:22',
'modified_by': 'Administrator',
'owner': 'Administrator'
},
# These values are common for all DocType
{
'_last_update': '1316509358',
'colour': 'White:FFF',
'default_print_format': 'Standard',
'doctype': 'DocType',
'document_type': 'Other',
'issingle': 1,
'module': 'Accounts',
'name': '__common__',
'section_style': 'Simple',
'show_in_menu': 1,
'version': 35
},
# These values are common for all DocField
{
'doctype': 'DocField',
'name': '__common__',
'parent': 'Internal Reconciliation',
'parentfield': 'fields',
'parenttype': 'DocType'
},
# These values are common for all DocPerm
{
'doctype': 'DocPerm',
'name': '__common__',
'parent': 'Internal Reconciliation',
'parentfield': 'permissions',
'parenttype': 'DocType',
'read': 1
},
# DocType, Internal Reconciliation
{
'doctype': 'DocType',
'name': 'Internal Reconciliation'
},
# DocPerm
{
'create': 1,
'doctype': 'DocPerm',
'permlevel': 0,
'role': 'System Manager',
'write': 1
},
# DocPerm
{
'create': 1,
'doctype': 'DocPerm',
'permlevel': 0,
'role': 'Accounts Manager',
'write': 1
},
# DocPerm
{
'create': 1,
'doctype': 'DocPerm',
'permlevel': 0,
'role': 'Accounts User',
'write': 1
},
# DocPerm
{
'doctype': 'DocPerm',
'permlevel': 1,
'role': 'All'
},
# DocField
{
'colour': 'White:FFF',
'doctype': 'DocField',
'fieldtype': 'Column Break',
'permlevel': 0,
'width': '50%'
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'account',
'fieldtype': 'Link',
'label': 'Account',
'options': 'Account',
'permlevel': 0
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'voucher_type',
'fieldtype': 'Select',
'label': 'Voucher Type',
'options': 'Sales Invoice\nPurchase Invoice\nJournal Voucher',
'permlevel': 0
},
# DocField
{
'colour': 'White:FFF',
'doctype': 'DocField',
'fieldname': 'voucher_no',
'fieldtype': 'Link',
'label': 'Voucher No',
'permlevel': 0,
'trigger': 'Client'
},
# DocField
{
'doctype': 'DocField',
'fieldtype': 'Column Break',
'permlevel': 0,
'width': '50%'
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'total_amount',
'fieldtype': 'Currency',
'label': 'Total Amount',
'permlevel': 1
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'pending_amt_to_reconcile',
'fieldtype': 'Currency',
'label': 'Pending Amt To Reconcile',
'permlevel': 1
},
# DocField
{
'colour': 'White:FFF',
'doctype': 'DocField',
'fieldtype': 'Section Break',
'label': 'Payment Entries',
'permlevel': 0
},
# DocField
{
'colour': 'White:FFF',
'doctype': 'DocField',
'fieldtype': 'Column Break',
'label': "<div class = 'field_description'>Filter payment entries based on date:</div>",
'permlevel': 0,
'width': '50%'
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'from_date',
'fieldtype': 'Date',
'label': 'From Date',
'permlevel': 0
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'to_date',
'fieldtype': 'Date',
'label': 'To Date',
'permlevel': 0
},
# DocField
{
'colour': 'White:FFF',
'doctype': 'DocField',
'fieldtype': 'Column Break',
'label': "<div class = 'field_description'>Filter payment entries based on amount:</div>",
'permlevel': 0,
'width': '50%'
},
# DocField
{
'colour': 'White:FFF',
'doctype': 'DocField',
'fieldname': 'amt_greater_than',
'fieldtype': 'Data',
'label': 'Amount >=',
'permlevel': 0
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'amt_less_than',
'fieldtype': 'Data',
'label': 'Amount <=',
'permlevel': 0
},
# DocField
{
'doctype': 'DocField',
'fieldtype': 'Section Break',
'options': 'Simple',
'permlevel': 0
},
# DocField
{
'doctype': 'DocField',
'fieldtype': 'Button',
'label': 'Pull Payment Entries',
'options': 'get_payment_entries',
'permlevel': 0
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'ir_payment_details',
'fieldtype': 'Table',
'label': 'Payment Entries',
'options': 'IR Payment Detail',
'permlevel': 0
},
# DocField
{
'colour': 'White:FFF',
'doctype': 'DocField',
'fieldtype': 'HTML',
'label': 'Reconcile HTML',
'options': "<div class='field_description'>Select Payment Voucher and Amount to Reconcile in the above table and then click Reconcile button</div>",
'permlevel': 0
},
# DocField
{
'colour': 'White:FFF',
'doctype': 'DocField',
'fieldtype': 'Button',
'label': 'Reconcile',
'options': 'reconcile',
'permlevel': 0,
'trigger': 'Client'
}
]

View File

@ -0,0 +1,169 @@
import unittest
import webnotes
from webnotes.model.doc import Document
from webnotes.model.code import get_obj
from webnotes.utils import cstr, flt
sql = webnotes.conn.sql
class TestInternalReco(unittest.TestCase):
def setUp(self):
webnotes.conn.begin()
comp1.save(1)
cust1.save(1)
bank1.save(1)
rv1.save(1)
rv_gle.save(1)
for t in jv1: t.save(1)
for t in jv1[1:]:
sql("update `tabJournal Voucher Detail` set parent = '%s' where name = '%s'" % (jv1[0].name, t.name))
ir[0].save()
for t in ir[1:]:
t.save(1)
sql("update `tabIR Payment Detail` set voucher_no = '%s', voucher_detail_no = '%s' where parent = 'Internal Reconciliation'" % (jv1[0].name, jv1[1].name))
sql("update `tabGL Entry` set voucher_no = %s, against_voucher = %s where voucher_no = 'rv1'", (rv1.name, rv1.name))
sql("update `tabSingles` set value = %s where doctype = 'Internal Reconciliation' and field = 'voucher_no'", rv1.name)
self.ir = get_obj('Internal Reconciliation', with_children=1)
self.ir.reconcile()
#===========================
def test_jv(self):
"""
Test whether JV has benn properly splitted and against doc has been updated
"""
amt_against_doc = [[cstr(d[0]), flt(d[1]), flt(d[2])]for d in sql("select against_invoice, debit, credit from `tabJournal Voucher Detail` where parent = %s and account = 'cust1 - c1'", jv1[0].name)]
self.assertTrue(amt_against_doc == [[rv1.name, 0, 100.0], ['', 0, 400.0]])
#============================
def test_gl_entry(self):
"""
Check proper gl entry has been made
"""
gle = [[cstr(d[0]), flt(d[1])] for d in sql("select against_voucher, sum(credit) - sum(debit) from `tabGL Entry` where voucher_no = %s and account = 'cust1 - c1' and ifnull(is_cancelled, 'No') = 'No' group by against_voucher", jv1[0].name)]
self.assertTrue([rv1.name, 100.0] in gle)
self.assertTrue(['', 400.0] in gle)
#============================
def test_outstanding(self):
"""
Check whether Outstanding amount has been properly updated in RV
"""
amt = sql("select outstanding_amount from `tabReceivable Voucher` where name = '%s'" % rv1.name)[0][0]
self.assertTrue(amt == 0)
#============================
def tearDown(self):
webnotes.conn.rollback()
# test data
#---------------
rv1 = Document(fielddata={
'doctype':'Receivable Voucher',
'docstatus':1,
'debit_to':'cust1 - c1',
'grand_total': 100,
'outstanding_amount': 100,
'name': 'rv1'
})
jv1 = [Document(fielddata={
'doctype':'Journal Voucher',
'docstatus':1,
'cheque_no': '163567',
'docstatus':1,
'company': 'comp1',
'posting_date' : '2011-05-02',
'remark': 'test data',
'fiscal_year': '2011-2012',
'total_debit': 500,
'total_credit': 500
}),
Document(fielddata = {
'parenttype':'Journal Voucher',
'parentfield':'entries',
'doctype':'Journal Voucher Detail',
'account' : 'cust1 - c1',
'credit':500,
'debit' : 0,
'docstatus':1
}),
Document(fielddata = {
'parenttype':'Journal Voucher',
'parentfield':'entries',
'doctype':'Journal Voucher Detail',
'account' : 'bank1 - c1',
'credit':0,
'debit' : 500,
'docstatus':1
})]
ir = [Document(fielddata = {
'doctype':'Internal Reconciliation',
'name' : 'Internal Reconciliation',
'account':'cust1 - c1',
'voucher_type' : 'Sales Invoice',
'voucher_no': 'rv1'
}),
Document(fielddata = {
'parenttype':'Internal Reconciliation',
'parentfield':'ir_payment_details',
'doctype':'IR Payment Detail',
'parent': 'Internal Reconciliation',
'voucher_no': 'jv1',
'name' : '123112',
'voucher_detail_no' : 'jvd1',
'selected' : 1,
'amt_due' : 500,
'amt_to_be_reconciled':100
})]
cust1 = Document(fielddata={
'doctype':'Account',
'docstatus':0,
'account_name' : 'cust1',
'debit_or_credit': 'Debit',
'company' : 'comp1',
'lft': 1,
'rgt': 2
})
bank1 = Document(fielddata={
'doctype':'Account',
'docstatus':0,
'account_name' : 'bank1',
'debit_or_credit': 'Debit',
'company' : 'comp1',
'lft': 3,
'rgt': 4
})
comp1 = Document(fielddata={
'doctype':'Company',
'abbr': 'c1',
'company_name' : 'comp1',
'name': 'comp1'
})
rv_gle = Document(fielddata={
'doctype':'GL Entry',
'account': 'cust1 - c1',
'company' : 'comp1',
'voucher_no': 'rv1',
'against_voucher': 'rv1',
'against_voucher_type': 'Receivable Voucher',
'voucher_type' : 'Receivable Voucher',
'debit': 100,
'credit': 0
})

View File

@ -0,0 +1,132 @@
# DocType, IR Payment Detail
[
# These values are common in all dictionaries
{
'creation': '2011-08-30 11:57:48',
'docstatus': 0,
'modified': '2011-09-20 15:18:02',
'modified_by': 'Administrator',
'owner': 'Administrator'
},
# These values are common for all DocType
{
'colour': 'White:FFF',
'default_print_format': 'Standard',
'doctype': 'DocType',
'istable': 1,
'module': 'Accounts',
'name': '__common__',
'section_style': 'Simple',
'show_in_menu': 0,
'version': 14
},
# These values are common for all DocField
{
'doctype': 'DocField',
'name': '__common__',
'parent': 'IR Payment Detail',
'parentfield': 'fields',
'parenttype': 'DocType'
},
# DocType, IR Payment Detail
{
'doctype': 'DocType',
'name': 'IR Payment Detail'
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'selected',
'fieldtype': 'Check',
'label': 'Select',
'permlevel': 0,
'reqd': 1,
'width': '60px'
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'voucher_no',
'fieldtype': 'Link',
'label': 'Voucher No',
'options': 'Journal Voucher',
'permlevel': 1,
'reqd': 0,
'width': '140px'
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'amt_due',
'fieldtype': 'Currency',
'label': 'Amt Due',
'permlevel': 1
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'amt_to_be_reconciled',
'fieldtype': 'Currency',
'label': 'Amt to be reconciled',
'permlevel': 0,
'reqd': 1
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'posting_date',
'fieldtype': 'Date',
'label': 'Posting Date',
'permlevel': 1
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'total_amt',
'fieldtype': 'Currency',
'label': 'Total Amt',
'permlevel': 1
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'against_account',
'fieldtype': 'Data',
'label': 'Against Account',
'permlevel': 1
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'remarks',
'fieldtype': 'Small Text',
'label': 'Remarks',
'permlevel': 1,
'width': '200px'
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'voucher_detail_no',
'fieldtype': 'Data',
'hidden': 1,
'label': 'Voucher Detail No',
'no_column': 0,
'permlevel': 1,
'print_hide': 1,
'reqd': 0
}
]