blob: db600251831b33534bb3b597145c13795f24db73 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
3
Anand Doshi50d7e8c2015-01-06 17:14:13 +05304frappe.provide("erpnext.pos");
5
6erpnext.pos.PointOfSale = Class.extend({
Rushabh Mehta4a008f52013-07-29 15:31:11 +05307 init: function(wrapper, frm) {
8 this.wrapper = wrapper;
9 this.frm = frm;
Nabin Hait3769d872015-12-18 13:12:02 +053010 this.wrapper.html(frappe.render_template("pos", {currency: this.frm.currency}));
Nabin Haited8a8452014-05-05 11:01:32 +053011
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053012 this.check_transaction_type();
Rushabh Mehta4a008f52013-07-29 15:31:11 +053013 this.make();
14
15 var me = this;
16 $(this.frm.wrapper).on("refresh-fields", function() {
17 me.refresh();
18 });
Rushabh Mehta986f6a72016-01-14 12:31:56 +053019
Nabin Hait3769d872015-12-18 13:12:02 +053020 this.wrapper.find('input.discount-percentage').on("change", function() {
Rushabh Mehta986f6a72016-01-14 12:31:56 +053021 frappe.model.set_value(me.frm.doctype, me.frm.docname,
Nabin Hait3769d872015-12-18 13:12:02 +053022 "additional_discount_percentage", flt(this.value));
23 });
Rushabh Mehta4a008f52013-07-29 15:31:11 +053024
Akhilesh Darjee57738a02014-01-03 18:15:07 +053025 this.wrapper.find('input.discount-amount').on("change", function() {
Nabin Hait15cd1222015-02-10 16:37:18 +053026 frappe.model.set_value(me.frm.doctype, me.frm.docname, "discount_amount", flt(this.value));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +053027 });
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053028 },
29 check_transaction_type: function() {
30 var me = this;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053031
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053032 // Check whether the transaction is "Sales" or "Purchase"
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053033 if (frappe.meta.has_field(cur_frm.doc.doctype, "customer")) {
Nabin Hait5690be12015-02-12 16:09:11 +053034 this.set_transaction_defaults("Customer");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053035 }
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053036 else if (frappe.meta.has_field(cur_frm.doc.doctype, "supplier")) {
Nabin Hait5690be12015-02-12 16:09:11 +053037 this.set_transaction_defaults("Supplier");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053038 }
39 },
Nabin Hait5690be12015-02-12 16:09:11 +053040 set_transaction_defaults: function(party) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053041 var me = this;
42 this.party = party;
Nabin Haited8a8452014-05-05 11:01:32 +053043 this.price_list = (party == "Customer" ?
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053044 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
Anand Doshicb39e082014-01-24 21:44:36 +053045 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053046 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053047 },
Rushabh Mehta4a008f52013-07-29 15:31:11 +053048 make: function() {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053049 this.make_party();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +053050 this.make_search();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053051 this.make_item_list();
Rushabh Mehta4a008f52013-07-29 15:31:11 +053052 },
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053053 make_party: function() {
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053054 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053055 this.party_field = frappe.ui.form.make_control({
Rushabh Mehta4a008f52013-07-29 15:31:11 +053056 df: {
57 "fieldtype": "Link",
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053058 "options": this.party,
59 "label": this.party,
Nabin Hait8b63b122016-01-18 17:56:45 +053060 "fieldname": this.party.toLowerCase(),
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053061 "placeholder": this.party
Rushabh Mehta4a008f52013-07-29 15:31:11 +053062 },
Rushabh Mehta069672e2013-10-28 18:21:07 +053063 parent: this.wrapper.find(".party-area"),
Rushabh Mehta986f6a72016-01-14 12:31:56 +053064 frm: this.frm,
65 doctype: this.frm.doctype,
66 docname: this.frm.docname,
Rushabh Mehta069672e2013-10-28 18:21:07 +053067 only_input: true,
Rushabh Mehta4a008f52013-07-29 15:31:11 +053068 });
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053069 this.party_field.make_input();
70 this.party_field.$input.on("change", function() {
71 if(!me.party_field.autocomplete_open)
Nabin Haited8a8452014-05-05 11:01:32 +053072 frappe.model.set_value(me.frm.doctype, me.frm.docname,
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053073 me.party.toLowerCase(), this.value);
Rushabh Mehta4a008f52013-07-29 15:31:11 +053074 });
75 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053076 make_search: function() {
77 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053078 this.search = frappe.ui.form.make_control({
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053079 df: {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053080 "fieldtype": "Data",
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053081 "label": "Item",
Rushabh Mehta16a62fa2013-08-23 13:16:22 +053082 "fieldname": "pos_item",
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053083 "placeholder": "Search Item"
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053084 },
Rushabh Mehta069672e2013-10-28 18:21:07 +053085 parent: this.wrapper.find(".search-area"),
86 only_input: true,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053087 });
88 this.search.make_input();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053089 this.search.$input.on("keypress", function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053090 if(!me.search.autocomplete_open)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053091 if(me.item_timeout)
92 clearTimeout(me.item_timeout);
93 me.item_timeout = setTimeout(function() { me.make_item_list(); }, 1000);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053094 });
95 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053096 make_item_list: function() {
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053097 var me = this;
Rushabh Mehta3d65d962014-11-28 14:56:10 +053098 if(!this.price_list) {
99 msgprint(__("Price List not found or disabled"));
100 return;
101 }
102
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530103 me.item_timeout = null;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530104 frappe.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530105 method: 'erpnext.accounts.doctype.sales_invoice.pos.get_items',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530106 args: {
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530107 sales_or_purchase: this.sales_or_purchase,
108 price_list: this.price_list,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530109 item: this.search.$input.val()
110 },
111 callback: function(r) {
112 var $wrap = me.wrapper.find(".item-list");
113 me.wrapper.find(".item-list").empty();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530114 if (r.message) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530115 if (r.message.length === 1) {
116 var item = r.message[0];
117 if (item.serial_no) {
118 me.add_to_cart(item.item_code, item.serial_no);
Rushabh Mehtab83fa3b2015-03-02 18:25:14 +0530119 me.search.$input.val("");
Anand Doshi9b955fe2015-01-05 16:19:12 +0530120 return;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530121
Anand Doshi9b955fe2015-01-05 16:19:12 +0530122 } else if (item.barcode) {
123 me.add_to_cart(item.item_code);
Rushabh Mehtab83fa3b2015-03-02 18:25:14 +0530124 me.search.$input.val("");
Anand Doshi9b955fe2015-01-05 16:19:12 +0530125 return;
126 }
127 }
128
129 $.each(r.message, function(index, obj) {
130 $(frappe.render_template("pos_item", {
131 item_code: obj.name,
132 item_price: format_currency(obj.price_list_rate, obj.currency),
133 item_name: obj.name===obj.item_name ? "" : obj.item_name,
Rushabh Mehta135fe342015-01-12 16:10:42 +0530134 item_image: obj.image ? "url('" + obj.image + "')" : null
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530135 })).tooltip().appendTo($wrap);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530136 });
137 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530138
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530139 // if form is local then allow this function
Nabin Haitcf301182013-10-01 18:14:16 +0530140 $(me.wrapper).find("div.pos-item").on("click", function() {
141 if(me.frm.doc.docstatus==0) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530142 me.add_to_cart($(this).attr("data-item-code"));
Nabin Haitcf301182013-10-01 18:14:16 +0530143 }
144 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530145 }
146 });
147 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530148 add_to_cart: function(item_code, serial_no) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530149 var me = this;
150 var caught = false;
151
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530152 if(!me.frm.doc[me.party.toLowerCase()] && ((me.frm.doctype == "Quotation" &&
153 me.frm.doc.quotation_to == "Customer")
154 || me.frm.doctype != "Quotation")) {
155 msgprint(__("Please select {0} first.", [me.party]));
156 return;
157 }
158
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530159 // get no_of_items
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530160 var no_of_items = me.wrapper.find(".pos-bill-item").length;
Nabin Haited8a8452014-05-05 11:01:32 +0530161
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530162 // check whether the item is already added
163 if (no_of_items != 0) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530164 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530165 if (d.item_code == item_code) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530166 caught = true;
Anand Doshicb39e082014-01-24 21:44:36 +0530167 if (serial_no)
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530168 frappe.model.set_value(d.doctype, d.name, "serial_no", d.serial_no + '\n' + serial_no);
Anand Doshicb39e082014-01-24 21:44:36 +0530169 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530170 frappe.model.set_value(d.doctype, d.name, "qty", d.qty + 1);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530171 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530172 });
173 }
Nabin Haited8a8452014-05-05 11:01:32 +0530174
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530175 // if item not found then add new item
Anand Doshicb39e082014-01-24 21:44:36 +0530176 if (!caught)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530177 this.add_new_item_to_grid(item_code, serial_no);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530178
179 this.refresh();
180 this.refresh_search_box();
181 },
182 add_new_item_to_grid: function(item_code, serial_no) {
183 var me = this;
184
Nabin Haitdd38a262014-12-26 13:15:21 +0530185 var child = frappe.model.add_child(me.frm.doc, this.frm.doctype + " Item", "items");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530186 child.item_code = item_code;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530187 child.qty = 1;
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530188
189 if (serial_no)
190 child.serial_no = serial_no;
191
192 this.frm.script_manager.trigger("item_code", child.doctype, child.name);
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530193 frappe.after_ajax(function() {
194 me.frm.script_manager.trigger("qty", child.doctype, child.name);
195 })
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530196 },
197 refresh_search_box: function() {
198 var me = this;
199
200 // Clear Item Box and remake item list
201 if (this.search.$input.val()) {
202 this.search.set_input("");
203 this.make_item_list();
204 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530205 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530206 update_qty: function(item_code, qty) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530207 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530208 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530209 if (d.item_code == item_code) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530210 if (qty == 0) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530211 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530212 me.refresh_grid();
213 } else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530214 frappe.model.set_value(d.doctype, d.name, "qty", qty);
Akhilesh Darjeed2714312013-08-28 19:35:22 +0530215 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530216 }
217 });
Anand Doshicb39e082014-01-24 21:44:36 +0530218 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530219 },
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530220 refresh: function() {
221 var me = this;
Anand Doshicb39e082014-01-24 21:44:36 +0530222
223 this.refresh_item_list();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530224 this.refresh_fields();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530225
226 // if form is local then only run all these functions
227 if (this.frm.doc.docstatus===0) {
228 this.call_when_local();
229 }
230
231 this.disable_text_box_and_button();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530232 this.set_primary_action();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530233
234 // If quotation to is not Customer then remove party
Rushabh Mehta72e17192014-08-08 15:30:49 +0530235 if (this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer") {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530236 this.party_field.$input.prop("disabled", true);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530237 }
238 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530239 refresh_fields: function() {
240 this.party_field.set_input(this.frm.doc[this.party.toLowerCase()]);
Nabin Hait3769d872015-12-18 13:12:02 +0530241 this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530242 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
243
244 this.show_items_in_item_cart();
245 this.show_taxes();
246 this.set_totals();
247 },
Anand Doshicb39e082014-01-24 21:44:36 +0530248 refresh_item_list: function() {
249 var me = this;
250 // refresh item list on change of price list
251 if (this.frm.doc[this.price_list_field] != this.price_list) {
252 this.price_list = this.frm.doc[this.price_list_field];
253 this.make_item_list();
254 }
255 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530256 show_items_in_item_cart: function() {
257 var me = this;
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530258 var $items = this.wrapper.find(".items").empty();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530259
Rushabh Mehta419ae332014-12-31 15:03:14 +0530260 $.each(this.frm.doc.items|| [], function(i, d) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530261 $(frappe.render_template("pos_bill_item", {
262 item_code: d.item_code,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530263 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530264 qty: d.qty,
Rushabh Mehta50918a82015-04-21 14:38:48 +0530265 actual_qty: d.actual_qty,
266 projected_qty: d.projected_qty,
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530267 rate: format_currency(d.rate, me.frm.doc.currency),
268 amount: format_currency(d.amount, me.frm.doc.currency)
269 })).appendTo($items);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530270 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530271
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530272 this.wrapper.find("input.pos-item-qty").on("focus", function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530273 $(this).select();
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530274 });
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530275 },
276 show_taxes: function() {
277 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530278 var taxes = this.frm.doc["taxes"] || [];
Anand Doshi9b955fe2015-01-05 16:19:12 +0530279 $(this.wrapper)
280 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
281 .find(".tax-table").empty();
Nabin Haited8a8452014-05-05 11:01:32 +0530282
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530283 $.each(taxes, function(i, d) {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530284 if (d.tax_amount) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530285 $(frappe.render_template("pos_tax_row", {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530286 description: d.description,
Nabin Haited8a8452014-05-05 11:01:32 +0530287 tax_amount: format_currency(flt(d.tax_amount)/flt(me.frm.doc.conversion_rate),
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530288 me.frm.doc.currency)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530289 })).appendTo(me.wrapper.find(".tax-table"));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530290 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530291 });
292 },
293 set_totals: function() {
294 var me = this;
Nabin Hait5690be12015-02-12 16:09:11 +0530295 this.wrapper.find(".net-total").text(format_currency(me.frm.doc["net_total"], me.frm.doc.currency));
296 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530297 },
298 call_when_local: function() {
299 var me = this;
300
301 // append quantity to the respective item after change from input box
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530302 $(this.wrapper).find("input.pos-item-qty").on("change", function() {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530303 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530304 me.update_qty(item_code, $(this).val());
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530305 });
306
Anand Doshicb39e082014-01-24 21:44:36 +0530307 // increase/decrease qty on plus/minus button
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530308 $(this.wrapper).find(".pos-qty-btn").on("click", function() {
309 var $item = $(this).parents(".pos-bill-item:first");
310 me.increase_decrease_qty($item, $(this).attr("data-action"));
Anand Doshicb39e082014-01-24 21:44:36 +0530311 });
312
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530313 this.focus();
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530314 },
315 focus: function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530316 if(this.frm.doc[this.party.toLowerCase()]) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530317 this.search.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530318 } else {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530319 if(!(this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer"))
320 this.party_field.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530321 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530322 },
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530323 increase_decrease_qty: function($item, operation) {
324 var item_code = $item.attr("data-item-code");
325 var item_qty = cint($item.find("input.pos-item-qty").val());
Anand Doshicb39e082014-01-24 21:44:36 +0530326
327 if (operation == "increase-qty")
328 this.update_qty(item_code, item_qty + 1);
Rushabh Mehta72e17192014-08-08 15:30:49 +0530329 else if (operation == "decrease-qty" && item_qty != 0)
Anand Doshicb39e082014-01-24 21:44:36 +0530330 this.update_qty(item_code, item_qty - 1);
331 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530332 disable_text_box_and_button: function() {
333 var me = this;
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530334 // if form is submitted & cancelled then disable all input box & buttons
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530335 $(this.wrapper)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530336 .find(".pos-qty-btn")
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530337 .toggle(this.frm.doc.docstatus===0);
Nabin Haited8a8452014-05-05 11:01:32 +0530338
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530339 $(this.wrapper).find('input, button').prop("disabled", !(this.frm.doc.docstatus===0));
Anand Doshi9b955fe2015-01-05 16:19:12 +0530340
341 this.wrapper.find(".pos-item-area").toggleClass("hide", me.frm.doc.docstatus!==0);
342
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530343 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530344 set_primary_action: function() {
345 var me = this;
Rushabh Mehta88655892015-05-28 10:47:55 +0530346 if (this.frm.page.current_view_name==="main") return;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530347
348 if (this.frm.doctype == "Sales Invoice" && this.frm.doc.docstatus===0) {
349 if (!this.frm.doc.is_pos) {
350 this.frm.set_value("is_pos", 1);
351 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530352 this.frm.page.set_primary_action(__("Pay"), function() {
353 me.make_payment();
354 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530355 } else if (this.frm.doc.docstatus===1) {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530356 this.frm.page.set_primary_action(__("New"), function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530357 erpnext.open_as_pos = true;
358 new_doc(me.frm.doctype);
359 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530360 }
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530361 },
362 refresh_delete_btn: function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530363 $(this.wrapper).find(".remove-items").toggle($(".item-cart .warning").length ? true : false);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530364 },
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530365 remove_selected_items: function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530366 var me = this;
367 var selected_items = [];
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530368 var no_of_items = $(this.wrapper).find("#cart tbody tr").length;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530369 for(var x=0; x<=no_of_items - 1; x++) {
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530370 var row = $(this.wrapper).find("#cart tbody tr:eq(" + x + ")");
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530371 if(row.attr("data-selected") == "true") {
372 selected_items.push(row.attr("id"));
373 }
374 }
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530375
Nabin Haitdd38a262014-12-26 13:15:21 +0530376 var child = this.frm.doc["items"] || [];
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530377
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530378 $.each(child, function(i, d) {
379 for (var i in selected_items) {
380 if (d.item_code == selected_items[i]) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530381 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530382 }
383 }
384 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530385
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530386 this.refresh_grid();
387 },
388 refresh_grid: function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530389 this.frm.dirty();
Nabin Haitdd38a262014-12-26 13:15:21 +0530390 this.frm.fields_dict["items"].grid.refresh();
Akhilesh Darjee2428e8d2013-09-27 12:32:26 +0530391 this.frm.script_manager.trigger("calculate_taxes_and_totals");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530392 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530393 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530394 with_modes_of_payment: function(callback) {
395 var me = this;
396 if(me.modes_of_payment) {
397 callback();
398 } else {
399 me.modes_of_payment = [];
400 $.ajax("/api/resource/Mode of Payment").success(function(data) {
401 $.each(data.data, function(i, d) { me.modes_of_payment.push(d.name); });
402 callback();
403 });
404 }
405 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530406 make_payment: function() {
407 var me = this;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530408 var no_of_items = this.frm.doc.items.length;
Nabin Haited8a8452014-05-05 11:01:32 +0530409
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530410 if (no_of_items == 0)
Pratik Vyasb52618c2014-04-14 16:25:30 +0530411 msgprint(__("Payment cannot be made for empty cart"));
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530412 else {
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530413
414 this.with_modes_of_payment(function() {
415 // prefer cash payment!
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530416 var default_mode = me.frm.doc.mode_of_payment ? me.frm.doc.mode_of_payment :
Neil Trini Lasrado75b00e32015-07-08 12:15:28 +0530417 me.modes_of_payment.indexOf(__("Cash"))!==-1 ? __("Cash") : undefined;
Nabin Hait8b63b122016-01-18 17:56:45 +0530418
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530419 // show payment wizard
420 var dialog = new frappe.ui.Dialog({
421 width: 400,
422 title: 'Payment',
423 fields: [
424 {fieldtype:'Currency',
Nabin Hait8b63b122016-01-18 17:56:45 +0530425 fieldname:'total_amount', label: __('Total Amount'),
426 "default": me.frm.doc.grand_total},
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530427 {fieldtype:'Select', fieldname:'mode_of_payment',
428 label: __('Mode of Payment'),
429 options: me.modes_of_payment.join('\n'), reqd: 1,
430 "default": default_mode},
431 {fieldtype:'Currency', fieldname:'paid_amount', label:__('Amount Paid'),
432 reqd:1, "default": me.frm.doc.grand_total, hidden: 1, change: function() {
433 var values = dialog.get_values();
Nabin Hait8b63b122016-01-18 17:56:45 +0530434
435 var actual_change = flt(values.paid_amount - values.total_amount,
436 precision("paid_amount"));
437
438 if (actual_change > 0) {
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530439 var rounded_change =
440 round_based_on_smallest_currency_fraction(actual_change,
441 me.frm.doc.currency, precision("paid_amount"));
Nabin Hait8b63b122016-01-18 17:56:45 +0530442 } else {
443 var rounded_change = 0;
444 }
445
446 dialog.set_value("change", rounded_change);
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530447 dialog.get_input("change").trigger("change");
448
449 }},
450 {fieldtype:'Currency', fieldname:'change', label: __('Change'),
451 "default": 0.0, hidden: 1, change: function() {
452 var values = dialog.get_values();
453 var write_off_amount = (flt(values.paid_amount) - flt(values.change)) - values.total_amount;
454 dialog.get_field("write_off_amount").toggle(write_off_amount);
455 dialog.set_value("write_off_amount", write_off_amount);
456 }
457 },
458 {fieldtype:'Currency', fieldname:'write_off_amount',
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530459 label: __('Write Off'), "default": 0.0, hidden: 1},
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530460 ]
461 });
462 me.dialog = dialog;
463 dialog.show();
464
465 // make read only
466 dialog.get_input("total_amount").prop("disabled", true);
467 dialog.get_input("write_off_amount").prop("disabled", true);
468
469 // toggle amount paid and change
470 dialog.get_input("mode_of_payment").on("change", function() {
471 var is_cash = dialog.get_value("mode_of_payment") === __("Cash");
472 dialog.get_field("paid_amount").toggle(is_cash);
473 dialog.get_field("change").toggle(is_cash);
474
475 if (is_cash && !dialog.get_value("change")) {
476 // set to nearest 5
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530477 dialog.set_value("paid_amount", dialog.get_value("total_amount"));
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530478 dialog.get_input("paid_amount").trigger("change");
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530479 }
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530480 }).trigger("change");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530481
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530482 me.set_pay_button(dialog);
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530483 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530484 }
485 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530486 set_pay_button: function(dialog) {
487 var me = this;
488 dialog.set_primary_action(__("Pay"), function() {
489 var values = dialog.get_values();
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530490 var is_cash = values.mode_of_payment === __("Cash");
491 if (!is_cash) {
492 values.write_off_amount = values.change = 0.0;
493 values.paid_amount = values.total_amount;
494 }
495 me.frm.set_value("mode_of_payment", values.mode_of_payment);
496
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530497 var paid_amount = flt((flt(values.paid_amount) - flt(values.change)), precision("paid_amount"));
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530498 me.frm.set_value("paid_amount", paid_amount);
Rushabh Mehta986f6a72016-01-14 12:31:56 +0530499
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530500 // specifying writeoff amount here itself, so as to avoid recursion issue
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530501 me.frm.set_value("write_off_amount", me.frm.doc.grand_total - paid_amount);
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530502 me.frm.set_value("outstanding_amount", 0);
503
504 me.frm.savesubmit(this);
505 dialog.hide();
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530506 })
507
508 }
Pratik Vyasb0f279a2014-04-14 17:14:23 +0530509});
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530510
511erpnext.pos.make_pos_btn = function(frm) {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530512 frm.page.add_menu_item(__("{0} View", [frm.page.current_view_name === "pos" ? "Form" : "Point-of-Sale"]), function() {
513 erpnext.pos.toggle(frm);
514 });
515
516 if(frm.pos_btn) return;
517
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530518 // Show POS button only if it is enabled from features setup
519 if (cint(sys_defaults.fs_pos_view)!==1 || frm.doctype==="Material Request") {
520 return;
521 }
522
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530523 if(!frm.pos_btn) {
524 frm.pos_btn = frm.page.add_action_icon("icon-th", function() {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530525 erpnext.pos.toggle(frm);
526 });
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530527 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530528
Rushabh Mehta88655892015-05-28 10:47:55 +0530529 if(erpnext.open_as_pos && frm.page.current_view_name !== "pos") {
530 erpnext.pos.toggle(frm, true);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530531 }
532}
533
534erpnext.pos.toggle = function(frm, show) {
535 // Check whether it is Selling or Buying cycle
536 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
537 frm.doc.selling_price_list : frm.doc.buying_price_list;
538
Rushabh Mehta88655892015-05-28 10:47:55 +0530539 if(show!==undefined) {
540 if((show===true && frm.page.current_view_name === "pos")
541 || (show===false && frm.page.current_view_name === "main")) {
542 return;
543 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530544 }
545
Rushabh Mehta88655892015-05-28 10:47:55 +0530546 if(frm.page.current_view_name!=="pos") {
547 // before switching, ask for pos name
548 if(!price_list) {
549 frappe.throw(__("Please select Price List"));
550 }
551
552 if(!frm.doc.company) {
553 frappe.throw(__("Please select Company"));
554 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530555 }
556
557 // make pos
558 if(!frm.pos) {
559 var wrapper = frm.page.add_view("pos", "<div>");
560 frm.pos = new erpnext.pos.PointOfSale(wrapper, frm);
561 }
562
563 // toggle view
Rushabh Mehta88655892015-05-28 10:47:55 +0530564 frm.page.set_view(frm.page.current_view_name==="pos" ? "main" : "pos");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530565
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530566 frm.toolbar.current_status = null;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530567 frm.refresh();
568
569 // refresh
Rushabh Mehta88655892015-05-28 10:47:55 +0530570 if(frm.page.current_view_name==="pos") {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530571 frm.pos.refresh();
572 }
573}