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 |
Anand Doshi | 7da72dd | 2013-03-20 17:00:41 +0530 | [diff] [blame] | 7 | from webnotes.utils import nowdate, cstr, flt, now |
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 |
| 15 | |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 16 | def get_fiscal_year(date=None, fiscal_year=None, label="Date", verbose=1): |
| 17 | return get_fiscal_years(date, fiscal_year, label, verbose=1)[0] |
Rushabh Mehta | c2563ef | 2013-02-05 23:25:37 +0530 | [diff] [blame] | 18 | |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 19 | 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] | 20 | # 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] | 21 | cond = "" |
| 22 | if fiscal_year: |
| 23 | cond = "name = '%s'" % fiscal_year |
| 24 | else: |
| 25 | cond = "'%s' >= year_start_date and '%s' < adddate(year_start_date, interval 1 year)" % \ |
| 26 | (date, date) |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 27 | fy = webnotes.conn.sql("""select name, year_start_date, |
| 28 | subdate(adddate(year_start_date, interval 1 year), interval 1 day) |
| 29 | as year_end_date |
| 30 | from `tabFiscal Year` |
Nabin Hait | 0930b94 | 2013-06-20 16:35:09 +0530 | [diff] [blame] | 31 | where %s |
| 32 | order by year_start_date desc""" % cond) |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 33 | |
| 34 | if not fy: |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 35 | error_msg = """%s %s not in any Fiscal Year""" % (label, 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: |
| 67 | from webnotes.utils import getdate |
| 68 | if getdate(date) > getdate(nowdate()): |
| 69 | # if fiscal year not found and the date is greater than today |
| 70 | # get fiscal year for today's date and its corresponding year start date |
| 71 | year_start_date = get_fiscal_year(nowdate(), verbose=1)[1] |
| 72 | else: |
| 73 | # this indicates that it is a date older than any existing fiscal year. |
| 74 | # hence, assuming balance as 0.0 |
| 75 | return 0.0 |
| 76 | |
| 77 | acc = webnotes.conn.get_value('Account', account, \ |
| 78 | ['lft', 'rgt', 'debit_or_credit', 'is_pl_account', 'group_or_ledger'], as_dict=1) |
| 79 | |
| 80 | # for pl accounts, get balance within a fiscal year |
| 81 | if acc.is_pl_account == 'Yes': |
| 82 | cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" \ |
| 83 | % year_start_date) |
| 84 | |
| 85 | # different filter for group and ledger - improved performance |
| 86 | if acc.group_or_ledger=="Group": |
| 87 | cond.append("""exists ( |
| 88 | select * from `tabAccount` ac where ac.name = gle.account |
| 89 | and ac.lft >= %s and ac.rgt <= %s |
| 90 | )""" % (acc.lft, acc.rgt)) |
| 91 | else: |
| 92 | cond.append("""gle.account = "%s" """ % (account, )) |
| 93 | |
| 94 | # join conditional conditions |
| 95 | cond = " and ".join(cond) |
| 96 | if cond: |
| 97 | cond += " and " |
| 98 | |
| 99 | bal = webnotes.conn.sql(""" |
| 100 | SELECT sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) |
| 101 | FROM `tabGL Entry` gle |
| 102 | WHERE %s ifnull(is_cancelled, 'No') = 'No' """ % (cond, ))[0][0] |
| 103 | |
| 104 | # if credit account, it should calculate credit - debit |
| 105 | if bal and acc.debit_or_credit == 'Credit': |
| 106 | bal = -bal |
| 107 | |
| 108 | # if bal is None, return 0 |
Anand Doshi | 6fa5e42 | 2013-07-19 15:33:11 +0530 | [diff] [blame] | 109 | return flt(bal) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 110 | |
| 111 | @webnotes.whitelist() |
| 112 | def add_ac(args=None): |
| 113 | if not args: |
| 114 | args = webnotes.form_dict |
| 115 | args.pop("cmd") |
Rushabh Mehta | aae4553 | 2013-02-15 14:39:34 +0530 | [diff] [blame] | 116 | |
Rushabh Mehta | c53231a | 2013-02-18 18:24:28 +0530 | [diff] [blame] | 117 | ac = webnotes.bean(args) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 118 | ac.doc.doctype = "Account" |
| 119 | ac.doc.old_parent = "" |
| 120 | ac.doc.freeze_account = "No" |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 121 | ac.insert() |
| 122 | return ac.doc.name |
| 123 | |
| 124 | @webnotes.whitelist() |
| 125 | def add_cc(args=None): |
| 126 | if not args: |
| 127 | args = webnotes.form_dict |
| 128 | args.pop("cmd") |
| 129 | |
Rushabh Mehta | c53231a | 2013-02-18 18:24:28 +0530 | [diff] [blame] | 130 | cc = webnotes.bean(args) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 131 | cc.doc.doctype = "Cost Center" |
| 132 | cc.doc.old_parent = "" |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 133 | cc.insert() |
| 134 | return cc.doc.name |
| 135 | |
| 136 | def reconcile_against_document(args): |
| 137 | """ |
| 138 | Cancel JV, Update aginst document, split if required and resubmit jv |
| 139 | """ |
| 140 | for d in args: |
| 141 | check_if_jv_modified(d) |
| 142 | |
| 143 | against_fld = { |
| 144 | 'Journal Voucher' : 'against_jv', |
| 145 | 'Sales Invoice' : 'against_invoice', |
| 146 | 'Purchase Invoice' : 'against_voucher' |
| 147 | } |
| 148 | |
| 149 | d['against_fld'] = against_fld[d['against_voucher_type']] |
| 150 | |
| 151 | # cancel JV |
| 152 | jv_obj = webnotes.get_obj('Journal Voucher', d['voucher_no'], with_children=1) |
| 153 | jv_obj.make_gl_entries(cancel=1, adv_adj=1) |
| 154 | |
| 155 | # update ref in JV Detail |
| 156 | update_against_doc(d, jv_obj) |
| 157 | |
| 158 | # re-submit JV |
| 159 | jv_obj = webnotes.get_obj('Journal Voucher', d['voucher_no'], with_children =1) |
| 160 | jv_obj.make_gl_entries(cancel = 0, adv_adj =1) |
| 161 | |
| 162 | |
| 163 | def check_if_jv_modified(args): |
| 164 | """ |
| 165 | check if there is already a voucher reference |
| 166 | check if amount is same |
| 167 | check if jv is submitted |
| 168 | """ |
| 169 | ret = webnotes.conn.sql(""" |
| 170 | select t2.%(dr_or_cr)s from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2 |
| 171 | where t1.name = t2.parent and t2.account = '%(account)s' |
| 172 | and ifnull(t2.against_voucher, '')='' |
| 173 | and ifnull(t2.against_invoice, '')='' and ifnull(t2.against_jv, '')='' |
| 174 | and t1.name = '%(voucher_no)s' and t2.name = '%(voucher_detail_no)s' |
| 175 | and t1.docstatus=1 and t2.%(dr_or_cr)s = %(unadjusted_amt)s""" % args) |
| 176 | |
| 177 | if not ret: |
| 178 | msgprint(_("""Payment Entry has been modified after you pulled it. |
| 179 | Please pull it again."""), raise_exception=1) |
| 180 | |
| 181 | def update_against_doc(d, jv_obj): |
| 182 | """ |
| 183 | Updates against document, if partial amount splits into rows |
| 184 | """ |
| 185 | |
| 186 | webnotes.conn.sql(""" |
| 187 | update `tabJournal Voucher Detail` t1, `tabJournal Voucher` t2 |
| 188 | set t1.%(dr_or_cr)s = '%(allocated_amt)s', |
| 189 | t1.%(against_fld)s = '%(against_voucher)s', t2.modified = now() |
| 190 | where t1.name = '%(voucher_detail_no)s' and t1.parent = t2.name""" % d) |
| 191 | |
| 192 | if d['allocated_amt'] < d['unadjusted_amt']: |
| 193 | jvd = webnotes.conn.sql("""select cost_center, balance, against_account, is_advance |
| 194 | from `tabJournal Voucher Detail` where name = %s""", d['voucher_detail_no']) |
| 195 | # new entry with balance amount |
| 196 | ch = addchild(jv_obj.doc, 'entries', 'Journal Voucher Detail') |
| 197 | ch.account = d['account'] |
| 198 | ch.cost_center = cstr(jvd[0][0]) |
| 199 | ch.balance = cstr(jvd[0][1]) |
| 200 | ch.fields[d['dr_or_cr']] = flt(d['unadjusted_amt']) - flt(d['allocated_amt']) |
| 201 | ch.fields[d['dr_or_cr']== 'debit' and 'credit' or 'debit'] = 0 |
| 202 | ch.against_account = cstr(jvd[0][2]) |
| 203 | ch.is_advance = cstr(jvd[0][3]) |
| 204 | ch.docstatus = 1 |
Nabin Hait | a76a068 | 2013-02-08 14:04:13 +0530 | [diff] [blame] | 205 | ch.save(1) |
| 206 | |
| 207 | def get_account_list(doctype, txt, searchfield, start, page_len, filters): |
| 208 | if not filters.get("group_or_ledger"): |
| 209 | filters["group_or_ledger"] = "Ledger" |
| 210 | |
| 211 | conditions, filter_values = build_filter_conditions(filters) |
| 212 | |
| 213 | return webnotes.conn.sql("""select name, parent_account from `tabAccount` |
| 214 | where docstatus < 2 %s and %s like %s order by name limit %s, %s""" % |
| 215 | (conditions, searchfield, "%s", "%s", "%s"), |
| 216 | tuple(filter_values + ["%%%s%%" % txt, start, page_len])) |
| 217 | |
| 218 | def get_cost_center_list(doctype, txt, searchfield, start, page_len, filters): |
| 219 | if not filters.get("group_or_ledger"): |
| 220 | filters["group_or_ledger"] = "Ledger" |
| 221 | |
| 222 | conditions, filter_values = build_filter_conditions(filters) |
| 223 | |
| 224 | return webnotes.conn.sql("""select name, parent_cost_center from `tabCost Center` |
| 225 | where docstatus < 2 %s and %s like %s order by name limit %s, %s""" % |
| 226 | (conditions, searchfield, "%s", "%s", "%s"), |
Anand Doshi | 7da72dd | 2013-03-20 17:00:41 +0530 | [diff] [blame] | 227 | tuple(filter_values + ["%%%s%%" % txt, start, page_len])) |
| 228 | |
| 229 | def remove_against_link_from_jv(ref_type, ref_no, against_field): |
| 230 | webnotes.conn.sql("""update `tabJournal Voucher Detail` set `%s`=null, |
| 231 | modified=%s, modified_by=%s |
| 232 | where `%s`=%s and docstatus < 2""" % (against_field, "%s", "%s", against_field, "%s"), |
| 233 | (now(), webnotes.session.user, ref_no)) |
| 234 | |
| 235 | webnotes.conn.sql("""update `tabGL Entry` |
| 236 | set against_voucher_type=null, against_voucher=null, |
| 237 | modified=%s, modified_by=%s |
| 238 | where against_voucher_type=%s and against_voucher=%s |
| 239 | and voucher_no != ifnull(against_voucher, "") |
| 240 | and ifnull(is_cancelled, "No")="No" """, |
| 241 | (now(), webnotes.session.user, ref_type, ref_no)) |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 242 | |
| 243 | @webnotes.whitelist() |
| 244 | def get_company_default(company, fieldname): |
| 245 | value = webnotes.conn.get_value("Company", company, fieldname) |
| 246 | |
| 247 | if not value: |
| 248 | msgprint(_("Please mention default value for '") + |
| 249 | _(webnotes.get_doctype("company").get_label(fieldname) + |
| 250 | _("' in Company: ") + company), raise_exception=True) |
| 251 | |
| 252 | return value |
| 253 | |
| 254 | def create_stock_in_hand_jv(reverse=False): |
| 255 | from webnotes.utils import nowdate |
| 256 | today = nowdate() |
| 257 | fiscal_year = get_fiscal_year(today)[0] |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 258 | jv_list = [] |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 259 | |
| 260 | for company in webnotes.conn.sql_list("select name from `tabCompany`"): |
| 261 | stock_rbnb_value = get_stock_rbnb_value(company) |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 262 | stock_rbnb_value = reverse and -1*stock_rbnb_value or stock_rbnb_value |
| 263 | if stock_rbnb_value: |
| 264 | jv = webnotes.bean([ |
| 265 | { |
| 266 | "doctype": "Journal Voucher", |
Anand Doshi | 72edc3d | 2013-05-29 12:13:16 +0530 | [diff] [blame] | 267 | "naming_series": "JV-AUTO-", |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 268 | "company": company, |
| 269 | "posting_date": today, |
| 270 | "fiscal_year": fiscal_year, |
| 271 | "voucher_type": "Journal Entry", |
Anand Doshi | 72edc3d | 2013-05-29 12:13:16 +0530 | [diff] [blame] | 272 | "user_remark": (_("Auto Inventory Accounting") + ": " + |
| 273 | (_("Disabled") if reverse else _("Enabled")) + ". " + |
| 274 | _("Journal Entry for inventory that is received but not yet invoiced")) |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 275 | }, |
| 276 | { |
| 277 | "doctype": "Journal Voucher Detail", |
| 278 | "parentfield": "entries", |
| 279 | "account": get_company_default(company, "stock_received_but_not_billed"), |
Anand Doshi | 72edc3d | 2013-05-29 12:13:16 +0530 | [diff] [blame] | 280 | (stock_rbnb_value > 0 and "credit" or "debit"): abs(stock_rbnb_value) |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 281 | }, |
| 282 | { |
| 283 | "doctype": "Journal Voucher Detail", |
| 284 | "parentfield": "entries", |
| 285 | "account": get_company_default(company, "stock_adjustment_account"), |
Anand Doshi | 72edc3d | 2013-05-29 12:13:16 +0530 | [diff] [blame] | 286 | (stock_rbnb_value > 0 and "debit" or "credit"): abs(stock_rbnb_value), |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 287 | "cost_center": get_company_default(company, "stock_adjustment_cost_center") |
| 288 | }, |
| 289 | ]) |
| 290 | jv.insert() |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 291 | |
| 292 | jv_list.append(jv.doc.name) |
| 293 | |
| 294 | if jv_list: |
Anand Doshi | 72edc3d | 2013-05-29 12:13:16 +0530 | [diff] [blame] | 295 | msgprint(_("Following Journal Vouchers have been created automatically") + \ |
| 296 | ":\n%s" % ("\n".join([("<a href=\"#Form/Journal Voucher/%s\">%s</a>" % (jv, jv)) for jv in jv_list]),)) |
| 297 | |
| 298 | msgprint(_("""These adjustment vouchers book the difference between \ |
| 299 | the total value of received items and the total value of invoiced items, \ |
| 300 | as a required step to use Auto Inventory Accounting. |
| 301 | This is an approximation to get you started. |
| 302 | You will need to submit these vouchers after checking if the values are correct. |
| 303 | For more details, read: \ |
| 304 | <a href="http://erpnext.com/auto-inventory-accounting" target="_blank">\ |
| 305 | Auto Inventory Accounting</a>""")) |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 306 | |
| 307 | webnotes.msgprint("""Please refresh the system to get effect of Auto Inventory Accounting""") |
| 308 | |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 309 | |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 310 | def get_stock_rbnb_value(company): |
| 311 | 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] | 312 | from `tabPurchase Receipt Item` pr_item where docstatus=1 |
| 313 | and exists(select name from `tabItem` where name = pr_item.item_code |
| 314 | and is_stock_item='Yes') |
Nabin Hait | 41fe356 | 2013-03-28 15:54:11 +0530 | [diff] [blame] | 315 | and exists(select name from `tabPurchase Receipt` |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 316 | where name = pr_item.parent and company = %s)""", company) |
| 317 | |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 318 | 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] | 319 | from `tabPurchase Invoice Item` pi_item where docstatus=1 |
| 320 | and exists(select name from `tabItem` where name = pi_item.item_code |
| 321 | and is_stock_item='Yes') |
Nabin Hait | 41fe356 | 2013-03-28 15:54:11 +0530 | [diff] [blame] | 322 | and exists(select name from `tabPurchase Invoice` |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 323 | where name = pi_item.parent and company = %s)""", company) |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 324 | 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] | 325 | |
| 326 | |
| 327 | def fix_total_debit_credit(): |
| 328 | vouchers = webnotes.conn.sql("""select voucher_type, voucher_no, |
| 329 | sum(debit) - sum(credit) as diff |
| 330 | from `tabGL Entry` |
| 331 | group by voucher_type, voucher_no |
| 332 | having sum(ifnull(debit, 0)) != sum(ifnull(credit, 0))""", as_dict=1) |
| 333 | |
| 334 | for d in vouchers: |
| 335 | if abs(d.diff) > 0: |
| 336 | dr_or_cr = d.voucher_type == "Sales Invoice" and "credit" or "debit" |
| 337 | |
| 338 | webnotes.conn.sql("""update `tabGL Entry` set %s = %s + %s |
| 339 | where voucher_type = %s and voucher_no = %s and %s > 0 limit 1""" % |
| 340 | (dr_or_cr, dr_or_cr, '%s', '%s', '%s', dr_or_cr), |
Anand Doshi | 4bef02e | 2013-06-28 19:21:44 +0530 | [diff] [blame] | 341 | (d.diff, d.voucher_type, d.voucher_no)) |