blob: c786d2ab20e33f28841b72a9d1f7800b3c5c943a [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 Hait613d0812015-02-23 11:58:15 +053076 validate_taxes_and_charges(tax)
77 validate_inclusive_tax(tax, self.doc)
78
Nabin Hait3237c752015-02-17 11:11:11 +053079 tax.item_wise_tax_detail = {}
80 tax_fields = ["total", "tax_amount_after_discount_amount",
81 "tax_amount_for_current_item", "grand_total_for_current_item",
82 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
83
Nabin Haitde9c8a92015-02-23 01:06:00 +053084 if tax.charge_type != "Actual" and \
85 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
86 tax_fields.append("tax_amount")
Nabin Hait3237c752015-02-17 11:11:11 +053087
88 for fieldname in tax_fields:
89 tax.set(fieldname, 0.0)
90
Nabin Hait3237c752015-02-17 11:11:11 +053091 self.doc.round_floats_in(tax)
92
Nabin Hait3237c752015-02-17 11:11:11 +053093 def determine_exclusive_rate(self):
Nabin Hait37b047d2015-02-23 16:01:33 +053094 if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))):
95 return
Nabin Hait3237c752015-02-17 11:11:11 +053096
97 for item in self.doc.get("items"):
98 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
99 cumulated_tax_fraction = 0
100 for i, tax in enumerate(self.doc.get("taxes")):
101 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
102
103 if i==0:
104 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
105 else:
106 tax.grand_total_fraction_for_current_item = \
107 self.doc.get("taxes")[i-1].grand_total_fraction_for_current_item \
108 + tax.tax_fraction_for_current_item
109
110 cumulated_tax_fraction += tax.tax_fraction_for_current_item
111
112 if cumulated_tax_fraction and not self.discount_amount_applied and item.qty:
Nabin Haite7679702015-02-20 14:40:35 +0530113 item.net_amount = flt(item.amount / (1 + cumulated_tax_fraction), item.precision("net_amount"))
114 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
115 item.discount_percentage = flt(item.discount_percentage, item.precision("discount_percentage"))
Nabin Hait3237c752015-02-17 11:11:11 +0530116
Nabin Hait3237c752015-02-17 11:11:11 +0530117
Nabin Haite7679702015-02-20 14:40:35 +0530118 self._set_in_company_currency(item, ["net_rate", "net_amount"])
119
120 # below part need to be fixed???
121
122 # if item.discount_percentage == 100:
123 # item.price_list_rate = item.net_rate
124 # item.base_price_list_rate = flt(item.price_list_rate*self.doc.conversion_rate,
125 # self.doc.precision("base_price_list_rate", item))
126 # item.rate = item.base_rate = item.net_rate = item.base_net_rate = 0.0
127 # else:
128 # item.base_price_list_rate = flt(item.net_rate / (1 - (item.discount_percentage / 100.0)),
129 # self.doc.precision("price_list_rate", item))
130
Nabin Hait3237c752015-02-17 11:11:11 +0530131
132 def _load_item_tax_rate(self, item_tax_rate):
133 return json.loads(item_tax_rate) if item_tax_rate else {}
134
135 def get_current_tax_fraction(self, tax, item_tax_map):
136 """
137 Get tax fraction for calculating tax exclusive amount
138 from tax inclusive amount
139 """
140 current_tax_fraction = 0
141
142 if cint(tax.included_in_print_rate):
143 tax_rate = self._get_tax_rate(tax, item_tax_map)
144
145 if tax.charge_type == "On Net Total":
146 current_tax_fraction = tax_rate / 100.0
147
148 elif tax.charge_type == "On Previous Row Amount":
149 current_tax_fraction = (tax_rate / 100.0) * \
150 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item
151
152 elif tax.charge_type == "On Previous Row Total":
153 current_tax_fraction = (tax_rate / 100.0) * \
154 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
155
156 return current_tax_fraction
157
158 def _get_tax_rate(self, tax, item_tax_map):
159 if item_tax_map.has_key(tax.account_head):
160 return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax))
161 else:
162 return tax.rate
163
164 def calculate_net_total(self):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530165 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 +0530166
167 for item in self.doc.get("items"):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530168 self.doc.total += item.amount
169 self.doc.base_total += item.base_amount
Nabin Haite7679702015-02-20 14:40:35 +0530170 self.doc.net_total += item.net_amount
171 self.doc.base_net_total += item.base_net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530172
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530173 self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530174
175 def calculate_taxes(self):
176 # maintain actual tax rate based on idx
Nabin Haite7679702015-02-20 14:40:35 +0530177 actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
Nabin Hait3237c752015-02-17 11:11:11 +0530178 for tax in self.doc.get("taxes") if tax.charge_type == "Actual"])
179
180 for n, item in enumerate(self.doc.get("items")):
181 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
182
183 for i, tax in enumerate(self.doc.get("taxes")):
184 # tax_amount represents the amount of tax for the current step
185 current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
186
187 # Adjust divisional loss to the last item
188 if tax.charge_type == "Actual":
189 actual_tax_dict[tax.idx] -= current_tax_amount
190 if n == len(self.doc.get("items")) - 1:
191 current_tax_amount += actual_tax_dict[tax.idx]
192
Nabin Hait2b019ed2015-02-22 23:03:07 +0530193 # accumulate tax amount into tax.tax_amount
Nabin Haitde9c8a92015-02-23 01:06:00 +0530194 if tax.charge_type != "Actual" and \
195 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
196 tax.tax_amount += current_tax_amount
Nabin Hait2b019ed2015-02-22 23:03:07 +0530197
Nabin Hait3237c752015-02-17 11:11:11 +0530198 # store tax_amount for current item as it will be used for
199 # charge type = 'On Previous Row Amount'
200 tax.tax_amount_for_current_item = current_tax_amount
201
Nabin Hait2b019ed2015-02-22 23:03:07 +0530202 # set tax after discount
Nabin Hait3237c752015-02-17 11:11:11 +0530203 tax.tax_amount_after_discount_amount += current_tax_amount
204
205 if getattr(tax, "category", None):
206 # if just for valuation, do not add the tax amount in total
207 # hence, setting it as 0 for further steps
208 current_tax_amount = 0.0 if (tax.category == "Valuation") \
209 else current_tax_amount
210
211 current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
212
213 # Calculate tax.total viz. grand total till that step
214 # note: grand_total_for_current_item contains the contribution of
215 # item's amount, previously applied tax and the current tax on that item
216 if i==0:
Nabin Haite7679702015-02-20 14:40:35 +0530217 tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530218 else:
219 tax.grand_total_for_current_item = \
Nabin Haite7679702015-02-20 14:40:35 +0530220 self.doc.get("taxes")[i-1].grand_total_for_current_item + current_tax_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530221
222 # in tax.total, accumulate grand total of each item
223 tax.total += tax.grand_total_for_current_item
224
225 # set precision in the last item iteration
226 if n == len(self.doc.get("items")) - 1:
227 self.round_off_totals(tax)
228
229 # adjust Discount Amount loss in last tax iteration
Nabin Haitde9c8a92015-02-23 01:06:00 +0530230 if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
231 and self.doc.apply_discount_on == "Grand Total":
232 self.adjust_discount_amount_loss(tax)
Nabin Hait3237c752015-02-17 11:11:11 +0530233
234 def get_current_tax_amount(self, item, tax, item_tax_map):
235 tax_rate = self._get_tax_rate(tax, item_tax_map)
236 current_tax_amount = 0.0
237
238 if tax.charge_type == "Actual":
239 # distribute the tax amount proportionally to each item row
Nabin Haite7679702015-02-20 14:40:35 +0530240 actual = flt(tax.tax_amount, tax.precision("tax_amount"))
241 current_tax_amount = item.net_amount*actual / self.doc.net_total if self.doc.net_total else 0.0
242
Nabin Hait3237c752015-02-17 11:11:11 +0530243 elif tax.charge_type == "On Net Total":
Nabin Haite7679702015-02-20 14:40:35 +0530244 current_tax_amount = (tax_rate / 100.0) * item.net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530245 elif tax.charge_type == "On Previous Row Amount":
246 current_tax_amount = (tax_rate / 100.0) * \
247 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item
248 elif tax.charge_type == "On Previous Row Total":
249 current_tax_amount = (tax_rate / 100.0) * \
250 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item
251
Nabin Haite7679702015-02-20 14:40:35 +0530252 # current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount", tax))
Nabin Hait3237c752015-02-17 11:11:11 +0530253
Nabin Haite7679702015-02-20 14:40:35 +0530254 self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530255
256 return current_tax_amount
257
Nabin Haite7679702015-02-20 14:40:35 +0530258 def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount):
259 # store tax breakup for each item
260 key = item.item_code or item.item_name
261 item_wise_tax_amount = current_tax_amount*self.doc.conversion_rate
262 if tax.item_wise_tax_detail.get(key):
263 item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
264
265 tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount, tax.precision("base_tax_amount"))]
266
Nabin Hait3237c752015-02-17 11:11:11 +0530267 def round_off_totals(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530268 tax.total = flt(tax.total, tax.precision("total"))
269 tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
270 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530271
Nabin Haitde9c8a92015-02-23 01:06:00 +0530272 self._set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Haitce245122015-02-22 20:14:49 +0530273
Nabin Hait3237c752015-02-17 11:11:11 +0530274 def adjust_discount_amount_loss(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530275 discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
Nabin Hait3237c752015-02-17 11:11:11 +0530276 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
277 discount_amount_loss, self.doc.precision("tax_amount", tax))
278 tax.total = flt(tax.total + discount_amount_loss, self.doc.precision("total", tax))
279
280 def calculate_totals(self):
Nabin Haite7679702015-02-20 14:40:35 +0530281 self.doc.grand_total = flt(self.doc.get("taxes")[-1].total
282 if self.doc.get("taxes") else self.doc.net_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530283
Nabin Haitbd00e812015-02-17 12:50:51 +0530284 if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
Nabin Haite7679702015-02-20 14:40:35 +0530285 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
286 if self.doc.total_taxes_and_charges else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530287 else:
Nabin Haite7679702015-02-20 14:40:35 +0530288 self.doc.taxes_and_charges_added, self.taxes_and_charges_deducted = 0.0, 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530289 for tax in self.doc.get("taxes"):
290 if tax.category in ["Valuation and Total", "Total"]:
291 if tax.add_deduct_tax == "Add":
Nabin Haite7679702015-02-20 14:40:35 +0530292 self.doc.taxes_and_charges_added += flt(tax.tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530293 else:
Nabin Haite7679702015-02-20 14:40:35 +0530294 self.doc.taxes_and_charges_deducted += flt(tax.tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530295
Nabin Haite7679702015-02-20 14:40:35 +0530296 self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530297
Nabin Haite7679702015-02-20 14:40:35 +0530298 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
299 if (self.doc.taxes_and_charges_added or self.doc.taxes_and_charges_deducted) \
300 else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530301
Nabin Haite7679702015-02-20 14:40:35 +0530302 self._set_in_company_currency(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530303
Nabin Haite7679702015-02-20 14:40:35 +0530304 self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total,
305 self.doc.precision("total_taxes_and_charges"))
Nabin Hait3237c752015-02-17 11:11:11 +0530306
Nabin Haite7679702015-02-20 14:40:35 +0530307 self._set_in_company_currency(self.doc, ["total_taxes_and_charges"])
308 self.doc.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530309
Nabin Hait3237c752015-02-17 11:11:11 +0530310 if self.doc.meta.get_field("rounded_total"):
311 self.doc.rounded_total = rounded(self.doc.grand_total)
Nabin Haite7679702015-02-20 14:40:35 +0530312 if self.doc.meta.get_field("base_rounded_total"):
313 self.doc.base_rounded_total = rounded(self.doc.base_grand_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530314
315 def _cleanup(self):
316 for tax in self.doc.get("taxes"):
317 tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':'))
318
319 def apply_discount_amount(self):
320 if self.doc.discount_amount:
Nabin Hait37b047d2015-02-23 16:01:33 +0530321 if not self.doc.apply_discount_on:
322 frappe.throw(_("Please select Apply Discount On"))
323
Nabin Hait3237c752015-02-17 11:11:11 +0530324 self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate,
325 self.doc.precision("base_discount_amount"))
326
Nabin Haite7679702015-02-20 14:40:35 +0530327 total_for_discount_amount = self.get_total_for_discount_amount()
Nabin Hait3237c752015-02-17 11:11:11 +0530328
Nabin Haite7679702015-02-20 14:40:35 +0530329 if total_for_discount_amount:
Nabin Hait3237c752015-02-17 11:11:11 +0530330 # calculate item amount after Discount Amount
331 for item in self.doc.get("items"):
Nabin Haite7679702015-02-20 14:40:35 +0530332 distributed_amount = flt(self.doc.discount_amount) * item.net_amount / total_for_discount_amount
333 item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
334 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
335
336 self._set_in_company_currency(item, ["net_rate", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530337
338 self.discount_amount_applied = True
339 self._calculate()
340 else:
341 self.doc.base_discount_amount = 0
342
Nabin Haite7679702015-02-20 14:40:35 +0530343 def get_total_for_discount_amount(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +0530344 if self.doc.apply_discount_on == "Net Total":
345 return self.doc.net_total
Nabin Haite7679702015-02-20 14:40:35 +0530346 else:
347 actual_taxes_dict = {}
Nabin Hait3237c752015-02-17 11:11:11 +0530348
Nabin Haite7679702015-02-20 14:40:35 +0530349 for tax in self.doc.get("taxes"):
350 if tax.charge_type == "Actual":
351 actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
352 elif tax.row_id in actual_taxes_dict:
353 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * flt(tax.rate) / 100
354 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530355
Nabin Haite7679702015-02-20 14:40:35 +0530356 return flt(self.doc.grand_total - sum(actual_taxes_dict.values()), self.doc.precision("grand_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530357
358
Nabin Hait7b19b9e2015-02-24 09:42:24 +0530359 def calculate_total_advance(self):
360 if self.doc.docstatus < 2:
Nabin Haite7679702015-02-20 14:40:35 +0530361 total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530362 for adv in self.doc.get("advances")])
363
Nabin Haite7679702015-02-20 14:40:35 +0530364 self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
Nabin Hait3237c752015-02-17 11:11:11 +0530365
Rushabh Mehta8bb6e532015-02-18 20:22:59 +0530366 if self.doc.docstatus == 0:
Nabin Hait3237c752015-02-17 11:11:11 +0530367 self.calculate_outstanding_amount()
368
369 def calculate_outstanding_amount(self):
370 # NOTE:
371 # write_off_amount is only for POS Invoice
372 # total_advance is only for non POS Invoice
373
Nabin Haitbd00e812015-02-17 12:50:51 +0530374 if self.doc.doctype == "Sales Invoice":
375 self.doc.round_floats_in(self.doc, ["base_grand_total", "total_advance", "write_off_amount", "paid_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530376 total_amount_to_pay = self.doc.base_grand_total - self.doc.write_off_amount
377 self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance - self.doc.paid_amount,
378 self.doc.precision("outstanding_amount"))
379 else:
Nabin Haitbd00e812015-02-17 12:50:51 +0530380 self.doc.round_floats_in(self.doc, ["total_advance", "write_off_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530381 self.doc.total_amount_to_pay = flt(self.doc.base_grand_total - self.doc.write_off_amount,
382 self.doc.precision("total_amount_to_pay"))
383 self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
384 self.doc.precision("outstanding_amount"))