-- 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;


-- Delete all but one customer
SET @cid = 6;
-- DELETE T FROM pmgr_transactions T
--  LEFT JOIN pmgr_customers C ON C.id = T.customer_id
--  WHERE C.id IS NOT NULL AND C.id <> @cid;
DELETE C FROM pmgr_customers C
 WHERE C.id <> @cid;
DELETE L FROM pmgr_leases L
 LEFT JOIN pmgr_customers C ON C.id = L.customer_id
 WHERE C.id IS NULL;
DELETE T FROM pmgr_transactions T
 LEFT JOIN pmgr_customers C ON C.id = T.customer_id
 WHERE C.id IS NULL;
DELETE SE FROM pmgr_statement_entries SE
 LEFT JOIN pmgr_customers C ON C.id = SE.customer_id
 WHERE C.id IS NULL;
DELETE LE FROM pmgr_ledger_entries LE
 LEFT JOIN pmgr_transactions T ON T.id = LE.transaction_id
 WHERE T.id IS NULL;
DELETE M FROM pmgr_tenders M
 LEFT JOIN pmgr_ledger_entries LE ON M.ledger_entry_id = LE.id
 WHERE LE.id IS NULL;
DELETE DE FROM pmgr_double_entries DE
 LEFT JOIN pmgr_ledger_entries LE ON LE.id = DE.debit_entry_id
 WHERE LE.id IS NULL;
DELETE DE FROM pmgr_double_entries DE
 LEFT JOIN pmgr_ledger_entries LE ON LE.id = DE.credit_entry_id
 WHERE LE.id IS NULL;
UPDATE pmgr_ledger_entries LE, pmgr_ledgers L, pmgr_accounts A
 SET LE.ledger_id = L.id
 WHERE A.id = LE.account_id AND L.account_id = A.id AND L.sequence = 1;
DELETE FROM pmgr_ledgers WHERE sequence > 1;
UPDATE pmgr_ledgers SET prior_ledger_id = NULL, close_transaction_id = NULL;


-- Delete a ledger entry, associated double entry, and matching ledger_entry
SET @leid = 1365;
DELETE FROM pmgr_ledger_entries WHERE id = @leid;
DELETE DE FROM pmgr_double_entries DE
 LEFT JOIN pmgr_ledger_entries LE ON LE.id = DE.debit_entry_id
 WHERE LE.id IS NULL;
DELETE DE FROM pmgr_double_entries DE
 LEFT JOIN pmgr_ledger_entries LE ON LE.id = DE.credit_entry_id
 WHERE LE.id IS NULL;
DELETE LE FROM pmgr_ledger_entries LE
 LEFT JOIN pmgr_double_entries DE 
   ON DE.credit_entry_id = LE.id OR DE.debit_entry_id = LE.id
 WHERE DE.id IS NULL;

-- Add and update every Tender.ledger_entry_id (for rolling up old databases)
-- Takes a while to complete (~30s at time of writing)
ALTER TABLE `pmgr_tenders`
 ADD `deposit_ledger_entry_id` INT UNSIGNED DEFAULT NULL
 AFTER `nsf_ledger_entry_id`;
UPDATE
pmgr_tenders Tnd
JOIN pmgr_tender_types TndT ON TndT.id = Tnd.tender_type_id
JOIN pmgr_transactions T ON T.id = Tnd.deposit_transaction_id
JOIN pmgr_ledger_entries LE ON LE.transaction_id = T.id AND LE.account_id = TndT.account_id
JOIN pmgr_double_entries DE ON DE.debit_entry_id = LE.id OR DE.credit_entry_id = LE.id
JOIN pmgr_ledger_entries LEd ON (DE.debit_entry_id = LEd.id OR DE.credit_entry_id = LEd.id)
    AND LEd.id <> LE.id    
SET Tnd.deposit_ledger_entry_id = LEd.id;


-- Add auto_deposit and deposit_account_id to tenders
ALTER TABLE `pmgr_tender_types`
 ADD `auto_deposit` TINYINT(1) UNSIGNED DEFAULT '0' NOT NULL
 AFTER `tillable`;
ALTER TABLE `pmgr_tender_types`
 ADD `deposit_account_id` INTEGER(10) UNSIGNED DEFAULT NULL
 AFTER `account_id`;


-- 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')

