blob: 8eb2efd9ebf8fea8b1674ae0cbf5faf2596f9bf5 [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">\
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530135 <div class="col-xs-1">Sr</div>\
136 <div class="col-xs-3">Customer</div>\
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530137 <div class="col-xs-2 text-left">Status</div>\
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530138 <div class="col-xs-3 text-right">Paid Amount</div>\
139 <div class="col-xs-3 text-right">Grand Total</div>\
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530140 </div>')
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530141
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530142 $.each(this.si_docs, function(index, data){
143 for(key in data) {
144 $(frappe.render_template("pos_invoice_list", {
145 sr: index + 1,
146 name: key,
147 customer: data[key].customer,
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530148 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530149 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
150 data: me.get_doctype_status(data[key])
151 })).appendTo($(me.list_body));
152 }
153 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530154
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530155 $(this.list_body).find('.list-row').click(function() {
156 me.name = $(this).attr('invoice-name')
157 doc_data = me.get_invoice_doc(me.si_docs)
158 if(doc_data){
159 me.frm.doc = doc_data[0][me.name];
160 me.set_missing_values();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530161 me.refresh(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530162 me.disable_input_field();
163 me.list_dialog.hide();
164 }
165 })
166 }else{
167 $(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")}))
168 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530169 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530170
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530171 get_doctype_status: function(doc){
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530172 if(doc.docstatus == 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530173 return {status: "Draft", indicator: "red"}
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530174 }else if(doc.outstanding_amount == 0) {
175 return {status: "Paid", indicator: "green"}
176 }else {
177 return {status: "Submitted", indicator: "blue"}
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530178 }
179 },
180
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530181 set_missing_values: function(){
182 var me = this;
183 doc = JSON.parse(localStorage.getItem('doc'))
184 if(this.frm.doc.payments.length == 0){
185 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530186 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530187 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530188
189 if(this.frm.doc.customer){
190 this.party_field.$input.val(this.frm.doc.customer);
191 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530192
193 if(!this.frm.doc.write_off_account){
194 this.frm.doc.write_off_account = doc.write_off_account
195 }
196
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530197 if(!this.frm.doc.account_for_change_amount){
198 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530199 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530200 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530201
202 get_invoice_doc: function(si_docs){
203 var me = this;
204 this.si_docs = this.get_doc_from_localstorage();
205
206 return $.grep(this.si_docs, function(data){
207 for(key in data){
208 return key == me.name
209 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530210 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530211 },
212
213 get_data_from_server: function(callback){
214 var me = this;
215 frappe.call({
216 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
217 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530218 freeze_message: __("Master data syncing, it might take some time"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530219 callback: function(r){
220 window.items = r.message.items;
221 window.customers = r.message.customers;
222 window.pricing_rules = r.message.pricing_rules;
223 window.meta = r.message.meta;
224 window.print_template = r.message.print_template;
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530225 me.default_customer = r.message.default_customer || null;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530226 localStorage.setItem('doc', JSON.stringify(r.message.doc));
227 if(callback){
228 callback();
229 }
230 }
231 })
232 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530233
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530234 save_previous_entry : function(){
235 if(this.frm.doc.items.length > 0){
236 this.create_invoice()
237 }
238 },
239
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530240 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530241 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530242 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530243 this.name = '';
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530244 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530245 this.setup();
246 },
247
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530248 load_data: function(load_doc){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530249 this.items = window.items;
250 this.customers = window.customers;
251 this.pricing_rules = window.pricing_rules;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530252
253 if(load_doc) {
254 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
255 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530256
257 $.each(window.meta, function(i, data){
258 frappe.meta.sync(data)
259 })
260
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530261 this.print_template = frappe.render_template("print_template",
Rohit Waghchaure3c4cdd22016-09-19 00:17:32 +0530262 {content: window.print_template, title:"POS",
263 base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css})
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530264 },
265
266 setup: function(){
267 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
268 this.set_transaction_defaults("Customer");
269 this.make();
270 this.set_primary_action();
271 },
272
273 set_transaction_defaults: function(party) {
274 var me = this;
275 this.party = party;
276 this.price_list = (party == "Customer" ?
277 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
278 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
279 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
280 },
281
282 make: function() {
283 this.make_search();
284 this.make_customer();
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530285 this.make_item_list(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530286 this.make_discount_field()
287 },
288
289 make_search: function() {
290 var me = this;
291 this.search = frappe.ui.form.make_control({
292 df: {
293 "fieldtype": "Data",
294 "label": "Item",
295 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530296 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530297 },
298 parent: this.wrapper.find(".search-area"),
299 only_input: true,
300 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530301
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530302 this.search.make_input();
303 this.search.$input.on("keyup", function() {
304 setTimeout(function() {
305 me.items = me.get_items();
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530306 me.make_item_list(false);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530307 }, 1000);
308 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530309
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530310 this.party_field = frappe.ui.form.make_control({
311 df: {
312 "fieldtype": "Data",
313 "options": this.party,
314 "label": this.party,
315 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530316 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530317 },
318 parent: this.wrapper.find(".party-area"),
319 only_input: true,
320 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530321
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530322 this.party_field.make_input();
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530323 this.set_focus()
324 },
325
326 set_focus: function(){
327 if(this.default_customer){
328 this.search.$input.focus();
329 }else{
330 this.party_field.$input.focus();
331 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530332 },
333
334 make_customer: function() {
335 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530336
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530337 if(this.default_customer && !this.frm.doc.customer){
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530338 this.party_field.$input.val(this.default_customer);
339 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530340 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530341
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530342 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530343 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530344 source: function (request, response) {
345 me.customer_data = me.get_customers(request.term)
346 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530347 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530348 customer_group: data.customer_group, territory: data.territory}
349 }))
350 },
351 change: function(event, ui){
352 if(ui.item){
353 me.frm.doc.customer = ui.item.label;
354 me.frm.doc.customer_name = ui.item.customer_name;
355 me.frm.doc.customer_group = ui.item.customer_group;
356 me.frm.doc.territory = ui.item.territory;
357 }else{
358 me.frm.doc.customer = me.party_field.$input.val();
359 }
360 me.refresh();
361 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530362 }).on("focus", function(){
363 setTimeout(function() {
364 if(!me.party_field.$input.val()) {
365 me.party_field.$input.autocomplete( "search", " " );
366 }
367 }, 500);
368 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530369 },
370
371 get_customers: function(key){
372 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530373 key = key.toLowerCase().trim()
374 if(key){
375 return $.grep(this.customers, function(data) {
376 if(data.name.toLowerCase().match(key)
377 || data.customer_name.toLowerCase().match(key)
378 || (data.customer_group && data.customer_group.toLowerCase().match(key))){
379 return data
380 }
381 })
382 }else{
383 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
384 return customers.slice(0, 20)
385 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530386 },
387
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530388 make_item_list: function(index_search) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530389 var me = this;
390 if(!this.price_list) {
391 msgprint(__("Price List not found or disabled"));
392 return;
393 }
394
395 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530396
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530397 var $wrap = me.wrapper.find(".item-list");
398 me.wrapper.find(".item-list").empty();
399
400 if (this.items) {
401 $.each(this.items, function(index, obj) {
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530402 if(!index_search || index < 16){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530403 $(frappe.render_template("pos_item", {
404 item_code: obj.name,
405 item_price: format_currency(obj.price_list_rate, obj.currency),
406 item_name: obj.name===obj.item_name ? "" : obj.item_name,
407 item_image: obj.image ? "url('" + obj.image + "')" : null,
408 color: frappe.get_palette(obj.item_name),
409 abbr: frappe.get_abbr(obj.item_name)
410 })).tooltip().appendTo($wrap);
411 }
412 });
413 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530414
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530415 if(this.items.length == 1
416 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530417 this.search.$input.val("");
418 this.add_to_cart();
419 }
420
421 // if form is local then allow this function
422 $(me.wrapper).find("div.pos-item").on("click", function() {
423 me.customer_validate();
424 if(me.frm.doc.docstatus==0) {
425 me.items = me.get_items($(this).attr("data-item-code"))
426 me.add_to_cart();
427 }
428 });
429 },
430
431 get_items: function(item_code){
432 // To search item as per the key enter
433
434 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530435 this.item_serial_no = {};
436 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530437
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530438 if(item_code){
439 return $.grep(window.items, function(item){
440 if(item.item_code == item_code ){
441 return true
442 }
443 })
444 }
445
446 key = this.search.$input.val().toLowerCase();
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530447 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530448
449 if(key){
450 return $.grep(window.items, function(item){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530451 if(search_status){
452 if(in_list(item.batch_nos, me.search.$input.val())){
453 search_status = false;
454 return me.item_batch_no[item.item_code] = me.search.$input.val()
455 } else if(in_list(Object.keys(item.serial_nos), me.search.$input.val())) {
456 search_status = false;
457 me.item_serial_no[item.item_code] = [me.search.$input.val(), item.serial_nos[me.search.$input.val()]]
458 return true
459 } else if(item.barcode == me.search.$input.val()) {
460 search_status = false;
461 return item.barcode == me.search.$input.val();
462 } else if((item.item_code.toLowerCase().match(key)) ||
463 (item.item_name.toLowerCase().match(key)) || (item.item_group.toLowerCase().match(key))) {
464 return true
465 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530466 }
467 })
468 }else{
469 return window.items;
470 }
471 },
472
473 update_qty: function() {
474 var me = this;
475
476 $(this.wrapper).find(".pos-item-qty").on("change", function(){
477 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530478 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530479 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530480
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530481 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
482 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
483 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530484 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530485 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530486
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530487 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
488 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
489 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530490 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530491 })
492 },
493
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530494 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530495 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530496
497 $(this.wrapper).find(".pos-item-rate").on("change", function(){
498 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
499 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
500 })
501 },
502
503 update_qty_rate_against_item_code: function(item_code, field, value){
504 var me = this;
505 if(value < 0){
506 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530507 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530508
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530509 this.remove_item = []
510 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530511 if(d.serial_no){
512 me.validate_serial_no_qty(d, item_code, field, value)
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530513 }
514
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530515 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530516 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530517 d.amount = flt(d.rate) * flt(d.qty);
518 if(d.qty==0){
519 me.remove_item.push(d.idx)
520 }
521 }
522 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530523
524 if(field == 'qty'){
525 this.remove_zero_qty_item();
526 }
527
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530528 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530529 },
530
531 remove_zero_qty_item: function(){
532 var me = this;
533 idx = 0
534 this.items = []
535 idx = 0
536 $.each(this.frm.doc["items"] || [], function(i, d) {
537 if(!in_list(me.remove_item, d.idx)){
538 d.idx = idx;
539 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530540 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530541 }
542 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530543
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530544 this.frm.doc["items"] = this.items;
545 },
546
547 make_discount_field: function(){
548 var me = this;
549
550 this.wrapper.find('input.discount-percentage').on("change", function() {
551 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
552 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530553
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530554 if(me.frm.doc.apply_discount_on == 'Net Total'){
555 total = me.frm.doc.net_total
556 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530557
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530558 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
559 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
560 me.refresh();
561 });
562
563 this.wrapper.find('input.discount-amount').on("change", function() {
564 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
565 me.frm.doc.additional_discount_percentage = 0.0;
566 me.wrapper.find('input.discount-percentage').val(0);
567 me.refresh();
568 });
569 },
570
571 customer_validate: function(){
572 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530573 if(!this.frm.doc.customer){
574 frappe.throw(__("Please select customer"))
575 }
576 },
577
578 add_to_cart: function() {
579 var me = this;
580 var caught = false;
581 var no_of_items = me.wrapper.find(".pos-bill-item").length;
582
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530583 this.customer_validate();
584 this.mandatory_batch_no();
585 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530586 this.validate_warehouse();
587
588 if (no_of_items != 0) {
589 $.each(this.frm.doc["items"] || [], function(i, d) {
590 if (d.item_code == me.items[0].item_code) {
591 caught = true;
592 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530593 d.amount = flt(d.rate) * flt(d.qty);
594 if(me.item_serial_no[d.item_code]){
595 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
596 d.warehouse = me.item_serial_no[d.item_code][1]
597 }
598
599 if(me.item_batch_no.length){
600 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530601 }
602 }
603 });
604 }
605
606 // if item not found then add new item
607 if (!caught)
608 this.add_new_item_to_grid();
609
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530610 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530611 },
612
613 add_new_item_to_grid: function() {
614 var me = this;
615 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
616 this.child.item_code = this.items[0].item_code;
617 this.child.item_name = this.items[0].item_name;
618 this.child.stock_uom = this.items[0].stock_uom;
619 this.child.description = this.items[0].description;
620 this.child.qty = 1;
621 this.child.item_group = this.items[0].item_group;
622 this.child.cost_center = this.items[0].cost_center;
623 this.child.income_account = this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530624 this.child.warehouse = (this.item_serial_no[this.child.item_code]
625 ? this.item_serial_no[this.child.item_code][1] : this.items[0].default_warehouse);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530626 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
627 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
628 this.child.actual_qty = this.items[0].actual_qty;
629 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530630 this.child.batch_no = this.item_batch_no[this.child.item_code];
631 this.child.serial_no = (this.item_serial_no[this.child.item_code]
632 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaure525900c2016-09-07 15:01:08 +0530633 this.child.item_tax_rate = this.items[0].taxes;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530634 },
635
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530636 update_paid_amount_status: function(update_paid_amount){
637 if(this.name){
638 update_paid_amount = update_paid_amount ? false : true;
639 }
640
641 this.refresh(update_paid_amount);
642 },
643
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530644 refresh: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530645 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530646 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530647 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530648 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530649 this.set_primary_action();
650 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530651
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530652 refresh_fields: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530653 this.apply_pricing_rule();
654 this.discount_amount_applied = false;
655 this._calculate_taxes_and_totals();
656 this.calculate_discount_amount();
657 this.show_items_in_item_cart();
658 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530659 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530660 this.set_totals();
661 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530662
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530663 get_company_currency: function() {
664 return erpnext.get_currency(this.frm.doc.company);
665 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530666
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530667 show_item_wise_taxes: function(){
668 return null;
669 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530670
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530671 show_items_in_item_cart: function() {
672 var me = this;
673 var $items = this.wrapper.find(".items").empty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530674 $.each(this.frm.doc.items|| [], function(i, d) {
675 $(frappe.render_template("pos_bill_item", {
676 item_code: d.item_code,
677 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
678 qty: d.qty,
679 actual_qty: d.actual_qty,
680 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530681 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530682 amount: format_currency(d.amount, me.frm.doc.currency)
683 })).appendTo($items);
684 });
685
686 this.wrapper.find("input.pos-item-qty").on("focus", function() {
687 $(this).select();
688 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530689
690 this.wrapper.find("input.pos-item-rate").on("focus", function() {
691 $(this).select();
692 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530693 },
694
695 set_taxes: function(){
696 var me = this;
697 me.frm.doc.total_taxes_and_charges = 0.0
698
699 var taxes = this.frm.doc.taxes || [];
700 $(this.wrapper)
701 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
702 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530703
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530704 $.each(taxes, function(i, d) {
705 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
706 $(frappe.render_template("pos_tax_row", {
707 description: d.description,
708 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
709 me.frm.doc.currency)
710 })).appendTo(me.wrapper.find(".tax-table"));
711 }
712 });
713 },
714
715 set_totals: function() {
716 var me = this;
717 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
718 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
719 },
720
721 set_primary_action: function() {
722 var me = this;
723
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530724 if (this.frm.doc.docstatus==0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530725 this.page.set_primary_action(__("Pay"), function() {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530726 me.validate();
727 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530728 me.create_invoice();
729 me.make_payment();
Faris Ansari8908d192016-07-20 13:59:19 +0530730 }, "octicon octicon-credit-card");
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530731 }else if(this.frm.doc.docstatus == 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530732 this.page.set_primary_action(__("Print"), function() {
733 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530734 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530735 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530736 }else {
737 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530738 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530739
740 this.page.set_secondary_action(__("New"), function() {
741 me.save_previous_entry();
742 me.create_new();
Faris Ansari8908d192016-07-20 13:59:19 +0530743 }, "octicon octicon-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530744 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530745
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530746 print_dialog: function(){
747 var me = this;
748
749 msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
750 style="margin-right: 5px;">{0}</a>\
751 <a class="btn btn-default new_doc">{1}</a>', [
752 __('Print'), __('New')
753 ]));
754
755 $('.print_doc').click(function(){
756 html = frappe.render(me.print_template, me.frm.doc)
757 me.print_document(html)
758 })
759
760 $('.new_doc').click(function(){
761 msgprint.hide()
762 me.create_new();
763 })
764 },
765
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530766 print_document: function(html){
767 var w = window.open();
768 w.document.write(html);
769 w.document.close();
770 setTimeout(function(){
771 w.print();
772 w.close();
773 }, 1000)
774 },
775
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530776 submit_invoice: function(){
777 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530778 this.change_status();
779 if(this.frm.doc.docstatus == 1){
780 this.print_dialog()
781 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530782 },
783
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530784 change_status: function(){
785 if(this.frm.doc.docstatus == 0){
786 this.frm.doc.docstatus = 1;
787 this.update_invoice();
788 this.disable_input_field();
789 }
790 },
791
792 disable_input_field: function(){
793 var pointer_events = 'inherit'
794 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530795
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530796 if(this.frm.doc.docstatus == 1){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530797 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530798 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530799 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530800
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530801 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
802 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
803 this.set_primary_action();
804 },
805
806 create_invoice: function(){
807 var me = this;
808 var invoice_data = {}
809 this.si_docs = this.get_doc_from_localstorage();
810 if(this.name){
811 this.update_invoice()
812 }else{
813 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +0530814 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +0530815 this.frm.doc.posting_date = frappe.datetime.get_today();
816 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530817 invoice_data[this.name] = this.frm.doc
818 this.si_docs.push(invoice_data)
819 this.update_localstorage();
820 this.set_primary_action();
821 }
822 },
823
824 update_invoice: function(){
825 var me = this;
826 this.si_docs = this.get_doc_from_localstorage();
827 $.each(this.si_docs, function(index, data){
828 for(key in data){
829 if(key == me.name){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530830 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530831 me.update_localstorage();
832 }
833 }
834 })
835 },
836
837 update_localstorage: function(){
838 try{
839 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
840 }catch(e){
841 frappe.throw(__("LocalStorage is full , did not save"))
842 }
843 },
844
845 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530846 try{
847 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
848 }catch(e){
849 return []
850 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530851 },
852
853 set_interval_for_si_sync: function(){
854 var me = this;
855 setInterval(function(){
856 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530857 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530858 },
859
860 sync_sales_invoice: function(){
861 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530862 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530863
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530864 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530865 frappe.call({
866 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
867 args: {
868 doc_list: me.si_docs
869 },
870 callback: function(r){
871 if(r.message){
872 me.removed_items = r.message;
873 me.remove_doc_from_localstorage();
874 }
875 }
876 })
877 }
878 },
879
880 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530881 var invoices = [];
882 var index = 1;
883 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530884 if(docs){
885 invoices = $.map(docs, function(data){
886 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530887 if(data[key].docstatus == 1 && index < 50){
888 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530889 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530890 return data
891 }
892 }
893 });
894 }
895
896 return invoices
897 },
898
899 remove_doc_from_localstorage: function(){
900 var me = this;
901 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530902 this.new_si_docs = [];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530903 if(this.removed_items){
904 $.each(this.si_docs, function(index, data){
905 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530906 if(!in_list(me.removed_items, key)){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530907 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530908 }
909 }
910 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530911 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530912 this.update_localstorage();
913 }
914 },
915
916 validate: function(){
917 var me = this;
918 this.customer_validate();
919 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530920 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530921 },
922
923 item_validate: function(){
924 if(this.frm.doc.items.length == 0){
925 frappe.throw(__("Select items to save the invoice"))
926 }
927 },
Saurabh9a6c6662016-06-27 16:51:44 +0530928
929 validate_mode_of_payments: function(){
930 if (this.frm.doc.payments.length === 0){
931 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
932 }
933 },
934
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530935 validate_serial_no: function(){
936 var me = this;
937 var item_code = serial_no = '';
938 for (key in this.item_serial_no){
939 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530940 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530941 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530942
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530943 if(item_code && serial_no){
944 $.each(this.frm.doc.items, function(index, data){
945 if(data.item_code == item_code){
946 if(in_list(data.serial_no.split('\n'), serial_no)){
947 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
948 'serial_no': serial_no
949 })))
950 }
951 }
952 })
953 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530954
955 if(this.items[0].has_serial_no && serial_no == ""){
956 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
957 'item': this.items[0].item_code
958 })))
959 }
960 },
961
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530962 validate_serial_no_qty: function(args, item_code, field, value){
963 var me = this;
964 if (args.item_code == item_code && args.serial_no
965 && field == 'qty' && cint(value) != value) {
966 args.qty = 0.0;
967 this.refresh();
968 frappe.throw(__("Serial no item cannot be a fraction"))
969 }
970
971 if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
972 args.qty = 0.0;
973 args.serial_no = ''
974 this.refresh();
975 frappe.throw(__("Total nos of serial no is not equal to quantity."))
976 }
977 },
978
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530979 mandatory_batch_no: function(){
980 var me = this;
981 if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
982 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
983 'item': this.items[0].item_code
984 })))
985 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530986 },
987
988 apply_pricing_rule: function(){
989 var me = this;
990 $.each(this.frm.doc["items"], function(n, item) {
991 pricing_rule = me.get_pricing_rule(item)
992 me.validate_pricing_rule(pricing_rule)
993 if(pricing_rule.length){
994 item.margin_type = pricing_rule[0].margin_type;
995 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
996 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
997 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
998 me.apply_pricing_rule_on_item(item)
999 }
1000 })
1001 },
1002
1003 get_pricing_rule: function(item){
1004 var me = this;
1005 return $.grep(this.pricing_rules, function(data){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301006 if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
1007 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
1008 if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
1009 return me.validate_condition(data)
1010 }else{
1011 return true
1012 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301013 }
1014 }
1015 })
1016 },
1017
1018 validate_condition: function(data){
1019 //This method check condition based on applicable for
1020 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
1021 if(in_list(condition[1], condition[0])){
1022 return true
1023 }
1024 },
1025
1026 get_mapper_for_pricing_rule: function(data){
1027 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301028 'Customer': [data.customer, [this.frm.doc.customer]],
1029 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1030 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301031 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301032 }
1033 },
1034
1035 validate_pricing_rule: function(pricing_rule){
1036 //This method validate duplicate pricing rule
1037 var pricing_rule_name = '';
1038 var priority = 0;
1039 var pricing_rule_list = [];
1040 var priority_list = []
1041
1042 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301043
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301044 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301045 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301046 priority_list.push(data.priority)
1047 if(priority <= data.priority){
1048 priority = data.priority
1049 pricing_rule_list.push(data)
1050 }
1051 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301052
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301053 count = 0
1054 $.each(priority_list, function(index, value){
1055 if(value == priority){
1056 count++
1057 }
1058 })
1059
1060 if(priority == 0 || count > 1){
1061 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1062 'pricing_rule': pricing_rule_name
1063 })))
1064 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301065
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301066 return pricing_rule_list
1067 }
1068 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301069
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301070 validate_warehouse: function(){
1071 if(!this.items[0].default_warehouse){
1072 frappe.throw(__("Deafault warehouse is required for selected item"))
1073 }
1074 }
1075})