Skip to content

BE-DP-40 · The money step on the local stack

ID BE-DP-40 · Level L0 · Pack Row 5 · The domain steps · Run order row 5, after the money step (team/306-money-step) is on integration, with its account rows in · Issue #306

A night on your local stack, against the real v1 copy, runs master data, vehicles, drivers, rentals and operations, then the money step, and reconciles them. The money step loads, from v1:

  • bills as charges, one each, as v1 billed them (R-MON-84, R-MON-64, R-MON-65), each on its lead's rental by R-INSP-17's lookup (R-MON-85), with its company from its car, its driver's pref_akun or its own payments' accounts (R-MON-95, R-MON-96), and a Klaim Panel charge linked to its inspection (R-MON-83).
  • The instalments as payments and allocations (R-MON-54), with one driver's instalments of one proof, day and account as one payment (R-MON-77), and who confirmed a v1 payments row as a note on its payment (R-MON-97).
  • One payment of dibayar for each Lunas bill with no instalment that dibayar covers (R-MON-60), and each confirmed payments row no instalment records (R-MON-58).
  • Rebates, then allocations, then refunds (R-MON-78), so a refund never reads as credit.
  • claim_letters as issued debt letters (R-COL-09 to R-COL-21). Debts load no charge: the bill is the charge (R-MON-76).

A row it can't place is an unmatched row with its reason (R-MIG-04), never a guess. It bills nothing.

legacy and the loaded rows are a copy of v1's data in your local database: personal data, as the dump is (D15). The counts below are safe to post. The reconcile's lines name v1 ids and hold one driver's amounts, so they stay on your Mac.

  • You are at the root of your clone, on integration with this step merged, after git pull, pnpm supabase db reset and BE-DP-07.
  • docker ps --filter name=v1copy lists the copy as Up, restored on the day you run.
  • The money step's migration holds the rows for the accounts rekTujuan names, keyed by their last four digits, and Kwitansi as cash (R-MON-90, R-MON-72). A value with no row stops block 1. So does a value of vehicle_master.data.platform or pref_akun other than Gojek, Grab and Keduanya (R-MON-95, R-MIG-02).
  • pnpm supabase test db needs a stack with no night loaded, so run it before this step or after a reset.

Once this page's Before holds, this runs every block below and compares the counts with be-dp-40.expect, beside this page. It keeps each block's full output in ../opleet-private, prints block names, counts and error keys only (D15), and ends the run itself: succeeded only when no count changed.

Terminal window
python3 tools/migration/night.py local be-dp-40

Post what it prints under "To post on the issue". A changed or new line goes on the issue, not into this page; once it's accepted, ../opleet-private/be-dp-40-counts.txt replaces be-dp-40.expect. The blocks below stay for a rerun block by block.

1 · A run, its snapshot, and what's unmapped

Section titled “1 · A run, its snapshot, and what's unmapped”
Terminal window
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 <<'SQL'
select migration.begin_run('BE-DP-40, local, on the v1 copy, with money') as run;
select migration.snapshot() as tables_copied;
select domain, field, v1_value, rows from migration.unmapped_values() order by 1, 2, 3;
SQL

If it prints a line, stop. End the run with docker exec supabase_db_opleet psql -qX -U postgres -d postgres -c "select migration.finish_run('failed')" and post the lines on #306. An account's line prints its last four digits, or the words left once the holder's text is stripped: check it holds no name before you post it.

