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