brotherton-erpnext/erpnext/accounts/page/pos/pos.js

1097 lines
30 KiB
JavaScript
Raw Normal View History

2016-04-09 09:01:09 +00:00
frappe.provide("erpnext.pos");
{% include "erpnext/public/js/controllers/taxes_and_totals.js" %}
2015-01-29 12:39:11 +00:00
frappe.pages['pos'].on_page_load = function(wrapper) {
var page = frappe.ui.make_app_page({
2014-08-08 10:00:49 +00:00
parent: wrapper,
2016-05-20 18:25:45 +00:00
title: __('Point of Sale'),
2014-08-08 10:00:49 +00:00
single_column: true
});
2016-05-11 09:34:57 +00:00
wrapper.pos = new erpnext.pos.PointOfSale(wrapper)
}
frappe.pages['pos'].refresh = function(wrapper) {
window.onbeforeunload = function () {
return wrapper.pos.beforeunload()
}
2016-04-09 09:01:09 +00:00
}
2014-08-08 10:00:49 +00:00
2016-05-11 09:34:57 +00:00
2016-04-09 09:01:09 +00:00
erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
2016-05-11 09:34:57 +00:00
init: function(wrapper){
this.page = wrapper.page;
this.wrapper = $(wrapper).find('.page-content');
2016-04-09 09:01:09 +00:00
this.set_indicator();
this.onload();
this.make_menu_list();
this.si_docs = this.get_doc_from_localstorage();
},
2016-05-20 07:24:44 +00:00
beforeunload: function(e){
if(this.connection_status == false && frappe.get_route()[0] == "pos"){
e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = __("You are in offline mode. You will not be able to reload until you have network.");
return
}
// For Safari
return __("You are in offline mode. You will not be able to reload until you have network.");
}
},
2016-04-09 09:01:09 +00:00
check_internet_connection: function(){
var me = this;
//Check Internet connection after every 30 seconds
setInterval(function(){
me.set_indicator();
2016-05-20 18:25:45 +00:00
}, 5000)
2016-04-09 09:01:09 +00:00
},
2014-08-08 10:00:49 +00:00
2016-04-09 09:01:09 +00:00
set_indicator: function(){
var me = this;
// navigator.onLine
2016-05-20 18:25:45 +00:00
this.connection_status = false;
2016-07-22 07:54:33 +00:00
this.page.set_indicator(__("Offline"), "grey")
2016-04-09 09:01:09 +00:00
frappe.call({
method:"frappe.handler.ping",
callback: function(r){
if(r.message){
me.connection_status = true;
2016-07-22 07:54:33 +00:00
me.page.set_indicator(__("Online"), "green")
2016-04-09 09:01:09 +00:00
}
}
})
},
2015-03-05 09:39:04 +00:00
2016-04-09 09:01:09 +00:00
onload: function(){
var me = this;
this.get_data_from_server(function(){
2016-05-20 07:24:44 +00:00
me.create_new();
2016-04-09 09:01:09 +00:00
});
},
make_menu_list: function(){
var me = this;
2016-05-20 07:24:44 +00:00
this.page.add_menu_item(__("New Sales Invoice"), function() {
me.save_previous_entry();
me.create_new();
2016-05-20 07:24:44 +00:00
})
this.page.add_menu_item(__("View Offline Records"), function(){
me.show_unsync_invoice_list();
2016-04-09 09:01:09 +00:00
});
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
this.page.add_menu_item(__("Sync Master Data"), function(){
2016-05-11 09:34:57 +00:00
me.get_data_from_server(function(){
2016-07-21 17:58:04 +00:00
me.load_data(false);
me.make_customer();
me.make_item_list();
2016-07-21 17:58:04 +00:00
me.set_missing_values();
2016-05-11 09:34:57 +00:00
})
2016-04-09 09:01:09 +00:00
});
2016-05-20 07:24:44 +00:00
this.page.add_menu_item(__("Sync Offline Invoices"), function(){
me.sync_sales_invoice()
});
2016-05-20 07:24:44 +00:00
this.page.add_menu_item(__("POS Profile"), function() {
2016-05-20 18:25:45 +00:00
frappe.set_route('List', 'POS Profile');
2016-05-20 07:24:44 +00:00
});
2016-04-09 09:01:09 +00:00
},
show_unsync_invoice_list: function(){
var me = this;
this.si_docs = this.get_doc_from_localstorage();
this.list_dialog = new frappe.ui.Dialog({
title: 'Invoice List'
});
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
this.list_dialog.show();
this.list_body = this.list_dialog.body;
if(this.si_docs.length > 0){
$(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\
2016-09-11 20:07:46 +00:00
<div class="col-xs-1">Sr</div>\
<div class="col-xs-3">Customer</div>\
<div class="col-xs-2 text-left">Status</div>\
2016-09-11 20:07:46 +00:00
<div class="col-xs-3 text-right">Paid Amount</div>\
<div class="col-xs-3 text-right">Grand Total</div>\
</div>')
$.each(this.si_docs, function(index, data){
for(key in data) {
$(frappe.render_template("pos_invoice_list", {
sr: index + 1,
name: key,
customer: data[key].customer,
2016-09-11 20:07:46 +00:00
paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
data: me.get_doctype_status(data[key])
})).appendTo($(me.list_body));
}
})
2016-05-20 07:24:44 +00:00
$(this.list_body).find('.list-row').click(function() {
me.name = $(this).attr('invoice-name')
doc_data = me.get_invoice_doc(me.si_docs)
if(doc_data){
me.frm.doc = doc_data[0][me.name];
me.set_missing_values();
2016-07-21 17:58:04 +00:00
me.refresh(false);
me.disable_input_field();
me.list_dialog.hide();
}
})
}else{
$(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")}))
}
2016-04-09 09:01:09 +00:00
},
2016-05-20 07:24:44 +00:00
2016-05-20 18:25:45 +00:00
get_doctype_status: function(doc){
2016-09-11 20:07:46 +00:00
if(doc.docstatus == 0) {
2016-05-20 18:25:45 +00:00
return {status: "Draft", indicator: "red"}
2016-09-11 20:07:46 +00:00
}else if(doc.outstanding_amount == 0) {
return {status: "Paid", indicator: "green"}
}else {
return {status: "Submitted", indicator: "blue"}
2016-05-20 18:25:45 +00:00
}
},
2016-05-11 09:34:57 +00:00
set_missing_values: function(){
var me = this;
doc = JSON.parse(localStorage.getItem('doc'))
if(this.frm.doc.payments.length == 0){
this.frm.doc.payments = doc.payments;
2016-08-01 13:05:18 +00:00
this.calculate_outstanding_amount();
2016-05-11 09:34:57 +00:00
}
2016-07-21 17:58:04 +00:00
if(this.frm.doc.customer){
this.party_field.$input.val(this.frm.doc.customer);
}
if(!this.frm.doc.write_off_account){
this.frm.doc.write_off_account = doc.write_off_account
}
if(!this.frm.doc.account_for_change_amount){
this.frm.doc.account_for_change_amount = doc.account_for_change_amount
}
2016-05-11 09:34:57 +00:00
},
2016-04-09 09:01:09 +00:00
get_invoice_doc: function(si_docs){
var me = this;
this.si_docs = this.get_doc_from_localstorage();
2014-08-08 10:00:49 +00:00
2016-04-09 09:01:09 +00:00
return $.grep(this.si_docs, function(data){
for(key in data){
return key == me.name
2014-08-08 10:00:49 +00:00
}
2016-05-20 07:24:44 +00:00
})
2016-04-09 09:01:09 +00:00
},
get_data_from_server: function(callback){
var me = this;
frappe.call({
method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
freeze: true,
2016-05-11 09:34:57 +00:00
freeze_message: __("Master data syncing, it might take some time"),
2016-04-09 09:01:09 +00:00
callback: function(r){
2016-11-20 18:11:13 +00:00
me.init_master_data(r)
2016-04-09 09:01:09 +00:00
localStorage.setItem('doc', JSON.stringify(r.message.doc));
2016-11-20 18:11:13 +00:00
me.set_interval_for_si_sync();
me.check_internet_connection();
2016-04-09 09:01:09 +00:00
if(callback){
callback();
}
}
})
},
2016-05-20 07:24:44 +00:00
2016-11-20 18:11:13 +00:00
init_master_data: function(r){
var me = this;
this.meta = r.message.meta;
this.item_data = r.message.items;
this.customers = r.message.customers;
this.serial_no_data = r.message.serial_no_data;
this.batch_no_data = r.message.batch_no_data;
this.tax_data = r.message.tax_data;
this.price_list_data = r.message.price_list_data;
this.bin_data = r.message.bin_data;
this.pricing_rules = r.message.pricing_rules;
this.print_template = r.message.print_template;
this.pos_profile_data = r.message.pos_profile;
this.default_customer = r.message.default_customer || null;
},
2016-05-20 18:25:45 +00:00
save_previous_entry : function(){
if(this.frm.doc.items.length > 0){
this.create_invoice()
}
},
2016-04-09 09:01:09 +00:00
create_new: function(){
var me = this;
2016-05-11 09:34:57 +00:00
this.frm = {}
2016-04-09 09:01:09 +00:00
this.name = '';
2016-07-21 17:58:04 +00:00
this.load_data(true);
2016-04-09 09:01:09 +00:00
this.setup();
},
2016-07-21 17:58:04 +00:00
load_data: function(load_doc){
2016-11-20 18:11:13 +00:00
var me = this;
2016-07-21 17:58:04 +00:00
2016-11-20 18:11:13 +00:00
this.items = this.item_data;
2016-11-27 06:34:12 +00:00
this.actual_qty_dict = {};
2016-07-21 17:58:04 +00:00
if(load_doc) {
this.frm.doc = JSON.parse(localStorage.getItem('doc'));
}
2016-04-09 09:01:09 +00:00
2016-11-20 18:11:13 +00:00
$.each(this.meta, function(i, data){
2016-04-09 09:01:09 +00:00
frappe.meta.sync(data)
})
2016-05-20 07:24:44 +00:00
this.print_template = frappe.render_template("print_template",
2016-11-20 18:11:13 +00:00
{content: this.print_template, title:"POS",
2016-09-18 18:47:32 +00:00
base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css})
2016-04-09 09:01:09 +00:00
},
setup: function(){
this.wrapper.html(frappe.render_template("pos", this.frm.doc));
this.set_transaction_defaults("Customer");
this.make();
this.set_primary_action();
},
set_transaction_defaults: function(party) {
var me = this;
this.party = party;
this.price_list = (party == "Customer" ?
this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
},
make: function() {
this.make_search();
this.make_customer();
this.make_item_list();
2016-04-09 09:01:09 +00:00
this.make_discount_field()
},
make_search: function() {
var me = this;
this.search = frappe.ui.form.make_control({
df: {
"fieldtype": "Data",
"label": "Item",
"fieldname": "pos_item",
2016-07-22 07:54:33 +00:00
"placeholder": __("Search Item")
2016-04-09 09:01:09 +00:00
},
parent: this.wrapper.find(".search-area"),
only_input: true,
});
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
this.search.make_input();
this.search.$input.on("keyup", function() {
setTimeout(function() {
me.items = me.get_items();
me.make_item_list();
2016-04-09 09:01:09 +00:00
}, 1000);
});
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
this.party_field = frappe.ui.form.make_control({
df: {
"fieldtype": "Data",
"options": this.party,
"label": this.party,
"fieldname": this.party.toLowerCase(),
2016-06-10 07:04:41 +00:00
"placeholder": __("Select or add new customer")
2016-04-09 09:01:09 +00:00
},
parent: this.wrapper.find(".party-area"),
only_input: true,
});
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
this.party_field.make_input();
2016-09-11 20:07:46 +00:00
this.set_focus()
},
set_focus: function(){
if(this.default_customer){
this.search.$input.focus();
}else{
this.party_field.$input.focus();
}
2016-04-09 09:01:09 +00:00
},
make_customer: function() {
var me = this;
2016-05-20 07:24:44 +00:00
2016-07-21 17:58:04 +00:00
if(this.default_customer && !this.frm.doc.customer){
this.party_field.$input.val(this.default_customer);
this.frm.doc.customer = this.default_customer;
2014-08-08 10:00:49 +00:00
}
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
this.party_field.$input.autocomplete({
2016-05-20 18:25:45 +00:00
autoFocus: true,
2016-04-09 09:01:09 +00:00
source: function (request, response) {
me.customer_data = me.get_customers(request.term)
response($.map(me.customer_data, function(data){
2016-05-20 07:24:44 +00:00
return {label: data.name, value: data.name,
2016-04-09 09:01:09 +00:00
customer_group: data.customer_group, territory: data.territory}
}))
},
change: function(event, ui){
if(ui.item){
me.frm.doc.customer = ui.item.label;
me.frm.doc.customer_name = ui.item.customer_name;
me.frm.doc.customer_group = ui.item.customer_group;
me.frm.doc.territory = ui.item.territory;
}else{
me.frm.doc.customer = me.party_field.$input.val();
}
me.refresh();
}
2016-05-20 18:25:45 +00:00
}).on("focus", function(){
setTimeout(function() {
if(!me.party_field.$input.val()) {
me.party_field.$input.autocomplete( "search", " " );
}
}, 500);
});
2016-04-09 09:01:09 +00:00
},
2014-08-08 10:00:49 +00:00
2016-04-09 09:01:09 +00:00
get_customers: function(key){
var me = this;
2016-05-20 18:25:45 +00:00
key = key.toLowerCase().trim()
var re = new RegExp('%', 'g');
var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'))
2016-05-20 18:25:45 +00:00
if(key){
return $.grep(this.customers, function(data) {
if(reg.test(data.name.toLowerCase())
|| reg.test(data.customer_name.toLowerCase())
|| (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
2016-05-20 18:25:45 +00:00
return data
}
})
}else{
customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
return customers.slice(0, 20)
}
2016-04-09 09:01:09 +00:00
},
make_item_list: function() {
2016-04-09 09:01:09 +00:00
var me = this;
if(!this.price_list) {
msgprint(__("Price List not found or disabled"));
return;
}
me.item_timeout = null;
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
var $wrap = me.wrapper.find(".item-list");
me.wrapper.find(".item-list").empty();
if (this.items.length > 0) {
2016-04-09 09:01:09 +00:00
$.each(this.items, function(index, obj) {
if(index < 30){
2016-04-09 09:01:09 +00:00
$(frappe.render_template("pos_item", {
item_code: obj.name,
2016-11-20 18:11:13 +00:00
item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
2016-04-09 09:01:09 +00:00
item_name: obj.name===obj.item_name ? "" : obj.item_name,
item_image: obj.image ? "url('" + obj.image + "')" : null,
color: frappe.get_palette(obj.item_name),
abbr: frappe.get_abbr(obj.item_name)
})).tooltip().appendTo($wrap);
}
});
} else {
$("<h4>Searching record not found.</h4>").appendTo($wrap)
2016-04-09 09:01:09 +00:00
}
2016-05-20 07:24:44 +00:00
if(this.items.length == 1
&& this.search.$input.val()) {
2016-04-09 09:01:09 +00:00
this.search.$input.val("");
this.add_to_cart();
}
// if form is local then allow this function
$(me.wrapper).find("div.pos-item").on("click", function() {
me.customer_validate();
if(me.frm.doc.docstatus==0) {
me.items = me.get_items($(this).attr("data-item-code"))
me.add_to_cart();
}
});
},
get_items: function(item_code){
// To search item as per the key enter
var me = this;
2016-07-15 21:32:20 +00:00
this.item_serial_no = {};
this.item_batch_no = {};
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
if(item_code){
2016-11-20 18:11:13 +00:00
return $.grep(this.item_data, function(item){
2016-04-09 09:01:09 +00:00
if(item.item_code == item_code ){
return true
}
})
}
key = this.search.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g,'\\$&');
var re = new RegExp('%', 'g');
var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
search_status = true
2016-04-09 09:01:09 +00:00
if(key){
2016-11-20 18:11:13 +00:00
return $.grep(this.item_data, function(item){
if(search_status){
2016-11-20 18:11:13 +00:00
if(in_list(me.batch_no_data[item.item_code], me.search.$input.val())){
search_status = false;
return me.item_batch_no[item.item_code] = me.search.$input.val()
2016-11-20 18:11:13 +00:00
} else if( me.serial_no_data[item.item_code]
&& in_list(Object.keys(me.serial_no_data[item.item_code]), me.search.$input.val())) {
search_status = false;
2016-11-20 18:11:13 +00:00
me.item_serial_no[item.item_code] = [me.search.$input.val(), me.serial_no_data[item.item_code][me.search.$input.val()]]
return true
} else if(item.barcode == me.search.$input.val()) {
search_status = false;
return item.barcode == me.search.$input.val();
} else if(reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase()) ){
return true
}
2016-04-09 09:01:09 +00:00
}
})
}else{
2016-11-20 18:11:13 +00:00
return this.item_data;
2016-04-09 09:01:09 +00:00
}
},
update_qty: function() {
var me = this;
$(this.wrapper).find(".pos-item-qty").on("change", function(){
var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
2016-05-20 18:25:45 +00:00
me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
2016-04-09 09:01:09 +00:00
})
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
$(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
2016-05-20 18:25:45 +00:00
me.update_qty_rate_against_item_code(item_code, "qty", qty);
2016-04-09 09:01:09 +00:00
})
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
$(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
2016-05-20 18:25:45 +00:00
me.update_qty_rate_against_item_code(item_code, "qty", qty);
})
},
update_rate: function() {
var me = this;
$(this.wrapper).find(".pos-item-rate").on("change", function(){
var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
2016-04-09 09:01:09 +00:00
})
},
2016-05-20 18:25:45 +00:00
update_qty_rate_against_item_code: function(item_code, field, value){
2016-04-09 09:01:09 +00:00
var me = this;
2016-05-20 18:25:45 +00:00
if(value < 0){
frappe.throw(__("Enter value must be positive"));
2016-04-09 09:01:09 +00:00
}
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
this.remove_item = []
$.each(this.frm.doc["items"] || [], function(i, d) {
if(d.serial_no){
me.validate_serial_no_qty(d, item_code, field, value)
2016-07-15 21:32:20 +00:00
}
2016-04-09 09:01:09 +00:00
if (d.item_code == item_code) {
2016-05-20 18:25:45 +00:00
d[field] = flt(value);
2016-04-09 09:01:09 +00:00
d.amount = flt(d.rate) * flt(d.qty);
if(d.qty==0){
me.remove_item.push(d.idx)
}
}
});
2016-05-20 18:25:45 +00:00
if(field == 'qty'){
this.remove_zero_qty_item();
}
this.update_paid_amount_status(false)
2016-04-09 09:01:09 +00:00
},
remove_zero_qty_item: function(){
var me = this;
idx = 0
this.items = []
idx = 0
$.each(this.frm.doc["items"] || [], function(i, d) {
if(!in_list(me.remove_item, d.idx)){
d.idx = idx;
me.items.push(d);
2016-05-20 07:24:44 +00:00
idx++;
2016-04-09 09:01:09 +00:00
}
});
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
this.frm.doc["items"] = this.items;
},
make_discount_field: function(){
var me = this;
this.wrapper.find('input.discount-percentage').on("change", function() {
me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
total = me.frm.doc.grand_total
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
if(me.frm.doc.apply_discount_on == 'Net Total'){
total = me.frm.doc.net_total
}
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
me.refresh();
});
this.wrapper.find('input.discount-amount').on("change", function() {
me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
me.frm.doc.additional_discount_percentage = 0.0;
me.wrapper.find('input.discount-percentage').val(0);
me.refresh();
});
},
customer_validate: function(){
var me = this;
if(!this.frm.doc.customer){
frappe.throw(__("Please select customer"))
}
},
add_to_cart: function() {
var me = this;
var caught = false;
var no_of_items = me.wrapper.find(".pos-bill-item").length;
2016-07-15 21:32:20 +00:00
this.customer_validate();
this.mandatory_batch_no();
this.validate_serial_no();
2016-04-09 09:01:09 +00:00
this.validate_warehouse();
if (no_of_items != 0) {
$.each(this.frm.doc["items"] || [], function(i, d) {
if (d.item_code == me.items[0].item_code) {
caught = true;
d.qty += 1;
2016-07-15 21:32:20 +00:00
d.amount = flt(d.rate) * flt(d.qty);
if(me.item_serial_no[d.item_code]){
d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
d.warehouse = me.item_serial_no[d.item_code][1]
}
if(me.item_batch_no.length){
d.batch_no = me.item_batch_no[d.item_code]
2016-04-09 09:01:09 +00:00
}
}
});
}
// if item not found then add new item
if (!caught)
this.add_new_item_to_grid();
this.update_paid_amount_status(false)
2016-04-09 09:01:09 +00:00
},
add_new_item_to_grid: function() {
var me = this;
this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
this.child.item_code = this.items[0].item_code;
this.child.item_name = this.items[0].item_name;
this.child.stock_uom = this.items[0].stock_uom;
this.child.description = this.items[0].description;
this.child.qty = 1;
this.child.item_group = this.items[0].item_group;
2016-11-20 18:11:13 +00:00
this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
2016-07-15 21:32:20 +00:00
this.child.warehouse = (this.item_serial_no[this.child.item_code]
2016-11-20 18:11:13 +00:00
? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse) );
this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
this.child.actual_qty = me.get_actual_qty(this.items[0]);
2016-04-09 09:01:09 +00:00
this.child.amount = flt(this.child.qty) * flt(this.child.rate);
2016-07-15 21:32:20 +00:00
this.child.batch_no = this.item_batch_no[this.child.item_code];
this.child.serial_no = (this.item_serial_no[this.child.item_code]
? this.item_serial_no[this.child.item_code][0] : '');
2016-11-20 18:11:13 +00:00
this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
2016-04-09 09:01:09 +00:00
},
update_paid_amount_status: function(update_paid_amount){
if(this.name){
update_paid_amount = update_paid_amount ? false : true;
}
this.refresh(update_paid_amount);
},
2016-07-21 17:58:04 +00:00
refresh: function(update_paid_amount) {
2016-04-09 09:01:09 +00:00
var me = this;
2016-07-21 17:58:04 +00:00
this.refresh_fields(update_paid_amount);
2016-04-09 09:01:09 +00:00
this.update_qty();
2016-05-20 18:25:45 +00:00
this.update_rate();
2016-04-09 09:01:09 +00:00
this.set_primary_action();
},
2016-07-21 17:58:04 +00:00
refresh_fields: function(update_paid_amount) {
2016-04-09 09:01:09 +00:00
this.apply_pricing_rule();
this.discount_amount_applied = false;
this._calculate_taxes_and_totals();
this.calculate_discount_amount();
this.show_items_in_item_cart();
this.set_taxes();
2016-07-21 17:58:04 +00:00
this.calculate_outstanding_amount(update_paid_amount);
2016-04-09 09:01:09 +00:00
this.set_totals();
},
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
get_company_currency: function() {
return erpnext.get_currency(this.frm.doc.company);
},
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
show_item_wise_taxes: function(){
return null;
},
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
show_items_in_item_cart: function() {
var me = this;
var $items = this.wrapper.find(".items").empty();
$.each(this.frm.doc.items|| [], function(i, d) {
$(frappe.render_template("pos_bill_item", {
item_code: d.item_code,
item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
qty: d.qty,
2016-11-27 06:34:12 +00:00
actual_qty: me.actual_qty_dict[d.item_code] || 0,
2016-04-09 09:01:09 +00:00
projected_qty: d.projected_qty,
2016-05-20 18:25:45 +00:00
rate: format_number(d.rate, me.frm.doc.currency),
2016-04-09 09:01:09 +00:00
amount: format_currency(d.amount, me.frm.doc.currency)
})).appendTo($items);
});
this.wrapper.find("input.pos-item-qty").on("focus", function() {
$(this).select();
});
2016-05-20 18:25:45 +00:00
this.wrapper.find("input.pos-item-rate").on("focus", function() {
$(this).select();
});
2016-04-09 09:01:09 +00:00
},
set_taxes: function(){
var me = this;
me.frm.doc.total_taxes_and_charges = 0.0
var taxes = this.frm.doc.taxes || [];
$(this.wrapper)
.find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
.find(".tax-table").empty();
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
$.each(taxes, function(i, d) {
if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
$(frappe.render_template("pos_tax_row", {
description: d.description,
tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
me.frm.doc.currency)
})).appendTo(me.wrapper.find(".tax-table"));
}
});
},
set_totals: function() {
var me = this;
this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
},
set_primary_action: function() {
var me = this;
if (this.frm.doc.docstatus==0) {
2016-04-09 09:01:09 +00:00
this.page.set_primary_action(__("Pay"), function() {
me.validate();
me.update_paid_amount_status(true);
2016-04-09 09:01:09 +00:00
me.create_invoice();
me.make_payment();
2016-07-20 08:29:19 +00:00
}, "octicon octicon-credit-card");
}else if(this.frm.doc.docstatus == 1) {
2016-04-09 09:01:09 +00:00
this.page.set_primary_action(__("Print"), function() {
html = frappe.render(me.print_template, me.frm.doc)
2016-05-20 18:25:45 +00:00
me.print_document(html)
2016-04-09 09:01:09 +00:00
})
2016-05-11 09:34:57 +00:00
}else {
this.page.clear_primary_action()
2016-04-09 09:01:09 +00:00
}
this.page.set_secondary_action(__("New"), function() {
me.save_previous_entry();
me.create_new();
2016-07-20 08:29:19 +00:00
}, "octicon octicon-plus").addClass("btn-primary");
2016-04-09 09:01:09 +00:00
},
2016-05-20 07:24:44 +00:00
2016-09-11 20:07:46 +00:00
print_dialog: function(){
var me = this;
msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
style="margin-right: 5px;">{0}</a>\
<a class="btn btn-default new_doc">{1}</a>', [
__('Print'), __('New')
]));
$('.print_doc').click(function(){
html = frappe.render(me.print_template, me.frm.doc)
me.print_document(html)
})
$('.new_doc').click(function(){
msgprint.hide()
me.create_new();
})
},
2016-05-20 18:25:45 +00:00
print_document: function(html){
var w = window.open();
w.document.write(html);
w.document.close();
setTimeout(function(){
w.print();
w.close();
}, 1000)
},
2016-05-11 09:34:57 +00:00
submit_invoice: function(){
var me = this;
2016-09-11 20:07:46 +00:00
this.change_status();
if(this.frm.doc.docstatus == 1){
this.print_dialog()
}
2016-05-11 09:34:57 +00:00
},
2016-04-09 09:01:09 +00:00
change_status: function(){
if(this.frm.doc.docstatus == 0){
this.frm.doc.docstatus = 1;
this.update_invoice();
this.disable_input_field();
}
},
disable_input_field: function(){
var pointer_events = 'inherit'
$(this.wrapper).find('input').attr("disabled", false);
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
if(this.frm.doc.docstatus == 1){
2016-07-15 21:32:20 +00:00
pointer_events = 'none';
2016-05-20 07:24:44 +00:00
$(this.wrapper).find('input').attr("disabled", true);
2016-04-09 09:01:09 +00:00
}
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
$(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
$(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
this.set_primary_action();
},
create_invoice: function(){
var me = this;
var invoice_data = {}
this.si_docs = this.get_doc_from_localstorage();
if(this.name){
this.update_invoice()
}else{
this.name = $.now();
this.frm.doc.offline_pos_name = this.name;
2016-08-11 11:24:30 +00:00
this.frm.doc.posting_date = frappe.datetime.get_today();
this.frm.doc.posting_time = frappe.datetime.now_time();
2016-04-09 09:01:09 +00:00
invoice_data[this.name] = this.frm.doc
this.si_docs.push(invoice_data)
this.update_localstorage();
this.set_primary_action();
}
},
update_invoice: function(){
var me = this;
this.si_docs = this.get_doc_from_localstorage();
$.each(this.si_docs, function(index, data){
for(key in data){
if(key == me.name){
2016-07-15 21:32:20 +00:00
me.si_docs[index][key] = me.frm.doc;
2016-04-09 09:01:09 +00:00
me.update_localstorage();
}
}
})
},
update_localstorage: function(){
try{
localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
}catch(e){
frappe.throw(__("LocalStorage is full , did not save"))
}
},
get_doc_from_localstorage: function(){
2016-05-11 09:34:57 +00:00
try{
return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
}catch(e){
return []
}
2016-04-09 09:01:09 +00:00
},
set_interval_for_si_sync: function(){
var me = this;
setInterval(function(){
me.sync_sales_invoice()
2016-05-11 09:34:57 +00:00
}, 60000)
2016-04-09 09:01:09 +00:00
},
sync_sales_invoice: function(){
var me = this;
2016-07-15 21:32:20 +00:00
this.si_docs = this.get_submitted_invoice();
2016-04-09 09:01:09 +00:00
2016-05-20 18:25:45 +00:00
if(this.si_docs.length){
2016-04-09 09:01:09 +00:00
frappe.call({
method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
args: {
doc_list: me.si_docs
},
callback: function(r){
if(r.message){
me.removed_items = r.message;
me.remove_doc_from_localstorage();
}
}
})
}
},
get_submitted_invoice: function(){
2016-05-20 18:25:45 +00:00
var invoices = [];
var index = 1;
docs = this.get_doc_from_localstorage();
2016-04-09 09:01:09 +00:00
if(docs){
invoices = $.map(docs, function(data){
for(key in data){
2016-05-20 18:25:45 +00:00
if(data[key].docstatus == 1 && index < 50){
index++
2016-08-01 13:05:18 +00:00
data[key].docstatus = 0;
2016-04-09 09:01:09 +00:00
return data
}
}
});
}
return invoices
},
remove_doc_from_localstorage: function(){
var me = this;
this.si_docs = this.get_doc_from_localstorage();
2016-07-15 21:32:20 +00:00
this.new_si_docs = [];
2016-04-09 09:01:09 +00:00
if(this.removed_items){
$.each(this.si_docs, function(index, data){
for(key in data){
2016-05-11 09:34:57 +00:00
if(!in_list(me.removed_items, key)){
2016-07-15 21:32:20 +00:00
me.new_si_docs.push(data);
2016-04-09 09:01:09 +00:00
}
}
})
2016-05-11 09:34:57 +00:00
this.si_docs = this.new_si_docs;
2016-04-09 09:01:09 +00:00
this.update_localstorage();
}
},
validate: function(){
var me = this;
this.customer_validate();
this.item_validate();
2016-07-15 21:32:20 +00:00
this.validate_mode_of_payments();
2016-04-09 09:01:09 +00:00
},
item_validate: function(){
if(this.frm.doc.items.length == 0){
frappe.throw(__("Select items to save the invoice"))
}
},
validate_mode_of_payments: function(){
if (this.frm.doc.payments.length === 0){
frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
}
},
2016-04-09 09:01:09 +00:00
validate_serial_no: function(){
var me = this;
var item_code = serial_no = '';
for (key in this.item_serial_no){
item_code = key;
2016-07-15 21:32:20 +00:00
serial_no = me.item_serial_no[key][0];
2016-04-09 09:01:09 +00:00
}
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
if(item_code && serial_no){
$.each(this.frm.doc.items, function(index, data){
if(data.item_code == item_code){
if(in_list(data.serial_no.split('\n'), serial_no)){
frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
'serial_no': serial_no
})))
}
}
})
}
2016-07-15 21:32:20 +00:00
if(this.items[0].has_serial_no && serial_no == ""){
frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
'item': this.items[0].item_code
})))
}
},
validate_serial_no_qty: function(args, item_code, field, value){
var me = this;
if (args.item_code == item_code && args.serial_no
&& field == 'qty' && cint(value) != value) {
args.qty = 0.0;
this.refresh();
frappe.throw(__("Serial no item cannot be a fraction"))
}
if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
args.qty = 0.0;
args.serial_no = ''
this.refresh();
frappe.throw(__("Total nos of serial no is not equal to quantity."))
}
},
2016-07-15 21:32:20 +00:00
mandatory_batch_no: function(){
var me = this;
if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
'item': this.items[0].item_code
})))
}
2016-04-09 09:01:09 +00:00
},
apply_pricing_rule: function(){
var me = this;
$.each(this.frm.doc["items"], function(n, item) {
pricing_rule = me.get_pricing_rule(item)
me.validate_pricing_rule(pricing_rule)
if(pricing_rule.length){
item.margin_type = pricing_rule[0].margin_type;
item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
me.apply_pricing_rule_on_item(item)
2016-11-07 09:59:17 +00:00
} else if(item.discount_percentage > 0 || item.margin_rate_or_amount > 0) {
item.margin_rate_or_amount = 0.0;
item.discount_percentage = 0.0;
me.apply_pricing_rule_on_item(item)
2016-04-09 09:01:09 +00:00
}
})
},
get_pricing_rule: function(item){
var me = this;
return $.grep(this.pricing_rules, function(data){
if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
return me.validate_condition(data)
}else{
return true
}
2016-04-09 09:01:09 +00:00
}
}
})
},
validate_condition: function(data){
//This method check condition based on applicable for
condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
if(in_list(condition[1], condition[0])){
return true
}
},
get_mapper_for_pricing_rule: function(data){
return {
2016-07-08 09:47:04 +00:00
'Customer': [data.customer, [this.frm.doc.customer]],
'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
'Campaign': [data.campaign, [this.frm.doc.campaign]],
2016-04-09 09:01:09 +00:00
}
},
validate_pricing_rule: function(pricing_rule){
//This method validate duplicate pricing rule
var pricing_rule_name = '';
var priority = 0;
var pricing_rule_list = [];
var priority_list = []
if(pricing_rule.length > 1){
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
$.each(pricing_rule, function(index, data){
2016-05-20 07:24:44 +00:00
pricing_rule_name += data.name + ','
2016-04-09 09:01:09 +00:00
priority_list.push(data.priority)
if(priority <= data.priority){
priority = data.priority
pricing_rule_list.push(data)
}
})
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
count = 0
$.each(priority_list, function(index, value){
if(value == priority){
count++
}
})
if(priority == 0 || count > 1){
frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
'pricing_rule': pricing_rule_name
})))
}
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
return pricing_rule_list
}
},
2016-05-20 07:24:44 +00:00
2016-04-09 09:01:09 +00:00
validate_warehouse: function(){
if(this.items[0].is_stock_item && !this.items[0].default_warehouse && !this.pos_profile_data['warehouse']){
2016-10-24 09:55:33 +00:00
frappe.throw(__("Default warehouse is required for selected item"))
2016-04-09 09:01:09 +00:00
}
2016-11-20 18:11:13 +00:00
},
get_actual_qty: function(item) {
this.actual_qty = 0.0;
2016-11-27 06:34:12 +00:00
2016-11-20 18:11:13 +00:00
var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
if(warehouse && this.bin_data[item.item_code]) {
this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
2016-11-27 06:34:12 +00:00
this.actual_qty_dict[item.item_code] = this.actual_qty
2016-11-20 18:11:13 +00:00
}
return this.actual_qty
2016-04-09 09:01:09 +00:00
}
})