blob: a34813804781d2a26f3039961f749905b0060ad3 [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 });
Nabin Hait3769d872015-12-18 13:12:02 +053019
20 this.wrapper.find('input.discount-percentage').on("change", function() {
21 frappe.model.set_value(me.frm.doctype, me.frm.docname,
22 "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,
60 "fieldname": "pos_party",
61 "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"),
64 only_input: true,
Rushabh Mehta4a008f52013-07-29 15:31:11 +053065 });
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053066 this.party_field.make_input();
67 this.party_field.$input.on("change", function() {
68 if(!me.party_field.autocomplete_open)
Nabin Haited8a8452014-05-05 11:01:32 +053069 frappe.model.set_value(me.frm.doctype, me.frm.docname,
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053070 me.party.toLowerCase(), this.value);
Rushabh Mehta4a008f52013-07-29 15:31:11 +053071 });
72 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053073 make_search: function() {
74 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053075 this.search = frappe.ui.form.make_control({
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053076 df: {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053077 "fieldtype": "Data",
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053078 "label": "Item",
Rushabh Mehta16a62fa2013-08-23 13:16:22 +053079 "fieldname": "pos_item",
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053080 "placeholder": "Search Item"
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053081 },
Rushabh Mehta069672e2013-10-28 18:21:07 +053082 parent: this.wrapper.find(".search-area"),
83 only_input: true,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053084 });
85 this.search.make_input();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053086 this.search.$input.on("keypress", function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053087 if(!me.search.autocomplete_open)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053088 if(me.item_timeout)
89 clearTimeout(me.item_timeout);
90 me.item_timeout = setTimeout(function() { me.make_item_list(); }, 1000);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053091 });
92 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053093 make_item_list: function() {
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053094 var me = this;
Rushabh Mehta3d65d962014-11-28 14:56:10 +053095 if(!this.price_list) {
96 msgprint(__("Price List not found or disabled"));
97 return;
98 }
99
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530100 me.item_timeout = null;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530101 frappe.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530102 method: 'erpnext.accounts.doctype.sales_invoice.pos.get_items',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530103 args: {
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530104 sales_or_purchase: this.sales_or_purchase,
105 price_list: this.price_list,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530106 item: this.search.$input.val()
107 },
108 callback: function(r) {
109 var $wrap = me.wrapper.find(".item-list");
110 me.wrapper.find(".item-list").empty();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530111 if (r.message) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530112 if (r.message.length === 1) {
113 var item = r.message[0];
114 if (item.serial_no) {
115 me.add_to_cart(item.item_code, item.serial_no);
Rushabh Mehtab83fa3b2015-03-02 18:25:14 +0530116 me.search.$input.val("");
Anand Doshi9b955fe2015-01-05 16:19:12 +0530117 return;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530118
Anand Doshi9b955fe2015-01-05 16:19:12 +0530119 } else if (item.barcode) {
120 me.add_to_cart(item.item_code);
Rushabh Mehtab83fa3b2015-03-02 18:25:14 +0530121 me.search.$input.val("");
Anand Doshi9b955fe2015-01-05 16:19:12 +0530122 return;
123 }
124 }
125
126 $.each(r.message, function(index, obj) {
127 $(frappe.render_template("pos_item", {
128 item_code: obj.name,
129 item_price: format_currency(obj.price_list_rate, obj.currency),
130 item_name: obj.name===obj.item_name ? "" : obj.item_name,
Rushabh Mehta135fe342015-01-12 16:10:42 +0530131 item_image: obj.image ? "url('" + obj.image + "')" : null
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530132 })).tooltip().appendTo($wrap);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530133 });
134 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530135
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530136 // if form is local then allow this function
Nabin Haitcf301182013-10-01 18:14:16 +0530137 $(me.wrapper).find("div.pos-item").on("click", function() {
138 if(me.frm.doc.docstatus==0) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530139 me.add_to_cart($(this).attr("data-item-code"));
Nabin Haitcf301182013-10-01 18:14:16 +0530140 }
141 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530142 }
143 });
144 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530145 add_to_cart: function(item_code, serial_no) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530146 var me = this;
147 var caught = false;
148
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530149 if(!me.frm.doc[me.party.toLowerCase()] && ((me.frm.doctype == "Quotation" &&
150 me.frm.doc.quotation_to == "Customer")
151 || me.frm.doctype != "Quotation")) {
152 msgprint(__("Please select {0} first.", [me.party]));
153 return;
154 }
155
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530156 // get no_of_items
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530157 var no_of_items = me.wrapper.find(".pos-bill-item").length;
Nabin Haited8a8452014-05-05 11:01:32 +0530158
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530159 // check whether the item is already added
160 if (no_of_items != 0) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530161 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530162 if (d.item_code == item_code) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530163 caught = true;
Anand Doshicb39e082014-01-24 21:44:36 +0530164 if (serial_no)
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530165 frappe.model.set_value(d.doctype, d.name, "serial_no", d.serial_no + '\n' + serial_no);
Anand Doshicb39e082014-01-24 21:44:36 +0530166 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530167 frappe.model.set_value(d.doctype, d.name, "qty", d.qty + 1);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530168 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530169 });
170 }
Nabin Haited8a8452014-05-05 11:01:32 +0530171
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530172 // if item not found then add new item
Anand Doshicb39e082014-01-24 21:44:36 +0530173 if (!caught)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530174 this.add_new_item_to_grid(item_code, serial_no);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530175
176 this.refresh();
177 this.refresh_search_box();
178 },
179 add_new_item_to_grid: function(item_code, serial_no) {
180 var me = this;
181
Nabin Haitdd38a262014-12-26 13:15:21 +0530182 var child = frappe.model.add_child(me.frm.doc, this.frm.doctype + " Item", "items");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530183 child.item_code = item_code;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530184 child.qty = 1;
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530185
186 if (serial_no)
187 child.serial_no = serial_no;
188
189 this.frm.script_manager.trigger("item_code", child.doctype, child.name);
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530190 frappe.after_ajax(function() {
191 me.frm.script_manager.trigger("qty", child.doctype, child.name);
192 })
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530193 },
194 refresh_search_box: function() {
195 var me = this;
196
197 // Clear Item Box and remake item list
198 if (this.search.$input.val()) {
199 this.search.set_input("");
200 this.make_item_list();
201 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530202 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530203 update_qty: function(item_code, qty) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530204 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530205 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530206 if (d.item_code == item_code) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530207 if (qty == 0) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530208 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530209 me.refresh_grid();
210 } else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530211 frappe.model.set_value(d.doctype, d.name, "qty", qty);
Akhilesh Darjeed2714312013-08-28 19:35:22 +0530212 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530213 }
214 });
Anand Doshicb39e082014-01-24 21:44:36 +0530215 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530216 },
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530217 refresh: function() {
218 var me = this;
Anand Doshicb39e082014-01-24 21:44:36 +0530219
220 this.refresh_item_list();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530221 this.refresh_fields();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530222
223 // if form is local then only run all these functions
224 if (this.frm.doc.docstatus===0) {
225 this.call_when_local();
226 }
227
228 this.disable_text_box_and_button();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530229 this.set_primary_action();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530230
231 // If quotation to is not Customer then remove party
Rushabh Mehta72e17192014-08-08 15:30:49 +0530232 if (this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer") {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530233 this.party_field.$input.prop("disabled", true);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530234 }
235 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530236 refresh_fields: function() {
237 this.party_field.set_input(this.frm.doc[this.party.toLowerCase()]);
Nabin Hait3769d872015-12-18 13:12:02 +0530238 this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530239 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
240
241 this.show_items_in_item_cart();
242 this.show_taxes();
243 this.set_totals();
244 },
Anand Doshicb39e082014-01-24 21:44:36 +0530245 refresh_item_list: function() {
246 var me = this;
247 // refresh item list on change of price list
248 if (this.frm.doc[this.price_list_field] != this.price_list) {
249 this.price_list = this.frm.doc[this.price_list_field];
250 this.make_item_list();
251 }
252 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530253 show_items_in_item_cart: function() {
254 var me = this;
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530255 var $items = this.wrapper.find(".items").empty();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530256
Rushabh Mehta419ae332014-12-31 15:03:14 +0530257 $.each(this.frm.doc.items|| [], function(i, d) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530258 $(frappe.render_template("pos_bill_item", {
259 item_code: d.item_code,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530260 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530261 qty: d.qty,
Rushabh Mehta50918a82015-04-21 14:38:48 +0530262 actual_qty: d.actual_qty,
263 projected_qty: d.projected_qty,
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530264 rate: format_currency(d.rate, me.frm.doc.currency),
265 amount: format_currency(d.amount, me.frm.doc.currency)
266 })).appendTo($items);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530267 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530268
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530269 this.wrapper.find("input.pos-item-qty").on("focus", function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530270 $(this).select();
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530271 });
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530272 },
273 show_taxes: function() {
274 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530275 var taxes = this.frm.doc["taxes"] || [];
Anand Doshi9b955fe2015-01-05 16:19:12 +0530276 $(this.wrapper)
277 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
278 .find(".tax-table").empty();
Nabin Haited8a8452014-05-05 11:01:32 +0530279
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530280 $.each(taxes, function(i, d) {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530281 if (d.tax_amount) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530282 $(frappe.render_template("pos_tax_row", {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530283 description: d.description,
Nabin Haited8a8452014-05-05 11:01:32 +0530284 tax_amount: format_currency(flt(d.tax_amount)/flt(me.frm.doc.conversion_rate),
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530285 me.frm.doc.currency)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530286 })).appendTo(me.wrapper.find(".tax-table"));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530287 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530288 });
289 },
290 set_totals: function() {
291 var me = this;
Nabin Hait5690be12015-02-12 16:09:11 +0530292 this.wrapper.find(".net-total").text(format_currency(me.frm.doc["net_total"], me.frm.doc.currency));
293 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530294 },
295 call_when_local: function() {
296 var me = this;
297
298 // append quantity to the respective item after change from input box
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530299 $(this.wrapper).find("input.pos-item-qty").on("change", function() {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530300 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530301 me.update_qty(item_code, $(this).val());
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530302 });
303
Anand Doshicb39e082014-01-24 21:44:36 +0530304 // increase/decrease qty on plus/minus button
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530305 $(this.wrapper).find(".pos-qty-btn").on("click", function() {
306 var $item = $(this).parents(".pos-bill-item:first");
307 me.increase_decrease_qty($item, $(this).attr("data-action"));
Anand Doshicb39e082014-01-24 21:44:36 +0530308 });
309
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530310 this.focus();
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530311 },
312 focus: function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530313 if(this.frm.doc[this.party.toLowerCase()]) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530314 this.search.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530315 } else {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530316 if(!(this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer"))
317 this.party_field.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530318 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530319 },
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530320 increase_decrease_qty: function($item, operation) {
321 var item_code = $item.attr("data-item-code");
322 var item_qty = cint($item.find("input.pos-item-qty").val());
Anand Doshicb39e082014-01-24 21:44:36 +0530323
324 if (operation == "increase-qty")
325 this.update_qty(item_code, item_qty + 1);
Rushabh Mehta72e17192014-08-08 15:30:49 +0530326 else if (operation == "decrease-qty" && item_qty != 0)
Anand Doshicb39e082014-01-24 21:44:36 +0530327 this.update_qty(item_code, item_qty - 1);
328 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530329 disable_text_box_and_button: function() {
330 var me = this;
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530331 // if form is submitted & cancelled then disable all input box & buttons
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530332 $(this.wrapper)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530333 .find(".pos-qty-btn")
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530334 .toggle(this.frm.doc.docstatus===0);
Nabin Haited8a8452014-05-05 11:01:32 +0530335
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530336 $(this.wrapper).find('input, button').prop("disabled", !(this.frm.doc.docstatus===0));
Anand Doshi9b955fe2015-01-05 16:19:12 +0530337
338 this.wrapper.find(".pos-item-area").toggleClass("hide", me.frm.doc.docstatus!==0);
339
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530340 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530341 set_primary_action: function() {
342 var me = this;
Rushabh Mehta88655892015-05-28 10:47:55 +0530343 if (this.frm.page.current_view_name==="main") return;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530344
345 if (this.frm.doctype == "Sales Invoice" && this.frm.doc.docstatus===0) {
346 if (!this.frm.doc.is_pos) {
347 this.frm.set_value("is_pos", 1);
348 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530349 this.frm.page.set_primary_action(__("Pay"), function() {
350 me.make_payment();
351 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530352 } else if (this.frm.doc.docstatus===1) {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530353 this.frm.page.set_primary_action(__("New"), function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530354 erpnext.open_as_pos = true;
355 new_doc(me.frm.doctype);
356 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530357 }
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530358 },
359 refresh_delete_btn: function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530360 $(this.wrapper).find(".remove-items").toggle($(".item-cart .warning").length ? true : false);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530361 },
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530362 remove_selected_items: function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530363 var me = this;
364 var selected_items = [];
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530365 var no_of_items = $(this.wrapper).find("#cart tbody tr").length;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530366 for(var x=0; x<=no_of_items - 1; x++) {
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530367 var row = $(this.wrapper).find("#cart tbody tr:eq(" + x + ")");
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530368 if(row.attr("data-selected") == "true") {
369 selected_items.push(row.attr("id"));
370 }
371 }
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530372
Nabin Haitdd38a262014-12-26 13:15:21 +0530373 var child = this.frm.doc["items"] || [];
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530374
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530375 $.each(child, function(i, d) {
376 for (var i in selected_items) {
377 if (d.item_code == selected_items[i]) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530378 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530379 }
380 }
381 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530382
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530383 this.refresh_grid();
384 },
385 refresh_grid: function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530386 this.frm.dirty();
Nabin Haitdd38a262014-12-26 13:15:21 +0530387 this.frm.fields_dict["items"].grid.refresh();
Akhilesh Darjee2428e8d2013-09-27 12:32:26 +0530388 this.frm.script_manager.trigger("calculate_taxes_and_totals");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530389 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530390 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530391 with_modes_of_payment: function(callback) {
392 var me = this;
393 if(me.modes_of_payment) {
394 callback();
395 } else {
396 me.modes_of_payment = [];
397 $.ajax("/api/resource/Mode of Payment").success(function(data) {
398 $.each(data.data, function(i, d) { me.modes_of_payment.push(d.name); });
399 callback();
400 });
401 }
402 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530403 make_payment: function() {
404 var me = this;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530405 var no_of_items = this.frm.doc.items.length;
Nabin Haited8a8452014-05-05 11:01:32 +0530406
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530407 if (no_of_items == 0)
Pratik Vyasb52618c2014-04-14 16:25:30 +0530408 msgprint(__("Payment cannot be made for empty cart"));
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530409 else {
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530410
411 this.with_modes_of_payment(function() {
412 // prefer cash payment!
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530413 var default_mode = me.frm.doc.mode_of_payment ? me.frm.doc.mode_of_payment :
Neil Trini Lasrado75b00e32015-07-08 12:15:28 +0530414 me.modes_of_payment.indexOf(__("Cash"))!==-1 ? __("Cash") : undefined;
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530415
416 // show payment wizard
417 var dialog = new frappe.ui.Dialog({
418 width: 400,
419 title: 'Payment',
420 fields: [
421 {fieldtype:'Currency',
422 fieldname:'total_amount', label: __('Total Amount'), read_only:1,
423 "default": me.frm.doc.grand_total, read_only: 1},
424 {fieldtype:'Select', fieldname:'mode_of_payment',
425 label: __('Mode of Payment'),
426 options: me.modes_of_payment.join('\n'), reqd: 1,
427 "default": default_mode},
428 {fieldtype:'Currency', fieldname:'paid_amount', label:__('Amount Paid'),
429 reqd:1, "default": me.frm.doc.grand_total, hidden: 1, change: function() {
430 var values = dialog.get_values();
431 dialog.set_value("change", Math.round(values.paid_amount - values.total_amount));
432 dialog.get_input("change").trigger("change");
433
434 }},
435 {fieldtype:'Currency', fieldname:'change', label: __('Change'),
436 "default": 0.0, hidden: 1, change: function() {
437 var values = dialog.get_values();
438 var write_off_amount = (flt(values.paid_amount) - flt(values.change)) - values.total_amount;
439 dialog.get_field("write_off_amount").toggle(write_off_amount);
440 dialog.set_value("write_off_amount", write_off_amount);
441 }
442 },
443 {fieldtype:'Currency', fieldname:'write_off_amount',
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530444 label: __('Write Off'), "default": 0.0, hidden: 1},
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530445 ]
446 });
447 me.dialog = dialog;
448 dialog.show();
449
450 // make read only
451 dialog.get_input("total_amount").prop("disabled", true);
452 dialog.get_input("write_off_amount").prop("disabled", true);
453
454 // toggle amount paid and change
455 dialog.get_input("mode_of_payment").on("change", function() {
456 var is_cash = dialog.get_value("mode_of_payment") === __("Cash");
457 dialog.get_field("paid_amount").toggle(is_cash);
458 dialog.get_field("change").toggle(is_cash);
459
460 if (is_cash && !dialog.get_value("change")) {
461 // set to nearest 5
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530462 dialog.set_value("paid_amount", dialog.get_value("total_amount"));
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530463 dialog.get_input("paid_amount").trigger("change");
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530464 }
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530465 }).trigger("change");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530466
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530467 me.set_pay_button(dialog);
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530468 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530469 }
470 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530471 set_pay_button: function(dialog) {
472 var me = this;
473 dialog.set_primary_action(__("Pay"), function() {
474 var values = dialog.get_values();
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530475 var is_cash = values.mode_of_payment === __("Cash");
476 if (!is_cash) {
477 values.write_off_amount = values.change = 0.0;
478 values.paid_amount = values.total_amount;
479 }
480 me.frm.set_value("mode_of_payment", values.mode_of_payment);
481
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530482 var paid_amount = flt((flt(values.paid_amount) - flt(values.change)), precision("paid_amount"));
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530483 me.frm.set_value("paid_amount", paid_amount);
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530484
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530485 // specifying writeoff amount here itself, so as to avoid recursion issue
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530486 me.frm.set_value("write_off_amount", me.frm.doc.grand_total - paid_amount);
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530487 me.frm.set_value("outstanding_amount", 0);
488
489 me.frm.savesubmit(this);
490 dialog.hide();
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530491 })
492
493 }
Pratik Vyasb0f279a2014-04-14 17:14:23 +0530494});
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530495
496erpnext.pos.make_pos_btn = function(frm) {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530497 frm.page.add_menu_item(__("{0} View", [frm.page.current_view_name === "pos" ? "Form" : "Point-of-Sale"]), function() {
498 erpnext.pos.toggle(frm);
499 });
500
501 if(frm.pos_btn) return;
502
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530503 // Show POS button only if it is enabled from features setup
504 if (cint(sys_defaults.fs_pos_view)!==1 || frm.doctype==="Material Request") {
505 return;
506 }
507
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530508 if(!frm.pos_btn) {
509 frm.pos_btn = frm.page.add_action_icon("icon-th", function() {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530510 erpnext.pos.toggle(frm);
511 });
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530512 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530513
Rushabh Mehta88655892015-05-28 10:47:55 +0530514 if(erpnext.open_as_pos && frm.page.current_view_name !== "pos") {
515 erpnext.pos.toggle(frm, true);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530516 }
517}
518
519erpnext.pos.toggle = function(frm, show) {
520 // Check whether it is Selling or Buying cycle
521 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
522 frm.doc.selling_price_list : frm.doc.buying_price_list;
523
Rushabh Mehta88655892015-05-28 10:47:55 +0530524 if(show!==undefined) {
525 if((show===true && frm.page.current_view_name === "pos")
526 || (show===false && frm.page.current_view_name === "main")) {
527 return;
528 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530529 }
530
Rushabh Mehta88655892015-05-28 10:47:55 +0530531 if(frm.page.current_view_name!=="pos") {
532 // before switching, ask for pos name
533 if(!price_list) {
534 frappe.throw(__("Please select Price List"));
535 }
536
537 if(!frm.doc.company) {
538 frappe.throw(__("Please select Company"));
539 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530540 }
541
542 // make pos
543 if(!frm.pos) {
544 var wrapper = frm.page.add_view("pos", "<div>");
545 frm.pos = new erpnext.pos.PointOfSale(wrapper, frm);
546 }
547
548 // toggle view
Rushabh Mehta88655892015-05-28 10:47:55 +0530549 frm.page.set_view(frm.page.current_view_name==="pos" ? "main" : "pos");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530550
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530551 frm.toolbar.current_status = null;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530552 frm.refresh();
553
554 // refresh
Rushabh Mehta88655892015-05-28 10:47:55 +0530555 if(frm.page.current_view_name==="pos") {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530556 frm.pos.refresh();
557 }
558}