blob: cfae51571d86a5594a78b5f34640fa7b70af1d91 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
3
Rushabh Mehta4a008f52013-07-29 15:31:11 +05304erpnext.POS = Class.extend({
5 init: function(wrapper, frm) {
6 this.wrapper = wrapper;
7 this.frm = frm;
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +05308 this.wrapper.html(frappe.render_template("pos", {}));
Nabin Haited8a8452014-05-05 11:01:32 +05309
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053010 this.check_transaction_type();
Rushabh Mehta4a008f52013-07-29 15:31:11 +053011 this.make();
12
13 var me = this;
14 $(this.frm.wrapper).on("refresh-fields", function() {
15 me.refresh();
16 });
17
Akhilesh Darjee57738a02014-01-03 18:15:07 +053018 this.wrapper.find('input.discount-amount').on("change", function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053019 frappe.model.set_value(me.frm.doctype, me.frm.docname, "discount_amount", this.value);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +053020 });
21
Nabin Haitdc15b4f2014-01-20 16:48:49 +053022 this.call_function("remove-items", function() {me.remove_selected_items();});
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053023 this.call_function("make-payment", function() {me.make_payment();});
24 },
25 check_transaction_type: function() {
26 var me = this;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053027
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053028 // Check whether the transaction is "Sales" or "Purchase"
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053029 if (frappe.meta.has_field(cur_frm.doc.doctype, "customer")) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053030 this.set_transaction_defaults("Customer", "export");
31 }
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053032 else if (frappe.meta.has_field(cur_frm.doc.doctype, "supplier")) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053033 this.set_transaction_defaults("Supplier", "import");
34 }
35 },
36 set_transaction_defaults: function(party, export_or_import) {
37 var me = this;
38 this.party = party;
Nabin Haited8a8452014-05-05 11:01:32 +053039 this.price_list = (party == "Customer" ?
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053040 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
Anand Doshicb39e082014-01-24 21:44:36 +053041 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053042 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
43 this.net_total = "net_total_" + export_or_import;
44 this.grand_total = "grand_total_" + export_or_import;
Nabin Hait1eb56012014-02-10 19:20:15 +053045 // this.amount = export_or_import + "_amount";
46 // this.rate = export_or_import + "_rate";
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053047 },
48 call_function: function(class_name, fn, event_name) {
49 this.wrapper.find("." + class_name).on(event_name || "click", fn);
Rushabh Mehta4a008f52013-07-29 15:31:11 +053050 },
51 make: function() {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053052 this.make_party();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053053 this.make_barcode();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +053054 this.make_search();
55 this.make_item_group();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053056 this.make_item_list();
Rushabh Mehta4a008f52013-07-29 15:31:11 +053057 },
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053058 make_party: function() {
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053059 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053060 this.party_field = frappe.ui.form.make_control({
Rushabh Mehta4a008f52013-07-29 15:31:11 +053061 df: {
62 "fieldtype": "Link",
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053063 "options": this.party,
64 "label": this.party,
65 "fieldname": "pos_party",
66 "placeholder": this.party
Rushabh Mehta4a008f52013-07-29 15:31:11 +053067 },
Rushabh Mehta069672e2013-10-28 18:21:07 +053068 parent: this.wrapper.find(".party-area"),
69 only_input: true,
Rushabh Mehta4a008f52013-07-29 15:31:11 +053070 });
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053071 this.party_field.make_input();
72 this.party_field.$input.on("change", function() {
73 if(!me.party_field.autocomplete_open)
Nabin Haited8a8452014-05-05 11:01:32 +053074 frappe.model.set_value(me.frm.doctype, me.frm.docname,
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053075 me.party.toLowerCase(), this.value);
Rushabh Mehta4a008f52013-07-29 15:31:11 +053076 });
77 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +053078 make_barcode: function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053079 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053080 this.barcode = frappe.ui.form.make_control({
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053081 df: {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +053082 "fieldtype": "Data",
83 "label": "Barcode",
84 "fieldname": "pos_barcode",
85 "placeholder": "Barcode / Serial No"
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053086 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +053087 parent: this.wrapper.find(".barcode-area"),
Rushabh Mehta069672e2013-10-28 18:21:07 +053088 only_input: true,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053089 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +053090 this.barcode.make_input();
91 this.barcode.$input.on("keypress", function() {
92 if(me.barcode_timeout)
93 clearTimeout(me.barcode_timeout);
94 me.barcode_timeout = setTimeout(function() { me.add_item_thru_barcode(); }, 1000);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053095 });
96 },
97 make_search: function() {
98 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053099 this.search = frappe.ui.form.make_control({
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530100 df: {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530101 "fieldtype": "Data",
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530102 "label": "Item",
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530103 "fieldname": "pos_item",
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530104 "placeholder": "Search Item"
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530105 },
Rushabh Mehta069672e2013-10-28 18:21:07 +0530106 parent: this.wrapper.find(".search-area"),
107 only_input: true,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530108 });
109 this.search.make_input();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530110 this.search.$input.on("keypress", function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530111 if(!me.search.autocomplete_open)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530112 if(me.item_timeout)
113 clearTimeout(me.item_timeout);
114 me.item_timeout = setTimeout(function() { me.make_item_list(); }, 1000);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530115 });
116 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530117 make_item_group: function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530118 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530119 this.item_group = frappe.ui.form.make_control({
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530120 df: {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530121 "fieldtype": "Link",
122 "options": "Item Group",
123 "label": "Item Group",
124 "fieldname": "pos_item_group",
125 "placeholder": "Item Group"
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530126 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530127 parent: this.wrapper.find(".item-group-area"),
Rushabh Mehta069672e2013-10-28 18:21:07 +0530128 only_input: true,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530129 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530130 this.item_group.make_input();
131 this.item_group.$input.on("change", function() {
132 if(!me.item_group.autocomplete_open)
133 me.make_item_list();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530134 });
135 },
136 make_item_list: function() {
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +0530137 var me = this;
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530138 me.item_timeout = null;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530139 frappe.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530140 method: 'erpnext.accounts.doctype.sales_invoice.pos.get_items',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530141 args: {
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530142 sales_or_purchase: this.sales_or_purchase,
143 price_list: this.price_list,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530144 item_group: this.item_group.$input.val(),
145 item: this.search.$input.val()
146 },
147 callback: function(r) {
148 var $wrap = me.wrapper.find(".item-list");
149 me.wrapper.find(".item-list").empty();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530150 if (r.message) {
151 $.each(r.message, function(index, obj) {
152 if (obj.image)
153 image = '<img src="' + obj.image + '" class="img-responsive" \
154 style="border:1px solid #eee; max-height: 140px;">';
155 else
156 image = '<div class="missing-image"><i class="icon-camera"></i></div>';
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530157
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530158 $(repl('<div class="col-xs-3 pos-item" data-item_code="%(item_code)s">\
159 <div style="height: 140px; overflow: hidden;">%(item_image)s</div>\
160 <div class="small">%(item_code)s</div>\
161 <div class="small">%(item_name)s</div>\
162 <div class="small">%(item_price)s</div>\
Nabin Haited8a8452014-05-05 11:01:32 +0530163 </div>',
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530164 {
165 item_code: obj.name,
Nabin Haita7f757a2014-02-10 17:54:04 +0530166 item_price: format_currency(obj.price_list_rate, obj.currency),
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530167 item_name: obj.name===obj.item_name ? "" : obj.item_name,
168 item_image: image
169 })).appendTo($wrap);
170 });
171 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530172
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530173 // if form is local then allow this function
Nabin Haitcf301182013-10-01 18:14:16 +0530174 $(me.wrapper).find("div.pos-item").on("click", function() {
175 if(me.frm.doc.docstatus==0) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530176 console.log($(this).attr("data-item_code"));
177 me.add_to_cart($(this).attr("data-item_code"));
Nabin Haitcf301182013-10-01 18:14:16 +0530178 }
179 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530180 }
181 });
182 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530183 add_to_cart: function(item_code, serial_no) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530184 var me = this;
185 var caught = false;
186
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530187 if(!me.frm.doc[me.party.toLowerCase()] && ((me.frm.doctype == "Quotation" &&
188 me.frm.doc.quotation_to == "Customer")
189 || me.frm.doctype != "Quotation")) {
190 msgprint(__("Please select {0} first.", [me.party]));
191 return;
192 }
193
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530194 // get no_of_items
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530195 var no_of_items = me.wrapper.find(".pos-bill-item").length;
Nabin Haited8a8452014-05-05 11:01:32 +0530196
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530197 // check whether the item is already added
198 if (no_of_items != 0) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530199 $.each(this.frm.doc[this.frm.cscript.fname] || [], function(i, d) {
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530200 if (d.item_code == item_code) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530201 caught = true;
Anand Doshicb39e082014-01-24 21:44:36 +0530202 if (serial_no)
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530203 frappe.model.set_value(d.doctype, d.name, "serial_no", d.serial_no + '\n' + serial_no);
Anand Doshicb39e082014-01-24 21:44:36 +0530204 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530205 frappe.model.set_value(d.doctype, d.name, "qty", d.qty + 1);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530206 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530207 });
208 }
Nabin Haited8a8452014-05-05 11:01:32 +0530209
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530210 // if item not found then add new item
Anand Doshicb39e082014-01-24 21:44:36 +0530211 if (!caught)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530212 this.add_new_item_to_grid(item_code, serial_no);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530213
214 this.refresh();
215 this.refresh_search_box();
216 },
217 add_new_item_to_grid: function(item_code, serial_no) {
218 var me = this;
219
Nabin Haited8a8452014-05-05 11:01:32 +0530220 var child = frappe.model.add_child(me.frm.doc, this.frm.doctype + " Item",
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530221 this.frm.cscript.fname);
222 child.item_code = item_code;
223
224 if (serial_no)
225 child.serial_no = serial_no;
226
227 this.frm.script_manager.trigger("item_code", child.doctype, child.name);
228 },
229 refresh_search_box: function() {
230 var me = this;
231
232 // Clear Item Box and remake item list
233 if (this.search.$input.val()) {
234 this.search.set_input("");
235 this.make_item_list();
236 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530237 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530238 update_qty: function(item_code, qty) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530239 console.log([item_code, qty]);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530240 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530241 $.each(this.frm.doc[this.frm.cscript.fname] || [], function(i, d) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530242 if (d.item_code == item_code) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530243 if (qty == 0) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530244 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530245 me.refresh_grid();
246 } else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530247 frappe.model.set_value(d.doctype, d.name, "qty", qty);
Akhilesh Darjeed2714312013-08-28 19:35:22 +0530248 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530249 }
250 });
Anand Doshicb39e082014-01-24 21:44:36 +0530251 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530252 },
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530253 refresh: function() {
254 var me = this;
Anand Doshicb39e082014-01-24 21:44:36 +0530255
256 this.refresh_item_list();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530257 this.party_field.set_input(this.frm.doc[this.party.toLowerCase()]);
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530258 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530259 this.barcode.set_input("");
260
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530261 this.show_items_in_item_cart();
262 this.show_taxes();
263 this.set_totals();
264
265 // if form is local then only run all these functions
266 if (this.frm.doc.docstatus===0) {
267 this.call_when_local();
268 }
269
270 this.disable_text_box_and_button();
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530271 this.hide_payment_button();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530272
273 // If quotation to is not Customer then remove party
Rushabh Mehta72e17192014-08-08 15:30:49 +0530274 if (this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer") {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530275 this.party_field.$input.prop("disabled", true);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530276 }
277 },
Anand Doshicb39e082014-01-24 21:44:36 +0530278 refresh_item_list: function() {
279 var me = this;
280 // refresh item list on change of price list
281 if (this.frm.doc[this.price_list_field] != this.price_list) {
282 this.price_list = this.frm.doc[this.price_list_field];
283 this.make_item_list();
284 }
285 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530286 show_items_in_item_cart: function() {
287 var me = this;
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530288 var $items = this.wrapper.find(".items").empty();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530289
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530290 $.each(this.frm.doc[this.frm.cscript.fname] || [], function(i, d) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530291 $(frappe.render_template("pos_bill_item", {
292 item_code: d.item_code,
293 item_name: d.item_name===d.item_code ? "" : ("<br>" + d.item_name),
294 qty: d.qty,
295 actual_qty: d.actual_qty,
296 rate: format_currency(d.rate, me.frm.doc.currency),
297 amount: format_currency(d.amount, me.frm.doc.currency)
298 })).appendTo($items);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530299 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530300
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530301 this.wrapper.find("input.pos-item-qty").on("focus", function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530302 $(this).select();
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530303 });
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530304 },
305 show_taxes: function() {
306 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530307 var taxes = this.frm.doc[this.frm.cscript.other_fname] || [];
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530308 $(this.wrapper).find(".tax-table")
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530309 .toggle((taxes && taxes.length) ? true : false)
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530310 .find("tbody").empty();
Nabin Haited8a8452014-05-05 11:01:32 +0530311
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530312 $.each(taxes, function(i, d) {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530313 if (d.tax_amount) {
314 $(repl('<tr>\
315 <td>%(description)s %(rate)s</td>\
316 <td style="text-align: right;">%(tax_amount)s</td>\
317 <tr>', {
318 description: d.description,
319 rate: ((d.charge_type == "Actual") ? '' : ("(" + d.rate + "%)")),
Nabin Haited8a8452014-05-05 11:01:32 +0530320 tax_amount: format_currency(flt(d.tax_amount)/flt(me.frm.doc.conversion_rate),
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530321 me.frm.doc.currency)
322 })).appendTo(".tax-table tbody");
323 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530324 });
325 },
326 set_totals: function() {
327 var me = this;
Nabin Haited8a8452014-05-05 11:01:32 +0530328 this.wrapper.find(".net-total").text(format_currency(this.frm.doc[this.net_total],
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530329 me.frm.doc.currency));
Nabin Haited8a8452014-05-05 11:01:32 +0530330 this.wrapper.find(".grand-total").text(format_currency(this.frm.doc[this.grand_total],
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530331 me.frm.doc.currency));
Nabin Haited8a8452014-05-05 11:01:32 +0530332
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530333 $(".paid-amount-area").toggle(!!this.frm.doc.paid_amount);
334 if(this.frm.doc.paid_amount) {
Nabin Haited8a8452014-05-05 11:01:32 +0530335 this.wrapper.find(".paid-amount").text(format_currency(this.frm.doc.paid_amount,
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530336 me.frm.doc.currency));
337 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530338 },
339 call_when_local: function() {
340 var me = this;
341
342 // append quantity to the respective item after change from input box
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530343 $(this.wrapper).find("input.pos-item-qty").on("change", function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530344 var item_code = $(this).closest("tr").attr("id");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530345 me.update_qty(item_code, $(this).val());
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530346 });
347
Anand Doshicb39e082014-01-24 21:44:36 +0530348 // increase/decrease qty on plus/minus button
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530349 $(this.wrapper).find(".pos-qty-btn").on("click", function() {
350 var $item = $(this).parents(".pos-bill-item:first");
351 me.increase_decrease_qty($item, $(this).attr("data-action"));
Anand Doshicb39e082014-01-24 21:44:36 +0530352 });
353
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530354 // on td click toggle the highlighting of row
355 $(this.wrapper).find("#cart tbody tr td").on("click", function() {
356 var row = $(this).closest("tr");
357 if (row.attr("data-selected") == "false") {
358 row.attr("class", "warning");
359 row.attr("data-selected", "true");
360 }
361 else {
362 row.prop("class", null);
363 row.attr("data-selected", "false");
364 }
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530365 me.refresh_delete_btn();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530366 });
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530367
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530368 me.refresh_delete_btn();
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530369 //me.focus();
370 },
371 focus: function() {
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530372 if(me.frm.doc[this.party]) {
373 this.barcode.$input.focus();
374 } else {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530375 if(!(this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer"))
376 this.party_field.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530377 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530378 },
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530379 increase_decrease_qty: function($item, operation) {
380 var item_code = $item.attr("data-item-code");
381 var item_qty = cint($item.find("input.pos-item-qty").val());
Anand Doshicb39e082014-01-24 21:44:36 +0530382
383 if (operation == "increase-qty")
384 this.update_qty(item_code, item_qty + 1);
Rushabh Mehta72e17192014-08-08 15:30:49 +0530385 else if (operation == "decrease-qty" && item_qty != 0)
Anand Doshicb39e082014-01-24 21:44:36 +0530386 this.update_qty(item_code, item_qty - 1);
387 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530388 disable_text_box_and_button: function() {
389 var me = this;
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530390 // if form is submitted & cancelled then disable all input box & buttons
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530391 $(this.wrapper)
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530392 .find(".remove-items, .make-payment, .pos-qty-btn")
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530393 .toggle(this.frm.doc.docstatus===0);
Nabin Haited8a8452014-05-05 11:01:32 +0530394
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530395 $(this.wrapper).find('input, button').prop("disabled", !(this.frm.doc.docstatus===0));
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530396 },
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530397 hide_payment_button: function() {
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530398 $(this.wrapper)
399 .find(".make-payment")
400 .toggle(this.frm.doctype == "Sales Invoice" && this.frm.doc.is_pos);
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530401 },
402 refresh_delete_btn: function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530403 $(this.wrapper).find(".remove-items").toggle($(".item-cart .warning").length ? true : false);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530404 },
405 add_item_thru_barcode: function() {
406 var me = this;
Akhilesh Darjeed2714312013-08-28 19:35:22 +0530407 me.barcode_timeout = null;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530408 frappe.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530409 method: 'erpnext.accounts.doctype.sales_invoice.pos.get_item_code',
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530410 args: {barcode_serial_no: this.barcode.$input.val()},
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530411 callback: function(r) {
412 if (r.message) {
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530413 if (r.message[1] == "serial_no")
414 me.add_to_cart(r.message[0][0].item_code, r.message[0][0].name);
415 else
416 me.add_to_cart(r.message[0][0].name);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530417 }
418 else
Pratik Vyasb52618c2014-04-14 16:25:30 +0530419 msgprint(__("Invalid Barcode"));
Akhilesh Darjee1f0b8602013-08-23 18:22:32 +0530420
421 me.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530422 }
423 });
424 },
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530425 remove_selected_items: function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530426 var me = this;
427 var selected_items = [];
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530428 var no_of_items = $(this.wrapper).find("#cart tbody tr").length;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530429 for(var x=0; x<=no_of_items - 1; x++) {
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530430 var row = $(this.wrapper).find("#cart tbody tr:eq(" + x + ")");
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530431 if(row.attr("data-selected") == "true") {
432 selected_items.push(row.attr("id"));
433 }
434 }
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530435
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530436 var child = this.frm.doc[this.frm.cscript.fname] || [];
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530437
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530438 $.each(child, function(i, d) {
439 for (var i in selected_items) {
440 if (d.item_code == selected_items[i]) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530441 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530442 }
443 }
444 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530445
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530446 this.refresh_grid();
447 },
448 refresh_grid: function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530449 this.frm.dirty();
Akhilesh Darjee2428e8d2013-09-27 12:32:26 +0530450 this.frm.fields_dict[this.frm.cscript.fname].grid.refresh();
451 this.frm.script_manager.trigger("calculate_taxes_and_totals");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530452 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530453 },
454 make_payment: function() {
455 var me = this;
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530456 var no_of_items = $(this.wrapper).find("#cart tbody tr").length;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530457 var mode_of_payment = [];
Nabin Haited8a8452014-05-05 11:01:32 +0530458
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530459 if (no_of_items == 0)
Pratik Vyasb52618c2014-04-14 16:25:30 +0530460 msgprint(__("Payment cannot be made for empty cart"));
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530461 else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530462 frappe.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530463 method: 'erpnext.accounts.doctype.sales_invoice.pos.get_mode_of_payment',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530464 callback: function(r) {
Nabin Haited8a8452014-05-05 11:01:32 +0530465 if(!r.message) {
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530466 msgprint(__("Please add to Modes of Payment from Setup."))
Nabin Haited8a8452014-05-05 11:01:32 +0530467 return;
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530468 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530469 for (x=0; x<=r.message.length - 1; x++) {
470 mode_of_payment.push(r.message[x].name);
471 }
472
473 // show payment wizard
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530474 var dialog = new frappe.ui.Dialog({
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530475 width: 400,
Nabin Haited8a8452014-05-05 11:01:32 +0530476 title: 'Payment',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530477 fields: [
478 {fieldtype:'Data', fieldname:'total_amount', label:'Total Amount', read_only:1},
Nabin Haited8a8452014-05-05 11:01:32 +0530479 {fieldtype:'Select', fieldname:'mode_of_payment', label:'Mode of Payment',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530480 options:mode_of_payment.join('\n'), reqd: 1},
481 {fieldtype:'Button', fieldname:'pay', label:'Pay'}
482 ]
483 });
484 dialog.set_values({
485 "total_amount": $(".grand-total").text()
486 });
487 dialog.show();
Anand Doshic8e39b02013-08-30 18:22:58 +0530488 dialog.get_input("total_amount").prop("disabled", true);
Nabin Haited8a8452014-05-05 11:01:32 +0530489
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530490 dialog.fields_dict.pay.input.onclick = function() {
Akhilesh Darjee2428e8d2013-09-27 12:32:26 +0530491 me.frm.set_value("mode_of_payment", dialog.get_values().mode_of_payment);
492 me.frm.set_value("paid_amount", dialog.get_values().total_amount);
493 me.frm.cscript.mode_of_payment(me.frm.doc);
494 me.frm.save();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530495 dialog.hide();
496 me.refresh();
497 };
498 }
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530499 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530500 }
501 },
Pratik Vyasb0f279a2014-04-14 17:14:23 +0530502});