diff --git a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php index 8c492eb1..6e6b2233 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php @@ -117,18 +117,20 @@ class CreateBookingRefundLogic extends AbstractControllerLogic $isFullyRefund = ($refund + $request->input('amount')) == $transaction->original_amount; $voucherCode = null; + $redemptionId = null; if ($transaction->voucherRedemption) { - $voucherCode = $transaction->voucherRedemption->voucher->code; + // $voucherCode = $transaction->voucherRedemption->voucher->code; + $redemptionId = $transaction->voucherRedemption->redemption_id; } - + $conversionObjectBeforeCurrentRefund = new CurrencyConversionObject($bookingAmountBeforeCurrentRefund, $booking->convertible_currency_id, $booking->service_id, $booking->fix_currency_id === 1 ? 0:1, $transaction->payment_method); $conversionObjectAfterRefund = new CurrencyConversionObject($isFullyRefund ? $request->input('amount') : $bookingAmountAfterRefunded, $booking->convertible_currency_id, $booking->service_id, $booking->fix_currency_id === 1 ? 0:1, $transaction->payment_method); - $quotationBeforeCurrentRefund = $this->fetchBookingQuotation->execute($booking->company, $conversionObjectBeforeCurrentRefund, $voucherCode); + $quotationBeforeCurrentRefund = $this->fetchBookingQuotation->execute($booking->company, $conversionObjectBeforeCurrentRefund, $voucherCode, $redemptionId); - $quotationAfterRefund = $this->fetchBookingQuotation->execute($booking->company, $conversionObjectAfterRefund, $voucherCode); + $quotationAfterRefund = $this->fetchBookingQuotation->execute($booking->company, $conversionObjectAfterRefund, $voucherCode, $redemptionId); $service_charges_to_refund = $isFullyRefund ? $quotationBeforeCurrentRefund->getServiceCharge() : $quotationBeforeCurrentRefund->getServiceCharge() - $quotationAfterRefund->getServiceCharge(); @@ -179,4 +181,4 @@ class CreateBookingRefundLogic extends AbstractControllerLogic } -} \ No newline at end of file +} diff --git a/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php b/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php index 1d3d7acc..e5535e2a 100644 --- a/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php +++ b/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php @@ -11,6 +11,7 @@ use App\Classes\Modules\Vouchers\DataTransferObjects\ValidatedVoucherObject; use App\Classes\Modules\Vouchers\DataTransferObjects\ValidateVoucherifyVoucherObject; use App\Classes\Modules\Currencies\Services\FetchesCurrency; use App\Classes\Modules\Vouchers\Services\Voucherify\ValidatesVoucherifyVoucher; +use App\Classes\Modules\Vouchers\Services\Voucherify\FetchesVoucherifyRedemption; use App\Models\Company; use App\Models\Currency; use Illuminate\Support\Facades\Log; @@ -27,27 +28,34 @@ class FetchesBookingQuotation /** @var ValidatesVoucherifyVoucher */ private $validatesVoucherifyVoucher; + /** @var FetchesVoucherifyRedemption */ + private $fetchesVoucherifyRedemption; + /** * FetchesBookingQuotation constructor. * @param FetchesCompanyServiceSettings $fetchesCompanyServiceSettings * @param FetchesCurrency $fetchesCurrency + * @param ValidatesVoucherifyVoucher $validatesVoucherifyVoucher + * @param FetchesVoucherifyRedemption $fetchesVoucherifyRedemption */ - public function __construct(FetchesCompanyServiceSettings $fetchesCompanyServiceSettings, FetchesCurrency $fetchesCurrency, ValidatesVoucherifyVoucher $validatesVoucherifyVoucher) + public function __construct(FetchesCompanyServiceSettings $fetchesCompanyServiceSettings, FetchesCurrency $fetchesCurrency, ValidatesVoucherifyVoucher $validatesVoucherifyVoucher, FetchesVoucherifyRedemption $fetchesVoucherifyRedemption) { $this->fetchesCompanyServiceSettings = $fetchesCompanyServiceSettings; $this->fetchesCurrency = $fetchesCurrency; $this->validatesVoucherifyVoucher = $validatesVoucherifyVoucher; + $this->fetchesVoucherifyRedemption = $fetchesVoucherifyRedemption; } /** * @param Company $company * @param CurrencyConversionObject $conversionObject - * @param string $voucherCode + * @param null|string $voucherCode + * @param null|string $redemptionId * @return CalculationObject * @throws MalformedRequestException */ - public function execute(Company $company, CurrencyConversionObject $conversionObject, ?string $voucherCode = null){ + 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); @@ -58,27 +66,37 @@ class FetchesBookingQuotation $calculationObject = new CalculationObject($conversionObject, $configurations, null); + $voucher = null; + //Voucherify if($voucherCode){ $employee = $company->employees()->first(); $validateVoucherifyVoucherObject = new ValidateVoucherifyVoucherObject($company->id, $voucherCode, $calculationObject->getSubTotal(), $employee); $result = $this->validatesVoucherifyVoucher->execute($validateVoucherifyVoucherObject); - - // //A minimum charge of RM5 applies when voucher used make price to be paid by customer RM0 - // if(isset($result->order->total_amount) && $result->order->total_amount === 0){ - // // $result->order->cief_original_total_amount = $result->order->total_amount; - // $result->order->total_amount = 500; - // $result->order->total_discount_amount = $result->order->total_discount_amount - $result->order->total_amount; - // Log::info('FetchesBookingQuotation order total_amount RM0 (voucher applied) for company id ' . $company->id. ' with original amount ' . $calculationObject->getSubTotal()); - // } - $voucher = [ "code" => $result->code, "discount" => property_exists($result, 'discount') ? $result->discount : null, "metadata" => $result->metadata, "order" => $result->order, ]; - $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); + } + 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); } diff --git a/app/Classes/Modules/Vouchers/Services/Voucherify/FetchesVoucherifyRedemption.php b/app/Classes/Modules/Vouchers/Services/Voucherify/FetchesVoucherifyRedemption.php new file mode 100644 index 00000000..03966926 --- /dev/null +++ b/app/Classes/Modules/Vouchers/Services/Voucherify/FetchesVoucherifyRedemption.php @@ -0,0 +1,38 @@ +voucherifyClient = createVoucherifyClient(); + } + + /** + * @param string $redemptionId + * @return null|object + * @throws \Voucherify\ClientException + */ + public function execute(string $redemptionId) + { + try { + $result = $this->voucherifyClient->redemptions->get($redemptionId); + return $result; + } catch (\Voucherify\ClientException $e) { + Log::error('FetchesVoucherifyRedemption '.$e); + return null; + } + } +} diff --git a/resources/assets/vue/components/bookings/forms/BookingPaymentQuotationComponent.vue b/resources/assets/vue/components/bookings/forms/BookingPaymentQuotationComponent.vue index f0528dbd..ba2d7893 100644 --- a/resources/assets/vue/components/bookings/forms/BookingPaymentQuotationComponent.vue +++ b/resources/assets/vue/components/bookings/forms/BookingPaymentQuotationComponent.vue @@ -116,7 +116,7 @@
{{this.data.fixed_currency.short_code}} {{(Math.round((this.data.outstanding_amount + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
-
+