Merge pull request #2225 from nabinhait/hotfix

GL Entries for future stock voucher and multiple minor fixes
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 3b4c92d..a2bf78c 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -6,10 +6,10 @@
 import frappe.defaults
 from frappe.utils import cint, cstr, flt
 from frappe import _, msgprint, throw
-
 from erpnext.accounts.party import get_party_account, get_due_date
 from erpnext.controllers.stock_controller import update_gl_entries_after
 from frappe.model.mapper import get_mapped_doc
+
 from erpnext.controllers.selling_controller import SellingController
 
 form_grid_templates = {
@@ -473,9 +473,8 @@
 
 			if repost_future_gle and cint(self.update_stock) \
 				and cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
-					items, warehouse_account = self.get_items_and_warehouse_accounts()
-					update_gl_entries_after(self.posting_date, self.posting_time,
-						warehouse_account, items)
+					items, warehouses = self.get_items_and_warehouses()
+					update_gl_entries_after(self.posting_date, self.posting_time, warehouses, items)
 
 	def get_gl_entries(self, warehouse_account=None):
 		from erpnext.accounts.general_ledger import merge_similar_entries
diff --git a/erpnext/accounts/report/sales_invoice_trends/sales_invoice_trends.py b/erpnext/accounts/report/sales_invoice_trends/sales_invoice_trends.py
index 4e90168..e9a9bd0 100644
--- a/erpnext/accounts/report/sales_invoice_trends/sales_invoice_trends.py
+++ b/erpnext/accounts/report/sales_invoice_trends/sales_invoice_trends.py
@@ -11,4 +11,4 @@
 	conditions = get_columns(filters, "Sales Invoice")
 	data = get_data(filters, conditions)
 
-	return conditions["columns"], data 
\ No newline at end of file
+	return conditions["columns"], data
diff --git a/erpnext/config/stock.py b/erpnext/config/stock.py
index 957abec..7a4345e 100644
--- a/erpnext/config/stock.py
+++ b/erpnext/config/stock.py
@@ -28,6 +28,11 @@
 				},
 				{
 					"type": "doctype",
+					"name": "Installation Note",
+					"description": _("Installation record for a Serial No.")
+				},
+				{
+					"type": "doctype",
 					"name": "Item",
 					"description": _("All Products or Services."),
 				},
@@ -59,11 +64,6 @@
 				},
 				{
 					"type": "doctype",
-					"name": "Installation Note",
-					"description": _("Installation record for a Serial No.")
-				},
-				{
-					"type": "doctype",
 					"name": "Packing Slip",
 					"description": _("Split Delivery Note into packages.")
 				},
diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py
index 1a11308..5755253 100644
--- a/erpnext/controllers/stock_controller.py
+++ b/erpnext/controllers/stock_controller.py
@@ -16,16 +16,15 @@
 			delete_gl_entries(voucher_type=self.doctype, voucher_no=self.name)
 
 		if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
-			warehouse_account = self.get_warehouse_account()
+			warehouse_account = get_warehouse_account()
 
 			if self.docstatus==1:
 				gl_entries = self.get_gl_entries(warehouse_account)
 				make_gl_entries(gl_entries)
 
 			if repost_future_gle:
-				items, warehouse_account = self.get_items_and_warehouse_accounts(warehouse_account)
-				update_gl_entries_after(self.posting_date, self.posting_time,
-					warehouse_account, items)
+				items, warehouses = self.get_items_and_warehouses()
+				update_gl_entries_after(self.posting_date, self.posting_time, warehouses, items, warehouse_account)
 
 	def get_gl_entries(self, warehouse_account=None, default_expense_account=None,
 			default_cost_center=None):
@@ -88,10 +87,8 @@
 
 			return details
 
-	def get_items_and_warehouse_accounts(self, warehouse_account=None):
+	def get_items_and_warehouses(self):
 		items, warehouses = [], []
-		if not warehouse_account:
-			warehouse_account = get_warehouse_account()
 
 		if hasattr(self, "fname"):
 			item_doclist = self.get(self.fname)
@@ -117,10 +114,7 @@
 					if d.get("t_warehouse") and d.t_warehouse not in warehouses:
 						warehouses.append(d.t_warehouse)
 
-			warehouse_account = {wh: warehouse_account[wh] for wh in warehouses
-				if warehouse_account.get(wh)}
-
-		return items, warehouse_account
+		return items, warehouses
 
 	def get_stock_ledger_details(self):
 		stock_ledger = {}
