Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 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 | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 7 | from webnotes.utils import nowdate, nowtime, 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): |
| 19 | return get_fiscal_years(date, fiscal_year, label, verbose=1)[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: |
| 27 | cond = "'%s' >= year_start_date and '%s' < adddate(year_start_date, interval 1 year)" % \ |
| 28 | (date, date) |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 29 | fy = webnotes.conn.sql("""select name, year_start_date, |
| 30 | subdate(adddate(year_start_date, interval 1 year), interval 1 day) |
| 31 | as year_end_date |
| 32 | from `tabFiscal Year` |
Nabin Hait | 0930b94 | 2013-06-20 16:35:09 +0530 | [diff] [blame] | 33 | where %s |
| 34 | order by year_start_date desc""" % cond) |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 35 | |
| 36 | if not fy: |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 37 | error_msg = """%s %s not in any Fiscal Year""" % (label, formatdate(date)) |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 38 | if verbose: webnotes.msgprint(error_msg) |
| 39 | raise FiscalYearError, error_msg |
| 40 | |
Rushabh Mehta | c2563ef | 2013-02-05 23:25:37 +0530 | [diff] [blame] | 41 | return fy |
Rushabh Mehta | 2e8d078 | 2013-02-05 11:31:27 +0530 | [diff] [blame] | 42 | |
| 43 | def validate_fiscal_year(date, fiscal_year, label="Date"): |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 44 | years = [f[0] for f in get_fiscal_years(date, label=label)] |
Rushabh Mehta | c2563ef | 2013-02-05 23:25:37 +0530 | [diff] [blame] | 45 | if fiscal_year not in years: |
Rushabh Mehta | 9607582 | 2013-02-05 11:39:49 +0530 | [diff] [blame] | 46 | webnotes.msgprint(("%(label)s '%(posting_date)s': " + _("not within Fiscal Year") + \ |
Rushabh Mehta | 2e8d078 | 2013-02-05 11:31:27 +0530 | [diff] [blame] | 47 | ": '%(fiscal_year)s'") % { |
| 48 | "label": label, |
| 49 | "posting_date": formatdate(date), |
| 50 | "fiscal_year": fiscal_year |
| 51 | }, raise_exception=1) |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 52 | |
| 53 | @webnotes.whitelist() |
| 54 | def get_balance_on(account=None, date=None): |
| 55 | if not account and webnotes.form_dict.get("account"): |
| 56 | account = webnotes.form_dict.get("account") |
| 57 | date = webnotes.form_dict.get("date") |
| 58 | |
| 59 | cond = [] |
| 60 | if date: |
| 61 | cond.append("posting_date <= '%s'" % date) |
| 62 | else: |
| 63 | # get balance of all entries that exist |
| 64 | date = nowdate() |
| 65 | |
| 66 | try: |
| 67 | year_start_date = get_fiscal_year(date, verbose=0)[1] |
| 68 | except FiscalYearError, e: |
| 69 | from webnotes.utils import getdate |
| 70 | if getdate(date) > getdate(nowdate()): |
| 71 | # if fiscal year not found and the date is greater than today |
| 72 | # get fiscal year for today's date and its corresponding year start date |
| 73 | year_start_date = get_fiscal_year(nowdate(), verbose=1)[1] |
| 74 | else: |
| 75 | # this indicates that it is a date older than any existing fiscal year. |
| 76 | # hence, assuming balance as 0.0 |
| 77 | return 0.0 |
| 78 | |
| 79 | acc = webnotes.conn.get_value('Account', account, \ |
| 80 | ['lft', 'rgt', 'debit_or_credit', 'is_pl_account', 'group_or_ledger'], as_dict=1) |
| 81 | |
| 82 | # for pl accounts, get balance within a fiscal year |
| 83 | if acc.is_pl_account == 'Yes': |
| 84 | cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" \ |
| 85 | % year_start_date) |
| 86 | |
| 87 | # different filter for group and ledger - improved performance |
| 88 | if acc.group_or_ledger=="Group": |
| 89 | cond.append("""exists ( |
| 90 | select * from `tabAccount` ac where ac.name = gle.account |
| 91 | and ac.lft >= %s and ac.rgt <= %s |
| 92 | )""" % (acc.lft, acc.rgt)) |
| 93 | else: |
| 94 | cond.append("""gle.account = "%s" """ % (account, )) |
| 95 | |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 96 | bal = webnotes.conn.sql(""" |
| 97 | SELECT sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) |
| 98 | FROM `tabGL Entry` gle |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 99 | WHERE %s""" % " and ".join(cond))[0][0] |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 100 | |
| 101 | # if credit account, it should calculate credit - debit |
| 102 | if bal and acc.debit_or_credit == 'Credit': |
| 103 | bal = -bal |
| 104 | |
| 105 | # if bal is None, return 0 |
Anand Doshi | 6fa5e42 | 2013-07-19 15:33:11 +0530 | [diff] [blame] | 106 | return flt(bal) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 107 | |
| 108 | @webnotes.whitelist() |
| 109 | def add_ac(args=None): |
| 110 | if not args: |
| 111 | args = webnotes.form_dict |
| 112 | args.pop("cmd") |
Rushabh Mehta | aae4553 | 2013-02-15 14:39:34 +0530 | [diff] [blame] | 113 | |
Rushabh Mehta | c53231a | 2013-02-18 18:24:28 +0530 | [diff] [blame] | 114 | ac = webnotes.bean(args) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 115 | ac.doc.doctype = "Account" |
| 116 | ac.doc.old_parent = "" |
| 117 | ac.doc.freeze_account = "No" |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 118 | ac.insert() |
| 119 | return ac.doc.name |
| 120 | |
| 121 | @webnotes.whitelist() |
| 122 | def add_cc(args=None): |
| 123 | if not args: |
| 124 | args = webnotes.form_dict |
| 125 | args.pop("cmd") |
| 126 | |
Rushabh Mehta | c53231a | 2013-02-18 18:24:28 +0530 | [diff] [blame] | 127 | cc = webnotes.bean(args) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 128 | cc.doc.doctype = "Cost Center" |
| 129 | cc.doc.old_parent = "" |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 130 | cc.insert() |
| 131 | return cc.doc.name |
| 132 | |
| 133 | def reconcile_against_document(args): |
| 134 | """ |
| 135 | Cancel JV, Update aginst document, split if required and resubmit jv |
| 136 | """ |
| 137 | for d in args: |
| 138 | check_if_jv_modified(d) |
| 139 | |
| 140 | against_fld = { |
| 141 | 'Journal Voucher' : 'against_jv', |
| 142 | 'Sales Invoice' : 'against_invoice', |
| 143 | 'Purchase Invoice' : 'against_voucher' |
| 144 | } |
| 145 | |
| 146 | d['against_fld'] = against_fld[d['against_voucher_type']] |
| 147 | |
| 148 | # cancel JV |
| 149 | jv_obj = webnotes.get_obj('Journal Voucher', d['voucher_no'], with_children=1) |
| 150 | jv_obj.make_gl_entries(cancel=1, adv_adj=1) |
| 151 | |
| 152 | # update ref in JV Detail |
| 153 | update_against_doc(d, jv_obj) |
| 154 | |
| 155 | # re-submit JV |
| 156 | jv_obj = webnotes.get_obj('Journal Voucher', d['voucher_no'], with_children =1) |
| 157 | jv_obj.make_gl_entries(cancel = 0, adv_adj =1) |
| 158 | |
| 159 | |
| 160 | def check_if_jv_modified(args): |
| 161 | """ |
| 162 | check if there is already a voucher reference |
| 163 | check if amount is same |
| 164 | check if jv is submitted |
| 165 | """ |
| 166 | ret = webnotes.conn.sql(""" |
| 167 | select t2.%(dr_or_cr)s from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2 |
| 168 | where t1.name = t2.parent and t2.account = '%(account)s' |
| 169 | and ifnull(t2.against_voucher, '')='' |
| 170 | and ifnull(t2.against_invoice, '')='' and ifnull(t2.against_jv, '')='' |
| 171 | and t1.name = '%(voucher_no)s' and t2.name = '%(voucher_detail_no)s' |
| 172 | and t1.docstatus=1 and t2.%(dr_or_cr)s = %(unadjusted_amt)s""" % args) |
| 173 | |
| 174 | if not ret: |
| 175 | msgprint(_("""Payment Entry has been modified after you pulled it. |
| 176 | Please pull it again."""), raise_exception=1) |
| 177 | |
| 178 | def update_against_doc(d, jv_obj): |
| 179 | """ |
| 180 | Updates against document, if partial amount splits into rows |
| 181 | """ |
| 182 | |
| 183 | webnotes.conn.sql(""" |
| 184 | update `tabJournal Voucher Detail` t1, `tabJournal Voucher` t2 |
| 185 | set t1.%(dr_or_cr)s = '%(allocated_amt)s', |
| 186 | t1.%(against_fld)s = '%(against_voucher)s', t2.modified = now() |
| 187 | where t1.name = '%(voucher_detail_no)s' and t1.parent = t2.name""" % d) |
| 188 | |
| 189 | if d['allocated_amt'] < d['unadjusted_amt']: |
| 190 | jvd = webnotes.conn.sql("""select cost_center, balance, against_account, is_advance |
| 191 | from `tabJournal Voucher Detail` where name = %s""", d['voucher_detail_no']) |
| 192 | # new entry with balance amount |
| 193 | ch = addchild(jv_obj.doc, 'entries', 'Journal Voucher Detail') |
| 194 | ch.account = d['account'] |
| 195 | ch.cost_center = cstr(jvd[0][0]) |
| 196 | ch.balance = cstr(jvd[0][1]) |
| 197 | ch.fields[d['dr_or_cr']] = flt(d['unadjusted_amt']) - flt(d['allocated_amt']) |
| 198 | ch.fields[d['dr_or_cr']== 'debit' and 'credit' or 'debit'] = 0 |
| 199 | ch.against_account = cstr(jvd[0][2]) |
| 200 | ch.is_advance = cstr(jvd[0][3]) |
| 201 | ch.docstatus = 1 |
Nabin Hait | a76a068 | 2013-02-08 14:04:13 +0530 | [diff] [blame] | 202 | ch.save(1) |
| 203 | |
| 204 | def get_account_list(doctype, txt, searchfield, start, page_len, filters): |
| 205 | if not filters.get("group_or_ledger"): |
| 206 | filters["group_or_ledger"] = "Ledger" |
| 207 | |
| 208 | conditions, filter_values = build_filter_conditions(filters) |
| 209 | |
| 210 | return webnotes.conn.sql("""select name, parent_account from `tabAccount` |
| 211 | where docstatus < 2 %s and %s like %s order by name limit %s, %s""" % |
| 212 | (conditions, searchfield, "%s", "%s", "%s"), |
| 213 | tuple(filter_values + ["%%%s%%" % txt, start, page_len])) |
| 214 | |
| 215 | def get_cost_center_list(doctype, txt, searchfield, start, page_len, filters): |
| 216 | if not filters.get("group_or_ledger"): |
| 217 | filters["group_or_ledger"] = "Ledger" |
| 218 | |
| 219 | conditions, filter_values = build_filter_conditions(filters) |
| 220 | |
| 221 | return webnotes.conn.sql("""select name, parent_cost_center from `tabCost Center` |
| 222 | where docstatus < 2 %s and %s like %s order by name limit %s, %s""" % |
| 223 | (conditions, searchfield, "%s", "%s", "%s"), |
Anand Doshi | 7da72dd | 2013-03-20 17:00:41 +0530 | [diff] [blame] | 224 | tuple(filter_values + ["%%%s%%" % txt, start, page_len])) |
| 225 | |
| 226 | def remove_against_link_from_jv(ref_type, ref_no, against_field): |
| 227 | webnotes.conn.sql("""update `tabJournal Voucher Detail` set `%s`=null, |
| 228 | modified=%s, modified_by=%s |
| 229 | where `%s`=%s and docstatus < 2""" % (against_field, "%s", "%s", against_field, "%s"), |
| 230 | (now(), webnotes.session.user, ref_no)) |
| 231 | |
| 232 | webnotes.conn.sql("""update `tabGL Entry` |
| 233 | set against_voucher_type=null, against_voucher=null, |
| 234 | modified=%s, modified_by=%s |
| 235 | where against_voucher_type=%s and against_voucher=%s |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 236 | and voucher_no != ifnull(against_voucher, '')""", |
Anand Doshi | 7da72dd | 2013-03-20 17:00:41 +0530 | [diff] [blame] | 237 | (now(), webnotes.session.user, ref_type, ref_no)) |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 238 | |
| 239 | @webnotes.whitelist() |
| 240 | def get_company_default(company, fieldname): |
| 241 | value = webnotes.conn.get_value("Company", company, fieldname) |
| 242 | |
| 243 | if not value: |
| 244 | msgprint(_("Please mention default value for '") + |
| 245 | _(webnotes.get_doctype("company").get_label(fieldname) + |
| 246 | _("' in Company: ") + company), raise_exception=True) |
| 247 | |
| 248 | return value |
| 249 | |
| 250 | def create_stock_in_hand_jv(reverse=False): |
| 251 | from webnotes.utils import nowdate |
| 252 | today = nowdate() |
| 253 | fiscal_year = get_fiscal_year(today)[0] |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 254 | jv_list = [] |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 255 | |
| 256 | for company in webnotes.conn.sql_list("select name from `tabCompany`"): |
| 257 | stock_rbnb_value = get_stock_rbnb_value(company) |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 258 | stock_rbnb_value = reverse and -1*stock_rbnb_value or stock_rbnb_value |
| 259 | if stock_rbnb_value: |
| 260 | jv = webnotes.bean([ |
| 261 | { |
| 262 | "doctype": "Journal Voucher", |
Anand Doshi | 72edc3d | 2013-05-29 12:13:16 +0530 | [diff] [blame] | 263 | "naming_series": "JV-AUTO-", |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 264 | "company": company, |
| 265 | "posting_date": today, |
| 266 | "fiscal_year": fiscal_year, |
| 267 | "voucher_type": "Journal Entry", |
Nabin Hait | 4af2dbf | 2013-08-12 12:24:08 +0530 | [diff] [blame] | 268 | "user_remark": (_("Perpetual Accounting") + ": " + |
Anand Doshi | 72edc3d | 2013-05-29 12:13:16 +0530 | [diff] [blame] | 269 | (_("Disabled") if reverse else _("Enabled")) + ". " + |
| 270 | _("Journal Entry for inventory that is received but not yet invoiced")) |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 271 | }, |
| 272 | { |
| 273 | "doctype": "Journal Voucher Detail", |
| 274 | "parentfield": "entries", |
| 275 | "account": get_company_default(company, "stock_received_but_not_billed"), |
Anand Doshi | 72edc3d | 2013-05-29 12:13:16 +0530 | [diff] [blame] | 276 | (stock_rbnb_value > 0 and "credit" or "debit"): abs(stock_rbnb_value) |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 277 | }, |
| 278 | { |
| 279 | "doctype": "Journal Voucher Detail", |
| 280 | "parentfield": "entries", |
| 281 | "account": get_company_default(company, "stock_adjustment_account"), |
Anand Doshi | 72edc3d | 2013-05-29 12:13:16 +0530 | [diff] [blame] | 282 | (stock_rbnb_value > 0 and "debit" or "credit"): abs(stock_rbnb_value), |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 283 | "cost_center": get_company_default(company, "stock_adjustment_cost_center") |
| 284 | }, |
| 285 | ]) |
| 286 | jv.insert() |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 287 | |
| 288 | jv_list.append(jv.doc.name) |
| 289 | |
| 290 | if jv_list: |
Anand Doshi | 72edc3d | 2013-05-29 12:13:16 +0530 | [diff] [blame] | 291 | msgprint(_("Following Journal Vouchers have been created automatically") + \ |
| 292 | ":\n%s" % ("\n".join([("<a href=\"#Form/Journal Voucher/%s\">%s</a>" % (jv, jv)) for jv in jv_list]),)) |
| 293 | |
| 294 | msgprint(_("""These adjustment vouchers book the difference between \ |
| 295 | the total value of received items and the total value of invoiced items, \ |
Nabin Hait | 4af2dbf | 2013-08-12 12:24:08 +0530 | [diff] [blame] | 296 | as a required step to use Perpetual Accounting. |
Anand Doshi | 72edc3d | 2013-05-29 12:13:16 +0530 | [diff] [blame] | 297 | This is an approximation to get you started. |
| 298 | You will need to submit these vouchers after checking if the values are correct. |
| 299 | For more details, read: \ |
| 300 | <a href="http://erpnext.com/auto-inventory-accounting" target="_blank">\ |
Nabin Hait | 4af2dbf | 2013-08-12 12:24:08 +0530 | [diff] [blame] | 301 | Perpetual Accounting</a>""")) |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 302 | |
Nabin Hait | 4af2dbf | 2013-08-12 12:24:08 +0530 | [diff] [blame] | 303 | webnotes.msgprint("""Please refresh the system to get effect of Perpetual Accounting""") |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 304 | |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 305 | |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 306 | def get_stock_rbnb_value(company): |
| 307 | total_received_amount = webnotes.conn.sql("""select sum(valuation_rate*qty*conversion_factor) |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 308 | from `tabPurchase Receipt Item` pr_item where docstatus=1 |
| 309 | and exists(select name from `tabItem` where name = pr_item.item_code |
| 310 | and is_stock_item='Yes') |
Nabin Hait | 41fe356 | 2013-03-28 15:54:11 +0530 | [diff] [blame] | 311 | and exists(select name from `tabPurchase Receipt` |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 312 | where name = pr_item.parent and company = %s)""", company) |
| 313 | |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 314 | total_billed_amount = webnotes.conn.sql("""select sum(valuation_rate*qty*conversion_factor) |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 315 | from `tabPurchase Invoice Item` pi_item where docstatus=1 |
| 316 | and exists(select name from `tabItem` where name = pi_item.item_code |
| 317 | and is_stock_item='Yes') |
Nabin Hait | 41fe356 | 2013-03-28 15:54:11 +0530 | [diff] [blame] | 318 | and exists(select name from `tabPurchase Invoice` |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 319 | where name = pi_item.parent and company = %s)""", company) |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 320 | return flt(total_received_amount[0][0]) - flt(total_billed_amount[0][0]) |
Nabin Hait | 8b509f5 | 2013-05-28 16:52:30 +0530 | [diff] [blame] | 321 | |
| 322 | |
| 323 | def fix_total_debit_credit(): |
| 324 | vouchers = webnotes.conn.sql("""select voucher_type, voucher_no, |
| 325 | sum(debit) - sum(credit) as diff |
| 326 | from `tabGL Entry` |
| 327 | group by voucher_type, voucher_no |
| 328 | having sum(ifnull(debit, 0)) != sum(ifnull(credit, 0))""", as_dict=1) |
| 329 | |
| 330 | for d in vouchers: |
| 331 | if abs(d.diff) > 0: |
| 332 | dr_or_cr = d.voucher_type == "Sales Invoice" and "credit" or "debit" |
| 333 | |
| 334 | webnotes.conn.sql("""update `tabGL Entry` set %s = %s + %s |
| 335 | where voucher_type = %s and voucher_no = %s and %s > 0 limit 1""" % |
| 336 | (dr_or_cr, dr_or_cr, '%s', '%s', '%s', dr_or_cr), |
Nabin Hait | cac622e | 2013-08-02 11:44:29 +0530 | [diff] [blame] | 337 | (d.diff, d.voucher_type, d.voucher_no)) |
| 338 | |
| 339 | def validate_stock_and_account_balance(): |
| 340 | difference = get_stock_and_account_difference() |
| 341 | if difference: |
| 342 | msgprint(_("Account balance must be synced with stock balance, \ |
| 343 | to enable perpetual accounting." + |
| 344 | _(" Following accounts are not synced with stock balance") + ": \n" + |
| 345 | "\n".join(difference.keys())), raise_exception=1) |
| 346 | |
| 347 | def get_stock_and_account_difference(warehouse_list=None): |
| 348 | from stock.utils import get_latest_stock_balance |
| 349 | |
| 350 | if not warehouse_list: |
| 351 | warehouse_list = webnotes.conn.sql_list("""select name from tabWarehouse |
| 352 | where docstatus<2""") |
Nabin Hait | 94d3963 | 2013-08-06 15:58:44 +0530 | [diff] [blame] | 353 | |
Nabin Hait | cac622e | 2013-08-02 11:44:29 +0530 | [diff] [blame] | 354 | account_warehouse_map = {} |
| 355 | warehouse_with_no_account = [] |
| 356 | difference = {} |
Nabin Hait | cac622e | 2013-08-02 11:44:29 +0530 | [diff] [blame] | 357 | warehouse_account = webnotes.conn.sql("""select name, account from tabWarehouse |
| 358 | where name in (%s)""" % ', '.join(['%s']*len(warehouse_list)), warehouse_list, as_dict=1) |
| 359 | |
| 360 | for wh in warehouse_account: |
| 361 | if not wh.account: warehouse_with_no_account.append(wh.name) |
| 362 | account_warehouse_map.setdefault(wh.account, []).append(wh.name) |
| 363 | |
| 364 | if warehouse_with_no_account: |
| 365 | msgprint(_("Please mention Perpetual Account in warehouse master for following warehouses") |
| 366 | + ": " + '\n'.join(warehouse_with_no_account), raise_exception=1) |
| 367 | |
Nabin Hait | 94d3963 | 2013-08-06 15:58:44 +0530 | [diff] [blame] | 368 | bin_map = get_latest_stock_balance() |
| 369 | for account, warehouse_list in account_warehouse_map.items(): |
Nabin Hait | cac622e | 2013-08-02 11:44:29 +0530 | [diff] [blame] | 370 | account_balance = get_balance_on(account) |
Nabin Hait | 94d3963 | 2013-08-06 15:58:44 +0530 | [diff] [blame] | 371 | stock_value = sum([sum(bin_map.get(warehouse, {}).values()) |
| 372 | for warehouse in warehouse_list]) |
Nabin Hait | aa34201 | 2013-08-07 14:21:04 +0530 | [diff] [blame] | 373 | if abs(flt(stock_value) - flt(account_balance)) > 0.005: |
Nabin Hait | 469ee71 | 2013-08-07 12:33:37 +0530 | [diff] [blame] | 374 | difference.setdefault(account, flt(stock_value) - flt(account_balance)) |
Nabin Hait | aa34201 | 2013-08-07 14:21:04 +0530 | [diff] [blame] | 375 | |
Nabin Hait | cc0e2d1 | 2013-08-06 16:19:18 +0530 | [diff] [blame] | 376 | return difference |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 377 | |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 378 | def validate_expense_against_budget(args): |
| 379 | args = webnotes._dict(args) |
| 380 | if webnotes.conn.get_value("Account", {"name": args.account, "is_pl_account": "Yes", |
| 381 | "debit_or_credit": "Debit"}): |
| 382 | budget = webnotes.conn.sql(""" |
| 383 | select bd.budget_allocated, cc.distribution_id |
| 384 | from `tabCost Center` cc, `tabBudget Detail` bd |
| 385 | where cc.name=bd.parent and cc.name=%s and account=%s and bd.fiscal_year=%s |
| 386 | """, (args.cost_center, args.account, args.fiscal_year), as_dict=True) |
| 387 | |
| 388 | if budget and budget[0].budget_allocated: |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 389 | yearly_action, monthly_action = webnotes.conn.get_value("Company", args.company, |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 390 | ["yearly_bgt_flag", "monthly_bgt_flag"]) |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 391 | action_for = action = "" |
| 392 | |
| 393 | if monthly_action in ["Stop", "Warn"]: |
| 394 | budget_amount = get_allocated_budget(budget[0].distribution_id, |
| 395 | args.posting_date, args.fiscal_year, budget[0].budget_allocated) |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 396 | |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 397 | args["month_end_date"] = webnotes.conn.sql("select LAST_DAY(%s)", |
| 398 | args.posting_date)[0][0] |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 399 | action_for, action = "Monthly", monthly_action |
| 400 | |
| 401 | elif yearly_action in ["Stop", "Warn"]: |
| 402 | budget_amount = budget[0].budget_allocated |
| 403 | action_for, action = "Monthly", yearly_action |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 404 | |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 405 | if action_for: |
| 406 | actual_expense = get_actual_expense(args) |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 407 | if actual_expense > budget_amount: |
| 408 | webnotes.msgprint(action_for + _(" budget ") + cstr(budget_amount) + |
| 409 | _(" for account ") + args.account + _(" against cost center ") + |
| 410 | args.cost_center + _(" will exceed by ") + |
| 411 | cstr(actual_expense - budget_amount) + _(" after this transaction.") |
| 412 | , raise_exception=BudgetError if action=="Stop" else False) |
| 413 | |
| 414 | def get_allocated_budget(distribution_id, posting_date, fiscal_year, yearly_budget): |
| 415 | if distribution_id: |
| 416 | distribution = {} |
| 417 | for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation |
| 418 | from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd |
| 419 | where bdd.parent=bd.name and bd.fiscal_year=%s""", fiscal_year, as_dict=1): |
| 420 | distribution.setdefault(d.month, d.percentage_allocation) |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 421 | |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 422 | dt = webnotes.conn.get_value("Fiscal Year", fiscal_year, "year_start_date") |
| 423 | budget_percentage = 0.0 |
| 424 | |
| 425 | while(dt <= getdate(posting_date)): |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 426 | if distribution_id: |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 427 | budget_percentage += distribution.get(getdate(dt).strftime("%B"), 0) |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 428 | else: |
| 429 | budget_percentage += 100.0/12 |
| 430 | |
| 431 | dt = add_months(dt, 1) |
| 432 | |
| 433 | return yearly_budget * budget_percentage / 100 |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 434 | |
| 435 | def get_actual_expense(args): |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 436 | args["condition"] = " and posting_date<='%s'" % args.month_end_date \ |
| 437 | if args.get("month_end_date") else "" |
| 438 | |
Nabin Hait | b4418a4 | 2013-08-22 14:38:46 +0530 | [diff] [blame] | 439 | return webnotes.conn.sql(""" |
| 440 | select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) |
| 441 | from `tabGL Entry` |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 442 | where account='%(account)s' and cost_center='%(cost_center)s' |
| 443 | and fiscal_year='%(fiscal_year)s' and company='%(company)s' %(condition)s |
| 444 | """ % (args))[0][0] |