blob: 8da1332ff1d13bc30fe7ddf644f5716477cad131 [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 Waghchaure3c4cdd22016-09-19 00:17:32 +0530258 {content: window.print_template, title:"POS",
259 base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css})
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530260 },
261
262 setup: function(){
263 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
264 this.set_transaction_defaults("Customer");
265 this.make();
266 this.set_primary_action();
267 },
268
269 set_transaction_defaults: function(party) {
270 var me = this;
271 this.party = party;
272 this.price_list = (party == "Customer" ?
273 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
274 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
275 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
276 },
277
278 make: function() {
279 this.make_search();
280 this.make_customer();
281 this.make_item_list();
282 this.make_discount_field()
283 },
284
285 make_search: function() {
286 var me = this;
287 this.search = frappe.ui.form.make_control({
288 df: {
289 "fieldtype": "Data",
290 "label": "Item",
291 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530292 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530293 },
294 parent: this.wrapper.find(".search-area"),
295 only_input: true,
296 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530297
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530298 this.search.make_input();
299 this.search.$input.on("keyup", function() {
300 setTimeout(function() {
301 me.items = me.get_items();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530302 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530303 }, 1000);
304 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530305
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530306 this.party_field = frappe.ui.form.make_control({
307 df: {
308 "fieldtype": "Data",
309 "options": this.party,
310 "label": this.party,
311 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530312 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530313 },
314 parent: this.wrapper.find(".party-area"),
315 only_input: true,
316 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530317
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530318 this.party_field.make_input();
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530319 this.set_focus()
320 },
321
322 set_focus: function(){
323 if(this.default_customer){
324 this.search.$input.focus();
325 }else{
326 this.party_field.$input.focus();
327 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530328 },
329
330 make_customer: function() {
331 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530332
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530333 if(this.default_customer && !this.frm.doc.customer){
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530334 this.party_field.$input.val(this.default_customer);
335 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530336 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530337
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530338 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530339 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530340 source: function (request, response) {
341 me.customer_data = me.get_customers(request.term)
342 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530343 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530344 customer_group: data.customer_group, territory: data.territory}
345 }))
346 },
347 change: function(event, ui){
348 if(ui.item){
349 me.frm.doc.customer = ui.item.label;
350 me.frm.doc.customer_name = ui.item.customer_name;
351 me.frm.doc.customer_group = ui.item.customer_group;
352 me.frm.doc.territory = ui.item.territory;
353 }else{
354 me.frm.doc.customer = me.party_field.$input.val();
355 }
356 me.refresh();
357 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530358 }).on("focus", function(){
359 setTimeout(function() {
360 if(!me.party_field.$input.val()) {
361 me.party_field.$input.autocomplete( "search", " " );
362 }
363 }, 500);
364 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530365 },
366
367 get_customers: function(key){
368 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530369 key = key.toLowerCase().trim()
370 if(key){
371 return $.grep(this.customers, function(data) {
372 if(data.name.toLowerCase().match(key)
373 || data.customer_name.toLowerCase().match(key)
374 || (data.customer_group && data.customer_group.toLowerCase().match(key))){
375 return data
376 }
377 })
378 }else{
379 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
380 return customers.slice(0, 20)
381 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530382 },
383
384 make_item_list: function() {
385 var me = this;
386 if(!this.price_list) {
387 msgprint(__("Price List not found or disabled"));
388 return;
389 }
390
391 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530392
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530393 var $wrap = me.wrapper.find(".item-list");
394 me.wrapper.find(".item-list").empty();
395
396 if (this.items) {
397 $.each(this.items, function(index, obj) {
398 if(index < 16){
399 $(frappe.render_template("pos_item", {
400 item_code: obj.name,
401 item_price: format_currency(obj.price_list_rate, obj.currency),
402 item_name: obj.name===obj.item_name ? "" : obj.item_name,
403 item_image: obj.image ? "url('" + obj.image + "')" : null,
404 color: frappe.get_palette(obj.item_name),
405 abbr: frappe.get_abbr(obj.item_name)
406 })).tooltip().appendTo($wrap);
407 }
408 });
409 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530410
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530411 if(this.items.length == 1
412 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530413 this.search.$input.val("");
414 this.add_to_cart();
415 }
416
417 // if form is local then allow this function
418 $(me.wrapper).find("div.pos-item").on("click", function() {
419 me.customer_validate();
420 if(me.frm.doc.docstatus==0) {
421 me.items = me.get_items($(this).attr("data-item-code"))
422 me.add_to_cart();
423 }
424 });
425 },
426
427 get_items: function(item_code){
428 // To search item as per the key enter
429
430 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530431 this.item_serial_no = {};
432 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530433
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530434 if(item_code){
435 return $.grep(window.items, function(item){
436 if(item.item_code == item_code ){
437 return true
438 }
439 })
440 }
441
442 key = this.search.$input.val().toLowerCase();
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530443 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530444
445 if(key){
446 return $.grep(window.items, function(item){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530447 if(search_status){
448 if(in_list(item.batch_nos, me.search.$input.val())){
449 search_status = false;
450 return me.item_batch_no[item.item_code] = me.search.$input.val()
451 } else if(in_list(Object.keys(item.serial_nos), me.search.$input.val())) {
452 search_status = false;
453 me.item_serial_no[item.item_code] = [me.search.$input.val(), item.serial_nos[me.search.$input.val()]]
454 return true
455 } else if(item.barcode == me.search.$input.val()) {
456 search_status = false;
457 return item.barcode == me.search.$input.val();
458 } else if((item.item_code.toLowerCase().match(key)) ||
459 (item.item_name.toLowerCase().match(key)) || (item.item_group.toLowerCase().match(key))) {
460 return true
461 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530462 }
463 })
464 }else{
465 return window.items;
466 }
467 },
468
469 update_qty: function() {
470 var me = this;
471
472 $(this.wrapper).find(".pos-item-qty").on("change", function(){
473 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530474 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530475 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530476
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530477 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
478 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
479 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530480 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530481 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530482
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530483 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
484 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
485 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530486 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530487 })
488 },
489
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530490 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530491 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530492
493 $(this.wrapper).find(".pos-item-rate").on("change", function(){
494 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
495 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
496 })
497 },
498
499 update_qty_rate_against_item_code: function(item_code, field, value){
500 var me = this;
501 if(value < 0){
502 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530503 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530504
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530505 this.remove_item = []
506 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530507 if(d.serial_no){
508 me.validate_serial_no_qty(d, item_code, field, value)
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530509 }
510
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530511 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530512 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530513 d.amount = flt(d.rate) * flt(d.qty);
514 if(d.qty==0){
515 me.remove_item.push(d.idx)
516 }
517 }
518 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530519
520 if(field == 'qty'){
521 this.remove_zero_qty_item();
522 }
523
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530524 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530525 },
526
527 remove_zero_qty_item: function(){
528 var me = this;
529 idx = 0
530 this.items = []
531 idx = 0
532 $.each(this.frm.doc["items"] || [], function(i, d) {
533 if(!in_list(me.remove_item, d.idx)){
534 d.idx = idx;
535 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530536 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530537 }
538 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530539
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530540 this.frm.doc["items"] = this.items;
541 },
542
543 make_discount_field: function(){
544 var me = this;
545
546 this.wrapper.find('input.discount-percentage').on("change", function() {
547 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
548 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530549
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530550 if(me.frm.doc.apply_discount_on == 'Net Total'){
551 total = me.frm.doc.net_total
552 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530553
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530554 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
555 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
556 me.refresh();
557 });
558
559 this.wrapper.find('input.discount-amount').on("change", function() {
560 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
561 me.frm.doc.additional_discount_percentage = 0.0;
562 me.wrapper.find('input.discount-percentage').val(0);
563 me.refresh();
564 });
565 },
566
567 customer_validate: function(){
568 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530569 if(!this.frm.doc.customer){
570 frappe.throw(__("Please select customer"))
571 }
572 },
573
574 add_to_cart: function() {
575 var me = this;
576 var caught = false;
577 var no_of_items = me.wrapper.find(".pos-bill-item").length;
578
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530579 this.customer_validate();
580 this.mandatory_batch_no();
581 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530582 this.validate_warehouse();
583
584 if (no_of_items != 0) {
585 $.each(this.frm.doc["items"] || [], function(i, d) {
586 if (d.item_code == me.items[0].item_code) {
587 caught = true;
588 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530589 d.amount = flt(d.rate) * flt(d.qty);
590 if(me.item_serial_no[d.item_code]){
591 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
592 d.warehouse = me.item_serial_no[d.item_code][1]
593 }
594
595 if(me.item_batch_no.length){
596 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530597 }
598 }
599 });
600 }
601
602 // if item not found then add new item
603 if (!caught)
604 this.add_new_item_to_grid();
605
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530606 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530607 },
608
609 add_new_item_to_grid: function() {
610 var me = this;
611 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
612 this.child.item_code = this.items[0].item_code;
613 this.child.item_name = this.items[0].item_name;
614 this.child.stock_uom = this.items[0].stock_uom;
615 this.child.description = this.items[0].description;
616 this.child.qty = 1;
617 this.child.item_group = this.items[0].item_group;
618 this.child.cost_center = this.items[0].cost_center;
619 this.child.income_account = this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530620 this.child.warehouse = (this.item_serial_no[this.child.item_code]
621 ? this.item_serial_no[this.child.item_code][1] : this.items[0].default_warehouse);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530622 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
623 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
624 this.child.actual_qty = this.items[0].actual_qty;
625 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530626 this.child.batch_no = this.item_batch_no[this.child.item_code];
627 this.child.serial_no = (this.item_serial_no[this.child.item_code]
628 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaure525900c2016-09-07 15:01:08 +0530629 this.child.item_tax_rate = this.items[0].taxes;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530630 },
631
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530632 update_paid_amount_status: function(update_paid_amount){
633 if(this.name){
634 update_paid_amount = update_paid_amount ? false : true;
635 }
636
637 this.refresh(update_paid_amount);
638 },
639
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530640 refresh: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530641 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530642 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530643 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530644 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530645 this.set_primary_action();
646 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530647
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530648 refresh_fields: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530649 this.apply_pricing_rule();
650 this.discount_amount_applied = false;
651 this._calculate_taxes_and_totals();
652 this.calculate_discount_amount();
653 this.show_items_in_item_cart();
654 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530655 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530656 this.set_totals();
657 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530658
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530659 get_company_currency: function() {
660 return erpnext.get_currency(this.frm.doc.company);
661 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530662
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530663 show_item_wise_taxes: function(){
664 return null;
665 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530666
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530667 show_items_in_item_cart: function() {
668 var me = this;
669 var $items = this.wrapper.find(".items").empty();
670 me.frm.doc.net_total = 0.0
671 $.each(this.frm.doc.items|| [], function(i, d) {
672 $(frappe.render_template("pos_bill_item", {
673 item_code: d.item_code,
674 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
675 qty: d.qty,
676 actual_qty: d.actual_qty,
677 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530678 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530679 amount: format_currency(d.amount, me.frm.doc.currency)
680 })).appendTo($items);
681 });
682
683 this.wrapper.find("input.pos-item-qty").on("focus", function() {
684 $(this).select();
685 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530686
687 this.wrapper.find("input.pos-item-rate").on("focus", function() {
688 $(this).select();
689 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530690 },
691
692 set_taxes: function(){
693 var me = this;
694 me.frm.doc.total_taxes_and_charges = 0.0
695
696 var taxes = this.frm.doc.taxes || [];
697 $(this.wrapper)
698 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
699 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530700
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530701 $.each(taxes, function(i, d) {
702 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
703 $(frappe.render_template("pos_tax_row", {
704 description: d.description,
705 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
706 me.frm.doc.currency)
707 })).appendTo(me.wrapper.find(".tax-table"));
708 }
709 });
710 },
711
712 set_totals: function() {
713 var me = this;
714 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
715 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
716 },
717
718 set_primary_action: function() {
719 var me = this;
720
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530721 if (this.frm.doc.docstatus==0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530722 this.page.set_primary_action(__("Pay"), function() {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530723 me.validate();
724 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530725 me.create_invoice();
726 me.make_payment();
Faris Ansari8908d192016-07-20 13:59:19 +0530727 }, "octicon octicon-credit-card");
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530728 }else if(this.frm.doc.docstatus == 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530729 this.page.set_primary_action(__("Print"), function() {
730 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530731 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530732 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530733 }else {
734 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530735 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530736
737 this.page.set_secondary_action(__("New"), function() {
738 me.save_previous_entry();
739 me.create_new();
Faris Ansari8908d192016-07-20 13:59:19 +0530740 }, "octicon octicon-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530741 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530742
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530743 print_dialog: function(){
744 var me = this;
745
746 msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
747 style="margin-right: 5px;">{0}</a>\
748 <a class="btn btn-default new_doc">{1}</a>', [
749 __('Print'), __('New')
750 ]));
751
752 $('.print_doc').click(function(){
753 html = frappe.render(me.print_template, me.frm.doc)
754 me.print_document(html)
755 })
756
757 $('.new_doc').click(function(){
758 msgprint.hide()
759 me.create_new();
760 })
761 },
762
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530763 print_document: function(html){
764 var w = window.open();
765 w.document.write(html);
766 w.document.close();
767 setTimeout(function(){
768 w.print();
769 w.close();
770 }, 1000)
771 },
772
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530773 submit_invoice: function(){
774 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530775 this.change_status();
776 if(this.frm.doc.docstatus == 1){
777 this.print_dialog()
778 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530779 },
780
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530781 change_status: function(){
782 if(this.frm.doc.docstatus == 0){
783 this.frm.doc.docstatus = 1;
784 this.update_invoice();
785 this.disable_input_field();
786 }
787 },
788
789 disable_input_field: function(){
790 var pointer_events = 'inherit'
791 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530792
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530793 if(this.frm.doc.docstatus == 1){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530794 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530795 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530796 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530797
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530798 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
799 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
800 this.set_primary_action();
801 },
802
803 create_invoice: function(){
804 var me = this;
805 var invoice_data = {}
806 this.si_docs = this.get_doc_from_localstorage();
807 if(this.name){
808 this.update_invoice()
809 }else{
810 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +0530811 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +0530812 this.frm.doc.posting_date = frappe.datetime.get_today();
813 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530814 invoice_data[this.name] = this.frm.doc
815 this.si_docs.push(invoice_data)
816 this.update_localstorage();
817 this.set_primary_action();
818 }
819 },
820
821 update_invoice: function(){
822 var me = this;
823 this.si_docs = this.get_doc_from_localstorage();
824 $.each(this.si_docs, function(index, data){
825 for(key in data){
826 if(key == me.name){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530827 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530828 me.update_localstorage();
829 }
830 }
831 })
832 },
833
834 update_localstorage: function(){
835 try{
836 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
837 }catch(e){
838 frappe.throw(__("LocalStorage is full , did not save"))
839 }
840 },
841
842 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530843 try{
844 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
845 }catch(e){
846 return []
847 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530848 },
849
850 set_interval_for_si_sync: function(){
851 var me = this;
852 setInterval(function(){
853 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530854 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530855 },
856
857 sync_sales_invoice: function(){
858 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530859 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530860
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530861 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530862 frappe.call({
863 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
864 args: {
865 doc_list: me.si_docs
866 },
867 callback: function(r){
868 if(r.message){
869 me.removed_items = r.message;
870 me.remove_doc_from_localstorage();
871 }
872 }
873 })
874 }
875 },
876
877 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530878 var invoices = [];
879 var index = 1;
880 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530881 if(docs){
882 invoices = $.map(docs, function(data){
883 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530884 if(data[key].docstatus == 1 && index < 50){
885 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530886 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530887 return data
888 }
889 }
890 });
891 }
892
893 return invoices
894 },
895
896 remove_doc_from_localstorage: function(){
897 var me = this;
898 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530899 this.new_si_docs = [];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530900 if(this.removed_items){
901 $.each(this.si_docs, function(index, data){
902 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530903 if(!in_list(me.removed_items, key)){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530904 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530905 }
906 }
907 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530908 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530909 this.update_localstorage();
910 }
911 },
912
913 validate: function(){
914 var me = this;
915 this.customer_validate();
916 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530917 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530918 },
919
920 item_validate: function(){
921 if(this.frm.doc.items.length == 0){
922 frappe.throw(__("Select items to save the invoice"))
923 }
924 },
Saurabh9a6c6662016-06-27 16:51:44 +0530925
926 validate_mode_of_payments: function(){
927 if (this.frm.doc.payments.length === 0){
928 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
929 }
930 },
931
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530932 validate_serial_no: function(){
933 var me = this;
934 var item_code = serial_no = '';
935 for (key in this.item_serial_no){
936 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530937 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530938 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530939
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530940 if(item_code && serial_no){
941 $.each(this.frm.doc.items, function(index, data){
942 if(data.item_code == item_code){
943 if(in_list(data.serial_no.split('\n'), serial_no)){
944 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
945 'serial_no': serial_no
946 })))
947 }
948 }
949 })
950 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530951
952 if(this.items[0].has_serial_no && serial_no == ""){
953 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
954 'item': this.items[0].item_code
955 })))
956 }
957 },
958
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530959 validate_serial_no_qty: function(args, item_code, field, value){
960 var me = this;
961 if (args.item_code == item_code && args.serial_no
962 && field == 'qty' && cint(value) != value) {
963 args.qty = 0.0;
964 this.refresh();
965 frappe.throw(__("Serial no item cannot be a fraction"))
966 }
967
968 if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
969 args.qty = 0.0;
970 args.serial_no = ''
971 this.refresh();
972 frappe.throw(__("Total nos of serial no is not equal to quantity."))
973 }
974 },
975
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530976 mandatory_batch_no: function(){
977 var me = this;
978 if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
979 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
980 'item': this.items[0].item_code
981 })))
982 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530983 },
984
985 apply_pricing_rule: function(){
986 var me = this;
987 $.each(this.frm.doc["items"], function(n, item) {
988 pricing_rule = me.get_pricing_rule(item)
989 me.validate_pricing_rule(pricing_rule)
990 if(pricing_rule.length){
991 item.margin_type = pricing_rule[0].margin_type;
992 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
993 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
994 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
995 me.apply_pricing_rule_on_item(item)
996 }
997 })
998 },
999
1000 get_pricing_rule: function(item){
1001 var me = this;
1002 return $.grep(this.pricing_rules, function(data){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301003 if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
1004 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
1005 if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
1006 return me.validate_condition(data)
1007 }else{
1008 return true
1009 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301010 }
1011 }
1012 })
1013 },
1014
1015 validate_condition: function(data){
1016 //This method check condition based on applicable for
1017 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
1018 if(in_list(condition[1], condition[0])){
1019 return true
1020 }
1021 },
1022
1023 get_mapper_for_pricing_rule: function(data){
1024 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301025 'Customer': [data.customer, [this.frm.doc.customer]],
1026 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1027 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301028 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301029 }
1030 },
1031
1032 validate_pricing_rule: function(pricing_rule){
1033 //This method validate duplicate pricing rule
1034 var pricing_rule_name = '';
1035 var priority = 0;
1036 var pricing_rule_list = [];
1037 var priority_list = []
1038
1039 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301040
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301041 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301042 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301043 priority_list.push(data.priority)
1044 if(priority <= data.priority){
1045 priority = data.priority
1046 pricing_rule_list.push(data)
1047 }
1048 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301049
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301050 count = 0
1051 $.each(priority_list, function(index, value){
1052 if(value == priority){
1053 count++
1054 }
1055 })
1056
1057 if(priority == 0 || count > 1){
1058 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1059 'pricing_rule': pricing_rule_name
1060 })))
1061 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301062
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301063 return pricing_rule_list
1064 }
1065 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301066
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301067 validate_warehouse: function(){
1068 if(!this.items[0].default_warehouse){
1069 frappe.throw(__("Deafault warehouse is required for selected item"))
1070 }
1071 }
1072})