diff --git a/app/Classes/General/Eloquent/Filters/HasActiveReward.php b/app/Classes/General/Eloquent/Filters/HasActiveReward.php index 5770ca6e..26385b96 100644 --- a/app/Classes/General/Eloquent/Filters/HasActiveReward.php +++ b/app/Classes/General/Eloquent/Filters/HasActiveReward.php @@ -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'); + }); } + } diff --git a/app/Classes/General/Eloquent/Filters/HasActiveRewardForAdmin.php b/app/Classes/General/Eloquent/Filters/HasActiveRewardForAdmin.php new file mode 100644 index 00000000..5d69aa4b --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/HasActiveRewardForAdmin.php @@ -0,0 +1,26 @@ +where('user_id', $value) + ->where(function ($query) { + $query->whereHas('reward', function ($subquery) { + $subquery->where('is_active', true); + }); + // ->orWhereDoesntHave('reward'); + }); + } + +} diff --git a/app/Classes/General/Eloquent/Filters/HasVouchersAllWithCompany.php b/app/Classes/General/Eloquent/Filters/HasVouchersAllWithCompany.php new file mode 100644 index 00000000..be4d335b --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/HasVouchersAllWithCompany.php @@ -0,0 +1,53 @@ +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'); + } + } +} diff --git a/app/Classes/General/Eloquent/Filters/HasVouchersAllWithUser.php b/app/Classes/General/Eloquent/Filters/HasVouchersAllWithUser.php new file mode 100644 index 00000000..79a02914 --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/HasVouchersAllWithUser.php @@ -0,0 +1,44 @@ +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'); + } + } +} diff --git a/app/Classes/General/Eloquent/Filters/RandomName.php b/app/Classes/General/Eloquent/Filters/RandomName.php deleted file mode 100644 index 54024e96..00000000 --- a/app/Classes/General/Eloquent/Filters/RandomName.php +++ /dev/null @@ -1,20 +0,0 @@ -where('is_active', $value); - } - -} diff --git a/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php b/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php index e5535e2a..feafba19 100644 --- a/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php +++ b/app/Classes/Modules/Bookings/Services/FetchesBookingQuotation.php @@ -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, diff --git a/app/Classes/Modules/Vouchers/ControllersLogic/CreateVoucherLogic.php b/app/Classes/Modules/Vouchers/ControllersLogic/CreateVoucherLogic.php index 5c85895a..e345ebcf 100644 --- a/app/Classes/Modules/Vouchers/ControllersLogic/CreateVoucherLogic.php +++ b/app/Classes/Modules/Vouchers/ControllersLogic/CreateVoucherLogic.php @@ -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); diff --git a/app/Classes/Modules/Vouchers/ControllersLogic/ValidateVoucherLogic.php b/app/Classes/Modules/Vouchers/ControllersLogic/ValidateVoucherLogic.php index 30e7fe12..4530292f 100644 --- a/app/Classes/Modules/Vouchers/ControllersLogic/ValidateVoucherLogic.php +++ b/app/Classes/Modules/Vouchers/ControllersLogic/ValidateVoucherLogic.php @@ -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]); } diff --git a/app/Classes/Modules/Vouchers/DataTransferObjects/CreateVoucherifyCustomerObject.php b/app/Classes/Modules/Vouchers/DataTransferObjects/CreateVoucherifyCustomerObject.php index 9678dafe..e61d1d00 100644 --- a/app/Classes/Modules/Vouchers/DataTransferObjects/CreateVoucherifyCustomerObject.php +++ b/app/Classes/Modules/Vouchers/DataTransferObjects/CreateVoucherifyCustomerObject.php @@ -65,9 +65,9 @@ class CreateVoucherifyCustomerObject implements DataTransferObject */ public function getAcquisitionChannel(): string { - if(!$this->isNew){ - return ""; - } + // if(!$this->isNew){ + // return ""; + // } return $this->acquisitionChannel; } diff --git a/app/Classes/Modules/Vouchers/DataTransferObjects/ValidateVoucherifyVoucherObject.php b/app/Classes/Modules/Vouchers/DataTransferObjects/ValidateVoucherifyVoucherObject.php index b48bee16..956afc00 100644 --- a/app/Classes/Modules/Vouchers/DataTransferObjects/ValidateVoucherifyVoucherObject.php +++ b/app/Classes/Modules/Vouchers/DataTransferObjects/ValidateVoucherifyVoucherObject.php @@ -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. diff --git a/app/Classes/Modules/Vouchers/Processors/Voucherify/BookingToVoucherifyProcessor.php b/app/Classes/Modules/Vouchers/Processors/Voucherify/BookingToVoucherifyProcessor.php index 6f89916b..90fba55a 100644 --- a/app/Classes/Modules/Vouchers/Processors/Voucherify/BookingToVoucherifyProcessor.php +++ b/app/Classes/Modules/Vouchers/Processors/Voucherify/BookingToVoucherifyProcessor.php @@ -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); diff --git a/app/Classes/Modules/Vouchers/Processors/Voucherify/NewCustomerToVoucherifyProcessor.php b/app/Classes/Modules/Vouchers/Processors/Voucherify/NewCustomerToVoucherifyProcessor.php index 006bcd94..3fd2ccee 100644 --- a/app/Classes/Modules/Vouchers/Processors/Voucherify/NewCustomerToVoucherifyProcessor.php +++ b/app/Classes/Modules/Vouchers/Processors/Voucherify/NewCustomerToVoucherifyProcessor.php @@ -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); } diff --git a/app/Http/Resources/VoucherResource.php b/app/Http/Resources/VoucherResource.php index 3f3112bb..55cc2b56 100644 --- a/app/Http/Resources/VoucherResource.php +++ b/app/Http/Resources/VoucherResource.php @@ -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{ diff --git a/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue b/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue index a5fb4e3b..0d52ff13 100644 --- a/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue +++ b/resources/assets/vue/components/bookings/elements/AvailableVouchersComponent.vue @@ -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){ diff --git a/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue b/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue index e55e5151..cc8afbf2 100644 --- a/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue +++ b/resources/assets/vue/components/bookings/elements/ListVouchersComponent.vue @@ -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){ diff --git a/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue b/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue index f28c88dd..d41c2b63 100644 --- a/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue +++ b/resources/assets/vue/components/companies/sections/CustomerRewardsAdminSectionComponent.vue @@ -57,7 +57,7 @@
- + @@ -74,10 +74,15 @@
- + + +
diff --git a/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue b/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue index 83567aaf..f6143340 100644 --- a/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue +++ b/resources/assets/vue/components/companies/sections/CustomerRewardsSectionComponent.vue @@ -19,7 +19,7 @@
-
+
@@ -52,7 +52,7 @@
- + @@ -69,11 +69,16 @@
- + +