blob: 572e1ca23974220a9dd2aa0179d42a8346bf19e7 [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
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +05306import frappe, erpnext
Nabin Hait3769d872015-12-18 13:12:02 +05307from frappe import _, scrub
Nabin Haitb962fc12017-07-17 18:02:31 +05308from frappe.utils import cint, flt, round_based_on_smallest_currency_fraction
Nabin Hait613d0812015-02-23 11:58:15 +05309from erpnext.controllers.accounts_controller import validate_conversion_rate, \
10 validate_taxes_and_charges, validate_inclusive_tax
Deepesh Gargef0d26c2020-01-06 15:34:15 +053011from erpnext.stock.get_item_details import _get_item_tax_template
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):
Nabin Haitb315acb2019-07-12 14:27:19 +053019 if not len(self.doc.get("items")):
20 return
21
Nabin Hait3237c752015-02-17 11:11:11 +053022 self.discount_amount_applied = False
23 self._calculate()
24
25 if self.doc.meta.get_field("discount_amount"):
Nabin Hait3769d872015-12-18 13:12:02 +053026 self.set_discount_amount()
Nabin Hait3237c752015-02-17 11:11:11 +053027 self.apply_discount_amount()
28
Nabin Haitbd00e812015-02-17 12:50:51 +053029 if self.doc.doctype in ["Sales Invoice", "Purchase Invoice"]:
Nabin Hait3237c752015-02-17 11:11:11 +053030 self.calculate_total_advance()
Vishal Dhayaguded42242d2017-11-29 16:09:59 +053031
Nabin Hait852cb642017-07-05 12:58:19 +053032 if self.doc.meta.get_field("other_charges_calculation"):
33 self.set_item_wise_tax_breakup()
Nabin Hait3237c752015-02-17 11:11:11 +053034
35 def _calculate(self):
Faris Ansarieae2dda2018-05-02 12:19:30 +053036 self.validate_conversion_rate()
Nabin Haite7679702015-02-20 14:40:35 +053037 self.calculate_item_values()
Deepesh Gargef0d26c2020-01-06 15:34:15 +053038 self.validate_item_tax_template()
Nabin Haite7679702015-02-20 14:40:35 +053039 self.initialize_taxes()
40 self.determine_exclusive_rate()
41 self.calculate_net_total()
42 self.calculate_taxes()
Nabin Haita1bf43b2015-03-17 10:50:47 +053043 self.manipulate_grand_total_for_inclusive_tax()
Nabin Haite7679702015-02-20 14:40:35 +053044 self.calculate_totals()
45 self._cleanup()
rohitwaghchaurea8fb2db2018-05-26 09:23:02 +053046 self.calculate_total_net_weight()
Nabin Haite7679702015-02-20 14:40:35 +053047
Deepesh Gargef0d26c2020-01-06 15:34:15 +053048 def validate_item_tax_template(self):
49 for item in self.doc.get('items'):
50 if item.item_code and item.get('item_tax_template'):
51 item_doc = frappe.get_cached_doc("Item", item.item_code)
52 args = {
53 'tax_category': self.doc.get('tax_category'),
54 'posting_date': self.doc.get('posting_date'),
55 'bill_date': self.doc.get('bill_date'),
mohammadahmad1990728bf0e2020-06-18 12:21:42 +050056 'transaction_date': self.doc.get('transaction_date'),
57 'company': self.doc.get('company')
Deepesh Gargef0d26c2020-01-06 15:34:15 +053058 }
59
60 item_group = item_doc.item_group
61 item_group_taxes = []
62
63 while item_group:
64 item_group_doc = frappe.get_cached_doc('Item Group', item_group)
65 item_group_taxes += item_group_doc.taxes or []
66 item_group = item_group_doc.parent_item_group
67
68 item_taxes = item_doc.taxes or []
69
70 if not item_group_taxes and (not item_taxes):
71 # No validation if no taxes in item or item group
72 continue
73
74 taxes = _get_item_tax_template(args, item_taxes + item_group_taxes, for_validate=True)
75
76 if item.item_tax_template not in taxes:
77 frappe.throw(_("Row {0}: Invalid Item Tax Template for item {1}").format(
78 item.idx, frappe.bold(item.item_code)
79 ))
80
Nabin Haite7679702015-02-20 14:40:35 +053081 def validate_conversion_rate(self):
Nabin Hait3237c752015-02-17 11:11:11 +053082 # validate conversion rate
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053083 company_currency = erpnext.get_company_currency(self.doc.company)
Nabin Hait3237c752015-02-17 11:11:11 +053084 if not self.doc.currency or self.doc.currency == company_currency:
85 self.doc.currency = company_currency
86 self.doc.conversion_rate = 1.0
87 else:
88 validate_conversion_rate(self.doc.currency, self.doc.conversion_rate,
89 self.doc.meta.get_label("conversion_rate"), self.doc.company)
90
91 self.doc.conversion_rate = flt(self.doc.conversion_rate)
92
Nabin Hait3237c752015-02-17 11:11:11 +053093 def calculate_item_values(self):
94 if not self.discount_amount_applied:
95 for item in self.doc.get("items"):
96 self.doc.round_floats_in(item)
97
98 if item.discount_percentage == 100:
99 item.rate = 0.0
Nabin Hait593242f2019-04-05 19:35:02 +0530100 elif item.price_list_rate:
101 if not item.rate or (item.pricing_rules and item.discount_percentage > 0):
102 item.rate = flt(item.price_list_rate *
103 (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
104 item.discount_amount = item.price_list_rate * (item.discount_percentage / 100.0)
105 elif item.discount_amount and item.pricing_rules:
106 item.rate = item.price_list_rate - item.discount_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530107
mbauskara52472c2016-03-05 15:10:25 +0530108 if item.doctype in ['Quotation Item', 'Sales Order Item', 'Delivery Note Item', 'Sales Invoice Item']:
Shreya Shahbe690ef2017-11-14 17:22:41 +0530109 item.rate_with_margin, item.base_rate_with_margin = self.calculate_margin(item)
Nabin Hait64bfdd92019-04-23 13:37:19 +0530110 if flt(item.rate_with_margin) > 0:
111 item.rate = flt(item.rate_with_margin * (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
112 item.discount_amount = item.rate_with_margin - item.rate
113 elif flt(item.price_list_rate) > 0:
114 item.discount_amount = item.price_list_rate - item.rate
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530115 elif flt(item.price_list_rate) > 0 and not item.discount_amount:
116 item.discount_amount = item.price_list_rate - item.rate
mbauskara52472c2016-03-05 15:10:25 +0530117
Nabin Haite7679702015-02-20 14:40:35 +0530118 item.net_rate = item.rate
Deepesh Gargb65c7612019-07-31 15:58:01 +0530119
deepeshgarg0078bf19ce2019-08-03 13:40:37 +0530120 if not item.qty and self.doc.get("is_return"):
Deepesh Gargb65c7612019-07-31 15:58:01 +0530121 item.amount = flt(-1 * item.rate, item.precision("amount"))
122 else:
123 item.amount = flt(item.rate * item.qty, item.precision("amount"))
124
Nabin Haite7679702015-02-20 14:40:35 +0530125 item.net_amount = item.amount
Nabin Hait3237c752015-02-17 11:11:11 +0530126
Nabin Haite7679702015-02-20 14:40:35 +0530127 self._set_in_company_currency(item, ["price_list_rate", "rate", "net_rate", "amount", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530128
Nabin Haite7679702015-02-20 14:40:35 +0530129 item.item_tax_amount = 0.0
130
131 def _set_in_company_currency(self, doc, fields):
Nabin Hait3237c752015-02-17 11:11:11 +0530132 """set values in base currency"""
Nabin Haite7679702015-02-20 14:40:35 +0530133 for f in fields:
134 val = flt(flt(doc.get(f), doc.precision(f)) * self.doc.conversion_rate, doc.precision("base_" + f))
135 doc.set("base_" + f, val)
Nabin Hait3237c752015-02-17 11:11:11 +0530136
137 def initialize_taxes(self):
138 for tax in self.doc.get("taxes"):
Nabin Hait86cd4cc2015-02-28 19:11:51 +0530139 if not self.discount_amount_applied:
140 validate_taxes_and_charges(tax)
141 validate_inclusive_tax(tax, self.doc)
Nabin Hait613d0812015-02-23 11:58:15 +0530142
Nabin Hait3237c752015-02-17 11:11:11 +0530143 tax.item_wise_tax_detail = {}
144 tax_fields = ["total", "tax_amount_after_discount_amount",
145 "tax_amount_for_current_item", "grand_total_for_current_item",
146 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
147
Nabin Haitde9c8a92015-02-23 01:06:00 +0530148 if tax.charge_type != "Actual" and \
149 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
150 tax_fields.append("tax_amount")
Nabin Hait3237c752015-02-17 11:11:11 +0530151
152 for fieldname in tax_fields:
153 tax.set(fieldname, 0.0)
154
Nabin Hait3237c752015-02-17 11:11:11 +0530155 self.doc.round_floats_in(tax)
156
Nabin Hait3237c752015-02-17 11:11:11 +0530157 def determine_exclusive_rate(self):
Nabin Hait37b047d2015-02-23 16:01:33 +0530158 if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))):
159 return
Nabin Hait3237c752015-02-17 11:11:11 +0530160
161 for item in self.doc.get("items"):
162 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
163 cumulated_tax_fraction = 0
164 for i, tax in enumerate(self.doc.get("taxes")):
165 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
166
167 if i==0:
168 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
169 else:
170 tax.grand_total_fraction_for_current_item = \
171 self.doc.get("taxes")[i-1].grand_total_fraction_for_current_item \
172 + tax.tax_fraction_for_current_item
173
174 cumulated_tax_fraction += tax.tax_fraction_for_current_item
175
176 if cumulated_tax_fraction and not self.discount_amount_applied and item.qty:
Nabin Hait2e4de832017-09-19 14:53:16 +0530177 item.net_amount = flt(item.amount / (1 + cumulated_tax_fraction))
Nabin Haite7679702015-02-20 14:40:35 +0530178 item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
Nabin Hait2e4de832017-09-19 14:53:16 +0530179 item.discount_percentage = flt(item.discount_percentage,
180 item.precision("discount_percentage"))
Nabin Hait3237c752015-02-17 11:11:11 +0530181
Nabin Haite7679702015-02-20 14:40:35 +0530182 self._set_in_company_currency(item, ["net_rate", "net_amount"])
183
Nabin Hait3237c752015-02-17 11:11:11 +0530184 def _load_item_tax_rate(self, item_tax_rate):
185 return json.loads(item_tax_rate) if item_tax_rate else {}
186
187 def get_current_tax_fraction(self, tax, item_tax_map):
188 """
189 Get tax fraction for calculating tax exclusive amount
190 from tax inclusive amount
191 """
192 current_tax_fraction = 0
193
194 if cint(tax.included_in_print_rate):
195 tax_rate = self._get_tax_rate(tax, item_tax_map)
196
197 if tax.charge_type == "On Net Total":
198 current_tax_fraction = tax_rate / 100.0
199
200 elif tax.charge_type == "On Previous Row Amount":
201 current_tax_fraction = (tax_rate / 100.0) * \
202 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item
203
204 elif tax.charge_type == "On Previous Row Total":
205 current_tax_fraction = (tax_rate / 100.0) * \
206 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
207
Nabin Haita6ee8292015-05-15 12:02:01 +0530208 if getattr(tax, "add_deduct_tax", None):
209 current_tax_fraction *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
Nabin Hait3237c752015-02-17 11:11:11 +0530210 return current_tax_fraction
211
212 def _get_tax_rate(self, tax, item_tax_map):
Achilles Rasquinha87dab142018-03-08 14:21:48 +0530213 if tax.account_head in item_tax_map:
Nabin Hait3237c752015-02-17 11:11:11 +0530214 return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax))
215 else:
216 return tax.rate
217
218 def calculate_net_total(self):
Shreya Shahe3290382018-05-28 11:49:08 +0530219 self.doc.total_qty = self.doc.total = self.doc.base_total = self.doc.net_total = self.doc.base_net_total = 0.0
rohitwaghchaure3a595d02018-06-25 10:10:29 +0530220
Nabin Hait3237c752015-02-17 11:11:11 +0530221 for item in self.doc.get("items"):
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530222 self.doc.total += item.amount
Shreya Shahe3290382018-05-28 11:49:08 +0530223 self.doc.total_qty += item.qty
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530224 self.doc.base_total += item.base_amount
Nabin Haite7679702015-02-20 14:40:35 +0530225 self.doc.net_total += item.net_amount
226 self.doc.base_net_total += item.base_net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530227
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530228 self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530229
rohitwaghchaure3a595d02018-06-25 10:10:29 +0530230 if self.doc.doctype == 'Sales Invoice' and self.doc.is_pos:
231 self.doc.pos_total_qty = self.doc.total_qty
232
Nabin Hait3237c752015-02-17 11:11:11 +0530233 def calculate_taxes(self):
Nabin Hait2e4de832017-09-19 14:53:16 +0530234 self.doc.rounding_adjustment = 0
Nabin Hait3237c752015-02-17 11:11:11 +0530235 # maintain actual tax rate based on idx
Nabin Haite7679702015-02-20 14:40:35 +0530236 actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
Nabin Hait3237c752015-02-17 11:11:11 +0530237 for tax in self.doc.get("taxes") if tax.charge_type == "Actual"])
238
239 for n, item in enumerate(self.doc.get("items")):
240 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
Nabin Hait3237c752015-02-17 11:11:11 +0530241 for i, tax in enumerate(self.doc.get("taxes")):
242 # tax_amount represents the amount of tax for the current step
243 current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
244
245 # Adjust divisional loss to the last item
246 if tax.charge_type == "Actual":
247 actual_tax_dict[tax.idx] -= current_tax_amount
248 if n == len(self.doc.get("items")) - 1:
249 current_tax_amount += actual_tax_dict[tax.idx]
250
Nabin Hait2b019ed2015-02-22 23:03:07 +0530251 # accumulate tax amount into tax.tax_amount
Nabin Haitde9c8a92015-02-23 01:06:00 +0530252 if tax.charge_type != "Actual" and \
253 not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
254 tax.tax_amount += current_tax_amount
Nabin Hait2b019ed2015-02-22 23:03:07 +0530255
Nabin Hait3237c752015-02-17 11:11:11 +0530256 # store tax_amount for current item as it will be used for
257 # charge type = 'On Previous Row Amount'
258 tax.tax_amount_for_current_item = current_tax_amount
259
Nabin Hait2b019ed2015-02-22 23:03:07 +0530260 # set tax after discount
Nabin Hait3237c752015-02-17 11:11:11 +0530261 tax.tax_amount_after_discount_amount += current_tax_amount
262
Nabin Haitcd951342017-07-31 18:07:45 +0530263 current_tax_amount = self.get_tax_amount_if_for_valuation_or_deduction(current_tax_amount, tax)
Nabin Hait3237c752015-02-17 11:11:11 +0530264
Nabin Hait3237c752015-02-17 11:11:11 +0530265 # note: grand_total_for_current_item contains the contribution of
266 # item's amount, previously applied tax and the current tax on that item
267 if i==0:
Nabin Haitcd951342017-07-31 18:07:45 +0530268 tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530269 else:
270 tax.grand_total_for_current_item = \
Nabin Haitcd951342017-07-31 18:07:45 +0530271 flt(self.doc.get("taxes")[i-1].grand_total_for_current_item + current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530272
273 # set precision in the last item iteration
274 if n == len(self.doc.get("items")) - 1:
275 self.round_off_totals(tax)
Nabin Haitcd951342017-07-31 18:07:45 +0530276 self.set_cumulative_total(i, tax)
277
278 self._set_in_company_currency(tax,
279 ["total", "tax_amount", "tax_amount_after_discount_amount"])
Anand Doshiec5ec602015-03-05 19:31:23 +0530280
Nabin Hait3237c752015-02-17 11:11:11 +0530281 # adjust Discount Amount loss in last tax iteration
Nabin Haitde9c8a92015-02-23 01:06:00 +0530282 if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
Nabin Haitdb53a782015-07-31 16:53:13 +0530283 and self.doc.discount_amount and self.doc.apply_discount_on == "Grand Total":
Nabin Hait2e4de832017-09-19 14:53:16 +0530284 self.doc.rounding_adjustment = flt(self.doc.grand_total
285 - flt(self.doc.discount_amount) - tax.total,
286 self.doc.precision("rounding_adjustment"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530287
Nabin Haitcd951342017-07-31 18:07:45 +0530288 def get_tax_amount_if_for_valuation_or_deduction(self, tax_amount, tax):
289 # if just for valuation, do not add the tax amount in total
290 # if tax/charges is for deduction, multiply by -1
291 if getattr(tax, "category", None):
292 tax_amount = 0.0 if (tax.category == "Valuation") else tax_amount
Rushabh Mehta30dc9a12017-11-17 14:31:09 +0530293 if self.doc.doctype in ["Purchase Order", "Purchase Invoice", "Purchase Receipt", "Supplier Quotation"]:
294 tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
Nabin Haitcd951342017-07-31 18:07:45 +0530295 return tax_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530296
Nabin Haitcd951342017-07-31 18:07:45 +0530297 def set_cumulative_total(self, row_idx, tax):
298 tax_amount = tax.tax_amount_after_discount_amount
299 tax_amount = self.get_tax_amount_if_for_valuation_or_deduction(tax_amount, tax)
300
301 if row_idx == 0:
302 tax.total = flt(self.doc.net_total + tax_amount, tax.precision("total"))
303 else:
304 tax.total = flt(self.doc.get("taxes")[row_idx-1].total + tax_amount, tax.precision("total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530305
306 def get_current_tax_amount(self, item, tax, item_tax_map):
307 tax_rate = self._get_tax_rate(tax, item_tax_map)
308 current_tax_amount = 0.0
309
310 if tax.charge_type == "Actual":
311 # distribute the tax amount proportionally to each item row
Nabin Haite7679702015-02-20 14:40:35 +0530312 actual = flt(tax.tax_amount, tax.precision("tax_amount"))
313 current_tax_amount = item.net_amount*actual / self.doc.net_total if self.doc.net_total else 0.0
314
Nabin Hait3237c752015-02-17 11:11:11 +0530315 elif tax.charge_type == "On Net Total":
Nabin Haite7679702015-02-20 14:40:35 +0530316 current_tax_amount = (tax_rate / 100.0) * item.net_amount
Nabin Hait3237c752015-02-17 11:11:11 +0530317 elif tax.charge_type == "On Previous Row Amount":
318 current_tax_amount = (tax_rate / 100.0) * \
319 self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item
320 elif tax.charge_type == "On Previous Row Total":
321 current_tax_amount = (tax_rate / 100.0) * \
322 self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item
Himanshu Mishra35b26272018-11-13 11:13:04 +0530323 elif tax.charge_type == "On Item Quantity":
324 current_tax_amount = tax_rate * item.stock_qty
Nabin Hait3237c752015-02-17 11:11:11 +0530325
Nabin Haite7679702015-02-20 14:40:35 +0530326 self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530327
328 return current_tax_amount
329
Nabin Haite7679702015-02-20 14:40:35 +0530330 def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount):
331 # store tax breakup for each item
332 key = item.item_code or item.item_name
333 item_wise_tax_amount = current_tax_amount*self.doc.conversion_rate
334 if tax.item_wise_tax_detail.get(key):
335 item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
336
Nabin Haitcaab5822017-08-24 16:22:28 +0530337 tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount)]
Nabin Haite7679702015-02-20 14:40:35 +0530338
Nabin Hait3237c752015-02-17 11:11:11 +0530339 def round_off_totals(self, tax):
Nabin Haite7679702015-02-20 14:40:35 +0530340 tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530341 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Nabin Haitcd951342017-07-31 18:07:45 +0530342 tax.precision("tax_amount"))
Nabin Haitce245122015-02-22 20:14:49 +0530343
Nabin Haita1bf43b2015-03-17 10:50:47 +0530344 def manipulate_grand_total_for_inclusive_tax(self):
345 # if fully inclusive taxes and diff
Nabin Hait2e4de832017-09-19 14:53:16 +0530346 if self.doc.get("taxes") and any([cint(t.included_in_print_rate) for t in self.doc.get("taxes")]):
Nabin Haita1bf43b2015-03-17 10:50:47 +0530347 last_tax = self.doc.get("taxes")[-1]
Nabin Hait2e4de832017-09-19 14:53:16 +0530348 non_inclusive_tax_amount = sum([flt(d.tax_amount_after_discount_amount)
349 for d in self.doc.get("taxes") if not d.included_in_print_rate])
Nabin Haitf32fc232019-12-25 13:59:24 +0530350
Nabin Hait2e4de832017-09-19 14:53:16 +0530351 diff = self.doc.total + non_inclusive_tax_amount \
352 - flt(last_tax.total, last_tax.precision("total"))
Nabin Haitf32fc232019-12-25 13:59:24 +0530353
354 # If discount amount applied, deduct the discount amount
355 # because self.doc.total is always without discount, but last_tax.total is after discount
356 if self.discount_amount_applied and self.doc.discount_amount:
357 diff -= flt(self.doc.discount_amount)
358
359 diff = flt(diff, self.doc.precision("rounding_adjustment"))
360
Nabin Hait2e4de832017-09-19 14:53:16 +0530361 if diff and abs(diff) <= (5.0 / 10**last_tax.precision("tax_amount")):
Nabin Haitf32fc232019-12-25 13:59:24 +0530362 self.doc.rounding_adjustment = diff
Nabin Hait3237c752015-02-17 11:11:11 +0530363
364 def calculate_totals(self):
Nabin Hait2e4de832017-09-19 14:53:16 +0530365 self.doc.grand_total = flt(self.doc.get("taxes")[-1].total) + flt(self.doc.rounding_adjustment) \
366 if self.doc.get("taxes") else flt(self.doc.net_total)
Nabin Hait3237c752015-02-17 11:11:11 +0530367
Nabin Hait2e4de832017-09-19 14:53:16 +0530368 self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total
369 - flt(self.doc.rounding_adjustment), self.doc.precision("total_taxes_and_charges"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530370
Nabin Hait2e4de832017-09-19 14:53:16 +0530371 self._set_in_company_currency(self.doc, ["total_taxes_and_charges", "rounding_adjustment"])
Anand Doshiec5ec602015-03-05 19:31:23 +0530372
Saqiba6f98d42020-07-23 18:51:26 +0530373 if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice", "POS Invoice"]:
Anurag Mishra8a06b8f2019-07-17 14:55:16 +0530374 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate, self.doc.precision("base_grand_total")) \
Nabin Haite7679702015-02-20 14:40:35 +0530375 if self.doc.total_taxes_and_charges else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530376 else:
Anand Doshiec5ec602015-03-05 19:31:23 +0530377 self.doc.taxes_and_charges_added = self.doc.taxes_and_charges_deducted = 0.0
Nabin Hait3237c752015-02-17 11:11:11 +0530378 for tax in self.doc.get("taxes"):
379 if tax.category in ["Valuation and Total", "Total"]:
380 if tax.add_deduct_tax == "Add":
Nabin Haitdb53a782015-07-31 16:53:13 +0530381 self.doc.taxes_and_charges_added += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530382 else:
Nabin Haitdb53a782015-07-31 16:53:13 +0530383 self.doc.taxes_and_charges_deducted += flt(tax.tax_amount_after_discount_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530384
Nabin Haite7679702015-02-20 14:40:35 +0530385 self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530386
Nabin Haite7679702015-02-20 14:40:35 +0530387 self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
388 if (self.doc.taxes_and_charges_added or self.doc.taxes_and_charges_deducted) \
389 else self.doc.base_net_total
Nabin Hait3237c752015-02-17 11:11:11 +0530390
Nabin Hait2e4de832017-09-19 14:53:16 +0530391 self._set_in_company_currency(self.doc,
392 ["taxes_and_charges_added", "taxes_and_charges_deducted"])
Nabin Hait3237c752015-02-17 11:11:11 +0530393
Nabin Haite7679702015-02-20 14:40:35 +0530394 self.doc.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
Nabin Hait3237c752015-02-17 11:11:11 +0530395
Nabin Hait2e4de832017-09-19 14:53:16 +0530396 self.set_rounded_total()
397
rohitwaghchaurea8fb2db2018-05-26 09:23:02 +0530398 def calculate_total_net_weight(self):
399 if self.doc.meta.get_field('total_net_weight'):
400 self.doc.total_net_weight = 0.0
401 for d in self.doc.items:
402 if d.total_weight:
403 self.doc.total_net_weight += d.total_weight
404
Nabin Hait2e4de832017-09-19 14:53:16 +0530405 def set_rounded_total(self):
Nabin Hait3237c752015-02-17 11:11:11 +0530406 if self.doc.meta.get_field("rounded_total"):
Nabin Hait877e1bb2017-11-17 12:27:43 +0530407 if self.doc.is_rounded_total_disabled():
408 self.doc.rounded_total = self.doc.base_rounded_total = 0
409 return
410
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530411 self.doc.rounded_total = round_based_on_smallest_currency_fraction(self.doc.grand_total,
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530412 self.doc.currency, self.doc.precision("rounded_total"))
Nabin Hait2e4de832017-09-19 14:53:16 +0530413
Nabin Hait877e1bb2017-11-17 12:27:43 +0530414 #if print_in_rate is set, we would have already calculated rounding adjustment
415 self.doc.rounding_adjustment += flt(self.doc.rounded_total - self.doc.grand_total,
416 self.doc.precision("rounding_adjustment"))
417
Nabin Hait02ac9012017-11-22 16:12:20 +0530418 self._set_in_company_currency(self.doc, ["rounding_adjustment", "rounded_total"])
Nabin Hait877e1bb2017-11-17 12:27:43 +0530419
Nabin Hait3237c752015-02-17 11:11:11 +0530420 def _cleanup(self):
421 for tax in self.doc.get("taxes"):
422 tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':'))
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530423
Nabin Hait3769d872015-12-18 13:12:02 +0530424 def set_discount_amount(self):
Nabin Haite0405102016-10-13 12:14:32 +0530425 if self.doc.additional_discount_percentage:
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530426 self.doc.discount_amount = flt(flt(self.doc.get(scrub(self.doc.apply_discount_on)))
Nabin Hait3769d872015-12-18 13:12:02 +0530427 * self.doc.additional_discount_percentage / 100, self.doc.precision("discount_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530428
429 def apply_discount_amount(self):
430 if self.doc.discount_amount:
Nabin Hait37b047d2015-02-23 16:01:33 +0530431 if not self.doc.apply_discount_on:
432 frappe.throw(_("Please select Apply Discount On"))
433
Nabin Hait3237c752015-02-17 11:11:11 +0530434 self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate,
435 self.doc.precision("base_discount_amount"))
436
Nabin Haite7679702015-02-20 14:40:35 +0530437 total_for_discount_amount = self.get_total_for_discount_amount()
Nabin Hait25bd84d2015-03-04 15:06:56 +0530438 taxes = self.doc.get("taxes")
439 net_total = 0
Nabin Hait3237c752015-02-17 11:11:11 +0530440
Nabin Haite7679702015-02-20 14:40:35 +0530441 if total_for_discount_amount:
Nabin Hait3237c752015-02-17 11:11:11 +0530442 # calculate item amount after Discount Amount
Nabin Hait25bd84d2015-03-04 15:06:56 +0530443 for i, item in enumerate(self.doc.get("items")):
444 distributed_amount = flt(self.doc.discount_amount) * \
445 item.net_amount / total_for_discount_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530446
Nabin Haite7679702015-02-20 14:40:35 +0530447 item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
Nabin Hait25bd84d2015-03-04 15:06:56 +0530448 net_total += item.net_amount
Anand Doshiec5ec602015-03-05 19:31:23 +0530449
Nabin Hait25bd84d2015-03-04 15:06:56 +0530450 # discount amount rounding loss adjustment if no taxes
Nabin Hait4d587342019-05-30 15:50:46 +0530451 if (self.doc.apply_discount_on == "Net Total" or not taxes or total_for_discount_amount==self.doc.net_total) \
Nabin Hait25bd84d2015-03-04 15:06:56 +0530452 and i == len(self.doc.get("items")) - 1:
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530453 discount_amount_loss = flt(self.doc.net_total - net_total - self.doc.discount_amount,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530454 self.doc.precision("net_total"))
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530455
Anand Doshiec5ec602015-03-05 19:31:23 +0530456 item.net_amount = flt(item.net_amount + discount_amount_loss,
Nabin Hait25bd84d2015-03-04 15:06:56 +0530457 item.precision("net_amount"))
Anand Doshiec5ec602015-03-05 19:31:23 +0530458
Nabin Hait51e980d2015-10-10 18:10:05 +0530459 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 +0530460
Nabin Haite7679702015-02-20 14:40:35 +0530461 self._set_in_company_currency(item, ["net_rate", "net_amount"])
Nabin Hait3237c752015-02-17 11:11:11 +0530462
463 self.discount_amount_applied = True
464 self._calculate()
465 else:
466 self.doc.base_discount_amount = 0
467
Nabin Haite7679702015-02-20 14:40:35 +0530468 def get_total_for_discount_amount(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +0530469 if self.doc.apply_discount_on == "Net Total":
470 return self.doc.net_total
Nabin Haite7679702015-02-20 14:40:35 +0530471 else:
472 actual_taxes_dict = {}
Nabin Hait3237c752015-02-17 11:11:11 +0530473
Nabin Haite7679702015-02-20 14:40:35 +0530474 for tax in self.doc.get("taxes"):
475 if tax.charge_type == "Actual":
Nabin Haitaf9bdfe2017-12-12 18:50:05 +0530476 tax_amount = self.get_tax_amount_if_for_valuation_or_deduction(tax.tax_amount, tax)
477 actual_taxes_dict.setdefault(tax.idx, tax_amount)
Nabin Haite7679702015-02-20 14:40:35 +0530478 elif tax.row_id in actual_taxes_dict:
479 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * flt(tax.rate) / 100
480 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
Nabin Hait3237c752015-02-17 11:11:11 +0530481
Nabin Hait877e1bb2017-11-17 12:27:43 +0530482 return flt(self.doc.grand_total - sum(actual_taxes_dict.values()),
483 self.doc.precision("grand_total"))
Nabin Hait3237c752015-02-17 11:11:11 +0530484
485
Nabin Hait7b19b9e2015-02-24 09:42:24 +0530486 def calculate_total_advance(self):
487 if self.doc.docstatus < 2:
Nabin Haite7679702015-02-20 14:40:35 +0530488 total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
Nabin Hait3237c752015-02-17 11:11:11 +0530489 for adv in self.doc.get("advances")])
490
Nabin Haite7679702015-02-20 14:40:35 +0530491 self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530492
Faris Ansari6041f5c2018-02-08 13:33:52 +0530493 grand_total = self.doc.rounded_total or self.doc.grand_total
494
Nabin Hait289ffb72016-02-08 11:06:55 +0530495 if self.doc.party_account_currency == self.doc.currency:
Faris Ansari6041f5c2018-02-08 13:33:52 +0530496 invoice_total = flt(grand_total - flt(self.doc.write_off_amount),
Nabin Hait289ffb72016-02-08 11:06:55 +0530497 self.doc.precision("grand_total"))
Nabin Hait8d8cba72017-04-03 17:26:22 +0530498 else:
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530499 base_write_off_amount = flt(flt(self.doc.write_off_amount) * self.doc.conversion_rate,
Nabin Hait8d8cba72017-04-03 17:26:22 +0530500 self.doc.precision("base_write_off_amount"))
Faris Ansari6041f5c2018-02-08 13:33:52 +0530501 invoice_total = flt(grand_total * self.doc.conversion_rate,
Nabin Hait8d8cba72017-04-03 17:26:22 +0530502 self.doc.precision("grand_total")) - base_write_off_amount
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530503
Nabin Haitadc09232016-02-09 10:31:11 +0530504 if invoice_total > 0 and self.doc.total_advance > invoice_total:
Nabin Hait289ffb72016-02-08 11:06:55 +0530505 frappe.throw(_("Advance amount cannot be greater than {0} {1}")
506 .format(self.doc.party_account_currency, invoice_total))
Nabin Hait3237c752015-02-17 11:11:11 +0530507
Rushabh Mehta8bb6e532015-02-18 20:22:59 +0530508 if self.doc.docstatus == 0:
Nabin Hait3237c752015-02-17 11:11:11 +0530509 self.calculate_outstanding_amount()
510
511 def calculate_outstanding_amount(self):
512 # NOTE:
513 # write_off_amount is only for POS Invoice
514 # total_advance is only for non POS Invoice
Rohit Waghchaureefb5bf22016-08-25 02:09:53 +0530515 if self.doc.doctype == "Sales Invoice":
516 self.calculate_paid_amount()
517
Deepesh Garg0ebace52020-02-25 13:21:16 +0530518 if self.doc.is_return and self.doc.return_against and not self.doc.get('is_pos'): return
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530519
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530520 self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530521 self._set_in_company_currency(self.doc, ['write_off_amount'])
522
Nabin Hait877e1bb2017-11-17 12:27:43 +0530523 if self.doc.doctype in ["Sales Invoice", "Purchase Invoice"]:
524 grand_total = self.doc.rounded_total or self.doc.grand_total
525 if self.doc.party_account_currency == self.doc.currency:
Manas Solankida486ee2018-07-06 12:36:57 +0530526 total_amount_to_pay = flt(grand_total - self.doc.total_advance
Nabin Hait877e1bb2017-11-17 12:27:43 +0530527 - flt(self.doc.write_off_amount), self.doc.precision("grand_total"))
528 else:
529 total_amount_to_pay = flt(flt(grand_total *
530 self.doc.conversion_rate, self.doc.precision("grand_total")) - self.doc.total_advance
531 - flt(self.doc.base_write_off_amount), self.doc.precision("grand_total"))
Anand Doshi15f7b1e2016-04-04 15:03:28 +0530532
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530533 self.doc.round_floats_in(self.doc, ["paid_amount"])
Nabin Hait877e1bb2017-11-17 12:27:43 +0530534 change_amount = 0
535
Deepesh Garg0ebace52020-02-25 13:21:16 +0530536 if self.doc.doctype == "Sales Invoice" and not self.doc.get('is_return'):
Nabin Hait877e1bb2017-11-17 12:27:43 +0530537 self.calculate_write_off_amount()
538 self.calculate_change_amount()
539 change_amount = self.doc.change_amount \
540 if self.doc.party_account_currency == self.doc.currency else self.doc.base_change_amount
541
Nabin Haitac3b2aa2017-05-30 15:35:01 +0530542 paid_amount = self.doc.paid_amount \
543 if self.doc.party_account_currency == self.doc.currency else self.doc.base_paid_amount
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530544
Nabin Hait877e1bb2017-11-17 12:27:43 +0530545 self.doc.outstanding_amount = flt(total_amount_to_pay - flt(paid_amount) + flt(change_amount),
546 self.doc.precision("outstanding_amount"))
Rushabh Mehtac6bd7ad2016-12-21 17:30:29 +0530547
Deepesh Garg0ebace52020-02-25 13:21:16 +0530548 if self.doc.doctype == 'Sales Invoice' and self.doc.get('is_pos') and self.doc.get('is_return'):
549 self.update_paid_amount_for_return(total_amount_to_pay)
550
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530551 def calculate_paid_amount(self):
Manas Solankida486ee2018-07-06 12:36:57 +0530552
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530553 paid_amount = base_paid_amount = 0.0
Rohit Waghchauref58cad62017-01-17 12:11:57 +0530554
555 if self.doc.is_pos:
556 for payment in self.doc.get('payments'):
Ayush Shuklae9cf1ab2017-05-25 14:14:55 +0530557 payment.amount = flt(payment.amount)
558 payment.base_amount = payment.amount * flt(self.doc.conversion_rate)
Rohit Waghchauref58cad62017-01-17 12:11:57 +0530559 paid_amount += payment.amount
560 base_paid_amount += payment.base_amount
rohitwaghchaure73456ac2017-05-16 11:29:57 +0530561 elif not self.doc.is_return:
562 self.doc.set('payments', [])
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530563
Manas Solankida486ee2018-07-06 12:36:57 +0530564 if self.doc.redeem_loyalty_points and self.doc.loyalty_amount:
565 base_paid_amount += self.doc.loyalty_amount
566 paid_amount += (self.doc.loyalty_amount / flt(self.doc.conversion_rate))
567
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530568 self.doc.paid_amount = flt(paid_amount, self.doc.precision("paid_amount"))
569 self.doc.base_paid_amount = flt(base_paid_amount, self.doc.precision("base_paid_amount"))
570
Nabin Hait3bb1a422016-08-02 16:41:10 +0530571 def calculate_change_amount(self):
572 self.doc.change_amount = 0.0
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530573 self.doc.base_change_amount = 0.0
Nabin Hait877e1bb2017-11-17 12:27:43 +0530574
575 if self.doc.doctype == "Sales Invoice" \
576 and self.doc.paid_amount > self.doc.grand_total and not self.doc.is_return \
Nabin Haitac3b2aa2017-05-30 15:35:01 +0530577 and any([d.type == "Cash" for d in self.doc.payments]):
Rohit Waghchauree8d22bb2018-02-05 18:13:29 +0530578 grand_total = self.doc.rounded_total or self.doc.grand_total
579 base_grand_total = self.doc.base_rounded_total or self.doc.base_grand_total
Nabin Haitac3b2aa2017-05-30 15:35:01 +0530580
Rohit Waghchauree8d22bb2018-02-05 18:13:29 +0530581 self.doc.change_amount = flt(self.doc.paid_amount - grand_total +
Nabin Hait3bb1a422016-08-02 16:41:10 +0530582 self.doc.write_off_amount, self.doc.precision("change_amount"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530583
Rohit Waghchauree8d22bb2018-02-05 18:13:29 +0530584 self.doc.base_change_amount = flt(self.doc.base_paid_amount - base_grand_total +
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530585 self.doc.base_write_off_amount, self.doc.precision("base_change_amount"))
mbauskar36b51892016-01-18 16:31:10 +0530586
Rohit Waghchaure7127a8f2016-08-04 14:56:15 +0530587 def calculate_write_off_amount(self):
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530588 if flt(self.doc.change_amount) > 0:
Nabin Hait877e1bb2017-11-17 12:27:43 +0530589 self.doc.write_off_amount = flt(self.doc.grand_total - self.doc.paid_amount
590 + self.doc.change_amount, self.doc.precision("write_off_amount"))
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530591 self.doc.base_write_off_amount = flt(self.doc.write_off_amount * self.doc.conversion_rate,
592 self.doc.precision("base_write_off_amount"))
Rohit Waghchaure7127a8f2016-08-04 14:56:15 +0530593
mbauskar36b51892016-01-18 16:31:10 +0530594 def calculate_margin(self, item):
Shreya Shahf718b0c2018-02-20 11:26:46 +0530595
Makarand Bauskar0e4c5c92017-05-11 11:40:02 +0530596 rate_with_margin = 0.0
Shreya Shahbe690ef2017-11-14 17:22:41 +0530597 base_rate_with_margin = 0.0
mbauskar36b51892016-01-18 16:31:10 +0530598 if item.price_list_rate:
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530599 if item.pricing_rules and not self.doc.ignore_pricing_rule:
Deepesh Garg7ac4ad82020-07-23 12:12:55 +0530600 for d in json.loads(item.pricing_rules):
rohitwaghchaurea85ddf22019-11-19 18:47:48 +0530601 pricing_rule = frappe.get_cached_doc('Pricing Rule', d)
Shreya Shahf718b0c2018-02-20 11:26:46 +0530602
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530603 if (pricing_rule.margin_type == 'Amount' and pricing_rule.currency == self.doc.currency)\
604 or (pricing_rule.margin_type == 'Percentage'):
605 item.margin_type = pricing_rule.margin_type
606 item.margin_rate_or_amount = pricing_rule.margin_rate_or_amount
607 else:
608 item.margin_type = None
609 item.margin_rate_or_amount = 0.0
mbauskar36b51892016-01-18 16:31:10 +0530610
mbauskara52472c2016-03-05 15:10:25 +0530611 if item.margin_type and item.margin_rate_or_amount:
612 margin_value = item.margin_rate_or_amount if item.margin_type == 'Amount' else flt(item.price_list_rate) * flt(item.margin_rate_or_amount) / 100
Makarand Bauskar0e4c5c92017-05-11 11:40:02 +0530613 rate_with_margin = flt(item.price_list_rate) + flt(margin_value)
Shreya Shahbe690ef2017-11-14 17:22:41 +0530614 base_rate_with_margin = flt(rate_with_margin) * flt(self.doc.conversion_rate)
mbauskar36b51892016-01-18 16:31:10 +0530615
Shreya Shahbe690ef2017-11-14 17:22:41 +0530616 return rate_with_margin, base_rate_with_margin
Nabin Hait852cb642017-07-05 12:58:19 +0530617
618 def set_item_wise_tax_breakup(self):
Nabin Hait9c421612017-07-20 13:32:01 +0530619 self.doc.other_charges_calculation = get_itemised_tax_breakup_html(self.doc)
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530620
Deepesh Garg0ebace52020-02-25 13:21:16 +0530621 def update_paid_amount_for_return(self, total_amount_to_pay):
Saqiba6f98d42020-07-23 18:51:26 +0530622 default_mode_of_payment = frappe.db.get_value('POS Payment Method',
623 {'parent': self.doc.pos_profile, 'default': 1}, ['mode_of_payment'], as_dict=1)
Deepesh Garg0ebace52020-02-25 13:21:16 +0530624
625 self.doc.payments = []
626
627 if default_mode_of_payment:
628 self.doc.append('payments', {
629 'mode_of_payment': default_mode_of_payment.mode_of_payment,
Deepesh Garg0ebace52020-02-25 13:21:16 +0530630 'amount': total_amount_to_pay
631 })
632 else:
633 self.doc.is_pos = 0
634 self.doc.pos_profile = ''
635
636 self.calculate_paid_amount()
637
638
Nabin Hait9c421612017-07-20 13:32:01 +0530639def get_itemised_tax_breakup_html(doc):
640 if not doc.taxes:
641 return
642 frappe.flags.company = doc.company
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530643
Nabin Hait9c421612017-07-20 13:32:01 +0530644 # get headers
Nabin Haitcaab5822017-08-24 16:22:28 +0530645 tax_accounts = []
646 for tax in doc.taxes:
647 if getattr(tax, "category", None) and tax.category=="Valuation":
648 continue
rohitwaghchaure4e17fae2017-12-12 14:40:52 +0530649 if tax.description not in tax_accounts:
Nabin Haitcaab5822017-08-24 16:22:28 +0530650 tax_accounts.append(tax.description)
651
Nabin Hait9c421612017-07-20 13:32:01 +0530652 headers = get_itemised_tax_breakup_header(doc.doctype + " Item", tax_accounts)
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530653
Nabin Hait9c421612017-07-20 13:32:01 +0530654 # get tax breakup data
655 itemised_tax, itemised_taxable_amount = get_itemised_tax_breakup_data(doc)
Nabin Haitcaab5822017-08-24 16:22:28 +0530656
657 get_rounded_tax_amount(itemised_tax, doc.precision("tax_amount", "taxes"))
658
rohitwaghchaured4526682017-12-28 14:20:13 +0530659 update_itemised_tax_data(doc)
Nabin Hait9c421612017-07-20 13:32:01 +0530660 frappe.flags.company = None
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530661
Nabin Hait9c421612017-07-20 13:32:01 +0530662 return frappe.render_template(
663 "templates/includes/itemised_tax_breakup.html", dict(
664 headers=headers,
665 itemised_tax=itemised_tax,
666 itemised_taxable_amount=itemised_taxable_amount,
667 tax_accounts=tax_accounts,
Saqib Ansari10a6a2d2020-04-13 16:40:13 +0530668 doc=doc
Nabin Haitb962fc12017-07-17 18:02:31 +0530669 )
Nabin Hait9c421612017-07-20 13:32:01 +0530670 )
Nabin Hait852cb642017-07-05 12:58:19 +0530671
rohitwaghchaured4526682017-12-28 14:20:13 +0530672
673@erpnext.allow_regional
674def update_itemised_tax_data(doc):
675 #Don't delete this method, used for localization
676 pass
677
Nabin Haitb962fc12017-07-17 18:02:31 +0530678@erpnext.allow_regional
679def get_itemised_tax_breakup_header(item_doctype, tax_accounts):
680 return [_("Item"), _("Taxable Amount")] + tax_accounts
681
682@erpnext.allow_regional
683def get_itemised_tax_breakup_data(doc):
684 itemised_tax = get_itemised_tax(doc.taxes)
685
686 itemised_taxable_amount = get_itemised_taxable_amount(doc.items)
687
688 return itemised_tax, itemised_taxable_amount
689
Nabin Hait34c551d2019-07-03 10:34:31 +0530690def get_itemised_tax(taxes, with_tax_account=False):
Nabin Haitb962fc12017-07-17 18:02:31 +0530691 itemised_tax = {}
692 for tax in taxes:
Nabin Haitcaab5822017-08-24 16:22:28 +0530693 if getattr(tax, "category", None) and tax.category=="Valuation":
694 continue
695
Nabin Haitb962fc12017-07-17 18:02:31 +0530696 item_tax_map = json.loads(tax.item_wise_tax_detail) if tax.item_wise_tax_detail else {}
Nabin Hait2e4de832017-09-19 14:53:16 +0530697 if item_tax_map:
698 for item_code, tax_data in item_tax_map.items():
699 itemised_tax.setdefault(item_code, frappe._dict())
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530700
Prateeksha Singhea7533f2018-06-11 13:31:33 +0530701 tax_rate = 0.0
702 tax_amount = 0.0
703
Nabin Hait2e4de832017-09-19 14:53:16 +0530704 if isinstance(tax_data, list):
Prateeksha Singhea7533f2018-06-11 13:31:33 +0530705 tax_rate = flt(tax_data[0])
706 tax_amount = flt(tax_data[1])
Nabin Hait2e4de832017-09-19 14:53:16 +0530707 else:
Prateeksha Singhea7533f2018-06-11 13:31:33 +0530708 tax_rate = flt(tax_data)
709
710 itemised_tax[item_code][tax.description] = frappe._dict(dict(
711 tax_rate = tax_rate,
712 tax_amount = tax_amount
713 ))
Rohit Waghchaure296fbfe2017-07-10 13:03:29 +0530714
Nabin Hait34c551d2019-07-03 10:34:31 +0530715 if with_tax_account:
716 itemised_tax[item_code][tax.description].tax_account = tax.account_head
717
Nabin Haitb962fc12017-07-17 18:02:31 +0530718 return itemised_tax
719
720def get_itemised_taxable_amount(items):
721 itemised_taxable_amount = frappe._dict()
722 for item in items:
Rohit Waghchaure296fbfe2017-07-10 13:03:29 +0530723 item_code = item.item_code or item.item_name
Nabin Haitb962fc12017-07-17 18:02:31 +0530724 itemised_taxable_amount.setdefault(item_code, 0)
725 itemised_taxable_amount[item_code] += item.net_amount
726
Nabin Haitcaab5822017-08-24 16:22:28 +0530727 return itemised_taxable_amount
728
729def get_rounded_tax_amount(itemised_tax, precision):
730 # Rounding based on tax_amount precision
731 for taxes in itemised_tax.values():
732 for tax_account in taxes:
Himanshu Mishra35b26272018-11-13 11:13:04 +0530733 taxes[tax_account]["tax_amount"] = flt(taxes[tax_account]["tax_amount"], precision)