Merge pull request #3358 from nabinhait/develop

General ledger, _idx and FY in Material request
diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py
index 2dde685..9a2da96 100644
--- a/erpnext/controllers/selling_controller.py
+++ b/erpnext/controllers/selling_controller.py
@@ -85,9 +85,9 @@
 			existing_shipping_charge = self.get("taxes", filters=shipping_charge)
 			if existing_shipping_charge:
 				# take the last record found
-				existing_shipping_charge[-1].rate = shipping_amount
+				existing_shipping_charge[-1].tax_amount = shipping_amount
 			else:
-				shipping_charge["rate"] = shipping_amount
+				shipping_charge["tax_amount"] = shipping_amount
 				shipping_charge["description"] = shipping_rule.label
 				self.append("taxes", shipping_charge)
 
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index 54e2d03..c471007 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -34,13 +34,13 @@
 
 website_route_rules = [
 	{"from_route": "/orders", "to_route": "Sales Order"},
-	{"from_route": "/orders/<name>", "to_route": "print", "defaults": {"doctype": "Sales Order"}},
+	{"from_route": "/orders/<path:name>", "to_route": "print", "defaults": {"doctype": "Sales Order"}},
 	{"from_route": "/invoices", "to_route": "Sales Invoice"},
-	{"from_route": "/invoices/<name>", "to_route": "print", "defaults": {"doctype": "Sales Invoice"}},
+	{"from_route": "/invoices/<path:name>", "to_route": "print", "defaults": {"doctype": "Sales Invoice"}},
 	{"from_route": "/shipments", "to_route": "Delivery Note"},
-	{"from_route": "/shipments/<name>", "to_route": "print", "defaults": {"doctype": "Delivery Note"}},
+	{"from_route": "/shipments/<path:name>", "to_route": "print", "defaults": {"doctype": "Delivery Note"}},
 	{"from_route": "/issues", "to_route": "Issue"},
-	{"from_route": "/issues/<name>", "to_route": "print", "defaults": {"doctype": "Issue"}},
+	{"from_route": "/issues/<path:name>", "to_route": "print", "defaults": {"doctype": "Issue"}},
 	{"from_route": "/addresses", "to_route": "Address"},
 ]
 
diff --git a/erpnext/manufacturing/doctype/production_order/production_order.py b/erpnext/manufacturing/doctype/production_order/production_order.py
index 6cd2ad2..529c2a4 100644
--- a/erpnext/manufacturing/doctype/production_order/production_order.py
+++ b/erpnext/manufacturing/doctype/production_order/production_order.py
@@ -176,18 +176,18 @@
 		self.set('operations', [])
 
 		operations = frappe.db.sql("""select operation, description, workstation, idx,
-			hour_rate, time_in_mins, "Pending" as status from `tabBOM Operation` 
+			hour_rate, time_in_mins, "Pending" as status from `tabBOM Operation`
 			where parent = %s order by idx""", self.bom_no, as_dict=1)
 
 		self.set('operations', operations)
 		self.calculate_time()
-		
+
 	def calculate_time(self):
 		bom_qty = frappe.db.get_value("BOM", self.bom_no, "quantity")
-		
+
 		for d in self.get("operations"):
 			d.time_in_mins = flt(d.time_in_mins) / flt(bom_qty) * flt(self.qty)
-			
+
 		self.calculate_operating_cost()
 
 	def get_holidays(self, workstation):
@@ -220,7 +220,9 @@
 			time_log = make_time_log(self.name, d.operation, d.planned_start_time, d.planned_end_time,
 				flt(self.qty) - flt(d.completed_qty), self.project_name, d.workstation, operation_id=d.name)
 
-			self.check_operation_fits_in_working_hours(d)
+			if d.workstation:
+				# validate operating hours if workstation [not mandatory] is specified
+				self.check_operation_fits_in_working_hours(d)
 
 			original_start_time = time_log.from_time
 			while True:
diff --git a/erpnext/manufacturing/doctype/workstation/workstation.py b/erpnext/manufacturing/doctype/workstation/workstation.py
index bc9b190..dae01df 100644
--- a/erpnext/manufacturing/doctype/workstation/workstation.py
+++ b/erpnext/manufacturing/doctype/workstation/workstation.py
@@ -4,7 +4,7 @@
 from __future__ import unicode_literals
 import frappe
 from frappe import _
