blob: fa88481652676a710f4cfc1f70b607a0202c8ac1 [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
Rushabh Mehta06fa46f2015-01-29 18:09:11 +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
11 wrapper.pos = new erpnext.pos.PointOfSale(wrapper)
Rushabh Mehta72e17192014-08-08 15:30:49 +053012}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053013
Rohit Waghchauree0934d12016-05-11 15:04:57 +053014frappe.pages['pos'].refresh = function(wrapper) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053015 window.onbeforeunload = function () {
16 return wrapper.pos.beforeunload()
17 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +053018 wrapper.pos.on_refresh_page()
19}
20
21
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053022erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
Rohit Waghchauree0934d12016-05-11 15:04:57 +053023 init: function(wrapper){
24 this.load = true;
25 this.page = wrapper.page;
26 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053027 this.set_indicator();
28 this.onload();
29 this.make_menu_list();
30 this.set_interval_for_si_sync();
31 this.si_docs = this.get_doc_from_localstorage();
32 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053033
Rohit Waghchauree0934d12016-05-11 15:04:57 +053034 on_refresh_page: function() {
35 var me = this;
36 if(this.load){
37 this.load = false;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053038 }else if(this.connection_status){
39 this.onload();
Rohit Waghchauree0934d12016-05-11 15:04:57 +053040 }else{
41 this.create_new();
42 }
43 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053044
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053045 beforeunload: function(e){
46 if(this.connection_status == false && frappe.get_route()[0] == "pos"){
47 e = e || window.event;
48
49 // For IE and Firefox prior to version 4
50 if (e) {
51 e.returnValue = __("You are in offline mode. You will not be able to reload until you have network.");
52 return
53 }
54
55 // For Safari
56 return __("You are in offline mode. You will not be able to reload until you have network.");
57 }
58 },
59
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053060 check_internet_connection: function(){
61 var me = this;
62 //Check Internet connection after every 30 seconds
63 setInterval(function(){
64 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053065 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053066 },
67
68 set_indicator: function(){
69 var me = this;
70 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053071 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053072 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053073 frappe.call({
74 method:"frappe.handler.ping",
75 callback: function(r){
76 if(r.message){
77 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053078 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053079 }
80 }
81 })
82 },
83
84 onload: function(){
85 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053086 this.get_data_from_server(function(){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053087 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053088 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053089
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053090 this.check_internet_connection();
91 },
92
93 make_menu_list: function(){
94 var me = this;
95
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053096 this.page.add_menu_item(__("New Sales Invoice"), function() {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053097 me.save_previous_entry();
98 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053099 })
100
101 this.page.add_menu_item(__("View Offline Records"), function(){
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530102 me.show_unsync_invoice_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530103 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530104
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530105 this.page.add_menu_item(__("Sync Master Data"), function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530106 me.get_data_from_server(function(){
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530107 me.load_data(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530108 me.make_customer();
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530109 me.make_item_list(true);
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530110 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530111 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530112 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530113
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530114 this.page.add_menu_item(__("Sync Offline Invoices"), function(){
115 me.sync_sales_invoice()
116 });
117
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530118 this.page.add_menu_item(__("POS Profile"), function() {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530119 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530120 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530121 },
122
123 show_unsync_invoice_list: function(){
124 var me = this;
125 this.si_docs = this.get_doc_from_localstorage();
126
127 this.list_dialog = new frappe.ui.Dialog({
128 title: 'Invoice List'
129 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530130
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530131 this.list_dialog.show();
132 this.list_body = this.list_dialog.body;
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530133 if(this.si_docs.length > 0){
134 $(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\
135 <div class="col-xs-2">Sr</div>\
136 <div class="col-xs-4">Customer</div>\
137 <div class="col-xs-2 text-left">Status</div>\
138 <div class="col-xs-4 text-right">Grand Total</div>\
139 </div>')
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530140
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530141 $.each(this.si_docs, function(index, data){
142 for(key in data) {
143 $(frappe.render_template("pos_invoice_list", {
144 sr: index + 1,
145 name: key,
146 customer: data[key].customer,
147 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
148 data: me.get_doctype_status(data[key])
149 })).appendTo($(me.list_body));
150 }
151 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530152
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530153 $(this.list_body).find('.list-row').click(function() {
154 me.name = $(this).attr('invoice-name')
155 doc_data = me.get_invoice_doc(me.si_docs)
156 if(doc_data){
157 me.frm.doc = doc_data[0][me.name];
158 me.set_missing_values();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530159 me.refresh(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530160 me.disable_input_field();
161 me.list_dialog.hide();
162 }
163 })
164 }else{
165 $(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")}))
166 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530167 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530168
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530169 get_doctype_status: function(doc){
170 if(doc.outstanding_amount == 0){
171 return {status: "Paid", indicator: "green"}
172 }else if(doc.docstatus == 0){
173 return {status: "Draft", indicator: "red"}
174 }else if(doc.paid_amount >= 0){
175 return {status: "Unpaid", indicator: "orange"}
176 }
177 },
178
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530179 set_missing_values: function(){
180 var me = this;
181 doc = JSON.parse(localStorage.getItem('doc'))
182 if(this.frm.doc.payments.length == 0){
183 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530184 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530185 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530186
187 if(this.frm.doc.customer){
188 this.party_field.$input.val(this.frm.doc.customer);
189 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530190
191 if(!this.frm.doc.write_off_account){
192 this.frm.doc.write_off_account = doc.write_off_account
193 }
194
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530195 if(!this.frm.doc.account_for_change_amount){
196 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530197 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530198 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530199
200 get_invoice_doc: function(si_docs){
201 var me = this;
202 this.si_docs = this.get_doc_from_localstorage();
203
204 return $.grep(this.si_docs, function(data){
205 for(key in data){
206 return key == me.name
207 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530208 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530209 },
210
211 get_data_from_server: function(callback){
212 var me = this;
213 frappe.call({
214 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
215 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530216 freeze_message: __("Master data syncing, it might take some time"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530217 callback: function(r){
218 window.items = r.message.items;
219 window.customers = r.message.customers;
220 window.pricing_rules = r.message.pricing_rules;
221 window.meta = r.message.meta;
222 window.print_template = r.message.print_template;
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530223 me.default_customer = r.message.default_customer || null;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530224 localStorage.setItem('doc', JSON.stringify(r.message.doc));
225 if(callback){
226 callback();
227 }
228 }
229 })
230 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530231
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530232 save_previous_entry : function(){
233 if(this.frm.doc.items.length > 0){
234 this.create_invoice()
235 }
236 },
237
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530238 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530239 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530240 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530241 this.name = '';
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530242 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530243 this.setup();
244 },
245
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530246 load_data: function(load_doc){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530247 this.items = window.items;
248 this.customers = window.customers;
249 this.pricing_rules = window.pricing_rules;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530250
251 if(load_doc) {
252 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
253 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530254
255 $.each(window.meta, function(i, data){
256 frappe.meta.sync(data)
257 })
258
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530259 this.print_template = frappe.render_template("print_template",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530260 {content: window.print_template, title:"POS"})
261 },
262
263 setup: function(){
264 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
265 this.set_transaction_defaults("Customer");
266 this.make();
267 this.set_primary_action();
268 },
269
270 set_transaction_defaults: function(party) {
271 var me = this;
272 this.party = party;
273 this.price_list = (party == "Customer" ?
274 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
275 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
276 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
277 },
278
279 make: function() {
280 this.make_search();
281 this.make_customer();
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530282 this.make_item_list(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530283 this.make_discount_field()
284 },
285
286 make_search: function() {
287 var me = this;
288 this.search = frappe.ui.form.make_control({
289 df: {
290 "fieldtype": "Data",
291 "label": "Item",
292 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530293 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530294 },
295 parent: this.wrapper.find(".search-area"),
296 only_input: true,
297 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530298
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530299 this.search.make_input();
300 this.search.$input.on("keyup", function() {
301 setTimeout(function() {
302 me.items = me.get_items();
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530303 me.make_item_list(false);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530304 }, 1000);
305 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530306
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530307 this.party_field = frappe.ui.form.make_control({
308 df: {
309 "fieldtype": "Data",
310 "options": this.party,
311 "label": this.party,
312 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530313 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530314 },
315 parent: this.wrapper.find(".party-area"),
316 only_input: true,
317 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530318
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530319 this.party_field.make_input();
320 },
321
322 make_customer: function() {
323 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530324
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530325 if(this.default_customer && !this.frm.doc.customer){
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530326 this.party_field.$input.val(this.default_customer);
327 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530328 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530329
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530330 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530331 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530332 source: function (request, response) {
333 me.customer_data = me.get_customers(request.term)
334 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530335 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530336 customer_group: data.customer_group, territory: data.territory}
337 }))
338 },
339 change: function(event, ui){
340 if(ui.item){
341 me.frm.doc.customer = ui.item.label;
342 me.frm.doc.customer_name = ui.item.customer_name;
343 me.frm.doc.customer_group = ui.item.customer_group;
344 me.frm.doc.territory = ui.item.territory;
345 }else{
346 me.frm.doc.customer = me.party_field.$input.val();
347 }
348 me.refresh();
349 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530350 }).on("focus", function(){
351 setTimeout(function() {
352 if(!me.party_field.$input.val()) {
353 me.party_field.$input.autocomplete( "search", " " );
354 }
355 }, 500);
356 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530357 },
358
359 get_customers: function(key){
360 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530361 key = key.toLowerCase().trim()
362 if(key){
363 return $.grep(this.customers, function(data) {
364 if(data.name.toLowerCase().match(key)
365 || data.customer_name.toLowerCase().match(key)
366 || (data.customer_group && data.customer_group.toLowerCase().match(key))){
367 return data
368 }
369 })
370 }else{
371 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
372 return customers.slice(0, 20)
373 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530374 },
375
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530376 make_item_list: function(index_search) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530377 var me = this;
378 if(!this.price_list) {
379 msgprint(__("Price List not found or disabled"));
380 return;
381 }
382
383 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530384
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530385 var $wrap = me.wrapper.find(".item-list");
386 me.wrapper.find(".item-list").empty();
387
388 if (this.items) {
389 $.each(this.items, function(index, obj) {
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530390 if(!index_search || index < 16){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530391 $(frappe.render_template("pos_item", {
392 item_code: obj.name,
393 item_price: format_currency(obj.price_list_rate, obj.currency),
394 item_name: obj.name===obj.item_name ? "" : obj.item_name,
395 item_image: obj.image ? "url('" + obj.image + "')" : null,
396 color: frappe.get_palette(obj.item_name),
397 abbr: frappe.get_abbr(obj.item_name)
398 })).tooltip().appendTo($wrap);
399 }
400 });
401 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530402
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530403 if(this.items.length == 1
404 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530405 this.search.$input.val("");
406 this.add_to_cart();
407 }
408
409 // if form is local then allow this function
410 $(me.wrapper).find("div.pos-item").on("click", function() {
411 me.customer_validate();
412 if(me.frm.doc.docstatus==0) {
413 me.items = me.get_items($(this).attr("data-item-code"))
414 me.add_to_cart();
415 }
416 });
417 },
418
419 get_items: function(item_code){
420 // To search item as per the key enter
421
422 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530423 this.item_serial_no = {};
424 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530425
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530426 if(item_code){
427 return $.grep(window.items, function(item){
428 if(item.item_code == item_code ){
429 return true
430 }
431 })
432 }
433
434 key = this.search.$input.val().toLowerCase();
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530435 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530436
437 if(key){
438 return $.grep(window.items, function(item){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530439 if(search_status){
440 if(in_list(item.batch_nos, me.search.$input.val())){
441 search_status = false;
442 return me.item_batch_no[item.item_code] = me.search.$input.val()
443 } else if(in_list(Object.keys(item.serial_nos), me.search.$input.val())) {
444 search_status = false;
445 me.item_serial_no[item.item_code] = [me.search.$input.val(), item.serial_nos[me.search.$input.val()]]
446 return true
447 } else if(item.barcode == me.search.$input.val()) {
448 search_status = false;
449 return item.barcode == me.search.$input.val();
450 } else if((item.item_code.toLowerCase().match(key)) ||
451 (item.item_name.toLowerCase().match(key)) || (item.item_group.toLowerCase().match(key))) {
452 return true
453 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530454 }
455 })
456 }else{
457 return window.items;
458 }
459 },
460
461 update_qty: function() {
462 var me = this;
463
464 $(this.wrapper).find(".pos-item-qty").on("change", function(){
465 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530466 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530467 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530468
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530469 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
470 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
471 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530472 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530473 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530474
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530475 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
476 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
477 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530478 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530479 })
480 },
481
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530482 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530483 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530484
485 $(this.wrapper).find(".pos-item-rate").on("change", function(){
486 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
487 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
488 })
489 },
490
491 update_qty_rate_against_item_code: function(item_code, field, value){
492 var me = this;
493 if(value < 0){
494 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530495 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530496
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530497 this.remove_item = []
498 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530499 if(d.serial_no){
500 me.validate_serial_no_qty(d, item_code, field, value)
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530501 }
502
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530503 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530504 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530505 d.amount = flt(d.rate) * flt(d.qty);
506 if(d.qty==0){
507 me.remove_item.push(d.idx)
508 }
509 }
510 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530511
512 if(field == 'qty'){
513 this.remove_zero_qty_item();
514 }
515
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530516 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530517 },
518
519 remove_zero_qty_item: function(){
520 var me = this;
521 idx = 0
522 this.items = []
523 idx = 0
524 $.each(this.frm.doc["items"] || [], function(i, d) {
525 if(!in_list(me.remove_item, d.idx)){
526 d.idx = idx;
527 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530528 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530529 }
530 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530531
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530532 this.frm.doc["items"] = this.items;
533 },
534
535 make_discount_field: function(){
536 var me = this;
537
538 this.wrapper.find('input.discount-percentage').on("change", function() {
539 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
540 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530541
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530542 if(me.frm.doc.apply_discount_on == 'Net Total'){
543 total = me.frm.doc.net_total
544 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530545
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530546 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
547 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
548 me.refresh();
549 });
550
551 this.wrapper.find('input.discount-amount').on("change", function() {
552 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
553 me.frm.doc.additional_discount_percentage = 0.0;
554 me.wrapper.find('input.discount-percentage').val(0);
555 me.refresh();
556 });
557 },
558
559 customer_validate: function(){
560 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530561 if(!this.frm.doc.customer){
562 frappe.throw(__("Please select customer"))
563 }
564 },
565
566 add_to_cart: function() {
567 var me = this;
568 var caught = false;
569 var no_of_items = me.wrapper.find(".pos-bill-item").length;
570
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530571 this.customer_validate();
572 this.mandatory_batch_no();
573 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530574 this.validate_warehouse();
575
576 if (no_of_items != 0) {
577 $.each(this.frm.doc["items"] || [], function(i, d) {
578 if (d.item_code == me.items[0].item_code) {
579 caught = true;
580 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530581 d.amount = flt(d.rate) * flt(d.qty);
582 if(me.item_serial_no[d.item_code]){
583 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
584 d.warehouse = me.item_serial_no[d.item_code][1]
585 }
586
587 if(me.item_batch_no.length){
588 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530589 }
590 }
591 });
592 }
593
594 // if item not found then add new item
595 if (!caught)
596 this.add_new_item_to_grid();
597
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530598 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530599 },
600
601 add_new_item_to_grid: function() {
602 var me = this;
603 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
604 this.child.item_code = this.items[0].item_code;
605 this.child.item_name = this.items[0].item_name;
606 this.child.stock_uom = this.items[0].stock_uom;
607 this.child.description = this.items[0].description;
608 this.child.qty = 1;
609 this.child.item_group = this.items[0].item_group;
610 this.child.cost_center = this.items[0].cost_center;
611 this.child.income_account = this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530612 this.child.warehouse = (this.item_serial_no[this.child.item_code]
613 ? this.item_serial_no[this.child.item_code][1] : this.items[0].default_warehouse);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530614 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
615 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
616 this.child.actual_qty = this.items[0].actual_qty;
617 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530618 this.child.batch_no = this.item_batch_no[this.child.item_code];
619 this.child.serial_no = (this.item_serial_no[this.child.item_code]
620 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaure525900c2016-09-07 15:01:08 +0530621 this.child.item_tax_rate = this.items[0].taxes;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530622 },
623
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530624 update_paid_amount_status: function(update_paid_amount){
625 if(this.name){
626 update_paid_amount = update_paid_amount ? false : true;
627 }
628
629 this.refresh(update_paid_amount);
630 },
631
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530632 refresh: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530633 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530634 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530635 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530636 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530637 this.set_primary_action();
638 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530639
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530640 refresh_fields: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530641 this.apply_pricing_rule();
642 this.discount_amount_applied = false;
643 this._calculate_taxes_and_totals();
644 this.calculate_discount_amount();
645 this.show_items_in_item_cart();
646 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530647 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530648 this.set_totals();
649 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530650
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530651 get_company_currency: function() {
652 return erpnext.get_currency(this.frm.doc.company);
653 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530654
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530655 show_item_wise_taxes: function(){
656 return null;
657 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530658
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530659 show_items_in_item_cart: function() {
660 var me = this;
661 var $items = this.wrapper.find(".items").empty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530662 $.each(this.frm.doc.items|| [], function(i, d) {
663 $(frappe.render_template("pos_bill_item", {
664 item_code: d.item_code,
665 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
666 qty: d.qty,
667 actual_qty: d.actual_qty,
668 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530669 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530670 amount: format_currency(d.amount, me.frm.doc.currency)
671 })).appendTo($items);
672 });
673
674 this.wrapper.find("input.pos-item-qty").on("focus", function() {
675 $(this).select();
676 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530677
678 this.wrapper.find("input.pos-item-rate").on("focus", function() {
679 $(this).select();
680 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530681 },
682
683 set_taxes: function(){
684 var me = this;
685 me.frm.doc.total_taxes_and_charges = 0.0
686
687 var taxes = this.frm.doc.taxes || [];
688 $(this.wrapper)
689 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
690 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530691
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530692 $.each(taxes, function(i, d) {
693 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
694 $(frappe.render_template("pos_tax_row", {
695 description: d.description,
696 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
697 me.frm.doc.currency)
698 })).appendTo(me.wrapper.find(".tax-table"));
699 }
700 });
701 },
702
703 set_totals: function() {
704 var me = this;
705 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
706 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
707 },
708
709 set_primary_action: function() {
710 var me = this;
711
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530712 if (this.frm.doc.docstatus==0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530713 this.page.set_primary_action(__("Pay"), function() {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530714 me.validate();
715 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530716 me.create_invoice();
717 me.make_payment();
Faris Ansari8908d192016-07-20 13:59:19 +0530718 }, "octicon octicon-credit-card");
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530719 }else if(this.frm.doc.docstatus == 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530720 this.page.set_primary_action(__("Print"), function() {
721 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530722 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530723 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530724 }else {
725 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530726 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530727
728 this.page.set_secondary_action(__("New"), function() {
729 me.save_previous_entry();
730 me.create_new();
Faris Ansari8908d192016-07-20 13:59:19 +0530731 }, "octicon octicon-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530732 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530733
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530734 print_document: function(html){
735 var w = window.open();
736 w.document.write(html);
737 w.document.close();
738 setTimeout(function(){
739 w.print();
740 w.close();
741 }, 1000)
742 },
743
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530744 submit_invoice: function(){
745 var me = this;
746 frappe.confirm(__("Do you really want to submit the invoice?"), function () {
747 me.change_status();
Rohit Waghchaure53bea822016-07-06 16:09:26 +0530748 frappe.msgprint(__("Sales invoice submitted sucessfully."))
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530749 })
750 },
751
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530752 change_status: function(){
753 if(this.frm.doc.docstatus == 0){
754 this.frm.doc.docstatus = 1;
755 this.update_invoice();
756 this.disable_input_field();
757 }
758 },
759
760 disable_input_field: function(){
761 var pointer_events = 'inherit'
762 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530763
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530764 if(this.frm.doc.docstatus == 1){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530765 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530766 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530767 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530768
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530769 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
770 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
771 this.set_primary_action();
772 },
773
774 create_invoice: function(){
775 var me = this;
776 var invoice_data = {}
777 this.si_docs = this.get_doc_from_localstorage();
778 if(this.name){
779 this.update_invoice()
780 }else{
781 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +0530782 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +0530783 this.frm.doc.posting_date = frappe.datetime.get_today();
784 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530785 invoice_data[this.name] = this.frm.doc
786 this.si_docs.push(invoice_data)
787 this.update_localstorage();
788 this.set_primary_action();
789 }
790 },
791
792 update_invoice: function(){
793 var me = this;
794 this.si_docs = this.get_doc_from_localstorage();
795 $.each(this.si_docs, function(index, data){
796 for(key in data){
797 if(key == me.name){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530798 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530799 me.update_localstorage();
800 }
801 }
802 })
803 },
804
805 update_localstorage: function(){
806 try{
807 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
808 }catch(e){
809 frappe.throw(__("LocalStorage is full , did not save"))
810 }
811 },
812
813 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530814 try{
815 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
816 }catch(e){
817 return []
818 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530819 },
820
821 set_interval_for_si_sync: function(){
822 var me = this;
823 setInterval(function(){
824 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530825 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530826 },
827
828 sync_sales_invoice: function(){
829 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530830 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530831
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530832 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530833 frappe.call({
834 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
835 args: {
836 doc_list: me.si_docs
837 },
838 callback: function(r){
839 if(r.message){
840 me.removed_items = r.message;
841 me.remove_doc_from_localstorage();
842 }
843 }
844 })
845 }
846 },
847
848 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530849 var invoices = [];
850 var index = 1;
851 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530852 if(docs){
853 invoices = $.map(docs, function(data){
854 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530855 if(data[key].docstatus == 1 && index < 50){
856 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530857 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530858 return data
859 }
860 }
861 });
862 }
863
864 return invoices
865 },
866
867 remove_doc_from_localstorage: function(){
868 var me = this;
869 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530870 this.new_si_docs = [];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530871 if(this.removed_items){
872 $.each(this.si_docs, function(index, data){
873 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530874 if(!in_list(me.removed_items, key)){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530875 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530876 }
877 }
878 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530879 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530880 this.update_localstorage();
881 }
882 },
883
884 validate: function(){
885 var me = this;
886 this.customer_validate();
887 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530888 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530889 },
890
891 item_validate: function(){
892 if(this.frm.doc.items.length == 0){
893 frappe.throw(__("Select items to save the invoice"))
894 }
895 },
Saurabh9a6c6662016-06-27 16:51:44 +0530896
897 validate_mode_of_payments: function(){
898 if (this.frm.doc.payments.length === 0){
899 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
900 }
901 },
902
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530903 validate_serial_no: function(){
904 var me = this;
905 var item_code = serial_no = '';
906 for (key in this.item_serial_no){
907 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530908 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530909 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530910
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530911 if(item_code && serial_no){
912 $.each(this.frm.doc.items, function(index, data){
913 if(data.item_code == item_code){
914 if(in_list(data.serial_no.split('\n'), serial_no)){
915 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
916 'serial_no': serial_no
917 })))
918 }
919 }
920 })
921 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530922
923 if(this.items[0].has_serial_no && serial_no == ""){
924 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
925 'item': this.items[0].item_code
926 })))
927 }
928 },
929
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530930 validate_serial_no_qty: function(args, item_code, field, value){
931 var me = this;
932 if (args.item_code == item_code && args.serial_no
933 && field == 'qty' && cint(value) != value) {
934 args.qty = 0.0;
935 this.refresh();
936 frappe.throw(__("Serial no item cannot be a fraction"))
937 }
938
939 if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
940 args.qty = 0.0;
941 args.serial_no = ''
942 this.refresh();
943 frappe.throw(__("Total nos of serial no is not equal to quantity."))
944 }
945 },
946
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530947 mandatory_batch_no: function(){
948 var me = this;
949 if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
950 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
951 'item': this.items[0].item_code
952 })))
953 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530954 },
955
956 apply_pricing_rule: function(){
957 var me = this;
958 $.each(this.frm.doc["items"], function(n, item) {
959 pricing_rule = me.get_pricing_rule(item)
960 me.validate_pricing_rule(pricing_rule)
961 if(pricing_rule.length){
962 item.margin_type = pricing_rule[0].margin_type;
963 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
964 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
965 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
966 me.apply_pricing_rule_on_item(item)
967 }
968 })
969 },
970
971 get_pricing_rule: function(item){
972 var me = this;
973 return $.grep(this.pricing_rules, function(data){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530974 if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
975 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
976 if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
977 return me.validate_condition(data)
978 }else{
979 return true
980 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530981 }
982 }
983 })
984 },
985
986 validate_condition: function(data){
987 //This method check condition based on applicable for
988 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
989 if(in_list(condition[1], condition[0])){
990 return true
991 }
992 },
993
994 get_mapper_for_pricing_rule: function(data){
995 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +0530996 'Customer': [data.customer, [this.frm.doc.customer]],
997 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
998 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530999 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301000 }
1001 },
1002
1003 validate_pricing_rule: function(pricing_rule){
1004 //This method validate duplicate pricing rule
1005 var pricing_rule_name = '';
1006 var priority = 0;
1007 var pricing_rule_list = [];
1008 var priority_list = []
1009
1010 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301011
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301012 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301013 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301014 priority_list.push(data.priority)
1015 if(priority <= data.priority){
1016 priority = data.priority
1017 pricing_rule_list.push(data)
1018 }
1019 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301020
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301021 count = 0
1022 $.each(priority_list, function(index, value){
1023 if(value == priority){
1024 count++
1025 }
1026 })
1027
1028 if(priority == 0 || count > 1){
1029 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1030 'pricing_rule': pricing_rule_name
1031 })))
1032 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301033
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301034 return pricing_rule_list
1035 }
1036 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301037
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301038 validate_warehouse: function(){
1039 if(!this.items[0].default_warehouse){
1040 frappe.throw(__("Deafault warehouse is required for selected item"))
1041 }
1042 }
1043})