Merge pull request #6168 from rohitwaghchaure/hotfix

[POS] Fixed payment gl entry for sales return
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 35df299..49af1c0 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -65,8 +65,8 @@
 		self.validate_fixed_asset()
 		self.set_income_account_for_fixed_assets()
 
-		# if cint(self.is_pos):
-			# self.validate_pos()
+		if cint(self.is_pos):
+			self.validate_pos()
 
 		if cint(self.update_stock):
 			self.validate_dropship_item()
@@ -219,6 +219,13 @@
 				timesheet.set_status()
 				timesheet.save()
 
+	def on_update(self):
+		self.set_paid_amount()
+
+	def set_paid_amount(self):
+		for data in self.payments:
+			data.base_amount = flt(data.amount*self.conversion_rate, self.precision("base_paid_amount"))
+
 	def validate_time_sheets_are_submitted(self):
 		for data in self.timesheets:
 			if data.time_sheet:
@@ -356,11 +363,8 @@
 				throw(_("Customer {0} does not belong to project {1}").format(self.customer,self.project))
 
 	def validate_pos(self):
-		if not self.cash_bank_account and flt(self.paid_amount):
-			frappe.throw(_("Cash or Bank Account is mandatory for making payment entry"))
-
 		if flt(self.paid_amount) + flt(self.write_off_amount) \
-				- flt(self.grand_total) > 1/(10**(self.precision("grand_total") + 1)):
+				- flt(self.grand_total) > 1/(10**(self.precision("grand_total") + 1)) and self.is_return:
 			frappe.throw(_("""Paid amount + Write Off Amount can not be greater than Grand Total"""))
 
 
@@ -588,35 +592,34 @@
 			gl_entries += super(SalesInvoice, self).get_gl_entries()
 
 	def make_pos_gl_entries(self, gl_entries):
-		if cint(self.is_pos) and self.paid_amount:
+		if cint(self.is_pos):
 			for payment_mode in self.payments:
-				if payment_mode.base_amount > 0:
-					# POS, make payment entries
-					gl_entries.append(
-						self.get_gl_dict({
-							"account": self.debit_to,
-							"party_type": "Customer",
-							"party": self.customer,
-							"against": payment_mode.account,
-							"credit": payment_mode.base_amount,
-							"credit_in_account_currency": payment_mode.base_amount \
-								if self.party_account_currency==self.company_currency \
-								else payment_mode.amount,
-							"against_voucher": self.return_against if cint(self.is_return) else self.name,
-							"against_voucher_type": self.doctype,
-						}, self.party_account_currency)
-					)
-					
-					payment_mode_account_currency = get_account_currency(payment_mode.account)
-					gl_entries.append(
-						self.get_gl_dict({
-							"account": payment_mode.account,
-							"against": self.customer,
-							"debit": payment_mode.base_amount,
-							"debit_in_account_currency": payment_mode.base_amount \
-								if payment_mode_account_currency==self.company_currency else payment_mode.amount
-						}, payment_mode_account_currency)
-					)
+				# POS, make payment entries
+				gl_entries.append(
+					self.get_gl_dict({
+						"account": self.debit_to,
+						"party_type": "Customer",
+						"party": self.customer,
+						"against": payment_mode.account,
+						"credit": payment_mode.base_amount,
+						"credit_in_account_currency": payment_mode.base_amount \
+							if self.party_account_currency==self.company_currency \
+							else payment_mode.amount,
+						"against_voucher": self.return_against if cint(self.is_return) else self.name,
+						"against_voucher_type": self.doctype,
+					}, self.party_account_currency)
+				)
+
+				payment_mode_account_currency = get_account_currency(payment_mode.account)
+				gl_entries.append(
+					self.get_gl_dict({
+						"account": payment_mode.account,
+						"against": self.customer,
+						"debit": payment_mode.base_amount,
+						"debit_in_account_currency": payment_mode.base_amount \
+							if payment_mode_account_currency==self.company_currency else payment_mode.amount
+					}, payment_mode_account_currency)
+				)
 				
 	def make_gle_for_change_amount(self, gl_entries):
 		if cint(self.is_pos) and self.change_amount:
diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py
index da803cf..30e21ce 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -420,8 +420,10 @@
 		# NOTE:
 		# write_off_amount is only for POS Invoice
 		# total_advance is only for non POS Invoice
-		if self.doc.is_return:
-			return
+		if self.doc.doctype == "Sales Invoice":
+			self.calculate_paid_amount()
+
+		if self.doc.is_return: return
 
 		self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])
 		self._set_in_company_currency(self.doc, ['write_off_amount'])