-from frappe.utils import flt, cint, getdate, formatdate, comma_and, time_diff_in_seconds, get_datetime
+from frappe.utils import flt, cint, getdate, formatdate, comma_and, time_diff_in_seconds, to_timedelta
 from frappe.model.document import Document
 from dateutil.parser import parse
 
@@ -60,7 +60,7 @@
 	workstation = frappe.get_doc("Workstation", workstation)
 
 	for working_hour in workstation.working_hours:
-		slot_length = (get_datetime(working_hour.end_time) - get_datetime(working_hour.start_time)).total_seconds()
+		slot_length = (to_timedelta(working_hour.end_time or "") - to_timedelta(working_hour.start_time or "")).total_seconds()
 		if slot_length >= operation_length:
 			return
 
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index fcf0ea7..5db55c6 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -158,3 +158,4 @@
 erpnext.patches.v5_0.rename_pos_setting
 erpnext.patches.v5_0.update_operation_description
 erpnext.patches.v5_0.set_footer_address
+execute:frappe.db.set_value("Backup Manager", None, "send_backups_to_dropbox", 1 if frappe.db.get_value("Backup Manager", None, "upload_backups_to_dropbox") in ("Daily", "Weekly") else 0)
diff --git a/erpnext/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.py b/erpnext/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.py
index 5bac1c5..f7aa70f 100644
--- a/erpnext/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.py
+++ b/erpnext/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.py
@@ -72,7 +72,7 @@
 	target_details = {}
 
 	for d in frappe.db.sql("""select md.name, mdp.month, mdp.percentage_allocation
-		from `tabMonthly Distribution Percentage` mdp, `tabMonthly Distribution` mdp
+		from `tabMonthly Distribution Percentage` mdp, `tabMonthly Distribution` md
 		where mdp.parent=md.name and md.fiscal_year=%s""", (filters["fiscal_year"]), as_dict=1):
 			target_details.setdefault(d.name, {}).setdefault(d.month, flt(d.percentage_allocation))
 
diff --git a/erpnext/setup/doctype/backup_manager/backup_dropbox.py b/erpnext/setup/doctype/backup_manager/backup_dropbox.py
index c6862d2..9e38833 100644
--- a/erpnext/setup/doctype/backup_manager/backup_dropbox.py
+++ b/erpnext/setup/doctype/backup_manager/backup_dropbox.py
@@ -48,6 +48,7 @@
 			frappe.db.set_value("Backup Manager", "Backup Manager", "dropbox_access_key", access_token.key)
 			frappe.db.set_value("Backup Manager", "Backup Manager", "dropbox_access_secret", access_token.secret)
 			frappe.db.set_value("Backup Manager", "Backup Manager", "dropbox_access_allowed", allowed)
+			frappe.db.set_value("Backup Manager", "Backup Manager", "send_backups_to_dropbox", 1)
 			dropbox_client = client.DropboxClient(sess)
 			try:
 				dropbox_client.file_create_folder("files")
