blob: cfc95821e2727efc91c3e405d4219afd073d17db [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}
19
20
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053021erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
Rohit Waghchauree0934d12016-05-11 15:04:57 +053022 init: function(wrapper){
Rohit Waghchauree0934d12016-05-11 15:04:57 +053023 this.page = wrapper.page;
24 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053025 this.set_indicator();
26 this.onload();
27 this.make_menu_list();
28 this.set_interval_for_si_sync();
29 this.si_docs = this.get_doc_from_localstorage();
30 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053031
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053032 beforeunload: function(e){
33 if(this.connection_status == false && frappe.get_route()[0] == "pos"){
34 e = e || window.event;
35
36 // For IE and Firefox prior to version 4
37 if (e) {
38 e.returnValue = __("You are in offline mode. You will not be able to reload until you have network.");
39 return
40 }
41
42 // For Safari
43 return __("You are in offline mode. You will not be able to reload until you have network.");
44 }
45 },
46
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053047 check_internet_connection: function(){
48 var me = this;
49 //Check Internet connection after every 30 seconds
50 setInterval(function(){
51 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053052 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053053 },
54
55 set_indicator: function(){
56 var me = this;
57 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053058 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053059 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053060 frappe.call({
61 method:"frappe.handler.ping",
62 callback: function(r){
63 if(r.message){
64 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053065 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053066 }
67 }
68 })
69 },
70
71 onload: function(){
72 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053073 this.get_data_from_server(function(){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053074 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053075 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053076
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053077 this.check_internet_connection();
78 },
79
80 make_menu_list: function(){
81 var me = this;
82
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053083 this.page.add_menu_item(__("New Sales Invoice"), function() {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053084 me.save_previous_entry();
85 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053086 })
87
88 this.page.add_menu_item(__("View Offline Records"), function(){
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053089 me.show_unsync_invoice_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053090 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053091
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053092 this.page.add_menu_item(__("Sync Master Data"), function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +053093 me.get_data_from_server(function(){
Rohit Waghchaureea278b52016-07-21 23:28:04 +053094 me.load_data(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053095 me.make_customer();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +053096 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +053097 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +053098 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053099 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530100
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530101 this.page.add_menu_item(__("Sync Offline Invoices"), function(){
102 me.sync_sales_invoice()
103 });
104
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530105 this.page.add_menu_item(__("POS Profile"), function() {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530106 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530107 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530108 },
109
110 show_unsync_invoice_list: function(){
111 var me = this;
112 this.si_docs = this.get_doc_from_localstorage();
113
114 this.list_dialog = new frappe.ui.Dialog({
115 title: 'Invoice List'
116 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530117
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530118 this.list_dialog.show();
119 this.list_body = this.list_dialog.body;
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530120 if(this.si_docs.length > 0){
121 $(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530122 <div class="col-xs-1">Sr</div>\
123 <div class="col-xs-3">Customer</div>\
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530124 <div class="col-xs-2 text-left">Status</div>\
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530125 <div class="col-xs-3 text-right">Paid Amount</div>\
126 <div class="col-xs-3 text-right">Grand Total</div>\
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530127 </div>')
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530128
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530129 $.each(this.si_docs, function(index, data){
130 for(key in data) {
131 $(frappe.render_template("pos_invoice_list", {
132 sr: index + 1,
133 name: key,
134 customer: data[key].customer,
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530135 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530136 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
137 data: me.get_doctype_status(data[key])
138 })).appendTo($(me.list_body));
139 }
140 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530141
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530142 $(this.list_body).find('.list-row').click(function() {
143 me.name = $(this).attr('invoice-name')
144 doc_data = me.get_invoice_doc(me.si_docs)
145 if(doc_data){
146 me.frm.doc = doc_data[0][me.name];
147 me.set_missing_values();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530148 me.refresh(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530149 me.disable_input_field();
150 me.list_dialog.hide();
151 }
152 })
153 }else{
154 $(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")}))
155 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530156 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530157
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530158 get_doctype_status: function(doc){
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530159 if(doc.docstatus == 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530160 return {status: "Draft", indicator: "red"}
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530161 }else if(doc.outstanding_amount == 0) {
162 return {status: "Paid", indicator: "green"}
163 }else {
164 return {status: "Submitted", indicator: "blue"}
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530165 }
166 },
167
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530168 set_missing_values: function(){
169 var me = this;
170 doc = JSON.parse(localStorage.getItem('doc'))
171 if(this.frm.doc.payments.length == 0){
172 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530173 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530174 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530175
176 if(this.frm.doc.customer){
177 this.party_field.$input.val(this.frm.doc.customer);
178 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530179
180 if(!this.frm.doc.write_off_account){
181 this.frm.doc.write_off_account = doc.write_off_account
182 }
183
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530184 if(!this.frm.doc.account_for_change_amount){
185 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530186 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530187 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530188
189 get_invoice_doc: function(si_docs){
190 var me = this;
191 this.si_docs = this.get_doc_from_localstorage();
192
193 return $.grep(this.si_docs, function(data){
194 for(key in data){
195 return key == me.name
196 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530197 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530198 },
199
200 get_data_from_server: function(callback){
201 var me = this;
202 frappe.call({
203 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
204 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530205 freeze_message: __("Master data syncing, it might take some time"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530206 callback: function(r){
207 window.items = r.message.items;
208 window.customers = r.message.customers;
209 window.pricing_rules = r.message.pricing_rules;
210 window.meta = r.message.meta;
211 window.print_template = r.message.print_template;
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530212 me.default_customer = r.message.default_customer || null;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530213 localStorage.setItem('doc', JSON.stringify(r.message.doc));
214 if(callback){
215 callback();
216 }
217 }
218 })
219 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530220
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530221 save_previous_entry : function(){
222 if(this.frm.doc.items.length > 0){
223 this.create_invoice()
224 }
225 },
226
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530227 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530228 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530229 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530230 this.name = '';
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530231 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530232 this.setup();
233 },
234
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530235 load_data: function(load_doc){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530236 this.items = window.items;
237 this.customers = window.customers;
238 this.pricing_rules = window.pricing_rules;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530239
240 if(load_doc) {
241 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
242 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530243
244 $.each(window.meta, function(i, data){
245 frappe.meta.sync(data)
246 })
247
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530248 this.print_template = frappe.render_template("print_template",
Rohit Waghchaure3c4cdd22016-09-19 00:17:32 +0530249 {content: window.print_template, title:"POS",
250 base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css})
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530251 },
252
253 setup: function(){
254 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
255 this.set_transaction_defaults("Customer");
256 this.make();
257 this.set_primary_action();
258 },
259
260 set_transaction_defaults: function(party) {
261 var me = this;
262 this.party = party;
263 this.price_list = (party == "Customer" ?
264 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
265 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
266 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
267 },
268
269 make: function() {
270 this.make_search();
271 this.make_customer();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530272 this.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530273 this.make_discount_field()
274 },
275
276 make_search: function() {
277 var me = this;
278 this.search = frappe.ui.form.make_control({
279 df: {
280 "fieldtype": "Data",
281 "label": "Item",
282 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530283 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530284 },
285 parent: this.wrapper.find(".search-area"),
286 only_input: true,
287 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530288
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530289 this.search.make_input();
290 this.search.$input.on("keyup", function() {
291 setTimeout(function() {
292 me.items = me.get_items();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530293 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530294 }, 1000);
295 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530296
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530297 this.party_field = frappe.ui.form.make_control({
298 df: {
299 "fieldtype": "Data",
300 "options": this.party,
301 "label": this.party,
302 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530303 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530304 },
305 parent: this.wrapper.find(".party-area"),
306 only_input: true,
307 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530308
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530309 this.party_field.make_input();
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530310 this.set_focus()
311 },
312
313 set_focus: function(){
314 if(this.default_customer){
315 this.search.$input.focus();
316 }else{
317 this.party_field.$input.focus();
318 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530319 },
320
321 make_customer: function() {
322 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530323
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530324 if(this.default_customer && !this.frm.doc.customer){
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530325 this.party_field.$input.val(this.default_customer);
326 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530327 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530328
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530329 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530330 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530331 source: function (request, response) {
332 me.customer_data = me.get_customers(request.term)
333 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530334 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530335 customer_group: data.customer_group, territory: data.territory}
336 }))
337 },
338 change: function(event, ui){
339 if(ui.item){
340 me.frm.doc.customer = ui.item.label;
341 me.frm.doc.customer_name = ui.item.customer_name;
342 me.frm.doc.customer_group = ui.item.customer_group;
343 me.frm.doc.territory = ui.item.territory;
344 }else{
345 me.frm.doc.customer = me.party_field.$input.val();
346 }
347 me.refresh();
348 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530349 }).on("focus", function(){
350 setTimeout(function() {
351 if(!me.party_field.$input.val()) {
352 me.party_field.$input.autocomplete( "search", " " );
353 }
354 }, 500);
355 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530356 },
357
358 get_customers: function(key){
359 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530360 key = key.toLowerCase().trim()
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530361 var re = new RegExp('%', 'g');
362 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'))
363
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530364 if(key){
365 return $.grep(this.customers, function(data) {
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530366 if(reg.test(data.name.toLowerCase())
367 || reg.test(data.customer_name.toLowerCase())
368 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530369 return data
370 }
371 })
372 }else{
373 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
374 return customers.slice(0, 20)
375 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530376 },
377
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530378 make_item_list: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530379 var me = this;
380 if(!this.price_list) {
381 msgprint(__("Price List not found or disabled"));
382 return;
383 }
384
385 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530386
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530387 var $wrap = me.wrapper.find(".item-list");
388 me.wrapper.find(".item-list").empty();
389
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530390 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530391 $.each(this.items, function(index, obj) {
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530392 if(index < 30){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530393 $(frappe.render_template("pos_item", {
394 item_code: obj.name,
Rohit Waghchaure53bb6872016-11-01 13:21:43 +0530395 item_price: format_currency(obj.price_list_rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530396 item_name: obj.name===obj.item_name ? "" : obj.item_name,
397 item_image: obj.image ? "url('" + obj.image + "')" : null,
398 color: frappe.get_palette(obj.item_name),
399 abbr: frappe.get_abbr(obj.item_name)
400 })).tooltip().appendTo($wrap);
401 }
402 });
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530403 } else {
404 $("<h4>Searching record not found.</h4>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530405 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530406
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530407 if(this.items.length == 1
408 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530409 this.search.$input.val("");
410 this.add_to_cart();
411 }
412
413 // if form is local then allow this function
414 $(me.wrapper).find("div.pos-item").on("click", function() {
415 me.customer_validate();
416 if(me.frm.doc.docstatus==0) {
417 me.items = me.get_items($(this).attr("data-item-code"))
418 me.add_to_cart();
419 }
420 });
421 },
422
423 get_items: function(item_code){
424 // To search item as per the key enter
425
426 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530427 this.item_serial_no = {};
428 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530429
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530430 if(item_code){
431 return $.grep(window.items, function(item){
432 if(item.item_code == item_code ){
433 return true
434 }
435 })
436 }
437
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530438 key = this.search.$input.val().toLowerCase();
439 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530440 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530441 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530442
443 if(key){
444 return $.grep(window.items, function(item){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530445 if(search_status){
446 if(in_list(item.batch_nos, me.search.$input.val())){
447 search_status = false;
448 return me.item_batch_no[item.item_code] = me.search.$input.val()
449 } else if(in_list(Object.keys(item.serial_nos), me.search.$input.val())) {
450 search_status = false;
451 me.item_serial_no[item.item_code] = [me.search.$input.val(), item.serial_nos[me.search.$input.val()]]
452 return true
453 } else if(item.barcode == me.search.$input.val()) {
454 search_status = false;
455 return item.barcode == me.search.$input.val();
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530456 } else if(reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
457 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase()) ){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530458 return true
459 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530460 }
461 })
462 }else{
463 return window.items;
464 }
465 },
466
467 update_qty: function() {
468 var me = this;
469
470 $(this.wrapper).find(".pos-item-qty").on("change", function(){
471 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530472 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530473 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530474
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530475 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
476 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
477 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530478 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530479 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530480
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530481 $(this.wrapper).find("[data-action='decrease-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 })
486 },
487
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530488 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530489 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530490
491 $(this.wrapper).find(".pos-item-rate").on("change", function(){
492 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
493 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
494 })
495 },
496
497 update_qty_rate_against_item_code: function(item_code, field, value){
498 var me = this;
499 if(value < 0){
500 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530501 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530502
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530503 this.remove_item = []
504 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530505 if(d.serial_no){
506 me.validate_serial_no_qty(d, item_code, field, value)
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530507 }
508
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530509 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530510 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530511 d.amount = flt(d.rate) * flt(d.qty);
512 if(d.qty==0){
513 me.remove_item.push(d.idx)
514 }
515 }
516 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530517
518 if(field == 'qty'){
519 this.remove_zero_qty_item();
520 }
521
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530522 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530523 },
524
525 remove_zero_qty_item: function(){
526 var me = this;
527 idx = 0
528 this.items = []
529 idx = 0
530 $.each(this.frm.doc["items"] || [], function(i, d) {
531 if(!in_list(me.remove_item, d.idx)){
532 d.idx = idx;
533 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530534 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530535 }
536 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530537
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530538 this.frm.doc["items"] = this.items;
539 },
540
541 make_discount_field: function(){
542 var me = this;
543
544 this.wrapper.find('input.discount-percentage').on("change", function() {
545 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
546 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530547
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530548 if(me.frm.doc.apply_discount_on == 'Net Total'){
549 total = me.frm.doc.net_total
550 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530551
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530552 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
553 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
554 me.refresh();
555 });
556
557 this.wrapper.find('input.discount-amount').on("change", function() {
558 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
559 me.frm.doc.additional_discount_percentage = 0.0;
560 me.wrapper.find('input.discount-percentage').val(0);
561 me.refresh();
562 });
563 },
564
565 customer_validate: function(){
566 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530567 if(!this.frm.doc.customer){
568 frappe.throw(__("Please select customer"))
569 }
570 },
571
572 add_to_cart: function() {
573 var me = this;
574 var caught = false;
575 var no_of_items = me.wrapper.find(".pos-bill-item").length;
576
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530577 this.customer_validate();
578 this.mandatory_batch_no();
579 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530580 this.validate_warehouse();
581
582 if (no_of_items != 0) {
583 $.each(this.frm.doc["items"] || [], function(i, d) {
584 if (d.item_code == me.items[0].item_code) {
585 caught = true;
586 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530587 d.amount = flt(d.rate) * flt(d.qty);
588 if(me.item_serial_no[d.item_code]){
589 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
590 d.warehouse = me.item_serial_no[d.item_code][1]
591 }
592
593 if(me.item_batch_no.length){
594 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530595 }
596 }
597 });
598 }
599
600 // if item not found then add new item
601 if (!caught)
602 this.add_new_item_to_grid();
603
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530604 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530605 },
606
607 add_new_item_to_grid: function() {
608 var me = this;
609 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
610 this.child.item_code = this.items[0].item_code;
611 this.child.item_name = this.items[0].item_name;
612 this.child.stock_uom = this.items[0].stock_uom;
613 this.child.description = this.items[0].description;
614 this.child.qty = 1;
615 this.child.item_group = this.items[0].item_group;
616 this.child.cost_center = this.items[0].cost_center;
617 this.child.income_account = this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530618 this.child.warehouse = (this.item_serial_no[this.child.item_code]
619 ? this.item_serial_no[this.child.item_code][1] : this.items[0].default_warehouse);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530620 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
621 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
622 this.child.actual_qty = this.items[0].actual_qty;
623 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530624 this.child.batch_no = this.item_batch_no[this.child.item_code];
625 this.child.serial_no = (this.item_serial_no[this.child.item_code]
626 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaure525900c2016-09-07 15:01:08 +0530627 this.child.item_tax_rate = this.items[0].taxes;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530628 },
629
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530630 update_paid_amount_status: function(update_paid_amount){
631 if(this.name){
632 update_paid_amount = update_paid_amount ? false : true;
633 }
634
635 this.refresh(update_paid_amount);
636 },
637
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530638 refresh: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530639 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530640 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530641 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530642 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530643 this.set_primary_action();
644 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530645
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530646 refresh_fields: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530647 this.apply_pricing_rule();
648 this.discount_amount_applied = false;
649 this._calculate_taxes_and_totals();
650 this.calculate_discount_amount();
651 this.show_items_in_item_cart();
652 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530653 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530654 this.set_totals();
655 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530656
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530657 get_company_currency: function() {
658 return erpnext.get_currency(this.frm.doc.company);
659 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530660
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530661 show_item_wise_taxes: function(){
662 return null;
663 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530664
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530665 show_items_in_item_cart: function() {
666 var me = this;
667 var $items = this.wrapper.find(".items").empty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530668 $.each(this.frm.doc.items|| [], function(i, d) {
669 $(frappe.render_template("pos_bill_item", {
670 item_code: d.item_code,
671 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
672 qty: d.qty,
673 actual_qty: d.actual_qty,
674 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530675 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530676 amount: format_currency(d.amount, me.frm.doc.currency)
677 })).appendTo($items);
678 });
679
680 this.wrapper.find("input.pos-item-qty").on("focus", function() {
681 $(this).select();
682 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530683
684 this.wrapper.find("input.pos-item-rate").on("focus", function() {
685 $(this).select();
686 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530687 },
688
689 set_taxes: function(){
690 var me = this;
691 me.frm.doc.total_taxes_and_charges = 0.0
692
693 var taxes = this.frm.doc.taxes || [];
694 $(this.wrapper)
695 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
696 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530697
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530698 $.each(taxes, function(i, d) {
699 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
700 $(frappe.render_template("pos_tax_row", {
701 description: d.description,
702 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
703 me.frm.doc.currency)
704 })).appendTo(me.wrapper.find(".tax-table"));
705 }
706 });
707 },
708
709 set_totals: function() {
710 var me = this;
711 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
712 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
713 },
714
715 set_primary_action: function() {
716 var me = this;
717
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530718 if (this.frm.doc.docstatus==0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530719 this.page.set_primary_action(__("Pay"), function() {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530720 me.validate();
721 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530722 me.create_invoice();
723 me.make_payment();
Faris Ansari8908d192016-07-20 13:59:19 +0530724 }, "octicon octicon-credit-card");
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530725 }else if(this.frm.doc.docstatus == 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530726 this.page.set_primary_action(__("Print"), function() {
727 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530728 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530729 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530730 }else {
731 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530732 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530733
734 this.page.set_secondary_action(__("New"), function() {
735 me.save_previous_entry();
736 me.create_new();
Faris Ansari8908d192016-07-20 13:59:19 +0530737 }, "octicon octicon-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530738 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530739
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530740 print_dialog: function(){
741 var me = this;
742
743 msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
744 style="margin-right: 5px;">{0}</a>\
745 <a class="btn btn-default new_doc">{1}</a>', [
746 __('Print'), __('New')
747 ]));
748
749 $('.print_doc').click(function(){
750 html = frappe.render(me.print_template, me.frm.doc)
751 me.print_document(html)
752 })
753
754 $('.new_doc').click(function(){
755 msgprint.hide()
756 me.create_new();
757 })
758 },
759
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530760 print_document: function(html){
761 var w = window.open();
762 w.document.write(html);
763 w.document.close();
764 setTimeout(function(){
765 w.print();
766 w.close();
767 }, 1000)
768 },
769
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530770 submit_invoice: function(){
771 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530772 this.change_status();
773 if(this.frm.doc.docstatus == 1){
774 this.print_dialog()
775 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530776 },
777
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530778 change_status: function(){
779 if(this.frm.doc.docstatus == 0){
780 this.frm.doc.docstatus = 1;
781 this.update_invoice();
782 this.disable_input_field();
783 }
784 },
785
786 disable_input_field: function(){
787 var pointer_events = 'inherit'
788 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530789
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530790 if(this.frm.doc.docstatus == 1){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530791 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530792 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530793 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530794
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530795 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
796 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
797 this.set_primary_action();
798 },
799
800 create_invoice: function(){
801 var me = this;
802 var invoice_data = {}
803 this.si_docs = this.get_doc_from_localstorage();
804 if(this.name){
805 this.update_invoice()
806 }else{
807 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +0530808 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +0530809 this.frm.doc.posting_date = frappe.datetime.get_today();
810 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530811 invoice_data[this.name] = this.frm.doc
812 this.si_docs.push(invoice_data)
813 this.update_localstorage();
814 this.set_primary_action();
815 }
816 },
817
818 update_invoice: function(){
819 var me = this;
820 this.si_docs = this.get_doc_from_localstorage();
821 $.each(this.si_docs, function(index, data){
822 for(key in data){
823 if(key == me.name){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530824 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530825 me.update_localstorage();
826 }
827 }
828 })
829 },
830
831 update_localstorage: function(){
832 try{
833 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
834 }catch(e){
835 frappe.throw(__("LocalStorage is full , did not save"))
836 }
837 },
838
839 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530840 try{
841 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
842 }catch(e){
843 return []
844 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530845 },
846
847 set_interval_for_si_sync: function(){
848 var me = this;
849 setInterval(function(){
850 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530851 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530852 },
853
854 sync_sales_invoice: function(){
855 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530856 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530857
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530858 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530859 frappe.call({
860 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
861 args: {
862 doc_list: me.si_docs
863 },
864 callback: function(r){
865 if(r.message){
866 me.removed_items = r.message;
867 me.remove_doc_from_localstorage();
868 }
869 }
870 })
871 }
872 },
873
874 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530875 var invoices = [];
876 var index = 1;
877 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530878 if(docs){
879 invoices = $.map(docs, function(data){
880 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530881 if(data[key].docstatus == 1 && index < 50){
882 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530883 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530884 return data
885 }
886 }
887 });
888 }
889
890 return invoices
891 },
892
893 remove_doc_from_localstorage: function(){
894 var me = this;
895 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530896 this.new_si_docs = [];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530897 if(this.removed_items){
898 $.each(this.si_docs, function(index, data){
899 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530900 if(!in_list(me.removed_items, key)){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530901 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530902 }
903 }
904 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530905 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530906 this.update_localstorage();
907 }
908 },
909
910 validate: function(){
911 var me = this;
912 this.customer_validate();
913 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530914 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530915 },
916
917 item_validate: function(){
918 if(this.frm.doc.items.length == 0){
919 frappe.throw(__("Select items to save the invoice"))
920 }
921 },
Saurabh9a6c6662016-06-27 16:51:44 +0530922
923 validate_mode_of_payments: function(){
924 if (this.frm.doc.payments.length === 0){
925 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
926 }
927 },
928
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530929 validate_serial_no: function(){
930 var me = this;
931 var item_code = serial_no = '';
932 for (key in this.item_serial_no){
933 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530934 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530935 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530936
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530937 if(item_code && serial_no){
938 $.each(this.frm.doc.items, function(index, data){
939 if(data.item_code == item_code){
940 if(in_list(data.serial_no.split('\n'), serial_no)){
941 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
942 'serial_no': serial_no
943 })))
944 }
945 }
946 })
947 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530948
949 if(this.items[0].has_serial_no && serial_no == ""){
950 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
951 'item': this.items[0].item_code
952 })))
953 }
954 },
955
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530956 validate_serial_no_qty: function(args, item_code, field, value){
957 var me = this;
958 if (args.item_code == item_code && args.serial_no
959 && field == 'qty' && cint(value) != value) {
960 args.qty = 0.0;
961 this.refresh();
962 frappe.throw(__("Serial no item cannot be a fraction"))
963 }
964
965 if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
966 args.qty = 0.0;
967 args.serial_no = ''
968 this.refresh();
969 frappe.throw(__("Total nos of serial no is not equal to quantity."))
970 }
971 },
972
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530973 mandatory_batch_no: function(){
974 var me = this;
975 if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
976 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
977 'item': this.items[0].item_code
978 })))
979 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530980 },
981
982 apply_pricing_rule: function(){
983 var me = this;
984 $.each(this.frm.doc["items"], function(n, item) {
985 pricing_rule = me.get_pricing_rule(item)
986 me.validate_pricing_rule(pricing_rule)
987 if(pricing_rule.length){
988 item.margin_type = pricing_rule[0].margin_type;
989 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
990 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
991 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
992 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure619c40b2016-11-07 15:29:17 +0530993 } else if(item.discount_percentage > 0 || item.margin_rate_or_amount > 0) {
994 item.margin_rate_or_amount = 0.0;
995 item.discount_percentage = 0.0;
996 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530997 }
998 })
999 },
1000
1001 get_pricing_rule: function(item){
1002 var me = this;
1003 return $.grep(this.pricing_rules, function(data){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301004 if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
1005 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
1006 if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
1007 return me.validate_condition(data)
1008 }else{
1009 return true
1010 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301011 }
1012 }
1013 })
1014 },
1015
1016 validate_condition: function(data){
1017 //This method check condition based on applicable for
1018 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
1019 if(in_list(condition[1], condition[0])){
1020 return true
1021 }
1022 },
1023
1024 get_mapper_for_pricing_rule: function(data){
1025 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301026 'Customer': [data.customer, [this.frm.doc.customer]],
1027 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1028 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301029 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301030 }
1031 },
1032
1033 validate_pricing_rule: function(pricing_rule){
1034 //This method validate duplicate pricing rule
1035 var pricing_rule_name = '';
1036 var priority = 0;
1037 var pricing_rule_list = [];
1038 var priority_list = []
1039
1040 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301041
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301042 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301043 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301044 priority_list.push(data.priority)
1045 if(priority <= data.priority){
1046 priority = data.priority
1047 pricing_rule_list.push(data)
1048 }
1049 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301050
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301051 count = 0
1052 $.each(priority_list, function(index, value){
1053 if(value == priority){
1054 count++
1055 }
1056 })
1057
1058 if(priority == 0 || count > 1){
1059 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1060 'pricing_rule': pricing_rule_name
1061 })))
1062 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301063
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301064 return pricing_rule_list
1065 }
1066 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301067
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301068 validate_warehouse: function(){
1069 if(!this.items[0].default_warehouse){
Neil Trini Lasrado5ddd7ac2016-10-24 15:25:33 +05301070 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301071 }
1072 }
1073})