blob: bd7322543c73ca493953a10f577fc88c64525066 [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 Hait613d0812015-02-23 11:58:15 +053080
Nabin Hait3237c752015-02-17 11:11:11 +053081 tax.item_wise_tax_detail = {}
82 tax_fields = ["total", "tax_amount_after_discount_amount",
83 "tax_amount_for_current_item", "grand_total_for_current_item",
84 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
85
Nabin Haitde9c8a92015-02-23 01:06:00 +053086 if tax.charge_type != "Actual" and \
87 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
88 tax_fields.append("tax_amount")
Nabin Hait3237c752015-02-17 11:11:11 +053089
90 for fieldname in tax_fields:
91 tax.set(fieldname, 0.0)
92
Nabin Hait3237c752015-02-17 11:11:11 +053093 self.doc.round_floats_in(tax)
94
Nabin Hait3237c752015-02-17 11:11:11 +053095 def determine_exclusive_rate(self):
Nabin Hait37b047d2015-02-23 16:01:33 +053096 if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))):
97 return
Nabin Hait3237c752015-02-17 11:11:11 +053098
99 for item in self.doc.get("items"):
100 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
101 cumulated_tax_fraction = 0
102 for i, tax in enumerate(self.doc.get("taxes")):
103 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
104
105 if i==0:
106 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
107 else:
108 tax.grand_total_fraction_for_current_item = \
109 self.doc.get("taxes")[i-1].grand_total_fraction_for_current_item \
110 + tax.tax_fraction_for_current_item
111
112 cumulated_tax_fraction += tax.tax_fraction_for_current_item
113
114 if cumulated_tax_fraction and not self.discount_amount_applied and item.qty:
Nabin Haite7679702015-02-20 14:40:35 +0530115 item.net_amount = flt(item.amount / (1 + cumulated_tax_fraction), item.precision("net_amount"))
116 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
117 item.discount_percentage = flt(item.discount_percentage, item.precision("discount_percentage"))
Nabin Hait3237c752015-02-17 11:11:11 +0530118
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
122 # below part need to be fixed???
123
124 # if item.discount_percentage == 100:
125 # item.price_list_rate = item.net_rate
126 # item.base_price_list_rate = flt(item.price_list_rate*self.doc.conversion_rate,
127 # self.doc.precision("base_price_list_rate", item))
128 # item.rate = item.base_rate = item.net_rate = item.base_net_rate = 0.0
129 # else:
130 # item.base_price_list_rate = flt(item.net_rate / (1 - (item.discount_percentage / 100.0)),
131 # self.doc.precision("price_list_rate", item))
132
Nabin Hait3237c752015-02-17 11:11:11 +0530133
134 def _load_item_tax_rate(self, item_tax_rate):
135 return json.loads(item_tax_rate) if item_tax_rate else {}
136
137 def get_current_tax_fraction(self, tax, item_tax_map):
138 """
139 Get tax fraction for calculating tax exclusive amount
140 from tax inclusive amount
141 """
142 current_tax_fraction = 0
143
144 if cint(tax.included_in_print_rate):
145 tax_rate = self._get_tax_rate(tax, item_tax_map)
146
147 if tax.charge_type == "On Net Total":
148 current_tax_fraction = tax_rate / 100.0
149
150 elif tax.charge_type == "On Previous Row Amount":
151 current_tax_fraction = (tax_rate / 100.0) * \
152 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item
153
154 elif tax.charge_type == "On Previous Row Total":
155 current_tax_fraction = (tax_rate / 100.0) * \
156 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
157
158 return current_tax_fraction
159
160 def _get_tax_rate(self, tax, item_tax_map):
161 if item_tax_map.has_key(tax.account_head):
162 return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax))
163 else:
164 return tax.rate
165
166 def calculate_net_total(self):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530167 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 +0530168
169 for item in self.doc.get("items"):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530170 self.doc.total += item.amount
171 self.doc.base_total += item.base_amount
Nabin Haite7679702015-02-20 14:40:35 +0530172 self.doc.net_total += item.net_amount
173 self.doc.base_net_total += item.base_net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530174
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530175 self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530176
177 def calculate_taxes(self):
178 # maintain actual tax rate based on idx
Nabin Haite7679702015-02-20 14:40:35 +0530179 actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
Nabin Hait3237c752015-02-17 11:11:11 +0530180 for tax in self.doc.get("taxes") if tax.charge_type == "Actual"])
181
182 for n, item in enumerate(self.doc.get("items")):
183 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
184
185 for i, tax in enumerate(self.doc.get("taxes")):
186 # tax_amount represents the amount of tax for the current step
187 current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
188
189 # Adjust divisional loss to the last item
190 if tax.charge_type == "Actual":
191 actual_tax_dict[tax.idx] -= current_tax_amount
192 if n == len(self.doc.get("items")) - 1:
193 current_tax_amount += actual_tax_dict[tax.idx]
194
Nabin Hait2b019ed2015-02-22 23:03:07 +0530195 # accumulate tax amount into tax.tax_amount
Nabin Haitde9c8a92015-02-23 01:06:00 +0530196 if tax.charge_type != "Actual" and \
197 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
198 tax.tax_amount += current_tax_amount
Nabin Hait2b019ed2015-02-22 23:03:07 +0530199
Nabin Hait3237c752015-02-17 11:11:11 +0530200 # store tax_amount for current item as it will be used for
201 # charge type = 'On Previous Row Amount'
202 tax.tax_amount_for_current_item = current_tax_amount
203
Nabin Hait2b019ed2015-02-22 23:03:07 +0530204 # set tax after discount
Nabin Hait3237c752015-02-17 11:11:11 +0530205 tax.tax_amount_after_discount_amount += current_tax_amount
206
207 if getattr(tax, "category", None):
208 # if just for valuation, do not add the tax amount in total
209 # hence, setting it as 0 for further steps
210 current_tax_amount = 0.0 if (tax.category == "Valuation") \
211 else current_tax_amount
212
213 current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
214
215 # Calculate tax.total viz. grand total till that step
216 # note: grand_total_for_current_item contains the contribution of
217 # item's amount, previously applied tax and the current tax on that item
218 if i==0:
Nabin Hait93b3a372015-02-24 17:08:34 +0530219 tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount, tax.precision("total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530220 else:
221 tax.grand_total_for_current_item = \
Rushabh Mehta6cdfd1e2015-02-25 15:55:57 +0530222 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 +0530223
224 # in tax.total, accumulate grand total of each item
225 tax.total += tax.grand_total_for_current_item
226
227 # set precision in the last item iteration
228 if n == len(self.doc.get("items")) - 1:
229 self.round_off_totals(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530230
Nabin Hait3237c752015-02-17 11:11:11 +0530231 # adjust Discount Amount loss in last tax iteration
Nabin Haitde9c8a92015-02-23 01:06:00 +0530232 if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
Nabin Hait25bd84d2015-03-04 15:06:56 +0530233 and self.doc.discount_amount:
Nabin Haitde9c8a92015-02-23 01:06:00 +0530234 self.adjust_discount_amount_loss(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530235
236
Nabin Hait3237c752015-02-17 11:11:11 +0530237
238 def get_current_tax_amount(self, item, tax, item_tax_map):
239 tax_rate = self._get_tax_rate(tax, item_tax_map)
240 current_tax_amount = 0.0
241
242 if tax.charge_type == "Actual":
243 # distribute the tax amount proportionally to each item row
Nabin Haite7679702015-02-20 14:40:35 +0530244 actual = flt(tax.tax_amount, tax.precision("tax_amount"))
245 current_tax_amount = item.net_amount*actual / self.doc.net_total if self.doc.net_total else 0.0
246
Nabin Hait3237c752015-02-17 11:11:11 +0530247 elif tax.charge_type == "On Net Total":
Nabin Haite7679702015-02-20 14:40:35 +0530248 current_tax_amount = (tax_rate / 100.0) * item.net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530249 elif tax.charge_type == "On Previous Row Amount":
250 current_tax_amount = (tax_rate / 100.0) * \
251 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item
252 elif tax.charge_type == "On Previous Row Total":
253 current_tax_amount = (tax_rate / 100.0) * \
254 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item
255
Nabin Hait93b3a372015-02-24 17:08:34 +0530256 current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530257
Nabin Haite7679702015-02-20 14:40:35 +0530258 self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530259
260 return current_tax_amount
261
Nabin Haite7679702015-02-20 14:40:35 +0530262 def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount):
263 # store tax breakup for each item
264 key = item.item_code or item.item_name
265 item_wise_tax_amount = current_tax_amount*self.doc.conversion_rate
266 if tax.item_wise_tax_detail.get(key):
267 item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
268
269 tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount, tax.precision("base_tax_amount"))]
270
Nabin Hait3237c752015-02-17 11:11:11 +0530271 def round_off_totals(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530272 tax.total = flt(tax.total, tax.precision("total"))
273 tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
274 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530275
Nabin Haitde9c8a92015-02-23 01:06:00 +0530276 self._set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Haitce245122015-02-22 20:14:49 +0530277
Nabin Hait3237c752015-02-17 11:11:11 +0530278 def adjust_discount_amount_loss(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530279 discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
Nabin Hait3237c752015-02-17 11:11:11 +0530280 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Nabin Hait95ff2d42015-03-04 16:02:03 +0530281 discount_amount_loss, tax.precision("tax_amount"))
282 tax.total = flt(tax.total + discount_amount_loss, tax.precision("total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530283
Nabin Hait95ff2d42015-03-04 16:02:03 +0530284 self._set_in_company_currency(tax, ["total", "tax_amount_after_discount_amount"])
Anand Doshi731b15e2015-04-05 20:09:09 +0530285
Nabin Haita1bf43b2015-03-17 10:50:47 +0530286 def manipulate_grand_total_for_inclusive_tax(self):
287 # if fully inclusive taxes and diff
288 if self.doc.get("taxes") and all(cint(t.included_in_print_rate) for t in self.doc.get("taxes")):
289
290 last_tax = self.doc.get("taxes")[-1]
291
Anand Doshi731b15e2015-04-05 20:09:09 +0530292 diff = self.doc.net_total - flt(last_tax.total / self.doc.conversion_rate,
293 self.doc.precision("grand_total_export"))
Nabin Haita1bf43b2015-03-17 10:50:47 +0530294
295 if diff and abs(diff) <= (2.0 / 10**last_tax.precision("tax_amount")):
296 adjustment_amount = flt(diff * self.doc.conversion_rate, last_tax.precision("tax_amount"))
297 last_tax.tax_amount += adjustment_amount
298 last_tax.tax_amount_after_discount_amount += adjustment_amount
299 last_tax.total += adjustment_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530300
301 def calculate_totals(self):
Nabin Haite7679702015-02-20 14:40:35 +0530302 self.doc.grand_total = flt(self.doc.get("taxes")[-1].total
303 if self.doc.get("taxes") else self.doc.net_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530304
Anand Doshiec5ec602015-03-05 19:31:23 +0530305 self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total,
306 self.doc.precision("total_taxes_and_charges"))
307
308 self._set_in_company_currency(self.doc, ["total_taxes_and_charges"])
309
Nabin Haitbd00e812015-02-17 12:50:51 +0530310 if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
Nabin Haite7679702015-02-20 14:40:35 +0530311 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
312 if self.doc.total_taxes_and_charges else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530313 else:
Anand Doshiec5ec602015-03-05 19:31:23 +0530314 self.doc.taxes_and_charges_added = self.doc.taxes_and_charges_deducted = 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530315 for tax in self.doc.get("taxes"):
316 if tax.category in ["Valuation and Total", "Total"]:
317 if tax.add_deduct_tax == "Add":
Nabin Haite7679702015-02-20 14:40:35 +0530318 self.doc.taxes_and_charges_added += flt(tax.tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530319 else:
Nabin Haite7679702015-02-20 14:40:35 +0530320 self.doc.taxes_and_charges_deducted += flt(tax.tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530321
Nabin Haite7679702015-02-20 14:40:35 +0530322 self.doc.round_floats_in(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.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
325 if (self.doc.taxes_and_charges_added or self.doc.taxes_and_charges_deducted) \
326 else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530327
Nabin Haite7679702015-02-20 14:40:35 +0530328 self._set_in_company_currency(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530329
Nabin Haite7679702015-02-20 14:40:35 +0530330 self.doc.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530331
Nabin Hait3237c752015-02-17 11:11:11 +0530332 if self.doc.meta.get_field("rounded_total"):
333 self.doc.rounded_total = rounded(self.doc.grand_total)
Nabin Haite7679702015-02-20 14:40:35 +0530334 if self.doc.meta.get_field("base_rounded_total"):
335 self.doc.base_rounded_total = rounded(self.doc.base_grand_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530336
337 def _cleanup(self):
338 for tax in self.doc.get("taxes"):
339 tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':'))
340
341 def apply_discount_amount(self):
342 if self.doc.discount_amount:
Nabin Hait37b047d2015-02-23 16:01:33 +0530343 if not self.doc.apply_discount_on:
344 frappe.throw(_("Please select Apply Discount On"))
345
Nabin Hait3237c752015-02-17 11:11:11 +0530346 self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate,
347 self.doc.precision("base_discount_amount"))
348
Nabin Haite7679702015-02-20 14:40:35 +0530349 total_for_discount_amount = self.get_total_for_discount_amount()
Nabin Hait25bd84d2015-03-04 15:06:56 +0530350 taxes = self.doc.get("taxes")
351 net_total = 0
Nabin Hait3237c752015-02-17 11:11:11 +0530352
Nabin Haite7679702015-02-20 14:40:35 +0530353 if total_for_discount_amount:
Nabin Hait3237c752015-02-17 11:11:11 +0530354 # calculate item amount after Discount Amount
Nabin Hait25bd84d2015-03-04 15:06:56 +0530355 for i, item in enumerate(self.doc.get("items")):
356 distributed_amount = flt(self.doc.discount_amount) * \
357 item.net_amount / total_for_discount_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530358
Nabin Haite7679702015-02-20 14:40:35 +0530359 item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
Nabin Hait25bd84d2015-03-04 15:06:56 +0530360 net_total += item.net_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530361
Nabin Hait25bd84d2015-03-04 15:06:56 +0530362 # discount amount rounding loss adjustment if no taxes
363 if (not taxes or self.doc.apply_discount_on == "Net Total") \
364 and i == len(self.doc.get("items")) - 1:
Anand Doshiec5ec602015-03-05 19:31:23 +0530365 discount_amount_loss = flt(self.doc.total - net_total - self.doc.discount_amount,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530366 self.doc.precision("net_total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530367 item.net_amount = flt(item.net_amount + discount_amount_loss,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530368 item.precision("net_amount"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530369
Nabin Haite7679702015-02-20 14:40:35 +0530370 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530371
Nabin Haite7679702015-02-20 14:40:35 +0530372 self._set_in_company_currency(item, ["net_rate", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530373
374 self.discount_amount_applied = True
375 self._calculate()
376 else:
377 self.doc.base_discount_amount = 0
378
Nabin Haite7679702015-02-20 14:40:35 +0530379 def get_total_for_discount_amount(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +0530380 if self.doc.apply_discount_on == "Net Total":
381 return self.doc.net_total
Nabin Haite7679702015-02-20 14:40:35 +0530382 else:
383 actual_taxes_dict = {}
Nabin Hait3237c752015-02-17 11:11:11 +0530384
Nabin Haite7679702015-02-20 14:40:35 +0530385 for tax in self.doc.get("taxes"):
386 if tax.charge_type == "Actual":
387 actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
388 elif tax.row_id in actual_taxes_dict:
389 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * flt(tax.rate) / 100
390 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530391
Nabin Haite7679702015-02-20 14:40:35 +0530392 return flt(self.doc.grand_total - sum(actual_taxes_dict.values()), self.doc.precision("grand_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530393
394
Nabin Hait7b19b9e2015-02-24 09:42:24 +0530395 def calculate_total_advance(self):
396 if self.doc.docstatus < 2:
Nabin Haite7679702015-02-20 14:40:35 +0530397 total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530398 for adv in self.doc.get("advances")])
399
Nabin Haite7679702015-02-20 14:40:35 +0530400 self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
Nabin Hait3237c752015-02-17 11:11:11 +0530401
Rushabh Mehta8bb6e532015-02-18 20:22:59 +0530402 if self.doc.docstatus == 0:
Nabin Hait3237c752015-02-17 11:11:11 +0530403 self.calculate_outstanding_amount()
404
405 def calculate_outstanding_amount(self):
406 # NOTE:
407 # write_off_amount is only for POS Invoice
408 # total_advance is only for non POS Invoice
409
Nabin Haitbd00e812015-02-17 12:50:51 +0530410 if self.doc.doctype == "Sales Invoice":
411 self.doc.round_floats_in(self.doc, ["base_grand_total", "total_advance", "write_off_amount", "paid_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530412 total_amount_to_pay = self.doc.base_grand_total - self.doc.write_off_amount
413 self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance - self.doc.paid_amount,
414 self.doc.precision("outstanding_amount"))
415 else:
Nabin Haitbd00e812015-02-17 12:50:51 +0530416 self.doc.round_floats_in(self.doc, ["total_advance", "write_off_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530417 self.doc.total_amount_to_pay = flt(self.doc.base_grand_total - self.doc.write_off_amount,
418 self.doc.precision("total_amount_to_pay"))
419 self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
420 self.doc.precision("outstanding_amount"))