Merge pull request #5327 from nabinhait/pp_tool

[fix] Create Material Requests for All Required Qty via Production Planning Tool
diff --git a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js
index 941aa87..bad826e 100644
--- a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js
+++ b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js
@@ -34,6 +34,12 @@
 			"fieldtype": "Link",
 			"options": "Company",
 			"default": frappe.defaults.get_user_default("Company")
+		},
+		{
+			"fieldname":"mode_of_payment",
+			"label": __("Mode of Payment"),
+			"fieldtype": "Link",
+			"options": "Mode of Payment"
 		}
 	]
 }
diff --git a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
index 9e7cdb6..d6bbee5 100644
--- a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
+++ b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
@@ -35,7 +35,7 @@
 
 		expense_account = d.expense_account or aii_account_map.get(d.company)
 		row = [d.item_code, d.item_name, d.item_group, d.parent, d.posting_date, d.supplier,
-			d.supplier_name, d.credit_to, d.project, d.company, d.purchase_order,
+			d.supplier_name, d.credit_to, d.mode_of_payment, d.project, d.company, d.purchase_order,
 			purchase_receipt, expense_account, d.qty, d.base_net_rate, d.base_net_amount]
 
 		for tax in tax_accounts:
@@ -53,7 +53,8 @@
 	return [_("Item Code") + ":Link/Item:120", _("Item Name") + "::120",
 		_("Item Group") + ":Link/Item Group:100", _("Invoice") + ":Link/Purchase Invoice:120",
 		_("Posting Date") + ":Date:80", _("Supplier") + ":Link/Supplier:120",
-		"Supplier Name::120", "Payable Account:Link/Account:120", _("Project") + ":Link/Project:80",
+		"Supplier Name::120", "Payable Account:Link/Account:120", 
+		_("Mode of Payment") + ":Link/Mode of Payment:80", _("Project") + ":Link/Project:80",
 		_("Company") + ":Link/Company:100", _("Purchase Order") + ":Link/Purchase Order:100",
 		_("Purchase Receipt") + ":Link/Purchase Receipt:100", _("Expense Account") + ":Link/Account:140",
 		_("Qty") + ":Float:120", _("Rate") + ":Currency/currency:120", _("Amount") + ":Currency/currency:120"
@@ -66,7 +67,8 @@
 		("supplier", " and pi.supplier = %(supplier)s"),
 		("item_code", " and pi_item.item_code = %(item_code)s"),
 		("from_date", " and pi.posting_date>=%(from_date)s"),
-		("to_date", " and pi.posting_date<=%(to_date)s")):
+		("to_date", " and pi.posting_date<=%(to_date)s"),
+		("mode_of_payment", " and ifnull(mode_of_payment, '') = %(mode_of_payment)s")):
 			if filters.get(opts[0]):
 				conditions += opts[1]
 
@@ -82,7 +84,7 @@
 			pi.supplier, pi.remarks, pi.base_net_total, pi_item.item_code, pi_item.item_name, 
 			pi_item.item_group, pi_item.project, pi_item.purchase_order, pi_item.purchase_receipt, 
 			pi_item.po_detail, pi_item.expense_account, pi_item.qty, pi_item.base_net_rate, 
