Either debit or credit amount should be entered in journal voucher

This commit is contained in:
Nabin Hait
2012-10-01 12:51:44 +05:30
parent ce15b3e3bc
commit ecf2e0ed10

View File

@@ -8,11 +8,11 @@
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# Please edit this list and import only required elements # Please edit this list and import only required elements
from __future__ import unicode_literals from __future__ import unicode_literals
@@ -35,431 +35,357 @@ convert_to_lists = webnotes.conn.convert_to_lists
from utilities.transaction_base import TransactionBase from utilities.transaction_base import TransactionBase
class DocType: class DocType:
def __init__(self,d,dl): def __init__(self,d,dl):
self.doc, self.doclist = d,dl self.doc, self.doclist = d,dl
self.master_type = {} self.master_type = {}
self.credit_days_for = {} self.credit_days_for = {}
self.credit_days_global = -1 self.credit_days_global = -1
self.is_approving_authority = -1 self.is_approving_authority = -1
#-------------------------------------------------------------------------------------------------------- def autoname(self):
# Autoname self.doc.name = make_autoname(self.doc.naming_series+'.#####')
#--------------------------------------------------------------------------------------------------------
def autoname(self): def get_outstanding(self, args):
self.doc.name = make_autoname(self.doc.naming_series+'.#####') args = eval(args)
o_s = sql("select outstanding_amount from `tab%s` where name = '%s'" % (args['doctype'],args['docname']))
#-------------------------------------------------------------------------------------------------------- if args['doctype'] == 'Purchase Invoice':
# Fetch outstanding amount from RV/PV return {'debit': o_s and flt(o_s[0][0]) or 0}
#-------------------------------------------------------------------------------------------------------- if args['doctype'] == 'Sales Invoice':
def get_outstanding(self, args): return {'credit': o_s and flt(o_s[0][0]) or 0}
args = eval(args)
o_s = sql("select outstanding_amount from `tab%s` where name = '%s'" % (args['doctype'],args['docname'])) def create_remarks(self):
if args['doctype'] == 'Purchase Invoice': r = []
return {'debit': o_s and flt(o_s[0][0]) or 0} if self.doc.cheque_no :
if args['doctype'] == 'Sales Invoice': if self.doc.cheque_date:
return {'credit': o_s and flt(o_s[0][0]) or 0} r.append('Via cheque #%s dated %s' % (self.doc.cheque_no, formatdate(self.doc.cheque_date)))
else :
#-------------------------------------------------------------------------------------------------------- msgprint("Please enter cheque date")
# Create remarks raise Exception
#--------------------------------------------------------------------------------------------------------
def create_remarks(self): for d in getlist(self.doclist, 'entries'):
r = [] if d.against_invoice and d.credit:
if self.doc.cheque_no : currency = sql("select currency from `tabSales Invoice` where name = '%s'" % d.against_invoice)
if self.doc.cheque_date: currency = currency and currency[0][0] or ''
r.append('Via cheque #%s dated %s' % (self.doc.cheque_no, formatdate(self.doc.cheque_date))) r.append('%s %s against Invoice: %s' % (cstr(currency), fmt_money(flt(d.credit)), d.against_invoice))
else : if d.against_voucher and d.debit:
msgprint("Please enter cheque date") bill_no = sql("select bill_no, bill_date, currency from `tabPurchase Invoice` where name=%s", d.against_voucher)
raise Exception if bill_no and bill_no[0][0] and bill_no[0][0].lower().strip() not in ['na', 'not applicable', 'none']:
bill_no = bill_no and bill_no[0]
for d in getlist(self.doclist, 'entries'): r.append('%s %s against Bill %s dated %s' % (bill_no[2] and cstr(bill_no[2]) or '', fmt_money(flt(d.debit)), bill_no[0], bill_no[1] and formatdate(bill_no[1].strftime('%Y-%m-%d')) or ''))
if d.against_invoice and d.credit: if self.doc.ded_amount:
currency = sql("select currency from `tabSales Invoice` where name = '%s'" % d.against_invoice) r.append("TDS Amount: %s" % self.doc.ded_amount)
currency = currency and currency[0][0] or ''
r.append('%s %s against Invoice: %s' % (cstr(currency), fmt_money(flt(d.credit)), d.against_invoice)) if self.doc.user_remark:
if d.against_voucher and d.debit: r.append("User Remark : %s"%self.doc.user_remark)
bill_no = sql("select bill_no, bill_date, currency from `tabPurchase Invoice` where name=%s", d.against_voucher)
if bill_no and bill_no[0][0] and bill_no[0][0].lower().strip() not in ['na', 'not applicable', 'none']: if r:
bill_no = bill_no and bill_no[0] self.doc.remark = ("\n").join(r)
r.append('%s %s against Bill %s dated %s' % (bill_no[2] and cstr(bill_no[2]) or '', fmt_money(flt(d.debit)), bill_no[0], bill_no[1] and formatdate(bill_no[1].strftime('%Y-%m-%d')) or ''))
if self.doc.ded_amount: def get_authorized_user(self):
r.append("TDS Amount: %s" % self.doc.ded_amount) if self.is_approving_authority==-1:
self.is_approving_authority = 0
if self.doc.user_remark:
r.append("User Remark : %s"%self.doc.user_remark) # Fetch credit controller role
approving_authority = sql("select value from `tabSingles` where field='credit_controller' and doctype='Global Defaults'")
if r: approving_authority = approving_authority and approving_authority[0][0] or ''
self.doc.remark = ("\n").join(r)
# Check logged-in user is authorized
# -------------------------------------------------------------------------------------------------------- if approving_authority in webnotes.user.get_roles():
# Check user role for approval process self.is_approving_authority = 1
# --------------------------------------------------------------------------------------------------------
def get_authorized_user(self): return self.is_approving_authority
if self.is_approving_authority==-1:
self.is_approving_authority = 0 def get_master_type(self, ac):
if not self.master_type.get(ac):
# Fetch credit controller role self.master_type[ac] = sql("select master_type from `tabAccount` where name=%s", ac)[0][0] or 'None'
approving_authority = sql("select value from `tabSingles` where field='credit_controller' and doctype='Global Defaults'") return self.master_type[ac]
approving_authority = approving_authority and approving_authority[0][0] or ''
def get_credit_days_for(self, ac):
# Check logged-in user is authorized if not self.credit_days_for.has_key(ac):
if approving_authority in webnotes.user.get_roles(): self.credit_days_for[ac] = sql("select credit_days from `tabAccount` where name='%s'" % ac)[0][0] or 0
self.is_approving_authority = 1
if not self.credit_days_for[ac]:
return self.is_approving_authority if self.credit_days_global==-1:
self.credit_days_global = sql("select credit_days from `tabCompany` where name='%s'" % self.doc.company)[0][0] or 0
# get master type return self.credit_days_global
# --------------- else:
def get_master_type(self, ac): return self.credit_days_for[ac]
if not self.master_type.get(ac):
self.master_type[ac] = sql("select master_type from `tabAccount` where name=%s", ac)[0][0] or 'None' def check_credit_days(self):
return self.master_type[ac] date_diff = 0
if self.doc.cheque_date:
# get credit days for date_diff = (getdate(self.doc.cheque_date)-getdate(self.doc.posting_date)).days
# -------------------
def get_credit_days_for(self, ac): if date_diff <= 0: return
if not self.credit_days_for.has_key(ac): # Get List of Customer Account
self.credit_days_for[ac] = sql("select credit_days from `tabAccount` where name='%s'" % ac)[0][0] or 0 acc_list = filter(lambda d: self.get_master_type(d.account)=='Customer', getlist(self.doclist,'entries'))
if not self.credit_days_for[ac]: for d in acc_list:
if self.credit_days_global==-1: credit_days = self.get_credit_days_for(d.account)
self.credit_days_global = sql("select credit_days from `tabCompany` where name='%s'" % self.doc.company)[0][0] or 0
return self.credit_days_global # Check credit days
else: if credit_days > 0 and not self.get_authorized_user() and cint(date_diff) > credit_days:
return self.credit_days_for[ac] msgprint("Credit Not Allowed: Cannot allow a check that is dated more than %s days after the posting date" % credit_days)
raise Exception
# -------------------------------------------------------------------------------------------------------- def check_account_against_entries(self):
# Check Credit Days - Cheque Date can not after (Posting date + Credit Days) for d in getlist(self.doclist,'entries'):
# -------------------------------------------------------------------------------------------------------- if d.against_invoice:
def check_credit_days(self): acc=sql("select debit_to from `tabSales Invoice` where name='%s'"%d.against_invoice)
date_diff = 0 if acc and acc[0][0] != d.account:
if self.doc.cheque_date: msgprint("Debit account is not matching with receivable voucher")
date_diff = (getdate(self.doc.cheque_date)-getdate(self.doc.posting_date)).days raise Exception
if date_diff <= 0: return if d.against_voucher:
acc=sql("select credit_to from `tabPurchase Invoice` where name='%s'"%d.against_voucher)
# Get List of Customer Account if acc and acc[0][0] != d.account:
acc_list = filter(lambda d: self.get_master_type(d.account)=='Customer', getlist(self.doclist,'entries')) msgprint("Credit account is not matching with payable voucher")
raise Exception
for d in acc_list:
credit_days = self.get_credit_days_for(d.account) def validate_cheque_info(self):
if self.doc.voucher_type in ['Bank Voucher']:
# Check credit days if not self.doc.cheque_no or not self.doc.cheque_date:
if credit_days > 0 and not self.get_authorized_user() and cint(date_diff) > credit_days: msgprint("Cheque No & Cheque Date is required for " + cstr(self.doc.voucher_type))
msgprint("Credit Not Allowed: Cannot allow a check that is dated more than %s days after the posting date" % credit_days) raise Exception
raise Exception
if self.doc.cheque_date and not self.doc.cheque_no:
#-------------------------------------------------------------------------------------------------------- msgprint("Cheque No is mandatory if you entered Cheque Date")
# validation of debit/credit account with Debit To Account(RV) or Credit To Account (PV) raise Exception
#--------------------------------------------------------------------------------------------------------
def check_account_against_entries(self): def validate_entries_for_advance(self):
for d in getlist(self.doclist,'entries'): for d in getlist(self.doclist,'entries'):
if d.against_invoice: if not d.is_advance and not d.against_voucher and not d.against_invoice and d.against_jv:
acc=sql("select debit_to from `tabSales Invoice` where name='%s'"%d.against_invoice) master_type = self.get_master_type(d.account)
if acc and acc[0][0] != d.account: if (master_type == 'Customer' and flt(d.credit) > 0) or (master_type == 'Supplier' and flt(d.debit) > 0):
msgprint("Debit account is not matching with receivable voucher") msgprint("Message: Please check Is Advance as 'Yes' against Account %s if this is an advance entry." % d.account)
raise Exception
def get_tds_category_account(self):
if d.against_voucher: for d in getlist(self.doclist,'entries'):
acc=sql("select credit_to from `tabPurchase Invoice` where name='%s'"%d.against_voucher) if flt(d.debit) > 0 and not d.against_voucher and d.is_advance == 'Yes':
if acc and acc[0][0] != d.account: acc = sql("select tds_applicable from `tabAccount` where name = '%s'" % d.account)
msgprint("Credit account is not matching with payable voucher") acc_tds_applicable = acc and acc[0][0] or 'No'
raise Exception if acc_tds_applicable == 'Yes':
# TDS applicable field become mandatory for advance payment towards supplier or related party
#-------------------------------------------------------------------------------------------------------- if not self.doc.tds_applicable:
# Validate Cheque Info: Mandatory for Bank/Contra voucher msgprint("Please select TDS Applicable or Not")
#-------------------------------------------------------------------------------------------------------- raise Exception
def validate_cheque_info(self):
if self.doc.voucher_type in ['Bank Voucher']: # If TDS applicable, category and supplier account bocome mandatory
if not self.doc.cheque_no or not self.doc.cheque_date: elif self.doc.tds_applicable == 'Yes':
msgprint("Cheque No & Cheque Date is required for " + cstr(self.doc.voucher_type)) self.validate_category_account(d.account)
raise Exception if self.doc.ded_amount and not self.doc.tax_code:
msgprint("Please enter Tax Code in TDS section")
if self.doc.cheque_date and not self.doc.cheque_no: raise Exception
msgprint("Cheque No is mandatory if you entered Cheque Date")
raise Exception #If TDS not applicable, all related fields should blank
else:
#-------------------------------------------------------------------------------------------------------- self.set_fields_null()
# Gives reminder for making is_advance = 'Yes' in Advance Entry
#-------------------------------------------------------------------------------------------------------- # If tds amount but tds applicability not mentioned in account master
def validate_entries_for_advance(self): elif self.doc.ded_amount:
for d in getlist(self.doclist,'entries'): msgprint("Please select TDS Applicable = 'Yes' in account head: '%s' if you want to deduct TDS." % self.doc.supplier_account)
if not d.is_advance and not d.against_voucher and not d.against_invoice and d.against_jv: raise Exception
master_type = self.get_master_type(d.account)
if (master_type == 'Customer' and flt(d.credit) > 0) or (master_type == 'Supplier' and flt(d.debit) > 0): def validate_category_account(self, credit_account):
msgprint("Message: Please check Is Advance as 'Yes' against Account %s if this is an advance entry." % d.account) if not self.doc.tds_category:
msgprint("Please select TDS Category")
#-------------------------------------------------------------------------------------------------------- raise Exception
# TDS: Validate tds related fields
#-------------------------------------------------------------------------------------------------------- if not self.doc.supplier_account:
def get_tds_category_account(self): self.doc.supplier_account = credit_account
for d in getlist(self.doclist,'entries'): elif self.doc.supplier_account and self.doc.supplier_account != credit_account:
if flt(d.debit) > 0 and not d.against_voucher and d.is_advance == 'Yes': msgprint("Supplier Account is not matching with the account mentioned in the table. Please select proper Supplier Account and click on 'Get TDS' button.")
acc = sql("select tds_applicable from `tabAccount` where name = '%s'" % d.account) raise Exception
acc_tds_applicable = acc and acc[0][0] or 'No'
if acc_tds_applicable == 'Yes': def set_fields_null(self):
# TDS applicable field become mandatory for advance payment towards supplier or related party self.doc.ded_amount = 0
if not self.doc.tds_applicable: self.doc.rate = 0
msgprint("Please select TDS Applicable or Not") self.doc.tax_code = ''
raise Exception self.doc.tds_category = ''
self.doc.supplier_account = ''
# If TDS applicable, category and supplier account bocome mandatory
elif self.doc.tds_applicable == 'Yes': def get_tds(self):
self.validate_category_account(d.account) if cstr(self.doc.is_opening) != 'Yes':
if self.doc.ded_amount and not self.doc.tax_code: if self.doc.total_debit > 0:
msgprint("Please enter Tax Code in TDS section") self.get_tds_category_account()
raise Exception if self.doc.supplier_account and self.doc.tds_category:
get_obj('TDS Control').get_tds_amount(self)
#If TDS not applicable, all related fields should blank
else: def get_balance(self):
self.set_fields_null() if not getlist(self.doclist,'entries'):
msgprint("Please enter atleast 1 entry in 'GL Entries' table")
# If tds amount but tds applicability not mentioned in account master else:
elif self.doc.ded_amount: flag, self.doc.total_debit, self.doc.total_credit = 0,0,0
msgprint("Please select TDS Applicable = 'Yes' in account head: '%s' if you want to deduct TDS." % self.doc.supplier_account) diff = flt(self.doc.difference)
raise Exception
# If any row without amount, set the diff on that row
for d in getlist(self.doclist,'entries'):
if (d.credit==0 or d.credit is None) and (d.debit==0 or d.debit is None) and (flt(diff) != 0):
#-------------------------------------------------------------------------------------------------------- if diff>0:
# If TDS applicable , TDS category and supplier account should be mandatory d.credit = flt(diff)
#-------------------------------------------------------------------------------------------------------- elif diff<0:
def validate_category_account(self, credit_account): d.debit = flt(diff)
if not self.doc.tds_category: flag = 1
msgprint("Please select TDS Category")
raise Exception # Set the diff in a new row
if flag == 0 and (flt(diff) != 0):
if not self.doc.supplier_account: jd = addchild(self.doc, 'entries', 'Journal Voucher Detail', 1, self.doclist)
self.doc.supplier_account = credit_account if diff>0:
elif self.doc.supplier_account and self.doc.supplier_account != credit_account: jd.credit = flt(diff)
msgprint("Supplier Account is not matching with the account mentioned in the table. Please select proper Supplier Account and click on 'Get TDS' button.") elif diff<0:
raise Exception jd.debit = flt(diff)
# Set the total debit, total credit and difference
#-------------------------------------------------------------------------------------------------------- for d in getlist(self.doclist,'entries'):
# If TDS is not applicable , all related fields should blank self.doc.total_debit += flt(d.debit)
#-------------------------------------------------------------------------------------------------------- self.doc.total_credit += flt(d.credit)
def set_fields_null(self):
self.doc.ded_amount = 0 if self.doc.tds_applicable == 'Yes':
self.doc.rate = 0 self.doc.total_credit = flt(self.doc.total_credit) + flt(self.doc.ded_amount)
self.doc.tax_code = ''
self.doc.tds_category = '' self.doc.difference = flt(self.doc.total_debit) - flt(self.doc.total_credit)
self.doc.supplier_account = ''
def get_against_account(self):
#-------------------------------------------------------------------------------------------------------- # Debit = Credit
# Get TDS amount debit, credit = 0.0, 0.0
#-------------------------------------------------------------------------------------------------------- debit_list, credit_list = [], []
def get_tds(self): for d in getlist(self.doclist, 'entries'):
if cstr(self.doc.is_opening) != 'Yes': debit += flt(d.debit)
if self.doc.total_debit > 0: credit += flt(d.credit)
self.get_tds_category_account() if flt(d.debit)>0 and (d.account not in debit_list): debit_list.append(d.account)
if self.doc.supplier_account and self.doc.tds_category: if flt(d.credit)>0 and (d.account not in credit_list): credit_list.append(d.account)
get_obj('TDS Control').get_tds_amount(self)
self.doc.total_debit = debit
if self.doc.tds_applicable == 'Yes':
#-------------------------------------------------------------------------------------------------------- self.doc.total_credit = credit + flt(self.doc.ded_amount)
# Insert new row to balance total debit and total credit else:
#-------------------------------------------------------------------------------------------------------- self.doc.total_credit = credit
def get_balance(self):
if not getlist(self.doclist,'entries'): if abs(self.doc.total_debit-self.doc.total_credit) > 0.001:
msgprint("Please enter atleast 1 entry in 'GL Entries' table") msgprint("Debit must be equal to Credit. The difference is %s" % (self.doc.total_debit-self.doc.total_credit))
else: raise Exception
flag, self.doc.total_debit, self.doc.total_credit = 0,0,0
diff = flt(self.doc.difference) # update against account
for d in getlist(self.doclist, 'entries'):
# If any row without amount, set the diff on that row if flt(d.debit) > 0: d.against_account = ', '.join(credit_list)
for d in getlist(self.doclist,'entries'): if flt(d.credit) > 0: d.against_account = ', '.join(debit_list)
if (d.credit==0 or d.credit is None) and (d.debit==0 or d.debit is None) and (flt(diff) != 0):
if diff>0: def set_aging_date(self):
d.credit = flt(diff) if self.doc.is_opening != 'Yes':
elif diff<0: self.doc.aging_date = self.doc.posting_date
d.debit = flt(diff) else:
flag = 1 # check account type whether supplier or customer
exists = ''
# Set the diff in a new row for d in getlist(self.doclist, 'entries'):
if flag == 0 and (flt(diff) != 0): exists = sql("select name from tabAccount where account_type in ('Supplier', 'Customer') and name = '%s'" % d.account)
jd = addchild(self.doc, 'entries', 'Journal Voucher Detail', 1, self.doclist) if exists:
if diff>0: break
jd.credit = flt(diff)
elif diff<0: # If cus/supp aging dt is mandatory
jd.debit = flt(diff) if exists and not self.doc.aging_date:
msgprint("Aging Date is mandatory for opening entry")
# Set the total debit, total credit and difference raise Exception
for d in getlist(self.doclist,'entries'): # otherwise aging dt = posting dt
self.doc.total_debit += flt(d.debit) else:
self.doc.total_credit += flt(d.credit) self.doc.aging_date = self.doc.posting_date
if self.doc.tds_applicable == 'Yes': def set_print_format_fields(self):
self.doc.total_credit = flt(self.doc.total_credit) + flt(self.doc.ded_amount) for d in getlist(self.doclist, 'entries'):
#msgprint(self.doc.company)
self.doc.difference = flt(self.doc.total_debit) - flt(self.doc.total_credit) chk_type = sql("select master_type, account_type from `tabAccount` where name='%s'" % d.account)
master_type, acc_type = chk_type and cstr(chk_type[0][0]) or '', chk_type and cstr(chk_type[0][1]) or ''
#-------------------------------------------------------------------------------------------------------- if master_type in ['Supplier', 'Customer']:
# Set against account if not self.doc.pay_to_recd_from:
#-------------------------------------------------------------------------------------------------------- self.doc.pay_to_recd_from = get_value(master_type, ' - '.join(d.account.split(' - ')[:-1]), master_type == 'Customer' and 'customer_name' or 'supplier_name')
def get_against_account(self):
# Debit = Credit if acc_type == 'Bank or Cash':
debit, credit = 0.0, 0.0 dcc = TransactionBase().get_company_currency(self.doc.company)
debit_list, credit_list = [], [] amt = cint(d.debit) and d.debit or d.credit
for d in getlist(self.doclist, 'entries'): self.doc.total_amount = dcc +' '+ cstr(amt)
debit += flt(d.debit) self.doc.total_amount_in_words = get_obj('Sales Common').get_total_in_words(dcc, cstr(amt))
credit += flt(d.credit)
if flt(d.debit)>0 and (d.account not in debit_list): debit_list.append(d.account) def get_values(self):
if flt(d.credit)>0 and (d.account not in credit_list): credit_list.append(d.account) cond = (flt(self.doc.write_off_amount) > 0) and ' and outstanding_amount <= '+self.doc.write_off_amount or ''
if self.doc.write_off_based_on == 'Accounts Receivable':
self.doc.total_debit = debit return sql("select name, debit_to, outstanding_amount from `tabSales Invoice` where docstatus = 1 and company = '%s' and outstanding_amount > 0 %s" % (self.doc.company, cond))
if self.doc.tds_applicable == 'Yes': elif self.doc.write_off_based_on == 'Accounts Payable':
self.doc.total_credit = credit + flt(self.doc.ded_amount) return sql("select name, credit_to, outstanding_amount from `tabPurchase Invoice` where docstatus = 1 and company = '%s' and outstanding_amount > 0 %s" % (self.doc.company, cond))
else:
self.doc.total_credit = credit
def get_outstanding_invoices(self):
if abs(self.doc.total_debit-self.doc.total_credit) > 0.001: self.doclist = self.doc.clear_table(self.doclist, 'entries')
msgprint("Debit must be equal to Credit. The difference is %s" % (self.doc.total_debit-self.doc.total_credit)) total = 0
raise Exception for d in self.get_values():
total += flt(d[2])
# update against account jd = addchild(self.doc, 'entries', 'Journal Voucher Detail', 1, self.doclist)
for d in getlist(self.doclist, 'entries'): jd.account = cstr(d[1])
if flt(d.debit) > 0: d.against_account = ', '.join(credit_list) if self.doc.write_off_based_on == 'Accounts Receivable':
if flt(d.credit) > 0: d.against_account = ', '.join(debit_list) jd.credit = flt(d[2])
jd.against_invoice = cstr(d[0])
elif self.doc.write_off_based_on == 'Accounts Payable':
# set aging date jd.debit = flt(d[2])
#--------------- jd.against_voucher = cstr(d[0])
def set_aging_date(self): jd.save(1)
if self.doc.is_opening != 'Yes': jd = addchild(self.doc, 'entries', 'Journal Voucher Detail', 1, self.doclist)
self.doc.aging_date = self.doc.posting_date if self.doc.write_off_based_on == 'Accounts Receivable':
else: jd.debit = total
# check account type whether supplier or customer elif self.doc.write_off_based_on == 'Accounts Payable':
exists = '' jd.credit = total
for d in getlist(self.doclist, 'entries'): jd.save(1)
exists = sql("select name from tabAccount where account_type in ('Supplier', 'Customer') and name = '%s'" % d.account)
if exists: def validate(self):
break if not self.doc.is_opening:
self.doc.is_opening='No'
# If cus/supp aging dt is mandatory self.validate_debit_credit()
if exists and not self.doc.aging_date: self.get_against_account()
msgprint("Aging Date is mandatory for opening entry") self.validate_cheque_info()
raise Exception self.create_remarks()
# otherwise aging dt = posting dt # tds
else: get_obj('TDS Control').validate_first_entry(self)
self.doc.aging_date = self.doc.posting_date self.get_tds_category_account()
# ------------------------ self.validate_entries_for_advance()
# set print format fields self.set_aging_date()
# ------------------------
def set_print_format_fields(self): self.validate_against_jv()
for d in getlist(self.doclist, 'entries'): self.set_print_format_fields()
#msgprint(self.doc.company)
chk_type = sql("select master_type, account_type from `tabAccount` where name='%s'" % d.account) #FY and Date validation
master_type, acc_type = chk_type and cstr(chk_type[0][0]) or '', chk_type and cstr(chk_type[0][1]) or '' get_obj('Sales Common').validate_fiscal_year(self.doc.fiscal_year, \
if master_type in ['Supplier', 'Customer']: self.doc.posting_date, 'Posting Date')
if not self.doc.pay_to_recd_from:
self.doc.pay_to_recd_from = get_value(master_type, ' - '.join(d.account.split(' - ')[:-1]), master_type == 'Customer' and 'customer_name' or 'supplier_name') def validate_debit_credit(self):
for d in getlist(self.doclist, 'entries'):
if acc_type == 'Bank or Cash': if d.debit and d.credit:
dcc = TransactionBase().get_company_currency(self.doc.company) msgprint("You cannot credit and debit same account at the same time.",
amt = cint(d.debit) and d.debit or d.credit raise_exception=1)
self.doc.total_amount = dcc +' '+ cstr(amt)
self.doc.total_amount_in_words = get_obj('Sales Common').get_total_in_words(dcc, cstr(amt)) def on_update(self):
pass
# -------------------------------- def on_submit(self):
# get outstanding invoices values if self.doc.voucher_type in ['Bank Voucher', 'Contra Voucher', 'Journal Entry']:
# -------------------------------- self.check_credit_days()
def get_values(self): self.check_account_against_entries()
cond = (flt(self.doc.write_off_amount) > 0) and ' and outstanding_amount <= '+self.doc.write_off_amount or '' get_obj(dt='GL Control').make_gl_entries(self.doc, self.doclist)
if self.doc.write_off_based_on == 'Accounts Receivable':
return sql("select name, debit_to, outstanding_amount from `tabSales Invoice` where docstatus = 1 and company = '%s' and outstanding_amount > 0 %s" % (self.doc.company, cond)) def validate_against_jv(self):
elif self.doc.write_off_based_on == 'Accounts Payable': for d in getlist(self.doclist, 'entries'):
return sql("select name, credit_to, outstanding_amount from `tabPurchase Invoice` where docstatus = 1 and company = '%s' and outstanding_amount > 0 %s" % (self.doc.company, cond)) if d.against_jv:
if d.against_jv == self.doc.name:
msgprint("You can not enter current voucher in 'Against JV' column")
# ------------------------- raise Exception
# get outstanding invoices elif not sql("select name from `tabJournal Voucher Detail` where account = '%s' and docstatus = 1 and parent = '%s'" % (d.account, d.against_jv)):
# ------------------------- msgprint("Against JV: "+ d.against_jv + " is not valid. Please check")
def get_outstanding_invoices(self): raise Exception
self.doclist = self.doc.clear_table(self.doclist, 'entries')
total = 0 def on_cancel(self):
for d in self.get_values(): self.check_tds_payment_voucher()
total += flt(d[2]) get_obj(dt='GL Control').make_gl_entries(self.doc, self.doclist, cancel=1)
jd = addchild(self.doc, 'entries', 'Journal Voucher Detail', 1, self.doclist)
jd.account = cstr(d[1]) def check_tds_payment_voucher(self):
if self.doc.write_off_based_on == 'Accounts Receivable': tdsp = sql("select parent from `tabTDS Payment Detail` where voucher_no = '%s' and docstatus = 1 and parent not like 'old%'")
jd.credit = flt(d[2]) if tdsp:
jd.against_invoice = cstr(d[0]) msgprint("TDS Payment voucher '%s' has been made against this voucher. Please cancel the payment voucher to proceed." % (tdsp and tdsp[0][0] or ''))
elif self.doc.write_off_based_on == 'Accounts Payable': raise Exception
jd.debit = flt(d[2])
jd.against_voucher = cstr(d[0])
jd.save(1)
jd = addchild(self.doc, 'entries', 'Journal Voucher Detail', 1, self.doclist)
if self.doc.write_off_based_on == 'Accounts Receivable':
jd.debit = total
elif self.doc.write_off_based_on == 'Accounts Payable':
jd.credit = total
jd.save(1)
#--------------------------------------------------------------------------------------------------------
# VALIDATE
#--------------------------------------------------------------------------------------------------------
def validate(self):
if not self.doc.is_opening:
self.doc.is_opening='No'
self.get_against_account()
self.validate_cheque_info()
self.create_remarks()
# tds
get_obj('TDS Control').validate_first_entry(self)
self.get_tds_category_account()
self.validate_entries_for_advance()
self.set_aging_date()
self.validate_against_jv()
self.set_print_format_fields()
#FY and Date validation
get_obj('Sales Common').validate_fiscal_year(self.doc.fiscal_year,self.doc.posting_date,'Posting Date')
#--------------------------------------------------------------------------------------------------------
# On Update - Update Feed
#--------------------------------------------------------------------------------------------------------
def on_update(self):
pass
#--------------------------------------------------------------------------------------------------------
# On submit
#--------------------------------------------------------------------------------------------------------
def on_submit(self):
if self.doc.voucher_type in ['Bank Voucher', 'Contra Voucher', 'Journal Entry']:
self.check_credit_days()
self.check_account_against_entries()
get_obj(dt='GL Control').make_gl_entries(self.doc, self.doclist)
# validate against jv no
def validate_against_jv(self):
for d in getlist(self.doclist, 'entries'):
if d.against_jv:
if d.against_jv == self.doc.name:
msgprint("You can not enter current voucher in 'Against JV' column")
raise Exception
elif not sql("select name from `tabJournal Voucher Detail` where account = '%s' and docstatus = 1 and parent = '%s'" % (d.account, d.against_jv)):
msgprint("Against JV: "+ d.against_jv + " is not valid. Please check")
raise Exception
#--------------------------------------------------------------------------------------------------------
# On cancel reverse gl entry
#--------------------------------------------------------------------------------------------------------
def on_cancel(self):
self.check_tds_payment_voucher()
get_obj(dt='GL Control').make_gl_entries(self.doc, self.doclist, cancel=1)
# Check whether tds payment voucher has been created against this voucher
#---------------------------------------------------------------------------
def check_tds_payment_voucher(self):
tdsp = sql("select parent from `tabTDS Payment Detail` where voucher_no = '%s' and docstatus = 1 and parent not like 'old%'")
if tdsp:
msgprint("TDS Payment voucher '%s' has been made against this voucher. Please cancel the payment voucher to proceed." % (tdsp and tdsp[0][0] or ''))
raise Exception