<?php
$topsdally_report = array();
		$topsdally_report["selectList"] = array(
		"subtype" => "sql",
		"sql" => "SELECT 
    d.txn_date,

    IFNULL(c.total_collections, 0) AS total_collections,
    IFNULL(e.total_expenses, 0) AS total_expenses,

    (IFNULL(c.total_collections, 0) - IFNULL(e.total_expenses, 0)) AS net_cash_flow

FROM
(
    SELECT DATE(pay_date) AS txn_date FROM collections
    UNION
    SELECT DATE(expense_date) AS txn_date FROM expenses
) d

LEFT JOIN
(
    SELECT 
        DATE(pay_date) AS pay_date,
        SUM(amount) AS total_collections
    FROM collections
    GROUP BY DATE(pay_date)
) c ON d.txn_date = c.pay_date

LEFT JOIN
(
    SELECT 
        DATE(expense_date) AS expense_date,
        SUM(amount) AS total_expenses
    FROM expenses
    GROUP BY DATE(expense_date)
) e ON d.txn_date = e.expense_date

ORDER BY d.txn_date DESC;
"
	);
		$tables_data["dally report"][".operations"] = &$topsdally_report;
?>