-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathProvisionLedgerDefaults.php
More file actions
148 lines (127 loc) · 5.88 KB
/
Copy pathProvisionLedgerDefaults.php
File metadata and controls
148 lines (127 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace Fleetbase\Ledger\Console\Commands;
use Fleetbase\Ledger\Seeders\LedgerSeeder;
use Fleetbase\Ledger\Services\WalletService;
use Fleetbase\Models\Company;
use Fleetbase\Models\User;
use Illuminate\Console\Command;
/**
* ProvisionLedgerDefaults.
*
* Backfills default chart-of-accounts and system wallets for all companies
* (or a specific one) that were registered before automatic provisioning was
* enabled, and provisions personal wallets for all existing driver/customer
* users. Safe to run multiple times — all operations are idempotent.
*
* Usage:
* php artisan ledger:provision # all companies + all users
* php artisan ledger:provision --company=<uuid> # one company + its users
* php artisan ledger:provision --accounts-only # skip wallets
* php artisan ledger:provision --wallets-only # skip accounts
*/
class ProvisionLedgerDefaults extends Command
{
protected $signature = 'ledger:provision
{--company= : UUID of a specific company to provision}
{--accounts-only : Only provision default chart of accounts, skip wallets}
{--wallets-only : Only provision system wallets, skip accounts}';
protected $description = 'Provision default accounts and wallets for all companies and users (or a specific company).';
public function handle(WalletService $walletService): int
{
$skipAccounts = (bool) $this->option('wallets-only');
$skipWallets = (bool) $this->option('accounts-only');
$companyUuid = $this->option('company');
// ── Companies ────────────────────────────────────────────────────────
$companies = $companyUuid
? Company::where('uuid', $companyUuid)->get()
: Company::all();
if ($companies->isEmpty()) {
$this->warn('[Ledger] No companies found to provision.');
return self::SUCCESS;
}
$seeder = $this->makeLedgerSeeder();
$accountsProvisioned = 0;
$companyWallets = 0;
$userWallets = 0;
$errors = 0;
$this->info('[Ledger] Provisioning ' . $companies->count() . ' company/companies...');
$bar = $this->output->createProgressBar($companies->count());
$bar->start();
foreach ($companies as $company) {
// Seed default chart of accounts
if (!$skipAccounts) {
try {
$seeder->runForCompany($company->uuid);
$accountsProvisioned++;
} catch (\Throwable $e) {
$this->newLine();
$this->error("[Ledger] Accounts failed for company {$company->uuid}: " . $e->getMessage());
$errors++;
}
}
// Provision company system wallets (Operating, Revenue, Payout Reserve, Refund Reserve)
if (!$skipWallets) {
try {
$walletService->provisionCompanyWallets($company);
$companyWallets++;
} catch (\Throwable $e) {
$this->newLine();
$this->error("[Ledger] Company wallets failed for {$company->uuid}: " . $e->getMessage());
$errors++;
}
}
$bar->advance();
}
$bar->finish();
$this->newLine(2);
// ── Users (personal wallets for all users) ───────────────────────────
if (!$skipWallets) {
$usersQuery = User::whereNotNull('company_uuid');
if ($companyUuid) {
$usersQuery->where('company_uuid', $companyUuid);
}
$users = $usersQuery->get();
if ($users->isNotEmpty()) {
$this->info('[Ledger] Provisioning personal wallets for ' . $users->count() . ' user(s)...');
$userBar = $this->output->createProgressBar($users->count());
$userBar->start();
foreach ($users as $user) {
try {
$walletService->provisionUserWallet($user);
$userWallets++;
} catch (\Throwable $e) {
$this->newLine();
$this->error("[Ledger] User wallet failed for {$user->uuid}: " . $e->getMessage());
$errors++;
}
$userBar->advance();
}
$userBar->finish();
$this->newLine(2);
} else {
$this->info('[Ledger] No users found to provision wallets for.');
}
}
// ── Summary ──────────────────────────────────────────────────────────
if (!$skipAccounts) {
$this->info("[Ledger] Chart of accounts provisioned for {$accountsProvisioned} company/companies.");
}
if (!$skipWallets) {
$this->info("[Ledger] System wallets provisioned for {$companyWallets} company/companies.");
$this->info("[Ledger] Personal wallets provisioned for {$userWallets} user(s).");
}
if ($errors > 0) {
$this->warn("[Ledger] {$errors} error(s) occurred — check logs for details.");
return self::FAILURE;
}
return self::SUCCESS;
}
/**
* Resolve the account seeder behind a narrow seam so command behavior can
* be tested without coupling tests to the seeder's database implementation.
*/
protected function makeLedgerSeeder(): LedgerSeeder
{
return new LedgerSeeder();
}
}