blob: 60801e628a5282e8a35865a0921cfe161e7c9ffd [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();
Kanchan Chauhan7379fd42016-04-04 14:39:29 +053089 this.search.$input.on("keyup", 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 Mehta203cc962016-04-07 15:25:43 +0530134 item_image: obj.image ? "url('" + obj.image + "')" : null,
135 color: frappe.get_palette(obj.item_name),
136 abbr: frappe.get_abbr(obj.item_name)
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530137 })).tooltip().appendTo($wrap);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530138 });
139 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530140
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530141 // if form is local then allow this function
Nabin Haitcf301182013-10-01 18:14:16 +0530142 $(me.wrapper).find("div.pos-item").on("click", function() {
143 if(me.frm.doc.docstatus==0) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530144 me.add_to_cart($(this).attr("data-item-code"));
Nabin Haitcf301182013-10-01 18:14:16 +0530145 }
146 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530147 }
148 });
149 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530150 add_to_cart: function(item_code, serial_no) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530151 var me = this;
152 var caught = false;
153
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530154 if(!me.frm.doc[me.party.toLowerCase()] && ((me.frm.doctype == "Quotation" &&
155 me.frm.doc.quotation_to == "Customer")
156 || me.frm.doctype != "Quotation")) {
157 msgprint(__("Please select {0} first.", [me.party]));
158 return;
159 }
160
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530161 // get no_of_items
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530162 var no_of_items = me.wrapper.find(".pos-bill-item").length;
Nabin Haited8a8452014-05-05 11:01:32 +0530163
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530164 // check whether the item is already added
165 if (no_of_items != 0) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530166 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530167 if (d.item_code == item_code) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530168 caught = true;
Anand Doshicb39e082014-01-24 21:44:36 +0530169 if (serial_no)
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530170 frappe.model.set_value(d.doctype, d.name, "serial_no", d.serial_no + '\n' + serial_no);
Anand Doshicb39e082014-01-24 21:44:36 +0530171 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530172 frappe.model.set_value(d.doctype, d.name, "qty", d.qty + 1);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530173 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530174 });
175 }
Nabin Haited8a8452014-05-05 11:01:32 +0530176
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530177 // if item not found then add new item
Anand Doshicb39e082014-01-24 21:44:36 +0530178 if (!caught)
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530179 this.add_new_item_to_grid(item_code, serial_no);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530180
181 this.refresh();
182 this.refresh_search_box();
183 },
184 add_new_item_to_grid: function(item_code, serial_no) {
185 var me = this;
186
Nabin Haitdd38a262014-12-26 13:15:21 +0530187 var child = frappe.model.add_child(me.frm.doc, this.frm.doctype + " Item", "items");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530188 child.item_code = item_code;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530189 child.qty = 1;
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530190
191 if (serial_no)
192 child.serial_no = serial_no;
193
194 this.frm.script_manager.trigger("item_code", child.doctype, child.name);
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530195 frappe.after_ajax(function() {
196 me.frm.script_manager.trigger("qty", child.doctype, child.name);
197 })
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530198 },
199 refresh_search_box: function() {
200 var me = this;
201
202 // Clear Item Box and remake item list
203 if (this.search.$input.val()) {
204 this.search.set_input("");
205 this.make_item_list();
206 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530207 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530208 update_qty: function(item_code, qty) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530209 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530210 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530211 if (d.item_code == item_code) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530212 if (qty == 0) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530213 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530214 me.refresh_grid();
215 } else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530216 frappe.model.set_value(d.doctype, d.name, "qty", qty);
Akhilesh Darjeed2714312013-08-28 19:35:22 +0530217 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530218 }
219 });
Anand Doshicb39e082014-01-24 21:44:36 +0530220 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530221 },
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530222 refresh: function() {
223 var me = this;
Anand Doshicb39e082014-01-24 21:44:36 +0530224
225 this.refresh_item_list();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530226 this.refresh_fields();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530227
228 // if form is local then only run all these functions
229 if (this.frm.doc.docstatus===0) {
230 this.call_when_local();
231 }
232
233 this.disable_text_box_and_button();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530234 this.set_primary_action();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530235
236 // If quotation to is not Customer then remove party
Rushabh Mehta72e17192014-08-08 15:30:49 +0530237 if (this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer") {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530238 this.party_field.$input.prop("disabled", true);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530239 }
240 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530241 refresh_fields: function() {
242 this.party_field.set_input(this.frm.doc[this.party.toLowerCase()]);
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530243 this.party_field.frm = this.frm;
244 this.party_field.doctype = this.frm.doctype;
245 this.party_field.docname = this.frm.docname;
246
Nabin Hait3769d872015-12-18 13:12:02 +0530247 this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530248 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
249
250 this.show_items_in_item_cart();
251 this.show_taxes();
252 this.set_totals();
253 },
Anand Doshicb39e082014-01-24 21:44:36 +0530254 refresh_item_list: function() {
255 var me = this;
256 // refresh item list on change of price list
257 if (this.frm.doc[this.price_list_field] != this.price_list) {
258 this.price_list = this.frm.doc[this.price_list_field];
259 this.make_item_list();
260 }
261 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530262 show_items_in_item_cart: function() {
263 var me = this;
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530264 var $items = this.wrapper.find(".items").empty();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530265
Rushabh Mehta419ae332014-12-31 15:03:14 +0530266 $.each(this.frm.doc.items|| [], function(i, d) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530267 $(frappe.render_template("pos_bill_item", {
268 item_code: d.item_code,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530269 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530270 qty: d.qty,
Rushabh Mehta50918a82015-04-21 14:38:48 +0530271 actual_qty: d.actual_qty,
272 projected_qty: d.projected_qty,
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530273 rate: format_currency(d.rate, me.frm.doc.currency),
274 amount: format_currency(d.amount, me.frm.doc.currency)
275 })).appendTo($items);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530276 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530277
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530278 this.wrapper.find("input.pos-item-qty").on("focus", function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530279 $(this).select();
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530280 });
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530281 },
282 show_taxes: function() {
283 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530284 var taxes = this.frm.doc["taxes"] || [];
Anand Doshi9b955fe2015-01-05 16:19:12 +0530285 $(this.wrapper)
286 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
287 .find(".tax-table").empty();
Nabin Haited8a8452014-05-05 11:01:32 +0530288
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530289 $.each(taxes, function(i, d) {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530290 if (d.tax_amount) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530291 $(frappe.render_template("pos_tax_row", {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530292 description: d.description,
Nabin Haited8a8452014-05-05 11:01:32 +0530293 tax_amount: format_currency(flt(d.tax_amount)/flt(me.frm.doc.conversion_rate),
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530294 me.frm.doc.currency)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530295 })).appendTo(me.wrapper.find(".tax-table"));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530296 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530297 });
298 },
299 set_totals: function() {
300 var me = this;
Nabin Hait5690be12015-02-12 16:09:11 +0530301 this.wrapper.find(".net-total").text(format_currency(me.frm.doc["net_total"], me.frm.doc.currency));
302 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530303 },
304 call_when_local: function() {
305 var me = this;
306
307 // append quantity to the respective item after change from input box
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530308 $(this.wrapper).find("input.pos-item-qty").on("change", function() {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530309 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530310 me.update_qty(item_code, $(this).val());
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530311 });
312
Anand Doshicb39e082014-01-24 21:44:36 +0530313 // increase/decrease qty on plus/minus button
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530314 $(this.wrapper).find(".pos-qty-btn").on("click", function() {
315 var $item = $(this).parents(".pos-bill-item:first");
316 me.increase_decrease_qty($item, $(this).attr("data-action"));
Anand Doshicb39e082014-01-24 21:44:36 +0530317 });
318
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530319 this.focus();
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530320 },
321 focus: function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530322 if(this.frm.doc[this.party.toLowerCase()]) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530323 this.search.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530324 } else {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530325 if(!(this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer"))
326 this.party_field.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530327 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530328 },
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530329 increase_decrease_qty: function($item, operation) {
330 var item_code = $item.attr("data-item-code");
331 var item_qty = cint($item.find("input.pos-item-qty").val());
Anand Doshicb39e082014-01-24 21:44:36 +0530332
333 if (operation == "increase-qty")
334 this.update_qty(item_code, item_qty + 1);
Rushabh Mehta72e17192014-08-08 15:30:49 +0530335 else if (operation == "decrease-qty" && item_qty != 0)
Anand Doshicb39e082014-01-24 21:44:36 +0530336 this.update_qty(item_code, item_qty - 1);
337 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530338 disable_text_box_and_button: function() {
339 var me = this;
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530340 // if form is submitted & cancelled then disable all input box & buttons
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530341 $(this.wrapper)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530342 .find(".pos-qty-btn")
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530343 .toggle(this.frm.doc.docstatus===0);
Nabin Haited8a8452014-05-05 11:01:32 +0530344
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530345 $(this.wrapper).find('input, button').prop("disabled", !(this.frm.doc.docstatus===0));
Anand Doshi9b955fe2015-01-05 16:19:12 +0530346
347 this.wrapper.find(".pos-item-area").toggleClass("hide", me.frm.doc.docstatus!==0);
348
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530349 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530350 set_primary_action: function() {
351 var me = this;
Rushabh Mehta88655892015-05-28 10:47:55 +0530352 if (this.frm.page.current_view_name==="main") return;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530353
354 if (this.frm.doctype == "Sales Invoice" && this.frm.doc.docstatus===0) {
355 if (!this.frm.doc.is_pos) {
356 this.frm.set_value("is_pos", 1);
357 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530358 this.frm.page.set_primary_action(__("Pay"), function() {
359 me.make_payment();
360 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530361 } else if (this.frm.doc.docstatus===1) {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530362 this.frm.page.set_primary_action(__("New"), function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530363 erpnext.open_as_pos = true;
364 new_doc(me.frm.doctype);
365 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530366 }
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530367 },
368 refresh_delete_btn: function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530369 $(this.wrapper).find(".remove-items").toggle($(".item-cart .warning").length ? true : false);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530370 },
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530371 remove_selected_items: function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530372 var me = this;
373 var selected_items = [];
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530374 var no_of_items = $(this.wrapper).find("#cart tbody tr").length;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530375 for(var x=0; x<=no_of_items - 1; x++) {
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530376 var row = $(this.wrapper).find("#cart tbody tr:eq(" + x + ")");
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530377 if(row.attr("data-selected") == "true") {
378 selected_items.push(row.attr("id"));
379 }
380 }
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530381
Nabin Haitdd38a262014-12-26 13:15:21 +0530382 var child = this.frm.doc["items"] || [];
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530383
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530384 $.each(child, function(i, d) {
385 for (var i in selected_items) {
386 if (d.item_code == selected_items[i]) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530387 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530388 }
389 }
390 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530391
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530392 this.refresh_grid();
393 },
394 refresh_grid: function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530395 this.frm.dirty();
Nabin Haitdd38a262014-12-26 13:15:21 +0530396 this.frm.fields_dict["items"].grid.refresh();
Akhilesh Darjee2428e8d2013-09-27 12:32:26 +0530397 this.frm.script_manager.trigger("calculate_taxes_and_totals");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530398 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530399 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530400 with_modes_of_payment: function(callback) {
401 var me = this;
402 if(me.modes_of_payment) {
403 callback();
404 } else {
405 me.modes_of_payment = [];
406 $.ajax("/api/resource/Mode of Payment").success(function(data) {
407 $.each(data.data, function(i, d) { me.modes_of_payment.push(d.name); });
408 callback();
409 });
410 }
411 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530412 make_payment: function() {
413 var me = this;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530414 var no_of_items = this.frm.doc.items.length;
Nabin Haited8a8452014-05-05 11:01:32 +0530415
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530416 if (no_of_items == 0)
Pratik Vyasb52618c2014-04-14 16:25:30 +0530417 msgprint(__("Payment cannot be made for empty cart"));
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530418 else {
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530419
420 this.with_modes_of_payment(function() {
421 // prefer cash payment!
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530422 var default_mode = me.frm.doc.mode_of_payment ? me.frm.doc.mode_of_payment :
Neil Trini Lasrado75b00e32015-07-08 12:15:28 +0530423 me.modes_of_payment.indexOf(__("Cash"))!==-1 ? __("Cash") : undefined;
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530424
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530425 // show payment wizard
426 var dialog = new frappe.ui.Dialog({
427 width: 400,
428 title: 'Payment',
429 fields: [
430 {fieldtype:'Currency',
Nabin Hait8b63b122016-01-18 17:56:45 +0530431 fieldname:'total_amount', label: __('Total Amount'),
432 "default": me.frm.doc.grand_total},
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530433 {fieldtype:'Select', fieldname:'mode_of_payment',
434 label: __('Mode of Payment'),
435 options: me.modes_of_payment.join('\n'), reqd: 1,
436 "default": default_mode},
437 {fieldtype:'Currency', fieldname:'paid_amount', label:__('Amount Paid'),
Anand Doshib073cf42016-03-08 17:59:04 +0530438 reqd:1, "default": me.frm.doc.grand_total,
439 change: function() {
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530440 var values = dialog.get_values();
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530441
442 var actual_change = flt(values.paid_amount - values.total_amount,
Nabin Hait8b63b122016-01-18 17:56:45 +0530443 precision("paid_amount"));
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530444
Nabin Hait8b63b122016-01-18 17:56:45 +0530445 if (actual_change > 0) {
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530446 var rounded_change =
447 round_based_on_smallest_currency_fraction(actual_change,
Nabin Haitfb0b24a2016-01-20 14:46:26 +0530448 me.frm.doc.currency, precision("paid_amount"));
Nabin Hait8b63b122016-01-18 17:56:45 +0530449 } else {
450 var rounded_change = 0;
451 }
Rushabh Mehtaeab16712016-02-11 18:35:59 +0530452
Nabin Hait8b63b122016-01-18 17:56:45 +0530453 dialog.set_value("change", rounded_change);
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530454 dialog.get_input("change").trigger("change");
455
456 }},
457 {fieldtype:'Currency', fieldname:'change', label: __('Change'),
458 "default": 0.0, hidden: 1, change: function() {
459 var values = dialog.get_values();
460 var write_off_amount = (flt(values.paid_amount) - flt(values.change)) - values.total_amount;
461 dialog.get_field("write_off_amount").toggle(write_off_amount);
462 dialog.set_value("write_off_amount", write_off_amount);
463 }
464 },
465 {fieldtype:'Currency', fieldname:'write_off_amount',
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530466 label: __('Write Off'), "default": 0.0, hidden: 1},
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530467 ]
468 });
469 me.dialog = dialog;
470 dialog.show();
471
472 // make read only
473 dialog.get_input("total_amount").prop("disabled", true);
474 dialog.get_input("write_off_amount").prop("disabled", true);
475
476 // toggle amount paid and change
477 dialog.get_input("mode_of_payment").on("change", function() {
478 var is_cash = dialog.get_value("mode_of_payment") === __("Cash");
479 dialog.get_field("paid_amount").toggle(is_cash);
480 dialog.get_field("change").toggle(is_cash);
481
482 if (is_cash && !dialog.get_value("change")) {
483 // set to nearest 5
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530484 dialog.set_value("paid_amount", dialog.get_value("total_amount"));
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530485 dialog.get_input("paid_amount").trigger("change");
Anand Doshib073cf42016-03-08 17:59:04 +0530486 } else if (!is_cash) {
487 dialog.set_value("paid_amount", dialog.get_value("total_amount"));
488 dialog.set_value("change", 0);
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530489 }
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530490 }).trigger("change");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530491
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530492 me.set_pay_button(dialog);
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530493 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530494 }
495 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530496 set_pay_button: function(dialog) {
497 var me = this;
498 dialog.set_primary_action(__("Pay"), function() {
499 var values = dialog.get_values();
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530500 var is_cash = values.mode_of_payment === __("Cash");
501 if (!is_cash) {
502 values.write_off_amount = values.change = 0.0;
503 values.paid_amount = values.total_amount;
504 }
505 me.frm.set_value("mode_of_payment", values.mode_of_payment);
506
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530507 var paid_amount = flt((flt(values.paid_amount) - flt(values.change)), precision("paid_amount"));
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530508 me.frm.set_value("paid_amount", paid_amount);
Rushabh Mehta986f6a72016-01-14 12:31:56 +0530509
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530510 // specifying writeoff amount here itself, so as to avoid recursion issue
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530511 me.frm.set_value("write_off_amount", me.frm.doc.grand_total - paid_amount);
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530512 me.frm.set_value("outstanding_amount", 0);
513
514 me.frm.savesubmit(this);
515 dialog.hide();
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530516 })
517
518 }
Pratik Vyasb0f279a2014-04-14 17:14:23 +0530519});
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530520
521erpnext.pos.make_pos_btn = function(frm) {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530522 frm.page.add_menu_item(__("{0} View", [frm.page.current_view_name === "pos" ? "Form" : "Point-of-Sale"]), function() {
523 erpnext.pos.toggle(frm);
524 });
525
526 if(frm.pos_btn) return;
527
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530528 // Show POS button only if it is enabled from features setup
529 if (cint(sys_defaults.fs_pos_view)!==1 || frm.doctype==="Material Request") {
530 return;
531 }
532
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530533 if(!frm.pos_btn) {
534 frm.pos_btn = frm.page.add_action_icon("icon-th", function() {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530535 erpnext.pos.toggle(frm);
536 });
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530537 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530538
Rushabh Mehta88655892015-05-28 10:47:55 +0530539 if(erpnext.open_as_pos && frm.page.current_view_name !== "pos") {
540 erpnext.pos.toggle(frm, true);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530541 }
542}
543
544erpnext.pos.toggle = function(frm, show) {
545 // Check whether it is Selling or Buying cycle
546 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
547 frm.doc.selling_price_list : frm.doc.buying_price_list;
548
Rushabh Mehta88655892015-05-28 10:47:55 +0530549 if(show!==undefined) {
550 if((show===true && frm.page.current_view_name === "pos")
551 || (show===false && frm.page.current_view_name === "main")) {
552 return;
553 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530554 }
555
Rushabh Mehta88655892015-05-28 10:47:55 +0530556 if(frm.page.current_view_name!=="pos") {
557 // before switching, ask for pos name
558 if(!price_list) {
559 frappe.throw(__("Please select Price List"));
560 }
561
562 if(!frm.doc.company) {
563 frappe.throw(__("Please select Company"));
564 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530565 }
566
567 // make pos
568 if(!frm.pos) {
569 var wrapper = frm.page.add_view("pos", "<div>");
570 frm.pos = new erpnext.pos.PointOfSale(wrapper, frm);
571 }
572
573 // toggle view
Rushabh Mehta88655892015-05-28 10:47:55 +0530574 frm.page.set_view(frm.page.current_view_name==="pos" ? "main" : "pos");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530575
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530576 frm.toolbar.current_status = null;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530577 frm.refresh();
578
579 // refresh
Rushabh Mehta88655892015-05-28 10:47:55 +0530580 if(frm.page.current_view_name==="pos") {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530581 frm.pos.refresh();
582 }
583}