fitness-web/frontend/web/js/product.sell.js
2015-10-05 07:37:17 +02:00

210 lines
5.3 KiB
JavaScript

function ProductSell(o){
var app = this;
var product = null;
this.defaults = {
selector_filter_text: '#filter_text',
lookup_product_url: '' ,
selector_form: '#product_form',
form_invalid: 'Az ürlap hibákat tartalmaz'
};
init();
function init(){
$.extend(app.defaults, o );
addBehaviorEnterPressedListener();
addBehaviourBtnSell();
addBehaviourBtnSellAndAppendToBill();
addBehaviorAjaxSubmit();
setFocus();
addBehaviorCountEnterPressedListener();
addBehaviorCountChangedListener();
addBehaviorCurrencyEnterPressedListener();
addBehaviorAccountEnterPressedListener();
addBehaviorDiscountEnterPressedListener();
productChanged();
}
function setFocus(){
$("#filter_text").focus();
}
function addBehaviorEnterPressedListener(){
$( app.defaults.selector_filter_text ).keypress(function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
lookupProduct();
}
});
}
function addBehaviorCountEnterPressedListener(){
$( '#productsaleform-count' ).keypress(function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
event.stopImmediatePropagation();
$('#productsaleform-id_currency').focus();
}
});
}
function addBehaviorCurrencyEnterPressedListener(){
$( '#productsaleform-id_currency' ).keypress(function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
event.stopImmediatePropagation();
$('#productsaleform-id_account').focus();
}
});
}
function addBehaviorAccountEnterPressedListener(){
$( '#productsaleform-id_account' ).keypress(function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
event.stopImmediatePropagation();
$('#productsaleform-id_discount').focus();
}
});
}
function addBehaviorDiscountEnterPressedListener(){
$( '#productsaleform-id_discount' ).keypress(function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
event.stopImmediatePropagation();
$('#productsaleform-comment').focus();
}
});
}
function addBehaviorCountChangedListener(){
$( '#productsaleform-count' ).change(function( event ) {
refreshCalculatedValues();
});
}
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
});
}
function onLookupProductReady( data ){
app.product = data.product;
productChanged();
}
function productChanged( ){
clearForm();
if ( app.product != null){
applyProduct( app.product);
refreshCalculatedValues();
$("#productsaleform-count").focus().select();
}
}
function clearForm(){
$('.product-name').html('-');
$('.product-price').html('-');
$('.product-number').html('-');
$('.product-barcode').html('-');
$('.product-stock').html('-');
$('.product-price').html('-');
$('.product-sale-price').html('-');
$("#productsaleform-count").val(1);
$('#productsaleform-id_product').val('');
$('#productsaleform-id_account').val('');
}
function applyProduct(product){
$('#productsaleform-id_product').val(product.id_product);
$('.product-name').html(product.name);
$('.product-sale-price').html(product.sale_price);
$('.product-number').html(product.product_number);
$('.product-barcode').html(product.barcode);
$('.product-stock').html(product.stock);
$('#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',submitSell);
}
function submitSell(){
$('#product_form').submit();
}
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();
}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;
});
}
}