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-29 06:02:38 +00:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
|
|
|
from frappe.utils import nowdate, cstr, flt, now, getdate, add_months
|
|
|
|
from frappe import msgprint, throw, _
|
|
|
|
from frappe.utils import formatdate
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.utilities import build_filter_conditions
|
2013-02-08 08:34:13 +00:00
|
|
|
|
2013-01-29 06:02:38 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
class FiscalYearError(frappe.ValidationError): pass
|
|
|
|
class BudgetError(frappe.ValidationError): pass
|
2013-08-22 12:55:43 +00:00
|
|
|
|
2013-01-29 06:02:38 +00:00
|
|
|
|
2013-07-11 12:19:18 +00:00
|
|
|
def get_fiscal_year(date=None, fiscal_year=None, label="Date", verbose=1):
|
2013-12-23 11:37:57 +00:00
|
|
|
return get_fiscal_years(date, fiscal_year, label, verbose)[0]
|
2013-02-05 17:55:37 +00:00
|
|
|
|
2013-07-11 12:19:18 +00:00
|
|
|
def get_fiscal_years(date=None, fiscal_year=None, label="Date", verbose=1):
|
2013-01-29 06:02:38 +00:00
|
|
|
# if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate)
|
2013-06-20 11:05:09 +00:00
|
|
|
cond = ""
|
|
|
|
if fiscal_year:
|
2014-03-03 10:21:13 +00:00
|
|
|
cond = "name = '%s'" % fiscal_year.replace("'", "\'")
|
2013-06-20 11:05:09 +00:00
|
|
|
else:
|
2013-11-25 14:21:18 +00:00
|
|
|
cond = "'%s' >= year_start_date and '%s' <= year_end_date" % \
|
2013-06-20 11:05:09 +00:00
|
|
|
(date, date)
|
2014-02-26 07:05:33 +00:00
|
|
|
fy = frappe.db.sql("""select name, year_start_date, year_end_date
|
2013-11-25 14:21:18 +00:00
|
|
|
from `tabFiscal Year` where %s order by year_start_date desc""" % cond)
|
2013-01-29 06:02:38 +00:00
|
|
|
|
|
|
|
if not fy:
|
2013-07-11 12:19:18 +00:00
|
|
|
error_msg = """%s %s not in any Fiscal Year""" % (label, formatdate(date))
|
2014-01-03 06:43:18 +00:00
|
|
|
error_msg = """{msg}: {date}""".format(msg=_("Fiscal Year does not exist for date"),
|
|
|
|
date=formatdate(date))
|
2014-02-14 10:17:51 +00:00
|
|
|
if verbose: frappe.msgprint(error_msg)
|
2013-01-29 06:02:38 +00:00
|
|
|
raise FiscalYearError, error_msg
|
|
|
|
|
2013-02-05 17:55:37 +00:00
|
|
|
return fy
|
2013-02-05 06:01:27 +00:00
|
|
|
|
|
|
|
def validate_fiscal_year(date, fiscal_year, label="Date"):
|
2013-07-11 12:19:18 +00:00
|
|
|
years = [f[0] for f in get_fiscal_years(date, label=label)]
|
2013-02-05 17:55:37 +00:00
|
|
|
if fiscal_year not in years:
|
2014-01-29 11:01:38 +00:00
|
|
|
throw(("%(label)s '%(posting_date)s': " + _("not within Fiscal Year") + \
|
2013-02-05 06:01:27 +00:00
|
|
|
": '%(fiscal_year)s'") % {
|
|
|
|
"label": label,
|
|
|
|
"posting_date": formatdate(date),
|
|
|
|
"fiscal_year": fiscal_year
|
2014-01-29 11:01:38 +00:00
|
|
|
})
|
2013-01-29 06:02:38 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2013-01-29 06:02:38 +00:00
|
|
|
def get_balance_on(account=None, date=None):
|
2014-02-14 10:17:51 +00:00
|
|
|
if not account and frappe.form_dict.get("account"):
|
|
|
|
account = frappe.form_dict.get("account")
|
|
|
|
date = frappe.form_dict.get("date")
|
2013-01-29 06:02:38 +00:00
|
|
|
|
|
|
|
cond = []
|
|
|
|
if date:
|
|
|
|
cond.append("posting_date <= '%s'" % date)
|
|
|
|
else:
|
|
|
|
# get balance of all entries that exist
|
|
|
|
date = nowdate()
|
|
|
|
|
|
|
|
try:
|
|
|
|
year_start_date = get_fiscal_year(date, verbose=0)[1]
|
|
|
|
except FiscalYearError, e:
|
|
|
|
if getdate(date) > getdate(nowdate()):
|
|
|
|
# if fiscal year not found and the date is greater than today
|
|
|
|
# get fiscal year for today's date and its corresponding year start date
|
|
|
|
year_start_date = get_fiscal_year(nowdate(), verbose=1)[1]
|
|
|
|
else:
|
|
|
|
# this indicates that it is a date older than any existing fiscal year.
|
|
|
|
# hence, assuming balance as 0.0
|
|
|
|
return 0.0
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
acc = frappe.db.get_value('Account', account, \
|
2014-03-25 12:58:49 +00:00
|
|
|
['lft', 'rgt', 'report_type', 'group_or_ledger'], as_dict=1)
|
2013-01-29 06:02:38 +00:00
|
|
|
|
|
|
|
# for pl accounts, get balance within a fiscal year
|
2014-03-25 12:58:49 +00:00
|
|
|
if acc.report_type == 'Profit and Loss':
|
2013-01-29 06:02:38 +00:00
|
|
|
cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" \
|
|
|
|
% year_start_date)
|
|
|
|
|
|
|
|
# different filter for group and ledger - improved performance
|
|
|
|
if acc.group_or_ledger=="Group":
|
|
|
|
cond.append("""exists (
|
|
|
|
select * from `tabAccount` ac where ac.name = gle.account
|
|
|
|
and ac.lft >= %s and ac.rgt <= %s
|
|
|
|
)""" % (acc.lft, acc.rgt))
|
|
|
|
else:
|
2014-03-03 10:21:13 +00:00
|
|
|
cond.append("""gle.account = "%s" """ % (account.replace('"', '\"'), ))
|
2013-01-29 06:02:38 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
bal = frappe.db.sql("""
|
2013-01-29 06:02:38 +00:00
|
|
|
SELECT sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))
|
|
|
|
FROM `tabGL Entry` gle
|
2013-08-21 12:17:11 +00:00
|
|
|
WHERE %s""" % " and ".join(cond))[0][0]
|
2013-01-29 06:02:38 +00:00
|
|
|
|
|
|
|
# if bal is None, return 0
|
2013-07-19 10:03:11 +00:00
|
|
|
return flt(bal)
|
2013-01-30 13:46:13 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2013-01-30 13:46:13 +00:00
|
|
|
def add_ac(args=None):
|
|
|
|
if not args:
|
2014-02-14 10:17:51 +00:00
|
|
|
args = frappe.local.form_dict
|
2013-01-30 13:46:13 +00:00
|
|
|
args.pop("cmd")
|
2013-02-15 09:09:34 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
ac = frappe.bean(args)
|
2013-01-30 13:46:13 +00:00
|
|
|
ac.doc.doctype = "Account"
|
|
|
|
ac.doc.old_parent = ""
|
|
|
|
ac.doc.freeze_account = "No"
|
|
|
|
ac.insert()
|
|
|
|
return ac.doc.name
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2013-01-30 13:46:13 +00:00
|
|
|
def add_cc(args=None):
|
|
|
|
if not args:
|
2014-02-14 10:17:51 +00:00
|
|
|
args = frappe.local.form_dict
|
2013-01-30 13:46:13 +00:00
|
|
|
args.pop("cmd")
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
cc = frappe.bean(args)
|
2013-01-30 13:46:13 +00:00
|
|
|
cc.doc.doctype = "Cost Center"
|
|
|
|
cc.doc.old_parent = ""
|
|
|
|
cc.insert()
|
|
|
|
return cc.doc.name
|
|
|
|
|
|
|
|
def reconcile_against_document(args):
|
|
|
|
"""
|
|
|
|
Cancel JV, Update aginst document, split if required and resubmit jv
|
|
|
|
"""
|
|
|
|
for d in args:
|
|
|
|
check_if_jv_modified(d)
|
|
|
|
|
|
|
|
against_fld = {
|
|
|
|
'Journal Voucher' : 'against_jv',
|
|
|
|
'Sales Invoice' : 'against_invoice',
|
|
|
|
'Purchase Invoice' : 'against_voucher'
|
|
|
|
}
|
|
|
|
|
|
|
|
d['against_fld'] = against_fld[d['against_voucher_type']]
|
|
|
|
|
|
|
|
# cancel JV
|
2014-02-14 10:17:51 +00:00
|
|
|
jv_obj = frappe.get_obj('Journal Voucher', d['voucher_no'], with_children=1)
|
2013-01-30 13:46:13 +00:00
|
|
|
jv_obj.make_gl_entries(cancel=1, adv_adj=1)
|
|
|
|
|
|
|
|
# update ref in JV Detail
|
|
|
|
update_against_doc(d, jv_obj)
|
|
|
|
|
|
|
|
# re-submit JV
|
2014-02-14 10:17:51 +00:00
|
|
|
jv_obj = frappe.get_obj('Journal Voucher', d['voucher_no'], with_children =1)
|
2013-01-30 13:46:13 +00:00
|
|
|
jv_obj.make_gl_entries(cancel = 0, adv_adj =1)
|
|
|
|
|
|
|
|
|
|
|
|
def check_if_jv_modified(args):
|
|
|
|
"""
|
|
|
|
check if there is already a voucher reference
|
|
|
|
check if amount is same
|
|
|
|
check if jv is submitted
|
|
|
|
"""
|
2014-02-26 07:05:33 +00:00
|
|
|
ret = frappe.db.sql("""
|
2013-01-30 13:46:13 +00:00
|
|
|
select t2.%(dr_or_cr)s from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2
|
|
|
|
where t1.name = t2.parent and t2.account = '%(account)s'
|
|
|
|
and ifnull(t2.against_voucher, '')=''
|
|
|
|
and ifnull(t2.against_invoice, '')='' and ifnull(t2.against_jv, '')=''
|
|
|
|
and t1.name = '%(voucher_no)s' and t2.name = '%(voucher_detail_no)s'
|
|
|
|
and t1.docstatus=1 and t2.%(dr_or_cr)s = %(unadjusted_amt)s""" % args)
|
|
|
|
|
|
|
|
if not ret:
|
2014-01-29 11:01:38 +00:00
|
|
|
throw(_("""Payment Entry has been modified after you pulled it. Please pull it again."""))
|
2013-01-30 13:46:13 +00:00
|
|
|
|
|
|
|
def update_against_doc(d, jv_obj):
|
|
|
|
"""
|
|
|
|
Updates against document, if partial amount splits into rows
|
|
|
|
"""
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.sql("""
|
2013-01-30 13:46:13 +00:00
|
|
|
update `tabJournal Voucher Detail` t1, `tabJournal Voucher` t2
|
|
|
|
set t1.%(dr_or_cr)s = '%(allocated_amt)s',
|
|
|
|
t1.%(against_fld)s = '%(against_voucher)s', t2.modified = now()
|
|
|
|
where t1.name = '%(voucher_detail_no)s' and t1.parent = t2.name""" % d)
|
|
|
|
|
|
|
|
if d['allocated_amt'] < d['unadjusted_amt']:
|
2014-02-26 07:05:33 +00:00
|
|
|
jvd = frappe.db.sql("""select cost_center, balance, against_account, is_advance
|
2013-01-30 13:46:13 +00:00
|
|
|
from `tabJournal Voucher Detail` where name = %s""", d['voucher_detail_no'])
|
|
|
|
# new entry with balance amount
|
2014-03-27 10:42:56 +00:00
|
|
|
ch = jv_obj.append("entries")
|
2013-01-30 13:46:13 +00:00
|
|
|
ch.account = d['account']
|
|
|
|
ch.cost_center = cstr(jvd[0][0])
|
|
|
|
ch.balance = cstr(jvd[0][1])
|
|
|
|
ch.fields[d['dr_or_cr']] = flt(d['unadjusted_amt']) - flt(d['allocated_amt'])
|
|
|
|
ch.fields[d['dr_or_cr']== 'debit' and 'credit' or 'debit'] = 0
|
|
|
|
ch.against_account = cstr(jvd[0][2])
|
|
|
|
ch.is_advance = cstr(jvd[0][3])
|
|
|
|
ch.docstatus = 1
|
2013-02-08 08:34:13 +00:00
|
|
|
ch.save(1)
|
|
|
|
|
|
|
|
def get_account_list(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
if not filters.get("group_or_ledger"):
|
|
|
|
filters["group_or_ledger"] = "Ledger"
|
|
|
|
|
|
|
|
conditions, filter_values = build_filter_conditions(filters)
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
return frappe.db.sql("""select name, parent_account from `tabAccount`
|
2013-02-08 08:34:13 +00:00
|
|
|
where docstatus < 2 %s and %s like %s order by name limit %s, %s""" %
|
|
|
|
(conditions, searchfield, "%s", "%s", "%s"),
|
|
|
|
tuple(filter_values + ["%%%s%%" % txt, start, page_len]))
|
|
|
|
|
|
|
|
def get_cost_center_list(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
if not filters.get("group_or_ledger"):
|
|
|
|
filters["group_or_ledger"] = "Ledger"
|
|
|
|
|
|
|
|
conditions, filter_values = build_filter_conditions(filters)
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
return frappe.db.sql("""select name, parent_cost_center from `tabCost Center`
|
2013-02-08 08:34:13 +00:00
|
|
|
where docstatus < 2 %s and %s like %s order by name limit %s, %s""" %
|
|
|
|
(conditions, searchfield, "%s", "%s", "%s"),
|
2013-03-20 11:30:41 +00:00
|
|
|
tuple(filter_values + ["%%%s%%" % txt, start, page_len]))
|
|
|
|
|
|
|
|
def remove_against_link_from_jv(ref_type, ref_no, against_field):
|
2014-02-26 07:05:33 +00:00
|
|
|
linked_jv = frappe.db.sql_list("""select parent from `tabJournal Voucher Detail`
|
2014-01-03 09:42:16 +00:00
|
|
|
where `%s`=%s and docstatus < 2""" % (against_field, "%s"), (ref_no))
|
|
|
|
|
|
|
|
if linked_jv:
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.sql("""update `tabJournal Voucher Detail` set `%s`=null,
|
2014-01-03 09:42:16 +00:00
|
|
|
modified=%s, modified_by=%s
|
|
|
|
where `%s`=%s and docstatus < 2""" % (against_field, "%s", "%s", against_field, "%s"),
|
2014-02-14 10:17:51 +00:00
|
|
|
(now(), frappe.session.user, ref_no))
|
2013-03-20 11:30:41 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.sql("""update `tabGL Entry`
|
2014-01-03 09:42:16 +00:00
|
|
|
set against_voucher_type=null, against_voucher=null,
|
|
|
|
modified=%s, modified_by=%s
|
|
|
|
where against_voucher_type=%s and against_voucher=%s
|
|
|
|
and voucher_no != ifnull(against_voucher, '')""",
|
2014-02-14 10:17:51 +00:00
|
|
|
(now(), frappe.session.user, ref_type, ref_no))
|
2014-01-03 09:42:16 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.msgprint("{msg} {linked_jv}".format(msg = _("""Following linked Journal Vouchers \
|
2014-01-03 09:42:16 +00:00
|
|
|
made against this transaction has been unlinked. You can link them again with other \
|
|
|
|
transactions via Payment Reconciliation Tool."""), linked_jv="\n".join(linked_jv)))
|
|
|
|
|
2013-03-25 05:36:00 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2013-03-25 05:36:00 +00:00
|
|
|
def get_company_default(company, fieldname):
|
2014-02-26 07:05:33 +00:00
|
|
|
value = frappe.db.get_value("Company", company, fieldname)
|
2013-03-25 05:36:00 +00:00
|
|
|
|
|
|
|
if not value:
|
2014-01-29 11:01:38 +00:00
|
|
|
throw(_("Please mention default value for '") +
|
2014-02-14 10:17:51 +00:00
|
|
|
_(frappe.get_doctype("company").get_label(fieldname) +
|
2014-01-29 11:01:38 +00:00
|
|
|
_("' in Company: ") + company))
|
2013-03-25 05:36:00 +00:00
|
|
|
|
|
|
|
return value
|
2013-05-28 11:22:30 +00:00
|
|
|
|
|
|
|
def fix_total_debit_credit():
|
2014-02-26 07:05:33 +00:00
|
|
|
vouchers = frappe.db.sql("""select voucher_type, voucher_no,
|
2013-05-28 11:22:30 +00:00
|
|
|
sum(debit) - sum(credit) as diff
|
|
|
|
from `tabGL Entry`
|
|
|
|
group by voucher_type, voucher_no
|
|
|
|
having sum(ifnull(debit, 0)) != sum(ifnull(credit, 0))""", as_dict=1)
|
|
|
|
|
|
|
|
for d in vouchers:
|
|
|
|
if abs(d.diff) > 0:
|
|
|
|
dr_or_cr = d.voucher_type == "Sales Invoice" and "credit" or "debit"
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.sql("""update `tabGL Entry` set %s = %s + %s
|
2013-05-28 11:22:30 +00:00
|
|
|
where voucher_type = %s and voucher_no = %s and %s > 0 limit 1""" %
|
|
|
|
(dr_or_cr, dr_or_cr, '%s', '%s', '%s', dr_or_cr),
|
2013-08-02 06:14:29 +00:00
|
|
|
(d.diff, d.voucher_type, d.voucher_no))
|
|
|
|
|
2013-08-26 11:23:30 +00:00
|
|
|
def get_stock_and_account_difference(account_list=None, posting_date=None):
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.stock.utils import get_stock_balance_on
|
2013-08-26 11:23:30 +00:00
|
|
|
|
|
|
|
if not posting_date: posting_date = nowdate()
|
|
|
|
|
2013-08-02 06:14:29 +00:00
|
|
|
difference = {}
|
2013-09-25 05:02:51 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
account_warehouse = dict(frappe.db.sql("""select name, master_name from tabAccount
|
2013-09-25 05:02:51 +00:00
|
|
|
where account_type = 'Warehouse' and ifnull(master_name, '') != ''
|
|
|
|
and name in (%s)""" % ', '.join(['%s']*len(account_list)), account_list))
|
2013-08-02 06:14:29 +00:00
|
|
|
|
2013-09-25 05:02:51 +00:00
|
|
|
for account, warehouse in account_warehouse.items():
|
2013-08-26 11:23:30 +00:00
|
|
|
account_balance = get_balance_on(account, posting_date)
|
2013-09-25 05:02:51 +00:00
|
|
|
stock_value = get_stock_balance_on(warehouse, posting_date)
|
2013-08-07 08:51:04 +00:00
|
|
|
if abs(flt(stock_value) - flt(account_balance)) > 0.005:
|
2013-08-07 07:03:37 +00:00
|
|
|
difference.setdefault(account, flt(stock_value) - flt(account_balance))
|
2013-08-07 08:51:04 +00:00
|
|
|
|
2013-08-06 10:49:18 +00:00
|
|
|
return difference
|
2013-08-22 09:08:46 +00:00
|
|
|
|
|
|
|
def validate_expense_against_budget(args):
|
2014-02-14 10:17:51 +00:00
|
|
|
args = frappe._dict(args)
|
2014-03-21 05:44:49 +00:00
|
|
|
if frappe.db.get_value("Account", {"name": args.account, "report_type": "Profit and Loss"}):
|
2014-02-26 07:05:33 +00:00
|
|
|
budget = frappe.db.sql("""
|
2013-08-22 09:08:46 +00:00
|
|
|
select bd.budget_allocated, cc.distribution_id
|
|
|
|
from `tabCost Center` cc, `tabBudget Detail` bd
|
|
|
|
where cc.name=bd.parent and cc.name=%s and account=%s and bd.fiscal_year=%s
|
|
|
|
""", (args.cost_center, args.account, args.fiscal_year), as_dict=True)
|
|
|
|
|
|
|
|
if budget and budget[0].budget_allocated:
|
2014-02-26 07:05:33 +00:00
|
|
|
yearly_action, monthly_action = frappe.db.get_value("Company", args.company,
|
2013-08-22 09:08:46 +00:00
|
|
|
["yearly_bgt_flag", "monthly_bgt_flag"])
|
2013-08-22 12:55:43 +00:00
|
|
|
action_for = action = ""
|
|
|
|
|
|
|
|
if monthly_action in ["Stop", "Warn"]:
|
|
|
|
budget_amount = get_allocated_budget(budget[0].distribution_id,
|
|
|
|
args.posting_date, args.fiscal_year, budget[0].budget_allocated)
|
2013-08-22 09:08:46 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
args["month_end_date"] = frappe.db.sql("select LAST_DAY(%s)",
|
2013-08-23 05:16:41 +00:00
|
|
|
args.posting_date)[0][0]
|
2013-08-22 12:55:43 +00:00
|
|
|
action_for, action = "Monthly", monthly_action
|
|
|
|
|
|
|
|
elif yearly_action in ["Stop", "Warn"]:
|
|
|
|
budget_amount = budget[0].budget_allocated
|
|
|
|
action_for, action = "Monthly", yearly_action
|
2013-08-23 05:16:41 +00:00
|
|
|
|
2013-08-22 12:55:43 +00:00
|
|
|
if action_for:
|
|
|
|
actual_expense = get_actual_expense(args)
|
|
|
|
if actual_expense > budget_amount:
|
2014-01-29 11:01:38 +00:00
|
|
|
throw(action_for + _(" budget ") + cstr(budget_amount) +
|
2013-08-22 12:55:43 +00:00
|
|
|
_(" for account ") + args.account + _(" against cost center ") +
|
|
|
|
args.cost_center + _(" will exceed by ") +
|
|
|
|
cstr(actual_expense - budget_amount) + _(" after this transaction.")
|
2014-01-29 11:01:38 +00:00
|
|
|
, exc=BudgetError if action=="Stop" else False)
|
2013-08-22 12:55:43 +00:00
|
|
|
|
|
|
|
def get_allocated_budget(distribution_id, posting_date, fiscal_year, yearly_budget):
|
|
|
|
if distribution_id:
|
|
|
|
distribution = {}
|
2014-02-26 07:05:33 +00:00
|
|
|
for d in frappe.db.sql("""select bdd.month, bdd.percentage_allocation
|
2013-08-22 12:55:43 +00:00
|
|
|
from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd
|
|
|
|
where bdd.parent=bd.name and bd.fiscal_year=%s""", fiscal_year, as_dict=1):
|
|
|
|
distribution.setdefault(d.month, d.percentage_allocation)
|
2013-08-23 05:16:41 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
dt = frappe.db.get_value("Fiscal Year", fiscal_year, "year_start_date")
|
2013-08-22 12:55:43 +00:00
|
|
|
budget_percentage = 0.0
|
|
|
|
|
|
|
|
while(dt <= getdate(posting_date)):
|
|
|
|
if distribution_id:
|
2013-08-23 05:16:41 +00:00
|
|
|
budget_percentage += distribution.get(getdate(dt).strftime("%B"), 0)
|
2013-08-22 12:55:43 +00:00
|
|
|
else:
|
|
|
|
budget_percentage += 100.0/12
|
|
|
|
|
|
|
|
dt = add_months(dt, 1)
|
|
|
|
|
|
|
|
return yearly_budget * budget_percentage / 100
|
2013-08-22 09:08:46 +00:00
|
|
|
|
|
|
|
def get_actual_expense(args):
|
2013-08-23 05:16:41 +00:00
|
|
|
args["condition"] = " and posting_date<='%s'" % args.month_end_date \
|
|
|
|
if args.get("month_end_date") else ""
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
return frappe.db.sql("""
|
2013-08-22 09:08:46 +00:00
|
|
|
select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))
|
|
|
|
from `tabGL Entry`
|
2013-08-23 05:16:41 +00:00
|
|
|
where account='%(account)s' and cost_center='%(cost_center)s'
|
|
|
|
and fiscal_year='%(fiscal_year)s' and company='%(company)s' %(condition)s
|
2013-11-26 09:33:51 +00:00
|
|
|
""" % (args))[0][0]
|
|
|
|
|
2014-02-05 12:45:12 +00:00
|
|
|
def rename_account_for(dt, olddn, newdn, merge, company):
|
2013-11-26 09:33:51 +00:00
|
|
|
old_account = get_account_for(dt, olddn)
|
|
|
|
if old_account:
|
|
|
|
new_account = None
|
|
|
|
if not merge:
|
2014-02-05 12:45:12 +00:00
|
|
|
if old_account == add_abbr_if_missing(olddn, company):
|
2014-02-14 10:17:51 +00:00
|
|
|
new_account = frappe.rename_doc("Account", old_account, newdn)
|
2013-11-26 09:33:51 +00:00
|
|
|
else:
|
|
|
|
existing_new_account = get_account_for(dt, newdn)
|
2014-02-14 10:17:51 +00:00
|
|
|
new_account = frappe.rename_doc("Account", old_account,
|
2013-11-26 09:33:51 +00:00
|
|
|
existing_new_account or newdn, merge=True if existing_new_account else False)
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.set_value("Account", new_account or old_account, "master_name", newdn)
|
2014-02-05 12:45:12 +00:00
|
|
|
|
|
|
|
def add_abbr_if_missing(dn, company):
|
2014-02-11 14:58:11 +00:00
|
|
|
from erpnext.setup.doctype.company.company import get_name_with_abbr
|
2014-02-05 12:45:12 +00:00
|
|
|
return get_name_with_abbr(dn, company)
|
2013-11-26 09:33:51 +00:00
|
|
|
|
|
|
|
def get_account_for(account_for_doctype, account_for):
|
|
|
|
if account_for_doctype in ["Customer", "Supplier"]:
|
|
|
|
account_for_field = "master_type"
|
|
|
|
elif account_for_doctype == "Warehouse":
|
|
|
|
account_for_field = "account_type"
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
return frappe.db.get_value("Account", {account_for_field: account_for_doctype,
|
2013-11-26 09:57:17 +00:00
|
|
|
"master_name": account_for})
|
2014-01-22 10:06:44 +00:00
|
|
|
|
|
|
|
def get_currency_precision(currency=None):
|
|
|
|
if not currency:
|
2014-02-26 07:05:33 +00:00
|
|
|
currency = frappe.db.get_value("Company",
|
|
|
|
frappe.db.get_default("company"), "default_currency")
|
|
|
|
currency_format = frappe.db.get_value("Currency", currency, "number_format")
|
2014-01-22 10:06:44 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
from frappe.utils import get_number_format_info
|
2014-01-24 16:14:36 +00:00
|
|
|
return get_number_format_info(currency_format)[2]
|