<commit_before>/**
 * FoodCoopShop - The open source software for your foodcoop
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @since         FoodCoopShop 1.0.0
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 * @author        <NAME> <<EMAIL>>
 * @copyright     Copyright (c) <NAME>, https://www.rothauer-it.com
 * @link          https://www.foodcoopshop.com
 */
foodcoopshop.Cart = {

    orderButtons: '.cart .btn-success.btn-order, .responsive-cart',
    
    cartButtonIcon : '',
    
    getPickupDayHeaderSelector : function(pickupDay) {
        return '.cart p.pickup-day-header:contains("' + pickupDay + '")';
    },
    
    addOrAppendProductToPickupDay : function(productId, amount, price, productName, unity, manufacturerLink, image, deposit, tax, timebasedCurrencyHours, orderedQuantityInUnits, unitName, unitAmount, priceInclPerUnit, pickupDay) {
        var pickupDayHeader = $(this.getPickupDayHeaderSelector(pickupDay));
        if (pickupDayHeader.length == 0) {
            $('.cart p.products').append('<p class="pickup-day-header">' + foodcoopshop.LocalizedJs.cart.PickupDay + ': <b>' + pickupDay + '</b></p>');
            pickupDayHeader = $(this.getPickupDayHeaderSelector(pickupDay)); // re-init after append
        }
        pickupDayHeader.append(
            foodcoopshop.Cart.getCartProductHtml(productId, amount, price, productName, unity, manufacturerLink, image, deposit, tax, timebasedCurrencyHours, orderedQuantityInUnits, unitName, unitAmount, priceInclPerUnit, pickupDay)
        );
    },
    
    setCartButtonIcon : function(cartButtonIcon) {
        this.cartButtonIcon = cartButtonIcon;
    },

    /**
     * cart products already existed in database
     */
    initCartProducts: function (cartProducts) {

        cartProducts = $.parseJSON(cartProducts).reverse();
        if (cartProducts.length == 0) {
            return;
        }

        $('.cart p.no-products').hide();
        var sum = 0;
        var depositSum = 0;
        var taxSum = 0;
        var timebasedCurrencyHoursSum = 0;
        
        for (var i = 0; i < cartProducts.length; i++) {
            var cp = cartProducts[i];
            var timebasedCurrencyHours = parseFloat(cp.timebasedCurrencySeconds / 3600);
            this.addOrAppendProductToPickupDay(cp.productId, cp.amount, cp.price, cp.productName, cp.unity_with_unit, cp.manufacturerLink, cp.image, cp.deposit, cp.tax, timebasedCurrencyHours, cp.orderedQuantityInUnits, cp.unitName, cp.unitAmount, cp.priceInclPerUnit, cp.nextDeliveryDay);
            sum += cp.price;
            depositSum += cp.deposit;
            taxSum += cp.tax;
        }
        this.updateCartSum(sum);
        this.updateCartDepositSum(depositSum);
        this.updateCartTaxSum(taxSum);
        this.updateCartTimebasedCurrencySum(timebasedCurrencyHoursSum);

        foodcoopshop.Cart.initRemoveFromCartLinks(); // bind click event
        $('.cart p.products').show();

        foodcoopshop.AppFeatherlight.initLightboxForImages('.cart .products a.image');
        foodcoopshop.Helper.onWindowScroll();

    },

    initCartFinish: function () {
        $('button.btn-order').on('click', function () {
            foodcoopshop.Helper.disableButton($(this));
            foodcoopshop.Helper.addSpinnerToButton($(this), 'fa-check');
            $(this).closest('form').submit();
        });
    },

    initCartErrors: function (cartErrors) {
        cartErrors = $.parseJSON(cartErrors);
        for (var key in cartErrors) {
            var productContainer = $('.carts.detail .cart:not(#cart) .product.' + key);
            productContainer.addClass('error');
            productContainer.after('<ul class="error-message ' + key + '"><li>' + cartErrors[key].join('</li><li>') + '</li></ul>');
        }
    },

    updateExistingProduct: function (productContainer, amount, price, deposit, tax, timebasedCurrencyHours, orderedQuantityInUnits, unitName, unitAmount, priceInclPerUnit) {

        // update amount
        var oldAmount = productContainer.find('span.amount span.value');
        var oldAmountValue = parseInt(oldAmount.html());
        var newAmount = oldAmountValue + parseInt(amount);
        oldAmount.html(newAmount);
        foodcoopshop.Helper.applyBlinkEffect(oldAmount);

        // update unity
        var oldUnity = productContainer.find('span.unity');
        var newUnityHtml = oldUnity.html();

        if (newAmount > 1 && oldAmountValue == 1) {
            var approxRegExp = new RegExp(foodcoopshop.LocalizedJs.cart.approx);
            newUnityHtml = newUnityHtml.replace(approxRegExp, foodcoopshop.LocalizedJs.cart.forEach + ' ' + foodcoopshop.LocalizedJs.cart.approx);
        }
        if (newAmount == 1 && oldAmountValue > 1) {
            var forEachApproxRegExp = new RegExp(foodcoopshop.LocalizedJs.cart.forEach + ' ' + foodcoopshop.LocalizedJs.cart.approx);
            newUnityHtml = newUnityHtml.replace(forEachApproxRegExp, foodcoopshop.LocalizedJs.cart.approx);
        }
        
        if (orderedQuantityInUnits > 0) {
            newUnityHtml = foodcoopshop.Helper.getStringAsFloat(oldUnity.html()) + (orderedQuantityInUnits * amount);
            newUnityHtml = newUnityHtml.toLocaleString();
            newUnityHtml += ' ' + unitName;
        }
        
        if (newUnityHtml != oldUnity.html()) {
            oldUnity.html(newUnityHtml);<commit_msg>fix: orderedQuantity was already multiplied<commit_after>/**
 * FoodCoopShop - The open source software for your foodcoop
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @since         FoodCoopShop 1.0.0
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 * @author        <NAME> <<EMAIL>>
 * @copyright     Copyright (c) <NAME>, https://www.rothauer-it.com
 * @link          https://www.foodcoopshop.com
 */
foodcoopshop.Cart = {

    orderButtons: '.cart .btn-success.btn-order, .responsive-cart',
    
    cartButtonIcon : '',
    
    getPickupDayHeaderSelector : function(pickupDay) {
        return '.cart p.pickup-day-header:contains("' + pickupDay + '")';
    },
    
    addOrAppendProductToPickupDay : function(productId, amount, price, productName, unity, manufacturerLink, image, deposit, tax, timebasedCurrencyHours, orderedQuantityInUnits, unitName, unitAmount, priceInclPerUnit, pickupDay) {
        var pickupDayHeader = $(this.getPickupDayHeaderSelector(pickupDay));
        if (pickupDayHeader.length == 0) {
            $('.cart p.products').append('<p class="pickup-day-header">' + foodcoopshop.LocalizedJs.cart.PickupDay + ': <b>' + pickupDay + '</b></p>');
            pickupDayHeader = $(this.getPickupDayHeaderSelector(pickupDay)); // re-init after append
        }
        pickupDayHeader.append(
            foodcoopshop.Cart.getCartProductHtml(productId, amount, price, productName, unity, manufacturerLink, image, deposit, tax, timebasedCurrencyHours, orderedQuantityInUnits, unitName, unitAmount, priceInclPerUnit, pickupDay)
        );
    },
    
    setCartButtonIcon : function(cartButtonIcon) {
        this.cartButtonIcon = cartButtonIcon;
    },

    /**
     * cart products already existed in database
     */
    initCartProducts: function (cartProducts) {

        cartProducts = $.parseJSON(cartProducts).reverse();
        if (cartProducts.length == 0) {
            return;
        }

        $('.cart p.no-products').hide();
        var sum = 0;
        var depositSum = 0;
        var taxSum = 0;
        var timebasedCurrencyHoursSum = 0;
        
        for (var i = 0; i < cartProducts.length; i++) {
            var cp = cartProducts[i];
            var timebasedCurrencyHours = parseFloat(cp.timebasedCurrencySeconds / 3600);
            this.addOrAppendProductToPickupDay(cp.productId, cp.amount, cp.price, cp.productName, cp.unity_with_unit, cp.manufacturerLink, cp.image, cp.deposit, cp.tax, timebasedCurrencyHours, cp.orderedQuantityInUnits, cp.unitName, cp.unitAmount, cp.priceInclPerUnit, cp.nextDeliveryDay);
            sum += cp.price;
            depositSum += cp.deposit;
            taxSum += cp.tax;
        }
        this.updateCartSum(sum);
        this.updateCartDepositSum(depositSum);
        this.updateCartTaxSum(taxSum);
        this.updateCartTimebasedCurrencySum(timebasedCurrencyHoursSum);

        foodcoopshop.Cart.initRemoveFromCartLinks(); // bind click event
        $('.cart p.products').show();

        foodcoopshop.AppFeatherlight.initLightboxForImages('.cart .products a.image');
        foodcoopshop.Helper.onWindowScroll();

    },

    initCartFinish: function () {
        $('button.btn-order').on('click', function () {
            foodcoopshop.Helper.disableButton($(this));
            foodcoopshop.Helper.addSpinnerToButton($(this), 'fa-check');
            $(this).closest('form').submit();
        });
    },

    initCartErrors: function (cartErrors) {
        cartErrors = $.parseJSON(cartErrors);
        for (var key in cartErrors) {
            var productContainer = $('.carts.detail .cart:not(#cart) .product.' + key);
            productContainer.addClass('error');
            productContainer.after('<ul class="error-message ' + key + '"><li>' + cartErrors[key].join('</li><li>') + '</li></ul>');
        }
    },

    updateExistingProduct: function (productContainer, amount, price, deposit, tax, timebasedCurrencyHours, orderedQuantityInUnits, unitName, unitAmount, priceInclPerUnit) {

        // update amount
        var oldAmount = productContainer.find('span.amount span.value');
        var oldAmountValue = parseInt(oldAmount.html());
        var newAmount = oldAmountValue + parseInt(amount);
        oldAmount.html(newAmount);
        foodcoopshop.Helper.applyBlinkEffect(oldAmount);

        // update unity
        var oldUnity = productContainer.find('span.unity');
        var newUnityHtml = oldUnity.html();

        if (newAmount > 1 && oldAmountValue == 1) {
            var approxRegExp = new RegExp(foodcoopshop.LocalizedJs.cart.approx);
            newUnityHtml = newUnityHtml.replace(approxRegExp, foodcoopshop.LocalizedJs.cart.forEach + ' ' + foodcoopshop.LocalizedJs.cart.approx);
        }
        if (newAmount == 1 && oldAmountValue > 1) {
            var forEachApproxRegExp = new RegExp(foodcoopshop.LocalizedJs.cart.forEach + ' ' + foodcoopshop.LocalizedJs.cart.approx);
            newUnityHtml = newUnityHtml.replace(forEachApproxRegExp, foodcoopshop.LocalizedJs.cart.approx);
        }
        
        if (orderedQuantityInUnits > 0) {
            newUnityHtml = foodcoopshop.Helper.getStringAsFloat(oldUnity.html()) + orderedQuantityInUnits;
            newUnityHtml = newUnityHtml.toLocaleString();
            newUnityHtml += ' ' + unitName;
        }
        
        if (newUnityHtml != oldUnity.html()) {
            oldUnity.html(newUnityHtml);
{'fraction_non_alphanumeric': 0.12273587456069208, 'fraction_numerical': 0.003424348923132378, 'mean_word_length': 2.8736474694589877, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5352164', 'n_tokens_mistral': 3306, 'n_tokens_neox': 3183, 'n_words': 604}