diff --git a/app/Console/Commands/LarkPushBooking2025TransactionsModuleCommand.php b/app/Console/Commands/LarkPushBooking2025TransactionsModuleCommand.php new file mode 100644 index 00000000..cff9b424 --- /dev/null +++ b/app/Console/Commands/LarkPushBooking2025TransactionsModuleCommand.php @@ -0,0 +1,123 @@ +error('[Lark ' . $module . '] Missing LARK_BOOKING1_WEBHOOK_ID'); + return self::FAILURE; + } + + $this->info('[Lark ' . $module . '] Start', ['env' => app()->environment()]); + + $limit = (int) $this->option('limit'); + $url = "https://hfbbr1an7s0.sg.larksuite.com/base/automation/webhook/event/{$webhookId}"; + $statusM = ApprovalStatus::APPROVAL_STATUS_ID; + $transactionTypeM = TransactionType::ID_TO_NAME; + + $bookingTransactions = BookingTransactions::query() + ->whereYear('transaction_created_at', '>=', 2025) + ->whereNotExists(function ($q) { + $b = new BookingTransactions; + $q->selectRaw(1) + ->from('lark_pushed') + ->whereColumn('lark_pushed.owner_id', 'booking_transactions_view.transaction_id') + ->where('lark_pushed.owner_type', $b->getMorphClass()) + ->where('lark_pushed.is_success', 1); + }) + ->limit($limit) + ->get(); + + if ($bookingTransactions->isEmpty()) { + $this->warn('[Lark ' . $module . '] Nothing to push.'); + return self::SUCCESS; + } + + $this->info("[Lark " . $module . "] Pushing {$bookingTransactions->count()} record(s)..."); + Log::info('[Lark ' . $module . '] Start', ['limit' => $limit, 'env' => app()->environment()]); + + foreach ($bookingTransactions as $b) { + $payload = [ + 'transaction_id' => $b->transaction_id, + 'transaction_type' => $transactionTypeM[$b->transaction_type], + 'transaction_status' => $statusM[$b->transaction_status], + 'transaction_bill_no' => $b->transaction_bill_no, + 'transaction_currency_rate' => $b->transaction_currency_rate, + 'transaction_service_charge' => $b->transaction_service_charge, + 'transaction_currency_code' => $b->transaction_currency_code, + 'transaction_amount' => $b->transaction_amount, + 'transaction_original_currency_code' => $b->transaction_original_currency_code, + 'transaction_original_amount' => $b->transaction_original_amount, + 'booking_id' => $b->booking_id, + 'booking_marking' => $b->booking_marking, + 'company_name' => $b->company_name, + 'company_reference' => $b->company_reference, + 'service_name' => $b->service_name, + 'currency_code' => $b->currency_code, + 'booking_amount' => $b->booking_amount, + 'transaction_created_at' => $b->transaction_created_at, + 'transaction_updated_at' => $b->transaction_updated_at, + 'need_e_invoice' => $b->need_e_invoice, + ]; + + try { + $res = Http::asJson()->post($url, $payload); + $ok = $res->successful() && (int) data_get($res->json(), 'code', -1) === 0; + + LarkPushed::create([ + 'owner_id' => $b->transaction_id, + 'owner_type' => $b->getMorphClass(), + 'is_success' => $ok ? 1 : 0, + ]); + + $trransactionTypeString = $transactionTypeM[$b->transaction_type]; + if ($ok) { + $this->info("[Lark $module]: ✅ Sent: {$b->transaction_id} | {$trransactionTypeString}"); + } else { + $this->error("[Lark $module] Failed {$b->transaction_id} | {$trransactionTypeString}: {$res->status()} {$res->body()}"); + Log::error('[Lark ' . $module . '] Push failed', [ + 'transaction_id' => $b->transaction_id, + 'status' => $res->status(), + 'body' => $res->body(), + ]); + } + } catch (\Throwable $e) { + LarkPushed::create([ + 'owner_id' => $b->transaction_id, + 'owner_type' => $b->getMorphClass(), + 'is_success' => 0, + ]); + $this->error("[Lark $module]: ❌ Exception {$b->id} | {$b->marking}: {$e->getMessage()}"); + Log::error('[Lark ' . $module . '] Exception', [ + 'transaction_id' => $b->transaction_id, + 'exception' => $e->getMessage(), + ]); + } + + // sleep(1); + usleep(100000); //0.1 second + } + + $this->info("[Lark " . $module . "] Finished {$bookingTransactions->count()} record(s)."); + Log::info('[Lark ' . $module . '] Finished', ['count' => $bookingTransactions->count()]); + + return self::SUCCESS; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 13761f5a..bae91157 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -109,6 +109,8 @@ class Kernel extends ConsoleKernel // // Push booking module to Lark // $schedule->command('lark:push-booking-transactions-module') // ->everyTenMinutes(); + $schedule->command('lark:push-booking-2025-transactions-module') + ->everyTenMinutes(); } /**