erpnext.PointOfSale.Controller = class { constructor(wrapper) { this.wrapper = $(wrapper).find('.layout-main-section'); this.page = wrapper.page; this.check_opening_entry(); } fetch_opening_entry() { return frappe.call("erpnext.selling.page.point_of_sale.point_of_sale.check_opening_entry", { "user": frappe.session.user }); } check_opening_entry() { this.fetch_opening_entry().then((r) => { if (r.message.length) { // assuming only one opening voucher is available for the current user this.prepare_app_defaults(r.message[0]); } else { this.create_opening_voucher(); } }); } create_opening_voucher() { const me = this; const table_fields = [ { fieldname: "mode_of_payment", fieldtype: "Link", in_list_view: 1, label: "Mode of Payment", options: "Mode of Payment", reqd: 1 }, { fieldname: "opening_amount", fieldtype: "Currency", in_list_view: 1, label: "Opening Amount", options: "company:company_currency", change: function () { dialog.fields_dict.balance_details.df.data.some(d => { if (d.idx == this.doc.idx) { d.opening_amount = this.value; dialog.fields_dict.balance_details.grid.refresh(); return true; } }); } } ]; const fetch_pos_payment_methods = () => { const pos_profile = dialog.fields_dict.pos_profile.get_value(); if (!pos_profile) return; frappe.db.get_doc("POS Profile", pos_profile).then(({ payments }) => { dialog.fields_dict.balance_details.df.data = []; payments.forEach(pay => { const { mode_of_payment } = pay; dialog.fields_dict.balance_details.df.data.push({ mode_of_payment, opening_amount: '0' }); }); dialog.fields_dict.balance_details.grid.refresh(); }); } const dialog = new frappe.ui.Dialog({ title: __('Create POS Opening Entry'), static: true, fields: [ { fieldtype: 'Link', label: __('Company'), default: frappe.defaults.get_default('company'), options: 'Company', fieldname: 'company', reqd: 1 }, { fieldtype: 'Link', label: __('POS Profile'), options: 'POS Profile', fieldname: 'pos_profile', reqd: 1, onchange: () => fetch_pos_payment_methods() }, { fieldname: "balance_details", fieldtype: "Table", label: "Opening Balance Details", cannot_add_rows: false, in_place_edit: true, reqd: 1, data: [], fields: table_fields } ], primary_action: async function({ company, pos_profile, balance_details }) { if (!balance_details.length) { frappe.show_alert({ message: __("Please add Mode of payments and opening balance details."), indicator: 'red' }) return frappe.utils.play_sound("error"); } const method = "erpnext.selling.page.point_of_sale.point_of_sale.create_opening_voucher"; const res = await frappe.call({ method, args: { pos_profile, company, balance_details }, freeze:true }); !res.exc && me.prepare_app_defaults(res.message); dialog.hide(); }, primary_action_label: __('Submit') }); dialog.show(); } async prepare_app_defaults(data) { this.pos_opening = data.name; this.company = data.company; this.pos_profile = data.pos_profile; this.pos_opening_time = data.period_start_date; this.item_stock_map = {}; this.settings = {}; frappe.db.get_value('Stock Settings', undefined, 'allow_negative_stock').then(({ message }) => { this.allow_negative_stock = flt(message.allow_negative_stock) || false; }); frappe.db.get_doc("POS Profile", this.pos_profile).then((profile) => { this.settings.customer_groups = profile.customer_groups.map(group => group.customer_group); this.settings.hide_images = profile.hide_images; this.settings.auto_add_item_to_cart = profile.auto_add_item_to_cart; this.make_app(); }); } set_opening_entry_status() { this.page.set_title_sub( ` Opened at ${moment(this.pos_opening_time).format("Do MMMM, h:mma")} `); } make_app() { this.prepare_dom(); this.prepare_components(); this.prepare_menu(); this.make_new_invoice(); } prepare_dom() { this.wrapper.append( `
` ); this.$components_wrapper = this.wrapper.find('.point-of-sale-app'); } prepare_components() { this.init_item_selector(); this.init_item_details(); this.init_item_cart(); this.init_payments(); this.init_recent_order_list(); this.init_order_summary(); } prepare_menu() { this.page.clear_menu(); this.page.add_menu_item(__("Open Form View"), this.open_form_view.bind(this), false, 'Ctrl+F'); this.page.add_menu_item(__("Toggle Recent Orders"), this.toggle_recent_order.bind(this), false, 'Ctrl+O'); this.page.add_menu_item(__("Save as Draft"), this.save_draft_invoice.bind(this), false, 'Ctrl+S'); this.page.add_menu_item(__('Close the POS'), this.close_pos.bind(this), false, 'Shift+Ctrl+C'); } open_form_view() { frappe.model.sync(this.frm.doc); frappe.set_route("Form", this.frm.doc.doctype, this.frm.doc.name); } toggle_recent_order() { const show = this.recent_order_list.$component.is(':hidden'); this.toggle_recent_order_list(show); } save_draft_invoice() { if (!this.$components_wrapper.is(":visible")) return; if (this.frm.doc.items.length == 0) { frappe.show_alert({ message:__("You must add atleast one item to save it as draft."), indicator:'red' }); frappe.utils.play_sound("error"); return; } this.frm.save(undefined, undefined, undefined, () => { frappe.show_alert({ message:__("There was an error saving the document."), indicator:'red' }); frappe.utils.play_sound("error"); }).then(() => { frappe.run_serially([ () => frappe.dom.freeze(), () => this.make_new_invoice(), () => frappe.dom.unfreeze(), ]); }) } close_pos() { if (!this.$components_wrapper.is(":visible")) return; let voucher = frappe.model.get_new_doc('POS Closing Entry'); voucher.pos_profile = this.frm.doc.pos_profile; voucher.user = frappe.session.user; voucher.company = this.frm.doc.company; voucher.pos_opening_entry = this.pos_opening; voucher.period_end_date = frappe.datetime.now_datetime(); voucher.posting_date = frappe.datetime.now_date(); frappe.set_route('Form', 'POS Closing Entry', voucher.name); } init_item_selector() { this.item_selector = new erpnext.PointOfSale.ItemSelector({ wrapper: this.$components_wrapper, pos_profile: this.pos_profile, settings: this.settings, events: { item_selected: args => this.on_cart_update(args), get_frm: () => this.frm || {} } }) } init_item_cart() { this.cart = new erpnext.PointOfSale.ItemCart({ wrapper: this.$components_wrapper, settings: this.settings, events: { get_frm: () => this.frm, cart_item_clicked: (item_code, batch_no, uom) => { const item_row = this.frm.doc.items.find( i => i.item_code === item_code && i.uom === uom && (!batch_no || (batch_no && i.batch_no === batch_no)) ); this.item_details.toggle_item_details_section(item_row); }, numpad_event: (value, action) => this.update_item_field(value, action), checkout: () => this.payment.checkout(), edit_cart: () => this.payment.edit_cart(), customer_details_updated: (details) => { this.customer_details = details; // will add/remove LP payment method this.payment.render_loyalty_points_payment_mode(); } } }) } init_item_details() { this.item_details = new erpnext.PointOfSale.ItemDetails({ wrapper: this.$components_wrapper, events: { get_frm: () => this.frm, toggle_item_selector: (minimize) => { this.item_selector.resize_selector(minimize); this.cart.toggle_numpad(minimize); }, form_updated: async (cdt, cdn, fieldname, value) => { const item_row = frappe.model.get_doc(cdt, cdn); if (item_row && item_row[fieldname] != value) { if (fieldname === 'qty' && flt(value) == 0) { this.remove_item_from_cart(); return; } const { item_code, batch_no, uom } = this.item_details.current_item; const event = { field: fieldname, value, item: { item_code, batch_no, uom } } return this.on_cart_update(event) } }, item_field_focused: (fieldname) => { this.cart.toggle_numpad_field_edit(fieldname); }, set_value_in_current_cart_item: (selector, value) => { this.cart.update_selector_value_in_cart_item(selector, value, this.item_details.current_item); }, clone_new_batch_item_in_frm: (batch_serial_map, current_item) => { // called if serial nos are 'auto_selected' and if those serial nos belongs to multiple batches // for each unique batch new item row is added in the form & cart Object.keys(batch_serial_map).forEach(batch => { const { item_code, batch_no } = current_item; const item_to_clone = this.frm.doc.items.find(i => i.item_code === item_code && i.batch_no === batch_no); const new_row = this.frm.add_child("items", { ...item_to_clone }); // update new serialno and batch new_row.batch_no = batch; new_row.serial_no = batch_serial_map[batch].join(`\n`); new_row.qty = batch_serial_map[batch].length; this.frm.doc.items.forEach(row => { if (item_code === row.item_code) { this.update_cart_html(row); } }); }) }, remove_item_from_cart: () => this.remove_item_from_cart(), get_item_stock_map: () => this.item_stock_map, close_item_details: () => { this.item_details.toggle_item_details_section(undefined); this.cart.prev_action = undefined; this.cart.toggle_item_highlight(); }, get_available_stock: (item_code, warehouse) => this.get_available_stock(item_code, warehouse) } }); } init_payments() { this.payment = new erpnext.PointOfSale.Payment({ wrapper: this.$components_wrapper, events: { get_frm: () => this.frm || {}, get_customer_details: () => this.customer_details || {}, toggle_other_sections: (show) => { if (show) { this.item_details.$component.is(':visible') ? this.item_details.$component.css('display', 'none') : ''; this.item_selector.$component.css('display', 'none'); } else { this.item_selector.$component.css('display', 'flex'); } }, submit_invoice: () => { this.frm.savesubmit() .then((r) => { this.toggle_components(false); this.order_summary.toggle_component(true); this.order_summary.load_summary_of(this.frm.doc, true); frappe.show_alert({ indicator: 'green', message: __('POS invoice {0} created succesfully', [r.doc.name]) }); }); } } }); } init_recent_order_list() { this.recent_order_list = new erpnext.PointOfSale.PastOrderList({ wrapper: this.$components_wrapper, events: { open_invoice_data: (name) => { frappe.db.get_doc('POS Invoice', name).then((doc) => { this.order_summary.load_summary_of(doc); }); }, reset_summary: () => this.order_summary.toggle_summary_placeholder(true) } }) } init_order_summary() { this.order_summary = new erpnext.PointOfSale.PastOrderSummary({ wrapper: this.$components_wrapper, events: { get_frm: () => this.frm, process_return: (name) => { this.recent_order_list.toggle_component(false); frappe.db.get_doc('POS Invoice', name).then((doc) => { frappe.run_serially([ () => this.make_return_invoice(doc), () => this.cart.load_invoice(), () => this.item_selector.toggle_component(true) ]); }); }, edit_order: (name) => { this.recent_order_list.toggle_component(false); frappe.run_serially([ () => this.frm.refresh(name), () => this.cart.load_invoice(), () => this.item_selector.toggle_component(true) ]); }, new_order: () => { frappe.run_serially([ () => frappe.dom.freeze(), () => this.make_new_invoice(), () => this.item_selector.toggle_component(true), () => frappe.dom.unfreeze(), ]); } } }) } toggle_recent_order_list(show) { this.toggle_components(!show); this.recent_order_list.toggle_component(show); this.order_summary.toggle_component(show); } toggle_components(show) { this.cart.toggle_component(show); this.item_selector.toggle_component(show); // do not show item details or payment if recent order is toggled off !show ? (this.item_details.toggle_component(false) || this.payment.toggle_component(false)) : ''; } make_new_invoice() { return frappe.run_serially([ () => frappe.dom.freeze(), () => this.make_sales_invoice_frm(), () => this.set_pos_profile_data(), () => this.set_pos_profile_status(), () => this.cart.load_invoice(), () => frappe.dom.unfreeze() ]); } make_sales_invoice_frm() { const doctype = 'POS Invoice'; return new Promise(resolve => { if (this.frm) { this.frm = this.get_new_frm(this.frm); this.frm.doc.items = []; this.frm.doc.is_pos = 1 resolve(); } else { frappe.model.with_doctype(doctype, () => { this.frm = this.get_new_frm(); this.frm.doc.items = []; this.frm.doc.is_pos = 1 resolve(); }); } }); } get_new_frm(_frm) { const doctype = 'POS Invoice'; const page = $('