fix: Quantity while creating pick list from work order

- Fix get_mapped_doc config to filter items
- Fix erroneous condition in get_mapped_doc used for creation of pick_list
in sales order

Co-authored-by: Nabin Hait <nabinhait@gmail.com>
diff --git a/erpnext/manufacturing/doctype/work_order/work_order.py b/erpnext/manufacturing/doctype/work_order/work_order.py
index c489fbc..c653dee 100644
--- a/erpnext/manufacturing/doctype/work_order/work_order.py
+++ b/erpnext/manufacturing/doctype/work_order/work_order.py
@@ -711,6 +711,14 @@
 
 @frappe.whitelist()
 def make_pick_list(source_name, target_doc=None):
+	def update_item_quantity(source, target, source_parent):
+		qty = source.required_qty - source.transferred_qty
+		target.qty = qty
+		target.stock_qty = qty
+		target.uom = frappe.get_value('Item', source.item_code, 'stock_uom')
+		target.stock_uom = target.uom
+		target.conversion_factor = 1
+
 	doc = get_mapped_doc("Work Order", source_name, {
 		"Work Order": {
 			"doctype": "Pick List",
@@ -720,13 +728,8 @@
 		},
 		"Work Order Item": {
 			"doctype": "Pick List Reference Item",
-			"field_map": {
-				"item_code": "item",
-				"required_qty": "qty",
-				"parenttype": "reference_doctype",
-				"parent": "reference_name",
-				"name": "reference_document_item"
-			},
+			"postprocess": update_item_quantity,
+			"condition": lambda doc: abs(doc.transferred_qty) < abs(doc.required_qty)
 		},
 	}, target_doc)
 
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index 2dc3788..dd156d0 100755
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -1024,7 +1024,7 @@
 				"name": "sales_order_item"
 			},
 			"postprocess": update_item_quantity,
-			"conditions": lambda doc: abs(doc.delivered_qty) < abs(doc.qty) and doc.delivered_by_supplier!=1
+			"condition": lambda doc: abs(doc.delivered_qty) < abs(doc.qty) and doc.delivered_by_supplier!=1
 		},
 	}, target_doc)
 
diff --git a/erpnext/stock/doctype/pick_list/pick_list.js b/erpnext/stock/doctype/pick_list/pick_list.js
index 2e72d98..b9b317b 100644
--- a/erpnext/stock/doctype/pick_list/pick_list.js
+++ b/erpnext/stock/doctype/pick_list/pick_list.js
@@ -28,17 +28,35 @@
 	},
 	make_delivery_note(frm) {
 		frappe.model.open_mapped_doc({
-			method: "erpnext.stock.doctype.pick_list.pick_list.make_delivery_note",
+			method: 'erpnext.stock.doctype.pick_list.pick_list.make_delivery_note',
 			frm: frm
 		});
 	},
 	add_get_items_button(frm) {
 		frm.remove_custom_button(__("Get items"));
 		let source_doctype = frm.doc.items_based_on;
+		let date_field = 'transaction_date';
+		let get_query_method = null;
+		let get_query_filters = {
+			docstatus: 1,
+			per_delivered: ['<', 100],
+			status: ['!=', '']
+		};
 		let method = 'erpnext.selling.doctype.sales_order.sales_order.make_pick_list';
 		if (source_doctype === 'Work Order') {
 			method = 'erpnext.manufacturing.doctype.work_order.work_order.make_pick_list';
+			date_field = 'planned_start_date';
+			get_query_method = 'erpnext.stock.doctype.pick_list.pick_list.get_pending_work_orders';
+			get_query_filters = null;
 		}
+
+		let get_query = () => {
+			return {
+				'query': get_query_method,
+				'filters': get_query_filters
+			};
+		};
+
 		frm.add_custom_button(__("Get items"), () => {
 			erpnext.utils.map_current_doc({
 				method: method,
@@ -47,10 +65,8 @@
 				setters: {
 					company: frm.doc.company,
 				},
-				date_field: 'creation',
-				get_query_filters: {
-					docstatus: 1,
-				}
+				date_field: date_field,
+				get_query: get_query
 			});
 		});
 	}
