blob: b348da67d9541d6d56788ce103960a9a87f2ed1b [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 Haite7679702015-02-20 14:40:35 +0530119 self._set_in_company_currency(item, ["net_rate", "net_amount"])
120
Nabin Hait3237c752015-02-17 11:11:11 +0530121 def _load_item_tax_rate(self, item_tax_rate):
122 return json.loads(item_tax_rate) if item_tax_rate else {}
123
124 def get_current_tax_fraction(self, tax, item_tax_map):
125 """
126 Get tax fraction for calculating tax exclusive amount
127 from tax inclusive amount
128 """
129 current_tax_fraction = 0
130
131 if cint(tax.included_in_print_rate):
132 tax_rate = self._get_tax_rate(tax, item_tax_map)
133
134 if tax.charge_type == "On Net Total":
135 current_tax_fraction = tax_rate / 100.0
136
137 elif tax.charge_type == "On Previous Row Amount":
138 current_tax_fraction = (tax_rate / 100.0) * \
139 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item
140
141 elif tax.charge_type == "On Previous Row Total":
142 current_tax_fraction = (tax_rate / 100.0) * \
143 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
144
Nabin Haita6ee8292015-05-15 12:02:01 +0530145 if getattr(tax, "add_deduct_tax", None):
146 current_tax_fraction *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
Nabin Hait3237c752015-02-17 11:11:11 +0530147 return current_tax_fraction
148
149 def _get_tax_rate(self, tax, item_tax_map):
150 if item_tax_map.has_key(tax.account_head):
151 return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax))
152 else:
153 return tax.rate
154
155 def calculate_net_total(self):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530156 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 +0530157
158 for item in self.doc.get("items"):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530159 self.doc.total += item.amount
160 self.doc.base_total += item.base_amount
Nabin Haite7679702015-02-20 14:40:35 +0530161 self.doc.net_total += item.net_amount
162 self.doc.base_net_total += item.base_net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530163
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530164 self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530165
166 def calculate_taxes(self):
167 # maintain actual tax rate based on idx
Nabin Haite7679702015-02-20 14:40:35 +0530168 actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
Nabin Hait3237c752015-02-17 11:11:11 +0530169 for tax in self.doc.get("taxes") if tax.charge_type == "Actual"])
170
171 for n, item in enumerate(self.doc.get("items")):
172 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
173
174 for i, tax in enumerate(self.doc.get("taxes")):
175 # tax_amount represents the amount of tax for the current step
176 current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
177
178 # Adjust divisional loss to the last item
179 if tax.charge_type == "Actual":
180 actual_tax_dict[tax.idx] -= current_tax_amount
181 if n == len(self.doc.get("items")) - 1:
182 current_tax_amount += actual_tax_dict[tax.idx]
183
Nabin Hait2b019ed2015-02-22 23:03:07 +0530184 # accumulate tax amount into tax.tax_amount
Nabin Haitde9c8a92015-02-23 01:06:00 +0530185 if tax.charge_type != "Actual" and \
186 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
187 tax.tax_amount += current_tax_amount
Nabin Hait2b019ed2015-02-22 23:03:07 +0530188
Nabin Hait3237c752015-02-17 11:11:11 +0530189 # store tax_amount for current item as it will be used for
190 # charge type = 'On Previous Row Amount'
191 tax.tax_amount_for_current_item = current_tax_amount
192
Nabin Hait2b019ed2015-02-22 23:03:07 +0530193 # set tax after discount
Nabin Hait3237c752015-02-17 11:11:11 +0530194 tax.tax_amount_after_discount_amount += current_tax_amount
195
196 if getattr(tax, "category", None):
197 # if just for valuation, do not add the tax amount in total
198 # hence, setting it as 0 for further steps
199 current_tax_amount = 0.0 if (tax.category == "Valuation") \
200 else current_tax_amount
201
202 current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
203
204 # Calculate tax.total viz. grand total till that step
205 # note: grand_total_for_current_item contains the contribution of
206 # item's amount, previously applied tax and the current tax on that item
207 if i==0:
Nabin Hait93b3a372015-02-24 17:08:34 +0530208 tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount, tax.precision("total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530209 else:
210 tax.grand_total_for_current_item = \
Rushabh Mehta6cdfd1e2015-02-25 15:55:57 +0530211 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 +0530212
213 # in tax.total, accumulate grand total of each item
214 tax.total += tax.grand_total_for_current_item
215
216 # set precision in the last item iteration
217 if n == len(self.doc.get("items")) - 1:
218 self.round_off_totals(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530219
Nabin Hait3237c752015-02-17 11:11:11 +0530220 # adjust Discount Amount loss in last tax iteration
Nabin Haitde9c8a92015-02-23 01:06:00 +0530221 if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
Nabin Haitdb53a782015-07-31 16:53:13 +0530222 and self.doc.discount_amount and self.doc.apply_discount_on == "Grand Total":
Nabin Haitde9c8a92015-02-23 01:06:00 +0530223 self.adjust_discount_amount_loss(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530224
225
Nabin Hait3237c752015-02-17 11:11:11 +0530226
227 def get_current_tax_amount(self, item, tax, item_tax_map):
228 tax_rate = self._get_tax_rate(tax, item_tax_map)
229 current_tax_amount = 0.0
230
231 if tax.charge_type == "Actual":
232 # distribute the tax amount proportionally to each item row
Nabin Haite7679702015-02-20 14:40:35 +0530233 actual = flt(tax.tax_amount, tax.precision("tax_amount"))
234 current_tax_amount = item.net_amount*actual / self.doc.net_total if self.doc.net_total else 0.0
235
Nabin Hait3237c752015-02-17 11:11:11 +0530236 elif tax.charge_type == "On Net Total":
Nabin Haite7679702015-02-20 14:40:35 +0530237 current_tax_amount = (tax_rate / 100.0) * item.net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530238 elif tax.charge_type == "On Previous Row Amount":
239 current_tax_amount = (tax_rate / 100.0) * \
240 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item
241 elif tax.charge_type == "On Previous Row Total":
242 current_tax_amount = (tax_rate / 100.0) * \
243 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item
244
Nabin Hait93b3a372015-02-24 17:08:34 +0530245 current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530246
Nabin Haite7679702015-02-20 14:40:35 +0530247 self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530248
249 return current_tax_amount
250
Nabin Haite7679702015-02-20 14:40:35 +0530251 def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount):
252 # store tax breakup for each item
253 key = item.item_code or item.item_name
254 item_wise_tax_amount = current_tax_amount*self.doc.conversion_rate
255 if tax.item_wise_tax_detail.get(key):
256 item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
257
258 tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount, tax.precision("base_tax_amount"))]
259
Nabin Hait3237c752015-02-17 11:11:11 +0530260 def round_off_totals(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530261 tax.total = flt(tax.total, tax.precision("total"))
262 tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
263 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530264
Nabin Haitde9c8a92015-02-23 01:06:00 +0530265 self._set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Haitce245122015-02-22 20:14:49 +0530266
Nabin Hait3237c752015-02-17 11:11:11 +0530267 def adjust_discount_amount_loss(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530268 discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
Nabin Hait3237c752015-02-17 11:11:11 +0530269 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Nabin Hait95ff2d42015-03-04 16:02:03 +0530270 discount_amount_loss, tax.precision("tax_amount"))
271 tax.total = flt(tax.total + discount_amount_loss, tax.precision("total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530272
Nabin Hait95ff2d42015-03-04 16:02:03 +0530273 self._set_in_company_currency(tax, ["total", "tax_amount_after_discount_amount"])
Anand Doshi731b15e2015-04-05 20:09:09 +0530274
Nabin Haita1bf43b2015-03-17 10:50:47 +0530275 def manipulate_grand_total_for_inclusive_tax(self):
276 # if fully inclusive taxes and diff
277 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 +0530278 last_tax = self.doc.get("taxes")[-1]
Nabin Haita6ee8292015-05-15 12:02:01 +0530279 diff = self.doc.total - flt(last_tax.total, self.doc.precision("grand_total"))
Nabin Haita1bf43b2015-03-17 10:50:47 +0530280
281 if diff and abs(diff) <= (2.0 / 10**last_tax.precision("tax_amount")):
Nabin Haita6ee8292015-05-15 12:02:01 +0530282 last_tax.tax_amount += diff
283 last_tax.tax_amount_after_discount_amount += diff
284 last_tax.total += diff
Nabin Hait82521172015-05-19 16:29:44 +0530285
286 self._set_in_company_currency(last_tax,
287 ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530288
289 def calculate_totals(self):
Nabin Haite7679702015-02-20 14:40:35 +0530290 self.doc.grand_total = flt(self.doc.get("taxes")[-1].total
291 if self.doc.get("taxes") else self.doc.net_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530292
Anand Doshiec5ec602015-03-05 19:31:23 +0530293 self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total,
294 self.doc.precision("total_taxes_and_charges"))
295
296 self._set_in_company_currency(self.doc, ["total_taxes_and_charges"])
297
Nabin Haitbd00e812015-02-17 12:50:51 +0530298 if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
Nabin Haite7679702015-02-20 14:40:35 +0530299 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
300 if self.doc.total_taxes_and_charges else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530301 else:
Anand Doshiec5ec602015-03-05 19:31:23 +0530302 self.doc.taxes_and_charges_added = self.doc.taxes_and_charges_deducted = 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530303 for tax in self.doc.get("taxes"):
304 if tax.category in ["Valuation and Total", "Total"]:
305 if tax.add_deduct_tax == "Add":
Nabin Haitdb53a782015-07-31 16:53:13 +0530306 self.doc.taxes_and_charges_added += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530307 else:
Nabin Haitdb53a782015-07-31 16:53:13 +0530308 self.doc.taxes_and_charges_deducted += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530309
Nabin Haite7679702015-02-20 14:40:35 +0530310 self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530311
Nabin Haite7679702015-02-20 14:40:35 +0530312 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
313 if (self.doc.taxes_and_charges_added or self.doc.taxes_and_charges_deducted) \
314 else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530315
Nabin Haite7679702015-02-20 14:40:35 +0530316 self._set_in_company_currency(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.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530319
Nabin Hait3237c752015-02-17 11:11:11 +0530320 if self.doc.meta.get_field("rounded_total"):
321 self.doc.rounded_total = rounded(self.doc.grand_total)
Nabin Haite7679702015-02-20 14:40:35 +0530322 if self.doc.meta.get_field("base_rounded_total"):
323 self.doc.base_rounded_total = rounded(self.doc.base_grand_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530324
325 def _cleanup(self):
326 for tax in self.doc.get("taxes"):
327 tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':'))
328
329 def apply_discount_amount(self):
330 if self.doc.discount_amount:
Nabin Hait37b047d2015-02-23 16:01:33 +0530331 if not self.doc.apply_discount_on:
332 frappe.throw(_("Please select Apply Discount On"))
333
Nabin Hait3237c752015-02-17 11:11:11 +0530334 self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate,
335 self.doc.precision("base_discount_amount"))
336
Nabin Haite7679702015-02-20 14:40:35 +0530337 total_for_discount_amount = self.get_total_for_discount_amount()
Nabin Hait25bd84d2015-03-04 15:06:56 +0530338 taxes = self.doc.get("taxes")
339 net_total = 0
Nabin Hait3237c752015-02-17 11:11:11 +0530340
Nabin Haite7679702015-02-20 14:40:35 +0530341 if total_for_discount_amount:
Nabin Hait3237c752015-02-17 11:11:11 +0530342 # calculate item amount after Discount Amount
Nabin Hait25bd84d2015-03-04 15:06:56 +0530343 for i, item in enumerate(self.doc.get("items")):
344 distributed_amount = flt(self.doc.discount_amount) * \
345 item.net_amount / total_for_discount_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530346
Nabin Haite7679702015-02-20 14:40:35 +0530347 item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
Nabin Hait25bd84d2015-03-04 15:06:56 +0530348 net_total += item.net_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530349
Nabin Hait25bd84d2015-03-04 15:06:56 +0530350 # discount amount rounding loss adjustment if no taxes
351 if (not taxes or self.doc.apply_discount_on == "Net Total") \
352 and i == len(self.doc.get("items")) - 1:
Anand Doshiec5ec602015-03-05 19:31:23 +0530353 discount_amount_loss = flt(self.doc.total - net_total - self.doc.discount_amount,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530354 self.doc.precision("net_total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530355 item.net_amount = flt(item.net_amount + discount_amount_loss,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530356 item.precision("net_amount"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530357
Nabin Hait51e980d2015-10-10 18:10:05 +0530358 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 +0530359
Nabin Haite7679702015-02-20 14:40:35 +0530360 self._set_in_company_currency(item, ["net_rate", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530361
362 self.discount_amount_applied = True
363 self._calculate()
364 else:
365 self.doc.base_discount_amount = 0
366
Nabin Haite7679702015-02-20 14:40:35 +0530367 def get_total_for_discount_amount(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +0530368 if self.doc.apply_discount_on == "Net Total":
369 return self.doc.net_total
Nabin Haite7679702015-02-20 14:40:35 +0530370 else:
371 actual_taxes_dict = {}
Nabin Hait3237c752015-02-17 11:11:11 +0530372
Nabin Haite7679702015-02-20 14:40:35 +0530373 for tax in self.doc.get("taxes"):
374 if tax.charge_type == "Actual":
375 actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
376 elif tax.row_id in actual_taxes_dict:
377 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * flt(tax.rate) / 100
378 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530379
Nabin Haite7679702015-02-20 14:40:35 +0530380 return flt(self.doc.grand_total - sum(actual_taxes_dict.values()), self.doc.precision("grand_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530381
382
Nabin Hait7b19b9e2015-02-24 09:42:24 +0530383 def calculate_total_advance(self):
384 if self.doc.docstatus < 2:
Nabin Haite7679702015-02-20 14:40:35 +0530385 total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530386 for adv in self.doc.get("advances")])
387
Nabin Haite7679702015-02-20 14:40:35 +0530388 self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
Nabin Hait3237c752015-02-17 11:11:11 +0530389
Rushabh Mehta8bb6e532015-02-18 20:22:59 +0530390 if self.doc.docstatus == 0:
Nabin Hait3237c752015-02-17 11:11:11 +0530391 self.calculate_outstanding_amount()
392
393 def calculate_outstanding_amount(self):
394 # NOTE:
395 # write_off_amount is only for POS Invoice
396 # total_advance is only for non POS Invoice
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530397 if self.doc.is_return:
398 return
399
400 self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530401 if self.doc.party_account_currency == self.doc.currency:
402 total_amount_to_pay = flt(self.doc.grand_total - self.doc.total_advance
403 - flt(self.doc.write_off_amount), self.doc.precision("grand_total"))
404 else:
405 total_amount_to_pay = flt(self.doc.base_grand_total - self.doc.total_advance
406 - flt(self.doc.base_write_off_amount), self.doc.precision("grand_total"))
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530407
Nabin Haitbd00e812015-02-17 12:50:51 +0530408 if self.doc.doctype == "Sales Invoice":
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530409 self.doc.round_floats_in(self.doc, ["paid_amount"])
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530410 paid_amount = self.doc.paid_amount \
411 if self.doc.party_account_currency == self.doc.currency else self.doc.base_paid_amount
412 self.doc.outstanding_amount = flt(total_amount_to_pay - flt(paid_amount),
413 self.doc.precision("outstanding_amount"))
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530414 elif self.doc.doctype == "Purchase Invoice":
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530415 self.doc.outstanding_amount = flt(total_amount_to_pay, self.doc.precision("outstanding_amount"))