blob: 599372411a02955653cc96b034926e80254db2c4 [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 Ansari519cc092017-08-10 11:22:03 +053011 wrapper.pos = new erpnext.pos.PointOfSale(wrapper);
12 cur_pos = wrapper.pos;
Rushabh Mehta72e17192014-08-08 15:30:49 +053013}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053014
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053015frappe.pages['pos'].refresh = function (wrapper) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053016 window.onbeforeunload = function () {
17 return wrapper.pos.beforeunload()
18 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +053019}
20
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053021erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053022 init: function (wrapper) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +053023 this.page_len = 20;
rohitwaghchaured2be55b2017-06-07 12:04:01 +053024 this.freeze = false;
Rohit Waghchauree0934d12016-05-11 15:04:57 +053025 this.page = wrapper.page;
26 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053027 this.set_indicator();
28 this.onload();
29 this.make_menu_list();
Rohit Waghchaure017f5002017-03-08 17:34:39 +053030 this.bind_events();
Rohit Waghchaure75177c02017-03-16 15:21:21 +053031 this.bind_items_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053032 this.si_docs = this.get_doc_from_localstorage();
33 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053034
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053035 beforeunload: function (e) {
36 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053037 e = e || window.event;
38
39 // For IE and Firefox prior to version 4
40 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053041 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 +053042 return
43 }
44
45 // For Safari
46 return __("You are in offline mode. You will not be able to reload until you have network.");
47 }
48 },
49
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053050 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053051 var me = this;
52 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053053 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053054 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053055 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053056 },
57
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053058 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053059 var me = this;
60 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053061 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053062 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053063 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053064 method: "frappe.handler.ping",
65 callback: function (r) {
66 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053067 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053068 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053069 }
70 }
71 })
72 },
73
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053074 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053075 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053076 this.get_data_from_server(function () {
rohitwaghchaured2be55b2017-06-07 12:04:01 +053077 me.make_control();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053078 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053079 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053080 },
81
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053082 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053083 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +053084 this.page.clear_menu();
Faris Ansari769b6ba2017-05-16 07:31:10 +053085
86 // for mobile
87 this.page.add_menu_item(__("Pay"), function () {
88 me.validate();
89 me.update_paid_amount_status(true);
90 me.create_invoice();
91 me.make_payment();
92 }).addClass('visible-xs');
93
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053094 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053095 me.save_previous_entry();
96 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053097 })
98
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053099 this.page.add_menu_item(__("Sync Master Data"), function () {
100 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530101 me.load_data(false);
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530102 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530103 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530104 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530105 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530106
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530107 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530108 me.sync_sales_invoice()
109 });
110
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530111 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530112 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530113 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530114 },
115
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530116 email_prompt: function() {
117 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530118 var fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530119 {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"},
120 {fieldtype: "Section Break"},
121 {label:__("Subject"), fieldtype:"Data", reqd: 1,
122 fieldname:"subject",length:524288},
123 {fieldtype: "Section Break"},
124 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
125 fieldname:"content"},
126 {fieldtype: "Section Break"},
127 {fieldtype: "Column Break"}];
128
129 this.email_dialog = new frappe.ui.Dialog({
130 title: "Email",
131 fields: fields,
132 primary_action_label: __("Send"),
133 primary_action: function() {
134 me.send_action();
135 }
136 });
137
138 this.email_dialog.show()
139 },
140
141 send_action: function() {
142 this.email_queue = this.get_email_queue()
143 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
144 this.update_email_queue()
145 this.email_dialog.hide()
146 },
147
148 update_email_queue: function () {
149 try {
150 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
151 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530152 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530153 }
154 },
155
156 get_email_queue: function () {
157 try {
158 return JSON.parse(localStorage.getItem('email_queue')) || {};
159 } catch (e) {
160 return {}
161 }
162 },
163
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530164 get_customers_details: function () {
165 try {
166 return JSON.parse(localStorage.getItem('customer_details')) || {};
167 } catch (e) {
168 return {}
169 }
170 },
171
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530172 dialog_actions: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530173 var me = this;
174
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530175 $(this.list_body).find('.list-select-all').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530176 me.removed_items = [];
177 $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked"))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530178 if ($(this).is(":checked")) {
179 $.each(me.si_docs, function (index, data) {
180 for (key in data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530181 me.removed_items.push(key)
182 }
183 })
184 }
185
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530186 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530187 })
188
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530189 $(this.list_body).find('.list-delete').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530190 me.name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530191 if ($(this).is(":checked")) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530192 me.removed_items.push(me.name);
193 } else {
194 me.removed_items.pop(me.name)
195 }
196
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530197 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530198 })
199 },
200
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530201 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530202 var me = this;
203
204 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530205 if (doc_data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530206 this.frm.doc = doc_data[0][this.name];
207 this.set_missing_values();
208 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530209 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530210 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530211 }
212 },
213
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530214 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530215 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530216 this.validate_list()
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530217 this.remove_doc_from_localstorage()
218 this.update_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530219 // this.dialog_actions();
220 this.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530221 },
222
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530223 validate_list: function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530224 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530225 this.si_docs = this.get_submitted_invoice()
226 $.each(this.removed_items, function(index, name){
227 $.each(me.si_docs, function(key, data){
228 if(me.si_docs[key][name] && me.si_docs[key][name].offline_pos_name == name ){
229 frappe.throw(__("Submitted orders can not be deleted"))
230 }
231 })
232 })
233 },
234
235 toggle_delete_button: function () {
236 var me = this;
237 if(this.pos_profile_data["allow_delete"]) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530238 if (this.removed_items && this.removed_items.length > 0) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530239 $(this.page.wrapper).find('.btn-danger').show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530240 } else {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530241 $(this.page.wrapper).find('.btn-danger').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530242 }
243 }
244 },
245
246 get_doctype_status: function (doc) {
247 if (doc.docstatus == 0) {
248 return { status: "Draft", indicator: "red" }
249 } else if (doc.outstanding_amount == 0) {
250 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530251 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530252 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530253 }
254 },
255
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530256 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530257 var me = this;
258 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530259 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530260 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530261 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530262 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530263
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530264 if (this.frm.doc.customer) {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530265 this.party_field.$input.val(this.frm.doc.customer);
266 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530267
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530268 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530269 this.frm.doc.write_off_account = doc.write_off_account
270 }
271
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530272 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530273 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530274 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530275 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530276
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530277 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530278 var me = this;
279 this.si_docs = this.get_doc_from_localstorage();
280
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530281 return $.grep(this.si_docs, function (data) {
282 for (key in data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530283 return key == me.name
284 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530285 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530286 },
287
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530288 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530289 var me = this;
290 frappe.call({
291 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
292 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530293 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530294 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530295 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530296 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530297 me.set_interval_for_si_sync();
298 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530299 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530300 callback();
301 }
302 }
303 })
304 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530305
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530306 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530307 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530308 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530309 this.meta = r.message.meta;
310 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530311 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530312 this.customers = r.message.customers;
313 this.serial_no_data = r.message.serial_no_data;
314 this.batch_no_data = r.message.batch_no_data;
315 this.tax_data = r.message.tax_data;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530316 this.contacts = r.message.contacts;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530317 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530318 this.price_list_data = r.message.price_list_data;
319 this.bin_data = r.message.bin_data;
320 this.pricing_rules = r.message.pricing_rules;
321 this.print_template = r.message.print_template;
322 this.pos_profile_data = r.message.pos_profile;
323 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530324 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530325 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 +0530326 },
327
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530328 save_previous_entry: function () {
329 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
330 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530331 }
332 },
333
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530334 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530335 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530336 this.frm = {}
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530337 this.name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530338 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530339 this.setup();
Rohit Waghchaure61165122017-05-02 13:04:08 +0530340 this.set_default_customer()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530341 },
342
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530343 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530344 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530345
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530346 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530347 this.actual_qty_dict = {};
348
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530349 if (load_doc) {
350 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530351 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530352
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530353 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530354 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530355 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530356 })
357
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530358 this.print_template_data = frappe.render_template("print_template", {
359 content: this.print_template,
Faris Ansari29d64ca2017-05-23 17:12:51 +0530360 title: "POS",
361 base_url: frappe.urllib.get_base_url(),
362 print_css: frappe.boot.print_css,
363 print_settings: this.print_settings,
364 header: this.letter_head.header,
365 footer: this.letter_head.footer,
Makarand Bauskar724cc352017-05-23 17:43:42 +0530366 landscape: false,
367 columns: []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530368 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530369 },
370
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530371 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530372 this.make();
373 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530374 this.party_field.$input.attr('disabled', false);
375 if(this.selected_row) {
376 this.selected_row.hide()
377 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530378 },
379
Rohit Waghchaure61165122017-05-02 13:04:08 +0530380 set_default_customer: function() {
381 if (this.default_customer && !this.frm.doc.customer) {
382 this.party_field.$input.val(this.default_customer);
383 this.frm.doc.customer = this.default_customer;
384 this.numeric_keypad.show();
385 this.toggle_list_customer(false)
386 this.toggle_item_cart(true)
387 }
388 },
389
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530390 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530391 var me = this;
392 this.party = party;
393 this.price_list = (party == "Customer" ?
394 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
395 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
396 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
397 },
398
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530399 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530400 this.make_item_list();
401 this.make_discount_field()
402 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200403
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530404 make_control: function() {
405 this.frm = {}
406 this.frm.doc = this.doc
407 this.set_transaction_defaults("Customer");
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530408 this.frm.doc["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530409 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530410 this.make_search();
411 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530412 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530413 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530414 },
415
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530416 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530417 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530418 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530419 df: {
420 "fieldtype": "Data",
421 "label": "Item",
422 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530423 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530424 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530425 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530426 only_input: true,
427 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530428
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530429 this.serach_item.make_input();
430 this.serach_item.$input.on("keyup", function () {
431 setTimeout(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530432 me.items = me.get_items();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530433 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530434 }, 1000);
435 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530436
Faris Ansari339d9c92017-03-07 18:14:06 +0530437 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530438 sorted_item_groups = this.get_sorted_item_groups()
439 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530440 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
441 }).join("");
442
443 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
444
445 this.search_item_group.on('click', '.dropdown-menu a', function() {
446 me.selected_item_group = $(this).attr('data-value');
447 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
448
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530449 me.page_len = 20;
450 me.items = me.get_items();
451 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530452 })
453
Faris Ansari45510102017-03-09 14:43:37 +0530454 me.toggle_more_btn();
455
456 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530457 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530458 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530459 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530460 me.toggle_more_btn();
461 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530462
Faris Ansari45510102017-03-09 14:43:37 +0530463 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530464 me.update_customer()
465 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530466 },
467
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530468 get_sorted_item_groups: function() {
469 list = {}
470 $.each(this.item_groups, function(i, data) {
471 list[i] = data[0]
472 })
473
474 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
475 },
476
Faris Ansari45510102017-03-09 14:43:37 +0530477 toggle_more_btn: function() {
478 if(!this.items || this.items.length <= this.page_len) {
479 this.wrapper.find(".btn-more").hide();
480 } else {
481 this.wrapper.find(".btn-more").show();
482 }
483 },
484
485 toggle_totals_area: function(show) {
486
Faris Ansari07c9f352017-03-09 17:03:14 +0530487 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530488 show = this.is_totals_area_collapsed;
489 }
490
491 var totals_area = this.wrapper.find('.totals-area');
492 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
493 .toggle(show);
494
495 if(show) {
496 totals_area.find('.collapse-btn i')
497 .removeClass('octicon-chevron-down')
498 .addClass('octicon-chevron-up');
499 } else {
500 totals_area.find('.collapse-btn i')
501 .removeClass('octicon-chevron-up')
502 .addClass('octicon-chevron-down');
503 }
504
505 this.is_totals_area_collapsed = !show;
506 },
507
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530508 make_list_customers: function () {
509 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530510 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530511 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530512 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530513 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530514 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530515 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530516
Faris Ansari45510102017-03-09 14:43:37 +0530517 me.render_list_customers();
518 me.toggle_totals_area(false);
519
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530520 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530521 $(this).toggleClass("view_customer");
522 if($(this).hasClass("view_customer")) {
523 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530524 me.list_customers.show();
525 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530526 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530527 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530528 } else {
529 if(me.frm.doc.docstatus == 0) {
530 me.party_field.$input.attr('disabled', false);
531 }
532 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530533 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530534 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530535 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530536 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530537 }
538 });
539 this.add_customer_btn.on('click', function() {
540 me.save_previous_entry();
541 me.create_new();
542 me.refresh();
543 me.set_focus();
544 });
Faris Ansari45510102017-03-09 14:43:37 +0530545 this.pos_bill.on('click', '.collapse-btn', function() {
546 me.toggle_totals_area();
547 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530548 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200549
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530550 bind_numeric_keypad: function() {
551 var me = this;
552 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
553 me.numeric_val = '';
554 })
555
556 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
557 me.numeric_id = $(this).attr("id") || me.numeric_id;
558 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530559 if(me.numeric_id) {
560 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
561 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530562
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530563 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530564 me.numeric_val += me.val;
565 me.selected_field.val(flt(me.numeric_val))
566 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530567 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530568 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530569
570 if(me.numeric_id && $(this).hasClass('pos-operation')) {
571 me.numeric_keypad.find('button.pos-operation').removeClass('active');
572 $(this).addClass('active');
573
574 me.selected_row.find('.pos-list-row').removeClass('active');
575 me.selected_field.closest('.pos-list-row').addClass('active');
576 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530577 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200578
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530579 $(this.numeric_keypad).find('.numeric-del').click(function(){
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530580 if(me.numeric_id) {
581 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
582 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
583 me.selected_field.val(me.numeric_val);
584 me.selected_field.trigger("change")
585 } else {
586 //Remove an item from the cart, if focus is at selected item
587 me.remove_selected_item()
588 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530589 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200590
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530591 $(this.numeric_keypad).find('.pos-pay').click(function(){
592 me.validate();
593 me.update_paid_amount_status(true);
594 me.create_invoice();
595 me.make_payment();
596 })
597 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530598
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530599 remove_selected_item: function() {
600 this.remove_item = []
601 idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx")
602 this.remove_item.push(idx)
603 this.remove_zero_qty_item()
604 this.update_paid_amount_status(false)
605 },
606
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530607 render_list_customers: function () {
608 var me = this;
609
610 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530611 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530612 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530613 if (!this.si_docs.length) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530614 return;
615 }
Faris Ansari45510102017-03-09 14:43:37 +0530616
617 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530618 if(this.si_docs.length) {
619 this.si_docs.forEach(function (data, i) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530620 for (var key in data) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530621 html += frappe.render_template("pos_invoice_list", {
622 sr: i + 1,
623 name: key,
624 customer: data[key].customer,
625 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
626 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
627 data: me.get_doctype_status(data[key])
628 });
629 }
630 });
631 }
Faris Ansari45510102017-03-09 14:43:37 +0530632 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530633
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530634 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530635 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530636 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530637 me.list_customers_btn.toggleClass("view_customer");
638 me.pos_bill.show();
639 me.list_customers_btn.show();
640 me.name = $(this).parents().attr('invoice-name')
641 me.edit_record();
642 })
643
644 //actions
645 $(this.wrapper).find('.list-select-all').click(function () {
646 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
647 me.removed_items = [];
648 if ($(this).is(":checked")) {
649 $.each(me.si_docs, function (index, data) {
650 for (key in data) {
651 me.removed_items.push(key)
652 }
653 });
654 }
655
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530656 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530657 });
658
659 $(this.wrapper).find('.list-delete').click(function () {
660 me.name = $(this).parent().parent().attr('invoice-name');
661 if ($(this).is(":checked")) {
662 me.removed_items.push(me.name);
663 } else {
664 me.removed_items.pop(me.name)
665 }
666
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530667 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530668 });
669 },
670
671 bind_delete_event: function() {
672 var me = this;
673
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530674 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530675 frappe.confirm(__("Delete permanently?"), function () {
676 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530677 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530678 me.render_list_customers();
679 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530680 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530681 },
682
683 set_focus: function () {
684 if (this.default_customer || this.frm.doc.customer) {
685 this.serach_item.$input.focus();
686 } 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);
731 var searchtext =
732 Object.keys(item)
Rohit Waghchaurec8490622017-06-10 12:21:37 +0530733 .filter(key => ['customer_name', 'customer_group', 'value', 'label', 'email_id', 'phone', 'mobile_no'].includes(key))
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530734 .map(key => item[key])
735 .join(" ")
736 .toLowerCase();
737
738 return searchtext.includes(input)
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530739 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530740 item: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530741 var d = this.get_item(item.value);
Faris Ansari45510102017-03-09 14:43:37 +0530742 var html = "<span>" + __(d.label || d.value) + "</span>";
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530743 if(d.customer_name) {
744 html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>';
745 }
746
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530747 return $('<li></li>')
748 .data('item.autocomplete', d)
749 .html('<a><p>' + html + '</p></a>')
750 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530751 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530752 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530753
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530754 this.prepare_customer_mapper()
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530755 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530756
757 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530758 .on('input', function (e) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530759 me.party_field.awesomeplete.list = me.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530760 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530761 .on('awesomplete-select', function (e) {
762 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530763 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530764 if (!customer) return;
765 // create customer link
766 if (customer.action) {
767 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530768 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530769 }
Faris Ansari45510102017-03-09 14:43:37 +0530770 me.toggle_list_customer(false);
771 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530772 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530773 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530774 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530775 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530776 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530777 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530778 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530779 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530780
781 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530782 me.toggle_list_customer(false)
783 me.toggle_item_cart(true)
784 } else {
785 me.toggle_list_customer(true)
786 me.toggle_item_cart(false)
787 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530788 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530789 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530790 var item = me.party_field.awesomeplete
791 .get_item(e.originalEvent.text.value);
792 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530793 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530794 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530795 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530796 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530797 },
798
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530799 prepare_customer_mapper: function() {
800 var me = this;
801
802 this.customers_mapper = this.customers.map(function (c) {
803 contact = me.contacts[c.name];
804 return {
805 label: c.name,
806 value: c.name,
807 customer_name: c.customer_name,
808 customer_group: c.customer_group,
809 territory: c.territory,
810 phone: contact ? contact["phone"] : '',
Rohit Waghchaurec8490622017-06-10 12:21:37 +0530811 mobile_no: contact ? contact["mobile_no"] : '',
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530812 email_id: contact ? contact["email_id"] : ''
813 }
814 });
815
816 this.customers_mapper.push({
817 label: "<span class='text-primary link-option'>"
818 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
819 + __("Create a new Customer")
820 + "</span>",
821 value: 'is_action',
822 action: me.add_customer
823 });
824 },
825
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530826 autocomplete_customers: function() {
827 this.party_field.awesomeplete.list = this.customers_mapper;
828 },
829
Faris Ansari45510102017-03-09 14:43:37 +0530830 toggle_edit_button: function(flag) {
831 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
832 },
833
834 toggle_list_customer: function(flag) {
835 this.list_customers.toggle(flag);
836 },
837
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530838 toggle_item_cart: function(flag) {
839 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
840 },
841
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530842 add_customer: function() {
843 this.frm.doc.customer = "";
rohitwaghchaure09483d32017-05-16 08:51:24 +0530844 this.update_customer(true);
845 this.numeric_keypad.show();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530846 },
847
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530848 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530849 var me = this;
Rushabh Mehta37146262017-02-01 18:17:35 +0530850
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530851 this.customer_doc = new frappe.ui.Dialog({
852 'title': 'Customer',
853 fields: [
854 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530855 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530856 "fieldname": "full_name",
857 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530858 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530859 },
860 {
861 "fieldtype": "Section Break"
862 },
863 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530864 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530865 "fieldname": "email_id",
866 "fieldtype": "Data"
867 },
868 {
869 "fieldtype": "Column Break"
870 },
871 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530872 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530873 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530874 "fieldtype": "Data"
875 },
876 {
877 "fieldtype": "Section Break"
878 },
879 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530880 "label": __("Address Name"),
881 "read_only": 1,
882 "fieldname": "name",
883 "fieldtype": "Data"
884 },
885 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530886 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530887 "fieldname": "address_line1",
888 "fieldtype": "Data"
889 },
890 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530891 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530892 "fieldname": "address_line2",
893 "fieldtype": "Data"
894 },
895 {
896 "fieldtype": "Column Break"
897 },
898 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530899 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530900 "fieldname": "city",
901 "fieldtype": "Data"
902 },
903 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530904 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530905 "fieldname": "state",
906 "fieldtype": "Data"
907 },
908 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530909 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530910 "fieldname": "pincode",
911 "fieldtype": "Data"
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530912 },
913 {
914 "label": __("Customer POS Id"),
915 "fieldname": "customer_pos_id",
916 "fieldtype": "Data",
917 "hidden": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530918 }
919 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530920 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530921 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530922 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530923
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530924 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530925 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530926 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530927 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530928 });
929 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530930
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530931 render_address_data: function() {
932 var me = this;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530933 this.address_data = this.address[this.frm.doc.customer] || {};
934 if(!this.address_data.email_id || !this.address_data.phone) {
935 this.address_data = this.contacts[this.frm.doc.customer];
936 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530937
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530938 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530939 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
940 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530941 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530942
943 if(!this.customer_doc.fields_dict.customer_pos_id.value) {
944 this.customer_doc.set_value("customer_pos_id", $.now())
945 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530946 },
947
948 get_address_from_localstorage: function() {
949 this.address_details = this.get_customers_details()
950 return this.address_details[this.frm.doc.customer]
951 },
952
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530953 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530954 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530955 this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530956 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530957 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530958 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530959 this.update_address_and_customer_list(new_customer)
960 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530961 this.update_customer_in_localstorage()
962 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530963 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530964 },
965
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530966 update_address_and_customer_list: function(new_customer) {
967 var me = this;
968 if(new_customer) {
969 this.customers_mapper.push({
970 label: this.frm.doc.customer,
971 value: this.frm.doc.customer,
972 customer_group: "",
973 territory: ""
974 });
975 }
976
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530977 this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details())
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530978 },
979
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530980 get_prompt_details: function() {
981 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530982 this.prompt_details['country'] = this.pos_profile_data.country;
rohitwaghchauree2176b82017-07-28 20:52:02 +0530983 this.prompt_details['territory'] = this.pos_profile_data["territory"];
984 this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"];
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530985 this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530986 return JSON.stringify(this.prompt_details)
987 },
988
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530989 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530990 var me = this;
991 this.frm.doc.customer = doc.label || doc.name;
992 this.frm.doc.customer_name = doc.customer_name;
993 this.frm.doc.customer_group = doc.customer_group;
994 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530995 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530996 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530997 },
998
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530999 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301000 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301001 if (!this.price_list) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301002 frappe.msgprint(__("Price List not found or disabled"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301003 return;
1004 }
1005
1006 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301007
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301008 var $wrap = me.wrapper.find(".item-list");
1009 me.wrapper.find(".item-list").empty();
1010
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301011 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301012 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301013 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301014 $(frappe.render_template("pos_item", {
1015 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301016 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301017 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +05301018 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +05301019 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301020 color: frappe.get_palette(obj.item_name),
1021 abbr: frappe.get_abbr(obj.item_name)
1022 })).tooltip().appendTo($wrap);
1023 }
1024 });
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001025
Faris Ansari45510102017-03-09 14:43:37 +05301026 $wrap.append(`
1027 <div class="image-view-item btn-more text-muted text-center">
1028 <div class="image-view-body">
1029 <i class="mega-octicon octicon-package"></i>
1030 <div>Load more items</div>
1031 </div>
1032 </div>
1033 `);
1034
1035 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301036 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +05301037 $("<p class='text-muted small' style='padding-left: 10px'>"
1038 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301039 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301040
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301041 if (this.items.length == 1
1042 && this.serach_item.$input.val()) {
1043 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301044 this.add_to_cart();
1045 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301046 },
1047
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301048 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301049 // To search item as per the key enter
1050
1051 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301052 this.item_serial_no = {};
1053 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301054
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301055 if (item_code) {
1056 return $.grep(this.item_data, function (item) {
1057 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301058 return true
1059 }
1060 })
1061 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301062
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301063 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301064
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301065 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301066 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301067 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301068 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301069
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301070 if (key) {
1071 return $.grep(this.items_list, function (item) {
1072 if (search_status) {
1073 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301074 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301075 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
1076 } else if (me.serial_no_data[item.item_code]
1077 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301078 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301079 me.item_serial_no[item.item_code] = [me.serach_item.$input.val(), me.serial_no_data[item.item_code][me.serach_item.$input.val()]]
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301080 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301081 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301082 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301083 return item.barcode == me.serach_item.$input.val();
rohitwaghchaure860144f2017-07-12 15:01:56 +05301084 } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) ||
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301085 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301086 return true
1087 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301088 }
1089 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301090 } else {
1091 return this.items_list;
1092 }
1093 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301094
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301095 apply_category: function() {
1096 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301097 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301098
1099 if(category == 'All Item Groups') {
1100 return this.item_data
1101 } else {
1102 return this.item_data.filter(function(element, index, array){
1103 return element.item_group == category;
1104 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301105 }
1106 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001107
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301108 bind_items_event: function() {
1109 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301110 $(this.wrapper).on('click', '.pos-bill-item', function() {
1111 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1112 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301113 me.numeric_val = "";
1114 me.numeric_id = ""
1115 me.item_code = $(this).attr("data-item-code");
1116 me.render_selected_item()
1117 me.bind_qty_event()
1118 me.update_rate()
1119 $(me.wrapper).find(".selected-item").scrollTop(1000);
1120 })
1121 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301122
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301123 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301124 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301125
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301126 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301127 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301128 var qty = $(this).val();
1129 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301130 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301131 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301132
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301133 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301134 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1135 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301136 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301137 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301138
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301139 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301140 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1141 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301142 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301143 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001144
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301145 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301146 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301147 var discount = $(this).val();
1148 me.update_discount(item_code, discount)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301149 me.update_value()
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301150 })
1151 },
1152
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301153 bind_events: function() {
1154 var me = this;
1155 // if form is local then allow this function
1156 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1157 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301158 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301159 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301160 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301161
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301162 if (me.frm.doc.docstatus == 0) {
1163 me.items = me.get_items($(this).attr("data-item-code"))
1164 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301165 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301166 }
1167 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301168
1169 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301170 },
1171
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301172 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301173 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301174 this.items = this.get_items(item_code);
1175 this.validate_serial_no()
1176 this.set_item_details(item_code, "qty", qty);
1177 },
1178
1179 update_discount: function(item_code, discount) {
1180 var me = this;
1181 this.items = this.get_items(item_code);
1182 this.set_item_details(item_code, "discount_percentage", discount);
1183 },
1184
1185 update_rate: function () {
1186 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301187 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301188 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301189 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301190 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301191 })
1192 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301193
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301194 update_value: function() {
1195 var me = this;
1196 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1197 "rate": ".pos-item-price", "amount": ".pos-amount"}
1198 this.child_doc = this.get_child_item(this.item_code);
1199
1200 if(me.child_doc.length) {
1201 $.each(fields, function(key, field) {
1202 $(me.selected_row).find(field).val(me.child_doc[0][key])
1203 })
1204 } else {
1205 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301206 }
1207 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301208
1209 clear_selected_row: function() {
1210 $(this.wrapper).find('.selected-item').empty();
1211 },
1212
1213 render_selected_item: function() {
1214 this.child_doc = this.get_child_item(this.item_code);
1215 $(this.wrapper).find('.selected-item').empty();
1216 if(this.child_doc.length) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301217 this.child_doc[0]["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301218 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1219 $(this.wrapper).find('.selected-item').html(this.selected_row)
1220 }
1221
1222 $(this.selected_row).find('.form-control').click(function(){
1223 $(this).select();
1224 })
1225 },
1226
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301227 get_child_item: function(item_code) {
1228 var me = this;
1229 return $.map(me.frm.doc.items, function(doc){
1230 if(doc.item_code == item_code) {
1231 return doc
1232 }
1233 })
1234 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301235
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301236 set_item_details: function (item_code, field, value) {
1237 var me = this;
1238 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301239 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301240 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301241
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301242 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301243 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301244 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301245 if (d.serial_no && field == 'qty') {
1246 me.validate_serial_no_qty(d, item_code, field, value)
1247 }
1248
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301249 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301250 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301251 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301252 me.remove_item.push(d.idx)
1253 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301254
1255 if(field=="discount_percentage" && value == 0) {
1256 d.rate = d.price_list_rate;
1257 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301258 }
1259 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301260
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301261 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301262 this.remove_zero_qty_item();
1263 }
1264
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301265 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301266 },
1267
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301268 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301269 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301270 var idx = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301271 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301272 $.each(this.frm.doc["items"] || [], function (i, d) {
1273 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301274 d.idx = idx;
1275 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301276 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301277 }
1278 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301279
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301280 this.frm.doc["items"] = this.items;
1281 },
1282
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301283 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301284 var me = this;
1285
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301286 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301287 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
Faris Ansariab74ca72017-05-30 12:54:42 +05301288 var total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301289
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301290 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301291 total = me.frm.doc.net_total
1292 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301293
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301294 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 +05301295 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
1296 me.refresh();
1297 });
1298
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301299 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301300 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1301 me.frm.doc.additional_discount_percentage = 0.0;
1302 me.wrapper.find('input.discount-percentage').val(0);
1303 me.refresh();
1304 });
1305 },
1306
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301307 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301308 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301309 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301310 frappe.throw(__("Please select customer"))
1311 }
1312 },
1313
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301314 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301315 var me = this;
1316 var caught = false;
1317 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1318
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301319 this.customer_validate();
1320 this.mandatory_batch_no();
1321 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301322 this.validate_warehouse();
1323
1324 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301325 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301326 if (d.item_code == me.items[0].item_code) {
1327 caught = true;
1328 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301329 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301330 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301331 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1332 d.warehouse = me.item_serial_no[d.item_code][1]
1333 }
1334
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301335 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301336 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301337 }
1338 }
1339 });
1340 }
1341
1342 // if item not found then add new item
1343 if (!caught)
1344 this.add_new_item_to_grid();
1345
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301346 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301347 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301348 },
1349
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301350 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301351 var me = this;
1352 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1353 this.child.item_code = this.items[0].item_code;
1354 this.child.item_name = this.items[0].item_name;
1355 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301356 this.child.brand = this.items[0].brand;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301357 this.child.description = this.items[0].description || this.items[0].item_name;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301358 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301359 this.child.qty = 1;
1360 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301361 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1362 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301363 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301364 ? 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 +05301365 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1366 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1367 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301368 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301369 this.child.batch_no = this.item_batch_no[this.child.item_code];
1370 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1371 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301372 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301373 },
1374
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301375 update_paid_amount_status: function (update_paid_amount) {
1376 if (this.name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301377 update_paid_amount = update_paid_amount ? false : true;
1378 }
1379
1380 this.refresh(update_paid_amount);
1381 },
1382
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301383 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301384 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301385 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301386 this.set_primary_action();
1387 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301388
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301389 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301390 this.apply_pricing_rule();
1391 this.discount_amount_applied = false;
1392 this._calculate_taxes_and_totals();
1393 this.calculate_discount_amount();
1394 this.show_items_in_item_cart();
1395 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301396 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301397 this.set_totals();
1398 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301399
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301400 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301401 return erpnext.get_currency(this.frm.doc.company);
1402 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301403
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301404 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301405 var me = this;
1406 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301407 var $no_items_message = this.wrapper.find(".no-items-message");
1408 $no_items_message.toggle(this.frm.doc.items.length === 0);
1409
1410 var $totals_area = this.wrapper.find('.totals-area');
1411 $totals_area.toggle(this.frm.doc.items.length > 0);
1412
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301413 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301414 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301415 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301416 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301417 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301418 discount_percentage: d.discount_percentage || 0.0,
1419 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301420 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301421 rate: format_currency(d.rate, me.frm.doc.currency),
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301422 amount: format_currency(d.amount, me.frm.doc.currency),
1423 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301424 })).appendTo($items);
1425 });
1426
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301427 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301428 $(this).select();
1429 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301430
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301431 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301432 $(this).select();
1433 });
1434
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301435 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301436 $(this).select();
1437 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301438 },
1439
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301440 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301441 var me = this;
1442 me.frm.doc.total_taxes_and_charges = 0.0
1443
1444 var taxes = this.frm.doc.taxes || [];
1445 $(this.wrapper)
1446 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1447 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301448
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301449 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301450 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1451 $(frappe.render_template("pos_tax_row", {
1452 description: d.description,
1453 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1454 me.frm.doc.currency)
1455 })).appendTo(me.wrapper.find(".tax-table"));
1456 }
1457 });
1458 },
1459
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301460 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301461 var me = this;
1462 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1463 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
1464 },
1465
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301466 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301467 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301468 this.page.set_primary_action(__("New Cart"), function () {
1469 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301470 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301471 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301472
1473 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301474 this.page.set_secondary_action(__("Print"), function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301475 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301476 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301477 })
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301478 this.page.add_menu_item(__("Email"), function () {
1479 me.email_prompt()
1480 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301481 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301482 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301483
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301484 make_new_cart: function (){
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301485 this.item_code = '';
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301486 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301487 this.save_previous_entry();
1488 this.create_new();
1489 this.refresh();
1490 this.toggle_input_field();
1491 this.render_list_customers();
1492 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301493 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301494
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301495 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301496 var me = this;
1497
mbauskar66e2a512017-06-22 16:18:12 +05301498 this.msgprint = frappe.msgprint(
Faris Ansariab74ca72017-05-30 12:54:42 +05301499 `<a class="btn btn-primary print_doc"
1500 style="margin-right: 5px;">${__('Print')}</a>
1501 <a class="btn btn-default new_doc">${__('New')}</a>`);
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301502
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301503 $('.print_doc').click(function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301504 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301505 me.print_document(html)
1506 })
1507
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301508 $('.new_doc').click(function () {
mbauskar66e2a512017-06-22 16:18:12 +05301509 me.msgprint.hide()
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301510 me.make_new_cart()
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301511 })
1512 },
1513
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301514 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301515 var w = window.open();
1516 w.document.write(html);
1517 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301518 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301519 w.print();
1520 w.close();
1521 }, 1000)
1522 },
1523
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301524 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301525 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301526 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301527 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301528 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301529 this.print_dialog()
1530 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301531 },
1532
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301533 update_serial_no: function() {
1534 var me = this;
1535
1536 //Remove the sold serial no from the cache
1537 $.each(this.frm.doc.items, function(index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301538 var sn = data.serial_no.split('\n')
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301539 if(sn.length) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301540 var serial_no_list = me.serial_no_data[data.item_code]
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301541 if(serial_no_list) {
1542 $.each(sn, function(i, serial_no) {
1543 if(in_list(Object.keys(serial_no_list), serial_no)) {
1544 delete serial_no_list[serial_no]
1545 }
1546 })
1547 me.serial_no_data[data.item_code] = serial_no_list;
1548 }
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301549 }
1550 })
1551 },
1552
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301553 change_status: function () {
1554 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301555 this.frm.doc.docstatus = 1;
1556 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301557 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301558 }
1559 },
1560
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301561 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301562 var pointer_events = 'inherit'
Faris Ansariab74ca72017-05-30 12:54:42 +05301563 var disabled = this.frm.doc.docstatus == 1 ? true: false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301564 $(this.wrapper).find('input').attr("disabled", disabled);
1565 $(this.wrapper).find('select').attr("disabled", disabled);
1566 $(this.wrapper).find('input').attr("disabled", disabled);
1567 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301568 $(this.wrapper).find('button').attr("disabled", disabled);
1569 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301570
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301571 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301572 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301573 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301574
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301575 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301576 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1577 this.set_primary_action();
1578 },
1579
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301580 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301581 var me = this;
1582 var invoice_data = {}
1583 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301584 if (this.name) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301585 this.update_invoice()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301586 } else {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301587 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +05301588 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301589 this.frm.doc.posting_date = frappe.datetime.get_today();
1590 this.frm.doc.posting_time = frappe.datetime.now_time();
Charles-Henri Decultot2305eda2017-06-19 05:55:54 +02001591 this.frm.doc.pos_profile = this.pos_profile_data['name'];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301592 invoice_data[this.name] = this.frm.doc
1593 this.si_docs.push(invoice_data)
1594 this.update_localstorage();
1595 this.set_primary_action();
1596 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301597 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301598 },
1599
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301600 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301601 var me = this;
1602 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301603 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301604 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301605 if (key == me.name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301606 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301607 me.update_localstorage();
1608 }
1609 }
1610 })
1611 },
1612
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301613 update_localstorage: function () {
1614 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301615 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301616 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301617 frappe.throw(__("LocalStorage is full , did not save"))
1618 }
1619 },
1620
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301621 get_doc_from_localstorage: function () {
1622 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301623 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301624 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301625 return []
1626 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301627 },
1628
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301629 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301630 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301631 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301632 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301633 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301634 },
1635
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301636 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301637 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301638 this.si_docs = this.get_submitted_invoice() || [];
1639 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301640 this.customers_list = this.get_customers_details() || {};
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301641 if(this.customer_doc) {
1642 this.freeze = this.customer_doc.display
1643 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301644
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301645 if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301646 frappe.call({
1647 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
1648 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301649 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301650 email_queue_list: me.email_queue_list,
1651 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301652 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301653 callback: function (r) {
1654 if (r.message) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301655 me.customers = r.message.synced_customers_list;
1656 me.address = r.message.synced_address;
1657 me.contacts = r.message.synced_contacts;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301658 me.removed_items = r.message.invoice;
1659 me.removed_email = r.message.email_queue
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301660 me.removed_customers = r.message.customers
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301661 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301662 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301663 me.remove_customer_from_localstorage();
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301664 me.prepare_customer_mapper()
1665 me.autocomplete_customers()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301666 }
1667 }
1668 })
1669 }
1670 },
1671
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301672 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301673 var invoices = [];
1674 var index = 1;
Faris Ansariab74ca72017-05-30 12:54:42 +05301675 var docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301676 if (docs) {
1677 invoices = $.map(docs, function (data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301678 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301679 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301680 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301681 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301682 return data
1683 }
1684 }
1685 });
1686 }
1687
1688 return invoices
1689 },
1690
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301691 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301692 var me = this;
1693 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301694 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301695 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301696 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301697 for (var key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301698 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301699 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301700 }
1701 }
1702 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301703 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301704 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301705 this.update_localstorage();
1706 }
1707 },
1708
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301709 remove_email_queue_from_localstorage: function() {
1710 var me = this;
1711 this.email_queue = this.get_email_queue()
1712 if (this.removed_email) {
1713 $.each(this.email_queue_list, function (index, data) {
1714 if (in_list(me.removed_email, index)) {
1715 delete me.email_queue[index]
1716 }
1717 })
1718 this.update_email_queue();
1719 }
1720 },
1721
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301722 remove_customer_from_localstorage: function() {
1723 var me = this;
1724 this.customer_details = this.get_customers_details()
1725 if (this.removed_customers) {
1726 $.each(this.customers_list, function (index, data) {
1727 if (in_list(me.removed_customers, index)) {
1728 delete me.customer_details[index]
1729 }
1730 })
1731 this.update_customer_in_localstorage();
1732 }
1733 },
1734
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301735 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301736 var me = this;
1737 this.customer_validate();
1738 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301739 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301740 },
1741
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301742 item_validate: function () {
1743 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301744 frappe.throw(__("Select items to save the invoice"))
1745 }
1746 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301747
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301748 validate_mode_of_payments: function () {
1749 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301750 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1751 }
1752 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301753
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301754 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301755 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301756 var item_code = ''
1757 var serial_no = '';
1758 for (var key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301759 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301760 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301761 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301762
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301763 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301764 this.refresh();
1765 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1766 'item': this.items[0].item_code
1767 })))
1768 }
1769
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301770 if (item_code && serial_no) {
1771 $.each(this.frm.doc.items, function (index, data) {
1772 if (data.item_code == item_code) {
1773 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301774 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1775 'serial_no': serial_no
1776 })))
1777 }
1778 }
1779 })
1780 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301781 },
1782
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301783 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301784 var me = this;
1785 if (args.item_code == item_code && args.serial_no
1786 && field == 'qty' && cint(value) != value) {
1787 args.qty = 0.0;
1788 this.refresh();
1789 frappe.throw(__("Serial no item cannot be a fraction"))
1790 }
1791
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301792 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301793 args.qty = 0.0;
1794 args.serial_no = ''
1795 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301796 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1797 'item': item_code
1798 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301799 }
1800 },
1801
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301802 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301803 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301804 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301805 frappe.prompt([{
1806 'fieldname': 'batch',
1807 'fieldtype': 'Select',
1808 'label': __('Batch No'),
1809 'reqd': 1,
1810 'options': this.batch_no_data[this.items[0].item_code]
1811 }],
1812 function(values){
1813 me.item_batch_no[me.items[0].item_code] = values.batch;
1814 },
1815 __('Select Batch No'))
1816 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301817 },
1818
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301819 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301820 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301821 $.each(this.frm.doc["items"], function (n, item) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301822 var pricing_rule = me.get_pricing_rule(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301823 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301824 if (pricing_rule.length) {
1825 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301826 item.margin_type = pricing_rule[0].margin_type;
1827 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1828 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1829 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301830 me.apply_pricing_rule_on_item(item)
1831 } else if (item.pricing_rule) {
1832 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301833 item.margin_rate_or_amount = 0.0;
1834 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301835 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301836 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301837 }
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001838
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301839 if(item.discount_percentage > 0) {
1840 me.apply_pricing_rule_on_item(item)
1841 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301842 })
1843 },
1844
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301845 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301846 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301847 return $.grep(this.pricing_rules, function (data) {
1848 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301849 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301850 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301851 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301852 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301853 return true
1854 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301855 }
1856 }
1857 })
1858 },
1859
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301860 validate_item_condition: function (data, item) {
1861 var apply_on = frappe.model.scrub(data.apply_on);
1862
1863 return (data.apply_on == 'Item Group')
1864 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
1865 },
1866
1867 validate_item_group: function (pr_item_group, cart_item_group) {
1868 //pr_item_group = pricing rule's item group
1869 //cart_item_group = cart item's item group
1870 //this.item_groups has information about item group's lft and rgt
1871 //for example: {'Foods': [12, 19]}
1872
1873 pr_item_group = this.item_groups[pr_item_group]
1874 cart_item_group = this.item_groups[cart_item_group]
1875
1876 return (cart_item_group[0] >= pr_item_group[0] &&
1877 cart_item_group[1] <= pr_item_group[1])
1878 },
1879
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301880 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301881 //This method check condition based on applicable for
Faris Ansariab74ca72017-05-30 12:54:42 +05301882 var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301883 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301884 return true
1885 }
1886 },
1887
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301888 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301889 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301890 'Customer': [data.customer, [this.frm.doc.customer]],
1891 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1892 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301893 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301894 }
1895 },
1896
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301897 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301898 //This method validate duplicate pricing rule
1899 var pricing_rule_name = '';
1900 var priority = 0;
1901 var pricing_rule_list = [];
1902 var priority_list = []
1903
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301904 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301905
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301906 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301907 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301908 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301909 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301910 priority = data.priority
1911 pricing_rule_list.push(data)
1912 }
1913 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301914
Faris Ansariab74ca72017-05-30 12:54:42 +05301915 var count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301916 $.each(priority_list, function (index, value) {
1917 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301918 count++
1919 }
1920 })
1921
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301922 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301923 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1924 'pricing_rule': pricing_rule_name
1925 })))
1926 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301927
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301928 return pricing_rule_list
1929 }
1930 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301931
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301932 validate_warehouse: function () {
1933 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 +05301934 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301935 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301936 },
1937
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301938 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301939 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301940
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301941 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301942 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301943 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301944 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301945 }
1946
1947 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301948 },
1949
1950 update_customer_in_localstorage: function() {
1951 var me = this;
1952 try {
1953 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
1954 } catch (e) {
1955 frappe.throw(__("LocalStorage is full , did not save"))
1956 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301957 }
Faris Ansari185762a2017-04-13 12:36:08 +05301958})