blob: 02eeab08379c7837ab7e434e189648bea736d854 [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;
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")) {
Nabin Hait5690be12015-02-12 16:09:11 +053029 this.set_transaction_defaults("Customer");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053030 }
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053031 else if (frappe.meta.has_field(cur_frm.doc.doctype, "supplier")) {
Nabin Hait5690be12015-02-12 16:09:11 +053032 this.set_transaction_defaults("Supplier");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053033 }
34 },
Nabin Hait5690be12015-02-12 16:09:11 +053035 set_transaction_defaults: function(party) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053036 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");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053042 },
Rushabh Mehta4a008f52013-07-29 15:31:11 +053043 make: function() {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053044 this.make_party();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +053045 this.make_search();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053046 this.make_item_list();
Rushabh Mehta4a008f52013-07-29 15:31:11 +053047 },
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053048 make_party: function() {
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053049 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053050 this.party_field = frappe.ui.form.make_control({
Rushabh Mehta4a008f52013-07-29 15:31:11 +053051 df: {
52 "fieldtype": "Link",
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053053 "options": this.party,
54 "label": this.party,
55 "fieldname": "pos_party",
56 "placeholder": this.party
Rushabh Mehta4a008f52013-07-29 15:31:11 +053057 },
Rushabh Mehta069672e2013-10-28 18:21:07 +053058 parent: this.wrapper.find(".party-area"),
59 only_input: true,
Rushabh Mehta4a008f52013-07-29 15:31:11 +053060 });
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053061 this.party_field.make_input();
62 this.party_field.$input.on("change", function() {
63 if(!me.party_field.autocomplete_open)
Nabin Haited8a8452014-05-05 11:01:32 +053064 frappe.model.set_value(me.frm.doctype, me.frm.docname,
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053065 me.party.toLowerCase(), this.value);
Rushabh Mehta4a008f52013-07-29 15:31:11 +053066 });
67 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053068 make_search: function() {
69 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053070 this.search = frappe.ui.form.make_control({
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053071 df: {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053072 "fieldtype": "Data",
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053073 "label": "Item",
Rushabh Mehta16a62fa2013-08-23 13:16:22 +053074 "fieldname": "pos_item",
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053075 "placeholder": "Search Item"
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053076 },
Rushabh Mehta069672e2013-10-28 18:21:07 +053077 parent: this.wrapper.find(".search-area"),
78 only_input: true,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053079 });
80 this.search.make_input();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053081 this.search.$input.on("keypress", function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053082 if(!me.search.autocomplete_open)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053083 if(me.item_timeout)
84 clearTimeout(me.item_timeout);
85 me.item_timeout = setTimeout(function() { me.make_item_list(); }, 1000);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053086 });
87 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053088 make_item_list: function() {
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053089 var me = this;
Rushabh Mehta3d65d962014-11-28 14:56:10 +053090 if(!this.price_list) {
91 msgprint(__("Price List not found or disabled"));
92 return;
93 }
94
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +053095 me.item_timeout = null;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053096 frappe.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +053097 method: 'erpnext.accounts.doctype.sales_invoice.pos.get_items',
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +053098 args: {
Akhilesh Darjeee9470812013-09-19 19:09:15 +053099 sales_or_purchase: this.sales_or_purchase,
100 price_list: this.price_list,
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530101 item: this.search.$input.val()
102 },
103 callback: function(r) {
104 var $wrap = me.wrapper.find(".item-list");
105 me.wrapper.find(".item-list").empty();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530106 if (r.message) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530107 if (r.message.length === 1) {
108 var item = r.message[0];
109 if (item.serial_no) {
110 me.add_to_cart(item.item_code, item.serial_no);
Rushabh Mehtab83fa3b2015-03-02 18:25:14 +0530111 me.search.$input.val("");
Anand Doshi9b955fe2015-01-05 16:19:12 +0530112 return;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530113
Anand Doshi9b955fe2015-01-05 16:19:12 +0530114 } else if (item.barcode) {
115 me.add_to_cart(item.item_code);
Rushabh Mehtab83fa3b2015-03-02 18:25:14 +0530116 me.search.$input.val("");
Anand Doshi9b955fe2015-01-05 16:19:12 +0530117 return;
118 }
119 }
120
121 $.each(r.message, function(index, obj) {
122 $(frappe.render_template("pos_item", {
123 item_code: obj.name,
124 item_price: format_currency(obj.price_list_rate, obj.currency),
125 item_name: obj.name===obj.item_name ? "" : obj.item_name,
Rushabh Mehta135fe342015-01-12 16:10:42 +0530126 item_image: obj.image ? "url('" + obj.image + "')" : null
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530127 })).tooltip().appendTo($wrap);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530128 });
129 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530130
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530131 // if form is local then allow this function
Nabin Haitcf301182013-10-01 18:14:16 +0530132 $(me.wrapper).find("div.pos-item").on("click", function() {
133 if(me.frm.doc.docstatus==0) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530134 me.add_to_cart($(this).attr("data-item-code"));
Nabin Haitcf301182013-10-01 18:14:16 +0530135 }
136 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530137 }
138 });
139 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530140 add_to_cart: function(item_code, serial_no) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530141 var me = this;
142 var caught = false;
143
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530144 if(!me.frm.doc[me.party.toLowerCase()] && ((me.frm.doctype == "Quotation" &&
145 me.frm.doc.quotation_to == "Customer")
146 || me.frm.doctype != "Quotation")) {
147 msgprint(__("Please select {0} first.", [me.party]));
148 return;
149 }
150
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530151 // get no_of_items
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530152 var no_of_items = me.wrapper.find(".pos-bill-item").length;
Nabin Haited8a8452014-05-05 11:01:32 +0530153
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530154 // check whether the item is already added
155 if (no_of_items != 0) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530156 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530157 if (d.item_code == item_code) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530158 caught = true;
Anand Doshicb39e082014-01-24 21:44:36 +0530159 if (serial_no)
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530160 frappe.model.set_value(d.doctype, d.name, "serial_no", d.serial_no + '\n' + serial_no);
Anand Doshicb39e082014-01-24 21:44:36 +0530161 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530162 frappe.model.set_value(d.doctype, d.name, "qty", d.qty + 1);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530163 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530164 });
165 }
Nabin Haited8a8452014-05-05 11:01:32 +0530166
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530167 // if item not found then add new item
Anand Doshicb39e082014-01-24 21:44:36 +0530168 if (!caught)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530169 this.add_new_item_to_grid(item_code, serial_no);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530170
171 this.refresh();
172 this.refresh_search_box();
173 },
174 add_new_item_to_grid: function(item_code, serial_no) {
175 var me = this;
176
Nabin Haitdd38a262014-12-26 13:15:21 +0530177 var child = frappe.model.add_child(me.frm.doc, this.frm.doctype + " Item", "items");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530178 child.item_code = item_code;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530179 child.qty = 1;
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530180
181 if (serial_no)
182 child.serial_no = serial_no;
183
184 this.frm.script_manager.trigger("item_code", child.doctype, child.name);
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530185 frappe.after_ajax(function() {
186 me.frm.script_manager.trigger("qty", child.doctype, child.name);
187 })
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530188 },
189 refresh_search_box: function() {
190 var me = this;
191
192 // Clear Item Box and remake item list
193 if (this.search.$input.val()) {
194 this.search.set_input("");
195 this.make_item_list();
196 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530197 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530198 update_qty: function(item_code, qty) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530199 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530200 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530201 if (d.item_code == item_code) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530202 if (qty == 0) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530203 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530204 me.refresh_grid();
205 } else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530206 frappe.model.set_value(d.doctype, d.name, "qty", qty);
Akhilesh Darjeed2714312013-08-28 19:35:22 +0530207 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530208 }
209 });
Anand Doshicb39e082014-01-24 21:44:36 +0530210 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530211 },
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530212 refresh: function() {
213 var me = this;
Anand Doshicb39e082014-01-24 21:44:36 +0530214
215 this.refresh_item_list();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530216 this.refresh_fields();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530217
218 // if form is local then only run all these functions
219 if (this.frm.doc.docstatus===0) {
220 this.call_when_local();
221 }
222
223 this.disable_text_box_and_button();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530224 this.set_primary_action();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530225
226 // If quotation to is not Customer then remove party
Rushabh Mehta72e17192014-08-08 15:30:49 +0530227 if (this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer") {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530228 this.party_field.$input.prop("disabled", true);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530229 }
230 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530231 refresh_fields: function() {
232 this.party_field.set_input(this.frm.doc[this.party.toLowerCase()]);
233 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
234
235 this.show_items_in_item_cart();
236 this.show_taxes();
237 this.set_totals();
238 },
Anand Doshicb39e082014-01-24 21:44:36 +0530239 refresh_item_list: function() {
240 var me = this;
241 // refresh item list on change of price list
242 if (this.frm.doc[this.price_list_field] != this.price_list) {
243 this.price_list = this.frm.doc[this.price_list_field];
244 this.make_item_list();
245 }
246 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530247 show_items_in_item_cart: function() {
248 var me = this;
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530249 var $items = this.wrapper.find(".items").empty();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530250
Rushabh Mehta419ae332014-12-31 15:03:14 +0530251 $.each(this.frm.doc.items|| [], function(i, d) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530252 $(frappe.render_template("pos_bill_item", {
253 item_code: d.item_code,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530254 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530255 qty: d.qty,
Rushabh Mehta50918a82015-04-21 14:38:48 +0530256 actual_qty: d.actual_qty,
257 projected_qty: d.projected_qty,
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530258 rate: format_currency(d.rate, me.frm.doc.currency),
259 amount: format_currency(d.amount, me.frm.doc.currency)
260 })).appendTo($items);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530261 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530262
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530263 this.wrapper.find("input.pos-item-qty").on("focus", function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530264 $(this).select();
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530265 });
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530266 },
267 show_taxes: function() {
268 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530269 var taxes = this.frm.doc["taxes"] || [];
Anand Doshi9b955fe2015-01-05 16:19:12 +0530270 $(this.wrapper)
271 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
272 .find(".tax-table").empty();
Nabin Haited8a8452014-05-05 11:01:32 +0530273
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530274 $.each(taxes, function(i, d) {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530275 if (d.tax_amount) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530276 $(frappe.render_template("pos_tax_row", {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530277 description: d.description,
Nabin Haited8a8452014-05-05 11:01:32 +0530278 tax_amount: format_currency(flt(d.tax_amount)/flt(me.frm.doc.conversion_rate),
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530279 me.frm.doc.currency)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530280 })).appendTo(me.wrapper.find(".tax-table"));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530281 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530282 });
283 },
284 set_totals: function() {
285 var me = this;
Nabin Hait5690be12015-02-12 16:09:11 +0530286 this.wrapper.find(".net-total").text(format_currency(me.frm.doc["net_total"], me.frm.doc.currency));
287 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530288 },
289 call_when_local: function() {
290 var me = this;
291
292 // append quantity to the respective item after change from input box
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530293 $(this.wrapper).find("input.pos-item-qty").on("change", function() {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530294 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530295 me.update_qty(item_code, $(this).val());
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530296 });
297
Anand Doshicb39e082014-01-24 21:44:36 +0530298 // increase/decrease qty on plus/minus button
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530299 $(this.wrapper).find(".pos-qty-btn").on("click", function() {
300 var $item = $(this).parents(".pos-bill-item:first");
301 me.increase_decrease_qty($item, $(this).attr("data-action"));
Anand Doshicb39e082014-01-24 21:44:36 +0530302 });
303
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530304 this.focus();
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530305 },
306 focus: function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530307 if(this.frm.doc[this.party.toLowerCase()]) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530308 this.search.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530309 } else {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530310 if(!(this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer"))
311 this.party_field.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530312 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530313 },
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530314 increase_decrease_qty: function($item, operation) {
315 var item_code = $item.attr("data-item-code");
316 var item_qty = cint($item.find("input.pos-item-qty").val());
Anand Doshicb39e082014-01-24 21:44:36 +0530317
318 if (operation == "increase-qty")
319 this.update_qty(item_code, item_qty + 1);
Rushabh Mehta72e17192014-08-08 15:30:49 +0530320 else if (operation == "decrease-qty" && item_qty != 0)
Anand Doshicb39e082014-01-24 21:44:36 +0530321 this.update_qty(item_code, item_qty - 1);
322 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530323 disable_text_box_and_button: function() {
324 var me = this;
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530325 // if form is submitted & cancelled then disable all input box & buttons
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530326 $(this.wrapper)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530327 .find(".pos-qty-btn")
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530328 .toggle(this.frm.doc.docstatus===0);
Nabin Haited8a8452014-05-05 11:01:32 +0530329
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530330 $(this.wrapper).find('input, button').prop("disabled", !(this.frm.doc.docstatus===0));
Anand Doshi9b955fe2015-01-05 16:19:12 +0530331
332 this.wrapper.find(".pos-item-area").toggleClass("hide", me.frm.doc.docstatus!==0);
333
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530334 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530335 set_primary_action: function() {
336 var me = this;
Rushabh Mehta88655892015-05-28 10:47:55 +0530337 if (this.frm.page.current_view_name==="main") return;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530338
339 if (this.frm.doctype == "Sales Invoice" && this.frm.doc.docstatus===0) {
340 if (!this.frm.doc.is_pos) {
341 this.frm.set_value("is_pos", 1);
342 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530343 this.frm.page.set_primary_action(__("Pay"), function() {
344 me.make_payment();
345 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530346 } else if (this.frm.doc.docstatus===1) {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530347 this.frm.page.set_primary_action(__("New"), function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530348 erpnext.open_as_pos = true;
349 new_doc(me.frm.doctype);
350 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530351 }
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530352 },
353 refresh_delete_btn: function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530354 $(this.wrapper).find(".remove-items").toggle($(".item-cart .warning").length ? true : false);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530355 },
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530356 remove_selected_items: function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530357 var me = this;
358 var selected_items = [];
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530359 var no_of_items = $(this.wrapper).find("#cart tbody tr").length;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530360 for(var x=0; x<=no_of_items - 1; x++) {
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530361 var row = $(this.wrapper).find("#cart tbody tr:eq(" + x + ")");
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530362 if(row.attr("data-selected") == "true") {
363 selected_items.push(row.attr("id"));
364 }
365 }
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530366
Nabin Haitdd38a262014-12-26 13:15:21 +0530367 var child = this.frm.doc["items"] || [];
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530368
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530369 $.each(child, function(i, d) {
370 for (var i in selected_items) {
371 if (d.item_code == selected_items[i]) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530372 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530373 }
374 }
375 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530376
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530377 this.refresh_grid();
378 },
379 refresh_grid: function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530380 this.frm.dirty();
Nabin Haitdd38a262014-12-26 13:15:21 +0530381 this.frm.fields_dict["items"].grid.refresh();
Akhilesh Darjee2428e8d2013-09-27 12:32:26 +0530382 this.frm.script_manager.trigger("calculate_taxes_and_totals");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530383 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530384 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530385 with_modes_of_payment: function(callback) {
386 var me = this;
387 if(me.modes_of_payment) {
388 callback();
389 } else {
390 me.modes_of_payment = [];
391 $.ajax("/api/resource/Mode of Payment").success(function(data) {
392 $.each(data.data, function(i, d) { me.modes_of_payment.push(d.name); });
393 callback();
394 });
395 }
396 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530397 make_payment: function() {
398 var me = this;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530399 var no_of_items = this.frm.doc.items.length;
Nabin Haited8a8452014-05-05 11:01:32 +0530400
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530401 if (no_of_items == 0)
Pratik Vyasb52618c2014-04-14 16:25:30 +0530402 msgprint(__("Payment cannot be made for empty cart"));
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530403 else {
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530404
405 this.with_modes_of_payment(function() {
406 // prefer cash payment!
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530407 var default_mode = me.frm.doc.mode_of_payment ? me.frm.doc.mode_of_payment :
Neil Trini Lasrado75b00e32015-07-08 12:15:28 +0530408 me.modes_of_payment.indexOf(__("Cash"))!==-1 ? __("Cash") : undefined;
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530409
410 // show payment wizard
411 var dialog = new frappe.ui.Dialog({
412 width: 400,
413 title: 'Payment',
414 fields: [
415 {fieldtype:'Currency',
416 fieldname:'total_amount', label: __('Total Amount'), read_only:1,
417 "default": me.frm.doc.grand_total, read_only: 1},
418 {fieldtype:'Select', fieldname:'mode_of_payment',
419 label: __('Mode of Payment'),
420 options: me.modes_of_payment.join('\n'), reqd: 1,
421 "default": default_mode},
422 {fieldtype:'Currency', fieldname:'paid_amount', label:__('Amount Paid'),
423 reqd:1, "default": me.frm.doc.grand_total, hidden: 1, change: function() {
424 var values = dialog.get_values();
425 dialog.set_value("change", Math.round(values.paid_amount - values.total_amount));
426 dialog.get_input("change").trigger("change");
427
428 }},
429 {fieldtype:'Currency', fieldname:'change', label: __('Change'),
430 "default": 0.0, hidden: 1, change: function() {
431 var values = dialog.get_values();
432 var write_off_amount = (flt(values.paid_amount) - flt(values.change)) - values.total_amount;
433 dialog.get_field("write_off_amount").toggle(write_off_amount);
434 dialog.set_value("write_off_amount", write_off_amount);
435 }
436 },
437 {fieldtype:'Currency', fieldname:'write_off_amount',
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530438 label: __('Write Off'), "default": 0.0, hidden: 1},
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530439 ]
440 });
441 me.dialog = dialog;
442 dialog.show();
443
444 // make read only
445 dialog.get_input("total_amount").prop("disabled", true);
446 dialog.get_input("write_off_amount").prop("disabled", true);
447
448 // toggle amount paid and change
449 dialog.get_input("mode_of_payment").on("change", function() {
450 var is_cash = dialog.get_value("mode_of_payment") === __("Cash");
451 dialog.get_field("paid_amount").toggle(is_cash);
452 dialog.get_field("change").toggle(is_cash);
453
454 if (is_cash && !dialog.get_value("change")) {
455 // set to nearest 5
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530456 dialog.set_value("paid_amount", dialog.get_value("total_amount"));
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530457 dialog.get_input("paid_amount").trigger("change");
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530458 }
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530459 }).trigger("change");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530460
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530461 me.set_pay_button(dialog);
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530462 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530463 }
464 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530465 set_pay_button: function(dialog) {
466 var me = this;
467 dialog.set_primary_action(__("Pay"), function() {
468 var values = dialog.get_values();
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530469 var is_cash = values.mode_of_payment === __("Cash");
470 if (!is_cash) {
471 values.write_off_amount = values.change = 0.0;
472 values.paid_amount = values.total_amount;
473 }
474 me.frm.set_value("mode_of_payment", values.mode_of_payment);
475
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530476 var paid_amount = flt((flt(values.paid_amount) - flt(values.change)), precision("paid_amount"));
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530477 me.frm.set_value("paid_amount", paid_amount);
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530478
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530479 // specifying writeoff amount here itself, so as to avoid recursion issue
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530480 me.frm.set_value("write_off_amount", me.frm.doc.grand_total - paid_amount);
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530481 me.frm.set_value("outstanding_amount", 0);
482
483 me.frm.savesubmit(this);
484 dialog.hide();
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530485 })
486
487 }
Pratik Vyasb0f279a2014-04-14 17:14:23 +0530488});
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530489
490erpnext.pos.make_pos_btn = function(frm) {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530491 frm.page.add_menu_item(__("{0} View", [frm.page.current_view_name === "pos" ? "Form" : "Point-of-Sale"]), function() {
492 erpnext.pos.toggle(frm);
493 });
494
495 if(frm.pos_btn) return;
496
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530497 // Show POS button only if it is enabled from features setup
498 if (cint(sys_defaults.fs_pos_view)!==1 || frm.doctype==="Material Request") {
499 return;
500 }
501
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530502 if(!frm.pos_btn) {
503 frm.pos_btn = frm.page.add_action_icon("icon-th", function() {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530504 erpnext.pos.toggle(frm);
505 });
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530506 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530507
Rushabh Mehta88655892015-05-28 10:47:55 +0530508 if(erpnext.open_as_pos && frm.page.current_view_name !== "pos") {
509 erpnext.pos.toggle(frm, true);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530510 }
511}
512
513erpnext.pos.toggle = function(frm, show) {
514 // Check whether it is Selling or Buying cycle
515 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
516 frm.doc.selling_price_list : frm.doc.buying_price_list;
517
Rushabh Mehta88655892015-05-28 10:47:55 +0530518 if(show!==undefined) {
519 if((show===true && frm.page.current_view_name === "pos")
520 || (show===false && frm.page.current_view_name === "main")) {
521 return;
522 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530523 }
524
Rushabh Mehta88655892015-05-28 10:47:55 +0530525 if(frm.page.current_view_name!=="pos") {
526 // before switching, ask for pos name
527 if(!price_list) {
528 frappe.throw(__("Please select Price List"));
529 }
530
531 if(!frm.doc.company) {
532 frappe.throw(__("Please select Company"));
533 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530534 }
535
536 // make pos
537 if(!frm.pos) {
538 var wrapper = frm.page.add_view("pos", "<div>");
539 frm.pos = new erpnext.pos.PointOfSale(wrapper, frm);
540 }
541
542 // toggle view
Rushabh Mehta88655892015-05-28 10:47:55 +0530543 frm.page.set_view(frm.page.current_view_name==="pos" ? "main" : "pos");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530544
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530545 frm.toolbar.current_status = null;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530546 frm.refresh();
547
548 // refresh
Rushabh Mehta88655892015-05-28 10:47:55 +0530549 if(frm.page.current_view_name==="pos") {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530550 frm.pos.refresh();
551 }
552}