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