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