253 lines
7.0 KiB
JavaScript
253 lines
7.0 KiB
JavaScript
function ProductSell(o){
|
|
|
|
/**reference for the instance*/
|
|
var app = this;
|
|
/**currently loaded product*/
|
|
var product = null;
|
|
|
|
this.defaults = {
|
|
/**id of filter text*/
|
|
selector_filter_text: '#filter_text',
|
|
/** ajax url for lookup service*/
|
|
lookup_product_url: '',
|
|
/**the id of form*/
|
|
selector_form: '#product_form',
|
|
/**form contains error text*/
|
|
form_invalid: 'Az ürlap hibákat tartalmaz',
|
|
/**list of sold items by current user*/
|
|
sold_items: [],
|
|
};
|
|
|
|
init();
|
|
|
|
function init(){
|
|
$.extend(app.defaults, o );
|
|
addBehaviorEnterPressedListener();
|
|
addBehaviourBtnSell();
|
|
addBehaviourBtnSellAndAppendToBill();
|
|
addBehaviorAjaxSubmit();
|
|
setFocus();
|
|
addBehaviorCountEnterPressedListener();
|
|
addBehaviorCountChangedListener();
|
|
addBehaviorCurrencyEnterPressedListener();
|
|
addBehaviorAccountEnterPressedListener();
|
|
addBehaviorDiscountEnterPressedListener();
|
|
productChanged();
|
|
createUserSoldItemsTable();
|
|
}
|
|
|
|
/**
|
|
* set the focus for text input #filter_text
|
|
* */
|
|
function setFocus(){
|
|
$("#filter_text").focus();
|
|
|
|
}
|
|
|
|
/**listen for enter key pressed on #filter_text*/
|
|
function addBehaviorEnterPressedListener(){
|
|
$( app.defaults.selector_filter_text ).keypress(function( event ) {
|
|
if ( event.which == 13 ) {
|
|
event.preventDefault();
|
|
lookupProduct();
|
|
}
|
|
});
|
|
}
|
|
|
|
/**listen for enter key pressed on #productsaleform-count*/
|
|
function addBehaviorCountEnterPressedListener(){
|
|
$( '#productsaleform-count' ).keypress(function( event ) {
|
|
if ( event.which == 13 ) {
|
|
event.preventDefault();
|
|
event.stopImmediatePropagation();
|
|
$('#productsaleform-id_currency').focus();
|
|
}
|
|
});
|
|
}
|
|
/**listen for enter key pressed on #productsaleform-id_currency*/
|
|
function addBehaviorCurrencyEnterPressedListener(){
|
|
$( '#productsaleform-id_currency' ).keypress(function( event ) {
|
|
if ( event.which == 13 ) {
|
|
event.preventDefault();
|
|
event.stopImmediatePropagation();
|
|
$('#productsaleform-id_account').focus();
|
|
}
|
|
});
|
|
}
|
|
/**listen for enter key pressed on #productsaleform-id_account*/
|
|
function addBehaviorAccountEnterPressedListener(){
|
|
$( '#productsaleform-id_account' ).keypress(function( event ) {
|
|
if ( event.which == 13 ) {
|
|
event.preventDefault();
|
|
event.stopImmediatePropagation();
|
|
$('#productsaleform-id_discount').focus();
|
|
}
|
|
});
|
|
}
|
|
/**listen for enter key pressed on #productsaleform-id_discount*/
|
|
function addBehaviorDiscountEnterPressedListener(){
|
|
$( '#productsaleform-id_discount' ).keypress(function( event ) {
|
|
if ( event.which == 13 ) {
|
|
event.preventDefault();
|
|
event.stopImmediatePropagation();
|
|
$('#productsaleform-comment').focus();
|
|
}
|
|
});
|
|
}
|
|
/**listen for event "change" #productsaleform-count*/
|
|
function addBehaviorCountChangedListener(){
|
|
$( '#productsaleform-count' ).change(function( event ) {
|
|
refreshCalculatedValues();
|
|
|
|
});
|
|
}
|
|
|
|
/**load a product from server by exact barcode or product number*/
|
|
function lookupProduct(){
|
|
var data, url;
|
|
|
|
url = app.defaults.lookup_product_url;
|
|
data = {
|
|
'query' : $( app.defaults.selector_filter_text ).val()
|
|
};
|
|
|
|
$.ajax({
|
|
dataType: "json",
|
|
url: url,
|
|
data: data,
|
|
success: onLookupProductReady
|
|
});
|
|
}
|
|
|
|
/**succes event handler after lookup product event*/
|
|
function onLookupProductReady( data ){
|
|
app.product = data.product;
|
|
productChanged();
|
|
}
|
|
|
|
/**handles product change.*/
|
|
function productChanged( ){
|
|
clearForm();
|
|
if ( app.product != null){
|
|
applyProduct( app.product);
|
|
refreshCalculatedValues();
|
|
$("#productsaleform-count").focus().select();
|
|
}
|
|
}
|
|
|
|
function clearForm(){
|
|
var table;
|
|
table = $('.table-product');
|
|
table.find('.product-name').html('-');
|
|
table.find('.product-price').html('-');
|
|
table.find('.product-number').html('-');
|
|
table.find('.product-barcode').html('-');
|
|
table.find('.product-stock').html('-');
|
|
table.find('.product-price').html('-');
|
|
table.find('.product-sale-price').html('-');
|
|
table.find("#productsaleform-count").val(1);
|
|
table.find('#productsaleform-id_product').val('');
|
|
table.find('#productsaleform-id_account').val('');
|
|
}
|
|
|
|
function applyProduct(product){
|
|
var table;
|
|
table = $('.table-product');
|
|
table.find('#productsaleform-id_product').val(product.id_product);
|
|
table.find('.product-name').html(product.name);
|
|
table.find('.product-sale-price').html(product.sale_price);
|
|
table.find('.product-number').html(product.product_number);
|
|
table.find('.product-barcode').html(product.barcode);
|
|
table.find('.product-stock').html(product.stock);
|
|
table.find('#productsaleform-id_account').val(product.id_account);
|
|
|
|
}
|
|
|
|
function refreshCalculatedValues(){
|
|
var count,price;
|
|
count = $("#productsaleform-count").val();
|
|
count = +count;
|
|
if ( isNaN(count)){
|
|
count = 0;
|
|
}
|
|
price = count * app.product.sale_price;
|
|
$('.product-count').html(count);
|
|
$('.product-price').html(price);
|
|
}
|
|
|
|
function addBehaviourBtnSell(){
|
|
$('#btn_sell').on('click',submitSell);
|
|
}
|
|
|
|
function addBehaviourBtnSellAndAppendToBill(){
|
|
$('#btn_sell_append').on('click',submitSellAndAppend);
|
|
}
|
|
|
|
function submitSell(){
|
|
$('#productsaleform-append_to_sold_list').val('');
|
|
$('#product_form').submit();
|
|
}
|
|
function submitSellAndAppend(){
|
|
$('#productsaleform-append_to_sold_list').val('append');
|
|
$('#product_form').submit();
|
|
}
|
|
|
|
|
|
function refreshSoldUseritems(){
|
|
$('.sold-items-container').transferList('option','transfers' , app.defaults.sold_items );
|
|
}
|
|
|
|
function addBehaviorAjaxSubmit(){
|
|
|
|
$('body').on('beforeSubmit', '#product_form', function () {
|
|
var form = $(this);
|
|
// return false if form still have some validation errors
|
|
if (form.find('.has-error').length) {
|
|
$.notify(app.defaults.form_invalid, { 'type' : 'success' });
|
|
return false;
|
|
}
|
|
// submit form
|
|
$.ajax({
|
|
url: form.attr('action'),
|
|
type: 'post',
|
|
data: form.serialize(),
|
|
success: function (response) {
|
|
// do something with response
|
|
if ( response.code == 'success'){
|
|
$.notify(response.message, { 'type' : 'success' });
|
|
clearForm();
|
|
refreshCalculatedValues();
|
|
$("#filter_text").val('');
|
|
setFocus();
|
|
app.defaults.sold_items = response.transfers;
|
|
$('.sold-items-container').transferList( 'option', 'transfers' , app.defaults.sold_items );
|
|
|
|
}else if ( response.code == 'invalid'){
|
|
if ( response.errors ){
|
|
$.each(response.errors, function (key, value){
|
|
var message;
|
|
message = $.map(value, function(obj){ return obj }).join(' ');
|
|
$.notify(message, { 'type' : 'danger' });
|
|
}
|
|
);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
return false;
|
|
});
|
|
|
|
}
|
|
|
|
|
|
function createUserSoldItemsTable(){
|
|
console.info( app.defaults.sold_items );
|
|
$('.sold-items-container').transferList({
|
|
'transfers' : app.defaults.sold_items
|
|
});
|
|
}
|
|
|
|
|
|
|
|
}
|