Nabin Hait | 3237c75 | 2015-02-17 11:11:11 +0530 | [diff] [blame^] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import json |
| 6 | from frappe import _, throw |
| 7 | from frappe.utils import cint, flt, rounded |
| 8 | from erpnext.setup.utils import get_company_currency |
| 9 | from erpnext.controllers.accounts_controller import validate_conversion_rate |
| 10 | |
| 11 | class taxes_and_totals(object): |
| 12 | def __init__(self, doc): |
| 13 | self.doc = doc |
| 14 | |
| 15 | def calculate(self): |
| 16 | self.discount_amount_applied = False |
| 17 | self._calculate() |
| 18 | |
| 19 | if self.doc.meta.get_field("discount_amount"): |
| 20 | self.apply_discount_amount() |
| 21 | |
| 22 | if self.doctype in ["Sales Invoice", "Purchase Invoice"]: |
| 23 | self.calculate_total_advance() |
| 24 | |
| 25 | def _calculate(self): |
| 26 | # validate conversion rate |
| 27 | company_currency = get_company_currency(self.doc.company) |
| 28 | if not self.doc.currency or self.doc.currency == company_currency: |
| 29 | self.doc.currency = company_currency |
| 30 | self.doc.conversion_rate = 1.0 |
| 31 | else: |
| 32 | validate_conversion_rate(self.doc.currency, self.doc.conversion_rate, |
| 33 | self.doc.meta.get_label("conversion_rate"), self.doc.company) |
| 34 | |
| 35 | self.doc.conversion_rate = flt(self.doc.conversion_rate) |
| 36 | |
| 37 | self.calculate_item_values() |
| 38 | self.initialize_taxes() |
| 39 | |
| 40 | if self.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]: |
| 41 | self.determine_exclusive_rate() |
| 42 | |
| 43 | self.calculate_net_total() |
| 44 | self.calculate_taxes() |
| 45 | self.calculate_totals() |
| 46 | self._cleanup() |
| 47 | |
| 48 | def calculate_item_values(self): |
| 49 | if not self.discount_amount_applied: |
| 50 | for item in self.doc.get("items"): |
| 51 | self.doc.round_floats_in(item) |
| 52 | |
| 53 | if item.discount_percentage == 100: |
| 54 | item.rate = 0.0 |
| 55 | elif not item.rate: |
| 56 | item.rate = flt(item.price_list_rate * (1.0 - (item.discount_percentage / 100.0)), |
| 57 | self.doc.precision("rate", item)) |
| 58 | |
| 59 | item.amount = flt(item.rate * item.qty, self.doc.precision("amount", item)) |
| 60 | item.item_tax_amount = 0.0; |
| 61 | |
| 62 | self._set_in_company_currency(item, "price_list_rate", "base_price_list_rate") |
| 63 | self._set_in_company_currency(item, "rate", "base_rate") |
| 64 | self._set_in_company_currency(item, "amount", "base_amount") |
| 65 | |
| 66 | def _set_in_company_currency(self, item, print_field, base_field): |
| 67 | """set values in base currency""" |
| 68 | value_in_company_currency = flt(self.doc.conversion_rate * |
| 69 | flt(item.get(print_field), self.doc.precision(print_field, item)), self.doc.precision(base_field, item)) |
| 70 | item.set(base_field, value_in_company_currency) |
| 71 | |
| 72 | def initialize_taxes(self): |
| 73 | for tax in self.doc.get("taxes"): |
| 74 | tax.item_wise_tax_detail = {} |
| 75 | tax_fields = ["total", "tax_amount_after_discount_amount", |
| 76 | "tax_amount_for_current_item", "grand_total_for_current_item", |
| 77 | "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"] |
| 78 | |
| 79 | if not self.discount_amount_applied: |
| 80 | tax_fields.append("tax_amount") |
| 81 | |
| 82 | for fieldname in tax_fields: |
| 83 | tax.set(fieldname, 0.0) |
| 84 | |
| 85 | self.validate_on_previous_row(tax) |
| 86 | self.validate_inclusive_tax(tax) |
| 87 | self.doc.round_floats_in(tax) |
| 88 | |
| 89 | def validate_on_previous_row(self, tax): |
| 90 | """ |
| 91 | validate if a valid row id is mentioned in case of |
| 92 | On Previous Row Amount and On Previous Row Total |
| 93 | """ |
| 94 | if tax.charge_type in ["On Previous Row Amount", "On Previous Row Total"] and \ |
| 95 | (not tax.row_id or cint(tax.row_id) >= tax.idx): |
| 96 | throw(_("Please specify a valid Row ID for {0} in row {1}").format(_(tax.doctype), tax.idx)) |
| 97 | |
| 98 | def validate_inclusive_tax(self, tax): |
| 99 | def _on_previous_row_error(row_range): |
| 100 | throw(_("To include tax in row {0} in Item rate, taxes in rows {1} must also be included").format(tax.idx, |
| 101 | row_range)) |
| 102 | |
| 103 | if cint(getattr(tax, "included_in_print_rate", None)): |
| 104 | if tax.charge_type == "Actual": |
| 105 | # inclusive tax cannot be of type Actual |
| 106 | throw(_("Charge of type 'Actual' in row {0} cannot be included in Item Rate").format(tax.idx)) |
| 107 | elif tax.charge_type == "On Previous Row Amount" and \ |
| 108 | not cint(self.doc.get("taxes")[cint(tax.row_id) - 1].included_in_print_rate): |
| 109 | # referred row should also be inclusive |
| 110 | _on_previous_row_error(tax.row_id) |
| 111 | elif tax.charge_type == "On Previous Row Total" and \ |
| 112 | not all([cint(t.included_in_print_rate) for t in self.doc.get("taxes")[:cint(tax.row_id) - 1]]): |
| 113 | # all rows about the reffered tax should be inclusive |
| 114 | _on_previous_row_error("1 - %d" % (tax.row_id,)) |
| 115 | |
| 116 | def determine_exclusive_rate(self): |
| 117 | if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))): |
| 118 | # no inclusive tax |
| 119 | return |
| 120 | |
| 121 | for item in self.doc.get("items"): |
| 122 | item_tax_map = self._load_item_tax_rate(item.item_tax_rate) |
| 123 | cumulated_tax_fraction = 0 |
| 124 | for i, tax in enumerate(self.doc.get("taxes")): |
| 125 | tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map) |
| 126 | |
| 127 | if i==0: |
| 128 | tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item |
| 129 | else: |
| 130 | tax.grand_total_fraction_for_current_item = \ |
| 131 | self.doc.get("taxes")[i-1].grand_total_fraction_for_current_item \ |
| 132 | + tax.tax_fraction_for_current_item |
| 133 | |
| 134 | cumulated_tax_fraction += tax.tax_fraction_for_current_item |
| 135 | |
| 136 | if cumulated_tax_fraction and not self.discount_amount_applied and item.qty: |
| 137 | item.base_amount = flt((item.amount * self.doc.conversion_rate) / |
| 138 | (1 + cumulated_tax_fraction), self.doc.precision("base_amount", item)) |
| 139 | |
| 140 | item.base_rate = flt(item.base_amount / item.qty, self.doc.precision("base_rate", item)) |
| 141 | item.discount_percentage = flt(item.discount_percentage, self.doc.precision("discount_percentage", item)) |
| 142 | |
| 143 | if item.discount_percentage == 100: |
| 144 | item.base_price_list_rate = item.base_rate |
| 145 | item.base_rate = 0.0 |
| 146 | else: |
| 147 | item.base_price_list_rate = flt(item.base_rate / (1 - (item.discount_percentage / 100.0)), |
| 148 | self.doc.precision("base_price_list_rate", item)) |
| 149 | |
| 150 | def _load_item_tax_rate(self, item_tax_rate): |
| 151 | return json.loads(item_tax_rate) if item_tax_rate else {} |
| 152 | |
| 153 | def get_current_tax_fraction(self, tax, item_tax_map): |
| 154 | """ |
| 155 | Get tax fraction for calculating tax exclusive amount |
| 156 | from tax inclusive amount |
| 157 | """ |
| 158 | current_tax_fraction = 0 |
| 159 | |
| 160 | if cint(tax.included_in_print_rate): |
| 161 | tax_rate = self._get_tax_rate(tax, item_tax_map) |
| 162 | |
| 163 | if tax.charge_type == "On Net Total": |
| 164 | current_tax_fraction = tax_rate / 100.0 |
| 165 | |
| 166 | elif tax.charge_type == "On Previous Row Amount": |
| 167 | current_tax_fraction = (tax_rate / 100.0) * \ |
| 168 | self.doc.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item |
| 169 | |
| 170 | elif tax.charge_type == "On Previous Row Total": |
| 171 | current_tax_fraction = (tax_rate / 100.0) * \ |
| 172 | self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item |
| 173 | |
| 174 | return current_tax_fraction |
| 175 | |
| 176 | def _get_tax_rate(self, tax, item_tax_map): |
| 177 | if item_tax_map.has_key(tax.account_head): |
| 178 | return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax)) |
| 179 | else: |
| 180 | return tax.rate |
| 181 | |
| 182 | def calculate_net_total(self): |
| 183 | self.doc.base_net_total = self.doc.net_total = 0.0 |
| 184 | |
| 185 | for item in self.doc.get("items"): |
| 186 | self.doc.base_net_total += item.base_amount |
| 187 | self.doc.net_total += item.amount |
| 188 | |
| 189 | self.round_floats_in(self.doc, ["base_net_total", "net_total"]) |
| 190 | |
| 191 | def calculate_taxes(self): |
| 192 | # maintain actual tax rate based on idx |
| 193 | actual_tax_dict = dict([[tax.idx, flt(tax.rate, self.doc.precision("tax_amount", tax))] |
| 194 | for tax in self.doc.get("taxes") if tax.charge_type == "Actual"]) |
| 195 | |
| 196 | for n, item in enumerate(self.doc.get("items")): |
| 197 | item_tax_map = self._load_item_tax_rate(item.item_tax_rate) |
| 198 | |
| 199 | for i, tax in enumerate(self.doc.get("taxes")): |
| 200 | # tax_amount represents the amount of tax for the current step |
| 201 | current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map) |
| 202 | |
| 203 | # Adjust divisional loss to the last item |
| 204 | if tax.charge_type == "Actual": |
| 205 | actual_tax_dict[tax.idx] -= current_tax_amount |
| 206 | if n == len(self.doc.get("items")) - 1: |
| 207 | current_tax_amount += actual_tax_dict[tax.idx] |
| 208 | |
| 209 | # store tax_amount for current item as it will be used for |
| 210 | # charge type = 'On Previous Row Amount' |
| 211 | tax.tax_amount_for_current_item = current_tax_amount |
| 212 | |
| 213 | # accumulate tax amount into tax.tax_amount |
| 214 | if not self.discount_amount_applied: |
| 215 | tax.tax_amount += current_tax_amount |
| 216 | |
| 217 | tax.tax_amount_after_discount_amount += current_tax_amount |
| 218 | |
| 219 | if getattr(tax, "category", None): |
| 220 | # if just for valuation, do not add the tax amount in total |
| 221 | # hence, setting it as 0 for further steps |
| 222 | current_tax_amount = 0.0 if (tax.category == "Valuation") \ |
| 223 | else current_tax_amount |
| 224 | |
| 225 | current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0 |
| 226 | |
| 227 | # Calculate tax.total viz. grand total till that step |
| 228 | # note: grand_total_for_current_item contains the contribution of |
| 229 | # item's amount, previously applied tax and the current tax on that item |
| 230 | if i==0: |
| 231 | tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount, |
| 232 | self.doc.precision("total", tax)) |
| 233 | else: |
| 234 | tax.grand_total_for_current_item = \ |
| 235 | flt(self.doc.get("taxes")[i-1].grand_total_for_current_item + |
| 236 | current_tax_amount, self.doc.precision("total", tax)) |
| 237 | |
| 238 | # in tax.total, accumulate grand total of each item |
| 239 | tax.total += tax.grand_total_for_current_item |
| 240 | |
| 241 | # set precision in the last item iteration |
| 242 | if n == len(self.doc.get("items")) - 1: |
| 243 | self.round_off_totals(tax) |
| 244 | |
| 245 | # adjust Discount Amount loss in last tax iteration |
| 246 | if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied: |
| 247 | self.adjust_discount_amount_loss(tax) |
| 248 | |
| 249 | def get_current_tax_amount(self, item, tax, item_tax_map): |
| 250 | tax_rate = self._get_tax_rate(tax, item_tax_map) |
| 251 | current_tax_amount = 0.0 |
| 252 | |
| 253 | if tax.charge_type == "Actual": |
| 254 | # distribute the tax amount proportionally to each item row |
| 255 | actual = flt(tax.rate, self.doc.precision("tax_amount", tax)) |
| 256 | current_tax_amount = (self.doc.base_net_total |
| 257 | and ((item.base_amount / self.doc.base_net_total) * actual) |
| 258 | or 0) |
| 259 | elif tax.charge_type == "On Net Total": |
| 260 | current_tax_amount = (tax_rate / 100.0) * item.base_amount |
| 261 | elif tax.charge_type == "On Previous Row Amount": |
| 262 | current_tax_amount = (tax_rate / 100.0) * \ |
| 263 | self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item |
| 264 | elif tax.charge_type == "On Previous Row Total": |
| 265 | current_tax_amount = (tax_rate / 100.0) * \ |
| 266 | self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item |
| 267 | |
| 268 | current_tax_amount = flt(current_tax_amount, self.doc.precision("tax_amount", tax)) |
| 269 | |
| 270 | # store tax breakup for each item |
| 271 | key = item.item_code or item.item_name |
| 272 | if tax.item_wise_tax_detail.get(key): |
| 273 | item_wise_tax_amount = tax.item_wise_tax_detail[key][1] + current_tax_amount |
| 274 | tax.item_wise_tax_detail[key] = [tax_rate,item_wise_tax_amount] |
| 275 | else: |
| 276 | tax.item_wise_tax_detail[key] = [tax_rate,current_tax_amount] |
| 277 | |
| 278 | return current_tax_amount |
| 279 | |
| 280 | def round_off_totals(self, tax): |
| 281 | tax.total = flt(tax.total, self.doc.precision("total", tax)) |
| 282 | tax.tax_amount = flt(tax.tax_amount, self.doc.precision("tax_amount", tax)) |
| 283 | tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, |
| 284 | self.doc.precision("tax_amount", tax)) |
| 285 | |
| 286 | def adjust_discount_amount_loss(self, tax): |
| 287 | discount_amount_loss = self.doc.base_grand_total - flt(self.doc.base_discount_amount) - tax.total |
| 288 | tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount + |
| 289 | discount_amount_loss, self.doc.precision("tax_amount", tax)) |
| 290 | tax.total = flt(tax.total + discount_amount_loss, self.doc.precision("total", tax)) |
| 291 | |
| 292 | def calculate_totals(self): |
| 293 | self.doc.base_grand_total = flt(self.doc.get("taxes")[-1].total |
| 294 | if self.doc.get("taxes") else self.doc.base_net_total) |
| 295 | |
| 296 | self.doc.base_total_taxes_and_charges = flt(self.doc.base_grand_total - self.doc.base_net_total, |
| 297 | self.doc.precision("base_total_taxes_and_charges")) |
| 298 | |
| 299 | if self.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]: |
| 300 | self.doc.grand_total = flt(self.doc.base_grand_total / self.doc.conversion_rate) \ |
| 301 | if (self.doc.base_total_taxes_and_charges or self.doc.discount_amount) else self.doc.net_total |
| 302 | |
| 303 | self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total + |
| 304 | flt(self.doc.discount_amount), self.doc.precision("total_taxes_and_charges")) |
| 305 | else: |
| 306 | self.doc.base_taxes_and_charges_added, self.base_taxes_and_charges_deducted = 0.0, 0.0 |
| 307 | for tax in self.doc.get("taxes"): |
| 308 | if tax.category in ["Valuation and Total", "Total"]: |
| 309 | if tax.add_deduct_tax == "Add": |
| 310 | self.doc.base_taxes_and_charges_added += flt(tax.tax_amount) |
| 311 | else: |
| 312 | self.doc.base_taxes_and_charges_deducted += flt(tax.tax_amount) |
| 313 | |
| 314 | self.doc.round_floats_in(self.doc, ["base_taxes_and_charges_added", "base_taxes_and_charges_deducted"]) |
| 315 | |
| 316 | self.doc.grand_total = flt(self.doc.base_grand_total / self.doc.conversion_rate) \ |
| 317 | if (self.doc.base_taxes_and_charges_added or self.doc.base_taxes_and_charges_deducted) else self.doc.net_total |
| 318 | |
| 319 | self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total, |
| 320 | self.doc.precision("total_taxes_and_charges")) |
| 321 | |
| 322 | self.doc.taxes_and_charges_added = flt(self.doc.base_taxes_and_charges_added / self.doc.conversion_rate, |
| 323 | self.doc.precision("taxes_and_charges_added")) |
| 324 | self.doc.taxes_and_charges_deducted = flt(self.doc.base_taxes_and_charges_deducted / self.doc.conversion_rate, |
| 325 | self.doc.precision("taxes_and_charges_deducted")) |
| 326 | |
| 327 | self.doc.base_grand_total = flt(self.doc.base_grand_total, self.doc.precision("base_grand_total")) |
| 328 | self.doc.grand_total = flt(self.doc.grand_total, self.doc.precision("grand_total")) |
| 329 | |
| 330 | if self.doc.meta.get_field("base_rounded_total"): |
| 331 | self.doc.base_rounded_total = rounded(self.doc.base_grand_total) |
| 332 | if self.doc.meta.get_field("rounded_total"): |
| 333 | self.doc.rounded_total = rounded(self.doc.grand_total) |
| 334 | |
| 335 | def _cleanup(self): |
| 336 | for tax in self.doc.get("taxes"): |
| 337 | tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':')) |
| 338 | |
| 339 | def apply_discount_amount(self): |
| 340 | if self.doc.discount_amount: |
| 341 | self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate, |
| 342 | self.doc.precision("base_discount_amount")) |
| 343 | |
| 344 | grand_total_for_discount_amount = self.get_grand_total_for_discount_amount() |
| 345 | |
| 346 | if grand_total_for_discount_amount: |
| 347 | # calculate item amount after Discount Amount |
| 348 | for item in self.doc.get("items"): |
| 349 | distributed_amount = flt(self.doc.base_discount_amount) * item.base_amount / grand_total_for_discount_amount |
| 350 | item.base_amount = flt(item.base_amount - distributed_amount, self.doc.precision("base_amount", item)) |
| 351 | |
| 352 | self.discount_amount_applied = True |
| 353 | self._calculate() |
| 354 | else: |
| 355 | self.doc.base_discount_amount = 0 |
| 356 | |
| 357 | def get_grand_total_for_discount_amount(self): |
| 358 | actual_taxes_dict = {} |
| 359 | |
| 360 | for tax in self.doc.get("taxes"): |
| 361 | if tax.charge_type == "Actual": |
| 362 | actual_taxes_dict.setdefault(tax.idx, tax.tax_amount) |
| 363 | elif tax.row_id in actual_taxes_dict: |
| 364 | actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * flt(tax.rate) / 100 |
| 365 | actual_taxes_dict.setdefault(tax.idx, actual_tax_amount) |
| 366 | |
| 367 | grand_total_for_discount_amount = flt(self.doc.base_grand_total - sum(actual_taxes_dict.values()), |
| 368 | self.doc.precision("base_grand_total")) |
| 369 | return grand_total_for_discount_amount |
| 370 | |
| 371 | |
| 372 | def calculate_total_advance(self, parenttype, advance_parentfield): |
| 373 | if self.docstatus < 2: |
| 374 | sum_of_allocated_amount = sum([flt(adv.allocated_amount, self.doc.precision("allocated_amount", adv)) |
| 375 | for adv in self.doc.get("advances")]) |
| 376 | |
| 377 | self.doc.total_advance = flt(sum_of_allocated_amount, self.doc.precision("total_advance")) |
| 378 | |
| 379 | if self.docstatus == 0: |
| 380 | self.calculate_outstanding_amount() |
| 381 | |
| 382 | def calculate_outstanding_amount(self): |
| 383 | # NOTE: |
| 384 | # write_off_amount is only for POS Invoice |
| 385 | # total_advance is only for non POS Invoice |
| 386 | |
| 387 | if self.doctype == "Sales Invoice": |
| 388 | self.round_floats_in(self.doc, ["base_grand_total", "total_advance", "write_off_amount", "paid_amount"]) |
| 389 | total_amount_to_pay = self.doc.base_grand_total - self.doc.write_off_amount |
| 390 | self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance - self.doc.paid_amount, |
| 391 | self.doc.precision("outstanding_amount")) |
| 392 | else: |
| 393 | self.round_floats_in(self.doc, ["total_advance", "write_off_amount"]) |
| 394 | self.doc.total_amount_to_pay = flt(self.doc.base_grand_total - self.doc.write_off_amount, |
| 395 | self.doc.precision("total_amount_to_pay")) |
| 396 | self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance, |
| 397 | self.doc.precision("outstanding_amount")) |