-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBackfillTransactionDirection.php
More file actions
86 lines (69 loc) · 2.46 KB
/
Copy pathBackfillTransactionDirection.php
File metadata and controls
86 lines (69 loc) · 2.46 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
<?php
namespace Fleetbase\Ledger\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
/**
* BackfillTransactionDirection.
*
* Sets the `direction` column on existing `transactions` rows that have
* a NULL direction. Direction is derived from the transaction `type`:
*
* credit → purchase, deposit, earning, transfer_in, topup, payment
* debit → refund, withdrawal, payout, fee, transfer_out, chargeback
*
* Any type not in the explicit map defaults to 'credit'.
*
* Usage:
* php artisan ledger:backfill-direction
* php artisan ledger:backfill-direction --chunk=500
*/
class BackfillTransactionDirection extends Command
{
protected $signature = 'ledger:backfill-direction
{--chunk=250 : Number of rows to process per batch}';
protected $description = 'Backfill the direction (credit/debit) column on existing transaction rows';
/**
* Transaction types that represent money going OUT (debit).
*/
private const DEBIT_TYPES = [
'refund',
'withdrawal',
'payout',
'fee',
'transfer_out',
'chargeback',
'reversal',
'void',
];
public function handle(): int
{
$chunk = (int) $this->option('chunk');
$total = DB::table('transactions')->whereNull('direction')->count();
if ($total === 0) {
$this->info('[Ledger] All transactions already have a direction set.');
return self::SUCCESS;
}
$this->info("[Ledger] Backfilling direction on {$total} transaction(s)...");
$bar = $this->output->createProgressBar($total);
$bar->start();
$processed = 0;
DB::table('transactions')
->whereNull('direction')
->chunkById($chunk, function ($rows) use ($bar, &$processed) {
foreach ($rows as $row) {
$direction = in_array(strtolower((string) $row->type), self::DEBIT_TYPES, true)
? 'debit'
: 'credit';
DB::table('transactions')
->where('id', $row->id)
->update(['direction' => $direction]);
$processed++;
}
$bar->advance(count($rows));
});
$bar->finish();
$this->newLine();
$this->info("[Ledger] Done — {$processed} transaction(s) updated.");
return self::SUCCESS;
}
}