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/topswtw.komma.pro/app/KommaApp/Shop/Orders/OrderDiscountService.php
<?php

namespace KommaApp\Shop\Orders;
use Carbon\Carbon;

/**
 * @author      Jaap Faes <jaap@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */
 
class OrderDiscountService
{
    protected $orderDiscountModel;

    public function __construct(OrderDiscount $orderDiscountModel)
    {
        $this->orderDiscountModel = $orderDiscountModel;
    }

    public function getDiscountPercentage($price, $couponCode = null)
    {
        $discountPrice = $price;
        $applicableDiscounts = $this->getApplicableDiscountRules($price, $couponCode);

        foreach($applicableDiscounts as $discount)
        {
            if(is_numeric($discount['discount_percentage']))
            {
                $discountPrice = $discountPrice * (1-($discount['discount_percentage'] / 100));
            }
            if(is_numeric($discount['discount_absolute']))
            {
                $discountPrice -= $discount['discount_absolute'];
            }
        }

        if($discountPrice != $price) return 100 / ($price / ($price - $discountPrice));
        return 0;
    }

    public function getDiscountPrice($price, $couponCode = null)
    {
        $discountPrice = $price;
        $applicableDiscounts = $this->getApplicableDiscountRules($price, $couponCode);

        foreach($applicableDiscounts as $discount)
        {
            if(is_numeric($discount['discount_percentage']))
            {
                $discountPrice = $discountPrice * (1-($discount['discount_percentage'] / 100));
            }
            if(is_numeric($discount['discount_absolute']))
            {
                $discountPrice -= $discount['discount_absolute'];
            }
        }
        return $price - round($discountPrice);
    }

    public function getDiscountSummary($price, $couponCode = null)
    {

        $applicableDiscounts = $this->getApplicableDiscountRules($price, $couponCode);
        $output = [];

        foreach($applicableDiscounts as $discount)
        {
            if(is_numeric($discount['discount_percentage']))
            {
                $output[] = $discount['discount_percentage'].'%';
                // Tops specifiek: QnD - Percentage als eerste laten zien
                //array_unshift($output,$discount->discount_percentage . '%');
            }
            if(is_numeric($discount['discount_absolute']))
            {
                $output[] = \Format::asPrice($discount['discount_absolute'], ['currency' => '€']);
            }
        }

        if(count($output) < 1) return '-';

        return implode('<br/>', $output);
    }

    public function hasNoShippingCosts($price, $couponCode = null)
    {

        $discounts = $this->getApplicableDiscountRules($price, $couponCode);
        foreach($discounts as $discount)
        {
            if($discount['discount_no_shipping_costs'] == 1) return true;

        }
        return false;
    }

    public function getApplicableDiscountRules($price, $couponCode = null)
    {
        $query = $this->orderDiscountModel
            ->join('discount_orders_translations', 'discount_orders_translations.discount_order_id', '=', 'discount_orders.id')
            ->where('active', '1')
            ->where('shop_id', \Shop::getId())
            ->where(function($query) use ($price){
                $query->whereNull('price_min')
                    ->orWhere('price_min', '<=', $price);
            })
            ->where(function($query) use ($price){
                $query->whereNull('price_max')
                    ->orWhere('price_max', '>=', $price);
            })
            ->where(function($query){
                $query->whereNull('date_min')
                    ->orWhere('date_min', '<=', Carbon::now());
            })
            ->where(function($query){
                $query->whereNull('date_max')
                    ->orWhere('date_max', '>=', Carbon::now());
            })
            ->where(function($query) use ($couponCode){
                $query->whereNull('custom_coupon_code')
                    ->orWhere('custom_coupon_code', '=', $couponCode);
            })
            ->orderBy('priority');

        // THIS IS A QUICK FIX TO PREVENT MULTIPLE DISCOUNTS
        // Only include condition when $currentLanguage != null
        // For example this is null when called through /api/shipping-costs/{$countryid}
        // If we comment this line we get multiple results for voucher and so double discounts (!!!)
        // Todo: language should be passed through the api so we always have a language id
        $currentLanguage = \Shop::getLanguageService()->getCurrentLanguageId();
        if($currentLanguage != null) $query = $query->where('language_id', $currentLanguage);

        // Get all discount orders
        $orderDiscounts = $query->get();


        $couponBatchIdByCode = $this->getCouponBatchIdByCode($couponCode);

        foreach($orderDiscounts as $key => $discount)
        {
            if(
                ( isset($discount->coupon_batch_id) ) &&
                ( $discount->coupon_batch_id != $couponBatchIdByCode )
            ){
                unset( $orderDiscounts[$key] );
            }
        }

        $discounts = [];
        foreach($orderDiscounts as $discount)
        {
            $discounts[] = $discount;
            if($discount->is_final) break;
        }

        return $discounts;
    }

    public function getDiscounts()
    {
        return $this->orderDiscountModel
            ->join('discount_orders_translations', 'discount_orders_translations.discount_order_id', '=', 'discount_orders.id')
            ->where('language_id', \Shop::getLanguageService()->getCurrentLanguageId())
            ->where('active', '1')
            ->where('shop_id', \Shop::getId())
            ->where(function($query){
                $query->whereNull('date_min')
                    ->orWhere('date_min', '<=', Carbon::now());
            })
            ->where(function($query){
                $query->whereNull('date_max')
                    ->orWhere('date_max', '>=', Carbon::now());
            })
            ->orderBy('priority')
            ->get();
    }

    public function getCouponBatchIdByCode($code)
    {
        $coupon = \DB::table('coupons')
            ->where('code', $code)
            ->whereNull('used_at')
            ->first();
        if($coupon) return $coupon->coupon_batch_id;
        return null;
    }

    public function isCouponInBatch($couponCode, $batchId){
        $coupon = \DB::table('coupons')
            ->where('code', $couponCode)
            ->where('coupon_batch_id', $batchId)
            ->whereNull('used_at')
            ->first();
        if($coupon) return $coupon->coupon_batch_id;
        return false;
    }


}