Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1 | frappe.provide("erpnext.pos"); |
| 2 | {% include "erpnext/public/js/controllers/taxes_and_totals.js" %} |
| 3 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 4 | frappe.pages['pos'].on_page_load = function (wrapper) { |
Rushabh Mehta | 4c36d73 | 2015-01-01 15:59:34 +0530 | [diff] [blame] | 5 | var page = frappe.ui.make_app_page({ |
Rushabh Mehta | 72e1719 | 2014-08-08 15:30:49 +0530 | [diff] [blame] | 6 | parent: wrapper, |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 7 | title: __('Point of Sale'), |
Rushabh Mehta | 72e1719 | 2014-08-08 15:30:49 +0530 | [diff] [blame] | 8 | single_column: true |
| 9 | }); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 10 | |
| 11 | wrapper.pos = new erpnext.pos.PointOfSale(wrapper) |
Rushabh Mehta | 72e1719 | 2014-08-08 15:30:49 +0530 | [diff] [blame] | 12 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 13 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 14 | frappe.pages['pos'].refresh = function (wrapper) { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 15 | window.onbeforeunload = function () { |
| 16 | return wrapper.pos.beforeunload() |
| 17 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 18 | } |
| 19 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 20 | erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({ |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 21 | init: function (wrapper) { |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 22 | this.page_len = 20; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 23 | this.freeze = false; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 24 | this.page = wrapper.page; |
| 25 | this.wrapper = $(wrapper).find('.page-content'); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 26 | this.set_indicator(); |
| 27 | this.onload(); |
| 28 | this.make_menu_list(); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 29 | this.bind_events(); |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 30 | this.bind_items_event(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 31 | this.si_docs = this.get_doc_from_localstorage(); |
| 32 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 33 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 34 | beforeunload: function (e) { |
| 35 | if (this.connection_status == false && frappe.get_route()[0] == "pos") { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 36 | e = e || window.event; |
| 37 | |
| 38 | // For IE and Firefox prior to version 4 |
| 39 | if (e) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 40 | e.returnValue = __("You are in offline mode. You will not be able to reload until you have network."); |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 41 | return |
| 42 | } |
| 43 | |
| 44 | // For Safari |
| 45 | return __("You are in offline mode. You will not be able to reload until you have network."); |
| 46 | } |
| 47 | }, |
| 48 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 49 | check_internet_connection: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 50 | var me = this; |
| 51 | //Check Internet connection after every 30 seconds |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 52 | setInterval(function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 53 | me.set_indicator(); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 54 | }, 5000) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 55 | }, |
| 56 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 57 | set_indicator: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 58 | var me = this; |
| 59 | // navigator.onLine |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 60 | this.connection_status = false; |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 61 | this.page.set_indicator(__("Offline"), "grey") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 62 | frappe.call({ |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 63 | method: "frappe.handler.ping", |
| 64 | callback: function (r) { |
| 65 | if (r.message) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 66 | me.connection_status = true; |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 67 | me.page.set_indicator(__("Online"), "green") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | }) |
| 71 | }, |
| 72 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 73 | onload: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 74 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 75 | this.get_data_from_server(function () { |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 76 | me.make_control(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 77 | me.create_new(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 78 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 79 | }, |
| 80 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 81 | make_menu_list: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 82 | var me = this; |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 83 | this.page.clear_menu(); |
Faris Ansari | 769b6ba | 2017-05-16 07:31:10 +0530 | [diff] [blame] | 84 | |
| 85 | // for mobile |
| 86 | this.page.add_menu_item(__("Pay"), function () { |
| 87 | me.validate(); |
| 88 | me.update_paid_amount_status(true); |
| 89 | me.create_invoice(); |
| 90 | me.make_payment(); |
| 91 | }).addClass('visible-xs'); |
| 92 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 93 | this.page.add_menu_item(__("New Sales Invoice"), function () { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 94 | me.save_previous_entry(); |
| 95 | me.create_new(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 96 | }) |
| 97 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 98 | this.page.add_menu_item(__("Sync Master Data"), function () { |
| 99 | me.get_data_from_server(function () { |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 100 | me.load_data(false); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 101 | me.make_item_list(); |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 102 | me.set_missing_values(); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 103 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 104 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 105 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 106 | this.page.add_menu_item(__("Sync Offline Invoices"), function () { |
Rohit Waghchaure | 82be020 | 2016-10-04 12:46:37 +0530 | [diff] [blame] | 107 | me.sync_sales_invoice() |
| 108 | }); |
| 109 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 110 | this.page.add_menu_item(__("POS Profile"), function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 111 | frappe.set_route('List', 'POS Profile'); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 112 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 113 | }, |
| 114 | |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 115 | email_prompt: function() { |
| 116 | var me = this; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 117 | var fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288}, |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 118 | {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"}, |
| 119 | {fieldtype: "Section Break"}, |
| 120 | {label:__("Subject"), fieldtype:"Data", reqd: 1, |
| 121 | fieldname:"subject",length:524288}, |
| 122 | {fieldtype: "Section Break"}, |
| 123 | {label:__("Message"), fieldtype:"Text Editor", reqd: 1, |
| 124 | fieldname:"content"}, |
| 125 | {fieldtype: "Section Break"}, |
| 126 | {fieldtype: "Column Break"}]; |
| 127 | |
| 128 | this.email_dialog = new frappe.ui.Dialog({ |
| 129 | title: "Email", |
| 130 | fields: fields, |
| 131 | primary_action_label: __("Send"), |
| 132 | primary_action: function() { |
| 133 | me.send_action(); |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | this.email_dialog.show() |
| 138 | }, |
| 139 | |
| 140 | send_action: function() { |
| 141 | this.email_queue = this.get_email_queue() |
| 142 | this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values()) |
| 143 | this.update_email_queue() |
| 144 | this.email_dialog.hide() |
| 145 | }, |
| 146 | |
| 147 | update_email_queue: function () { |
| 148 | try { |
| 149 | localStorage.setItem('email_queue', JSON.stringify(this.email_queue)); |
| 150 | } catch (e) { |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 151 | frappe.throw(__("LocalStorage is full, did not save")) |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 152 | } |
| 153 | }, |
| 154 | |
| 155 | get_email_queue: function () { |
| 156 | try { |
| 157 | return JSON.parse(localStorage.getItem('email_queue')) || {}; |
| 158 | } catch (e) { |
| 159 | return {} |
| 160 | } |
| 161 | }, |
| 162 | |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 163 | get_customers_details: function () { |
| 164 | try { |
| 165 | return JSON.parse(localStorage.getItem('customer_details')) || {}; |
| 166 | } catch (e) { |
| 167 | return {} |
| 168 | } |
| 169 | }, |
| 170 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 171 | dialog_actions: function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 172 | var me = this; |
| 173 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 174 | $(this.list_body).find('.list-select-all').click(function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 175 | me.removed_items = []; |
| 176 | $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked")) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 177 | if ($(this).is(":checked")) { |
| 178 | $.each(me.si_docs, function (index, data) { |
| 179 | for (key in data) { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 180 | me.removed_items.push(key) |
| 181 | } |
| 182 | }) |
| 183 | } |
| 184 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 185 | me.toggle_delete_button(); |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 186 | }) |
| 187 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 188 | $(this.list_body).find('.list-delete').click(function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 189 | me.name = $(this).parent().parent().attr('invoice-name'); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 190 | if ($(this).is(":checked")) { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 191 | me.removed_items.push(me.name); |
| 192 | } else { |
| 193 | me.removed_items.pop(me.name) |
| 194 | } |
| 195 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 196 | me.toggle_delete_button(); |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 197 | }) |
| 198 | }, |
| 199 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 200 | edit_record: function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 201 | var me = this; |
| 202 | |
| 203 | doc_data = this.get_invoice_doc(this.si_docs); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 204 | if (doc_data) { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 205 | this.frm.doc = doc_data[0][this.name]; |
| 206 | this.set_missing_values(); |
| 207 | this.refresh(false); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 208 | this.toggle_input_field(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 209 | this.list_dialog && this.list_dialog.hide(); |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 210 | } |
| 211 | }, |
| 212 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 213 | delete_records: function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 214 | var me = this; |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 215 | this.validate_list() |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 216 | this.remove_doc_from_localstorage() |
| 217 | this.update_localstorage(); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 218 | // this.dialog_actions(); |
| 219 | this.toggle_delete_button(); |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 220 | }, |
| 221 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 222 | validate_list: function() { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 223 | var me = this; |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 224 | this.si_docs = this.get_submitted_invoice() |
| 225 | $.each(this.removed_items, function(index, name){ |
| 226 | $.each(me.si_docs, function(key, data){ |
| 227 | if(me.si_docs[key][name] && me.si_docs[key][name].offline_pos_name == name ){ |
| 228 | frappe.throw(__("Submitted orders can not be deleted")) |
| 229 | } |
| 230 | }) |
| 231 | }) |
| 232 | }, |
| 233 | |
| 234 | toggle_delete_button: function () { |
| 235 | var me = this; |
| 236 | if(this.pos_profile_data["allow_delete"]) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 237 | if (this.removed_items && this.removed_items.length > 0) { |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 238 | $(this.page.wrapper).find('.btn-danger').show(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 239 | } else { |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 240 | $(this.page.wrapper).find('.btn-danger').hide(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | }, |
| 244 | |
| 245 | get_doctype_status: function (doc) { |
| 246 | if (doc.docstatus == 0) { |
| 247 | return { status: "Draft", indicator: "red" } |
| 248 | } else if (doc.outstanding_amount == 0) { |
| 249 | return { status: "Paid", indicator: "green" } |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 250 | } else { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 251 | return { status: "Submitted", indicator: "blue" } |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 252 | } |
| 253 | }, |
| 254 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 255 | set_missing_values: function () { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 256 | var me = this; |
| 257 | doc = JSON.parse(localStorage.getItem('doc')) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 258 | if (this.frm.doc.payments.length == 0) { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 259 | this.frm.doc.payments = doc.payments; |
Rohit Waghchaure | b77a105 | 2016-08-01 18:35:18 +0530 | [diff] [blame] | 260 | this.calculate_outstanding_amount(); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 261 | } |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 262 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 263 | if (this.frm.doc.customer) { |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 264 | this.party_field.$input.val(this.frm.doc.customer); |
| 265 | } |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 266 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 267 | if (!this.frm.doc.write_off_account) { |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 268 | this.frm.doc.write_off_account = doc.write_off_account |
| 269 | } |
| 270 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 271 | if (!this.frm.doc.account_for_change_amount) { |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 272 | this.frm.doc.account_for_change_amount = doc.account_for_change_amount |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 273 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 274 | }, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 275 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 276 | get_invoice_doc: function (si_docs) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 277 | var me = this; |
| 278 | this.si_docs = this.get_doc_from_localstorage(); |
| 279 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 280 | return $.grep(this.si_docs, function (data) { |
| 281 | for (key in data) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 282 | return key == me.name |
| 283 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 284 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 285 | }, |
| 286 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 287 | get_data_from_server: function (callback) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 288 | var me = this; |
| 289 | frappe.call({ |
| 290 | method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data", |
| 291 | freeze: true, |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 292 | freeze_message: __("Master data syncing, it might take some time"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 293 | callback: function (r) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 294 | localStorage.setItem('doc', JSON.stringify(r.message.doc)); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 295 | me.init_master_data(r) |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 296 | me.set_interval_for_si_sync(); |
| 297 | me.check_internet_connection(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 298 | if (callback) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 299 | callback(); |
| 300 | } |
| 301 | } |
| 302 | }) |
| 303 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 304 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 305 | init_master_data: function (r) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 306 | var me = this; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 307 | this.doc = JSON.parse(localStorage.getItem('doc')); |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 308 | this.meta = r.message.meta; |
| 309 | this.item_data = r.message.items; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 310 | this.item_groups = r.message.item_groups; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 311 | this.customers = r.message.customers; |
| 312 | this.serial_no_data = r.message.serial_no_data; |
| 313 | this.batch_no_data = r.message.batch_no_data; |
| 314 | this.tax_data = r.message.tax_data; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 315 | this.contacts = r.message.contacts; |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 316 | this.address = r.message.address || {}; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 317 | this.price_list_data = r.message.price_list_data; |
| 318 | this.bin_data = r.message.bin_data; |
| 319 | this.pricing_rules = r.message.pricing_rules; |
| 320 | this.print_template = r.message.print_template; |
| 321 | this.pos_profile_data = r.message.pos_profile; |
| 322 | this.default_customer = r.message.default_customer || null; |
rohitwaghchaure | 34e820d | 2016-12-20 16:54:24 +0530 | [diff] [blame] | 323 | this.print_settings = locals[":Print Settings"]["Print Settings"]; |
Rohit Waghchaure | d567e57 | 2016-12-21 13:18:36 +0530 | [diff] [blame] | 324 | this.letter_head = (this.pos_profile_data.length > 0) ? frappe.boot.letter_heads[this.pos_profile_data[letter_head]] : {}; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 325 | }, |
| 326 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 327 | save_previous_entry: function () { |
| 328 | if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) { |
| 329 | this.create_invoice(); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 330 | } |
| 331 | }, |
| 332 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 333 | create_new: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 334 | var me = this; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 335 | this.frm = {} |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 336 | this.name = null; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 337 | this.load_data(true); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 338 | this.setup(); |
Rohit Waghchaure | 6116512 | 2017-05-02 13:04:08 +0530 | [diff] [blame] | 339 | this.set_default_customer() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 340 | }, |
| 341 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 342 | load_data: function (load_doc) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 343 | var me = this; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 344 | |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 345 | this.items = this.item_data; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 346 | this.actual_qty_dict = {}; |
| 347 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 348 | if (load_doc) { |
| 349 | this.frm.doc = JSON.parse(localStorage.getItem('doc')); |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 350 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 351 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 352 | $.each(this.meta, function (i, data) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 353 | frappe.meta.sync(data) |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 354 | locals["DocType"][data.name] = data; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 355 | }) |
| 356 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 357 | this.print_template_data = frappe.render_template("print_template", { |
| 358 | content: this.print_template, |
Faris Ansari | 29d64ca | 2017-05-23 17:12:51 +0530 | [diff] [blame] | 359 | title: "POS", |
| 360 | base_url: frappe.urllib.get_base_url(), |
| 361 | print_css: frappe.boot.print_css, |
| 362 | print_settings: this.print_settings, |
| 363 | header: this.letter_head.header, |
| 364 | footer: this.letter_head.footer, |
Makarand Bauskar | 724cc35 | 2017-05-23 17:43:42 +0530 | [diff] [blame] | 365 | landscape: false, |
| 366 | columns: [] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 367 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 368 | }, |
| 369 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 370 | setup: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 371 | this.make(); |
| 372 | this.set_primary_action(); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 373 | this.party_field.$input.attr('disabled', false); |
| 374 | if(this.selected_row) { |
| 375 | this.selected_row.hide() |
| 376 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 377 | }, |
| 378 | |
Rohit Waghchaure | 6116512 | 2017-05-02 13:04:08 +0530 | [diff] [blame] | 379 | set_default_customer: function() { |
| 380 | if (this.default_customer && !this.frm.doc.customer) { |
| 381 | this.party_field.$input.val(this.default_customer); |
| 382 | this.frm.doc.customer = this.default_customer; |
| 383 | this.numeric_keypad.show(); |
| 384 | this.toggle_list_customer(false) |
| 385 | this.toggle_item_cart(true) |
| 386 | } |
| 387 | }, |
| 388 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 389 | set_transaction_defaults: function (party) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 390 | var me = this; |
| 391 | this.party = party; |
| 392 | this.price_list = (party == "Customer" ? |
| 393 | this.frm.doc.selling_price_list : this.frm.doc.buying_price_list); |
| 394 | this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list"); |
| 395 | this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase"); |
| 396 | }, |
| 397 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 398 | make: function () { |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 399 | this.make_item_list(); |
| 400 | this.make_discount_field() |
| 401 | }, |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 402 | |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 403 | make_control: function() { |
| 404 | this.frm = {} |
| 405 | this.frm.doc = this.doc |
| 406 | this.set_transaction_defaults("Customer"); |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 407 | this.frm.doc["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false, |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 408 | this.wrapper.html(frappe.render_template("pos", this.frm.doc)); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 409 | this.make_search(); |
| 410 | this.make_customer(); |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 411 | this.make_list_customers(); |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 412 | this.bind_numeric_keypad(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 413 | }, |
| 414 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 415 | make_search: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 416 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 417 | this.serach_item = frappe.ui.form.make_control({ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 418 | df: { |
| 419 | "fieldtype": "Data", |
| 420 | "label": "Item", |
| 421 | "fieldname": "pos_item", |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 422 | "placeholder": __("Search Item") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 423 | }, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 424 | parent: this.wrapper.find(".search-item"), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 425 | only_input: true, |
| 426 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 427 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 428 | this.serach_item.make_input(); |
Faris Ansari | 8fbc08f | 2017-08-28 16:52:20 +0530 | [diff] [blame] | 429 | |
Revant Nandgaonkar | 2f7cb82 | 2017-08-26 09:58:15 +0530 | [diff] [blame] | 430 | this.serach_item.$input.on("keypress", function (event) { |
Faris Ansari | 8fbc08f | 2017-08-28 16:52:20 +0530 | [diff] [blame] | 431 | |
| 432 | clearTimeout(me.last_search_timeout); |
| 433 | me.last_search_timeout = setTimeout(() => { |
| 434 | if((me.serach_item.$input.val() != "") || (event.which == 13)) { |
| 435 | me.items = me.get_items(); |
| 436 | me.make_item_list(); |
| 437 | } |
| 438 | }, 400); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 439 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 440 | |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 441 | this.search_item_group = this.wrapper.find('.search-item-group'); |
Rohit Waghchaure | d7de3c6 | 2017-04-17 13:36:08 +0530 | [diff] [blame] | 442 | sorted_item_groups = this.get_sorted_item_groups() |
| 443 | var dropdown_html = sorted_item_groups.map(function(item_group) { |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 444 | return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>"; |
| 445 | }).join(""); |
| 446 | |
| 447 | this.search_item_group.find('.dropdown-menu').html(dropdown_html); |
| 448 | |
| 449 | this.search_item_group.on('click', '.dropdown-menu a', function() { |
| 450 | me.selected_item_group = $(this).attr('data-value'); |
| 451 | me.search_item_group.find('.dropdown-text').text(me.selected_item_group); |
| 452 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 453 | me.page_len = 20; |
| 454 | me.items = me.get_items(); |
| 455 | me.make_item_list(); |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 456 | }) |
| 457 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 458 | me.toggle_more_btn(); |
| 459 | |
| 460 | this.wrapper.on("click", ".btn-more", function() { |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 461 | me.page_len += 20; |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 462 | me.items = me.get_items(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 463 | me.make_item_list(); |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 464 | me.toggle_more_btn(); |
| 465 | }); |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 466 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 467 | this.page.wrapper.on("click", ".edit-customer-btn", function() { |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 468 | me.update_customer() |
| 469 | }) |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 470 | }, |
| 471 | |
Rohit Waghchaure | d7de3c6 | 2017-04-17 13:36:08 +0530 | [diff] [blame] | 472 | get_sorted_item_groups: function() { |
| 473 | list = {} |
| 474 | $.each(this.item_groups, function(i, data) { |
| 475 | list[i] = data[0] |
| 476 | }) |
| 477 | |
| 478 | return Object.keys(list).sort(function(a,b){return list[a]-list[b]}) |
| 479 | }, |
| 480 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 481 | toggle_more_btn: function() { |
| 482 | if(!this.items || this.items.length <= this.page_len) { |
| 483 | this.wrapper.find(".btn-more").hide(); |
| 484 | } else { |
| 485 | this.wrapper.find(".btn-more").show(); |
| 486 | } |
| 487 | }, |
| 488 | |
| 489 | toggle_totals_area: function(show) { |
| 490 | |
Faris Ansari | 07c9f35 | 2017-03-09 17:03:14 +0530 | [diff] [blame] | 491 | if(show === undefined) { |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 492 | show = this.is_totals_area_collapsed; |
| 493 | } |
| 494 | |
| 495 | var totals_area = this.wrapper.find('.totals-area'); |
| 496 | totals_area.find('.net-total-area, .tax-area, .discount-amount-area') |
| 497 | .toggle(show); |
| 498 | |
| 499 | if(show) { |
| 500 | totals_area.find('.collapse-btn i') |
| 501 | .removeClass('octicon-chevron-down') |
| 502 | .addClass('octicon-chevron-up'); |
| 503 | } else { |
| 504 | totals_area.find('.collapse-btn i') |
| 505 | .removeClass('octicon-chevron-up') |
| 506 | .addClass('octicon-chevron-down'); |
| 507 | } |
| 508 | |
| 509 | this.is_totals_area_collapsed = !show; |
| 510 | }, |
| 511 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 512 | make_list_customers: function () { |
| 513 | var me = this; |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 514 | this.list_customers_btn = this.page.wrapper.find('.list-customers-btn'); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 515 | this.add_customer_btn = this.wrapper.find('.add-customer-btn'); |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 516 | this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 517 | this.list_customers = this.wrapper.find('.list-customers'); |
Rohit Waghchaure | e30f83a | 2017-02-24 18:02:50 +0530 | [diff] [blame] | 518 | this.numeric_keypad = this.wrapper.find('.numeric_keypad'); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 519 | this.list_customers_btn.addClass("view_customer") |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 520 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 521 | me.render_list_customers(); |
| 522 | me.toggle_totals_area(false); |
| 523 | |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 524 | this.page.wrapper.on('click', '.list-customers-btn', function() { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 525 | $(this).toggleClass("view_customer"); |
| 526 | if($(this).hasClass("view_customer")) { |
| 527 | me.render_list_customers(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 528 | me.list_customers.show(); |
| 529 | me.pos_bill.hide(); |
Rohit Waghchaure | e30f83a | 2017-02-24 18:02:50 +0530 | [diff] [blame] | 530 | me.numeric_keypad.hide(); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 531 | me.toggle_delete_button() |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 532 | } else { |
| 533 | if(me.frm.doc.docstatus == 0) { |
| 534 | me.party_field.$input.attr('disabled', false); |
| 535 | } |
| 536 | me.pos_bill.show(); |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 537 | me.toggle_totals_area(false); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 538 | me.toggle_delete_button() |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 539 | me.list_customers.hide(); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 540 | me.numeric_keypad.show(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 541 | } |
| 542 | }); |
| 543 | this.add_customer_btn.on('click', function() { |
| 544 | me.save_previous_entry(); |
| 545 | me.create_new(); |
| 546 | me.refresh(); |
| 547 | me.set_focus(); |
| 548 | }); |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 549 | this.pos_bill.on('click', '.collapse-btn', function() { |
| 550 | me.toggle_totals_area(); |
| 551 | }); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 552 | }, |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 553 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 554 | bind_numeric_keypad: function() { |
| 555 | var me = this; |
| 556 | $(this.numeric_keypad).find('.pos-operation').on('click', function(){ |
| 557 | me.numeric_val = ''; |
| 558 | }) |
| 559 | |
| 560 | $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){ |
| 561 | me.numeric_id = $(this).attr("id") || me.numeric_id; |
| 562 | me.val = $(this).attr("val") |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 563 | if(me.numeric_id) { |
| 564 | me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id) |
| 565 | } |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 566 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 567 | if(me.val && me.numeric_id) { |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 568 | me.numeric_val += me.val; |
| 569 | me.selected_field.val(flt(me.numeric_val)) |
| 570 | me.selected_field.trigger("change") |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 571 | // me.render_selected_item() |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 572 | } |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 573 | |
| 574 | if(me.numeric_id && $(this).hasClass('pos-operation')) { |
| 575 | me.numeric_keypad.find('button.pos-operation').removeClass('active'); |
| 576 | $(this).addClass('active'); |
| 577 | |
| 578 | me.selected_row.find('.pos-list-row').removeClass('active'); |
| 579 | me.selected_field.closest('.pos-list-row').addClass('active'); |
| 580 | } |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 581 | }) |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 582 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 583 | $(this.numeric_keypad).find('.numeric-del').click(function(){ |
rohitwaghchaure | 8f51a5e | 2017-06-23 18:17:04 +0530 | [diff] [blame] | 584 | if(me.numeric_id) { |
| 585 | me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id) |
| 586 | me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1); |
| 587 | me.selected_field.val(me.numeric_val); |
| 588 | me.selected_field.trigger("change") |
| 589 | } else { |
| 590 | //Remove an item from the cart, if focus is at selected item |
| 591 | me.remove_selected_item() |
| 592 | } |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 593 | }) |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 594 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 595 | $(this.numeric_keypad).find('.pos-pay').click(function(){ |
| 596 | me.validate(); |
| 597 | me.update_paid_amount_status(true); |
| 598 | me.create_invoice(); |
| 599 | me.make_payment(); |
| 600 | }) |
| 601 | }, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 602 | |
rohitwaghchaure | 8f51a5e | 2017-06-23 18:17:04 +0530 | [diff] [blame] | 603 | remove_selected_item: function() { |
| 604 | this.remove_item = [] |
| 605 | idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx") |
| 606 | this.remove_item.push(idx) |
| 607 | this.remove_zero_qty_item() |
| 608 | this.update_paid_amount_status(false) |
| 609 | }, |
| 610 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 611 | render_list_customers: function () { |
| 612 | var me = this; |
| 613 | |
| 614 | this.removed_items = []; |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 615 | // this.list_customers.empty(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 616 | this.si_docs = this.get_doc_from_localstorage(); |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 617 | if (!this.si_docs.length) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 618 | return; |
| 619 | } |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 620 | |
| 621 | var html = ""; |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 622 | if(this.si_docs.length) { |
| 623 | this.si_docs.forEach(function (data, i) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 624 | for (var key in data) { |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 625 | html += frappe.render_template("pos_invoice_list", { |
| 626 | sr: i + 1, |
| 627 | name: key, |
| 628 | customer: data[key].customer, |
| 629 | paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency), |
| 630 | grand_total: format_currency(data[key].grand_total, me.frm.doc.currency), |
| 631 | data: me.get_doctype_status(data[key]) |
| 632 | }); |
| 633 | } |
| 634 | }); |
| 635 | } |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 636 | this.list_customers.find('.list-customers-table').html(html); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 637 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 638 | this.list_customers.on('click', '.customer-row', function () { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 639 | me.list_customers.hide(); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 640 | me.numeric_keypad.show(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 641 | me.list_customers_btn.toggleClass("view_customer"); |
| 642 | me.pos_bill.show(); |
| 643 | me.list_customers_btn.show(); |
| 644 | me.name = $(this).parents().attr('invoice-name') |
| 645 | me.edit_record(); |
| 646 | }) |
| 647 | |
| 648 | //actions |
| 649 | $(this.wrapper).find('.list-select-all').click(function () { |
| 650 | me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked")) |
| 651 | me.removed_items = []; |
| 652 | if ($(this).is(":checked")) { |
| 653 | $.each(me.si_docs, function (index, data) { |
| 654 | for (key in data) { |
| 655 | me.removed_items.push(key) |
| 656 | } |
| 657 | }); |
| 658 | } |
| 659 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 660 | me.toggle_delete_button(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 661 | }); |
| 662 | |
| 663 | $(this.wrapper).find('.list-delete').click(function () { |
| 664 | me.name = $(this).parent().parent().attr('invoice-name'); |
| 665 | if ($(this).is(":checked")) { |
| 666 | me.removed_items.push(me.name); |
| 667 | } else { |
| 668 | me.removed_items.pop(me.name) |
| 669 | } |
| 670 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 671 | me.toggle_delete_button(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 672 | }); |
| 673 | }, |
| 674 | |
| 675 | bind_delete_event: function() { |
| 676 | var me = this; |
| 677 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 678 | $(this.page.wrapper).on('click', '.btn-danger', function(){ |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 679 | frappe.confirm(__("Delete permanently?"), function () { |
| 680 | me.delete_records(); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 681 | me.list_customers.find('.list-customers-table').html(""); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 682 | me.render_list_customers(); |
| 683 | }) |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 684 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 685 | }, |
| 686 | |
| 687 | set_focus: function () { |
| 688 | if (this.default_customer || this.frm.doc.customer) { |
| 689 | this.serach_item.$input.focus(); |
| 690 | } else { |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 691 | this.party_field.$input.focus(); |
| 692 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 693 | }, |
| 694 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 695 | make_customer: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 696 | var me = this; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 697 | |
| 698 | if(!this.party_field) { |
| 699 | if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) { |
| 700 | $(frappe.render_template('customer_toolbar', { |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 701 | allow_delete: this.pos_profile_data["allow_delete"] |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 702 | })).insertAfter(this.page.$title_area.hide()); |
| 703 | } |
| 704 | |
| 705 | this.party_field = frappe.ui.form.make_control({ |
| 706 | df: { |
| 707 | "fieldtype": "Data", |
| 708 | "options": this.party, |
| 709 | "label": this.party, |
| 710 | "fieldname": this.party.toLowerCase(), |
| 711 | "placeholder": __("Select or add new customer") |
| 712 | }, |
| 713 | parent: this.page.wrapper.find(".party-area"), |
| 714 | only_input: true, |
| 715 | }); |
| 716 | |
| 717 | this.party_field.make_input(); |
| 718 | setTimeout(this.set_focus.bind(this), 500); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 719 | me.toggle_delete_button(); |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 720 | } |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 721 | |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 722 | this.party_field.awesomeplete = |
| 723 | new Awesomplete(this.party_field.$input.get(0), { |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 724 | minChars: 0, |
| 725 | maxItems: 99, |
| 726 | autoFirst: true, |
| 727 | list: [], |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 728 | filter: function (item, input) { |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 729 | if (item.value.includes('is_action')) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 730 | return true; |
| 731 | } |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 732 | |
| 733 | input = input.toLowerCase(); |
| 734 | item = this.get_item(item.value); |
rohitwaghchaure | 56e31d0 | 2017-08-31 14:19:58 +0530 | [diff] [blame] | 735 | return item.searchtext.includes(input) |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 736 | }, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 737 | item: function (item, input) { |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 738 | var d = this.get_item(item.value); |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 739 | var html = "<span>" + __(d.label || d.value) + "</span>"; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 740 | if(d.customer_name) { |
| 741 | html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>'; |
| 742 | } |
| 743 | |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 744 | return $('<li></li>') |
| 745 | .data('item.autocomplete', d) |
| 746 | .html('<a><p>' + html + '</p></a>') |
| 747 | .get(0); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 748 | } |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 749 | }); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 750 | |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 751 | this.prepare_customer_mapper() |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 752 | this.autocomplete_customers(); |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 753 | |
| 754 | this.party_field.$input |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 755 | .on('input', function (e) { |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 756 | me.party_field.awesomeplete.list = me.customers_mapper; |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 757 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 758 | .on('awesomplete-select', function (e) { |
| 759 | var customer = me.party_field.awesomeplete |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 760 | .get_item(e.originalEvent.text.value); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 761 | if (!customer) return; |
| 762 | // create customer link |
| 763 | if (customer.action) { |
| 764 | customer.action.apply(me); |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 765 | return; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 766 | } |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 767 | me.toggle_list_customer(false); |
| 768 | me.toggle_edit_button(true); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 769 | me.update_customer_data(customer); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 770 | me.refresh(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 771 | me.set_focus(); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 772 | me.list_customers_btn.removeClass("view_customer"); |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 773 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 774 | .on('focus', function (e) { |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 775 | $(e.target).val('').trigger('input'); |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 776 | me.toggle_edit_button(false); |
Faris Ansari | 1fe1518 | 2017-03-10 14:50:39 +0530 | [diff] [blame] | 777 | |
| 778 | if(me.frm.doc.items.length) { |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 779 | me.toggle_list_customer(false) |
| 780 | me.toggle_item_cart(true) |
| 781 | } else { |
| 782 | me.toggle_list_customer(true) |
| 783 | me.toggle_item_cart(false) |
| 784 | } |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 785 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 786 | .on("awesomplete-selectcomplete", function (e) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 787 | var item = me.party_field.awesomeplete |
| 788 | .get_item(e.originalEvent.text.value); |
| 789 | // clear text input if item is action |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 790 | if (item.action) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 791 | $(this).val(""); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 792 | } |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 793 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 794 | }, |
| 795 | |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 796 | prepare_customer_mapper: function() { |
| 797 | var me = this; |
| 798 | |
| 799 | this.customers_mapper = this.customers.map(function (c) { |
| 800 | contact = me.contacts[c.name]; |
| 801 | return { |
| 802 | label: c.name, |
| 803 | value: c.name, |
| 804 | customer_name: c.customer_name, |
| 805 | customer_group: c.customer_group, |
| 806 | territory: c.territory, |
| 807 | phone: contact ? contact["phone"] : '', |
Rohit Waghchaure | c849062 | 2017-06-10 12:21:37 +0530 | [diff] [blame] | 808 | mobile_no: contact ? contact["mobile_no"] : '', |
rohitwaghchaure | 56e31d0 | 2017-08-31 14:19:58 +0530 | [diff] [blame] | 809 | email_id: contact ? contact["email_id"] : '', |
| 810 | searchtext: ['customer_name', 'customer_group', 'value', |
| 811 | 'label', 'email_id', 'phone', 'mobile_no'] |
| 812 | .map(key => c[key]).join(' ') |
| 813 | .toLowerCase() |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 814 | } |
| 815 | }); |
| 816 | |
| 817 | this.customers_mapper.push({ |
| 818 | label: "<span class='text-primary link-option'>" |
| 819 | + "<i class='fa fa-plus' style='margin-right: 5px;'></i> " |
| 820 | + __("Create a new Customer") |
| 821 | + "</span>", |
| 822 | value: 'is_action', |
| 823 | action: me.add_customer |
| 824 | }); |
| 825 | }, |
| 826 | |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 827 | autocomplete_customers: function() { |
| 828 | this.party_field.awesomeplete.list = this.customers_mapper; |
| 829 | }, |
| 830 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 831 | toggle_edit_button: function(flag) { |
| 832 | this.page.wrapper.find('.edit-customer-btn').toggle(flag); |
| 833 | }, |
| 834 | |
| 835 | toggle_list_customer: function(flag) { |
| 836 | this.list_customers.toggle(flag); |
| 837 | }, |
| 838 | |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 839 | toggle_item_cart: function(flag) { |
| 840 | this.wrapper.find('.pos-bill-wrapper').toggle(flag); |
| 841 | }, |
| 842 | |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 843 | add_customer: function() { |
| 844 | this.frm.doc.customer = ""; |
rohitwaghchaure | 09483d3 | 2017-05-16 08:51:24 +0530 | [diff] [blame] | 845 | this.update_customer(true); |
| 846 | this.numeric_keypad.show(); |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 847 | }, |
| 848 | |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 849 | update_customer: function (new_customer) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 850 | var me = this; |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 851 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 852 | this.customer_doc = new frappe.ui.Dialog({ |
| 853 | 'title': 'Customer', |
| 854 | fields: [ |
| 855 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 856 | "label": __("Full Name"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 857 | "fieldname": "full_name", |
| 858 | "fieldtype": "Data", |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 859 | "reqd": 1 |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 860 | }, |
| 861 | { |
| 862 | "fieldtype": "Section Break" |
| 863 | }, |
| 864 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 865 | "label": __("Email Id"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 866 | "fieldname": "email_id", |
| 867 | "fieldtype": "Data" |
| 868 | }, |
| 869 | { |
| 870 | "fieldtype": "Column Break" |
| 871 | }, |
| 872 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 873 | "label": __("Contact Number"), |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 874 | "fieldname": "phone", |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 875 | "fieldtype": "Data" |
| 876 | }, |
| 877 | { |
| 878 | "fieldtype": "Section Break" |
| 879 | }, |
| 880 | { |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 881 | "label": __("Address Name"), |
| 882 | "read_only": 1, |
| 883 | "fieldname": "name", |
| 884 | "fieldtype": "Data" |
| 885 | }, |
| 886 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 887 | "label": __("Address Line 1"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 888 | "fieldname": "address_line1", |
| 889 | "fieldtype": "Data" |
| 890 | }, |
| 891 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 892 | "label": __("Address Line 2"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 893 | "fieldname": "address_line2", |
| 894 | "fieldtype": "Data" |
| 895 | }, |
| 896 | { |
| 897 | "fieldtype": "Column Break" |
| 898 | }, |
| 899 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 900 | "label": __("City"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 901 | "fieldname": "city", |
| 902 | "fieldtype": "Data" |
| 903 | }, |
| 904 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 905 | "label": __("State"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 906 | "fieldname": "state", |
| 907 | "fieldtype": "Data" |
| 908 | }, |
| 909 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 910 | "label": __("ZIP Code"), |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 911 | "fieldname": "pincode", |
| 912 | "fieldtype": "Data" |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 913 | }, |
| 914 | { |
| 915 | "label": __("Customer POS Id"), |
| 916 | "fieldname": "customer_pos_id", |
| 917 | "fieldtype": "Data", |
| 918 | "hidden": 1 |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 919 | } |
| 920 | ] |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 921 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 922 | this.customer_doc.show() |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 923 | this.render_address_data() |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 924 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 925 | this.customer_doc.set_primary_action(__("Save"), function () { |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 926 | me.make_offline_customer(new_customer); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 927 | me.pos_bill.show(); |
Rohit Waghchaure | fd37516 | 2017-05-02 18:27:09 +0530 | [diff] [blame] | 928 | me.list_customers.hide(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 929 | }); |
| 930 | }, |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 931 | |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 932 | render_address_data: function() { |
| 933 | var me = this; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 934 | this.address_data = this.address[this.frm.doc.customer] || {}; |
| 935 | if(!this.address_data.email_id || !this.address_data.phone) { |
| 936 | this.address_data = this.contacts[this.frm.doc.customer]; |
| 937 | } |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 938 | |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 939 | this.customer_doc.set_values(this.address_data) |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 940 | if(!this.customer_doc.fields_dict.full_name.$input.val()) { |
| 941 | this.customer_doc.set_value("full_name", this.frm.doc.customer) |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 942 | } |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 943 | |
| 944 | if(!this.customer_doc.fields_dict.customer_pos_id.value) { |
| 945 | this.customer_doc.set_value("customer_pos_id", $.now()) |
| 946 | } |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 947 | }, |
| 948 | |
| 949 | get_address_from_localstorage: function() { |
| 950 | this.address_details = this.get_customers_details() |
| 951 | return this.address_details[this.frm.doc.customer] |
| 952 | }, |
| 953 | |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 954 | make_offline_customer: function(new_customer) { |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 955 | this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 956 | this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value; |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 957 | this.customer_details = this.get_customers_details(); |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 958 | this.customer_details[this.frm.doc.customer] = this.get_prompt_details(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 959 | this.party_field.$input.val(this.frm.doc.customer); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 960 | this.update_address_and_customer_list(new_customer) |
| 961 | this.autocomplete_customers(); |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 962 | this.update_customer_in_localstorage() |
| 963 | this.update_customer_in_localstorage() |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 964 | this.customer_doc.hide() |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 965 | }, |
| 966 | |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 967 | update_address_and_customer_list: function(new_customer) { |
| 968 | var me = this; |
| 969 | if(new_customer) { |
| 970 | this.customers_mapper.push({ |
| 971 | label: this.frm.doc.customer, |
| 972 | value: this.frm.doc.customer, |
| 973 | customer_group: "", |
| 974 | territory: "" |
| 975 | }); |
| 976 | } |
| 977 | |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 978 | this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details()) |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 979 | }, |
| 980 | |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 981 | get_prompt_details: function() { |
| 982 | this.prompt_details = this.customer_doc.get_values(); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 983 | this.prompt_details['country'] = this.pos_profile_data.country; |
rohitwaghchaure | e2176b8 | 2017-07-28 20:52:02 +0530 | [diff] [blame] | 984 | this.prompt_details['territory'] = this.pos_profile_data["territory"]; |
| 985 | this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"]; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 986 | this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value; |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 987 | return JSON.stringify(this.prompt_details) |
| 988 | }, |
| 989 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 990 | update_customer_data: function (doc) { |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 991 | var me = this; |
| 992 | this.frm.doc.customer = doc.label || doc.name; |
| 993 | this.frm.doc.customer_name = doc.customer_name; |
| 994 | this.frm.doc.customer_group = doc.customer_group; |
| 995 | this.frm.doc.territory = doc.territory; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 996 | this.pos_bill.show(); |
Rohit Waghchaure | e30f83a | 2017-02-24 18:02:50 +0530 | [diff] [blame] | 997 | this.numeric_keypad.show(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 998 | }, |
| 999 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1000 | make_item_list: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1001 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1002 | if (!this.price_list) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1003 | frappe.msgprint(__("Price List not found or disabled")); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1004 | return; |
| 1005 | } |
| 1006 | |
| 1007 | me.item_timeout = null; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1008 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1009 | var $wrap = me.wrapper.find(".item-list"); |
| 1010 | me.wrapper.find(".item-list").empty(); |
| 1011 | |
Rohit Waghchaure | 801029e | 2016-11-17 00:14:21 +0530 | [diff] [blame] | 1012 | if (this.items.length > 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1013 | $.each(this.items, function(index, obj) { |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 1014 | if(index < me.page_len) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1015 | $(frappe.render_template("pos_item", { |
| 1016 | item_code: obj.name, |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1017 | item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1018 | item_name: obj.name === obj.item_name ? "" : obj.item_name, |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 1019 | item_image: obj.image, |
Faris Ansari | 185762a | 2017-04-13 12:36:08 +0530 | [diff] [blame] | 1020 | item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1021 | color: frappe.get_palette(obj.item_name), |
| 1022 | abbr: frappe.get_abbr(obj.item_name) |
| 1023 | })).tooltip().appendTo($wrap); |
| 1024 | } |
| 1025 | }); |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 1026 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 1027 | $wrap.append(` |
| 1028 | <div class="image-view-item btn-more text-muted text-center"> |
| 1029 | <div class="image-view-body"> |
| 1030 | <i class="mega-octicon octicon-package"></i> |
| 1031 | <div>Load more items</div> |
| 1032 | </div> |
| 1033 | </div> |
| 1034 | `); |
| 1035 | |
| 1036 | me.toggle_more_btn(); |
Rohit Waghchaure | 801029e | 2016-11-17 00:14:21 +0530 | [diff] [blame] | 1037 | } else { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 1038 | $("<p class='text-muted small' style='padding-left: 10px'>" |
| 1039 | +__("Not items found")+"</p>").appendTo($wrap) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1040 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1041 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1042 | if (this.items.length == 1 |
| 1043 | && this.serach_item.$input.val()) { |
| 1044 | this.serach_item.$input.val(""); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1045 | this.add_to_cart(); |
| 1046 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1047 | }, |
| 1048 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1049 | get_items: function (item_code) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1050 | // To search item as per the key enter |
| 1051 | |
| 1052 | var me = this; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1053 | this.item_serial_no = {}; |
| 1054 | this.item_batch_no = {}; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1055 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1056 | if (item_code) { |
| 1057 | return $.grep(this.item_data, function (item) { |
| 1058 | if (item.item_code == item_code) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1059 | return true |
| 1060 | } |
| 1061 | }) |
| 1062 | } |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 1063 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1064 | this.items_list = this.apply_category(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1065 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1066 | key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&'); |
Rohit Waghchaure | 163e359 | 2016-10-05 13:38:02 +0530 | [diff] [blame] | 1067 | var re = new RegExp('%', 'g'); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 1068 | var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*')) |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1069 | search_status = true |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1070 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1071 | if (key) { |
| 1072 | return $.grep(this.items_list, function (item) { |
| 1073 | if (search_status) { |
| 1074 | if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1075 | search_status = false; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1076 | return me.item_batch_no[item.item_code] = me.serach_item.$input.val() |
| 1077 | } else if (me.serial_no_data[item.item_code] |
| 1078 | && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1079 | search_status = false; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1080 | me.item_serial_no[item.item_code] = [me.serach_item.$input.val(), me.serial_no_data[item.item_code][me.serach_item.$input.val()]] |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1081 | return true |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1082 | } else if (item.barcode == me.serach_item.$input.val()) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1083 | search_status = false; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1084 | return item.barcode == me.serach_item.$input.val(); |
rohitwaghchaure | 860144f | 2017-07-12 15:01:56 +0530 | [diff] [blame] | 1085 | } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) || |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1086 | reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1087 | return true |
| 1088 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1089 | } |
| 1090 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1091 | } else { |
| 1092 | return this.items_list; |
| 1093 | } |
| 1094 | }, |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 1095 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1096 | apply_category: function() { |
| 1097 | var me = this; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1098 | category = this.selected_item_group || "All Item Groups"; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1099 | |
| 1100 | if(category == 'All Item Groups') { |
| 1101 | return this.item_data |
| 1102 | } else { |
| 1103 | return this.item_data.filter(function(element, index, array){ |
| 1104 | return element.item_group == category; |
| 1105 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1106 | } |
| 1107 | }, |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 1108 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1109 | bind_items_event: function() { |
| 1110 | var me = this; |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 1111 | $(this.wrapper).on('click', '.pos-bill-item', function() { |
| 1112 | $(me.wrapper).find('.pos-bill-item').removeClass('active'); |
| 1113 | $(this).addClass('active'); |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1114 | me.numeric_val = ""; |
| 1115 | me.numeric_id = "" |
| 1116 | me.item_code = $(this).attr("data-item-code"); |
| 1117 | me.render_selected_item() |
| 1118 | me.bind_qty_event() |
| 1119 | me.update_rate() |
| 1120 | $(me.wrapper).find(".selected-item").scrollTop(1000); |
| 1121 | }) |
| 1122 | }, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1123 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1124 | bind_qty_event: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1125 | var me = this; |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1126 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1127 | $(this.wrapper).on("change", ".pos-item-qty", function () { |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1128 | var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code"); |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1129 | var qty = $(this).val(); |
| 1130 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1131 | me.update_value() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1132 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1133 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1134 | $(this.wrapper).find("[data-action='increase-qty']").on("click", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1135 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 1136 | var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1; |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1137 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1138 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1139 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1140 | $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1141 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 1142 | var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1; |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1143 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1144 | }) |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 1145 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1146 | $(this.wrapper).on("change", ".pos-item-disc", function () { |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1147 | var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code"); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1148 | var discount = $(this).val(); |
| 1149 | me.update_discount(item_code, discount) |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1150 | me.update_value() |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1151 | }) |
| 1152 | }, |
| 1153 | |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1154 | bind_events: function() { |
| 1155 | var me = this; |
| 1156 | // if form is local then allow this function |
| 1157 | // $(me.wrapper).find(".pos-item-wrapper").on("click", function () { |
| 1158 | $(this.wrapper).on("click", ".pos-item-wrapper", function () { |
Rohit Waghchaure | a86bb69 | 2017-03-30 19:16:38 +0530 | [diff] [blame] | 1159 | me.item_code = ''; |
Rohit Waghchaure | d7de3c6 | 2017-04-17 13:36:08 +0530 | [diff] [blame] | 1160 | me.customer_validate(); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 1161 | if($(me.pos_bill).is(":hidden")) return; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1162 | |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1163 | if (me.frm.doc.docstatus == 0) { |
| 1164 | me.items = me.get_items($(this).attr("data-item-code")) |
| 1165 | me.add_to_cart(); |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1166 | me.clear_selected_row(); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1167 | } |
| 1168 | }); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1169 | |
| 1170 | me.bind_delete_event() |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1171 | }, |
| 1172 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1173 | update_qty: function (item_code, qty) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1174 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1175 | this.items = this.get_items(item_code); |
| 1176 | this.validate_serial_no() |
| 1177 | this.set_item_details(item_code, "qty", qty); |
| 1178 | }, |
| 1179 | |
| 1180 | update_discount: function(item_code, discount) { |
| 1181 | var me = this; |
| 1182 | this.items = this.get_items(item_code); |
| 1183 | this.set_item_details(item_code, "discount_percentage", discount); |
| 1184 | }, |
| 1185 | |
| 1186 | update_rate: function () { |
| 1187 | var me = this; |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1188 | $(this.wrapper).on("change", ".pos-item-price", function () { |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1189 | var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code"); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1190 | me.set_item_details(item_code, "rate", $(this).val()); |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1191 | me.update_value() |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1192 | }) |
| 1193 | }, |
Rohit Waghchaure | 56a7974 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1194 | |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1195 | update_value: function() { |
| 1196 | var me = this; |
| 1197 | var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc", |
| 1198 | "rate": ".pos-item-price", "amount": ".pos-amount"} |
| 1199 | this.child_doc = this.get_child_item(this.item_code); |
| 1200 | |
| 1201 | if(me.child_doc.length) { |
| 1202 | $.each(fields, function(key, field) { |
| 1203 | $(me.selected_row).find(field).val(me.child_doc[0][key]) |
| 1204 | }) |
| 1205 | } else { |
| 1206 | this.clear_selected_row(); |
Rohit Waghchaure | 56a7974 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1207 | } |
| 1208 | }, |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1209 | |
| 1210 | clear_selected_row: function() { |
| 1211 | $(this.wrapper).find('.selected-item').empty(); |
| 1212 | }, |
| 1213 | |
| 1214 | render_selected_item: function() { |
| 1215 | this.child_doc = this.get_child_item(this.item_code); |
| 1216 | $(this.wrapper).find('.selected-item').empty(); |
| 1217 | if(this.child_doc.length) { |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 1218 | this.child_doc[0]["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false, |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1219 | this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0])) |
| 1220 | $(this.wrapper).find('.selected-item').html(this.selected_row) |
| 1221 | } |
| 1222 | |
| 1223 | $(this.selected_row).find('.form-control').click(function(){ |
| 1224 | $(this).select(); |
| 1225 | }) |
| 1226 | }, |
| 1227 | |
Rohit Waghchaure | 86ab6a9 | 2017-02-24 18:02:50 +0530 | [diff] [blame] | 1228 | get_child_item: function(item_code) { |
| 1229 | var me = this; |
| 1230 | return $.map(me.frm.doc.items, function(doc){ |
| 1231 | if(doc.item_code == item_code) { |
| 1232 | return doc |
| 1233 | } |
| 1234 | }) |
| 1235 | }, |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1236 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1237 | set_item_details: function (item_code, field, value) { |
| 1238 | var me = this; |
| 1239 | if (value < 0) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1240 | frappe.throw(__("Enter value must be positive")); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1241 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1242 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1243 | this.remove_item = [] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1244 | $.each(this.frm.doc["items"] || [], function (i, d) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1245 | if (d.item_code == item_code) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1246 | if (d.serial_no && field == 'qty') { |
| 1247 | me.validate_serial_no_qty(d, item_code, field, value) |
| 1248 | } |
| 1249 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1250 | d[field] = flt(value); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1251 | d.amount = flt(d.rate) * flt(d.qty); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1252 | if (d.qty == 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1253 | me.remove_item.push(d.idx) |
| 1254 | } |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1255 | |
| 1256 | if(field=="discount_percentage" && value == 0) { |
| 1257 | d.rate = d.price_list_rate; |
| 1258 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1259 | } |
| 1260 | }); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1261 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1262 | if (field == 'qty') { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1263 | this.remove_zero_qty_item(); |
| 1264 | } |
| 1265 | |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 1266 | this.update_paid_amount_status(false) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1267 | }, |
| 1268 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1269 | remove_zero_qty_item: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1270 | var me = this; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1271 | var idx = 0; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1272 | this.items = [] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1273 | $.each(this.frm.doc["items"] || [], function (i, d) { |
| 1274 | if (!in_list(me.remove_item, d.idx)) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1275 | d.idx = idx; |
| 1276 | me.items.push(d); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1277 | idx++; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1278 | } |
| 1279 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1280 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1281 | this.frm.doc["items"] = this.items; |
| 1282 | }, |
| 1283 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1284 | make_discount_field: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1285 | var me = this; |
| 1286 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1287 | this.wrapper.find('input.discount-percentage').on("change", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1288 | me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage")); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1289 | var total = me.frm.doc.grand_total |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1290 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1291 | if (me.frm.doc.apply_discount_on == 'Net Total') { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1292 | total = me.frm.doc.net_total |
| 1293 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1294 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1295 | me.frm.doc.discount_amount = flt(total * flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount")); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1296 | me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount) |
| 1297 | me.refresh(); |
| 1298 | }); |
| 1299 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1300 | this.wrapper.find('input.discount-amount').on("change", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1301 | me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount")); |
| 1302 | me.frm.doc.additional_discount_percentage = 0.0; |
| 1303 | me.wrapper.find('input.discount-percentage').val(0); |
| 1304 | me.refresh(); |
| 1305 | }); |
| 1306 | }, |
| 1307 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1308 | customer_validate: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1309 | var me = this; |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 1310 | if (!this.frm.doc.customer || this.party_field.get_value() == "") { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1311 | frappe.throw(__("Please select customer")) |
| 1312 | } |
| 1313 | }, |
| 1314 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1315 | add_to_cart: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1316 | var me = this; |
| 1317 | var caught = false; |
| 1318 | var no_of_items = me.wrapper.find(".pos-bill-item").length; |
| 1319 | |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1320 | this.customer_validate(); |
| 1321 | this.mandatory_batch_no(); |
| 1322 | this.validate_serial_no(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1323 | this.validate_warehouse(); |
| 1324 | |
| 1325 | if (no_of_items != 0) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1326 | $.each(this.frm.doc["items"] || [], function (i, d) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1327 | if (d.item_code == me.items[0].item_code) { |
| 1328 | caught = true; |
| 1329 | d.qty += 1; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1330 | d.amount = flt(d.rate) * flt(d.qty); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1331 | if (me.item_serial_no[d.item_code]) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1332 | d.serial_no += '\n' + me.item_serial_no[d.item_code][0] |
| 1333 | d.warehouse = me.item_serial_no[d.item_code][1] |
| 1334 | } |
| 1335 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1336 | if (me.item_batch_no.length) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1337 | d.batch_no = me.item_batch_no[d.item_code] |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | }); |
| 1341 | } |
| 1342 | |
| 1343 | // if item not found then add new item |
| 1344 | if (!caught) |
| 1345 | this.add_new_item_to_grid(); |
| 1346 | |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 1347 | this.update_paid_amount_status(false) |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 1348 | this.wrapper.find(".item-cart-items").scrollTop(1000); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1349 | }, |
| 1350 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1351 | add_new_item_to_grid: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1352 | var me = this; |
| 1353 | this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items"); |
| 1354 | this.child.item_code = this.items[0].item_code; |
| 1355 | this.child.item_name = this.items[0].item_name; |
| 1356 | this.child.stock_uom = this.items[0].stock_uom; |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1357 | this.child.brand = this.items[0].brand; |
rohitwaghchaure | 860144f | 2017-07-12 15:01:56 +0530 | [diff] [blame] | 1358 | this.child.description = this.items[0].description || this.items[0].item_name; |
Rohit Waghchaure | e30f83a | 2017-02-24 18:02:50 +0530 | [diff] [blame] | 1359 | this.child.discount_percentage = 0.0; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1360 | this.child.qty = 1; |
| 1361 | this.child.item_group = this.items[0].item_group; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1362 | this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center; |
| 1363 | this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1364 | this.child.warehouse = (this.item_serial_no[this.child.item_code] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1365 | ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse)); |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1366 | this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9); |
| 1367 | this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9); |
| 1368 | this.child.actual_qty = me.get_actual_qty(this.items[0]); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1369 | this.child.amount = flt(this.child.qty) * flt(this.child.rate); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1370 | this.child.batch_no = this.item_batch_no[this.child.item_code]; |
| 1371 | this.child.serial_no = (this.item_serial_no[this.child.item_code] |
| 1372 | ? this.item_serial_no[this.child.item_code][0] : ''); |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1373 | this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1374 | }, |
| 1375 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1376 | update_paid_amount_status: function (update_paid_amount) { |
| 1377 | if (this.name) { |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 1378 | update_paid_amount = update_paid_amount ? false : true; |
| 1379 | } |
| 1380 | |
| 1381 | this.refresh(update_paid_amount); |
| 1382 | }, |
| 1383 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1384 | refresh: function (update_paid_amount) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1385 | var me = this; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 1386 | this.refresh_fields(update_paid_amount); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1387 | this.set_primary_action(); |
| 1388 | }, |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 1389 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1390 | refresh_fields: function (update_paid_amount) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1391 | this.apply_pricing_rule(); |
| 1392 | this.discount_amount_applied = false; |
| 1393 | this._calculate_taxes_and_totals(); |
| 1394 | this.calculate_discount_amount(); |
| 1395 | this.show_items_in_item_cart(); |
| 1396 | this.set_taxes(); |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 1397 | this.calculate_outstanding_amount(update_paid_amount); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1398 | this.set_totals(); |
| 1399 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1400 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1401 | get_company_currency: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1402 | return erpnext.get_currency(this.frm.doc.company); |
| 1403 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1404 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1405 | show_items_in_item_cart: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1406 | var me = this; |
| 1407 | var $items = this.wrapper.find(".items").empty(); |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 1408 | var $no_items_message = this.wrapper.find(".no-items-message"); |
| 1409 | $no_items_message.toggle(this.frm.doc.items.length === 0); |
| 1410 | |
| 1411 | var $totals_area = this.wrapper.find('.totals-area'); |
| 1412 | $totals_area.toggle(this.frm.doc.items.length > 0); |
| 1413 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1414 | $.each(this.frm.doc.items || [], function (i, d) { |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 1415 | $(frappe.render_template("pos_bill_item_new", { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1416 | item_code: d.item_code, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1417 | item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1418 | qty: d.qty, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1419 | discount_percentage: d.discount_percentage || 0.0, |
| 1420 | actual_qty: me.actual_qty_dict[d.item_code] || 0.0, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1421 | projected_qty: d.projected_qty, |
mbauskar | 22cedeb | 2017-04-17 12:24:11 +0530 | [diff] [blame] | 1422 | rate: format_currency(d.rate, me.frm.doc.currency), |
Rohit Waghchaure | 6ee91ec | 2017-03-21 15:55:09 +0530 | [diff] [blame] | 1423 | amount: format_currency(d.amount, me.frm.doc.currency), |
| 1424 | selected_class: (me.item_code == d.item_code) ? "active" : "" |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1425 | })).appendTo($items); |
| 1426 | }); |
| 1427 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1428 | this.wrapper.find("input.pos-item-qty").on("focus", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1429 | $(this).select(); |
| 1430 | }); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1431 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1432 | this.wrapper.find("input.pos-item-disc").on("focus", function () { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1433 | $(this).select(); |
| 1434 | }); |
| 1435 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1436 | this.wrapper.find("input.pos-item-price").on("focus", function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1437 | $(this).select(); |
| 1438 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1439 | }, |
| 1440 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1441 | set_taxes: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1442 | var me = this; |
| 1443 | me.frm.doc.total_taxes_and_charges = 0.0 |
| 1444 | |
| 1445 | var taxes = this.frm.doc.taxes || []; |
| 1446 | $(this.wrapper) |
| 1447 | .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true) |
| 1448 | .find(".tax-table").empty(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1449 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1450 | $.each(taxes, function (i, d) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1451 | if (d.tax_amount && cint(d.included_in_print_rate) == 0) { |
| 1452 | $(frappe.render_template("pos_tax_row", { |
| 1453 | description: d.description, |
| 1454 | tax_amount: format_currency(flt(d.tax_amount_after_discount_amount), |
| 1455 | me.frm.doc.currency) |
| 1456 | })).appendTo(me.wrapper.find(".tax-table")); |
| 1457 | } |
| 1458 | }); |
| 1459 | }, |
| 1460 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1461 | set_totals: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1462 | var me = this; |
| 1463 | this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency)); |
| 1464 | this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency)); |
| 1465 | }, |
| 1466 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1467 | set_primary_action: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1468 | var me = this; |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1469 | this.page.set_primary_action(__("New Cart"), function () { |
| 1470 | me.make_new_cart() |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1471 | me.make_menu_list() |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 1472 | }, "fa fa-plus") |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1473 | |
| 1474 | if (this.frm.doc.docstatus == 1) { |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 1475 | this.page.set_secondary_action(__("Print"), function () { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1476 | var html = frappe.render(me.print_template_data, me.frm.doc) |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1477 | me.print_document(html) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1478 | }) |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1479 | this.page.add_menu_item(__("Email"), function () { |
| 1480 | me.email_prompt() |
| 1481 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1482 | } |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1483 | }, |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 1484 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1485 | make_new_cart: function (){ |
Rohit Waghchaure | a86bb69 | 2017-03-30 19:16:38 +0530 | [diff] [blame] | 1486 | this.item_code = ''; |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1487 | this.page.clear_secondary_action(); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1488 | this.save_previous_entry(); |
| 1489 | this.create_new(); |
| 1490 | this.refresh(); |
| 1491 | this.toggle_input_field(); |
| 1492 | this.render_list_customers(); |
| 1493 | this.set_focus(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1494 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1495 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1496 | print_dialog: function () { |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1497 | var me = this; |
| 1498 | |
mbauskar | 66e2a51 | 2017-06-22 16:18:12 +0530 | [diff] [blame] | 1499 | this.msgprint = frappe.msgprint( |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1500 | `<a class="btn btn-primary print_doc" |
| 1501 | style="margin-right: 5px;">${__('Print')}</a> |
| 1502 | <a class="btn btn-default new_doc">${__('New')}</a>`); |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1503 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1504 | $('.print_doc').click(function () { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1505 | var html = frappe.render(me.print_template_data, me.frm.doc) |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1506 | me.print_document(html) |
| 1507 | }) |
| 1508 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1509 | $('.new_doc').click(function () { |
mbauskar | 66e2a51 | 2017-06-22 16:18:12 +0530 | [diff] [blame] | 1510 | me.msgprint.hide() |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1511 | me.make_new_cart() |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1512 | }) |
| 1513 | }, |
| 1514 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1515 | print_document: function (html) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1516 | var w = window.open(); |
| 1517 | w.document.write(html); |
| 1518 | w.document.close(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1519 | setTimeout(function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1520 | w.print(); |
| 1521 | w.close(); |
| 1522 | }, 1000) |
| 1523 | }, |
| 1524 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1525 | submit_invoice: function () { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1526 | var me = this; |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1527 | this.change_status(); |
Rohit Waghchaure | e259a9b | 2017-03-14 19:03:49 +0530 | [diff] [blame] | 1528 | this.update_serial_no() |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1529 | if (this.frm.doc.docstatus == 1) { |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1530 | this.print_dialog() |
| 1531 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1532 | }, |
| 1533 | |
Rohit Waghchaure | e259a9b | 2017-03-14 19:03:49 +0530 | [diff] [blame] | 1534 | update_serial_no: function() { |
| 1535 | var me = this; |
| 1536 | |
| 1537 | //Remove the sold serial no from the cache |
| 1538 | $.each(this.frm.doc.items, function(index, data) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1539 | var sn = data.serial_no.split('\n') |
Rohit Waghchaure | e259a9b | 2017-03-14 19:03:49 +0530 | [diff] [blame] | 1540 | if(sn.length) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1541 | var serial_no_list = me.serial_no_data[data.item_code] |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 1542 | if(serial_no_list) { |
| 1543 | $.each(sn, function(i, serial_no) { |
| 1544 | if(in_list(Object.keys(serial_no_list), serial_no)) { |
| 1545 | delete serial_no_list[serial_no] |
| 1546 | } |
| 1547 | }) |
| 1548 | me.serial_no_data[data.item_code] = serial_no_list; |
| 1549 | } |
Rohit Waghchaure | e259a9b | 2017-03-14 19:03:49 +0530 | [diff] [blame] | 1550 | } |
| 1551 | }) |
| 1552 | }, |
| 1553 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1554 | change_status: function () { |
| 1555 | if (this.frm.doc.docstatus == 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1556 | this.frm.doc.docstatus = 1; |
| 1557 | this.update_invoice(); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1558 | this.toggle_input_field(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1559 | } |
| 1560 | }, |
| 1561 | |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1562 | toggle_input_field: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1563 | var pointer_events = 'inherit' |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1564 | var disabled = this.frm.doc.docstatus == 1 ? true: false; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1565 | $(this.wrapper).find('input').attr("disabled", disabled); |
| 1566 | $(this.wrapper).find('select').attr("disabled", disabled); |
| 1567 | $(this.wrapper).find('input').attr("disabled", disabled); |
| 1568 | $(this.wrapper).find('select').attr("disabled", disabled); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1569 | $(this.wrapper).find('button').attr("disabled", disabled); |
| 1570 | this.party_field.$input.attr('disabled', disabled); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1571 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1572 | if (this.frm.doc.docstatus == 1) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1573 | pointer_events = 'none'; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1574 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1575 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1576 | $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1577 | $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events); |
| 1578 | this.set_primary_action(); |
| 1579 | }, |
| 1580 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1581 | create_invoice: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1582 | var me = this; |
| 1583 | var invoice_data = {} |
| 1584 | this.si_docs = this.get_doc_from_localstorage(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1585 | if (this.name) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1586 | this.update_invoice() |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1587 | } else { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1588 | this.name = $.now(); |
Rohit Waghchaure | 377c7ac | 2016-09-05 17:56:44 +0530 | [diff] [blame] | 1589 | this.frm.doc.offline_pos_name = this.name; |
Rohit Waghchaure | 9cd356c | 2016-08-11 16:54:30 +0530 | [diff] [blame] | 1590 | this.frm.doc.posting_date = frappe.datetime.get_today(); |
| 1591 | this.frm.doc.posting_time = frappe.datetime.now_time(); |
Charles-Henri Decultot | 2305eda | 2017-06-19 05:55:54 +0200 | [diff] [blame] | 1592 | this.frm.doc.pos_profile = this.pos_profile_data['name']; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1593 | invoice_data[this.name] = this.frm.doc |
| 1594 | this.si_docs.push(invoice_data) |
| 1595 | this.update_localstorage(); |
| 1596 | this.set_primary_action(); |
| 1597 | } |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1598 | return invoice_data; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1599 | }, |
| 1600 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1601 | update_invoice: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1602 | var me = this; |
| 1603 | this.si_docs = this.get_doc_from_localstorage(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1604 | $.each(this.si_docs, function (index, data) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1605 | for (var key in data) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1606 | if (key == me.name) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1607 | me.si_docs[index][key] = me.frm.doc; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1608 | me.update_localstorage(); |
| 1609 | } |
| 1610 | } |
| 1611 | }) |
| 1612 | }, |
| 1613 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1614 | update_localstorage: function () { |
| 1615 | try { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1616 | localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs)); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1617 | } catch (e) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1618 | frappe.throw(__("LocalStorage is full , did not save")) |
| 1619 | } |
| 1620 | }, |
| 1621 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1622 | get_doc_from_localstorage: function () { |
| 1623 | try { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1624 | return JSON.parse(localStorage.getItem('sales_invoice_doc')) || []; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1625 | } catch (e) { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1626 | return [] |
| 1627 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1628 | }, |
| 1629 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1630 | set_interval_for_si_sync: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1631 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1632 | setInterval(function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1633 | me.sync_sales_invoice() |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1634 | }, 60000) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1635 | }, |
| 1636 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1637 | sync_sales_invoice: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1638 | var me = this; |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 1639 | this.si_docs = this.get_submitted_invoice() || []; |
| 1640 | this.email_queue_list = this.get_email_queue() || {}; |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1641 | this.customers_list = this.get_customers_details() || {}; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 1642 | if(this.customer_doc) { |
| 1643 | this.freeze = this.customer_doc.display |
| 1644 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1645 | |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 1646 | if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1647 | frappe.call({ |
| 1648 | method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice", |
| 1649 | args: { |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 1650 | doc_list: me.si_docs, |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1651 | email_queue_list: me.email_queue_list, |
| 1652 | customers_list: me.customers_list |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1653 | }, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1654 | callback: function (r) { |
| 1655 | if (r.message) { |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 1656 | me.customers = r.message.synced_customers_list; |
| 1657 | me.address = r.message.synced_address; |
| 1658 | me.contacts = r.message.synced_contacts; |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 1659 | me.removed_items = r.message.invoice; |
| 1660 | me.removed_email = r.message.email_queue |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1661 | me.removed_customers = r.message.customers |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1662 | me.remove_doc_from_localstorage(); |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 1663 | me.remove_email_queue_from_localstorage(); |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1664 | me.remove_customer_from_localstorage(); |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 1665 | me.prepare_customer_mapper() |
| 1666 | me.autocomplete_customers() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1667 | } |
| 1668 | } |
| 1669 | }) |
| 1670 | } |
| 1671 | }, |
| 1672 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1673 | get_submitted_invoice: function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1674 | var invoices = []; |
| 1675 | var index = 1; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1676 | var docs = this.get_doc_from_localstorage(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1677 | if (docs) { |
| 1678 | invoices = $.map(docs, function (data) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1679 | for (var key in data) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1680 | if (data[key].docstatus == 1 && index < 50) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1681 | index++ |
Rohit Waghchaure | b77a105 | 2016-08-01 18:35:18 +0530 | [diff] [blame] | 1682 | data[key].docstatus = 0; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1683 | return data |
| 1684 | } |
| 1685 | } |
| 1686 | }); |
| 1687 | } |
| 1688 | |
| 1689 | return invoices |
| 1690 | }, |
| 1691 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1692 | remove_doc_from_localstorage: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1693 | var me = this; |
| 1694 | this.si_docs = this.get_doc_from_localstorage(); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1695 | this.new_si_docs = []; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1696 | if (this.removed_items) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1697 | $.each(this.si_docs, function (index, data) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1698 | for (var key in data) { |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1699 | if (!in_list(me.removed_items, key)) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1700 | me.new_si_docs.push(data); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | }) |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1704 | this.removed_items = []; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1705 | this.si_docs = this.new_si_docs; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1706 | this.update_localstorage(); |
| 1707 | } |
| 1708 | }, |
| 1709 | |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 1710 | remove_email_queue_from_localstorage: function() { |
| 1711 | var me = this; |
| 1712 | this.email_queue = this.get_email_queue() |
| 1713 | if (this.removed_email) { |
| 1714 | $.each(this.email_queue_list, function (index, data) { |
| 1715 | if (in_list(me.removed_email, index)) { |
| 1716 | delete me.email_queue[index] |
| 1717 | } |
| 1718 | }) |
| 1719 | this.update_email_queue(); |
| 1720 | } |
| 1721 | }, |
| 1722 | |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1723 | remove_customer_from_localstorage: function() { |
| 1724 | var me = this; |
| 1725 | this.customer_details = this.get_customers_details() |
| 1726 | if (this.removed_customers) { |
| 1727 | $.each(this.customers_list, function (index, data) { |
| 1728 | if (in_list(me.removed_customers, index)) { |
| 1729 | delete me.customer_details[index] |
| 1730 | } |
| 1731 | }) |
| 1732 | this.update_customer_in_localstorage(); |
| 1733 | } |
| 1734 | }, |
| 1735 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1736 | validate: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1737 | var me = this; |
| 1738 | this.customer_validate(); |
| 1739 | this.item_validate(); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1740 | this.validate_mode_of_payments(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1741 | }, |
| 1742 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1743 | item_validate: function () { |
| 1744 | if (this.frm.doc.items.length == 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1745 | frappe.throw(__("Select items to save the invoice")) |
| 1746 | } |
| 1747 | }, |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 1748 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1749 | validate_mode_of_payments: function () { |
| 1750 | if (this.frm.doc.payments.length === 0) { |
Saurabh | 9a6c666 | 2016-06-27 16:51:44 +0530 | [diff] [blame] | 1751 | frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile.")) |
| 1752 | } |
| 1753 | }, |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 1754 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1755 | validate_serial_no: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1756 | var me = this; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1757 | var item_code = '' |
| 1758 | var serial_no = ''; |
| 1759 | for (var key in this.item_serial_no) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1760 | item_code = key; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1761 | serial_no = me.item_serial_no[key][0]; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1762 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1763 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1764 | if (this.items[0].has_serial_no && serial_no == "") { |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1765 | this.refresh(); |
| 1766 | frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", { |
| 1767 | 'item': this.items[0].item_code |
| 1768 | }))) |
| 1769 | } |
| 1770 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1771 | if (item_code && serial_no) { |
| 1772 | $.each(this.frm.doc.items, function (index, data) { |
| 1773 | if (data.item_code == item_code) { |
| 1774 | if (in_list(data.serial_no.split('\n'), serial_no)) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1775 | frappe.throw(__(repl("Serial no %(serial_no)s is already taken", { |
| 1776 | 'serial_no': serial_no |
| 1777 | }))) |
| 1778 | } |
| 1779 | } |
| 1780 | }) |
| 1781 | } |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1782 | }, |
| 1783 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1784 | validate_serial_no_qty: function (args, item_code, field, value) { |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 1785 | var me = this; |
| 1786 | if (args.item_code == item_code && args.serial_no |
| 1787 | && field == 'qty' && cint(value) != value) { |
| 1788 | args.qty = 0.0; |
| 1789 | this.refresh(); |
| 1790 | frappe.throw(__("Serial no item cannot be a fraction")) |
| 1791 | } |
| 1792 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1793 | if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) { |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 1794 | args.qty = 0.0; |
| 1795 | args.serial_no = '' |
| 1796 | this.refresh(); |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1797 | frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", { |
| 1798 | 'item': item_code |
| 1799 | }))) |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 1800 | } |
| 1801 | }, |
| 1802 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1803 | mandatory_batch_no: function () { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1804 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1805 | if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1806 | frappe.prompt([{ |
| 1807 | 'fieldname': 'batch', |
| 1808 | 'fieldtype': 'Select', |
| 1809 | 'label': __('Batch No'), |
| 1810 | 'reqd': 1, |
| 1811 | 'options': this.batch_no_data[this.items[0].item_code] |
| 1812 | }], |
| 1813 | function(values){ |
| 1814 | me.item_batch_no[me.items[0].item_code] = values.batch; |
| 1815 | }, |
| 1816 | __('Select Batch No')) |
| 1817 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1818 | }, |
| 1819 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1820 | apply_pricing_rule: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1821 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1822 | $.each(this.frm.doc["items"], function (n, item) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1823 | var pricing_rule = me.get_pricing_rule(item) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1824 | me.validate_pricing_rule(pricing_rule) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1825 | if (pricing_rule.length) { |
| 1826 | item.pricing_rule = pricing_rule[0].name; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1827 | item.margin_type = pricing_rule[0].margin_type; |
| 1828 | item.price_list_rate = pricing_rule[0].price || item.price_list_rate; |
| 1829 | item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount; |
| 1830 | item.discount_percentage = pricing_rule[0].discount_percentage || 0.0; |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1831 | me.apply_pricing_rule_on_item(item) |
| 1832 | } else if (item.pricing_rule) { |
| 1833 | item.price_list_rate = me.price_list_data[item.item_code] |
Rohit Waghchaure | 619c40b | 2016-11-07 15:29:17 +0530 | [diff] [blame] | 1834 | item.margin_rate_or_amount = 0.0; |
| 1835 | item.discount_percentage = 0.0; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1836 | item.pricing_rule = null; |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1837 | me.apply_pricing_rule_on_item(item) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1838 | } |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 1839 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1840 | if(item.discount_percentage > 0) { |
| 1841 | me.apply_pricing_rule_on_item(item) |
| 1842 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1843 | }) |
| 1844 | }, |
| 1845 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1846 | get_pricing_rule: function (item) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1847 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1848 | return $.grep(this.pricing_rules, function (data) { |
| 1849 | if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) { |
Rohit Waghchaure | d7de3c6 | 2017-04-17 13:36:08 +0530 | [diff] [blame] | 1850 | if (me.validate_item_condition(data, item)) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1851 | if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) { |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 1852 | return me.validate_condition(data) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1853 | } else { |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 1854 | return true |
| 1855 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1856 | } |
| 1857 | } |
| 1858 | }) |
| 1859 | }, |
| 1860 | |
Rohit Waghchaure | d7de3c6 | 2017-04-17 13:36:08 +0530 | [diff] [blame] | 1861 | validate_item_condition: function (data, item) { |
| 1862 | var apply_on = frappe.model.scrub(data.apply_on); |
| 1863 | |
| 1864 | return (data.apply_on == 'Item Group') |
| 1865 | ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]); |
| 1866 | }, |
| 1867 | |
| 1868 | validate_item_group: function (pr_item_group, cart_item_group) { |
| 1869 | //pr_item_group = pricing rule's item group |
| 1870 | //cart_item_group = cart item's item group |
| 1871 | //this.item_groups has information about item group's lft and rgt |
| 1872 | //for example: {'Foods': [12, 19]} |
| 1873 | |
| 1874 | pr_item_group = this.item_groups[pr_item_group] |
| 1875 | cart_item_group = this.item_groups[cart_item_group] |
| 1876 | |
| 1877 | return (cart_item_group[0] >= pr_item_group[0] && |
| 1878 | cart_item_group[1] <= pr_item_group[1]) |
| 1879 | }, |
| 1880 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1881 | validate_condition: function (data) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1882 | //This method check condition based on applicable for |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1883 | var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1884 | if (in_list(condition[1], condition[0])) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1885 | return true |
| 1886 | } |
| 1887 | }, |
| 1888 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1889 | get_mapper_for_pricing_rule: function (data) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1890 | return { |
Rohit Waghchaure | 89ee309 | 2016-07-08 15:17:04 +0530 | [diff] [blame] | 1891 | 'Customer': [data.customer, [this.frm.doc.customer]], |
| 1892 | 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']], |
| 1893 | 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']], |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 1894 | 'Campaign': [data.campaign, [this.frm.doc.campaign]], |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1895 | } |
| 1896 | }, |
| 1897 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1898 | validate_pricing_rule: function (pricing_rule) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1899 | //This method validate duplicate pricing rule |
| 1900 | var pricing_rule_name = ''; |
| 1901 | var priority = 0; |
| 1902 | var pricing_rule_list = []; |
| 1903 | var priority_list = [] |
| 1904 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1905 | if (pricing_rule.length > 1) { |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1906 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1907 | $.each(pricing_rule, function (index, data) { |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1908 | pricing_rule_name += data.name + ',' |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1909 | priority_list.push(data.priority) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1910 | if (priority <= data.priority) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1911 | priority = data.priority |
| 1912 | pricing_rule_list.push(data) |
| 1913 | } |
| 1914 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1915 | |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1916 | var count = 0 |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1917 | $.each(priority_list, function (index, value) { |
| 1918 | if (value == priority) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1919 | count++ |
| 1920 | } |
| 1921 | }) |
| 1922 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1923 | if (priority == 0 || count > 1) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1924 | frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", { |
| 1925 | 'pricing_rule': pricing_rule_name |
| 1926 | }))) |
| 1927 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1928 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1929 | return pricing_rule_list |
| 1930 | } |
| 1931 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1932 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1933 | validate_warehouse: function () { |
| 1934 | if (this.items[0].is_stock_item && !this.items[0].default_warehouse && !this.pos_profile_data['warehouse']) { |
Neil Trini Lasrado | 5ddd7ac | 2016-10-24 15:25:33 +0530 | [diff] [blame] | 1935 | frappe.throw(__("Default warehouse is required for selected item")) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1936 | } |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1937 | }, |
| 1938 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1939 | get_actual_qty: function (item) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1940 | this.actual_qty = 0.0; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 1941 | |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1942 | var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1943 | if (warehouse && this.bin_data[item.item_code]) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1944 | this.actual_qty = this.bin_data[item.item_code][warehouse] || 0; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 1945 | this.actual_qty_dict[item.item_code] = this.actual_qty |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | return this.actual_qty |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1949 | }, |
| 1950 | |
| 1951 | update_customer_in_localstorage: function() { |
| 1952 | var me = this; |
| 1953 | try { |
| 1954 | localStorage.setItem('customer_details', JSON.stringify(this.customer_details)); |
| 1955 | } catch (e) { |
| 1956 | frappe.throw(__("LocalStorage is full , did not save")) |
| 1957 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1958 | } |
Faris Ansari | 185762a | 2017-04-13 12:36:08 +0530 | [diff] [blame] | 1959 | }) |