Al-HUWAITI Shell
Al-huwaiti


Server : LiteSpeed
System : Linux server335.web-hosting.com 4.18.0-553.62.1.lve.el8.x86_64 #1 SMP Mon Jul 21 17:50:35 UTC 2025 x86_64
User : cardxfeb ( 2452)
PHP Version : 8.1.34
Disable Function : NONE
Directory :  /home/cardxfeb/www/app/Repositories/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/cardxfeb/www/app/Repositories/CouponCodeRepository.php
<?php

namespace App\Repositories;

use App\Models\CouponCode;
use App\Models\PlanCustomField;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;

/**
 * Class featureRepository
 */
class CouponCodeRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'coupon_name', 'type',
    ];

    /**
     * Return searchable fields
     */
    public function getFieldsSearchable(): array
    {
        return $this->fieldSearchable;
    }

    /**
     * Configure the Model
     **/
    public function model()
    {
        return CouponCode::class;
    }

    public function getAfterDiscountData($input)
    {
        $planId = $input['planId'];
        $planPrice = $input['planPrice'];
        $couponCode = $input['couponCode'];
        $customFieldId = $input['customFieldId'] ?? null;

        $newPlan = getProratedPlanData($planId);

        if($customFieldId != null){
         $customPlan = PlanCustomField::where('id',$customFieldId)->first();
         $newPlanPrice = intval($customPlan->custom_vcard_price);
        }else{
         $newPlanPrice = $newPlan['amountToPay'] + $newPlan['remainingBalance'];
        }

        if($customFieldId == null){
        if (intval($newPlanPrice) != intval($planPrice)) {
            return false;
        }
         }

        if (empty($couponCode)) {
         if($customFieldId){
         $newPlan['amountToPay'] = $newPlanPrice - $newPlan['remainingBalance'];
         $newPlan['amountToPay'] = round($newPlan['amountToPay'] , 2);
            return $newPlan;
         }
         else{
             return $newPlan;
         }
        }


        $couponCode = strtoupper($couponCode);
        $coupon = CouponCode::whereCouponName($couponCode)->whereStatus(CouponCode::ACTIVE)->first();
        if (empty($coupon)) {
            throw new UnprocessableEntityHttpException(__('validation.coupon_code.not_found'));
        }
        if ($coupon->is_expired) {
            throw new UnprocessableEntityHttpException(__('validation.coupon_code.expired'));
        }

        if ($coupon->type == CouponCode::FIXED_TYPE) {
            $discount = $coupon->discount;
            $priceAfterDiscount = $newPlanPrice - $discount;
        } else {
            $discount = round(($newPlanPrice * $coupon->discount) / 100);
            $priceAfterDiscount = $newPlanPrice - $discount;
        }
        $amountToPay = $priceAfterDiscount - $newPlan['remainingBalance'];

        if ($priceAfterDiscount < 0) {
            $priceAfterDiscount = $amountToPay = 0;
            $discount = $newPlanPrice;
        }

        $amountToPay = ($amountToPay < 0) ? 0 : $amountToPay;

        $discountData['discountType'] = $coupon->type;
        $discountData['priceAfterDiscount'] = $priceAfterDiscount;
        $discountData['amountToPay'] = $amountToPay;
        $discountData['discount'] = $discount;
        $discountData['couponCode'] = $couponCode;
        $discountData['couponId'] = $coupon->id;
        $newPlan['afterDiscount'] = $discountData;

        return $newPlan;
    }
}

Al-HUWAITI Shell