Merge pull request #4400 from nabinhait/total_amount_in_jv

[fix] Total Amount in Journal Entry in bank currency
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index 12934a1..d916b61 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -150,10 +150,14 @@
 		against_accounts = []
 		stock_items = self.get_stock_items()
 		for item in self.get("items"):
+			# in case of auto inventory accounting, 
+			# against expense account is always "Stock Received But Not Billed"
+			# for a stock item and if not epening entry and not drop-ship entry
+			
 			if auto_accounting_for_stock and item.item_code in stock_items \
-					and self.is_opening == 'No':
-				# in case of auto inventory accounting, against expense account is always
-				# Stock Received But Not Billed for a stock item
+				and self.is_opening == 'No' and (not item.po_detail or 
+					not frappe.db.get_value("Purchase Order Item", item.po_detail, "delivered_by_supplier")):
+				
 				item.expense_account = stock_not_billed_account
 				item.cost_center = None
 
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js
index 0d139df..f1179ee 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.js
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.js
@@ -47,20 +47,20 @@
 				cur_frm.add_custom_button(__('Close'), this.close_purchase_order);
 			}
 
-			if(is_drop_ship && doc.status!="Delivered"){
-				cur_frm.add_custom_button(__('Mark as Delivered'), this.delivered_by_supplier);
-			}
-
 			if(flt(doc.per_billed)==0) {
 				cur_frm.add_custom_button(__('Payment'), cur_frm.cscript.make_bank_entry);
 			}
-
+			
+			if(is_drop_ship && doc.status!="Delivered"){
+				cur_frm.add_custom_button(__('Mark as Delivered'),
+					 this.delivered_by_supplier).addClass("btn-primary");
+			}
 		} else if(doc.docstatus===0) {
 			cur_frm.cscript.add_from_mappers();
 		} 
 		
 		if(doc.docstatus == 1 && !in_list(["Stopped", "Closed"], doc.status)) {
-			if(flt(doc.per_received, 2) < 100 && this.frm.doc.__onload.has_stock_item && allow_receipt) {
+			if(flt(doc.per_received, 2) < 100 && allow_receipt) {
 				cur_frm.add_custom_button(__('Receive'), this.make_purchase_receipt).addClass("btn-primary");
 
 				if(doc.is_subcontracted==="Yes") {
@@ -70,12 +70,13 @@
 			}
 
 			if(flt(doc.per_billed, 2) < 100)
-				cur_frm.add_custom_button(__('Invoice'), this.make_purchase_invoice);	
+				cur_frm.add_custom_button(__('Invoice'), 
+					this.make_purchase_invoice).addClass("btn-primary");	
 		}
 		
 		if(doc.docstatus == 1 && in_list(["Stopped", "Closed", "Delivered"], doc.status)) {
 			if (this.frm.has_perm("submit")) {
-				cur_frm.add_custom_button(__('Re-open'), this.unstop_purchase_order);
+				cur_frm.add_custom_button(__('Re-open'), this.unstop_purchase_order).addClass("btn-primary");
 			}
 		}
 	},
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json
index bd3f6fb..e52081b 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.json
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.json
@@ -418,29 +418,6 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
-   "fieldname": "delivered_by_supplier", 
-   "fieldtype": "Check", 
-   "hidden": 0, 
-   "ignore_user_permissions": 0, 
-   "in_filter": 0, 
-   "in_list_view": 0, 
-   "label": "To be delivered to customer", 
-   "length": 0, 
-   "no_copy": 0, 
-   "permlevel": 0, 
-   "precision": "", 
-   "print_hide": 1, 
-   "read_only": 1, 
-   "report_hide": 0, 
-   "reqd": 0, 
-   "search_index": 0, 
-   "set_only_once": 0, 
-   "unique": 0
-  }, 
-  {
-   "allow_on_submit": 0, 
-   "bold": 0, 
-   "collapsible": 0, 
    "fieldname": "column_break_19", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -2406,7 +2383,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2015-11-20 15:51:09.314885", 
+ "modified": "2015-11-20 15:51:09.314888", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Purchase Order", 
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index 4670e35..1212899 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -33,9 +33,6 @@
 			'overflow_type': 'order'
 		}]
 
-	def onload(self):
-		self.set_onload("has_stock_item", len(self.get_stock_items()) > 0)
-
 	def validate(self):
 		super(PurchaseOrder, self).validate()
 
@@ -51,7 +48,7 @@
 		self.validate_for_subcontracting()
 		self.validate_minimum_order_qty()
 		self.create_raw_materials_supplied("supplied_items")
