blob: b8a6d32a0b96c0eeffce55dd987d0d0620c6781c [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()]);
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530241 this.party_field.frm = this.frm;
242 this.party_field.doctype = this.frm.doctype;
243 this.party_field.docname = this.frm.docname;
244
Nabin Hait3769d872015-12-18 13:12:02 +0530245 this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530246 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
247
248 this.show_items_in_item_cart();
249 this.show_taxes();
250 this.set_totals();
251 },
Anand Doshicb39e082014-01-24 21:44:36 +0530252 refresh_item_list: function() {
253 var me = this;
254 // refresh item list on change of price list
255 if (this.frm.doc[this.price_list_field] != this.price_list) {
256 this.price_list = this.frm.doc[this.price_list_field];
257 this.make_item_list();
258 }
259 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530260 show_items_in_item_cart: function() {
261 var me = this;
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530262 var $items = this.wrapper.find(".items").empty();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530263
Rushabh Mehta419ae332014-12-31 15:03:14 +0530264 $.each(this.frm.doc.items|| [], function(i, d) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530265 $(frappe.render_template("pos_bill_item", {
266 item_code: d.item_code,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530267 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530268 qty: d.qty,
Rushabh Mehta50918a82015-04-21 14:38:48 +0530269 actual_qty: d.actual_qty,
270 projected_qty: d.projected_qty,
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530271 rate: format_currency(d.rate, me.frm.doc.currency),
272 amount: format_currency(d.amount, me.frm.doc.currency)
273 })).appendTo($items);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530274 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530275
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530276 this.wrapper.find("input.pos-item-qty").on("focus", function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530277 $(this).select();
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530278 });
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530279 },
280 show_taxes: function() {
281 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530282 var taxes = this.frm.doc["taxes"] || [];
Anand Doshi9b955fe2015-01-05 16:19:12 +0530283 $(this.wrapper)
284 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
285 .find(".tax-table").empty();
Nabin Haited8a8452014-05-05 11:01:32 +0530286
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530287 $.each(taxes, function(i, d) {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530288 if (d.tax_amount) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530289 $(frappe.render_template("pos_tax_row", {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530290 description: d.description,
Nabin Haited8a8452014-05-05 11:01:32 +0530291 tax_amount: format_currency(flt(d.tax_amount)/flt(me.frm.doc.conversion_rate),
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530292 me.frm.doc.currency)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530293 })).appendTo(me.wrapper.find(".tax-table"));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530294 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530295 });
296 },
297 set_totals: function() {
298 var me = this;
Nabin Hait5690be12015-02-12 16:09:11 +0530299 this.wrapper.find(".net-total").text(format_currency(me.frm.doc["net_total"], me.frm.doc.currency));
300 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530301 },
302 call_when_local: function() {
303 var me = this;
304
305 // append quantity to the respective item after change from input box
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530306 $(this.wrapper).find("input.pos-item-qty").on("change", function() {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530307 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530308 me.update_qty(item_code, $(this).val());
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530309 });
310
Anand Doshicb39e082014-01-24 21:44:36 +0530311 // increase/decrease qty on plus/minus button
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530312 $(this.wrapper).find(".pos-qty-btn").on("click", function() {
313 var $item = $(this).parents(".pos-bill-item:first");
314 me.increase_decrease_qty($item, $(this).attr("data-action"));
Anand Doshicb39e082014-01-24 21:44:36 +0530315 });
316
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530317 this.focus();
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530318 },
319 focus: function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530320 if(this.frm.doc[this.party.toLowerCase()]) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530321 this.search.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530322 } else {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530323 if(!(this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer"))
324 this.party_field.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530325 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530326 },
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530327 increase_decrease_qty: function($item, operation) {
328 var item_code = $item.attr("data-item-code");
329 var item_qty = cint($item.find("input.pos-item-qty").val());
Anand Doshicb39e082014-01-24 21:44:36 +0530330
331 if (operation == "increase-qty")
332 this.update_qty(item_code, item_qty + 1);
Rushabh Mehta72e17192014-08-08 15:30:49 +0530333 else if (operation == "decrease-qty" && item_qty != 0)
Anand Doshicb39e082014-01-24 21:44:36 +0530334 this.update_qty(item_code, item_qty - 1);
335 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530336 disable_text_box_and_button: function() {
337 var me = this;
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530338 // if form is submitted & cancelled then disable all input box & buttons
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530339 $(this.wrapper)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530340 .find(".pos-qty-btn")
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530341 .toggle(this.frm.doc.docstatus===0);
Nabin Haited8a8452014-05-05 11:01:32 +0530342
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530343 $(this.wrapper).find('input, button').prop("disabled", !(this.frm.doc.docstatus===0));
Anand Doshi9b955fe2015-01-05 16:19:12 +0530344
345 this.wrapper.find(".pos-item-area").toggleClass("hide", me.frm.doc.docstatus!==0);
346
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530347 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530348 set_primary_action: function() {
349 var me = this;
Rushabh Mehta88655892015-05-28 10:47:55 +0530350 if (this.frm.page.current_view_name==="main") return;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530351
352 if (this.frm.doctype == "Sales Invoice" && this.frm.doc.docstatus===0) {
353 if (!this.frm.doc.is_pos) {
354 this.frm.set_value("is_pos", 1);
355 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530356 this.frm.page.set_primary_action(__("Pay"), function() {
357 me.make_payment();
358 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530359 } else if (this.frm.doc.docstatus===1) {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530360 this.frm.page.set_primary_action(__("New"), function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530361 erpnext.open_as_pos = true;
362 new_doc(me.frm.doctype);
363 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530364 }
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530365 },
366 refresh_delete_btn: function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530367 $(this.wrapper).find(".remove-items").toggle($(".item-cart .warning").length ? true : false);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530368 },
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530369 remove_selected_items: function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530370 var me = this;
371 var selected_items = [];
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530372 var no_of_items = $(this.wrapper).find("#cart tbody tr").length;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530373 for(var x=0; x<=no_of_items - 1; x++) {
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530374 var row = $(this.wrapper).find("#cart tbody tr:eq(" + x + ")");
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530375 if(row.attr("data-selected") == "true") {
376 selected_items.push(row.attr("id"));
377 }
378 }
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530379
Nabin Haitdd38a262014-12-26 13:15:21 +0530380 var child = this.frm.doc["items"] || [];
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530381
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530382 $.each(child, function(i, d) {
383 for (var i in selected_items) {
384 if (d.item_code == selected_items[i]) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530385 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530386 }
387 }
388 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530389
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530390 this.refresh_grid();
391 },
392 refresh_grid: function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530393 this.frm.dirty();
Nabin Haitdd38a262014-12-26 13:15:21 +0530394 this.frm.fields_dict["items"].grid.refresh();
Akhilesh Darjee2428e8d2013-09-27 12:32:26 +0530395 this.frm.script_manager.trigger("calculate_taxes_and_totals");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530396 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530397 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530398 with_modes_of_payment: function(callback) {
399 var me = this;
400 if(me.modes_of_payment) {
401 callback();
402 } else {
403 me.modes_of_payment = [];
404 $.ajax("/api/resource/Mode of Payment").success(function(data) {
405 $.each(data.data, function(i, d) { me.modes_of_payment.push(d.name); });
406 callback();
407 });
408 }
409 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530410 make_payment: function() {
411 var me = this;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530412 var no_of_items = this.frm.doc.items.length;
Nabin Haited8a8452014-05-05 11:01:32 +0530413
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530414 if (no_of_items == 0)
Pratik Vyasb52618c2014-04-14 16:25:30 +0530415 msgprint(__("Payment cannot be made for empty cart"));
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530416 else {
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530417
418 this.with_modes_of_payment(function() {
419 // prefer cash payment!
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530420 var default_mode = me.frm.doc.mode_of_payment ? me.frm.doc.mode_of_payment :
Neil Trini Lasrado75b00e32015-07-08 12:15:28 +0530421 me.modes_of_payment.indexOf(__("Cash"))!==-1 ? __("Cash") : undefined;
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530422
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530423 // show payment wizard
424 var dialog = new frappe.ui.Dialog({
425 width: 400,
426 title: 'Payment',
427 fields: [
428 {fieldtype:'Currency',
Nabin Hait8b63b122016-01-18 17:56:45 +0530429 fieldname:'total_amount', label: __('Total Amount'),
430 "default": me.frm.doc.grand_total},
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530431 {fieldtype:'Select', fieldname:'mode_of_payment',
432 label: __('Mode of Payment'),
433 options: me.modes_of_payment.join('\n'), reqd: 1,
434 "default": default_mode},
435 {fieldtype:'Currency', fieldname:'paid_amount', label:__('Amount Paid'),
436 reqd:1, "default": me.frm.doc.grand_total, hidden: 1, change: function() {
437 var values = dialog.get_values();
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530438
439 var actual_change = flt(values.paid_amount - values.total_amount,
Nabin Hait8b63b122016-01-18 17:56:45 +0530440 precision("paid_amount"));
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530441
Nabin Hait8b63b122016-01-18 17:56:45 +0530442 if (actual_change > 0) {
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530443 var rounded_change =
444 round_based_on_smallest_currency_fraction(actual_change,
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530445 me.frm.doc.currency, precision("paid_amount"));
Nabin Hait8b63b122016-01-18 17:56:45 +0530446 } else {
447 var rounded_change = 0;
448 }
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530449
Nabin Hait8b63b122016-01-18 17:56:45 +0530450 dialog.set_value("change", rounded_change);
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530451 dialog.get_input("change").trigger("change");
452
453 }},
454 {fieldtype:'Currency', fieldname:'change', label: __('Change'),
455 "default": 0.0, hidden: 1, change: function() {
456 var values = dialog.get_values();
457 var write_off_amount = (flt(values.paid_amount) - flt(values.change)) - values.total_amount;
458 dialog.get_field("write_off_amount").toggle(write_off_amount);
459 dialog.set_value("write_off_amount", write_off_amount);
460 }
461 },
462 {fieldtype:'Currency', fieldname:'write_off_amount',
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530463 label: __('Write Off'), "default": 0.0, hidden: 1},
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530464 ]
465 });
466 me.dialog = dialog;
467 dialog.show();
468
469 // make read only
470 dialog.get_input("total_amount").prop("disabled", true);
471 dialog.get_input("write_off_amount").prop("disabled", true);
472
473 // toggle amount paid and change
474 dialog.get_input("mode_of_payment").on("change", function() {
475 var is_cash = dialog.get_value("mode_of_payment") === __("Cash");
476 dialog.get_field("paid_amount").toggle(is_cash);
477 dialog.get_field("change").toggle(is_cash);
478
479 if (is_cash && !dialog.get_value("change")) {
480 // set to nearest 5
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530481 dialog.set_value("paid_amount", dialog.get_value("total_amount"));
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530482 dialog.get_input("paid_amount").trigger("change");
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530483 }
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530484 }).trigger("change");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530485
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530486 me.set_pay_button(dialog);
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530487 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530488 }
489 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530490 set_pay_button: function(dialog) {
491 var me = this;
492 dialog.set_primary_action(__("Pay"), function() {
493 var values = dialog.get_values();
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530494 var is_cash = values.mode_of_payment === __("Cash");
495 if (!is_cash) {
496 values.write_off_amount = values.change = 0.0;
497 values.paid_amount = values.total_amount;
498 }
499 me.frm.set_value("mode_of_payment", values.mode_of_payment);
500
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530501 var paid_amount = flt((flt(values.paid_amount) - flt(values.change)), precision("paid_amount"));
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530502 me.frm.set_value("paid_amount", paid_amount);
Rushabh Mehta986f6a72016-01-14 12:31:56 +0530503
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530504 // specifying writeoff amount here itself, so as to avoid recursion issue
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530505 me.frm.set_value("write_off_amount", me.frm.doc.grand_total - paid_amount);
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530506 me.frm.set_value("outstanding_amount", 0);
507
508 me.frm.savesubmit(this);
509 dialog.hide();
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530510 })
511
512 }
Pratik Vyasb0f279a2014-04-14 17:14:23 +0530513});
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530514
515erpnext.pos.make_pos_btn = function(frm) {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530516 frm.page.add_menu_item(__("{0} View", [frm.page.current_view_name === "pos" ? "Form" : "Point-of-Sale"]), function() {
517 erpnext.pos.toggle(frm);
518 });
519
520 if(frm.pos_btn) return;
521
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530522 // Show POS button only if it is enabled from features setup
523 if (cint(sys_defaults.fs_pos_view)!==1 || frm.doctype==="Material Request") {
524 return;
525 }
526
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530527 if(!frm.pos_btn) {
528 frm.pos_btn = frm.page.add_action_icon("icon-th", function() {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530529 erpnext.pos.toggle(frm);
530 });
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530531 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530532
Rushabh Mehta88655892015-05-28 10:47:55 +0530533 if(erpnext.open_as_pos && frm.page.current_view_name !== "pos") {
534 erpnext.pos.toggle(frm, true);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530535 }
536}
537
538erpnext.pos.toggle = function(frm, show) {
539 // Check whether it is Selling or Buying cycle
540 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
541 frm.doc.selling_price_list : frm.doc.buying_price_list;
542
Rushabh Mehta88655892015-05-28 10:47:55 +0530543 if(show!==undefined) {
544 if((show===true && frm.page.current_view_name === "pos")
545 || (show===false && frm.page.current_view_name === "main")) {
546 return;
547 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530548 }
549
Rushabh Mehta88655892015-05-28 10:47:55 +0530550 if(frm.page.current_view_name!=="pos") {
551 // before switching, ask for pos name
552 if(!price_list) {
553 frappe.throw(__("Please select Price List"));
554 }
555
556 if(!frm.doc.company) {
557 frappe.throw(__("Please select Company"));
558 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530559 }
560
561 // make pos
562 if(!frm.pos) {
563 var wrapper = frm.page.add_view("pos", "<div>");
564 frm.pos = new erpnext.pos.PointOfSale(wrapper, frm);
565 }
566
567 // toggle view
Rushabh Mehta88655892015-05-28 10:47:55 +0530568 frm.page.set_view(frm.page.current_view_name==="pos" ? "main" : "pos");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530569
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530570 frm.toolbar.current_status = null;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530571 frm.refresh();
572
573 // refresh
Rushabh Mehta88655892015-05-28 10:47:55 +0530574 if(frm.page.current_view_name==="pos") {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530575 frm.pos.refresh();
576 }
577}