Merge pull request #4444 from nabinhait/get_incoming_rate_fix

[fix] Get incoming rate if args as json
diff --git a/erpnext/__version__.py b/erpnext/__version__.py
index d004216..e0496c6 100644
--- a/erpnext/__version__.py
+++ b/erpnext/__version__.py
@@ -1,2 +1,2 @@
 from __future__ import unicode_literals
-__version__ = '6.12.3'
+__version__ = '6.12.4'
diff --git a/erpnext/accounts/doctype/sales_invoice/pos.py b/erpnext/accounts/doctype/sales_invoice/pos.py
index 145766f..18faae7 100644
--- a/erpnext/accounts/doctype/sales_invoice/pos.py
+++ b/erpnext/accounts/doctype/sales_invoice/pos.py
@@ -52,4 +52,5 @@
 			{condition}
 		order by
 			{order_by}
-			i.name""".format(condition=condition, order_by=order_by), args, as_dict=1)
+			i.name
+		limit 24""".format(condition=condition, order_by=order_by), args, as_dict=1)
diff --git a/erpnext/accounts/report/bank_reconciliation_statement/bank_reconciliation_statement.html b/erpnext/accounts/report/bank_reconciliation_statement/bank_reconciliation_statement.html
index d749383..ee77085 100644
--- a/erpnext/accounts/report/bank_reconciliation_statement/bank_reconciliation_statement.html
+++ b/erpnext/accounts/report/bank_reconciliation_statement/bank_reconciliation_statement.html
@@ -2,7 +2,7 @@
 	{%= frappe.boot.letter_heads[frappe.defaults.get_default("letter_head")] %}
 </div>
 <h2 class="text-center">{%= __("Bank Reconciliation Statement") %}</h2>
-<h4 class="text-center">{%= filters.account && (filters.account + ", ")  || "" %} {%= filters.company %}</h4>
+<h4 class="text-center">{%= filters.account %}</h4>
 <hr>
 <table class="table table-bordered">
 	<thead>