diff --git a/erpnext/setup/doctype/backup_manager/backup_files_list.html b/erpnext/setup/doctype/backup_manager/backup_files_list.html
new file mode 100644
index 0000000..5ee52ef
--- /dev/null
+++ b/erpnext/setup/doctype/backup_manager/backup_files_list.html
@@ -0,0 +1,30 @@
+<table class="table table-striped" style="max-width: 600px;">
+    <thead>
+        <tr>
+            <th style="width: 30%;">
+                {{ __("Date") }}
+            </th>
+            <th style="width: 50%;">
+                {{ __("File") }}
+            </th>
+            <th>
+                {{ __("Size") }}
+            </th>
+        </tr>
+    </thead>
+    <tbody>
+        {% for (var i=0; i < files.length; i++) { %}
+        <tr>
+            <td>
+                {{ files[i][1] }}
+            </td>
+            <td>
+                <a href="{{ files[i][0] }}" target="_blank">{{ files[i][0] }}</a>
+            </td>
+            <td>
+                {{ files[i][2] }}
+            </td>
+        </tr>
+        {% } %}
+    </tbody>
+</table>
diff --git a/erpnext/setup/doctype/backup_manager/backup_manager.js b/erpnext/setup/doctype/backup_manager/backup_manager.js
index 6f4ec6b..cb4104a 100644
--- a/erpnext/setup/doctype/backup_manager/backup_manager.js
+++ b/erpnext/setup/doctype/backup_manager/backup_manager.js
@@ -2,35 +2,16 @@
 // License: GNU General Public License v3. See license.txt
 
 $.extend(cur_frm.cscript, {
+	onload_post_render: function() {
+		cur_frm.fields_dict.allow_dropbox_access.$input.addClass("btn-primary");
+
+		if(cur_frm.doc.__onload && cur_frm.doc.__onload.files) {
+			$(frappe.render_template("backup_files_list", {files:cur_frm.doc.__onload.files}))
+				.appendTo(cur_frm.fields_dict.current_backups.$wrapper.empty());
+		}
+	},
 	refresh: function() {
 		cur_frm.disable_save();
-
-		if(!(cint(cur_frm.doc.dropbox_access_allowed) ||
-			cint(cur_frm.doc.gdrive_access_allowed))) {
-				cur_frm.set_intro(__("You can start by selecting backup frequency and granting access for sync"));
-		} else {
-			var services = {
-				"dropbox": __("Dropbox")
-				// "gdrive": __("Google Drive")
-			}
-			var active_services = [];
-
-			$.each(services, function(service, label) {
-				var access_allowed = cint(cur_frm.doc[service + "_access_allowed"]);
-				var frequency = cur_frm.doc["upload_backups_to_" + service];
-				if(access_allowed && frequency && frequency !== "Never") {
-					active_services.push(label + " [" + frequency + "]");
-				}
-			});
-
-			if(active_services.length > 0) {
-				cur_frm.set_intro(__("Backups will be uploaded to") + ": " +
-					frappe.utils.comma_and(active_services));
-			} else {
-				cur_frm.set_intro("");
-			}
-		}
-
 	},
 
 	validate_send_notifications_to: function() {
diff --git a/erpnext/setup/doctype/backup_manager/backup_manager.json b/erpnext/setup/doctype/backup_manager/backup_manager.json
index e8840d5..7aa038e 100644
--- a/erpnext/setup/doctype/backup_manager/backup_manager.json
+++ b/erpnext/setup/doctype/backup_manager/backup_manager.json
@@ -8,16 +8,22 @@
   {
    "fieldname": "setup", 
    "fieldtype": "Section Break", 
-   "label": "Setup", 
+   "label": "Download Backups", 
    "permlevel": 0
   }, 
   {
-   "description": "Email ids separated by commas.", 
-   "fieldname": "send_notifications_to", 
-   "fieldtype": "Data", 
-   "label": "Send Notifications To", 
+   "fieldname": "current_backups", 
+   "fieldtype": "HTML", 
+   "label": "Current Backups", 
    "permlevel": 0, 
-   "reqd": 1
+   "precision": ""
+  }, 
+  {
+   "description": "", 
+   "fieldname": "sync_with_dropbox", 
+   "fieldtype": "Section Break", 
+   "label": "Sync with Dropbox", 
+   "permlevel": 0
   }, 
   {
    "fieldname": "backup_right_now", 
@@ -28,13 +34,15 @@
    "read_only": 1
   }, 
   {
-   "description": "Note: Backups and files are not deleted from Dropbox, you will have to delete them manually.", 
-   "fieldname": "sync_with_dropbox", 
-   "fieldtype": "Section Break", 
-   "label": "Sync with Dropbox", 
-   "permlevel": 0
+   "fieldname": "send_backups_to_dropbox", 
+   "fieldtype": "Check", 
+   "label": "Send Backups to Dropbox", 
+   "permlevel": 0, 
+   "precision": ""
   }, 
   {
+   "depends_on": "send_backups_to_dropbox", 
+   "description": "Note: Backups and files are not deleted from Dropbox, you will have to delete them manually.", 
    "fieldname": "upload_backups_to_dropbox", 
    "fieldtype": "Select", 
    "label": "Upload Backups to Dropbox", 
@@ -42,6 +50,15 @@
    "permlevel": 0
   }, 
   {
+   "depends_on": "send_backups_to_dropbox", 
+   "description": "Email ids separated by commas.", 
+   "fieldname": "send_notifications_to", 
+   "fieldtype": "Data", 
+   "label": "Send Notifications To", 
+   "permlevel": 0, 
+   "reqd": 0
+  }, 
+  {
    "fieldname": "dropbox_access_key", 
    "fieldtype": "Data", 
    "hidden": 1, 
@@ -66,6 +83,7 @@
    "read_only": 1
   }, 
   {
+   "depends_on": "send_backups_to_dropbox", 
    "fieldname": "allow_dropbox_access", 
    "fieldtype": "Button", 
    "label": "Allow Dropbox Access", 
@@ -140,7 +158,7 @@
  "icon": "icon-cloud-upload", 
  "idx": 1, 
  "issingle": 1, 
- "modified": "2015-02-05 05:11:34.700674", 
+ "modified": "2015-05-26 04:54:10.193573", 
  "modified_by": "Administrator", 
  "module": "Setup", 
  "name": "Backup Manager", 
diff --git a/erpnext/setup/doctype/backup_manager/backup_manager.py b/erpnext/setup/doctype/backup_manager/backup_manager.py
index 4a38a6a..8d9d48f 100644
--- a/erpnext/setup/doctype/backup_manager/backup_manager.py
+++ b/erpnext/setup/doctype/backup_manager/backup_manager.py
@@ -4,13 +4,36 @@
 # For license information, please see license.txt
 
 from __future__ import unicode_literals
+from frappe.utils import get_site_path
+from frappe.utils.data import convert_utc_to_user_timezone
+import os
+import datetime
 import frappe
-from frappe import _
 
 from frappe.model.document import Document
 
 class BackupManager(Document):
-	pass
+	def onload(self):
+		self.set_onload("files", get_files())
+
+def get_files():
+	def get_time(path):
+		dt = os.path.getmtime(path)
+		return convert_utc_to_user_timezone(datetime.datetime.utcfromtimestamp(dt)).strftime('%Y-%m-%d %H:%M')
+
+	def get_size(path):
+		size = os.path.getsize(path)
+		if size > 1048576:
+			return "{0:.1f}M".format(float(size) / 1048576)
+		else:
+			return "{0:.1f}K".format(float(size) / 1024)
+
+	path = get_site_path('private', 'backups')
+	files = [x for x in os.listdir(path) if os.path.isfile(os.path.join(path, x))]
+	files = [('/backups/' + _file,
+		get_time(os.path.join(path, _file)),
+		get_size(os.path.join(path, _file))) for _file in files]
+	return files
 
 def take_backups_daily():
 	take_backups_if("Daily")
@@ -19,11 +42,12 @@
 	take_backups_if("Weekly")
 
 def take_backups_if(freq):
-	if frappe.db.get_value("Backup Manager", None, "upload_backups_to_dropbox")==freq:
-		take_backups_dropbox()
+	if frappe.db.get_value("Backup Manager", None, "send_backups_to_dropbox"):
+		if frappe.db.get_value("Backup Manager", None, "upload_backups_to_dropbox")==freq:
+			take_backups_dropbox()
 
-	# if frappe.db.get_value("Backup Manager", None, "upload_backups_to_gdrive")==freq:
-	# 	take_backups_gdrive()
+		# if frappe.db.get_value("Backup Manager", None, "upload_backups_to_gdrive")==freq:
+		# 	take_backups_gdrive()
 
 @frappe.whitelist()
 def take_backups_dropbox():
diff --git a/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.py b/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.py
index 1ad344a..5fba1ff 100644
--- a/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.py
+++ b/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.py
@@ -130,6 +130,9 @@
 
 	def get_price_list(self, billing_territory):
 		price_list = self.get_name_from_territory(billing_territory, "price_lists", "selling_price_list")
+		if not (price_list and price_list[0]):
+			price_list = self.get_name_from_territory(self.default_territory, "price_lists", "selling_price_list")
+
 		return price_list and price_list[0] or None
 
 	def get_tax_master(self, billing_territory):