fetchesCompanyServiceSettings = $fetchesCompanyServiceSettings; $this->fetchesCurrency = $fetchesCurrency; $this->validatesVoucherifyVoucher = $validatesVoucherifyVoucher; $this->fetchesVoucherifyRedemption = $fetchesVoucherifyRedemption; } /** * @param Company $company * @param CurrencyConversionObject $conversionObject * @param null|string $voucherCode * @param null|string $redemptionId * @return CalculationObject * @throws MalformedRequestException */ public function execute(Company $company, CurrencyConversionObject $conversionObject, ?string $voucherCode = null, ?string $redemptionId = null){ if($conversionObject->getAmount() <= 0) throw new MalformedRequestException('Your transfer must be greater than zero.'); $configurations = $this->fetchesCompanyServiceSettings->execute($company, $conversionObject); /** @var Currency $currency */ $currency = $this->fetchesCurrency->execute(['id' => $conversionObject->getCurrencyId()]); if($conversionObject->getAmount() > $configurations->getMaxLimit()) throw new MalformedRequestException('Your transfer can\'t be greater than '.number_format( floatval(str_replace(',', '', $configurations->getMaxLimit())), 2, '.', ',').' '.$currency->short_code); $calculationObject = new CalculationObject($conversionObject, $configurations, null); $voucher = null; //Voucherify if($voucherCode){ $employeeWhoOwnsTheVoucher = null; $employees = $company->first()->employees; if(count($employees) > 1){ foreach($employees as $singleEmployee){ $userRewards = $singleEmployee->rewards; foreach($userRewards as $userReward){ if ($userReward->voucher && $userReward->voucher->code === $voucherCode) { Log::info('1. Company with multiple employees: ' . json_encode($singleEmployee) . ", voucher: " . $voucherCode); $employeeWhoOwnsTheVoucher = $singleEmployee; } } } } if(!$employeeWhoOwnsTheVoucher){ $employeeWhoOwnsTheVoucher = $company->employees()->first(); } $validateVoucherifyVoucherObject = new ValidateVoucherifyVoucherObject($company->id, $voucherCode, $calculationObject->getSubTotal(), $employeeWhoOwnsTheVoucher); $result = $this->validatesVoucherifyVoucher->execute($validateVoucherifyVoucherObject); $voucher = [ "code" => $result->code, "discount" => property_exists($result, 'discount') ? $result->discount : null, "metadata" => $result->metadata, "order" => $result->order, ]; } else if($redemptionId){ $result = $this->fetchesVoucherifyRedemption->execute($redemptionId); $voucher = [ "code" => $result->voucher->code ?? null, "discount" => null, "metadata" => null, "order" => $result->order ?? null, ]; } if($voucher){ $validatedVoucherObject = new ValidatedVoucherObject( isset($voucher['metadata']->name) ? $voucher['metadata']->name : "", $voucher['code'], $voucher['discount']->type ?? 'AMOUNT', $voucher['order']->total_discount_amount, $voucher['order']->total_amount); $calculationObject = new CalculationObject($conversionObject, $configurations, $validatedVoucherObject); } return $calculationObject; } }