From 5186369d8a9b3a26f8b9376feecdd373e56c91f8 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Sun, 31 Dec 2023 04:46:15 +0800 Subject: [PATCH] Laravel Vapor - Code sync with Shipping Portal, Version 2 of Commands --- .env.example | 1 - ...ditAndUpdateWallletBalanceV2CommandJob.php | 73 +++++++++++++++++ .../DebugBillplzFailedPaymentV2CommandJob.php | 63 +++++++++++++++ .../V2/DeleteBulkInvoiceFilesV2CommandJob.php | 51 ++++++++++++ .../Commands/V2/DeleteOrderV2CommandJob.php | 81 +++++++++++++++++++ .../Commands/V2/EmailDoToVTV2CommandJob.php | 78 ++++++++++++++++++ ...moveSeasonalSegmentCompanyV2CommandJob.php | 52 ++++++++++++ .../DeleteOrderV2CommandObject.php | 26 ++++++ .../AuditAndUpdateWallletBalanceV2Command.php | 34 ++++++++ .../V2/DebugBillplzFailedPaymentV2Command.php | 44 ++++++++++ .../V2/DeleteBulkInvoiceFilesV2Command.php | 23 ++++++ .../Commands/V2/DeleteOrderV2Command.php | 47 +++++++++++ .../Commands/V2/EmailDoToVTV2Command.php | 45 +++++++++++ .../RemoveSeasonalSegmentCompanyV2Command.php | 34 ++++++++ app/Console/Kernel.php | 12 +++ 15 files changed, 663 insertions(+), 1 deletion(-) create mode 100644 app/Classes/Jobs/Commands/V2/AuditAndUpdateWallletBalanceV2CommandJob.php create mode 100644 app/Classes/Jobs/Commands/V2/DebugBillplzFailedPaymentV2CommandJob.php create mode 100644 app/Classes/Jobs/Commands/V2/DeleteBulkInvoiceFilesV2CommandJob.php create mode 100644 app/Classes/Jobs/Commands/V2/DeleteOrderV2CommandJob.php create mode 100644 app/Classes/Jobs/Commands/V2/EmailDoToVTV2CommandJob.php create mode 100644 app/Classes/Jobs/Commands/V2/RemoveSeasonalSegmentCompanyV2CommandJob.php create mode 100644 app/Classes/Modules/Jobs/DataTransferObjects/DeleteOrderV2CommandObject.php create mode 100644 app/Console/Commands/V2/AuditAndUpdateWallletBalanceV2Command.php create mode 100644 app/Console/Commands/V2/DebugBillplzFailedPaymentV2Command.php create mode 100644 app/Console/Commands/V2/DeleteBulkInvoiceFilesV2Command.php create mode 100644 app/Console/Commands/V2/DeleteOrderV2Command.php create mode 100644 app/Console/Commands/V2/EmailDoToVTV2Command.php create mode 100644 app/Console/Commands/V2/RemoveSeasonalSegmentCompanyV2Command.php diff --git a/.env.example b/.env.example index bf7407de..235fbc41 100644 --- a/.env.example +++ b/.env.example @@ -69,6 +69,5 @@ VOUCHERIFY_CLIENT_SECRET_KEY="" VOUCHERIFY_VERSION="v2018-08-01" VOUCHERIFY_URL="https://as1.api.voucherify.io" - LARAVEL_VAPOR_ENABLED=false COMMANDS_V2_ENABLED=false diff --git a/app/Classes/Jobs/Commands/V2/AuditAndUpdateWallletBalanceV2CommandJob.php b/app/Classes/Jobs/Commands/V2/AuditAndUpdateWallletBalanceV2CommandJob.php new file mode 100644 index 00000000..12a2af44 --- /dev/null +++ b/app/Classes/Jobs/Commands/V2/AuditAndUpdateWallletBalanceV2CommandJob.php @@ -0,0 +1,73 @@ +removesCompanyFromSegment = $removesCompanyFromSegment; + } + + + public function handle() + { + Log::info(Carbon::now() . ': Start job - Audit and Update Wallet Balance.'); + $start = new Carbon(); + + $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++; + + Log::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]); + } + + $end = new Carbon(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + Log::info(Carbon::now() . ': End job - Audit and Update Wallet Balance. ElapsedTime: ' . $elapsedTime . '.'); + } +} diff --git a/app/Classes/Jobs/Commands/V2/DebugBillplzFailedPaymentV2CommandJob.php b/app/Classes/Jobs/Commands/V2/DebugBillplzFailedPaymentV2CommandJob.php new file mode 100644 index 00000000..7e626fd6 --- /dev/null +++ b/app/Classes/Jobs/Commands/V2/DebugBillplzFailedPaymentV2CommandJob.php @@ -0,0 +1,63 @@ +where('payment_method', PaymentMethodType::PAYMENT_GATEWAY)->whereNotIn('status', [ApprovalStatus::COMPLETED, ApprovalStatus::APPROVED])->get(); + + $i = 0; + $totalAmount = 0; + foreach ($transactions as $transaction){ + $response = Http::withBasicAuth(config('billplz.api_key').':', '')->get(config('billplz.base_url').'/api/v3/bills/'.$transaction->payment_reference); + + // dd($response); + + if($response->successful()){ + $data = $response->json(); + if($data['paid']){ + if (!in_array($transaction->status, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])) { + $this->returnLog('Paid transaction', $transaction); + } + } + // else { + // $totalAmount += $transaction->amount; + // $this->returnLog('Unpaid Transaction', $transaction); + // } + }else{ + $this->returnLog('billplz error', $transaction); + } + } + + $end = new Carbon(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + Log::info(Carbon::now() . ': End job - Debug Billplz Failed Payment. ElapsedTime: ' . $elapsedTime . '.'); + } + + public function returnLog($text, $transaction) { + $approvalStatusArray = ApprovalStatus::APPROVAL_STATUS_ID; + Log::info($text . ' - id: '. $transaction->id . '. Booking Marking: '. $transaction->owner->marking . ' - Date: '.$transaction->created_at->format('d-m-Y').' - Amount: '. $transaction->amount . '. Current Status: ' . $approvalStatusArray[$transaction->status]); + } +} diff --git a/app/Classes/Jobs/Commands/V2/DeleteBulkInvoiceFilesV2CommandJob.php b/app/Classes/Jobs/Commands/V2/DeleteBulkInvoiceFilesV2CommandJob.php new file mode 100644 index 00000000..3b6815d5 --- /dev/null +++ b/app/Classes/Jobs/Commands/V2/DeleteBulkInvoiceFilesV2CommandJob.php @@ -0,0 +1,51 @@ +diff($end)->format('%H:%I:%S'); + + Log::info(Carbon::now() . ' Process ended. ElapsedTime: ' . $elapsedTime); + } + + $end = new Carbon(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + Log::info(Carbon::now() . ': End job - Delete all the bulk download files. ElapsedTime: ' . $elapsedTime . '.'); + } +} diff --git a/app/Classes/Jobs/Commands/V2/DeleteOrderV2CommandJob.php b/app/Classes/Jobs/Commands/V2/DeleteOrderV2CommandJob.php new file mode 100644 index 00000000..a5489eec --- /dev/null +++ b/app/Classes/Jobs/Commands/V2/DeleteOrderV2CommandJob.php @@ -0,0 +1,81 @@ +deleteOrderV2CommandObject = $deleteOrderV2CommandObject; + } + + + public function handle() + { + Log::info(Carbon::now() . ': Start job - Change refunded booking status to expired.'); + $start = new Carbon(); + + $bookings_reference = $this->deleteOrderV2CommandObject->getBookingsReference(); + // $bookings_reference = '28546,38599,44487,71086,70133,58580,42831,96028,41188,33894,95877,86732,31894,50962,44215,92894,40968,30303,89762,74693,45271,27169'; + $bookings_reference = explode(',', $bookings_reference); + + + foreach ($bookings_reference as $reference) { + $booking = Booking::where('marking', $reference)->first(); + + if (!$booking) { + $this->logOutput('Booking not found: ' . $reference); + } else { + $payment = $booking->transactions() + ->payments()->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) + ->first(); + + $payment->status = ApprovalStatus::EXPIRED; + $payment->save(); + + $this->logOutput('Booking ' . $reference . ' Payment deleted: ' . $payment->id); + } + } + + + + $end = new Carbon(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + Log::info(Carbon::now() . ': End job - Change refunded booking status to expired. ElapsedTime: ' . $elapsedTime . '.'); + $this->logOutput('Process ended. ElapsedTime: ' . $elapsedTime); + } + + public function logOutput($text) //cief todo: what is the purpose of this? + { + if (is_array($text)) { + $text = implode(', ', $text); + } + + Log::info(Carbon::now() . ' : ' . $text); + + $filePath = storage_path('logs/delete-orders.log'); + $textToAppend = Carbon::now()->format('[Y-m-d H:i:s]') . ' ' . $text . PHP_EOL; + file_put_contents($filePath, $textToAppend, FILE_APPEND); + } +} diff --git a/app/Classes/Jobs/Commands/V2/EmailDoToVTV2CommandJob.php b/app/Classes/Jobs/Commands/V2/EmailDoToVTV2CommandJob.php new file mode 100644 index 00000000..555f1f56 --- /dev/null +++ b/app/Classes/Jobs/Commands/V2/EmailDoToVTV2CommandJob.php @@ -0,0 +1,78 @@ +format('Y_m_d').'.zip'; + $attachment = storage_path().'/'.$zip_file; + + $startDate = Carbon::yesterday(); + $endDate = Carbon::yesterday(); + + $zip = new ZipArchive(); + if ($zip->open($attachment, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { + + $bookings = \App\Models\Booking::where('status', ApprovalStatus::COMPLETED)->whereDate('updated_at', '>=', $startDate)->whereDate('updated_at', '<=', $endDate) + ->whereHas('transactions', function ($query){ + return $query->where('type', TransactionType::PAYMENT)->whereHas('transactions', function ($query){ + return $query->where('issuer', 2); + }); + })->get(); + + if(!count($bookings)){ return; } + + foreach ($bookings as $booking) { + $file = $booking->documents()->where('document_type', DocumentType::SUPPLIER_DELIVER_ORDER)->first()->files()->first(); + $zip->addFile(Storage::disk('documents')->path($file->file->file_info->original->file), $booking->created_at->format('d_m_Y').'_'.$booking->marking.'.pdf'); + + } + + $zip->close(); + + Mail::raw( "Attention to VT Admin team:\r\n\r\nKindly refer to the attachment for our DAILY DO COMPILATION ".$startDate->format('d-m-Y')." - ".$endDate->format('d-m-Y').".\r\n\r\n**This is an automatically generated email – please do not reply to it. If you have any queries kindly contact our admin team through Wechat.\r\n\r\n\r\nCIEF WORLDWIDE SDN BHD", function($message) use ($attachment, $startDate, $endDate){ + $message->from('exchange@cief-malaysia.com'); + $message->to(['vtnation16@gmail.com', 'vtnation@gmail.com', 'atvantic04@gmail.com', 'vtnation2@gmail.com']); + $message->cc(['shafiqa_sukeri@cief-malaysia.com', 'frontendcief@gmail.com', 'pm@cief-malaysia.com', 'hasan@cief-malaysia.com', 'shipping_admin@cief-malaysia.com', 'hasanakbar27@gmail.com', 'pmwong2019@gmail.com', 'uldvstar@gmail.com']); + $message->subject('CIEF DO COMPILATION '.$startDate->format('d-m-Y').' - '.$endDate->format('d-m-Y')); + + $message->attach($attachment); + }); + + File::delete($attachment); + Log::info('success'); + + } + + $end = new Carbon(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + Log::info(Carbon::now() . ': End job - Send do to vt nation. ElapsedTime: ' . $elapsedTime . '.'); + } +} diff --git a/app/Classes/Jobs/Commands/V2/RemoveSeasonalSegmentCompanyV2CommandJob.php b/app/Classes/Jobs/Commands/V2/RemoveSeasonalSegmentCompanyV2CommandJob.php new file mode 100644 index 00000000..dcd9c8bf --- /dev/null +++ b/app/Classes/Jobs/Commands/V2/RemoveSeasonalSegmentCompanyV2CommandJob.php @@ -0,0 +1,52 @@ +removesCompanyFromSegment = $removesCompanyFromSegment; + } + + + public function handle() + { + Log::info(Carbon::now() . ': Start job - Perform soft delete on Seasonal Segment Company.'); + $start = new Carbon(); + + $seasonalSegment = SeasonalSegment::where('ending_on', '<=', Carbon::today())->get(); + + if (count($seasonalSegment)){ + foreach ($seasonalSegment as $seasonalSegmentCompany) { + $this->removesCompanyFromSegment->execute($seasonalSegmentCompany->company, $seasonalSegmentCompany->segment); + $seasonalSegmentCompany->delete(); + Log::info(Carbon::now() . ' : Deleted id: ' . $seasonalSegmentCompany->id); + } + } + + $end = new Carbon(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + Log::info(Carbon::now() . ': End job - Perform soft delete on Seasonal Segment Company. ElapsedTime: ' . $elapsedTime . '.'); + } +} diff --git a/app/Classes/Modules/Jobs/DataTransferObjects/DeleteOrderV2CommandObject.php b/app/Classes/Modules/Jobs/DataTransferObjects/DeleteOrderV2CommandObject.php new file mode 100644 index 00000000..3f385686 --- /dev/null +++ b/app/Classes/Modules/Jobs/DataTransferObjects/DeleteOrderV2CommandObject.php @@ -0,0 +1,26 @@ +bookingsReference = $bookingsReference; + } + + /** + * @return string + */ + public function getBookingsReference(): string + { + return $this->bookingsReference; + } + +} diff --git a/app/Console/Commands/V2/AuditAndUpdateWallletBalanceV2Command.php b/app/Console/Commands/V2/AuditAndUpdateWallletBalanceV2Command.php new file mode 100644 index 00000000..1bcfdc59 --- /dev/null +++ b/app/Console/Commands/V2/AuditAndUpdateWallletBalanceV2Command.php @@ -0,0 +1,34 @@ +argument('bookings_reference') + ); + DeleteOrderV2CommandJob::dispatch($deleteOrderV2CommandObject); + } +} diff --git a/app/Console/Commands/V2/EmailDoToVTV2Command.php b/app/Console/Commands/V2/EmailDoToVTV2Command.php new file mode 100644 index 00000000..8ea1710d --- /dev/null +++ b/app/Console/Commands/V2/EmailDoToVTV2Command.php @@ -0,0 +1,45 @@ +command('dummy-command') ->everyFiveMinutes() ->withoutOverlapping(); + + $schedule->command('email-do-to-vt-command') + ->dailyAt('10:00') + ->withoutOverlapping(); + + $schedule->command('seasonal-segmant-company-remove-command') + ->dailyAt('01:00') + ->withoutOverlapping(); + + $schedule->command('delete-bulk-download-files-command') + ->hourly() + ->withoutOverlapping(); } //Commands Version 1: Before Laravel Vapor/AWS else{