-		self.set_received_qty_and_billed_amount_for_drop_ship_items()
+		self.set_received_qty_for_drop_ship_items()
 
 	def validate_with_previous_doc(self):
 		super(PurchaseOrder, self).validate_with_previous_doc({
@@ -256,11 +253,10 @@
 		
 		return is_drop_ship
 	
-	def set_received_qty_and_billed_amount_for_drop_ship_items(self):
+	def set_received_qty_for_drop_ship_items(self):
 		for item in self.items:
 			if item.delivered_by_supplier == 1:
 				item.received_qty = item.qty
-				item.billed_amt = item.amount
 
 @frappe.whitelist()
 def stop_or_unstop_purchase_orders(names, status):
@@ -345,7 +341,7 @@
 				"parent": "purchase_order",
 			},
 			"postprocess": update_item,
-			"condition": lambda doc: (doc.base_amount==0 or doc.billed_amt < doc.amount) and doc.delivered_by_supplier!=1
+			"condition": lambda doc: (doc.base_amount==0 or doc.billed_amt < doc.amount)
 		},
 		"Purchase Taxes and Charges": {
 			"doctype": "Purchase Taxes and Charges",
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 65f39aa..861ec8e 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -133,6 +133,14 @@
 	def set_missing_item_details(self):
 		"""set missing item values"""
 		from erpnext.stock.get_item_details import get_item_details
+		
+		if self.doctype == "Purchase Invoice":
+			auto_accounting_for_stock = cint(frappe.defaults.get_global_default("auto_accounting_for_stock"))
+
+			if auto_accounting_for_stock:
+				stock_not_billed_account = self.get_company_default("stock_received_but_not_billed")
+				
+			stock_items = self.get_stock_items()
 
 		if hasattr(self, "items"):
 			parent_dict = {}
@@ -170,6 +178,15 @@
 						if item.price_list_rate:
 							item.rate = flt(item.price_list_rate *
 								(1.0 - (flt(item.discount_percentage) / 100.0)), item.precision("rate"))
+								
+					if self.doctype == "Purchase Invoice":
+						if auto_accounting_for_stock and item.item_code in stock_items \
+							and self.is_opening == 'No' \
+							and (not item.po_detail or not frappe.db.get_value("Purchase Order Item", 
+								item.po_detail, "delivered_by_supplier")):
+				
+								item.expense_account = stock_not_billed_account
+								item.cost_center = None
 
 	def set_taxes(self):
 		if not self.meta.get_field("taxes"):
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 97d1d93..cd38e67 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -237,3 +237,4 @@
 erpnext.patches.v6_10.fix_ordered_received_billed
 erpnext.patches.v6_10.fix_jv_total_amount #2015-11-30
 erpnext.patches.v6_10.email_digest_default_quote
+erpnext.patches.v6_10.fix_billed_amount_in_drop_ship_po
\ No newline at end of file
diff --git a/erpnext/patches/v6_10/fix_billed_amount_in_drop_ship_po.py b/erpnext/patches/v6_10/fix_billed_amount_in_drop_ship_po.py
new file mode 100644
index 0000000..4aecaa7
--- /dev/null
+++ b/erpnext/patches/v6_10/fix_billed_amount_in_drop_ship_po.py
@@ -0,0 +1,18 @@
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+	frappe.db.sql("""update `tabPurchase Order Item` set billed_amt = 0
+			where delivered_by_supplier=1 and docstatus=1""")
+			
+	drop_ship_pos = frappe.db.sql("""select distinct parent from `tabPurchase Order Item` 
+		where delivered_by_supplier=1 and docstatus=1""")
+		
+	for po in drop_ship_pos:
+		invoices = frappe.db.sql("""select distinct parent from `tabPurchase Invoice Item`
+			where purchase_order=%s and docstatus=1""", po[0])
+		if invoices:
+			for inv in invoices:
+				frappe.get_doc("Purchase Invoice", inv[0]).update_qty(change_modified=False)
+		else:
+			frappe.db.sql("""update `tabPurchase Order` set per_billed=0 where name=%s""", po[0])
\ No newline at end of file
diff --git a/erpnext/setup/setup_wizard/setup_wizard.py b/erpnext/setup/setup_wizard/setup_wizard.py
index 2289e26..4d4cc6d 100644
--- a/erpnext/setup/setup_wizard/setup_wizard.py
+++ b/erpnext/setup/setup_wizard/setup_wizard.py
@@ -7,8 +7,6 @@
 from frappe.utils import cstr, flt, getdate
 from frappe import _
 from frappe.utils.file_manager import save_file
-from frappe.translate import set_default_language
-from frappe.geo.country_info import get_country_info
 from .default_website import website_maker
 import install_fixtures
 from .sample_data import make_sample_data
@@ -19,11 +17,6 @@
 	if frappe.db.sql("select name from tabCompany"):
 		frappe.throw(_("Setup Already Complete!!"))
 
-	if args.language and args.language != "english":
-		set_default_language(args.language)
-
-	frappe.clear_cache()
-
 	install_fixtures.install(args.get("country"))
 
 	update_user_name(args)
@@ -141,26 +134,7 @@
 
 	global_defaults.save()
 
-	number_format = get_country_info(args.get("country")).get("number_format", "#,###.##")
-
-	# replace these as float number formats, as they have 0 precision
-	# and are currency number formats and not for floats
-	if number_format=="#.###":
-		number_format = "#.###,##"
-	elif number_format=="#,###":
-		number_format = "#,###.##"
-
-	system_settings = frappe.get_doc("System Settings", "System Settings")
-	system_settings.update({
-		"language": args.get("language"),
-		"time_zone": args.get("timezone"),
-		"float_precision": 3,
-		"email_footer_address": args.get("company"),
-		'date_format': frappe.db.get_value("Country", args.get("country"), "date_format"),
-		'number_format': number_format,
-		'enable_scheduler': 1 if not frappe.flags.in_test else 0
-	})
-	system_settings.save()
+	frappe.db.set_value("System Settings", None, "email_footer_address", args.get("company"))
 
 	accounts_settings = frappe.get_doc("Accounts Settings")
 	accounts_settings.auto_accounting_for_stock = 1