From 80abad2ee0c29eacb2796761da76e0943e549909 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 19 Mar 2013 18:18:52 +0530 Subject: [PATCH] aii: default account from company --- .../purchase_invoice/purchase_invoice.py | 7 ++- .../doctype/sales_invoice/sales_invoice.py | 7 +-- controllers/accounts_controller.py | 25 +++++---- controllers/stock_controller.py | 2 +- setup/doctype/company/company.py | 5 ++ setup/doctype/company/company.txt | 9 +++- .../global_defaults/global_defaults.txt | 6 +-- stock/doctype/delivery_note/delivery_note.py | 16 +++--- .../purchase_receipt/purchase_receipt.py | 2 +- stock/doctype/stock_entry/stock_entry.js | 50 +++++++++++++++--- .../stock_reconciliation.js | 51 ++++++++++++++----- 11 files changed, 130 insertions(+), 50 deletions(-) diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py index 7722c98a07..b56e2ace10 100644 --- a/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -444,6 +444,9 @@ 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") + for item in self.doclist.get({"parentfield": "entries"}): if auto_inventory_accounting and item.item_code in self.stock_items: if flt(item.valuation_rate): @@ -455,7 +458,7 @@ class DocType(BuyingController): gl_entries.append( self.get_gl_dict({ - "account": "Stock Received But Not Billed - %s" % (self.company_abbr,), + "account": stock_acocunt, "against": self.doc.credit_to, "debit": flt(item.valuation_rate) * flt(item.conversion_factor) \ * flt(item.qty), @@ -480,7 +483,7 @@ class DocType(BuyingController): # this will balance out valuation amount included in cost of goods sold gl_entries.append( self.get_gl_dict({ - "account": "Expenses Included In Valuation - %s" % (self.company_abbr,), + "account": self.get_default_account("expenses_included_in_valuation"), "cost_center": "Auto Inventory Accounting - %s" % self.company_abbr, "against": self.doc.credit_to, "credit": valuation_tax, diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py index b6b1f0ba10..f81a71b1a4 100644 --- a/accounts/doctype/sales_invoice/sales_invoice.py +++ b/accounts/doctype/sales_invoice/sales_invoice.py @@ -704,9 +704,9 @@ class DocType(SellingController): if auto_inventory_accounting: if cint(self.doc.is_pos) and cint(self.doc.update_stock): - stock_account = self.get_stock_in_hand_account() + stock_account = self.get_default_account("stock_in_hand_account") else: - stock_account = "Stock Delivered But Not Billed - %s" % (self.company_abbr,) + stock_account = self.get_default_account("stock_delivered_but_not_billed") for item in self.doclist.get({"parentfield": "entries"}): # income account gl entries @@ -794,7 +794,8 @@ class DocType(SellingController): stock_ledger_entries = item_sales_bom = None for item in self.doclist.get({"parentfield": "entries"}): - if item.item_code in self.stock_items: + if item.item_code in self.stock_items or \ + (item_sales_bom and item_sales_bom.get(item.item_code)): item.buying_amount = self.get_item_buying_amount(item, stock_ledger_entries, item_sales_bom) webnotes.conn.set_value("Sales Invoice Item", item.name, diff --git a/controllers/accounts_controller.py b/controllers/accounts_controller.py index 66a5d9e09e..df78212cd1 100644 --- a/controllers/accounts_controller.py +++ b/controllers/accounts_controller.py @@ -71,15 +71,15 @@ class AccountsController(TransactionBase): "advance_amount": flt(d.amount), "allocate_amount": 0 }) - - def get_stock_in_hand_account(self): - stock_in_hand_account = webnotes.conn.get_value("Company", self.doc.company, "stock_in_hand_account") - if not stock_in_hand_account: - msgprint(_("Missing") + ": " - + _(webnotes.get_doctype("company").get_label("stock_in_hand_account") - + " " + _("for Company") + " " + self.doc.company), raise_exception=True) - return stock_in_hand_account + def get_default_account(self, account_for): + account = webnotes.conn.get_value("Company", self.doc.company, account_for) + if not account: + msgprint(_("Please mention default account for '") + + _(webnotes.get_doctype("company").get_label(account_for) + + _("' in Company: ") + self.doc.company), raise_exception=True) + + return account @property def stock_items(self): @@ -88,7 +88,7 @@ class AccountsController(TransactionBase): 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 @@ -96,4 +96,9 @@ class AccountsController(TransactionBase): if not hasattr(self, "_abbr"): self._abbr = webnotes.conn.get_value("Company", self.doc.company, "abbr") - return self._abbr \ No newline at end of file + return self._abbr + + +@webnotes.whitelist() +def get_default_account(account_for, company): + return webnotes.conn.get_value("Company", company, account_for) \ No newline at end of file diff --git a/controllers/stock_controller.py b/controllers/stock_controller.py index 3a900aa8be..eec7352f1a 100644 --- a/controllers/stock_controller.py +++ b/controllers/stock_controller.py @@ -20,7 +20,7 @@ from controllers.accounts_controller import AccountsController class StockController(AccountsController): def make_gl_entries(self, against_stock_account, amount, cost_center=None): - stock_in_hand_account = self.get_stock_in_hand_account() + stock_in_hand_account = self.get_default_account("stock_in_hand_account") if amount: gl_entries = [ diff --git a/setup/doctype/company/company.py b/setup/doctype/company/company.py index 6ba7985d6d..15241a2ba6 100644 --- a/setup/doctype/company/company.py +++ b/setup/doctype/company/company.py @@ -214,6 +214,11 @@ class DocType: "Stock Adjustment - " + self.doc.abbr): webnotes.conn.set(self.doc, "stock_adjustment_account", "Stock Adjustment - " + self.doc.abbr) + + if not self.doc.expenses_included_in_valuation and webnotes.conn.exists("Account", + "Expenses Included In Valuation - " + self.doc.abbr): + webnotes.conn.set(self.doc, "expenses_included_in_valuation", + "Expenses Included In Valuation - " + self.doc.abbr) # Create default cost center # --------------------------------------------------- diff --git a/setup/doctype/company/company.txt b/setup/doctype/company/company.txt index d8c649ff28..4d2dcdae68 100644 --- a/setup/doctype/company/company.txt +++ b/setup/doctype/company/company.txt @@ -2,7 +2,7 @@ { "creation": "2013-02-27 09:38:05", "docstatus": 0, - "modified": "2013-03-18 16:34:04", + "modified": "2013-03-19 12:52:00", "modified_by": "Administrator", "owner": "Administrator" }, @@ -194,6 +194,13 @@ "label": "Stock Adjustment Account", "options": "Account" }, + { + "doctype": "DocField", + "fieldname": "expenses_included_in_valuation", + "fieldtype": "Link", + "label": "Expenses Included In Valuation", + "options": "Account" + }, { "doctype": "DocField", "fieldname": "col_break23", diff --git a/setup/doctype/global_defaults/global_defaults.txt b/setup/doctype/global_defaults/global_defaults.txt index 960da7e231..d75f1efe88 100644 --- a/setup/doctype/global_defaults/global_defaults.txt +++ b/setup/doctype/global_defaults/global_defaults.txt @@ -1,8 +1,8 @@ [ { - "creation": "2013-02-19 12:28:27", + "creation": "2013-02-21 14:54:43", "docstatus": 0, - "modified": "2013-02-20 14:09:00", + "modified": "2013-03-19 14:46:49", "modified_by": "Administrator", "owner": "Administrator" }, @@ -403,7 +403,7 @@ "fieldname": "emp_created_by", "fieldtype": "Select", "label": "Employee Records to be created by ", - "options": "\nNaming Series\nEmployee Number" + "options": "Naming Series\nEmployee Number" }, { "doctype": "DocField", diff --git a/stock/doctype/delivery_note/delivery_note.py b/stock/doctype/delivery_note/delivery_note.py index aef79390c2..62d5c5f8b2 100644 --- a/stock/doctype/delivery_note/delivery_note.py +++ b/stock/doctype/delivery_note/delivery_note.py @@ -400,12 +400,14 @@ class DocType(SellingController): if stock_ledger_entries: for item in self.doclist.get({"parentfield": "delivery_note_details"}): - buying_amount = get_buying_amount(item.item_code, item.warehouse, -1*item.qty, - self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, - item_sales_bom) - item.buying_amount = buying_amount > 0 and buying_amount or 0 - webnotes.conn.set_value("Delivery Note Item", item.name, "buying_amount", - item.buying_amount) + if item.item_code in self.stock_items or \ + (item_sales_bom and item_sales_bom.get(item.item_code)): + buying_amount = get_buying_amount(item.item_code, item.warehouse, -1*item.qty, + self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, + item_sales_bom) + item.buying_amount = buying_amount > 0 and buying_amount or 0 + webnotes.conn.set_value("Delivery Note Item", item.name, "buying_amount", + item.buying_amount) self.validate_warehouse() @@ -420,7 +422,7 @@ class DocType(SellingController): if not cint(webnotes.defaults.get_global_default("auto_inventory_accounting")): return - against_stock_account = "Stock Delivered But Not Billed - %s" % (self.company_abbr,) + against_stock_account = self.get_default_account("stock_delivered_but_not_billed") total_buying_amount = self.get_total_buying_amount() super(DocType, self).make_gl_entries(against_stock_account, -1*total_buying_amount) diff --git a/stock/doctype/purchase_receipt/purchase_receipt.py b/stock/doctype/purchase_receipt/purchase_receipt.py index e26c0a6d1e..e7d030d719 100644 --- a/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/stock/doctype/purchase_receipt/purchase_receipt.py @@ -318,7 +318,7 @@ class DocType(BuyingController): if not cint(webnotes.defaults.get_global_default("auto_inventory_accounting")): return - against_stock_account = "Stock Received But Not Billed - %s" % (self.company_abbr,) + against_stock_account = self.get_default_account("stock_received_but_not_billed") total_valuation_amount = self.get_total_valuation_amount() super(DocType, self).make_gl_entries(against_stock_account, total_valuation_amount) diff --git a/stock/doctype/stock_entry/stock_entry.js b/stock/doctype/stock_entry/stock_entry.js index 0a75914a62..2eb538c53d 100644 --- a/stock/doctype/stock_entry/stock_entry.js +++ b/stock/doctype/stock_entry/stock_entry.js @@ -18,6 +18,47 @@ wn.require("public/app/js/controllers/stock_controller.js"); wn.provide("erpnext.stock"); erpnext.stock.StockEntry = erpnext.stock.StockController.extend({ + onload: function() { + this.set_default_account(); + }, + + set_default_account: function() { + var me = this; + + if (sys_defaults.auto_inventory_accounting && !this.frm.doc.expense_adjustment_account) { + if (this.frm.doc.purpose == "Sales Return") + account_for = "stock_delivered_but_not_billed"; + else if (this.frm.doc.purpose == "Purchase Return") + account_for = "stock_received_but_not_billed"; + else account_for = "stock_adjustment_account"; + + this.frm.call({ + method: "controllers.accounts_controller.get_default_account", + args: { + "account_for": account_for, + "company": this.frm.doc.company + }, + callback: function(r) { + if (!r.exc) me.frm.set_value("expense_adjustment_account", r.message); + } + }); + } + }, + + setup: function() { + var me = this; + if (sys_defaults.auto_inventory_accounting) { + this.frm.add_fetch("company", "expense_adjustment_account", "stock_adjustment_account"); + + this.frm.fields_dict["expense_adjustment_account"].get_query = function() { + return { + "query": "accounts.utils.get_account_list", + "filters": { "company": me.frm.doc.company } + } + } + } + }, + onload_post_render: function() { if(this.frm.doc.__islocal && (this.frm.doc.production_order || this.frm.doc.bom_no) && !getchildren('Stock Entry Detail', this.frm.doc.name, 'mtn_details').length) { @@ -234,11 +275,4 @@ cur_frm.cscript.validate_items = function(doc) { cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query; -cur_frm.fields_dict.supplier.get_query = erpnext.utils.supplier_query; - -cur_frm.fields_dict["expense_adjustment_account"].get_query = function(doc) { - return { - "query": "accounts.utils.get_account_list", - "filters": { "company": doc.company } - } -} \ No newline at end of file +cur_frm.fields_dict.supplier.get_query = erpnext.utils.supplier_query; \ No newline at end of file diff --git a/stock/doctype/stock_reconciliation/stock_reconciliation.js b/stock/doctype/stock_reconciliation/stock_reconciliation.js index fb4053ca9e..372166eaac 100644 --- a/stock/doctype/stock_reconciliation/stock_reconciliation.js +++ b/stock/doctype/stock_reconciliation/stock_reconciliation.js @@ -18,10 +18,44 @@ wn.require("public/app/js/controllers/stock_controller.js"); wn.provide("erpnext.stock"); erpnext.stock.StockReconciliation = erpnext.stock.StockController.extend({ - setup: function() { - this.frm.add_fetch("company", "stock_adjustment_account", "expense_account"); + onload: function() { + this.set_default_expense_account(); }, + set_default_expense_account: function() { + var me = this; + + if (sys_defaults.auto_inventory_accounting && !this.frm.doc.expense_account) { + this.frm.call({ + method: "controllers.accounts_controller.get_default_account", + args: { + "account_for": "stock_adjustment_account", + "company": this.frm.doc.company + }, + callback: function(r) { + if (!r.exc) me.frm.set_value("expense_account", r.message); + } + }); + } + }, + + setup: function() { + var me = this; + + this.frm.add_fetch("company", "expense_account", "stock_adjustment_account"); + + this.frm.fields_dict["expense_account"].get_query = function() { + return { + "query": "accounts.utils.get_account_list", + "filters": { + "is_pl_account": "Yes", + "debit_or_credit": "Debit", + "company": me.frm.doc.company + } + } + } + }, + refresh: function() { if(this.frm.doc.docstatus===0) { this.show_download_template(); @@ -126,15 +160,4 @@ erpnext.stock.StockReconciliation = erpnext.stock.StockController.extend({ }, }); -cur_frm.cscript = new erpnext.stock.StockReconciliation({frm: cur_frm}); - -cur_frm.fields_dict["expense_account"].get_query = function(doc) { - return { - "query": "accounts.utils.get_account_list", - "filters": { - "is_pl_account": "Yes", - "debit_or_credit": "Debit", - "company": doc.company - } - } -} \ No newline at end of file +cur_frm.cscript = new erpnext.stock.StockReconciliation({frm: cur_frm}); \ No newline at end of file