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