Merge branch 'dillon/63.7-company-with-multiple-employees' into 'master'

Allow user account to see and use all vouchers that belongs to all employees...

See merge request CIEFWorldwideSdnBhd/exchange-2.0!176
This commit is contained in:
Dillon Ngo
2024-08-23 12:12:19 +00:00
17 changed files with 230 additions and 68 deletions
@@ -2,7 +2,6 @@
namespace App\Classes\General\Eloquent\Filters;
use App\Classes\ValueObjects\Constants\RoleTypes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
@@ -12,33 +11,17 @@ class HasActiveReward implements Filter
/**
* @param Builder $builder
* @param $value
* @return mixed
* @return Builder|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;
return $builder->where('user_id', $userId)
->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);
return $builder->where('user_id', Auth::user()->id) //cief todo: should not use Auth::user()->id
->where(function ($query) {
$query->whereHas('reward', function ($subquery) {
$subquery->where('is_active', true);
});
}
else{
return $builder->where('user_id', Auth::user()->id)
->where(function ($query) {
$query->whereHas('reward', function ($subquery) {
$subquery->where('is_active', true);
})
->orWhereDoesntHave('reward');
})
->whereDoesntHave('voucher.redemptions.transaction.owner');
}
// ->orWhereDoesntHave('reward');
});
}
}
@@ -0,0 +1,26 @@
<?php
namespace App\Classes\General\Eloquent\Filters;
use Illuminate\Database\Eloquent\Builder;
class HasActiveRewardForAdmin implements Filter
{
/**
* @param Builder $builder
* @param $value
* @return Builder|mixed
*/
public static function apply(Builder $builder, $value)
{
return $builder->where('user_id', $value)
->where(function ($query) {
$query->whereHas('reward', function ($subquery) {
$subquery->where('is_active', true);
});
// ->orWhereDoesntHave('reward');
});
}
}
@@ -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 HasVouchersAllWithCompany 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');
}
}
}
@@ -0,0 +1,44 @@
<?php
namespace App\Classes\General\Eloquent\Filters;
use App\Classes\ValueObjects\Constants\RoleTypes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
class HasVouchersAllWithUser 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;
return $builder->where('user_id', $userId)
->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{
return $builder->where('user_id', Auth::user()->id)
->where(function ($query) {
$query->whereHas('reward', function ($subquery) {
$subquery->where('is_active', true);
})
->orWhereDoesntHave('reward');
})
->whereDoesntHave('voucher.redemptions.transaction.owner');
}
}
}
@@ -1,20 +0,0 @@
<?php
namespace App\Classes\General\Eloquent\Filters;
use Illuminate\Database\Eloquent\Builder;
class RandomName implements Filter
{
/**
* @param Builder $builder
* @param $value
* @return Builder|mixed
*/
public static function apply(Builder $builder, $value)
{
return $builder->where('is_active', $value);
}
}
@@ -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,
@@ -11,6 +11,7 @@ use App\Classes\Modules\Vouchers\Services\Voucherify\ValidatesVoucherifyVoucher;
use App\Classes\Modules\Vouchers\Services\Voucherify\CreatesVoucherifyVoucherInACampaign;
use App\Classes\Modules\Vouchers\Services\Voucherify\ListsVoucherifyVouchers;
use App\Classes\Modules\Vouchers\Services\Voucherify\FetchesVoucherifyCampaign;
use App\Classes\Modules\Vouchers\Processors\Voucherify\NewCustomerToVoucherifyProcessor;
use App\Classes\Modules\Vouchers\Services\CreatesVoucher;
use App\Classes\Modules\Vouchers\Services\FetchesVoucher;
use App\Classes\Modules\Vouchers\Services\UpdatesVoucherCampaign;
@@ -75,6 +76,9 @@ class CreateVoucherLogic extends AbstractControllerLogic
/** @var UpdatesVoucherCampaign */
private $updatesVoucherCampaign;
/** @var NewCustomerToVoucherifyProcessor */
private $newCustomerToVoucherifyProcessor;
/**
* CreateVoucherLogic constructor.
* @param ValidatesVoucherifyVoucher $validatesVoucherifyVoucher
@@ -87,8 +91,9 @@ class CreateVoucherLogic extends AbstractControllerLogic
* @param CreatesKeyValuePair $createsKeyValuePair
* @param UpdatesKeyValuePair $updatesKeyValuePair
* @param UpdatesVoucherCampaign $updatesVoucherCampaign
* @param NewCustomerToVoucherifyProcessor $newCustomerToVoucherifyProcessor
*/
public function __construct(CreatesUserReward $createsUserReward, ValidatesVoucherifyVoucher $validatesVoucherifyVoucher, CreateVoucherProcessor $createVoucherProcessor, CreatesVoucherifyVoucherInACampaign $createsVoucherifyVoucherInACampaign, CanCreateVoucher $canCreateVoucher, ListsVoucherifyVouchers $listsVoucherifyVouchers, FetchesVoucherifyCampaign $fetchesVoucherifyCampaign, CreatesKeyValuePair $createsKeyValuePair, UpdatesKeyValuePair $updatesKeyValuePair, UpdatesVoucherCampaign $updatesVoucherCampaign)
public function __construct(CreatesUserReward $createsUserReward, ValidatesVoucherifyVoucher $validatesVoucherifyVoucher, CreateVoucherProcessor $createVoucherProcessor, CreatesVoucherifyVoucherInACampaign $createsVoucherifyVoucherInACampaign, CanCreateVoucher $canCreateVoucher, ListsVoucherifyVouchers $listsVoucherifyVouchers, FetchesVoucherifyCampaign $fetchesVoucherifyCampaign, CreatesKeyValuePair $createsKeyValuePair, UpdatesKeyValuePair $updatesKeyValuePair, UpdatesVoucherCampaign $updatesVoucherCampaign, NewCustomerToVoucherifyProcessor $newCustomerToVoucherifyProcessor)
{
$this->createsUserReward = $createsUserReward;
$this->validatesVoucherifyVoucher = $validatesVoucherifyVoucher;
@@ -100,6 +105,7 @@ class CreateVoucherLogic extends AbstractControllerLogic
$this->createsKeyValuePair = $createsKeyValuePair;
$this->updatesKeyValuePair = $updatesKeyValuePair;
$this->updatesVoucherCampaign = $updatesVoucherCampaign;
$this->newCustomerToVoucherifyProcessor = $newCustomerToVoucherifyProcessor;
}
/**
@@ -129,6 +135,12 @@ class CreateVoucherLogic extends AbstractControllerLogic
$user = $userParam ? $userParam : $user;
}
//Voucherify - To check if user exist at Voucherify, create if not exist
$voucherify_entity = $user->voucherifyEntities()->first();
if(!$voucherify_entity){
$this->newCustomerToVoucherifyProcessor->execute($user->company()->first()->id, $user, false);
}
//Voucherify - creates new voucher at voucherify
if ($voucherCodeInput === Vouchers::SORRY_50 || $voucherCodeInput === Vouchers::SORRY_100 || $voucherCodeInput === Vouchers::SORRY_200 ) {
$result = $this->newVoucherifyVoucherIssuanceHandler($voucherCodeInput);
@@ -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]);
}
@@ -65,9 +65,9 @@ class CreateVoucherifyCustomerObject implements DataTransferObject
*/
public function getAcquisitionChannel(): string
{
if(!$this->isNew){
return "";
}
// if(!$this->isNew){
// return "";
// }
return $this->acquisitionChannel;
}
@@ -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);
@@ -44,7 +44,9 @@ class NewCustomerToVoucherifyProcessor
$createVoucherifyCustomerObject = new CreateVoucherifyCustomerObject($companyId, $user, $isNew);
$result = $this->createsVoucherifyCustomer->execute($createVoucherifyCustomerObject);
if($result && isset($result->id)){
$voucherify_entity = $user->voucherifyEntities()->get();
if($result && isset($result->id) && count($voucherify_entity) === 0){
$voucherEntityObject = new VoucherEntityObject($result->id, VoucherifyEntityType::CUSTOMER);
$this->createsVoucherEntityMapping->execute($createVoucherifyCustomerObject->getUser(), $voucherEntityObject);
}
+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_active_reward")) {
if ($request->has('filters') && (str_contains($request->input('filters'), "has_vouchers_all_with_user") )) {
//|| str_contains($request->input('filters'), "has_vouchers_all_with_company")
$filteredRedemptions = new ArrayObject([]);
}
else{
@@ -95,7 +95,7 @@
fetchVouchers(){
this.isLoading = true;
if(this.employee){
this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_active_reward': this.employee.id} ), 'get', this.section, false, false);
this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_vouchers_all_with_company': this.employee.id} ), 'get', this.section, false, false);
}
},
successHandler(response){
@@ -76,7 +76,7 @@
fetchVouchers(){
this.isLoading = true;
if(this.employee){
this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_active_reward': this.employee.id} ), 'get', this.section, false, false);
this.submit(route('api.voucher.user.list') + '?filters=' + JSON.stringify( { 'has_vouchers_all_with_company': this.employee.id} ), 'get', this.section, false, false);
}
},
successHandler(response){
@@ -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_active_reward': id }">
<list-component section="customerVouchersListSection" :endpoint="route('api.voucher.user.list')" :options="{ 'has_vouchers_all_with_user': id }">
<template slot="list" slot-scope="{data}">
<single-user-reward-item-component :data="data"></single-user-reward-item-component>
</template>
@@ -74,10 +74,15 @@
<div class="col">
<div class="row">
<div class="col" v-if="this.$store.getters.getUserId">
<list-component section="customerRewardsListSection" :endpoint="route('api.reward.list.details.admin', id)" :options="{'is_active': true, order_by:{ column:'order', DESC:false}}">
<!-- <list-component section="customerRewardsListSection" :endpoint="route('api.reward.list.details.admin', id)" :options="{'is_active': true, order_by:{ column:'order', DESC:false}}">
<template slot="list" slot-scope="{data}">
<single-reward-details-item-component :data="data"></single-reward-details-item-component>
</template>
</list-component> -->
<list-component section="customerRewardsListSection" :endpoint="route('api.voucher.user.list')" :options="{'has_active_reward_for_admin': id}">
<template slot="list" slot-scope="{data}">
<single-user-reward-item-component :data="data"></single-user-reward-item-component>
</template>
</list-component>
</div>
</div>
@@ -19,7 +19,7 @@
</div>
</div>
</div>
<div class="col m-r-5 d-none d-md-inline">
<!-- <div class="col m-r-5 d-none d-md-inline">
<div class="row fs-12 text-center">
<div class="col p-t-20 p-b-20 bg-master-lighter tabButton" tab-name="rewards">
<div class="row">
@@ -29,7 +29,7 @@
</div>
</div>
</div>
</div>
</div> -->
<div class="col m-r-5 d-none d-md-inline">
<div class="row fs-12 text-center">
<div class="col p-t-20 p-b-20 bg-master-lighter tabButton" tab-name="used">
@@ -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_active_reward': true }">
<list-component section="customerVouchersListSection" :endpoint="route('api.voucher.user.list')" :options="{ 'has_vouchers_all_with_user': true }">
<template slot="list" slot-scope="{data}">
<single-user-reward-item-component :data="data"></single-user-reward-item-component>
</template>
@@ -69,11 +69,16 @@
<div class="col">
<div class="row">
<div class="col" v-if="this.$store.getters.getUserId">
<list-component section="customerRewardsListSection" :endpoint="route('api.reward.list.details')" :options="{'random_name': true, order_by:{ column:'order', DESC:false}}">
<!-- <list-component section="customerRewardsListSection" :endpoint="route('api.reward.list.details')" :options="{'is_active': true, order_by:{ column:'order', DESC:false}}">
<template slot="list" slot-scope="{data}">
<single-reward-details-item-component :data="data"></single-reward-details-item-component>
</template>
</list-component>
</list-component> -->
<!-- <list-component section="customerRewardsListSection" :endpoint="route('api.voucher.user.list')" :options="{'has_active_reward': true}">
<template slot="list" slot-scope="{data}">
<single-user-reward-item-component :data="data"></single-user-reward-item-component>
</template>
</list-component> -->
</div>
</div>
</div>