2013-11-20 07:29:58 +00:00
|
|
|
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
// License: GNU General Public License v3. See license.txt
|
|
|
|
|
2015-01-06 11:44:13 +00:00
|
|
|
frappe.provide("erpnext.pos");
|
|
|
|
|
|
|
|
erpnext.pos.PointOfSale = Class.extend({
|
2013-07-29 10:01:11 +00:00
|
|
|
init: function(wrapper, frm) {
|
|
|
|
this.wrapper = wrapper;
|
|
|
|
this.frm = frm;
|
2014-12-19 10:50:32 +00:00
|
|
|
this.wrapper.html(frappe.render_template("pos", {}));
|
2014-05-05 05:31:32 +00:00
|
|
|
|
2013-11-27 10:46:32 +00:00
|
|
|
this.check_transaction_type();
|
2013-07-29 10:01:11 +00:00
|
|
|
this.make();
|
|
|
|
|
|
|
|
var me = this;
|
|
|
|
$(this.frm.wrapper).on("refresh-fields", function() {
|
|
|
|
me.refresh();
|
|
|
|
});
|
|
|
|
|
2014-01-03 12:45:07 +00:00
|
|
|
this.wrapper.find('input.discount-amount').on("change", function() {
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.model.set_value(me.frm.doctype, me.frm.docname, "discount_amount", this.value);
|
2013-12-26 15:01:42 +00:00
|
|
|
});
|
2013-11-27 10:46:32 +00:00
|
|
|
},
|
|
|
|
check_transaction_type: function() {
|
|
|
|
var me = this;
|
2013-08-22 11:56:43 +00:00
|
|
|
|
2013-11-27 10:46:32 +00:00
|
|
|
// Check whether the transaction is "Sales" or "Purchase"
|
2014-02-14 10:17:51 +00:00
|
|
|
if (frappe.meta.has_field(cur_frm.doc.doctype, "customer")) {
|
2013-11-27 10:46:32 +00:00
|
|
|
this.set_transaction_defaults("Customer", "export");
|
|
|
|
}
|
2014-02-14 10:17:51 +00:00
|
|
|
else if (frappe.meta.has_field(cur_frm.doc.doctype, "supplier")) {
|
2013-11-27 10:46:32 +00:00
|
|
|
this.set_transaction_defaults("Supplier", "import");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
set_transaction_defaults: function(party, export_or_import) {
|
|
|
|
var me = this;
|
|
|
|
this.party = party;
|
2014-05-05 05:31:32 +00:00
|
|
|
this.price_list = (party == "Customer" ?
|
2013-11-27 10:46:32 +00:00
|
|
|
this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
|
2014-01-22 11:25:32 +00:00
|
|
|
this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
|
2013-11-27 10:46:32 +00:00
|
|
|
this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
|
|
|
|
this.net_total = "net_total_" + export_or_import;
|
|
|
|
this.grand_total = "grand_total_" + export_or_import;
|
2014-02-10 13:50:15 +00:00
|
|
|
// this.amount = export_or_import + "_amount";
|
|
|
|
// this.rate = export_or_import + "_rate";
|
2013-11-27 10:46:32 +00:00
|
|
|
},
|
2013-07-29 10:01:11 +00:00
|
|
|
make: function() {
|
2013-09-18 13:05:12 +00:00
|
|
|
this.make_party();
|
2013-12-26 15:01:42 +00:00
|
|
|
this.make_search();
|
2013-08-22 11:56:43 +00:00
|
|
|
this.make_item_list();
|
2013-07-29 10:01:11 +00:00
|
|
|
},
|
2013-09-18 13:05:12 +00:00
|
|
|
make_party: function() {
|
2013-09-23 06:57:16 +00:00
|
|
|
var me = this;
|
2014-02-14 10:17:51 +00:00
|
|
|
this.party_field = frappe.ui.form.make_control({
|
2013-07-29 10:01:11 +00:00
|
|
|
df: {
|
|
|
|
"fieldtype": "Link",
|
2013-09-18 13:05:12 +00:00
|
|
|
"options": this.party,
|
|
|
|
"label": this.party,
|
|
|
|
"fieldname": "pos_party",
|
|
|
|
"placeholder": this.party
|
2013-07-29 10:01:11 +00:00
|
|
|
},
|
2013-10-28 12:51:07 +00:00
|
|
|
parent: this.wrapper.find(".party-area"),
|
|
|
|
only_input: true,
|
2013-07-29 10:01:11 +00:00
|
|
|
});
|
2013-09-18 13:05:12 +00:00
|
|
|
this.party_field.make_input();
|
|
|
|
this.party_field.$input.on("change", function() {
|
|
|
|
if(!me.party_field.autocomplete_open)
|
2014-05-05 05:31:32 +00:00
|
|
|
frappe.model.set_value(me.frm.doctype, me.frm.docname,
|
2013-09-18 13:05:12 +00:00
|
|
|
me.party.toLowerCase(), this.value);
|
2013-08-22 11:56:43 +00:00
|
|
|
});
|
2013-07-29 10:01:11 +00:00
|
|
|
},
|
2013-08-22 11:56:43 +00:00
|
|
|
make_search: function() {
|
|
|
|
var me = this;
|
2014-02-14 10:17:51 +00:00
|
|
|
this.search = frappe.ui.form.make_control({
|
2013-08-22 11:56:43 +00:00
|
|
|
df: {
|
2013-11-27 10:46:32 +00:00
|
|
|
"fieldtype": "Data",
|
2013-08-22 11:56:43 +00:00
|
|
|
"label": "Item",
|
2013-08-23 07:46:22 +00:00
|
|
|
"fieldname": "pos_item",
|
2013-11-27 10:46:32 +00:00
|
|
|
"placeholder": "Search Item"
|
2013-08-22 11:56:43 +00:00
|
|
|
},
|
2013-10-28 12:51:07 +00:00
|
|
|
parent: this.wrapper.find(".search-area"),
|
|
|
|
only_input: true,
|
2013-08-22 11:56:43 +00:00
|
|
|
});
|
|
|
|
this.search.make_input();
|
2013-11-27 10:46:32 +00:00
|
|
|
this.search.$input.on("keypress", function() {
|
2013-08-22 11:56:43 +00:00
|
|
|
if(!me.search.autocomplete_open)
|
2013-11-27 10:46:32 +00:00
|
|
|
if(me.item_timeout)
|
|
|
|
clearTimeout(me.item_timeout);
|
|
|
|
me.item_timeout = setTimeout(function() { me.make_item_list(); }, 1000);
|
2013-08-22 11:56:43 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
make_item_list: function() {
|
2013-09-23 06:57:16 +00:00
|
|
|
var me = this;
|
2014-11-28 09:26:10 +00:00
|
|
|
if(!this.price_list) {
|
|
|
|
msgprint(__("Price List not found or disabled"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-27 10:46:32 +00:00
|
|
|
me.item_timeout = null;
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.call({
|
2013-12-12 13:42:19 +00:00
|
|
|
method: 'erpnext.accounts.doctype.sales_invoice.pos.get_items',
|
2013-08-22 11:56:43 +00:00
|
|
|
args: {
|
2013-09-19 13:39:15 +00:00
|
|
|
sales_or_purchase: this.sales_or_purchase,
|
|
|
|
price_list: this.price_list,
|
2013-08-22 11:56:43 +00:00
|
|
|
item: this.search.$input.val()
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
var $wrap = me.wrapper.find(".item-list");
|
|
|
|
me.wrapper.find(".item-list").empty();
|
2013-11-27 10:46:32 +00:00
|
|
|
if (r.message) {
|
2015-01-05 10:49:12 +00:00
|
|
|
if (r.message.length === 1) {
|
|
|
|
var item = r.message[0];
|
|
|
|
if (item.serial_no) {
|
|
|
|
me.add_to_cart(item.item_code, item.serial_no);
|
|
|
|
this.search.$input.val("");
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else if (item.barcode) {
|
|
|
|
me.add_to_cart(item.item_code);
|
|
|
|
this.search.$input.val("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-27 10:46:32 +00:00
|
|
|
$.each(r.message, function(index, obj) {
|
2015-01-05 10:49:12 +00:00
|
|
|
$(frappe.render_template("pos_item", {
|
|
|
|
item_code: obj.name,
|
|
|
|
item_price: format_currency(obj.price_list_rate, obj.currency),
|
|
|
|
item_name: obj.name===obj.item_name ? "" : obj.item_name,
|
|
|
|
item_image: obj.image
|
2015-01-06 11:44:13 +00:00
|
|
|
})).tooltip().appendTo($wrap);
|
2013-11-27 10:46:32 +00:00
|
|
|
});
|
|
|
|
}
|
2013-08-22 11:56:43 +00:00
|
|
|
|
2013-09-02 13:18:39 +00:00
|
|
|
// if form is local then allow this function
|
2013-10-01 12:44:16 +00:00
|
|
|
$(me.wrapper).find("div.pos-item").on("click", function() {
|
|
|
|
if(me.frm.doc.docstatus==0) {
|
2015-01-05 10:49:12 +00:00
|
|
|
me.add_to_cart($(this).attr("data-item-code"));
|
2013-10-01 12:44:16 +00:00
|
|
|
}
|
|
|
|
});
|
2013-08-22 11:56:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2013-10-18 08:54:21 +00:00
|
|
|
add_to_cart: function(item_code, serial_no) {
|
2013-08-22 11:56:43 +00:00
|
|
|
var me = this;
|
|
|
|
var caught = false;
|
|
|
|
|
2014-12-19 10:50:32 +00:00
|
|
|
if(!me.frm.doc[me.party.toLowerCase()] && ((me.frm.doctype == "Quotation" &&
|
|
|
|
me.frm.doc.quotation_to == "Customer")
|
|
|
|
|| me.frm.doctype != "Quotation")) {
|
|
|
|
msgprint(__("Please select {0} first.", [me.party]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-22 11:56:43 +00:00
|
|
|
// get no_of_items
|
2014-12-19 10:50:32 +00:00
|
|
|
var no_of_items = me.wrapper.find(".pos-bill-item").length;
|
2014-05-05 05:31:32 +00:00
|
|
|
|
2013-08-22 11:56:43 +00:00
|
|
|
// check whether the item is already added
|
|
|
|
if (no_of_items != 0) {
|
2014-12-26 07:45:21 +00:00
|
|
|
$.each(this.frm.doc["items"] || [], function(i, d) {
|
2013-10-18 08:54:21 +00:00
|
|
|
if (d.item_code == item_code) {
|
2013-08-22 11:56:43 +00:00
|
|
|
caught = true;
|
2014-01-22 13:55:51 +00:00
|
|
|
if (serial_no)
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.model.set_value(d.doctype, d.name, "serial_no", d.serial_no + '\n' + serial_no);
|
2014-01-22 13:55:51 +00:00
|
|
|
else
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.model.set_value(d.doctype, d.name, "qty", d.qty + 1);
|
2013-10-18 08:54:21 +00:00
|
|
|
}
|
2013-08-22 11:56:43 +00:00
|
|
|
});
|
|
|
|
}
|
2014-05-05 05:31:32 +00:00
|
|
|
|
2013-10-18 08:54:21 +00:00
|
|
|
// if item not found then add new item
|
2014-01-22 13:55:51 +00:00
|
|
|
if (!caught)
|
2013-11-27 10:46:32 +00:00
|
|
|
this.add_new_item_to_grid(item_code, serial_no);
|
|
|
|
|
|
|
|
this.refresh();
|
|
|
|
this.refresh_search_box();
|
|
|
|
},
|
|
|
|
add_new_item_to_grid: function(item_code, serial_no) {
|
|
|
|
var me = this;
|
|
|
|
|
2014-12-26 07:45:21 +00:00
|
|
|
var child = frappe.model.add_child(me.frm.doc, this.frm.doctype + " Item", "items");
|
2013-11-27 10:46:32 +00:00
|
|
|
child.item_code = item_code;
|
2015-01-05 10:49:12 +00:00
|
|
|
child.qty = 1;
|
2013-10-18 08:54:21 +00:00
|
|
|
|
2013-11-27 10:46:32 +00:00
|
|
|
if (serial_no)
|
|
|
|
child.serial_no = serial_no;
|
2013-10-18 08:54:21 +00:00
|
|
|
|
2013-11-27 10:46:32 +00:00
|
|
|
this.frm.script_manager.trigger("item_code", child.doctype, child.name);
|
|
|
|
},
|
|
|
|
refresh_search_box: function() {
|
|
|
|
var me = this;
|
|
|
|
|
|
|
|
// Clear Item Box and remake item list
|
|
|
|
if (this.search.$input.val()) {
|
|
|
|
this.search.set_input("");
|
|
|
|
this.make_item_list();
|
2013-08-22 11:56:43 +00:00
|
|
|
}
|
|
|
|
},
|
2013-10-18 08:54:21 +00:00
|
|
|
update_qty: function(item_code, qty) {
|
2013-08-22 11:56:43 +00:00
|
|
|
var me = this;
|
2014-12-26 07:45:21 +00:00
|
|
|
$.each(this.frm.doc["items"] || [], function(i, d) {
|
2013-08-22 11:56:43 +00:00
|
|
|
if (d.item_code == item_code) {
|
2013-11-27 10:46:32 +00:00
|
|
|
if (qty == 0) {
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.model.clear_doc(d.doctype, d.name);
|
2013-11-27 10:46:32 +00:00
|
|
|
me.refresh_grid();
|
|
|
|
} else {
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.model.set_value(d.doctype, d.name, "qty", qty);
|
2013-08-28 14:05:22 +00:00
|
|
|
}
|
2013-08-22 11:56:43 +00:00
|
|
|
}
|
2013-07-29 10:01:11 +00:00
|
|
|
});
|
2014-01-22 11:25:32 +00:00
|
|
|
this.refresh();
|
2013-07-29 10:01:11 +00:00
|
|
|
},
|
|
|
|
refresh: function() {
|
|
|
|
var me = this;
|
2014-01-22 11:25:32 +00:00
|
|
|
|
|
|
|
this.refresh_item_list();
|
2015-01-06 11:44:13 +00:00
|
|
|
this.refresh_fields();
|
2013-11-27 10:46:32 +00:00
|
|
|
|
|
|
|
// if form is local then only run all these functions
|
|
|
|
if (this.frm.doc.docstatus===0) {
|
|
|
|
this.call_when_local();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.disable_text_box_and_button();
|
2015-01-06 11:44:13 +00:00
|
|
|
this.set_primary_action();
|
2013-11-27 10:46:32 +00:00
|
|
|
|
|
|
|
// If quotation to is not Customer then remove party
|
2014-08-08 10:00:49 +00:00
|
|
|
if (this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer") {
|
2014-12-19 10:50:32 +00:00
|
|
|
this.party_field.$input.prop("disabled", true);
|
2013-11-27 10:46:32 +00:00
|
|
|
}
|
|
|
|
},
|
2015-01-06 11:44:13 +00:00
|
|
|
refresh_fields: function() {
|
|
|
|
this.party_field.set_input(this.frm.doc[this.party.toLowerCase()]);
|
|
|
|
this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
|
|
|
|
|
|
|
|
this.show_items_in_item_cart();
|
|
|
|
this.show_taxes();
|
|
|
|
this.set_totals();
|
|
|
|
},
|
2014-01-22 11:25:32 +00:00
|
|
|
refresh_item_list: function() {
|
|
|
|
var me = this;
|
|
|
|
// refresh item list on change of price list
|
|
|
|
if (this.frm.doc[this.price_list_field] != this.price_list) {
|
|
|
|
this.price_list = this.frm.doc[this.price_list_field];
|
|
|
|
this.make_item_list();
|
|
|
|
}
|
|
|
|
},
|
2013-11-27 10:46:32 +00:00
|
|
|
show_items_in_item_cart: function() {
|
|
|
|
var me = this;
|
2014-12-19 10:50:32 +00:00
|
|
|
var $items = this.wrapper.find(".items").empty();
|
2013-08-22 11:56:43 +00:00
|
|
|
|
2014-12-31 09:33:14 +00:00
|
|
|
$.each(this.frm.doc.items|| [], function(i, d) {
|
2014-12-19 10:50:32 +00:00
|
|
|
$(frappe.render_template("pos_bill_item", {
|
|
|
|
item_code: d.item_code,
|
2015-01-05 10:49:12 +00:00
|
|
|
item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
|
2014-12-19 10:50:32 +00:00
|
|
|
qty: d.qty,
|
|
|
|
rate: format_currency(d.rate, me.frm.doc.currency),
|
|
|
|
amount: format_currency(d.amount, me.frm.doc.currency)
|
|
|
|
})).appendTo($items);
|
2013-08-22 11:56:43 +00:00
|
|
|
});
|
2014-01-13 08:04:34 +00:00
|
|
|
|
2014-12-19 10:50:32 +00:00
|
|
|
this.wrapper.find("input.pos-item-qty").on("focus", function() {
|
2014-01-22 11:25:32 +00:00
|
|
|
$(this).select();
|
2014-01-13 08:04:34 +00:00
|
|
|
});
|
2013-11-27 10:46:32 +00:00
|
|
|
},
|
|
|
|
show_taxes: function() {
|
|
|
|
var me = this;
|
2014-12-26 07:45:21 +00:00
|
|
|
var taxes = this.frm.doc["taxes"] || [];
|
2015-01-05 10:49:12 +00:00
|
|
|
$(this.wrapper)
|
|
|
|
.find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
|
|
|
|
.find(".tax-table").empty();
|
2014-05-05 05:31:32 +00:00
|
|
|
|
2013-08-23 07:46:22 +00:00
|
|
|
$.each(taxes, function(i, d) {
|
2014-01-10 09:35:44 +00:00
|
|
|
if (d.tax_amount) {
|
2015-01-05 10:49:12 +00:00
|
|
|
$(frappe.render_template("pos_tax_row", {
|
2014-01-10 09:35:44 +00:00
|
|
|
description: d.description,
|
2014-05-05 05:31:32 +00:00
|
|
|
tax_amount: format_currency(flt(d.tax_amount)/flt(me.frm.doc.conversion_rate),
|
2014-01-10 09:35:44 +00:00
|
|
|
me.frm.doc.currency)
|
2015-01-05 10:49:12 +00:00
|
|
|
})).appendTo(me.wrapper.find(".tax-table"));
|
2014-01-10 09:35:44 +00:00
|
|
|
}
|
2013-08-23 07:46:22 +00:00
|
|
|
});
|
2013-11-27 10:46:32 +00:00
|
|
|
},
|
|
|
|
set_totals: function() {
|
|
|
|
var me = this;
|
2014-05-05 05:31:32 +00:00
|
|
|
this.wrapper.find(".net-total").text(format_currency(this.frm.doc[this.net_total],
|
2013-11-27 10:46:32 +00:00
|
|
|
me.frm.doc.currency));
|
2014-05-05 05:31:32 +00:00
|
|
|
this.wrapper.find(".grand-total").text(format_currency(this.frm.doc[this.grand_total],
|
2013-11-27 10:46:32 +00:00
|
|
|
me.frm.doc.currency));
|
|
|
|
},
|
|
|
|
call_when_local: function() {
|
|
|
|
var me = this;
|
2013-08-23 07:46:22 +00:00
|
|
|
|
2013-11-27 10:46:32 +00:00
|
|
|
// append quantity to the respective item after change from input box
|
2014-12-19 10:50:32 +00:00
|
|
|
$(this.wrapper).find("input.pos-item-qty").on("change", function() {
|
2015-01-05 10:49:12 +00:00
|
|
|
var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
|
2013-11-27 10:46:32 +00:00
|
|
|
me.update_qty(item_code, $(this).val());
|
|
|
|
});
|
2013-08-22 11:56:43 +00:00
|
|
|
|
2014-01-22 11:25:32 +00:00
|
|
|
// increase/decrease qty on plus/minus button
|
2014-12-19 10:50:32 +00:00
|
|
|
$(this.wrapper).find(".pos-qty-btn").on("click", function() {
|
|
|
|
var $item = $(this).parents(".pos-bill-item:first");
|
|
|
|
me.increase_decrease_qty($item, $(this).attr("data-action"));
|
2014-01-22 11:25:32 +00:00
|
|
|
});
|
|
|
|
|
2015-01-06 11:44:13 +00:00
|
|
|
this.focus();
|
2014-12-19 10:50:32 +00:00
|
|
|
},
|
|
|
|
focus: function() {
|
2015-01-06 11:44:13 +00:00
|
|
|
if(this.frm.doc[this.party.toLowerCase()]) {
|
2015-01-05 10:49:12 +00:00
|
|
|
this.search.$input.focus();
|
2014-08-12 09:45:39 +00:00
|
|
|
} else {
|
2014-12-19 10:50:32 +00:00
|
|
|
if(!(this.frm.doctype == "Quotation" && this.frm.doc.quotation_to!="Customer"))
|
|
|
|
this.party_field.$input.focus();
|
2014-08-12 09:45:39 +00:00
|
|
|
}
|
2013-11-27 10:46:32 +00:00
|
|
|
},
|
2014-12-19 10:50:32 +00:00
|
|
|
increase_decrease_qty: function($item, operation) {
|
|
|
|
var item_code = $item.attr("data-item-code");
|
|
|
|
var item_qty = cint($item.find("input.pos-item-qty").val());
|
2014-01-22 11:25:32 +00:00
|
|
|
|
|
|
|
if (operation == "increase-qty")
|
|
|
|
this.update_qty(item_code, item_qty + 1);
|
2014-08-08 10:00:49 +00:00
|
|
|
else if (operation == "decrease-qty" && item_qty != 0)
|
2014-01-22 11:25:32 +00:00
|
|
|
this.update_qty(item_code, item_qty - 1);
|
|
|
|
},
|
2013-11-27 10:46:32 +00:00
|
|
|
disable_text_box_and_button: function() {
|
|
|
|
var me = this;
|
2013-09-02 13:18:39 +00:00
|
|
|
// if form is submitted & cancelled then disable all input box & buttons
|
2014-03-06 10:10:22 +00:00
|
|
|
$(this.wrapper)
|
2015-01-05 10:49:12 +00:00
|
|
|
.find(".pos-qty-btn")
|
2014-03-06 10:10:22 +00:00
|
|
|
.toggle(this.frm.doc.docstatus===0);
|
2014-05-05 05:31:32 +00:00
|
|
|
|
2014-03-06 10:10:22 +00:00
|
|
|
$(this.wrapper).find('input, button').prop("disabled", !(this.frm.doc.docstatus===0));
|
2015-01-05 10:49:12 +00:00
|
|
|
|
|
|
|
this.wrapper.find(".pos-item-area").toggleClass("hide", me.frm.doc.docstatus!==0);
|
|
|
|
|
2013-11-27 10:46:32 +00:00
|
|
|
},
|
2015-01-06 11:44:13 +00:00
|
|
|
set_primary_action: function() {
|
|
|
|
var me = this;
|
|
|
|
if (!this.frm.pos_active) return;
|
|
|
|
|
|
|
|
if (this.frm.doctype == "Sales Invoice" && this.frm.doc.docstatus===0) {
|
|
|
|
if (!this.frm.doc.is_pos) {
|
|
|
|
this.frm.set_value("is_pos", 1);
|
|
|
|
}
|
|
|
|
this.frm.page.clear_actions();
|
|
|
|
this.frm.page.set_primary_action(__("Pay"), function() {
|
|
|
|
me.make_payment();
|
|
|
|
});
|
|
|
|
this.frm.toolbar.current_status = null;
|
|
|
|
} else if (this.frm.doc.docstatus===1) {
|
|
|
|
this.frm.page.clear_actions();
|
|
|
|
this.frm.page.set_primary_action(__("New"), function() {
|
|
|
|
me.frm.pos_active = false;
|
|
|
|
erpnext.open_as_pos = true;
|
|
|
|
new_doc(me.frm.doctype);
|
|
|
|
});
|
|
|
|
this.frm.toolbar.current_status = null;
|
|
|
|
}
|
2013-08-23 07:46:22 +00:00
|
|
|
},
|
|
|
|
refresh_delete_btn: function() {
|
2014-01-10 09:35:44 +00:00
|
|
|
$(this.wrapper).find(".remove-items").toggle($(".item-cart .warning").length ? true : false);
|
2013-08-22 11:56:43 +00:00
|
|
|
},
|
2014-01-10 09:35:44 +00:00
|
|
|
remove_selected_items: function() {
|
2013-08-22 11:56:43 +00:00
|
|
|
var me = this;
|
|
|
|
var selected_items = [];
|
2013-09-30 12:48:19 +00:00
|
|
|
var no_of_items = $(this.wrapper).find("#cart tbody tr").length;
|
2013-08-22 11:56:43 +00:00
|
|
|
for(var x=0; x<=no_of_items - 1; x++) {
|
2013-09-30 12:48:19 +00:00
|
|
|
var row = $(this.wrapper).find("#cart tbody tr:eq(" + x + ")");
|
2013-08-22 11:56:43 +00:00
|
|
|
if(row.attr("data-selected") == "true") {
|
|
|
|
selected_items.push(row.attr("id"));
|
|
|
|
}
|
|
|
|
}
|
2013-09-19 13:39:15 +00:00
|
|
|
|
2014-12-26 07:45:21 +00:00
|
|
|
var child = this.frm.doc["items"] || [];
|
2013-09-19 13:39:15 +00:00
|
|
|
|
2013-08-22 11:56:43 +00:00
|
|
|
$.each(child, function(i, d) {
|
|
|
|
for (var i in selected_items) {
|
|
|
|
if (d.item_code == selected_items[i]) {
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.model.clear_doc(d.doctype, d.name);
|
2013-08-22 11:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-01-10 09:35:44 +00:00
|
|
|
|
2013-11-27 10:46:32 +00:00
|
|
|
this.refresh_grid();
|
|
|
|
},
|
|
|
|
refresh_grid: function() {
|
2014-01-22 13:55:51 +00:00
|
|
|
this.frm.dirty();
|
2014-12-26 07:45:21 +00:00
|
|
|
this.frm.fields_dict["items"].grid.refresh();
|
2013-09-27 07:02:26 +00:00
|
|
|
this.frm.script_manager.trigger("calculate_taxes_and_totals");
|
2013-11-27 10:46:32 +00:00
|
|
|
this.refresh();
|
2013-08-22 11:56:43 +00:00
|
|
|
},
|
|
|
|
make_payment: function() {
|
|
|
|
var me = this;
|
2015-01-05 10:49:12 +00:00
|
|
|
var no_of_items = this.frm.doc.items.length;
|
2014-05-05 05:31:32 +00:00
|
|
|
|
2013-08-22 11:56:43 +00:00
|
|
|
if (no_of_items == 0)
|
2014-04-14 10:55:30 +00:00
|
|
|
msgprint(__("Payment cannot be made for empty cart"));
|
2013-08-22 11:56:43 +00:00
|
|
|
else {
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.call({
|
2013-12-12 13:42:19 +00:00
|
|
|
method: 'erpnext.accounts.doctype.sales_invoice.pos.get_mode_of_payment',
|
2013-08-22 11:56:43 +00:00
|
|
|
callback: function(r) {
|
2014-05-05 05:31:32 +00:00
|
|
|
if(!r.message) {
|
2014-03-06 10:10:22 +00:00
|
|
|
msgprint(__("Please add to Modes of Payment from Setup."))
|
2014-05-05 05:31:32 +00:00
|
|
|
return;
|
2014-03-06 10:10:22 +00:00
|
|
|
}
|
2015-01-06 11:44:13 +00:00
|
|
|
|
|
|
|
var modes_of_payment = r.message;
|
|
|
|
|
|
|
|
// prefer cash payment!
|
|
|
|
var default_mode = modes_of_payment.indexOf(__("Cash"))!==-1 ? __("Cash") : undefined;
|
2013-08-22 11:56:43 +00:00
|
|
|
|
|
|
|
// show payment wizard
|
2014-02-14 10:17:51 +00:00
|
|
|
var dialog = new frappe.ui.Dialog({
|
2013-08-22 11:56:43 +00:00
|
|
|
width: 400,
|
2014-05-05 05:31:32 +00:00
|
|
|
title: 'Payment',
|
2013-08-22 11:56:43 +00:00
|
|
|
fields: [
|
2015-01-06 11:44:13 +00:00
|
|
|
{fieldtype:'Currency', fieldname:'total_amount', label: __('Total Amount'), read_only:1,
|
|
|
|
options:"currency", default:me.frm.doc.grand_total_export, read_only: 1},
|
|
|
|
{fieldtype:'Select', fieldname:'mode_of_payment', label: __('Mode of Payment'),
|
|
|
|
options:modes_of_payment.join('\n'), reqd: 1, default: default_mode},
|
|
|
|
{fieldtype:'Currency', fieldname:'paid_amount', label:__('Amount Paid'), reqd:1,
|
|
|
|
options: "currency",
|
|
|
|
default:me.frm.doc.grand_total_export, hidden: 1},
|
|
|
|
{fieldtype:'Currency', fieldname:'change', label: __('Change'), options: "currency",
|
|
|
|
default: 0.0, hidden: 1},
|
|
|
|
{fieldtype:'Currency', fieldname:'write_off_amount', label: __('Write Off'), options: "currency",
|
|
|
|
default: 0.0, hidden: 1},
|
2013-08-22 11:56:43 +00:00
|
|
|
{fieldtype:'Button', fieldname:'pay', label:'Pay'}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
dialog.show();
|
2015-01-06 11:44:13 +00:00
|
|
|
|
|
|
|
// make read only
|
2013-08-30 12:52:58 +00:00
|
|
|
dialog.get_input("total_amount").prop("disabled", true);
|
2015-01-06 11:44:13 +00:00
|
|
|
dialog.get_input("write_off_amount").prop("disabled", true);
|
|
|
|
|
|
|
|
dialog.get_input("paid_amount").on("change", function() {
|
|
|
|
var values = dialog.get_values();
|
|
|
|
dialog.set_value("change", Math.round(values.paid_amount - values.total_amount));
|
|
|
|
dialog.get_input("change").trigger("change");
|
|
|
|
});
|
|
|
|
|
|
|
|
dialog.get_input("change").on("change", function() {
|
|
|
|
var values = dialog.get_values();
|
|
|
|
var write_off_amount = (flt(values.paid_amount) - flt(values.change)) - values.total_amount;
|
|
|
|
dialog.set_value("write_off_amount", write_off_amount);
|
|
|
|
dialog.fields_dict.write_off_amount.$wrapper.toggleClass("hide", !!!write_off_amount);
|
|
|
|
});
|
|
|
|
|
|
|
|
// toggle amount paid and change
|
|
|
|
dialog.get_input("mode_of_payment").on("change", function() {
|
|
|
|
var is_cash = dialog.get_value("mode_of_payment") === __("Cash");
|
|
|
|
dialog.fields_dict.paid_amount.$wrapper.toggleClass("hide", !is_cash);
|
|
|
|
dialog.fields_dict.change.$wrapper.toggleClass("hide", !is_cash);
|
|
|
|
|
|
|
|
if (is_cash && !dialog.get_value("change")) {
|
|
|
|
// set to nearest 5
|
|
|
|
var paid_amount = 5 * Math.ceil(dialog.get_value("total_amount") / 5);
|
|
|
|
dialog.set_value("paid_amount", paid_amount);
|
|
|
|
dialog.get_input("paid_amount").trigger("change");
|
|
|
|
}
|
|
|
|
}).trigger("change");
|
2014-05-05 05:31:32 +00:00
|
|
|
|
2013-08-22 11:56:43 +00:00
|
|
|
dialog.fields_dict.pay.input.onclick = function() {
|
2015-01-06 11:44:13 +00:00
|
|
|
var values = dialog.get_values();
|
|
|
|
var is_cash = values.mode_of_payment === __("Cash");
|
|
|
|
if (!is_cash) {
|
|
|
|
values.write_off_amount = values.change = 0.0;
|
|
|
|
values.paid_amount = values.total_amount;
|
|
|
|
}
|
|
|
|
me.frm.set_value("mode_of_payment", values.mode_of_payment);
|
|
|
|
|
|
|
|
var paid_amount = flt((flt(values.paid_amount) - flt(values.change)) / me.frm.doc.conversion_rate, precision("paid_amount"));
|
|
|
|
me.frm.set_value("paid_amount", paid_amount);
|
|
|
|
|
|
|
|
// specifying writeoff amount here itself, so as to avoid recursion issue
|
|
|
|
me.frm.set_value("write_off_amount", me.frm.doc.grand_total - paid_amount);
|
|
|
|
me.frm.set_value("outstanding_amount", 0);
|
|
|
|
|
|
|
|
me.frm.savesubmit(this);
|
2013-08-22 11:56:43 +00:00
|
|
|
dialog.hide();
|
|
|
|
me.refresh();
|
|
|
|
};
|
|
|
|
}
|
2013-07-29 10:01:11 +00:00
|
|
|
});
|
2013-08-22 11:56:43 +00:00
|
|
|
}
|
|
|
|
},
|
2014-04-14 11:44:23 +00:00
|
|
|
});
|
2015-01-06 11:44:13 +00:00
|
|
|
|
|
|
|
erpnext.pos.make_pos_btn = function(frm) {
|
|
|
|
// Show POS button only if it is enabled from features setup
|
|
|
|
if (cint(sys_defaults.fs_pos_view)!==1 || frm.doctype==="Material Request") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(frm.doc.docstatus <= 1) {
|
|
|
|
if(!frm.pos_active) {
|
|
|
|
var btn_label = __("POS View"),
|
|
|
|
icon = "icon-th";
|
|
|
|
} else {
|
|
|
|
var btn_label = __("Form View"),
|
|
|
|
icon = "icon-file-text";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(erpnext.open_as_pos) {
|
|
|
|
erpnext.pos.toggle(frm, true);
|
|
|
|
erpnext.open_as_pos = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
frm.$pos_btn && frm.$pos_btn.remove();
|
|
|
|
|
|
|
|
frm.$pos_btn = frm.page.add_menu_item(btn_label, function() {
|
|
|
|
erpnext.pos.toggle(frm);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// hack: will avoid calling refresh from refresh
|
|
|
|
setTimeout(function() { erpnext.pos.toggle(frm, false); }, 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
erpnext.pos.toggle = function(frm, show) {
|
|
|
|
// Check whether it is Selling or Buying cycle
|
|
|
|
var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
|
|
|
|
frm.doc.selling_price_list : frm.doc.buying_price_list;
|
|
|
|
|
|
|
|
if((show===true && frm.pos_active) || (show===false && !frm.pos_active)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(show && !price_list) {
|
|
|
|
frappe.throw(__("Please select Price List"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// make pos
|
|
|
|
if(!frm.pos) {
|
|
|
|
var wrapper = frm.page.add_view("pos", "<div>");
|
|
|
|
frm.pos = new erpnext.pos.PointOfSale(wrapper, frm);
|
|
|
|
}
|
|
|
|
|
|
|
|
// toggle view
|
|
|
|
frm.page.set_view(frm.pos_active ? "main" : "pos");
|
|
|
|
frm.pos_active = !frm.pos_active;
|
|
|
|
|
|
|
|
frm.refresh();
|
|
|
|
|
|
|
|
// refresh
|
|
|
|
if(frm.pos_active) {
|
|
|
|
frm.pos.refresh();
|
|
|
|
}
|
|
|
|
}
|