blob: 6b5803352625fc66d5ab49b7effea579f9f416c4 [file] [log] [blame]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301frappe.provide("erpnext.pos");
2{% include "erpnext/public/js/controllers/taxes_and_totals.js" %}
3
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05304frappe.pages['pos'].on_page_load = function (wrapper) {
Rushabh Mehta4c36d732015-01-01 15:59:34 +05305 var page = frappe.ui.make_app_page({
Rushabh Mehta72e17192014-08-08 15:30:49 +05306 parent: wrapper,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05307 title: __('Point of Sale'),
Rushabh Mehta72e17192014-08-08 15:30:49 +05308 single_column: true
9 });
Rohit Waghchauree0934d12016-05-11 15:04:57 +053010
Faris Ansaric70bbac2017-08-29 15:27:17 +053011 frappe.db.get_value('POS Settings', {name: 'POS Settings'}, 'is_online', (r) => {
Rohit Waghchaure1a779222017-09-11 11:49:25 +053012 if (r && r.use_pos_in_offline_mode && cint(r.use_pos_in_offline_mode)) {
Faris Ansaric70bbac2017-08-29 15:27:17 +053013 // offline
14 wrapper.pos = new erpnext.pos.PointOfSale(wrapper);
15 cur_pos = wrapper.pos;
16 } else {
17 // online
18 frappe.set_route('point-of-sale');
19 }
20 });
Rushabh Mehta72e17192014-08-08 15:30:49 +053021}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053022
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053023frappe.pages['pos'].refresh = function (wrapper) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053024 window.onbeforeunload = function () {
25 return wrapper.pos.beforeunload()
26 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +053027}
28
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053029erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053030 init: function (wrapper) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +053031 this.page_len = 20;
rohitwaghchaured2be55b2017-06-07 12:04:01 +053032 this.freeze = false;
Rohit Waghchauree0934d12016-05-11 15:04:57 +053033 this.page = wrapper.page;
34 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053035 this.set_indicator();
36 this.onload();
37 this.make_menu_list();
Rohit Waghchaure017f5002017-03-08 17:34:39 +053038 this.bind_events();
Rohit Waghchaure75177c02017-03-16 15:21:21 +053039 this.bind_items_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053040 this.si_docs = this.get_doc_from_localstorage();
41 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053042
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053043 beforeunload: function (e) {
44 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053045 e = e || window.event;
46
47 // For IE and Firefox prior to version 4
48 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053049 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 +053050 return
51 }
52
53 // For Safari
54 return __("You are in offline mode. You will not be able to reload until you have network.");
55 }
56 },
57
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053058 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053059 var me = this;
60 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053061 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053062 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053063 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053064 },
65
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053066 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053067 var me = this;
68 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053069 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053070 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053071 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053072 method: "frappe.handler.ping",
73 callback: function (r) {
74 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053075 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053076 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053077 }
78 }
79 })
80 },
81
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053082 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053083 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053084 this.get_data_from_server(function () {
rohitwaghchaured2be55b2017-06-07 12:04:01 +053085 me.make_control();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053086 me.create_new();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +053087 me.make();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053088 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053089 },
90
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053091 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053092 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +053093 this.page.clear_menu();
Faris Ansari769b6ba2017-05-16 07:31:10 +053094
95 // for mobile
96 this.page.add_menu_item(__("Pay"), function () {
97 me.validate();
98 me.update_paid_amount_status(true);
99 me.create_invoice();
100 me.make_payment();
101 }).addClass('visible-xs');
102
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530103 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530104 me.save_previous_entry();
105 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530106 })
107
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530108 this.page.add_menu_item(__("Sync Master Data"), function () {
109 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530110 me.load_data(false);
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530111 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530112 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530113 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530114 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530115
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530116 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
rohitwaghchaure88491712017-10-02 12:13:36 +0530117 me.freeze_screen = true;
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530118 me.sync_sales_invoice()
119 });
120
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530121 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530122 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530123 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530124 },
125
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530126 email_prompt: function() {
127 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530128 var fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530129 {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"},
130 {fieldtype: "Section Break"},
131 {label:__("Subject"), fieldtype:"Data", reqd: 1,
132 fieldname:"subject",length:524288},
133 {fieldtype: "Section Break"},
134 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
135 fieldname:"content"},
136 {fieldtype: "Section Break"},
137 {fieldtype: "Column Break"}];
138
139 this.email_dialog = new frappe.ui.Dialog({
140 title: "Email",
141 fields: fields,
142 primary_action_label: __("Send"),
143 primary_action: function() {
144 me.send_action();
145 }
146 });
147
148 this.email_dialog.show()
149 },
150
151 send_action: function() {
152 this.email_queue = this.get_email_queue()
153 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
154 this.update_email_queue()
155 this.email_dialog.hide()
156 },
157
158 update_email_queue: function () {
159 try {
160 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
161 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530162 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530163 }
164 },
165
166 get_email_queue: function () {
167 try {
168 return JSON.parse(localStorage.getItem('email_queue')) || {};
169 } catch (e) {
170 return {}
171 }
172 },
173
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530174 get_customers_details: function () {
175 try {
176 return JSON.parse(localStorage.getItem('customer_details')) || {};
177 } catch (e) {
178 return {}
179 }
180 },
181
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530182 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530183 var me = this;
184
185 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530186 if (doc_data) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530187 this.frm.doc = doc_data[0][this.frm.doc.offline_pos_name];
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530188 this.set_missing_values();
189 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530190 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530191 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530192 }
193 },
194
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530195 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530196 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530197 this.validate_list()
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530198 this.remove_doc_from_localstorage()
199 this.update_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530200 this.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530201 },
202
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530203 validate_list: function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530204 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530205 this.si_docs = this.get_submitted_invoice()
rohitwaghchauref688af32017-11-08 11:33:39 +0530206 $.each(this.removed_items, function(index, pos_name){
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530207 $.each(me.si_docs, function(key, data){
rohitwaghchauref688af32017-11-08 11:33:39 +0530208 if(me.si_docs[key][pos_name] && me.si_docs[key][pos_name].offline_pos_name == pos_name ){
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530209 frappe.throw(__("Submitted orders can not be deleted"))
210 }
211 })
212 })
213 },
214
215 toggle_delete_button: function () {
216 var me = this;
217 if(this.pos_profile_data["allow_delete"]) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530218 if (this.removed_items && this.removed_items.length > 0) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530219 $(this.page.wrapper).find('.btn-danger').show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530220 } else {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530221 $(this.page.wrapper).find('.btn-danger').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530222 }
223 }
224 },
225
226 get_doctype_status: function (doc) {
227 if (doc.docstatus == 0) {
228 return { status: "Draft", indicator: "red" }
229 } else if (doc.outstanding_amount == 0) {
230 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530231 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530232 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530233 }
234 },
235
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530236 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530237 var me = this;
238 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530239 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530240 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530241 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530242 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530243
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530244 this.set_customer_value_in_party_field();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530245
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530246 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530247 this.frm.doc.write_off_account = doc.write_off_account
248 }
249
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530250 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530251 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530252 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530253 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530254
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530255 set_customer_value_in_party_field: function() {
256 if (this.frm.doc.customer) {
257 this.party_field.$input.val(this.frm.doc.customer);
258 }
259 },
260
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530261 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530262 var me = this;
263 this.si_docs = this.get_doc_from_localstorage();
264
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530265 return $.grep(this.si_docs, function (data) {
266 for (key in data) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530267 return key == me.frm.doc.offline_pos_name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530268 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530269 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530270 },
271
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530272 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530273 var me = this;
274 frappe.call({
275 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
276 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530277 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530278 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530279 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530280 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530281 me.set_interval_for_si_sync();
282 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530283 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530284 callback();
285 }
286 }
287 })
288 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530289
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530290 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530291 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530292 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530293 this.meta = r.message.meta;
294 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530295 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530296 this.customers = r.message.customers;
297 this.serial_no_data = r.message.serial_no_data;
298 this.batch_no_data = r.message.batch_no_data;
299 this.tax_data = r.message.tax_data;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530300 this.contacts = r.message.contacts;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530301 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530302 this.price_list_data = r.message.price_list_data;
303 this.bin_data = r.message.bin_data;
304 this.pricing_rules = r.message.pricing_rules;
305 this.print_template = r.message.print_template;
306 this.pos_profile_data = r.message.pos_profile;
307 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530308 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530309 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 +0530310 },
311
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530312 save_previous_entry: function () {
313 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
314 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530315 }
316 },
317
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530318 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530319 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530320 this.frm = {}
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530321 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530322 this.setup();
Rohit Waghchaure61165122017-05-02 13:04:08 +0530323 this.set_default_customer()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530324 },
325
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530326 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530327 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530328
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530329 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530330 this.actual_qty_dict = {};
331
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530332 if (load_doc) {
333 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
rohitwaghchauref688af32017-11-08 11:33:39 +0530334 this.frm.doc.offline_pos_name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530335 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530336
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530337 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530338 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530339 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530340 })
341
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530342 this.print_template_data = frappe.render_template("print_template", {
343 content: this.print_template,
Faris Ansari29d64ca2017-05-23 17:12:51 +0530344 title: "POS",
345 base_url: frappe.urllib.get_base_url(),
346 print_css: frappe.boot.print_css,
347 print_settings: this.print_settings,
348 header: this.letter_head.header,
349 footer: this.letter_head.footer,
Makarand Bauskar724cc352017-05-23 17:43:42 +0530350 landscape: false,
351 columns: []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530352 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530353 },
354
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530355 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530356 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530357 this.party_field.$input.attr('disabled', false);
358 if(this.selected_row) {
359 this.selected_row.hide()
360 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530361 },
362
Rohit Waghchaure61165122017-05-02 13:04:08 +0530363 set_default_customer: function() {
364 if (this.default_customer && !this.frm.doc.customer) {
365 this.party_field.$input.val(this.default_customer);
366 this.frm.doc.customer = this.default_customer;
367 this.numeric_keypad.show();
368 this.toggle_list_customer(false)
369 this.toggle_item_cart(true)
370 }
371 },
372
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530373 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530374 var me = this;
375 this.party = party;
376 this.price_list = (party == "Customer" ?
377 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
378 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
379 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
380 },
381
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530382 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530383 this.make_item_list();
384 this.make_discount_field()
385 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200386
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530387 make_control: function() {
388 this.frm = {}
389 this.frm.doc = this.doc
390 this.set_transaction_defaults("Customer");
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530391 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 +0530392 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530393 this.make_search();
394 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530395 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530396 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530397 },
398
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530399 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530400 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530401 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530402 df: {
403 "fieldtype": "Data",
404 "label": "Item",
405 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530406 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530407 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530408 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530409 only_input: true,
410 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530411
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530412 this.serach_item.make_input();
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530413
Revant Nandgaonkar2f7cb822017-08-26 09:58:15 +0530414 this.serach_item.$input.on("keypress", function (event) {
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530415
416 clearTimeout(me.last_search_timeout);
417 me.last_search_timeout = setTimeout(() => {
418 if((me.serach_item.$input.val() != "") || (event.which == 13)) {
419 me.items = me.get_items();
420 me.make_item_list();
421 }
422 }, 400);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530423 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530424
Faris Ansari339d9c92017-03-07 18:14:06 +0530425 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530426 sorted_item_groups = this.get_sorted_item_groups()
427 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530428 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
429 }).join("");
430
431 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
432
433 this.search_item_group.on('click', '.dropdown-menu a', function() {
434 me.selected_item_group = $(this).attr('data-value');
435 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
436
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530437 me.page_len = 20;
438 me.items = me.get_items();
439 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530440 })
441
Faris Ansari45510102017-03-09 14:43:37 +0530442 me.toggle_more_btn();
443
444 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530445 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530446 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530447 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530448 me.toggle_more_btn();
449 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530450
Faris Ansari45510102017-03-09 14:43:37 +0530451 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530452 me.update_customer()
453 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530454 },
455
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530456 get_sorted_item_groups: function() {
457 list = {}
458 $.each(this.item_groups, function(i, data) {
459 list[i] = data[0]
460 })
461
462 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
463 },
464
Faris Ansari45510102017-03-09 14:43:37 +0530465 toggle_more_btn: function() {
466 if(!this.items || this.items.length <= this.page_len) {
467 this.wrapper.find(".btn-more").hide();
468 } else {
469 this.wrapper.find(".btn-more").show();
470 }
471 },
472
473 toggle_totals_area: function(show) {
474
Faris Ansari07c9f352017-03-09 17:03:14 +0530475 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530476 show = this.is_totals_area_collapsed;
477 }
478
479 var totals_area = this.wrapper.find('.totals-area');
480 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
481 .toggle(show);
482
483 if(show) {
484 totals_area.find('.collapse-btn i')
485 .removeClass('octicon-chevron-down')
486 .addClass('octicon-chevron-up');
487 } else {
488 totals_area.find('.collapse-btn i')
489 .removeClass('octicon-chevron-up')
490 .addClass('octicon-chevron-down');
491 }
492
493 this.is_totals_area_collapsed = !show;
494 },
495
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530496 make_list_customers: function () {
497 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530498 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530499 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530500 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530501 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530502 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530503 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530504
Faris Ansari45510102017-03-09 14:43:37 +0530505 me.render_list_customers();
506 me.toggle_totals_area(false);
507
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530508 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530509 $(this).toggleClass("view_customer");
510 if($(this).hasClass("view_customer")) {
511 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530512 me.list_customers.show();
513 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530514 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530515 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530516 } else {
517 if(me.frm.doc.docstatus == 0) {
518 me.party_field.$input.attr('disabled', false);
519 }
520 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530521 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530522 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530523 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530524 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530525 }
526 });
527 this.add_customer_btn.on('click', function() {
528 me.save_previous_entry();
529 me.create_new();
530 me.refresh();
531 me.set_focus();
532 });
Faris Ansari45510102017-03-09 14:43:37 +0530533 this.pos_bill.on('click', '.collapse-btn', function() {
534 me.toggle_totals_area();
535 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530536 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200537
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530538 bind_numeric_keypad: function() {
539 var me = this;
540 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
541 me.numeric_val = '';
542 })
543
544 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
545 me.numeric_id = $(this).attr("id") || me.numeric_id;
546 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530547 if(me.numeric_id) {
548 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
549 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530550
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530551 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530552 me.numeric_val += me.val;
553 me.selected_field.val(flt(me.numeric_val))
554 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530555 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530556 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530557
558 if(me.numeric_id && $(this).hasClass('pos-operation')) {
559 me.numeric_keypad.find('button.pos-operation').removeClass('active');
560 $(this).addClass('active');
561
562 me.selected_row.find('.pos-list-row').removeClass('active');
563 me.selected_field.closest('.pos-list-row').addClass('active');
564 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530565 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200566
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530567 $(this.numeric_keypad).find('.numeric-del').click(function(){
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530568 if(me.numeric_id) {
569 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
570 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
571 me.selected_field.val(me.numeric_val);
572 me.selected_field.trigger("change")
573 } else {
574 //Remove an item from the cart, if focus is at selected item
575 me.remove_selected_item()
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('.pos-pay').click(function(){
580 me.validate();
581 me.update_paid_amount_status(true);
582 me.create_invoice();
583 me.make_payment();
584 })
585 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530586
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530587 remove_selected_item: function() {
588 this.remove_item = []
589 idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx")
590 this.remove_item.push(idx)
591 this.remove_zero_qty_item()
592 this.update_paid_amount_status(false)
593 },
594
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530595 render_list_customers: function () {
596 var me = this;
597
598 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530599 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530600 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530601 if (!this.si_docs.length) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530602 this.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530603 return;
604 }
Faris Ansari45510102017-03-09 14:43:37 +0530605
606 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530607 if(this.si_docs.length) {
608 this.si_docs.forEach(function (data, i) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530609 for (var key in data) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530610 html += frappe.render_template("pos_invoice_list", {
611 sr: i + 1,
612 name: key,
613 customer: data[key].customer,
614 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
615 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
616 data: me.get_doctype_status(data[key])
617 });
618 }
619 });
620 }
Faris Ansari45510102017-03-09 14:43:37 +0530621 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530622
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530623 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530624 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530625 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530626 me.list_customers_btn.toggleClass("view_customer");
627 me.pos_bill.show();
628 me.list_customers_btn.show();
rohitwaghchauref688af32017-11-08 11:33:39 +0530629 me.frm.doc.offline_pos_name = $(this).parents().attr('invoice-name')
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530630 me.edit_record();
631 })
632
633 //actions
634 $(this.wrapper).find('.list-select-all').click(function () {
635 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
636 me.removed_items = [];
637 if ($(this).is(":checked")) {
638 $.each(me.si_docs, function (index, data) {
639 for (key in data) {
640 me.removed_items.push(key)
641 }
642 });
643 }
644
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530645 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530646 });
647
648 $(this.wrapper).find('.list-delete').click(function () {
rohitwaghchauref688af32017-11-08 11:33:39 +0530649 me.frm.doc.offline_pos_name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530650 if ($(this).is(":checked")) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530651 me.removed_items.push(me.frm.doc.offline_pos_name);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530652 } else {
rohitwaghchauref688af32017-11-08 11:33:39 +0530653 me.removed_items.pop(me.frm.doc.offline_pos_name)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530654 }
655
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530656 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530657 });
658 },
659
660 bind_delete_event: function() {
661 var me = this;
662
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530663 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530664 frappe.confirm(__("Delete permanently?"), function () {
665 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530666 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530667 me.render_list_customers();
668 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530669 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530670 },
671
672 set_focus: function () {
673 if (this.default_customer || this.frm.doc.customer) {
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530674 this.set_customer_value_in_party_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530675 this.serach_item.$input.focus();
676 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530677 this.party_field.$input.focus();
678 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530679 },
680
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530681 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530682 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530683
684 if(!this.party_field) {
685 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
686 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530687 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530688 })).insertAfter(this.page.$title_area.hide());
689 }
690
691 this.party_field = frappe.ui.form.make_control({
692 df: {
693 "fieldtype": "Data",
694 "options": this.party,
695 "label": this.party,
696 "fieldname": this.party.toLowerCase(),
697 "placeholder": __("Select or add new customer")
698 },
699 parent: this.page.wrapper.find(".party-area"),
700 only_input: true,
701 });
702
703 this.party_field.make_input();
704 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530705 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530706 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530707
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530708 this.party_field.awesomeplete =
709 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530710 minChars: 0,
711 maxItems: 99,
712 autoFirst: true,
713 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530714 filter: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530715 if (item.value.includes('is_action')) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530716 return true;
717 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530718
719 input = input.toLowerCase();
720 item = this.get_item(item.value);
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530721 result = item ? item.searchtext.includes(input) : '';
722 if(!result) {
723 me.prepare_customer_mapper(input);
724 } else {
725 return result;
726 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530727 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530728 item: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530729 var d = this.get_item(item.value);
Faris Ansari45510102017-03-09 14:43:37 +0530730 var html = "<span>" + __(d.label || d.value) + "</span>";
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530731 if(d.customer_name) {
732 html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>';
733 }
734
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530735 return $('<li></li>')
736 .data('item.autocomplete', d)
737 .html('<a><p>' + html + '</p></a>')
738 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530739 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530740 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530741
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530742 this.prepare_customer_mapper()
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530743 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530744
745 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530746 .on('input', function (e) {
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530747 if(me.customers_mapper.length <= 1) {
748 me.prepare_customer_mapper(e.target.value);
749 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530750 me.party_field.awesomeplete.list = me.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530751 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530752 .on('awesomplete-select', function (e) {
753 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530754 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530755 if (!customer) return;
756 // create customer link
757 if (customer.action) {
758 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530759 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530760 }
Faris Ansari45510102017-03-09 14:43:37 +0530761 me.toggle_list_customer(false);
762 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530763 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530764 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530765 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530766 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530767 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530768 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530769 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530770 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530771
772 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530773 me.toggle_list_customer(false)
774 me.toggle_item_cart(true)
775 } else {
776 me.toggle_list_customer(true)
777 me.toggle_item_cart(false)
778 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530779 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530780 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530781 var item = me.party_field.awesomeplete
782 .get_item(e.originalEvent.text.value);
783 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530784 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530785 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530786 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530787 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530788 },
789
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530790 prepare_customer_mapper: function(key) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530791 var me = this;
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530792 var customer_data = '';
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530793
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530794 if (key) {
795 key = key.toLowerCase().trim();
796 var re = new RegExp('%', 'g');
797 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'));
798
799 customer_data = $.grep(this.customers, function(data) {
800 contact = me.contacts[data.name];
801 if(reg.test(data.name.toLowerCase())
802 || reg.test(data.customer_name.toLowerCase())
803 || (contact && reg.test(contact["mobile_no"]))
804 || (contact && reg.test(contact["phone"]))
805 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
806 return data;
807 }
808 })
809 } else {
810 customer_data = this.customers;
811 }
812
813 this.customers_mapper = [];
814
815 customer_data.forEach(function (c, index) {
816 if(index < 30) {
817 contact = me.contacts[c.name];
818 if(contact && !c['phone']) {
819 c["phone"] = contact["phone"];
820 c["email_id"] = contact["email_id"];
821 c["mobile_no"] = contact["mobile_no"];
822 }
823
824 me.customers_mapper.push({
825 label: c.name,
826 value: c.name,
827 customer_name: c.customer_name,
828 customer_group: c.customer_group,
829 territory: c.territory,
830 phone: contact ? contact["phone"] : '',
831 mobile_no: contact ? contact["mobile_no"] : '',
832 email_id: contact ? contact["email_id"] : '',
833 searchtext: ['customer_name', 'customer_group', 'name', 'value',
834 'label', 'email_id', 'phone', 'mobile_no']
835 .map(key => c[key]).join(' ')
836 .toLowerCase()
837 });
838 } else {
839 return;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530840 }
841 });
842
843 this.customers_mapper.push({
844 label: "<span class='text-primary link-option'>"
845 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
846 + __("Create a new Customer")
847 + "</span>",
848 value: 'is_action',
849 action: me.add_customer
850 });
851 },
852
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530853 autocomplete_customers: function() {
854 this.party_field.awesomeplete.list = this.customers_mapper;
855 },
856
Faris Ansari45510102017-03-09 14:43:37 +0530857 toggle_edit_button: function(flag) {
858 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
859 },
860
861 toggle_list_customer: function(flag) {
862 this.list_customers.toggle(flag);
863 },
864
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530865 toggle_item_cart: function(flag) {
866 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
867 },
868
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530869 add_customer: function() {
870 this.frm.doc.customer = "";
rohitwaghchaure09483d32017-05-16 08:51:24 +0530871 this.update_customer(true);
872 this.numeric_keypad.show();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530873 },
874
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530875 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530876 var me = this;
Rushabh Mehta37146262017-02-01 18:17:35 +0530877
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530878 this.customer_doc = new frappe.ui.Dialog({
879 'title': 'Customer',
880 fields: [
881 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530882 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530883 "fieldname": "full_name",
884 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530885 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530886 },
887 {
888 "fieldtype": "Section Break"
889 },
890 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530891 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530892 "fieldname": "email_id",
893 "fieldtype": "Data"
894 },
895 {
896 "fieldtype": "Column Break"
897 },
898 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530899 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530900 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530901 "fieldtype": "Data"
902 },
903 {
904 "fieldtype": "Section Break"
905 },
906 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530907 "label": __("Address Name"),
908 "read_only": 1,
909 "fieldname": "name",
910 "fieldtype": "Data"
911 },
912 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530913 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530914 "fieldname": "address_line1",
915 "fieldtype": "Data"
916 },
917 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530918 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530919 "fieldname": "address_line2",
920 "fieldtype": "Data"
921 },
922 {
923 "fieldtype": "Column Break"
924 },
925 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530926 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530927 "fieldname": "city",
928 "fieldtype": "Data"
929 },
930 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530931 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530932 "fieldname": "state",
933 "fieldtype": "Data"
934 },
935 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530936 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530937 "fieldname": "pincode",
938 "fieldtype": "Data"
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530939 },
940 {
941 "label": __("Customer POS Id"),
942 "fieldname": "customer_pos_id",
943 "fieldtype": "Data",
944 "hidden": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530945 }
946 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530947 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530948 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530949 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530950
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530951 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530952 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530953 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530954 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530955 });
956 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530957
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530958 render_address_data: function() {
959 var me = this;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530960 this.address_data = this.address[this.frm.doc.customer] || {};
961 if(!this.address_data.email_id || !this.address_data.phone) {
962 this.address_data = this.contacts[this.frm.doc.customer];
963 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530964
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530965 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530966 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
967 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530968 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530969
970 if(!this.customer_doc.fields_dict.customer_pos_id.value) {
971 this.customer_doc.set_value("customer_pos_id", $.now())
972 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530973 },
974
975 get_address_from_localstorage: function() {
976 this.address_details = this.get_customers_details()
977 return this.address_details[this.frm.doc.customer]
978 },
979
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530980 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530981 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530982 this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530983 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530984 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530985 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530986 this.update_address_and_customer_list(new_customer)
987 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530988 this.update_customer_in_localstorage()
989 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530990 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530991 },
992
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530993 update_address_and_customer_list: function(new_customer) {
994 var me = this;
995 if(new_customer) {
996 this.customers_mapper.push({
997 label: this.frm.doc.customer,
998 value: this.frm.doc.customer,
999 customer_group: "",
1000 territory: ""
1001 });
1002 }
1003
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301004 this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details())
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301005 },
1006
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301007 get_prompt_details: function() {
1008 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301009 this.prompt_details['country'] = this.pos_profile_data.country;
rohitwaghchauree2176b82017-07-28 20:52:02 +05301010 this.prompt_details['territory'] = this.pos_profile_data["territory"];
1011 this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"];
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301012 this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301013 return JSON.stringify(this.prompt_details)
1014 },
1015
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301016 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301017 var me = this;
1018 this.frm.doc.customer = doc.label || doc.name;
1019 this.frm.doc.customer_name = doc.customer_name;
1020 this.frm.doc.customer_group = doc.customer_group;
1021 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301022 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301023 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301024 },
1025
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301026 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301027 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301028 if (!this.price_list) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301029 frappe.msgprint(__("Price List not found or disabled"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301030 return;
1031 }
1032
1033 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301034
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301035 var $wrap = me.wrapper.find(".item-list");
1036 me.wrapper.find(".item-list").empty();
1037
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301038 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301039 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301040 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301041 $(frappe.render_template("pos_item", {
1042 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301043 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301044 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +05301045 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +05301046 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301047 color: frappe.get_palette(obj.item_name),
1048 abbr: frappe.get_abbr(obj.item_name)
1049 })).tooltip().appendTo($wrap);
1050 }
1051 });
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001052
Faris Ansari45510102017-03-09 14:43:37 +05301053 $wrap.append(`
1054 <div class="image-view-item btn-more text-muted text-center">
1055 <div class="image-view-body">
1056 <i class="mega-octicon octicon-package"></i>
1057 <div>Load more items</div>
1058 </div>
1059 </div>
1060 `);
1061
1062 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301063 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +05301064 $("<p class='text-muted small' style='padding-left: 10px'>"
1065 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301066 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301067
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301068 if (this.items.length == 1
1069 && this.serach_item.$input.val()) {
1070 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301071 this.add_to_cart();
1072 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301073 },
1074
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301075 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301076 // To search item as per the key enter
1077
1078 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301079 this.item_serial_no = {};
1080 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301081
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301082 if (item_code) {
1083 return $.grep(this.item_data, function (item) {
1084 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301085 return true
1086 }
1087 })
1088 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301089
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301090 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301091
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301092 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301093 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301094 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301095 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301096
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301097 if (key) {
1098 return $.grep(this.items_list, function (item) {
1099 if (search_status) {
1100 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301101 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301102 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
1103 } else if (me.serial_no_data[item.item_code]
1104 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301105 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301106 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 +05301107 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301108 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301109 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301110 return item.barcode == me.serach_item.$input.val();
rohitwaghchaure860144f2017-07-12 15:01:56 +05301111 } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) ||
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301112 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301113 return true
1114 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301115 }
1116 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301117 } else {
1118 return this.items_list;
1119 }
1120 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301121
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301122 apply_category: function() {
1123 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301124 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301125
1126 if(category == 'All Item Groups') {
1127 return this.item_data
1128 } else {
1129 return this.item_data.filter(function(element, index, array){
1130 return element.item_group == category;
1131 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301132 }
1133 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001134
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301135 bind_items_event: function() {
1136 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301137 $(this.wrapper).on('click', '.pos-bill-item', function() {
1138 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1139 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301140 me.numeric_val = "";
1141 me.numeric_id = ""
1142 me.item_code = $(this).attr("data-item-code");
1143 me.render_selected_item()
1144 me.bind_qty_event()
1145 me.update_rate()
1146 $(me.wrapper).find(".selected-item").scrollTop(1000);
1147 })
1148 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301149
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301150 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301151 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301152
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301153 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301154 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301155 var qty = $(this).val();
1156 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301157 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301158 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301159
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301160 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301161 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1162 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301163 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301164 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301165
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301166 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301167 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1168 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301169 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301170 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001171
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301172 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301173 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301174 var discount = $(this).val();
1175 me.update_discount(item_code, discount)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301176 me.update_value()
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301177 })
1178 },
1179
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301180 bind_events: function() {
1181 var me = this;
1182 // if form is local then allow this function
1183 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1184 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301185 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301186 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301187 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301188
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301189 if (me.frm.doc.docstatus == 0) {
1190 me.items = me.get_items($(this).attr("data-item-code"))
1191 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301192 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301193 }
1194 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301195
1196 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301197 },
1198
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301199 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301200 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301201 this.items = this.get_items(item_code);
1202 this.validate_serial_no()
1203 this.set_item_details(item_code, "qty", qty);
1204 },
1205
1206 update_discount: function(item_code, discount) {
1207 var me = this;
1208 this.items = this.get_items(item_code);
1209 this.set_item_details(item_code, "discount_percentage", discount);
1210 },
1211
1212 update_rate: function () {
1213 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301214 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301215 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301216 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301217 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301218 })
1219 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301220
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301221 update_value: function() {
1222 var me = this;
1223 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1224 "rate": ".pos-item-price", "amount": ".pos-amount"}
1225 this.child_doc = this.get_child_item(this.item_code);
1226
1227 if(me.child_doc.length) {
1228 $.each(fields, function(key, field) {
1229 $(me.selected_row).find(field).val(me.child_doc[0][key])
1230 })
1231 } else {
1232 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301233 }
1234 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301235
1236 clear_selected_row: function() {
1237 $(this.wrapper).find('.selected-item').empty();
1238 },
1239
1240 render_selected_item: function() {
1241 this.child_doc = this.get_child_item(this.item_code);
1242 $(this.wrapper).find('.selected-item').empty();
1243 if(this.child_doc.length) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301244 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 +05301245 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1246 $(this.wrapper).find('.selected-item').html(this.selected_row)
1247 }
1248
1249 $(this.selected_row).find('.form-control').click(function(){
1250 $(this).select();
1251 })
1252 },
1253
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301254 get_child_item: function(item_code) {
1255 var me = this;
1256 return $.map(me.frm.doc.items, function(doc){
1257 if(doc.item_code == item_code) {
1258 return doc
1259 }
1260 })
1261 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301262
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301263 set_item_details: function (item_code, field, value) {
1264 var me = this;
1265 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301266 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301267 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301268
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301269 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301270 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301271 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301272 if (d.serial_no && field == 'qty') {
1273 me.validate_serial_no_qty(d, item_code, field, value)
1274 }
1275
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301276 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301277 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301278 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301279 me.remove_item.push(d.idx)
1280 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301281
1282 if(field=="discount_percentage" && value == 0) {
1283 d.rate = d.price_list_rate;
1284 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301285 }
1286 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301287
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301288 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301289 this.remove_zero_qty_item();
1290 }
1291
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301292 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301293 },
1294
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301295 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301296 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301297 var idx = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301298 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301299 $.each(this.frm.doc["items"] || [], function (i, d) {
1300 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301301 d.idx = idx;
1302 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301303 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301304 }
1305 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301306
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301307 this.frm.doc["items"] = this.items;
1308 },
1309
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301310 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301311 var me = this;
1312
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301313 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301314 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301315
1316 if(me.frm.doc.additional_discount_percentage && me.frm.doc.discount_amount) {
1317 // Reset discount amount
1318 me.frm.doc.discount_amount = 0;
1319 }
1320
Faris Ansariab74ca72017-05-30 12:54:42 +05301321 var total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301322
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301323 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301324 total = me.frm.doc.net_total
1325 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301326
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301327 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 +05301328 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301329 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301330 });
1331
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301332 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301333 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1334 me.frm.doc.additional_discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301335 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301336 me.wrapper.find('input.discount-percentage').val(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301337 });
1338 },
1339
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301340 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301341 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301342 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301343 frappe.throw(__("Please select customer"))
1344 }
1345 },
1346
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301347 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301348 var me = this;
1349 var caught = false;
1350 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1351
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301352 this.customer_validate();
1353 this.mandatory_batch_no();
1354 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301355 this.validate_warehouse();
1356
1357 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301358 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301359 if (d.item_code == me.items[0].item_code) {
1360 caught = true;
1361 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301362 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301363 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301364 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1365 d.warehouse = me.item_serial_no[d.item_code][1]
1366 }
1367
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301368 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301369 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301370 }
1371 }
1372 });
1373 }
1374
1375 // if item not found then add new item
1376 if (!caught)
1377 this.add_new_item_to_grid();
1378
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301379 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301380 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301381 },
1382
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301383 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301384 var me = this;
1385 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1386 this.child.item_code = this.items[0].item_code;
1387 this.child.item_name = this.items[0].item_name;
1388 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301389 this.child.brand = this.items[0].brand;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301390 this.child.description = this.items[0].description || this.items[0].item_name;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301391 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301392 this.child.qty = 1;
1393 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301394 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1395 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301396 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301397 ? 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 +05301398 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1399 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1400 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301401 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301402 this.child.batch_no = this.item_batch_no[this.child.item_code];
1403 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1404 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301405 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301406 },
1407
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301408 update_paid_amount_status: function (update_paid_amount) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301409 if (this.frm.doc.offline_pos_name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301410 update_paid_amount = update_paid_amount ? false : true;
1411 }
1412
1413 this.refresh(update_paid_amount);
1414 },
1415
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301416 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301417 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301418 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301419 this.set_primary_action();
1420 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301421
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301422 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301423 this.apply_pricing_rule();
1424 this.discount_amount_applied = false;
1425 this._calculate_taxes_and_totals();
1426 this.calculate_discount_amount();
1427 this.show_items_in_item_cart();
1428 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301429 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301430 this.set_totals();
1431 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301432
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301433 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301434 return erpnext.get_currency(this.frm.doc.company);
1435 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301436
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301437 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301438 var me = this;
1439 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301440 var $no_items_message = this.wrapper.find(".no-items-message");
1441 $no_items_message.toggle(this.frm.doc.items.length === 0);
1442
1443 var $totals_area = this.wrapper.find('.totals-area');
1444 $totals_area.toggle(this.frm.doc.items.length > 0);
1445
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301446 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301447 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301448 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301449 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301450 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301451 discount_percentage: d.discount_percentage || 0.0,
1452 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301453 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301454 rate: format_currency(d.rate, me.frm.doc.currency),
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301455 amount: format_currency(d.amount, me.frm.doc.currency),
1456 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301457 })).appendTo($items);
1458 });
1459
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301460 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301461 $(this).select();
1462 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301463
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301464 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301465 $(this).select();
1466 });
1467
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301468 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301469 $(this).select();
1470 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301471 },
1472
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301473 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301474 var me = this;
1475 me.frm.doc.total_taxes_and_charges = 0.0
1476
1477 var taxes = this.frm.doc.taxes || [];
1478 $(this.wrapper)
1479 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1480 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301481
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301482 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301483 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1484 $(frappe.render_template("pos_tax_row", {
1485 description: d.description,
1486 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1487 me.frm.doc.currency)
1488 })).appendTo(me.wrapper.find(".tax-table"));
1489 }
1490 });
1491 },
1492
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301493 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301494 var me = this;
1495 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1496 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301497 this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage);
1498 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301499 },
1500
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301501 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301502 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301503 this.page.set_primary_action(__("New Cart"), function () {
1504 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301505 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301506 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301507
1508 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301509 this.page.set_secondary_action(__("Print"), function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301510 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301511 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301512 })
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301513 this.page.add_menu_item(__("Email"), function () {
1514 me.email_prompt()
1515 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301516 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301517 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301518
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301519 make_new_cart: function (){
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301520 this.item_code = '';
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301521 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301522 this.save_previous_entry();
1523 this.create_new();
1524 this.refresh();
1525 this.toggle_input_field();
1526 this.render_list_customers();
1527 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301528 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301529
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301530 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301531 var me = this;
1532
mbauskar66e2a512017-06-22 16:18:12 +05301533 this.msgprint = frappe.msgprint(
Faris Ansariab74ca72017-05-30 12:54:42 +05301534 `<a class="btn btn-primary print_doc"
1535 style="margin-right: 5px;">${__('Print')}</a>
1536 <a class="btn btn-default new_doc">${__('New')}</a>`);
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301537
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301538 $('.print_doc').click(function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301539 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301540 me.print_document(html)
1541 })
1542
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301543 $('.new_doc').click(function () {
mbauskar66e2a512017-06-22 16:18:12 +05301544 me.msgprint.hide()
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301545 me.make_new_cart()
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301546 })
1547 },
1548
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301549 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301550 var w = window.open();
1551 w.document.write(html);
1552 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301553 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301554 w.print();
1555 w.close();
1556 }, 1000)
1557 },
1558
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301559 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301560 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301561 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301562 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301563 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301564 this.print_dialog()
1565 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301566 },
1567
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301568 update_serial_no: function() {
1569 var me = this;
1570
1571 //Remove the sold serial no from the cache
1572 $.each(this.frm.doc.items, function(index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301573 var sn = data.serial_no.split('\n')
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301574 if(sn.length) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301575 var serial_no_list = me.serial_no_data[data.item_code]
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301576 if(serial_no_list) {
1577 $.each(sn, function(i, serial_no) {
1578 if(in_list(Object.keys(serial_no_list), serial_no)) {
1579 delete serial_no_list[serial_no]
1580 }
1581 })
1582 me.serial_no_data[data.item_code] = serial_no_list;
1583 }
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301584 }
1585 })
1586 },
1587
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301588 change_status: function () {
1589 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301590 this.frm.doc.docstatus = 1;
1591 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301592 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301593 }
1594 },
1595
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301596 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301597 var pointer_events = 'inherit'
Faris Ansariab74ca72017-05-30 12:54:42 +05301598 var disabled = this.frm.doc.docstatus == 1 ? true: false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301599 $(this.wrapper).find('input').attr("disabled", disabled);
1600 $(this.wrapper).find('select').attr("disabled", disabled);
1601 $(this.wrapper).find('input').attr("disabled", disabled);
1602 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301603 $(this.wrapper).find('button').attr("disabled", disabled);
1604 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301605
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301606 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301607 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301608 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301609
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301610 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301611 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1612 this.set_primary_action();
1613 },
1614
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301615 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301616 var me = this;
rohitwaghchauref688af32017-11-08 11:33:39 +05301617 var invoice_data = {};
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301618 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchauref688af32017-11-08 11:33:39 +05301619 if (this.frm.doc.offline_pos_name) {
1620 this.update_invoice();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301621 } else {
rohitwaghchauref688af32017-11-08 11:33:39 +05301622 this.frm.doc.offline_pos_name = $.now();
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301623 this.frm.doc.posting_date = frappe.datetime.get_today();
1624 this.frm.doc.posting_time = frappe.datetime.now_time();
Charles-Henri Decultot2305eda2017-06-19 05:55:54 +02001625 this.frm.doc.pos_profile = this.pos_profile_data['name'];
rohitwaghchauref688af32017-11-08 11:33:39 +05301626 invoice_data[this.frm.doc.offline_pos_name] = this.frm.doc;
1627 this.si_docs.push(invoice_data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301628 this.update_localstorage();
1629 this.set_primary_action();
1630 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301631 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301632 },
1633
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301634 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301635 var me = this;
1636 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301637 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301638 for (var key in data) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301639 if (key == me.frm.doc.offline_pos_name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301640 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301641 me.update_localstorage();
1642 }
1643 }
rohitwaghchauref688af32017-11-08 11:33:39 +05301644 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301645 },
1646
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301647 update_localstorage: function () {
1648 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301649 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301650 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301651 frappe.throw(__("LocalStorage is full , did not save"))
1652 }
1653 },
1654
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301655 get_doc_from_localstorage: function () {
1656 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301657 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301658 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301659 return []
1660 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301661 },
1662
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301663 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301664 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301665 setInterval(function () {
rohitwaghchaure88491712017-10-02 12:13:36 +05301666 me.freeze_screen = false;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301667 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301668 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301669 },
1670
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301671 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301672 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301673 this.si_docs = this.get_submitted_invoice() || [];
1674 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301675 this.customers_list = this.get_customers_details() || {};
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301676 if(this.customer_doc) {
1677 this.freeze = this.customer_doc.display
1678 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301679
rohitwaghchaure88491712017-10-02 12:13:36 +05301680 freeze_screen = this.freeze_screen || false;
1681
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301682 if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301683 this.freeze = true;
1684
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301685 frappe.call({
1686 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
rohitwaghchaure88491712017-10-02 12:13:36 +05301687 freeze: freeze_screen,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301688 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301689 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301690 email_queue_list: me.email_queue_list,
1691 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301692 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301693 callback: function (r) {
1694 if (r.message) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301695 me.freeze = false;
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301696 me.customers = r.message.synced_customers_list;
1697 me.address = r.message.synced_address;
1698 me.contacts = r.message.synced_contacts;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301699 me.removed_items = r.message.invoice;
rohitwaghchauref688af32017-11-08 11:33:39 +05301700 me.removed_email = r.message.email_queue;
1701 me.removed_customers = r.message.customers;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301702 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301703 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301704 me.remove_customer_from_localstorage();
rohitwaghchauref688af32017-11-08 11:33:39 +05301705 me.prepare_customer_mapper();
1706 me.autocomplete_customers();
1707 me.render_list_customers();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301708 }
1709 }
1710 })
1711 }
1712 },
1713
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301714 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301715 var invoices = [];
1716 var index = 1;
Faris Ansariab74ca72017-05-30 12:54:42 +05301717 var docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301718 if (docs) {
1719 invoices = $.map(docs, function (data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301720 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301721 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301722 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301723 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301724 return data
1725 }
1726 }
1727 });
1728 }
1729
1730 return invoices
1731 },
1732
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301733 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301734 var me = this;
1735 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301736 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301737 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301738 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301739 for (var key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301740 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301741 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301742 }
1743 }
1744 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301745 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301746 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301747 this.update_localstorage();
1748 }
1749 },
1750
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301751 remove_email_queue_from_localstorage: function() {
1752 var me = this;
1753 this.email_queue = this.get_email_queue()
1754 if (this.removed_email) {
1755 $.each(this.email_queue_list, function (index, data) {
1756 if (in_list(me.removed_email, index)) {
1757 delete me.email_queue[index]
1758 }
1759 })
1760 this.update_email_queue();
1761 }
1762 },
1763
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301764 remove_customer_from_localstorage: function() {
1765 var me = this;
1766 this.customer_details = this.get_customers_details()
1767 if (this.removed_customers) {
1768 $.each(this.customers_list, function (index, data) {
1769 if (in_list(me.removed_customers, index)) {
1770 delete me.customer_details[index]
1771 }
1772 })
1773 this.update_customer_in_localstorage();
1774 }
1775 },
1776
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301777 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301778 var me = this;
1779 this.customer_validate();
1780 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301781 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301782 },
1783
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301784 item_validate: function () {
1785 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301786 frappe.throw(__("Select items to save the invoice"))
1787 }
1788 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301789
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301790 validate_mode_of_payments: function () {
1791 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301792 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1793 }
1794 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301795
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301796 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301797 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301798 var item_code = ''
1799 var serial_no = '';
1800 for (var key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301801 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301802 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301803 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301804
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301805 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301806 this.refresh();
1807 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1808 'item': this.items[0].item_code
1809 })))
1810 }
1811
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301812 if (item_code && serial_no) {
1813 $.each(this.frm.doc.items, function (index, data) {
1814 if (data.item_code == item_code) {
1815 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301816 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1817 'serial_no': serial_no
1818 })))
1819 }
1820 }
1821 })
1822 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301823 },
1824
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301825 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301826 var me = this;
1827 if (args.item_code == item_code && args.serial_no
1828 && field == 'qty' && cint(value) != value) {
1829 args.qty = 0.0;
1830 this.refresh();
1831 frappe.throw(__("Serial no item cannot be a fraction"))
1832 }
1833
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301834 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301835 args.qty = 0.0;
1836 args.serial_no = ''
1837 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301838 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1839 'item': item_code
1840 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301841 }
1842 },
1843
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301844 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301845 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301846 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301847 frappe.prompt([{
1848 'fieldname': 'batch',
1849 'fieldtype': 'Select',
1850 'label': __('Batch No'),
1851 'reqd': 1,
1852 'options': this.batch_no_data[this.items[0].item_code]
1853 }],
1854 function(values){
1855 me.item_batch_no[me.items[0].item_code] = values.batch;
1856 },
1857 __('Select Batch No'))
1858 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301859 },
1860
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301861 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301862 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301863 $.each(this.frm.doc["items"], function (n, item) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301864 var pricing_rule = me.get_pricing_rule(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301865 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301866 if (pricing_rule.length) {
1867 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301868 item.margin_type = pricing_rule[0].margin_type;
1869 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1870 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1871 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301872 me.apply_pricing_rule_on_item(item)
1873 } else if (item.pricing_rule) {
1874 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301875 item.margin_rate_or_amount = 0.0;
1876 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301877 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301878 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301879 }
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001880
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301881 if(item.discount_percentage > 0) {
1882 me.apply_pricing_rule_on_item(item)
1883 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301884 })
1885 },
1886
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301887 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301888 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301889 return $.grep(this.pricing_rules, function (data) {
1890 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301891 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301892 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301893 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301894 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301895 return true
1896 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301897 }
1898 }
1899 })
1900 },
1901
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301902 validate_item_condition: function (data, item) {
1903 var apply_on = frappe.model.scrub(data.apply_on);
1904
1905 return (data.apply_on == 'Item Group')
1906 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
1907 },
1908
1909 validate_item_group: function (pr_item_group, cart_item_group) {
1910 //pr_item_group = pricing rule's item group
1911 //cart_item_group = cart item's item group
1912 //this.item_groups has information about item group's lft and rgt
1913 //for example: {'Foods': [12, 19]}
1914
1915 pr_item_group = this.item_groups[pr_item_group]
1916 cart_item_group = this.item_groups[cart_item_group]
1917
1918 return (cart_item_group[0] >= pr_item_group[0] &&
1919 cart_item_group[1] <= pr_item_group[1])
1920 },
1921
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301922 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301923 //This method check condition based on applicable for
Faris Ansariab74ca72017-05-30 12:54:42 +05301924 var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301925 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301926 return true
1927 }
1928 },
1929
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301930 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301931 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301932 'Customer': [data.customer, [this.frm.doc.customer]],
1933 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1934 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301935 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301936 }
1937 },
1938
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301939 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301940 //This method validate duplicate pricing rule
1941 var pricing_rule_name = '';
1942 var priority = 0;
1943 var pricing_rule_list = [];
1944 var priority_list = []
1945
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301946 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301947
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301948 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301949 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301950 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301951 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301952 priority = data.priority
1953 pricing_rule_list.push(data)
1954 }
1955 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301956
Faris Ansariab74ca72017-05-30 12:54:42 +05301957 var count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301958 $.each(priority_list, function (index, value) {
1959 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301960 count++
1961 }
1962 })
1963
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301964 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301965 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1966 'pricing_rule': pricing_rule_name
1967 })))
1968 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301969
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301970 return pricing_rule_list
1971 }
1972 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301973
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301974 validate_warehouse: function () {
1975 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 +05301976 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301977 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301978 },
1979
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301980 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301981 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301982
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301983 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301984 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301985 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301986 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301987 }
1988
1989 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301990 },
1991
1992 update_customer_in_localstorage: function() {
1993 var me = this;
1994 try {
1995 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
1996 } catch (e) {
1997 frappe.throw(__("LocalStorage is full , did not save"))
1998 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301999 }
Faris Ansari185762a2017-04-13 12:36:08 +05302000})