blob: d4a64f2e7f0049259f31a478906a1196b12076f0 [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
Nabin Hait37b047d2015-02-23 16:01:33 +05306import frappe
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 Hait3237c752015-02-17 11:11:11 +05309from erpnext.setup.utils import get_company_currency
Nabin Hait613d0812015-02-23 11:58:15 +053010from erpnext.controllers.accounts_controller import validate_conversion_rate, \
11 validate_taxes_and_charges, validate_inclusive_tax
Nabin Hait3237c752015-02-17 11:11:11 +053012
Nabin Haitfe81da22015-02-18 12:23:18 +053013class calculate_taxes_and_totals(object):
Nabin Hait3237c752015-02-17 11:11:11 +053014 def __init__(self, doc):
15 self.doc = doc
Nabin Haitfe81da22015-02-18 12:23:18 +053016 self.calculate()
17
Nabin Hait3237c752015-02-17 11:11:11 +053018 def calculate(self):
19 self.discount_amount_applied = False
20 self._calculate()
21
22 if self.doc.meta.get_field("discount_amount"):
Nabin Hait3769d872015-12-18 13:12:02 +053023 self.set_discount_amount()
Nabin Hait3237c752015-02-17 11:11:11 +053024 self.apply_discount_amount()
25
Nabin Haitbd00e812015-02-17 12:50:51 +053026 if self.doc.doctype in ["Sales Invoice", "Purchase Invoice"]:
Nabin Hait3237c752015-02-17 11:11:11 +053027 self.calculate_total_advance()
28
29 def _calculate(self):
Nabin Haite7679702015-02-20 14:40:35 +053030 self.calculate_item_values()
31 self.initialize_taxes()
32 self.determine_exclusive_rate()
33 self.calculate_net_total()
34 self.calculate_taxes()
Nabin Haita1bf43b2015-03-17 10:50:47 +053035 self.manipulate_grand_total_for_inclusive_tax()
Nabin Haite7679702015-02-20 14:40:35 +053036 self.calculate_totals()
37 self._cleanup()
38
39 def validate_conversion_rate(self):
Nabin Hait3237c752015-02-17 11:11:11 +053040 # 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 Hait3237c752015-02-17 11:11:11 +053051 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 Haite7679702015-02-20 14:40:35 +053059 item.rate = flt(item.price_list_rate *
60 (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
Nabin Hait3237c752015-02-17 11:11:11 +053061
Nabin Haite7679702015-02-20 14:40:35 +053062 item.net_rate = item.rate
63 item.amount = flt(item.rate * item.qty, item.precision("amount"))
64 item.net_amount = item.amount
Nabin Hait3237c752015-02-17 11:11:11 +053065
Nabin Haite7679702015-02-20 14:40:35 +053066 self._set_in_company_currency(item, ["price_list_rate", "rate", "net_rate", "amount", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +053067
Nabin Haite7679702015-02-20 14:40:35 +053068 item.item_tax_amount = 0.0
69
70 def _set_in_company_currency(self, doc, fields):
Nabin Hait3237c752015-02-17 11:11:11 +053071 """set values in base currency"""
Nabin Haite7679702015-02-20 14:40:35 +053072 for f in fields:
73 val = flt(flt(doc.get(f), doc.precision(f)) * self.doc.conversion_rate, doc.precision("base_" + f))
74 doc.set("base_" + f, val)
Nabin Hait3237c752015-02-17 11:11:11 +053075
76 def initialize_taxes(self):
77 for tax in self.doc.get("taxes"):
Nabin Hait86cd4cc2015-02-28 19:11:51 +053078 if not self.discount_amount_applied:
79 validate_taxes_and_charges(tax)
80 validate_inclusive_tax(tax, self.doc)
Nabin Hait613d0812015-02-23 11:58:15 +053081
Nabin Hait3237c752015-02-17 11:11:11 +053082 tax.item_wise_tax_detail = {}
83 tax_fields = ["total", "tax_amount_after_discount_amount",
84 "tax_amount_for_current_item", "grand_total_for_current_item",
85 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
86
Nabin Haitde9c8a92015-02-23 01:06:00 +053087 if tax.charge_type != "Actual" and \
88 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
89 tax_fields.append("tax_amount")
Nabin Hait3237c752015-02-17 11:11:11 +053090
91 for fieldname in tax_fields:
92 tax.set(fieldname, 0.0)
93
Nabin Hait3237c752015-02-17 11:11:11 +053094 self.doc.round_floats_in(tax)
95
Nabin Hait3237c752015-02-17 11:11:11 +053096 def determine_exclusive_rate(self):
Nabin Hait37b047d2015-02-23 16:01:33 +053097 if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))):
98 return
Nabin Hait3237c752015-02-17 11:11:11 +053099
100 for item in self.doc.get("items"):
101 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
102 cumulated_tax_fraction = 0
103 for i, tax in enumerate(self.doc.get("taxes")):
104 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
105
106 if i==0:
107 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
108 else:
109 tax.grand_total_fraction_for_current_item = \
110 self.doc.get("taxes")[i-1].grand_total_fraction_for_current_item \
111 + tax.tax_fraction_for_current_item
112
113 cumulated_tax_fraction += tax.tax_fraction_for_current_item
114
115 if cumulated_tax_fraction and not self.discount_amount_applied and item.qty:
Nabin Haite7679702015-02-20 14:40:35 +0530116 item.net_amount = flt(item.amount / (1 + cumulated_tax_fraction), item.precision("net_amount"))
117 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
118 item.discount_percentage = flt(item.discount_percentage, item.precision("discount_percentage"))
Nabin Hait3237c752015-02-17 11:11:11 +0530119
Nabin Haite7679702015-02-20 14:40:35 +0530120 self._set_in_company_currency(item, ["net_rate", "net_amount"])
121
Nabin Hait3237c752015-02-17 11:11:11 +0530122 def _load_item_tax_rate(self, item_tax_rate):
123 return json.loads(item_tax_rate) if item_tax_rate else {}
124
125 def get_current_tax_fraction(self, tax, item_tax_map):
126 """
127 Get tax fraction for calculating tax exclusive amount
128 from tax inclusive amount
129 """
130 current_tax_fraction = 0
131
132 if cint(tax.included_in_print_rate):
133 tax_rate = self._get_tax_rate(tax, item_tax_map)
134
135 if tax.charge_type == "On Net Total":
136 current_tax_fraction = tax_rate / 100.0
137
138 elif tax.charge_type == "On Previous Row Amount":
139 current_tax_fraction = (tax_rate / 100.0) * \
140 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item
141
142 elif tax.charge_type == "On Previous Row Total":
143 current_tax_fraction = (tax_rate / 100.0) * \
144 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
145
Nabin Haita6ee8292015-05-15 12:02:01 +0530146 if getattr(tax, "add_deduct_tax", None):
147 current_tax_fraction *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
Nabin Hait3237c752015-02-17 11:11:11 +0530148 return current_tax_fraction
149
150 def _get_tax_rate(self, tax, item_tax_map):
151 if item_tax_map.has_key(tax.account_head):
152 return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax))
153 else:
154 return tax.rate
155
156 def calculate_net_total(self):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530157 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 +0530158
159 for item in self.doc.get("items"):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530160 self.doc.total += item.amount
161 self.doc.base_total += item.base_amount
Nabin Haite7679702015-02-20 14:40:35 +0530162 self.doc.net_total += item.net_amount
163 self.doc.base_net_total += item.base_net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530164
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530165 self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530166
167 def calculate_taxes(self):
168 # maintain actual tax rate based on idx
Nabin Haite7679702015-02-20 14:40:35 +0530169 actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
Nabin Hait3237c752015-02-17 11:11:11 +0530170 for tax in self.doc.get("taxes") if tax.charge_type == "Actual"])
171
172 for n, item in enumerate(self.doc.get("items")):
173 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
174
175 for i, tax in enumerate(self.doc.get("taxes")):
176 # tax_amount represents the amount of tax for the current step
177 current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
178
179 # Adjust divisional loss to the last item
180 if tax.charge_type == "Actual":
181 actual_tax_dict[tax.idx] -= current_tax_amount
182 if n == len(self.doc.get("items")) - 1:
183 current_tax_amount += actual_tax_dict[tax.idx]
184
Nabin Hait2b019ed2015-02-22 23:03:07 +0530185 # accumulate tax amount into tax.tax_amount
Nabin Haitde9c8a92015-02-23 01:06:00 +0530186 if tax.charge_type != "Actual" and \
187 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
188 tax.tax_amount += current_tax_amount
Nabin Hait2b019ed2015-02-22 23:03:07 +0530189
Nabin Hait3237c752015-02-17 11:11:11 +0530190 # store tax_amount for current item as it will be used for
191 # charge type = 'On Previous Row Amount'
192 tax.tax_amount_for_current_item = current_tax_amount
193
Nabin Hait2b019ed2015-02-22 23:03:07 +0530194 # set tax after discount
Nabin Hait3237c752015-02-17 11:11:11 +0530195 tax.tax_amount_after_discount_amount += current_tax_amount
196
197 if getattr(tax, "category", None):
198 # if just for valuation, do not add the tax amount in total
199 # hence, setting it as 0 for further steps
200 current_tax_amount = 0.0 if (tax.category == "Valuation") \
201 else current_tax_amount
202
203 current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
204
205 # Calculate tax.total viz. grand total till that step
206 # note: grand_total_for_current_item contains the contribution of
207 # item's amount, previously applied tax and the current tax on that item
208 if i==0:
Nabin Hait93b3a372015-02-24 17:08:34 +0530209 tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount, tax.precision("total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530210 else:
211 tax.grand_total_for_current_item = \
Rushabh Mehta6cdfd1e2015-02-25 15:55:57 +0530212 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 +0530213
214 # in tax.total, accumulate grand total of each item
215 tax.total += tax.grand_total_for_current_item
216
217 # set precision in the last item iteration
218 if n == len(self.doc.get("items")) - 1:
219 self.round_off_totals(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530220
Nabin Hait3237c752015-02-17 11:11:11 +0530221 # adjust Discount Amount loss in last tax iteration
Nabin Haitde9c8a92015-02-23 01:06:00 +0530222 if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
Nabin Haitdb53a782015-07-31 16:53:13 +0530223 and self.doc.discount_amount and self.doc.apply_discount_on == "Grand Total":
Nabin Haitde9c8a92015-02-23 01:06:00 +0530224 self.adjust_discount_amount_loss(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530225
226
Nabin Hait3237c752015-02-17 11:11:11 +0530227
228 def get_current_tax_amount(self, item, tax, item_tax_map):
229 tax_rate = self._get_tax_rate(tax, item_tax_map)
230 current_tax_amount = 0.0
231
232 if tax.charge_type == "Actual":
233 # distribute the tax amount proportionally to each item row
Nabin Haite7679702015-02-20 14:40:35 +0530234 actual = flt(tax.tax_amount, tax.precision("tax_amount"))
235 current_tax_amount = item.net_amount*actual / self.doc.net_total if self.doc.net_total else 0.0
236
Nabin Hait3237c752015-02-17 11:11:11 +0530237 elif tax.charge_type == "On Net Total":
Nabin Haite7679702015-02-20 14:40:35 +0530238 current_tax_amount = (tax_rate / 100.0) * item.net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530239 elif tax.charge_type == "On Previous Row Amount":
240 current_tax_amount = (tax_rate / 100.0) * \
241 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item
242 elif tax.charge_type == "On Previous Row Total":
243 current_tax_amount = (tax_rate / 100.0) * \
244 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item
245
Nabin Hait93b3a372015-02-24 17:08:34 +0530246 current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530247
Nabin Haite7679702015-02-20 14:40:35 +0530248 self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530249
250 return current_tax_amount
251
Nabin Haite7679702015-02-20 14:40:35 +0530252 def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount):
253 # store tax breakup for each item
254 key = item.item_code or item.item_name
255 item_wise_tax_amount = current_tax_amount*self.doc.conversion_rate
256 if tax.item_wise_tax_detail.get(key):
257 item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
258
259 tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount, tax.precision("base_tax_amount"))]
260
Nabin Hait3237c752015-02-17 11:11:11 +0530261 def round_off_totals(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530262 tax.total = flt(tax.total, tax.precision("total"))
263 tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
264 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530265
Nabin Haitde9c8a92015-02-23 01:06:00 +0530266 self._set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Haitce245122015-02-22 20:14:49 +0530267
Nabin Hait3237c752015-02-17 11:11:11 +0530268 def adjust_discount_amount_loss(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530269 discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
Nabin Hait3237c752015-02-17 11:11:11 +0530270 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Nabin Hait95ff2d42015-03-04 16:02:03 +0530271 discount_amount_loss, tax.precision("tax_amount"))
272 tax.total = flt(tax.total + discount_amount_loss, tax.precision("total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530273
Nabin Hait95ff2d42015-03-04 16:02:03 +0530274 self._set_in_company_currency(tax, ["total", "tax_amount_after_discount_amount"])
Anand Doshi731b15e2015-04-05 20:09:09 +0530275
Nabin Haita1bf43b2015-03-17 10:50:47 +0530276 def manipulate_grand_total_for_inclusive_tax(self):
277 # if fully inclusive taxes and diff
278 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 +0530279 last_tax = self.doc.get("taxes")[-1]
Nabin Haita6ee8292015-05-15 12:02:01 +0530280 diff = self.doc.total - flt(last_tax.total, self.doc.precision("grand_total"))
Nabin Haita1bf43b2015-03-17 10:50:47 +0530281
282 if diff and abs(diff) <= (2.0 / 10**last_tax.precision("tax_amount")):
Nabin Haita6ee8292015-05-15 12:02:01 +0530283 last_tax.tax_amount += diff
284 last_tax.tax_amount_after_discount_amount += diff
285 last_tax.total += diff
Nabin Hait82521172015-05-19 16:29:44 +0530286
287 self._set_in_company_currency(last_tax,
288 ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530289
290 def calculate_totals(self):
Nabin Haite7679702015-02-20 14:40:35 +0530291 self.doc.grand_total = flt(self.doc.get("taxes")[-1].total
292 if self.doc.get("taxes") else self.doc.net_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530293
Anand Doshiec5ec602015-03-05 19:31:23 +0530294 self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total,
295 self.doc.precision("total_taxes_and_charges"))
296
297 self._set_in_company_currency(self.doc, ["total_taxes_and_charges"])
298
Nabin Haitbd00e812015-02-17 12:50:51 +0530299 if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
Nabin Haite7679702015-02-20 14:40:35 +0530300 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
301 if self.doc.total_taxes_and_charges else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530302 else:
Anand Doshiec5ec602015-03-05 19:31:23 +0530303 self.doc.taxes_and_charges_added = self.doc.taxes_and_charges_deducted = 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530304 for tax in self.doc.get("taxes"):
305 if tax.category in ["Valuation and Total", "Total"]:
306 if tax.add_deduct_tax == "Add":
Nabin Haitdb53a782015-07-31 16:53:13 +0530307 self.doc.taxes_and_charges_added += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530308 else:
Nabin Haitdb53a782015-07-31 16:53:13 +0530309 self.doc.taxes_and_charges_deducted += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530310
Nabin Haite7679702015-02-20 14:40:35 +0530311 self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530312
Nabin Haite7679702015-02-20 14:40:35 +0530313 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
314 if (self.doc.taxes_and_charges_added or self.doc.taxes_and_charges_deducted) \
315 else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530316
Nabin Haite7679702015-02-20 14:40:35 +0530317 self._set_in_company_currency(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530318
Nabin Haite7679702015-02-20 14:40:35 +0530319 self.doc.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530320
Nabin Hait3237c752015-02-17 11:11:11 +0530321 if self.doc.meta.get_field("rounded_total"):
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530322 self.doc.rounded_total = round_based_on_smallest_currency_fraction(self.doc.grand_total,
323 self.doc.currency, self.doc.precision("rounded_total"))
Nabin Haite7679702015-02-20 14:40:35 +0530324 if self.doc.meta.get_field("base_rounded_total"):
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530325 company_currency = get_company_currency(self.doc.company)
326
327 self.doc.base_rounded_total = \
328 round_based_on_smallest_currency_fraction(self.doc.base_grand_total,
329 company_currency, self.doc.precision("base_rounded_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530330
331 def _cleanup(self):
332 for tax in self.doc.get("taxes"):
333 tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':'))
Nabin Hait3769d872015-12-18 13:12:02 +0530334
335 def set_discount_amount(self):
336 if not self.doc.discount_amount and self.doc.additional_discount_percentage:
337 self.doc.discount_amount = flt(flt(self.doc.get(scrub(self.doc.apply_discount_on)))
338 * self.doc.additional_discount_percentage / 100, self.doc.precision("discount_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530339
340 def apply_discount_amount(self):
341 if self.doc.discount_amount:
Nabin Hait37b047d2015-02-23 16:01:33 +0530342 if not self.doc.apply_discount_on:
343 frappe.throw(_("Please select Apply Discount On"))
344
Nabin Hait3237c752015-02-17 11:11:11 +0530345 self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate,
346 self.doc.precision("base_discount_amount"))
347
Nabin Haite7679702015-02-20 14:40:35 +0530348 total_for_discount_amount = self.get_total_for_discount_amount()
Nabin Hait25bd84d2015-03-04 15:06:56 +0530349 taxes = self.doc.get("taxes")
350 net_total = 0
Nabin Hait3237c752015-02-17 11:11:11 +0530351
Nabin Haite7679702015-02-20 14:40:35 +0530352 if total_for_discount_amount:
Nabin Hait3237c752015-02-17 11:11:11 +0530353 # calculate item amount after Discount Amount
Nabin Hait25bd84d2015-03-04 15:06:56 +0530354 for i, item in enumerate(self.doc.get("items")):
355 distributed_amount = flt(self.doc.discount_amount) * \
356 item.net_amount / total_for_discount_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530357
Nabin Haite7679702015-02-20 14:40:35 +0530358 item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
Nabin Hait25bd84d2015-03-04 15:06:56 +0530359 net_total += item.net_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530360
Nabin Hait25bd84d2015-03-04 15:06:56 +0530361 # discount amount rounding loss adjustment if no taxes
362 if (not taxes or self.doc.apply_discount_on == "Net Total") \
363 and i == len(self.doc.get("items")) - 1:
Anand Doshiec5ec602015-03-05 19:31:23 +0530364 discount_amount_loss = flt(self.doc.total - net_total - self.doc.discount_amount,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530365 self.doc.precision("net_total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530366 item.net_amount = flt(item.net_amount + discount_amount_loss,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530367 item.precision("net_amount"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530368
Nabin Hait51e980d2015-10-10 18:10:05 +0530369 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 +0530370
Nabin Haite7679702015-02-20 14:40:35 +0530371 self._set_in_company_currency(item, ["net_rate", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530372
373 self.discount_amount_applied = True
374 self._calculate()
375 else:
376 self.doc.base_discount_amount = 0
377
Nabin Haite7679702015-02-20 14:40:35 +0530378 def get_total_for_discount_amount(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +0530379 if self.doc.apply_discount_on == "Net Total":
380 return self.doc.net_total
Nabin Haite7679702015-02-20 14:40:35 +0530381 else:
382 actual_taxes_dict = {}
Nabin Hait3237c752015-02-17 11:11:11 +0530383
Nabin Haite7679702015-02-20 14:40:35 +0530384 for tax in self.doc.get("taxes"):
385 if tax.charge_type == "Actual":
386 actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
387 elif tax.row_id in actual_taxes_dict:
388 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * flt(tax.rate) / 100
389 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530390
Nabin Haite7679702015-02-20 14:40:35 +0530391 return flt(self.doc.grand_total - sum(actual_taxes_dict.values()), self.doc.precision("grand_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530392
393
Nabin Hait7b19b9e2015-02-24 09:42:24 +0530394 def calculate_total_advance(self):
395 if self.doc.docstatus < 2:
Nabin Haite7679702015-02-20 14:40:35 +0530396 total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530397 for adv in self.doc.get("advances")])
398
Nabin Haite7679702015-02-20 14:40:35 +0530399 self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
Nabin Hait289ffb72016-02-08 11:06:55 +0530400
401 if self.doc.party_account_currency == self.doc.currency:
402 invoice_total = self.doc.grand_total
403 else:
404 invoice_total = flt(self.doc.grand_total * self.doc.conversion_rate,
405 self.doc.precision("grand_total"))
406
Nabin Haitadc09232016-02-09 10:31:11 +0530407 if invoice_total > 0 and self.doc.total_advance > invoice_total:
Nabin Hait289ffb72016-02-08 11:06:55 +0530408 frappe.throw(_("Advance amount cannot be greater than {0} {1}")
409 .format(self.doc.party_account_currency, invoice_total))
Nabin Hait3237c752015-02-17 11:11:11 +0530410
Rushabh Mehta8bb6e532015-02-18 20:22:59 +0530411 if self.doc.docstatus == 0:
Nabin Hait3237c752015-02-17 11:11:11 +0530412 self.calculate_outstanding_amount()
413
414 def calculate_outstanding_amount(self):
415 # NOTE:
416 # write_off_amount is only for POS Invoice
417 # total_advance is only for non POS Invoice
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530418 if self.doc.is_return:
419 return
420
421 self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530422 if self.doc.party_account_currency == self.doc.currency:
423 total_amount_to_pay = flt(self.doc.grand_total - self.doc.total_advance
424 - flt(self.doc.write_off_amount), self.doc.precision("grand_total"))
425 else:
Nabin Haitd4507ac2016-01-20 16:14:27 +0530426 total_amount_to_pay = flt(flt(self.doc.grand_total *
427 self.doc.conversion_rate, self.doc.precision("grand_total")) - self.doc.total_advance
428 - flt(self.doc.base_write_off_amount), self.doc.precision("grand_total"))
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530429
Nabin Haitbd00e812015-02-17 12:50:51 +0530430 if self.doc.doctype == "Sales Invoice":
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530431 self.doc.round_floats_in(self.doc, ["paid_amount"])
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530432 paid_amount = self.doc.paid_amount \
433 if self.doc.party_account_currency == self.doc.currency else self.doc.base_paid_amount
434 self.doc.outstanding_amount = flt(total_amount_to_pay - flt(paid_amount),
435 self.doc.precision("outstanding_amount"))
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530436 elif self.doc.doctype == "Purchase Invoice":
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530437 self.doc.outstanding_amount = flt(total_amount_to_pay, self.doc.precision("outstanding_amount"))