diff --git a/posnext/fixtures/custom_field.json b/posnext/fixtures/custom_field.json index b8ae7ff..1dad5dc 100644 --- a/posnext/fixtures/custom_field.json +++ b/posnext/fixtures/custom_field.json @@ -391,6 +391,62 @@ "unique": 0, "width": null }, + { + "allow_in_quick_entry": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "collapsible_depends_on": null, + "columns": 0, + "default": null, + "depends_on": null, + "description": null, + "docstatus": 0, + "doctype": "Custom Field", + "dt": "POS Profile", + "fetch_from": null, + "fetch_if_empty": 0, + "fieldname": "custom_notification_message_whatsapp", + "fieldtype": "Check", + "hidden": 0, + "hide_border": 0, + "hide_days": 0, + "hide_seconds": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_preview": 0, + "in_standard_filter": 0, + "insert_after": "custom_mobile_number_based_customer", + "is_system_generated": 0, + "is_virtual": 0, + "label": "Notification Message Whatsapp", + "length": 0, + "link_filters": null, + "mandatory_depends_on": null, + "modified": "2025-04-08 09:29:23.094086", + "module": "Posnext", + "name": "POS Profile-custom_notification_message_whatsapp", + "no_copy": 0, + "non_negative": 0, + "options": null, + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "print_width": null, + "read_only": 0, + "read_only_depends_on": null, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "show_dashboard": 0, + "sort_options": 0, + "translatable": 0, + "unique": 0, + "width": null + }, { "allow_in_quick_entry": 0, "allow_on_submit": 0, diff --git a/posnext/public/js/pos_past_order_summary.js b/posnext/public/js/pos_past_order_summary.js index ec71778..3f9e165 100644 --- a/posnext/public/js/pos_past_order_summary.js +++ b/posnext/public/js/pos_past_order_summary.js @@ -160,78 +160,109 @@ posnext.PointOfSale.PastOrderSummary = class { } bind_events() { - this.$summary_container.on('click', '.return-btn', () => { - this.events.process_return(this.doc.name); - this.toggle_component(false); - this.$component.find('.no-summary-placeholder').css('display', 'flex'); - this.$summary_wrapper.css('display', 'none'); - }); - - this.$summary_container.on('click', '.edit-btn', () => { - this.events.edit_order(this.doc.name); - this.toggle_component(false); - this.$component.find('.no-summary-placeholder').css('display', 'flex'); - this.$summary_wrapper.css('display', 'none'); - }); - - this.$summary_container.on('click', '.delete-btn', () => { - this.events.delete_order(this.doc.name); - this.show_summary_placeholder(); - }); - - this.$summary_container.on('click', '.send-btn', () => { - // this.events.delete_order(this.doc.name); - // this.show_summary_placeholder(); - console.log(this.pos_profile) - var field_names = this.pos_profile.custom_whatsapp_field_names.map(x => this.doc[x.field_names.toString()]); - console.log(field_names) - console.log(field_names.join(",")) - var message = "https://wa.me/" + this.doc.customer +"?text=" - message += formatString(this.pos_profile.custom_whatsapp_message, field_names); - console.log(message) - // message += "Hello, here is the file you requested." - frappe.call({ - method: "posnext.posnext.page.posnext.point_of_sale.generate_pdf_and_save", - args: { - docname: this.doc.name, - doctype: this.doc.doctype, - print_format: this.pos_profile.print_format - }, - freeze: true, - freeze_message: "Creating file then send to whatsapp thru link....", - callback: function (r) { - message += "Please Find your invoice here \n "+window.origin+r.message.file_url - window.open(message) - } - }) - // this.toggle_component(false); - // this.$component.find('.no-summary-placeholder').removeClass('d-none'); - // this.$summary_wrapper.addClass('d-none'); - }); - function formatString(str, args) { - return str.replace(/{(\d+)}/g, function(match, number) { - return typeof args[number] !== 'undefined' - ? args[number] - : match; - }); - } - - this.$summary_container.on('click', '.new-btn', () => { - this.events.new_order(); - this.toggle_component(false); - this.$component.find('.no-summary-placeholder').css('display', 'flex'); - this.$summary_wrapper.css('display', 'none'); - }); - - this.$summary_container.on('click', '.email-btn', () => { - this.email_dialog.fields_dict.email_id.set_value(this.customer_email); - this.email_dialog.show(); - }); + this.$summary_container.on('click', '.return-btn', () => { + this.events.process_return(this.doc.name); + this.toggle_component(false); + this.$component.find('.no-summary-placeholder').css('display', 'flex'); + this.$summary_wrapper.css('display', 'none'); + }); + + this.$summary_container.on('click', '.edit-btn', () => { + this.events.edit_order(this.doc.name); + this.toggle_component(false); + this.$component.find('.no-summary-placeholder').css('display', 'flex'); + this.$summary_wrapper.css('display', 'none'); + }); + + this.$summary_container.on('click', '.delete-btn', () => { + this.events.delete_order(this.doc.name); + this.show_summary_placeholder(); + }); + + this.$summary_container.on('click', '.send-btn', () => { + if (!this.pos_profile.custom_notification_message_whatsapp) { + frappe.show_alert({ + message: __('WhatsApp notification is not enabled in POS Profile'), + indicator: 'orange' + }); + return; + } + + if (!this.doc.customer) { + frappe.throw(__('Please select a customer first')); + return; + } + + frappe.db.get_value('Customer', this.doc.customer, 'mobile_no') + .then(({ message }) => { + if (message.mobile_no) { + const mobile_no = message.mobile_no.replace(/[^0-9]/g, ''); + const whatsapp_message = "https://wa.me/" + mobile_no + "?text="; + + // Get the print URL directly + const print_url = frappe.urllib.get_full_url( + '/printview?doctype=' + encodeURIComponent(this.doc.doctype) + + '&name=' + encodeURIComponent(this.doc.name) + + '&format=' + encodeURIComponent(this.pos_profile.print_format) + + '&no_letterhead=0' + + '&_lang=' + encodeURIComponent(frappe.boot.lang) + + '&trigger_print=1' + ); + + const final_message = whatsapp_message + + encodeURIComponent("Please find your invoice here \n" + print_url); + window.open(final_message); + } else { + var field_values = this.pos_profile.custom_whatsapp_field_names.map(x => this.doc[x.field_name]); + + var message_body = formatString(this.pos_profile.custom_whatsapp_message, field_values); + + const print_url = frappe.urllib.get_full_url( + '/printview?doctype=' + encodeURIComponent(this.doc.doctype) + + '&name=' + encodeURIComponent(this.doc.name) + + '&format=' + encodeURIComponent(this.pos_profile.print_format) + + '&no_letterhead=0' + + '&_lang=' + encodeURIComponent(frappe.boot.lang) + + '&trigger_print=1' + ); + + message_body += "\n\nPlease find your invoice here:\n" + print_url; + + var encoded_message = encodeURIComponent(message_body); + + var phone_number = this.doc.customer; + + var whatsapp_url = "https://wa.me/" + phone_number + "?text=" + encoded_message; + + window.open(whatsapp_url, '_blank'); + } + }); + }); + + function formatString(str, args) { + return str.replace(/{(\d+)}/g, function(match, number) { + return typeof args[number] !== 'undefined' + ? args[number] + : match; + }); + } - this.$summary_container.on('click', '.print-btn', () => { - this.print_receipt(); - }); - } + this.$summary_container.on('click', '.new-btn', () => { + this.events.new_order(); + this.toggle_component(false); + this.$component.find('.no-summary-placeholder').css('display', 'flex'); + this.$summary_wrapper.css('display', 'none'); + }); + + this.$summary_container.on('click', '.email-btn', () => { + this.email_dialog.fields_dict.email_id.set_value(this.customer_email); + this.email_dialog.show(); + }); + + this.$summary_container.on('click', '.print-btn', () => { + this.print_receipt(); + }); + } print_receipt() { const frm = this.events.get_frm();