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/deensekroon.komma-mediadesign.nl/wwwroot/App/Order/GiftCardService.php
<?php


namespace App\Order;


class GiftCardService
{
    /**
     * All cards to process
     *
     * @var array
     */
    private $cards;

    /**
     * Subtotal of all products before gift cards
     *
     * @var float
     */
    private $originalProductCost;

    /**
     * Subtotal of all products after gift cards
     *
     * @var float
     */
    private $remainingProductCost;

    /**
     * Shipping cost before gift cards
     *
     * @var float
     */
    private $originalShippingCost;

    /**
     * Shipping cost after gift cards
     *
     * @var float
     */
    private $remainingShippingCost;

    /**
     * The total used amount of all gift cards
     *
     * @var float
     */
    private $totalUsedGiftCardAmount;

    /**
     * GiftCardService constructor.
     * @param $productCost
     * @param $shippingCost
     */
    public function __construct($cards, $productCost, $shippingCost)
    {
        $this->cards = $cards;
        $this->originalProductCost = $this->remainingProductCost = $productCost;
        $this->originalShippingCost = $this->remainingShippingCost = $shippingCost;
    }

    /**
     * Process cards
     */
    public function processAll()
    {
        // Loop through each card
        foreach($this->cards as $key => $card)
        {
            $this->cards[$key] = $this->processCard($card);
        }

        return $this->cards;
    }

    /**
     * @return float
     */
    public function getRemainingProductCost()
    {
        return $this->remainingProductCost;
    }

    /**
     * @return float
     */
    public function getRemainingShippingCost()
    {
        return $this->remainingShippingCost;
    }

    /**
     * @return float
     */
    public function getTotalUsedGiftCardAmount()
    {
        return $this->totalUsedGiftCardAmount;
    }


    /**
     * Process one card
     *
     * @param $card
     * @return mixed
     */
    private function processCard($card)
    {
        if($card['amount'] < 0) return false;

        // (re)set variable used amount
        $card['usedAmount'] = 0;

        // Set start
        $productCostsAtStartCard = $this->remainingProductCost;
        $shippingCostsAtStartCard = $this->remainingShippingCost;

        // Subtract card amount from the remaining amount
        $this->remainingProductCost -= $card['amount'];

        // If there are remaining product cost
        // That means the card is fully used
        if($this->remainingProductCost > 0) return $this->cardIsFullyUsed($card);

        // Negative value becomes remaining amount
        $remainingCardAmount = $this->remainingProductCost * -1;

        // Costs become 0
        $this->remainingProductCost = 0;

        // Calculate shipping costs
        $this->remainingShippingCost -= $remainingCardAmount;

        // If there are remaining product cost
        // That means the card is fully used
        if($this->remainingShippingCost > 0) return $this->cardIsFullyUsed($card);

        // Costs become 0
        $this->remainingShippingCost = 0;

        // Set used amount
        // We managed to get product and shipping for free
        $card['usedAmount'] = $productCostsAtStartCard + $shippingCostsAtStartCard;

        // Add cart amount to the total used gift card amount
        $this->totalUsedGiftCardAmount += $card['usedAmount'];

        return $card;
    }

    /**
     * When a card is fully used
     */
    private function cardIsFullyUsed($card)
    {
        // Fully used
        $card['usedAmount'] = $card['amount'];

        // Add cart amount to the total used gift card amount
        $this->totalUsedGiftCardAmount += $card['amount'];

        return $card;
    }



}