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