blob: 07e55d9a5c83c3d4914bd0cd4389163a64aecfbd [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();
109 me.make_item_list();
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
114 this.page.add_menu_item(__("POS Profile"), function() {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530115 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530116 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530117 },
118
119 show_unsync_invoice_list: function(){
120 var me = this;
121 this.si_docs = this.get_doc_from_localstorage();
122
123 this.list_dialog = new frappe.ui.Dialog({
124 title: 'Invoice List'
125 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530126
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530127 this.list_dialog.show();
128 this.list_body = this.list_dialog.body;
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530129 if(this.si_docs.length > 0){
130 $(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530131 <div class="col-xs-1">Sr</div>\
132 <div class="col-xs-3">Customer</div>\
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530133 <div class="col-xs-2 text-left">Status</div>\
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530134 <div class="col-xs-3 text-right">Paid Amount</div>\
135 <div class="col-xs-3 text-right">Grand Total</div>\
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530136 </div>')
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530137
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530138 $.each(this.si_docs, function(index, data){
139 for(key in data) {
140 $(frappe.render_template("pos_invoice_list", {
141 sr: index + 1,
142 name: key,
143 customer: data[key].customer,
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530144 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530145 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
146 data: me.get_doctype_status(data[key])
147 })).appendTo($(me.list_body));
148 }
149 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530150
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530151 $(this.list_body).find('.list-row').click(function() {
152 me.name = $(this).attr('invoice-name')
153 doc_data = me.get_invoice_doc(me.si_docs)
154 if(doc_data){
155 me.frm.doc = doc_data[0][me.name];
156 me.set_missing_values();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530157 me.refresh(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530158 me.disable_input_field();
159 me.list_dialog.hide();
160 }
161 })
162 }else{
163 $(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")}))
164 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530165 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530166
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530167 get_doctype_status: function(doc){
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530168 if(doc.docstatus == 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530169 return {status: "Draft", indicator: "red"}
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530170 }else if(doc.outstanding_amount == 0) {
171 return {status: "Paid", indicator: "green"}
172 }else {
173 return {status: "Submitted", indicator: "blue"}
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530174 }
175 },
176
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530177 set_missing_values: function(){
178 var me = this;
179 doc = JSON.parse(localStorage.getItem('doc'))
180 if(this.frm.doc.payments.length == 0){
181 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530182 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530183 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530184
185 if(this.frm.doc.customer){
186 this.party_field.$input.val(this.frm.doc.customer);
187 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530188
189 if(!this.frm.doc.write_off_account){
190 this.frm.doc.write_off_account = doc.write_off_account
191 }
192
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530193 if(!this.frm.doc.account_for_change_amount){
194 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530195 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530196 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530197
198 get_invoice_doc: function(si_docs){
199 var me = this;
200 this.si_docs = this.get_doc_from_localstorage();
201
202 return $.grep(this.si_docs, function(data){
203 for(key in data){
204 return key == me.name
205 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530206 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530207 },
208
209 get_data_from_server: function(callback){
210 var me = this;
211 frappe.call({
212 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
213 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530214 freeze_message: __("Master data syncing, it might take some time"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530215 callback: function(r){
216 window.items = r.message.items;
217 window.customers = r.message.customers;
218 window.pricing_rules = r.message.pricing_rules;
219 window.meta = r.message.meta;
220 window.print_template = r.message.print_template;
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530221 me.default_customer = r.message.default_customer || null;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530222 localStorage.setItem('doc', JSON.stringify(r.message.doc));
223 if(callback){
224 callback();
225 }
226 }
227 })
228 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530229
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530230 save_previous_entry : function(){
231 if(this.frm.doc.items.length > 0){
232 this.create_invoice()
233 }
234 },
235
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530236 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530237 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530238 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530239 this.name = '';
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530240 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530241 this.setup();
242 },
243
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530244 load_data: function(load_doc){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530245 this.items = window.items;
246 this.customers = window.customers;
247 this.pricing_rules = window.pricing_rules;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530248
249 if(load_doc) {
250 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
251 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530252
253 $.each(window.meta, function(i, data){
254 frappe.meta.sync(data)
255 })
256
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530257 this.print_template = frappe.render_template("print_template",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530258 {content: window.print_template, title:"POS"})
259 },
260
261 setup: function(){
262 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
263 this.set_transaction_defaults("Customer");
264 this.make();
265 this.set_primary_action();
266 },
267
268 set_transaction_defaults: function(party) {
269 var me = this;
270 this.party = party;
271 this.price_list = (party == "Customer" ?
272 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
273 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
274 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
275 },
276
277 make: function() {
278 this.make_search();
279 this.make_customer();
280 this.make_item_list();
281 this.make_discount_field()
282 },
283
284 make_search: function() {
285 var me = this;
286 this.search = frappe.ui.form.make_control({
287 df: {
288 "fieldtype": "Data",
289 "label": "Item",
290 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530291 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530292 },
293 parent: this.wrapper.find(".search-area"),
294 only_input: true,
295 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530296
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530297 this.search.make_input();
298 this.search.$input.on("keyup", function() {
299 setTimeout(function() {
300 me.items = me.get_items();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530301 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530302 }, 1000);
303 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530304
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530305 this.party_field = frappe.ui.form.make_control({
306 df: {
307 "fieldtype": "Data",
308 "options": this.party,
309 "label": this.party,
310 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530311 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530312 },
313 parent: this.wrapper.find(".party-area"),
314 only_input: true,
315 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530316
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530317 this.party_field.make_input();
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530318 this.set_focus()
319 },
320
321 set_focus: function(){
322 if(this.default_customer){
323 this.search.$input.focus();
324 }else{
325 this.party_field.$input.focus();
326 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530327 },
328
329 make_customer: function() {
330 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530331
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530332 if(this.default_customer && !this.frm.doc.customer){
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530333 this.party_field.$input.val(this.default_customer);
334 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530335 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530336
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530337 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530338 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530339 source: function (request, response) {
340 me.customer_data = me.get_customers(request.term)
341 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530342 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530343 customer_group: data.customer_group, territory: data.territory}
344 }))
345 },
346 change: function(event, ui){
347 if(ui.item){
348 me.frm.doc.customer = ui.item.label;
349 me.frm.doc.customer_name = ui.item.customer_name;
350 me.frm.doc.customer_group = ui.item.customer_group;
351 me.frm.doc.territory = ui.item.territory;
352 }else{
353 me.frm.doc.customer = me.party_field.$input.val();
354 }
355 me.refresh();
356 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530357 }).on("focus", function(){
358 setTimeout(function() {
359 if(!me.party_field.$input.val()) {
360 me.party_field.$input.autocomplete( "search", " " );
361 }
362 }, 500);
363 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530364 },
365
366 get_customers: function(key){
367 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530368 key = key.toLowerCase().trim()
369 if(key){
370 return $.grep(this.customers, function(data) {
371 if(data.name.toLowerCase().match(key)
372 || data.customer_name.toLowerCase().match(key)
373 || (data.customer_group && data.customer_group.toLowerCase().match(key))){
374 return data
375 }
376 })
377 }else{
378 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
379 return customers.slice(0, 20)
380 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530381 },
382
383 make_item_list: function() {
384 var me = this;
385 if(!this.price_list) {
386 msgprint(__("Price List not found or disabled"));
387 return;
388 }
389
390 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530391
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530392 var $wrap = me.wrapper.find(".item-list");
393 me.wrapper.find(".item-list").empty();
394
395 if (this.items) {
396 $.each(this.items, function(index, obj) {
397 if(index < 16){
398 $(frappe.render_template("pos_item", {
399 item_code: obj.name,
400 item_price: format_currency(obj.price_list_rate, obj.currency),
401 item_name: obj.name===obj.item_name ? "" : obj.item_name,
402 item_image: obj.image ? "url('" + obj.image + "')" : null,
403 color: frappe.get_palette(obj.item_name),
404 abbr: frappe.get_abbr(obj.item_name)
405 })).tooltip().appendTo($wrap);
406 }
407 });
408 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530409
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530410 if(this.items.length == 1
411 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530412 this.search.$input.val("");
413 this.add_to_cart();
414 }
415
416 // if form is local then allow this function
417 $(me.wrapper).find("div.pos-item").on("click", function() {
418 me.customer_validate();
419 if(me.frm.doc.docstatus==0) {
420 me.items = me.get_items($(this).attr("data-item-code"))
421 me.add_to_cart();
422 }
423 });
424 },
425
426 get_items: function(item_code){
427 // To search item as per the key enter
428
429 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530430 this.item_serial_no = {};
431 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530432
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530433 if(item_code){
434 return $.grep(window.items, function(item){
435 if(item.item_code == item_code ){
436 return true
437 }
438 })
439 }
440
441 key = this.search.$input.val().toLowerCase();
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530442 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530443
444 if(key){
445 return $.grep(window.items, function(item){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530446 if(search_status){
447 if(in_list(item.batch_nos, me.search.$input.val())){
448 search_status = false;
449 return me.item_batch_no[item.item_code] = me.search.$input.val()
450 } else if(in_list(Object.keys(item.serial_nos), me.search.$input.val())) {
451 search_status = false;
452 me.item_serial_no[item.item_code] = [me.search.$input.val(), item.serial_nos[me.search.$input.val()]]
453 return true
454 } else if(item.barcode == me.search.$input.val()) {
455 search_status = false;
456 return item.barcode == me.search.$input.val();
457 } else if((item.item_code.toLowerCase().match(key)) ||
458 (item.item_name.toLowerCase().match(key)) || (item.item_group.toLowerCase().match(key))) {
459 return true
460 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530461 }
462 })
463 }else{
464 return window.items;
465 }
466 },
467
468 update_qty: function() {
469 var me = this;
470
471 $(this.wrapper).find(".pos-item-qty").on("change", function(){
472 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530473 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530474 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530475
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530476 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
477 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
478 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530479 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530480 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530481
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530482 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
483 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
484 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530485 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530486 })
487 },
488
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530489 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530490 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530491
492 $(this.wrapper).find(".pos-item-rate").on("change", function(){
493 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
494 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
495 })
496 },
497
498 update_qty_rate_against_item_code: function(item_code, field, value){
499 var me = this;
500 if(value < 0){
501 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530502 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530503
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530504 this.remove_item = []
505 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530506 if(d.serial_no){
507 me.validate_serial_no_qty(d, item_code, field, value)
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530508 }
509
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530510 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530511 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530512 d.amount = flt(d.rate) * flt(d.qty);
513 if(d.qty==0){
514 me.remove_item.push(d.idx)
515 }
516 }
517 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530518
519 if(field == 'qty'){
520 this.remove_zero_qty_item();
521 }
522
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530523 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530524 },
525
526 remove_zero_qty_item: function(){
527 var me = this;
528 idx = 0
529 this.items = []
530 idx = 0
531 $.each(this.frm.doc["items"] || [], function(i, d) {
532 if(!in_list(me.remove_item, d.idx)){
533 d.idx = idx;
534 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530535 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530536 }
537 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530538
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530539 this.frm.doc["items"] = this.items;
540 },
541
542 make_discount_field: function(){
543 var me = this;
544
545 this.wrapper.find('input.discount-percentage').on("change", function() {
546 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
547 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530548
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530549 if(me.frm.doc.apply_discount_on == 'Net Total'){
550 total = me.frm.doc.net_total
551 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530552
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530553 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
554 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
555 me.refresh();
556 });
557
558 this.wrapper.find('input.discount-amount').on("change", function() {
559 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
560 me.frm.doc.additional_discount_percentage = 0.0;
561 me.wrapper.find('input.discount-percentage').val(0);
562 me.refresh();
563 });
564 },
565
566 customer_validate: function(){
567 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530568 if(!this.frm.doc.customer){
569 frappe.throw(__("Please select customer"))
570 }
571 },
572
573 add_to_cart: function() {
574 var me = this;
575 var caught = false;
576 var no_of_items = me.wrapper.find(".pos-bill-item").length;
577
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530578 this.customer_validate();
579 this.mandatory_batch_no();
580 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530581 this.validate_warehouse();
582
583 if (no_of_items != 0) {
584 $.each(this.frm.doc["items"] || [], function(i, d) {
585 if (d.item_code == me.items[0].item_code) {
586 caught = true;
587 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530588 d.amount = flt(d.rate) * flt(d.qty);
589 if(me.item_serial_no[d.item_code]){
590 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
591 d.warehouse = me.item_serial_no[d.item_code][1]
592 }
593
594 if(me.item_batch_no.length){
595 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530596 }
597 }
598 });
599 }
600
601 // if item not found then add new item
602 if (!caught)
603 this.add_new_item_to_grid();
604
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530605 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530606 },
607
608 add_new_item_to_grid: function() {
609 var me = this;
610 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
611 this.child.item_code = this.items[0].item_code;
612 this.child.item_name = this.items[0].item_name;
613 this.child.stock_uom = this.items[0].stock_uom;
614 this.child.description = this.items[0].description;
615 this.child.qty = 1;
616 this.child.item_group = this.items[0].item_group;
617 this.child.cost_center = this.items[0].cost_center;
618 this.child.income_account = this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530619 this.child.warehouse = (this.item_serial_no[this.child.item_code]
620 ? this.item_serial_no[this.child.item_code][1] : this.items[0].default_warehouse);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530621 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
622 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
623 this.child.actual_qty = this.items[0].actual_qty;
624 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530625 this.child.batch_no = this.item_batch_no[this.child.item_code];
626 this.child.serial_no = (this.item_serial_no[this.child.item_code]
627 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaure525900c2016-09-07 15:01:08 +0530628 this.child.item_tax_rate = this.items[0].taxes;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530629 },
630
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530631 update_paid_amount_status: function(update_paid_amount){
632 if(this.name){
633 update_paid_amount = update_paid_amount ? false : true;
634 }
635
636 this.refresh(update_paid_amount);
637 },
638
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530639 refresh: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530640 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530641 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530642 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530643 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530644 this.set_primary_action();
645 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530646
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530647 refresh_fields: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530648 this.apply_pricing_rule();
649 this.discount_amount_applied = false;
650 this._calculate_taxes_and_totals();
651 this.calculate_discount_amount();
652 this.show_items_in_item_cart();
653 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530654 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530655 this.set_totals();
656 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530657
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530658 get_company_currency: function() {
659 return erpnext.get_currency(this.frm.doc.company);
660 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530661
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530662 show_item_wise_taxes: function(){
663 return null;
664 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530665
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530666 show_items_in_item_cart: function() {
667 var me = this;
668 var $items = this.wrapper.find(".items").empty();
669 me.frm.doc.net_total = 0.0
670 $.each(this.frm.doc.items|| [], function(i, d) {
671 $(frappe.render_template("pos_bill_item", {
672 item_code: d.item_code,
673 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
674 qty: d.qty,
675 actual_qty: d.actual_qty,
676 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530677 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530678 amount: format_currency(d.amount, me.frm.doc.currency)
679 })).appendTo($items);
680 });
681
682 this.wrapper.find("input.pos-item-qty").on("focus", function() {
683 $(this).select();
684 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530685
686 this.wrapper.find("input.pos-item-rate").on("focus", function() {
687 $(this).select();
688 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530689 },
690
691 set_taxes: function(){
692 var me = this;
693 me.frm.doc.total_taxes_and_charges = 0.0
694
695 var taxes = this.frm.doc.taxes || [];
696 $(this.wrapper)
697 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
698 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530699
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530700 $.each(taxes, function(i, d) {
701 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
702 $(frappe.render_template("pos_tax_row", {
703 description: d.description,
704 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
705 me.frm.doc.currency)
706 })).appendTo(me.wrapper.find(".tax-table"));
707 }
708 });
709 },
710
711 set_totals: function() {
712 var me = this;
713 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
714 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
715 },
716
717 set_primary_action: function() {
718 var me = this;
719
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530720 if (this.frm.doc.docstatus==0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530721 this.page.set_primary_action(__("Pay"), function() {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530722 me.validate();
723 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530724 me.create_invoice();
725 me.make_payment();
Faris Ansari8908d192016-07-20 13:59:19 +0530726 }, "octicon octicon-credit-card");
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530727 }else if(this.frm.doc.docstatus == 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530728 this.page.set_primary_action(__("Print"), function() {
729 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530730 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530731 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530732 }else {
733 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530734 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530735
736 this.page.set_secondary_action(__("New"), function() {
737 me.save_previous_entry();
738 me.create_new();
Faris Ansari8908d192016-07-20 13:59:19 +0530739 }, "octicon octicon-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530740 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530741
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530742 print_dialog: function(){
743 var me = this;
744
745 msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
746 style="margin-right: 5px;">{0}</a>\
747 <a class="btn btn-default new_doc">{1}</a>', [
748 __('Print'), __('New')
749 ]));
750
751 $('.print_doc').click(function(){
752 html = frappe.render(me.print_template, me.frm.doc)
753 me.print_document(html)
754 })
755
756 $('.new_doc').click(function(){
757 msgprint.hide()
758 me.create_new();
759 })
760 },
761
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530762 print_document: function(html){
763 var w = window.open();
764 w.document.write(html);
765 w.document.close();
766 setTimeout(function(){
767 w.print();
768 w.close();
769 }, 1000)
770 },
771
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530772 submit_invoice: function(){
773 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530774 this.change_status();
775 if(this.frm.doc.docstatus == 1){
776 this.print_dialog()
777 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530778 },
779
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530780 change_status: function(){
781 if(this.frm.doc.docstatus == 0){
782 this.frm.doc.docstatus = 1;
783 this.update_invoice();
784 this.disable_input_field();
785 }
786 },
787
788 disable_input_field: function(){
789 var pointer_events = 'inherit'
790 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530791
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530792 if(this.frm.doc.docstatus == 1){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530793 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530794 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530795 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530796
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530797 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
798 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
799 this.set_primary_action();
800 },
801
802 create_invoice: function(){
803 var me = this;
804 var invoice_data = {}
805 this.si_docs = this.get_doc_from_localstorage();
806 if(this.name){
807 this.update_invoice()
808 }else{
809 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +0530810 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +0530811 this.frm.doc.posting_date = frappe.datetime.get_today();
812 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530813 invoice_data[this.name] = this.frm.doc
814 this.si_docs.push(invoice_data)
815 this.update_localstorage();
816 this.set_primary_action();
817 }
818 },
819
820 update_invoice: function(){
821 var me = this;
822 this.si_docs = this.get_doc_from_localstorage();
823 $.each(this.si_docs, function(index, data){
824 for(key in data){
825 if(key == me.name){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530826 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530827 me.update_localstorage();
828 }
829 }
830 })
831 },
832
833 update_localstorage: function(){
834 try{
835 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
836 }catch(e){
837 frappe.throw(__("LocalStorage is full , did not save"))
838 }
839 },
840
841 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530842 try{
843 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
844 }catch(e){
845 return []
846 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530847 },
848
849 set_interval_for_si_sync: function(){
850 var me = this;
851 setInterval(function(){
852 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530853 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530854 },
855
856 sync_sales_invoice: function(){
857 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530858 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530859
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530860 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530861 frappe.call({
862 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
863 args: {
864 doc_list: me.si_docs
865 },
866 callback: function(r){
867 if(r.message){
868 me.removed_items = r.message;
869 me.remove_doc_from_localstorage();
870 }
871 }
872 })
873 }
874 },
875
876 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530877 var invoices = [];
878 var index = 1;
879 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530880 if(docs){
881 invoices = $.map(docs, function(data){
882 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530883 if(data[key].docstatus == 1 && index < 50){
884 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530885 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530886 return data
887 }
888 }
889 });
890 }
891
892 return invoices
893 },
894
895 remove_doc_from_localstorage: function(){
896 var me = this;
897 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530898 this.new_si_docs = [];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530899 if(this.removed_items){
900 $.each(this.si_docs, function(index, data){
901 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530902 if(!in_list(me.removed_items, key)){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530903 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530904 }
905 }
906 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530907 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530908 this.update_localstorage();
909 }
910 },
911
912 validate: function(){
913 var me = this;
914 this.customer_validate();
915 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530916 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530917 },
918
919 item_validate: function(){
920 if(this.frm.doc.items.length == 0){
921 frappe.throw(__("Select items to save the invoice"))
922 }
923 },
Saurabh9a6c6662016-06-27 16:51:44 +0530924
925 validate_mode_of_payments: function(){
926 if (this.frm.doc.payments.length === 0){
927 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
928 }
929 },
930
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530931 validate_serial_no: function(){
932 var me = this;
933 var item_code = serial_no = '';
934 for (key in this.item_serial_no){
935 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530936 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530937 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530938
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530939 if(item_code && serial_no){
940 $.each(this.frm.doc.items, function(index, data){
941 if(data.item_code == item_code){
942 if(in_list(data.serial_no.split('\n'), serial_no)){
943 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
944 'serial_no': serial_no
945 })))
946 }
947 }
948 })
949 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530950
951 if(this.items[0].has_serial_no && serial_no == ""){
952 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
953 'item': this.items[0].item_code
954 })))
955 }
956 },
957
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530958 validate_serial_no_qty: function(args, item_code, field, value){
959 var me = this;
960 if (args.item_code == item_code && args.serial_no
961 && field == 'qty' && cint(value) != value) {
962 args.qty = 0.0;
963 this.refresh();
964 frappe.throw(__("Serial no item cannot be a fraction"))
965 }
966
967 if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
968 args.qty = 0.0;
969 args.serial_no = ''
970 this.refresh();
971 frappe.throw(__("Total nos of serial no is not equal to quantity."))
972 }
973 },
974
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530975 mandatory_batch_no: function(){
976 var me = this;
977 if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
978 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
979 'item': this.items[0].item_code
980 })))
981 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530982 },
983
984 apply_pricing_rule: function(){
985 var me = this;
986 $.each(this.frm.doc["items"], function(n, item) {
987 pricing_rule = me.get_pricing_rule(item)
988 me.validate_pricing_rule(pricing_rule)
989 if(pricing_rule.length){
990 item.margin_type = pricing_rule[0].margin_type;
991 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
992 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
993 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
994 me.apply_pricing_rule_on_item(item)
995 }
996 })
997 },
998
999 get_pricing_rule: function(item){
1000 var me = this;
1001 return $.grep(this.pricing_rules, function(data){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301002 if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
1003 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
1004 if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
1005 return me.validate_condition(data)
1006 }else{
1007 return true
1008 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301009 }
1010 }
1011 })
1012 },
1013
1014 validate_condition: function(data){
1015 //This method check condition based on applicable for
1016 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
1017 if(in_list(condition[1], condition[0])){
1018 return true
1019 }
1020 },
1021
1022 get_mapper_for_pricing_rule: function(data){
1023 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301024 'Customer': [data.customer, [this.frm.doc.customer]],
1025 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1026 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301027 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301028 }
1029 },
1030
1031 validate_pricing_rule: function(pricing_rule){
1032 //This method validate duplicate pricing rule
1033 var pricing_rule_name = '';
1034 var priority = 0;
1035 var pricing_rule_list = [];
1036 var priority_list = []
1037
1038 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301039
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301040 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301041 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301042 priority_list.push(data.priority)
1043 if(priority <= data.priority){
1044 priority = data.priority
1045 pricing_rule_list.push(data)
1046 }
1047 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301048
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301049 count = 0
1050 $.each(priority_list, function(index, value){
1051 if(value == priority){
1052 count++
1053 }
1054 })
1055
1056 if(priority == 0 || count > 1){
1057 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1058 'pricing_rule': pricing_rule_name
1059 })))
1060 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301061
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301062 return pricing_rule_list
1063 }
1064 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301065
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301066 validate_warehouse: function(){
1067 if(!this.items[0].default_warehouse){
1068 frappe.throw(__("Deafault warehouse is required for selected item"))
1069 }
1070 }
1071})