Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +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 | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 19 | from webnotes import _, msgprint |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 20 | from webnotes.utils import flt, cint |
| 21 | import json |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 22 | |
| 23 | from buying.utils import get_item_details |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 24 | from setup.utils import get_company_currency |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 25 | from webnotes.model.utils import round_floats_in_doc |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 26 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 27 | from controllers.accounts_controller import AccountsController |
| 28 | |
| 29 | class BuyingController(AccountsController): |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 30 | def validate(self): |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 31 | if self.meta.get_field("currency"): |
| 32 | self.company_currency = get_company_currency(self.doc.company) |
| 33 | self.validate_conversion_rate("currency", "conversion_rate") |
Anand Doshi | 8957bf9 | 2013-01-17 20:29:28 +0530 | [diff] [blame] | 34 | |
| 35 | if self.doc.price_list_name and self.doc.price_list_currency: |
| 36 | self.validate_conversion_rate("price_list_currency", "plc_conversion_rate") |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 37 | |
| 38 | # IMPORTANT: enable this only when client side code is similar to this one |
Anand Doshi | 3576b18 | 2013-02-08 19:30:06 +0530 | [diff] [blame] | 39 | # self.calculate_taxes_and_totals() |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 40 | |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 41 | # set total in words |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 42 | self.set_total_in_words() |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 43 | |
| 44 | def update_item_details(self): |
| 45 | for item in self.doclist.get({"parentfield": self.fname}): |
| 46 | ret = get_item_details({ |
| 47 | "doctype": self.doc.doctype, |
| 48 | "docname": self.doc.name, |
| 49 | "item_code": item.item_code, |
| 50 | "warehouse": item.warehouse, |
| 51 | "supplier": self.doc.supplier, |
| 52 | "transaction_date": self.doc.posting_date, |
Nabin Hait | c10c90a | 2013-02-06 11:44:43 +0530 | [diff] [blame] | 53 | "conversion_rate": self.doc.conversion_rate, |
| 54 | "price_list_name": self.doc.price_list_name, |
| 55 | "price_list_currency": self.doc.price_list_currency, |
| 56 | "plc_conversion_rate": self.doc.plc_conversion_rate |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 57 | }) |
| 58 | for r in ret: |
| 59 | if not item.fields.get(r): |
| 60 | item.fields[r] = ret[r] |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 61 | |
| 62 | def validate_conversion_rate(self, currency_field, conversion_rate_field): |
| 63 | """common validation for currency and price list currency""" |
| 64 | |
| 65 | currency = self.doc.fields.get(currency_field) |
| 66 | conversion_rate = flt(self.doc.fields.get(conversion_rate_field)) |
| 67 | conversion_rate_label = self.meta.get_label(conversion_rate_field) |
| 68 | |
| 69 | if conversion_rate == 0: |
| 70 | msgprint(conversion_rate_label + _(' cannot be 0'), raise_exception=True) |
| 71 | |
| 72 | # parenthesis for 'OR' are necessary as we want it to evaluate as |
| 73 | # mandatory valid condition and (1st optional valid condition |
| 74 | # or 2nd optional valid condition) |
| 75 | valid_conversion_rate = (conversion_rate and |
| 76 | ((currency == self.company_currency and conversion_rate == 1.00) |
| 77 | or (currency != self.company_currency and conversion_rate != 1.00))) |
| 78 | |
| 79 | if not valid_conversion_rate: |
| 80 | msgprint(_('Please enter valid ') + conversion_rate_label + (': ') |
Nabin Hait | 1022198 | 2013-01-18 16:47:39 +0530 | [diff] [blame] | 81 | + ("1 %s = [?] %s" % (currency, self.company_currency)), |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 82 | raise_exception=True) |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 83 | |
| 84 | def set_total_in_words(self): |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 85 | from webnotes.utils import money_in_words |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 86 | company_currency = get_company_currency(self.doc.company) |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 87 | if self.meta.get_field("in_words"): |
| 88 | self.doc.in_words = money_in_words(self.doc.grand_total, company_currency) |
| 89 | if self.meta.get_field("in_words_import"): |
| 90 | self.doc.in_words_import = money_in_words(self.doc.grand_total_import, |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 91 | self.doc.currency) |
| 92 | |
| 93 | def calculate_taxes_and_totals(self): |
Anand Doshi | 6209046 | 2013-02-07 19:47:34 +0530 | [diff] [blame] | 94 | self.doc.conversion_rate = flt(self.doc.conversion_rate) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 95 | self.item_doclist = self.doclist.get({"parentfield": self.fname}) |
| 96 | self.tax_doclist = self.doclist.get({"parentfield": "purchase_tax_details"}) |
Anand Doshi | 6209046 | 2013-02-07 19:47:34 +0530 | [diff] [blame] | 97 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 98 | self.calculate_item_values() |
| 99 | self.initialize_taxes() |
| 100 | self.calculate_net_total() |
| 101 | self.calculate_taxes() |
| 102 | self.calculate_totals() |
| 103 | self.calculate_outstanding_amount() |
| 104 | |
| 105 | self._cleanup() |
| 106 | |
| 107 | def calculate_item_values(self): |
| 108 | def _set_base(item, print_field, base_field): |
| 109 | """set values in base currency""" |
| 110 | item.fields[base_field] = flt((flt(item.fields[print_field], |
| 111 | self.precision.item[print_field]) * self.doc.conversion_rate), |
| 112 | self.precision.item[base_field]) |
| 113 | |
| 114 | for item in self.item_doclist: |
| 115 | round_floats_in_doc(item, self.precision.item) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 116 | |
| 117 | # hack! - cleaned up in _cleanup() |
| 118 | if self.doc.doctype != "Purchase Invoice": |
| 119 | item.rate = item.purchase_rate |
| 120 | self.precision.item.rate = self.precision.item.purchase_rate |
| 121 | |
| 122 | item.discount = item.discount_rate |
| 123 | self.precision.item.discount = self.precision.item.discount_rate |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 124 | |
| 125 | if item.discount == 100: |
| 126 | if not item.import_ref_rate: |
| 127 | item.import_ref_rate = item.import_rate |
| 128 | item.import_rate = 0 |
| 129 | else: |
| 130 | if item.import_ref_rate: |
| 131 | item.import_rate = flt(item.import_ref_rate * |
| 132 | (1.0 - (item.discount_rate / 100.0)), |
| 133 | self.precision.item.import_rate) |
| 134 | else: |
| 135 | # assume that print rate and discount are specified |
| 136 | item.import_ref_rate = flt(item.import_rate / |
| 137 | (1.0 - (item.discount_rate / 100.0)), |
| 138 | self.precision.item.import_ref_rate) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 139 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 140 | item.import_amount = flt(item.import_rate * item.qty, |
| 141 | self.precision.item.import_amount) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 142 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 143 | _set_base(item, "import_ref_rate", "purchase_ref_rate") |
| 144 | _set_base(item, "import_rate", "rate") |
| 145 | _set_base(item, "import_amount", "amount") |
| 146 | |
| 147 | def initialize_taxes(self): |
| 148 | for tax in self.tax_doclist: |
| 149 | # initialize totals to 0 |
| 150 | tax.tax_amount = tax.total = 0.0 |
| 151 | |
| 152 | # temporary fields |
| 153 | tax.tax_amount_for_current_item = tax.grand_total_for_current_item = 0.0 |
| 154 | |
| 155 | tax.item_wise_tax_detail = {} |
| 156 | |
| 157 | self.validate_on_previous_row(tax) |
| 158 | |
| 159 | round_floats_in_doc(tax, self.precision.tax) |
| 160 | |
| 161 | def calculate_net_total(self): |
| 162 | self.doc.net_total = 0 |
| 163 | self.doc.net_total_import = 0 |
| 164 | |
| 165 | for item in self.item_doclist: |
| 166 | self.doc.net_total += item.amount |
| 167 | self.doc.net_total_import += item.import_amount |
| 168 | |
| 169 | self.doc.net_total = flt(self.doc.net_total, self.precision.main.net_total) |
| 170 | self.doc.net_total_import = flt(self.doc.net_total_import, |
| 171 | self.precision.main.net_total_import) |
| 172 | |
| 173 | def calculate_taxes(self): |
| 174 | for item in self.item_doclist: |
| 175 | item_tax_map = self._load_item_tax_rate(item.item_tax_rate) |
| 176 | item.item_tax_amount = 0 |
| 177 | |
| 178 | for i, tax in enumerate(self.tax_doclist): |
| 179 | # tax_amount represents the amount of tax for the current step |
| 180 | current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map) |
| 181 | |
| 182 | self.set_item_tax_amount(item, tax, current_tax_amount) |
| 183 | |
| 184 | # case when net total is 0 but there is an actual type charge |
| 185 | # in this case add the actual amount to tax.tax_amount |
| 186 | # and tax.grand_total_for_current_item for the first such iteration |
| 187 | if not (current_tax_amount or self.doc.net_total or tax.tax_amount) and \ |
| 188 | tax.charge_type=="Actual": |
| 189 | zero_net_total_adjustment = flt(tax.rate, self.precision.tax.tax_amount) |
| 190 | current_tax_amount += zero_net_total_adjustment |
| 191 | |
| 192 | # store tax_amount for current item as it will be used for |
| 193 | # charge type = 'On Previous Row Amount' |
| 194 | tax.tax_amount_for_current_item = current_tax_amount |
| 195 | |
| 196 | # accumulate tax amount into tax.tax_amount |
| 197 | tax.tax_amount += tax.tax_amount_for_current_item |
| 198 | |
| 199 | if tax.category == "Valuation": |
| 200 | # if just for valuation, do not add the tax amount in total |
| 201 | # hence, setting it as 0 for further steps |
| 202 | current_tax_amount = 0 |
| 203 | else: |
| 204 | current_tax_amount *= tax.add_deduct_tax == "Deduct" and -1.0 or 1.0 |
| 205 | |
| 206 | # Calculate tax.total viz. grand total till that step |
| 207 | # note: grand_total_for_current_item contains the contribution of |
| 208 | # item's amount, previously applied tax and the current tax on that item |
| 209 | if i==0: |
| 210 | tax.grand_total_for_current_item = flt(item.amount + |
| 211 | current_tax_amount, self.precision.tax.total) |
| 212 | |
| 213 | else: |
| 214 | tax.grand_total_for_current_item = \ |
| 215 | flt(self.tax_doclist[i-1].grand_total_for_current_item + |
| 216 | current_tax_amount, self.precision.tax.total) |
| 217 | |
| 218 | # in tax.total, accumulate grand total of each item |
| 219 | tax.total += tax.grand_total_for_current_item |
| 220 | |
| 221 | # store tax_breakup for each item |
| 222 | # DOUBT: should valuation type amount also be stored? |
| 223 | tax.item_wise_tax_detail[item.item_code] = current_tax_amount |
| 224 | |
| 225 | def calculate_totals(self): |
| 226 | if self.tax_doclist: |
| 227 | self.doc.grand_total = flt(self.tax_doclist[-1].total, |
| 228 | self.precision.main.grand_total) |
| 229 | self.doc.grand_total_import = flt( |
| 230 | self.doc.grand_total / self.doc.conversion_rate, |
| 231 | self.precision.main.grand_total_import) |
| 232 | else: |
| 233 | self.doc.grand_total = flt(self.doc.net_total, |
| 234 | self.precision.main.grand_total) |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 235 | self.doc.grand_total_import = flt( |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 236 | self.doc.grand_total / self.doc.conversion_rate, |
| 237 | self.precision.main.grand_total_import) |
| 238 | |
| 239 | self.doc.total_tax = \ |
| 240 | flt(self.doc.grand_total - self.doc.net_total, |
| 241 | self.precision.main.total_tax) |
| 242 | |
| 243 | if self.meta.get_field("rounded_total"): |
| 244 | self.doc.rounded_total = round(self.doc.grand_total) |
| 245 | |
| 246 | if self.meta.get_field("rounded_total_import"): |
| 247 | self.doc.rounded_total_import = round(self.doc.grand_total_import) |
| 248 | |
| 249 | def calculate_outstanding_amount(self): |
| 250 | if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus == 0: |
| 251 | self.doc.total_advance = flt(self.doc.total_advance, |
| 252 | self.precision.main.total_advance) |
Anand Doshi | 9d794fc | 2013-02-27 13:34:22 +0530 | [diff] [blame] | 253 | self.doc.total_amount_to_pay = flt(self.doc.grand_total - flt(self.doc.write_off_amount, |
| 254 | self.precision.main.write_off_amount), self.precision.main.total_amount_to_pay) |
| 255 | self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance, |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 256 | self.precision.main.outstanding_amount) |
| 257 | |
| 258 | def _cleanup(self): |
| 259 | for tax in self.tax_doclist: |
| 260 | del tax.fields["grand_total_for_current_item"] |
| 261 | del tax.fields["tax_amount_for_current_item"] |
| 262 | tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail) |
| 263 | |
| 264 | # except in purchase invoice, rate field is purchase_rate |
| 265 | if self.doc.doctype != "Purchase Invoice": |
| 266 | for item in self.item_doclist: |
| 267 | item.purchase_rate = item.rate |
| 268 | del item.fields["rate"] |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 269 | |
| 270 | item.discount_rate = item.discount |
| 271 | del item.fields["discount"] |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 272 | |
| 273 | def validate_on_previous_row(self, tax): |
| 274 | """ |
| 275 | validate if a valid row id is mentioned in case of |
| 276 | On Previous Row Amount and On Previous Row Total |
| 277 | """ |
| 278 | if tax.charge_type in ["On Previous Row Amount", "On Previous Row Total"] and \ |
| 279 | (not tax.row_id or cint(tax.row_id) >= tax.idx): |
| 280 | msgprint((_("Row") + " # %(idx)s [%(taxes_doctype)s]: " + \ |
| 281 | _("Please specify a valid") + " %(row_id_label)s") % { |
| 282 | "idx": tax.idx, |
| 283 | "taxes_doctype": tax.parenttype, |
| 284 | "row_id_label": self.meta.get_label("row_id", |
| 285 | parentfield="purchase_tax_details") |
| 286 | }, raise_exception=True) |
| 287 | |
| 288 | def _load_item_tax_rate(self, item_tax_rate): |
| 289 | if not item_tax_rate: |
| 290 | return {} |
| 291 | return json.loads(item_tax_rate) |
| 292 | |
| 293 | def get_current_tax_amount(self, item, tax, item_tax_map): |
| 294 | tax_rate = self._get_tax_rate(tax, item_tax_map) |
| 295 | |
| 296 | if tax.charge_type == "Actual": |
| 297 | # distribute the tax amount proportionally to each item row |
| 298 | actual = flt(tax.rate, self.precision.tax.tax_amount) |
| 299 | current_tax_amount = (self.doc.net_total |
| 300 | and ((item.amount / self.doc.net_total) * actual) |
| 301 | or 0) |
| 302 | elif tax.charge_type == "On Net Total": |
| 303 | current_tax_amount = (tax_rate / 100.0) * item.amount |
| 304 | elif tax.charge_type == "On Previous Row Amount": |
| 305 | current_tax_amount = (tax_rate / 100.0) * \ |
| 306 | self.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item |
| 307 | elif tax.charge_type == "On Previous Row Total": |
| 308 | current_tax_amount = (tax_rate / 100.0) * \ |
| 309 | self.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item |
| 310 | |
| 311 | return flt(current_tax_amount, self.precision.tax.tax_amount) |
| 312 | |
| 313 | def _get_tax_rate(self, tax, item_tax_map): |
| 314 | if item_tax_map.has_key(tax.account_head): |
| 315 | return flt(item_tax_map.get(tax.account_head), self.precision.tax.rate) |
| 316 | else: |
| 317 | return tax.rate |
| 318 | |
| 319 | def set_item_tax_amount(self, item, tax, current_tax_amount): |
| 320 | """ |
| 321 | item_tax_amount is the total tax amount applied on that item |
| 322 | stored for valuation |
| 323 | |
| 324 | TODO: rename item_tax_amount to valuation_tax_amount |
| 325 | """ |
| 326 | if tax.category in ["Valuation", "Valuation and Total"] and \ |
| 327 | item.item_code in self.stock_items: |
| 328 | item.item_tax_amount += flt(current_tax_amount, |
| 329 | self.precision.item.item_tax_amount) |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 330 | |
| 331 | # update valuation rate |
| 332 | def update_valuation_rate(self, parentfield): |
| 333 | for d in self.doclist.get({"parentfield": parentfield}): |
Nabin Hait | a9afc25 | 2013-03-01 10:41:41 +0530 | [diff] [blame] | 334 | d.conversion_factor = d.conversion_factor or webnotes.conn.get_value( |
| 335 | "UOM Conversion Detail", {"parent": d.item_code, "uom": d.uom}, |
| 336 | "conversion_factor") or 1 |
Anand Doshi | 1bfec3a | 2013-02-28 19:17:46 +0530 | [diff] [blame] | 337 | if d.item_code and d.qty: |
| 338 | # if no item code, which is sometimes the case in purchase invoice, |
| 339 | # then it is not possible to track valuation against it |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 340 | d.valuation_rate = (flt(d.purchase_rate or d.rate) |
| 341 | + (flt(d.item_tax_amount) + flt(d.rm_supp_cost)) / flt(d.qty) |
| 342 | ) / flt(d.conversion_factor) |
| 343 | else: |
| 344 | d.valuation_rate = 0.0 |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 345 | |
| 346 | @property |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 347 | def precision(self): |
| 348 | if not hasattr(self, "_precision"): |
| 349 | self._precision = webnotes._dict() |
| 350 | self._precision.main = self.meta.get_precision_map() |
| 351 | self._precision.item = self.meta.get_precision_map(parentfield = self.fname) |
| 352 | if self.meta.get_field("purchase_tax_details"): |
| 353 | self._precision.tax = self.meta.get_precision_map(parentfield = \ |
| 354 | "purchase_tax_details") |
| 355 | return self._precision |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 356 | |
| 357 | @property |
| 358 | def sub_contracted_items(self): |
| 359 | if not hasattr(self, "_sub_contracted_items"): |
| 360 | item_codes = list(set(item.item_code for item in |
| 361 | self.doclist.get({"parentfield": self.fname}))) |
| 362 | self._sub_contracted_items = [r[0] for r in webnotes.conn.sql("""select name |
| 363 | from `tabItem` where name in (%s) and is_sub_contracted_item='Yes'""" % \ |
| 364 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
| 365 | |
| 366 | return self._sub_contracted_items |
| 367 | |
| 368 | @property |
| 369 | def purchase_items(self): |
| 370 | if not hasattr(self, "_purchase_items"): |
| 371 | item_codes = list(set(item.item_code for item in |
| 372 | self.doclist.get({"parentfield": self.fname}))) |
| 373 | self._purchase_items = [r[0] for r in webnotes.conn.sql("""select name |
| 374 | from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \ |
| 375 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
| 376 | |
Anand Doshi | 55d9a62 | 2013-02-27 18:14:28 +0530 | [diff] [blame] | 377 | return self._purchase_items |