blob: c846dd08cdc948e47ff5abadb054e5301ec3e816 [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
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()
34 self.calculate_totals()
35 self._cleanup()
36
37 def validate_conversion_rate(self):
Nabin Hait3237c752015-02-17 11:11:11 +053038 # validate conversion rate
39 company_currency = get_company_currency(self.doc.company)
40 if not self.doc.currency or self.doc.currency == company_currency:
41 self.doc.currency = company_currency
42 self.doc.conversion_rate = 1.0
43 else:
44 validate_conversion_rate(self.doc.currency, self.doc.conversion_rate,
45 self.doc.meta.get_label("conversion_rate"), self.doc.company)
46
47 self.doc.conversion_rate = flt(self.doc.conversion_rate)
48
Nabin Hait3237c752015-02-17 11:11:11 +053049 def calculate_item_values(self):
50 if not self.discount_amount_applied:
51 for item in self.doc.get("items"):
52 self.doc.round_floats_in(item)
53
54 if item.discount_percentage == 100:
55 item.rate = 0.0
56 elif not item.rate:
Nabin Haite7679702015-02-20 14:40:35 +053057 item.rate = flt(item.price_list_rate *
58 (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
Nabin Hait3237c752015-02-17 11:11:11 +053059
Nabin Haite7679702015-02-20 14:40:35 +053060 item.net_rate = item.rate
61 item.amount = flt(item.rate * item.qty, item.precision("amount"))
62 item.net_amount = item.amount
Nabin Hait3237c752015-02-17 11:11:11 +053063
Nabin Haite7679702015-02-20 14:40:35 +053064 self._set_in_company_currency(item, ["price_list_rate", "rate", "net_rate", "amount", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +053065
Nabin Haite7679702015-02-20 14:40:35 +053066 item.item_tax_amount = 0.0
67
68 def _set_in_company_currency(self, doc, fields):
Nabin Hait3237c752015-02-17 11:11:11 +053069 """set values in base currency"""
Nabin Haite7679702015-02-20 14:40:35 +053070 for f in fields:
71 val = flt(flt(doc.get(f), doc.precision(f)) * self.doc.conversion_rate, doc.precision("base_" + f))
72 doc.set("base_" + f, val)
Nabin Hait3237c752015-02-17 11:11:11 +053073
74 def initialize_taxes(self):
75 for tax in self.doc.get("taxes"):
Nabin Hait86cd4cc2015-02-28 19:11:51 +053076 if not self.discount_amount_applied:
77 validate_taxes_and_charges(tax)
78 validate_inclusive_tax(tax, self.doc)
Nabin Hait613d0812015-02-23 11:58:15 +053079
Nabin Hait3237c752015-02-17 11:11:11 +053080 tax.item_wise_tax_detail = {}
81 tax_fields = ["total", "tax_amount_after_discount_amount",
82 "tax_amount_for_current_item", "grand_total_for_current_item",
83 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
84
Nabin Haitde9c8a92015-02-23 01:06:00 +053085 if tax.charge_type != "Actual" and \
86 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
87 tax_fields.append("tax_amount")
Nabin Hait3237c752015-02-17 11:11:11 +053088
89 for fieldname in tax_fields:
90 tax.set(fieldname, 0.0)
91
Nabin Hait3237c752015-02-17 11:11:11 +053092 self.doc.round_floats_in(tax)
93
Nabin Hait3237c752015-02-17 11:11:11 +053094 def determine_exclusive_rate(self):
Nabin Hait37b047d2015-02-23 16:01:33 +053095 if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))):
96 return
Nabin Hait3237c752015-02-17 11:11:11 +053097
98 for item in self.doc.get("items"):
99 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
100 cumulated_tax_fraction = 0
101 for i, tax in enumerate(self.doc.get("taxes")):
102 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
103
104 if i==0:
105 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
106 else:
107 tax.grand_total_fraction_for_current_item = \
108 self.doc.get("taxes")[i-1].grand_total_fraction_for_current_item \
109 + tax.tax_fraction_for_current_item
110
111 cumulated_tax_fraction += tax.tax_fraction_for_current_item
112
113 if cumulated_tax_fraction and not self.discount_amount_applied and item.qty:
Nabin Haite7679702015-02-20 14:40:35 +0530114 item.net_amount = flt(item.amount / (1 + cumulated_tax_fraction), item.precision("net_amount"))
115 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
116 item.discount_percentage = flt(item.discount_percentage, item.precision("discount_percentage"))
Nabin Hait3237c752015-02-17 11:11:11 +0530117
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
121 # below part need to be fixed???
122
123 # if item.discount_percentage == 100:
124 # item.price_list_rate = item.net_rate
125 # item.base_price_list_rate = flt(item.price_list_rate*self.doc.conversion_rate,
126 # self.doc.precision("base_price_list_rate", item))
127 # item.rate = item.base_rate = item.net_rate = item.base_net_rate = 0.0
128 # else:
129 # item.base_price_list_rate = flt(item.net_rate / (1 - (item.discount_percentage / 100.0)),
130 # self.doc.precision("price_list_rate", item))
131
Nabin Hait3237c752015-02-17 11:11:11 +0530132
133 def _load_item_tax_rate(self, item_tax_rate):
134 return json.loads(item_tax_rate) if item_tax_rate else {}
135
136 def get_current_tax_fraction(self, tax, item_tax_map):
137 """
138 Get tax fraction for calculating tax exclusive amount
139 from tax inclusive amount
140 """
141 current_tax_fraction = 0
142
143 if cint(tax.included_in_print_rate):
144 tax_rate = self._get_tax_rate(tax, item_tax_map)
145
146 if tax.charge_type == "On Net Total":
147 current_tax_fraction = tax_rate / 100.0
148
149 elif tax.charge_type == "On Previous Row Amount":
150 current_tax_fraction = (tax_rate / 100.0) * \
151 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item
152
153 elif tax.charge_type == "On Previous Row Total":
154 current_tax_fraction = (tax_rate / 100.0) * \
155 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
156
157 return current_tax_fraction
158
159 def _get_tax_rate(self, tax, item_tax_map):
160 if item_tax_map.has_key(tax.account_head):
161 return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax))
162 else:
163 return tax.rate
164
165 def calculate_net_total(self):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530166 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 +0530167
168 for item in self.doc.get("items"):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530169 self.doc.total += item.amount
170 self.doc.base_total += item.base_amount
Nabin Haite7679702015-02-20 14:40:35 +0530171 self.doc.net_total += item.net_amount
172 self.doc.base_net_total += item.base_net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530173
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530174 self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530175
176 def calculate_taxes(self):
177 # maintain actual tax rate based on idx
Nabin Haite7679702015-02-20 14:40:35 +0530178 actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
Nabin Hait3237c752015-02-17 11:11:11 +0530179 for tax in self.doc.get("taxes") if tax.charge_type == "Actual"])
180
181 for n, item in enumerate(self.doc.get("items")):
182 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
183
184 for i, tax in enumerate(self.doc.get("taxes")):
185 # tax_amount represents the amount of tax for the current step
186 current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
187
188 # Adjust divisional loss to the last item
189 if tax.charge_type == "Actual":
190 actual_tax_dict[tax.idx] -= current_tax_amount
191 if n == len(self.doc.get("items")) - 1:
192 current_tax_amount += actual_tax_dict[tax.idx]
193
Nabin Hait2b019ed2015-02-22 23:03:07 +0530194 # accumulate tax amount into tax.tax_amount
Nabin Haitde9c8a92015-02-23 01:06:00 +0530195 if tax.charge_type != "Actual" and \
196 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
197 tax.tax_amount += current_tax_amount
Nabin Hait2b019ed2015-02-22 23:03:07 +0530198
Nabin Hait3237c752015-02-17 11:11:11 +0530199 # store tax_amount for current item as it will be used for
200 # charge type = 'On Previous Row Amount'
201 tax.tax_amount_for_current_item = current_tax_amount
202
Nabin Hait2b019ed2015-02-22 23:03:07 +0530203 # set tax after discount
Nabin Hait3237c752015-02-17 11:11:11 +0530204 tax.tax_amount_after_discount_amount += current_tax_amount
205
206 if getattr(tax, "category", None):
207 # if just for valuation, do not add the tax amount in total
208 # hence, setting it as 0 for further steps
209 current_tax_amount = 0.0 if (tax.category == "Valuation") \
210 else current_tax_amount
211
212 current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
213
214 # Calculate tax.total viz. grand total till that step
215 # note: grand_total_for_current_item contains the contribution of
216 # item's amount, previously applied tax and the current tax on that item
217 if i==0:
Nabin Hait93b3a372015-02-24 17:08:34 +0530218 tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount, tax.precision("total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530219 else:
220 tax.grand_total_for_current_item = \
Rushabh Mehta6cdfd1e2015-02-25 15:55:57 +0530221 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 +0530222
223 # in tax.total, accumulate grand total of each item
224 tax.total += tax.grand_total_for_current_item
225
226 # set precision in the last item iteration
227 if n == len(self.doc.get("items")) - 1:
228 self.round_off_totals(tax)
229
230 # adjust Discount Amount loss in last tax iteration
Nabin Haitde9c8a92015-02-23 01:06:00 +0530231 if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
232 and self.doc.apply_discount_on == "Grand Total":
233 self.adjust_discount_amount_loss(tax)
Nabin Hait3237c752015-02-17 11:11:11 +0530234
235 def get_current_tax_amount(self, item, tax, item_tax_map):
236 tax_rate = self._get_tax_rate(tax, item_tax_map)
237 current_tax_amount = 0.0
238
239 if tax.charge_type == "Actual":
240 # distribute the tax amount proportionally to each item row
Nabin Haite7679702015-02-20 14:40:35 +0530241 actual = flt(tax.tax_amount, tax.precision("tax_amount"))
242 current_tax_amount = item.net_amount*actual / self.doc.net_total if self.doc.net_total else 0.0
243
Nabin Hait3237c752015-02-17 11:11:11 +0530244 elif tax.charge_type == "On Net Total":
Nabin Haite7679702015-02-20 14:40:35 +0530245 current_tax_amount = (tax_rate / 100.0) * item.net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530246 elif tax.charge_type == "On Previous Row Amount":
247 current_tax_amount = (tax_rate / 100.0) * \
248 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item
249 elif tax.charge_type == "On Previous Row Total":
250 current_tax_amount = (tax_rate / 100.0) * \
251 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item
252
Nabin Hait93b3a372015-02-24 17:08:34 +0530253 current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530254
Nabin Haite7679702015-02-20 14:40:35 +0530255 self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530256
257 return current_tax_amount
258
Nabin Haite7679702015-02-20 14:40:35 +0530259 def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount):
260 # store tax breakup for each item
261 key = item.item_code or item.item_name
262 item_wise_tax_amount = current_tax_amount*self.doc.conversion_rate
263 if tax.item_wise_tax_detail.get(key):
264 item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
265
266 tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount, tax.precision("base_tax_amount"))]
267
Nabin Hait3237c752015-02-17 11:11:11 +0530268 def round_off_totals(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530269 tax.total = flt(tax.total, tax.precision("total"))
270 tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
271 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530272
Nabin Haitde9c8a92015-02-23 01:06:00 +0530273 self._set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Haitce245122015-02-22 20:14:49 +0530274
Nabin Hait3237c752015-02-17 11:11:11 +0530275 def adjust_discount_amount_loss(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530276 discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
Nabin Hait3237c752015-02-17 11:11:11 +0530277 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
278 discount_amount_loss, self.doc.precision("tax_amount", tax))
279 tax.total = flt(tax.total + discount_amount_loss, self.doc.precision("total", tax))
280
281 def calculate_totals(self):
Nabin Haite7679702015-02-20 14:40:35 +0530282 self.doc.grand_total = flt(self.doc.get("taxes")[-1].total
283 if self.doc.get("taxes") else self.doc.net_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530284
Nabin Haitbd00e812015-02-17 12:50:51 +0530285 if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
Nabin Haite7679702015-02-20 14:40:35 +0530286 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
287 if self.doc.total_taxes_and_charges else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530288 else:
Nabin Haite7679702015-02-20 14:40:35 +0530289 self.doc.taxes_and_charges_added, self.taxes_and_charges_deducted = 0.0, 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530290 for tax in self.doc.get("taxes"):
291 if tax.category in ["Valuation and Total", "Total"]:
292 if tax.add_deduct_tax == "Add":
Nabin Haite7679702015-02-20 14:40:35 +0530293 self.doc.taxes_and_charges_added += flt(tax.tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530294 else:
Nabin Haite7679702015-02-20 14:40:35 +0530295 self.doc.taxes_and_charges_deducted += flt(tax.tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530296
Nabin Haite7679702015-02-20 14:40:35 +0530297 self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530298
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.taxes_and_charges_added or self.doc.taxes_and_charges_deducted) \
301 else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530302
Nabin Haite7679702015-02-20 14:40:35 +0530303 self._set_in_company_currency(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530304
Nabin Haite7679702015-02-20 14:40:35 +0530305 self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total,
306 self.doc.precision("total_taxes_and_charges"))
Nabin Hait3237c752015-02-17 11:11:11 +0530307
Nabin Haite7679702015-02-20 14:40:35 +0530308 self._set_in_company_currency(self.doc, ["total_taxes_and_charges"])
309 self.doc.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530310
Nabin Hait3237c752015-02-17 11:11:11 +0530311 if self.doc.meta.get_field("rounded_total"):
312 self.doc.rounded_total = rounded(self.doc.grand_total)
Nabin Haite7679702015-02-20 14:40:35 +0530313 if self.doc.meta.get_field("base_rounded_total"):
314 self.doc.base_rounded_total = rounded(self.doc.base_grand_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530315
316 def _cleanup(self):
317 for tax in self.doc.get("taxes"):
318 tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':'))
319
320 def apply_discount_amount(self):
321 if self.doc.discount_amount:
Nabin Hait37b047d2015-02-23 16:01:33 +0530322 if not self.doc.apply_discount_on:
323 frappe.throw(_("Please select Apply Discount On"))
324
Nabin Hait3237c752015-02-17 11:11:11 +0530325 self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate,
326 self.doc.precision("base_discount_amount"))
327
Nabin Haite7679702015-02-20 14:40:35 +0530328 total_for_discount_amount = self.get_total_for_discount_amount()
Nabin Hait3237c752015-02-17 11:11:11 +0530329
Nabin Haite7679702015-02-20 14:40:35 +0530330 if total_for_discount_amount:
Nabin Hait3237c752015-02-17 11:11:11 +0530331 # calculate item amount after Discount Amount
332 for item in self.doc.get("items"):
Nabin Haite7679702015-02-20 14:40:35 +0530333 distributed_amount = flt(self.doc.discount_amount) * item.net_amount / total_for_discount_amount
334 item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
335 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
336
337 self._set_in_company_currency(item, ["net_rate", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530338
339 self.discount_amount_applied = True
340 self._calculate()
341 else:
342 self.doc.base_discount_amount = 0
343
Nabin Haite7679702015-02-20 14:40:35 +0530344 def get_total_for_discount_amount(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +0530345 if self.doc.apply_discount_on == "Net Total":
346 return self.doc.net_total
Nabin Haite7679702015-02-20 14:40:35 +0530347 else:
348 actual_taxes_dict = {}
Nabin Hait3237c752015-02-17 11:11:11 +0530349
Nabin Haite7679702015-02-20 14:40:35 +0530350 for tax in self.doc.get("taxes"):
351 if tax.charge_type == "Actual":
352 actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
353 elif tax.row_id in actual_taxes_dict:
354 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * flt(tax.rate) / 100
355 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530356
Nabin Haite7679702015-02-20 14:40:35 +0530357 return flt(self.doc.grand_total - sum(actual_taxes_dict.values()), self.doc.precision("grand_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530358
359
Nabin Hait7b19b9e2015-02-24 09:42:24 +0530360 def calculate_total_advance(self):
361 if self.doc.docstatus < 2:
Nabin Haite7679702015-02-20 14:40:35 +0530362 total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530363 for adv in self.doc.get("advances")])
364
Nabin Haite7679702015-02-20 14:40:35 +0530365 self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
Nabin Hait3237c752015-02-17 11:11:11 +0530366
Rushabh Mehta8bb6e532015-02-18 20:22:59 +0530367 if self.doc.docstatus == 0:
Nabin Hait3237c752015-02-17 11:11:11 +0530368 self.calculate_outstanding_amount()
369
370 def calculate_outstanding_amount(self):
371 # NOTE:
372 # write_off_amount is only for POS Invoice
373 # total_advance is only for non POS Invoice
374
Nabin Haitbd00e812015-02-17 12:50:51 +0530375 if self.doc.doctype == "Sales Invoice":
376 self.doc.round_floats_in(self.doc, ["base_grand_total", "total_advance", "write_off_amount", "paid_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530377 total_amount_to_pay = self.doc.base_grand_total - self.doc.write_off_amount
378 self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance - self.doc.paid_amount,
379 self.doc.precision("outstanding_amount"))
380 else:
Nabin Haitbd00e812015-02-17 12:50:51 +0530381 self.doc.round_floats_in(self.doc, ["total_advance", "write_off_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530382 self.doc.total_amount_to_pay = flt(self.doc.base_grand_total - self.doc.write_off_amount,
383 self.doc.precision("total_amount_to_pay"))
384 self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
385 self.doc.precision("outstanding_amount"))