blob: 362d07515bcf9f2d2b4a127d6b819d1accd6d2a0 [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)
mbauskar287fe812017-04-10 19:15:57 +053063
mbauskardcd8b772016-01-20 12:39:57 +053064 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
mbauskara52472c2016-03-05 15:10:25 +053066
Nabin Haite7679702015-02-20 14:40:35 +053067 item.net_rate = item.rate
68 item.amount = flt(item.rate * item.qty, item.precision("amount"))
69 item.net_amount = item.amount
Nabin Hait3237c752015-02-17 11:11:11 +053070
Nabin Haite7679702015-02-20 14:40:35 +053071 self._set_in_company_currency(item, ["price_list_rate", "rate", "net_rate", "amount", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +053072
Nabin Haite7679702015-02-20 14:40:35 +053073 item.item_tax_amount = 0.0
74
75 def _set_in_company_currency(self, doc, fields):
Nabin Hait3237c752015-02-17 11:11:11 +053076 """set values in base currency"""
Nabin Haite7679702015-02-20 14:40:35 +053077 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 Hait3237c752015-02-17 11:11:11 +053080
81 def initialize_taxes(self):
82 for tax in self.doc.get("taxes"):
Nabin Hait86cd4cc2015-02-28 19:11:51 +053083 if not self.discount_amount_applied:
84 validate_taxes_and_charges(tax)
85 validate_inclusive_tax(tax, self.doc)
Nabin Hait613d0812015-02-23 11:58:15 +053086
Nabin Hait3237c752015-02-17 11:11:11 +053087 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 Haitde9c8a92015-02-23 01:06:00 +053092 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 Hait3237c752015-02-17 11:11:11 +053095
96 for fieldname in tax_fields:
97 tax.set(fieldname, 0.0)
98
Nabin Hait3237c752015-02-17 11:11:11 +053099 self.doc.round_floats_in(tax)
100
Nabin Hait3237c752015-02-17 11:11:11 +0530101 def determine_exclusive_rate(self):
Nabin Hait37b047d2015-02-23 16:01:33 +0530102 if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))):
103 return
Nabin Hait3237c752015-02-17 11:11:11 +0530104
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 Haite7679702015-02-20 14:40:35 +0530121 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 Hait3237c752015-02-17 11:11:11 +0530124
Nabin Haite7679702015-02-20 14:40:35 +0530125 self._set_in_company_currency(item, ["net_rate", "net_amount"])
126
Nabin Hait3237c752015-02-17 11:11:11 +0530127 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 Haita6ee8292015-05-15 12:02:01 +0530151 if getattr(tax, "add_deduct_tax", None):
152 current_tax_fraction *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
Nabin Hait3237c752015-02-17 11:11:11 +0530153 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 Haitf0bc9b62015-02-23 01:40:01 +0530162 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 +0530163
164 for item in self.doc.get("items"):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530165 self.doc.total += item.amount
166 self.doc.base_total += item.base_amount
Nabin Haite7679702015-02-20 14:40:35 +0530167 self.doc.net_total += item.net_amount
168 self.doc.base_net_total += item.base_net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530169
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530170 self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530171
172 def calculate_taxes(self):
173 # maintain actual tax rate based on idx
Nabin Haite7679702015-02-20 14:40:35 +0530174 actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
Nabin Hait3237c752015-02-17 11:11:11 +0530175 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 Hait2b019ed2015-02-22 23:03:07 +0530190 # accumulate tax amount into tax.tax_amount
Nabin Haitde9c8a92015-02-23 01:06:00 +0530191 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 Hait2b019ed2015-02-22 23:03:07 +0530194
Nabin Hait3237c752015-02-17 11:11:11 +0530195 # 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 Hait2b019ed2015-02-22 23:03:07 +0530199 # set tax after discount
Nabin Hait3237c752015-02-17 11:11:11 +0530200 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 Hait93b3a372015-02-24 17:08:34 +0530214 tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount, tax.precision("total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530215 else:
216 tax.grand_total_for_current_item = \
Rushabh Mehta6cdfd1e2015-02-25 15:55:57 +0530217 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 +0530218
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 Doshiec5ec602015-03-05 19:31:23 +0530225
Nabin Hait3237c752015-02-17 11:11:11 +0530226 # adjust Discount Amount loss in last tax iteration
Nabin Haitde9c8a92015-02-23 01:06:00 +0530227 if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
Nabin Haitdb53a782015-07-31 16:53:13 +0530228 and self.doc.discount_amount and self.doc.apply_discount_on == "Grand Total":
Nabin Haitde9c8a92015-02-23 01:06:00 +0530229 self.adjust_discount_amount_loss(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530230
231
Nabin Hait3237c752015-02-17 11:11:11 +0530232
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 Haite7679702015-02-20 14:40:35 +0530239 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 Hait3237c752015-02-17 11:11:11 +0530242 elif tax.charge_type == "On Net Total":
Nabin Haite7679702015-02-20 14:40:35 +0530243 current_tax_amount = (tax_rate / 100.0) * item.net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530244 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 Hait93b3a372015-02-24 17:08:34 +0530251 current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530252
Nabin Haite7679702015-02-20 14:40:35 +0530253 self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530254
255 return current_tax_amount
256
Nabin Haite7679702015-02-20 14:40:35 +0530257 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 Hait3237c752015-02-17 11:11:11 +0530266 def round_off_totals(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530267 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 Hait3237c752015-02-17 11:11:11 +0530270
Nabin Haitde9c8a92015-02-23 01:06:00 +0530271 self._set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Haitce245122015-02-22 20:14:49 +0530272
Nabin Hait3237c752015-02-17 11:11:11 +0530273 def adjust_discount_amount_loss(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530274 discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
Nabin Hait3237c752015-02-17 11:11:11 +0530275 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Nabin Hait95ff2d42015-03-04 16:02:03 +0530276 discount_amount_loss, tax.precision("tax_amount"))
277 tax.total = flt(tax.total + discount_amount_loss, tax.precision("total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530278
Nabin Hait95ff2d42015-03-04 16:02:03 +0530279 self._set_in_company_currency(tax, ["total", "tax_amount_after_discount_amount"])
Anand Doshi731b15e2015-04-05 20:09:09 +0530280
Nabin Haita1bf43b2015-03-17 10:50:47 +0530281 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 Haita1bf43b2015-03-17 10:50:47 +0530284 last_tax = self.doc.get("taxes")[-1]
Nabin Haita6ee8292015-05-15 12:02:01 +0530285 diff = self.doc.total - flt(last_tax.total, self.doc.precision("grand_total"))
Nabin Haita1bf43b2015-03-17 10:50:47 +0530286
287 if diff and abs(diff) <= (2.0 / 10**last_tax.precision("tax_amount")):
Nabin Haita6ee8292015-05-15 12:02:01 +0530288 last_tax.tax_amount += diff
289 last_tax.tax_amount_after_discount_amount += diff
290 last_tax.total += diff
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530291
292 self._set_in_company_currency(last_tax,
Nabin Hait82521172015-05-19 16:29:44 +0530293 ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530294
295 def calculate_totals(self):
Nabin Haite7679702015-02-20 14:40:35 +0530296 self.doc.grand_total = flt(self.doc.get("taxes")[-1].total
297 if self.doc.get("taxes") else self.doc.net_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530298
Anand Doshiec5ec602015-03-05 19:31:23 +0530299 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 Haitbd00e812015-02-17 12:50:51 +0530304 if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
Nabin Haite7679702015-02-20 14:40:35 +0530305 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 Hait3237c752015-02-17 11:11:11 +0530307 else:
Anand Doshiec5ec602015-03-05 19:31:23 +0530308 self.doc.taxes_and_charges_added = self.doc.taxes_and_charges_deducted = 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530309 for tax in self.doc.get("taxes"):
310 if tax.category in ["Valuation and Total", "Total"]:
311 if tax.add_deduct_tax == "Add":
Nabin Haitdb53a782015-07-31 16:53:13 +0530312 self.doc.taxes_and_charges_added += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530313 else:
Nabin Haitdb53a782015-07-31 16:53:13 +0530314 self.doc.taxes_and_charges_deducted += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530315
Nabin Haite7679702015-02-20 14:40:35 +0530316 self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530317
Nabin Haite7679702015-02-20 14:40:35 +0530318 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 Hait3237c752015-02-17 11:11:11 +0530321
Nabin Haite7679702015-02-20 14:40:35 +0530322 self._set_in_company_currency(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530323
Nabin Haite7679702015-02-20 14:40:35 +0530324 self.doc.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530325
Nabin Hait3237c752015-02-17 11:11:11 +0530326 if self.doc.meta.get_field("rounded_total"):
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530327 self.doc.rounded_total = round_based_on_smallest_currency_fraction(self.doc.grand_total,
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530328 self.doc.currency, self.doc.precision("rounded_total"))
Nabin Haite7679702015-02-20 14:40:35 +0530329 if self.doc.meta.get_field("base_rounded_total"):
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +0530330 company_currency = erpnext.get_company_currency(self.doc.company)
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530331
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530332 self.doc.base_rounded_total = \
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530333 round_based_on_smallest_currency_fraction(self.doc.base_grand_total,
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530334 company_currency, self.doc.precision("base_rounded_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530335
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 Doshi15f7b1e2016-04-04 15:03:28 +0530339
Nabin Hait3769d872015-12-18 13:12:02 +0530340 def set_discount_amount(self):
Nabin Haite0405102016-10-13 12:14:32 +0530341 if self.doc.additional_discount_percentage:
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530342 self.doc.discount_amount = flt(flt(self.doc.get(scrub(self.doc.apply_discount_on)))
Nabin Hait3769d872015-12-18 13:12:02 +0530343 * self.doc.additional_discount_percentage / 100, self.doc.precision("discount_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530344
345 def apply_discount_amount(self):
346 if self.doc.discount_amount:
Nabin Hait37b047d2015-02-23 16:01:33 +0530347 if not self.doc.apply_discount_on:
348 frappe.throw(_("Please select Apply Discount On"))
349
Nabin Hait3237c752015-02-17 11:11:11 +0530350 self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate,
351 self.doc.precision("base_discount_amount"))
352
Nabin Haite7679702015-02-20 14:40:35 +0530353 total_for_discount_amount = self.get_total_for_discount_amount()
Nabin Hait25bd84d2015-03-04 15:06:56 +0530354 taxes = self.doc.get("taxes")
355 net_total = 0
Nabin Hait3237c752015-02-17 11:11:11 +0530356
Nabin Haite7679702015-02-20 14:40:35 +0530357 if total_for_discount_amount:
Nabin Hait3237c752015-02-17 11:11:11 +0530358 # calculate item amount after Discount Amount
Nabin Hait25bd84d2015-03-04 15:06:56 +0530359 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 Doshiec5ec602015-03-05 19:31:23 +0530362
Nabin Haite7679702015-02-20 14:40:35 +0530363 item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
Nabin Hait25bd84d2015-03-04 15:06:56 +0530364 net_total += item.net_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530365
Nabin Hait25bd84d2015-03-04 15:06:56 +0530366 # 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:
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530369 discount_amount_loss = flt(self.doc.net_total - net_total - self.doc.discount_amount,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530370 self.doc.precision("net_total"))
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530371
Anand Doshiec5ec602015-03-05 19:31:23 +0530372 item.net_amount = flt(item.net_amount + discount_amount_loss,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530373 item.precision("net_amount"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530374
Nabin Hait51e980d2015-10-10 18:10:05 +0530375 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 +0530376
Nabin Haite7679702015-02-20 14:40:35 +0530377 self._set_in_company_currency(item, ["net_rate", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530378
379 self.discount_amount_applied = True
380 self._calculate()
381 else:
382 self.doc.base_discount_amount = 0
383
Nabin Haite7679702015-02-20 14:40:35 +0530384 def get_total_for_discount_amount(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +0530385 if self.doc.apply_discount_on == "Net Total":
386 return self.doc.net_total
Nabin Haite7679702015-02-20 14:40:35 +0530387 else:
388 actual_taxes_dict = {}
Nabin Hait3237c752015-02-17 11:11:11 +0530389
Nabin Haite7679702015-02-20 14:40:35 +0530390 for tax in self.doc.get("taxes"):
391 if tax.charge_type == "Actual":
392 actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
393 elif tax.row_id in actual_taxes_dict:
394 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * flt(tax.rate) / 100
395 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530396
Nabin Haite7679702015-02-20 14:40:35 +0530397 return flt(self.doc.grand_total - sum(actual_taxes_dict.values()), self.doc.precision("grand_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530398
399
Nabin Hait7b19b9e2015-02-24 09:42:24 +0530400 def calculate_total_advance(self):
401 if self.doc.docstatus < 2:
Nabin Haite7679702015-02-20 14:40:35 +0530402 total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530403 for adv in self.doc.get("advances")])
404
Nabin Haite7679702015-02-20 14:40:35 +0530405 self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530406
Nabin Hait289ffb72016-02-08 11:06:55 +0530407 if self.doc.party_account_currency == self.doc.currency:
Nabin Hait6f038bc2017-04-03 17:56:05 +0530408 invoice_total = flt(self.doc.grand_total - flt(self.doc.write_off_amount),
Nabin Hait289ffb72016-02-08 11:06:55 +0530409 self.doc.precision("grand_total"))
Nabin Hait8d8cba72017-04-03 17:26:22 +0530410 else:
Nabin Hait6f038bc2017-04-03 17:56:05 +0530411 base_write_off_amount = flt(flt(self.doc.write_off_amount) * self.doc.conversion_rate,
Nabin Hait8d8cba72017-04-03 17:26:22 +0530412 self.doc.precision("base_write_off_amount"))
413 invoice_total = flt(self.doc.grand_total * self.doc.conversion_rate,
414 self.doc.precision("grand_total")) - base_write_off_amount
415
Nabin Haitadc09232016-02-09 10:31:11 +0530416 if invoice_total > 0 and self.doc.total_advance > invoice_total:
Nabin Hait289ffb72016-02-08 11:06:55 +0530417 frappe.throw(_("Advance amount cannot be greater than {0} {1}")
418 .format(self.doc.party_account_currency, invoice_total))
Nabin Hait3237c752015-02-17 11:11:11 +0530419
Rushabh Mehta8bb6e532015-02-18 20:22:59 +0530420 if self.doc.docstatus == 0:
Nabin Hait3237c752015-02-17 11:11:11 +0530421 self.calculate_outstanding_amount()
422
423 def calculate_outstanding_amount(self):
424 # NOTE:
425 # write_off_amount is only for POS Invoice
426 # total_advance is only for non POS Invoice
Rohit Waghchaureefb5bf22016-08-25 02:09:53 +0530427 if self.doc.doctype == "Sales Invoice":
428 self.calculate_paid_amount()
429
430 if self.doc.is_return: return
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530431
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530432 self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530433 self._set_in_company_currency(self.doc, ['write_off_amount'])
434
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530435 if self.doc.party_account_currency == self.doc.currency:
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530436 total_amount_to_pay = flt(self.doc.grand_total - self.doc.total_advance
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530437 - flt(self.doc.write_off_amount), self.doc.precision("grand_total"))
438 else:
Nabin Haitd4507ac2016-01-20 16:14:27 +0530439 total_amount_to_pay = flt(flt(self.doc.grand_total *
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530440 self.doc.conversion_rate, self.doc.precision("grand_total")) - self.doc.total_advance
Nabin Haitd4507ac2016-01-20 16:14:27 +0530441 - flt(self.doc.base_write_off_amount), self.doc.precision("grand_total"))
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530442
Nabin Haitbd00e812015-02-17 12:50:51 +0530443 if self.doc.doctype == "Sales Invoice":
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530444 self.doc.round_floats_in(self.doc, ["paid_amount"])
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530445 paid_amount = self.doc.paid_amount \
446 if self.doc.party_account_currency == self.doc.currency else self.doc.base_paid_amount
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530447
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530448 change_amount = self.doc.change_amount \
449 if self.doc.party_account_currency == self.doc.currency else self.doc.base_change_amount
450
Rohit Waghchaure7127a8f2016-08-04 14:56:15 +0530451 self.calculate_write_off_amount()
Nabin Hait3bb1a422016-08-02 16:41:10 +0530452 self.calculate_change_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530453
Nabin Hait3bb1a422016-08-02 16:41:10 +0530454 self.doc.outstanding_amount = flt(total_amount_to_pay - flt(paid_amount) +
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530455 flt(change_amount), self.doc.precision("outstanding_amount"))
456
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530457 elif self.doc.doctype == "Purchase Invoice":
mbauskar36b51892016-01-18 16:31:10 +0530458 self.doc.outstanding_amount = flt(total_amount_to_pay, self.doc.precision("outstanding_amount"))
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530459
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530460 def calculate_paid_amount(self):
461 paid_amount = base_paid_amount = 0.0
Rohit Waghchauref58cad62017-01-17 12:11:57 +0530462
463 if self.doc.is_pos:
464 for payment in self.doc.get('payments'):
465 payment.base_amount = flt(payment.amount * self.doc.conversion_rate)
466 paid_amount += payment.amount
467 base_paid_amount += payment.base_amount
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530468
469 self.doc.paid_amount = flt(paid_amount, self.doc.precision("paid_amount"))
470 self.doc.base_paid_amount = flt(base_paid_amount, self.doc.precision("base_paid_amount"))
471
Nabin Hait3bb1a422016-08-02 16:41:10 +0530472 def calculate_change_amount(self):
473 self.doc.change_amount = 0.0
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530474 self.doc.base_change_amount = 0.0
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530475 if self.doc.paid_amount > self.doc.grand_total:
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530476 self.doc.change_amount = flt(self.doc.paid_amount - self.doc.grand_total +
Nabin Hait3bb1a422016-08-02 16:41:10 +0530477 self.doc.write_off_amount, self.doc.precision("change_amount"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530478
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530479 self.doc.base_change_amount = flt(self.doc.base_paid_amount - self.doc.base_grand_total +
480 self.doc.base_write_off_amount, self.doc.precision("base_change_amount"))
mbauskar36b51892016-01-18 16:31:10 +0530481
Rohit Waghchaure7127a8f2016-08-04 14:56:15 +0530482 def calculate_write_off_amount(self):
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530483 if flt(self.doc.change_amount) > 0:
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530484 self.doc.write_off_amount = flt(self.doc.grand_total - self.doc.paid_amount + self.doc.change_amount,
485 self.doc.precision("write_off_amount"))
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530486 self.doc.base_write_off_amount = flt(self.doc.write_off_amount * self.doc.conversion_rate,
487 self.doc.precision("base_write_off_amount"))
Rohit Waghchaure7127a8f2016-08-04 14:56:15 +0530488
mbauskar36b51892016-01-18 16:31:10 +0530489 def calculate_margin(self, item):
490 total_margin = 0.0
491 if item.price_list_rate:
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530492 if item.pricing_rule and not self.doc.ignore_pricing_rule:
mbauskar36b51892016-01-18 16:31:10 +0530493 pricing_rule = frappe.get_doc('Pricing Rule', item.pricing_rule)
mbauskara52472c2016-03-05 15:10:25 +0530494 item.margin_type = pricing_rule.margin_type
495 item.margin_rate_or_amount = pricing_rule.margin_rate_or_amount
mbauskar36b51892016-01-18 16:31:10 +0530496
mbauskara52472c2016-03-05 15:10:25 +0530497 if item.margin_type and item.margin_rate_or_amount:
498 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 +0530499 total_margin = flt(item.price_list_rate) + flt(margin_value)
500
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530501 return total_margin