-			pi_item.base_net_amount, pi.supplier_name
+			pi_item.base_net_amount, pi.supplier_name, pi.mode_of_payment
 		from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` pi_item
 		where pi.name = pi_item.parent and pi.docstatus = 1 %s %s
 		order by pi.posting_date desc, pi_item.item_code desc
diff --git a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.js b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.js
index d322406..142a55f 100644
--- a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.js
+++ b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.js
@@ -28,6 +28,12 @@
 			"fieldtype": "Link",
 			"options": "Company",
 			"default": frappe.defaults.get_user_default("Company")
+		},
+		{
+			"fieldname":"mode_of_payment",
+			"label": __("Mode of Payment"),
+			"fieldtype": "Link",
+			"options": "Mode of Payment"
 		}
 	]
 }
diff --git a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
index beca96e..6fc7349 100644
--- a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
+++ b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
@@ -32,7 +32,7 @@
 			from `tabDelivery Note Item` where docstatus=1 and so_detail=%s""", d.so_detail))
 
 		row = [d.item_code, d.item_name, d.item_group, d.parent, d.posting_date, d.customer, d.customer_name,
-			d.customer_group, d.debit_to, d.territory, d.project, d.company, d.sales_order,
+			d.customer_group, d.debit_to, d.mode_of_payment, d.territory, d.project, d.company, d.sales_order,
 			delivery_note, d.income_account, d.qty, d.base_net_rate, d.base_net_amount]
 
 		for tax in tax_accounts:
@@ -51,7 +51,8 @@
 		_("Item Group") + ":Link/Item Group:100", _("Invoice") + ":Link/Sales Invoice:120",
 		_("Posting Date") + ":Date:80", _("Customer") + ":Link/Customer:120",
 		_("Customer Name") + "::120", _("Customer Group") + ":Link/Customer Group:120",
-		_("Receivable Account") + ":Link/Account:120", _("Territory") + ":Link/Territory:80",
+		_("Receivable Account") + ":Link/Account:120", 
+		_("Mode of Payment") + ":Link/Mode of Payment:80", _("Territory") + ":Link/Territory:80", 
 		_("Project") + ":Link/Project:80", _("Company") + ":Link/Company:100",
 		_("Sales Order") + ":Link/Sales Order:100", _("Delivery Note") + ":Link/Delivery Note:100",
 		_("Income Account") + ":Link/Account:140", _("Qty") + ":Float:120",
@@ -65,7 +66,8 @@
 		("customer", " and si.customer = %(customer)s"),
 		("item_code", " and si_item.item_code = %(item_code)s"),
 		("from_date", " and si.posting_date>=%(from_date)s"),
-		("to_date", " and si.posting_date<=%(to_date)s")):
+		("to_date", " and si.posting_date<=%(to_date)s"),
+		("mode_of_payment", " and ifnull(mode_of_payment, '') = %(mode_of_payment)s")):
 			if filters.get(opts[0]):
 				conditions += opts[1]
 
@@ -80,7 +82,7 @@
 			si_item.item_code, si_item.item_name, si_item.item_group, si_item.sales_order, 
 			si_item.delivery_note, si_item.income_account, si_item.qty, 
 			si_item.base_net_rate, si_item.base_net_amount, si.customer_name,
-			si.customer_group, si_item.so_detail
+			si.customer_group, si_item.so_detail, si.mode_of_payment
 		from `tabSales Invoice` si, `tabSales Invoice Item` si_item
 		where si.name = si_item.parent and si.docstatus = 1 %s
 		order by si.posting_date desc, si_item.item_code desc""" % conditions, filters, as_dict=1)
diff --git a/erpnext/accounts/report/purchase_register/purchase_register.js b/erpnext/accounts/report/purchase_register/purchase_register.js
index 15300c3..d32f7b6 100644
--- a/erpnext/accounts/report/purchase_register/purchase_register.js
+++ b/erpnext/accounts/report/purchase_register/purchase_register.js
@@ -28,6 +28,12 @@
 			"fieldtype": "Link",
 			"options": "Company",
 			"default": frappe.defaults.get_user_default("Company")
+		},
+		{
+			"fieldname":"mode_of_payment",
+			"label": __("Mode of Payment"),
+			"fieldtype": "Link",
+			"options": "Mode of Payment"
 		}
 	]
 }
diff --git a/erpnext/accounts/report/purchase_register/purchase_register.py b/erpnext/accounts/report/purchase_register/purchase_register.py
index 53cb7af66..9a90f2f 100644
--- a/erpnext/accounts/report/purchase_register/purchase_register.py
+++ b/erpnext/accounts/report/purchase_register/purchase_register.py
@@ -33,7 +33,7 @@
 
 		row = [inv.name, inv.posting_date, inv.supplier, inv.supplier_name,
 			supplier_details.get(inv.supplier),
-			inv.credit_to, ", ".join(project), inv.bill_no, inv.bill_date, inv.remarks,
+			inv.credit_to, inv.mode_of_payment, ", ".join(project), inv.bill_no, inv.bill_date, inv.remarks,
 			", ".join(purchase_order), ", ".join(purchase_receipt), company_currency]
 
 		# map expense values
@@ -64,10 +64,10 @@
 def get_columns(invoice_list):
 	"""return columns based on filters"""
 	columns = [
-		_("Invoice") + ":Link/Purchase Invoice:120", 
-		_("Posting Date") + ":Date:80", _("Supplier Id") + "::120",
-		_("Supplier Name") + "::120", _("Supplier Type") + ":Link/Supplier Type:120", 
-		_("Payable Account") + ":Link/Account:120", _("Project") + ":Link/Project:80", 
+		_("Invoice") + ":Link/Purchase Invoice:120", _("Posting Date") + ":Date:80", 
+		_("Supplier Id") + "::120", _("Supplier Name") + "::120", 
+		_("Supplier Type") + ":Link/Supplier Type:120", _("Payable Account") + ":Link/Account:120", 
+		_("Mode of Payment") + ":Link/Mode of Payment:80", _("Project") + ":Link/Project:80", 
 		_("Bill No") + "::120", _("Bill Date") + ":Date:80", _("Remarks") + "::150",
 		_("Purchase Order") + ":Link/Purchase Order:100", 
 		_("Purchase Receipt") + ":Link/Purchase Receipt:100",
@@ -114,6 +114,8 @@
 
 	if filters.get("from_date"): conditions += " and posting_date>=%(from_date)s"
 	if filters.get("to_date"): conditions += " and posting_date<=%(to_date)s"
+	
+	if filters.get("mode_of_payment"): conditions += " and ifnull(mode_of_payment, '') = %(mode_of_payment)s"
 
 	return conditions
 
@@ -121,8 +123,8 @@
 	conditions = get_conditions(filters)
 	return frappe.db.sql("""
 		select 
-			name, posting_date, credit_to, supplier, supplier_name,
-			bill_no, bill_date, remarks, base_net_total, base_grand_total, outstanding_amount
+			name, posting_date, credit_to, supplier, supplier_name, bill_no, bill_date, remarks, 
+			base_net_total, base_grand_total, outstanding_amount, mode_of_payment
 		from `tabPurchase Invoice` 
 		where docstatus = 1 %s
 		order by posting_date desc, name desc""" % conditions, filters, as_dict=1)
diff --git a/erpnext/accounts/report/sales_register/sales_register.js b/erpnext/accounts/report/sales_register/sales_register.js
index 4764255..d7aac5a 100644
--- a/erpnext/accounts/report/sales_register/sales_register.js
+++ b/erpnext/accounts/report/sales_register/sales_register.js
@@ -28,6 +28,12 @@
 			"fieldtype": "Link",
 			"options": "Company",
 			"default": frappe.defaults.get_user_default("Company")
+		},
+		{
+			"fieldname":"mode_of_payment",
+			"label": __("Mode of Payment"),
+			"fieldtype": "Link",
+			"options": "Mode of Payment"
 		}
 	]
 }
diff --git a/erpnext/accounts/report/sales_register/sales_register.py b/erpnext/accounts/report/sales_register/sales_register.py
index d09cfc1..663c5c9 100644
--- a/erpnext/accounts/report/sales_register/sales_register.py
+++ b/erpnext/accounts/report/sales_register/sales_register.py
@@ -33,7 +33,7 @@
 		row = [inv.name, inv.posting_date, inv.customer, inv.customer_name,
 		customer_map.get(inv.customer, {}).get("customer_group"), 
 		customer_map.get(inv.customer, {}).get("territory"),
-		inv.debit_to, inv.project, inv.remarks, 
+		inv.debit_to, inv.mode_of_payment, inv.project, inv.remarks, 
 		", ".join(sales_order), ", ".join(delivery_note), company_currency]
 
 		# map income values
@@ -65,9 +65,11 @@
 def get_columns(invoice_list):
 	"""return columns based on filters"""
 	columns = [
-		_("Invoice") + ":Link/Sales Invoice:120", _("Posting Date") + ":Date:80", _("Customer Id") + "::120",
-		_("Customer Name") + "::120", _("Customer Group") + ":Link/Customer Group:120", _("Territory") + ":Link/Territory:80",
-		_("Receivable Account") + ":Link/Account:120", _("Project") +":Link/Project:80", _("Remarks") + "::150",
+		_("Invoice") + ":Link/Sales Invoice:120", _("Posting Date") + ":Date:80", 
+		_("Customer Id") + "::120", _("Customer Name") + "::120", 
+		_("Customer Group") + ":Link/Customer Group:120", _("Territory") + ":Link/Territory:80", 
+		_("Receivable Account") + ":Link/Account:120", _("Mode of Payment") + ":Link/Mode of Payment:80", 
+		_("Project") +":Link/Project:80", _("Remarks") + "::150", 
 		_("Sales Order") + ":Link/Sales Order:100", _("Delivery Note") + ":Link/Delivery Note:100",
 		{
 			"fieldname": "currency",
@@ -110,13 +112,15 @@
 
 	if filters.get("from_date"): conditions += " and posting_date >= %(from_date)s"
 	if filters.get("to_date"): conditions += " and posting_date <= %(to_date)s"
+	
+	if filters.get("mode_of_payment"): conditions += " and ifnull(mode_of_payment, '') = %(mode_of_payment)s"
 
 	return conditions
 
 def get_invoices(filters):
 	conditions = get_conditions(filters)
-	return frappe.db.sql("""select name, posting_date, debit_to, project, customer,
-		customer_name, remarks, base_net_total, base_grand_total, base_rounded_total, outstanding_amount
+	return frappe.db.sql("""select name, posting_date, debit_to, project, customer, customer_name, remarks, 
+		base_net_total, base_grand_total, base_rounded_total, outstanding_amount, mode_of_payment
 		from `tabSales Invoice`
 		where docstatus = 1 %s order by posting_date desc, name desc""" %
 		conditions, filters, as_dict=1)