blob: 57a41d187076bec64855ad4e89b55d675e0078b2 [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
Nabin Hait3769d872015-12-18 13:12:02 +05307from frappe import _, scrub
Nabin Haitfb0b24a2016-01-20 14:46:26 +05308from frappe.utils import cint, flt, round_based_on_smallest_currency_fraction
Nabin Hait3237c752015-02-17 11:11:11 +05309from 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"):
Nabin Hait3769d872015-12-18 13:12:02 +053023 self.set_discount_amount()
Nabin Hait3237c752015-02-17 11:11:11 +053024 self.apply_discount_amount()
25
Nabin Haitbd00e812015-02-17 12:50:51 +053026 if self.doc.doctype in ["Sales Invoice", "Purchase Invoice"]:
Nabin Hait3237c752015-02-17 11:11:11 +053027 self.calculate_total_advance()
28
29 def _calculate(self):
Nabin Haite7679702015-02-20 14:40:35 +053030 self.calculate_item_values()
31 self.initialize_taxes()
32 self.determine_exclusive_rate()
33 self.calculate_net_total()
34 self.calculate_taxes()
Nabin Haita1bf43b2015-03-17 10:50:47 +053035 self.manipulate_grand_total_for_inclusive_tax()
Nabin Haite7679702015-02-20 14:40:35 +053036 self.calculate_totals()
37 self._cleanup()
38
39 def validate_conversion_rate(self):
Nabin Hait3237c752015-02-17 11:11:11 +053040 # validate conversion rate
41 company_currency = get_company_currency(self.doc.company)
42 if not self.doc.currency or self.doc.currency == company_currency:
43 self.doc.currency = company_currency
44 self.doc.conversion_rate = 1.0
45 else:
46 validate_conversion_rate(self.doc.currency, self.doc.conversion_rate,
47 self.doc.meta.get_label("conversion_rate"), self.doc.company)
48
49 self.doc.conversion_rate = flt(self.doc.conversion_rate)
50
Nabin Hait3237c752015-02-17 11:11:11 +053051 def calculate_item_values(self):
52 if not self.discount_amount_applied:
53 for item in self.doc.get("items"):
54 self.doc.round_floats_in(item)
55
56 if item.discount_percentage == 100:
57 item.rate = 0.0
58 elif not item.rate:
Nabin Haite7679702015-02-20 14:40:35 +053059 item.rate = flt(item.price_list_rate *
60 (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
Nabin Hait3237c752015-02-17 11:11:11 +053061
mbauskar36b51892016-01-18 16:31:10 +053062 if item.doctype in ['Quotation Item', 'Sales Order Item']:
63 item.total_margin = self.calculate_margin(item)
64 item.rate = item.total_margin if item.total_margin > 0 else item.rate
65
Nabin Haite7679702015-02-20 14:40:35 +053066 item.net_rate = item.rate
67 item.amount = flt(item.rate * item.qty, item.precision("amount"))
68 item.net_amount = item.amount
Nabin Hait3237c752015-02-17 11:11:11 +053069
Nabin Haite7679702015-02-20 14:40:35 +053070 self._set_in_company_currency(item, ["price_list_rate", "rate", "net_rate", "amount", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +053071
Nabin Haite7679702015-02-20 14:40:35 +053072 item.item_tax_amount = 0.0
73
74 def _set_in_company_currency(self, doc, fields):
Nabin Hait3237c752015-02-17 11:11:11 +053075 """set values in base currency"""
Nabin Haite7679702015-02-20 14:40:35 +053076 for f in fields:
77 val = flt(flt(doc.get(f), doc.precision(f)) * self.doc.conversion_rate, doc.precision("base_" + f))
78 doc.set("base_" + f, val)
Nabin Hait3237c752015-02-17 11:11:11 +053079
80 def initialize_taxes(self):
81 for tax in self.doc.get("taxes"):
Nabin Hait86cd4cc2015-02-28 19:11:51 +053082 if not self.discount_amount_applied:
83 validate_taxes_and_charges(tax)
84 validate_inclusive_tax(tax, self.doc)
Nabin Hait613d0812015-02-23 11:58:15 +053085
Nabin Hait3237c752015-02-17 11:11:11 +053086 tax.item_wise_tax_detail = {}
87 tax_fields = ["total", "tax_amount_after_discount_amount",
88 "tax_amount_for_current_item", "grand_total_for_current_item",
89 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
90
Nabin Haitde9c8a92015-02-23 01:06:00 +053091 if tax.charge_type != "Actual" and \
92 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
93 tax_fields.append("tax_amount")
Nabin Hait3237c752015-02-17 11:11:11 +053094
95 for fieldname in tax_fields:
96 tax.set(fieldname, 0.0)
97
Nabin Hait3237c752015-02-17 11:11:11 +053098 self.doc.round_floats_in(tax)
99
Nabin Hait3237c752015-02-17 11:11:11 +0530100 def determine_exclusive_rate(self):
Nabin Hait37b047d2015-02-23 16:01:33 +0530101 if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))):
102 return
Nabin Hait3237c752015-02-17 11:11:11 +0530103
104 for item in self.doc.get("items"):
105 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
106 cumulated_tax_fraction = 0
107 for i, tax in enumerate(self.doc.get("taxes")):
108 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
109
110 if i==0:
111 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
112 else:
113 tax.grand_total_fraction_for_current_item = \
114 self.doc.get("taxes")[i-1].grand_total_fraction_for_current_item \
115 + tax.tax_fraction_for_current_item
116
117 cumulated_tax_fraction += tax.tax_fraction_for_current_item
118
119 if cumulated_tax_fraction and not self.discount_amount_applied and item.qty:
Nabin Haite7679702015-02-20 14:40:35 +0530120 item.net_amount = flt(item.amount / (1 + cumulated_tax_fraction), item.precision("net_amount"))
121 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
122 item.discount_percentage = flt(item.discount_percentage, item.precision("discount_percentage"))
Nabin Hait3237c752015-02-17 11:11:11 +0530123
Nabin Haite7679702015-02-20 14:40:35 +0530124 self._set_in_company_currency(item, ["net_rate", "net_amount"])
125
Nabin Hait3237c752015-02-17 11:11:11 +0530126 def _load_item_tax_rate(self, item_tax_rate):
127 return json.loads(item_tax_rate) if item_tax_rate else {}
128
129 def get_current_tax_fraction(self, tax, item_tax_map):
130 """
131 Get tax fraction for calculating tax exclusive amount
132 from tax inclusive amount
133 """
134 current_tax_fraction = 0
135
136 if cint(tax.included_in_print_rate):
137 tax_rate = self._get_tax_rate(tax, item_tax_map)
138
139 if tax.charge_type == "On Net Total":
140 current_tax_fraction = tax_rate / 100.0
141
142 elif tax.charge_type == "On Previous Row Amount":
143 current_tax_fraction = (tax_rate / 100.0) * \
144 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item
145
146 elif tax.charge_type == "On Previous Row Total":
147 current_tax_fraction = (tax_rate / 100.0) * \
148 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
149
Nabin Haita6ee8292015-05-15 12:02:01 +0530150 if getattr(tax, "add_deduct_tax", None):
151 current_tax_fraction *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
Nabin Hait3237c752015-02-17 11:11:11 +0530152 return current_tax_fraction
153
154 def _get_tax_rate(self, tax, item_tax_map):
155 if item_tax_map.has_key(tax.account_head):
156 return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax))
157 else:
158 return tax.rate
159
160 def calculate_net_total(self):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530161 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 +0530162
163 for item in self.doc.get("items"):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530164 self.doc.total += item.amount
165 self.doc.base_total += item.base_amount
Nabin Haite7679702015-02-20 14:40:35 +0530166 self.doc.net_total += item.net_amount
167 self.doc.base_net_total += item.base_net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530168
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530169 self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530170
171 def calculate_taxes(self):
172 # maintain actual tax rate based on idx
Nabin Haite7679702015-02-20 14:40:35 +0530173 actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
Nabin Hait3237c752015-02-17 11:11:11 +0530174 for tax in self.doc.get("taxes") if tax.charge_type == "Actual"])
175
176 for n, item in enumerate(self.doc.get("items")):
177 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
178
179 for i, tax in enumerate(self.doc.get("taxes")):
180 # tax_amount represents the amount of tax for the current step
181 current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
182
183 # Adjust divisional loss to the last item
184 if tax.charge_type == "Actual":
185 actual_tax_dict[tax.idx] -= current_tax_amount
186 if n == len(self.doc.get("items")) - 1:
187 current_tax_amount += actual_tax_dict[tax.idx]
188
Nabin Hait2b019ed2015-02-22 23:03:07 +0530189 # accumulate tax amount into tax.tax_amount
Nabin Haitde9c8a92015-02-23 01:06:00 +0530190 if tax.charge_type != "Actual" and \
191 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
192 tax.tax_amount += current_tax_amount
Nabin Hait2b019ed2015-02-22 23:03:07 +0530193
Nabin Hait3237c752015-02-17 11:11:11 +0530194 # store tax_amount for current item as it will be used for
195 # charge type = 'On Previous Row Amount'
196 tax.tax_amount_for_current_item = current_tax_amount
197
Nabin Hait2b019ed2015-02-22 23:03:07 +0530198 # set tax after discount
Nabin Hait3237c752015-02-17 11:11:11 +0530199 tax.tax_amount_after_discount_amount += current_tax_amount
200
201 if getattr(tax, "category", None):
202 # if just for valuation, do not add the tax amount in total
203 # hence, setting it as 0 for further steps
204 current_tax_amount = 0.0 if (tax.category == "Valuation") \
205 else current_tax_amount
206
207 current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
208
209 # Calculate tax.total viz. grand total till that step
210 # note: grand_total_for_current_item contains the contribution of
211 # item's amount, previously applied tax and the current tax on that item
212 if i==0:
Nabin Hait93b3a372015-02-24 17:08:34 +0530213 tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount, tax.precision("total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530214 else:
215 tax.grand_total_for_current_item = \
Rushabh Mehta6cdfd1e2015-02-25 15:55:57 +0530216 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 +0530217
218 # in tax.total, accumulate grand total of each item
219 tax.total += tax.grand_total_for_current_item
220
221 # set precision in the last item iteration
222 if n == len(self.doc.get("items")) - 1:
223 self.round_off_totals(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530224
Nabin Hait3237c752015-02-17 11:11:11 +0530225 # adjust Discount Amount loss in last tax iteration
Nabin Haitde9c8a92015-02-23 01:06:00 +0530226 if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
Nabin Haitdb53a782015-07-31 16:53:13 +0530227 and self.doc.discount_amount and self.doc.apply_discount_on == "Grand Total":
Nabin Haitde9c8a92015-02-23 01:06:00 +0530228 self.adjust_discount_amount_loss(tax)
Anand Doshiec5ec602015-03-05 19:31:23 +0530229
230
Nabin Hait3237c752015-02-17 11:11:11 +0530231
232 def get_current_tax_amount(self, item, tax, item_tax_map):
233 tax_rate = self._get_tax_rate(tax, item_tax_map)
234 current_tax_amount = 0.0
235
236 if tax.charge_type == "Actual":
237 # distribute the tax amount proportionally to each item row
Nabin Haite7679702015-02-20 14:40:35 +0530238 actual = flt(tax.tax_amount, tax.precision("tax_amount"))
239 current_tax_amount = item.net_amount*actual / self.doc.net_total if self.doc.net_total else 0.0
240
Nabin Hait3237c752015-02-17 11:11:11 +0530241 elif tax.charge_type == "On Net Total":
Nabin Haite7679702015-02-20 14:40:35 +0530242 current_tax_amount = (tax_rate / 100.0) * item.net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530243 elif tax.charge_type == "On Previous Row Amount":
244 current_tax_amount = (tax_rate / 100.0) * \
245 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item
246 elif tax.charge_type == "On Previous Row Total":
247 current_tax_amount = (tax_rate / 100.0) * \
248 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item
249
Nabin Hait93b3a372015-02-24 17:08:34 +0530250 current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530251
Nabin Haite7679702015-02-20 14:40:35 +0530252 self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530253
254 return current_tax_amount
255
Nabin Haite7679702015-02-20 14:40:35 +0530256 def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount):
257 # store tax breakup for each item
258 key = item.item_code or item.item_name
259 item_wise_tax_amount = current_tax_amount*self.doc.conversion_rate
260 if tax.item_wise_tax_detail.get(key):
261 item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
262
263 tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount, tax.precision("base_tax_amount"))]
264
Nabin Hait3237c752015-02-17 11:11:11 +0530265 def round_off_totals(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530266 tax.total = flt(tax.total, tax.precision("total"))
267 tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
268 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, tax.precision("tax_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530269
Nabin Haitde9c8a92015-02-23 01:06:00 +0530270 self._set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Haitce245122015-02-22 20:14:49 +0530271
Nabin Hait3237c752015-02-17 11:11:11 +0530272 def adjust_discount_amount_loss(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530273 discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
Nabin Hait3237c752015-02-17 11:11:11 +0530274 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Nabin Hait95ff2d42015-03-04 16:02:03 +0530275 discount_amount_loss, tax.precision("tax_amount"))
276 tax.total = flt(tax.total + discount_amount_loss, tax.precision("total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530277
Nabin Hait95ff2d42015-03-04 16:02:03 +0530278 self._set_in_company_currency(tax, ["total", "tax_amount_after_discount_amount"])
Anand Doshi731b15e2015-04-05 20:09:09 +0530279
Nabin Haita1bf43b2015-03-17 10:50:47 +0530280 def manipulate_grand_total_for_inclusive_tax(self):
281 # if fully inclusive taxes and diff
282 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 +0530283 last_tax = self.doc.get("taxes")[-1]
Nabin Haita6ee8292015-05-15 12:02:01 +0530284 diff = self.doc.total - flt(last_tax.total, self.doc.precision("grand_total"))
Nabin Haita1bf43b2015-03-17 10:50:47 +0530285
286 if diff and abs(diff) <= (2.0 / 10**last_tax.precision("tax_amount")):
Nabin Haita6ee8292015-05-15 12:02:01 +0530287 last_tax.tax_amount += diff
288 last_tax.tax_amount_after_discount_amount += diff
289 last_tax.total += diff
Nabin Hait82521172015-05-19 16:29:44 +0530290
291 self._set_in_company_currency(last_tax,
292 ["total", "tax_amount", "tax_amount_after_discount_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530293
294 def calculate_totals(self):
Nabin Haite7679702015-02-20 14:40:35 +0530295 self.doc.grand_total = flt(self.doc.get("taxes")[-1].total
296 if self.doc.get("taxes") else self.doc.net_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530297
Anand Doshiec5ec602015-03-05 19:31:23 +0530298 self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total,
299 self.doc.precision("total_taxes_and_charges"))
300
301 self._set_in_company_currency(self.doc, ["total_taxes_and_charges"])
302
Nabin Haitbd00e812015-02-17 12:50:51 +0530303 if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
Nabin Haite7679702015-02-20 14:40:35 +0530304 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
305 if self.doc.total_taxes_and_charges else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530306 else:
Anand Doshiec5ec602015-03-05 19:31:23 +0530307 self.doc.taxes_and_charges_added = self.doc.taxes_and_charges_deducted = 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530308 for tax in self.doc.get("taxes"):
309 if tax.category in ["Valuation and Total", "Total"]:
310 if tax.add_deduct_tax == "Add":
Nabin Haitdb53a782015-07-31 16:53:13 +0530311 self.doc.taxes_and_charges_added += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530312 else:
Nabin Haitdb53a782015-07-31 16:53:13 +0530313 self.doc.taxes_and_charges_deducted += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530314
Nabin Haite7679702015-02-20 14:40:35 +0530315 self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530316
Nabin Haite7679702015-02-20 14:40:35 +0530317 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
318 if (self.doc.taxes_and_charges_added or self.doc.taxes_and_charges_deducted) \
319 else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530320
Nabin Haite7679702015-02-20 14:40:35 +0530321 self._set_in_company_currency(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.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530324
Nabin Hait3237c752015-02-17 11:11:11 +0530325 if self.doc.meta.get_field("rounded_total"):
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530326 self.doc.rounded_total = round_based_on_smallest_currency_fraction(self.doc.grand_total,
327 self.doc.currency, self.doc.precision("rounded_total"))
Nabin Haite7679702015-02-20 14:40:35 +0530328 if self.doc.meta.get_field("base_rounded_total"):
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530329 company_currency = get_company_currency(self.doc.company)
330
331 self.doc.base_rounded_total = \
332 round_based_on_smallest_currency_fraction(self.doc.base_grand_total,
333 company_currency, self.doc.precision("base_rounded_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530334
335 def _cleanup(self):
336 for tax in self.doc.get("taxes"):
337 tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':'))
Nabin Hait3769d872015-12-18 13:12:02 +0530338
339 def set_discount_amount(self):
340 if not self.doc.discount_amount and self.doc.additional_discount_percentage:
341 self.doc.discount_amount = flt(flt(self.doc.get(scrub(self.doc.apply_discount_on)))
342 * self.doc.additional_discount_percentage / 100, self.doc.precision("discount_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530343
344 def apply_discount_amount(self):
345 if self.doc.discount_amount:
Nabin Hait37b047d2015-02-23 16:01:33 +0530346 if not self.doc.apply_discount_on:
347 frappe.throw(_("Please select Apply Discount On"))
348
Nabin Hait3237c752015-02-17 11:11:11 +0530349 self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate,
350 self.doc.precision("base_discount_amount"))
351
Nabin Haite7679702015-02-20 14:40:35 +0530352 total_for_discount_amount = self.get_total_for_discount_amount()
Nabin Hait25bd84d2015-03-04 15:06:56 +0530353 taxes = self.doc.get("taxes")
354 net_total = 0
Nabin Hait3237c752015-02-17 11:11:11 +0530355
Nabin Haite7679702015-02-20 14:40:35 +0530356 if total_for_discount_amount:
Nabin Hait3237c752015-02-17 11:11:11 +0530357 # calculate item amount after Discount Amount
Nabin Hait25bd84d2015-03-04 15:06:56 +0530358 for i, item in enumerate(self.doc.get("items")):
359 distributed_amount = flt(self.doc.discount_amount) * \
360 item.net_amount / total_for_discount_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530361
Nabin Haite7679702015-02-20 14:40:35 +0530362 item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
Nabin Hait25bd84d2015-03-04 15:06:56 +0530363 net_total += item.net_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530364
Nabin Hait25bd84d2015-03-04 15:06:56 +0530365 # discount amount rounding loss adjustment if no taxes
366 if (not taxes or self.doc.apply_discount_on == "Net Total") \
367 and i == len(self.doc.get("items")) - 1:
Anand Doshiec5ec602015-03-05 19:31:23 +0530368 discount_amount_loss = flt(self.doc.total - net_total - self.doc.discount_amount,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530369 self.doc.precision("net_total"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530370 item.net_amount = flt(item.net_amount + discount_amount_loss,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530371 item.precision("net_amount"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530372
Nabin Hait51e980d2015-10-10 18:10:05 +0530373 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 +0530374
Nabin Haite7679702015-02-20 14:40:35 +0530375 self._set_in_company_currency(item, ["net_rate", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530376
377 self.discount_amount_applied = True
378 self._calculate()
379 else:
380 self.doc.base_discount_amount = 0
381
Nabin Haite7679702015-02-20 14:40:35 +0530382 def get_total_for_discount_amount(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +0530383 if self.doc.apply_discount_on == "Net Total":
384 return self.doc.net_total
Nabin Haite7679702015-02-20 14:40:35 +0530385 else:
386 actual_taxes_dict = {}
Nabin Hait3237c752015-02-17 11:11:11 +0530387
Nabin Haite7679702015-02-20 14:40:35 +0530388 for tax in self.doc.get("taxes"):
389 if tax.charge_type == "Actual":
390 actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
391 elif tax.row_id in actual_taxes_dict:
392 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * flt(tax.rate) / 100
393 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530394
Nabin Haite7679702015-02-20 14:40:35 +0530395 return flt(self.doc.grand_total - sum(actual_taxes_dict.values()), self.doc.precision("grand_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530396
397
Nabin Hait7b19b9e2015-02-24 09:42:24 +0530398 def calculate_total_advance(self):
399 if self.doc.docstatus < 2:
Nabin Haite7679702015-02-20 14:40:35 +0530400 total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530401 for adv in self.doc.get("advances")])
402
Nabin Haite7679702015-02-20 14:40:35 +0530403 self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
Nabin Hait289ffb72016-02-08 11:06:55 +0530404
405 if self.doc.party_account_currency == self.doc.currency:
406 invoice_total = self.doc.grand_total
407 else:
408 invoice_total = flt(self.doc.grand_total * self.doc.conversion_rate,
409 self.doc.precision("grand_total"))
410
Nabin Haitadc09232016-02-09 10:31:11 +0530411 if invoice_total > 0 and self.doc.total_advance > invoice_total:
Nabin Hait289ffb72016-02-08 11:06:55 +0530412 frappe.throw(_("Advance amount cannot be greater than {0} {1}")
413 .format(self.doc.party_account_currency, invoice_total))
Nabin Hait3237c752015-02-17 11:11:11 +0530414
Rushabh Mehta8bb6e532015-02-18 20:22:59 +0530415 if self.doc.docstatus == 0:
Nabin Hait3237c752015-02-17 11:11:11 +0530416 self.calculate_outstanding_amount()
417
418 def calculate_outstanding_amount(self):
419 # NOTE:
420 # write_off_amount is only for POS Invoice
421 # total_advance is only for non POS Invoice
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530422 if self.doc.is_return:
423 return
424
425 self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530426 if self.doc.party_account_currency == self.doc.currency:
427 total_amount_to_pay = flt(self.doc.grand_total - self.doc.total_advance
428 - flt(self.doc.write_off_amount), self.doc.precision("grand_total"))
429 else:
Nabin Haitd4507ac2016-01-20 16:14:27 +0530430 total_amount_to_pay = flt(flt(self.doc.grand_total *
431 self.doc.conversion_rate, self.doc.precision("grand_total")) - self.doc.total_advance
432 - flt(self.doc.base_write_off_amount), self.doc.precision("grand_total"))
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530433
Nabin Haitbd00e812015-02-17 12:50:51 +0530434 if self.doc.doctype == "Sales Invoice":
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530435 self.doc.round_floats_in(self.doc, ["paid_amount"])
Nabin Hait90c6d7b2015-11-13 13:03:10 +0530436 paid_amount = self.doc.paid_amount \
437 if self.doc.party_account_currency == self.doc.currency else self.doc.base_paid_amount
438 self.doc.outstanding_amount = flt(total_amount_to_pay - flt(paid_amount),
439 self.doc.precision("outstanding_amount"))
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530440 elif self.doc.doctype == "Purchase Invoice":
mbauskar36b51892016-01-18 16:31:10 +0530441 self.doc.outstanding_amount = flt(total_amount_to_pay, self.doc.precision("outstanding_amount"))
442
443 def calculate_margin(self, item):
444 total_margin = 0.0
445 if item.price_list_rate:
446 if item.pricing_rule:
447 pricing_rule = frappe.get_doc('Pricing Rule', item.pricing_rule)
448 if not item.type: item.type = pricing_rule.type
449 if not item.rate_or_amount: item.rate_or_amount = pricing_rule.rate
450
451 if item.type and item.rate_or_amount:
452 margin_value = item.rate_or_amount if item.type == 'Amount' else flt(item.price_list_rate) * flt(item.rate_or_amount) / 100
453 total_margin = flt(item.price_list_rate) + flt(margin_value)
454
455 return total_margin