Terminal window
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 <<'SQL'
select migration.run('master_data') as master_rows;
select migration.run('vehicles') as vehicle_rows;
select migration.run('drivers') as driver_rows;
select migration.run('rentals') as rental_rows;
select migration.run('operations') as operation_rows;
select migration.run('money') as money_rows;
select table_name, (new_data ->> 'rows')::int as rows from public.audit_log
where action = 'migrated' and record_id = (select max(id) from migration.runs)::text
and new_data ->> 'step' = 'money' order by id;
SQL
Terminal window
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 <<'SQL'
-- Unmatched, by table and reason (the letter duplicate's reason is shortened: it names a number).
select v1_table, regexp_replace(reason, 'letter .*$', 'letter') as reason, count(*) as rows from migration.unmatched
where run_id = (select max(id) from migration.runs) and step = 'money' group by 1, 2 order by 1, 3 desc;
-- Charges by type and status, with a rental and a billing day (R-MON-84, R-MON-85, R-MON-86).
select t.code, c.status, count(*) as charges, sum(c.amount) as amount, count(c.rental_id) as with_rental,
count(c.billing_day) as with_day, count(c.legal_entity_id) as with_entity, count(c.inspection_id) as with_inspection
from public.charges c join public.charge_types t on t.id = c.charge_type_id group by 1, 2 order by 1, 2;
-- Payments by source and method (R-MON-55, R-MON-72).
select migrated_from, method, count(*) as payments, sum(amount) as amount, count(bank_account_id) as with_account,
count(*) filter (where voided_at is not null) as voided
from public.payments group by 1, 2 order by 1, 2;
-- Allocations and adjustments (R-MON-78).
select count(*) as allocations, sum(amount) as allocated from public.payment_allocations;
select kind, count(*) as adjustments, sum(amount) as amount, count(*) filter (where reason = 'v1: no reason recorded') as no_reason
from public.charge_adjustments group by 1 order by 1;
-- The balance, and the credit, over all drivers (R-MON-79, R-MON-92).
select sum(open_balance) as open_balance, count(*) filter (where open_balance > 0) as drivers_owing from public.v_driver_balance;
select sum(open) as open_of_record, sum(credit) as credit_of_record, count(*) filter (where credit > 0) as drivers_in_credit
from migration.money_record where run_id = (select max(id) from migration.runs);
-- Letters and their items (R-COL-09, R-COL-20).
select status, count(*) as letters, count(rental_id) as with_rental from public.debt_letters group by 1;
select count(*) as items, count(legacy_amount) as with_amount from public.debt_letter_items;
-- R-MON-63: the migrated payments with no proof or account, expected by rule. Never signed.
select * from migration.check_money('expected');
SQL
Terminal window
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 <<'SQL'
select check_name, count(*) as lines from migration.reconcile() group by 1 order by 1;
SQL
mkdir -p ../opleet-private
docker exec supabase_db_opleet psql -qX -U postgres -d postgres -c "select * from migration.reconcile() where check_name like 'money%' or check_name = 'unmatched rows' or check_name = 'v1 unclaimed'" > ../opleet-private/reconcile-be-dp-40.txt
wc -l ../opleet-private/reconcile-be-dp-40.txt

The file holds v1 ids and one driver's amounts; it stays on your Mac.

Terminal window
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 <<'SQL'
select row_key, detail from migration.reconcile()
where check_name = 'v1 unclaimed' and row_key ~ '^(table|column) (bills|payments|debts|claim_letters)\y' order by 1;
SQL
Terminal window
docker exec supabase_db_opleet psql -qX -U postgres -d postgres -c "select migration.finish_run('succeeded')"

Then run BE-DP-23, with this night's steps in its domain_steps, so the payment and rebate folders attach.

These are run 2's counts, from the copy of 2026-11-05 (#306). v1 moves (D75): a difference goes on #306, not into this page.

  • Block 1: a run id, tables_copied 55 or more, and no unmapped line.
  • Block 2: the five earlier steps load what BE-DP-38 loads. The money step loads 174,516 rows, and its audit lines are one per table it loaded, plus etle_violations (charge link), the violations it linked, whose count adds to the rows (R-ETLE-09). Block 2 took 6 min 56 s, all six steps. If it passes 15 minutes, cancel it and post on #306 which statement the cancel's CONTEXT names.
  • Block 3:
    • Unmatched: 143. debts 31 (R-MON-81). bills 78: 1 due before its charge and 77 on leads the drivers step left out. claim_letters 29: 8 with a debtId that names no bill, 7 with no one driver, 6 on left-out leads, 4 whose bill loaded no charge, 3 with no bill found and 1 duplicate. payments 5.
    • Charges: 26,183 in all, one per bill. with_entity 25,162 (R-MON-95). Rent charges with a rental 1,587.
    • Payments: 72,442: bills.data.pembayaran 54,324, bills.data.dibayar 18,116 and payments 2, the confirmed rows no instalment records (R-MON-58). Allocations 72,483.
    • Adjustments: refunds 161, rebates 141.
    • Balance: open_balance equals open_of_record to the rupiah. 57 drivers are in credit, and payments less allocations equals the credit of record exactly.
    • Letters: 1,170, all issued.
    • Items: with_amount 1,843 of 1,934: R-COL-21 pairs by type.
    • ETLE link (R-ETLE-09): in check_money('expected'), etle linked near 237, etle unlinked near 23 and etle several 0, on the 260 rows of the copy of 2026-11-05. v1 moves (D75), so a small difference isn't a failure: a bill on a left-out lead loads no charge, so its row counts as unlinked. Those counts came from a read that matched loosely, in upper case with punctuation stripped, and this step's key is exact. So a linked count well below 237 means source and no_tilang differ in form: post the three lines on #538 and stop.
  • Block 4: money balance per driver, money credit per driver, money payments per day and account, money paid bills per day and money allocations print no line, so each is 0; a line on any of them needs a look. The rest are the lists you sign or have fixed in v1: money pending payments 327, with the Ignore rows, falling as finance clears them (R-MON-59), money lunas short 57, money owed with dibayar 1, money zero instalments 38, money refund flag with no amount 1, money refunds on bills not paid in full 60, money debts of another amount 1, money letters renumbered 43, money letters whose text or plate disagree 10, money letters on several leads 32, money letters with another lead's item 29, money charges with no company 1,021, by v1 id (R-MON-96), and money violations with several bills 0, by v1 id, never by ticket (R-ETLE-09). Five are one-line counts: charges with no rental, panel charges with no inspection, and letters with no plate, with no rental and not paired, which falls from run 1's 124 (R-COL-21). Across all the steps, unmatched rows prints 226 lines: money 143, drivers 71, rentals 12. v1 unclaimed prints 32.
  • Block 5: payments' 7 columns the step doesn't read print here: bukti, jenis, pic, plat, tgl_hari, edited_at and edited_by, less any another step claims. They are for you to sign or send back.
  • Block 1 prints a line: see block 1. The first night after R-MON-95 may stop here once: vehicle_master.data->platform and leads.data->pref_akun have rows only for Gojek, Grab and Keduanya, so any other value prints with its count. That first run is the list of values. Post the lines, zuki maps each (R-MIG-02), and the night runs again.
  • migration.step_money: run the … steps first: run all six in one run, from block 2.
  • allocation.exceeds_balance or allocation.exceeds_payment: the step's arithmetic and the guard disagree. Stop, end the run as failed, and post the ERROR line without its DETAIL, which holds one driver's amounts.
  • A foreign key error on bank_account_id: a value_map row names an account migration.bank_accounts doesn't have. Post the line.
  • Anything else: end the run as failed and post the ERROR line. Read it first: drop any detail with a name or an amount.

pnpm supabase db reset, then BE-DP-07. The copy and the dump are untouched.

Log each run on #306: the date of the copy, block 2's numbers, block 3's counts, block 4's lines per check (counts only), and block 5's rows.