@@ -130,73 +124,6 @@
 				stock_ledger.setdefault(sle.voucher_detail_no, []).append(sle)
 		return stock_ledger
 
-	def get_warehouse_account(self):
-		warehouse_account = dict(frappe.db.sql("""select master_name, name from tabAccount
-			where account_type = 'Warehouse' and ifnull(master_name, '') != ''"""))
-		return warehouse_account
-
-	def update_gl_entries_after(self, warehouse_account=None):
-		future_stock_vouchers = self.get_future_stock_vouchers()
-		gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
-		if not warehouse_account:
-			warehouse_account = self.get_warehouse_account()
-		for voucher_type, voucher_no in future_stock_vouchers:
-			existing_gle = gle.get((voucher_type, voucher_no), [])
-			voucher_obj = frappe.get_doc(voucher_type, voucher_no)
-			expected_gle = voucher_obj.get_gl_entries(warehouse_account)
-			if expected_gle:
-				matched = True
-				if existing_gle:
-					for entry in expected_gle:
-						for e in existing_gle:
-							if entry.account==e.account \
-								and entry.against_account==e.against_account\
-								and entry.cost_center==e.cost_center:
-									if entry.debit != e.debit or entry.credit != e.credit:
-										matched = False
-										break
-				else:
-					matched = False
-
-				if not matched:
-					self.delete_gl_entries(voucher_type, voucher_no)
-					voucher_obj.make_gl_entries(repost_future_gle=False)
-			else:
-				self.delete_gl_entries(voucher_type, voucher_no)
-
-
-	def get_future_stock_vouchers(self):
-		condition = ""
-		item_list = []
-		if getattr(self, "fname", None):
-			item_list = [d.item_code for d in self.get(self.fname)]
-			if item_list:
-				condition = "and item_code in ({})".format(", ".join(["%s"] * len(item_list)))
-
-		future_stock_vouchers = frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no
-			from `tabStock Ledger Entry` sle
-			where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) {condition}
-			order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""".format(
-				condition=condition), tuple([self.posting_date, self.posting_date] + item_list),
-				as_list=True)
-
-		return future_stock_vouchers
-
-	def get_voucherwise_gl_entries(self, future_stock_vouchers):
-		gl_entries = {}
-		if future_stock_vouchers:
-			for d in frappe.db.sql("""select * from `tabGL Entry`
-				where posting_date >= %s and voucher_no in (%s)""" %
-				('%s', ', '.join(['%s']*len(future_stock_vouchers))),
-				tuple([self.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
-					gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
-
-		return gl_entries
-
-	def delete_gl_entries(self, voucher_type, voucher_no):
-		frappe.db.sql("""delete from `tabGL Entry`
-			where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
-
 	def make_adjustment_entry(self, expected_gle, voucher_obj):
 		from erpnext.accounts.utils import get_stock_and_account_difference
 		account_list = [d.account for d in expected_gle]
@@ -287,15 +214,15 @@
 
 		return serialized_items
 
