App.__cart = {
uuidInc: 0,
items: {},
uuid: function() {
var id = 'c-' + App.cart.uuidInc;
App.cart.uuidInc++;
return id;
},
add: function(item) {
var
id = App.cart.uuid(),
opt = App.cached['Dish'][item].options(),
options = [];
if (arguments[1]) {
options = arguments[1].options;
} else {
for (var x in opt) {
if (opt[x]['default'] == 1) {
options[options.length] = opt[x].id_option;
}
}
}
App.cart.items[id] = {
id: item,
options: options
};
var el = $('
');
el.append('
');
el.append('' + App.cache('Dish',item).name + ' ' + (App.cache('Dish',item).description != null ? App.cache('Dish',item).description : '') + '
');
if (App.cached['Dish'][item].options().length) {
el.append('');
}
el.hide();
// If it is a mobile add the items at the top #1035
if( $( window ).width() > 769 ){
$('.cart-items-content').append(el);
} else {
$('.cart-items-content').prepend(el);
}
//el.fadeIn();
el.show();
if( parseInt( App.cache( 'Dish', item ).expand_view ) > 0 ){
App.cart.customize( el );
}
App.cart.updateTotal();
App.track('Dish added', {
id_dish: App.cache('Dish',item).id_dish,
name: App.cache('Dish',item).name
});
},
clone: function(item) {
var
cartid = item.attr('data-cart_id'),
cart = App.cart.items[cartid],
newoptions = [];
for (var x in cart.options) {
newoptions[newoptions.length] = cart.options[x];
}
App.cart.add(cart.id, {
options: newoptions
});
App.track('Dish cloned');
},
remove: function(item) {
var
cart = item.attr('data-cart_id');
App.track('Dish removed');
delete App.cart.items[cart];
item.remove();
$('.cart-item-customize[data-id_cart_item="' + cart + '"]').remove();
App.cart.updateTotal();
},
/**
* Gets called after the cart is updarted to refresh the total
*
* @todo Gets called many times before the cart is updated, on load, and shouldn't
*
* @return void
*/
updateTotal: function() {
var
totalText = ( App.config.ab && App.config.ab.dollarSign == 'show' ? '$' : '' ) + this.charged(),
tipText = '',
feesText = '',
totalItems = 0,
credit = 0,
hasFees = ((App.restaurant.delivery_fee && App.order.delivery_type == 'delivery') || App.restaurant.fee_customer) ? true : false;
if( App.credit.restaurant[ App.restaurant.id ] ){
credit = parseFloat( App.credit.restaurant[ App.restaurant.id ] );
}
for (var x in App.cart.items) {
totalItems++;
}
App.cart.autotip();
/* If the user changed the delivery method to takeout and the payment is card
* the default tip will be 0%. If the delivery method is delivery and the payment is
* card the default tip will be autotip.
* If the user had changed the tip value the default value will be the chosen one.
*/
var wasTipChanged = false;
if( App.order.delivery_type == 'takeout' && App.order['pay_type'] == 'card' ){
if( typeof App.order.tipHasChanged == 'undefined' ){
App.order.tip = 0;
wasTipChanged = true;
}
} else if( App.order.delivery_type == 'delivery' && App.order['pay_type'] == 'card' ){
if( typeof App.order.tipHasChanged == 'undefined' ){
App.order.tip = ( App.config.user.last_tip ) ? App.config.user.last_tip : 'autotip';
App.order.tip = App.lastTipNormalize( App.order.tip );
wasTipChanged = true;
}
}
if( wasTipChanged ){
$('[name="pay-tip"]').val( App.order.tip );
// Forces the recalculation of total because the tip was changed.
totalText = ( App.config.ab && App.config.ab.dollarSign == 'show' ? '$' : '' ) + this.charged();
}
if (App.restaurant.meetDeliveryMin() && App.order.delivery_type == 'delivery') {
$('.delivery-minimum-error').show();
$('.delivery-min-diff').html(App.restaurant.deliveryDiff());
} else {
$('.delivery-minimum-error').hide();
}
$('.cart-summary-item-count span').html(totalItems);
/* If no items, hide payment line
* .payment-total line for new customers
* .dp-display-payment is for stored customers
*/
if (!this.subtotal()) {
$('.payment-total, .dp-display-payment').hide();
} else {
$('.payment-total, .dp-display-payment').show();
}
var breakdown = App.cart.totalbreakdown();
var extraCharges = App.cart.extraChargesText(breakdown);
if (extraCharges) {
$('.cart-breakdownDescription').html( ( App.config.ab && App.config.ab.dollarSign == 'show' ? '$' : '' ) + this.subtotal().toFixed(2) + ' (+'+ extraCharges +')' );
} else {
$('.cart-breakdownDescription').html( ( App.config.ab && App.config.ab.dollarSign == 'show' ? '$' : '' ) + this.subtotal().toFixed(2));
}
if( App.order.pay_type == 'card' && credit > 0 ){
var creditLeft = '';
if( this.total() < credit ){
var creditLeft = ' - You\'ll still have ' + ( App.config.ab && App.config.ab.dollarSign == 'show' ? '$' : '' ) + App.ceil( ( credit - this.total() ) ).toFixed( 2 ) + ' gift card left ';
credit = this.total();
}
$('.cart-gift').html( ' (- ' + ( App.config.ab && App.config.ab.dollarSign == 'show' ? '$' : '' ) + App.ceil( credit ).toFixed( 2 ) + ' credit ' + creditLeft + ') ' );
} else {
$('.cart-gift').html( '' );
}
setTimeout( function(){
if( App.order.pay_type == 'cash' && credit > 0 /* && App.giftcard.showGiftCardCashMessage */ ){
$( '.cart-giftcard-message' ).html( 'Pay with a card, NOT CASH, to use your ' + ( App.config.ab && App.config.ab.dollarSign == 'show' ? '$' : '' ) + App.ceil( credit ).toFixed( 2 ) + ' gift card!' );
} else {
$( '.cart-giftcard-message' ).html( '' );
}
}, 1000 );
$('.cart-total').html( totalText );
/**
* Crunchbutton doesnt collect the cash, the restaurant will ring up
* the order in their register, which may have different prices. The
* restaurant collects the cash, so its posible things may be
* different. This differs from when its card, crunchbutton collects
* the money directly so the price cant vary
*/
if (App.order['pay_type'] == 'card') {
$('.cash-order-aprox').html('');
$('.cart-paymentType').html('by card');
} else {
$('.cash-order-aprox').html('approximately');
$('.cart-paymentType').html('');
}
if ($('.cart-total').html() == totalText) {
//return;
}
if (!totalItems) {
$('.default-order-check').hide();
} else {
$('.default-order-check').show();
}
var
totalItems = {},
name,
text = '';
$('.cart-summary-items').html('');
for (var x in App.cart.items) {
name = App.cached['Dish'][App.cart.items[x].id].name;
if (totalItems[name]) {
totalItems[name]++;
} else {
totalItems[name] = 1;
}
}
for (x in totalItems) {
text = ', ' + text;
if (totalItems[x] > 1) {
text = x + ' (' + totalItems[x] + ')' + text;
} else {
text = x + text;
}
}
$('.cart-summary-items').html(text.substr(0,text.length-13));
$('.cart-item-customize-price').each(function() {
var dish = $(this).closest('.cart-item-customize').attr('data-id_cart_item'),
option = $(this).closest('.cart-item-customize-item').attr('data-id_option'),
cartitem = App.cart.items[dish],
opt = App.cached['Option'][option],
price = opt.optionPrice(cartitem.options);
$(this).html(App.cart.customizeItemPrice(price));
});
},
customizeItemPrice: function(price, force) {
var priceText = '';
if( price != '0.00' || force ){
priceText = ' (';
priceText += ( price < 0 ) ? 'minus $' : '+ $';
priceText += parseFloat( Math.abs( price ) ).toFixed(2);
priceText += ')';
}
return priceText;
},
customize: function(item) {
var
cart = item.attr('data-cart_id'),
old = $('.cart-item-customize[data-id_cart_item="' + cart + '"]');
if (old.length) {
old.remove();
} else {
var
el = $('').insertAfter(item),
cartitem = App.cart.items[cart],
obj = App.cached['Dish'][cartitem.id],
opt = obj.options();
// First the basic options
for (var x in opt) {
if (opt[x].id_option_parent) {
continue;
}
if (opt[x].type == 'check') {
var price = opt[x].optionPrice(cartitem.options);
var check = $('');
if ($.inArray(opt[x].id_option, cartitem.options) !== -1) {
check.attr('checked','checked');
}
var option = $('')
.append(check)
.append(''
);
el.append(option);
}
}
// Second the customizable options
for (var x in opt) {
if (opt[x].id_option_parent) {
continue;
}
if (opt[x].type == 'select') {
var select = $('