Merge branch 'develop' into publish-item
diff --git a/erpnext/hub_node/api.py b/erpnext/hub_node/api.py
index f362539..2035174 100644
--- a/erpnext/hub_node/api.py
+++ b/erpnext/hub_node/api.py
@@ -70,7 +70,7 @@
 	field_mappings = get_field_mappings()
 	table_fields = [d.fieldname for d in frappe.get_meta('Item').get_table_fields()]
 
-	hub_seller_name = frappe.db.get_value('Marketplace Settings' , 'Marketplace Settings', 'hub_seller_name')
+	hub_seller_name = frappe.db.get_value('Marketplace Settings', 'Marketplace Settings', 'hub_seller_name')
 
 	for item in items:
 		for fieldname in table_fields:
@@ -129,6 +129,7 @@
 @frappe.whitelist()
 def publish_selected_items(items_to_publish):
 	items_to_publish = json.loads(items_to_publish)
+	items_to_update = []
 	if not len(items_to_publish):
 		frappe.throw(_('No items to publish'))
 
@@ -136,14 +137,24 @@
 		item_code = item.get('item_code')
 		frappe.db.set_value('Item', item_code, 'publish_in_hub', 1)
 
-		frappe.get_doc({
+		hub_dict = {
 			'doctype': 'Hub Tracked Item',
 			'item_code': item_code,
+			'published': 1,
 			'hub_category': item.get('hub_category'),
 			'image_list': item.get('image_list')
-		}).insert(ignore_if_duplicate=True)
+		}
+		if frappe.db.exists('Hub Tracked Item', item_code):
+			items_to_update.append(item)
+			hub_tracked_item = frappe.get_doc('Hub Tracked Item', item_code)
+			hub_tracked_item.update(hub_dict)
+			hub_tracked_item.save()
+		else:
+			frappe.get_doc(hub_dict).insert(ignore_if_duplicate=True)
 
-	items = map_fields(items_to_publish)
+	items_to_publish = list(filter(lambda x: x not in items_to_update, items_to_publish))
+	new_items = map_fields(items_to_publish)
+	existing_items = map_fields(items_to_update)
 
 	try:
 		item_sync_preprocess(len(items))
@@ -151,13 +162,27 @@
 
 		# TODO: Publish Progress
 		connection = get_hub_connection()
-		connection.insert_many(items)
+		connection.insert_many(new_items)
+		connection.bulk_update(existing_items)
 
 		item_sync_postprocess()
 	except Exception as e:
 		frappe.log_error(message=e, title='Hub Sync Error')
 
 @frappe.whitelist()
+def unpublish_item(item_code, hub_item_name):
+	''' Remove item listing from the marketplace '''
+
+	response = call_hub_method('unpublish_item', {
+		'hub_item_name': hub_item_name
+	})
+
+	if response:
+		frappe.db.set_value('Item', item_code, 'publish_in_hub', 0)
+	else:
+		frappe.throw(_('Unable to update remote activity'))
+
+@frappe.whitelist()
 def get_unregistered_users():
 	settings = frappe.get_single('Marketplace Settings')
 	registered_users = [user.user for user in settings.users] + ['Administrator', 'Guest']
diff --git a/erpnext/hub_node/doctype/hub_tracked_item/hub_tracked_item.json b/erpnext/hub_node/doctype/hub_tracked_item/hub_tracked_item.json
index 2e89887..7d07ba4 100644
--- a/erpnext/hub_node/doctype/hub_tracked_item/hub_tracked_item.json
+++ b/erpnext/hub_node/doctype/hub_tracked_item/hub_tracked_item.json
@@ -78,6 +78,38 @@
    "unique": 0
   }, 
   {
+    "allow_bulk_edit": 0,
+    "allow_in_quick_entry": 0,
+    "allow_on_submit": 0,
+    "bold": 0,
+    "collapsible": 0,
+    "columns": 0,
+    "fieldname": "published",
+    "fieldtype": "Check",
+    "hidden": 0,
+    "ignore_user_permissions": 0,
+    "ignore_xss_filter": 0,
+    "in_filter": 0,
+    "in_global_search": 0,
+    "in_list_view": 0,
+    "in_standard_filter": 0,
+    "label": "Published",
+    "length": 0,
+    "no_copy": 0,
+    "permlevel": 0,
+    "precision": "",
+    "print_hide": 0,
+    "print_hide_if_no_value": 0,
+    "read_only": 0,
+    "remember_last_selected_value": 0,
+    "report_hide": 0,
+    "reqd": 0,
+    "search_index": 0,
+    "set_only_once": 0,
+    "translatable": 0,
+    "unique": 0
+   },
+  {
    "allow_bulk_edit": 0, 
    "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
@@ -120,7 +152,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-09-10 11:37:35.951019", 
+ "modified": "2019-12-10 11:37:35.951019", 
  "modified_by": "Administrator", 
  "module": "Hub Node", 
  "name": "Hub Tracked Item", 
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 89be499..742fc6b 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -653,3 +653,4 @@
 erpnext.patches.v12_0.set_against_blanket_order_in_sales_and_purchase_order
 erpnext.patches.v12_0.set_cost_center_in_child_table_of_expense_claim
 erpnext.patches.v12_0.set_lead_title_field
+erpnext.patches.v12_0.set_published_in_hub_tracked_item
\ No newline at end of file
diff --git a/erpnext/patches/v12_0/set_published_in_hub_tracked_item.py b/erpnext/patches/v12_0/set_published_in_hub_tracked_item.py
new file mode 100644
index 0000000..7c4c011
--- /dev/null
+++ b/erpnext/patches/v12_0/set_published_in_hub_tracked_item.py
@@ -0,0 +1,12 @@
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+	frappe.reload_doc("Hub Node", "doctype", "Hub Tracked Item")
+	if frappe.db.a_row_exists("Hub Tracked Item"):
+		return
+
+	frappe.db.sql('''
+		Update `tabHub Tracked Item`
+		SET published = 1
+	''')
\ No newline at end of file
diff --git a/erpnext/public/js/hub/components/item_publish_dialog.js b/erpnext/public/js/hub/components/item_publish_dialog.js
index e49ba53..08de5b3 100644
--- a/erpnext/public/js/hub/components/item_publish_dialog.js
+++ b/erpnext/public/js/hub/components/item_publish_dialog.js
@@ -3,24 +3,24 @@
 		title: __('Edit Publishing Details'),
 		fields: [
 			{
-				"label": "Item Code",
-				"fieldname": "item_code",
-				"fieldtype": "Data",
-				"read_only": 1
+				label: __('Item Code'),
+				fieldname: 'item_code',
+				fieldtype: 'Data',
+				read_only: 1
 			},
 			{
-				"label": "Hub Category",
-				"fieldname": "hub_category",
-				"fieldtype": "Autocomplete",
-				"options": [],
-				"reqd": 1
+				label: __('Hub Category'),
+				fieldname: 'hub_category',
+				fieldtype: 'Autocomplete',
+				options: [],
+				reqd: 1
 			},
 			{
-				"label": "Images",
-				"fieldname": "image_list",
-				"fieldtype": "MultiSelect",
-				"options": [],
-				"reqd": 1
+				label: __('Images'),
+				fieldname: 'image_list',
+				fieldtype: 'MultiSelect',
+				options: [],
+				reqd: 1
 			}
 		],
 		primary_action_label: primary_action.label || __('Set Details'),
@@ -28,15 +28,12 @@
 		secondary_action: secondary_action.fn
 	});
 
