blob: 0355c269d0bbf0bed22252bc7874281eb9a8a593 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Nabin Hait3237c752015-02-17 11:11:11 +05302# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5import json
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +05306import frappe, erpnext
Nabin Hait3769d872015-12-18 13:12:02 +05307from frappe import _, scrub
Nabin Haitfb0b24a2016-01-20 14:46:26 +05308from frappe.utils import cint, flt, round_based_on_smallest_currency_fraction
Nabin Hait613d0812015-02-23 11:58:15 +05309from erpnext.controllers.accounts_controller import validate_conversion_rate, \
10 validate_taxes_and_charges, validate_inclusive_tax
Nabin Hait3237c752015-02-17 11:11:11 +053011
Nabin Haitfe81da22015-02-18 12:23:18 +053012class calculate_taxes_and_totals(object):
Nabin Hait3237c752015-02-17 11:11:11 +053013 def __init__(self, doc):
14 self.doc = doc
Nabin Haitfe81da22015-02-18 12:23:18 +053015 self.calculate()
16
Nabin Hait3237c752015-02-17 11:11:11 +053017 def calculate(self):
18 self.discount_amount_applied = False
19 self._calculate()
20
21 if self.doc.meta.get_field("discount_amount"):
Nabin Hait3769d872015-12-18 13:12:02 +053022 self.set_discount_amount()
Nabin Hait3237c752015-02-17 11:11:11 +053023 self.apply_discount_amount()
24
Nabin Haitbd00e812015-02-17 12:50:51 +053025 if self.doc.doctype in ["Sales Invoice", "Purchase Invoice"]:
Nabin Hait3237c752015-02-17 11:11:11 +053026 self.calculate_total_advance()
27
28 def _calculate(self):
Nabin Haite7679702015-02-20 14:40:35 +053029 self.calculate_item_values()
30 self.initialize_taxes()
31 self.determine_exclusive_rate()
32 self.calculate_net_total()
33 self.calculate_taxes()
Nabin Haita1bf43b2015-03-17 10:50:47 +053034 self.manipulate_grand_total_for_inclusive_tax()
Nabin Haite7679702015-02-20 14:40:35 +053035 self.calculate_totals()
36 self._cleanup()
37
38 def validate_conversion_rate(self):
Nabin Hait3237c752015-02-17 11:11:11 +053039 # validate conversion rate
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053040 company_currency = erpnext.get_company_currency(self.doc.company)
Nabin Hait3237c752015-02-17 11:11:11 +053041 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 Hait3237c752015-02-17 11:11:11 +053050 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 Haite7679702015-02-20 14:40:35 +053058 item.rate = flt(item.price_list_rate *
59 (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
Nabin Hait3237c752015-02-17 11:11:11 +053060
mbauskara52472c2016-03-05 15:10:25 +053061 if item.doctype in ['Quotation Item', 'Sales Order Item', 'Delivery Note Item', 'Sales Invoice Item']:
mbauskar36b51892016-01-18 16:31:10 +053062 item.total_margin = self.calculate_margin(item)
mbauskardcd8b772016-01-20 12:39:57 +053063 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
mbauskara52472c2016-03-05 15:10:25 +053065
Nabin Haite7679702015-02-20 14:40:35 +053066 item.net_rate = item.rate
67 item.amount = flt(item.rate * item.qty, item.precision("amount"))
68 item.net_amount = item.amount
Nabin Hait3237c752015-02-17 11:11:11 +053069
Nabin Haite7679702015-02-20 14:40:35 +053070 self._set_in_company_currency(item, ["price_list_rate", "rate", "net_rate", "amount", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +053071
Nabin Haite7679702015-02-20 14:40:35 +053072 item.item_tax_amount = 0.0
73
74 def _set_in_company_currency(self, doc, fields):
Nabin Hait3237c752015-02-17 11:11:11 +053075 """set values in base currency"""
Nabin Haite7679702015-02-20 14:40:35 +053076 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 Hait3237c752015-02-17 11:11:11 +053079
80 def initialize_taxes(self):
81 for tax in self.doc.get("taxes"):
Nabin Hait86cd4cc2015-02-28 19:11:51 +053082 if not self.discount_amount_applied:
83 validate_taxes_and_charges(tax)
84 validate_inclusive_tax(tax, self.doc)
Nabin Hait613d0812015-02-23 11:58:15 +053085
Nabin Hait3237c752015-02-17 11:11:11 +053086 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 Haitde9c8a92015-02-23 01:06:00 +053091 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 Hait3237c752015-02-17 11:11:11 +053094
95 for fieldname in tax_fields:
96 tax.set(fieldname, 0.0)
97
Nabin Hait3237c752015-02-17 11:11:11 +053098 self.doc.round_floats_in(tax)
99
Nabin Hait3237c752015-02-17 11:11:11 +0530100 def determine_exclusive_rate(self):
Nabin Hait37b047d2015-02-23 16:01:33 +0530101 if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))):
102 return
Nabin Hait3237c752015-02-17 11:11:11 +0530103
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 Haite7679702015-02-20 14:40:35 +0530120 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 Hait3237c752015-02-17 11:11:11 +0530123
Nabin Haite7679702015-02-20 14:40:35 +0530124 self._set_in_company_currency(item, ["net_rate", "net_amount"])
125
Nabin Hait3237c752015-02-17 11:11:11 +0530126 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 Haita6ee8292015-05-15 12:02:01 +0530150 if getattr(tax, "add_deduct_tax", None):
151 current_tax_fraction *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
Nabin Hait3237c752015-02-17 11:11:11 +0530152 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 Haitf0bc9b62015-02-23 01:40:01 +0530161 self.doc.total = self.doc.base_total = self.doc.net_total = self.doc.base_net_total = 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530162
163 for item in self.doc.get("items"):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530164 self.doc.total += item.amount
165 self.doc.base_total += item.base_amount
Nabin Haite7679702015-02-20 14:40:35 +0530166 self.doc.net_total += item.net_amount
167 self.doc.base_net_total += item.base_net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530168
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530169 self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530170
171 def calculate_taxes(self):
172 # maintain actual tax rate based on idx
Nabin Haite7679702015-02-20 14:40:35 +0530173 actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
Nabin Hait3237c752015-02-17 11:11:11 +0530174 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 Hait2b019ed2015-02-22 23:03:07 +0530189 # accumulate tax amount into tax.tax_amount
Nabin Haitde9c8a92015-02-23 01:06:00 +0530190 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 Hait2b019ed2015-02-22 23:03:07 +0530193
Nabin Hait3237c752015-02-17 11:11:11 +0530194 # 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 Hait2b019ed2015-02-22 23:03:07 +0530198 # set tax after discount
Nabin Hait3237c752015-02-17 11:11:11 +0530199 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 Hait93b3a372015-02-24 17:08:34 +0530213 tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount, tax.precision("total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530214 else:
215 tax.grand_total_for_current_item = \
Rushabh Mehta6cdfd1e2015-02-25 15:55:57 +0530216 flt(self.doc.get("taxes")[i-1].grand_total_for_current_item + current_tax_amount, tax.precision("total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530217
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 Doshiec5ec602015-03-05 19:31:23 +0530224
Nabin Hait3237c752015-02-17 11:11:11 +0530225 # adjust Discount Amount loss in last tax iteration
Nabin Haitde9c8a92015-02-23 01:06:00 +0530226 if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
Nabin Haitdb53a782015-07-31 16:53:13 +0530227 and self.doc.discount_amount and self.doc.apply_discount_on == "Grand Total":
Nabin Haitde9c8a92015-02-23 01:06:00 +0530228 self.adjust_discount_amount_loss(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530229
230
Nabin Hait3237c752015-02-17 11:11:11 +0530231
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 Haite7679702015-02-20 14:40:35 +0530238 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 Hait3237c752015-02-17 11:11:11 +0530241 elif tax.charge_type == "On Net Total":
Nabin Haite7679702015-02-20 14:40:35 +0530242 current_tax_amount = (tax_rate / 100.0) * item.net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530243 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 Hait93b3a372015-02-24 17:08:34 +0530250 current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530251
Nabin Haite7679702015-02-20 14:40:35 +0530252 self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530253
254 return current_tax_amount
255
Nabin Haite7679702015-02-20 14:40:35 +0530256 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 Hait3237c752015-02-17 11:11:11 +0530265 def round_off_totals(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530266 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 Hait3237c752015-02-17 11:11:11 +0530269
Nabin Haitde9c8a92015-02-23 01:06:00 +0530270 self._set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Haitce245122015-02-22 20:14:49 +0530271
Nabin Hait3237c752015-02-17 11:11:11 +0530272 def adjust_discount_amount_loss(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530273 discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
Nabin Hait3237c752015-02-17 11:11:11 +0530274 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Nabin Hait95ff2d42015-03-04 16:02:03 +0530275 discount_amount_loss, tax.precision("tax_amount"))
276 tax.total = flt(tax.total + discount_amount_loss, tax.precision("total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530277
Nabin Hait95ff2d42015-03-04 16:02:03 +0530278 self._set_in_company_currency(tax, ["total", "tax_amount_after_discount_amount"])
Anand Doshi731b15e2015-04-05 20:09:09 +0530279
Nabin Haita1bf43b2015-03-17 10:50:47 +0530280 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 Haita1bf43b2015-03-17 10:50:47 +0530283 last_tax = self.doc.get("taxes")[-1]
Nabin Haita6ee8292015-05-15 12:02:01 +0530284 diff = self.doc.total - flt(last_tax.total, self.doc.precision("grand_total"))
Nabin Haita1bf43b2015-03-17 10:50:47 +0530285
286 if diff and abs(diff) <= (2.0 / 10**last_tax.precision("tax_amount")):
Nabin Haita6ee8292015-05-15 12:02:01 +0530287 last_tax.tax_amount += diff
288 last_tax.tax_amount_after_discount_amount += diff
289 last_tax.total += diff
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530290
291 self._set_in_company_currency(last_tax,
Nabin Hait82521172015-05-19 16:29:44 +0530292 ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530293
294 def calculate_totals(self):
Nabin Haite7679702015-02-20 14:40:35 +0530295 self.doc.grand_total = flt(self.doc.get("taxes")[-1].total
296 if self.doc.get("taxes") else self.doc.net_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530297
Anand Doshiec5ec602015-03-05 19:31:23 +0530298 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 Haitbd00e812015-02-17 12:50:51 +0530303 if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
Nabin Haite7679702015-02-20 14:40:35 +0530304 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 Hait3237c752015-02-17 11:11:11 +0530306 else:
Anand Doshiec5ec602015-03-05 19:31:23 +0530307 self.doc.taxes_and_charges_added = self.doc.taxes_and_charges_deducted = 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530308 for tax in self.doc.get("taxes"):
309 if tax.category in ["Valuation and Total", "Total"]:
310 if tax.add_deduct_tax == "Add":
Nabin Haitdb53a782015-07-31 16:53:13 +0530311 self.doc.taxes_and_charges_added += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530312 else:
Nabin Haitdb53a782015-07-31 16:53:13 +0530313 self.doc.taxes_and_charges_deducted += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530314
Nabin Haite7679702015-02-20 14:40:35 +0530315 self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530316
Nabin Haite7679702015-02-20 14:40:35 +0530317 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 Hait3237c752015-02-17 11:11:11 +0530320
Nabin Haite7679702015-02-20 14:40:35 +0530321 self._set_in_company_currency(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530322
Nabin Haite7679702015-02-20 14:40:35 +0530323 self.doc.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530324
Nabin Hait3237c752015-02-17 11:11:11 +0530325 if self.doc.meta.get_field("rounded_total"):
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530326 self.doc.rounded_total = round_based_on_smallest_currency_fraction(self.doc.grand_total,
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530327 self.doc.currency, self.doc.precision("rounded_total"))
Nabin Haite7679702015-02-20 14:40:35 +0530328 if self.doc.meta.get_field("base_rounded_total"):
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +0530329 company_currency = erpnext.get_company_currency(self.doc.company)
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530330
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530331 self.doc.base_rounded_total = \
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530332 round_based_on_smallest_currency_fraction(self.doc.base_grand_total,
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530333 company_currency, self.doc.precision("base_rounded_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530334
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 Doshi15f7b1e2016-04-04 15:03:28 +0530338
Nabin Hait3769d872015-12-18 13:12:02 +0530339 def set_discount_amount(self):
Nabin Haite0405102016-10-13 12:14:32 +0530340 if self.doc.additional_discount_percentage:
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530341 self.doc.discount_amount = flt(flt(self.doc.get(scrub(self.doc.apply_discount_on)))
Nabin Hait3769d872015-12-18 13:12:02 +0530342 * self.doc.additional_discount_percentage / 100, self.doc.precision("discount_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530343
344 def apply_discount_amount(self):
345 if self.doc.discount_amount:
Nabin Hait37b047d2015-02-23 16:01:33 +0530346 if not self.doc.apply_discount_on:
347 frappe.throw(_("Please select Apply Discount On"))
348
Nabin Hait3237c752015-02-17 11:11:11 +0530349 self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate,
350 self.doc.precision("base_discount_amount"))
351
Nabin Haite7679702015-02-20 14:40:35 +0530352 total_for_discount_amount = self.get_total_for_discount_amount()
Nabin Hait25bd84d2015-03-04 15:06:56 +0530353 taxes = self.doc.get("taxes")
354 net_total = 0
Nabin Hait3237c752015-02-17 11:11:11 +0530355
Nabin Haite7679702015-02-20 14:40:35 +0530356 if total_for_discount_amount:
Nabin Hait3237c752015-02-17 11:11:11 +0530357 # calculate item amount after Discount Amount
Nabin Hait25bd84d2015-03-04 15:06:56 +0530358 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 Doshiec5ec602015-03-05 19:31:23 +0530361
Nabin Haite7679702015-02-20 14:40:35 +0530362 item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
Nabin Hait25bd84d2015-03-04 15:06:56 +0530363 net_total += item.net_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530364
Nabin Hait25bd84d2015-03-04 15:06:56 +0530365 # 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 Mehtac6bd7ad2016-12-21 17:30:29 +0530368 discount_amount_loss = flt(self.doc.net_total - net_total - self.doc.discount_amount,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530369 self.doc.precision("net_total"))
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530370
Anand Doshiec5ec602015-03-05 19:31:23 +0530371 item.net_amount = flt(item.net_amount + discount_amount_loss,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530372 item.precision("net_amount"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530373
Nabin Hait51e980d2015-10-10 18:10:05 +0530374 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate")) if item.qty else 0
Anand Doshiec5ec602015-03-05 19:31:23 +0530375
Nabin Haite7679702015-02-20 14:40:35 +0530376 self._set_in_company_currency(item, ["net_rate", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530377
378 self.discount_amount_applied = True
379 self._calculate()
380 else:
381 self.doc.base_discount_amount = 0
382
Nabin Haite7679702015-02-20 14:40:35 +0530383 def get_total_for_discount_amount(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +0530384 if self.doc.apply_discount_on == "Net Total":
385 return self.doc.net_total
Nabin Haite7679702015-02-20 14:40:35 +0530386 else:
387 actual_taxes_dict = {}
Nabin Hait3237c752015-02-17 11:11:11 +0530388
Nabin Haite7679702015-02-20 14:40:35 +0530389 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 Hait3237c752015-02-17 11:11:11 +0530395
Nabin Haite7679702015-02-20 14:40:35 +0530396 return flt(self.doc.grand_total - sum(actual_taxes_dict.values()), self.doc.precision("grand_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530397
398
Nabin Hait7b19b9e2015-02-24 09:42:24 +0530399 def calculate_total_advance(self):
400 if self.doc.docstatus < 2:
Nabin Haite7679702015-02-20 14:40:35 +0530401 total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530402 for adv in self.doc.get("advances")])
403
Nabin Haite7679702015-02-20 14:40:35 +0530404 self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530405
Nabin Hait289ffb72016-02-08 11:06:55 +0530406 if self.doc.party_account_currency == self.doc.currency:
Nabin Hait6f038bc2017-04-03 17:56:05 +0530407 invoice_total = flt(self.doc.grand_total - flt(self.doc.write_off_amount),
Nabin Hait289ffb72016-02-08 11:06:55 +0530408 self.doc.precision("grand_total"))
Nabin Hait8d8cba72017-04-03 17:26:22 +0530409 else:
Nabin Hait6f038bc2017-04-03 17:56:05 +0530410 base_write_off_amount = flt(flt(self.doc.write_off_amount) * self.doc.conversion_rate,
Nabin Hait8d8cba72017-04-03 17:26:22 +0530411 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 Haitadc09232016-02-09 10:31:11 +0530415 if invoice_total > 0 and self.doc.total_advance > invoice_total:
Nabin Hait289ffb72016-02-08 11:06:55 +0530416 frappe.throw(_("Advance amount cannot be greater than {0} {1}")
417 .format(self.doc.party_account_currency, invoice_total))
Nabin Hait3237c752015-02-17 11:11:11 +0530418
Rushabh Mehta8bb6e532015-02-18 20:22:59 +0530419 if self.doc.docstatus == 0:
Nabin Hait3237c752015-02-17 11:11:11 +0530420 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 Waghchaureefb5bf22016-08-25 02:09:53 +0530426 if self.doc.doctype == "Sales Invoice":
427 self.calculate_paid_amount()
428
429 if self.doc.is_return: return
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530430
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530431 self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530432 self._set_in_company_currency(self.doc, ['write_off_amount'])
433
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530434 if self.doc.party_account_currency == self.doc.currency:
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530435 total_amount_to_pay = flt(self.doc.grand_total - self.doc.total_advance
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530436 - flt(self.doc.write_off_amount), self.doc.precision("grand_total"))
437 else:
Nabin Haitd4507ac2016-01-20 16:14:27 +0530438 total_amount_to_pay = flt(flt(self.doc.grand_total *
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530439 self.doc.conversion_rate, self.doc.precision("grand_total")) - self.doc.total_advance
Nabin Haitd4507ac2016-01-20 16:14:27 +0530440 - flt(self.doc.base_write_off_amount), self.doc.precision("grand_total"))
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530441
Nabin Haitbd00e812015-02-17 12:50:51 +0530442 if self.doc.doctype == "Sales Invoice":
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530443 self.doc.round_floats_in(self.doc, ["paid_amount"])
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530444 paid_amount = self.doc.paid_amount \
445 if self.doc.party_account_currency == self.doc.currency else self.doc.base_paid_amount
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530446
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530447 change_amount = self.doc.change_amount \
448 if self.doc.party_account_currency == self.doc.currency else self.doc.base_change_amount
449
Rohit Waghchaure7127a8f2016-08-04 14:56:15 +0530450 self.calculate_write_off_amount()
Nabin Hait3bb1a422016-08-02 16:41:10 +0530451 self.calculate_change_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530452
Nabin Hait3bb1a422016-08-02 16:41:10 +0530453 self.doc.outstanding_amount = flt(total_amount_to_pay - flt(paid_amount) +
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530454 flt(change_amount), self.doc.precision("outstanding_amount"))
455
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530456 elif self.doc.doctype == "Purchase Invoice":
mbauskar36b51892016-01-18 16:31:10 +0530457 self.doc.outstanding_amount = flt(total_amount_to_pay, self.doc.precision("outstanding_amount"))
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530458
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530459 def calculate_paid_amount(self):
460 paid_amount = base_paid_amount = 0.0
Rohit Waghchauref58cad62017-01-17 12:11:57 +0530461
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 Waghchaure6087fe12016-04-09 14:31:09 +0530467
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 Hait3bb1a422016-08-02 16:41:10 +0530471 def calculate_change_amount(self):
472 self.doc.change_amount = 0.0
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530473 self.doc.base_change_amount = 0.0
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530474 if self.doc.paid_amount > self.doc.grand_total:
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530475 self.doc.change_amount = flt(self.doc.paid_amount - self.doc.grand_total +
Nabin Hait3bb1a422016-08-02 16:41:10 +0530476 self.doc.write_off_amount, self.doc.precision("change_amount"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530477
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530478 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"))
mbauskar36b51892016-01-18 16:31:10 +0530480
Rohit Waghchaure7127a8f2016-08-04 14:56:15 +0530481 def calculate_write_off_amount(self):
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530482 if flt(self.doc.change_amount) > 0:
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530483 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 Waghchaurebaef2622016-08-05 15:41:36 +0530485 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 Waghchaure7127a8f2016-08-04 14:56:15 +0530487
mbauskar36b51892016-01-18 16:31:10 +0530488 def calculate_margin(self, item):
489 total_margin = 0.0
490 if item.price_list_rate:
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530491 if item.pricing_rule and not self.doc.ignore_pricing_rule:
mbauskar36b51892016-01-18 16:31:10 +0530492 pricing_rule = frappe.get_doc('Pricing Rule', item.pricing_rule)
mbauskara52472c2016-03-05 15:10:25 +0530493 item.margin_type = pricing_rule.margin_type
494 item.margin_rate_or_amount = pricing_rule.margin_rate_or_amount
mbauskar36b51892016-01-18 16:31:10 +0530495
mbauskara52472c2016-03-05 15:10:25 +0530496 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
mbauskar36b51892016-01-18 16:31:10 +0530498 total_margin = flt(item.price_list_rate) + flt(margin_value)
499
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530500 return total_margin