blob: f22b62488bb19def28c10c64d3cc8dd7c71283b2 [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
7from frappe import _
Nabin Hait3237c752015-02-17 11:11:11 +05308from frappe.utils import cint, flt, rounded
9from 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"):
23 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
40 company_currency = get_company_currency(self.doc.company)
41 if not self.doc.currency or self.doc.currency == company_currency:
42 self.doc.currency = company_currency
43 self.doc.conversion_rate = 1.0
44 else:
45 validate_conversion_rate(self.doc.currency, self.doc.conversion_rate,
46 self.doc.meta.get_label("conversion_rate"), self.doc.company)
47
48 self.doc.conversion_rate = flt(self.doc.conversion_rate)
49
Nabin 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
Nabin Haite7679702015-02-20 14:40:35 +053061 item.net_rate = item.rate
62 item.amount = flt(item.rate * item.qty, item.precision("amount"))
63 item.net_amount = item.amount
Nabin Hait3237c752015-02-17 11:11:11 +053064
Nabin Haite7679702015-02-20 14:40:35 +053065 self._set_in_company_currency(item, ["price_list_rate", "rate", "net_rate", "amount", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +053066
Nabin Haite7679702015-02-20 14:40:35 +053067 item.item_tax_amount = 0.0
68
69 def _set_in_company_currency(self, doc, fields):
Nabin Hait3237c752015-02-17 11:11:11 +053070 """set values in base currency"""
Nabin Haite7679702015-02-20 14:40:35 +053071 for f in fields:
72 val = flt(flt(doc.get(f), doc.precision(f)) * self.doc.conversion_rate, doc.precision("base_" + f))
73 doc.set("base_" + f, val)
Nabin Hait3237c752015-02-17 11:11:11 +053074
75 def initialize_taxes(self):
76 for tax in self.doc.get("taxes"):
Nabin Hait86cd4cc2015-02-28 19:11:51 +053077 if not self.discount_amount_applied:
78 validate_taxes_and_charges(tax)
79 validate_inclusive_tax(tax, self.doc)
Nabin Hait1d218422015-07-17 15:19:02 +053080
81 if self.doc.meta.get_field("is_return") and self.doc.is_return and tax.charge_type == "Actual":
82 tax.tax_amount = -1 * tax.tax_amount
Nabin Hait613d0812015-02-23 11:58:15 +053083
Nabin Hait3237c752015-02-17 11:11:11 +053084 tax.item_wise_tax_detail = {}
85 tax_fields = ["total", "tax_amount_after_discount_amount",
86 "tax_amount_for_current_item", "grand_total_for_current_item",
87 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
88
Nabin Haitde9c8a92015-02-23 01:06:00 +053089 if tax.charge_type != "Actual" and \
90 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
91 tax_fields.append("tax_amount")
Nabin Hait3237c752015-02-17 11:11:11 +053092
93 for fieldname in tax_fields:
94 tax.set(fieldname, 0.0)
95
Nabin Hait3237c752015-02-17 11:11:11 +053096 self.doc.round_floats_in(tax)
97
Nabin Hait3237c752015-02-17 11:11:11 +053098 def determine_exclusive_rate(self):
Nabin Hait37b047d2015-02-23 16:01:33 +053099 if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))):
100 return
Nabin Hait3237c752015-02-17 11:11:11 +0530101
102 for item in self.doc.get("items"):
103 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
104 cumulated_tax_fraction = 0
105 for i, tax in enumerate(self.doc.get("taxes")):
106 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
107
108 if i==0:
109 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
110 else:
111 tax.grand_total_fraction_for_current_item = \
112 self.doc.get("taxes")[i-1].grand_total_fraction_for_current_item \
113 + tax.tax_fraction_for_current_item
114
115 cumulated_tax_fraction += tax.tax_fraction_for_current_item
116
117 if cumulated_tax_fraction and not self.discount_amount_applied and item.qty:
Nabin Haite7679702015-02-20 14:40:35 +0530118 item.net_amount = flt(item.amount / (1 + cumulated_tax_fraction), item.precision("net_amount"))
119 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
120 item.discount_percentage = flt(item.discount_percentage, item.precision("discount_percentage"))
Nabin Hait3237c752015-02-17 11:11:11 +0530121
Nabin Haite7679702015-02-20 14:40:35 +0530122 self._set_in_company_currency(item, ["net_rate", "net_amount"])
123
Nabin Hait3237c752015-02-17 11:11:11 +0530124 def _load_item_tax_rate(self, item_tax_rate):
125 return json.loads(item_tax_rate) if item_tax_rate else {}
126
127 def get_current_tax_fraction(self, tax, item_tax_map):
128 """
129 Get tax fraction for calculating tax exclusive amount
130 from tax inclusive amount
131 """
132 current_tax_fraction = 0
133
134 if cint(tax.included_in_print_rate):
135 tax_rate = self._get_tax_rate(tax, item_tax_map)
136
137 if tax.charge_type == "On Net Total":
138 current_tax_fraction = tax_rate / 100.0
139
140 elif tax.charge_type == "On Previous Row Amount":
141 current_tax_fraction = (tax_rate / 100.0) * \
142 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item
143
144 elif tax.charge_type == "On Previous Row Total":
145 current_tax_fraction = (tax_rate / 100.0) * \
146 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
147
Nabin Haita6ee8292015-05-15 12:02:01 +0530148 if getattr(tax, "add_deduct_tax", None):
149 current_tax_fraction *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
Nabin Hait3237c752015-02-17 11:11:11 +0530150 return current_tax_fraction
151
152 def _get_tax_rate(self, tax, item_tax_map):
153 if item_tax_map.has_key(tax.account_head):
154 return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax))
155 else:
156 return tax.rate
157
158 def calculate_net_total(self):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530159 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 +0530160
161 for item in self.doc.get("items"):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530162 self.doc.total += item.amount
163 self.doc.base_total += item.base_amount
Nabin Haite7679702015-02-20 14:40:35 +0530164 self.doc.net_total += item.net_amount
165 self.doc.base_net_total += item.base_net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530166
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530167 self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530168
169 def calculate_taxes(self):
170 # maintain actual tax rate based on idx
Nabin Haite7679702015-02-20 14:40:35 +0530171 actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
Nabin Hait3237c752015-02-17 11:11:11 +0530172 for tax in self.doc.get("taxes") if tax.charge_type == "Actual"])
173
174 for n, item in enumerate(self.doc.get("items")):
175 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
176
177 for i, tax in enumerate(self.doc.get("taxes")):
178 # tax_amount represents the amount of tax for the current step
179 current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
180
181 # Adjust divisional loss to the last item
182 if tax.charge_type == "Actual":
183 actual_tax_dict[tax.idx] -= current_tax_amount
184 if n == len(self.doc.get("items")) - 1:
185 current_tax_amount += actual_tax_dict[tax.idx]
186
Nabin Hait2b019ed2015-02-22 23:03:07 +0530187 # accumulate tax amount into tax.tax_amount
Nabin Haitde9c8a92015-02-23 01:06:00 +0530188 if tax.charge_type != "Actual" and \
189 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
190 tax.tax_amount += current_tax_amount
Nabin Hait2b019ed2015-02-22 23:03:07 +0530191
Nabin Hait3237c752015-02-17 11:11:11 +0530192 # store tax_amount for current item as it will be used for
193 # charge type = 'On Previous Row Amount'
194 tax.tax_amount_for_current_item = current_tax_amount
195
Nabin Hait2b019ed2015-02-22 23:03:07 +0530196 # set tax after discount
Nabin Hait3237c752015-02-17 11:11:11 +0530197 tax.tax_amount_after_discount_amount += current_tax_amount
198
199 if getattr(tax, "category", None):
200 # if just for valuation, do not add the tax amount in total
201 # hence, setting it as 0 for further steps
202 current_tax_amount = 0.0 if (tax.category == "Valuation") \
203 else current_tax_amount
204
205 current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
206
207 # Calculate tax.total viz. grand total till that step
208 # note: grand_total_for_current_item contains the contribution of
209 # item's amount, previously applied tax and the current tax on that item
210 if i==0:
Nabin Hait93b3a372015-02-24 17:08:34 +0530211 tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount, tax.precision("total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530212 else:
213 tax.grand_total_for_current_item = \
Rushabh Mehta6cdfd1e2015-02-25 15:55:57 +0530214 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 +0530215
216 # in tax.total, accumulate grand total of each item
217 tax.total += tax.grand_total_for_current_item
218
219 # set precision in the last item iteration
220 if n == len(self.doc.get("items")) - 1:
221 self.round_off_totals(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530222
Nabin Hait3237c752015-02-17 11:11:11 +0530223 # adjust Discount Amount loss in last tax iteration
Nabin Haitde9c8a92015-02-23 01:06:00 +0530224 if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
Nabin Hait25bd84d2015-03-04 15:06:56 +0530225 and self.doc.discount_amount:
Nabin Haitde9c8a92015-02-23 01:06:00 +0530226 self.adjust_discount_amount_loss(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530227
228
Nabin Hait3237c752015-02-17 11:11:11 +0530229
230 def get_current_tax_amount(self, item, tax, item_tax_map):
231 tax_rate = self._get_tax_rate(tax, item_tax_map)
232 current_tax_amount = 0.0
233
234 if tax.charge_type == "Actual":
235 # distribute the tax amount proportionally to each item row
Nabin Haite7679702015-02-20 14:40:35 +0530236 actual = flt(tax.tax_amount, tax.precision("tax_amount"))
237 current_tax_amount = item.net_amount*actual / self.doc.net_total if self.doc.net_total else 0.0
238
Nabin Hait3237c752015-02-17 11:11:11 +0530239 elif tax.charge_type == "On Net Total":
Nabin Haite7679702015-02-20 14:40:35 +0530240 current_tax_amount = (tax_rate / 100.0) * item.net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530241 elif tax.charge_type == "On Previous Row Amount":
242 current_tax_amount = (tax_rate / 100.0) * \
243 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item
244 elif tax.charge_type == "On Previous Row Total":
245 current_tax_amount = (tax_rate / 100.0) * \
246 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item
247
Nabin Hait93b3a372015-02-24 17:08:34 +0530248 current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530249
Nabin Haite7679702015-02-20 14:40:35 +0530250 self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530251
252 return current_tax_amount
253
Nabin Haite7679702015-02-20 14:40:35 +0530254 def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount):
255 # store tax breakup for each item
256 key = item.item_code or item.item_name
257 item_wise_tax_amount = current_tax_amount*self.doc.conversion_rate
258 if tax.item_wise_tax_detail.get(key):
259 item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
260
261 tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount, tax.precision("base_tax_amount"))]
262
Nabin Hait3237c752015-02-17 11:11:11 +0530263 def round_off_totals(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530264 tax.total = flt(tax.total, tax.precision("total"))
265 tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
266 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530267
Nabin Haitde9c8a92015-02-23 01:06:00 +0530268 self._set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Haitce245122015-02-22 20:14:49 +0530269
Nabin Hait3237c752015-02-17 11:11:11 +0530270 def adjust_discount_amount_loss(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530271 discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
Nabin Hait3237c752015-02-17 11:11:11 +0530272 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Nabin Hait95ff2d42015-03-04 16:02:03 +0530273 discount_amount_loss, tax.precision("tax_amount"))
274 tax.total = flt(tax.total + discount_amount_loss, tax.precision("total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530275
Nabin Hait95ff2d42015-03-04 16:02:03 +0530276 self._set_in_company_currency(tax, ["total", "tax_amount_after_discount_amount"])
Anand Doshi731b15e2015-04-05 20:09:09 +0530277
Nabin Haita1bf43b2015-03-17 10:50:47 +0530278 def manipulate_grand_total_for_inclusive_tax(self):
279 # if fully inclusive taxes and diff
280 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 +0530281 last_tax = self.doc.get("taxes")[-1]
Nabin Haita6ee8292015-05-15 12:02:01 +0530282 diff = self.doc.total - flt(last_tax.total, self.doc.precision("grand_total"))
Nabin Haita1bf43b2015-03-17 10:50:47 +0530283
284 if diff and abs(diff) <= (2.0 / 10**last_tax.precision("tax_amount")):
Nabin Haita6ee8292015-05-15 12:02:01 +0530285 last_tax.tax_amount += diff
286 last_tax.tax_amount_after_discount_amount += diff
287 last_tax.total += diff
Nabin Hait82521172015-05-19 16:29:44 +0530288
289 self._set_in_company_currency(last_tax,
290 ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530291
292 def calculate_totals(self):
Nabin Haite7679702015-02-20 14:40:35 +0530293 self.doc.grand_total = flt(self.doc.get("taxes")[-1].total
294 if self.doc.get("taxes") else self.doc.net_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530295
Anand Doshiec5ec602015-03-05 19:31:23 +0530296 self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total,
297 self.doc.precision("total_taxes_and_charges"))
298
299 self._set_in_company_currency(self.doc, ["total_taxes_and_charges"])
300
Nabin Haitbd00e812015-02-17 12:50:51 +0530301 if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
Nabin Haite7679702015-02-20 14:40:35 +0530302 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
303 if self.doc.total_taxes_and_charges else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530304 else:
Anand Doshiec5ec602015-03-05 19:31:23 +0530305 self.doc.taxes_and_charges_added = self.doc.taxes_and_charges_deducted = 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530306 for tax in self.doc.get("taxes"):
307 if tax.category in ["Valuation and Total", "Total"]:
308 if tax.add_deduct_tax == "Add":
Nabin Haite7679702015-02-20 14:40:35 +0530309 self.doc.taxes_and_charges_added += flt(tax.tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530310 else:
Nabin Haite7679702015-02-20 14:40:35 +0530311 self.doc.taxes_and_charges_deducted += flt(tax.tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530312
Nabin Haite7679702015-02-20 14:40:35 +0530313 self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530314
Nabin Haite7679702015-02-20 14:40:35 +0530315 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
316 if (self.doc.taxes_and_charges_added or self.doc.taxes_and_charges_deducted) \
317 else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530318
Nabin Haite7679702015-02-20 14:40:35 +0530319 self._set_in_company_currency(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530320
Nabin Haite7679702015-02-20 14:40:35 +0530321 self.doc.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530322
Nabin Hait3237c752015-02-17 11:11:11 +0530323 if self.doc.meta.get_field("rounded_total"):
324 self.doc.rounded_total = rounded(self.doc.grand_total)
Nabin Haite7679702015-02-20 14:40:35 +0530325 if self.doc.meta.get_field("base_rounded_total"):
326 self.doc.base_rounded_total = rounded(self.doc.base_grand_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530327
328 def _cleanup(self):
329 for tax in self.doc.get("taxes"):
330 tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':'))
331
332 def apply_discount_amount(self):
333 if self.doc.discount_amount:
Nabin Hait37b047d2015-02-23 16:01:33 +0530334 if not self.doc.apply_discount_on:
335 frappe.throw(_("Please select Apply Discount On"))
336
Nabin Hait3237c752015-02-17 11:11:11 +0530337 self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate,
338 self.doc.precision("base_discount_amount"))
339
Nabin Haite7679702015-02-20 14:40:35 +0530340 total_for_discount_amount = self.get_total_for_discount_amount()
Nabin Hait25bd84d2015-03-04 15:06:56 +0530341 taxes = self.doc.get("taxes")
342 net_total = 0
Nabin Hait3237c752015-02-17 11:11:11 +0530343
Nabin Haite7679702015-02-20 14:40:35 +0530344 if total_for_discount_amount:
Nabin Hait3237c752015-02-17 11:11:11 +0530345 # calculate item amount after Discount Amount
Nabin Hait25bd84d2015-03-04 15:06:56 +0530346 for i, item in enumerate(self.doc.get("items")):
347 distributed_amount = flt(self.doc.discount_amount) * \
348 item.net_amount / total_for_discount_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530349
Nabin Haite7679702015-02-20 14:40:35 +0530350 item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
Nabin Hait25bd84d2015-03-04 15:06:56 +0530351 net_total += item.net_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530352
Nabin Hait25bd84d2015-03-04 15:06:56 +0530353 # discount amount rounding loss adjustment if no taxes
354 if (not taxes or self.doc.apply_discount_on == "Net Total") \
355 and i == len(self.doc.get("items")) - 1:
Anand Doshiec5ec602015-03-05 19:31:23 +0530356 discount_amount_loss = flt(self.doc.total - net_total - self.doc.discount_amount,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530357 self.doc.precision("net_total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530358 item.net_amount = flt(item.net_amount + discount_amount_loss,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530359 item.precision("net_amount"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530360
Nabin Haite7679702015-02-20 14:40:35 +0530361 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530362
Nabin Haite7679702015-02-20 14:40:35 +0530363 self._set_in_company_currency(item, ["net_rate", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530364
365 self.discount_amount_applied = True
366 self._calculate()
367 else:
368 self.doc.base_discount_amount = 0
369
Nabin Haite7679702015-02-20 14:40:35 +0530370 def get_total_for_discount_amount(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +0530371 if self.doc.apply_discount_on == "Net Total":
372 return self.doc.net_total
Nabin Haite7679702015-02-20 14:40:35 +0530373 else:
374 actual_taxes_dict = {}
Nabin Hait3237c752015-02-17 11:11:11 +0530375
Nabin Haite7679702015-02-20 14:40:35 +0530376 for tax in self.doc.get("taxes"):
377 if tax.charge_type == "Actual":
378 actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
379 elif tax.row_id in actual_taxes_dict:
380 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * flt(tax.rate) / 100
381 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530382
Nabin Haite7679702015-02-20 14:40:35 +0530383 return flt(self.doc.grand_total - sum(actual_taxes_dict.values()), self.doc.precision("grand_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530384
385
Nabin Hait7b19b9e2015-02-24 09:42:24 +0530386 def calculate_total_advance(self):
387 if self.doc.docstatus < 2:
Nabin Haite7679702015-02-20 14:40:35 +0530388 total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530389 for adv in self.doc.get("advances")])
390
Nabin Haite7679702015-02-20 14:40:35 +0530391 self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
Nabin Hait3237c752015-02-17 11:11:11 +0530392
Rushabh Mehta8bb6e532015-02-18 20:22:59 +0530393 if self.doc.docstatus == 0:
Nabin Hait3237c752015-02-17 11:11:11 +0530394 self.calculate_outstanding_amount()
395
396 def calculate_outstanding_amount(self):
397 # NOTE:
398 # write_off_amount is only for POS Invoice
399 # total_advance is only for non POS Invoice
400
Nabin Haitbd00e812015-02-17 12:50:51 +0530401 if self.doc.doctype == "Sales Invoice":
Nabin Hait1d218422015-07-17 15:19:02 +0530402 if not self.doc.is_return:
403 self.doc.round_floats_in(self.doc, ["base_grand_total", "total_advance", "write_off_amount", "paid_amount"])
404 total_amount_to_pay = self.doc.base_grand_total - self.doc.write_off_amount
405 self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance - self.doc.paid_amount,
406 self.doc.precision("outstanding_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530407 else:
Nabin Haitbd00e812015-02-17 12:50:51 +0530408 self.doc.round_floats_in(self.doc, ["total_advance", "write_off_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530409 self.doc.total_amount_to_pay = flt(self.doc.base_grand_total - self.doc.write_off_amount,
410 self.doc.precision("total_amount_to_pay"))
Nabin Hait1d218422015-07-17 15:19:02 +0530411 if not self.doc.is_return:
412 self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
413 self.doc.precision("outstanding_amount"))