[fixes] stock reco

This commit is contained in:
Rushabh Mehta 2015-02-20 15:11:56 +05:30
parent d51a47155b
commit dc93e0adc6
11 changed files with 173 additions and 89 deletions

View File

@ -120,3 +120,4 @@ erpnext.patches.v5_0.manufacturing_activity_type
erpnext.patches.v5_0.update_item_description_and_image erpnext.patches.v5_0.update_item_description_and_image
erpnext.patches.v5_0.update_material_transferred_for_qty erpnext.patches.v5_0.update_material_transferred_for_qty
erpnext.patches.v5_0.stock_entry_update_value erpnext.patches.v5_0.stock_entry_update_value
erpnext.patches.v5_0.convert_stock_reconciliation

View File

@ -0,0 +1,21 @@
import frappe, json
def execute():
# stock reco now amendable
frappe.db.sql("""update tabDocPerm set `amend` = 1 where parent='Stock Reconciliation' and submit = 1""")
if frappe.db.has_column("Stock Reconciliation", "reconciliation_json"):
for sr in frappe.db.get_all("Stock Reconciliation", ["name"],
{"reconciliation_json": ["!=", ""]}):
sr = frappe.get_doc("Stock Reconciliation", sr.name)
for item in json.loads(sr.reconciliation_json):
sr.append("items", {
"item_code": item.item_code,
"warehouse": item.warehouse,
"valuation_rate": item.valuation_rate,
"qty": item.qty
})
for item in sr.items:
item.db_update()

View File