@@ -435,7 +437,6 @@
 					- flt(self.doc.base_write_off_amount), self.doc.precision("grand_total"))
 
 		if self.doc.doctype == "Sales Invoice":
-			self.calculate_paid_amount()
 			self.doc.round_floats_in(self.doc, ["paid_amount"])
 			paid_amount = self.doc.paid_amount \
 				if self.doc.party_account_currency == self.doc.currency else self.doc.base_paid_amount
@@ -452,10 +453,9 @@
 	def calculate_paid_amount(self):
 		paid_amount = base_paid_amount = 0.0
 		for payment in self.doc.get('payments'):
-			if flt(payment.amount) > 0:
-				payment.base_amount = flt(payment.amount * self.doc.conversion_rate)
-				paid_amount += payment.amount
-				base_paid_amount += payment.base_amount
+			payment.base_amount = flt(payment.amount * self.doc.conversion_rate)
+			paid_amount += payment.amount
+			base_paid_amount += payment.base_amount
 
 		self.doc.paid_amount = flt(paid_amount, self.doc.precision("paid_amount"))
 		self.doc.base_paid_amount = flt(base_paid_amount, self.doc.precision("base_paid_amount"))
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index ef9f4da..76a2409 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -310,4 +310,5 @@
 erpnext.patches.v7_0.rename_examination_to_assessment
 erpnext.patches.v7_0.set_portal_settings
 erpnext.patches.v7_0.repost_future_gle_for_purchase_invoice
-erpnext.patches.v7_0.fix_duplicate_icons
\ No newline at end of file
+erpnext.patches.v7_0.fix_duplicate_icons
+erpnext.patches.v7_0.repost_gle_for_pos_sales_return
\ No newline at end of file
diff --git a/erpnext/patches/v7_0/repost_gle_for_pos_sales_return.py b/erpnext/patches/v7_0/repost_gle_for_pos_sales_return.py
new file mode 100644
index 0000000..3d7b43c
--- /dev/null
+++ b/erpnext/patches/v7_0/repost_gle_for_pos_sales_return.py
@@ -0,0 +1,24 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+from frappe.utils import cint
+
+def execute():
+	frappe.reload_doctype("Sales Invoice")
+	
+	for si in frappe.get_all("Sales Invoice", fields = ["name"], 
+		filters={"docstatus": 1, "is_pos": 1, "is_return": 1}):
+		si_doc = frappe.get_doc("Sales Invoice", si.name)
+		if len(si_doc.payments) > 0:
+			delete_gle_for_voucher(si_doc.name)
+			si_doc.set_paid_amount()
+			si_doc.flags.ignore_validate_update_after_submit = True
+			si_doc.save()
+			si_doc.run_method("make_gl_entries")
+
+def delete_gle_for_voucher(voucher_no):
+	frappe.db.sql("""delete from `tabGL Entry` where voucher_no = %(voucher_no)s""",
+		{'voucher_no': voucher_no})
+	
\ No newline at end of file
diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js
index c3f4b70..06c2b3b 100644
--- a/erpnext/public/js/controllers/taxes_and_totals.js
+++ b/erpnext/public/js/controllers/taxes_and_totals.js
@@ -532,6 +532,11 @@
 		// NOTE:
 		// paid_amount and write_off_amount is only for POS Invoice
 		// total_advance is only for non POS Invoice
+		
+		if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.is_return){
+			this.calculate_paid_amount()
+		}
+
 		if(this.frm.doc.is_return || this.frm.doc.docstatus > 0) return;
 
 		frappe.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount"]);
@@ -594,11 +599,9 @@
 		var me = this;
 		var paid_amount = base_paid_amount = 0.0;
 		$.each(this.frm.doc['payments'] || [], function(index, data){
-			if(data.amount > -1){
-				data.base_amount = flt(data.amount * me.frm.doc.conversion_rate);
-				paid_amount += data.amount;
-				base_paid_amount += data.base_amount;
-			}
+			data.base_amount = flt(data.amount * me.frm.doc.conversion_rate);
+			paid_amount += data.amount;
+			base_paid_amount += data.base_amount;
 		})
 
 		this.frm.doc.paid_amount = flt(paid_amount, precision("paid_amount"));
@@ -607,7 +610,7 @@
 
 	calculate_change_amount: function(){
 		this.frm.doc.change_amount = 0.0;
-		if(this.frm.doc.paid_amount > this.frm.doc.grand_total){
+		if(this.frm.doc.paid_amount > this.frm.doc.grand_total && !this.frm.doc.is_return){
 			this.frm.doc.change_amount = flt(this.frm.doc.paid_amount - this.frm.doc.grand_total + 
 				this.frm.doc.write_off_amount, precision("change_amount"));
 		}