Merge branch 'dillon/63.6-company-with-multiple-employees' into development

This commit is contained in:
Dillon Ngo
2024-08-20 17:31:08 +08:00
15 changed files with 265 additions and 14 deletions
@@ -0,0 +1,53 @@
<?php
namespace App\Classes\General\Eloquent\Filters;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
class HasActiveRewardWithCompany implements Filter
{
/**
* @param Builder $builder
* @param $value
* @return mixed
*/
public static function apply(Builder $builder, $value)
{
if(in_array(Auth::user()->type, RoleTypes::ADMIN_ROLES)){
// $userId = $value !== 1 ? $value : Auth::user()->id;
$userId = $value;
$user = User::where('id', $userId)->first();
$users = $user->company()->first()->employees;
$userIds = $users->pluck('id');
return $builder->whereIn('user_id', $userIds)
->where(function ($query) {
$query->whereHas('reward', function ($subquery) {
$subquery->where('is_active', true);
})
->orWhereDoesntHave('reward');
})
->whereDoesntHave('voucher.redemptions.transaction.booking.company.employees', function ($query) use ($userId) {
$query->where('user_id', $userId);
});
}
else{
$user = User::where('id', Auth::user()->id)->first();
$users = $user->company()->first()->employees;
$userIds = $users->pluck('id');
return $builder->whereIn('user_id', $userIds)
->where(function ($query) {
$query->whereHas('reward', function ($subquery) {
$subquery->where('is_active', true);
})
->orWhereDoesntHave('reward');
})
->whereDoesntHave('voucher.redemptions.transaction.owner');
}
}
}
@@ -5,7 +5,7 @@ namespace App\Classes\General\Eloquent\Filters;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
class HasActiveReward implements Filter
class HasActiveRewardWithUser implements Filter
{
/**
@@ -70,8 +70,24 @@ class FetchesBookingQuotation
//Voucherify
if($voucherCode){
$employee = $company->employees()->first();
$validateVoucherifyVoucherObject = new ValidateVoucherifyVoucherObject($company->id, $voucherCode, $calculationObject->getSubTotal(), $employee);
$employeeWhoOwnsTheVoucher = null;
$employees = $company->first()->employees;
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,
@@ -0,0 +1,112 @@
<?php
namespace App\Classes\Modules\Transactions\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\DeletesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Transaction;
use App\Classes\Modules\Bookings\ControllersLogic\UpdateBookingAmountLogic;
use App\Classes\Modules\Bookings\Services\CalculatesBookingRefundAmount;
use Illuminate\Support\Facades\Log;
use App\Classes\Modules\Bookings\Services\CalculatesBookingPaidAmount;
class DeleteRefundTransactionLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification(): array
{
return [
'title' => 'Deleted Refund Transaction',
'message' => 'You have successfully deleted a transaction'
];
}
/** @var FetchesTransaction */
private $fetchesTransaction;
/** @var DeletesTransaction */
private $deletesTransaction;
/** @var UpdatesTransactionStatus */
private $updatesTransactionStatus;
/** @var CalculatesBookingRefundAmount */
private $calculatesBookingRefundAmount;
/** @var CalculatesBookingPaidAmount */
private $calculatesBookingPaidAmount;
/** @var UpdateBookingAmountLogic */
private $updateBookingAmountLogic;
/**
* CreatePaymentVerificationDocumentLogic constructor.
* @param FetchesTransaction $fetchesTransaction
* @param DeletesTransaction $deletesTransaction
* @param CalculatesBookingRefundAmount $calculatesBookingRefundAmount
* @param UpdateBookingAmountLogic $updateBookingAmountLogic
* @param calculatesBookingPaidAmount $calculatesBookingPaidAmount
*/
public function __construct(FetchesTransaction $fetchesTransaction, DeletesTransaction $deletesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, CalculatesBookingRefundAmount $calculatesBookingRefundAmount, UpdateBookingAmountLogic $updateBookingAmountLogic, CalculatesBookingPaidAmount $calculatesBookingPaidAmount)
{
$this->fetchesTransaction = $fetchesTransaction;
$this->deletesTransaction = $deletesTransaction;
$this->updatesTransactionStatus = $updatesTransactionStatus;
$this->calculatesBookingRefundAmount = $calculatesBookingRefundAmount;
$this->updateBookingAmountLogic = $updateBookingAmountLogic;
$this->calculatesBookingPaidAmount = $calculatesBookingPaidAmount;
}
/**
* @param Request $request
* @return JsonResponse
* @throws \App\Classes\Exceptions\MalformedRequestException
*/
public function logic(Request $request): JsonResponse
{
// delete refund transaction
$transaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]);
$this->deletesTransaction->execute($transaction);
// Update payment_transaction status
$payment_transaction = $transaction->owner;
$this->updatesTransactionStatus->execute($payment_transaction, ApprovalStatus::APPROVED);
// delete wallet top up transaction
$booking = $transaction->owner->owner;
Transaction::where('type', TransactionType::CREDIT_NOTE)
->where('amount', $transaction->amount)
->where('payment_reference', 'like', '%' . $booking->marking . '%')
->delete();
// update back the latest booking amount
$request['fix_amount'] = $this->calculatesBookingPaidAmount->execute($booking);
$request->route()->setParameter('id', $booking->id);
$this->updateBookingAmountLogic->execute($request);
// if have SUPPLIER_REFUND transaction
$bookingInWhiteForm = $payment_transaction->transactions()->bills()->first();
if ($bookingInWhiteForm) {
$whiteFormTransaction = Transaction::where('type', TransactionType::SUPPLIER_REFUND)
->where('payment_reference', $transaction->payment_reference)
->first();
// Log::info($whiteFormTransaction->id);
$whiteFormTransaction->delete();
}
return $this->response([]);
}
}
@@ -10,6 +10,7 @@ use App\Classes\Modules\Vouchers\DataTransferObjects\ValidateVoucherifyVoucherOb
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Models\Booking;
use Illuminate\Support\Facades\Log;
class ValidateVoucherLogic extends AbstractControllerLogic
{
@@ -42,11 +43,27 @@ class ValidateVoucherLogic extends AbstractControllerLogic
*/
public function logic(Request $request) : JsonResponse
{
$employeeWhoOwnsTheVoucher = null;
$booking = Booking::find($request->input('itemId'));
$employee = $booking->company->employees()->first();
$employees = $booking->company->employees()->get();
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, $employee);
$validateVoucherifyVoucherObject = new ValidateVoucherifyVoucherObject($booking->company_id, $request->input('voucherCode'), $amount, $employeeWhoOwnsTheVoucher);
$result = $this->validatesVoucherifyVoucher->execute($validateVoucherifyVoucherObject);
return $this->response(['data' => $result]);
}
@@ -17,8 +17,8 @@ class ValidateVoucherifyVoucherObject implements DataTransferObject
/** @var float */
private $amount;
/** @var User */
private $user;
/** @var User */
private $user; //this will affect certain voucher that limit user redemption e.g. one user one redemption per campaign
/**
* ValidateVoucherifyVoucherObject constructor.
@@ -83,7 +83,24 @@ class BookingToVoucherifyProcessor
$voucherify_customer_id = "";
$voucherify_order_id = "";
if($voucherCode){
$redeemVoucherifyVoucherObject = new RedeemVoucherifyVoucherObject($companyId, $transaction->id, $voucherCode, $amount, $user);
$employeeWhoOwnsTheVoucher = null;
$employees = $user->company()->first()->employees;
foreach($employees as $singleEmployee){
$userRewards = $singleEmployee->rewards;
foreach($userRewards as $userReward){
if ($userReward->voucher && $userReward->voucher->code === $voucherCode) {
Log::info('3. Company with multiple employees: ' . json_encode($singleEmployee) . ", voucher: " . $voucherCode);
$employeeWhoOwnsTheVoucher = $singleEmployee;
}
}
}
if(!$employeeWhoOwnsTheVoucher){
$employeeWhoOwnsTheVoucher = $user;
}
$redeemVoucherifyVoucherObject = new RedeemVoucherifyVoucherObject($companyId, $transaction->id, $voucherCode, $amount, $employeeWhoOwnsTheVoucher);
$redeemVoucherResult = $this->redeemsVoucherifyVoucher->execute($redeemVoucherifyVoucherObject);
// Log::info('redeemVoucherResult: '.json_encode($redeemVoucherResult));
@@ -101,7 +118,7 @@ class BookingToVoucherifyProcessor
$voucher = $this->recordVoucherInfo($redeemedVoucher);
$this->createsVoucherRedemption->execute($transaction, $voucher, $redemptionId, $voucherDiscountAmount);
$this->recordVoucherForUserInfo($user, $voucher);
$this->recordVoucherForUserInfo($employeeWhoOwnsTheVoucher, $voucher);
}
else{
$createVoucherifyOrderObject = new CreateVoucherifyOrderObject($user, $companyId, $transaction->id, $amount, true, $transaction->type == TransactionType::TOP_UP);
@@ -0,0 +1,14 @@
<?php
namespace App\Http\Controllers\Transactions;
use App\Classes\Modules\Transactions\ControllersLogic\DeleteRefundTransactionLogic;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class DeleteRefundTransactionController
{
public function delete(Request $request, DeleteRefundTransactionLogic $logic): JsonResponse {
return $logic->execute($request);
}
}
+3 -1
View File
@@ -4,6 +4,7 @@ namespace App\Http\Resources;
use ArrayObject;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Log;
class VoucherResource extends JsonResource
{
@@ -16,7 +17,8 @@ class VoucherResource extends JsonResource
public function toArray($request)
{
$filteredRedemptions = new ArrayObject([]);
if ($request->has('filters') && str_contains($request->input('filters'), "has_vouchers_all")) {
if ($request->has('filters') && (str_contains($request->input('filters'), "has_active_reward_with_user") )) {
//|| str_contains($request->input('filters'), "has_active_reward_with_company")
$filteredRedemptions = new ArrayObject([]);
}
else{
@@ -98,7 +98,7 @@
fetchVouchers(){
this.isLoading = true;
if(this.employee){
this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_vouchers_all': this.employee.id} ), 'get', this.section, false, false);
this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_active_reward_with_company': this.employee.id} ), 'get', this.section, false, false);
}
},
successHandler(response){
@@ -79,7 +79,7 @@
fetchVouchers(){
this.isLoading = true;
if(this.employee){
this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_vouchers_all': this.employee.id} ), 'get', this.section, false, false);
this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_active_reward_with_company': this.employee.id} ), 'get', this.section, false, false);
}
},
successHandler(response){
@@ -403,6 +403,24 @@
</modal-component>
</div>
</div>
<div class="row m-b-15 text-right parentContainer" v-if="$store.getters.isSuperAdmin && refund.status === 2">
<div class="col">
<button class="btn btn-xs btn-outline-danger b-rad-none m-r-5 requestModal" data-type="deleteRefund">
<i class="fa fa-times fa-fw"></i>
</button>
<modal-component class="animate__animated animate__fast animate__fadeIn" type="deleteRefund">
<general-confirmation-form-component
contentText="Are you sure you want to delete this refund?"
modalType="delete"
class="text-center"
:apiRoute="route('api.transaction.refund.delete', refund.id)"
apiMethod="delete"
:section="section"
>
</general-confirmation-form-component>
</modal-component>
</div>
</div>
</div>
</div>
</div>
@@ -57,7 +57,7 @@
<div class="col">
<div class="row">
<div class="col" v-if="this.$store.getters.getUserId">
<list-component section="customerVouchersListSection" :endpoint="route('api.voucher.user.list')" :options="{ 'has_vouchers_all': id }">
<list-component section="customerVouchersListSection" :endpoint="route('api.voucher.user.list')" :options="{ 'has_active_reward_with_user': id }">
<template slot="list" slot-scope="{data}">
<single-user-reward-item-component :data="data"></single-user-reward-item-component>
</template>
@@ -52,7 +52,7 @@
<div class="col">
<div class="row">
<div class="col" v-if="this.$store.getters.getUserId">
<list-component section="customerVouchersListSection" :endpoint="route('api.voucher.user.list')" :options="{ 'has_vouchers_all': true }">
<list-component section="customerVouchersListSection" :endpoint="route('api.voucher.user.list')" :options="{ 'has_active_reward_with_user': true }">
<template slot="list" slot-scope="{data}">
<single-user-reward-item-component :data="data"></single-user-reward-item-component>
</template>
+2
View File
@@ -14,6 +14,8 @@ Route::group(['prefix' => 'transactions', 'namespace' => 'Transactions', 'as' =>
Route::put('/{id}/bill/{status}', 'UpdatePaymentTransactionStatusController@update')->where('status', 'pending|complete')->name('bill.status');
Route::put('/{id}/refund/status/update/{status}', 'UpdateRefundTransactionStatusController@update')->name('refund.status.update');
Route::delete('/refund/{id}/delete', 'DeleteRefundTransactionController@delete')->name('refund.delete');
route::delete('{id}/bill/delete', 'DeletePaymentProofDocumentController@delete')->name('bill.delete');
Route::post('booking/{id}/details/update', 'CreatePurchaseOrderTransactionController@create')->name('po.create');