blob: e28c7189e85efe638570b1d23a696183edf1568b [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);
185 },
186 refresh_search_box: function() {
187 var me = this;
188
189 // Clear Item Box and remake item list
190 if (this.search.$input.val()) {
191 this.search.set_input("");
192 this.make_item_list();
193 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530194 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530195 update_qty: function(item_code, qty) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530196 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530197 $.each(this.frm.doc["items"] || [], function(i, d) {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530198 if (d.item_code == item_code) {
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530199 if (qty == 0) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530200 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530201 me.refresh_grid();
202 } else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530203 frappe.model.set_value(d.doctype, d.name, "qty", qty);
Akhilesh Darjeed2714312013-08-28 19:35:22 +0530204 }
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530205 }
206 });
Anand Doshicb39e082014-01-24 21:44:36 +0530207 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530208 },
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530209 refresh: function() {
210 var me = this;
Anand Doshicb39e082014-01-24 21:44:36 +0530211
212 this.refresh_item_list();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530213 this.refresh_fields();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530214
215 // if form is local then only run all these functions
216 if (this.frm.doc.docstatus===0) {
217 this.call_when_local();
218 }
219
220 this.disable_text_box_and_button();
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530221 this.set_primary_action();
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530222
223 // If quotation to is not Customer then remove party
Rushabh Mehta72e17192014-08-08 15:30:49 +0530224 if (this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer") {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530225 this.party_field.$input.prop("disabled", true);
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530226 }
227 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530228 refresh_fields: function() {
229 this.party_field.set_input(this.frm.doc[this.party.toLowerCase()]);
230 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
231
232 this.show_items_in_item_cart();
233 this.show_taxes();
234 this.set_totals();
235 },
Anand Doshicb39e082014-01-24 21:44:36 +0530236 refresh_item_list: function() {
237 var me = this;
238 // refresh item list on change of price list
239 if (this.frm.doc[this.price_list_field] != this.price_list) {
240 this.price_list = this.frm.doc[this.price_list_field];
241 this.make_item_list();
242 }
243 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530244 show_items_in_item_cart: function() {
245 var me = this;
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530246 var $items = this.wrapper.find(".items").empty();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530247
Rushabh Mehta419ae332014-12-31 15:03:14 +0530248 $.each(this.frm.doc.items|| [], function(i, d) {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530249 $(frappe.render_template("pos_bill_item", {
250 item_code: d.item_code,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530251 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530252 qty: d.qty,
Rushabh Mehta50918a82015-04-21 14:38:48 +0530253 actual_qty: d.actual_qty,
254 projected_qty: d.projected_qty,
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530255 rate: format_currency(d.rate, me.frm.doc.currency),
256 amount: format_currency(d.amount, me.frm.doc.currency)
257 })).appendTo($items);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530258 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530259
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530260 this.wrapper.find("input.pos-item-qty").on("focus", function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530261 $(this).select();
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530262 });
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530263 },
264 show_taxes: function() {
265 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +0530266 var taxes = this.frm.doc["taxes"] || [];
Anand Doshi9b955fe2015-01-05 16:19:12 +0530267 $(this.wrapper)
268 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
269 .find(".tax-table").empty();
Nabin Haited8a8452014-05-05 11:01:32 +0530270
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530271 $.each(taxes, function(i, d) {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530272 if (d.tax_amount) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530273 $(frappe.render_template("pos_tax_row", {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530274 description: d.description,
Nabin Haited8a8452014-05-05 11:01:32 +0530275 tax_amount: format_currency(flt(d.tax_amount)/flt(me.frm.doc.conversion_rate),
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530276 me.frm.doc.currency)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530277 })).appendTo(me.wrapper.find(".tax-table"));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530278 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530279 });
280 },
281 set_totals: function() {
282 var me = this;
Nabin Hait5690be12015-02-12 16:09:11 +0530283 this.wrapper.find(".net-total").text(format_currency(me.frm.doc["net_total"], me.frm.doc.currency));
284 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530285 },
286 call_when_local: function() {
287 var me = this;
288
289 // append quantity to the respective item after change from input box
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530290 $(this.wrapper).find("input.pos-item-qty").on("change", function() {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530291 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530292 me.update_qty(item_code, $(this).val());
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530293 });
294
Anand Doshicb39e082014-01-24 21:44:36 +0530295 // increase/decrease qty on plus/minus button
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530296 $(this.wrapper).find(".pos-qty-btn").on("click", function() {
297 var $item = $(this).parents(".pos-bill-item:first");
298 me.increase_decrease_qty($item, $(this).attr("data-action"));
Anand Doshicb39e082014-01-24 21:44:36 +0530299 });
300
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530301 this.focus();
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530302 },
303 focus: function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530304 if(this.frm.doc[this.party.toLowerCase()]) {
Anand Doshi9b955fe2015-01-05 16:19:12 +0530305 this.search.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530306 } else {
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530307 if(!(this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer"))
308 this.party_field.$input.focus();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +0530309 }
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530310 },
Rushabh Mehtaa9ba5ef2014-12-19 16:20:32 +0530311 increase_decrease_qty: function($item, operation) {
312 var item_code = $item.attr("data-item-code");
313 var item_qty = cint($item.find("input.pos-item-qty").val());
Anand Doshicb39e082014-01-24 21:44:36 +0530314
315 if (operation == "increase-qty")
316 this.update_qty(item_code, item_qty + 1);
Rushabh Mehta72e17192014-08-08 15:30:49 +0530317 else if (operation == "decrease-qty" && item_qty != 0)
Anand Doshicb39e082014-01-24 21:44:36 +0530318 this.update_qty(item_code, item_qty - 1);
319 },
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530320 disable_text_box_and_button: function() {
321 var me = this;
Akhilesh Darjee12425ce2013-09-02 18:48:39 +0530322 // if form is submitted & cancelled then disable all input box & buttons
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530323 $(this.wrapper)
Anand Doshi9b955fe2015-01-05 16:19:12 +0530324 .find(".pos-qty-btn")
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530325 .toggle(this.frm.doc.docstatus===0);
Nabin Haited8a8452014-05-05 11:01:32 +0530326
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530327 $(this.wrapper).find('input, button').prop("disabled", !(this.frm.doc.docstatus===0));
Anand Doshi9b955fe2015-01-05 16:19:12 +0530328
329 this.wrapper.find(".pos-item-area").toggleClass("hide", me.frm.doc.docstatus!==0);
330
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530331 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530332 set_primary_action: function() {
333 var me = this;
Rushabh Mehta88655892015-05-28 10:47:55 +0530334 if (this.frm.page.current_view_name==="main") return;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530335
336 if (this.frm.doctype == "Sales Invoice" && this.frm.doc.docstatus===0) {
337 if (!this.frm.doc.is_pos) {
338 this.frm.set_value("is_pos", 1);
339 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530340 this.frm.page.set_primary_action(__("Pay"), function() {
341 me.make_payment();
342 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530343 } else if (this.frm.doc.docstatus===1) {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530344 this.frm.page.set_primary_action(__("New"), function() {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530345 erpnext.open_as_pos = true;
346 new_doc(me.frm.doctype);
347 });
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530348 }
Rushabh Mehta16a62fa2013-08-23 13:16:22 +0530349 },
350 refresh_delete_btn: function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530351 $(this.wrapper).find(".remove-items").toggle($(".item-cart .warning").length ? true : false);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530352 },
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530353 remove_selected_items: function() {
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530354 var me = this;
355 var selected_items = [];
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530356 var no_of_items = $(this.wrapper).find("#cart tbody tr").length;
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530357 for(var x=0; x<=no_of_items - 1; x++) {
Akhilesh Darjee4233ae22013-09-30 18:18:19 +0530358 var row = $(this.wrapper).find("#cart tbody tr:eq(" + x + ")");
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530359 if(row.attr("data-selected") == "true") {
360 selected_items.push(row.attr("id"));
361 }
362 }
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530363
Nabin Haitdd38a262014-12-26 13:15:21 +0530364 var child = this.frm.doc["items"] || [];
Akhilesh Darjeee9470812013-09-19 19:09:15 +0530365
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530366 $.each(child, function(i, d) {
367 for (var i in selected_items) {
368 if (d.item_code == selected_items[i]) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530369 frappe.model.clear_doc(d.doctype, d.name);
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530370 }
371 }
372 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530373
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530374 this.refresh_grid();
375 },
376 refresh_grid: function() {
Anand Doshicb39e082014-01-24 21:44:36 +0530377 this.frm.dirty();
Nabin Haitdd38a262014-12-26 13:15:21 +0530378 this.frm.fields_dict["items"].grid.refresh();
Akhilesh Darjee2428e8d2013-09-27 12:32:26 +0530379 this.frm.script_manager.trigger("calculate_taxes_and_totals");
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530380 this.refresh();
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530381 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530382 with_modes_of_payment: function(callback) {
383 var me = this;
384 if(me.modes_of_payment) {
385 callback();
386 } else {
387 me.modes_of_payment = [];
388 $.ajax("/api/resource/Mode of Payment").success(function(data) {
389 $.each(data.data, function(i, d) { me.modes_of_payment.push(d.name); });
390 callback();
391 });
392 }
393 },
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530394 make_payment: function() {
395 var me = this;
Anand Doshi9b955fe2015-01-05 16:19:12 +0530396 var no_of_items = this.frm.doc.items.length;
Nabin Haited8a8452014-05-05 11:01:32 +0530397
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530398 if (no_of_items == 0)
Pratik Vyasb52618c2014-04-14 16:25:30 +0530399 msgprint(__("Payment cannot be made for empty cart"));
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530400 else {
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530401
402 this.with_modes_of_payment(function() {
403 // prefer cash payment!
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530404 var default_mode = me.frm.doc.mode_of_payment ? me.frm.doc.mode_of_payment :
Neil Trini Lasrado75b00e32015-07-08 12:15:28 +0530405 me.modes_of_payment.indexOf(__("Cash"))!==-1 ? __("Cash") : undefined;
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530406
407 // show payment wizard
408 var dialog = new frappe.ui.Dialog({
409 width: 400,
410 title: 'Payment',
411 fields: [
412 {fieldtype:'Currency',
413 fieldname:'total_amount', label: __('Total Amount'), read_only:1,
414 "default": me.frm.doc.grand_total, read_only: 1},
415 {fieldtype:'Select', fieldname:'mode_of_payment',
416 label: __('Mode of Payment'),
417 options: me.modes_of_payment.join('\n'), reqd: 1,
418 "default": default_mode},
419 {fieldtype:'Currency', fieldname:'paid_amount', label:__('Amount Paid'),
420 reqd:1, "default": me.frm.doc.grand_total, hidden: 1, change: function() {
421 var values = dialog.get_values();
422 dialog.set_value("change", Math.round(values.paid_amount - values.total_amount));
423 dialog.get_input("change").trigger("change");
424
425 }},
426 {fieldtype:'Currency', fieldname:'change', label: __('Change'),
427 "default": 0.0, hidden: 1, change: function() {
428 var values = dialog.get_values();
429 var write_off_amount = (flt(values.paid_amount) - flt(values.change)) - values.total_amount;
430 dialog.get_field("write_off_amount").toggle(write_off_amount);
431 dialog.set_value("write_off_amount", write_off_amount);
432 }
433 },
434 {fieldtype:'Currency', fieldname:'write_off_amount',
435 label: __('Write Off'), default: 0.0, hidden: 1},
436 ]
437 });
438 me.dialog = dialog;
439 dialog.show();
440
441 // make read only
442 dialog.get_input("total_amount").prop("disabled", true);
443 dialog.get_input("write_off_amount").prop("disabled", true);
444
445 // toggle amount paid and change
446 dialog.get_input("mode_of_payment").on("change", function() {
447 var is_cash = dialog.get_value("mode_of_payment") === __("Cash");
448 dialog.get_field("paid_amount").toggle(is_cash);
449 dialog.get_field("change").toggle(is_cash);
450
451 if (is_cash && !dialog.get_value("change")) {
452 // set to nearest 5
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530453 dialog.set_value("paid_amount", dialog.get_value("total_amount"));
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530454 dialog.get_input("paid_amount").trigger("change");
Rushabh Mehtad8de9212014-03-06 15:40:22 +0530455 }
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530456 }).trigger("change");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530457
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530458 me.set_pay_button(dialog);
Rushabh Mehta4a008f52013-07-29 15:31:11 +0530459 });
Akhilesh Darjeed6aa4bf2013-08-22 17:26:43 +0530460 }
461 },
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530462 set_pay_button: function(dialog) {
463 var me = this;
464 dialog.set_primary_action(__("Pay"), function() {
465 var values = dialog.get_values();
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530466 var is_cash = values.mode_of_payment === __("Cash");
467 if (!is_cash) {
468 values.write_off_amount = values.change = 0.0;
469 values.paid_amount = values.total_amount;
470 }
471 me.frm.set_value("mode_of_payment", values.mode_of_payment);
472
473 var paid_amount = flt((flt(values.paid_amount) - flt(values.change)) / me.frm.doc.conversion_rate, precision("paid_amount"));
474 me.frm.set_value("paid_amount", paid_amount);
475
476 // specifying writeoff amount here itself, so as to avoid recursion issue
477 me.frm.set_value("write_off_amount", me.frm.doc.base_grand_total - paid_amount);
478 me.frm.set_value("outstanding_amount", 0);
479
480 me.frm.savesubmit(this);
481 dialog.hide();
482 me.refresh();
483 })
484
485 }
Pratik Vyasb0f279a2014-04-14 17:14:23 +0530486});
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530487
488erpnext.pos.make_pos_btn = function(frm) {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530489 frm.page.add_menu_item(__("{0} View", [frm.page.current_view_name === "pos" ? "Form" : "Point-of-Sale"]), function() {
490 erpnext.pos.toggle(frm);
491 });
492
493 if(frm.pos_btn) return;
494
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530495 // Show POS button only if it is enabled from features setup
496 if (cint(sys_defaults.fs_pos_view)!==1 || frm.doctype==="Material Request") {
497 return;
498 }
499
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530500 if(!frm.pos_btn) {
501 frm.pos_btn = frm.page.add_action_icon("icon-th", function() {
Rushabh Mehta2b49f9b2015-07-14 11:01:42 +0530502 erpnext.pos.toggle(frm);
503 });
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530504 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530505
Rushabh Mehta88655892015-05-28 10:47:55 +0530506 if(erpnext.open_as_pos && frm.page.current_view_name !== "pos") {
507 erpnext.pos.toggle(frm, true);
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530508 }
509}
510
511erpnext.pos.toggle = function(frm, show) {
512 // Check whether it is Selling or Buying cycle
513 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
514 frm.doc.selling_price_list : frm.doc.buying_price_list;
515
Rushabh Mehta88655892015-05-28 10:47:55 +0530516 if(show!==undefined) {
517 if((show===true && frm.page.current_view_name === "pos")
518 || (show===false && frm.page.current_view_name === "main")) {
519 return;
520 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530521 }
522
Rushabh Mehta88655892015-05-28 10:47:55 +0530523 if(frm.page.current_view_name!=="pos") {
524 // before switching, ask for pos name
525 if(!price_list) {
526 frappe.throw(__("Please select Price List"));
527 }
528
529 if(!frm.doc.company) {
530 frappe.throw(__("Please select Company"));
531 }
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530532 }
533
534 // make pos
535 if(!frm.pos) {
536 var wrapper = frm.page.add_view("pos", "<div>");
537 frm.pos = new erpnext.pos.PointOfSale(wrapper, frm);
538 }
539
540 // toggle view
Rushabh Mehta88655892015-05-28 10:47:55 +0530541 frm.page.set_view(frm.page.current_view_name==="pos" ? "main" : "pos");
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530542
Rushabh Mehtac4dce992015-03-05 15:09:04 +0530543 frm.toolbar.current_status = null;
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530544 frm.refresh();
545
546 // refresh
Rushabh Mehta88655892015-05-28 10:47:55 +0530547 if(frm.page.current_view_name==="pos") {
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530548 frm.pos.refresh();
549 }
550}