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