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 | 89a94d8 | 2013-03-19 12:01:46 +0530 | [diff] [blame] | 27 | from controllers.stock_controller import StockController |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 28 | |
Rushabh Mehta | ecb36f2 | 2013-05-02 11:34:37 +0530 | [diff] [blame] | 29 | class WrongWarehouseCompany(Exception): pass |
| 30 | |
Nabin Hait | 89a94d8 | 2013-03-19 12:01:46 +0530 | [diff] [blame] | 31 | class BuyingController(StockController): |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 32 | def validate(self): |
| 33 | super(BuyingController, self).validate() |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 34 | self.validate_stock_or_nonstock_items() |
Rushabh Mehta | ecb36f2 | 2013-05-02 11:34:37 +0530 | [diff] [blame] | 35 | self.validate_warehouse_belongs_to_company() |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 36 | if self.meta.get_field("currency"): |
| 37 | self.company_currency = get_company_currency(self.doc.company) |
| 38 | self.validate_conversion_rate("currency", "conversion_rate") |
Anand Doshi | 8957bf9 | 2013-01-17 20:29:28 +0530 | [diff] [blame] | 39 | |
| 40 | if self.doc.price_list_name and self.doc.price_list_currency: |
| 41 | self.validate_conversion_rate("price_list_currency", "plc_conversion_rate") |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 42 | |
| 43 | # 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] | 44 | # self.calculate_taxes_and_totals() |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 45 | |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 46 | # set total in words |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 47 | self.set_total_in_words() |
Rushabh Mehta | ecb36f2 | 2013-05-02 11:34:37 +0530 | [diff] [blame] | 48 | |
| 49 | def validate_warehouse_belongs_to_company(self): |
Rushabh Mehta | c711598 | 2013-05-02 14:22:40 +0530 | [diff] [blame] | 50 | for warehouse, company in webnotes.conn.get_values("Warehouse", |
| 51 | self.doclist.get_distinct_values("warehouse"), "company").items(): |
| 52 | if company and company != self.doc.company: |
Anand Doshi | eb41ffd | 2013-05-02 18:10:19 +0530 | [diff] [blame] | 53 | webnotes.msgprint(_("Company mismatch for Warehouse") + (": %s" % (warehouse,)), |
Rushabh Mehta | ecb36f2 | 2013-05-02 11:34:37 +0530 | [diff] [blame] | 54 | raise_exception=WrongWarehouseCompany) |
Rushabh Mehta | c711598 | 2013-05-02 14:22:40 +0530 | [diff] [blame] | 55 | |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 56 | def validate_stock_or_nonstock_items(self): |
| 57 | items = [d.item_code for d in self.doclist.get({"parentfield": self.fname})] |
Nabin Hait | d58d1b5 | 2013-04-26 17:25:44 +0530 | [diff] [blame] | 58 | if self.stock_items: |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 59 | nonstock_items = list(set(items) - set(self.stock_items)) |
Nabin Hait | d58d1b5 | 2013-04-26 17:25:44 +0530 | [diff] [blame] | 60 | if nonstock_items: |
| 61 | webnotes.msgprint(_("Stock and non-stock items can not be entered in the same ") + |
| 62 | self.doc.doctype + _(""". You should make separate documents for them. |
| 63 | Stock Items: """) + ", ".join(self.stock_items) + _(""" |
| 64 | Non-stock Items: """) + ", ".join(nonstock_items), raise_exception=1) |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 65 | |
| 66 | elif items and not self.stock_items: |
| 67 | tax_for_valuation = [d.account_head for d in |
| 68 | self.doclist.get({"parentfield": "purchase_tax_details"}) |
| 69 | if d.category in ["Valuation", "Valuation and Total"]] |
| 70 | if tax_for_valuation: |
| 71 | webnotes.msgprint(_("""Tax Category can not be 'Valuation' or 'Valuation and Total' |
| 72 | as all items are non-stock items"""), raise_exception=1) |
| 73 | |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 74 | def update_item_details(self): |
| 75 | for item in self.doclist.get({"parentfield": self.fname}): |
| 76 | ret = get_item_details({ |
| 77 | "doctype": self.doc.doctype, |
| 78 | "docname": self.doc.name, |
| 79 | "item_code": item.item_code, |
| 80 | "warehouse": item.warehouse, |
| 81 | "supplier": self.doc.supplier, |
| 82 | "transaction_date": self.doc.posting_date, |
Nabin Hait | c10c90a | 2013-02-06 11:44:43 +0530 | [diff] [blame] | 83 | "conversion_rate": self.doc.conversion_rate, |
| 84 | "price_list_name": self.doc.price_list_name, |
| 85 | "price_list_currency": self.doc.price_list_currency, |
| 86 | "plc_conversion_rate": self.doc.plc_conversion_rate |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 87 | }) |
| 88 | for r in ret: |
| 89 | if not item.fields.get(r): |
| 90 | item.fields[r] = ret[r] |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 91 | |
| 92 | def validate_conversion_rate(self, currency_field, conversion_rate_field): |
| 93 | """common validation for currency and price list currency""" |
| 94 | |
| 95 | currency = self.doc.fields.get(currency_field) |
| 96 | conversion_rate = flt(self.doc.fields.get(conversion_rate_field)) |
| 97 | conversion_rate_label = self.meta.get_label(conversion_rate_field) |
| 98 | |
| 99 | if conversion_rate == 0: |
| 100 | msgprint(conversion_rate_label + _(' cannot be 0'), raise_exception=True) |
| 101 | |
| 102 | # parenthesis for 'OR' are necessary as we want it to evaluate as |
| 103 | # mandatory valid condition and (1st optional valid condition |
| 104 | # or 2nd optional valid condition) |
| 105 | valid_conversion_rate = (conversion_rate and |
| 106 | ((currency == self.company_currency and conversion_rate == 1.00) |
| 107 | or (currency != self.company_currency and conversion_rate != 1.00))) |
| 108 | |
| 109 | if not valid_conversion_rate: |
| 110 | msgprint(_('Please enter valid ') + conversion_rate_label + (': ') |
Nabin Hait | 1022198 | 2013-01-18 16:47:39 +0530 | [diff] [blame] | 111 | + ("1 %s = [?] %s" % (currency, self.company_currency)), |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 112 | raise_exception=True) |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 113 | |
| 114 | def set_total_in_words(self): |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 115 | from webnotes.utils import money_in_words |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 116 | company_currency = get_company_currency(self.doc.company) |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 117 | if self.meta.get_field("in_words"): |
| 118 | self.doc.in_words = money_in_words(self.doc.grand_total, company_currency) |
| 119 | if self.meta.get_field("in_words_import"): |
| 120 | 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] | 121 | self.doc.currency) |
| 122 | |
| 123 | def calculate_taxes_and_totals(self): |
Anand Doshi | 6209046 | 2013-02-07 19:47:34 +0530 | [diff] [blame] | 124 | self.doc.conversion_rate = flt(self.doc.conversion_rate) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 125 | self.item_doclist = self.doclist.get({"parentfield": self.fname}) |
| 126 | self.tax_doclist = self.doclist.get({"parentfield": "purchase_tax_details"}) |
Anand Doshi | 6209046 | 2013-02-07 19:47:34 +0530 | [diff] [blame] | 127 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 128 | self.calculate_item_values() |
| 129 | self.initialize_taxes() |
| 130 | self.calculate_net_total() |
| 131 | self.calculate_taxes() |
| 132 | self.calculate_totals() |
| 133 | self.calculate_outstanding_amount() |
| 134 | |
| 135 | self._cleanup() |
| 136 | |
| 137 | def calculate_item_values(self): |
| 138 | def _set_base(item, print_field, base_field): |
| 139 | """set values in base currency""" |
| 140 | item.fields[base_field] = flt((flt(item.fields[print_field], |
| 141 | self.precision.item[print_field]) * self.doc.conversion_rate), |
| 142 | self.precision.item[base_field]) |
| 143 | |
| 144 | for item in self.item_doclist: |
| 145 | round_floats_in_doc(item, self.precision.item) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 146 | |
| 147 | # hack! - cleaned up in _cleanup() |
| 148 | if self.doc.doctype != "Purchase Invoice": |
| 149 | item.rate = item.purchase_rate |
| 150 | self.precision.item.rate = self.precision.item.purchase_rate |
| 151 | |
| 152 | item.discount = item.discount_rate |
| 153 | self.precision.item.discount = self.precision.item.discount_rate |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 154 | |
| 155 | if item.discount == 100: |
| 156 | if not item.import_ref_rate: |
| 157 | item.import_ref_rate = item.import_rate |
| 158 | item.import_rate = 0 |
| 159 | else: |
| 160 | if item.import_ref_rate: |
| 161 | item.import_rate = flt(item.import_ref_rate * |
| 162 | (1.0 - (item.discount_rate / 100.0)), |
| 163 | self.precision.item.import_rate) |
| 164 | else: |
| 165 | # assume that print rate and discount are specified |
| 166 | item.import_ref_rate = flt(item.import_rate / |
| 167 | (1.0 - (item.discount_rate / 100.0)), |
| 168 | self.precision.item.import_ref_rate) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 169 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 170 | item.import_amount = flt(item.import_rate * item.qty, |
| 171 | self.precision.item.import_amount) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 172 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 173 | _set_base(item, "import_ref_rate", "purchase_ref_rate") |
| 174 | _set_base(item, "import_rate", "rate") |
| 175 | _set_base(item, "import_amount", "amount") |
| 176 | |
| 177 | def initialize_taxes(self): |
| 178 | for tax in self.tax_doclist: |
| 179 | # initialize totals to 0 |
| 180 | tax.tax_amount = tax.total = 0.0 |
| 181 | |
| 182 | # temporary fields |
| 183 | tax.tax_amount_for_current_item = tax.grand_total_for_current_item = 0.0 |
| 184 | |
| 185 | tax.item_wise_tax_detail = {} |
| 186 | |
| 187 | self.validate_on_previous_row(tax) |
| 188 | |
| 189 | round_floats_in_doc(tax, self.precision.tax) |
| 190 | |
| 191 | def calculate_net_total(self): |
| 192 | self.doc.net_total = 0 |
| 193 | self.doc.net_total_import = 0 |
| 194 | |
| 195 | for item in self.item_doclist: |
| 196 | self.doc.net_total += item.amount |
| 197 | self.doc.net_total_import += item.import_amount |
| 198 | |
| 199 | self.doc.net_total = flt(self.doc.net_total, self.precision.main.net_total) |
| 200 | self.doc.net_total_import = flt(self.doc.net_total_import, |
| 201 | self.precision.main.net_total_import) |
| 202 | |
| 203 | def calculate_taxes(self): |
| 204 | for item in self.item_doclist: |
| 205 | item_tax_map = self._load_item_tax_rate(item.item_tax_rate) |
| 206 | item.item_tax_amount = 0 |
| 207 | |
| 208 | for i, tax in enumerate(self.tax_doclist): |
| 209 | # tax_amount represents the amount of tax for the current step |
| 210 | current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map) |
| 211 | |
| 212 | self.set_item_tax_amount(item, tax, current_tax_amount) |
| 213 | |
| 214 | # case when net total is 0 but there is an actual type charge |
| 215 | # in this case add the actual amount to tax.tax_amount |
| 216 | # and tax.grand_total_for_current_item for the first such iteration |
| 217 | if not (current_tax_amount or self.doc.net_total or tax.tax_amount) and \ |
| 218 | tax.charge_type=="Actual": |
| 219 | zero_net_total_adjustment = flt(tax.rate, self.precision.tax.tax_amount) |
| 220 | current_tax_amount += zero_net_total_adjustment |
| 221 | |
| 222 | # store tax_amount for current item as it will be used for |
| 223 | # charge type = 'On Previous Row Amount' |
| 224 | tax.tax_amount_for_current_item = current_tax_amount |
| 225 | |
| 226 | # accumulate tax amount into tax.tax_amount |
| 227 | tax.tax_amount += tax.tax_amount_for_current_item |
| 228 | |
| 229 | if tax.category == "Valuation": |
| 230 | # if just for valuation, do not add the tax amount in total |
| 231 | # hence, setting it as 0 for further steps |
| 232 | current_tax_amount = 0 |
| 233 | else: |
| 234 | current_tax_amount *= tax.add_deduct_tax == "Deduct" and -1.0 or 1.0 |
| 235 | |
| 236 | # Calculate tax.total viz. grand total till that step |
| 237 | # note: grand_total_for_current_item contains the contribution of |
| 238 | # item's amount, previously applied tax and the current tax on that item |
| 239 | if i==0: |
| 240 | tax.grand_total_for_current_item = flt(item.amount + |
| 241 | current_tax_amount, self.precision.tax.total) |
| 242 | |
| 243 | else: |
| 244 | tax.grand_total_for_current_item = \ |
| 245 | flt(self.tax_doclist[i-1].grand_total_for_current_item + |
| 246 | current_tax_amount, self.precision.tax.total) |
| 247 | |
| 248 | # in tax.total, accumulate grand total of each item |
| 249 | tax.total += tax.grand_total_for_current_item |
| 250 | |
| 251 | # store tax_breakup for each item |
| 252 | # DOUBT: should valuation type amount also be stored? |
| 253 | tax.item_wise_tax_detail[item.item_code] = current_tax_amount |
| 254 | |
| 255 | def calculate_totals(self): |
| 256 | if self.tax_doclist: |
| 257 | self.doc.grand_total = flt(self.tax_doclist[-1].total, |
| 258 | self.precision.main.grand_total) |
| 259 | self.doc.grand_total_import = flt( |
| 260 | self.doc.grand_total / self.doc.conversion_rate, |
| 261 | self.precision.main.grand_total_import) |
| 262 | else: |
| 263 | self.doc.grand_total = flt(self.doc.net_total, |
| 264 | self.precision.main.grand_total) |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 265 | self.doc.grand_total_import = flt( |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 266 | self.doc.grand_total / self.doc.conversion_rate, |
| 267 | self.precision.main.grand_total_import) |
| 268 | |
| 269 | self.doc.total_tax = \ |
| 270 | flt(self.doc.grand_total - self.doc.net_total, |
| 271 | self.precision.main.total_tax) |
| 272 | |
| 273 | if self.meta.get_field("rounded_total"): |
| 274 | self.doc.rounded_total = round(self.doc.grand_total) |
| 275 | |
| 276 | if self.meta.get_field("rounded_total_import"): |
| 277 | self.doc.rounded_total_import = round(self.doc.grand_total_import) |
| 278 | |
| 279 | def calculate_outstanding_amount(self): |
| 280 | if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus == 0: |
| 281 | self.doc.total_advance = flt(self.doc.total_advance, |
| 282 | self.precision.main.total_advance) |
Anand Doshi | 9d794fc | 2013-02-27 13:34:22 +0530 | [diff] [blame] | 283 | self.doc.total_amount_to_pay = flt(self.doc.grand_total - flt(self.doc.write_off_amount, |
| 284 | self.precision.main.write_off_amount), self.precision.main.total_amount_to_pay) |
| 285 | 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] | 286 | self.precision.main.outstanding_amount) |
| 287 | |
| 288 | def _cleanup(self): |
| 289 | for tax in self.tax_doclist: |
| 290 | del tax.fields["grand_total_for_current_item"] |
| 291 | del tax.fields["tax_amount_for_current_item"] |
| 292 | tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail) |
| 293 | |
| 294 | # except in purchase invoice, rate field is purchase_rate |
| 295 | if self.doc.doctype != "Purchase Invoice": |
| 296 | for item in self.item_doclist: |
| 297 | item.purchase_rate = item.rate |
| 298 | del item.fields["rate"] |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 299 | |
| 300 | item.discount_rate = item.discount |
| 301 | del item.fields["discount"] |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 302 | |
| 303 | def validate_on_previous_row(self, tax): |
| 304 | """ |
| 305 | validate if a valid row id is mentioned in case of |
| 306 | On Previous Row Amount and On Previous Row Total |
| 307 | """ |
| 308 | if tax.charge_type in ["On Previous Row Amount", "On Previous Row Total"] and \ |
| 309 | (not tax.row_id or cint(tax.row_id) >= tax.idx): |
| 310 | msgprint((_("Row") + " # %(idx)s [%(taxes_doctype)s]: " + \ |
| 311 | _("Please specify a valid") + " %(row_id_label)s") % { |
| 312 | "idx": tax.idx, |
| 313 | "taxes_doctype": tax.parenttype, |
| 314 | "row_id_label": self.meta.get_label("row_id", |
| 315 | parentfield="purchase_tax_details") |
| 316 | }, raise_exception=True) |
| 317 | |
| 318 | def _load_item_tax_rate(self, item_tax_rate): |
| 319 | if not item_tax_rate: |
| 320 | return {} |
| 321 | return json.loads(item_tax_rate) |
| 322 | |
| 323 | def get_current_tax_amount(self, item, tax, item_tax_map): |
| 324 | tax_rate = self._get_tax_rate(tax, item_tax_map) |
| 325 | |
| 326 | if tax.charge_type == "Actual": |
| 327 | # distribute the tax amount proportionally to each item row |
| 328 | actual = flt(tax.rate, self.precision.tax.tax_amount) |
| 329 | current_tax_amount = (self.doc.net_total |
| 330 | and ((item.amount / self.doc.net_total) * actual) |
| 331 | or 0) |
| 332 | elif tax.charge_type == "On Net Total": |
| 333 | current_tax_amount = (tax_rate / 100.0) * item.amount |
| 334 | elif tax.charge_type == "On Previous Row Amount": |
| 335 | current_tax_amount = (tax_rate / 100.0) * \ |
| 336 | self.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item |
| 337 | elif tax.charge_type == "On Previous Row Total": |
| 338 | current_tax_amount = (tax_rate / 100.0) * \ |
| 339 | self.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item |
| 340 | |
| 341 | return flt(current_tax_amount, self.precision.tax.tax_amount) |
| 342 | |
| 343 | def _get_tax_rate(self, tax, item_tax_map): |
| 344 | if item_tax_map.has_key(tax.account_head): |
| 345 | return flt(item_tax_map.get(tax.account_head), self.precision.tax.rate) |
| 346 | else: |
| 347 | return tax.rate |
| 348 | |
| 349 | def set_item_tax_amount(self, item, tax, current_tax_amount): |
| 350 | """ |
| 351 | item_tax_amount is the total tax amount applied on that item |
| 352 | stored for valuation |
| 353 | |
| 354 | TODO: rename item_tax_amount to valuation_tax_amount |
| 355 | """ |
| 356 | if tax.category in ["Valuation", "Valuation and Total"] and \ |
| 357 | item.item_code in self.stock_items: |
| 358 | item.item_tax_amount += flt(current_tax_amount, |
| 359 | self.precision.item.item_tax_amount) |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 360 | |
| 361 | # update valuation rate |
| 362 | def update_valuation_rate(self, parentfield): |
| 363 | for d in self.doclist.get({"parentfield": parentfield}): |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 364 | d.conversion_factor = d.conversion_factor or flt(webnotes.conn.get_value( |
Nabin Hait | a9afc25 | 2013-03-01 10:41:41 +0530 | [diff] [blame] | 365 | "UOM Conversion Detail", {"parent": d.item_code, "uom": d.uom}, |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 366 | "conversion_factor")) or 1 |
Anand Doshi | 1bfec3a | 2013-02-28 19:17:46 +0530 | [diff] [blame] | 367 | if d.item_code and d.qty: |
| 368 | # if no item code, which is sometimes the case in purchase invoice, |
| 369 | # then it is not possible to track valuation against it |
Nabin Hait | fdefe07 | 2013-05-20 13:59:24 +0530 | [diff] [blame] | 370 | d.valuation_rate = flt(((flt(d.purchase_rate, self.precision.item.purchase_rate) or |
| 371 | flt(d.rate, self.precision.item.rate)) + |
Nabin Hait | e1b2ae5 | 2013-05-09 16:56:19 +0530 | [diff] [blame] | 372 | (flt(d.item_tax_amount, self.precision.item.item_tax_amount) + |
| 373 | flt(d.rm_supp_cost, self.precision.item.rm_supp_cost)) / |
| 374 | flt(d.qty, self.precision.item.qty)) / |
| 375 | flt(d.conversion_factor, self.precision.item.conversion_factor), |
Nabin Hait | fdefe07 | 2013-05-20 13:59:24 +0530 | [diff] [blame] | 376 | self.precision.item.valuation_rate) |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 377 | else: |
| 378 | d.valuation_rate = 0.0 |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 379 | |
| 380 | def validate_for_subcontracting(self): |
| 381 | if not self.doc.is_subcontracted and self.sub_contracted_items: |
| 382 | webnotes.msgprint(_("""Please enter whether %s is made for subcontracting or purchasing, |
| 383 | in 'Is Subcontracted' field""" % self.doc.doctype), raise_exception=1) |
| 384 | |
| 385 | if self.doc.doctype == "Purchase Receipt" and self.doc.is_subcontracted=="Yes" \ |
| 386 | and not self.doc.supplier_warehouse: |
| 387 | webnotes.msgprint(_("Supplier Warehouse mandatory subcontracted purchase receipt"), |
| 388 | raise_exception=1) |
| 389 | |
| 390 | def update_raw_materials_supplied(self, raw_material_table): |
| 391 | self.doclist = self.doc.clear_table(self.doclist, raw_material_table) |
| 392 | if self.doc.is_subcontracted=="Yes": |
| 393 | for item in self.doclist.get({"parentfield": self.fname}): |
| 394 | if item.item_code in self.sub_contracted_items: |
| 395 | self.add_bom_items(item, raw_material_table) |
| 396 | |
| 397 | def add_bom_items(self, d, raw_material_table): |
| 398 | bom_items = self.get_items_from_default_bom(d.item_code) |
| 399 | raw_materials_cost = 0 |
| 400 | for item in bom_items: |
| 401 | required_qty = flt(item.qty_consumed_per_unit) * flt(d.qty) * flt(d.conversion_factor) |
| 402 | rm_doclist = { |
| 403 | "parentfield": raw_material_table, |
| 404 | "doctype": self.doc.doctype + " Item Supplied", |
| 405 | "reference_name": d.name, |
| 406 | "bom_detail_no": item.name, |
| 407 | "main_item_code": d.item_code, |
| 408 | "rm_item_code": item.item_code, |
| 409 | "stock_uom": item.stock_uom, |
| 410 | "required_qty": required_qty, |
| 411 | "conversion_factor": d.conversion_factor, |
| 412 | "rate": item.rate, |
| 413 | "amount": required_qty * flt(item.rate) |
| 414 | } |
| 415 | if self.doc.doctype == "Purchase Receipt": |
| 416 | rm_doclist.update({ |
| 417 | "consumed_qty": required_qty, |
| 418 | "description": item.description, |
| 419 | }) |
| 420 | |
| 421 | self.doclist.append(rm_doclist) |
| 422 | |
| 423 | raw_materials_cost += required_qty * flt(item.rate) |
| 424 | |
| 425 | if self.doc.doctype == "Purchase Receipt": |
| 426 | d.rm_supp_cost = raw_materials_cost |
| 427 | |
| 428 | def get_items_from_default_bom(self, item_code): |
| 429 | # print webnotes.conn.sql("""select name from `tabBOM` where item = '_Test FG Item'""") |
| 430 | bom_items = webnotes.conn.sql("""select t2.item_code, t2.qty_consumed_per_unit, |
| 431 | t2.rate, t2.stock_uom, t2.name, t2.description |
| 432 | from `tabBOM` t1, `tabBOM Item` t2 |
| 433 | where t2.parent = t1.name and t1.item = %s and t1.is_default = 1 |
| 434 | and t1.docstatus = 1 and t1.is_active = 1""", item_code, as_dict=1) |
| 435 | if not bom_items: |
| 436 | msgprint(_("No default BOM exists for item: ") + item_code, raise_exception=1) |
| 437 | |
| 438 | return bom_items |
| 439 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 440 | |
| 441 | @property |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 442 | def precision(self): |
| 443 | if not hasattr(self, "_precision"): |
| 444 | self._precision = webnotes._dict() |
| 445 | self._precision.main = self.meta.get_precision_map() |
| 446 | self._precision.item = self.meta.get_precision_map(parentfield = self.fname) |
| 447 | if self.meta.get_field("purchase_tax_details"): |
| 448 | self._precision.tax = self.meta.get_precision_map(parentfield = \ |
| 449 | "purchase_tax_details") |
| 450 | return self._precision |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 451 | |
| 452 | @property |
| 453 | def sub_contracted_items(self): |
| 454 | if not hasattr(self, "_sub_contracted_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 455 | self._sub_contracted_items = [] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 456 | item_codes = list(set(item.item_code for item in |
| 457 | self.doclist.get({"parentfield": self.fname}))) |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 458 | if item_codes: |
| 459 | self._sub_contracted_items = [r[0] for r in webnotes.conn.sql("""select name |
| 460 | from `tabItem` where name in (%s) and is_sub_contracted_item='Yes'""" % \ |
| 461 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 462 | |
| 463 | return self._sub_contracted_items |
| 464 | |
| 465 | @property |
| 466 | def purchase_items(self): |
| 467 | if not hasattr(self, "_purchase_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 468 | self._purchase_items = [] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 469 | item_codes = list(set(item.item_code for item in |
| 470 | self.doclist.get({"parentfield": self.fname}))) |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 471 | if item_codes: |
| 472 | self._purchase_items = [r[0] for r in webnotes.conn.sql("""select name |
| 473 | from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \ |
| 474 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 475 | |
Anand Doshi | 55d9a62 | 2013-02-27 18:14:28 +0530 | [diff] [blame] | 476 | return self._purchase_items |