Merge pull request #3744 from nabinhait/return

Sales / Purchase Return Improvements
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
index 3e4522c..8e91250 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -20,27 +20,19 @@
 		this._super();
 
 		// Show / Hide button
-		if(doc.docstatus==1 && doc.outstanding_amount > 0)
-			this.frm.add_custom_button(__('Make Payment Entry'), this.make_bank_entry);
-
-		if(doc.docstatus==1) {
-			cur_frm.add_custom_button(__('Make Purchase Return'), this.make_purchase_return);
+		this.show_general_ledger();
+		
+		if(!doc.is_return) {
+			if(doc.docstatus==1) {
+				if(doc.outstanding_amount > 0) {
+					this.frm.add_custom_button(__('Make Payment Entry'), this.make_bank_entry);
+				}
+				
+				cur_frm.add_custom_button(__('Make Debit Note'), this.make_debit_note);
+			}
 			
-			cur_frm.add_custom_button(__('View Ledger'), function() {
-				frappe.route_options = {
-					"voucher_no": doc.name,
-					"from_date": doc.posting_date,
-					"to_date": doc.posting_date,
-					"company": doc.company,
-					group_by_voucher: 0
-				};
-				frappe.set_route("query-report", "General Ledger");
-			});
-		}
-
-		if(doc.docstatus===0) {
-			cur_frm.add_custom_button(__('From Purchase Order'),
-				function() {
+			if(doc.docstatus===0) {
+				cur_frm.add_custom_button(__('From Purchase Order'), function() {
 					frappe.model.map_current_doc({
 						method: "erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_invoice",
 						source_doctype: "Purchase Order",
@@ -54,8 +46,7 @@
 					})
 				});
 
-			cur_frm.add_custom_button(__('From Purchase Receipt'),
-				function() {
+				cur_frm.add_custom_button(__('From Purchase Receipt'), function() {
 					frappe.model.map_current_doc({
 						method: "erpnext.stock.doctype.purchase_receipt.purchase_receipt.make_purchase_invoice",
 						source_doctype: "Purchase Receipt",
@@ -66,7 +57,7 @@
 						}
 					})
 				});
-
+			}
 		}
 	},
 
@@ -112,9 +103,9 @@
 		})
 	},
 	
-	make_purchase_return: function() {
+	make_debit_note: function() {
 		frappe.model.open_mapped_doc({
-			method: "erpnext.accounts.doctype.purchase_invoice.purchase_invoice.make_purchase_return",
+			method: "erpnext.accounts.doctype.purchase_invoice.purchase_invoice.make_debit_note",
 			frm: cur_frm
 		})
 	},
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index cc77394..00ea6ae 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -255,7 +255,7 @@
 					"against": self.against_expense_account,
 					"credit": self.total_amount_to_pay,
 					"remarks": self.remarks,
-					"against_voucher": self.name,
+					"against_voucher": self.return_against if cint(self.is_return) else self.name,
 					"against_voucher_type": self.doctype,
 				})
 			)
@@ -411,6 +411,6 @@
 			'txt': "%%%s%%" % frappe.db.escape(txt), 'mcond':get_match_cond(doctype)})
 
 @frappe.whitelist()
-def make_purchase_return(source_name, target_doc=None):
+def make_debit_note(source_name, target_doc=None):
 	from erpnext.controllers.sales_and_purchase_return import make_return_doc