@@ -16,31 +16,31 @@
 	</thead>
 	<tbody>
 		{% for(var i=0, l=data.length; i<l; i++) { %}
-			{% if (data[i][__("Posting Date")]) { %}
+			{% if (data[i]["posting_date"]) { %}
 			<tr>
-				<td>{%= dateutil.str_to_user(data[i][__("Posting Date")]) %}</td>
-				<td>{%= data[i][__("Journal Entry")] %}</td>
-				<td>{%= __("Against") %}: {%= data[i][__("Against Account")] %}
-					{% if (data[i][__("Reference")]) { %}
-						<br>{%= __("Reference") %}: {%= data[i][__("Reference")] %}
-						{% if (data[i][__("Ref Date")]) { %}
-							<br>{%= __("Reference Date") %}: {%= dateutil.str_to_user(data[i][__("Ref Date")]) %}
+				<td>{%= dateutil.str_to_user(data[i]["posting_date"]) %}</td>
+				<td>{%= data[i]["journal_entry"] %}</td>
+				<td>{%= __("Against") %}: {%= data[i]["against_account"] %}
+					{% if (data[i]["reference"]) { %}
+						<br>{%= __("Reference") %}: {%= data[i]["reference"] %}
+						{% if (data[i]["ref_date"]) { %}
+							<br>{%= __("Reference Date") %}: {%= dateutil.str_to_user(data[i]["ref_date"]) %}
 						{% } %}
 					{% } %}
-					{% if (data[i][__("Clearance Date")]) { %}
-						<br>{%= __("Clearance Date") %}: {%= dateutil.str_to_user(data[i][__("Clearance Date")]) %}
+					{% if (data[i]["clearance_date"]) { %}
+						<br>{%= __("Clearance Date") %}: {%= dateutil.str_to_user(data[i]["clearance_date"]) %}
 					{% } %}
 				</td>
-				<td style="text-align: right">{%= format_currency(data[i][__("Debit")]) %}</td>
-				<td style="text-align: right">{%= format_currency(data[i][__("Credit")]) %}</td>
+				<td style="text-align: right">{%= format_currency(data[i]["debit"]) %}</td>
+				<td style="text-align: right">{%= format_currency(data[i]["credit"]) %}</td>
 			</tr>
 			{% } else { %}
 			<tr>
 				<td></td>
 				<td></td>
-				<td>{%= data[i][__("Journal Entry")] %}</td>
-				<td style="text-align: right">{%= format_currency(data[i][__("Debit")]) %}</td>
-				<td style="text-align: right">{%= format_currency(data[i][__("Credit")]) %}</td>
+				<td>{%= data[i]["journal_entry"] %}</td>
+				<td style="text-align: right">{%= format_currency(data[i]["debit"]) %}</td>
+				<td style="text-align: right">{%= format_currency(data[i]["credit"]) %}</td>
 			</tr>
 			{% } %}
 		{% } %}
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index feb144d..b10b72f 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -159,6 +159,7 @@
 	def on_cancel(self):
 		if self.has_drop_ship_item():
 			self.update_status_updater()
+			self.update_delivered_qty_in_sales_order()
 
 		pc_obj = frappe.get_doc('Purchase Common')
 		self.check_for_stopped_or_closed_status(pc_obj)
@@ -216,7 +217,7 @@
 
 		for so_name in sales_orders_to_update:
 			so = frappe.get_doc("Sales Order", so_name)
-			so.update_delivery_status(self.name)
+			so.update_delivery_status()
 			so.set_status(update=True)
 			so.notify_update()
 
diff --git a/erpnext/controllers/status_updater.py b/erpnext/controllers/status_updater.py
index eb8d5e9..73e9d7f 100644
--- a/erpnext/controllers/status_updater.py
+++ b/erpnext/controllers/status_updater.py
@@ -76,10 +76,10 @@
 		self.update_qty()
 		self.validate_qty()
 
-	def set_status(self, update=False, status=None):
+	def set_status(self, update=False, status=None, update_modified=True):
 		if self.is_new():
 			return
-
+				
 		if self.doctype in status_map:
 			_status = self.status
 
@@ -102,9 +102,10 @@
 
 			if self.status != _status and self.status not in ("Submitted", "Cancelled"):
 				self.add_comment("Label", _(self.status))
-
+			
 			if update:
-				frappe.db.set_value(self.doctype, self.name, "status", self.status)
+				frappe.db.set_value(self.doctype, self.name, "status", self.status, 
+					update_modified=update_modified)
 
 	def validate_qty(self):
 		"""Validates qty at row level"""
diff --git a/erpnext/docs/current/api/buying/print_format/drop_shipping/index.txt b/erpnext/docs/current/api/buying/print_format/drop_shipping/index.txt
deleted file mode 100644
index 5c383bd..0000000
--- a/erpnext/docs/current/api/buying/print_format/drop_shipping/index.txt
+++ /dev/null
@@ -1 +0,0 @@
-erpnext.buying.print_format.drop_shipping
\ No newline at end of file
diff --git a/erpnext/docs/current/api/buying/print_format/drop_shipping/erpnext.buying.print_format.drop_shipping.html b/erpnext/docs/current/api/buying/print_format/drop_shipping_format/erpnext.buying.print_format.drop_shipping_format.html
similarity index 78%
rename from erpnext/docs/current/api/buying/print_format/drop_shipping/erpnext.buying.print_format.drop_shipping.html
rename to erpnext/docs/current/api/buying/print_format/drop_shipping_format/erpnext.buying.print_format.drop_shipping_format.html
index d84423e..9c2648b 100644
--- a/erpnext/docs/current/api/buying/print_format/drop_shipping/erpnext.buying.print_format.drop_shipping.html
+++ b/erpnext/docs/current/api/buying/print_format/drop_shipping_format/erpnext.buying.print_format.drop_shipping_format.html
@@ -1,10 +1,10 @@
-<!-- title: erpnext.buying.print_format.drop_shipping --><div class="dev-header">
+<!-- title: erpnext.buying.print_format.drop_shipping_format --><div class="dev-header">
 
 <a class="btn btn-default btn-sm" disabled style="margin-bottom: 10px;">
 	Version 6.x.x</a>
 
 
-	<a class="btn btn-default btn-sm" href="https://github.com/frappe/erpnext/blob/develop/erpnext/buying/print_format/drop_shipping.py"
+	<a class="btn btn-default btn-sm" href="https://github.com/frappe/erpnext/blob/develop/erpnext/buying/print_format/drop_shipping_format.py"
 		target="_blank" style="margin-left: 10px; margin-bottom: 10px;"><i class="octicon octicon-mark-github"></i> Source</a>
 
 </div>
diff --git a/erpnext/docs/current/api/buying/print_format/drop_shipping/index.html b/erpnext/docs/current/api/buying/print_format/drop_shipping_format/index.html
similarity index 81%
rename from erpnext/docs/current/api/buying/print_format/drop_shipping/index.html
rename to erpnext/docs/current/api/buying/print_format/drop_shipping_format/index.html
index 47647f0..ef09e77 100644
--- a/erpnext/docs/current/api/buying/print_format/drop_shipping/index.html
+++ b/erpnext/docs/current/api/buying/print_format/drop_shipping_format/index.html
@@ -1,4 +1,4 @@
-<!-- title: drop_shipping -->
+<!-- title: drop_shipping_format -->
 
 
 <div class="dev-header">
@@ -7,7 +7,7 @@
 	Version 6.x.x</a>
 
 
-	<a class="btn btn-default btn-sm" href="https://github.com/frappe/erpnext/tree/develop/drop_shipping"
+	<a class="btn btn-default btn-sm" href="https://github.com/frappe/erpnext/tree/develop/drop_shipping_format"
 		target="_blank" style="margin-left: 10px; margin-bottom: 10px;"><i class="octicon octicon-mark-github"></i> Source</a>
 
 </div>
diff --git a/erpnext/docs/current/api/buying/print_format/drop_shipping_format/index.txt b/erpnext/docs/current/api/buying/print_format/drop_shipping_format/index.txt
new file mode 100644
index 0000000..590a2c9
--- /dev/null
+++ b/erpnext/docs/current/api/buying/print_format/drop_shipping_format/index.txt
@@ -0,0 +1 @@
+erpnext.buying.print_format.drop_shipping_format
\ No newline at end of file
diff --git a/erpnext/docs/current/api/stock/erpnext.stock.utils.html b/erpnext/docs/current/api/stock/erpnext.stock.utils.html
index 7f2d4e2..1fb0155 100644
--- a/erpnext/docs/current/api/stock/erpnext.stock.utils.html
+++ b/erpnext/docs/current/api/stock/erpnext.stock.utils.html
@@ -79,7 +79,9 @@
 	
         
     
-    
+    <p><span class="label label-info">Public API</span>
+        <br><code>/api/method/erpnext.stock.utils.get_incoming_rate</code>
+    </p>
 	<p class="docs-attr-name">
         <a name="erpnext.stock.utils.get_incoming_rate" href="#erpnext.stock.utils.get_incoming_rate" class="text-muted small">
             <i class="icon-link small" style="color: #ccc;"></i></a>
diff --git a/erpnext/docs/current/models/accounts/sales_invoice.html b/erpnext/docs/current/models/accounts/sales_invoice.html
index 8f0f364..76924c3 100644
--- a/erpnext/docs/current/models/accounts/sales_invoice.html
+++ b/erpnext/docs/current/models/accounts/sales_invoice.html
@@ -2476,6 +2476,20 @@
     
     
 	<p class="docs-attr-name">
+        <a name="validate_dropship_item" href="#validate_dropship_item" class="text-muted small">
+            <i class="icon-link small" style="color: #ccc;"></i></a>
+		<b>validate_dropship_item</b>
+        <i class="text-muted">(self)</i>
+    </p>
+	<div class="docs-attr-desc"><p><span class="text-muted">No docs</span></p>
+</div>
+	<br>
+
+        
+        
+    
+    
+	<p class="docs-attr-name">
         <a name="validate_fixed_asset_account" href="#validate_fixed_asset_account" class="text-muted small">
             <i class="icon-link small" style="color: #ccc;"></i></a>
 		<b>validate_fixed_asset_account</b>
diff --git a/erpnext/docs/current/models/home/index.html b/erpnext/docs/current/models/home/index.html
deleted file mode 100644
index e21468e..0000000
--- a/erpnext/docs/current/models/home/index.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<!-- title: Module home -->
-
-
-<div class="dev-header">
-
-<a class="btn btn-default btn-sm" disabled style="margin-bottom: 10px;">
-	Version 6.x.x</a>
-
-
-	<a class="btn btn-default btn-sm" href="https://github.com/frappe/erpnext/tree/develop/erpnext/home"
-		target="_blank" style="margin-left: 10px; margin-bottom: 10px;"><i class="octicon octicon-mark-github"></i> Source</a>
-
-</div>
-
-<h3>DocTypes for home</h3>
-
-{index}
-
-<!-- autodoc -->
\ No newline at end of file
diff --git a/erpnext/docs/current/models/home/index.txt b/erpnext/docs/current/models/home/index.txt
deleted file mode 100644
index e69de29..0000000
--- a/erpnext/docs/current/models/home/index.txt
+++ /dev/null
diff --git a/erpnext/docs/current/models/hr/leave_application.html b/erpnext/docs/current/models/hr/leave_application.html
index bc91ea5..e40ce3a 100644
--- a/erpnext/docs/current/models/hr/leave_application.html
+++ b/erpnext/docs/current/models/hr/leave_application.html
@@ -867,7 +867,7 @@
         <a name="erpnext.hr.doctype.leave_application.leave_application.get_leave_balance_on" href="#erpnext.hr.doctype.leave_application.leave_application.get_leave_balance_on" class="text-muted small">
             <i class="icon-link small" style="color: #ccc;"></i></a>
 		erpnext.hr.doctype.leave_application.leave_application.<b>get_leave_balance_on</b>
-        <i class="text-muted">(employee, leave_type, date, allocation_records=None)</i>
+        <i class="text-muted">(employee, leave_type, date, allocation_records=None, consider_all_leaves_in_the_allocation_period=False)</i>
     </p>
 	<div class="docs-attr-desc"><p><span class="text-muted">No docs</span></p>
 </div>
diff --git a/erpnext/docs/user/manual/en/customize-erpnext/articles/index.txt b/erpnext/docs/user/manual/en/customize-erpnext/articles/index.txt
index 3ac0897..a810705 100644
--- a/erpnext/docs/user/manual/en/customize-erpnext/articles/index.txt
+++ b/erpnext/docs/user/manual/en/customize-erpnext/articles/index.txt
@@ -9,8 +9,8 @@
 making-custom-reports-in-erpnext
 managing-dynamic-link-fields
 module-visibility
-perm-level-error-in-permission-manager-1
+perm-level-error-in-permission-manager
 search-record-by-specific-field
 set-language
 set-precision-for-float-currency-and-percent-fields
-user-restriction
\ No newline at end of file
+user-restriction
diff --git a/erpnext/docs/user/manual/en/customize-erpnext/articles/perm-level-error-in-permission-manager-1.md b/erpnext/docs/user/manual/en/customize-erpnext/articles/perm-level-error-in-permission-manager.md
similarity index 100%
rename from erpnext/docs/user/manual/en/customize-erpnext/articles/perm-level-error-in-permission-manager-1.md
rename to erpnext/docs/user/manual/en/customize-erpnext/articles/perm-level-error-in-permission-manager.md
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index 744db44..22a093d 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -7,7 +7,7 @@
 app_description = """ERP made simple"""
 app_icon = "icon-th"
 app_color = "#e74c3c"
-app_version = "6.12.3"
+app_version = "6.12.4"
 app_email = "info@erpnext.com"
 app_license = "GNU General Public License (v3)"
 source_link = "https://github.com/frappe/erpnext"
@@ -111,6 +111,9 @@
 	"Price List": {
 		"on_update": "erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings.validate_cart_settings"
 	},
+	"Address": {
+		"validate": "erpnext.shopping_cart.cart.set_customer_in_address"
+	}
 }
 
 scheduler_events = {
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index cd38e67..302a338 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -237,4 +237,5 @@
 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
+erpnext.patches.v6_10.fix_billed_amount_in_drop_ship_po
+erpnext.patches.v6_10.fix_delivery_status_of_drop_ship_item
\ No newline at end of file
diff --git a/erpnext/patches/v6_10/fix_delivery_status_of_drop_ship_item.py b/erpnext/patches/v6_10/fix_delivery_status_of_drop_ship_item.py
new file mode 100644
index 0000000..a54d0ab
--- /dev/null
+++ b/erpnext/patches/v6_10/fix_delivery_status_of_drop_ship_item.py
@@ -0,0 +1,17 @@
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+	sales_orders_to_update = []
+
+	for item in frappe.get_all("Purchase Order Item", filters={"delivered_by_supplier": 1}, 
+		fields=["prevdoc_doctype", "prevdoc_docname"]):
+		
+		if item.prevdoc_doctype == "Sales Order":
+			if item.prevdoc_docname not in sales_orders_to_update:
+				sales_orders_to_update.append(item.prevdoc_docname)
+
+	for so_name in sales_orders_to_update:
+		so = frappe.get_doc("Sales Order", so_name)
+		so.update_delivery_status()
+		so.set_status(update=True, update_modified=False)
\ No newline at end of file
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index 05cb50c..7712f5e 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -265,27 +265,29 @@
 		if exc_list:
 			frappe.throw('\n'.join(exc_list))
 
-	def update_delivery_status(self, po_name):
+	def update_delivery_status(self):
 		"""Update delivery status from Purchase Order for drop shipping"""
 		tot_qty, delivered_qty = 0.0, 0.0
 
 		for item in self.items:
 			if item.delivered_by_supplier:
-				item_delivered_qty  = frappe.db.sql("""select qty
+				item_delivered_qty  = frappe.db.sql("""select sum(qty)
 					from `tabPurchase Order Item` poi, `tabPurchase Order` po
-					where poi.prevdoc_docname = %s
+					where poi.prevdoc_detail_docname = %s
 						and poi.prevdoc_doctype = 'Sales Order'
 						and poi.item_code = %s
 						and poi.parent = po.name
-						and po.status = 'Delivered'""", (self.name, item.item_code))
+						and po.docstatus = 1
+						and po.status = 'Delivered'""", (item.name, item.item_code))
 
 				item_delivered_qty = item_delivered_qty[0][0] if item_delivered_qty else 0
-				item.db_set("delivered_qty", item_delivered_qty)
+				item.db_set("delivered_qty", flt(item_delivered_qty), update_modified=False)
 
 			delivered_qty += item.delivered_qty
 			tot_qty += item.qty
-
-		frappe.db.set_value("Sales Order", self.name, "per_delivered", flt(delivered_qty/tot_qty) * 100)
+			
+		frappe.db.set_value("Sales Order", self.name, "per_delivered", flt(delivered_qty/tot_qty) * 100, 
+		update_modified=False)
 
 def get_list_context(context=None):
 	from erpnext.controllers.website_list_for_contact import get_list_context
diff --git a/erpnext/shopping_cart/cart.py b/erpnext/shopping_cart/cart.py
index c116bdc..561a50c 100644
--- a/erpnext/shopping_cart/cart.py
+++ b/erpnext/shopping_cart/cart.py
@@ -280,6 +280,7 @@
 		user = frappe.session.user
 
 	customer = frappe.db.get_value("Contact", {"email_id": user}, "customer")
+
 	if customer:
 		return frappe.get_doc("Customer", customer)
 
@@ -320,6 +321,17 @@
 
 	return address_docs
 
+def set_customer_in_address(doc, method=None):
+	if doc.flags.linked:
+		return
+
+	doc.check_if_linked()
+
+	if not doc.flags.linked and (frappe.db.get_value("User", frappe.session.user, "user_type") == "Website User"):
+		# creates a customer if one does not exist
+		get_customer()
+		doc.link_address()
+
 @frappe.whitelist()
 def apply_shipping_rule(shipping_rule):
 	quotation = _get_cart_quotation()
diff --git a/erpnext/templates/includes/address_row.html b/erpnext/templates/includes/address_row.html
index 717ca75..f6ec819 100644
--- a/erpnext/templates/includes/address_row.html
+++ b/erpnext/templates/includes/address_row.html
@@ -3,7 +3,6 @@
         <h4 class="strong">{{ doc.address_title }}</h4>
         <p class="text-muted small">
             {{ frappe.get_doc(doc).get_display() }}
-        </div>
-    </div>
+        </p>
     </a>
 </div>
diff --git a/erpnext/utilities/doctype/address/address.py b/erpnext/utilities/doctype/address/address.py
index a482891..da529fb 100644
--- a/erpnext/utilities/doctype/address/address.py
+++ b/erpnext/utilities/doctype/address/address.py
@@ -10,6 +10,9 @@
 from frappe.model.document import Document
 
 class Address(Document):
+	def __setup__(self):
+		self.flags.linked = False
+
 	def autoname(self):
 		if not self.address_title:
 			self.address_title = self.customer \
@@ -42,13 +45,10 @@
 
 	def link_address(self):
 		"""Link address based on owner"""
-		linked = False
-		for fieldname in self.link_fields:
-			if self.get(fieldname):
-				linked = True
-				break
+		if not self.flags.linked:
+			self.check_if_linked()
 
-		if not linked:
+		if not self.flags.linked:
 			contact = frappe.db.get_value("Contact", {"email_id": self.owner},
 				("name", "customer", "supplier"), as_dict = True)
 			if contact:
@@ -57,6 +57,11 @@
 
 			self.lead = frappe.db.get_value("Lead", {"email_id": self.owner})
 
+	def check_if_linked(self):
+		for fieldname in self.link_fields:
+			if self.get(fieldname):
+				self.flags.linked = True
+				break
 
 	def validate_shipping_address(self):
 		"""Validate that there can only be one shipping address for particular customer, supplier"""
diff --git a/setup.py b/setup.py
index 66d4dcc..56c5b7d 100644
--- a/setup.py
+++ b/setup.py
@@ -1,6 +1,6 @@
 from setuptools import setup, find_packages
 
-version = "6.12.3"
+version = "6.12.4"
 
 with open("requirements.txt", "r") as f:
 	install_requires = f.readlines()