mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-31 10:49:09 +00:00
aii: patches and fine tuning
This commit is contained in:
@@ -33,6 +33,47 @@ class DocType(BuyingController):
|
||||
self.doc, self.doclist = d, dl
|
||||
self.tname = 'Purchase Invoice Item'
|
||||
self.fname = 'entries'
|
||||
|
||||
def validate(self):
|
||||
super(DocType, self).validate()
|
||||
|
||||
self.po_required()
|
||||
self.pr_required()
|
||||
self.check_active_purchase_items()
|
||||
self.check_conversion_rate()
|
||||
self.validate_bill_no_date()
|
||||
self.validate_bill_no()
|
||||
self.validate_reference_value()
|
||||
self.validate_credit_acc()
|
||||
self.clear_unallocated_advances("Purchase Invoice Advance", "advance_allocation_details")
|
||||
self.check_for_acc_head_of_supplier()
|
||||
self.check_for_stopped_status()
|
||||
|
||||
self.po_list, self.pr_list = [], []
|
||||
for d in getlist(self.doclist, 'entries'):
|
||||
self.validate_supplier(d)
|
||||
self.validate_po_pr(d)
|
||||
if not d.purchase_order in self.po_list:
|
||||
self.po_list.append(d.purchase_order)
|
||||
if not d.purhcase_receipt in self.pr_list:
|
||||
self.pr_list.append(d.purchase_receipt)
|
||||
|
||||
|
||||
if not self.doc.is_opening:
|
||||
self.doc.is_opening = 'No'
|
||||
|
||||
self.set_aging_date()
|
||||
|
||||
#set against account for credit to
|
||||
self.set_against_expense_account()
|
||||
|
||||
#FY validation
|
||||
get_obj('Sales Common').validate_fiscal_year(self.doc.fiscal_year,
|
||||
self.doc.posting_date,'Posting Date')
|
||||
|
||||
self.validate_write_off_account()
|
||||
self.update_raw_material_cost()
|
||||
self.update_valuation_rate("entries")
|
||||
|
||||
def get_credit_to(self):
|
||||
acc_head = sql("select name, credit_days from `tabAccount` where (name = %s or (master_name = %s and master_type = 'supplier')) and docstatus != 2", (cstr(self.doc.supplier) + " - " + self.company_abbr,self.doc.supplier))
|
||||
@@ -265,7 +306,7 @@ class DocType(BuyingController):
|
||||
def set_against_expense_account(self):
|
||||
auto_inventory_accounting = \
|
||||
cint(webnotes.defaults.get_global_default("auto_inventory_accounting"))
|
||||
stock_not_billed_account = "Stock Received But Not Billed - %s" % self.company_abbr
|
||||
stock_not_billed_account = self.get_company_default("stock_received_but_not_billed")
|
||||
|
||||
against_accounts = []
|
||||
for item in self.doclist.get({"parentfield": "entries"}):
|
||||
@@ -277,6 +318,10 @@ class DocType(BuyingController):
|
||||
if stock_not_billed_account not in against_accounts:
|
||||
against_accounts.append(stock_not_billed_account)
|
||||
|
||||
elif not item.expense_head:
|
||||
msgprint(_("""Expense account is mandatory for item: """) + item.item_code,
|
||||
raise_exception=1)
|
||||
|
||||
elif item.expense_head not in against_accounts:
|
||||
# if no auto_inventory_accounting or not a stock item
|
||||
against_accounts.append(item.expense_head)
|
||||
@@ -303,47 +348,6 @@ class DocType(BuyingController):
|
||||
if self.doc.write_off_amount and not self.doc.write_off_account:
|
||||
msgprint("Please enter Write Off Account", raise_exception=1)
|
||||
|
||||
def validate(self):
|
||||
super(DocType, self).validate()
|
||||
|
||||
self.po_required()
|
||||
self.pr_required()
|
||||
self.check_active_purchase_items()
|
||||
self.check_conversion_rate()
|
||||
self.validate_bill_no_date()
|
||||
self.validate_bill_no()
|
||||
self.validate_reference_value()
|
||||
self.validate_credit_acc()
|
||||
self.clear_unallocated_advances("Purchase Invoice Advance", "advance_allocation_details")
|
||||
self.check_for_acc_head_of_supplier()
|
||||
self.check_for_stopped_status()
|
||||
|
||||
self.po_list, self.pr_list = [], []
|
||||
for d in getlist(self.doclist, 'entries'):
|
||||
self.validate_supplier(d)
|
||||
self.validate_po_pr(d)
|
||||
if not d.purchase_order in self.po_list:
|
||||
self.po_list.append(d.purchase_order)
|
||||
if not d.purhcase_receipt in self.pr_list:
|
||||
self.pr_list.append(d.purchase_receipt)
|
||||
|
||||
|
||||
if not self.doc.is_opening:
|
||||
self.doc.is_opening = 'No'
|
||||
|
||||
self.set_aging_date()
|
||||
|
||||
#set against account for credit to
|
||||
self.set_against_expense_account()
|
||||
|
||||
#FY validation
|
||||
get_obj('Sales Common').validate_fiscal_year(self.doc.fiscal_year,
|
||||
self.doc.posting_date,'Posting Date')
|
||||
|
||||
self.validate_write_off_account()
|
||||
self.update_raw_material_cost()
|
||||
self.update_valuation_rate("entries")
|
||||
|
||||
def check_prev_docstatus(self):
|
||||
for d in getlist(self.doclist,'entries'):
|
||||
if d.purchase_order:
|
||||
@@ -445,7 +449,7 @@ class DocType(BuyingController):
|
||||
# item gl entries
|
||||
stock_item_and_auto_inventory_accounting = False
|
||||
if auto_inventory_accounting:
|
||||
stock_acocunt = self.get_default_account("stock_received_but_not_billed")
|
||||
stock_account = self.get_company_default("stock_received_but_not_billed")
|
||||
|
||||
for item in self.doclist.get({"parentfield": "entries"}):
|
||||
if auto_inventory_accounting and item.item_code in self.stock_items:
|
||||
@@ -458,7 +462,7 @@ class DocType(BuyingController):
|
||||
|
||||
gl_entries.append(
|
||||
self.get_gl_dict({
|
||||
"account": stock_acocunt,
|
||||
"account": stock_account,
|
||||
"against": self.doc.credit_to,
|
||||
"debit": flt(item.valuation_rate) * flt(item.conversion_factor) \
|
||||
* flt(item.qty),
|
||||
@@ -483,8 +487,8 @@ class DocType(BuyingController):
|
||||
# this will balance out valuation amount included in cost of goods sold
|
||||
gl_entries.append(
|
||||
self.get_gl_dict({
|
||||
"account": self.get_default_account("expenses_included_in_valuation"),
|
||||
"cost_center": "Auto Inventory Accounting - %s" % self.company_abbr,
|
||||
"account": self.get_company_default("expenses_included_in_valuation"),
|
||||
"cost_center": self.get_company_default("stock_adjustment_cost_center"),
|
||||
"against": self.doc.credit_to,
|
||||
"credit": valuation_tax,
|
||||
"remarks": self.doc.remarks or "Accounting Entry for Stock"
|
||||
@@ -525,8 +529,8 @@ class DocType(BuyingController):
|
||||
and is_active = 1 """, (d.item_code,))
|
||||
rm_cost = rm_cost and flt(rm_cost[0][0]) or 0
|
||||
|
||||
d.conversion_factor = d.conversion_factor or webnotes.conn.get_value(
|
||||
d.conversion_factor = d.conversion_factor or flt(webnotes.conn.get_value(
|
||||
"UOM Conversion Detail", {"parent": d.item_code, "uom": d.uom},
|
||||
"conversion_factor") or 1
|
||||
"conversion_factor")) or 1
|
||||
|
||||
d.rm_supp_cost = rm_cost * flt(d.qty) * flt(d.conversion_factor)
|
||||
@@ -109,12 +109,49 @@ class TestPurchaseInvoice(unittest.TestCase):
|
||||
# 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]
|
||||
["_Test Item Home Desktop 100", 90, 59],
|
||||
["_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_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()
|
||||
|
||||
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 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 \
|
||||
|
||||
@@ -249,7 +249,7 @@ class DocType(SellingController):
|
||||
['Delivery Note Item', 'Sales Invoice Item'],
|
||||
['Sales Taxes and Charges','Sales Taxes and Charges'],
|
||||
['Sales Team','Sales Team']]""")
|
||||
self.get_income_account('entries')
|
||||
self.get_income_expense_account('entries')
|
||||
|
||||
elif self.doc.sales_order_main:
|
||||
self.validate_prev_docname('sales order')
|
||||
@@ -259,7 +259,7 @@ class DocType(SellingController):
|
||||
"""[['Sales Order', 'Sales Invoice'],['Sales Order Item', 'Sales Invoice Item'],
|
||||
['Sales Taxes and Charges','Sales Taxes and Charges'],
|
||||
['Sales Team', 'Sales Team']]""")
|
||||
self.get_income_account('entries')
|
||||
self.get_income_expense_account('entries')
|
||||
|
||||
ret = self.get_debit_to()
|
||||
self.doc.debit_to = ret.get('debit_to')
|
||||
@@ -269,16 +269,21 @@ class DocType(SellingController):
|
||||
"""
|
||||
Loads default accounts from items, customer when called from mapper
|
||||
"""
|
||||
self.get_income_account('entries')
|
||||
self.get_income_expense_account('entries')
|
||||
|
||||
|
||||
def get_income_account(self,doctype):
|
||||
def get_income_expense_account(self,doctype):
|
||||
for d in getlist(self.doclist, doctype):
|
||||
if d.item_code:
|
||||
item = webnotes.conn.get_value("Item", d.item_code,
|
||||
["default_income_account", "default_sales_cost_center"], as_dict=True)
|
||||
item = webnotes.conn.get_value("Item", d.item_code, ["default_income_account",
|
||||
"default_sales_cost_center", "purchase_account", "cost_center"], as_dict=True)
|
||||
d.income_account = item['default_income_account'] or ""
|
||||
d.cost_center = item['default_sales_cost_center'] or ""
|
||||
d.cost_center = item['default_sales_cost_center'] or ""
|
||||
|
||||
if cint(webnotes.defaults.get_global_default("auto_inventory_accounting")) \
|
||||
and cint(self.doc.is_pos) and cint(self.doc.update_stock):
|
||||
d.expense_account = item['purchase_account'] or ""
|
||||
d.purchase_cost_center = item['cost_center'] or ""
|
||||
|
||||
|
||||
def get_item_details(self, args=None):
|
||||
@@ -294,8 +299,10 @@ class DocType(SellingController):
|
||||
'item_code':doc.fields.get('item_code'),
|
||||
'income_account':doc.fields.get('income_account'),
|
||||
'cost_center': doc.fields.get('cost_center'),
|
||||
'warehouse': doc.fields.get('warehouse')
|
||||
};
|
||||
'warehouse': doc.fields.get('warehouse'),
|
||||
'expense_account': doc.fields.get('expense_account'),
|
||||
'purchase_cost_center': doc.fields.get('purchase_cost_center')
|
||||
}
|
||||
|
||||
ret = self.get_pos_details(arg)
|
||||
for r in ret:
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
{
|
||||
"creation": "2013-03-07 11:42:55",
|
||||
"docstatus": 0,
|
||||
"modified": "2013-03-21 18:35:47",
|
||||
"modified": "2013-03-22 18:40:48",
|
||||
"modified_by": "Administrator",
|
||||
"owner": "Administrator"
|
||||
},
|
||||
@@ -222,7 +222,7 @@
|
||||
"fieldname": "cost_center",
|
||||
"fieldtype": "Link",
|
||||
"in_filter": 1,
|
||||
"label": "Cost Center",
|
||||
"label": "Sales Cost Center",
|
||||
"oldfieldname": "cost_center",
|
||||
"oldfieldtype": "Link",
|
||||
"options": "Cost Center",
|
||||
@@ -232,6 +232,17 @@
|
||||
"reqd": 0,
|
||||
"width": "120px"
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"fieldname": "purchase_cost_center",
|
||||
"fieldtype": "Link",
|
||||
"hidden": 1,
|
||||
"in_filter": 1,
|
||||
"label": "Purchase Cost Center",
|
||||
"options": "Cost Center",
|
||||
"print_hide": 1,
|
||||
"width": "120px"
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"fieldname": "serial_no",
|
||||
|
||||
@@ -248,3 +248,64 @@ def remove_against_link_from_jv(ref_type, ref_no, against_field):
|
||||
and voucher_no != ifnull(against_voucher, "")
|
||||
and ifnull(is_cancelled, "No")="No" """,
|
||||
(now(), webnotes.session.user, ref_type, ref_no))
|
||||
|
||||
@webnotes.whitelist()
|
||||
def get_company_default(company, fieldname):
|
||||
value = webnotes.conn.get_value("Company", company, fieldname)
|
||||
|
||||
if not value:
|
||||
msgprint(_("Please mention default value for '") +
|
||||
_(webnotes.get_doctype("company").get_label(fieldname) +
|
||||
_("' in Company: ") + company), raise_exception=True)
|
||||
|
||||
return value
|
||||
|
||||
def create_stock_in_hand_jv(reverse=False):
|
||||
from webnotes.utils import nowdate
|
||||
today = nowdate()
|
||||
fiscal_year = get_fiscal_year(today)[0]
|
||||
|
||||
for company in webnotes.conn.sql_list("select name from `tabCompany`"):
|
||||
stock_rbnb_value = get_stock_rbnb_value(company)
|
||||
|
||||
jv = webnotes.bean([
|
||||
{
|
||||
"doctype": "Journal Voucher",
|
||||
"naming_series": "_PATCH-",
|
||||
"company": company,
|
||||
"posting_date": today,
|
||||
"fiscal_year": fiscal_year,
|
||||
"voucher_type": "Journal Entry"
|
||||
},
|
||||
{
|
||||
"doctype": "Journal Voucher Detail",
|
||||
"parentfield": "entries",
|
||||
"account": get_company_default(company, "stock_received_but_not_billed"),
|
||||
(reverse and "debit" or "credit"): stock_rbnb_value
|
||||
},
|
||||
{
|
||||
"doctype": "Journal Voucher Detail",
|
||||
"parentfield": "entries",
|
||||
"account": get_company_default(company, "stock_adjustment_account"),
|
||||
(reverse and "credit" or "debit"): stock_rbnb_value
|
||||
},
|
||||
])
|
||||
jv.insert()
|
||||
jv.submit()
|
||||
|
||||
def get_stock_rbnb_value(company):
|
||||
total_received_amount = webnotes.conn.sql("""select sum(valuation_amount)
|
||||
from `tabPurchase Receipt Item` pr_item where docstatus=1
|
||||
and exists(select name from `tabItem` where name = pr_item.item_code
|
||||
and is_stock_item='Yes')
|
||||
and exist(select name from `tabPurchase Receipt`
|
||||
where name = pr_item.parent and company = %s)""", company)
|
||||
|
||||
total_billed_amount = webnotes.conn.sql("""select sum(valuation_amount)
|
||||
from `tabPurchase Invoice Item` pi_item where docstatus=1
|
||||
and exists(select name from `tabItem` where name = pi_item.item_code
|
||||
and is_stock_item='Yes')
|
||||
and exist(select name from `tabPurchase Invoice`
|
||||
where name = pi_item.parent and company = %s)""", company)
|
||||
|
||||
return flt(total_received_amount[0][0]) - flt(total_billed_amount[0][0])
|
||||
|
||||
Reference in New Issue
Block a user