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