Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | |
| 6 | import webnotes |
Nabin Hait | 33f6b9d | 2014-01-03 15:12:16 +0530 | [diff] [blame] | 7 | from webnotes.utils import nowdate, cstr, flt, now, getdate, add_months |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 8 | from webnotes.model.doc import addchild |
| 9 | from webnotes import msgprint, _ |
Rushabh Mehta | 2e8d078 | 2013-02-05 11:31:27 +0530 | [diff] [blame] | 10 | from webnotes.utils import formatdate |
Nabin Hait | a76a068 | 2013-02-08 14:04:13 +0530 | [diff] [blame] | 11 | from utilities import build_filter_conditions |
| 12 | |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 13 | |
| 14 | class FiscalYearError(webnotes.ValidationError): pass |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 15 | class BudgetError(webnotes.ValidationError): pass |
| 16 | |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 17 | |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 18 | def get_fiscal_year(date=None, fiscal_year=None, label="Date", verbose=1): |
Nabin Hait | c38527e | 2013-12-23 17:07:57 +0530 | [diff] [blame] | 19 | return get_fiscal_years(date, fiscal_year, label, verbose)[0] |
Rushabh Mehta | c2563ef | 2013-02-05 23:25:37 +0530 | [diff] [blame] | 20 | |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 21 | def get_fiscal_years(date=None, fiscal_year=None, label="Date", verbose=1): |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 22 | # if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate) |
Nabin Hait | 0930b94 | 2013-06-20 16:35:09 +0530 | [diff] [blame] | 23 | cond = "" |
| 24 | if fiscal_year: |
| 25 | cond = "name = '%s'" % fiscal_year |
| 26 | else: |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 27 | cond = "'%s' >= year_start_date and '%s' <= year_end_date" % \ |
Nabin Hait | 0930b94 | 2013-06-20 16:35:09 +0530 | [diff] [blame] | 28 | (date, date) |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 29 | fy = webnotes.conn.sql("""select name, year_start_date, year_end_date |
| 30 | from `tabFiscal Year` where %s order by year_start_date desc""" % cond) |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 31 | |
| 32 | if not fy: |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 33 | error_msg = """%s %s not in any Fiscal Year""" % (label, formatdate(date)) |
Nabin Hait | a4db83a | 2014-01-03 12:13:18 +0530 | [diff] [blame] | 34 | error_msg = """{msg}: {date}""".format(msg=_("Fiscal Year does not exist for date"), |
| 35 | date=formatdate(date)) |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 36 | if verbose: webnotes.msgprint(error_msg) |
| 37 | raise FiscalYearError, error_msg |
| 38 | |
Rushabh Mehta | c2563ef | 2013-02-05 23:25:37 +0530 | [diff] [blame] | 39 | return fy |
Rushabh Mehta | 2e8d078 | 2013-02-05 11:31:27 +0530 | [diff] [blame] | 40 | |
| 41 | def validate_fiscal_year(date, fiscal_year, label="Date"): |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 42 | years = [f[0] for f in get_fiscal_years(date, label=label)] |
Rushabh Mehta | c2563ef | 2013-02-05 23:25:37 +0530 | [diff] [blame] | 43 | if fiscal_year not in years: |
Rushabh Mehta | 9607582 | 2013-02-05 11:39:49 +0530 | [diff] [blame] | 44 | webnotes.msgprint(("%(label)s '%(posting_date)s': " + _("not within Fiscal Year") + \ |
Rushabh Mehta | 2e8d078 | 2013-02-05 11:31:27 +0530 | [diff] [blame] | 45 | ": '%(fiscal_year)s'") % { |
| 46 | "label": label, |
| 47 | "posting_date": formatdate(date), |
| 48 | "fiscal_year": fiscal_year |
| 49 | }, raise_exception=1) |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 50 | |
| 51 | @webnotes.whitelist() |
| 52 | def get_balance_on(account=None, date=None): |
| 53 | if not account and webnotes.form_dict.get("account"): |
| 54 | account = webnotes.form_dict.get("account") |
| 55 | date = webnotes.form_dict.get("date") |
| 56 | |
| 57 | cond = [] |
| 58 | if date: |
| 59 | cond.append("posting_date <= '%s'" % date) |
| 60 | else: |
| 61 | # get balance of all entries that exist |
| 62 | date = nowdate() |
| 63 | |
| 64 | try: |
| 65 | year_start_date = get_fiscal_year(date, verbose=0)[1] |
| 66 | except FiscalYearError, e: |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 67 | if getdate(date) > getdate(nowdate()): |
| 68 | # if fiscal year not found and the date is greater than today |
| 69 | # get fiscal year for today's date and its corresponding year start date |
| 70 | year_start_date = get_fiscal_year(nowdate(), verbose=1)[1] |
| 71 | else: |
| 72 | # this indicates that it is a date older than any existing fiscal year. |
| 73 | # hence, assuming balance as 0.0 |
| 74 | return 0.0 |
| 75 | |
| 76 | acc = webnotes.conn.get_value('Account', account, \ |
| 77 | ['lft', 'rgt', 'debit_or_credit', 'is_pl_account', 'group_or_ledger'], as_dict=1) |
| 78 | |
| 79 | # for pl accounts, get balance within a fiscal year |
| 80 | if acc.is_pl_account == 'Yes': |
| 81 | cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" \ |
| 82 | % year_start_date) |
| 83 | |
| 84 | # different filter for group and ledger - improved performance |
| 85 | if acc.group_or_ledger=="Group": |
| 86 | cond.append("""exists ( |
| 87 | select * from `tabAccount` ac where ac.name = gle.account |
| 88 | and ac.lft >= %s and ac.rgt <= %s |
| 89 | )""" % (acc.lft, acc.rgt)) |
| 90 | else: |
| 91 | cond.append("""gle.account = "%s" """ % (account, )) |
| 92 | |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 93 | bal = webnotes.conn.sql(""" |
| 94 | SELECT sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) |
| 95 | FROM `tabGL Entry` gle |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 96 | WHERE %s""" % " and ".join(cond))[0][0] |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 97 | |
| 98 | # if credit account, it should calculate credit - debit |
| 99 | if bal and acc.debit_or_credit == 'Credit': |
| 100 | bal = -bal |
| 101 | |
| 102 | # if bal is None, return 0 |
Anand Doshi | 6fa5e42 | 2013-07-19 15:33:11 +0530 | [diff] [blame] | 103 | return flt(bal) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 104 | |
| 105 | @webnotes.whitelist() |
| 106 | def add_ac(args=None): |
| 107 | if not args: |
Rushabh Mehta | 8c2e4e1 | 2013-10-02 14:34:25 +0530 | [diff] [blame] | 108 | args = webnotes.local.form_dict |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 109 | args.pop("cmd") |
Rushabh Mehta | aae4553 | 2013-02-15 14:39:34 +0530 | [diff] [blame] | 110 | |
Rushabh Mehta | c53231a | 2013-02-18 18:24:28 +0530 | [diff] [blame] | 111 | ac = webnotes.bean(args) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 112 | ac.doc.doctype = "Account" |
| 113 | ac.doc.old_parent = "" |
| 114 | ac.doc.freeze_account = "No" |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 115 | ac.insert() |
| 116 | return ac.doc.name |
| 117 | |
| 118 | @webnotes.whitelist() |
| 119 | def add_cc(args=None): |
| 120 | if not args: |
Rushabh Mehta | 8c2e4e1 | 2013-10-02 14:34:25 +0530 | [diff] [blame] | 121 | args = webnotes.local.form_dict |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 122 | args.pop("cmd") |
| 123 | |
Rushabh Mehta | c53231a | 2013-02-18 18:24:28 +0530 | [diff] [blame] | 124 | cc = webnotes.bean(args) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 125 | cc.doc.doctype = "Cost Center" |
| 126 | cc.doc.old_parent = "" |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 127 | cc.insert() |
| 128 | return cc.doc.name |
| 129 | |
| 130 | def reconcile_against_document(args): |
| 131 | """ |
| 132 | Cancel JV, Update aginst document, split if required and resubmit jv |
| 133 | """ |
| 134 | for d in args: |
| 135 | check_if_jv_modified(d) |
| 136 | |
| 137 | against_fld = { |
| 138 | 'Journal Voucher' : 'against_jv', |
| 139 | 'Sales Invoice' : 'against_invoice', |
| 140 | 'Purchase Invoice' : 'against_voucher' |
| 141 | } |
| 142 | |
| 143 | d['against_fld'] = against_fld[d['against_voucher_type']] |
| 144 | |
| 145 | # cancel JV |
| 146 | jv_obj = webnotes.get_obj('Journal Voucher', d['voucher_no'], with_children=1) |
| 147 | jv_obj.make_gl_entries(cancel=1, adv_adj=1) |
| 148 | |
| 149 | # update ref in JV Detail |
| 150 | update_against_doc(d, jv_obj) |
| 151 | |
| 152 | # re-submit JV |
| 153 | jv_obj = webnotes.get_obj('Journal Voucher', d['voucher_no'], with_children =1) |
| 154 | jv_obj.make_gl_entries(cancel = 0, adv_adj =1) |
| 155 | |
| 156 | |
| 157 | def check_if_jv_modified(args): |
| 158 | """ |
| 159 | check if there is already a voucher reference |
| 160 | check if amount is same |
| 161 | check if jv is submitted |
| 162 | """ |
| 163 | ret = webnotes.conn.sql(""" |
| 164 | select t2.%(dr_or_cr)s from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2 |
| 165 | where t1.name = t2.parent and t2.account = '%(account)s' |
| 166 | and ifnull(t2.against_voucher, '')='' |
| 167 | and ifnull(t2.against_invoice, '')='' and ifnull(t2.against_jv, '')='' |
| 168 | and t1.name = '%(voucher_no)s' and t2.name = '%(voucher_detail_no)s' |
| 169 | and t1.docstatus=1 and t2.%(dr_or_cr)s = %(unadjusted_amt)s""" % args) |
| 170 | |
| 171 | if not ret: |
| 172 | msgprint(_("""Payment Entry has been modified after you pulled it. |
| 173 | Please pull it again."""), raise_exception=1) |
| 174 | |
| 175 | def update_against_doc(d, jv_obj): |
| 176 | """ |
| 177 | Updates against document, if partial amount splits into rows |
| 178 | """ |
| 179 | |
| 180 | webnotes.conn.sql(""" |
| 181 | update `tabJournal Voucher Detail` t1, `tabJournal Voucher` t2 |
| 182 | set t1.%(dr_or_cr)s = '%(allocated_amt)s', |
| 183 | t1.%(against_fld)s = '%(against_voucher)s', t2.modified = now() |
| 184 | where t1.name = '%(voucher_detail_no)s' and t1.parent = t2.name""" % d) |
| 185 | |
| 186 | if d['allocated_amt'] < d['unadjusted_amt']: |
| 187 | jvd = webnotes.conn.sql("""select cost_center, balance, against_account, is_advance |
| 188 | from `tabJournal Voucher Detail` where name = %s""", d['voucher_detail_no']) |
| 189 | # new entry with balance amount |
| 190 | ch = addchild(jv_obj.doc, 'entries', 'Journal Voucher Detail') |
| 191 | ch.account = d['account'] |
| 192 | ch.cost_center = cstr(jvd[0][0]) |
| 193 | ch.balance = cstr(jvd[0][1]) |
| 194 | ch.fields[d['dr_or_cr']] = flt(d['unadjusted_amt']) - flt(d['allocated_amt']) |
| 195 | ch.fields[d['dr_or_cr']== 'debit' and 'credit' or 'debit'] = 0 |
| 196 | ch.against_account = cstr(jvd[0][2]) |
| 197 | ch.is_advance = cstr(jvd[0][3]) |
| 198 | ch.docstatus = 1 |
Nabin Hait | a76a068 | 2013-02-08 14:04:13 +0530 | [diff] [blame] | 199 | ch.save(1) |
| 200 | |
| 201 | def get_account_list(doctype, txt, searchfield, start, page_len, filters): |
| 202 | if not filters.get("group_or_ledger"): |
| 203 | filters["group_or_ledger"] = "Ledger" |
| 204 | |
| 205 | conditions, filter_values = build_filter_conditions(filters) |
| 206 | |
| 207 | return webnotes.conn.sql("""select name, parent_account from `tabAccount` |
| 208 | where docstatus < 2 %s and %s like %s order by name limit %s, %s""" % |
| 209 | (conditions, searchfield, "%s", "%s", "%s"), |
| 210 | tuple(filter_values + ["%%%s%%" % txt, start, page_len])) |
| 211 | |
| 212 | def get_cost_center_list(doctype, txt, searchfield, start, page_len, filters): |
| 213 | if not filters.get("group_or_ledger"): |
| 214 | filters["group_or_ledger"] = "Ledger" |
| 215 | |
| 216 | conditions, filter_values = build_filter_conditions(filters) |
| 217 | |
| 218 | return webnotes.conn.sql("""select name, parent_cost_center from `tabCost Center` |
| 219 | where docstatus < 2 %s and %s like %s order by name limit %s, %s""" % |
| 220 | (conditions, searchfield, "%s", "%s", "%s"), |
Anand Doshi | 7da72dd | 2013-03-20 17:00:41 +0530 | [diff] [blame] | 221 | tuple(filter_values + ["%%%s%%" % txt, start, page_len])) |
| 222 | |
| 223 | def remove_against_link_from_jv(ref_type, ref_no, against_field): |
Nabin Hait | 33f6b9d | 2014-01-03 15:12:16 +0530 | [diff] [blame] | 224 | linked_jv = webnotes.conn.sql_list("""select parent from `tabJournal Voucher Detail` |
| 225 | where `%s`=%s and docstatus < 2""" % (against_field, "%s"), (ref_no)) |
| 226 | |
| 227 | if linked_jv: |
| 228 | webnotes.conn.sql("""update `tabJournal Voucher Detail` set `%s`=null, |
| 229 | modified=%s, modified_by=%s |
| 230 | where `%s`=%s and docstatus < 2""" % (against_field, "%s", "%s", against_field, "%s"), |
| 231 | (now(), webnotes.session.user, ref_no)) |
Anand Doshi | 7da72dd | 2013-03-20 17:00:41 +0530 | [diff] [blame] | 232 | |
Nabin Hait | 33f6b9d | 2014-01-03 15:12:16 +0530 | [diff] [blame] | 233 | webnotes.conn.sql("""update `tabGL Entry` |
| 234 | set against_voucher_type=null, against_voucher=null, |
| 235 | modified=%s, modified_by=%s |
| 236 | where against_voucher_type=%s and against_voucher=%s |
| 237 | and voucher_no != ifnull(against_voucher, '')""", |
| 238 | (now(), webnotes.session.user, ref_type, ref_no)) |
| 239 | |
| 240 | webnotes.msgprint("{msg} {linked_jv}".format(msg = _("""Following linked Journal Vouchers \ |
| 241 | made against this transaction has been unlinked. You can link them again with other \ |
| 242 | transactions via Payment Reconciliation Tool."""), linked_jv="\n".join(linked_jv))) |
| 243 | |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 244 | |
| 245 | @webnotes.whitelist() |
| 246 | def get_company_default(company, fieldname): |
| 247 | value = webnotes.conn.get_value("Company", company, fieldname) |
| 248 | |
| 249 | if not value: |
| 250 | msgprint(_("Please mention default value for '") + |
| 251 | _(webnotes.get_doctype("company").get_label(fieldname) + |
| 252 | _("' in Company: ") + company), raise_exception=True) |
| 253 | |
| 254 | return value |
Nabin Hait | 8b509f5 | 2013-05-28 16:52:30 +0530 | [diff] [blame] | 255 | |
| 256 | def fix_total_debit_credit(): |
| 257 | vouchers = webnotes.conn.sql("""select voucher_type, voucher_no, |
| 258 | sum(debit) - sum(credit) as diff |
| 259 | from `tabGL Entry` |
| 260 | group by voucher_type, voucher_no |
| 261 | having sum(ifnull(debit, 0)) != sum(ifnull(credit, 0))""", as_dict=1) |
| 262 | |
| 263 | for d in vouchers: |
| 264 | if abs(d.diff) > 0: |
| 265 | dr_or_cr = d.voucher_type == "Sales Invoice" and "credit" or "debit" |
| 266 | |
| 267 | webnotes.conn.sql("""update `tabGL Entry` set %s = %s + %s |
| 268 | where voucher_type = %s and voucher_no = %s and %s > 0 limit 1""" % |
| 269 | (dr_or_cr, dr_or_cr, '%s', '%s', '%s', dr_or_cr), |
Nabin Hait | cac622e | 2013-08-02 11:44:29 +0530 | [diff] [blame] | 270 | (d.diff, d.voucher_type, d.voucher_no)) |
Nabin Hait | cac622e | 2013-08-02 11:44:29 +0530 | [diff] [blame] | 271 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 272 | def get_stock_and_account_difference(account_list=None, posting_date=None): |
| 273 | from stock.utils import get_stock_balance_on |
| 274 | |
| 275 | if not posting_date: posting_date = nowdate() |
| 276 | |
Nabin Hait | cac622e | 2013-08-02 11:44:29 +0530 | [diff] [blame] | 277 | difference = {} |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 278 | |
| 279 | account_warehouse = dict(webnotes.conn.sql("""select name, master_name from tabAccount |
| 280 | where account_type = 'Warehouse' and ifnull(master_name, '') != '' |
| 281 | and name in (%s)""" % ', '.join(['%s']*len(account_list)), account_list)) |
Nabin Hait | cac622e | 2013-08-02 11:44:29 +0530 | [diff] [blame] | 282 | |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 283 | for account, warehouse in account_warehouse.items(): |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 284 | account_balance = get_balance_on(account, posting_date) |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 285 | stock_value = get_stock_balance_on(warehouse, posting_date) |
Nabin Hait | aa34201 | 2013-08-07 14:21:04 +0530 | [diff] [blame] | 286 | if abs(flt(stock_value) - flt(account_balance)) > 0.005: |
Nabin Hait | 469ee71 | 2013-08-07 12:33:37 +0530 | [diff] [blame] | 287 | difference.setdefault(account, flt(stock_value) - flt(account_balance)) |
Nabin Hait | aa34201 | 2013-08-07 14:21:04 +0530 | [diff] [blame] | 288 | |
Nabin Hait | cc0e2d1 | 2013-08-06 16:19:18 +0530 | [diff] [blame] | 289 | return difference |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 290 | |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 291 | def validate_expense_against_budget(args): |
| 292 | args = webnotes._dict(args) |
| 293 | if webnotes.conn.get_value("Account", {"name": args.account, "is_pl_account": "Yes", |
| 294 | "debit_or_credit": "Debit"}): |
| 295 | budget = webnotes.conn.sql(""" |
| 296 | select bd.budget_allocated, cc.distribution_id |
| 297 | from `tabCost Center` cc, `tabBudget Detail` bd |
| 298 | where cc.name=bd.parent and cc.name=%s and account=%s and bd.fiscal_year=%s |
| 299 | """, (args.cost_center, args.account, args.fiscal_year), as_dict=True) |
| 300 | |
| 301 | if budget and budget[0].budget_allocated: |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 302 | yearly_action, monthly_action = webnotes.conn.get_value("Company", args.company, |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 303 | ["yearly_bgt_flag", "monthly_bgt_flag"]) |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 304 | action_for = action = "" |
| 305 | |
| 306 | if monthly_action in ["Stop", "Warn"]: |
| 307 | budget_amount = get_allocated_budget(budget[0].distribution_id, |
| 308 | args.posting_date, args.fiscal_year, budget[0].budget_allocated) |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 309 | |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 310 | args["month_end_date"] = webnotes.conn.sql("select LAST_DAY(%s)", |
| 311 | args.posting_date)[0][0] |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 312 | action_for, action = "Monthly", monthly_action |
| 313 | |
| 314 | elif yearly_action in ["Stop", "Warn"]: |
| 315 | budget_amount = budget[0].budget_allocated |
| 316 | action_for, action = "Monthly", yearly_action |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 317 | |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 318 | if action_for: |
| 319 | actual_expense = get_actual_expense(args) |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 320 | if actual_expense > budget_amount: |
| 321 | webnotes.msgprint(action_for + _(" budget ") + cstr(budget_amount) + |
| 322 | _(" for account ") + args.account + _(" against cost center ") + |
| 323 | args.cost_center + _(" will exceed by ") + |
| 324 | cstr(actual_expense - budget_amount) + _(" after this transaction.") |
| 325 | , raise_exception=BudgetError if action=="Stop" else False) |
| 326 | |
| 327 | def get_allocated_budget(distribution_id, posting_date, fiscal_year, yearly_budget): |
| 328 | if distribution_id: |
| 329 | distribution = {} |
| 330 | for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation |
| 331 | from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd |
| 332 | where bdd.parent=bd.name and bd.fiscal_year=%s""", fiscal_year, as_dict=1): |
| 333 | distribution.setdefault(d.month, d.percentage_allocation) |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 334 | |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 335 | dt = webnotes.conn.get_value("Fiscal Year", fiscal_year, "year_start_date") |
| 336 | budget_percentage = 0.0 |
| 337 | |
| 338 | while(dt <= getdate(posting_date)): |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 339 | if distribution_id: |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 340 | budget_percentage += distribution.get(getdate(dt).strftime("%B"), 0) |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 341 | else: |
| 342 | budget_percentage += 100.0/12 |
| 343 | |
| 344 | dt = add_months(dt, 1) |
| 345 | |
| 346 | return yearly_budget * budget_percentage / 100 |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 347 | |
| 348 | def get_actual_expense(args): |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 349 | args["condition"] = " and posting_date<='%s'" % args.month_end_date \ |
| 350 | if args.get("month_end_date") else "" |
| 351 | |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 352 | return webnotes.conn.sql(""" |
| 353 | select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) |
| 354 | from `tabGL Entry` |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 355 | where account='%(account)s' and cost_center='%(cost_center)s' |
| 356 | and fiscal_year='%(fiscal_year)s' and company='%(company)s' %(condition)s |
Nabin Hait | 131939a | 2013-11-26 15:03:51 +0530 | [diff] [blame] | 357 | """ % (args))[0][0] |
| 358 | |
| 359 | def rename_account_for(dt, olddn, newdn, merge): |
| 360 | old_account = get_account_for(dt, olddn) |
| 361 | if old_account: |
| 362 | new_account = None |
| 363 | if not merge: |
| 364 | if old_account == olddn: |
| 365 | new_account = webnotes.rename_doc("Account", olddn, newdn) |
| 366 | else: |
| 367 | existing_new_account = get_account_for(dt, newdn) |
| 368 | new_account = webnotes.rename_doc("Account", old_account, |
| 369 | existing_new_account or newdn, merge=True if existing_new_account else False) |
| 370 | |
| 371 | if new_account: |
| 372 | webnotes.conn.set_value("Account", new_account, "master_name", newdn) |
| 373 | |
| 374 | def get_account_for(account_for_doctype, account_for): |
| 375 | if account_for_doctype in ["Customer", "Supplier"]: |
| 376 | account_for_field = "master_type" |
| 377 | elif account_for_doctype == "Warehouse": |
| 378 | account_for_field = "account_type" |
| 379 | |
| 380 | return webnotes.conn.get_value("Account", {account_for_field: account_for_doctype, |
Nabin Hait | c767679 | 2014-01-22 15:36:44 +0530 | [diff] [blame] | 381 | "master_name": account_for}) |
| 382 | |
| 383 | def get_currency_precision(currency=None): |
| 384 | if not currency: |
| 385 | currency = webnotes.conn.get_value("Company", |
| 386 | webnotes.conn.get_default("company"), "default_currency") |
| 387 | currency_format = webnotes.conn.get_value("Currency", currency, "number_format") |
| 388 | |
| 389 | from webnotes.utils import get_number_format_info |
| 390 | return get_number_format_info(currency_format)[2] |