blob: f72827dd5b389191b4c670893374262512102c74 [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
rohitwaghchaurec13dbd42017-02-01 18:07:22 +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
rohitwaghchaurec13dbd42017-02-01 18:07:22 +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
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053020erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053021 init: function (wrapper) {
22 this.page_len = 20;
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();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053028 this.si_docs = this.get_doc_from_localstorage();
29 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053030
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053031 beforeunload: function (e) {
32 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053033 e = e || window.event;
34
35 // For IE and Firefox prior to version 4
36 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053037 e.returnValue = __("You are in offline mode. You will not be able to reload until you have network.");
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053038 return
39 }
40
41 // For Safari
42 return __("You are in offline mode. You will not be able to reload until you have network.");
43 }
44 },
45
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053046 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053047 var me = this;
48 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053049 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053050 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053051 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053052 },
53
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053054 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053055 var me = this;
56 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053057 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053058 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053059 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053060 method: "frappe.handler.ping",
61 callback: function (r) {
62 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053063 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053064 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053065 }
66 }
67 })
68 },
69
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053070 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053071 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053072 this.get_data_from_server(function () {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053073 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053074 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053075 },
76
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053077 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053078 var me = this;
79
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053080 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053081 me.save_previous_entry();
82 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053083 })
84
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053085 this.page.add_menu_item(__("Sync Master Data"), function () {
86 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +053087 me.load_data(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053088 me.make_customer();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +053089 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +053090 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +053091 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053092 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053093
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053094 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
Rohit Waghchaure82be0202016-10-04 12:46:37 +053095 me.sync_sales_invoice()
96 });
97
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053098 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053099 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530100 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530101 },
102
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530103 dialog_actions: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530104 var me = this;
105
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530106 $(this.list_body).find('.list-column').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530107 me.name = $(this).parents().attr('invoice-name')
108 me.edit_record();
109 })
110
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530111 $(this.list_body).find('.list-select-all').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530112 me.removed_items = [];
113 $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked"))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530114 if ($(this).is(":checked")) {
115 $.each(me.si_docs, function (index, data) {
116 for (key in data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530117 me.removed_items.push(key)
118 }
119 })
120 }
121
122 me.toggle_primary_action();
123 })
124
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530125 $(this.list_body).find('.list-delete').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530126 me.name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530127 if ($(this).is(":checked")) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530128 me.removed_items.push(me.name);
129 } else {
130 me.removed_items.pop(me.name)
131 }
132
133 me.toggle_primary_action();
134 })
135 },
136
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530137 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530138 var me = this;
139
140 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530141 if (doc_data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530142 this.frm.doc = doc_data[0][this.name];
143 this.set_missing_values();
144 this.refresh(false);
145 this.disable_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530146 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530147 }
148 },
149
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530150 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530151 var me = this;
152 this.remove_doc_from_localstorage()
153 this.update_localstorage();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530154 this.dialog_actions();
155 this.toggle_primary_action();
156 },
157
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530158 toggle_primary_action: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530159 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530160 if(this.frm.doc.allow_delete) {
161 if (this.removed_items && this.removed_items.length > 0) {
162 $(this.wrapper).find('.btn-danger').show();
163 } else {
164 $(this.wrapper).find('.btn-danger').hide();
165 }
166 }
167 },
168
169 get_doctype_status: function (doc) {
170 if (doc.docstatus == 0) {
171 return { status: "Draft", indicator: "red" }
172 } else if (doc.outstanding_amount == 0) {
173 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530174 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530175 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530176 }
177 },
178
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530179 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530180 var me = this;
181 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530182 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530183 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530184 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530185 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530186
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530187 if (this.frm.doc.customer) {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530188 this.party_field.$input.val(this.frm.doc.customer);
189 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530190
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530191 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530192 this.frm.doc.write_off_account = doc.write_off_account
193 }
194
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530195 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530196 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530197 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530198 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530199
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530200 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530201 var me = this;
202 this.si_docs = this.get_doc_from_localstorage();
203
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530204 return $.grep(this.si_docs, function (data) {
205 for (key in data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530206 return key == me.name
207 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530208 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530209 },
210
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530211 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530212 var me = this;
213 frappe.call({
214 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
215 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530216 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530217 callback: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530218 me.init_master_data(r)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530219 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530220 me.set_interval_for_si_sync();
221 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530222 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530223 callback();
224 }
225 }
226 })
227 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530228
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530229 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530230 var me = this;
231 this.meta = r.message.meta;
232 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530233 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530234 this.customers = r.message.customers;
235 this.serial_no_data = r.message.serial_no_data;
236 this.batch_no_data = r.message.batch_no_data;
237 this.tax_data = r.message.tax_data;
238 this.price_list_data = r.message.price_list_data;
239 this.bin_data = r.message.bin_data;
240 this.pricing_rules = r.message.pricing_rules;
241 this.print_template = r.message.print_template;
242 this.pos_profile_data = r.message.pos_profile;
243 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530244 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530245 this.letter_head = (this.pos_profile_data.length > 0) ? frappe.boot.letter_heads[this.pos_profile_data[letter_head]] : {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530246 },
247
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530248 save_previous_entry: function () {
249 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
250 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530251 }
252 },
253
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530254 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530255 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530256 this.frm = {}
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530257 this.name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530258 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530259 this.setup();
260 },
261
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530262 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530263 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530264
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530265 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530266 this.actual_qty_dict = {};
267
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530268 if (load_doc) {
269 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530270 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530271
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530272 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530273 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530274 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530275 })
276
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530277 this.print_template_data = frappe.render_template("print_template", {
278 content: this.print_template,
279 title: "POS", base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css,
280 print_settings: this.print_settings, header: this.letter_head.header, footer: this.letter_head.footer
281 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530282 },
283
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530284 setup: function () {
285 this.frm.doc.allow_delete = this.pos_profile_data["allow_delete"];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530286 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
287 this.set_transaction_defaults("Customer");
288 this.make();
289 this.set_primary_action();
290 },
291
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530292 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530293 var me = this;
294 this.party = party;
295 this.price_list = (party == "Customer" ?
296 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
297 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
298 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
299 },
300
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530301 make: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530302 this.make_search();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530303 this.make_list_customers();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530304 this.make_customer();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530305 this.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530306 this.make_discount_field()
307 },
308
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530309 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530310 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530311 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530312 df: {
313 "fieldtype": "Data",
314 "label": "Item",
315 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530316 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530317 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530318 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530319 only_input: true,
320 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530321
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530322 this.serach_item.make_input();
323 this.serach_item.$input.on("keyup", function () {
324 setTimeout(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530325 me.items = me.get_items();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530326 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530327 }, 1000);
328 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530329
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530330 this.search_item_group = frappe.ui.form.make_control({
331 df: {
332 "fieldtype": "Select",
333 "options": me.item_groups,
334 "label": __("Item Group"),
335 "fieldname": "item_group",
336 "placeholder": __("Item Group")
337 },
338 parent: this.wrapper.find(".search-item-group"),
339 only_input: true,
340 });
341
342 this.search_item_group.make_input();
343 this.search_item_group.$input.on("change", function () {
344 me.page_len = 20;
345 me.items = me.get_items();
346 me.make_item_list();
347 });
348
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530349 this.party_field = frappe.ui.form.make_control({
350 df: {
351 "fieldtype": "Data",
352 "options": this.party,
353 "label": this.party,
354 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530355 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530356 },
357 parent: this.wrapper.find(".party-area"),
358 only_input: true,
359 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530360
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530361 this.party_field.make_input();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530362 setTimeout(this.set_focus.bind(this), 500);
363
364 this.wrapper.find(".btn-more").on("click", function() {
365 me.page_len += 20;
366 me.make_item_list();
367 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530368 },
369
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530370 make_list_customers: function () {
371 var me = this;
372 this.list_customers_btn = this.wrapper.find('.list-customers-btn');
373 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
374 this.pos_bill = this.wrapper.find('.pos-bill').hide();
375 this.list_customers = this.wrapper.find('.list-customers');
376
377 this.list_customers_btn.on('click', function () {
378 $(this).toggleClass("view_customer");
379 if($(this).hasClass("view_customer")) {
380 me.render_list_customers();
381 me.bind_delete_event()
382 me.party_field.$input.attr('disabled', true);
383 me.list_customers.show();
384 me.pos_bill.hide();
385 } else {
386 if(me.frm.doc.docstatus == 0) {
387 me.party_field.$input.attr('disabled', false);
388 }
389 me.pos_bill.show();
390 me.list_customers.hide()
391 }
392 });
393 this.add_customer_btn.on('click', function() {
394 me.save_previous_entry();
395 me.create_new();
396 me.refresh();
397 me.set_focus();
398 });
399 },
400
401 render_list_customers: function () {
402 var me = this;
403
404 this.removed_items = [];
405 this.list_customers.empty();
406 this.si_docs = this.get_doc_from_localstorage();
407
408 if (!this.si_docs.length) {
409 this.list_customers.append(
410 '<div style="padding: 12px; margin-left:-12px;">' + __("No offline records.") + '</div>'
411 )
412 return;
413 }
414 var html = '<div class="row list-row list-row-head pos-invoice-list">\
415 <div class="col-xs-1"><input class="list-select-all" type="checkbox"></div>\
416 <div class="col-xs-3">Customer</div>\
417 <div class="col-xs-2 text-left">Status</div>\
418 <div class="col-xs-3 text-right">Paid Amount</div>\
419 <div class="col-xs-3 text-right">Grand Total</div>\
420 </div>';
421 this.si_docs.forEach(function (data, i) {
422 for (key in data) {
423 html += frappe.render_template("pos_invoice_list", {
424 sr: i + 1,
425 name: key,
426 customer: data[key].customer,
427 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
428 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
429 data: me.get_doctype_status(data[key])
430 });
431 }
432 });
433 this.list_customers.append(html);
434
435 this.list_customers.find('.list-column').click(function () {
436 me.list_customers.hide();
437 me.list_customers_btn.toggleClass("view_customer");
438 me.pos_bill.show();
439 me.list_customers_btn.show();
440 me.name = $(this).parents().attr('invoice-name')
441 me.edit_record();
442 })
443
444 //actions
445 $(this.wrapper).find('.list-select-all').click(function () {
446 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
447 me.removed_items = [];
448 if ($(this).is(":checked")) {
449 $.each(me.si_docs, function (index, data) {
450 for (key in data) {
451 me.removed_items.push(key)
452 }
453 });
454 }
455
456 me.toggle_primary_action();
457 });
458
459 $(this.wrapper).find('.list-delete').click(function () {
460 me.name = $(this).parent().parent().attr('invoice-name');
461 if ($(this).is(":checked")) {
462 me.removed_items.push(me.name);
463 } else {
464 me.removed_items.pop(me.name)
465 }
466
467 me.toggle_primary_action();
468 });
469 },
470
471 bind_delete_event: function() {
472 var me = this;
473
474 $(this.wrapper).find('.btn-danger').click(function(){
475 frappe.confirm(__("Delete permanently?"), function () {
476 me.delete_records();
477 me.render_list_customers();
478 })
479 })
480 },
481
482 set_focus: function () {
483 if (this.default_customer || this.frm.doc.customer) {
484 this.serach_item.$input.focus();
485 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530486 this.party_field.$input.focus();
487 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530488 },
489
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530490 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530491 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530492
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530493 if (this.default_customer && !this.frm.doc.customer) {
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530494 this.party_field.$input.val(this.default_customer);
495 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530496 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530497
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530498 this.party_field.awesomeplete =
499 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530500 minChars: 0,
501 maxItems: 99,
502 autoFirst: true,
503 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530504 filter: function (item, input) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530505 var value = item.value.toLowerCase();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530506 if (value.indexOf('is_action') !== -1 ||
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530507 value.indexOf(input) !== -1) {
508 return true;
509 }
510 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530511 item: function (item, input) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530512 var d = item;
513 var html = "<span>" + __(d.label || d.value) + "</span>";
514 return $('<li></li>')
515 .data('item.autocomplete', d)
516 .html('<a><p>' + html + '</p></a>')
517 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530518 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530519 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530520 var customers = this.customers.map(function (c) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530521 return {
522 label: c.name,
523 value: c.name,
524 customer_group: c.customer_group,
525 territory: c.territory
526 }
527 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530528
529 customers.push({
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530530 label: "<span class='text-primary link-option'>"
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530531 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
532 + __("Create a new Customer")
533 + "</span>",
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530534 value: 'is_action',
535 action: me.new_customer
536 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530537 this.party_field.awesomeplete.list = customers;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530538
539 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530540 .on('input', function (e) {
541 me.party_field.awesomeplete.list = customers;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530542 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530543 .on('awesomplete-select', function (e) {
544 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530545 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530546 if (!customer) return;
547 // create customer link
548 if (customer.action) {
549 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530550 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530551 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530552 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530553 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530554 me.set_focus();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530555 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530556 .on('change', function (e) {
557 if (!e.originalEvent.text) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530558 me.frm.doc.customer = $(this).val();
559 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530560 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530561 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530562 $(e.target).val('').trigger('input');
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530563 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530564 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530565 var item = me.party_field.awesomeplete
566 .get_item(e.originalEvent.text.value);
567 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530568 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530569 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530570 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530571 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530572 },
573
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530574 new_customer: function () {
575 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530576 if (!this.connection_status) return;
577
578 this.customer_doc = new frappe.ui.Dialog({
579 'title': 'Customer',
580 fields: [
581 {
582 "label": __("Full Name"),
583 "fieldname": "full_name",
584 "fieldtype": "Data",
585 "reqd": 1
586 },
587 {
588 "fieldtype": "Section Break"
589 },
590 {
591 "label": __("Email Id"),
592 "fieldname": "email_id",
593 "fieldtype": "Data"
594 },
595 {
596 "fieldtype": "Column Break"
597 },
598 {
599 "label": __("Contact Number"),
600 "fieldname": "contact_no",
601 "fieldtype": "Data"
602 },
603 {
604 "fieldtype": "Section Break"
605 },
606 {
607 "label": __("Address Line 1"),
608 "fieldname": "address_line1",
609 "fieldtype": "Data"
610 },
611 {
612 "label": __("Address Line 2"),
613 "fieldname": "address_line2",
614 "fieldtype": "Data"
615 },
616 {
617 "fieldtype": "Column Break"
618 },
619 {
620 "label": __("City"),
621 "fieldname": "city",
622 "fieldtype": "Data"
623 },
624 {
625 "label": __("State"),
626 "fieldname": "state",
627 "fieldtype": "Data"
628 },
629 {
630 "label": __("ZIP Code"),
631 "fieldname": "zip_code",
632 "fieldtype": "Data"
633 }
634 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530635 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530636
637 this.customer_doc.show()
638
639 this.customer_doc.set_primary_action(__("Save"), function () {
640 me.make_offline_customer();
641 me.pos_bill.show();
642 });
643 },
644
645 make_offline_customer: function() {
646 this.frm.doc.customer = this.customer_doc.get_values().full_name;
647 this.frm.doc.contact_details = JSON.stringify(this.customer_doc.get_values());
648 this.party_field.$input.val(this.frm.doc.customer);
649 this.customers.push({
650 name: this.frm.doc.customer,
651 customer_name: this.frm.doc.customer
652 });
653
654 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530655 },
656
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530657 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530658 var me = this;
659 this.frm.doc.customer = doc.label || doc.name;
660 this.frm.doc.customer_name = doc.customer_name;
661 this.frm.doc.customer_group = doc.customer_group;
662 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530663 this.pos_bill.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530664 },
665
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530666 get_customers: function (key) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530667 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530668 key = key.toLowerCase().trim()
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530669 var re = new RegExp('%', 'g');
670 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'))
671
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530672 if (key) {
673 return $.grep(this.customers, function (data) {
674 if (reg.test(data.name.toLowerCase())
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530675 || reg.test(data.customer_name.toLowerCase())
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530676 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530677 return data
678 }
679 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530680 } else {
681 customers = this.customers.sort(function (a, b) { return a.idx < b.idx })
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530682 return customers.slice(0, 20)
683 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530684 },
685
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530686 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530687 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530688 if (!this.price_list) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530689 msgprint(__("Price List not found or disabled"));
690 return;
691 }
692
693 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530694
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530695 var $wrap = me.wrapper.find(".item-list");
696 me.wrapper.find(".item-list").empty();
697
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530698 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530699 $.each(this.items, function(index, obj) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530700 if(index < me.page_len){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530701 $(frappe.render_template("pos_item", {
702 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530703 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530704 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530705 item_image: obj.image ? "url('" + obj.image + "')" : null,
706 color: frappe.get_palette(obj.item_name),
707 abbr: frappe.get_abbr(obj.item_name)
708 })).tooltip().appendTo($wrap);
709 }
710 });
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530711 } else {
712 $("<h4>Searching record not found.</h4>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530713 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530714
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530715 if (this.items.length == 1
716 && this.serach_item.$input.val()) {
717 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530718 this.add_to_cart();
719 }
720
721 // if form is local then allow this function
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530722 $(me.wrapper).find("div.pos-item").on("click", function () {
723 if(me.list_customers_btn.hasClass("view_customer")) return;
724
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530725 me.customer_validate();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530726 if (me.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530727 me.items = me.get_items($(this).attr("data-item-code"))
728 me.add_to_cart();
729 }
730 });
731 },
732
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530733 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530734 // To search item as per the key enter
735
736 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530737 this.item_serial_no = {};
738 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530739
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530740 if (item_code) {
741 return $.grep(this.item_data, function (item) {
742 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530743 return true
744 }
745 })
746 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530747
748 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530749
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530750 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530751 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530752 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530753 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530754
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530755 if (key) {
756 return $.grep(this.items_list, function (item) {
757 if (search_status) {
758 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530759 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530760 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
761 } else if (me.serial_no_data[item.item_code]
762 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530763 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530764 me.item_serial_no[item.item_code] = [me.serach_item.$input.val(), me.serial_no_data[item.item_code][me.serach_item.$input.val()]]
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530765 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530766 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530767 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530768 return item.barcode == me.serach_item.$input.val();
769 } else if (reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
770 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530771 return true
772 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530773 }
774 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530775 } else {
776 return this.items_list;
777 }
778 },
779
780 apply_category: function() {
781 var me = this;
782 category = this.search_item_group.$input.val();
783
784 if(category == 'All Item Groups') {
785 return this.item_data
786 } else {
787 return this.item_data.filter(function(element, index, array){
788 return element.item_group == category;
789 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530790 }
791 },
792
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530793 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530794 var me = this;
795
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530796 $(this.wrapper).find(".pos-item-qty").on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530797 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +0530798 var qty = $(this).val();
799 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530800 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530801
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530802 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530803 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
804 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +0530805 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530806 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530807
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530808 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530809 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
810 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +0530811 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530812 })
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530813
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530814 $(this.wrapper).find(".pos-item-discount").on("change", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530815 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530816 var discount = $(this).val();
817 me.update_discount(item_code, discount)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530818 })
819 },
820
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530821 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530822 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530823 this.items = this.get_items(item_code);
824 this.validate_serial_no()
825 this.set_item_details(item_code, "qty", qty);
826 },
827
828 update_discount: function(item_code, discount) {
829 var me = this;
830 this.items = this.get_items(item_code);
831 this.set_item_details(item_code, "discount_percentage", discount);
832 },
833
834 update_rate: function () {
835 var me = this;
836
837 $(this.wrapper).find(".pos-item-rate").on("change", function () {
838 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
839 me.set_item_details(item_code, "rate", $(this).val());
840 })
841 },
842
843 set_item_details: function (item_code, field, value) {
844 var me = this;
845 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530846 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530847 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530848
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530849 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530850 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530851 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530852 if (d.serial_no && field == 'qty') {
853 me.validate_serial_no_qty(d, item_code, field, value)
854 }
855
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530856 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530857 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530858 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530859 me.remove_item.push(d.idx)
860 }
861 }
862 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530863
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530864 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530865 this.remove_zero_qty_item();
866 }
867
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530868 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530869 },
870
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530871 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530872 var me = this;
873 idx = 0
874 this.items = []
875 idx = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530876 $.each(this.frm.doc["items"] || [], function (i, d) {
877 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530878 d.idx = idx;
879 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530880 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530881 }
882 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530883
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530884 this.frm.doc["items"] = this.items;
885 },
886
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530887 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530888 var me = this;
889
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530890 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530891 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
892 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530893
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530894 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530895 total = me.frm.doc.net_total
896 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530897
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530898 me.frm.doc.discount_amount = flt(total * flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530899 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
900 me.refresh();
901 });
902
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530903 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530904 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
905 me.frm.doc.additional_discount_percentage = 0.0;
906 me.wrapper.find('input.discount-percentage').val(0);
907 me.refresh();
908 });
909 },
910
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530911 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530912 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530913 if (!this.frm.doc.customer) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530914 frappe.throw(__("Please select customer"))
915 }
916 },
917
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530918 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530919 var me = this;
920 var caught = false;
921 var no_of_items = me.wrapper.find(".pos-bill-item").length;
922
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530923 this.customer_validate();
924 this.mandatory_batch_no();
925 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530926 this.validate_warehouse();
927
928 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530929 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530930 if (d.item_code == me.items[0].item_code) {
931 caught = true;
932 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530933 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530934 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530935 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
936 d.warehouse = me.item_serial_no[d.item_code][1]
937 }
938
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530939 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530940 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530941 }
942 }
943 });
944 }
945
946 // if item not found then add new item
947 if (!caught)
948 this.add_new_item_to_grid();
949
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530950 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530951 },
952
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530953 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530954 var me = this;
955 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
956 this.child.item_code = this.items[0].item_code;
957 this.child.item_name = this.items[0].item_name;
958 this.child.stock_uom = this.items[0].stock_uom;
959 this.child.description = this.items[0].description;
960 this.child.qty = 1;
961 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530962 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
963 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530964 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530965 ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530966 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
967 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
968 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530969 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530970 this.child.batch_no = this.item_batch_no[this.child.item_code];
971 this.child.serial_no = (this.item_serial_no[this.child.item_code]
972 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530973 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530974 },
975
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530976 update_paid_amount_status: function (update_paid_amount) {
977 if (this.name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530978 update_paid_amount = update_paid_amount ? false : true;
979 }
980
981 this.refresh(update_paid_amount);
982 },
983
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530984 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530985 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530986 this.refresh_fields(update_paid_amount);
Rohit Waghchauredd58d532016-12-21 17:23:01 +0530987 this.bind_qty_event();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530988 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530989 this.set_primary_action();
990 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530991
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530992 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530993 this.apply_pricing_rule();
994 this.discount_amount_applied = false;
995 this._calculate_taxes_and_totals();
996 this.calculate_discount_amount();
997 this.show_items_in_item_cart();
998 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530999 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301000 this.set_totals();
1001 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301002
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301003 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301004 return erpnext.get_currency(this.frm.doc.company);
1005 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301006
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301007 show_item_wise_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301008 return null;
1009 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301010
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301011 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301012 var me = this;
1013 var $items = this.wrapper.find(".items").empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301014 $.each(this.frm.doc.items || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301015 $(frappe.render_template("pos_bill_item", {
1016 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301017 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301018 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301019 discount_percentage: d.discount_percentage || 0.0,
1020 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301021 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301022 rate: format_number(d.rate, me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301023 enabled: me.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301024 amount: format_currency(d.amount, me.frm.doc.currency)
1025 })).appendTo($items);
1026 });
1027
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301028 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301029 $(this).select();
1030 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301031
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301032 this.wrapper.find("input.pos-item-discount").on("focus", function () {
1033 $(this).select();
1034 });
1035
1036 this.wrapper.find("input.pos-item-rate").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301037 $(this).select();
1038 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301039 },
1040
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301041 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301042 var me = this;
1043 me.frm.doc.total_taxes_and_charges = 0.0
1044
1045 var taxes = this.frm.doc.taxes || [];
1046 $(this.wrapper)
1047 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1048 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301049
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301050 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301051 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1052 $(frappe.render_template("pos_tax_row", {
1053 description: d.description,
1054 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1055 me.frm.doc.currency)
1056 })).appendTo(me.wrapper.find(".tax-table"));
1057 }
1058 });
1059 },
1060
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301061 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301062 var me = this;
1063 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1064 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
1065 },
1066
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301067 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301068 var me = this;
1069
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301070 if (this.frm.doc.docstatus == 0) {
1071 this.page.set_primary_action(__("Pay"), function () {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301072 me.validate();
1073 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301074 me.create_invoice();
1075 me.make_payment();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301076 }, "fa fa-credit-card");
1077 } else if (this.frm.doc.docstatus == 1) {
1078 this.page.set_primary_action(__("Print"), function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +05301079 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301080 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301081 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301082 } else {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301083 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301084 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301085
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301086 // this.page.set_secondary_action(__("New"), function () {
1087 // me.save_previous_entry();
1088 // me.create_new();
1089 // }, "fa fa-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301090 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301091
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301092 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301093 var me = this;
1094
1095 msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
1096 style="margin-right: 5px;">{0}</a>\
1097 <a class="btn btn-default new_doc">{1}</a>', [
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301098 __('Print'), __('New')
1099 ]));
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301100
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301101 $('.print_doc').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +05301102 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301103 me.print_document(html)
1104 })
1105
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301106 $('.new_doc').click(function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301107 msgprint.hide()
1108 me.create_new();
1109 })
1110 },
1111
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301112 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301113 var w = window.open();
1114 w.document.write(html);
1115 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301116 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301117 w.print();
1118 w.close();
1119 }, 1000)
1120 },
1121
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301122 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301123 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301124 this.change_status();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301125 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301126 this.print_dialog()
1127 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301128 },
1129
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301130 change_status: function () {
1131 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301132 this.frm.doc.docstatus = 1;
1133 this.update_invoice();
1134 this.disable_input_field();
1135 }
1136 },
1137
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301138 disable_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301139 var pointer_events = 'inherit'
1140 $(this.wrapper).find('input').attr("disabled", false);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301141 $(this.wrapper).find('select').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301142
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301143 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301144 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301145 $(this.wrapper).find('input').attr("disabled", true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301146 $(this.wrapper).find('select').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301147 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301148
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301149 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301150 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1151 this.set_primary_action();
1152 },
1153
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301154 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301155 var me = this;
1156 var invoice_data = {}
1157 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301158 if (this.name) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301159 this.update_invoice()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301160 } else {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301161 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +05301162 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301163 this.frm.doc.posting_date = frappe.datetime.get_today();
1164 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301165 invoice_data[this.name] = this.frm.doc
1166 this.si_docs.push(invoice_data)
1167 this.update_localstorage();
1168 this.set_primary_action();
1169 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301170 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301171 },
1172
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301173 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301174 var me = this;
1175 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301176 $.each(this.si_docs, function (index, data) {
1177 for (key in data) {
1178 if (key == me.name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301179 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301180 me.update_localstorage();
1181 }
1182 }
1183 })
1184 },
1185
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301186 update_localstorage: function () {
1187 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301188 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301189 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301190 frappe.throw(__("LocalStorage is full , did not save"))
1191 }
1192 },
1193
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301194 get_doc_from_localstorage: function () {
1195 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301196 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301197 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301198 return []
1199 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301200 },
1201
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301202 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301203 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301204 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301205 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301206 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301207 },
1208
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301209 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301210 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301211 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301212
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301213 if (this.si_docs.length) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301214 frappe.call({
1215 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
1216 args: {
1217 doc_list: me.si_docs
1218 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301219 callback: function (r) {
1220 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301221 me.removed_items = r.message;
1222 me.remove_doc_from_localstorage();
1223 }
1224 }
1225 })
1226 }
1227 },
1228
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301229 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301230 var invoices = [];
1231 var index = 1;
1232 docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301233 if (docs) {
1234 invoices = $.map(docs, function (data) {
1235 for (key in data) {
1236 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301237 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301238 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301239 return data
1240 }
1241 }
1242 });
1243 }
1244
1245 return invoices
1246 },
1247
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301248 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301249 var me = this;
1250 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301251 this.new_si_docs = [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301252 if (this.removed_items) {
1253 $.each(this.si_docs, function (index, data) {
1254 for (key in data) {
1255 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301256 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301257 }
1258 }
1259 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301260 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301261 this.update_localstorage();
1262 }
1263 },
1264
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301265 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301266 var me = this;
1267 this.customer_validate();
1268 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301269 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301270 },
1271
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301272 item_validate: function () {
1273 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301274 frappe.throw(__("Select items to save the invoice"))
1275 }
1276 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301277
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301278 validate_mode_of_payments: function () {
1279 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301280 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1281 }
1282 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301283
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301284 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301285 var me = this;
1286 var item_code = serial_no = '';
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301287 for (key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301288 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301289 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301290 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301291
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301292 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301293 this.refresh();
1294 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1295 'item': this.items[0].item_code
1296 })))
1297 }
1298
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301299 if (item_code && serial_no) {
1300 $.each(this.frm.doc.items, function (index, data) {
1301 if (data.item_code == item_code) {
1302 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301303 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1304 'serial_no': serial_no
1305 })))
1306 }
1307 }
1308 })
1309 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301310 },
1311
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301312 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301313 var me = this;
1314 if (args.item_code == item_code && args.serial_no
1315 && field == 'qty' && cint(value) != value) {
1316 args.qty = 0.0;
1317 this.refresh();
1318 frappe.throw(__("Serial no item cannot be a fraction"))
1319 }
1320
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301321 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301322 args.qty = 0.0;
1323 args.serial_no = ''
1324 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301325 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1326 'item': item_code
1327 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301328 }
1329 },
1330
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301331 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301332 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301333 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301334 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
1335 'item': this.items[0].item_code
1336 })))
1337 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301338 },
1339
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301340 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301341 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301342 $.each(this.frm.doc["items"], function (n, item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301343 pricing_rule = me.get_pricing_rule(item)
1344 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301345 if (pricing_rule.length) {
1346 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301347 item.margin_type = pricing_rule[0].margin_type;
1348 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1349 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1350 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301351 } else if ((item.discount_percentage > 0 || item.margin_rate_or_amount > 0) && item.pricing_rule) {
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301352 item.margin_rate_or_amount = 0.0;
1353 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301354 item.pricing_rule = null;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301355 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301356
1357 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301358 })
1359 },
1360
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301361 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301362 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301363 return $.grep(this.pricing_rules, function (data) {
1364 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
1365 if (data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
1366 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301367 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301368 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301369 return true
1370 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301371 }
1372 }
1373 })
1374 },
1375
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301376 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301377 //This method check condition based on applicable for
1378 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301379 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301380 return true
1381 }
1382 },
1383
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301384 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301385 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301386 'Customer': [data.customer, [this.frm.doc.customer]],
1387 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1388 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301389 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301390 }
1391 },
1392
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301393 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301394 //This method validate duplicate pricing rule
1395 var pricing_rule_name = '';
1396 var priority = 0;
1397 var pricing_rule_list = [];
1398 var priority_list = []
1399
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301400 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301401
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301402 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301403 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301404 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301405 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301406 priority = data.priority
1407 pricing_rule_list.push(data)
1408 }
1409 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301410
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301411 count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301412 $.each(priority_list, function (index, value) {
1413 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301414 count++
1415 }
1416 })
1417
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301418 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301419 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1420 'pricing_rule': pricing_rule_name
1421 })))
1422 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301423
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301424 return pricing_rule_list
1425 }
1426 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301427
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301428 validate_warehouse: function () {
1429 if (this.items[0].is_stock_item && !this.items[0].default_warehouse && !this.pos_profile_data['warehouse']) {
Neil Trini Lasrado5ddd7ac2016-10-24 15:25:33 +05301430 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301431 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301432 },
1433
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301434 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301435 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301436
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301437 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301438 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301439 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301440 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301441 }
1442
1443 return this.actual_qty
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301444 }
1445})