blob: 668b377fda6ed3af35bc245f0073b317658af91b [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 Waghchaure82be0202016-10-04 12:46:37 +053096 me.make_item_list(true);
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">\
122 <div class="col-xs-2">Sr</div>\
123 <div class="col-xs-4">Customer</div>\
124 <div class="col-xs-2 text-left">Status</div>\
125 <div class="col-xs-4 text-right">Grand Total</div>\
126 </div>')
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530127
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530128 $.each(this.si_docs, function(index, data){
129 for(key in data) {
130 $(frappe.render_template("pos_invoice_list", {
131 sr: index + 1,
132 name: key,
133 customer: data[key].customer,
134 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
135 data: me.get_doctype_status(data[key])
136 })).appendTo($(me.list_body));
137 }
138 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530139
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530140 $(this.list_body).find('.list-row').click(function() {
141 me.name = $(this).attr('invoice-name')
142 doc_data = me.get_invoice_doc(me.si_docs)
143 if(doc_data){
144 me.frm.doc = doc_data[0][me.name];
145 me.set_missing_values();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530146 me.refresh(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530147 me.disable_input_field();
148 me.list_dialog.hide();
149 }
150 })
151 }else{
152 $(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")}))
153 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530154 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530155
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530156 get_doctype_status: function(doc){
157 if(doc.outstanding_amount == 0){
158 return {status: "Paid", indicator: "green"}
159 }else if(doc.docstatus == 0){
160 return {status: "Draft", indicator: "red"}
161 }else if(doc.paid_amount >= 0){
162 return {status: "Unpaid", indicator: "orange"}
163 }
164 },
165
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530166 set_missing_values: function(){
167 var me = this;
168 doc = JSON.parse(localStorage.getItem('doc'))
169 if(this.frm.doc.payments.length == 0){
170 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530171 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530172 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530173
174 if(this.frm.doc.customer){
175 this.party_field.$input.val(this.frm.doc.customer);
176 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530177
178 if(!this.frm.doc.write_off_account){
179 this.frm.doc.write_off_account = doc.write_off_account
180 }
181
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530182 if(!this.frm.doc.account_for_change_amount){
183 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530184 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530185 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530186
187 get_invoice_doc: function(si_docs){
188 var me = this;
189 this.si_docs = this.get_doc_from_localstorage();
190
191 return $.grep(this.si_docs, function(data){
192 for(key in data){
193 return key == me.name
194 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530195 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530196 },
197
198 get_data_from_server: function(callback){
199 var me = this;
200 frappe.call({
201 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
202 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530203 freeze_message: __("Master data syncing, it might take some time"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530204 callback: function(r){
205 window.items = r.message.items;
206 window.customers = r.message.customers;
207 window.pricing_rules = r.message.pricing_rules;
208 window.meta = r.message.meta;
209 window.print_template = r.message.print_template;
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530210 me.default_customer = r.message.default_customer || null;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530211 localStorage.setItem('doc', JSON.stringify(r.message.doc));
212 if(callback){
213 callback();
214 }
215 }
216 })
217 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530218
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530219 save_previous_entry : function(){
220 if(this.frm.doc.items.length > 0){
221 this.create_invoice()
222 }
223 },
224
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530225 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530226 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530227 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530228 this.name = '';
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530229 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530230 this.setup();
231 },
232
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530233 load_data: function(load_doc){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530234 this.items = window.items;
235 this.customers = window.customers;
236 this.pricing_rules = window.pricing_rules;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530237
238 if(load_doc) {
239 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
240 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530241
242 $.each(window.meta, function(i, data){
243 frappe.meta.sync(data)
244 })
245
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530246 this.print_template = frappe.render_template("print_template",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530247 {content: window.print_template, title:"POS"})
248 },
249
250 setup: function(){
251 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
252 this.set_transaction_defaults("Customer");
253 this.make();
254 this.set_primary_action();
255 },
256
257 set_transaction_defaults: function(party) {
258 var me = this;
259 this.party = party;
260 this.price_list = (party == "Customer" ?
261 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
262 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
263 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
264 },
265
266 make: function() {
267 this.make_search();
268 this.make_customer();
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530269 this.make_item_list(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530270 this.make_discount_field()
271 },
272
273 make_search: function() {
274 var me = this;
275 this.search = frappe.ui.form.make_control({
276 df: {
277 "fieldtype": "Data",
278 "label": "Item",
279 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530280 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530281 },
282 parent: this.wrapper.find(".search-area"),
283 only_input: true,
284 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530285
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530286 this.search.make_input();
287 this.search.$input.on("keyup", function() {
288 setTimeout(function() {
289 me.items = me.get_items();
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530290 me.make_item_list(false);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530291 }, 1000);
292 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530293
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530294 this.party_field = frappe.ui.form.make_control({
295 df: {
296 "fieldtype": "Data",
297 "options": this.party,
298 "label": this.party,
299 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530300 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530301 },
302 parent: this.wrapper.find(".party-area"),
303 only_input: true,
304 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530305
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530306 this.party_field.make_input();
307 },
308
309 make_customer: function() {
310 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530311
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530312 if(this.default_customer && !this.frm.doc.customer){
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530313 this.party_field.$input.val(this.default_customer);
314 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530315 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530316
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530317 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530318 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530319 source: function (request, response) {
320 me.customer_data = me.get_customers(request.term)
321 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530322 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530323 customer_group: data.customer_group, territory: data.territory}
324 }))
325 },
326 change: function(event, ui){
327 if(ui.item){
328 me.frm.doc.customer = ui.item.label;
329 me.frm.doc.customer_name = ui.item.customer_name;
330 me.frm.doc.customer_group = ui.item.customer_group;
331 me.frm.doc.territory = ui.item.territory;
332 }else{
333 me.frm.doc.customer = me.party_field.$input.val();
334 }
335 me.refresh();
336 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530337 }).on("focus", function(){
338 setTimeout(function() {
339 if(!me.party_field.$input.val()) {
340 me.party_field.$input.autocomplete( "search", " " );
341 }
342 }, 500);
343 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530344 },
345
346 get_customers: function(key){
347 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530348 key = key.toLowerCase().trim()
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530349 var re = new RegExp('%', 'g');
350 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'))
351
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530352 if(key){
353 return $.grep(this.customers, function(data) {
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530354 if(reg.test(data.name.toLowerCase())
355 || reg.test(data.customer_name.toLowerCase())
356 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530357 return data
358 }
359 })
360 }else{
361 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
362 return customers.slice(0, 20)
363 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530364 },
365
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530366 make_item_list: function(index_search) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530367 var me = this;
368 if(!this.price_list) {
369 msgprint(__("Price List not found or disabled"));
370 return;
371 }
372
373 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530374
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530375 var $wrap = me.wrapper.find(".item-list");
376 me.wrapper.find(".item-list").empty();
377
378 if (this.items) {
379 $.each(this.items, function(index, obj) {
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530380 if(!index_search || index < 16){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530381 $(frappe.render_template("pos_item", {
382 item_code: obj.name,
383 item_price: format_currency(obj.price_list_rate, obj.currency),
384 item_name: obj.name===obj.item_name ? "" : obj.item_name,
385 item_image: obj.image ? "url('" + obj.image + "')" : null,
386 color: frappe.get_palette(obj.item_name),
387 abbr: frappe.get_abbr(obj.item_name)
388 })).tooltip().appendTo($wrap);
389 }
390 });
391 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530392
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530393 if(this.items.length == 1
394 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530395 this.search.$input.val("");
396 this.add_to_cart();
397 }
398
399 // if form is local then allow this function
400 $(me.wrapper).find("div.pos-item").on("click", function() {
401 me.customer_validate();
402 if(me.frm.doc.docstatus==0) {
403 me.items = me.get_items($(this).attr("data-item-code"))
404 me.add_to_cart();
405 }
406 });
407 },
408
409 get_items: function(item_code){
410 // To search item as per the key enter
411
412 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530413 this.item_serial_no = {};
414 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530415
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530416 if(item_code){
417 return $.grep(window.items, function(item){
418 if(item.item_code == item_code ){
419 return true
420 }
421 })
422 }
423
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530424 key = this.search.$input.val().toLowerCase();
425 var re = new RegExp('%', 'g');
426 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530427 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530428
429 if(key){
430 return $.grep(window.items, function(item){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530431 if(search_status){
432 if(in_list(item.batch_nos, me.search.$input.val())){
433 search_status = false;
434 return me.item_batch_no[item.item_code] = me.search.$input.val()
435 } else if(in_list(Object.keys(item.serial_nos), me.search.$input.val())) {
436 search_status = false;
437 me.item_serial_no[item.item_code] = [me.search.$input.val(), item.serial_nos[me.search.$input.val()]]
438 return true
439 } else if(item.barcode == me.search.$input.val()) {
440 search_status = false;
441 return item.barcode == me.search.$input.val();
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530442 } else if(reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
443 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase()) ){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530444 return true
445 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530446 }
447 })
448 }else{
449 return window.items;
450 }
451 },
452
453 update_qty: function() {
454 var me = this;
455
456 $(this.wrapper).find(".pos-item-qty").on("change", function(){
457 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530458 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530459 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530460
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530461 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
462 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
463 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530464 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530465 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530466
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530467 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
468 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
469 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530470 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530471 })
472 },
473
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530474 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530475 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530476
477 $(this.wrapper).find(".pos-item-rate").on("change", function(){
478 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
479 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
480 })
481 },
482
483 update_qty_rate_against_item_code: function(item_code, field, value){
484 var me = this;
485 if(value < 0){
486 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530487 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530488
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530489 this.remove_item = []
490 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530491 if(d.serial_no){
492 me.validate_serial_no_qty(d, item_code, field, value)
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530493 }
494
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530495 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530496 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530497 d.amount = flt(d.rate) * flt(d.qty);
498 if(d.qty==0){
499 me.remove_item.push(d.idx)
500 }
501 }
502 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530503
504 if(field == 'qty'){
505 this.remove_zero_qty_item();
506 }
507
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530508 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530509 },
510
511 remove_zero_qty_item: function(){
512 var me = this;
513 idx = 0
514 this.items = []
515 idx = 0
516 $.each(this.frm.doc["items"] || [], function(i, d) {
517 if(!in_list(me.remove_item, d.idx)){
518 d.idx = idx;
519 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530520 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530521 }
522 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530523
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530524 this.frm.doc["items"] = this.items;
525 },
526
527 make_discount_field: function(){
528 var me = this;
529
530 this.wrapper.find('input.discount-percentage').on("change", function() {
531 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
532 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530533
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530534 if(me.frm.doc.apply_discount_on == 'Net Total'){
535 total = me.frm.doc.net_total
536 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530537
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530538 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
539 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
540 me.refresh();
541 });
542
543 this.wrapper.find('input.discount-amount').on("change", function() {
544 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
545 me.frm.doc.additional_discount_percentage = 0.0;
546 me.wrapper.find('input.discount-percentage').val(0);
547 me.refresh();
548 });
549 },
550
551 customer_validate: function(){
552 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530553 if(!this.frm.doc.customer){
554 frappe.throw(__("Please select customer"))
555 }
556 },
557
558 add_to_cart: function() {
559 var me = this;
560 var caught = false;
561 var no_of_items = me.wrapper.find(".pos-bill-item").length;
562
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530563 this.customer_validate();
564 this.mandatory_batch_no();
565 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530566 this.validate_warehouse();
567
568 if (no_of_items != 0) {
569 $.each(this.frm.doc["items"] || [], function(i, d) {
570 if (d.item_code == me.items[0].item_code) {
571 caught = true;
572 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530573 d.amount = flt(d.rate) * flt(d.qty);
574 if(me.item_serial_no[d.item_code]){
575 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
576 d.warehouse = me.item_serial_no[d.item_code][1]
577 }
578
579 if(me.item_batch_no.length){
580 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530581 }
582 }
583 });
584 }
585
586 // if item not found then add new item
587 if (!caught)
588 this.add_new_item_to_grid();
589
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530590 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530591 },
592
593 add_new_item_to_grid: function() {
594 var me = this;
595 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
596 this.child.item_code = this.items[0].item_code;
597 this.child.item_name = this.items[0].item_name;
598 this.child.stock_uom = this.items[0].stock_uom;
599 this.child.description = this.items[0].description;
600 this.child.qty = 1;
601 this.child.item_group = this.items[0].item_group;
602 this.child.cost_center = this.items[0].cost_center;
603 this.child.income_account = this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530604 this.child.warehouse = (this.item_serial_no[this.child.item_code]
605 ? this.item_serial_no[this.child.item_code][1] : this.items[0].default_warehouse);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530606 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
607 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
608 this.child.actual_qty = this.items[0].actual_qty;
609 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530610 this.child.batch_no = this.item_batch_no[this.child.item_code];
611 this.child.serial_no = (this.item_serial_no[this.child.item_code]
612 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaure525900c2016-09-07 15:01:08 +0530613 this.child.item_tax_rate = this.items[0].taxes;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530614 },
615
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530616 update_paid_amount_status: function(update_paid_amount){
617 if(this.name){
618 update_paid_amount = update_paid_amount ? false : true;
619 }
620
621 this.refresh(update_paid_amount);
622 },
623
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530624 refresh: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530625 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530626 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530627 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530628 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530629 this.set_primary_action();
630 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530631
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530632 refresh_fields: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530633 this.apply_pricing_rule();
634 this.discount_amount_applied = false;
635 this._calculate_taxes_and_totals();
636 this.calculate_discount_amount();
637 this.show_items_in_item_cart();
638 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530639 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530640 this.set_totals();
641 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530642
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530643 get_company_currency: function() {
644 return erpnext.get_currency(this.frm.doc.company);
645 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530646
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530647 show_item_wise_taxes: function(){
648 return null;
649 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530650
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530651 show_items_in_item_cart: function() {
652 var me = this;
653 var $items = this.wrapper.find(".items").empty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530654 $.each(this.frm.doc.items|| [], function(i, d) {
655 $(frappe.render_template("pos_bill_item", {
656 item_code: d.item_code,
657 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
658 qty: d.qty,
659 actual_qty: d.actual_qty,
660 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530661 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530662 amount: format_currency(d.amount, me.frm.doc.currency)
663 })).appendTo($items);
664 });
665
666 this.wrapper.find("input.pos-item-qty").on("focus", function() {
667 $(this).select();
668 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530669
670 this.wrapper.find("input.pos-item-rate").on("focus", function() {
671 $(this).select();
672 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530673 },
674
675 set_taxes: function(){
676 var me = this;
677 me.frm.doc.total_taxes_and_charges = 0.0
678
679 var taxes = this.frm.doc.taxes || [];
680 $(this.wrapper)
681 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
682 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530683
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530684 $.each(taxes, function(i, d) {
685 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
686 $(frappe.render_template("pos_tax_row", {
687 description: d.description,
688 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
689 me.frm.doc.currency)
690 })).appendTo(me.wrapper.find(".tax-table"));
691 }
692 });
693 },
694
695 set_totals: function() {
696 var me = this;
697 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
698 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
699 },
700
701 set_primary_action: function() {
702 var me = this;
703
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530704 if (this.frm.doc.docstatus==0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530705 this.page.set_primary_action(__("Pay"), function() {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530706 me.validate();
707 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530708 me.create_invoice();
709 me.make_payment();
Faris Ansari8908d192016-07-20 13:59:19 +0530710 }, "octicon octicon-credit-card");
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530711 }else if(this.frm.doc.docstatus == 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530712 this.page.set_primary_action(__("Print"), function() {
713 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530714 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530715 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530716 }else {
717 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530718 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530719
720 this.page.set_secondary_action(__("New"), function() {
721 me.save_previous_entry();
722 me.create_new();
Faris Ansari8908d192016-07-20 13:59:19 +0530723 }, "octicon octicon-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530724 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530725
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530726 print_document: function(html){
727 var w = window.open();
728 w.document.write(html);
729 w.document.close();
730 setTimeout(function(){
731 w.print();
732 w.close();
733 }, 1000)
734 },
735
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530736 submit_invoice: function(){
737 var me = this;
738 frappe.confirm(__("Do you really want to submit the invoice?"), function () {
739 me.change_status();
Rohit Waghchaure53bea822016-07-06 16:09:26 +0530740 frappe.msgprint(__("Sales invoice submitted sucessfully."))
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530741 })
742 },
743
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530744 change_status: function(){
745 if(this.frm.doc.docstatus == 0){
746 this.frm.doc.docstatus = 1;
747 this.update_invoice();
748 this.disable_input_field();
749 }
750 },
751
752 disable_input_field: function(){
753 var pointer_events = 'inherit'
754 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530755
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530756 if(this.frm.doc.docstatus == 1){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530757 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530758 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530759 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530760
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530761 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
762 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
763 this.set_primary_action();
764 },
765
766 create_invoice: function(){
767 var me = this;
768 var invoice_data = {}
769 this.si_docs = this.get_doc_from_localstorage();
770 if(this.name){
771 this.update_invoice()
772 }else{
773 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +0530774 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +0530775 this.frm.doc.posting_date = frappe.datetime.get_today();
776 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530777 invoice_data[this.name] = this.frm.doc
778 this.si_docs.push(invoice_data)
779 this.update_localstorage();
780 this.set_primary_action();
781 }
782 },
783
784 update_invoice: function(){
785 var me = this;
786 this.si_docs = this.get_doc_from_localstorage();
787 $.each(this.si_docs, function(index, data){
788 for(key in data){
789 if(key == me.name){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530790 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530791 me.update_localstorage();
792 }
793 }
794 })
795 },
796
797 update_localstorage: function(){
798 try{
799 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
800 }catch(e){
801 frappe.throw(__("LocalStorage is full , did not save"))
802 }
803 },
804
805 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530806 try{
807 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
808 }catch(e){
809 return []
810 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530811 },
812
813 set_interval_for_si_sync: function(){
814 var me = this;
815 setInterval(function(){
816 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530817 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530818 },
819
820 sync_sales_invoice: function(){
821 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530822 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530823
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530824 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530825 frappe.call({
826 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
827 args: {
828 doc_list: me.si_docs
829 },
830 callback: function(r){
831 if(r.message){
832 me.removed_items = r.message;
833 me.remove_doc_from_localstorage();
834 }
835 }
836 })
837 }
838 },
839
840 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530841 var invoices = [];
842 var index = 1;
843 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530844 if(docs){
845 invoices = $.map(docs, function(data){
846 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530847 if(data[key].docstatus == 1 && index < 50){
848 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530849 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530850 return data
851 }
852 }
853 });
854 }
855
856 return invoices
857 },
858
859 remove_doc_from_localstorage: function(){
860 var me = this;
861 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530862 this.new_si_docs = [];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530863 if(this.removed_items){
864 $.each(this.si_docs, function(index, data){
865 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530866 if(!in_list(me.removed_items, key)){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530867 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530868 }
869 }
870 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530871 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530872 this.update_localstorage();
873 }
874 },
875
876 validate: function(){
877 var me = this;
878 this.customer_validate();
879 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530880 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530881 },
882
883 item_validate: function(){
884 if(this.frm.doc.items.length == 0){
885 frappe.throw(__("Select items to save the invoice"))
886 }
887 },
Saurabh9a6c6662016-06-27 16:51:44 +0530888
889 validate_mode_of_payments: function(){
890 if (this.frm.doc.payments.length === 0){
891 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
892 }
893 },
894
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530895 validate_serial_no: function(){
896 var me = this;
897 var item_code = serial_no = '';
898 for (key in this.item_serial_no){
899 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530900 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530901 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530902
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530903 if(item_code && serial_no){
904 $.each(this.frm.doc.items, function(index, data){
905 if(data.item_code == item_code){
906 if(in_list(data.serial_no.split('\n'), serial_no)){
907 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
908 'serial_no': serial_no
909 })))
910 }
911 }
912 })
913 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530914
915 if(this.items[0].has_serial_no && serial_no == ""){
916 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
917 'item': this.items[0].item_code
918 })))
919 }
920 },
921
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530922 validate_serial_no_qty: function(args, item_code, field, value){
923 var me = this;
924 if (args.item_code == item_code && args.serial_no
925 && field == 'qty' && cint(value) != value) {
926 args.qty = 0.0;
927 this.refresh();
928 frappe.throw(__("Serial no item cannot be a fraction"))
929 }
930
931 if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
932 args.qty = 0.0;
933 args.serial_no = ''
934 this.refresh();
935 frappe.throw(__("Total nos of serial no is not equal to quantity."))
936 }
937 },
938
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530939 mandatory_batch_no: function(){
940 var me = this;
941 if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
942 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
943 'item': this.items[0].item_code
944 })))
945 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530946 },
947
948 apply_pricing_rule: function(){
949 var me = this;
950 $.each(this.frm.doc["items"], function(n, item) {
951 pricing_rule = me.get_pricing_rule(item)
952 me.validate_pricing_rule(pricing_rule)
953 if(pricing_rule.length){
954 item.margin_type = pricing_rule[0].margin_type;
955 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
956 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
957 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
958 me.apply_pricing_rule_on_item(item)
959 }
960 })
961 },
962
963 get_pricing_rule: function(item){
964 var me = this;
965 return $.grep(this.pricing_rules, function(data){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530966 if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
967 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
968 if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
969 return me.validate_condition(data)
970 }else{
971 return true
972 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530973 }
974 }
975 })
976 },
977
978 validate_condition: function(data){
979 //This method check condition based on applicable for
980 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
981 if(in_list(condition[1], condition[0])){
982 return true
983 }
984 },
985
986 get_mapper_for_pricing_rule: function(data){
987 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +0530988 'Customer': [data.customer, [this.frm.doc.customer]],
989 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
990 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530991 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530992 }
993 },
994
995 validate_pricing_rule: function(pricing_rule){
996 //This method validate duplicate pricing rule
997 var pricing_rule_name = '';
998 var priority = 0;
999 var pricing_rule_list = [];
1000 var priority_list = []
1001
1002 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301003
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301004 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301005 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301006 priority_list.push(data.priority)
1007 if(priority <= data.priority){
1008 priority = data.priority
1009 pricing_rule_list.push(data)
1010 }
1011 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301012
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301013 count = 0
1014 $.each(priority_list, function(index, value){
1015 if(value == priority){
1016 count++
1017 }
1018 })
1019
1020 if(priority == 0 || count > 1){
1021 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1022 'pricing_rule': pricing_rule_name
1023 })))
1024 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301025
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301026 return pricing_rule_list
1027 }
1028 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301029
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301030 validate_warehouse: function(){
1031 if(!this.items[0].default_warehouse){
1032 frappe.throw(__("Deafault warehouse is required for selected item"))
1033 }
1034 }
1035})