@ -1,6 +1,20 @@
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt // License: GNU General Public License v3. See license.txt
frappe.ui.form.on("Customer", "refresh", function(frm) {
cur_frm.cscript.setup_dashboard(frm.doc);
if(frappe.defaults.get_default("cust_master_name")!="Naming Series") {
frm.toggle_display("naming_series", false);
} else {
erpnext.toggle_naming_series();
}
frm.toggle_display(['address_html','contact_html'], !frm.doc.__islocal);
if(!frm.doc.__islocal) erpnext.utils.render_address_and_contact(frm);
})
cur_frm.cscript.onload = function(doc, dt, dn) { cur_frm.cscript.onload = function(doc, dt, dn) {
cur_frm.cscript.load_defaults(doc, dt, dn); cur_frm.cscript.load_defaults(doc, dt, dn);
} }
@ -16,24 +30,6 @@ cur_frm.cscript.load_defaults = function(doc, dt, dn) {
cur_frm.add_fetch('lead_name', 'company_name', 'customer_name'); cur_frm.add_fetch('lead_name', 'company_name', 'customer_name');
cur_frm.add_fetch('default_sales_partner','commission_rate','default_commission_rate'); cur_frm.add_fetch('default_sales_partner','commission_rate','default_commission_rate');
cur_frm.cscript.refresh = function(doc, dt, dn) {
cur_frm.cscript.setup_dashboard(doc);
if(frappe.defaults.get_default("cust_master_name")!="Naming Series") {
cur_frm.toggle_display("naming_series", false);
} else {
erpnext.toggle_naming_series();
}
if(doc.__islocal){
hide_field(['address_html','contact_html']);
}else{
unhide_field(['address_html','contact_html']);
// make lists
erpnext.utils.render_address_and_contact(cur_frm);
}
}
cur_frm.cscript.validate = function(doc, dt, dn) { cur_frm.cscript.validate = function(doc, dt, dn) {
if(doc.lead_name) frappe.model.clear_doc("Lead", doc.lead_name); if(doc.lead_name) frappe.model.clear_doc("Lead", doc.lead_name);
} }

View File

@ -11,7 +11,7 @@
{ {
"fieldname": "basic_info", "fieldname": "basic_info",
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "Basic Info", "label": "",
"oldfieldtype": "Section Break", "oldfieldtype": "Section Break",
"options": "icon-user", "options": "icon-user",
"permlevel": 0, "permlevel": 0,
@ -105,7 +105,7 @@
"depends_on": "eval:!doc.__islocal", "depends_on": "eval:!doc.__islocal",
"fieldname": "address_contacts", "fieldname": "address_contacts",
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "Address & Contacts", "label": "",
"options": "icon-map-marker", "options": "icon-map-marker",
"permlevel": 0 "permlevel": 0
}, },
@ -137,6 +137,7 @@
"permlevel": 0 "permlevel": 0
}, },
{ {
"description": "",
"fieldname": "accounts", "fieldname": "accounts",
"fieldtype": "Table", "fieldtype": "Table",
"label": "Accounts", "label": "Accounts",
@ -146,7 +147,7 @@
{ {
"fieldname": "more_info", "fieldname": "more_info",
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "More Info", "label": "",
"oldfieldtype": "Section Break", "oldfieldtype": "Section Break",
"options": "icon-file-text", "options": "icon-file-text",
"permlevel": 0 "permlevel": 0
@ -223,7 +224,7 @@
{ {
"fieldname": "sales_team_section_break", "fieldname": "sales_team_section_break",
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "Sales Team", "label": "",
"oldfieldtype": "Section Break", "oldfieldtype": "Section Break",
"options": "icon-group", "options": "icon-group",
"permlevel": 0 "permlevel": 0
@ -267,7 +268,7 @@
], ],
"icon": "icon-user", "icon": "icon-user",
"idx": 1, "idx": 1,
"modified": "2015-02-05 05:11:36.603762", "modified": "2015-02-20 01:39:41.062529",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Selling", "module": "Selling",
"name": "Customer", "name": "Customer",

View File

@ -160,7 +160,7 @@ class Item(WebsiteGenerator):
# delete missing variants # delete missing variants
existing_variants = [d.name for d in frappe.get_all("Item", existing_variants = [d.name for d in frappe.get_all("Item",
{"variant_of":self.name})] filters={"variant_of":self.name})]
updated, deleted = [], [] updated, deleted = [], []
for existing_variant in existing_variants: for existing_variant in existing_variants:
@ -385,7 +385,7 @@ class Item(WebsiteGenerator):
super(Item, self).on_trash() super(Item, self).on_trash()
frappe.db.sql("""delete from tabBin where item_code=%s""", self.item_code) frappe.db.sql("""delete from tabBin where item_code=%s""", self.item_code)
frappe.db.sql("delete from `tabItem Price` where item_code=%s", self.name) frappe.db.sql("delete from `tabItem Price` where item_code=%s", self.name)
for variant_of in frappe.get_all("Item", {"variant_of": self.name}): for variant_of in frappe.get_all("Item", filters={"variant_of": self.name}):
frappe.delete_doc("Item", variant_of.name) frappe.delete_doc("Item", variant_of.name)
def before_rename(self, olddn, newdn, merge=False): def before_rename(self, olddn, newdn, merge=False):

View File

@ -1,6 +1,6 @@
{ {
"allow_import": 1, "allow_import": 1,
"autoname": "RFD/.#####", "autoname": "ITEM-PRICE-.#####",
"creation": "2013-05-02 16:29:48", "creation": "2013-05-02 16:29:48",
"description": "Multiple Item prices.", "description": "Multiple Item prices.",
"docstatus": 0, "docstatus": 0,
@ -105,7 +105,7 @@
"idx": 1, "idx": 1,
"in_create": 0, "in_create": 0,
"istable": 0, "istable": 0,
"modified": "2015-02-05 05:11:39.951804", "modified": "2015-02-20 00:26:20.161437",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Stock", "module": "Stock",
"name": "Item Price", "name": "Item Price",

View File

@ -10,7 +10,11 @@ frappe.ui.form.on("Stock Reconciliation", "get_items", function(frm) {
function(data) { function(data) {
frappe.call({ frappe.call({
method:"erpnext.stock.doctype.stock_reconciliation.stock_reconciliation.get_items", method:"erpnext.stock.doctype.stock_reconciliation.stock_reconciliation.get_items",
args: {warehouse: data.warehouse}, args: {
warehouse: data.warehouse,
posting_date: frm.doc.posting_date,
posting_time: frm.doc.posting_time
},
callback: function(r) { callback: function(r) {
var items = []; var items = [];
frm.clear_table("items"); frm.clear_table("items");

View File

@ -32,11 +32,8 @@
"reqd": 1 "reqd": 1
}, },
{ {
"depends_on": "eval:cint(sys_defaults.auto_accounting_for_stock)", "fieldname": "col1",
"fieldname": "expense_account", "fieldtype": "Column Break",
"fieldtype": "Link",
"label": "Difference Account",
"options": "Account",
"permlevel": 0 "permlevel": 0
}, },
{ {
@ -50,11 +47,6 @@
"print_hide": 1, "print_hide": 1,
"read_only": 1 "read_only": 1
}, },
{
"fieldname": "col1",
"fieldtype": "Column Break",
"permlevel": 0
},
{ {
"fieldname": "company", "fieldname": "company",
"fieldtype": "Link", "fieldtype": "Link",
@ -63,23 +55,6 @@
"permlevel": 0, "permlevel": 0,
"reqd": 1 "reqd": 1
}, },
{
"fieldname": "fiscal_year",
"fieldtype": "Link",
"label": "Fiscal Year",
"options": "Fiscal Year",
"permlevel": 0,
"print_hide": 1,
"reqd": 1
},
{
"depends_on": "eval:cint(sys_defaults.auto_accounting_for_stock)",
"fieldname": "cost_center",
"fieldtype": "Link",
"label": "Cost Center",
"options": "Cost Center",
"permlevel": 0
},
{ {
"fieldname": "sb9", "fieldname": "sb9",
"fieldtype": "Section Break", "fieldtype": "Section Break",
@ -108,28 +83,20 @@
"precision": "" "precision": ""
}, },
{ {
"fieldname": "upload_html", "depends_on": "eval:cint(sys_defaults.auto_accounting_for_stock)",
"fieldtype": "HTML", "fieldname": "expense_account",
"label": "Upload HTML", "fieldtype": "Link",
"permlevel": 0, "label": "Difference Account",
"print_hide": 1, "options": "Account",
"read_only": 1
},
{
"depends_on": "reconciliation_json",
"fieldname": "sb2",
"fieldtype": "Section Break",
"label": "Reconciliation Data",
"permlevel": 0 "permlevel": 0
}, },
{ {
"fieldname": "reconciliation_html", "depends_on": "eval:cint(sys_defaults.auto_accounting_for_stock)",
"fieldtype": "HTML", "fieldname": "cost_center",
"hidden": 0, "fieldtype": "Link",
"label": "Reconciliation HTML", "label": "Cost Center",
"permlevel": 0, "options": "Cost Center",
"print_hide": 0, "permlevel": 0
"read_only": 1
}, },
{ {
"fieldname": "reconciliation_json", "fieldname": "reconciliation_json",
@ -140,20 +107,55 @@
"permlevel": 0, "permlevel": 0,
"print_hide": 1, "print_hide": 1,
"read_only": 1 "read_only": 1
},
{
"fieldname": "column_break_13",
"fieldtype": "Column Break",
"permlevel": 0,
"precision": ""
},
{
"fieldname": "difference_amount",
"fieldtype": "Currency",
"label": "Difference Amount",
"permlevel": 0,
"precision": "",
"read_only": 1
},
{
"fieldname": "fold_15",
"fieldtype": "Fold",
"permlevel": 0,
"precision": ""
},
{
"fieldname": "section_break_16",
"fieldtype": "Section Break",
"permlevel": 0,
"precision": ""
},
{
"fieldname": "fiscal_year",
"fieldtype": "Link",
"label": "Fiscal Year",
"options": "Fiscal Year",
"permlevel": 0,
"print_hide": 1,
"reqd": 1
} }
], ],
"icon": "icon-upload-alt", "icon": "icon-upload-alt",
"idx": 1, "idx": 1,
"is_submittable": 1, "is_submittable": 1,
"max_attachments": 1, "max_attachments": 1,
"modified": "2015-02-17 02:09:17.483016", "modified": "2015-02-20 04:39:46.585018",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Stock", "module": "Stock",
"name": "Stock Reconciliation", "name": "Stock Reconciliation",
"owner": "Administrator", "owner": "Administrator",
"permissions": [ "permissions": [
{ {
"amend": 0, "amend": 1,
"cancel": 1, "cancel": 1,
"create": 1, "create": 1,
"delete": 1, "delete": 1,

View File

@ -8,6 +8,7 @@ from frappe import msgprint, _
from frappe.utils import cstr, flt, cint from frappe.utils import cstr, flt, cint
from erpnext.stock.stock_ledger import update_entries_after from erpnext.stock.stock_ledger import update_entries_after
from erpnext.controllers.stock_controller import StockController from erpnext.controllers.stock_controller import StockController
from erpnext.stock.utils import get_stock_balance
class StockReconciliation(StockController): class StockReconciliation(StockController):
def __init__(self, arg1, arg2=None): def __init__(self, arg1, arg2=None):
@ -15,6 +16,7 @@ class StockReconciliation(StockController):
self.head_row = ["Item Code", "Warehouse", "Quantity", "Valuation Rate"] self.head_row = ["Item Code", "Warehouse", "Quantity", "Valuation Rate"]
def validate(self): def validate(self):
self.remove_items_with_no_change()
self.validate_data() self.validate_data()
self.validate_expense_account() self.validate_expense_account()
@ -26,6 +28,28 @@ class StockReconciliation(StockController):
self.delete_and_repost_sle() self.delete_and_repost_sle()
self.make_gl_entries_on_cancel() self.make_gl_entries_on_cancel()
def remove_items_with_no_change(self):
"""Remove items if qty or rate is not changed"""
self.difference_amount = 0.0
def _changed(item):
qty, rate = get_stock_balance(item.item_code, item.warehouse,
self.posting_date, self.posting_time, with_valuation_rate=True)
if (item.qty==None or item.qty==qty) and (item.valuation_rate==None or item.valuation_rate==rate):
return False
else:
item.current_qty = qty
item.current_valuation_rate = rate
self.difference_amount += ((item.qty or qty) * (item.valuation_rate or rate) - (qty * rate))
return True
items = filter(lambda d: _changed(d), self.items)
if len(items) != len(self.items):
self.items = items
for i, item in enumerate(self.items):
item.idx = i + 1
frappe.msgprint(_("Removed items with no change in quantity or value."))
def validate_data(self): def validate_data(self):
def _get_msg(row_num, msg): def _get_msg(row_num, msg):
return _("Row # {0}: ").format(row_num+1) + msg return _("Row # {0}: ").format(row_num+1) + msg
@ -38,6 +62,7 @@ class StockReconciliation(StockController):
# validate no of rows # validate no of rows
if len(self.items) > 100: if len(self.items) > 100:
frappe.throw(_("""Max 100 rows for Stock Reconciliation.""")) frappe.throw(_("""Max 100 rows for Stock Reconciliation."""))
for row_num, row in enumerate(self.items): for row_num, row in enumerate(self.items):
# find duplicates # find duplicates
if [row.item_code, row.warehouse] in item_warehouse_combinations: if [row.item_code, row.warehouse] in item_warehouse_combinations:
@ -88,8 +113,6 @@ class StockReconciliation(StockController):
try: try:
item = frappe.get_doc("Item", item_code) item = frappe.get_doc("Item", item_code)
if not item:
raise frappe.ValidationError, (_("Item: {0} not found in the system").format(item_code))
# end of life and stock item # end of life and stock item
validate_end_of_life(item_code, item.end_of_life, verbose=0) validate_end_of_life(item_code, item.end_of_life, verbose=0)
@ -194,14 +217,16 @@ class StockReconciliation(StockController):
frappe.throw(_("Difference Account must be a 'Liability' type account, since this Stock Reconciliation is an Opening Entry")) frappe.throw(_("Difference Account must be a 'Liability' type account, since this Stock Reconciliation is an Opening Entry"))
@frappe.whitelist() @frappe.whitelist()
def get_items(warehouse): def get_items(warehouse, posting_date, posting_time):
from erpnext.stock.utils import get_stock_balance
items = frappe.get_list("Item", fields=["name"], filters= items = frappe.get_list("Item", fields=["name"], filters=
{"is_stock_item": "Yes", "has_serial_no": "No", "has_batch_no": "No"}) {"is_stock_item": "Yes", "has_serial_no": "No", "has_batch_no": "No"})
for item in items: for item in items:
item.item_code = item.name item.item_code = item.name
item.warehouse = warehouse item.warehouse = warehouse
item.qty, item.valuation_rate = get_stock_balance(item.name, warehouse,
posting_date, posting_time, with_valuation_rate=True)
item.current_qty = item.qty
item.current_valuation_rate = item.valuation_rate
del item["name"] del item["name"]
item.qty, item.valuation_rate = get_stock_balance(item.name, warehouse, with_valuation_rate=True)
return items return items

View File

@ -48,6 +48,12 @@
"search_index": 0, "search_index": 0,
"set_only_once": 0 "set_only_once": 0
}, },
{
"fieldname": "section_break_3",
"fieldtype": "Section Break",
"permlevel": 0,
"precision": ""
},
{ {
"allow_on_submit": 0, "allow_on_submit": 0,
"description": "Leave blank if no change", "description": "Leave blank if no change",
@ -87,6 +93,30 @@
"reqd": 0, "reqd": 0,
"search_index": 0, "search_index": 0,
"set_only_once": 0 "set_only_once": 0
},
{
"fieldname": "column_break_6",
"fieldtype": "Column Break",
"permlevel": 0,
"precision": ""
},
{
"description": "Before reconciliation",
"fieldname": "current_qty",
"fieldtype": "Float",
"label": "Current Qty",
"permlevel": 0,
"precision": "",
"read_only": 1
},
{
"description": "Before reconciliation",
"fieldname": "current_valuation_rate",
"fieldtype": "Read Only",
"label": "Current Valuation Rate",
"permlevel": 0,
"precision": "",
"read_only": 1
} }
], ],
"hide_heading": 0, "hide_heading": 0,
@ -96,7 +126,7 @@
"is_submittable": 0, "is_submittable": 0,
"issingle": 0, "issingle": 0,
"istable": 1, "istable": 1,
"modified": "2015-02-17 01:07:50.200649", "modified": "2015-02-20 04:20:46.692967",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Stock", "module": "Stock",
"name": "Stock Reconciliation Item", "name": "Stock Reconciliation Item",

View File

@ -38,18 +38,22 @@ def get_stock_balance(item_code, warehouse, posting_date=None, posting_time=None
"""Returns stock balance quantity at given warehouse on given posting date or current date. """Returns stock balance quantity at given warehouse on given posting date or current date.
If `with_valuation_rate` is True, will return tuple (qty, rate)""" If `with_valuation_rate` is True, will return tuple (qty, rate)"""
from erpnext.stock.stock_ledger import get_previous_sle
if not posting_date: posting_date = nowdate() if not posting_date: posting_date = nowdate()
if not posting_time: posting_time = nowtime() if not posting_time: posting_time = nowtime()
last_entry = frappe.db.sql("""select qty_after_transaction, valuation_rate from `tabStock Ledger Entry`
where item_code=%s and warehouse=%s last_entry = get_previous_sle({
and timestamp(posting_date, posting_time) < timestamp(%s, %s) "item_code": item_code,
order by timestamp(posting_date, posting_time) limit 1""", "warehouse":warehouse,
(item_code, warehouse, posting_date, posting_time)) "posting_date": posting_date,
"posting_time": posting_time })
if with_valuation_rate: if with_valuation_rate:
return (last_entry[0][0], last_entry[0][1]) if last_entry else (0.0, 0.0) return (last_entry.qty_after_transaction, last_entry.valuation_rate) if last_entry else (0.0, 0.0)
else: else:
return last_entry[0][0] if last_entry else 0.0 return last_entry.qty_after_transaction or 0.0
def get_latest_stock_balance(): def get_latest_stock_balance():
bin_map = {} bin_map = {}