-def update_gl_entries_after(posting_date, posting_time, warehouse_account=None, for_items=None):
+def update_gl_entries_after(posting_date, posting_time, for_warehouses=None, for_items=None, warehouse_account=None):
 	def _delete_gl_entries(voucher_type, voucher_no):
 		frappe.db.sql("""delete from `tabGL Entry`
 			where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
 
 	if not warehouse_account:
 		warehouse_account = get_warehouse_account()
-	future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time,
-		warehouse_account, for_items)
+
+	future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time, for_warehouses, for_items)
 	gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date)
 
 	for voucher_type, voucher_no in future_stock_vouchers:
@@ -321,7 +248,7 @@
 					break
 	return matched
 
-def get_future_stock_vouchers(posting_date, posting_time, warehouse_account=None, for_items=None):
+def get_future_stock_vouchers(posting_date, posting_time, for_warehouses=None, for_items=None):
 	future_stock_vouchers = []
 
 	values = []
@@ -330,9 +257,9 @@
 		condition += " and item_code in ({})".format(", ".join(["%s"] * len(for_items)))
 		values += for_items
 
-	if warehouse_account:
-		condition += " and warehouse in ({})".format(", ".join(["%s"] * len(warehouse_account.keys())))
-		values += warehouse_account.keys()
+	if for_warehouses:
+		condition += " and warehouse in ({})".format(", ".join(["%s"] * len(for_warehouses)))
+		values += for_warehouses
 
 	for d in frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no
 		from `tabStock Ledger Entry` sle
diff --git a/erpnext/public/js/purchase_trends_filters.js b/erpnext/public/js/purchase_trends_filters.js
index cab8bbf..d609cf8 100644
--- a/erpnext/public/js/purchase_trends_filters.js
+++ b/erpnext/public/js/purchase_trends_filters.js
@@ -24,7 +24,7 @@
 				{ "value": "Item Group", "label": __("Item Group") },
 				{ "value": "Supplier", "label": __("Supplier") },
 				{ "value": "Supplier Type", "label": __("Supplier Type") },
-				{ "value": "Supplier Type", "label": __("Project") }
+				{ "value": "Project", "label": __("Project") }
 			],
 			"default": "Item"
 		},
diff --git a/erpnext/public/js/sales_trends_filters.js b/erpnext/public/js/sales_trends_filters.js
index 46070b4..0503133 100644
--- a/erpnext/public/js/sales_trends_filters.js
+++ b/erpnext/public/js/sales_trends_filters.js
@@ -25,7 +25,7 @@
 				{ "value": "Customer", "label": __("Customer") },
 				{ "value": "Customer Group", "label": __("Customer Group") },
 				{ "value": "Territory", "label": __("Territory") },
-				{ "value": "Supplier Type", "label": __("Project") }
+				{ "value": "Project", "label": __("Project") }
 			],
 			"default": "Item"
 		},
diff --git a/erpnext/setup/doctype/naming_series/naming_series.py b/erpnext/setup/doctype/naming_series/naming_series.py
index cb0d437..09a019c 100644
--- a/erpnext/setup/doctype/naming_series/naming_series.py
+++ b/erpnext/setup/doctype/naming_series/naming_series.py
@@ -48,11 +48,10 @@
 		# validate names
 		for i in options: self.validate_series_name(i)
 
-		if self.user_must_always_select:
+		if options and self.user_must_always_select:
 			options = [''] + options
-			default = ''
-		else:
-			default = options[0]
+
+		default = options[0] if options else ''
 
 		# update in property setter
 		prop_dict = {'options': "\n".join(options), 'default': default}
diff --git a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py
index e6bb68b..b4fa971 100644
--- a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py
+++ b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py
@@ -15,7 +15,7 @@
 		self.set("landed_cost_items", [])
 		for pr in self.get("landed_cost_purchase_receipts"):
 			pr_items = frappe.db.sql("""select pr_item.item_code, pr_item.description,
-				pr_item.qty, pr_item.rate, pr_item.amount, pr_item.name
+				pr_item.qty, pr_item.base_rate, pr_item.base_amount, pr_item.name
 				from `tabPurchase Receipt Item` pr_item where parent = %s
 				and exists(select name from tabItem where name = pr_item.item_code and is_stock_item = 'Yes')""",
 				pr.purchase_receipt, as_dict=True)
@@ -25,8 +25,8 @@
 				item.item_code = d.item_code
 				item.description = d.description
 				item.qty = d.qty
-				item.rate = d.rate
-				item.amount = d.amount
+				item.rate = d.base_rate
+				item.amount = d.base_amount
 				item.purchase_receipt = pr.purchase_receipt
 				item.purchase_receipt_item = d.name
 
diff --git a/erpnext/support/doctype/maintenance_visit/maintenance_visit.json b/erpnext/support/doctype/maintenance_visit/maintenance_visit.json
index 743d210..2612776 100644
--- a/erpnext/support/doctype/maintenance_visit/maintenance_visit.json
+++ b/erpnext/support/doctype/maintenance_visit/maintenance_visit.json
@@ -279,7 +279,7 @@
  "icon": "icon-file-text", 
  "idx": 1, 
  "is_submittable": 1, 
- "modified": "2014-06-23 07:55:49.200714", 
+ "modified": "2014-09-26 11:37:41.026433", 
  "modified_by": "Administrator", 
  "module": "Support", 
  "name": "Maintenance Visit", 
@@ -301,7 +301,7 @@
    "write": 1
   }
  ], 
- "search_fields": "status,maintenance_type,customer,customer_name, address,mntc_date,company,fiscal_year", 
+ "search_fields": "status,maintenance_type,customer,customer_name,mntc_date,company,fiscal_year", 
  "sort_field": "modified", 
  "sort_order": "DESC"
 }
\ No newline at end of file