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