2018-08-25 08:39:01 +00:00
|
|
|
import Vue from 'vue/dist/vue.js';
|
2018-08-26 14:18:52 +00:00
|
|
|
import './vue-plugins';
|
2018-08-25 08:39:01 +00:00
|
|
|
|
2018-08-19 18:55:26 +00:00
|
|
|
// components
|
2018-08-27 05:19:42 +00:00
|
|
|
import PageContainer from './PageContainer.vue';
|
2018-08-26 16:50:16 +00:00
|
|
|
import Sidebar from './Sidebar.vue';
|
2018-08-19 18:55:26 +00:00
|
|
|
import { ProfileDialog } from './components/profile_dialog';
|
|
|
|
|
2018-08-01 08:39:07 +00:00
|
|
|
// helpers
|
|
|
|
import './hub_call';
|
|
|
|
|
2018-07-26 13:07:01 +00:00
|
|
|
frappe.provide('hub');
|
|
|
|
frappe.provide('erpnext.hub');
|
2018-09-11 12:08:31 +00:00
|
|
|
frappe.provide('frappe.route');
|
2018-07-26 13:07:01 +00:00
|
|
|
|
2018-11-02 08:00:51 +00:00
|
|
|
frappe.utils.make_event_emitter(frappe.route);
|
|
|
|
frappe.utils.make_event_emitter(erpnext.hub);
|
2018-08-17 10:34:14 +00:00
|
|
|
|
2018-07-26 13:07:01 +00:00
|
|
|
erpnext.hub.Marketplace = class Marketplace {
|
|
|
|
constructor({ parent }) {
|
|
|
|
this.$parent = $(parent);
|
|
|
|
this.page = parent.page;
|
|
|
|
|
2018-09-03 13:19:33 +00:00
|
|
|
this.update_hub_settings().then(() => {
|
2018-09-03 10:12:00 +00:00
|
|
|
|
2018-08-31 10:45:06 +00:00
|
|
|
this.setup_header();
|
|
|
|
this.make_sidebar();
|
|
|
|
this.make_body();
|
|
|
|
this.setup_events();
|
|
|
|
this.refresh();
|
2018-09-03 10:12:00 +00:00
|
|
|
|
2018-09-11 12:08:31 +00:00
|
|
|
if (!hub.is_server) {
|
|
|
|
if (!hub.is_seller_registered()) {
|
|
|
|
this.page.set_primary_action('Become a Seller', this.show_register_dialog.bind(this))
|
|
|
|
} else {
|
|
|
|
this.page.set_secondary_action('Add Users', this.show_add_user_dialog.bind(this));
|
|
|
|
}
|
2018-08-31 10:45:06 +00:00
|
|
|
}
|
|
|
|
});
|
2018-07-26 13:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setup_header() {
|
2018-09-11 12:08:31 +00:00
|
|
|
if (hub.is_server) return;
|
2018-07-26 13:07:01 +00:00
|
|
|
this.page.set_title(__('Marketplace'));
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_events() {
|
|
|
|
this.$parent.on('click', '[data-route]', (e) => {
|
|
|
|
const $target = $(e.currentTarget);
|
|
|
|
const route = $target.data().route;
|
|
|
|
frappe.set_route(route);
|
|
|
|
});
|
2018-08-19 18:55:26 +00:00
|
|
|
|
|
|
|
// generic action handler
|
|
|
|
this.$parent.on('click', '[data-action]', e => {
|
|
|
|
const $target = $(e.currentTarget);
|
|
|
|
const action = $target.data().action;
|
|
|
|
|
|
|
|
if (action && this[action]) {
|
|
|
|
this[action].apply(this, $target);
|
|
|
|
}
|
|
|
|
})
|
2018-07-26 13:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
make_sidebar() {
|
|
|
|
this.$sidebar = this.$parent.find('.layout-side-section').addClass('hidden-xs');
|
|
|
|
|
2018-08-26 16:50:16 +00:00
|
|
|
new Vue({
|
|
|
|
el: $('<div>').appendTo(this.$sidebar)[0],
|
|
|
|
render: h => h(Sidebar)
|
|
|
|
});
|
2018-07-26 13:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
make_body() {
|
|
|
|
this.$body = this.$parent.find('.layout-main-section');
|
2018-08-26 14:18:52 +00:00
|
|
|
this.$page_container = $('<div class="hub-page-container">').appendTo(this.$body);
|
2018-08-26 07:11:02 +00:00
|
|
|
|
2018-08-26 14:18:52 +00:00
|
|
|
new Vue({
|
|
|
|
el: '.hub-page-container',
|
|
|
|
render: h => h(PageContainer)
|
|
|
|
});
|
2018-08-26 07:11:02 +00:00
|
|
|
|
2018-09-11 12:08:31 +00:00
|
|
|
if (!hub.is_server) {
|
|
|
|
erpnext.hub.on('seller-registered', () => {
|
|
|
|
this.page.clear_primary_action();
|
|
|
|
});
|
|
|
|
}
|
2018-07-26 13:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
2018-08-26 14:18:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-19 18:55:26 +00:00
|
|
|
show_register_dialog() {
|
2018-09-03 11:09:59 +00:00
|
|
|
if(frappe.session.user === 'Administrator') {
|
|
|
|
frappe.msgprint(__('You need to be a user other than Administrator with System Manager and Item Manager roles to register on Marketplace.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-03 12:01:43 +00:00
|
|
|
if (!is_subset(['System Manager', 'Item Manager'], frappe.user_roles)) {
|
|
|
|
frappe.msgprint(__('You need to be a user with System Manager and Item Manager roles to register on Marketplace.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-19 19:48:41 +00:00
|
|
|
this.register_dialog = ProfileDialog(
|
|
|
|
__('Become a Seller'),
|
|
|
|
{
|
|
|
|
label: __('Register'),
|
2018-09-01 04:25:43 +00:00
|
|
|
on_submit: this.register_marketplace.bind(this)
|
2018-08-19 19:48:41 +00:00
|
|
|
}
|
|
|
|
);
|
2018-08-19 18:55:26 +00:00
|
|
|
|
2018-08-19 19:48:41 +00:00
|
|
|
this.register_dialog.show();
|
2018-08-19 18:55:26 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 07:41:23 +00:00
|
|
|
register_marketplace({company, company_description}) {
|
2018-08-19 18:55:26 +00:00
|
|
|
frappe.call({
|
2018-09-01 04:25:43 +00:00
|
|
|
method: 'erpnext.hub_node.api.register_marketplace',
|
2018-09-01 09:00:46 +00:00
|
|
|
args: {
|
|
|
|
company,
|
2018-09-03 07:41:23 +00:00
|
|
|
company_description
|
2018-09-01 09:00:46 +00:00
|
|
|
}
|
|
|
|
}).then((r) => {
|
|
|
|
if (r.message && r.message.ok) {
|
|
|
|
this.register_dialog.hide();
|
2018-09-03 13:19:33 +00:00
|
|
|
|
|
|
|
this.update_hub_settings()
|
|
|
|
.then(() => {
|
|
|
|
frappe.set_route('marketplace', 'publish');
|
|
|
|
erpnext.hub.trigger('seller-registered');
|
|
|
|
});
|
2018-09-01 09:00:46 +00:00
|
|
|
}
|
2018-08-19 18:55:26 +00:00
|
|
|
});
|
|
|
|
}
|
2018-08-28 09:34:33 +00:00
|
|
|
|
2018-09-02 12:59:00 +00:00
|
|
|
show_add_user_dialog() {
|
2018-09-03 12:01:43 +00:00
|
|
|
if (!is_subset(['System Manager', 'Item Manager'], frappe.user_roles)) {
|
|
|
|
frappe.msgprint(__('You need to be a user with System Manager and Item Manager roles to add users to Marketplace.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-03 10:12:00 +00:00
|
|
|
this.get_unregistered_users()
|
|
|
|
.then(r => {
|
|
|
|
const user_list = r.message;
|
|
|
|
|
|
|
|
const d = new frappe.ui.Dialog({
|
|
|
|
title: __('Add Users to Marketplace'),
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
label: __('Users'),
|
|
|
|
fieldname: 'users',
|
|
|
|
fieldtype: 'MultiSelect',
|
|
|
|
reqd: 1,
|
|
|
|
get_data() {
|
|
|
|
return user_list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
primary_action({ users }) {
|
|
|
|
const selected_users = users.split(',').map(d => d.trim()).filter(Boolean);
|
|
|
|
|
|
|
|
if (!selected_users.every(user => user_list.includes(user))) {
|
|
|
|
d.set_df_property('users', 'description', __('Some emails are invalid'));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
d.set_df_property('users', 'description', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
frappe.call('erpnext.hub_node.api.register_users', {
|
|
|
|
user_list: selected_users
|
|
|
|
})
|
|
|
|
.then(r => {
|
|
|
|
d.hide();
|
|
|
|
|
|
|
|
if (r.message && r.message.length) {
|
|
|
|
frappe.show_alert(__('Added {0} users', [r.message.length]));
|
|
|
|
}
|
|
|
|
});
|
2018-09-02 12:59:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-03 10:12:00 +00:00
|
|
|
d.show();
|
|
|
|
});
|
2018-09-02 12:59:00 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 10:12:00 +00:00
|
|
|
get_unregistered_users() {
|
|
|
|
return frappe.call('erpnext.hub_node.api.get_unregistered_users')
|
|
|
|
}
|
|
|
|
|
2018-09-03 13:19:33 +00:00
|
|
|
update_hub_settings() {
|
2018-09-11 12:08:31 +00:00
|
|
|
return hub.get_settings().then(doc => {
|
2018-09-03 13:19:33 +00:00
|
|
|
hub.settings = doc;
|
|
|
|
});
|
|
|
|
}
|
2018-09-03 10:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(hub, {
|
|
|
|
is_seller_registered() {
|
|
|
|
return hub.settings.registered;
|
|
|
|
},
|
|
|
|
|
|
|
|
is_user_registered() {
|
|
|
|
return this.is_seller_registered() && hub.settings.users
|
|
|
|
.filter(hub_user => hub_user.user === frappe.session.user)
|
|
|
|
.length === 1;
|
|
|
|
},
|
2018-09-11 12:08:31 +00:00
|
|
|
|
|
|
|
get_settings() {
|
|
|
|
if (frappe.session.user === 'Guest') {
|
|
|
|
return Promise.resolve({
|
|
|
|
registered: 0
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return frappe.db.get_doc('Marketplace Settings');
|
|
|
|
}
|
2018-09-03 10:12:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if list_a is subset of list_b
|
|
|
|
* @param {Array} list_a
|
|
|
|
* @param {Array} list_b
|
|
|
|
*/
|
|
|
|
function is_subset(list_a, list_b) {
|
|
|
|
return list_a.every(item => list_b.includes(item));
|
2018-07-26 13:07:01 +00:00
|
|
|
}
|