mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
79 lines
2.7 KiB
PHP
79 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Vouchers\ControllersLogic;
|
|
|
|
|
|
use App\Classes\Exceptions\MalformedRequestException;
|
|
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
|
use App\Classes\Modules\Vouchers\Services\Voucherify\ValidatesVoucherifyVoucher;
|
|
use App\Classes\Modules\Vouchers\DataTransferObjects\ValidateVoucherifyVoucherObject;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Booking;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ValidateVoucherLogic extends AbstractControllerLogic
|
|
{
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function notification():array {
|
|
return [
|
|
'title' => 'Fetch Voucher',
|
|
'message' => 'You have successfully fetched a voucher'
|
|
];
|
|
}
|
|
|
|
/** @var ValidatesVoucherifyVoucher */
|
|
private $validatesVoucherifyVoucher;
|
|
|
|
/**
|
|
* ValidateVoucherLogic constructor.
|
|
* @param ValidatesVoucherifyVoucher $validatesVoucherifyVoucher
|
|
*/
|
|
public function __construct(ValidatesVoucherifyVoucher $validatesVoucherifyVoucher)
|
|
{
|
|
$this->validatesVoucherifyVoucher = $validatesVoucherifyVoucher;
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws MalformedRequestException
|
|
*/
|
|
public function logic(Request $request) : JsonResponse
|
|
{
|
|
$employeeWhoOwnsTheVoucher = null;
|
|
$booking = Booking::find($request->input('itemId'));
|
|
$employees = $booking->company->employees()->get();
|
|
|
|
if(count($employees) > 1){
|
|
foreach($employees as $singleEmployee){
|
|
$userRewards = $singleEmployee->rewards;
|
|
foreach($userRewards as $userReward){
|
|
if ($userReward->voucher && $userReward->voucher->code === $request->input('voucherCode')) {
|
|
Log::info('2. Company with multiple employees: ' . json_encode($singleEmployee) . ", voucher: " . $request->input('voucherCode'));
|
|
$employeeWhoOwnsTheVoucher = $singleEmployee;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!$employeeWhoOwnsTheVoucher){
|
|
$employeeWhoOwnsTheVoucher = $booking->company->employees()->first();
|
|
}
|
|
|
|
$amount = $this->floatvalue($request->input('amount'));
|
|
|
|
$validateVoucherifyVoucherObject = new ValidateVoucherifyVoucherObject($booking->company_id, $request->input('voucherCode'), $amount, $employeeWhoOwnsTheVoucher);
|
|
$result = $this->validatesVoucherifyVoucher->execute($validateVoucherifyVoucherObject);
|
|
return $this->response(['data' => $result]);
|
|
}
|
|
|
|
private function floatvalue($val){
|
|
$val = str_replace(",",".",$val);
|
|
$val = preg_replace('/\.(?=.*\.)/', '', $val);
|
|
return floatval($val);
|
|
}
|
|
}
|