blob: ce1f34bb27aa3ee4e131c7404933d95adbd3d3e0 [file] [log] [blame]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301frappe.provide("erpnext.pos");
2{% include "erpnext/public/js/controllers/taxes_and_totals.js" %}
3
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05304frappe.pages['pos'].on_page_load = function (wrapper) {
Rushabh Mehta4c36d732015-01-01 15:59:34 +05305 var page = frappe.ui.make_app_page({
Rushabh Mehta72e17192014-08-08 15:30:49 +05306 parent: wrapper,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05307 title: __('Point of Sale'),
Rushabh Mehta72e17192014-08-08 15:30:49 +05308 single_column: true
9 });
Rohit Waghchauree0934d12016-05-11 15:04:57 +053010
Faris Ansaric70bbac2017-08-29 15:27:17 +053011 frappe.db.get_value('POS Settings', {name: 'POS Settings'}, 'is_online', (r) => {
Rohit Waghchaure1a779222017-09-11 11:49:25 +053012 if (r && r.use_pos_in_offline_mode && cint(r.use_pos_in_offline_mode)) {
Faris Ansaric70bbac2017-08-29 15:27:17 +053013 // offline
14 wrapper.pos = new erpnext.pos.PointOfSale(wrapper);
15 cur_pos = wrapper.pos;
16 } else {
17 // online
rohitwaghchaurea4c40e02018-02-05 12:24:06 +053018 frappe.flags.is_online = true
Faris Ansaric70bbac2017-08-29 15:27:17 +053019 frappe.set_route('point-of-sale');
20 }
21 });
Rushabh Mehta72e17192014-08-08 15:30:49 +053022}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053023
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053024frappe.pages['pos'].refresh = function (wrapper) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053025 window.onbeforeunload = function () {
26 return wrapper.pos.beforeunload()
27 }
rohitwaghchaurea4c40e02018-02-05 12:24:06 +053028
29 if (frappe.flags.is_online) {
30 frappe.set_route('point-of-sale');
31 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +053032}
33
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053034erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053035 init: function (wrapper) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +053036 this.page_len = 20;
rohitwaghchaured2be55b2017-06-07 12:04:01 +053037 this.freeze = false;
Rohit Waghchauree0934d12016-05-11 15:04:57 +053038 this.page = wrapper.page;
39 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053040 this.set_indicator();
41 this.onload();
42 this.make_menu_list();
Rohit Waghchaure017f5002017-03-08 17:34:39 +053043 this.bind_events();
Rohit Waghchaure75177c02017-03-16 15:21:21 +053044 this.bind_items_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053045 this.si_docs = this.get_doc_from_localstorage();
46 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053047
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053048 beforeunload: function (e) {
49 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053050 e = e || window.event;
51
52 // For IE and Firefox prior to version 4
53 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053054 e.returnValue = __("You are in offline mode. You will not be able to reload until you have network.");
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053055 return
56 }
57
58 // For Safari
59 return __("You are in offline mode. You will not be able to reload until you have network.");
60 }
61 },
62
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053063 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053064 var me = this;
65 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053066 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053067 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053068 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053069 },
70
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053071 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053072 var me = this;
73 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053074 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053075 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053076 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053077 method: "frappe.handler.ping",
78 callback: function (r) {
79 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053080 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053081 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053082 }
83 }
84 })
85 },
86
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053087 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053088 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053089 this.get_data_from_server(function () {
rohitwaghchaured2be55b2017-06-07 12:04:01 +053090 me.make_control();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053091 me.create_new();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +053092 me.make();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053093 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053094 },
95
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053096 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053097 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +053098 this.page.clear_menu();
Faris Ansari769b6ba2017-05-16 07:31:10 +053099
100 // for mobile
101 this.page.add_menu_item(__("Pay"), function () {
102 me.validate();
103 me.update_paid_amount_status(true);
104 me.create_invoice();
105 me.make_payment();
106 }).addClass('visible-xs');
107
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530108 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530109 me.save_previous_entry();
110 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530111 })
112
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530113 this.page.add_menu_item(__("Sync Master Data"), function () {
114 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530115 me.load_data(false);
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530116 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530117 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530118 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530119 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530120
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530121 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
rohitwaghchaure88491712017-10-02 12:13:36 +0530122 me.freeze_screen = true;
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530123 me.sync_sales_invoice()
124 });
125
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530126 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530127 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530128 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530129 },
130
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530131 email_prompt: function() {
132 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530133 var fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
Shreya Shah44e719d2018-04-20 10:28:52 +0530134 {fieldtype: "Section Break", collapsible: 1, label: "CC & Email Template"},
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530135 {fieldtype: "Section Break"},
136 {label:__("Subject"), fieldtype:"Data", reqd: 1,
137 fieldname:"subject",length:524288},
138 {fieldtype: "Section Break"},
139 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
140 fieldname:"content"},
141 {fieldtype: "Section Break"},
142 {fieldtype: "Column Break"}];
143
144 this.email_dialog = new frappe.ui.Dialog({
145 title: "Email",
146 fields: fields,
147 primary_action_label: __("Send"),
148 primary_action: function() {
149 me.send_action();
150 }
151 });
152
153 this.email_dialog.show()
154 },
155
156 send_action: function() {
157 this.email_queue = this.get_email_queue()
158 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
159 this.update_email_queue()
160 this.email_dialog.hide()
161 },
162
163 update_email_queue: function () {
164 try {
165 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
166 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530167 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530168 }
169 },
170
171 get_email_queue: function () {
172 try {
173 return JSON.parse(localStorage.getItem('email_queue')) || {};
174 } catch (e) {
175 return {}
176 }
177 },
178
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530179 get_customers_details: function () {
180 try {
181 return JSON.parse(localStorage.getItem('customer_details')) || {};
182 } catch (e) {
183 return {}
184 }
185 },
186
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530187 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530188 var me = this;
189
190 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530191 if (doc_data) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530192 this.frm.doc = doc_data[0][this.frm.doc.offline_pos_name];
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530193 this.set_missing_values();
194 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530195 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530196 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530197 }
198 },
199
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530200 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530201 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530202 this.validate_list()
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530203 this.remove_doc_from_localstorage()
204 this.update_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530205 this.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530206 },
207
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530208 validate_list: function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530209 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530210 this.si_docs = this.get_submitted_invoice()
rohitwaghchauref688af32017-11-08 11:33:39 +0530211 $.each(this.removed_items, function(index, pos_name){
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530212 $.each(me.si_docs, function(key, data){
rohitwaghchauref688af32017-11-08 11:33:39 +0530213 if(me.si_docs[key][pos_name] && me.si_docs[key][pos_name].offline_pos_name == pos_name ){
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530214 frappe.throw(__("Submitted orders can not be deleted"))
215 }
216 })
217 })
218 },
219
220 toggle_delete_button: function () {
221 var me = this;
222 if(this.pos_profile_data["allow_delete"]) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530223 if (this.removed_items && this.removed_items.length > 0) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530224 $(this.page.wrapper).find('.btn-danger').show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530225 } else {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530226 $(this.page.wrapper).find('.btn-danger').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530227 }
228 }
229 },
230
231 get_doctype_status: function (doc) {
232 if (doc.docstatus == 0) {
233 return { status: "Draft", indicator: "red" }
234 } else if (doc.outstanding_amount == 0) {
235 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530236 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530237 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530238 }
239 },
240
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530241 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530242 var me = this;
243 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530244 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530245 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530246 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530247 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530248
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530249 this.set_customer_value_in_party_field();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530250
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530251 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530252 this.frm.doc.write_off_account = doc.write_off_account
253 }
254
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530255 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530256 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530257 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530258 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530259
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530260 set_customer_value_in_party_field: function() {
261 if (this.frm.doc.customer) {
262 this.party_field.$input.val(this.frm.doc.customer);
263 }
264 },
265
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530266 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530267 var me = this;
268 this.si_docs = this.get_doc_from_localstorage();
269
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530270 return $.grep(this.si_docs, function (data) {
271 for (key in data) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530272 return key == me.frm.doc.offline_pos_name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530273 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530274 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530275 },
276
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530277 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530278 var me = this;
279 frappe.call({
280 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
281 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530282 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530283 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530284 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530285 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530286 me.set_interval_for_si_sync();
287 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530288 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530289 callback();
290 }
Rushabh Mehta69fa8082018-07-17 18:22:51 +0530291 },
292 error: () => {
293 setTimeout(() => frappe.set_route('List', 'POS Profile'), 2000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530294 }
295 })
296 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530297
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530298 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530299 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530300 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530301 this.meta = r.message.meta;
302 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530303 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530304 this.customers = r.message.customers;
305 this.serial_no_data = r.message.serial_no_data;
306 this.batch_no_data = r.message.batch_no_data;
Giovanni2144e022017-12-10 17:27:09 +0100307 this.barcode_data = r.message.barcode_data;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530308 this.tax_data = r.message.tax_data;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530309 this.contacts = r.message.contacts;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530310 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530311 this.price_list_data = r.message.price_list_data;
312 this.bin_data = r.message.bin_data;
313 this.pricing_rules = r.message.pricing_rules;
314 this.print_template = r.message.print_template;
315 this.pos_profile_data = r.message.pos_profile;
316 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530317 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530318 this.letter_head = (this.pos_profile_data.length > 0) ? frappe.boot.letter_heads[this.pos_profile_data[letter_head]] : {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530319 },
320
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530321 save_previous_entry: function () {
322 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
323 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530324 }
325 },
326
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530327 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530328 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530329 this.frm = {}
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530330 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530331 this.setup();
Rohit Waghchaure61165122017-05-02 13:04:08 +0530332 this.set_default_customer()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530333 },
334
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530335 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530336 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530337
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530338 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530339 this.actual_qty_dict = {};
340
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530341 if (load_doc) {
342 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
rohitwaghchauref688af32017-11-08 11:33:39 +0530343 this.frm.doc.offline_pos_name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530344 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530345
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530346 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530347 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530348 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530349 })
350
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530351 this.print_template_data = frappe.render_template("print_template", {
352 content: this.print_template,
Faris Ansari29d64ca2017-05-23 17:12:51 +0530353 title: "POS",
354 base_url: frappe.urllib.get_base_url(),
355 print_css: frappe.boot.print_css,
356 print_settings: this.print_settings,
357 header: this.letter_head.header,
358 footer: this.letter_head.footer,
Makarand Bauskar724cc352017-05-23 17:43:42 +0530359 landscape: false,
360 columns: []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530361 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530362 },
363
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530364 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530365 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530366 this.party_field.$input.attr('disabled', false);
367 if(this.selected_row) {
368 this.selected_row.hide()
369 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530370 },
371
Rohit Waghchaure61165122017-05-02 13:04:08 +0530372 set_default_customer: function() {
373 if (this.default_customer && !this.frm.doc.customer) {
374 this.party_field.$input.val(this.default_customer);
375 this.frm.doc.customer = this.default_customer;
376 this.numeric_keypad.show();
377 this.toggle_list_customer(false)
378 this.toggle_item_cart(true)
379 }
380 },
381
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530382 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530383 var me = this;
384 this.party = party;
385 this.price_list = (party == "Customer" ?
386 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
387 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
388 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
389 },
390
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530391 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530392 this.make_item_list();
393 this.make_discount_field()
394 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200395
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530396 make_control: function() {
397 this.frm = {}
398 this.frm.doc = this.doc
399 this.set_transaction_defaults("Customer");
rohitwaghchaurea32e57a2018-03-08 16:21:13 +0530400 this.frm.doc["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false;
401 this.frm.doc["allow_user_to_edit_discount"] = this.pos_profile_data["allow_user_to_edit_discount"] ? true : false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530402 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530403 this.make_search();
404 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530405 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530406 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530407 },
408
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530409 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530410 var me = this;
Rohit Waghchaureeee830e2018-05-13 16:17:18 +0530411 this.search_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530412 df: {
413 "fieldtype": "Data",
Mohammed R Abukhamseen8662a4f2018-07-01 14:14:09 +0300414 "label": __("Item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530415 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530416 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530417 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530418 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530419 only_input: true,
420 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530421
Rohit Waghchaureeee830e2018-05-13 16:17:18 +0530422 this.search_item.make_input();
Giovanni2144e022017-12-10 17:27:09 +0100423
Rohit Waghchaureeee830e2018-05-13 16:17:18 +0530424 this.search_item.$input.on("keypress", function (event) {
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530425
426 clearTimeout(me.last_search_timeout);
427 me.last_search_timeout = setTimeout(() => {
Rohit Waghchaureeee830e2018-05-13 16:17:18 +0530428 if((me.search_item.$input.val() != "") || (event.which == 13)) {
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530429 me.items = me.get_items();
430 me.make_item_list();
Giovanni2144e022017-12-10 17:27:09 +0100431 }
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530432 }, 400);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530433 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530434
Faris Ansari339d9c92017-03-07 18:14:06 +0530435 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530436 sorted_item_groups = this.get_sorted_item_groups()
437 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530438 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
439 }).join("");
440
441 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
442
443 this.search_item_group.on('click', '.dropdown-menu a', function() {
444 me.selected_item_group = $(this).attr('data-value');
445 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
446
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530447 me.page_len = 20;
448 me.items = me.get_items();
449 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530450 })
451
Faris Ansari45510102017-03-09 14:43:37 +0530452 me.toggle_more_btn();
453
454 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530455 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530456 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530457 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530458 me.toggle_more_btn();
459 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530460
Faris Ansari45510102017-03-09 14:43:37 +0530461 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530462 me.update_customer()
463 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530464 },
465
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530466 get_sorted_item_groups: function() {
467 list = {}
468 $.each(this.item_groups, function(i, data) {
469 list[i] = data[0]
470 })
471
472 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
473 },
474
Faris Ansari45510102017-03-09 14:43:37 +0530475 toggle_more_btn: function() {
476 if(!this.items || this.items.length <= this.page_len) {
477 this.wrapper.find(".btn-more").hide();
478 } else {
479 this.wrapper.find(".btn-more").show();
480 }
481 },
482
483 toggle_totals_area: function(show) {
484
Faris Ansari07c9f352017-03-09 17:03:14 +0530485 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530486 show = this.is_totals_area_collapsed;
487 }
488
489 var totals_area = this.wrapper.find('.totals-area');
490 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
491 .toggle(show);
492
493 if(show) {
494 totals_area.find('.collapse-btn i')
495 .removeClass('octicon-chevron-down')
496 .addClass('octicon-chevron-up');
497 } else {
498 totals_area.find('.collapse-btn i')
499 .removeClass('octicon-chevron-up')
500 .addClass('octicon-chevron-down');
501 }
502
503 this.is_totals_area_collapsed = !show;
504 },
505
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530506 make_list_customers: function () {
507 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530508 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530509 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530510 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530511 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530512 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530513 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530514
Faris Ansari45510102017-03-09 14:43:37 +0530515 me.render_list_customers();
516 me.toggle_totals_area(false);
517
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530518 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530519 $(this).toggleClass("view_customer");
520 if($(this).hasClass("view_customer")) {
521 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530522 me.list_customers.show();
523 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530524 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530525 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530526 } else {
527 if(me.frm.doc.docstatus == 0) {
528 me.party_field.$input.attr('disabled', false);
529 }
530 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530531 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530532 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530533 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530534 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530535 }
536 });
537 this.add_customer_btn.on('click', function() {
538 me.save_previous_entry();
539 me.create_new();
540 me.refresh();
541 me.set_focus();
542 });
Faris Ansari45510102017-03-09 14:43:37 +0530543 this.pos_bill.on('click', '.collapse-btn', function() {
544 me.toggle_totals_area();
545 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530546 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200547
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530548 bind_numeric_keypad: function() {
549 var me = this;
550 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
551 me.numeric_val = '';
552 })
553
554 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
555 me.numeric_id = $(this).attr("id") || me.numeric_id;
556 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530557 if(me.numeric_id) {
558 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
559 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530560
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530561 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530562 me.numeric_val += me.val;
563 me.selected_field.val(flt(me.numeric_val))
564 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530565 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530566 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530567
568 if(me.numeric_id && $(this).hasClass('pos-operation')) {
569 me.numeric_keypad.find('button.pos-operation').removeClass('active');
570 $(this).addClass('active');
571
572 me.selected_row.find('.pos-list-row').removeClass('active');
573 me.selected_field.closest('.pos-list-row').addClass('active');
574 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530575 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200576
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530577 $(this.numeric_keypad).find('.numeric-del').click(function(){
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530578 if(me.numeric_id) {
579 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
580 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
581 me.selected_field.val(me.numeric_val);
582 me.selected_field.trigger("change")
583 } else {
584 //Remove an item from the cart, if focus is at selected item
585 me.remove_selected_item()
586 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530587 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200588
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530589 $(this.numeric_keypad).find('.pos-pay').click(function(){
590 me.validate();
591 me.update_paid_amount_status(true);
592 me.create_invoice();
593 me.make_payment();
594 })
595 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530596
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530597 remove_selected_item: function() {
598 this.remove_item = []
599 idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx")
600 this.remove_item.push(idx)
601 this.remove_zero_qty_item()
602 this.update_paid_amount_status(false)
603 },
604
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530605 render_list_customers: function () {
606 var me = this;
607
608 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530609 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530610 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530611 if (!this.si_docs.length) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530612 this.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530613 return;
614 }
Faris Ansari45510102017-03-09 14:43:37 +0530615
616 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530617 if(this.si_docs.length) {
618 this.si_docs.forEach(function (data, i) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530619 for (var key in data) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530620 html += frappe.render_template("pos_invoice_list", {
621 sr: i + 1,
622 name: key,
623 customer: data[key].customer,
624 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
625 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
626 data: me.get_doctype_status(data[key])
627 });
628 }
629 });
630 }
Faris Ansari45510102017-03-09 14:43:37 +0530631 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530632
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530633 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530634 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530635 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530636 me.list_customers_btn.toggleClass("view_customer");
637 me.pos_bill.show();
638 me.list_customers_btn.show();
rohitwaghchauref688af32017-11-08 11:33:39 +0530639 me.frm.doc.offline_pos_name = $(this).parents().attr('invoice-name')
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530640 me.edit_record();
641 })
642
643 //actions
644 $(this.wrapper).find('.list-select-all').click(function () {
645 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
646 me.removed_items = [];
647 if ($(this).is(":checked")) {
648 $.each(me.si_docs, function (index, data) {
649 for (key in data) {
650 me.removed_items.push(key)
651 }
652 });
653 }
654
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530655 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530656 });
657
658 $(this.wrapper).find('.list-delete').click(function () {
rohitwaghchauref688af32017-11-08 11:33:39 +0530659 me.frm.doc.offline_pos_name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530660 if ($(this).is(":checked")) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530661 me.removed_items.push(me.frm.doc.offline_pos_name);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530662 } else {
rohitwaghchauref688af32017-11-08 11:33:39 +0530663 me.removed_items.pop(me.frm.doc.offline_pos_name)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530664 }
665
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530666 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530667 });
668 },
669
670 bind_delete_event: function() {
671 var me = this;
672
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530673 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530674 frappe.confirm(__("Delete permanently?"), function () {
675 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530676 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530677 me.render_list_customers();
678 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530679 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530680 },
681
682 set_focus: function () {
683 if (this.default_customer || this.frm.doc.customer) {
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530684 this.set_customer_value_in_party_field();
Rohit Waghchaureeee830e2018-05-13 16:17:18 +0530685 this.search_item.$input.focus();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530686 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530687 this.party_field.$input.focus();
688 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530689 },
690
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530691 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530692 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530693
694 if(!this.party_field) {
695 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
696 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530697 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530698 })).insertAfter(this.page.$title_area.hide());
699 }
700
701 this.party_field = frappe.ui.form.make_control({
702 df: {
703 "fieldtype": "Data",
704 "options": this.party,
705 "label": this.party,
706 "fieldname": this.party.toLowerCase(),
707 "placeholder": __("Select or add new customer")
708 },
709 parent: this.page.wrapper.find(".party-area"),
710 only_input: true,
711 });
712
713 this.party_field.make_input();
714 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530715 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530716 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530717
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530718 this.party_field.awesomeplete =
719 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530720 minChars: 0,
721 maxItems: 99,
722 autoFirst: true,
723 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530724 filter: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530725 if (item.value.includes('is_action')) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530726 return true;
727 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530728
729 input = input.toLowerCase();
730 item = this.get_item(item.value);
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530731 result = item ? item.searchtext.includes(input) : '';
732 if(!result) {
733 me.prepare_customer_mapper(input);
734 } else {
735 return result;
736 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530737 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530738 item: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530739 var d = this.get_item(item.value);
Faris Ansari45510102017-03-09 14:43:37 +0530740 var html = "<span>" + __(d.label || d.value) + "</span>";
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530741 if(d.customer_name) {
742 html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>';
743 }
744
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530745 return $('<li></li>')
746 .data('item.autocomplete', d)
747 .html('<a><p>' + html + '</p></a>')
748 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530749 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530750 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530751
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530752 this.prepare_customer_mapper()
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530753 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530754
755 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530756 .on('input', function (e) {
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530757 if(me.customers_mapper.length <= 1) {
758 me.prepare_customer_mapper(e.target.value);
759 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530760 me.party_field.awesomeplete.list = me.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530761 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530762 .on('awesomplete-select', function (e) {
763 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530764 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530765 if (!customer) return;
766 // create customer link
767 if (customer.action) {
768 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530769 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530770 }
Faris Ansari45510102017-03-09 14:43:37 +0530771 me.toggle_list_customer(false);
772 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530773 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530774 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530775 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530776 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530777 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530778 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530779 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530780 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530781
782 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530783 me.toggle_list_customer(false)
784 me.toggle_item_cart(true)
785 } else {
786 me.toggle_list_customer(true)
787 me.toggle_item_cart(false)
788 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530789 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530790 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530791 var item = me.party_field.awesomeplete
792 .get_item(e.originalEvent.text.value);
793 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530794 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530795 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530796 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530797 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530798 },
799
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530800 prepare_customer_mapper: function(key) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530801 var me = this;
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530802 var customer_data = '';
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530803
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530804 if (key) {
805 key = key.toLowerCase().trim();
806 var re = new RegExp('%', 'g');
807 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'));
808
809 customer_data = $.grep(this.customers, function(data) {
810 contact = me.contacts[data.name];
811 if(reg.test(data.name.toLowerCase())
812 || reg.test(data.customer_name.toLowerCase())
813 || (contact && reg.test(contact["mobile_no"]))
814 || (contact && reg.test(contact["phone"]))
815 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
816 return data;
817 }
818 })
819 } else {
820 customer_data = this.customers;
821 }
822
823 this.customers_mapper = [];
824
825 customer_data.forEach(function (c, index) {
826 if(index < 30) {
827 contact = me.contacts[c.name];
828 if(contact && !c['phone']) {
829 c["phone"] = contact["phone"];
830 c["email_id"] = contact["email_id"];
831 c["mobile_no"] = contact["mobile_no"];
832 }
833
834 me.customers_mapper.push({
835 label: c.name,
836 value: c.name,
837 customer_name: c.customer_name,
838 customer_group: c.customer_group,
839 territory: c.territory,
840 phone: contact ? contact["phone"] : '',
841 mobile_no: contact ? contact["mobile_no"] : '',
842 email_id: contact ? contact["email_id"] : '',
843 searchtext: ['customer_name', 'customer_group', 'name', 'value',
844 'label', 'email_id', 'phone', 'mobile_no']
845 .map(key => c[key]).join(' ')
846 .toLowerCase()
847 });
848 } else {
849 return;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530850 }
851 });
852
853 this.customers_mapper.push({
854 label: "<span class='text-primary link-option'>"
855 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
856 + __("Create a new Customer")
857 + "</span>",
858 value: 'is_action',
859 action: me.add_customer
860 });
861 },
862
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530863 autocomplete_customers: function() {
864 this.party_field.awesomeplete.list = this.customers_mapper;
865 },
866
Faris Ansari45510102017-03-09 14:43:37 +0530867 toggle_edit_button: function(flag) {
868 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
869 },
870
871 toggle_list_customer: function(flag) {
872 this.list_customers.toggle(flag);
873 },
874
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530875 toggle_item_cart: function(flag) {
876 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
877 },
878
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530879 add_customer: function() {
880 this.frm.doc.customer = "";
rohitwaghchaure09483d32017-05-16 08:51:24 +0530881 this.update_customer(true);
882 this.numeric_keypad.show();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530883 },
884
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530885 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530886 var me = this;
Rushabh Mehta37146262017-02-01 18:17:35 +0530887
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530888 this.customer_doc = new frappe.ui.Dialog({
889 'title': 'Customer',
890 fields: [
891 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530892 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530893 "fieldname": "full_name",
894 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530895 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530896 },
897 {
898 "fieldtype": "Section Break"
899 },
900 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530901 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530902 "fieldname": "email_id",
903 "fieldtype": "Data"
904 },
905 {
906 "fieldtype": "Column Break"
907 },
908 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530909 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530910 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530911 "fieldtype": "Data"
912 },
913 {
914 "fieldtype": "Section Break"
915 },
916 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530917 "label": __("Address Name"),
918 "read_only": 1,
919 "fieldname": "name",
920 "fieldtype": "Data"
921 },
922 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530923 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530924 "fieldname": "address_line1",
925 "fieldtype": "Data"
926 },
927 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530928 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530929 "fieldname": "address_line2",
930 "fieldtype": "Data"
931 },
932 {
933 "fieldtype": "Column Break"
934 },
935 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530936 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530937 "fieldname": "city",
938 "fieldtype": "Data"
939 },
940 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530941 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530942 "fieldname": "state",
943 "fieldtype": "Data"
944 },
945 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530946 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530947 "fieldname": "pincode",
948 "fieldtype": "Data"
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530949 },
950 {
951 "label": __("Customer POS Id"),
952 "fieldname": "customer_pos_id",
953 "fieldtype": "Data",
954 "hidden": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530955 }
956 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530957 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530958 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530959 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530960
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530961 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530962 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530963 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530964 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530965 });
966 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530967
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530968 render_address_data: function() {
969 var me = this;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530970 this.address_data = this.address[this.frm.doc.customer] || {};
971 if(!this.address_data.email_id || !this.address_data.phone) {
972 this.address_data = this.contacts[this.frm.doc.customer];
973 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530974
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530975 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530976 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
977 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530978 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530979
980 if(!this.customer_doc.fields_dict.customer_pos_id.value) {
981 this.customer_doc.set_value("customer_pos_id", $.now())
982 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530983 },
984
985 get_address_from_localstorage: function() {
986 this.address_details = this.get_customers_details()
987 return this.address_details[this.frm.doc.customer]
988 },
989
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530990 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530991 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530992 this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530993 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530994 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530995 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530996 this.update_address_and_customer_list(new_customer)
997 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530998 this.update_customer_in_localstorage()
999 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301000 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301001 },
1002
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301003 update_address_and_customer_list: function(new_customer) {
1004 var me = this;
1005 if(new_customer) {
1006 this.customers_mapper.push({
1007 label: this.frm.doc.customer,
1008 value: this.frm.doc.customer,
1009 customer_group: "",
1010 territory: ""
1011 });
1012 }
1013
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301014 this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details())
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301015 },
1016
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301017 get_prompt_details: function() {
1018 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301019 this.prompt_details['country'] = this.pos_profile_data.country;
rohitwaghchauree2176b82017-07-28 20:52:02 +05301020 this.prompt_details['territory'] = this.pos_profile_data["territory"];
1021 this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"];
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301022 this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301023 return JSON.stringify(this.prompt_details)
1024 },
1025
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301026 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301027 var me = this;
1028 this.frm.doc.customer = doc.label || doc.name;
1029 this.frm.doc.customer_name = doc.customer_name;
1030 this.frm.doc.customer_group = doc.customer_group;
1031 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301032 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301033 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301034 },
1035
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301036 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301037 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301038 if (!this.price_list) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301039 frappe.msgprint(__("Price List not found or disabled"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301040 return;
1041 }
1042
1043 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301044
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301045 var $wrap = me.wrapper.find(".item-list");
1046 me.wrapper.find(".item-list").empty();
1047
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301048 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301049 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301050 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301051 $(frappe.render_template("pos_item", {
1052 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301053 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301054 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +05301055 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +05301056 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Valmik Janglab52d5b22018-05-17 00:02:03 -07001057 item_uom: obj.stock_uom,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301058 color: frappe.get_palette(obj.item_name),
1059 abbr: frappe.get_abbr(obj.item_name)
1060 })).tooltip().appendTo($wrap);
1061 }
1062 });
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001063
Faris Ansari45510102017-03-09 14:43:37 +05301064 $wrap.append(`
1065 <div class="image-view-item btn-more text-muted text-center">
1066 <div class="image-view-body">
1067 <i class="mega-octicon octicon-package"></i>
1068 <div>Load more items</div>
1069 </div>
1070 </div>
1071 `);
1072
1073 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301074 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +05301075 $("<p class='text-muted small' style='padding-left: 10px'>"
1076 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301077 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301078
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301079 if (this.items.length == 1
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301080 && this.search_item.$input.val()) {
1081 this.search_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301082 this.add_to_cart();
1083 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301084 },
1085
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301086 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301087 // To search item as per the key enter
1088
1089 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301090 this.item_serial_no = {};
1091 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301092
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301093 if (item_code) {
1094 return $.grep(this.item_data, function (item) {
1095 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301096 return true
1097 }
1098 })
1099 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301100
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301101 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301102
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301103 key = this.search_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301104 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301105 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301106 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301107
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301108 if (key) {
1109 return $.grep(this.items_list, function (item) {
1110 if (search_status) {
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301111 if (in_list(me.batch_no_data[item.item_code], me.search_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301112 search_status = false;
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301113 return me.item_batch_no[item.item_code] = me.search_item.$input.val()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301114 } else if (me.serial_no_data[item.item_code]
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301115 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.search_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301116 search_status = false;
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301117 me.item_serial_no[item.item_code] = [me.search_item.$input.val(), me.serial_no_data[item.item_code][me.search_item.$input.val()]]
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301118 return true
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301119 } else if (in_list(me.barcode_data[item.item_code], me.search_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301120 search_status = false;
Giovanni2144e022017-12-10 17:27:09 +01001121 return true;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301122 } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) ||
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301123 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301124 return true
1125 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301126 }
1127 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301128 } else {
1129 return this.items_list;
1130 }
1131 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301132
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301133 apply_category: function() {
1134 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301135 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301136
1137 if(category == 'All Item Groups') {
1138 return this.item_data
1139 } else {
1140 return this.item_data.filter(function(element, index, array){
1141 return element.item_group == category;
1142 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301143 }
1144 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001145
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301146 bind_items_event: function() {
1147 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301148 $(this.wrapper).on('click', '.pos-bill-item', function() {
1149 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1150 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301151 me.numeric_val = "";
1152 me.numeric_id = ""
1153 me.item_code = $(this).attr("data-item-code");
1154 me.render_selected_item()
1155 me.bind_qty_event()
1156 me.update_rate()
1157 $(me.wrapper).find(".selected-item").scrollTop(1000);
1158 })
1159 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301160
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301161 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301162 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301163
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301164 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301165 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301166 var qty = $(this).val();
1167 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301168 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301169 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301170
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301171 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301172 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1173 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301174 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301175 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301176
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301177 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301178 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1179 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301180 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301181 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001182
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301183 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301184 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301185 var discount = $(this).val();
Saurabh1a4d77a2018-02-08 01:32:48 +05301186 if(discount > 100){
1187 discount = $(this).val('');
1188 frappe.show_alert({
1189 indicator: 'red',
1190 message: __('Discount amount cannot be greater than 100%')
1191 });
1192 me.update_discount(item_code, discount);
Rushabh Mehta69fa8082018-07-17 18:22:51 +05301193 }else{
Saurabh1a4d77a2018-02-08 01:32:48 +05301194 me.update_discount(item_code, discount);
1195 me.update_value();
1196 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301197 })
1198 },
1199
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301200 bind_events: function() {
1201 var me = this;
1202 // if form is local then allow this function
1203 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1204 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301205 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301206 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301207 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301208
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301209 if (me.frm.doc.docstatus == 0) {
1210 me.items = me.get_items($(this).attr("data-item-code"))
1211 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301212 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301213 }
1214 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301215
1216 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301217 },
1218
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301219 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301220 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301221 this.items = this.get_items(item_code);
1222 this.validate_serial_no()
1223 this.set_item_details(item_code, "qty", qty);
1224 },
1225
1226 update_discount: function(item_code, discount) {
1227 var me = this;
1228 this.items = this.get_items(item_code);
1229 this.set_item_details(item_code, "discount_percentage", discount);
1230 },
1231
1232 update_rate: function () {
1233 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301234 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301235 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301236 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301237 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301238 })
1239 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301240
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301241 update_value: function() {
1242 var me = this;
1243 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1244 "rate": ".pos-item-price", "amount": ".pos-amount"}
1245 this.child_doc = this.get_child_item(this.item_code);
1246
1247 if(me.child_doc.length) {
1248 $.each(fields, function(key, field) {
1249 $(me.selected_row).find(field).val(me.child_doc[0][key])
1250 })
1251 } else {
1252 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301253 }
1254 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301255
1256 clear_selected_row: function() {
1257 $(this.wrapper).find('.selected-item').empty();
1258 },
1259
1260 render_selected_item: function() {
1261 this.child_doc = this.get_child_item(this.item_code);
1262 $(this.wrapper).find('.selected-item').empty();
1263 if(this.child_doc.length) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301264 this.child_doc[0]["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
rohitwaghchaurea32e57a2018-03-08 16:21:13 +05301265 this.child_doc[0]["allow_user_to_edit_discount"] = this.pos_profile_data["allow_user_to_edit_discount"] ? true : false;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301266 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1267 $(this.wrapper).find('.selected-item').html(this.selected_row)
1268 }
1269
1270 $(this.selected_row).find('.form-control').click(function(){
1271 $(this).select();
1272 })
1273 },
1274
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301275 get_child_item: function(item_code) {
1276 var me = this;
1277 return $.map(me.frm.doc.items, function(doc){
1278 if(doc.item_code == item_code) {
1279 return doc
1280 }
1281 })
1282 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301283
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301284 set_item_details: function (item_code, field, value) {
1285 var me = this;
1286 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301287 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301288 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301289
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301290 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301291 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301292 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301293 if (d.serial_no && field == 'qty') {
1294 me.validate_serial_no_qty(d, item_code, field, value)
1295 }
1296
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301297 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301298 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301299 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301300 me.remove_item.push(d.idx)
1301 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301302
1303 if(field=="discount_percentage" && value == 0) {
1304 d.rate = d.price_list_rate;
1305 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301306 }
1307 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301308
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301309 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301310 },
1311
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301312 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301313 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301314 var idx = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301315 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301316 $.each(this.frm.doc["items"] || [], function (i, d) {
1317 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301318 d.idx = idx;
1319 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301320 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301321 }
1322 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301323
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301324 this.frm.doc["items"] = this.items;
1325 },
1326
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301327 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301328 var me = this;
1329
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301330 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301331 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301332
1333 if(me.frm.doc.additional_discount_percentage && me.frm.doc.discount_amount) {
1334 // Reset discount amount
1335 me.frm.doc.discount_amount = 0;
1336 }
1337
Faris Ansariab74ca72017-05-30 12:54:42 +05301338 var total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301339
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301340 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301341 total = me.frm.doc.net_total
1342 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301343
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301344 me.frm.doc.discount_amount = flt(total * flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301345 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301346 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301347 });
1348
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301349 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301350 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1351 me.frm.doc.additional_discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301352 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301353 me.wrapper.find('input.discount-percentage').val(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301354 });
1355 },
1356
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301357 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301358 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301359 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301360 frappe.throw(__("Please select customer"))
1361 }
1362 },
1363
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301364 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301365 var me = this;
1366 var caught = false;
1367 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1368
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301369 this.customer_validate();
1370 this.mandatory_batch_no();
1371 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301372 this.validate_warehouse();
1373
1374 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301375 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301376 if (d.item_code == me.items[0].item_code) {
1377 caught = true;
1378 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301379 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301380 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301381 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1382 d.warehouse = me.item_serial_no[d.item_code][1]
1383 }
1384
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301385 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301386 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301387 }
1388 }
1389 });
1390 }
1391
1392 // if item not found then add new item
1393 if (!caught)
1394 this.add_new_item_to_grid();
1395
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301396 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301397 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301398 },
1399
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301400 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301401 var me = this;
1402 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1403 this.child.item_code = this.items[0].item_code;
1404 this.child.item_name = this.items[0].item_name;
1405 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301406 this.child.brand = this.items[0].brand;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301407 this.child.description = this.items[0].description || this.items[0].item_name;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301408 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301409 this.child.qty = 1;
1410 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301411 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1412 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301413 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301414 ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301415 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1416 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1417 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301418 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301419 this.child.batch_no = this.item_batch_no[this.child.item_code];
1420 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1421 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301422 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301423 },
1424
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301425 update_paid_amount_status: function (update_paid_amount) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301426 if (this.frm.doc.offline_pos_name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301427 update_paid_amount = update_paid_amount ? false : true;
1428 }
1429
1430 this.refresh(update_paid_amount);
1431 },
1432
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301433 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301434 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301435 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301436 this.set_primary_action();
1437 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301438
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301439 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301440 this.apply_pricing_rule();
1441 this.discount_amount_applied = false;
1442 this._calculate_taxes_and_totals();
1443 this.calculate_discount_amount();
1444 this.show_items_in_item_cart();
1445 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301446 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301447 this.set_totals();
pawan53a4d2c2018-03-21 09:19:36 +05301448 this.update_total_qty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301449 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301450
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301451 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301452 return erpnext.get_currency(this.frm.doc.company);
1453 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301454
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301455 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301456 var me = this;
1457 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301458 var $no_items_message = this.wrapper.find(".no-items-message");
1459 $no_items_message.toggle(this.frm.doc.items.length === 0);
1460
1461 var $totals_area = this.wrapper.find('.totals-area');
1462 $totals_area.toggle(this.frm.doc.items.length > 0);
1463
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301464 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301465 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301466 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301467 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301468 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301469 discount_percentage: d.discount_percentage || 0.0,
1470 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301471 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301472 rate: format_currency(d.rate, me.frm.doc.currency),
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301473 amount: format_currency(d.amount, me.frm.doc.currency),
1474 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301475 })).appendTo($items);
1476 });
1477
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301478 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301479 $(this).select();
1480 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301481
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301482 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301483 $(this).select();
1484 });
1485
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301486 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301487 $(this).select();
1488 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301489 },
1490
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301491 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301492 var me = this;
1493 me.frm.doc.total_taxes_and_charges = 0.0
1494
1495 var taxes = this.frm.doc.taxes || [];
1496 $(this.wrapper)
1497 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1498 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301499
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301500 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301501 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1502 $(frappe.render_template("pos_tax_row", {
1503 description: d.description,
1504 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1505 me.frm.doc.currency)
1506 })).appendTo(me.wrapper.find(".tax-table"));
1507 }
1508 });
1509 },
1510
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301511 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301512 var me = this;
1513 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1514 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301515 this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage);
1516 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301517 },
1518
pawan53a4d2c2018-03-21 09:19:36 +05301519 update_total_qty: function() {
1520 var me = this;
1521 var qty_total = 0;
1522 $.each(this.frm.doc["items"] || [], function (i, d) {
1523 if (d.item_code) {
1524 qty_total += d.qty;
1525 }
1526 });
1527 this.frm.doc.qty_total = qty_total;
1528 this.wrapper.find('.qty-total').text(this.frm.doc.qty_total);
1529 },
1530
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301531 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301532 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301533 this.page.set_primary_action(__("New Cart"), function () {
1534 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301535 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301536 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301537
augustinfotech135a54c2017-11-16 15:28:42 +05301538 if (this.frm.doc.docstatus == 1 || this.pos_profile_data["allow_print_before_pay"]) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301539 this.page.set_secondary_action(__("Print"), function () {
augustinfotech135a54c2017-11-16 15:28:42 +05301540 me.create_invoice();
Faris Ansariab74ca72017-05-30 12:54:42 +05301541 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301542 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301543 })
augustinfotech135a54c2017-11-16 15:28:42 +05301544 }
Giovanni2144e022017-12-10 17:27:09 +01001545
1546 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301547 this.page.add_menu_item(__("Email"), function () {
1548 me.email_prompt()
1549 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301550 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301551 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301552
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301553 make_new_cart: function (){
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301554 this.item_code = '';
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301555 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301556 this.save_previous_entry();
1557 this.create_new();
1558 this.refresh();
1559 this.toggle_input_field();
1560 this.render_list_customers();
1561 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301562 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301563
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301564 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301565 var me = this;
1566
mbauskar66e2a512017-06-22 16:18:12 +05301567 this.msgprint = frappe.msgprint(
Faris Ansariab74ca72017-05-30 12:54:42 +05301568 `<a class="btn btn-primary print_doc"
1569 style="margin-right: 5px;">${__('Print')}</a>
1570 <a class="btn btn-default new_doc">${__('New')}</a>`);
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301571
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301572 $('.print_doc').click(function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301573 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301574 me.print_document(html)
1575 })
1576
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301577 $('.new_doc').click(function () {
mbauskar66e2a512017-06-22 16:18:12 +05301578 me.msgprint.hide()
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301579 me.make_new_cart()
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301580 })
1581 },
1582
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301583 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301584 var w = window.open();
1585 w.document.write(html);
1586 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301587 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301588 w.print();
1589 w.close();
1590 }, 1000)
1591 },
1592
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301593 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301594 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301595 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301596 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301597 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301598 this.print_dialog()
1599 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301600 },
1601
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301602 update_serial_no: function() {
1603 var me = this;
1604
1605 //Remove the sold serial no from the cache
1606 $.each(this.frm.doc.items, function(index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301607 var sn = data.serial_no.split('\n')
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301608 if(sn.length) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301609 var serial_no_list = me.serial_no_data[data.item_code]
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301610 if(serial_no_list) {
1611 $.each(sn, function(i, serial_no) {
1612 if(in_list(Object.keys(serial_no_list), serial_no)) {
1613 delete serial_no_list[serial_no]
1614 }
1615 })
1616 me.serial_no_data[data.item_code] = serial_no_list;
1617 }
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301618 }
1619 })
1620 },
1621
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301622 change_status: function () {
1623 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301624 this.frm.doc.docstatus = 1;
1625 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301626 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301627 }
1628 },
1629
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301630 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301631 var pointer_events = 'inherit'
Faris Ansariab74ca72017-05-30 12:54:42 +05301632 var disabled = this.frm.doc.docstatus == 1 ? true: false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301633 $(this.wrapper).find('input').attr("disabled", disabled);
1634 $(this.wrapper).find('select').attr("disabled", disabled);
1635 $(this.wrapper).find('input').attr("disabled", disabled);
1636 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301637 $(this.wrapper).find('button').attr("disabled", disabled);
1638 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301639
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301640 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301641 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301642 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301643
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301644 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301645 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1646 this.set_primary_action();
1647 },
1648
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301649 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301650 var me = this;
rohitwaghchauref688af32017-11-08 11:33:39 +05301651 var invoice_data = {};
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301652 this.si_docs = this.get_doc_from_localstorage();
pawan53a4d2c2018-03-21 09:19:36 +05301653
rohitwaghchauref688af32017-11-08 11:33:39 +05301654 if (this.frm.doc.offline_pos_name) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301655 this.update_invoice()
Helkyd2b6bf822017-11-22 14:49:51 +01001656 //to retrieve and set the default payment
rohitwaghchaure441032d2018-02-01 16:54:10 +05301657 invoice_data[this.frm.doc.offline_pos_name] = this.frm.doc;
1658 invoice_data[this.frm.doc.offline_pos_name].payments[0].amount = this.frm.doc.net_total
1659 invoice_data[this.frm.doc.offline_pos_name].payments[0].base_amount = this.frm.doc.net_total
Helkyd2b6bf822017-11-22 14:49:51 +01001660
1661 this.frm.doc.paid_amount = this.frm.doc.net_total
1662 this.frm.doc.outstanding_amount = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301663 } else {
rohitwaghchauref688af32017-11-08 11:33:39 +05301664 this.frm.doc.offline_pos_name = $.now();
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301665 this.frm.doc.posting_date = frappe.datetime.get_today();
1666 this.frm.doc.posting_time = frappe.datetime.now_time();
pawan53a4d2c2018-03-21 09:19:36 +05301667 this.frm.doc.pos_total_qty = this.frm.doc.qty_total;
Charles-Henri Decultot2305eda2017-06-19 05:55:54 +02001668 this.frm.doc.pos_profile = this.pos_profile_data['name'];
rohitwaghchauref688af32017-11-08 11:33:39 +05301669 invoice_data[this.frm.doc.offline_pos_name] = this.frm.doc;
1670 this.si_docs.push(invoice_data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301671 this.update_localstorage();
1672 this.set_primary_action();
1673 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301674 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301675 },
1676
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301677 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301678 var me = this;
1679 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301680 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301681 for (var key in data) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301682 if (key == me.frm.doc.offline_pos_name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301683 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301684 me.update_localstorage();
1685 }
1686 }
rohitwaghchauref688af32017-11-08 11:33:39 +05301687 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301688 },
1689
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301690 update_localstorage: function () {
1691 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301692 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301693 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301694 frappe.throw(__("LocalStorage is full , did not save"))
1695 }
1696 },
1697
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301698 get_doc_from_localstorage: function () {
1699 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301700 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301701 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301702 return []
1703 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301704 },
1705
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301706 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301707 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301708 setInterval(function () {
rohitwaghchaure88491712017-10-02 12:13:36 +05301709 me.freeze_screen = false;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301710 me.sync_sales_invoice()
rohitwaghchaurea32e57a2018-03-08 16:21:13 +05301711 }, 180000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301712 },
1713
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301714 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301715 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301716 this.si_docs = this.get_submitted_invoice() || [];
1717 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301718 this.customers_list = this.get_customers_details() || {};
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301719 if(this.customer_doc) {
1720 this.freeze = this.customer_doc.display
1721 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301722
rohitwaghchaure88491712017-10-02 12:13:36 +05301723 freeze_screen = this.freeze_screen || false;
1724
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301725 if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301726 this.freeze = true;
1727
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301728 frappe.call({
1729 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
rohitwaghchaure88491712017-10-02 12:13:36 +05301730 freeze: freeze_screen,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301731 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301732 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301733 email_queue_list: me.email_queue_list,
1734 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301735 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301736 callback: function (r) {
1737 if (r.message) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301738 me.freeze = false;
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301739 me.customers = r.message.synced_customers_list;
1740 me.address = r.message.synced_address;
1741 me.contacts = r.message.synced_contacts;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301742 me.removed_items = r.message.invoice;
rohitwaghchauref688af32017-11-08 11:33:39 +05301743 me.removed_email = r.message.email_queue;
1744 me.removed_customers = r.message.customers;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301745 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301746 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301747 me.remove_customer_from_localstorage();
rohitwaghchauref688af32017-11-08 11:33:39 +05301748 me.prepare_customer_mapper();
1749 me.autocomplete_customers();
1750 me.render_list_customers();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301751 }
1752 }
1753 })
1754 }
1755 },
1756
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301757 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301758 var invoices = [];
1759 var index = 1;
Faris Ansariab74ca72017-05-30 12:54:42 +05301760 var docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301761 if (docs) {
1762 invoices = $.map(docs, function (data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301763 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301764 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301765 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301766 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301767 return data
1768 }
1769 }
1770 });
1771 }
1772
1773 return invoices
1774 },
1775
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301776 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301777 var me = this;
1778 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301779 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301780 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301781 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301782 for (var key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301783 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301784 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301785 }
1786 }
1787 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301788 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301789 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301790 this.update_localstorage();
1791 }
1792 },
1793
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301794 remove_email_queue_from_localstorage: function() {
1795 var me = this;
1796 this.email_queue = this.get_email_queue()
1797 if (this.removed_email) {
1798 $.each(this.email_queue_list, function (index, data) {
1799 if (in_list(me.removed_email, index)) {
1800 delete me.email_queue[index]
1801 }
1802 })
1803 this.update_email_queue();
1804 }
1805 },
1806
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301807 remove_customer_from_localstorage: function() {
1808 var me = this;
1809 this.customer_details = this.get_customers_details()
1810 if (this.removed_customers) {
1811 $.each(this.customers_list, function (index, data) {
1812 if (in_list(me.removed_customers, index)) {
1813 delete me.customer_details[index]
1814 }
1815 })
1816 this.update_customer_in_localstorage();
1817 }
1818 },
1819
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301820 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301821 var me = this;
1822 this.customer_validate();
1823 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301824 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301825 },
1826
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301827 item_validate: function () {
1828 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301829 frappe.throw(__("Select items to save the invoice"))
1830 }
1831 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301832
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301833 validate_mode_of_payments: function () {
1834 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301835 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1836 }
1837 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301838
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301839 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301840 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301841 var item_code = ''
1842 var serial_no = '';
1843 for (var key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301844 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301845 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301846 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301847
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301848 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301849 this.refresh();
1850 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1851 'item': this.items[0].item_code
1852 })))
1853 }
1854
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301855 if (item_code && serial_no) {
1856 $.each(this.frm.doc.items, function (index, data) {
1857 if (data.item_code == item_code) {
1858 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301859 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1860 'serial_no': serial_no
1861 })))
1862 }
1863 }
1864 })
1865 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301866 },
1867
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301868 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301869 var me = this;
1870 if (args.item_code == item_code && args.serial_no
1871 && field == 'qty' && cint(value) != value) {
1872 args.qty = 0.0;
1873 this.refresh();
1874 frappe.throw(__("Serial no item cannot be a fraction"))
1875 }
1876
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301877 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301878 args.qty = 0.0;
1879 args.serial_no = ''
1880 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301881 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1882 'item': item_code
1883 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301884 }
1885 },
1886
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301887 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301888 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301889 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301890 frappe.prompt([{
1891 'fieldname': 'batch',
1892 'fieldtype': 'Select',
1893 'label': __('Batch No'),
1894 'reqd': 1,
1895 'options': this.batch_no_data[this.items[0].item_code]
1896 }],
1897 function(values){
1898 me.item_batch_no[me.items[0].item_code] = values.batch;
1899 },
1900 __('Select Batch No'))
1901 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301902 },
1903
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301904 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301905 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301906 $.each(this.frm.doc["items"], function (n, item) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301907 var pricing_rule = me.get_pricing_rule(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301908 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301909 if (pricing_rule.length) {
1910 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301911 item.margin_type = pricing_rule[0].margin_type;
1912 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1913 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1914 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301915 me.apply_pricing_rule_on_item(item)
1916 } else if (item.pricing_rule) {
1917 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301918 item.margin_rate_or_amount = 0.0;
1919 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301920 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301921 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301922 }
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001923
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301924 if(item.discount_percentage > 0) {
1925 me.apply_pricing_rule_on_item(item)
1926 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301927 })
1928 },
1929
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301930 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301931 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301932 return $.grep(this.pricing_rules, function (data) {
1933 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301934 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301935 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301936 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301937 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301938 return true
1939 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301940 }
1941 }
1942 })
1943 },
1944
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301945 validate_item_condition: function (data, item) {
1946 var apply_on = frappe.model.scrub(data.apply_on);
1947
1948 return (data.apply_on == 'Item Group')
1949 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
1950 },
1951
1952 validate_item_group: function (pr_item_group, cart_item_group) {
1953 //pr_item_group = pricing rule's item group
1954 //cart_item_group = cart item's item group
1955 //this.item_groups has information about item group's lft and rgt
1956 //for example: {'Foods': [12, 19]}
1957
1958 pr_item_group = this.item_groups[pr_item_group]
1959 cart_item_group = this.item_groups[cart_item_group]
1960
1961 return (cart_item_group[0] >= pr_item_group[0] &&
1962 cart_item_group[1] <= pr_item_group[1])
1963 },
1964
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301965 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301966 //This method check condition based on applicable for
Faris Ansariab74ca72017-05-30 12:54:42 +05301967 var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301968 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301969 return true
1970 }
1971 },
1972
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301973 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301974 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301975 'Customer': [data.customer, [this.frm.doc.customer]],
1976 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1977 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301978 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301979 }
1980 },
1981
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301982 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301983 //This method validate duplicate pricing rule
1984 var pricing_rule_name = '';
1985 var priority = 0;
1986 var pricing_rule_list = [];
1987 var priority_list = []
1988
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301989 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301990
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301991 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301992 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301993 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301994 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301995 priority = data.priority
1996 pricing_rule_list.push(data)
1997 }
1998 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301999
Faris Ansariab74ca72017-05-30 12:54:42 +05302000 var count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302001 $.each(priority_list, function (index, value) {
2002 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302003 count++
2004 }
2005 })
2006
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302007 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302008 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
2009 'pricing_rule': pricing_rule_name
2010 })))
2011 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05302012
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302013 return pricing_rule_list
2014 }
2015 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05302016
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302017 validate_warehouse: function () {
2018 if (this.items[0].is_stock_item && !this.items[0].default_warehouse && !this.pos_profile_data['warehouse']) {
Neil Trini Lasrado5ddd7ac2016-10-24 15:25:33 +05302019 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302020 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302021 },
2022
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302023 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302024 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05302025
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302026 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302027 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302028 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05302029 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302030 }
2031
2032 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05302033 },
2034
2035 update_customer_in_localstorage: function() {
2036 var me = this;
2037 try {
2038 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
2039 } catch (e) {
2040 frappe.throw(__("LocalStorage is full , did not save"))
2041 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302042 }
Saurabh1a4d77a2018-02-08 01:32:48 +05302043})