diff --git a/erpnext/stock/doctype/pick_list/pick_list.json b/erpnext/stock/doctype/pick_list/pick_list.json
index c7a5fc8..70272bf 100644
--- a/erpnext/stock/doctype/pick_list/pick_list.json
+++ b/erpnext/stock/doctype/pick_list/pick_list.json
@@ -57,10 +57,10 @@
    "fieldname": "items_based_on",
    "fieldtype": "Select",
    "label": "Items Based On",
-   "options": "\nSales Order\nWork Order"
+   "options": "Sales Order\nWork Order"
   }
  ],
- "modified": "2019-08-13 19:30:01.151720",
+ "modified": "2019-08-19 12:31:54.023456",
  "modified_by": "Administrator",
  "module": "Stock",
  "name": "Pick List",
diff --git a/erpnext/stock/doctype/pick_list/pick_list.py b/erpnext/stock/doctype/pick_list/pick_list.py
index 20b4230..91bdcb3 100644
--- a/erpnext/stock/doctype/pick_list/pick_list.py
+++ b/erpnext/stock/doctype/pick_list/pick_list.py
@@ -10,6 +10,8 @@
 from frappe.utils import floor, flt, today
 from erpnext.selling.doctype.sales_order.sales_order import make_delivery_note as make_delivery_note_from_sales_order
 
+# TODO: Prioritize SO or WO group warehouse
+
 class PickList(Document):
 	def set_item_locations(self):
 		reference_items = self.reference_items
@@ -154,36 +156,6 @@
 		if required_qty <= 0:
 			break
 
-	# required_qty = item_doc.qty
-	# while required_qty > 0 and batches:
-	# 	batch = batches.pop()
-	# 	batch_expiry = frappe.get_value('Batch', batch.batch_no, 'expiry_date')
-	# 	if batch_expiry and batch_expiry <= frappe.utils.getdate():
-	# 		frappe.msgprint('Skipping expired Batch {}'.format(batch.batch_no))
-	# 		continue
-	# 	item_doc.batch_no = batch.batch_no
-	# 	if batch.qty >= item_doc.qty:
-	# 		required_qty = 0
-	# 		break
-	# 	else:
-	# 		# split item_code if quantity of item_code in batch is less that required
-	# 		# Look for another batch
-
-	# 		required_qty -= batch.qty
-	# 		# set quantity of current item_code equal to batch quantity
-	# 		item_doc.set('qty', batch.qty)
-	# 		item_doc = parent_doc.append('items', {
-	# 			'item_code': item_doc.item_code,
-	# 			'qty': required_qty,
-	# 			'warehouse': item_doc.warehouse,
-	# 			'sales_order': item_doc.sales_order,
-	# 			'sales_order_item': item_doc.sales_order_item,
-	# 			'uom': item_doc.uom,
-	# 			'stock_uom': item_doc.stock_uom,
-	# 			'conversion_factor': item_doc.conversion_factor,
-	# 			'stock_qty': qty * item_doc.conversion_factor,
-	# 		})
-
 	if required_qty:
 		frappe.msgprint('No batches found for {} qty of {}.'.format(required_qty, item_doc.item_code))
 
@@ -250,4 +222,27 @@
 				'company': delivery_note.company
 			})
 
-	target.cost_center = cost_center
\ No newline at end of file
+	target.cost_center = cost_center
+
+@frappe.whitelist()
+def get_pending_work_orders(doctype, txt, searchfield, start, page_length, filters, as_dict):
+	return frappe.db.sql("""
+		SELECT
+			`name`, `company`, `planned_start_date`
+		FROM
+			`tabWork Order`
+		WHERE
+			`qty` > `produced_qty`
+			AND `status` not in ('Completed', 'Stopped')
+			AND name like %(txt)s
+			AND docstatus = 1
+		ORDER BY
+			if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), name
+		LIMIT
+			%(start)s, %(page_length)s""",
+		{
+			'txt': "%%%s%%" % txt,
+			'_txt': txt.replace('%', ''),
+			'start': start,
+			'page_length': frappe.utils.cint(page_length),
+		}, as_dict=as_dict)