blob: 4c13fe372015bec5545efbde35081772da179c5d [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
390 if (this.items) {
391 $.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 });
403 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530404
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530405 if(this.items.length == 1
406 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530407 this.search.$input.val("");
408 this.add_to_cart();
409 }
410
411 // if form is local then allow this function
412 $(me.wrapper).find("div.pos-item").on("click", function() {
413 me.customer_validate();
414 if(me.frm.doc.docstatus==0) {
415 me.items = me.get_items($(this).attr("data-item-code"))
416 me.add_to_cart();
417 }
418 });
419 },
420
421 get_items: function(item_code){
422 // To search item as per the key enter
423
424 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530425 this.item_serial_no = {};
426 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530427
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530428 if(item_code){
429 return $.grep(window.items, function(item){
430 if(item.item_code == item_code ){
431 return true
432 }
433 })
434 }
435
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530436 key = this.search.$input.val().toLowerCase();
437 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530438 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530439 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530440
441 if(key){
442 return $.grep(window.items, function(item){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530443 if(search_status){
444 if(in_list(item.batch_nos, me.search.$input.val())){
445 search_status = false;
446 return me.item_batch_no[item.item_code] = me.search.$input.val()
447 } else if(in_list(Object.keys(item.serial_nos), me.search.$input.val())) {
448 search_status = false;
449 me.item_serial_no[item.item_code] = [me.search.$input.val(), item.serial_nos[me.search.$input.val()]]
450 return true
451 } else if(item.barcode == me.search.$input.val()) {
452 search_status = false;
453 return item.barcode == me.search.$input.val();
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530454 } else if(reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
455 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase()) ){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530456 return true
457 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530458 }
459 })
460 }else{
461 return window.items;
462 }
463 },
464
465 update_qty: function() {
466 var me = this;
467
468 $(this.wrapper).find(".pos-item-qty").on("change", function(){
469 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530470 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530471 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530472
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530473 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
474 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
475 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530476 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530477 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530478
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530479 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
480 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
481 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530482 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530483 })
484 },
485
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530486 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530487 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530488
489 $(this.wrapper).find(".pos-item-rate").on("change", function(){
490 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
491 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
492 })
493 },
494
495 update_qty_rate_against_item_code: function(item_code, field, value){
496 var me = this;
497 if(value < 0){
498 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530499 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530500
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530501 this.remove_item = []
502 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530503 if(d.serial_no){
504 me.validate_serial_no_qty(d, item_code, field, value)
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530505 }
506
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530507 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530508 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530509 d.amount = flt(d.rate) * flt(d.qty);
510 if(d.qty==0){
511 me.remove_item.push(d.idx)
512 }
513 }
514 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530515
516 if(field == 'qty'){
517 this.remove_zero_qty_item();
518 }
519
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530520 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530521 },
522
523 remove_zero_qty_item: function(){
524 var me = this;
525 idx = 0
526 this.items = []
527 idx = 0
528 $.each(this.frm.doc["items"] || [], function(i, d) {
529 if(!in_list(me.remove_item, d.idx)){
530 d.idx = idx;
531 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530532 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530533 }
534 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530535
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530536 this.frm.doc["items"] = this.items;
537 },
538
539 make_discount_field: function(){
540 var me = this;
541
542 this.wrapper.find('input.discount-percentage').on("change", function() {
543 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
544 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530545
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530546 if(me.frm.doc.apply_discount_on == 'Net Total'){
547 total = me.frm.doc.net_total
548 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530549
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530550 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
551 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
552 me.refresh();
553 });
554
555 this.wrapper.find('input.discount-amount').on("change", function() {
556 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
557 me.frm.doc.additional_discount_percentage = 0.0;
558 me.wrapper.find('input.discount-percentage').val(0);
559 me.refresh();
560 });
561 },
562
563 customer_validate: function(){
564 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530565 if(!this.frm.doc.customer){
566 frappe.throw(__("Please select customer"))
567 }
568 },
569
570 add_to_cart: function() {
571 var me = this;
572 var caught = false;
573 var no_of_items = me.wrapper.find(".pos-bill-item").length;
574
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530575 this.customer_validate();
576 this.mandatory_batch_no();
577 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530578 this.validate_warehouse();
579
580 if (no_of_items != 0) {
581 $.each(this.frm.doc["items"] || [], function(i, d) {
582 if (d.item_code == me.items[0].item_code) {
583 caught = true;
584 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530585 d.amount = flt(d.rate) * flt(d.qty);
586 if(me.item_serial_no[d.item_code]){
587 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
588 d.warehouse = me.item_serial_no[d.item_code][1]
589 }
590
591 if(me.item_batch_no.length){
592 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530593 }
594 }
595 });
596 }
597
598 // if item not found then add new item
599 if (!caught)
600 this.add_new_item_to_grid();
601
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530602 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530603 },
604
605 add_new_item_to_grid: function() {
606 var me = this;
607 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
608 this.child.item_code = this.items[0].item_code;
609 this.child.item_name = this.items[0].item_name;
610 this.child.stock_uom = this.items[0].stock_uom;
611 this.child.description = this.items[0].description;
612 this.child.qty = 1;
613 this.child.item_group = this.items[0].item_group;
614 this.child.cost_center = this.items[0].cost_center;
615 this.child.income_account = this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530616 this.child.warehouse = (this.item_serial_no[this.child.item_code]
617 ? this.item_serial_no[this.child.item_code][1] : this.items[0].default_warehouse);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530618 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
619 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
620 this.child.actual_qty = this.items[0].actual_qty;
621 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530622 this.child.batch_no = this.item_batch_no[this.child.item_code];
623 this.child.serial_no = (this.item_serial_no[this.child.item_code]
624 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaure525900c2016-09-07 15:01:08 +0530625 this.child.item_tax_rate = this.items[0].taxes;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530626 },
627
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530628 update_paid_amount_status: function(update_paid_amount){
629 if(this.name){
630 update_paid_amount = update_paid_amount ? false : true;
631 }
632
633 this.refresh(update_paid_amount);
634 },
635
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530636 refresh: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530637 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530638 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530639 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530640 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530641 this.set_primary_action();
642 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530643
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530644 refresh_fields: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530645 this.apply_pricing_rule();
646 this.discount_amount_applied = false;
647 this._calculate_taxes_and_totals();
648 this.calculate_discount_amount();
649 this.show_items_in_item_cart();
650 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530651 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530652 this.set_totals();
653 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530654
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530655 get_company_currency: function() {
656 return erpnext.get_currency(this.frm.doc.company);
657 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530658
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530659 show_item_wise_taxes: function(){
660 return null;
661 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530662
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530663 show_items_in_item_cart: function() {
664 var me = this;
665 var $items = this.wrapper.find(".items").empty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530666 $.each(this.frm.doc.items|| [], function(i, d) {
667 $(frappe.render_template("pos_bill_item", {
668 item_code: d.item_code,
669 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
670 qty: d.qty,
671 actual_qty: d.actual_qty,
672 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530673 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530674 amount: format_currency(d.amount, me.frm.doc.currency)
675 })).appendTo($items);
676 });
677
678 this.wrapper.find("input.pos-item-qty").on("focus", function() {
679 $(this).select();
680 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530681
682 this.wrapper.find("input.pos-item-rate").on("focus", function() {
683 $(this).select();
684 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530685 },
686
687 set_taxes: function(){
688 var me = this;
689 me.frm.doc.total_taxes_and_charges = 0.0
690
691 var taxes = this.frm.doc.taxes || [];
692 $(this.wrapper)
693 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
694 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530695
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530696 $.each(taxes, function(i, d) {
697 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
698 $(frappe.render_template("pos_tax_row", {
699 description: d.description,
700 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
701 me.frm.doc.currency)
702 })).appendTo(me.wrapper.find(".tax-table"));
703 }
704 });
705 },
706
707 set_totals: function() {
708 var me = this;
709 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
710 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
711 },
712
713 set_primary_action: function() {
714 var me = this;
715
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530716 if (this.frm.doc.docstatus==0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530717 this.page.set_primary_action(__("Pay"), function() {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530718 me.validate();
719 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530720 me.create_invoice();
721 me.make_payment();
Faris Ansari8908d192016-07-20 13:59:19 +0530722 }, "octicon octicon-credit-card");
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530723 }else if(this.frm.doc.docstatus == 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530724 this.page.set_primary_action(__("Print"), function() {
725 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530726 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530727 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530728 }else {
729 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530730 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530731
732 this.page.set_secondary_action(__("New"), function() {
733 me.save_previous_entry();
734 me.create_new();
Faris Ansari8908d192016-07-20 13:59:19 +0530735 }, "octicon octicon-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530736 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530737
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530738 print_dialog: function(){
739 var me = this;
740
741 msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
742 style="margin-right: 5px;">{0}</a>\
743 <a class="btn btn-default new_doc">{1}</a>', [
744 __('Print'), __('New')
745 ]));
746
747 $('.print_doc').click(function(){
748 html = frappe.render(me.print_template, me.frm.doc)
749 me.print_document(html)
750 })
751
752 $('.new_doc').click(function(){
753 msgprint.hide()
754 me.create_new();
755 })
756 },
757
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530758 print_document: function(html){
759 var w = window.open();
760 w.document.write(html);
761 w.document.close();
762 setTimeout(function(){
763 w.print();
764 w.close();
765 }, 1000)
766 },
767
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530768 submit_invoice: function(){
769 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530770 this.change_status();
771 if(this.frm.doc.docstatus == 1){
772 this.print_dialog()
773 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530774 },
775
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530776 change_status: function(){
777 if(this.frm.doc.docstatus == 0){
778 this.frm.doc.docstatus = 1;
779 this.update_invoice();
780 this.disable_input_field();
781 }
782 },
783
784 disable_input_field: function(){
785 var pointer_events = 'inherit'
786 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530787
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530788 if(this.frm.doc.docstatus == 1){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530789 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530790 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530791 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530792
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530793 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
794 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
795 this.set_primary_action();
796 },
797
798 create_invoice: function(){
799 var me = this;
800 var invoice_data = {}
801 this.si_docs = this.get_doc_from_localstorage();
802 if(this.name){
803 this.update_invoice()
804 }else{
805 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +0530806 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +0530807 this.frm.doc.posting_date = frappe.datetime.get_today();
808 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530809 invoice_data[this.name] = this.frm.doc
810 this.si_docs.push(invoice_data)
811 this.update_localstorage();
812 this.set_primary_action();
813 }
814 },
815
816 update_invoice: function(){
817 var me = this;
818 this.si_docs = this.get_doc_from_localstorage();
819 $.each(this.si_docs, function(index, data){
820 for(key in data){
821 if(key == me.name){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530822 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530823 me.update_localstorage();
824 }
825 }
826 })
827 },
828
829 update_localstorage: function(){
830 try{
831 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
832 }catch(e){
833 frappe.throw(__("LocalStorage is full , did not save"))
834 }
835 },
836
837 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530838 try{
839 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
840 }catch(e){
841 return []
842 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530843 },
844
845 set_interval_for_si_sync: function(){
846 var me = this;
847 setInterval(function(){
848 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530849 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530850 },
851
852 sync_sales_invoice: function(){
853 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530854 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530855
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530856 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530857 frappe.call({
858 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
859 args: {
860 doc_list: me.si_docs
861 },
862 callback: function(r){
863 if(r.message){
864 me.removed_items = r.message;
865 me.remove_doc_from_localstorage();
866 }
867 }
868 })
869 }
870 },
871
872 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530873 var invoices = [];
874 var index = 1;
875 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530876 if(docs){
877 invoices = $.map(docs, function(data){
878 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530879 if(data[key].docstatus == 1 && index < 50){
880 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530881 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530882 return data
883 }
884 }
885 });
886 }
887
888 return invoices
889 },
890
891 remove_doc_from_localstorage: function(){
892 var me = this;
893 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530894 this.new_si_docs = [];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530895 if(this.removed_items){
896 $.each(this.si_docs, function(index, data){
897 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530898 if(!in_list(me.removed_items, key)){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530899 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530900 }
901 }
902 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530903 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530904 this.update_localstorage();
905 }
906 },
907
908 validate: function(){
909 var me = this;
910 this.customer_validate();
911 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530912 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530913 },
914
915 item_validate: function(){
916 if(this.frm.doc.items.length == 0){
917 frappe.throw(__("Select items to save the invoice"))
918 }
919 },
Saurabh9a6c6662016-06-27 16:51:44 +0530920
921 validate_mode_of_payments: function(){
922 if (this.frm.doc.payments.length === 0){
923 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
924 }
925 },
926
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530927 validate_serial_no: function(){
928 var me = this;
929 var item_code = serial_no = '';
930 for (key in this.item_serial_no){
931 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530932 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530933 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530934
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530935 if(item_code && serial_no){
936 $.each(this.frm.doc.items, function(index, data){
937 if(data.item_code == item_code){
938 if(in_list(data.serial_no.split('\n'), serial_no)){
939 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
940 'serial_no': serial_no
941 })))
942 }
943 }
944 })
945 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530946
947 if(this.items[0].has_serial_no && serial_no == ""){
948 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
949 'item': this.items[0].item_code
950 })))
951 }
952 },
953
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530954 validate_serial_no_qty: function(args, item_code, field, value){
955 var me = this;
956 if (args.item_code == item_code && args.serial_no
957 && field == 'qty' && cint(value) != value) {
958 args.qty = 0.0;
959 this.refresh();
960 frappe.throw(__("Serial no item cannot be a fraction"))
961 }
962
963 if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
964 args.qty = 0.0;
965 args.serial_no = ''
966 this.refresh();
967 frappe.throw(__("Total nos of serial no is not equal to quantity."))
968 }
969 },
970
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530971 mandatory_batch_no: function(){
972 var me = this;
973 if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
974 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
975 'item': this.items[0].item_code
976 })))
977 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530978 },
979
980 apply_pricing_rule: function(){
981 var me = this;
982 $.each(this.frm.doc["items"], function(n, item) {
983 pricing_rule = me.get_pricing_rule(item)
984 me.validate_pricing_rule(pricing_rule)
985 if(pricing_rule.length){
986 item.margin_type = pricing_rule[0].margin_type;
987 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
988 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
989 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
990 me.apply_pricing_rule_on_item(item)
991 }
992 })
993 },
994
995 get_pricing_rule: function(item){
996 var me = this;
997 return $.grep(this.pricing_rules, function(data){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530998 if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
999 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
1000 if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
1001 return me.validate_condition(data)
1002 }else{
1003 return true
1004 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301005 }
1006 }
1007 })
1008 },
1009
1010 validate_condition: function(data){
1011 //This method check condition based on applicable for
1012 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
1013 if(in_list(condition[1], condition[0])){
1014 return true
1015 }
1016 },
1017
1018 get_mapper_for_pricing_rule: function(data){
1019 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301020 'Customer': [data.customer, [this.frm.doc.customer]],
1021 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1022 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301023 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301024 }
1025 },
1026
1027 validate_pricing_rule: function(pricing_rule){
1028 //This method validate duplicate pricing rule
1029 var pricing_rule_name = '';
1030 var priority = 0;
1031 var pricing_rule_list = [];
1032 var priority_list = []
1033
1034 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301035
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301036 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301037 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301038 priority_list.push(data.priority)
1039 if(priority <= data.priority){
1040 priority = data.priority
1041 pricing_rule_list.push(data)
1042 }
1043 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301044
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301045 count = 0
1046 $.each(priority_list, function(index, value){
1047 if(value == priority){
1048 count++
1049 }
1050 })
1051
1052 if(priority == 0 || count > 1){
1053 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1054 'pricing_rule': pricing_rule_name
1055 })))
1056 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301057
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301058 return pricing_rule_list
1059 }
1060 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301061
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301062 validate_warehouse: function(){
1063 if(!this.items[0].default_warehouse){
Neil Trini Lasrado5ddd7ac2016-10-24 15:25:33 +05301064 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301065 }
1066 }
1067})