-	hub.call('get_categories')
-		.then(categories => {
-			categories = categories.map(d => d.name);
-			dialog.fields_dict.hub_category.set_data(categories);
-		});
+	hub.call('get_categories').then(categories => {
+		categories = categories.map(d => d.name);
+		dialog.fields_dict.hub_category.set_data(categories);
+	});
 
 	return dialog;
 }
 
-export {
-	ItemPublishDialog
-}
+export { ItemPublishDialog };
diff --git a/erpnext/public/js/hub/pages/Home.vue b/erpnext/public/js/hub/pages/Home.vue
index aaeaa7e..8fe8245 100644
--- a/erpnext/public/js/hub/pages/Home.vue
+++ b/erpnext/public/js/hub/pages/Home.vue
@@ -58,6 +58,13 @@
 		this.search_value = '';
 		this.get_items();
 	},
+	mounted() {
+		frappe.route.on('change', () => {
+			if (frappe.get_route_str() === 'marketplace/home') {
+				this.get_items();
+			}
+		})
+	},
 	methods: {
 		get_items() {
 			hub.call('get_data_for_homepage', frappe.defaults ? {
diff --git a/erpnext/public/js/hub/pages/Item.vue b/erpnext/public/js/hub/pages/Item.vue
index 1174478..51ade42 100644
--- a/erpnext/public/js/hub/pages/Item.vue
+++ b/erpnext/public/js/hub/pages/Item.vue
@@ -161,7 +161,8 @@
 	},
 	methods: {
 		get_item_details() {
-			this.item_received = hub.call('get_item_details', { hub_item_name: this.hub_item_name })
+			this.item_received = hub
+				.call('get_item_details', { hub_item_name: this.hub_item_name })
 				.then(item => {
 					this.init = false;
 					this.item = item;
@@ -205,9 +206,7 @@
 					hub_user: frappe.session.user
 				})
 				.then(() => {
-					const saved_items_link = `<b><a href="#marketplace/saved-items">${__(
-						'Saved'
-					)}</a></b>`;
+					const saved_items_link = `<b><a href="#marketplace/saved-items">${__('Saved')}</a></b>`;
 					frappe.show_alert(saved_items_link);
 					erpnext.hub.trigger('action:item_save');
 				})
@@ -222,9 +221,7 @@
 					hub_user: frappe.session.user
 				})
 				.then(() => {
-					const featured_items_link = `<b><a href="#marketplace/featured-items">${__(
-						'Added to Featured Items'
-					)}</a></b>`;
+					const featured_items_link = `<b><a href="#marketplace/featured-items">${__('Added to Featured Items')}</a></b>`;
 					frappe.show_alert(featured_items_link);
 					erpnext.hub.trigger('action:item_feature');
 				})
@@ -340,7 +337,17 @@
 		},
 
 		unpublish_item() {
-			frappe.msgprint(__('This feature is under development...'));
+			frappe.confirm(__(`Unpublish {0}?`, [this.item.item_name]), () => {
+				frappe
+					.call('erpnext.hub_node.api.unpublish_item', {
+						item_code: this.item.item_code,
+						hub_item_name: this.hub_item_name
+					})
+					.then(r => {
+						frappe.set_route(`marketplace/home`);
+						frappe.show_alert(__('Item listing removed'));
+					});
+			});
 		}
 	}
 };