diff --git a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php index a6c0b7fd..186ef3b6 100644 --- a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php +++ b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php @@ -25,6 +25,7 @@ use App\Classes\Modules\Transactions\Services\FetchesTransaction; use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; +use App\Classes\Modules\Wallets\Services\RecalculatesWalletBalance; class CallbackBillplzLogic { @@ -45,20 +46,25 @@ class CallbackBillplzLogic /** @var CreateCashBackTransactionProcessor */ private $createCashBackTransactionProcessor; + /** @var RecalculatesWalletBalance */ + private $recalculatesWalletBalance; + /** * CallbackBillplzLogic constructor. * @param GetBillplzBill $getBillplzBill * @param FetchesTransaction $fetchesTransaction * @param UpdatesTransactionStatus $updatesTransactionStatus * @param UpdatesWalletBalance $updatesWalletBalance + * @param RecalculatesWalletBalance $recalculatesWalletBalance */ - public function __construct(GetBillplzBill $getBillplzBill, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, UpdatesWalletBalance $updatesWalletBalance, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor) + public function __construct(GetBillplzBill $getBillplzBill, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, UpdatesWalletBalance $updatesWalletBalance, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor, RecalculatesWalletBalance $recalculatesWalletBalance) { $this->getBillplzBill = $getBillplzBill; $this->fetchesTransaction = $fetchesTransaction; $this->updatesTransactionStatus = $updatesTransactionStatus; $this->updatesWalletBalance = $updatesWalletBalance; $this->createCashBackTransactionProcessor = $createCashBackTransactionProcessor; + $this->recalculatesWalletBalance = $recalculatesWalletBalance; } @@ -91,13 +97,12 @@ class CallbackBillplzLogic $status = $billplzXSignatureObject->getStatus() === 'failed' ? ApprovalStatus::REJECTED : ApprovalStatus::PENDING_VERIFICATION; } - if($transaction->status !== ApprovalStatus::COMPLETED){ - - if($transaction->owner instanceof Wallet && $transaction->status !== ApprovalStatus::APPROVED && $status === ApprovalStatus::APPROVED) { - $this->updatesWalletBalance->execute($transaction->owner, $transaction->amount); - } + if($transaction->status !== ApprovalStatus::COMPLETED && $transaction->status !== ApprovalStatus::APPROVED){ $this->updatesTransactionStatus->execute($transaction, $status); + } + if($transaction->owner instanceof Wallet) { + $this->updatesWalletBalance->execute($transaction->owner, $transaction->amount); } // if ($transaction->type == TransactionType::PAYMENT) { diff --git a/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php b/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php index 1bed083c..a2947e82 100644 --- a/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php +++ b/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php @@ -4,6 +4,7 @@ namespace App\Classes\Modules\Billplzs\Services; use Illuminate\Support\Facades\Http; use App\Classes\Exceptions\MalformedRequestException; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Log; class CreatesBillplzBill @@ -26,7 +27,7 @@ class CreatesBillplzBill $response = Http::withBasicAuth(config('billplz.api_key').':', '')->post(config('billplz.base_url').'/api/v3/bills', [ 'collection_id' => $wallet ? config('billplz.wallet_collection_id') : config('billplz.collection_id'), 'name' => $name, - 'email' => $email, + 'email' => App::environment('production') ? $email : 'development@cief-malaysia.com', 'description' => $description, 'amount' => $this->finalizeAmount($amount), 'redirect_url' => route('online_payment.redirect'), diff --git a/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php index 1983952e..bc837878 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php @@ -65,7 +65,7 @@ class AutoPurchaseOrderFillLogic extends AbstractControllerLogic public function logic(Request $request) : JsonResponse { $bookings = Booking::where('service_id', '!=', 4)->where(function($query){ - return $query->whereMonth('created_at', '>', 07)->whereYear('created_at', 2022); + return $query->whereMonth('created_at', '<', 03)->whereYear('created_at', 2023); })->whereDoesntHave('transactions', function($q){ $q->where('type', TransactionType::PURCHASE_ORDER); $q->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED]); diff --git a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php index 1140ecd9..77d8252d 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php @@ -29,6 +29,7 @@ use App\Models\Wallet; use Carbon\Carbon; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use App\Classes\Modules\Wallets\Services\RecalculatesWalletBalance; class CreateBookingPaymentLogic extends AbstractControllerLogic { @@ -70,6 +71,9 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic /** @var CreateCashBackTransactionProcessor */ private $createCashBackTransactionProcessor; + /** @var RecalculatesWalletBalance */ + private $recalculatesWalletBalance; + /** * CreateBookingPaymentLogic constructor. * @param FetchesBookingQuotation $fetchBookingQuotation @@ -81,8 +85,9 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic * @param UpdatesWalletBalance $updatesWalletBalance * @param UpdatesTransactionStatus $updatesTransactionStatus * @param CreateCashBackTransactionProcessor $createCashBackTransactionProcessor + * @param RecalculatesWalletBalance $recalculatesWalletBalance */ - public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor) + public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor, RecalculatesWalletBalance $recalculatesWalletBalance) { $this->fetchBookingQuotation = $fetchBookingQuotation; $this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit; @@ -93,6 +98,7 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic $this->updatesWalletBalance = $updatesWalletBalance; $this->updatesTransactionStatus = $updatesTransactionStatus; $this->createCashBackTransactionProcessor = $createCashBackTransactionProcessor; + $this->recalculatesWalletBalance = $recalculatesWalletBalance; } /** @@ -138,7 +144,8 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic $paymentReference = $billNumber; - $this->updatesWalletBalance->execute($wallet, ($amount * -1)); + $walletBalance = $this->recalculatesWalletBalance->execute($wallet); + $this->updatesWalletBalance->execute($wallet, $walletBalance); } $billNumber = $this->generatesTransactionBillNumber->execute('PYMT-'); diff --git a/app/Classes/Modules/Imports/Services/GenericImport.php b/app/Classes/Modules/Imports/Services/GenericImport.php new file mode 100644 index 00000000..b2ffdb8f --- /dev/null +++ b/app/Classes/Modules/Imports/Services/GenericImport.php @@ -0,0 +1,23 @@ +rows = $collection; + } +} diff --git a/app/Classes/Modules/Wallets/Services/RecalculatesWalletBalance.php b/app/Classes/Modules/Wallets/Services/RecalculatesWalletBalance.php new file mode 100644 index 00000000..651bee4a --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/RecalculatesWalletBalance.php @@ -0,0 +1,48 @@ +updatesWalletBalance = $updatesWalletBalance; + } + /** + * @param Wallet $model + * @param $amount + * @return \Illuminate\Database\Eloquent\Model + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function execute(Wallet $wallet) + { + + $i = 0; + $topups = 0; + $credit = 0; + $payments = 0; + $debit = 0; + + foreach ($wallet->transactions as $transaction) { + if (!in_array((int) $transaction->status, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])) continue; + if ((int) $transaction->type === TransactionType::TOP_UP) { + $topups += (float) $transaction->amount; + } + if ((int) $transaction->type === TransactionType::CREDIT_NOTE) $credit += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::PAYMENT) $payments += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::DEBIT_NOTE) $debit += (float) $transaction->amount; + } + $auditBalance = ($topups + $credit) - ($payments + $debit); + + return $auditBalance; + } +} diff --git a/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php index a724cf24..4812660a 100644 --- a/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php +++ b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php @@ -3,10 +3,9 @@ namespace App\Classes\Modules\Wallets\Services; use App\Classes\General\Eloquent\AbstractUpdateRecord; -use App\Classes\General\Eloquent\AbstractUpdateRelationshipRecord; -use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; +use App\Classes\ValueObjects\Constants\ApprovalStatus; +use App\Classes\ValueObjects\Constants\TransactionType; use App\Models\Wallet; -use App\Models\Company; class UpdatesWalletBalance extends AbstractUpdateRecord { @@ -16,10 +15,24 @@ class UpdatesWalletBalance extends AbstractUpdateRecord * @return \Illuminate\Database\Eloquent\Model * @throws \App\Classes\Exceptions\MalformedRequestException */ - public function execute(Wallet $model, $amount) { + public function execute(Wallet $model, $amount) + { + $i = 0; + $topups = 0; + $credit = 0; + $payments = 0; + $debit = 0; - $model->amount = $model->amount + $amount; + foreach ($model->transactions as $transaction) { + if (!in_array((int) $transaction->status, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])) continue; + if ((int) $transaction->type === TransactionType::TOP_UP) $topups += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::CREDIT_NOTE) $credit += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::PAYMENT) $payments += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::DEBIT_NOTE) $debit += (float) $transaction->amount; + } + $auditBalance = ($topups + $credit) - ($payments + $debit); + + $model->amount = $auditBalance; return $this->handler($model); - } } diff --git a/app/Console/Commands/AuditAndUpdateWallletBalance.php b/app/Console/Commands/AuditAndUpdateWallletBalance.php new file mode 100644 index 00000000..0dd1be05 --- /dev/null +++ b/app/Console/Commands/AuditAndUpdateWallletBalance.php @@ -0,0 +1,79 @@ +removesCompanyFromSegment = $removesCompanyFromSegment; + } + + /** + * Execute the console command. + * + * @return int + */ + public function handle() + { + $wallets = Wallet::all(); + + $i = 0; + foreach ($wallets as $wallet) { + $topups = 0; + $credit = 0; + $payments = 0; + $debit = 0; + + foreach ($wallet->transactions as $transaction) { + if (!in_array((int) $transaction->status, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])) continue; + if ((int) $transaction->type === TransactionType::TOP_UP) { + $topups += (float) $transaction->amount; + } + if ((int) $transaction->type === TransactionType::CREDIT_NOTE) $credit += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::PAYMENT) $payments += (float) $transaction->amount; + if ((int) $transaction->type === TransactionType::DEBIT_NOTE) $debit += (float) $transaction->amount; + } + $auditBalance = ($topups + $credit) - ($payments + $debit); + $diffenrence = round((float) $wallet->amount - (($topups + $credit) - ($payments + $debit)), 2); + if ((($diffenrence == 0) || ($diffenrence == -0)) and $wallet->amount > -0.01) continue; + + $i++; + + $this->info($i . ". Marking: " . $wallet->owner->reference . "(" . $wallet->id . ")" . PHP_EOL . "Current Balance: " . $wallet->amount . PHP_EOL . "Audit Balance: " . ($auditBalance) . PHP_EOL . "Difference: " . $diffenrence . PHP_EOL); + + Wallet::where('id', $wallet->id)->update(['amount' => $auditBalance]); + } + } +} diff --git a/app/Http/Controllers/Imports/ImportHoneyTrapController.php b/app/Http/Controllers/Imports/ImportHoneyTrapController.php new file mode 100644 index 00000000..81ef237b --- /dev/null +++ b/app/Http/Controllers/Imports/ImportHoneyTrapController.php @@ -0,0 +1,116 @@ +input('files'), '', ApprovalStatus::APPROVED, 'imports'); + $file = json_decode($object->getFiles()[0])->file_info->original->file; + + $import = new GenericImport(); + Excel::import($import, $file); + $excelRows = $import->rows; + $excelRows = $excelRows->toArray(); + + $returnArray = []; + + $segment_id = Segment::where('name', 'honey trap platinum')->first()->id; + + foreach ($excelRows as $row) { + + if (is_null($row['email']) || empty($row['email'])) { + continue; + } + + if (is_null($row['end_date']) || empty($row['end_date'])) { + // use csv import must have an end_date + $row['status'] = 'failed'; + $row['message'] = 'End Date is required'; + $returnArray[] = $row; + continue; + } + + $row['end_date'] = $end_date = $this->changeExcelDate($row['end_date']); + $start_date = $row['start_date'] ? $this->changeExcelDate($row['start_date']) : Carbon::now(); + $row['start_date'] = $start_date; + + $end_date = Carbon::parse($end_date); + // Check if the end_date is in the past + if ($end_date->isPast()) { + // end_date must be after today's date + $row['status'] = 'failed'; + $row['message'] = "End Date must be after today's date"; + $returnArray[] = $row; + continue; + } + + $user = User::where('email', $row['email'])->first(); + if (!$user) { + // if user not found + $row['status'] = 'failed'; + $row['message'] = 'Email not found'; + $returnArray[] = $row; + continue; + } + + $company = $user->company->first(); + if (!$company) { + // if company not found + $row['status'] = 'failed'; + $row['message'] = 'Company not found'; + $returnArray[] = $row; + continue; + } + + $company_seasonal_honey_trap_count = SeasonalSegment::where('company_id', $company->id)->where('segment_id', $segment_id)->get(); + if (count($company_seasonal_honey_trap_count)) { + // seasonal segment already exists + $row['status'] = 'failed'; + $row['message'] = 'Company is already in the Honey Trap Segment'; + $returnArray[] = $row; + continue; + } + + $seasonalSegmentObject = new SeasonalSegmentObject($company->id, $segment_id, $start_date, $end_date ?? null); + + (App()->make(createsSeasonalSegment::class))->execute($seasonalSegmentObject); + (App()->make(assignSegmentProcessor::class))->execute($company, $segment_id); + + // success added honey trap seasonal segment + $row['status'] = 'success'; + $row['message'] = ''; + $returnArray[] = $row; + continue; + } + + return response()->json($returnArray); + } + + public function changeExcelDate($date) + { + $unixTime = (($date - 25569) * 86400); + $date = new DateTime("@$unixTime"); + return $date->format('d/m/Y'); + } +} diff --git a/resources/assets/vue/components/companies/elements/UploadHoneyTrapCsvComponent.vue b/resources/assets/vue/components/companies/elements/UploadHoneyTrapCsvComponent.vue new file mode 100644 index 00000000..6b3a6569 --- /dev/null +++ b/resources/assets/vue/components/companies/elements/UploadHoneyTrapCsvComponent.vue @@ -0,0 +1,94 @@ + + + \ No newline at end of file diff --git a/resources/views/pages/honey_trap.blade.php b/resources/views/pages/honey_trap.blade.php new file mode 100644 index 00000000..f0de7afe --- /dev/null +++ b/resources/views/pages/honey_trap.blade.php @@ -0,0 +1,12 @@ +@extends('layouts.base_portal') +@section('inner_content') +
+
+
+
+ +
+
+
+
+@endsection \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 6c17a107..b3e0002c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -29,6 +29,7 @@ Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function Route::get('/storage/{fileName}/fetch', 'Documents\RenderDocumentController@fileStorageServe')->where(['fileName' => '.*'])->name('storage.document.file'); Route::post('/import/update-debtor/f614e339d7058904a831aad742e24d55', 'Imports\ImportUpdateDebtorController@import')->name('debtor.import'); + Route::post('/import/upload-honey-trap', 'Imports\ImportHoneyTrapController@import')->name('honey_trap.upload'); require __DIR__ . '/company.php'; diff --git a/routes/web.php b/routes/web.php index 1e9c3d00..2547d087 100644 --- a/routes/web.php +++ b/routes/web.php @@ -598,3 +598,6 @@ Route::get('/po/outsource/check', function(){ }); +Route::get('/upload-honey-trap', function () { + return view('pages.honey_trap'); +})->name('upload_honey_trap'); \ No newline at end of file