-	return make_return_doc("Purchase Invoice", source_name, target_doc)	
\ No newline at end of file
+	return make_return_doc("Purchase Invoice", source_name, target_doc)
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice_list.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice_list.js
index dfb8329..ccad5ce 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice_list.js
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice_list.js
@@ -4,9 +4,11 @@
 // render
 frappe.listview_settings['Purchase Invoice'] = {
 	add_fields: ["supplier", "supplier_name", "base_grand_total", "outstanding_amount", "due_date", "company",
-		"currency"],
+		"currency", "is_return"],
 	get_indicator: function(doc) {
-		if(flt(doc.outstanding_amount) > 0 && doc.docstatus==1) {
+		if(cint(doc.is_return)==1) {
+			return [__("Return"), "darkgrey", "is_return,=,1"];
+		} else if(flt(doc.outstanding_amount) > 0 && doc.docstatus==1) {
 			if(frappe.datetime.get_diff(doc.due_date) < 0) {
 				return [__("Overdue"), "red", "outstanding_amount,>,0|due_date,<,Today"];
 			} else {
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index fdc1a58..017ab3a 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -43,18 +43,11 @@
 		
 		this.frm.toggle_reqd("due_date", !this.frm.doc.is_return);
 		
-		if(doc.docstatus==1) {
-			cur_frm.add_custom_button('View Ledger', function() {
-				frappe.route_options = {
-					"voucher_no": doc.name,
-					"from_date": doc.posting_date,
-					"to_date": doc.posting_date,
-					"company": doc.company,
-					group_by_voucher: 0
-				};
-				frappe.set_route("query-report", "General Ledger");
-			});
-
+		this.show_general_ledger();
+		
+		if(doc.update_stock) this.show_stock_ledger();
+		
+		if(doc.docstatus==1 && !doc.is_return) {
 			if(cint(doc.update_stock)!=1) {
 				// show Make Delivery Note button only if Sales Invoice is not created from Delivery Note
 				var from_delivery_note = false;
@@ -72,11 +65,12 @@
 				cur_frm.add_custom_button(__('Make Payment Entry'), cur_frm.cscript.make_bank_entry);
 			}
 			
-			cur_frm.add_custom_button(__('Make Sales Return'), this.make_sales_return);
+			cur_frm.add_custom_button(doc.update_stock ? __('Make Sales Return') : __('Make Credit Note'), 
+				this.make_sales_return);
 		}
 
 		// Show buttons only when pos view is active
-		if (cint(doc.docstatus==0) && cur_frm.page.current_view_name!=="pos") {
+		if (cint(doc.docstatus==0) && cur_frm.page.current_view_name!=="pos" && !doc.is_return) {
 			cur_frm.cscript.sales_order_btn();
 			cur_frm.cscript.delivery_note_btn();
 		}
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice_list.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice_list.js
index 6d152f8..11c9789 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice_list.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice_list.js
@@ -4,9 +4,11 @@
 // render
 frappe.listview_settings['Sales Invoice'] = {
 	add_fields: ["customer", "customer_name", "base_grand_total", "outstanding_amount", "due_date", "company",
-		"currency"],
+		"currency", "is_return"],
 	get_indicator: function(doc) {
-		if(flt(doc.outstanding_amount)==0) {
+		if(cint(doc.is_return)==1) {
+			return [__("Return"), "darkgrey", "is_return,=,1"];
+		} else if(flt(doc.outstanding_amount)==0) {
 			return [__("Paid"), "green", "outstanding_amount,=,0"]
 		} else if (flt(doc.outstanding_amount) > 0 && doc.due_date > frappe.datetime.get_today()) {
 			return [__("Unpaid"), "orange", "outstanding_amount,>,0|due_date,>,Today"]
diff --git a/erpnext/controllers/sales_and_purchase_return.py b/erpnext/controllers/sales_and_purchase_return.py
index 899d1c1..de4d280 100644
--- a/erpnext/controllers/sales_and_purchase_return.py
+++ b/erpnext/controllers/sales_and_purchase_return.py
@@ -107,6 +107,8 @@
 		doc.is_return = 1
 		doc.return_against = source.name
 		doc.ignore_pricing_rule = 1
+		if doctype == "Sales Invoice":
+			doc.is_pos = 0
 		doc.run_method("calculate_taxes_and_totals")
 
 	def update_item(source_doc, target_doc, source_parent):
diff --git a/erpnext/public/js/controllers/stock_controller.js b/erpnext/public/js/controllers/stock_controller.js
index c5cad95..f5f7aa0 100644
--- a/erpnext/public/js/controllers/stock_controller.js
+++ b/erpnext/public/js/controllers/stock_controller.js
@@ -58,7 +58,7 @@
 
 	show_general_ledger: function() {
 		var me = this;
-		if(this.frm.doc.docstatus===1 && cint(frappe.defaults.get_default("auto_accounting_for_stock"))) {
+		if(this.frm.doc.docstatus===1) {
 			cur_frm.add_custom_button(__('Accounting Ledger'), function() {
 				frappe.route_options = {
 					voucher_no: me.frm.doc.name,
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.js b/erpnext/stock/doctype/delivery_note/delivery_note.js
index 794d6fd..94356da 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.js
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.js
@@ -8,57 +8,64 @@
 erpnext.stock.DeliveryNoteController = erpnext.selling.SellingController.extend({
 	refresh: function(doc, dt, dn) {
 		this._super();
+		
+		if (!doc.is_return) {
+			if(doc.__onload && !doc.__onload.billing_complete && doc.docstatus==1) {
+				// show Make Invoice button only if Delivery Note is not created from Sales Invoice
+				var from_sales_invoice = false;
+				from_sales_invoice = cur_frm.doc.items.some(function(item) {
+						return item.against_sales_invoice ? true : false;
+					});
 
-		if(doc.__onload && !doc.__onload.billing_complete && doc.docstatus==1) {
-			// show Make Invoice button only if Delivery Note is not created from Sales Invoice
-			var from_sales_invoice = false;
-			from_sales_invoice = cur_frm.doc.items.some(function(item) {
-					return item.against_sales_invoice ? true : false;
-				});
+				if(!from_sales_invoice)
+					cur_frm.add_custom_button(__('Make Invoice'), this.make_sales_invoice);
+			}
 
-			if(!from_sales_invoice)
-				cur_frm.add_custom_button(__('Make Invoice'), this.make_sales_invoice);
+			if(flt(doc.per_installed, 2) < 100 && doc.docstatus==1)
+				cur_frm.add_custom_button(__('Make Installation Note'), this.make_installation_note);
+
+			if (doc.docstatus==1) {
+				cur_frm.add_custom_button(__('Make Sales Return'), this.make_sales_return);
+			}
+
+			if(doc.docstatus==0 && !doc.__islocal) {
+				cur_frm.add_custom_button(__('Make Packing Slip'),
+					cur_frm.cscript['Make Packing Slip'], frappe.boot.doctype_icons["Packing Slip"]);
+			}
+
+			if (this.frm.doc.docstatus===0) {
+				cur_frm.add_custom_button(__('From Sales Order'),
+					function() {
+						frappe.model.map_current_doc({
+							method: "erpnext.selling.doctype.sales_order.sales_order.make_delivery_note",
+							source_doctype: "Sales Order",
+							get_query_filters: {
+								docstatus: 1,
+								status: ["!=", "Stopped"],
+								per_delivered: ["<", 99.99],
+								project_name: cur_frm.doc.project_name || undefined,
+								customer: cur_frm.doc.customer || undefined,
+								company: cur_frm.doc.company
+							}
+						})
+					});
+			}
 		}
-
-		if(flt(doc.per_installed, 2) < 100 && doc.docstatus==1)
-			cur_frm.add_custom_button(__('Make Installation Note'), this.make_installation_note);
-
+		
 		if (doc.docstatus==1) {
-			cur_frm.add_custom_button(__('Make Sales Return'), this.make_sales_return);
-				
 			this.show_stock_ledger();
-			this.show_general_ledger();
+			if (cint(frappe.defaults.get_default("auto_accounting_for_stock"))) {
+				this.show_general_ledger();
+			}
 		}
-
-		if(doc.docstatus==0 && !doc.__islocal) {
-			cur_frm.add_custom_button(__('Make Packing Slip'),
-				cur_frm.cscript['Make Packing Slip'], frappe.boot.doctype_icons["Packing Slip"]);
-		}
+		
+			
 
 		erpnext.stock.delivery_note.set_print_hide(doc, dt, dn);
 
 		// unhide expense_account and cost_center is auto_accounting_for_stock enabled
 		var aii_enabled = cint(sys_defaults.auto_accounting_for_stock)
 		cur_frm.fields_dict["items"].grid.set_column_disp(["expense_account", "cost_center"], aii_enabled);
-
-		if (this.frm.doc.docstatus===0) {
-			cur_frm.add_custom_button(__('From Sales Order'),
-				function() {
-					frappe.model.map_current_doc({
-						method: "erpnext.selling.doctype.sales_order.sales_order.make_delivery_note",
-						source_doctype: "Sales Order",
-						get_query_filters: {
-							docstatus: 1,
-							status: ["!=", "Stopped"],
-							per_delivered: ["<", 99.99],
-							project_name: cur_frm.doc.project_name || undefined,
-							customer: cur_frm.doc.customer || undefined,
-							company: cur_frm.doc.company
-						}
-					})
-				});
-		}
-
 	},
 
 	make_sales_invoice: function() {
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note_list.js b/erpnext/stock/doctype/delivery_note/delivery_note_list.js
index d30dec9..dce6863 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note_list.js
+++ b/erpnext/stock/doctype/delivery_note/delivery_note_list.js
@@ -1,4 +1,9 @@
 frappe.listview_settings['Delivery Note'] = {
 	add_fields: ["customer", "customer_name", "base_grand_total", "per_installed",
-		"transporter_name", "grand_total"]
+		"transporter_name", "grand_total", "is_return"],
+	get_indicator: function(doc) {
+		if(cint(doc.is_return)==1) {
+			return [__("Return"), "darkgrey", "is_return,=,1"];
+		}
+	}
 };
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
index 13e104e..fcaf9f8 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
@@ -29,32 +29,37 @@
 	refresh: function() {
 		this._super();
 
-		if(this.frm.doc.docstatus == 1) {
-			if(this.frm.doc.__onload && !this.frm.doc.__onload.billing_complete) {
-				cur_frm.add_custom_button(__('Make Purchase Invoice'), this.make_purchase_invoice);
+		this.show_stock_ledger();
+		if (cint(frappe.defaults.get_default("auto_accounting_for_stock"))) {
+			this.show_general_ledger();
+		}
+		if(!this.frm.doc.is_return) {
+			if(this.frm.doc.docstatus==0) {
+				cur_frm.add_custom_button(__('From Purchase Order'),
+					function() {
+						frappe.model.map_current_doc({
+							method: "erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_receipt",
+							source_doctype: "Purchase Order",
+							get_query_filters: {
+								supplier: cur_frm.doc.supplier || undefined,
+								docstatus: 1,
+								status: ["!=", "Stopped"],
+								per_received: ["<", 99.99],
+								company: cur_frm.doc.company
+							}
+						})
+				});
 			}
 			
-			cur_frm.add_custom_button(__('Make Purchase Return'), this.make_purchase_return);
-
-			this.show_stock_ledger();
-			this.show_general_ledger();
-		} else {
-			cur_frm.add_custom_button(__('From Purchase Order'),
-				function() {
-					frappe.model.map_current_doc({
-						method: "erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_receipt",
-						source_doctype: "Purchase Order",
-						get_query_filters: {
-							supplier: cur_frm.doc.supplier || undefined,
-							docstatus: 1,
-							status: ["!=", "Stopped"],
-							per_received: ["<", 99.99],
-							company: cur_frm.doc.company
-						}
-					})
-				});
+			if(this.frm.doc.docstatus == 1) {
+				if(this.frm.doc.__onload && !this.frm.doc.__onload.billing_complete) {
+					cur_frm.add_custom_button(__('Make Purchase Invoice'), this.make_purchase_invoice);
+				}
+			
+				cur_frm.add_custom_button(__('Make Purchase Return'), this.make_purchase_return);
+			}
 		}
-
+		
 		this.frm.toggle_reqd("supplier_warehouse", this.frm.doc.is_subcontracted==="Yes");
 	},
 
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt_list.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt_list.js
index b0559ce..64d5456 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt_list.js
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt_list.js
@@ -1,4 +1,9 @@
 frappe.listview_settings['Purchase Receipt'] = {
 	add_fields: ["supplier", "supplier_name", "base_grand_total", "is_subcontracted",
-		"transporter_name"]
+		"transporter_name", "is_return"],
+	get_indicator: function(doc) {
+		if(cint(doc.is_return)==1) {
+			return [__("Return"), "darkgrey", "is_return,=,1"];
+		}
+	}
 };
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js
index 5da6e53..d5bdde7 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.js
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.js
@@ -57,7 +57,9 @@
 		this.toggle_related_fields(this.frm.doc);
 		this.toggle_enable_bom();
 		this.show_stock_ledger();
-		this.show_general_ledger();
+		if (cint(frappe.defaults.get_default("auto_accounting_for_stock"))) {
+			this.show_general_ledger();
+		}
 	},
 
 	on_submit: function() {
diff --git a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.js b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.js
index f833a25..4b67c0a 100644
--- a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.js
+++ b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.js
@@ -83,7 +83,9 @@
 	refresh: function() {
 		if(this.frm.doc.docstatus==1) {
 			this.show_stock_ledger();
-			this.show_general_ledger();
+			if (cint(frappe.defaults.get_default("auto_accounting_for_stock"))) {
+				this.show_general_ledger();
+			}
 		}
 	},