fitness-web/frontend/web/js/product.sell.js

413 lines
11 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: '',
/**mark list paid url*/
clear_list_url: '',
/**the id of form*/
selector_form: '#product_form',
/**form contains error text*/
form_invalid: 'Az ürlap hibákat tartalmaz',
message_paid: 'Tételek fizetve',
/**list of sold items by current user*/
sold_items: [],
discounts: [],
};
init();
function init(){
$.extend(app.defaults, o );
app.keyDate = null;
app.keySeq = '';
app.enterPressed = 0;
addBehaviourBtnSell();
addBehaviourBtnSellAndAppendToBill();
addBehaviorAjaxSubmit();
setFocus();
addEnterPressedBehaviours();
addBehaviorCountChangedListener();
addBehaviourDisocuntChanged();
createUserSoldItemsTable();
addBehaviourBtnPaid();
productChanged();
addDocumentKeypressedListener();
}
function addBehaviourDisocuntChanged(){
$("#productsaleform-id_discount").change(refreshCalculatedValues);
}
function addDocumentKeypressedListener(){
$( document ).keypress(function( event ) {
var tag = event.target.tagName.toLowerCase();
if ( tag != 'input' && tag != 'textarea') {
resetSequenceIfToMuchTimePassedOrEnterWasPressed(event);
if ( event.which == 13 ) {
app.enterPressed = 1;
$( document ).trigger( "wordtyped", [ { originalEvent: event, 'word': app.seq } ] );
}else {
appendCharToSeq(event);
}
}
});
$( document ).on( "wordtyped", function( event, data ) {
var word;
word = data.word;
if ( word.length > 0){
if ( word.length >= 6 && word.length <= 8 ){
//lookupuser
}else{
$("#filter_text").val(data.word);
_lookupProduct(data.word);
}
}
});
}
function resetSequenceIfToMuchTimePassedOrEnterWasPressed(){
var prevDate, timePassed;
prevDate= app.keyDate;
app.keyDate = new Date().getTime();
timePassed = app.keyDate - prevDate;
if ( app.enterPressed > 0 || timePassed > 2000 ){
app.seq = '';
app.enterPressed = 0;
}
}
function appendCharToSeq(event){
var code,s;
code = event.which;
if ( code ){
s = String.fromCharCode(code);
app.seq += s;
}
}
function addEnterPressedBehaviours(){
addBehaviorEnterPressedListener();
addBehaviorCountEnterPressedListener();
addBehaviorCurrencyEnterPressedListener();
addBehaviorAccountEnterPressedListener();
addBehaviorDiscountEnterPressedListener();
}
/**
* 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(){
_lookupProduct($( app.defaults.selector_filter_text ).val());
}
function _lookupProduct(s){
var data, url;
url = app.defaults.lookup_product_url;
data = {
'query' : s,
};
$.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('-');
$('#productsaleform-id_product').val('');
$('#productsaleform-id_account').val('');
$("#productsaleform-count").val(1);
}
function applyProduct(product){
var table;
table = $('.table-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);
$('#productsaleform-id_product').val(product.id_product);
$('#productsaleform-id_account').val(product.id_account);
}
function refreshCalculatedValues(){
var count,price,id_discount,discount;
var table;
table = $('.table-product');
count = $("#productsaleform-count").val();
id_discount = $("#productsaleform-id_discount").val();
app.discount = findDiscount( id_discount );
count = +count;
if ( isNaN(count)){
count = 0;
}
price = count * app.product.sale_price;
discount = calcDiscount(price, app.discount);
price = deductDiscount(price,discount);
price = normalizePrice(price);
table.find('.product-count').html(count);
table.find('.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('afterValidate', '#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' : 'warning' });
}
});
$('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) {
if ( response.code == 'success'){
$.notify(response.message, { 'type' : 'success' });
clearForm();
refreshCalculatedValues();
$("#filter_text").val('');
setFocus();
app.defaults.sold_items = response.transfers;
refreshSoldItemsList();
}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 refreshSoldItemsList(){
$('.sold-items-container').transferList( 'option', 'transfers' , app.defaults.sold_items );
}
function addBehaviourBtnPaid( ){
$('#btn_paid').on('click',function(){
$.ajax({
url: app.defaults.clear_list_url,
type: 'post',
data: {},
success: function (response) {
if ( response.code == 'success'){
app.defaults.sold_items= response.transfers;
refreshSoldItemsList();
$.notify(app.defaults.message_paid, { 'type' : 'success' });
}
}
}
);
});
}
function createUserSoldItemsTable(){
$('.sold-items-container').transferList({
'transfers' : app.defaults.sold_items
});
}
function findDiscount(id_discount){
var discount;
discount = null;
$.each( app.defaults.discounts , function(e,i){
if ( e.id_discount == id_discount ){
discount = e;
}
});
return discount;
}
function calcDiscount(price,discount){
var result, discountMoney;
result = 0;
if ( discount ){
result = price * ( discount.value / 100 );
}
return result;
}
function deductDiscount( price , discount ){
var result;
result = price - discount;
return result;
}
function normalizePrice( price ){
var result;
result = hufRound(price);
return result;
}
function hufRound(x)
{
if (x > 0) {
return (x % 5) >= 2.5 ? parseInt(x / 5) * 5 + 5 : parseInt(x / 5) * 5;
} else {
return ((Math.abs(x) % 5) >= 2.5 ? parseInt(Math.abs(x) / 5) * 5 + 5 : parseInt(Math.abs(x) / 5) * 5)*-1;
}
}
}