-- Delete bad transaction(s) DELETE M FROM pmgr_ledger_entries LE, pmgr_tenders M WHERE M.ledger_entry_id = LE.id AND LE.transaction_id IN (467); DELETE LE FROM pmgr_ledger_entries LE WHERE LE.transaction_id IN (467); DELETE SE FROM pmgr_statement_entries SE WHERE SE.transaction_id IN (467); DELETE T FROM pmgr_transactions T WHERE T.id IN (467); -- Delete bad transaction, one variable setting SET @tid = 467; DELETE M FROM pmgr_ledger_entries LE, pmgr_tenders M WHERE M.ledger_entry_id = LE.id AND LE.transaction_id = @tid; DELETE LE FROM pmgr_ledger_entries LE WHERE LE.transaction_id = @tid; DELETE SE FROM pmgr_statement_entries SE WHERE SE.transaction_id = @tid; DELETE T FROM pmgr_transactions T WHERE T.id = @tid; -- Determine economic conditions SELECT `status`, COUNT(id), SUM(rent) FROM pmgr_units GROUP BY `status` WITH ROLLUP; -- Check that transaction totals add up correctly SELECT T.id, T.type, T.amount, -- T.type, A.type, E.crdr, SUM(IF(E.account_id = T.account_id, IF(A.type IN ('ASSET','EXPENSE') XOR E.crdr='DEBIT',-1,1),0) *E.amount) AS Tamt, SUM(IF(E.account_id = T.account_id, 0,IF(A.type IN ('ASSET','EXPENSE') XOR E.crdr='DEBIT',-1,1)) *E.amount) AS Oamt, COUNT(E.id) AS Ecnt FROM pmgr_transactions T -- LEFT JOIN pmgr_statement_entries E ON E.transaction_id = T.id LEFT JOIN pmgr_ledger_entries E ON E.transaction_id = T.id LEFT JOIN pmgr_accounts A ON A.id = T.account_id -- E.account_id -- WHERE -- E.account_id != T.account_id GROUP BY T.id HAVING (T.type = 'INVOICE' AND Tamt <> T.amount) OR (T.type <> 'INVOICE' AND Oamt <> T.amount) OR (Tamt * -1 <> Oamt) -- Verify that statement entries all have the correct type SELECT SE.id, SE.type, T.id, T.type FROM pmgr_statement_entries SE LEFT JOIN pmgr_transactions T ON T.id = SE.transaction_id WHERE ((T.type = 'RECEIPT' OR T.type = 'CREDIT_NOTE') AND SE.type NOT IN ('DISBURSEMENT', 'WAIVER', 'REVERSAL', 'WRITEOFF', 'SURPLUS') ) OR ((T.type = 'INVOICE' OR T.type = 'PAYMENT') AND SE.type NOT IN ('CHARGE', 'PAYMENT', 'REFUND') ) -- catch other types not considered in this query OR T.type NOT IN ('RECEIPT', 'CREDIT_NOTE', 'INVOICE', 'PAYMENT')