erpnext.PointOfSale.ItemCart = class { constructor({ wrapper, events }) { this.wrapper = wrapper; this.events = events; this.customer_info = undefined; this.init_component(); } init_component() { this.prepare_dom(); this.init_child_components(); this.bind_events(); this.attach_shortcuts(); } prepare_dom() { this.wrapper.append( `
` ) this.$component = this.wrapper.find('.item-cart'); } init_child_components() { this.init_customer_selector(); this.init_cart_components(); } init_customer_selector() { this.$component.append( `
` ) this.$customer_section = this.$component.find('.customer-section'); } reset_customer_selector() { const frm = this.events.get_frm(); frm.set_value('customer', ''); this.$customer_section.removeClass('border pr-4 pl-4'); this.make_customer_selector(); this.customer_field.set_focus(); } init_cart_components() { this.$component.append( `
Item
Qty
Amount
` ); this.$cart_container = this.$component.find('.cart-container'); this.make_cart_totals_section(); this.make_cart_items_section(); this.make_cart_numpad(); } make_cart_items_section() { this.$cart_header = this.$component.find('.cart-header'); this.$cart_items_wrapper = this.$component.find('.cart-items-section'); this.make_no_items_placeholder(); } make_no_items_placeholder() { this.$cart_header.addClass('d-none'); this.$cart_items_wrapper.html( `
No items in cart
` ) this.$cart_items_wrapper.addClass('mt-4 border-grey border-dashed'); } make_cart_totals_section() { this.$totals_section = this.$component.find('.cart-totals-section'); this.$totals_section.append( `
+ Add Discount
Net Total
0.00
Grand Total
0.00
Checkout
Edit Cart
` ) this.$add_discount_elem = this.$component.find(".add-discount"); } make_cart_numpad() { this.$numpad_section = this.$component.find('.numpad-section'); this.number_pad = new erpnext.PointOfSale.NumberPad({ wrapper: this.$numpad_section, events: { numpad_event: this.on_numpad_event.bind(this) }, cols: 5, keys: [ [ 1, 2, 3, 'Quantity' ], [ 4, 5, 6, 'Discount' ], [ 7, 8, 9, 'Rate' ], [ '.', 0, 'Delete', 'Remove' ] ], css_classes: [ [ '', '', '', 'col-span-2' ], [ '', '', '', 'col-span-2' ], [ '', '', '', 'col-span-2' ], [ '', '', '', 'col-span-2 text-bold text-danger' ] ], fieldnames_map: { 'Quantity': 'qty', 'Discount': 'discount_percentage' } }) this.$numpad_section.prepend( `
` ) this.$numpad_section.append( `
Checkout
` ) } bind_events() { const me = this; this.$customer_section.on('click', '.add-remove-customer', function (e) { const customer_info_is_visible = me.$cart_container.hasClass('d-none'); customer_info_is_visible ? me.toggle_customer_info(false) : me.reset_customer_selector(); }); this.$customer_section.on('click', '.customer-header', function(e) { // don't triggger the event if .add-remove-customer btn is clicked which is under .customer-header if ($(e.target).closest('.add-remove-customer').length) return; const show = !me.$cart_container.hasClass('d-none'); me.toggle_customer_info(show); }); this.$cart_items_wrapper.on('click', '.cart-item-wrapper', function() { const $cart_item = $(this); me.toggle_item_highlight(this); const payment_section_hidden = me.$totals_section.find('.edit-cart-btn').hasClass('d-none'); if (!payment_section_hidden) { // payment section is visible // edit cart first and then open item details section me.$totals_section.find(".edit-cart-btn").click(); } const item_code = unescape($cart_item.attr('data-item-code')); const batch_no = unescape($cart_item.attr('data-batch-no')); const uom = unescape($cart_item.attr('data-uom')); me.events.cart_item_clicked(item_code, batch_no, uom); this.numpad_value = ''; }); this.$component.on('click', '.checkout-btn', function() { if (!$(this).hasClass('bg-primary')) return; me.events.checkout(); me.toggle_checkout_btn(false); me.$add_discount_elem.removeClass("d-none"); }); this.$totals_section.on('click', '.edit-cart-btn', () => { this.events.edit_cart(); this.toggle_checkout_btn(true); this.$add_discount_elem.addClass("d-none"); }); this.$component.on('click', '.add-discount', () => { const can_edit_discount = this.$add_discount_elem.find('.edit-discount').length; if(!this.discount_field || can_edit_discount) this.show_discount_control(); }); frappe.ui.form.on("POS Invoice", "paid_amount", frm => { // called when discount is applied this.update_totals_section(frm); }); } attach_shortcuts() { for (let row of this.number_pad.keys) { for (let btn of row) { let shortcut_key = `ctrl+${frappe.scrub(String(btn))[0]}`; if (btn === 'Delete') shortcut_key = 'ctrl+backspace'; if (btn === 'Remove') shortcut_key = 'shift+ctrl+backspace' if (btn === '.') shortcut_key = 'ctrl+>'; // to account for fieldname map const fieldname = this.number_pad.fieldnames[btn] ? this.number_pad.fieldnames[btn] : typeof btn === 'string' ? frappe.scrub(btn) : btn; frappe.ui.keys.on(`${shortcut_key}`, () => { const cart_is_visible = this.$component.is(":visible"); if (cart_is_visible && this.item_is_selected && this.$numpad_section.is(":visible")) { this.$numpad_section.find(`.numpad-btn[data-button-value="${fieldname}"]`).click(); } }) } } frappe.ui.keys.on("ctrl+enter", () => { const cart_is_visible = this.$component.is(":visible"); const payment_section_hidden = this.$totals_section.find('.edit-cart-btn').hasClass('d-none'); if (cart_is_visible && payment_section_hidden) { this.$component.find(".checkout-btn").click(); } }); } toggle_item_highlight(item) { const $cart_item = $(item); const item_is_highlighted = $cart_item.hasClass("shadow"); if (!item || item_is_highlighted) { this.item_is_selected = false; this.$cart_container.find('.cart-item-wrapper').removeClass("shadow").css("opacity", "1"); } else { $cart_item.addClass("shadow"); this.item_is_selected = true; this.$cart_container.find('.cart-item-wrapper').css("opacity", "1"); this.$cart_container.find('.cart-item-wrapper').not(item).removeClass("shadow").css("opacity", "0.65"); } // highlight with inner shadow // $cart_item.addClass("shadow-inner bg-selected"); // me.$cart_container.find('.cart-item-wrapper').not(this).removeClass("shadow-inner bg-selected"); } make_customer_selector() { this.$customer_section.html(`
`); const me = this; const query = { query: 'erpnext.controllers.queries.customer_query' }; const allowed_customer_group = this.events.get_allowed_customer_group() || []; if (allowed_customer_group.length) { query.filters = { customer_group: ['in', allowed_customer_group] } } this.customer_field = frappe.ui.form.make_control({ df: { label: __('Customer'), fieldtype: 'Link', options: 'Customer', placeholder: __('Search by customer name, phone, email.'), get_query: () => query, onchange: function() { if (this.value) { const frm = me.events.get_frm(); frappe.dom.freeze(); frappe.model.set_value(frm.doc.doctype, frm.doc.name, 'customer', this.value); frm.script_manager.trigger('customer', frm.doc.doctype, frm.doc.name).then(() => { frappe.run_serially([ () => me.fetch_customer_details(this.value), () => me.events.customer_details_updated(me.customer_info), () => me.update_customer_section(), () => me.update_totals_section(), () => frappe.dom.unfreeze() ]); }) } }, }, parent: this.$customer_section.find('.customer-search-field'), render_input: true, }); this.customer_field.toggle_label(false); } fetch_customer_details(customer) { if (customer) { return new Promise((resolve) => { frappe.db.get_value('Customer', customer, ["email_id", "mobile_no", "image", "loyalty_program"]).then(({ message }) => { const { loyalty_program } = message; // if loyalty program then fetch loyalty points too if (loyalty_program) { frappe.call({ method: "erpnext.accounts.doctype.loyalty_program.loyalty_program.get_loyalty_program_details_with_points", args: { customer, loyalty_program, "silent": true }, callback: (r) => { const { loyalty_points, conversion_factor } = r.message; if (!r.exc) { this.customer_info = { ...message, customer, loyalty_points, conversion_factor }; resolve(); } } }); } else { this.customer_info = { ...message, customer }; resolve(); } }); }); } else { return new Promise((resolve) => { this.customer_info = {} resolve(); }); } } show_discount_control() { this.$add_discount_elem.removeClass("pr-4 pl-4"); this.$add_discount_elem.html( `
` ); const me = this; this.discount_field = frappe.ui.form.make_control({ df: { label: __('Discount'), fieldtype: 'Data', placeholder: __('Enter discount percentage.'), onchange: function() { if (this.value || this.value == 0) { const frm = me.events.get_frm(); frappe.model.set_value(frm.doc.doctype, frm.doc.name, 'additional_discount_percentage', this.value); me.hide_discount_control(this.value); } }, }, parent: this.$add_discount_elem.find('.add-dicount-field'), render_input: true, }); this.discount_field.toggle_label(false); this.discount_field.set_focus(); } hide_discount_control(discount) { this.$add_discount_elem.addClass('pr-4 pl-4'); this.$add_discount_elem.html( `
${String(discount).bold()}% off
` ); } update_customer_section() { const { customer, email_id='', mobile_no='', image } = this.customer_info || {}; if (customer) { this.$customer_section.addClass('border pr-4 pl-4').html( `
${get_customer_image()}
${customer}
${get_customer_description()}
` ); } else { // reset customer selector this.reset_customer_selector(); } function get_customer_description() { if (!email_id && !mobile_no) { return `
Click to add email / phone
` } else if (email_id && !mobile_no) { return `
${email_id}
` } else if (mobile_no && !email_id) { return `
${mobile_no}
` } else { return `
${email_id} | ${mobile_no}
` } } function get_customer_image() { if (image) { return `
${image}
` } else { return `
${frappe.get_abbr(customer)}
` } } } update_totals_section(frm) { if (!frm) frm = this.events.get_frm(); this.render_net_total(frm.doc.base_net_total); this.render_grand_total(frm.doc.base_grand_total); const taxes = frm.doc.taxes.map(t => { return { description: t.description, rate: t.rate }}) this.render_taxes(frm.doc.base_total_taxes_and_charges, taxes); } render_net_total(value) { const currency = this.events.get_frm().doc.currency; this.$totals_section.find('.net-total').html( `
Net Total
${format_currency(value, currency)}
` ) this.$numpad_section.find('.numpad-net-total').html(`Net Total: ${format_currency(value, currency)}`) } render_grand_total(value) { const currency = this.events.get_frm().doc.currency; this.$totals_section.find('.grand-total').html( `
Grand Total
${format_currency(value, currency)}
` ) this.$numpad_section.find('.numpad-grand-total').html(`Grand Total: ${format_currency(value, currency)}`) } render_taxes(value, taxes) { if (taxes.length) { const currency = this.events.get_frm().doc.currency; this.$totals_section.find('.taxes').html( `
Tax Charges
${ taxes.map((t, i) => { let margin_left = ''; if (i !== 0) margin_left = 'ml-2'; return `${t.description}` }).join('') }
${format_currency(value, currency)}
` ) } else { this.$totals_section.find('.taxes').html('') } } get_cart_item({ item_code, batch_no, uom }) { const batch_attr = `[data-batch-no="${escape(batch_no)}"]`; const item_code_attr = `[data-item-code="${escape(item_code)}"]`; const uom_attr = `[data-uom=${escape(uom)}]`; const item_selector = batch_no ? `.cart-item-wrapper${batch_attr}${uom_attr}` : `.cart-item-wrapper${item_code_attr}${uom_attr}`; return this.$cart_items_wrapper.find(item_selector); } update_item_html(item, remove_item) { const $item = this.get_cart_item(item); if (remove_item) { $item && $item.remove(); } else { const { item_code, batch_no, uom } = item; const search_field = batch_no ? 'batch_no' : 'item_code'; const search_value = batch_no || item_code; const item_row = this.events.get_frm().doc.items.find(i => i[search_field] === search_value && i.uom === uom); this.render_cart_item(item_row, $item); } const no_of_cart_items = this.$cart_items_wrapper.children().length; no_of_cart_items > 0 && this.highlight_checkout_btn(no_of_cart_items > 0); this.update_empty_cart_section(no_of_cart_items); } render_cart_item(item_data, $item_to_update) { const currency = this.events.get_frm().doc.currency; const me = this; if (!$item_to_update.length) { this.$cart_items_wrapper.append( `
` ) $item_to_update = this.get_cart_item(item_data); } $item_to_update.html( `
${item_data.item_name}
${get_description_html()}
${get_rate_discount_html()} ` ) set_dynamic_rate_header_width(); this.scroll_to_item($item_to_update); function set_dynamic_rate_header_width() { const rate_cols = Array.from(me.$cart_items_wrapper.find(".rate-col")); me.$cart_header.find(".rate-list-header").css("width", ""); me.$cart_items_wrapper.find(".rate-col").css("width", ""); let max_width = rate_cols.reduce((max_width, elm) => { if ($(elm).width() > max_width) max_width = $(elm).width(); return max_width; }, 0); max_width += 1; if (max_width == 1) max_width = ""; me.$cart_header.find(".rate-list-header").css("width", max_width); me.$cart_items_wrapper.find(".rate-col").css("width", max_width); } function get_rate_discount_html() { if (item_data.rate && item_data.amount && item_data.rate !== item_data.amount) { return `
${item_data.qty || 0}
${format_currency(item_data.amount, currency)}
${format_currency(item_data.rate, currency)}
` } else { return `
${item_data.qty || 0}
${format_currency(item_data.rate, currency)}
` } } function get_description_html() { if (item_data.description) { if (item_data.description.indexOf('
') != -1) { try { item_data.description = $(item_data.description).text(); } catch (error) { item_data.description = item_data.description.replace(/
/g, ' ').replace(/<\/div>/g, ' ').replace(/ +/g, ' '); } } item_data.description = frappe.ellipsis(item_data.description, 45); return `
${item_data.description}
` } return ``; } } scroll_to_item($item) { if ($item.length === 0) return; const scrollTop = $item.offset().top - this.$cart_items_wrapper.offset().top + this.$cart_items_wrapper.scrollTop(); this.$cart_items_wrapper.animate({ scrollTop }); } update_selector_value_in_cart_item(selector, value, item) { const $item_to_update = this.get_cart_item(item); $item_to_update.attr(`data-${selector}`, value); } toggle_checkout_btn(show_checkout) { if (show_checkout) { this.$totals_section.find('.checkout-btn').removeClass('d-none'); this.$totals_section.find('.edit-cart-btn').addClass('d-none'); } else { this.$totals_section.find('.checkout-btn').addClass('d-none'); this.$totals_section.find('.edit-cart-btn').removeClass('d-none'); } } highlight_checkout_btn(toggle) { const has_primary_class = this.$totals_section.find('.checkout-btn').hasClass('bg-primary'); if (toggle && !has_primary_class) { this.$totals_section.find('.checkout-btn').addClass('bg-primary text-white text-lg'); } else if (!toggle && has_primary_class) { this.$totals_section.find('.checkout-btn').removeClass('bg-primary text-white text-lg'); } } update_empty_cart_section(no_of_cart_items) { const $no_item_element = this.$cart_items_wrapper.find('.no-item-wrapper'); // if cart has items and no item is present no_of_cart_items > 0 && $no_item_element && $no_item_element.remove() && this.$cart_items_wrapper.removeClass('mt-4 border-grey border-dashed') && this.$cart_header.removeClass('d-none'); no_of_cart_items === 0 && !$no_item_element.length && this.make_no_items_placeholder(); } on_numpad_event($btn) { const current_action = $btn.attr('data-button-value'); const action_is_field_edit = ['qty', 'discount_percentage', 'rate'].includes(current_action); this.highlight_numpad_btn($btn, current_action); const action_is_pressed_twice = this.prev_action === current_action; const first_click_event = !this.prev_action; const field_to_edit_changed = this.prev_action && this.prev_action != current_action; if (action_is_field_edit) { if (first_click_event || field_to_edit_changed) { this.prev_action = current_action; } else if (action_is_pressed_twice) { this.prev_action = undefined; } this.numpad_value = ''; } else if (current_action === 'checkout') { this.prev_action = undefined; this.toggle_item_highlight(); this.events.numpad_event(undefined, current_action); return; } else if (current_action === 'remove') { this.prev_action = undefined; this.toggle_item_highlight(); this.events.numpad_event(undefined, current_action); return; } else { this.numpad_value = current_action === 'delete' ? this.numpad_value.slice(0, -1) : this.numpad_value + current_action; this.numpad_value = this.numpad_value || 0; } const first_click_event_is_not_field_edit = !action_is_field_edit && first_click_event; if (first_click_event_is_not_field_edit) { frappe.show_alert({ indicator: 'red', message: __('Please select a field to edit from numpad') }); frappe.utils.play_sound("error"); return; } if (flt(this.numpad_value) > 100 && this.prev_action === 'discount_percentage') { frappe.show_alert({ message: __('Discount cannot be greater than 100%'), indicator: 'orange' }); frappe.utils.play_sound("error"); this.numpad_value = current_action; } this.events.numpad_event(this.numpad_value, this.prev_action); } highlight_numpad_btn($btn, curr_action) { const curr_action_is_highlighted = $btn.hasClass('shadow-inner'); const curr_action_is_action = ['qty', 'discount_percentage', 'rate', 'done'].includes(curr_action); if (!curr_action_is_highlighted) { $btn.addClass('shadow-inner bg-selected'); } if (this.prev_action === curr_action && curr_action_is_highlighted) { // if Qty is pressed twice $btn.removeClass('shadow-inner bg-selected'); } if (this.prev_action && this.prev_action !== curr_action && curr_action_is_action) { // Order: Qty -> Rate then remove Qty highlight const prev_btn = $(`[data-button-value='${this.prev_action}']`); prev_btn.removeClass('shadow-inner bg-selected'); } if (!curr_action_is_action || curr_action === 'done') { // if numbers are clicked setTimeout(() => { $btn.removeClass('shadow-inner bg-selected'); }, 100); } } toggle_numpad(show) { if (show) { this.$totals_section.addClass('d-none'); this.$numpad_section.removeClass('d-none'); } else { this.$totals_section.removeClass('d-none'); this.$numpad_section.addClass('d-none'); } this.reset_numpad(); } reset_numpad() { this.numpad_value = ''; this.prev_action = undefined; this.$numpad_section.find('.shadow-inner').removeClass('shadow-inner bg-selected'); } toggle_numpad_field_edit(fieldname) { if (['qty', 'discount_percentage', 'rate'].includes(fieldname)) { this.$numpad_section.find(`[data-button-value="${fieldname}"]`).click(); } } toggle_customer_info(show) { if (show) { this.$cart_container.addClass('d-none') this.$customer_section.addClass('flex-1 scroll-y').removeClass('mb-0 border pr-4 pl-4') this.$customer_section.find('.icon').addClass('w-24 h-24 text-2xl').removeClass('w-12 h-12 text-md') this.$customer_section.find('.customer-header').removeClass('h-18'); this.$customer_section.find('.customer-details').addClass('sticky z-100 bg-white'); this.$customer_section.find('.customer-name').html( `
${this.customer_info.customer}
` ) this.$customer_section.find('.customer-details').append( `
CONTACT DETAILS
RECENT TRANSACTIONS
` ) // transactions need to be in diff div from sticky elem for scrolling this.$customer_section.append(`
`) this.render_customer_info_form(); this.fetch_customer_transactions(); } else { this.$cart_container.removeClass('d-none'); this.$customer_section.removeClass('flex-1 scroll-y').addClass('mb-0 border pr-4 pl-4'); this.$customer_section.find('.icon').addClass('w-12 h-12 text-md').removeClass('w-24 h-24 text-2xl'); this.$customer_section.find('.customer-header').addClass('h-18') this.$customer_section.find('.customer-details').removeClass('sticky z-100 bg-white'); this.update_customer_section(); } } render_customer_info_form() { const $customer_form = this.$customer_section.find('.customer-form'); const dfs = [{ fieldname: 'email_id', label: __('Email'), fieldtype: 'Data', options: 'email', placeholder: __("Enter customer's email") },{ fieldname: 'mobile_no', label: __('Phone Number'), fieldtype: 'Data', placeholder: __("Enter customer's phone number") },{ fieldname: 'loyalty_program', label: __('Loyalty Program'), fieldtype: 'Link', options: 'Loyalty Program', placeholder: __("Select Loyalty Program") },{ fieldname: 'loyalty_points', label: __('Loyalty Points'), fieldtype: 'Int', read_only: 1 }]; const me = this; dfs.forEach(df => { this[`customer_${df.fieldname}_field`] = frappe.ui.form.make_control({ df: { ...df, onchange: handle_customer_field_change, }, parent: $customer_form.find(`.${df.fieldname}-field`), render_input: true, }); this[`customer_${df.fieldname}_field`].set_value(this.customer_info[df.fieldname]); }) function handle_customer_field_change() { const current_value = me.customer_info[this.df.fieldname]; const current_customer = me.customer_info.customer; if (this.value && current_value != this.value && this.df.fieldname != 'loyalty_points') { frappe.call({ method: 'erpnext.selling.page.point_of_sale.point_of_sale.set_customer_info', args: { fieldname: this.df.fieldname, customer: current_customer, value: this.value }, callback: (r) => { if(!r.exc) { me.customer_info[this.df.fieldname] = this.value; frappe.show_alert({ message: __("Customer contact updated successfully."), indicator: 'green' }); frappe.utils.play_sound("submit"); } } }); } } } fetch_customer_transactions() { frappe.db.get_list('POS Invoice', { filters: { customer: this.customer_info.customer, docstatus: 1 }, fields: ['name', 'grand_total', 'status', 'posting_date', 'posting_time', 'currency'], limit: 20 }).then((res) => { const transaction_container = this.$customer_section.find('.customer-transactions'); if (!res.length) { transaction_container.removeClass('flex-1 border rounded').html( `
No recent transactions found
` ) return; }; const elapsed_time = moment(res[0].posting_date+" "+res[0].posting_time).fromNow(); this.$customer_section.find('.last-transacted-on').html(`Last transacted ${elapsed_time}`); res.forEach(invoice => { const posting_datetime = moment(invoice.posting_date+" "+invoice.posting_time).format("Do MMMM, h:mma"); let indicator_color = ''; if (in_list(['Paid', 'Consolidated'], invoice.status)) (indicator_color = 'green'); if (invoice.status === 'Draft') (indicator_color = 'red'); if (invoice.status === 'Return') (indicator_color = 'grey'); transaction_container.append( `
${invoice.name}
${posting_datetime}
${format_currency(invoice.grand_total, invoice.currency, 0) || 0}
${invoice.status}
` ) }); }) } load_invoice() { const frm = this.events.get_frm(); this.fetch_customer_details(frm.doc.customer).then(() => { this.events.customer_details_updated(this.customer_info); this.update_customer_section(); }) this.$cart_items_wrapper.html(''); if (frm.doc.items.length) { frm.doc.items.forEach(item => { this.update_item_html(item); }); } else { this.make_no_items_placeholder(); this.highlight_checkout_btn(false); } this.update_totals_section(frm); if(frm.doc.docstatus === 1) { this.$totals_section.find('.checkout-btn').addClass('d-none'); this.$totals_section.find('.edit-cart-btn').addClass('d-none'); this.$totals_section.find('.grand-total').removeClass('border-b-grey'); } else { this.$totals_section.find('.checkout-btn').removeClass('d-none'); this.$totals_section.find('.edit-cart-btn').addClass('d-none'); this.$totals_section.find('.grand-total').addClass('border-b-grey'); } this.toggle_component(true); } toggle_component(show) { show ? this.$component.removeClass('d-none') : this.$component.addClass('d-none'); } }