mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-04 14:08:29 +00:00
[calculations] [client/server] first cut
This commit is contained in:
@@ -17,34 +17,45 @@
|
||||
from __future__ import unicode_literals
|
||||
import webnotes
|
||||
from webnotes import _, msgprint
|
||||
from webnotes.utils import flt, cint
|
||||
import json
|
||||
from webnotes.utils import flt
|
||||
|
||||
from buying.utils import get_item_details
|
||||
from setup.utils import get_company_currency
|
||||
from utilities.transaction_base import validate_conversion_rate
|
||||
|
||||
from controllers.stock_controller import StockController
|
||||
|
||||
class WrongWarehouseCompany(Exception): pass
|
||||
|
||||
class BuyingController(StockController):
|
||||
def onload_post_render(self):
|
||||
self.set_price_list_currency("buying")
|
||||
|
||||
# contact, address, item details
|
||||
self.set_missing_values()
|
||||
|
||||
self.set_taxes("Purchase Taxes and Charges", "purchase_tax_details", "purchase_other_charges")
|
||||
|
||||
def validate(self):
|
||||
super(BuyingController, self).validate()
|
||||
self.validate_stock_or_nonstock_items()
|
||||
self.validate_warehouse_belongs_to_company()
|
||||
|
||||
if self.meta.get_field("currency"):
|
||||
self.company_currency = get_company_currency(self.doc.company)
|
||||
validate_conversion_rate(self.doc.currency, self.doc.conversion_rate,
|
||||
self.meta.get_label("conversion_rate"), self.doc.company)
|
||||
|
||||
# IMPORTANT: enable this only when client side code is similar to this one
|
||||
# self.calculate_taxes_and_totals()
|
||||
def set_missing_values(self, for_validate=False):
|
||||
# set contact and address details for supplier, if they are not mentioned
|
||||
if self.doc.supplier and not (self.doc.contact_person and self.doc.supplier_address):
|
||||
for fieldname, val in self.get_default_address_and_contact("supplier").items():
|
||||
if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
|
||||
self.doc.fields[fieldname] = val
|
||||
|
||||
self.set_missing_item_details(get_item_details)
|
||||
|
||||
def set_supplier_defaults(self):
|
||||
self.get_default_supplier_address()
|
||||
|
||||
# set total in words
|
||||
self.set_total_in_words()
|
||||
|
||||
def get_purchase_tax_details(self):
|
||||
self.doclist = self.doc.clear_table(self.doclist, "purchase_tax_details")
|
||||
self.set_taxes()
|
||||
|
||||
def validate_warehouse_belongs_to_company(self):
|
||||
for warehouse, company in webnotes.conn.get_values("Warehouse",
|
||||
self.doclist.get_distinct_values("warehouse"), "company").items():
|
||||
@@ -70,24 +81,6 @@ class BuyingController(StockController):
|
||||
webnotes.msgprint(_("""Tax Category can not be 'Valuation' or 'Valuation and Total'
|
||||
as all items are non-stock items"""), raise_exception=1)
|
||||
|
||||
def update_item_details(self):
|
||||
for item in self.doclist.get({"parentfield": self.fname}):
|
||||
ret = get_item_details({
|
||||
"doctype": self.doc.doctype,
|
||||
"docname": self.doc.name,
|
||||
"item_code": item.item_code,
|
||||
"warehouse": item.warehouse,
|
||||
"supplier": self.doc.supplier,
|
||||
"transaction_date": self.doc.posting_date,
|
||||
"conversion_rate": self.doc.conversion_rate,
|
||||
"price_list_name": self.doc.price_list_name,
|
||||
"price_list_currency": self.doc.price_list_currency,
|
||||
"plc_conversion_rate": self.doc.plc_conversion_rate
|
||||
})
|
||||
for r in ret:
|
||||
if not item.fields.get(r):
|
||||
item.fields[r] = ret[r]
|
||||
|
||||
def set_total_in_words(self):
|
||||
from webnotes.utils import money_in_words
|
||||
company_currency = get_company_currency(self.doc.company)
|
||||
@@ -98,26 +91,11 @@ class BuyingController(StockController):
|
||||
self.doc.currency)
|
||||
|
||||
def calculate_taxes_and_totals(self):
|
||||
self.doc.conversion_rate = flt(self.doc.conversion_rate)
|
||||
self.item_doclist = self.doclist.get({"parentfield": self.fname})
|
||||
self.tax_doclist = self.doclist.get({"parentfield": "purchase_tax_details"})
|
||||
|
||||
self.calculate_item_values()
|
||||
self.initialize_taxes()
|
||||
self.calculate_net_total()
|
||||
self.calculate_taxes()
|
||||
self.calculate_totals()
|
||||
self.other_fname = "purchase_tax_details"
|
||||
super(BuyingController, self).calculate_taxes_and_totals()
|
||||
self.calculate_outstanding_amount()
|
||||
|
||||
self._cleanup()
|
||||
|
||||
def calculate_item_values(self):
|
||||
def _set_base(item, print_field, base_field):
|
||||
"""set values in base currency"""
|
||||
item.fields[base_field] = flt((flt(item.fields[print_field],
|
||||
self.precision(print_field, item)) * self.doc.conversion_rate),
|
||||
self.precision(base_field, item))
|
||||
|
||||
# hack! - cleaned up in _cleanup()
|
||||
if self.doc.doctype != "Purchase Invoice":
|
||||
df = self.meta.get_field("purchase_rate", parentfield=self.fname)
|
||||
@@ -130,119 +108,36 @@ class BuyingController(StockController):
|
||||
|
||||
self.round_floats_in(item)
|
||||
|
||||
if item.discount_rate == 100:
|
||||
item.import_ref_rate = item.import_ref_rate or item.import_rate
|
||||
item.import_rate = 0
|
||||
else:
|
||||
if item.import_ref_rate:
|
||||
item.import_rate = flt(item.import_ref_rate * (1.0 - (item.discount_rate / 100.0)),
|
||||
self.precision("import_rate", item))
|
||||
else:
|
||||
# assume that print rate and discount_rate are specified
|
||||
item.import_ref_rate = flt(item.import_rate / (1.0 - (item.discount_rate / 100.0)),
|
||||
self.precision("import_ref_rate", item))
|
||||
if item.discount_rate == 100.0:
|
||||
item.import_rate = 0.0
|
||||
elif item.import_ref_rate:
|
||||
item.import_rate = flt(item.import_ref_rate * (1.0 - (item.discount_rate / 100.0)),
|
||||
self.precision("import_rate", item))
|
||||
|
||||
item.import_amount = flt(item.import_rate * item.qty,
|
||||
self.precision("import_amount", item))
|
||||
item.item_tax_amount = 0.0;
|
||||
|
||||
_set_base(item, "import_ref_rate", "purchase_ref_rate")
|
||||
_set_base(item, "import_rate", "rate")
|
||||
_set_base(item, "import_amount", "amount")
|
||||
self._set_in_company_currency(item, "import_ref_rate", "purchase_ref_rate")
|
||||
self._set_in_company_currency(item, "import_rate", "rate")
|
||||
self._set_in_company_currency(item, "import_amount", "amount")
|
||||
|
||||
def initialize_taxes(self):
|
||||
for tax in self.tax_doclist:
|
||||
# initialize totals to 0
|
||||
tax.tax_amount = tax.total = 0.0
|
||||
|
||||
# temporary fields
|
||||
tax.tax_amount_for_current_item = tax.grand_total_for_current_item = 0.0
|
||||
|
||||
tax.item_wise_tax_detail = {}
|
||||
|
||||
self.validate_on_previous_row(tax)
|
||||
|
||||
self.round_floats_in(tax)
|
||||
|
||||
def calculate_net_total(self):
|
||||
self.doc.net_total = 0
|
||||
self.doc.net_total_import = 0
|
||||
self.doc.net_total = self.doc.net_total_import = 0.0
|
||||
|
||||
for item in self.item_doclist:
|
||||
self.doc.net_total += item.amount
|
||||
self.doc.net_total_import += item.import_amount
|
||||
|
||||
self.doc.net_total = flt(self.doc.net_total, self.precision("net_total"))
|
||||
self.doc.net_total_import = flt(self.doc.net_total_import,
|
||||
self.precision("net_total_import"))
|
||||
|
||||
def calculate_taxes(self):
|
||||
for item in self.item_doclist:
|
||||
item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
|
||||
item.item_tax_amount = 0
|
||||
|
||||
for i, tax in enumerate(self.tax_doclist):
|
||||
# tax_amount represents the amount of tax for the current step
|
||||
current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
|
||||
|
||||
self.set_item_tax_amount(item, tax, current_tax_amount)
|
||||
|
||||
# case when net total is 0 but there is an actual type charge
|
||||
# in this case add the actual amount to tax.tax_amount
|
||||
# and tax.grand_total_for_current_item for the first such iteration
|
||||
if not (current_tax_amount or self.doc.net_total or tax.tax_amount) and \
|
||||
tax.charge_type=="Actual":
|
||||
zero_net_total_adjustment = flt(tax.rate, self.precision("tax_amount", tax))
|
||||
current_tax_amount += zero_net_total_adjustment
|
||||
|
||||
# store tax_amount for current item as it will be used for
|
||||
# charge type = 'On Previous Row Amount'
|
||||
tax.tax_amount_for_current_item = current_tax_amount
|
||||
|
||||
# accumulate tax amount into tax.tax_amount
|
||||
tax.tax_amount += tax.tax_amount_for_current_item
|
||||
|
||||
if tax.category == "Valuation":
|
||||
# if just for valuation, do not add the tax amount in total
|
||||
# hence, setting it as 0 for further steps
|
||||
current_tax_amount = 0
|
||||
else:
|
||||
current_tax_amount *= tax.add_deduct_tax == "Deduct" and -1.0 or 1.0
|
||||
|
||||
# Calculate tax.total viz. grand total till that step
|
||||
# note: grand_total_for_current_item contains the contribution of
|
||||
# item's amount, previously applied tax and the current tax on that item
|
||||
if i==0:
|
||||
tax.grand_total_for_current_item = flt(item.amount +
|
||||
current_tax_amount, self.precision("total", tax))
|
||||
|
||||
else:
|
||||
tax.grand_total_for_current_item = \
|
||||
flt(self.tax_doclist[i-1].grand_total_for_current_item +
|
||||
current_tax_amount, self.precision("total", tax))
|
||||
|
||||
# in tax.total, accumulate grand total of each item
|
||||
tax.total += tax.grand_total_for_current_item
|
||||
|
||||
# store tax_breakup for each item
|
||||
# DOUBT: should valuation type amount also be stored?
|
||||
tax.item_wise_tax_detail[item.item_code] = current_tax_amount
|
||||
self.round_floats_in(self.doc, ["net_total", "net_total_import"])
|
||||
|
||||
def calculate_totals(self):
|
||||
if self.tax_doclist:
|
||||
self.doc.grand_total = flt(self.tax_doclist[-1].total,
|
||||
self.precision("grand_total"))
|
||||
self.doc.grand_total_import = flt(
|
||||
self.doc.grand_total / self.doc.conversion_rate,
|
||||
self.precision("grand_total_import"))
|
||||
else:
|
||||
self.doc.grand_total = flt(self.doc.net_total,
|
||||
self.precision("grand_total"))
|
||||
self.doc.grand_total_import = flt(
|
||||
self.doc.grand_total / self.doc.conversion_rate,
|
||||
self.precision("grand_total_import"))
|
||||
self.doc.grand_total = flt(self.tax_doclist and \
|
||||
self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total"))
|
||||
self.doc.grand_total_import = flt(self.doc.grand_total / self.doc.conversion_rate,
|
||||
self.precision("grand_total_import"))
|
||||
|
||||
self.doc.total_tax = \
|
||||
flt(self.doc.grand_total - self.doc.net_total,
|
||||
self.doc.total_tax = flt(self.doc.grand_total - self.doc.net_total,
|
||||
self.precision("total_tax"))
|
||||
|
||||
if self.meta.get_field("rounded_total"):
|
||||
@@ -261,68 +156,18 @@ class BuyingController(StockController):
|
||||
self.precision("outstanding_amount"))
|
||||
|
||||
def _cleanup(self):
|
||||
for tax in self.tax_doclist:
|
||||
del tax.fields["grand_total_for_current_item"]
|
||||
del tax.fields["tax_amount_for_current_item"]
|
||||
tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail)
|
||||
|
||||
# except in purchase invoice, rate field is purchase_rate
|
||||
if self.doc.doctype != "Purchase Invoice":
|
||||
for item in self.item_doclist:
|
||||
item.purchase_rate = item.rate
|
||||
del item.fields["rate"]
|
||||
|
||||
super(BuyingController, self)._cleanup()
|
||||
|
||||
# except in purchase invoice, rate field is purchase_rate
|
||||
# reset fieldname of rate
|
||||
if self.doc.doctype != "Purchase Invoice":
|
||||
df = self.meta.get_field("rate", parentfield=self.fname)
|
||||
df.fieldname = "purchase_rate"
|
||||
|
||||
def validate_on_previous_row(self, tax):
|
||||
"""
|
||||
validate if a valid row id is mentioned in case of
|
||||
On Previous Row Amount and On Previous Row Total
|
||||
"""
|
||||
if tax.charge_type in ["On Previous Row Amount", "On Previous Row Total"] and \
|
||||
(not tax.row_id or cint(tax.row_id) >= tax.idx):
|
||||
msgprint((_("Row") + " # %(idx)s [%(taxes_doctype)s]: " + \
|
||||
_("Please specify a valid") + " %(row_id_label)s") % {
|
||||
"idx": tax.idx,
|
||||
"taxes_doctype": tax.parenttype,
|
||||
"row_id_label": self.meta.get_label("row_id",
|
||||
parentfield="purchase_tax_details")
|
||||
}, raise_exception=True)
|
||||
|
||||
def _load_item_tax_rate(self, item_tax_rate):
|
||||
if not item_tax_rate:
|
||||
return {}
|
||||
return json.loads(item_tax_rate)
|
||||
|
||||
def get_current_tax_amount(self, item, tax, item_tax_map):
|
||||
tax_rate = self._get_tax_rate(tax, item_tax_map)
|
||||
|
||||
if tax.charge_type == "Actual":
|
||||
# distribute the tax amount proportionally to each item row
|
||||
actual = flt(tax.rate, self.precision("tax_amount", tax))
|
||||
current_tax_amount = (self.doc.net_total
|
||||
and ((item.amount / self.doc.net_total) * actual)
|
||||
or 0)
|
||||
elif tax.charge_type == "On Net Total":
|
||||
current_tax_amount = (tax_rate / 100.0) * item.amount
|
||||
elif tax.charge_type == "On Previous Row Amount":
|
||||
current_tax_amount = (tax_rate / 100.0) * \
|
||||
self.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item
|
||||
elif tax.charge_type == "On Previous Row Total":
|
||||
current_tax_amount = (tax_rate / 100.0) * \
|
||||
self.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item
|
||||
|
||||
return flt(current_tax_amount, self.precision("tax_amount", tax))
|
||||
|
||||
def _get_tax_rate(self, tax, item_tax_map):
|
||||
if item_tax_map.has_key(tax.account_head):
|
||||
return flt(item_tax_map.get(tax.account_head), self.precision("rate", tax))
|
||||
else:
|
||||
return tax.rate
|
||||
|
||||
for item in self.item_doclist:
|
||||
item.purchase_rate = item.rate
|
||||
del item.fields["rate"]
|
||||
|
||||
def set_item_tax_amount(self, item, tax, current_tax_amount):
|
||||
"""
|
||||
item_tax_amount is the total tax amount applied on that item
|
||||
@@ -330,10 +175,8 @@ class BuyingController(StockController):
|
||||
|
||||
TODO: rename item_tax_amount to valuation_tax_amount
|
||||
"""
|
||||
if tax.category in ["Valuation", "Valuation and Total"] and \
|
||||
item.item_code in self.stock_items:
|
||||
item.item_tax_amount += flt(current_tax_amount,
|
||||
self.precision("item_tax_amount", item))
|
||||
if tax.category in ["Valuation", "Valuation and Total"]:
|
||||
item.item_tax_amount += flt(current_tax_amount, self.precision("item_tax_amount", item))
|
||||
|
||||
# update valuation rate
|
||||
def update_valuation_rate(self, parentfield):
|
||||
|
||||
Reference in New Issue
Block a user