mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-27 08:54:45 +00:00
[calculations] [client/server] first cut
This commit is contained in:
@@ -34,67 +34,12 @@ erpnext.buying.PurchaseInvoiceController = erpnext.buying.BuyingController.exten
|
||||
|
||||
cur_frm.cscript.is_opening(doc);
|
||||
},
|
||||
onload_post_render: function(doc, dt, dn) {
|
||||
var me = this;
|
||||
var callback1 = function(doc, dt, dn) {
|
||||
var callback2 = function(doc, dt, dn) {
|
||||
if(doc.__islocal && doc.supplier) cur_frm.cscript.supplier(doc, dt, dn);
|
||||
}
|
||||
me.update_item_details(doc, dt, dn, callback2);
|
||||
}
|
||||
|
||||
// TODO: improve this
|
||||
if(this.frm.doc.__islocal) {
|
||||
if (this.frm.fields_dict.price_list_name && this.frm.doc.price_list_name) {
|
||||
this.price_list_name(callback1);
|
||||
} else {
|
||||
callback1(doc, dt, dn);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// for backward compatibility: combine new and previous states
|
||||
$.extend(cur_frm.cscript, new erpnext.buying.PurchaseInvoiceController({frm: cur_frm}));
|
||||
|
||||
|
||||
cur_frm.cscript.onload = function(doc,dt,dn) {
|
||||
if(!doc.posting_date) set_multiple(dt,dn,{posting_date:get_today()});
|
||||
}
|
||||
|
||||
cur_frm.cscript.supplier = function(doc,dt,dn) {
|
||||
var callback = function(r,rt) {
|
||||
var doc = locals[cur_frm.doctype][cur_frm.docname];
|
||||
get_server_fields('get_credit_to','','',doc, dt, dn, 0, callback2);
|
||||
}
|
||||
|
||||
var callback2 = function(r,rt){
|
||||
var doc = locals[cur_frm.doctype][cur_frm.docname];
|
||||
var el = getchildren('Purchase Invoice Item',doc.name,'entries');
|
||||
for(var i in el){
|
||||
if(el[i].item_code && (!el[i].expense_head || !el[i].cost_center)){
|
||||
args = {
|
||||
item_code: el[i].item_code,
|
||||
expense_head: el[i].expense_head,
|
||||
cost_center: el[i].cost_center
|
||||
};
|
||||
get_server_fields('get_default_values', JSON.stringify(args), 'entries', doc, el[i].doctype, el[i].name, 1);
|
||||
}
|
||||
}
|
||||
cur_frm.cscript.calc_amount(doc, 1);
|
||||
}
|
||||
|
||||
if (doc.supplier) {
|
||||
get_server_fields('get_default_supplier_address',
|
||||
JSON.stringify({ supplier: doc.supplier }),'', doc, dt, dn, 1, function(doc, dt, dn) {
|
||||
cur_frm.refresh();
|
||||
callback(doc, dt, dn);
|
||||
});
|
||||
unhide_field(['supplier_address','contact_person']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
cur_frm.cscript.supplier_address = cur_frm.cscript.contact_person = function(doc,dt,dn) {
|
||||
if(doc.supplier) get_server_fields('get_supplier_address', JSON.stringify({supplier: doc.supplier, address: doc.supplier_address, contact: doc.contact_person}),'', doc, dt, dn, 1);
|
||||
}
|
||||
|
||||
@@ -91,6 +91,11 @@ class DocType(BuyingController):
|
||||
msgprint("%s does not have an Account Head in %s. You must first create it from the Supplier Master" % (self.doc.supplier, self.doc.company))
|
||||
return ret
|
||||
|
||||
def set_supplier_defaults(self):
|
||||
# TODO cleanup these methods
|
||||
self.doc.fields.update(self.get_credit_to())
|
||||
super(DocType, self).set_supplier_defaults()
|
||||
|
||||
def get_cust(self):
|
||||
ret = {}
|
||||
if self.doc.credit_to:
|
||||
@@ -100,31 +105,6 @@ class DocType(BuyingController):
|
||||
|
||||
return ret
|
||||
|
||||
def get_default_values(self, args):
|
||||
if isinstance(args, basestring):
|
||||
import json
|
||||
args = json.loads(args)
|
||||
|
||||
out = webnotes._dict()
|
||||
|
||||
item = webnotes.conn.sql("""select name, purchase_account, cost_center,
|
||||
is_stock_item from `tabItem` where name=%s""", args.get("item_code"), as_dict=1)
|
||||
|
||||
if item and item[0]:
|
||||
item = item[0]
|
||||
|
||||
if cint(webnotes.defaults.get_global_default("auto_inventory_accounting")) and \
|
||||
item.is_stock_item == "Yes":
|
||||
# unset expense head for stock item and auto inventory accounting
|
||||
out.expense_head = out.cost_center = None
|
||||
else:
|
||||
if not args.get("expense_head"):
|
||||
out.expense_head = item.purchase_account
|
||||
if not args.get("cost_center"):
|
||||
out.cost_center = item.cost_center
|
||||
|
||||
return out
|
||||
|
||||
def pull_details(self):
|
||||
if self.doc.purchase_receipt_main:
|
||||
self.validate_duplicate_docname('purchase_receipt')
|
||||
|
||||
@@ -119,25 +119,6 @@ class TestPurchaseInvoice(unittest.TestCase):
|
||||
wrapper.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])
|
||||
|
||||
expected_values = [
|
||||
["_Test Item Home Desktop 100", 90, 59],
|
||||
["_Test Item Home Desktop 200", 135, 177]
|
||||
@@ -147,13 +128,41 @@ class TestPurchaseInvoice(unittest.TestCase):
|
||||
self.assertEqual(item.item_tax_amount, expected_values[i][1])
|
||||
self.assertEqual(item.valuation_rate, expected_values[i][2])
|
||||
|
||||
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])
|
||||
|
||||
def test_purchase_invoice_with_subcontracted_item(self):
|
||||
wrapper = webnotes.bean(copy=test_records[0])
|
||||
wrapper.doclist[1].item_code = "_Test FG Item"
|
||||
wrapper.run_method("calculate_taxes_and_totals")
|
||||
wrapper.insert()
|
||||
wrapper.load_from_db()
|
||||
|
||||
|
||||
expected_values = [
|
||||
["_Test FG Item", 90, 7059],
|
||||
["_Test Item Home Desktop 200", 135, 177]
|
||||
]
|
||||
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(item.valuation_rate, expected_values[i][2])
|
||||
|
||||
self.assertEqual(wrapper.doclist[0].net_total, 1250)
|
||||
|
||||
# tax amounts
|
||||
@@ -172,15 +181,6 @@ class TestPurchaseInvoice(unittest.TestCase):
|
||||
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 FG Item", 90, 7059],
|
||||
["_Test Item Home Desktop 200", 135, 177]
|
||||
]
|
||||
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(item.valuation_rate, expected_values[i][2])
|
||||
|
||||
def test_purchase_invoice_with_advance(self):
|
||||
from accounts.doctype.journal_voucher.test_journal_voucher \
|
||||
|
||||
@@ -110,8 +110,8 @@ class TestSalesInvoice(unittest.TestCase):
|
||||
for i, tax in enumerate(si.doclist.get({"parentfield": "other_charges"})):
|
||||
tax.idx = i+1
|
||||
|
||||
si.doclist[1].export_rate = 62.5
|
||||
si.doclist[1].export_rate = 191
|
||||
si.doclist[1].ref_rate = 62.5
|
||||
si.doclist[1].ref_rate = 191
|
||||
for i in [3, 5, 6, 7, 8, 9]:
|
||||
si.doclist[i].included_in_print_rate = 1
|
||||
|
||||
@@ -173,9 +173,9 @@ class TestSalesInvoice(unittest.TestCase):
|
||||
si = webnotes.bean(copy=test_records[3])
|
||||
si.doc.currency = "USD"
|
||||
si.doc.conversion_rate = 50
|
||||
si.doclist[1].export_rate = 50
|
||||
si.doclist[1].ref_rate = 55.56
|
||||
si.doclist[1].adj_rate = 10
|
||||
si.doclist[2].export_rate = 150
|
||||
si.doclist[2].ref_rate = 187.5
|
||||
si.doclist[2].adj_rate = 20
|
||||
si.doclist[9].rate = 5000
|
||||
|
||||
@@ -317,7 +317,7 @@ class TestSalesInvoice(unittest.TestCase):
|
||||
pos[0]["cash_bank_account"] = "_Test Account Bank Account - _TC"
|
||||
pos[0]["paid_amount"] = 600.0
|
||||
|
||||
si = webnotes.bean(pos)
|
||||
si = webnotes.bean(copy=pos)
|
||||
si.insert()
|
||||
si.submit()
|
||||
|
||||
@@ -340,11 +340,11 @@ class TestSalesInvoice(unittest.TestCase):
|
||||
|
||||
expected_gl_entries = sorted([
|
||||
[si.doc.debit_to, 630.0, 0.0],
|
||||
[test_records[1][1]["income_account"], 0.0, 500.0],
|
||||
[test_records[1][2]["account_head"], 0.0, 80.0],
|
||||
[test_records[1][3]["account_head"], 0.0, 50.0],
|
||||
[pos[1]["income_account"], 0.0, 500.0],
|
||||
[pos[2]["account_head"], 0.0, 80.0],
|
||||
[pos[3]["account_head"], 0.0, 50.0],
|
||||
[stock_in_hand_account, 0.0, 375.0],
|
||||
[test_records[1][1]["expense_account"], 375.0, 0.0],
|
||||
[pos[1]["expense_account"], 375.0, 0.0],
|
||||
[si.doc.debit_to, 0.0, 600.0],
|
||||
["_Test Account Bank Account - _TC", 600.0, 0.0]
|
||||
])
|
||||
@@ -760,6 +760,7 @@ test_records = [
|
||||
"item_code": "_Test Item Home Desktop 100",
|
||||
"item_name": "_Test Item Home Desktop 100",
|
||||
"qty": 10,
|
||||
"ref_rate": 50,
|
||||
"export_rate": 50,
|
||||
"stock_uom": "_Test UOM",
|
||||
"item_tax_rate": json.dumps({"_Test Account Excise Duty - _TC": 10}),
|
||||
@@ -773,6 +774,7 @@ test_records = [
|
||||
"item_code": "_Test Item Home Desktop 200",
|
||||
"item_name": "_Test Item Home Desktop 200",
|
||||
"qty": 5,
|
||||
"ref_rate": 150,
|
||||
"export_rate": 150,
|
||||
"stock_uom": "_Test UOM",
|
||||
"income_account": "Sales - _TC",
|
||||
@@ -883,6 +885,7 @@ test_records = [
|
||||
"item_code": "_Test Item Home Desktop 100",
|
||||
"item_name": "_Test Item Home Desktop 100",
|
||||
"qty": 10,
|
||||
"ref_rate": 62.5,
|
||||
"export_rate": 62.5,
|
||||
"stock_uom": "_Test UOM",
|
||||
"item_tax_rate": json.dumps({"_Test Account Excise Duty - _TC": 10}),
|
||||
@@ -896,6 +899,7 @@ test_records = [
|
||||
"item_code": "_Test Item Home Desktop 200",
|
||||
"item_name": "_Test Item Home Desktop 200",
|
||||
"qty": 5,
|
||||
"ref_rate": 190.66,
|
||||
"export_rate": 190.66,
|
||||
"stock_uom": "_Test UOM",
|
||||
"income_account": "Sales - _TC",
|
||||
|
||||
Reference in New Issue
Block a user