blob: e6394f6a1cfeaec298e90c20a1a3496a00aaf37f [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
3
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;
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +053010 this.wrapper.html(frappe.render_template("pos", {}));
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 });
19
Akhilesh Darjee57738a02014-01-03 18:15:07 +053020 this.wrapper.find('input.discount-amount').on("change", function() {
Nabin Hait15cd1222015-02-10 16:37:18 +053021 frappe.model.set_value(me.frm.doctype, me.frm.docname, "discount_amount", flt(this.value));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +053022 });
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053023 },
24 check_transaction_type: function() {
25 var me = this;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053026
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053027 // Check whether the transaction is "Sales" or "Purchase"
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053028 if (frappe.meta.has_field(cur_frm.doc.doctype, "customer")) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053029 this.set_transaction_defaults("Customer", "export");
30 }
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053031 else if (frappe.meta.has_field(cur_frm.doc.doctype, "supplier")) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053032 this.set_transaction_defaults("Supplier", "import");
33 }
34 },
35 set_transaction_defaults: function(party, export_or_import) {
36 var me = this;
37 this.party = party;
Nabin Haited8a8452014-05-05 11:01:32 +053038 this.price_list = (party == "Customer" ?
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053039 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
Anand Doshicb39e082014-01-24 21:44:36 +053040 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053041 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
42 this.net_total = "net_total_" + export_or_import;
43 this.grand_total = "grand_total_" + export_or_import;
Nabin Hait1eb56012014-02-10 19:20:15 +053044 // this.amount = export_or_import + "_amount";
45 // this.rate = export_or_import + "_rate";
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053046 },
Rushabh Mehta4a008f52013-07-29 15:31:11 +053047 make: function() {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053048 this.make_party();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +053049 this.make_search();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053050 this.make_item_list();
Rushabh Mehta4a008f52013-07-29 15:31:11 +053051 },
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053052 make_party: function() {
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053053 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053054 this.party_field = frappe.ui.form.make_control({
Rushabh Mehta4a008f52013-07-29 15:31:11 +053055 df: {
56 "fieldtype": "Link",
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053057 "options": this.party,
58 "label": this.party,
59 "fieldname": "pos_party",
60 "placeholder": this.party
Rushabh Mehta4a008f52013-07-29 15:31:11 +053061 },
Rushabh Mehta069672e2013-10-28 18:21:07 +053062 parent: this.wrapper.find(".party-area"),
63 only_input: true,
Rushabh Mehta4a008f52013-07-29 15:31:11 +053064 });
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053065 this.party_field.make_input();
66 this.party_field.$input.on("change", function() {
67 if(!me.party_field.autocomplete_open)
Nabin Haited8a8452014-05-05 11:01:32 +053068 frappe.model.set_value(me.frm.doctype, me.frm.docname,
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053069 me.party.toLowerCase(), this.value);
Rushabh Mehta4a008f52013-07-29 15:31:11 +053070 });
71 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053072 make_search: function() {
73 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053074 this.search = frappe.ui.form.make_control({
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053075 df: {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053076 "fieldtype": "Data",
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053077 "label": "Item",
Rushabh Mehta16a62fa2013-08-23 13:16:22 +053078 "fieldname": "pos_item",
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053079 "placeholder": "Search Item"
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053080 },
Rushabh Mehta069672e2013-10-28 18:21:07 +053081 parent: this.wrapper.find(".search-area"),
82 only_input: true,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053083 });
84 this.search.make_input();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053085 this.search.$input.on("keypress", function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053086 if(!me.search.autocomplete_open)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053087 if(me.item_timeout)
88 clearTimeout(me.item_timeout);
89 me.item_timeout = setTimeout(function() { me.make_item_list(); }, 1000);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053090 });
91 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053092 make_item_list: function() {
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053093 var me = this;
Rushabh Mehta3d65d962014-11-28 14:56:10 +053094 if(!this.price_list) {
95 msgprint(__("Price List not found or disabled"));
96 return;
97 }
98
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053099 me.item_timeout = null;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530100 frappe.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530101 method: 'erpnext.accounts.doctype.sales_invoice.pos.get_items',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530102 args: {
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530103 sales_or_purchase: this.sales_or_purchase,
104 price_list: this.price_list,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530105 item: this.search.$input.val()
106 },
107 callback: function(r) {
108 var $wrap = me.wrapper.find(".item-list");
109 me.wrapper.find(".item-list").empty();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530110 if (r.message) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530111 if (r.message.length === 1) {
112 var item = r.message[0];
113 if (item.serial_no) {
114 me.add_to_cart(item.item_code, item.serial_no);
115 this.search.$input.val("");
116 return;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530117
Anand Doshi9b955fe2015-01-05 16:19:12 +0530118 } else if (item.barcode) {
119 me.add_to_cart(item.item_code);
120 this.search.$input.val("");
121 return;
122 }
123 }
124
125 $.each(r.message, function(index, obj) {
126 $(frappe.render_template("pos_item", {
127 item_code: obj.name,
128 item_price: format_currency(obj.price_list_rate, obj.currency),
129 item_name: obj.name===obj.item_name ? "" : obj.item_name,
Rushabh Mehta135fe342015-01-12 16:10:42 +0530130 item_image: obj.image ? "url('" + obj.image + "')" : null
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530131 })).tooltip().appendTo($wrap);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530132 });
133 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530134
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530135 // if form is local then allow this function
Nabin Haitcf301182013-10-01 18:14:16 +0530136 $(me.wrapper).find("div.pos-item").on("click", function() {
137 if(me.frm.doc.docstatus==0) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530138 me.add_to_cart($(this).attr("data-item-code"));
Nabin Haitcf301182013-10-01 18:14:16 +0530139 }
140 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530141 }
142 });
143 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530144 add_to_cart: function(item_code, serial_no) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530145 var me = this;
146 var caught = false;
147
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530148 if(!me.frm.doc[me.party.toLowerCase()] && ((me.frm.doctype == "Quotation" &&
149 me.frm.doc.quotation_to == "Customer")
150 || me.frm.doctype != "Quotation")) {
151 msgprint(__("Please select {0} first.", [me.party]));
152 return;
153 }
154
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530155 // get no_of_items
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530156 var no_of_items = me.wrapper.find(".pos-bill-item").length;
Nabin Haited8a8452014-05-05 11:01:32 +0530157
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530158 // check whether the item is already added
159 if (no_of_items != 0) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530160 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530161 if (d.item_code == item_code) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530162 caught = true;
Anand Doshicb39e082014-01-24 21:44:36 +0530163 if (serial_no)
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530164 frappe.model.set_value(d.doctype, d.name, "serial_no", d.serial_no + '\n' + serial_no);
Anand Doshicb39e082014-01-24 21:44:36 +0530165 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530166 frappe.model.set_value(d.doctype, d.name, "qty", d.qty + 1);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530167 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530168 });
169 }
Nabin Haited8a8452014-05-05 11:01:32 +0530170
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530171 // if item not found then add new item
Anand Doshicb39e082014-01-24 21:44:36 +0530172 if (!caught)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530173 this.add_new_item_to_grid(item_code, serial_no);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530174
175 this.refresh();
176 this.refresh_search_box();
177 },
178 add_new_item_to_grid: function(item_code, serial_no) {
179 var me = this;
180
Nabin Haitdd38a262014-12-26 13:15:21 +0530181 var child = frappe.model.add_child(me.frm.doc, this.frm.doctype + " Item", "items");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530182 child.item_code = item_code;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530183 child.qty = 1;
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530184
185 if (serial_no)
186 child.serial_no = serial_no;
187
188 this.frm.script_manager.trigger("item_code", child.doctype, child.name);
189 },
190 refresh_search_box: function() {
191 var me = this;
192
193 // Clear Item Box and remake item list
194 if (this.search.$input.val()) {
195 this.search.set_input("");
196 this.make_item_list();
197 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530198 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530199 update_qty: function(item_code, qty) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530200 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530201 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530202 if (d.item_code == item_code) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530203 if (qty == 0) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530204 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530205 me.refresh_grid();
206 } else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530207 frappe.model.set_value(d.doctype, d.name, "qty", qty);
Akhilesh Darjeed2714312013-08-28 19:35:22 +0530208 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530209 }
210 });
Anand Doshicb39e082014-01-24 21:44:36 +0530211 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530212 },
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530213 refresh: function() {
214 var me = this;
Anand Doshicb39e082014-01-24 21:44:36 +0530215
216 this.refresh_item_list();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530217 this.refresh_fields();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530218
219 // if form is local then only run all these functions
220 if (this.frm.doc.docstatus===0) {
221 this.call_when_local();
222 }
223
224 this.disable_text_box_and_button();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530225 this.set_primary_action();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530226
227 // If quotation to is not Customer then remove party
Rushabh Mehta72e17192014-08-08 15:30:49 +0530228 if (this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer") {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530229 this.party_field.$input.prop("disabled", true);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530230 }
231 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530232 refresh_fields: function() {
233 this.party_field.set_input(this.frm.doc[this.party.toLowerCase()]);
234 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
235
236 this.show_items_in_item_cart();
237 this.show_taxes();
238 this.set_totals();
239 },
Anand Doshicb39e082014-01-24 21:44:36 +0530240 refresh_item_list: function() {
241 var me = this;
242 // refresh item list on change of price list
243 if (this.frm.doc[this.price_list_field] != this.price_list) {
244 this.price_list = this.frm.doc[this.price_list_field];
245 this.make_item_list();
246 }
247 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530248 show_items_in_item_cart: function() {
249 var me = this;
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530250 var $items = this.wrapper.find(".items").empty();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530251
Rushabh Mehta419ae332014-12-31 15:03:14 +0530252 $.each(this.frm.doc.items|| [], function(i, d) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530253 $(frappe.render_template("pos_bill_item", {
254 item_code: d.item_code,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530255 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530256 qty: d.qty,
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530257 rate: format_currency(d.rate, me.frm.doc.currency),
258 amount: format_currency(d.amount, me.frm.doc.currency)
259 })).appendTo($items);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530260 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530261
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530262 this.wrapper.find("input.pos-item-qty").on("focus", function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530263 $(this).select();
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530264 });
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530265 },
266 show_taxes: function() {
267 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530268 var taxes = this.frm.doc["taxes"] || [];
Anand Doshi9b955fe2015-01-05 16:19:12 +0530269 $(this.wrapper)
270 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
271 .find(".tax-table").empty();
Nabin Haited8a8452014-05-05 11:01:32 +0530272
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530273 $.each(taxes, function(i, d) {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530274 if (d.tax_amount) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530275 $(frappe.render_template("pos_tax_row", {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530276 description: d.description,
Nabin Haited8a8452014-05-05 11:01:32 +0530277 tax_amount: format_currency(flt(d.tax_amount)/flt(me.frm.doc.conversion_rate),
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530278 me.frm.doc.currency)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530279 })).appendTo(me.wrapper.find(".tax-table"));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530280 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530281 });
282 },
283 set_totals: function() {
284 var me = this;
Nabin Haited8a8452014-05-05 11:01:32 +0530285 this.wrapper.find(".net-total").text(format_currency(this.frm.doc[this.net_total],
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530286 me.frm.doc.currency));
Nabin Haited8a8452014-05-05 11:01:32 +0530287 this.wrapper.find(".grand-total").text(format_currency(this.frm.doc[this.grand_total],
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530288 me.frm.doc.currency));
289 },
290 call_when_local: function() {
291 var me = this;
292
293 // append quantity to the respective item after change from input box
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530294 $(this.wrapper).find("input.pos-item-qty").on("change", function() {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530295 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530296 me.update_qty(item_code, $(this).val());
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530297 });
298
Anand Doshicb39e082014-01-24 21:44:36 +0530299 // increase/decrease qty on plus/minus button
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530300 $(this.wrapper).find(".pos-qty-btn").on("click", function() {
301 var $item = $(this).parents(".pos-bill-item:first");
302 me.increase_decrease_qty($item, $(this).attr("data-action"));
Anand Doshicb39e082014-01-24 21:44:36 +0530303 });
304
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530305 this.focus();
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530306 },
307 focus: function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530308 if(this.frm.doc[this.party.toLowerCase()]) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530309 this.search.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530310 } else {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530311 if(!(this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer"))
312 this.party_field.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530313 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530314 },
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530315 increase_decrease_qty: function($item, operation) {
316 var item_code = $item.attr("data-item-code");
317 var item_qty = cint($item.find("input.pos-item-qty").val());
Anand Doshicb39e082014-01-24 21:44:36 +0530318
319 if (operation == "increase-qty")
320 this.update_qty(item_code, item_qty + 1);
Rushabh Mehta72e17192014-08-08 15:30:49 +0530321 else if (operation == "decrease-qty" && item_qty != 0)
Anand Doshicb39e082014-01-24 21:44:36 +0530322 this.update_qty(item_code, item_qty - 1);
323 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530324 disable_text_box_and_button: function() {
325 var me = this;
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530326 // if form is submitted & cancelled then disable all input box & buttons
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530327 $(this.wrapper)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530328 .find(".pos-qty-btn")
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530329 .toggle(this.frm.doc.docstatus===0);
Nabin Haited8a8452014-05-05 11:01:32 +0530330
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530331 $(this.wrapper).find('input, button').prop("disabled", !(this.frm.doc.docstatus===0));
Anand Doshi9b955fe2015-01-05 16:19:12 +0530332
333 this.wrapper.find(".pos-item-area").toggleClass("hide", me.frm.doc.docstatus!==0);
334
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530335 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530336 set_primary_action: function() {
337 var me = this;
338 if (!this.frm.pos_active) return;
339
340 if (this.frm.doctype == "Sales Invoice" && this.frm.doc.docstatus===0) {
341 if (!this.frm.doc.is_pos) {
342 this.frm.set_value("is_pos", 1);
343 }
344 this.frm.page.clear_actions();
345 this.frm.page.set_primary_action(__("Pay"), function() {
346 me.make_payment();
347 });
348 this.frm.toolbar.current_status = null;
349 } else if (this.frm.doc.docstatus===1) {
350 this.frm.page.clear_actions();
351 this.frm.page.set_primary_action(__("New"), function() {
352 me.frm.pos_active = false;
353 erpnext.open_as_pos = true;
354 new_doc(me.frm.doctype);
355 });
356 this.frm.toolbar.current_status = null;
357 }
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 },
391 make_payment: function() {
392 var me = this;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530393 var no_of_items = this.frm.doc.items.length;
Nabin Haited8a8452014-05-05 11:01:32 +0530394
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530395 if (no_of_items == 0)
Pratik Vyasb52618c2014-04-14 16:25:30 +0530396 msgprint(__("Payment cannot be made for empty cart"));
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530397 else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530398 frappe.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530399 method: 'erpnext.accounts.doctype.sales_invoice.pos.get_mode_of_payment',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530400 callback: function(r) {
Nabin Haited8a8452014-05-05 11:01:32 +0530401 if(!r.message) {
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530402 msgprint(__("Please add to Modes of Payment from Setup."))
Nabin Haited8a8452014-05-05 11:01:32 +0530403 return;
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530404 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530405
406 var modes_of_payment = r.message;
407
408 // prefer cash payment!
409 var default_mode = modes_of_payment.indexOf(__("Cash"))!==-1 ? __("Cash") : undefined;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530410
411 // show payment wizard
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530412 var dialog = new frappe.ui.Dialog({
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530413 width: 400,
Nabin Haited8a8452014-05-05 11:01:32 +0530414 title: 'Payment',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530415 fields: [
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530416 {fieldtype:'Currency', fieldname:'total_amount', label: __('Total Amount'), read_only:1,
417 options:"currency", default:me.frm.doc.grand_total_export, read_only: 1},
418 {fieldtype:'Select', fieldname:'mode_of_payment', label: __('Mode of Payment'),
419 options:modes_of_payment.join('\n'), reqd: 1, default: default_mode},
420 {fieldtype:'Currency', fieldname:'paid_amount', label:__('Amount Paid'), reqd:1,
421 options: "currency",
422 default:me.frm.doc.grand_total_export, hidden: 1},
423 {fieldtype:'Currency', fieldname:'change', label: __('Change'), options: "currency",
424 default: 0.0, hidden: 1},
425 {fieldtype:'Currency', fieldname:'write_off_amount', label: __('Write Off'), options: "currency",
426 default: 0.0, hidden: 1},
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530427 {fieldtype:'Button', fieldname:'pay', label:'Pay'}
428 ]
429 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530430 dialog.show();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530431
432 // make read only
Anand Doshic8e39b02013-08-30 18:22:58 +0530433 dialog.get_input("total_amount").prop("disabled", true);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530434 dialog.get_input("write_off_amount").prop("disabled", true);
435
436 dialog.get_input("paid_amount").on("change", function() {
437 var values = dialog.get_values();
438 dialog.set_value("change", Math.round(values.paid_amount - values.total_amount));
439 dialog.get_input("change").trigger("change");
440 });
441
442 dialog.get_input("change").on("change", function() {
443 var values = dialog.get_values();
444 var write_off_amount = (flt(values.paid_amount) - flt(values.change)) - values.total_amount;
445 dialog.set_value("write_off_amount", write_off_amount);
446 dialog.fields_dict.write_off_amount.$wrapper.toggleClass("hide", !!!write_off_amount);
447 });
448
449 // toggle amount paid and change
450 dialog.get_input("mode_of_payment").on("change", function() {
451 var is_cash = dialog.get_value("mode_of_payment") === __("Cash");
452 dialog.fields_dict.paid_amount.$wrapper.toggleClass("hide", !is_cash);
453 dialog.fields_dict.change.$wrapper.toggleClass("hide", !is_cash);
454
455 if (is_cash && !dialog.get_value("change")) {
456 // set to nearest 5
457 var paid_amount = 5 * Math.ceil(dialog.get_value("total_amount") / 5);
458 dialog.set_value("paid_amount", paid_amount);
459 dialog.get_input("paid_amount").trigger("change");
460 }
461 }).trigger("change");
Nabin Haited8a8452014-05-05 11:01:32 +0530462
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530463 dialog.fields_dict.pay.input.onclick = function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530464 var values = dialog.get_values();
465 var is_cash = values.mode_of_payment === __("Cash");
466 if (!is_cash) {
467 values.write_off_amount = values.change = 0.0;
468 values.paid_amount = values.total_amount;
469 }
470 me.frm.set_value("mode_of_payment", values.mode_of_payment);
471
472 var paid_amount = flt((flt(values.paid_amount) - flt(values.change)) / me.frm.doc.conversion_rate, precision("paid_amount"));
473 me.frm.set_value("paid_amount", paid_amount);
474
475 // specifying writeoff amount here itself, so as to avoid recursion issue
476 me.frm.set_value("write_off_amount", me.frm.doc.grand_total - paid_amount);
477 me.frm.set_value("outstanding_amount", 0);
478
479 me.frm.savesubmit(this);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530480 dialog.hide();
481 me.refresh();
482 };
483 }
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530484 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530485 }
486 },
Pratik Vyasb0f279a2014-04-14 17:14:23 +0530487});
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530488
489erpnext.pos.make_pos_btn = function(frm) {
490 // Show POS button only if it is enabled from features setup
491 if (cint(sys_defaults.fs_pos_view)!==1 || frm.doctype==="Material Request") {
492 return;
493 }
494
495 if(frm.doc.docstatus <= 1) {
496 if(!frm.pos_active) {
497 var btn_label = __("POS View"),
498 icon = "icon-th";
499 } else {
500 var btn_label = __("Form View"),
501 icon = "icon-file-text";
502 }
503
504 if(erpnext.open_as_pos) {
505 erpnext.pos.toggle(frm, true);
506 erpnext.open_as_pos = false;
507 }
508
509 frm.$pos_btn && frm.$pos_btn.remove();
510
511 frm.$pos_btn = frm.page.add_menu_item(btn_label, function() {
512 erpnext.pos.toggle(frm);
513 });
514 } else {
515 // hack: will avoid calling refresh from refresh
516 setTimeout(function() { erpnext.pos.toggle(frm, false); }, 100);
517 }
518}
519
520erpnext.pos.toggle = function(frm, show) {
521 // Check whether it is Selling or Buying cycle
522 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
523 frm.doc.selling_price_list : frm.doc.buying_price_list;
524
525 if((show===true && frm.pos_active) || (show===false && !frm.pos_active)) {
526 return;
527 }
528
529 if(show && !price_list) {
530 frappe.throw(__("Please select Price List"));
531 }
532
533 // make pos
534 if(!frm.pos) {
535 var wrapper = frm.page.add_view("pos", "<div>");
536 frm.pos = new erpnext.pos.PointOfSale(wrapper, frm);
537 }
538
539 // toggle view
540 frm.page.set_view(frm.pos_active ? "main" : "pos");
541 frm.pos_active = !frm.pos_active;
542
543 frm.refresh();
544
545 // refresh
546 if(frm.pos_active) {
547 frm.pos.refresh();
548 }
549}