2013-11-20 07:29:58 +00:00
|
|
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
2013-01-30 07:19:08 +00:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
|
|
|
from frappe.utils import flt, cstr
|
|
|
|
from frappe import _
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.accounts.utils import validate_expense_against_budget
|
2013-01-30 07:19:08 +00:00
|
|
|
|
2013-08-29 12:49:37 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
class StockAccountInvalidTransaction(frappe.ValidationError): pass
|
2013-01-30 07:19:08 +00:00
|
|
|
|
|
|
|
def make_gl_entries(gl_map, cancel=False, adv_adj=False, merge_entries=True,
|
|
|
|
update_outstanding='Yes'):
|
2013-08-28 13:23:11 +00:00
|
|
|
if gl_map:
|
|
|
|
if not cancel:
|
|
|
|
gl_map = process_gl_map(gl_map, merge_entries)
|
|
|
|
save_entries(gl_map, adv_adj, update_outstanding)
|
|
|
|
else:
|
|
|
|
delete_gl_entries(gl_map, adv_adj=adv_adj, update_outstanding=update_outstanding)
|
2013-01-30 07:19:08 +00:00
|
|
|
|
2013-08-26 11:23:30 +00:00
|
|
|
def process_gl_map(gl_map, merge_entries=True):
|
2013-01-30 07:19:08 +00:00
|
|
|
if merge_entries:
|
|
|
|
gl_map = merge_similar_entries(gl_map)
|
|
|
|
|
2013-08-26 11:23:30 +00:00
|
|
|
for entry in gl_map:
|
|
|
|
# toggle debit, credit if negative entry
|
2013-10-23 10:59:19 +00:00
|
|
|
if flt(entry.debit) < 0:
|
|
|
|
entry.credit = flt(entry.credit) - flt(entry.debit)
|
|
|
|
entry.debit = 0.0
|
|
|
|
if flt(entry.credit) < 0:
|
|
|
|
entry.debit = flt(entry.debit) - flt(entry.credit)
|
|
|
|
entry.credit = 0.0
|
2013-02-14 08:08:12 +00:00
|
|
|
|
2013-08-26 11:23:30 +00:00
|
|
|
return gl_map
|
2013-01-30 07:19:08 +00:00
|
|
|
|
|
|
|
def merge_similar_entries(gl_map):
|
|
|
|
merged_gl_map = []
|
|
|
|
for entry in gl_map:
|
|
|
|
# if there is already an entry in this account then just add it
|
|
|
|
# to that entry
|
|
|
|
same_head = check_if_in_list(entry, merged_gl_map)
|
|
|
|
if same_head:
|
2013-08-28 13:23:11 +00:00
|
|
|
same_head.debit = flt(same_head.debit) + flt(entry.debit)
|
|
|
|
same_head.credit = flt(same_head.credit) + flt(entry.credit)
|
2013-01-30 07:19:08 +00:00
|
|
|
else:
|
|
|
|
merged_gl_map.append(entry)
|
2013-12-23 06:44:45 +00:00
|
|
|
|
2013-08-07 11:30:01 +00:00
|
|
|
# filter zero debit and credit entries
|
2013-08-28 13:23:11 +00:00
|
|
|
merged_gl_map = filter(lambda x: flt(x.debit)!=0 or flt(x.credit)!=0, merged_gl_map)
|
2013-01-30 07:19:08 +00:00
|
|
|
return merged_gl_map
|
|
|
|
|
2013-08-29 12:49:37 +00:00
|
|
|
def check_if_in_list(gle, gl_map):
|
|
|
|
for e in gl_map:
|
2013-08-28 13:23:11 +00:00
|
|
|
if e.account == gle.account and \
|
2013-01-30 07:19:08 +00:00
|
|
|
cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \
|
|
|
|
and cstr(e.get('against_voucher_type')) == \
|
|
|
|
cstr(gle.get('against_voucher_type')) \
|
|
|
|
and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')):
|
|
|
|
return e
|
|
|
|
|
2013-08-21 12:17:11 +00:00
|
|
|
def save_entries(gl_map, adv_adj, update_outstanding):
|
2013-08-29 12:49:37 +00:00
|
|
|
validate_account_for_auto_accounting_for_stock(gl_map)
|
|
|
|
|
2013-01-30 07:19:08 +00:00
|
|
|
total_debit = total_credit = 0.0
|
|
|
|
for entry in gl_map:
|
2013-08-23 09:47:36 +00:00
|
|
|
make_entry(entry, adv_adj, update_outstanding)
|
2013-08-26 11:23:30 +00:00
|
|
|
# check against budget
|
2013-08-23 05:16:41 +00:00
|
|
|
validate_expense_against_budget(entry)
|
2013-02-13 10:20:39 +00:00
|
|
|
|
2013-01-30 07:19:08 +00:00
|
|
|
|
|
|
|
# update total debit / credit
|
2013-08-28 13:23:11 +00:00
|
|
|
total_debit += flt(entry.debit)
|
|
|
|
total_credit += flt(entry.credit)
|
2013-01-30 07:19:08 +00:00
|
|
|
|
2013-08-21 12:17:11 +00:00
|
|
|
validate_total_debit_credit(total_debit, total_credit)
|
2013-01-30 07:19:08 +00:00
|
|
|
|
2013-08-23 09:47:36 +00:00
|
|
|
def make_entry(args, adv_adj, update_outstanding):
|
|
|
|
args.update({"doctype": "GL Entry"})
|
2014-02-14 10:17:51 +00:00
|
|
|
gle = frappe.bean([args])
|
2013-08-23 09:47:36 +00:00
|
|
|
gle.ignore_permissions = 1
|
|
|
|
gle.insert()
|
|
|
|
gle.run_method("on_update_with_args", adv_adj, update_outstanding)
|
|
|
|
gle.submit()
|
2013-01-30 07:19:08 +00:00
|
|
|
|
|
|
|
def validate_total_debit_credit(total_debit, total_credit):
|
|
|
|
if abs(total_debit - total_credit) > 0.005:
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.throw(_("Debit and Credit not equal for this voucher: Diff (Debit) is ") +
|
2013-08-21 12:17:11 +00:00
|
|
|
cstr(total_debit - total_credit))
|
2013-08-29 12:49:37 +00:00
|
|
|
|
|
|
|
def validate_account_for_auto_accounting_for_stock(gl_map):
|
|
|
|
if gl_map[0].voucher_type=="Journal Voucher":
|
2014-02-26 07:05:33 +00:00
|
|
|
aii_accounts = [d[0] for d in frappe.db.sql("""select name from tabAccount
|
2013-09-17 09:45:16 +00:00
|
|
|
where account_type = 'Warehouse' and ifnull(master_name, '')!=''""")]
|
2013-08-29 12:49:37 +00:00
|
|
|
|
|
|
|
for entry in gl_map:
|
|
|
|
if entry.account in aii_accounts:
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.throw(_("Account") + ": " + entry.account +
|
2013-08-29 12:49:37 +00:00
|
|
|
_(" can only be debited/credited through Stock transactions"),
|
|
|
|
StockAccountInvalidTransaction)
|
|
|
|
|
2013-08-21 12:17:11 +00:00
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None,
|
|
|
|
adv_adj=False, update_outstanding="Yes"):
|
|
|
|
|
2014-03-03 13:48:17 +00:00
|
|
|
from erpnext.accounts.doctype.gl_entry.gl_entry import validate_balance_type, \
|
2013-09-24 09:06:55 +00:00
|
|
|
check_freezing_date, update_outstanding_amt, validate_frozen_account
|
2013-08-28 13:23:11 +00:00
|
|
|
|
|
|
|
if not gl_entries:
|
2014-02-26 07:05:33 +00:00
|
|
|
gl_entries = frappe.db.sql("""select * from `tabGL Entry`
|
2013-08-28 13:23:11 +00:00
|
|
|
where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no), as_dict=True)
|
2013-08-26 11:23:30 +00:00
|
|
|
if gl_entries:
|
|
|
|
check_freezing_date(gl_entries[0]["posting_date"], adv_adj)
|
2013-08-21 12:17:11 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.sql("""delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s""",
|
2013-08-28 13:23:11 +00:00
|
|
|
(voucher_type or gl_entries[0]["voucher_type"], voucher_no or gl_entries[0]["voucher_no"]))
|
2013-08-21 12:17:11 +00:00
|
|
|
|
|
|
|
for entry in gl_entries:
|
2013-09-24 09:06:55 +00:00
|
|
|
validate_frozen_account(entry["account"], adv_adj)
|
2014-03-03 13:48:17 +00:00
|
|
|
validate_balance_type(entry["account"], adv_adj)
|
2013-08-22 12:55:43 +00:00
|
|
|
validate_expense_against_budget(entry)
|
|
|
|
|
2013-08-21 12:17:11 +00:00
|
|
|
if entry.get("against_voucher") and entry.get("against_voucher_type") != "POS" \
|
|
|
|
and update_outstanding == 'Yes':
|
|
|
|
update_outstanding_amt(entry["account"], entry.get("against_voucher_type"),
|
2013-08-23 09:47:36 +00:00
|
|
|
entry.get("against_voucher"), on_cancel=True)
|