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