Fix an error with refund for transaction that used a voucher earlier, get discount amount for voucher so to get details breakdown

This commit is contained in:
Dillon Ngo
2024-08-12 09:34:45 +08:00
parent 3245abe05d
commit cfcc5fbe26
4 changed files with 76 additions and 18 deletions
@@ -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);
}