Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +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 | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 19 | from webnotes.utils import cint, flt, comma_or |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 20 | from setup.utils import get_company_currency |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 21 | from webnotes import msgprint, _ |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 22 | import json |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 23 | |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 24 | from controllers.stock_controller import StockController |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 25 | |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 26 | class SellingController(StockController): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 27 | def onload_post_render(self): |
| 28 | self.set_price_list_currency() |
| 29 | |
| 30 | # contact, address, item details and pos details (if applicable) |
| 31 | self.set_missing_values() |
| 32 | |
| 33 | if self.meta.get_field("other_charges"): |
| 34 | self.set_taxes() |
| 35 | |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 36 | def validate(self): |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 37 | super(SellingController, self).validate() |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 38 | # self.calculate_taxes_and_totals() |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 39 | self.set_total_in_words() |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 40 | self.set_missing_values(for_validate=True) |
| 41 | |
| 42 | def set_price_list_currency(self): |
| 43 | if self.doc.price_list_name and not self.doc.price_list_currency: |
| 44 | # TODO - change this, since price list now has only one currency allowed |
| 45 | from setup.utils import get_price_list_currency |
| 46 | self.doc.fields.update(get_price_list_currency( |
| 47 | {"price_list_name": self.doc.price_list_name, "use_for": "selling"})) |
| 48 | |
| 49 | def set_missing_values(self, for_validate=False): |
| 50 | # set contact and address details for customer, if they are not mentioned |
| 51 | if self.doc.customer and not (self.doc.contact_person and self.doc.customer_address): |
| 52 | for fieldname, val in self.get_default_address_and_contact("customer").items(): |
| 53 | if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname): |
| 54 | self.doc.fields[fieldname] = val |
| 55 | |
| 56 | # set missing item values |
| 57 | from selling.utils import get_item_details |
| 58 | for item in self.doclist.get({"parentfield": "entries"}): |
| 59 | if item.fields.get("item_code"): |
Anand Doshi | 65d8755 | 2013-05-21 19:53:33 +0530 | [diff] [blame] | 60 | args = item.fields.copy().update(self.doc.fields) |
| 61 | ret = get_item_details(args) |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 62 | for fieldname, value in ret.items(): |
| 63 | if self.meta.get_field(fieldname, parentfield="entries") and \ |
| 64 | not item.fields.get(fieldname): |
| 65 | item.fields[fieldname] = value |
| 66 | |
| 67 | def set_taxes(self): |
| 68 | if not self.doclist.get({"parentfield": "other_charges"}): |
| 69 | if not self.doc.charge: |
| 70 | # get the default tax master |
| 71 | self.doc.charge = webnotes.conn.get_value("Sales Taxes and Charges Master", |
| 72 | {"is_default": 1}) |
| 73 | |
| 74 | if self.doc.charge: |
| 75 | from webnotes.model import default_fields |
| 76 | tax_master = webnotes.bean("Sales Taxes and Charges Master", self.doc.charge) |
| 77 | for i, tax in enumerate(tax_master.doclist.get({"parentfield": "other_charges"})): |
| 78 | for fieldname in default_fields: |
| 79 | tax.fields[fieldname] = None |
| 80 | |
| 81 | tax.fields.update({ |
| 82 | "doctype": "Sales Taxes and Charges", |
| 83 | "parentfield": "other_charges", |
| 84 | "idx": i+1 |
| 85 | }) |
| 86 | |
| 87 | self.doclist.append(tax) |
| 88 | |
| 89 | def get_other_charges(self): |
| 90 | self.doclist = self.doc.clear_table(self.doclist, "other_charges") |
| 91 | self.set_taxes() |
| 92 | |
| 93 | def set_customer_defaults(self): |
| 94 | self.get_default_customer_address() |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 95 | |
| 96 | def set_total_in_words(self): |
| 97 | from webnotes.utils import money_in_words |
| 98 | company_currency = get_company_currency(self.doc.company) |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 99 | |
| 100 | disable_rounded_total = cint(webnotes.conn.get_value("Global Defaults", None, |
| 101 | "disable_rounded_total")) |
| 102 | |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 103 | if self.meta.get_field("in_words"): |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 104 | self.doc.in_words = money_in_words(disable_rounded_total and |
| 105 | self.doc.grand_total or self.doc.rounded_total, company_currency) |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 106 | if self.meta.get_field("in_words_export"): |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 107 | self.doc.in_words_export = money_in_words(disable_rounded_total and |
Nabin Hait | 8c7234f | 2013-03-11 16:32:33 +0530 | [diff] [blame] | 108 | self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency) |
Nabin Hait | a0e7c15 | 2013-03-21 18:37:06 +0530 | [diff] [blame] | 109 | |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 110 | def set_buying_amount(self, stock_ledger_entries = None): |
Anand Doshi | 8c5844e | 2013-03-21 20:24:10 +0530 | [diff] [blame] | 111 | from stock.utils import get_buying_amount |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 112 | if not stock_ledger_entries: |
| 113 | stock_ledger_entries = self.get_stock_ledger_entries() |
Anand Doshi | 8c5844e | 2013-03-21 20:24:10 +0530 | [diff] [blame] | 114 | |
| 115 | item_sales_bom = {} |
| 116 | for d in self.doclist.get({"parentfield": "packing_details"}): |
| 117 | new_d = webnotes._dict(d.fields.copy()) |
| 118 | new_d.total_qty = -1 * d.qty |
| 119 | item_sales_bom.setdefault(d.parent_item, []).append(new_d) |
Nabin Hait | a0e7c15 | 2013-03-21 18:37:06 +0530 | [diff] [blame] | 120 | |
| 121 | if stock_ledger_entries: |
| 122 | for item in self.doclist.get({"parentfield": self.fname}): |
| 123 | if item.item_code in self.stock_items or \ |
| 124 | (item_sales_bom and item_sales_bom.get(item.item_code)): |
| 125 | buying_amount = get_buying_amount(item.item_code, item.warehouse, -1*item.qty, |
| 126 | self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, |
| 127 | item_sales_bom) |
Nabin Hait | f2d4df9 | 2013-05-07 13:12:02 +0530 | [diff] [blame] | 128 | |
| 129 | item.buying_amount = buying_amount >= 0.01 and buying_amount or 0 |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 130 | webnotes.conn.set_value(item.doctype, item.name, "buying_amount", |
| 131 | item.buying_amount) |
| 132 | |
| 133 | def check_expense_account(self, item): |
| 134 | if item.buying_amount and not item.expense_account: |
| 135 | msgprint(_("""Expense account is mandatory for item: """) + item.item_code, |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 136 | raise_exception=1) |
| 137 | |
| 138 | if item.buying_amount and not item.cost_center: |
| 139 | msgprint(_("""Cost Center is mandatory for item: """) + item.item_code, |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 140 | raise_exception=1) |
| 141 | |
| 142 | def calculate_taxes_and_totals(self): |
| 143 | self.doc.conversion_rate = flt(self.doc.conversion_rate) |
| 144 | self.item_doclist = self.doclist.get({"parentfield": self.fname}) |
| 145 | self.tax_doclist = self.doclist.get({"parentfield": "other_charges"}) |
| 146 | |
| 147 | self.calculate_item_values() |
| 148 | self.initialize_taxes() |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 149 | self.determine_exclusive_rate() |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 150 | self.calculate_net_total() |
| 151 | self.calculate_taxes() |
| 152 | self.calculate_totals() |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 153 | self.calculate_commission() |
| 154 | self.calculate_contribution() |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 155 | # self.calculate_outstanding_amount() |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 156 | self._cleanup() |
| 157 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 158 | # TODO |
| 159 | # print format: show net_total_export instead of net_total |
| 160 | |
| 161 | def determine_exclusive_rate(self): |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 162 | if not any((cint(tax.included_in_print_rate) for tax in self.tax_doclist)): |
| 163 | # no inclusive tax |
| 164 | return |
| 165 | |
| 166 | for item in self.item_doclist: |
| 167 | item_tax_map = self._load_item_tax_rate(item.item_tax_rate) |
| 168 | cumulated_tax_fraction = 0 |
| 169 | for i, tax in enumerate(self.tax_doclist): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 170 | tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map) |
| 171 | |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 172 | if i==0: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 173 | tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 174 | else: |
| 175 | tax.grand_total_fraction_for_current_item = \ |
| 176 | self.tax_doclist[i-1].grand_total_fraction_for_current_item \ |
| 177 | + tax.tax_fraction_for_current_item |
| 178 | |
| 179 | cumulated_tax_fraction += tax.tax_fraction_for_current_item |
| 180 | |
| 181 | if cumulated_tax_fraction: |
| 182 | item.basic_rate = flt((item.export_rate * self.doc.conversion_rate) / |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 183 | (1 + cumulated_tax_fraction), self.precision("basic_rate", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 184 | |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 185 | item.amount = flt(item.basic_rate * item.qty, self.precision("amount", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 186 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 187 | if item.adj_rate == 100: |
| 188 | item.base_ref_rate = item.basic_rate |
| 189 | item.basic_rate = 0.0 |
| 190 | else: |
| 191 | item.base_ref_rate = flt(item.basic_rate / (1 - (item.adj_rate / 100.0)), |
| 192 | self.precision("base_ref_rate", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 193 | |
| 194 | def get_current_tax_fraction(self, tax, item_tax_map): |
| 195 | """ |
| 196 | Get tax fraction for calculating tax exclusive amount |
| 197 | from tax inclusive amount |
| 198 | """ |
| 199 | current_tax_fraction = 0 |
| 200 | |
| 201 | if cint(tax.included_in_print_rate): |
| 202 | tax_rate = self._get_tax_rate(tax, item_tax_map) |
| 203 | |
| 204 | if tax.charge_type == "On Net Total": |
| 205 | current_tax_fraction = tax_rate / 100.0 |
| 206 | |
| 207 | elif tax.charge_type == "On Previous Row Amount": |
| 208 | current_tax_fraction = (tax_rate / 100.0) * \ |
| 209 | self.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item |
| 210 | |
| 211 | elif tax.charge_type == "On Previous Row Total": |
| 212 | current_tax_fraction = (tax_rate / 100.0) * \ |
| 213 | self.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item |
| 214 | |
| 215 | return current_tax_fraction |
| 216 | |
| 217 | def calculate_item_values(self): |
| 218 | def _set_base(item, print_field, base_field): |
| 219 | """set values in base currency""" |
| 220 | item.fields[base_field] = flt((flt(item.fields[print_field], |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 221 | self.precision(print_field, item)) * self.doc.conversion_rate), |
| 222 | self.precision(base_field, item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 223 | |
| 224 | for item in self.item_doclist: |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 225 | self.round_floats_in(item) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 226 | |
| 227 | if item.adj_rate == 100: |
| 228 | item.ref_rate = item.ref_rate or item.export_rate |
| 229 | item.export_rate = 0 |
| 230 | else: |
| 231 | if item.ref_rate: |
| 232 | item.export_rate = flt(item.ref_rate * (1.0 - (item.adj_rate / 100.0)), |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 233 | self.precision("export_rate", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 234 | else: |
| 235 | # assume that print rate and discount are specified |
| 236 | item.ref_rate = flt(item.export_rate / (1.0 - (item.adj_rate / 100.0)), |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 237 | self.precision("ref_rate", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 238 | |
| 239 | item.export_amount = flt(item.export_rate * item.qty, |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 240 | self.precision("export_amount", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 241 | |
| 242 | _set_base(item, "ref_rate", "base_ref_rate") |
| 243 | _set_base(item, "export_rate", "basic_rate") |
| 244 | _set_base(item, "export_amount", "amount") |
| 245 | |
| 246 | def initialize_taxes(self): |
| 247 | for tax in self.tax_doclist: |
| 248 | tax.tax_amount = tax.total = 0.0 |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 249 | tax.item_wise_tax_detail = {} |
| 250 | |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 251 | # temporary fields |
| 252 | tax.tax_amount_for_current_item = tax.grand_total_for_current_item = 0.0 |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 253 | |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 254 | self.validate_on_previous_row(tax) |
| 255 | self.validate_inclusive_tax(tax) |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 256 | self.round_floats_in(tax) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 257 | |
| 258 | def calculate_net_total(self): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 259 | self.doc.net_total = self.doc.net_total_export = 0.0 |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 260 | |
| 261 | for item in self.item_doclist: |
| 262 | self.doc.net_total += item.amount |
| 263 | self.doc.net_total_export += item.export_amount |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 264 | |
| 265 | self.round_floats_in(self.doc, ["net_total", "net_total_export"]) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 266 | |
| 267 | def calculate_taxes(self): |
| 268 | for item in self.item_doclist: |
| 269 | item_tax_map = self._load_item_tax_rate(item.item_tax_rate) |
| 270 | |
| 271 | for i, tax in enumerate(self.tax_doclist): |
| 272 | # tax_amount represents the amount of tax for the current step |
| 273 | current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map) |
| 274 | |
| 275 | # case when net total is 0 but there is an actual type charge |
| 276 | # in this case add the actual amount to tax.tax_amount |
| 277 | # and tax.grand_total_for_current_item for the first such iteration |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 278 | if tax.charge_type=="Actual" and \ |
| 279 | not (current_tax_amount or self.doc.net_total or tax.tax_amount): |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 280 | zero_net_total_adjustment = flt(tax.rate, self.precision("tax_amount", tax)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 281 | current_tax_amount += zero_net_total_adjustment |
| 282 | |
| 283 | # store tax_amount for current item as it will be used for |
| 284 | # charge type = 'On Previous Row Amount' |
| 285 | tax.tax_amount_for_current_item = current_tax_amount |
| 286 | |
| 287 | # accumulate tax amount into tax.tax_amount |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 288 | tax.tax_amount += current_tax_amount |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 289 | |
| 290 | # Calculate tax.total viz. grand total till that step |
| 291 | # note: grand_total_for_current_item contains the contribution of |
| 292 | # item's amount, previously applied tax and the current tax on that item |
| 293 | if i==0: |
| 294 | tax.grand_total_for_current_item = flt(item.amount + |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 295 | current_tax_amount, self.precision("total", tax)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 296 | |
| 297 | else: |
| 298 | tax.grand_total_for_current_item = \ |
| 299 | flt(self.tax_doclist[i-1].grand_total_for_current_item + |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 300 | current_tax_amount, self.precision("total", tax)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 301 | |
| 302 | # in tax.total, accumulate grand total of each item |
| 303 | tax.total += tax.grand_total_for_current_item |
| 304 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 305 | # store tax breakup for each item |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 306 | tax.item_wise_tax_detail[item.item_code] = current_tax_amount |
| 307 | |
| 308 | def calculate_totals(self): |
| 309 | self.doc.grand_total = flt(self.tax_doclist and \ |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 310 | self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total")) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 311 | self.doc.grand_total_export = flt(self.doc.grand_total / self.doc.conversion_rate, |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 312 | self.precision("grand_total_export")) |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 313 | |
| 314 | self.doc.other_charges_total = flt(self.doc.grand_total - self.doc.net_total, |
| 315 | self.precision("other_charges_total")) |
| 316 | self.doc.other_charges_total_export = flt(self.doc.grand_total_export - self.doc.net_total_export, |
| 317 | self.precision("other_charges_total_export")) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 318 | |
| 319 | self.doc.rounded_total = round(self.doc.grand_total) |
| 320 | self.doc.rounded_total_export = round(self.doc.grand_total_export) |
| 321 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 322 | def calculate_commission(self): |
Anand Doshi | 65d8755 | 2013-05-21 19:53:33 +0530 | [diff] [blame] | 323 | self.round_floats_in(self.doc, ["net_total", "commission_rate"]) |
| 324 | if self.doc.commission_rate > 100.0: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 325 | msgprint(_(self.meta.get_label("commission_rate")) + " " + |
| 326 | _("cannot be greater than 100"), raise_exception=True) |
| 327 | |
| 328 | self.doc.total_commission = flt(self.doc.net_total * self.doc.commission_rate / 100.0, |
| 329 | self.precision("total_commission")) |
| 330 | |
| 331 | def calculate_contribution(self): |
| 332 | total = 0.0 |
| 333 | sales_team = self.doclist.get({"parentfield": "sales_team"}) |
| 334 | for sales_person in sales_team: |
| 335 | self.round_floats_in(sales_person) |
| 336 | |
| 337 | sales_person.allocated_amount = flt( |
| 338 | self.doc.net_total * sales_person.allocated_percentage / 100.0, |
| 339 | self.precision("allocated_amount", sales_person)) |
| 340 | |
| 341 | total += sales_person.allocated_percentage |
| 342 | |
| 343 | if sales_team and total != 100.0: |
| 344 | msgprint(_("Total") + " " + |
| 345 | _(self.meta.get_label("allocated_percentage", parentfield="sales_team")) + |
| 346 | " " + _("should be 100%"), raise_exception=True) |
| 347 | |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 348 | def get_current_tax_amount(self, item, tax, item_tax_map): |
| 349 | tax_rate = self._get_tax_rate(tax, item_tax_map) |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 350 | current_tax_amount = 0.0 |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 351 | |
| 352 | if tax.charge_type == "Actual": |
| 353 | # distribute the tax amount proportionally to each item row |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 354 | actual = flt(tax.rate, self.precision("tax_amount", tax)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 355 | current_tax_amount = (self.doc.net_total |
| 356 | and ((item.amount / self.doc.net_total) * actual) |
| 357 | or 0) |
| 358 | elif tax.charge_type == "On Net Total": |
| 359 | current_tax_amount = (tax_rate / 100.0) * item.amount |
| 360 | elif tax.charge_type == "On Previous Row Amount": |
| 361 | current_tax_amount = (tax_rate / 100.0) * \ |
| 362 | self.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item |
| 363 | elif tax.charge_type == "On Previous Row Total": |
| 364 | current_tax_amount = (tax_rate / 100.0) * \ |
| 365 | self.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item |
| 366 | |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 367 | return flt(current_tax_amount, self.precision("tax_amount", tax)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 368 | |
| 369 | def validate_on_previous_row(self, tax): |
| 370 | """ |
| 371 | validate if a valid row id is mentioned in case of |
| 372 | On Previous Row Amount and On Previous Row Total |
| 373 | """ |
| 374 | if tax.charge_type in ["On Previous Row Amount", "On Previous Row Total"] and \ |
| 375 | (not tax.row_id or cint(tax.row_id) >= tax.idx): |
| 376 | msgprint((_("Row") + " # %(idx)s [%(taxes_doctype)s]: " + \ |
| 377 | _("Please specify a valid") + " %(row_id_label)s") % { |
| 378 | "idx": tax.idx, |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 379 | "taxes_doctype": tax.doctype, |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 380 | "row_id_label": self.meta.get_label("row_id", |
| 381 | parentfield="other_charges") |
| 382 | }, raise_exception=True) |
| 383 | |
| 384 | def validate_inclusive_tax(self, tax): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 385 | def _on_previous_row_error(row_range): |
| 386 | msgprint((_("Row") + " # %(idx)s [%(doctype)s]: " + |
| 387 | _("to be included in Item's rate, it is required that: ") + |
| 388 | " [" + _("Row") + " # %(row_range)s] " + _("also be included in Item's rate")) % { |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 389 | "idx": tax.idx, |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 390 | "doctype": tax.doctype, |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 391 | "inclusive_label": self.meta.get_label("included_in_print_rate", |
| 392 | parentfield="other_charges"), |
| 393 | "charge_type_label": self.meta.get_label("charge_type", |
| 394 | parentfield="other_charges"), |
| 395 | "charge_type": tax.charge_type, |
| 396 | "row_range": row_range |
| 397 | }, raise_exception=True) |
| 398 | |
| 399 | if cint(tax.included_in_print_rate): |
| 400 | if tax.charge_type == "Actual": |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 401 | # inclusive tax cannot be of type Actual |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 402 | msgprint((_("Row") |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 403 | + " # %(idx)s [%(doctype)s]: %(charge_type_label)s = \"%(charge_type)s\" " |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 404 | + "cannot be included in Item's rate") % { |
| 405 | "idx": tax.idx, |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 406 | "doctype": tax.doctype, |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 407 | "charge_type_label": self.meta.get_label("charge_type", |
| 408 | parentfield="other_charges"), |
| 409 | "charge_type": tax.charge_type, |
| 410 | }, raise_exception=True) |
| 411 | elif tax.charge_type == "On Previous Row Amount" and \ |
| 412 | not cint(self.tax_doclist[tax.row_id - 1].included_in_print_rate): |
| 413 | # referred row should also be inclusive |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 414 | _on_previous_row_error(tax.row_id) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 415 | elif tax.charge_type == "On Previous Row Total" and \ |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 416 | not all([cint(t.included_in_print_rate) for t in self.tax_doclist[:tax.row_id - 1]]): |
| 417 | # all rows about the reffered tax should be inclusive |
| 418 | _on_previous_row_error("1 - %d" % (tax.row_id,)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 419 | |
| 420 | def _load_item_tax_rate(self, item_tax_rate): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 421 | return json.loads(item_tax_rate) if item_tax_rate else {} |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 422 | |
| 423 | def _get_tax_rate(self, tax, item_tax_map): |
| 424 | if item_tax_map.has_key(tax.account_head): |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 425 | return flt(item_tax_map.get(tax.account_head), self.precision("rate", tax)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 426 | else: |
| 427 | return tax.rate |
| 428 | |
| 429 | def _cleanup(self): |
| 430 | for tax in self.tax_doclist: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 431 | for fieldname in ("grand_total_for_current_item", |
| 432 | "tax_amount_for_current_item", |
| 433 | "tax_fraction_for_current_item", |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 434 | "grand_total_fraction_for_current_item"): |
| 435 | if fieldname in tax.fields: |
| 436 | del tax.fields[fieldname] |
| 437 | |
| 438 | tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail) |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 439 | |
| 440 | def validate_order_type(self): |
| 441 | valid_types = ["Sales", "Maintenance"] |
| 442 | if self.doc.order_type not in valid_types: |
| 443 | msgprint(_(self.meta.get_label("order_type")) + " " + |
| 444 | _("must be one of") + ": " + comma_or(valid_types), |
| 445 | raise_exception=True) |