blob: 1b1527b7eb0538c6c229d2c81449940e104339d5 [file] [log] [blame]
Nabin Hait3237c752015-02-17 11:11:11 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5import json
6from frappe import _, throw
7from frappe.utils import cint, flt, rounded
8from erpnext.setup.utils import get_company_currency
9from erpnext.controllers.accounts_controller import validate_conversion_rate
10
Nabin Haitfe81da22015-02-18 12:23:18 +053011class calculate_taxes_and_totals(object):
Nabin Hait3237c752015-02-17 11:11:11 +053012 def __init__(self, doc):
13 self.doc = doc
Nabin Haitfe81da22015-02-18 12:23:18 +053014 self.calculate()
15
Nabin Hait3237c752015-02-17 11:11:11 +053016 def calculate(self):
17 self.discount_amount_applied = False
18 self._calculate()
19
20 if self.doc.meta.get_field("discount_amount"):
21 self.apply_discount_amount()
22
Nabin Haitbd00e812015-02-17 12:50:51 +053023 if self.doc.doctype in ["Sales Invoice", "Purchase Invoice"]:
Nabin Hait3237c752015-02-17 11:11:11 +053024 self.calculate_total_advance()
25
26 def _calculate(self):
Nabin Haite7679702015-02-20 14:40:35 +053027 self.calculate_item_values()
28 self.initialize_taxes()
29 self.determine_exclusive_rate()
30 self.calculate_net_total()
31 self.calculate_taxes()
32 self.calculate_totals()
33 self._cleanup()
34
35 def validate_conversion_rate(self):
Nabin Hait3237c752015-02-17 11:11:11 +053036 # validate conversion rate
37 company_currency = get_company_currency(self.doc.company)
38 if not self.doc.currency or self.doc.currency == company_currency:
39 self.doc.currency = company_currency
40 self.doc.conversion_rate = 1.0
41 else:
42 validate_conversion_rate(self.doc.currency, self.doc.conversion_rate,
43 self.doc.meta.get_label("conversion_rate"), self.doc.company)
44
45 self.doc.conversion_rate = flt(self.doc.conversion_rate)
46
Nabin Hait3237c752015-02-17 11:11:11 +053047 def calculate_item_values(self):
48 if not self.discount_amount_applied:
49 for item in self.doc.get("items"):
50 self.doc.round_floats_in(item)
51
52 if item.discount_percentage == 100:
53 item.rate = 0.0
54 elif not item.rate:
Nabin Haite7679702015-02-20 14:40:35 +053055 item.rate = flt(item.price_list_rate *
56 (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
Nabin Hait3237c752015-02-17 11:11:11 +053057
Nabin Haite7679702015-02-20 14:40:35 +053058 item.net_rate = item.rate
59 item.amount = flt(item.rate * item.qty, item.precision("amount"))
60 item.net_amount = item.amount
Nabin Hait3237c752015-02-17 11:11:11 +053061
Nabin Haite7679702015-02-20 14:40:35 +053062 self._set_in_company_currency(item, ["price_list_rate", "rate", "net_rate", "amount", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +053063
Nabin Haite7679702015-02-20 14:40:35 +053064 item.item_tax_amount = 0.0
65
66 def _set_in_company_currency(self, doc, fields):
Nabin Hait3237c752015-02-17 11:11:11 +053067 """set values in base currency"""
Nabin Haite7679702015-02-20 14:40:35 +053068 for f in fields:
69 val = flt(flt(doc.get(f), doc.precision(f)) * self.doc.conversion_rate, doc.precision("base_" + f))
70 doc.set("base_" + f, val)
Nabin Hait3237c752015-02-17 11:11:11 +053071
72 def initialize_taxes(self):
73 for tax in self.doc.get("taxes"):
74 tax.item_wise_tax_detail = {}
75 tax_fields = ["total", "tax_amount_after_discount_amount",
76 "tax_amount_for_current_item", "grand_total_for_current_item",
77 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
78
Nabin Haitde9c8a92015-02-23 01:06:00 +053079 if tax.charge_type != "Actual" and \
80 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
81 tax_fields.append("tax_amount")
Nabin Hait3237c752015-02-17 11:11:11 +053082
83 for fieldname in tax_fields:
84 tax.set(fieldname, 0.0)
85
86 self.validate_on_previous_row(tax)
87 self.validate_inclusive_tax(tax)
88 self.doc.round_floats_in(tax)
89
90 def validate_on_previous_row(self, tax):
91 """
92 validate if a valid row id is mentioned in case of
93 On Previous Row Amount and On Previous Row Total
94 """
95 if tax.charge_type in ["On Previous Row Amount", "On Previous Row Total"] and \
96 (not tax.row_id or cint(tax.row_id) >= tax.idx):
97 throw(_("Please specify a valid Row ID for {0} in row {1}").format(_(tax.doctype), tax.idx))
98
99 def validate_inclusive_tax(self, tax):
100 def _on_previous_row_error(row_range):
101 throw(_("To include tax in row {0} in Item rate, taxes in rows {1} must also be included").format(tax.idx,
102 row_range))
103
104 if cint(getattr(tax, "included_in_print_rate", None)):
105 if tax.charge_type == "Actual":
106 # inclusive tax cannot be of type Actual
107 throw(_("Charge of type 'Actual' in row {0} cannot be included in Item Rate").format(tax.idx))
108 elif tax.charge_type == "On Previous Row Amount" and \
109 not cint(self.doc.get("taxes")[cint(tax.row_id) - 1].included_in_print_rate):
110 # referred row should also be inclusive
111 _on_previous_row_error(tax.row_id)
112 elif tax.charge_type == "On Previous Row Total" and \
113 not all([cint(t.included_in_print_rate) for t in self.doc.get("taxes")[:cint(tax.row_id) - 1]]):
114 # all rows about the reffered tax should be inclusive
115 _on_previous_row_error("1 - %d" % (tax.row_id,))
116
117 def determine_exclusive_rate(self):
Nabin Haite7679702015-02-20 14:40:35 +0530118 if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))) or \
119 self.doc.doctype not in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
120 return
Nabin Hait3237c752015-02-17 11:11:11 +0530121
122 for item in self.doc.get("items"):
123 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
124 cumulated_tax_fraction = 0
125 for i, tax in enumerate(self.doc.get("taxes")):
126 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
127
128 if i==0:
129 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
130 else:
131 tax.grand_total_fraction_for_current_item = \
132 self.doc.get("taxes")[i-1].grand_total_fraction_for_current_item \
133 + tax.tax_fraction_for_current_item
134
135 cumulated_tax_fraction += tax.tax_fraction_for_current_item
136
137 if cumulated_tax_fraction and not self.discount_amount_applied and item.qty:
Nabin Haite7679702015-02-20 14:40:35 +0530138 item.net_amount = flt(item.amount / (1 + cumulated_tax_fraction), item.precision("net_amount"))
139 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
140 item.discount_percentage = flt(item.discount_percentage, item.precision("discount_percentage"))
Nabin Hait3237c752015-02-17 11:11:11 +0530141
Nabin Hait3237c752015-02-17 11:11:11 +0530142
Nabin Haite7679702015-02-20 14:40:35 +0530143 self._set_in_company_currency(item, ["net_rate", "net_amount"])
144
145 # below part need to be fixed???
146
147 # if item.discount_percentage == 100:
148 # item.price_list_rate = item.net_rate
149 # item.base_price_list_rate = flt(item.price_list_rate*self.doc.conversion_rate,
150 # self.doc.precision("base_price_list_rate", item))
151 # item.rate = item.base_rate = item.net_rate = item.base_net_rate = 0.0
152 # else:
153 # item.base_price_list_rate = flt(item.net_rate / (1 - (item.discount_percentage / 100.0)),
154 # self.doc.precision("price_list_rate", item))
155
Nabin Hait3237c752015-02-17 11:11:11 +0530156
157 def _load_item_tax_rate(self, item_tax_rate):
158 return json.loads(item_tax_rate) if item_tax_rate else {}
159
160 def get_current_tax_fraction(self, tax, item_tax_map):
161 """
162 Get tax fraction for calculating tax exclusive amount
163 from tax inclusive amount
164 """
165 current_tax_fraction = 0
166
167 if cint(tax.included_in_print_rate):
168 tax_rate = self._get_tax_rate(tax, item_tax_map)
169
170 if tax.charge_type == "On Net Total":
171 current_tax_fraction = tax_rate / 100.0
172
173 elif tax.charge_type == "On Previous Row Amount":
174 current_tax_fraction = (tax_rate / 100.0) * \
175 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item
176
177 elif tax.charge_type == "On Previous Row Total":
178 current_tax_fraction = (tax_rate / 100.0) * \
179 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
180
181 return current_tax_fraction
182
183 def _get_tax_rate(self, tax, item_tax_map):
184 if item_tax_map.has_key(tax.account_head):
185 return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax))
186 else:
187 return tax.rate
188
189 def calculate_net_total(self):
Nabin Haite7679702015-02-20 14:40:35 +0530190 self.doc.print_total = self.doc.base_print_total = self.doc.net_total = self.doc.base_net_total = 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530191
192 for item in self.doc.get("items"):
Nabin Haite7679702015-02-20 14:40:35 +0530193 self.doc.print_total += item.amount
194 self.doc.base_print_total += item.base_amount
195 self.doc.net_total += item.net_amount
196 self.doc.base_net_total += item.base_net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530197
Nabin Haite7679702015-02-20 14:40:35 +0530198 self.doc.round_floats_in(self.doc, ["print_total", "base_print_total", "net_total", "base_net_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530199
200 def calculate_taxes(self):
201 # maintain actual tax rate based on idx
Nabin Haite7679702015-02-20 14:40:35 +0530202 actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
Nabin Hait3237c752015-02-17 11:11:11 +0530203 for tax in self.doc.get("taxes") if tax.charge_type == "Actual"])
204
205 for n, item in enumerate(self.doc.get("items")):
206 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
207
208 for i, tax in enumerate(self.doc.get("taxes")):
209 # tax_amount represents the amount of tax for the current step
210 current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
211
212 # Adjust divisional loss to the last item
213 if tax.charge_type == "Actual":
214 actual_tax_dict[tax.idx] -= current_tax_amount
215 if n == len(self.doc.get("items")) - 1:
216 current_tax_amount += actual_tax_dict[tax.idx]
217
Nabin Hait2b019ed2015-02-22 23:03:07 +0530218 # accumulate tax amount into tax.tax_amount
Nabin Haitde9c8a92015-02-23 01:06:00 +0530219 if tax.charge_type != "Actual" and \
220 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
221 tax.tax_amount += current_tax_amount
Nabin Hait2b019ed2015-02-22 23:03:07 +0530222
Nabin Hait3237c752015-02-17 11:11:11 +0530223 # store tax_amount for current item as it will be used for
224 # charge type = 'On Previous Row Amount'
225 tax.tax_amount_for_current_item = current_tax_amount
226
Nabin Hait2b019ed2015-02-22 23:03:07 +0530227 # set tax after discount
Nabin Hait3237c752015-02-17 11:11:11 +0530228 tax.tax_amount_after_discount_amount += current_tax_amount
229
230 if getattr(tax, "category", None):
231 # if just for valuation, do not add the tax amount in total
232 # hence, setting it as 0 for further steps
233 current_tax_amount = 0.0 if (tax.category == "Valuation") \
234 else current_tax_amount
235
236 current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
237
238 # Calculate tax.total viz. grand total till that step
239 # note: grand_total_for_current_item contains the contribution of
240 # item's amount, previously applied tax and the current tax on that item
241 if i==0:
Nabin Haite7679702015-02-20 14:40:35 +0530242 tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530243 else:
244 tax.grand_total_for_current_item = \
Nabin Haite7679702015-02-20 14:40:35 +0530245 self.doc.get("taxes")[i-1].grand_total_for_current_item + current_tax_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530246
247 # in tax.total, accumulate grand total of each item
248 tax.total += tax.grand_total_for_current_item
249
250 # set precision in the last item iteration
251 if n == len(self.doc.get("items")) - 1:
252 self.round_off_totals(tax)
253
254 # adjust Discount Amount loss in last tax iteration
Nabin Haitde9c8a92015-02-23 01:06:00 +0530255 if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
256 and self.doc.apply_discount_on == "Grand Total":
257 self.adjust_discount_amount_loss(tax)
Nabin Hait3237c752015-02-17 11:11:11 +0530258
259 def get_current_tax_amount(self, item, tax, item_tax_map):
260 tax_rate = self._get_tax_rate(tax, item_tax_map)
261 current_tax_amount = 0.0
262
263 if tax.charge_type == "Actual":
264 # distribute the tax amount proportionally to each item row
Nabin Haite7679702015-02-20 14:40:35 +0530265 actual = flt(tax.tax_amount, tax.precision("tax_amount"))
266 current_tax_amount = item.net_amount*actual / self.doc.net_total if self.doc.net_total else 0.0
267
Nabin Hait3237c752015-02-17 11:11:11 +0530268 elif tax.charge_type == "On Net Total":
Nabin Haite7679702015-02-20 14:40:35 +0530269 current_tax_amount = (tax_rate / 100.0) * item.net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530270 elif tax.charge_type == "On Previous Row Amount":
271 current_tax_amount = (tax_rate / 100.0) * \
272 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item
273 elif tax.charge_type == "On Previous Row Total":
274 current_tax_amount = (tax_rate / 100.0) * \
275 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item
276
Nabin Haite7679702015-02-20 14:40:35 +0530277 # current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount", tax))
Nabin Hait3237c752015-02-17 11:11:11 +0530278
Nabin Haite7679702015-02-20 14:40:35 +0530279 self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530280
281 return current_tax_amount
282
Nabin Haite7679702015-02-20 14:40:35 +0530283 def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount):
284 # store tax breakup for each item
285 key = item.item_code or item.item_name
286 item_wise_tax_amount = current_tax_amount*self.doc.conversion_rate
287 if tax.item_wise_tax_detail.get(key):
288 item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
289
290 tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount, tax.precision("base_tax_amount"))]
291
Nabin Hait3237c752015-02-17 11:11:11 +0530292 def round_off_totals(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530293 tax.total = flt(tax.total, tax.precision("total"))
294 tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
295 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530296
Nabin Haitde9c8a92015-02-23 01:06:00 +0530297 self._set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Haitce245122015-02-22 20:14:49 +0530298
Nabin Hait3237c752015-02-17 11:11:11 +0530299 def adjust_discount_amount_loss(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530300 discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
Nabin Hait3237c752015-02-17 11:11:11 +0530301 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
302 discount_amount_loss, self.doc.precision("tax_amount", tax))
303 tax.total = flt(tax.total + discount_amount_loss, self.doc.precision("total", tax))
304
305 def calculate_totals(self):
Nabin Haite7679702015-02-20 14:40:35 +0530306 self.doc.grand_total = flt(self.doc.get("taxes")[-1].total
307 if self.doc.get("taxes") else self.doc.net_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530308
Nabin Haitbd00e812015-02-17 12:50:51 +0530309 if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
Nabin Haite7679702015-02-20 14:40:35 +0530310 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
311 if self.doc.total_taxes_and_charges else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530312 else:
Nabin Haite7679702015-02-20 14:40:35 +0530313 self.doc.taxes_and_charges_added, self.taxes_and_charges_deducted = 0.0, 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530314 for tax in self.doc.get("taxes"):
315 if tax.category in ["Valuation and Total", "Total"]:
316 if tax.add_deduct_tax == "Add":
Nabin Haite7679702015-02-20 14:40:35 +0530317 self.doc.taxes_and_charges_added += flt(tax.tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530318 else:
Nabin Haite7679702015-02-20 14:40:35 +0530319 self.doc.taxes_and_charges_deducted += flt(tax.tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530320
Nabin Haite7679702015-02-20 14:40:35 +0530321 self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530322
Nabin Haite7679702015-02-20 14:40:35 +0530323 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
324 if (self.doc.taxes_and_charges_added or self.doc.taxes_and_charges_deducted) \
325 else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530326
Nabin Haite7679702015-02-20 14:40:35 +0530327 self._set_in_company_currency(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530328
Nabin Haite7679702015-02-20 14:40:35 +0530329 self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total,
330 self.doc.precision("total_taxes_and_charges"))
Nabin Hait3237c752015-02-17 11:11:11 +0530331
Nabin Haite7679702015-02-20 14:40:35 +0530332 self._set_in_company_currency(self.doc, ["total_taxes_and_charges"])
333 self.doc.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530334
Nabin Hait3237c752015-02-17 11:11:11 +0530335 if self.doc.meta.get_field("rounded_total"):
336 self.doc.rounded_total = rounded(self.doc.grand_total)
Nabin Haite7679702015-02-20 14:40:35 +0530337 if self.doc.meta.get_field("base_rounded_total"):
338 self.doc.base_rounded_total = rounded(self.doc.base_grand_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530339
340 def _cleanup(self):
341 for tax in self.doc.get("taxes"):
342 tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':'))
343
344 def apply_discount_amount(self):
345 if self.doc.discount_amount:
346 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 Hait3237c752015-02-17 11:11:11 +0530350
Nabin Haite7679702015-02-20 14:40:35 +0530351 if total_for_discount_amount:
Nabin Hait3237c752015-02-17 11:11:11 +0530352 # calculate item amount after Discount Amount
353 for item in self.doc.get("items"):
Nabin Haite7679702015-02-20 14:40:35 +0530354 distributed_amount = flt(self.doc.discount_amount) * item.net_amount / total_for_discount_amount
355 item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
356 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
357
358 self._set_in_company_currency(item, ["net_rate", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530359
360 self.discount_amount_applied = True
361 self._calculate()
362 else:
363 self.doc.base_discount_amount = 0
364
Nabin Haite7679702015-02-20 14:40:35 +0530365 def get_total_for_discount_amount(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +0530366 if self.doc.apply_discount_on == "Net Total":
367 return self.doc.net_total
Nabin Haite7679702015-02-20 14:40:35 +0530368 else:
369 actual_taxes_dict = {}
Nabin Hait3237c752015-02-17 11:11:11 +0530370
Nabin Haite7679702015-02-20 14:40:35 +0530371 for tax in self.doc.get("taxes"):
372 if tax.charge_type == "Actual":
373 actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
374 elif tax.row_id in actual_taxes_dict:
375 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * flt(tax.rate) / 100
376 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530377
Nabin Haite7679702015-02-20 14:40:35 +0530378 return flt(self.doc.grand_total - sum(actual_taxes_dict.values()), self.doc.precision("grand_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530379
380
Nabin Haite7679702015-02-20 14:40:35 +0530381 def calculate_total_advance(self, parenttype, advance_parentfield):
382 if self.docstatus < 2:
383 total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530384 for adv in self.doc.get("advances")])
385
Nabin Haite7679702015-02-20 14:40:35 +0530386 self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
Nabin Hait3237c752015-02-17 11:11:11 +0530387
Rushabh Mehta8bb6e532015-02-18 20:22:59 +0530388 if self.doc.docstatus == 0:
Nabin Hait3237c752015-02-17 11:11:11 +0530389 self.calculate_outstanding_amount()
390
391 def calculate_outstanding_amount(self):
392 # NOTE:
393 # write_off_amount is only for POS Invoice
394 # total_advance is only for non POS Invoice
395
Nabin Haitbd00e812015-02-17 12:50:51 +0530396 if self.doc.doctype == "Sales Invoice":
397 self.doc.round_floats_in(self.doc, ["base_grand_total", "total_advance", "write_off_amount", "paid_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530398 total_amount_to_pay = self.doc.base_grand_total - self.doc.write_off_amount
399 self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance - self.doc.paid_amount,
400 self.doc.precision("outstanding_amount"))
401 else:
Nabin Haitbd00e812015-02-17 12:50:51 +0530402 self.doc.round_floats_in(self.doc, ["total_advance", "write_off_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530403 self.doc.total_amount_to_pay = flt(self.doc.base_grand_total - self.doc.write_off_amount,
404 self.doc.precision("total_amount_to_pay"))
405 self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
406 self.doc.precision("outstanding_amount"))