purchase invoice taxes and totals calculations with test cases

This commit is contained in:
Anand Doshi 2013-02-08 19:28:14 +05:30
parent a11a21c6a9
commit 8595fcf910
5 changed files with 321 additions and 182 deletions

View File

@ -32,7 +32,6 @@ class DocType(BuyingController):
self.tname = 'Purchase Invoice Item'
self.fname = 'entries'
# ************************** Trigger Functions ****************************
# Credit To

View File

@ -19,154 +19,41 @@ from __future__ import unicode_literals
import unittest
import webnotes
import webnotes.model
from webnotes.utils import nowdate
from stock.doctype.purchase_receipt import test_purchase_receipt
company = webnotes.conn.get_default("company")
abbr = webnotes.conn.get_value("Company", company, "abbr")
def load_data():
test_purchase_receipt.load_data()
webnotes.insert({"doctype": "Account", "account_name": "Cost for Goods Sold",
"parent_account": "Expenses - %s" % abbr, "company": company,
"group_or_ledger": "Ledger"})
webnotes.insert({"doctype": "Account", "account_name": "Excise Duty",
"parent_account": "_Test Tax Assets - %s" % abbr, "company": company,
"group_or_ledger": "Ledger"})
webnotes.insert({"doctype": "Account", "account_name": "Education Cess",
"parent_account": "_Test Tax Assets - %s" % abbr, "company": company,
"group_or_ledger": "Ledger"})
webnotes.insert({"doctype": "Account", "account_name": "S&H Education Cess",
"parent_account": "_Test Tax Assets - %s" % abbr, "company": company,
"group_or_ledger": "Ledger"})
webnotes.insert({"doctype": "Account", "account_name": "CST",
"parent_account": "Direct Expenses - %s" % abbr, "company": company,
"group_or_ledger": "Ledger"})
webnotes.insert({"doctype": "Account", "account_name": "Discount",
"parent_account": "Direct Expenses - %s" % abbr, "company": company,
"group_or_ledger": "Ledger"})
from webnotes.model.doc import Document
item = Document("Item", "Home Desktop 100")
# excise duty
item_tax = item.addchild("item_tax", "Item Tax")
item_tax.tax_type = "Excise Duty - %s" % abbr
item_tax.tax_rate = 10
item_tax.save()
import json
purchase_invoice_doclist = [
# parent
{
"doctype": "Purchase Invoice",
"credit_to": "East Wind Inc. - %s" % abbr,
"supplier_name": "East Wind Inc.",
"naming_series": "BILL", "posting_date": nowdate(),
"company": company, "fiscal_year": webnotes.conn.get_default("fiscal_year"),
"currency": webnotes.conn.get_default("currency"), "conversion_rate": 1,
'net_total': 1250.00, 'grand_total': 1512.30, 'grand_total_import': 1512.30,
},
# items
{
"doctype": "Purchase Invoice Item",
"item_code": "Home Desktop 100", "qty": 10, "import_rate": 50, "rate": 50,
"amount": 500, "import_amount": 500, "parentfield": "entries",
"uom": "Nos", "item_tax_rate": json.dumps({"Excise Duty - %s" % abbr: 10}),
"expense_head": "Cost for Goods Sold - %s" % abbr,
"cost_center": "Default Cost Center - %s" % abbr
},
{
"doctype": "Purchase Invoice Item",
"item_code": "Home Desktop 200", "qty": 5, "import_rate": 150, "rate": 150,
"amount": 750, "import_amount": 750, "parentfield": "entries", "uom": "Nos",
"expense_head": "Cost for Goods Sold - %s" % abbr,
"cost_center": "Default Cost Center - %s" % abbr
},
# taxes
{
"doctype": "Purchase Taxes and Charges", "charge_type": "Actual",
"account_head": "Shipping Charges - %s" % abbr, "rate": 100, "tax_amount": 100,
"category": "Valuation and Total", "parentfield": "purchase_tax_details",
"cost_center": "Default Cost Center - %s" % abbr, "add_deduct_tax": "Add"
},
{
"doctype": "Purchase Taxes and Charges", "charge_type": "On Net Total",
"account_head": "Customs Duty - %s" % abbr, "rate": 10, "tax_amount": 125.00,
"category": "Valuation", "parentfield": "purchase_tax_details",
"cost_center": "Default Cost Center - %s" % abbr, "add_deduct_tax": "Add"
},
{
"doctype": "Purchase Taxes and Charges", "charge_type": "On Net Total",
"account_head": "Excise Duty - %s" % abbr, "rate": 12, "tax_amount": 140.00,
"category": "Total", "parentfield": "purchase_tax_details", "add_deduct_tax": "Add"
},
{
"doctype": "Purchase Taxes and Charges", "charge_type": "On Previous Row Amount",
"account_head": "Education Cess - %s" % abbr, "rate": 2, "row_id": 3, "tax_amount": 2.80,
"category": "Total", "parentfield": "purchase_tax_details", "add_deduct_tax": "Add"
},
{
"doctype": "Purchase Taxes and Charges", "charge_type": "On Previous Row Amount",
"account_head": "S&H Education Cess - %s" % abbr, "rate": 1, "row_id": 3,
"tax_amount": 1.4, "category": "Total", "parentfield": "purchase_tax_details",
"add_deduct_tax": "Add"
},
{
"doctype": "Purchase Taxes and Charges", "charge_type": "On Previous Row Total",
"account_head": "CST - %s" % abbr, "rate": 2, "row_id": 5, "tax_amount": 29.88,
"category": "Total", "parentfield": "purchase_tax_details",
"cost_center": "Default Cost Center - %s" % abbr, "add_deduct_tax": "Add"
},
{
"doctype": "Purchase Taxes and Charges", "charge_type": "On Net Total",
"account_head": "VAT - Test - %s" % abbr, "rate": 12.5, "tax_amount": 156.25,
"category": "Total", "parentfield": "purchase_tax_details", "add_deduct_tax": "Add"
},
{
"doctype": "Purchase Taxes and Charges", "charge_type": "On Previous Row Total",
"account_head": "Discount - %s" % abbr, "rate": 10, "row_id": 7, "tax_amount": 168.03,
"category": "Total", "parentfield": "purchase_tax_details",
"cost_center": "Default Cost Center - %s" % abbr, "add_deduct_tax": "Deduct"
},
]
class TestPurchaseInvoice(unittest.TestCase):
def setUp(self):
webnotes.conn.begin()
load_data()
# webnotes.conn.set_value("Global Defaults", None, "automatic_inventory_accounting", 1)
self.load_test_data()
# webnotes.conn.set_value("Global Defaults", None,
# "automatic_inventory_accounting", 1)
def tearDown(self):
webnotes.conn.rollback()
def load_test_data(self):
from webnotes.test_runner import make_test_records
webnotes.test_objects = {}
make_test_records("Cost Center", verbose=0)
make_test_records("Item", verbose=0)
make_test_records("Purchase Invoice", verbose=0)
def atest_gl_entries(self):
wrapper = webnotes.model_wrapper(purchase_invoice_doclist).insert()
def test_gl_entries(self):
wrapper = webnotes.model_wrapper(self.get_test_doclist()).insert()
wrapper.submit()
wrapper.load_from_db()
dl = wrapper.doclist
expected_gl_entries = {
"East Wind Inc. - %s" % abbr : [0, 1512.30],
"Cost for Goods Sold - %s" % abbr : [1250, 0],
"Shipping Charges - %s" % abbr : [100, 0],
"Excise Duty - %s" % abbr : [140, 0],
"Education Cess - %s" % abbr : [2.8, 0],
"S&H Education Cess - %s" % abbr : [1.4, 0],
"CST - %s" % abbr : [29.88, 0],
"VAT - Test - %s" % abbr : [156.25, 0],
"Discount - %s" % abbr : [0, 168.03],
"_Test Supplier - _TC": [0, 1512.30],
"_Test Account Cost for Goods Sold - _TC": [1250, 0],
"_Test Account Shipping Charges - _TC": [100, 0],
"_Test Account Excise Duty - _TC": [140, 0],
"_Test Account Education Cess - _TC": [2.8, 0],
"_Test Account S&H Education Cess - _TC": [1.4, 0],
"_Test Account CST - _TC": [29.88, 0],
"_Test Account VAT - _TC": [156.25, 0],
"_Test Account Discount - _TC": [0, 168.03],
}
gl_entries = webnotes.conn.sql("""select account, debit, credit from `tabGL Entry`
where voucher_type = 'Purchase Invoice' and voucher_no = %s""", dl[0].name, as_dict=1)
@ -174,7 +61,39 @@ class TestPurchaseInvoice(unittest.TestCase):
self.assertEqual([d.debit, d.credit], expected_gl_entries.get(d.account))
def test_purchase_invoice_calculation(self):
test_doclist = [
wrapper = webnotes.model_wrapper(self.get_test_doclist()).insert()
wrapper.load_from_db()
self.assertEqual(wrapper.doclist[0].net_total, 1250)
# tax amounts
expected_values = [
["_Test Account Shipping Charges - _TC", 100, 1350],
["_Test Account Customs Duty - _TC", 125, 1350],
["_Test Account Excise Duty - _TC", 140, 1490],
["_Test Account Education Cess - _TC", 2.8, 1492.8],
["_Test Account S&H Education Cess - _TC", 1.4, 1494.2],
["_Test Account CST - _TC", 29.88, 1524.08],
["_Test Account VAT - _TC", 156.25, 1680.33],
["_Test Account Discount - _TC", 168.03, 1512.30],
]
for i, tax in enumerate(wrapper.doclist.get({"parentfield": "purchase_tax_details"})):
self.assertEqual(tax.account_head, expected_values[i][0])
self.assertEqual(tax.tax_amount, expected_values[i][1])
self.assertEqual(tax.total, expected_values[i][2])
# print tax.account_head, tax.tax_amount, tax.item_wise_tax_detail
expected_values = [
["_Test Item Home Desktop 100", 90],
["_Test Item Home Desktop 200", 135]
]
for i, item in enumerate(wrapper.doclist.get({"parentfield": "entries"})):
self.assertEqual(item.item_code, expected_values[i][0])
self.assertEqual(item.item_tax_amount, expected_values[i][1])
def get_test_doclist(self):
return [
# parent
{
"doctype": "Purchase Invoice",
@ -183,7 +102,7 @@ class TestPurchaseInvoice(unittest.TestCase):
"credit_to": "_Test Supplier - _TC",
"bill_no": "NA",
"posting_date": "2013-02-03",
"fiscal_year": "_Test Fiscal Year",
"fiscal_year": "_Test Fiscal Year 2013",
"company": "_Test Company",
"currency": "INR",
"conversion_rate": 1,
@ -199,7 +118,7 @@ class TestPurchaseInvoice(unittest.TestCase):
"import_rate": 50,
"import_amount": 500,
"rate": 50,
"amount": 50,
"amount": 500,
"uom": "_Test UOM",
"item_tax_rate": json.dumps({"_Test Account Excise Duty - _TC": 10}),
"expense_head": "_Test Account Cost for Goods Sold - _TC",
@ -313,37 +232,4 @@ class TestPurchaseInvoice(unittest.TestCase):
"rate": 10,
"row_id": 7
},
]
wrapper = webnotes.model_wrapper(test_doclist).insert()
wrapper.load_from_db()
# tax amounts
expected_values = [
["_Test Account Shipping Charges - _TC", 100, 1350],
["_Test Account Customs Duty - _TC", 125, 1350],
["_Test Account Excise Duty - _TC", 140, 1490],
["_Test Account Education Cess - _TC", 2.8, 1492.8],
["_Test Account S&H Education Cess - _TC", 1.4, 1494.2],
["_Test Account CST - _TC", 29.88, 1524.08],
["_Test Account VAT - _TC", 156.25, 1680.33],
["_Test Account Discount - _TC", 168.03, 1512.30],
]
for i, tax in enumerate(wrapper.doclist.get({"parentfield": "purchase_tax_details"})):
self.assertEqual(tax.account_head, expected_values[i][0])
self.assertEqual(tax.tax_amount, expected_values[i][1])
self.assertEqual(tax.total, expected_values[i][2])
expected_values = [
["_Test Item Home Desktop 100", 90],
["_Test Item Home Desktop 200", 135]
]
for i, item in enumerate(wrapper.doclist.get({"parentfield": "entries"})):
self.assertEqual(item.item_code, expected_values[i][0])
self.assertEqual(item.item_tax_amount, expected_values[i][1])
# self.assertEqual(dl[0].net_total, 1250)
def tearDown(self):
webnotes.conn.rollback()
]

