Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | from __future__ import unicode_literals |
| 18 | import webnotes |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 19 | from webnotes import _, msgprint |
| 20 | from webnotes.utils import flt, cint |
| 21 | from setup.utils import get_company_currency, get_price_list_currency |
| 22 | from utilities.transaction_base import TransactionBase, validate_conversion_rate |
| 23 | import json |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 24 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 25 | class AccountsController(TransactionBase): |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 26 | def validate(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 27 | self.set_missing_values(for_validate=True) |
| 28 | |
| 29 | if self.meta.get_field("currency"): |
| 30 | self.company_currency = get_company_currency(self.doc.company) |
| 31 | |
| 32 | validate_conversion_rate(self.doc.currency, self.doc.conversion_rate, |
| 33 | self.meta.get_label("conversion_rate"), self.doc.company) |
| 34 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 35 | self.calculate_taxes_and_totals() |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 36 | self.validate_value("grand_total", ">=", 0) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 37 | self.set_total_in_words() |
| 38 | |
| 39 | def set_price_list_currency(self, buying_or_selling): |
| 40 | # TODO - change this, since price list now has only one currency allowed |
| 41 | if self.meta.get_field("price_list_name") and self.doc.price_list_name and \ |
| 42 | not self.doc.price_list_currency: |
| 43 | self.doc.fields.update(get_price_list_currency({ |
| 44 | "price_list_name": self.doc.price_list_name, |
| 45 | "use_for": buying_or_selling |
| 46 | })) |
| 47 | |
| 48 | def set_missing_item_details(self, get_item_details): |
| 49 | """set missing item values""" |
| 50 | for item in self.doclist.get({"parentfield": self.fname}): |
| 51 | if item.fields.get("item_code"): |
| 52 | args = item.fields.copy().update(self.doc.fields) |
| 53 | ret = get_item_details(args) |
| 54 | for fieldname, value in ret.items(): |
| 55 | if self.meta.get_field(fieldname, parentfield=self.fname) and \ |
| 56 | not item.fields.get(fieldname): |
| 57 | item.fields[fieldname] = value |
| 58 | |
| 59 | def set_taxes(self, tax_doctype, tax_parentfield, tax_master_field): |
| 60 | if not self.meta.get_field(tax_parentfield): |
| 61 | return |
| 62 | |
| 63 | if not self.doclist.get({"parentfield": tax_parentfield}): |
| 64 | if not self.doc.fields.get(tax_master_field): |
| 65 | # get the default tax master |
| 66 | self.doc.fields[tax_master_field] = \ |
| 67 | webnotes.conn.get_value(tax_doctype + " Master", {"is_default": 1}) |
| 68 | |
| 69 | if self.doc.fields.get(tax_master_field): |
| 70 | from webnotes.model import default_fields |
| 71 | tax_master = webnotes.bean(tax_doctype, self.doc.fields.get(tax_master_field)) |
| 72 | |
| 73 | for i, tax in enumerate(tax_master.doclist.get({"parentfield": tax_parentfield})): |
| 74 | for fieldname in default_fields: |
| 75 | tax.fields[fieldname] = None |
| 76 | |
| 77 | tax.fields.update({ |
| 78 | "doctype": tax_doctype, |
| 79 | "parentfield": tax_parentfield, |
| 80 | "idx": i+1 |
| 81 | }) |
| 82 | |
| 83 | self.doclist.append(tax) |
| 84 | |
| 85 | def calculate_taxes_and_totals(self): |
| 86 | self.doc.conversion_rate = flt(self.doc.conversion_rate) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 87 | self.item_doclist = self.doclist.get({"parentfield": self.fname}) |
| 88 | self.tax_doclist = self.doclist.get({"parentfield": self.other_fname}) |
| 89 | |
| 90 | self.calculate_item_values() |
| 91 | self.initialize_taxes() |
| 92 | |
| 93 | if hasattr(self, "determine_exclusive_rate"): |
| 94 | self.determine_exclusive_rate() |
| 95 | |
| 96 | self.calculate_net_total() |
| 97 | self.calculate_taxes() |
| 98 | self.calculate_totals() |
| 99 | self._cleanup() |
| 100 | |
| 101 | # TODO |
| 102 | # print format: show net_total_export instead of net_total |
| 103 | |
| 104 | def initialize_taxes(self): |
| 105 | for tax in self.tax_doclist: |
| 106 | tax.item_wise_tax_detail = {} |
| 107 | for fieldname in ["tax_amount", "total", |
| 108 | "tax_amount_for_current_item", "grand_total_for_current_item", |
| 109 | "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]: |
| 110 | tax.fields[fieldname] = 0.0 |
| 111 | |
| 112 | self.validate_on_previous_row(tax) |
| 113 | self.validate_inclusive_tax(tax) |
| 114 | self.round_floats_in(tax) |
| 115 | |
| 116 | def validate_on_previous_row(self, tax): |
| 117 | """ |
| 118 | validate if a valid row id is mentioned in case of |
| 119 | On Previous Row Amount and On Previous Row Total |
| 120 | """ |
| 121 | if tax.charge_type in ["On Previous Row Amount", "On Previous Row Total"] and \ |
| 122 | (not tax.row_id or cint(tax.row_id) >= tax.idx): |
| 123 | msgprint((_("Row") + " # %(idx)s [%(taxes_doctype)s]: " + \ |
| 124 | _("Please specify a valid") + " %(row_id_label)s") % { |
| 125 | "idx": tax.idx, |
| 126 | "taxes_doctype": tax.doctype, |
| 127 | "row_id_label": self.meta.get_label("row_id", |
| 128 | parentfield=self.other_fname) |
| 129 | }, raise_exception=True) |
| 130 | |
| 131 | def validate_inclusive_tax(self, tax): |
| 132 | def _on_previous_row_error(row_range): |
| 133 | msgprint((_("Row") + " # %(idx)s [%(doctype)s]: " + |
| 134 | _("to be included in Item's rate, it is required that: ") + |
| 135 | " [" + _("Row") + " # %(row_range)s] " + _("also be included in Item's rate")) % { |
| 136 | "idx": tax.idx, |
| 137 | "doctype": tax.doctype, |
| 138 | "inclusive_label": self.meta.get_label("included_in_print_rate", |
| 139 | parentfield=self.other_fname), |
| 140 | "charge_type_label": self.meta.get_label("charge_type", |
| 141 | parentfield=self.other_fname), |
| 142 | "charge_type": tax.charge_type, |
| 143 | "row_range": row_range |
| 144 | }, raise_exception=True) |
| 145 | |
| 146 | if cint(tax.included_in_print_rate): |
| 147 | if tax.charge_type == "Actual": |
| 148 | # inclusive tax cannot be of type Actual |
| 149 | msgprint((_("Row") |
| 150 | + " # %(idx)s [%(doctype)s]: %(charge_type_label)s = \"%(charge_type)s\" " |
| 151 | + "cannot be included in Item's rate") % { |
| 152 | "idx": tax.idx, |
| 153 | "doctype": tax.doctype, |
| 154 | "charge_type_label": self.meta.get_label("charge_type", |
| 155 | parentfield=self.other_fname), |
| 156 | "charge_type": tax.charge_type, |
| 157 | }, raise_exception=True) |
| 158 | elif tax.charge_type == "On Previous Row Amount" and \ |
| 159 | not cint(self.tax_doclist[tax.row_id - 1].included_in_print_rate): |
| 160 | # referred row should also be inclusive |
| 161 | _on_previous_row_error(tax.row_id) |
| 162 | elif tax.charge_type == "On Previous Row Total" and \ |
| 163 | not all([cint(t.included_in_print_rate) for t in self.tax_doclist[:tax.row_id - 1]]): |
| 164 | # all rows about the reffered tax should be inclusive |
| 165 | _on_previous_row_error("1 - %d" % (tax.row_id,)) |
| 166 | |
| 167 | def calculate_taxes(self): |
| 168 | for item in self.item_doclist: |
| 169 | item_tax_map = self._load_item_tax_rate(item.item_tax_rate) |
| 170 | |
| 171 | for i, tax in enumerate(self.tax_doclist): |
| 172 | # tax_amount represents the amount of tax for the current step |
| 173 | current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map) |
| 174 | |
| 175 | if hasattr(self, "set_item_tax_amount"): |
| 176 | self.set_item_tax_amount(item, tax, current_tax_amount) |
| 177 | |
| 178 | # case when net total is 0 but there is an actual type charge |
| 179 | # in this case add the actual amount to tax.tax_amount |
| 180 | # and tax.grand_total_for_current_item for the first such iteration |
| 181 | if tax.charge_type=="Actual" and \ |
| 182 | not (current_tax_amount or self.doc.net_total or tax.tax_amount): |
| 183 | zero_net_total_adjustment = flt(tax.rate, self.precision("tax_amount", tax)) |
| 184 | current_tax_amount += zero_net_total_adjustment |
| 185 | |
| 186 | # store tax_amount for current item as it will be used for |
| 187 | # charge type = 'On Previous Row Amount' |
| 188 | tax.tax_amount_for_current_item = current_tax_amount |
| 189 | |
| 190 | # accumulate tax amount into tax.tax_amount |
| 191 | tax.tax_amount += current_tax_amount |
| 192 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 193 | if tax.category: |
| 194 | # if just for valuation, do not add the tax amount in total |
| 195 | # hence, setting it as 0 for further steps |
| 196 | current_tax_amount = 0.0 if (tax.category == "Valuation") else current_tax_amount |
| 197 | |
| 198 | current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0 |
| 199 | |
| 200 | # Calculate tax.total viz. grand total till that step |
| 201 | # note: grand_total_for_current_item contains the contribution of |
| 202 | # item's amount, previously applied tax and the current tax on that item |
| 203 | if i==0: |
| 204 | tax.grand_total_for_current_item = flt(item.amount + |
| 205 | current_tax_amount, self.precision("total", tax)) |
| 206 | |
| 207 | else: |
| 208 | tax.grand_total_for_current_item = \ |
| 209 | flt(self.tax_doclist[i-1].grand_total_for_current_item + |
| 210 | current_tax_amount, self.precision("total", tax)) |
| 211 | |
| 212 | # in tax.total, accumulate grand total of each item |
| 213 | tax.total += tax.grand_total_for_current_item |
| 214 | |
| 215 | def get_current_tax_amount(self, item, tax, item_tax_map): |
| 216 | tax_rate = self._get_tax_rate(tax, item_tax_map) |
| 217 | current_tax_amount = 0.0 |
| 218 | |
| 219 | if tax.charge_type == "Actual": |
| 220 | # distribute the tax amount proportionally to each item row |
| 221 | actual = flt(tax.rate, self.precision("tax_amount", tax)) |
| 222 | current_tax_amount = (self.doc.net_total |
| 223 | and ((item.amount / self.doc.net_total) * actual) |
| 224 | or 0) |
| 225 | elif tax.charge_type == "On Net Total": |
| 226 | current_tax_amount = (tax_rate / 100.0) * item.amount |
| 227 | elif tax.charge_type == "On Previous Row Amount": |
| 228 | current_tax_amount = (tax_rate / 100.0) * \ |
| 229 | self.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item |
| 230 | elif tax.charge_type == "On Previous Row Total": |
| 231 | current_tax_amount = (tax_rate / 100.0) * \ |
| 232 | self.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 233 | |
| 234 | current_tax_amount = flt(current_tax_amount, self.precision("tax_amount", tax)) |
| 235 | |
| 236 | # store tax breakup for each item |
| 237 | tax.item_wise_tax_detail[item.item_code or item.item_name] = [tax_rate, current_tax_amount] |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 238 | |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 239 | return current_tax_amount |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 240 | |
| 241 | def _load_item_tax_rate(self, item_tax_rate): |
| 242 | return json.loads(item_tax_rate) if item_tax_rate else {} |
| 243 | |
| 244 | def _get_tax_rate(self, tax, item_tax_map): |
| 245 | if item_tax_map.has_key(tax.account_head): |
| 246 | return flt(item_tax_map.get(tax.account_head), self.precision("rate", tax)) |
| 247 | else: |
| 248 | return tax.rate |
| 249 | |
| 250 | def _cleanup(self): |
| 251 | for tax in self.tax_doclist: |
| 252 | for fieldname in ("grand_total_for_current_item", |
| 253 | "tax_amount_for_current_item", |
| 254 | "tax_fraction_for_current_item", |
| 255 | "grand_total_fraction_for_current_item"): |
| 256 | if fieldname in tax.fields: |
| 257 | del tax.fields[fieldname] |
| 258 | |
| 259 | tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail) |
| 260 | |
| 261 | def _set_in_company_currency(self, item, print_field, base_field): |
| 262 | """set values in base currency""" |
| 263 | item.fields[base_field] = flt((flt(item.fields[print_field], |
| 264 | self.precision(print_field, item)) * self.doc.conversion_rate), |
| 265 | self.precision(base_field, item)) |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 266 | |
| 267 | def calculate_total_advance(self, parenttype, advance_parentfield): |
| 268 | if self.doc.doctype == parenttype and self.doc.docstatus < 2: |
| 269 | sum_of_allocated_amount = sum([flt(adv.allocated_amount, self.precision("allocated_amount", adv)) |
| 270 | for adv in self.doclist.get({"parentfield": advance_parentfield})]) |
| 271 | |
| 272 | self.doc.total_advance = flt(sum_of_allocated_amount, self.precision("total_advance")) |
| 273 | |
| 274 | self.calculate_outstanding_amount() |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 275 | |
Nabin Hait | 8c7234f | 2013-03-11 16:32:33 +0530 | [diff] [blame] | 276 | def get_gl_dict(self, args, cancel=None): |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 277 | """this method populates the common properties of a gl entry record""" |
Anand Doshi | 1cf7391 | 2013-03-15 13:42:31 +0530 | [diff] [blame] | 278 | if cancel is None: |
| 279 | cancel = (self.doc.docstatus == 2) |
| 280 | |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 281 | gl_dict = { |
| 282 | 'company': self.doc.company, |
| 283 | 'posting_date': self.doc.posting_date, |
| 284 | 'voucher_type': self.doc.doctype, |
| 285 | 'voucher_no': self.doc.name, |
| 286 | 'aging_date': self.doc.fields.get("aging_date") or self.doc.posting_date, |
| 287 | 'remarks': self.doc.remarks, |
Anand Doshi | 1cf7391 | 2013-03-15 13:42:31 +0530 | [diff] [blame] | 288 | 'is_cancelled': cancel and "Yes" or "No", |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 289 | 'fiscal_year': self.doc.fiscal_year, |
| 290 | 'debit': 0, |
| 291 | 'credit': 0, |
| 292 | 'is_opening': self.doc.fields.get("is_opening") or "No", |
| 293 | } |
| 294 | gl_dict.update(args) |
| 295 | return gl_dict |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 296 | |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 297 | def clear_unallocated_advances(self, childtype, parentfield): |
| 298 | self.doclist.remove_items({"parentfield": parentfield, "allocated_amount": ["in", [0, None, ""]]}) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 299 | |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 300 | webnotes.conn.sql("""delete from `tab%s` where parentfield=%s and parent = %s |
| 301 | and ifnull(allocated_amount, 0) = 0""" % (childtype, '%s', '%s'), (parentfield, self.doc.name)) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 302 | |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 303 | def get_advances(self, account_head, child_doctype, parentfield, dr_or_cr): |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 304 | res = webnotes.conn.sql("""select t1.name as jv_no, t1.remark, |
| 305 | t2.%s as amount, t2.name as jv_detail_no |
| 306 | from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2 |
| 307 | where t1.name = t2.parent and t2.account = %s and t2.is_advance = 'Yes' |
| 308 | and (t2.against_voucher is null or t2.against_voucher = '') |
| 309 | and (t2.against_invoice is null or t2.against_invoice = '') |
| 310 | and (t2.against_jv is null or t2.against_jv = '') |
| 311 | and t1.docstatus = 1 order by t1.posting_date""" % |
| 312 | (dr_or_cr, '%s'), account_head, as_dict=1) |
| 313 | |
| 314 | self.doclist = self.doc.clear_table(self.doclist, parentfield) |
| 315 | for d in res: |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 316 | self.doclist.append({ |
| 317 | "doctype": child_doctype, |
| 318 | "parentfield": parentfield, |
| 319 | "journal_voucher": d.jv_no, |
| 320 | "jv_detail_no": d.jv_detail_no, |
| 321 | "remarks": d.remark, |
| 322 | "advance_amount": flt(d.amount), |
| 323 | "allocate_amount": 0 |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 324 | }) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 325 | |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 326 | def get_company_default(self, fieldname): |
| 327 | from accounts.utils import get_company_default |
| 328 | return get_company_default(self.doc.company, fieldname) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 329 | |
| 330 | @property |
| 331 | def stock_items(self): |
| 332 | if not hasattr(self, "_stock_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 333 | self._stock_items = [] |
| 334 | item_codes = list(set(item.item_code for item in |
| 335 | self.doclist.get({"parentfield": self.fname}))) |
| 336 | if item_codes: |
| 337 | self._stock_items = [r[0] for r in webnotes.conn.sql("""select name |
| 338 | from `tabItem` where name in (%s) and is_stock_item='Yes'""" % \ |
| 339 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Nabin Hait | 80abad2 | 2013-03-19 18:18:52 +0530 | [diff] [blame] | 340 | |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 341 | return self._stock_items |
| 342 | |
| 343 | @property |
| 344 | def company_abbr(self): |
| 345 | if not hasattr(self, "_abbr"): |
| 346 | self._abbr = webnotes.conn.get_value("Company", self.doc.company, "abbr") |
| 347 | |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 348 | return self._abbr |