HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/debierbaron.komma.pro/wwwroot/js/main.ng.js
var app = angular.module('bierbaron', ['ngSanitize', 'filters', 'ui.date']);


app.controller('checkoutController', ['$scope', '$http', function ($scope, $http, $filter) {

    /**
     * Change the value of a cart item
     *
     * @param quantity
     * @param productId
     */
    $scope.changeValue = function (quantity, productId) {
        //Change the value of the angular cart
        $scope.cart.products[productId].quantity = quantity;
        //Change the value on the php cart with an ajax request
        $scope.updateProductAjax(productId, quantity)
    }

    /**
     * Delete a product from the cart
     *
     * @param productId
     */
    $scope.deleteProduct = function (productId) {
        //Delete the value from the angular cart
        delete $scope.cart.products[productId];
        //Delete the product from the php cart with an ajax request
        $scope.deleteProductAjax(productId)
    }

    /**
     * This method calculates the total productPrice
     * of all the products in the angular cart.
     *
     * @returns {number}
     */
    $scope.calculatedTotalProductPrice = function () {
        //Set totalPrice to 0
        var totalPrice = 0
        //Loop trough each product
        angular.forEach($scope.cart.products, function (product) {
            //Add the productPrice multiplied by the quantity
            totalPrice += product.quantity * product.price
        })
        //Divide by 100, the prices are cent based
        totalPrice = totalPrice / 100
        //Set the totalPrice to the scope
        $scope.cart.totalPrice = totalPrice;
        return totalPrice;

    }

    $scope.calculatedTotalProductPriceWithDiscount = function(){
        totalPrice = $scope.calculatedTotalProductPrice();

        if(!$scope.cart.discount) return totalPrice;

        if(!$scope.cart.discount.type) return totalPrice;

        if($scope.cart.discount.type == 'absolute') return totalPrice - ($scope.cart.discount.value/100);

        if($scope.cart.discount.type == 'percentage') return totalPrice * (100-$scope.cart.discount.value)/100;
    }


    /**
     * This method will update a product in the php cart
     * @param productId
     * @param quantity
     */
    $scope.updateProductAjax = function (productId, quantity) {
        //If the quantity is not set return
        if (!quantity) return;
        //Open an ajax request
        $http({
            method: 'POST',
            url: '/api/cart/update-product/' + productId,
            data: {quantity: quantity}
        }).then(function successCallback(response) {
            //Set the new quantity to the quantity of the angular cart
            $scope.cart.products[productId].quantity = quantity;
        }, function errorCallback(response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });

    }

    /**
     * This method will delete a product in the php cart
     * @param productId
     */
    $scope.deleteProductAjax = function (productId) {
        //Open an ajax request
        $http({
            method: 'POST',
            url: '/api/cart/delete-product/' + productId,
        }).then(function successCallback(response) {


        }, function errorCallback(response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    }

    /**
     * This enables the popup for the delete confirmation
     * @param productId
     */
    $scope.enablePopup = function (productId) {
        //Set the product name to the popup
        $scope.popupProductName = $scope.cart.products[productId].name
        //Set the productId
        $scope.popupProductId = productId
        //Show the popup
        angular.element('#confirmation-popup').removeClass("hidden");
    }

    /**
     * This is called when a user clicks on the yes/no button
     * That is shown on the popup after a delete request.
     *
     * @param choice
     * @param productId
     */
    $scope.setConfirmation = function (choice, productId) {
        //Hide the popup box
        angular.element('#confirmation-popup').addClass("hidden");
        //reset the values (just to be sure)
        $scope.popupProductName = '';
        $scope.popupProductId = 0;

        //If the choice is true, delete the product based on the productId (when this is not 0)
        if (choice == true && productId != 0) $scope.deleteProduct(productId)
    }

    var myDate = new Date(new Date().setYear(new Date().getFullYear() - 18));

    $scope.dateOptions = {
        changeYear: true,
        changeMonth: true,
        yearRange: '1900:-0',
        dateFormat: 'dd-mm-yy',
        defaultDate: myDate
    };

    /**
     * This will validate the coupon,
     * and make sure the price recalculates
     * @param coupon
     */
    $scope.validateCoupon = function (coupon) {

        $scope.validateCouponAjax(coupon);
    }

    $scope.validateCouponAjax = function (coupon) {
        $http({
            method: 'POST',
            url: '/api/cart/validate-coupon/',
            data: {coupon: coupon}
        }).then(function successCallback(response) {
            console.log(response);
            //Set the new quantity to the quantity of the angular cart
            $scope.cart = response.data
        }, function errorCallback(response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });

    }


}])