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/Invoice/InvoiceService.php
<?php

namespace App\Invoice;

class InvoiceService
{
    /**
     * Shipping costs where never stored in the database in the old days.
     * For all orders from that time we need to estimate the shipping costs
     *
     * @param $totalAmount
     * @param $productsAmount
     * @param $giftCardAmount
     * @param $shippingCountry
     * @return int
     */
    public function estimateShippingCosts($totalAmount, $productsAmount, $giftCardAmount, $shippingCountry)
    {
        // What would the old way calculate
        $shippingCosts = ($totalAmount + $giftCardAmount) - $productsAmount;

        // If we have shipping costs, let's get them by country
        // Use the countries where in old orders exists to prevent and extra query
        switch(strtolower($shippingCountry))
        {
            case 'nl':
            case 'be':
                if($shippingCosts > 4.95) $shippingCosts = 4.95;
                break;
            case 'de':
                $shippingCosts = 3.5;
                if($shippingCosts > 3.5) $shippingCosts = 3.5;

                break;
            case 'dk':
            case 'gb':
                $shippingCosts = 8.5;
                if($shippingCosts > 8.5) $shippingCosts = 8.5;
            break;
        }

        return $shippingCosts;

    }

    /**
     * Because we're working with floats (unfortunately)
     * we need to multiply with a 100 to make sure we're calculating correctly
     *
     * @param $totalPrice
     * @param $subtotalPrice
     * @return float
     */
    public function calculateOrderDiscount($totalPrice, $subtotalPrice, $shippingCosts, $giftCardAmount)
    {
        $totalPrice = $totalPrice*100;
        $totalPrice = (int) $totalPrice;

        $giftCardAmount = $giftCardAmount*100;
        $giftCardAmount = (int) $giftCardAmount;

        $subtotalPrice = $subtotalPrice*100;
        $subtotalPrice = (int) $subtotalPrice;

        $shippingCosts = $shippingCosts*100;
        $shippingCosts = (int) $shippingCosts;

        $orderDiscount = $totalPrice + $giftCardAmount - $subtotalPrice - $shippingCosts;

        return (float) $orderDiscount/100;
    }
    
    
}