blob: 5357d8eba35027949fb3e1da421cfa12eb57d69f [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;
Rushabh Mehta3d65d962014-11-28 14:56:10 +0530138 if(!this.price_list) {
139 msgprint(__("Price List not found or disabled"));
140 return;
141 }
142
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530143 me.item_timeout = null;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530144 frappe.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530145 method: 'erpnext.accounts.doctype.sales_invoice.pos.get_items',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530146 args: {
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530147 sales_or_purchase: this.sales_or_purchase,
148 price_list: this.price_list,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530149 item_group: this.item_group.$input.val(),
150 item: this.search.$input.val()
151 },
152 callback: function(r) {
153 var $wrap = me.wrapper.find(".item-list");
154 me.wrapper.find(".item-list").empty();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530155 if (r.message) {
156 $.each(r.message, function(index, obj) {
157 if (obj.image)
158 image = '<img src="' + obj.image + '" class="img-responsive" \
159 style="border:1px solid #eee; max-height: 140px;">';
160 else
161 image = '<div class="missing-image"><i class="icon-camera"></i></div>';
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530162
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530163 $(repl('<div class="col-xs-3 pos-item" data-item_code="%(item_code)s">\
164 <div style="height: 140px; overflow: hidden;">%(item_image)s</div>\
165 <div class="small">%(item_code)s</div>\
166 <div class="small">%(item_name)s</div>\
167 <div class="small">%(item_price)s</div>\
Nabin Haited8a8452014-05-05 11:01:32 +0530168 </div>',
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530169 {
170 item_code: obj.name,
Nabin Haita7f757a2014-02-10 17:54:04 +0530171 item_price: format_currency(obj.price_list_rate, obj.currency),
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530172 item_name: obj.name===obj.item_name ? "" : obj.item_name,
173 item_image: image
174 })).appendTo($wrap);
175 });
176 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530177
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530178 // if form is local then allow this function
Nabin Haitcf301182013-10-01 18:14:16 +0530179 $(me.wrapper).find("div.pos-item").on("click", function() {
180 if(me.frm.doc.docstatus==0) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530181 console.log($(this).attr("data-item_code"));
182 me.add_to_cart($(this).attr("data-item_code"));
Nabin Haitcf301182013-10-01 18:14:16 +0530183 }
184 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530185 }
186 });
187 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530188 add_to_cart: function(item_code, serial_no) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530189 var me = this;
190 var caught = false;
191
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530192 if(!me.frm.doc[me.party.toLowerCase()] && ((me.frm.doctype == "Quotation" &&
193 me.frm.doc.quotation_to == "Customer")
194 || me.frm.doctype != "Quotation")) {
195 msgprint(__("Please select {0} first.", [me.party]));
196 return;
197 }
198
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530199 // get no_of_items
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530200 var no_of_items = me.wrapper.find(".pos-bill-item").length;
Nabin Haited8a8452014-05-05 11:01:32 +0530201
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530202 // check whether the item is already added
203 if (no_of_items != 0) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530204 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530205 if (d.item_code == item_code) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530206 caught = true;
Anand Doshicb39e082014-01-24 21:44:36 +0530207 if (serial_no)
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530208 frappe.model.set_value(d.doctype, d.name, "serial_no", d.serial_no + '\n' + serial_no);
Anand Doshicb39e082014-01-24 21:44:36 +0530209 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530210 frappe.model.set_value(d.doctype, d.name, "qty", d.qty + 1);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530211 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530212 });
213 }
Nabin Haited8a8452014-05-05 11:01:32 +0530214
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530215 // if item not found then add new item
Anand Doshicb39e082014-01-24 21:44:36 +0530216 if (!caught)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530217 this.add_new_item_to_grid(item_code, serial_no);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530218
219 this.refresh();
220 this.refresh_search_box();
221 },
222 add_new_item_to_grid: function(item_code, serial_no) {
223 var me = this;
224
Nabin Haitdd38a262014-12-26 13:15:21 +0530225 var child = frappe.model.add_child(me.frm.doc, this.frm.doctype + " Item", "items");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530226 child.item_code = item_code;
227
228 if (serial_no)
229 child.serial_no = serial_no;
230
231 this.frm.script_manager.trigger("item_code", child.doctype, child.name);
232 },
233 refresh_search_box: function() {
234 var me = this;
235
236 // Clear Item Box and remake item list
237 if (this.search.$input.val()) {
238 this.search.set_input("");
239 this.make_item_list();
240 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530241 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530242 update_qty: function(item_code, qty) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530243 console.log([item_code, qty]);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530244 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530245 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530246 if (d.item_code == item_code) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530247 if (qty == 0) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530248 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530249 me.refresh_grid();
250 } else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530251 frappe.model.set_value(d.doctype, d.name, "qty", qty);
Akhilesh Darjeed2714312013-08-28 19:35:22 +0530252 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530253 }
254 });
Anand Doshicb39e082014-01-24 21:44:36 +0530255 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530256 },
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530257 refresh: function() {
258 var me = this;
Anand Doshicb39e082014-01-24 21:44:36 +0530259
260 this.refresh_item_list();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530261 this.party_field.set_input(this.frm.doc[this.party.toLowerCase()]);
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530262 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530263 this.barcode.set_input("");
264
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530265 this.show_items_in_item_cart();
266 this.show_taxes();
267 this.set_totals();
268
269 // if form is local then only run all these functions
270 if (this.frm.doc.docstatus===0) {
271 this.call_when_local();
272 }
273
274 this.disable_text_box_and_button();
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530275 this.hide_payment_button();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530276
277 // If quotation to is not Customer then remove party
Rushabh Mehta72e17192014-08-08 15:30:49 +0530278 if (this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer") {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530279 this.party_field.$input.prop("disabled", true);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530280 }
281 },
Anand Doshicb39e082014-01-24 21:44:36 +0530282 refresh_item_list: function() {
283 var me = this;
284 // refresh item list on change of price list
285 if (this.frm.doc[this.price_list_field] != this.price_list) {
286 this.price_list = this.frm.doc[this.price_list_field];
287 this.make_item_list();
288 }
289 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530290 show_items_in_item_cart: function() {
291 var me = this;
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530292 var $items = this.wrapper.find(".items").empty();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530293
Rushabh Mehta419ae332014-12-31 15:03:14 +0530294 $.each(this.frm.doc.items|| [], function(i, d) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530295 $(frappe.render_template("pos_bill_item", {
296 item_code: d.item_code,
297 item_name: d.item_name===d.item_code ? "" : ("<br>" + d.item_name),
298 qty: d.qty,
299 actual_qty: d.actual_qty,
300 rate: format_currency(d.rate, me.frm.doc.currency),
301 amount: format_currency(d.amount, me.frm.doc.currency)
302 })).appendTo($items);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530303 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530304
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530305 this.wrapper.find("input.pos-item-qty").on("focus", function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530306 $(this).select();
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530307 });
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530308 },
309 show_taxes: function() {
310 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530311 var taxes = this.frm.doc["taxes"] || [];
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530312 $(this.wrapper).find(".tax-table")
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530313 .toggle((taxes && taxes.length) ? true : false)
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530314 .find("tbody").empty();
Nabin Haited8a8452014-05-05 11:01:32 +0530315
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530316 $.each(taxes, function(i, d) {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530317 if (d.tax_amount) {
318 $(repl('<tr>\
319 <td>%(description)s %(rate)s</td>\
320 <td style="text-align: right;">%(tax_amount)s</td>\
321 <tr>', {
322 description: d.description,
323 rate: ((d.charge_type == "Actual") ? '' : ("(" + d.rate + "%)")),
Nabin Haited8a8452014-05-05 11:01:32 +0530324 tax_amount: format_currency(flt(d.tax_amount)/flt(me.frm.doc.conversion_rate),
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530325 me.frm.doc.currency)
326 })).appendTo(".tax-table tbody");
327 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530328 });
329 },
330 set_totals: function() {
331 var me = this;
Nabin Haited8a8452014-05-05 11:01:32 +0530332 this.wrapper.find(".net-total").text(format_currency(this.frm.doc[this.net_total],
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530333 me.frm.doc.currency));
Nabin Haited8a8452014-05-05 11:01:32 +0530334 this.wrapper.find(".grand-total").text(format_currency(this.frm.doc[this.grand_total],
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530335 me.frm.doc.currency));
Nabin Haited8a8452014-05-05 11:01:32 +0530336
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530337 $(".paid-amount-area").toggle(!!this.frm.doc.paid_amount);
338 if(this.frm.doc.paid_amount) {
Nabin Haited8a8452014-05-05 11:01:32 +0530339 this.wrapper.find(".paid-amount").text(format_currency(this.frm.doc.paid_amount,
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530340 me.frm.doc.currency));
341 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530342 },
343 call_when_local: function() {
344 var me = this;
345
346 // append quantity to the respective item after change from input box
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530347 $(this.wrapper).find("input.pos-item-qty").on("change", function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530348 var item_code = $(this).closest("tr").attr("id");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530349 me.update_qty(item_code, $(this).val());
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530350 });
351
Anand Doshicb39e082014-01-24 21:44:36 +0530352 // increase/decrease qty on plus/minus button
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530353 $(this.wrapper).find(".pos-qty-btn").on("click", function() {
354 var $item = $(this).parents(".pos-bill-item:first");
355 me.increase_decrease_qty($item, $(this).attr("data-action"));
Anand Doshicb39e082014-01-24 21:44:36 +0530356 });
357
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530358 // on td click toggle the highlighting of row
359 $(this.wrapper).find("#cart tbody tr td").on("click", function() {
360 var row = $(this).closest("tr");
361 if (row.attr("data-selected") == "false") {
362 row.attr("class", "warning");
363 row.attr("data-selected", "true");
364 }
365 else {
366 row.prop("class", null);
367 row.attr("data-selected", "false");
368 }
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530369 me.refresh_delete_btn();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530370 });
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530371
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530372 me.refresh_delete_btn();
Rushabh Mehta419ae332014-12-31 15:03:14 +0530373<<<<<<< HEAD:erpnext/accounts/doctype/sales_invoice/pos.js
Nabin Hait40431cb2014-12-30 17:53:16 +0530374 if(me.frm.doc[this.party.toLowerCase()]) {
Rushabh Mehta419ae332014-12-31 15:03:14 +0530375=======
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530376 //me.focus();
377 },
378 focus: function() {
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530379 if(me.frm.doc[this.party]) {
Rushabh Mehta419ae332014-12-31 15:03:14 +0530380>>>>>>> v5-design:erpnext/public/js/pos/pos.js
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530381 this.barcode.$input.focus();
382 } else {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530383 if(!(this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer"))
384 this.party_field.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530385 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530386 },
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530387 increase_decrease_qty: function($item, operation) {
388 var item_code = $item.attr("data-item-code");
389 var item_qty = cint($item.find("input.pos-item-qty").val());
Anand Doshicb39e082014-01-24 21:44:36 +0530390
391 if (operation == "increase-qty")
392 this.update_qty(item_code, item_qty + 1);
Rushabh Mehta72e17192014-08-08 15:30:49 +0530393 else if (operation == "decrease-qty" && item_qty != 0)
Anand Doshicb39e082014-01-24 21:44:36 +0530394 this.update_qty(item_code, item_qty - 1);
395 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530396 disable_text_box_and_button: function() {
397 var me = this;
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530398 // if form is submitted & cancelled then disable all input box & buttons
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530399 $(this.wrapper)
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530400 .find(".remove-items, .make-payment, .pos-qty-btn")
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530401 .toggle(this.frm.doc.docstatus===0);
Nabin Haited8a8452014-05-05 11:01:32 +0530402
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530403 $(this.wrapper).find('input, button').prop("disabled", !(this.frm.doc.docstatus===0));
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530404 },
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530405 hide_payment_button: function() {
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530406 $(this.wrapper)
407 .find(".make-payment")
408 .toggle(this.frm.doctype == "Sales Invoice" && this.frm.doc.is_pos);
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530409 },
410 refresh_delete_btn: function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530411 $(this.wrapper).find(".remove-items").toggle($(".item-cart .warning").length ? true : false);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530412 },
413 add_item_thru_barcode: function() {
414 var me = this;
Akhilesh Darjeed2714312013-08-28 19:35:22 +0530415 me.barcode_timeout = null;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530416 frappe.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530417 method: 'erpnext.accounts.doctype.sales_invoice.pos.get_item_code',
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530418 args: {barcode_serial_no: this.barcode.$input.val()},
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530419 callback: function(r) {
420 if (r.message) {
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530421 if (r.message[1] == "serial_no")
422 me.add_to_cart(r.message[0][0].item_code, r.message[0][0].name);
423 else
424 me.add_to_cart(r.message[0][0].name);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530425 }
426 else
Pratik Vyasb52618c2014-04-14 16:25:30 +0530427 msgprint(__("Invalid Barcode"));
Akhilesh Darjee1f0b8602013-08-23 18:22:32 +0530428
429 me.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530430 }
431 });
432 },
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530433 remove_selected_items: function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530434 var me = this;
435 var selected_items = [];
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530436 var no_of_items = $(this.wrapper).find("#cart tbody tr").length;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530437 for(var x=0; x<=no_of_items - 1; x++) {
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530438 var row = $(this.wrapper).find("#cart tbody tr:eq(" + x + ")");
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530439 if(row.attr("data-selected") == "true") {
440 selected_items.push(row.attr("id"));
441 }
442 }
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530443
Nabin Haitdd38a262014-12-26 13:15:21 +0530444 var child = this.frm.doc["items"] || [];
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530445
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530446 $.each(child, function(i, d) {
447 for (var i in selected_items) {
448 if (d.item_code == selected_items[i]) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530449 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530450 }
451 }
452 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530453
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530454 this.refresh_grid();
455 },
456 refresh_grid: function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530457 this.frm.dirty();
Nabin Haitdd38a262014-12-26 13:15:21 +0530458 this.frm.fields_dict["items"].grid.refresh();
Akhilesh Darjee2428e8d2013-09-27 12:32:26 +0530459 this.frm.script_manager.trigger("calculate_taxes_and_totals");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530460 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530461 },
462 make_payment: function() {
463 var me = this;
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530464 var no_of_items = $(this.wrapper).find("#cart tbody tr").length;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530465 var mode_of_payment = [];
Nabin Haited8a8452014-05-05 11:01:32 +0530466
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530467 if (no_of_items == 0)
Pratik Vyasb52618c2014-04-14 16:25:30 +0530468 msgprint(__("Payment cannot be made for empty cart"));
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530469 else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530470 frappe.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530471 method: 'erpnext.accounts.doctype.sales_invoice.pos.get_mode_of_payment',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530472 callback: function(r) {
Nabin Haited8a8452014-05-05 11:01:32 +0530473 if(!r.message) {
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530474 msgprint(__("Please add to Modes of Payment from Setup."))
Nabin Haited8a8452014-05-05 11:01:32 +0530475 return;
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530476 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530477 for (x=0; x<=r.message.length - 1; x++) {
478 mode_of_payment.push(r.message[x].name);
479 }
480
481 // show payment wizard
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530482 var dialog = new frappe.ui.Dialog({
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530483 width: 400,
Nabin Haited8a8452014-05-05 11:01:32 +0530484 title: 'Payment',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530485 fields: [
486 {fieldtype:'Data', fieldname:'total_amount', label:'Total Amount', read_only:1},
Nabin Haited8a8452014-05-05 11:01:32 +0530487 {fieldtype:'Select', fieldname:'mode_of_payment', label:'Mode of Payment',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530488 options:mode_of_payment.join('\n'), reqd: 1},
489 {fieldtype:'Button', fieldname:'pay', label:'Pay'}
490 ]
491 });
492 dialog.set_values({
493 "total_amount": $(".grand-total").text()
494 });
495 dialog.show();
Anand Doshic8e39b02013-08-30 18:22:58 +0530496 dialog.get_input("total_amount").prop("disabled", true);
Nabin Haited8a8452014-05-05 11:01:32 +0530497
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530498 dialog.fields_dict.pay.input.onclick = function() {
Akhilesh Darjee2428e8d2013-09-27 12:32:26 +0530499 me.frm.set_value("mode_of_payment", dialog.get_values().mode_of_payment);
500 me.frm.set_value("paid_amount", dialog.get_values().total_amount);
501 me.frm.cscript.mode_of_payment(me.frm.doc);
502 me.frm.save();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530503 dialog.hide();
504 me.refresh();
505 };
506 }
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530507 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530508 }
509 },
Pratik Vyasb0f279a2014-04-14 17:14:23 +0530510});