View File

@ -1,8 +1,8 @@
[
{
"creation": "2013-01-28 10:05:58",
"creation": "2013-01-29 20:53:00",
"docstatus": 0,
"modified": "2013-01-29 16:28:04",
"modified": "2013-02-08 14:06:17",
"modified_by": "Administrator",
"owner": "Administrator"
},
@ -268,6 +268,20 @@
"read_only": 1,
"report_hide": 1
},
{
"doctype": "DocField",
"fieldname": "item_tax_amount",
"fieldtype": "Currency",
"hidden": 1,
"label": "Item Tax Amount",
"no_copy": 1,
"options": "Company:company:default_currency",
"print_hide": 1,
"print_width": "150px",
"read_only": 1,
"search_index": 0,
"width": "150px"
},
{
"allow_on_submit": 1,
"doctype": "DocField",

View File

@ -17,10 +17,12 @@
from __future__ import unicode_literals
import webnotes
from webnotes import _, msgprint
from webnotes.utils import flt
from webnotes.utils import flt, cint
import json
from buying.utils import get_item_details
from setup.utils import get_company_currency
from webnotes.model.utils import round_floats_in_doc
from controllers.accounts_controller import AccountsController
@ -32,7 +34,10 @@ class BuyingController(AccountsController):
if self.doc.price_list_name and self.doc.price_list_currency:
self.validate_conversion_rate("price_list_currency", "plc_conversion_rate")
# IMPORTANT: enable this only when client side code is similar to this one
self.calculate_taxes_and_totals()
# set total in words
self.set_total_in_words()
@ -87,11 +92,246 @@ class BuyingController(AccountsController):
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.set_total_in_words()
self.calculate_item_values()
self.initialize_taxes()
self.calculate_net_total()
self.calculate_taxes()
self.calculate_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.item[print_field]) * self.doc.conversion_rate),
self.precision.item[base_field])
for item in self.item_doclist:
round_floats_in_doc(item, self.precision.item)
if item.discount == 100:
if not item.import_ref_rate:
item.import_ref_rate = 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.item.import_rate)
else:
# assume that print rate and discount are specified
item.import_ref_rate = flt(item.import_rate /
(1.0 - (item.discount_rate / 100.0)),
self.precision.item.import_ref_rate)
item.import_amount = flt(item.import_rate * item.qty,
self.precision.item.import_amount)
_set_base(item, "import_ref_rate", "purchase_ref_rate")
_set_base(item, "import_rate", "rate")
_set_base(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)
round_floats_in_doc(tax, self.precision.tax)
def calculate_net_total(self):
self.doc.net_total = 0
self.doc.net_total_import = 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.main.net_total)
self.doc.net_total_import = flt(self.doc.net_total_import,
self.precision.main.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.tax_amount)
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.tax.total)
else:
tax.grand_total_for_current_item = \
flt(self.tax_doclist[i-1].grand_total_for_current_item +
current_tax_amount, self.precision.tax.total)
# 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
def calculate_totals(self):
if self.tax_doclist:
self.doc.grand_total = flt(self.tax_doclist[-1].total,
self.precision.main.grand_total)
self.doc.grand_total_import = flt(
self.doc.grand_total / self.doc.conversion_rate,
self.precision.main.grand_total_import)
else:
self.doc.grand_total = flt(self.doc.net_total,
self.precision.main.grand_total)
self.doc.grand_total_print = flt(
self.doc.grand_total / self.doc.conversion_rate,
self.precision.main.grand_total_import)
self.doc.total_tax = \
flt(self.doc.grand_total - self.doc.net_total,
self.precision.main.total_tax)
if self.meta.get_field("rounded_total"):
self.doc.rounded_total = round(self.doc.grand_total)
if self.meta.get_field("rounded_total_import"):
self.doc.rounded_total_import = round(self.doc.grand_total_import)
def calculate_outstanding_amount(self):
if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus == 0:
self.doc.total_advance = flt(self.doc.total_advance,
self.precision.main.total_advance)
self.doc.outstanding_amount = flt(self.doc.grand_total - self.doc.total_advance,
self.precision.main.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"]
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.tax_amount)
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.tax_amount)
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.tax.rate)
else:
return tax.rate
def set_item_tax_amount(self, item, tax, current_tax_amount):
"""
item_tax_amount is the total tax amount applied on that item
stored for valuation
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.item_tax_amount)
@property
def stock_items(self):
if not hasattr(self, "_stock_items"):
item_codes = list(set(item.item_code for item in self.item_doclist))
self._stock_items = [r[0] for r in webnotes.conn.sql("""select name
from `tabItem` where name in (%s) and is_stock_item='Yes'""" % \
(", ".join((["%s"]*len(item_codes))),), item_codes)]
return self._stock_items
@property
def precision(self):
if not hasattr(self, "_precision"):
self._precision = webnotes._dict()
self._precision.main = self.meta.get_precision_map()
self._precision.item = self.meta.get_precision_map(parentfield = self.fname)
if self.meta.get_field("purchase_tax_details"):
self._precision.tax = self.meta.get_precision_map(parentfield = \
"purchase_tax_details")
return self._precision

View File

@ -1,8 +1,8 @@
[
{
"creation": "2013-01-30 12:49:56",
"creation": "2013-02-07 08:28:23",
"docstatus": 0,
"modified": "2013-02-07 10:50:00",
"modified": "2013-02-08 14:06:15",
"modified_by": "Administrator",
"owner": "Administrator"
},
@ -407,7 +407,7 @@
"print_width": "150px",
"read_only": 1,
"reqd": 0,
"search_index": 1,
"search_index": 0,
"width": "150px"
},
{