<?php
include("../public/impOpreation.php");
$mySaveEx = new SaveMySqlExtDAO();

$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");

if (empty($do)) {
    $smarty->display("header.html");

    if ($_SESSION['searchinonesave'] == 0) {
        if ($_SESSION['saveids'] == 0) {
            $saveData = $mySaveEx->queryAllEX2($queryString);
        } else {
            $queryString .= ' and saveid in (' . $_SESSION['saveids'] . ')';
            $saveData = $mySaveEx->queryAllEX2($queryString);
        }
    } else {
        $queryString .= ' and saveid = ' . $_SESSION['saveid'] . ' ';
        $saveData = $mySaveEx->queryAllEX2($queryString);
    }
    $smarty->assign('allSave', $saveData);

    $smarty->display("savereportview/show.html");
    $smarty->assign("charity", 1);
    $smarty->display("footer.html");
} elseif ($do == "show") {
    $smarty->display("header.html");

    if ($_SESSION['searchinonesave'] == 0) {
        if ($_SESSION['saveids'] == 0) {
            $saveData = $mySaveEx->queryAllEX2($queryString);
        } else {
            $queryString .= ' and saveid in (' . $_SESSION['saveids'] . ')';
            $saveData = $mySaveEx->queryAllEX2($queryString);
        }
    } else {
        $queryString .= ' and saveid = ' . $_SESSION['saveid'] . ' ';
        $saveData = $mySaveEx->queryAllEX2($queryString);
    }
    $smarty->assign('allSave', $saveData);

    $from = filter_input(INPUT_POST, 'fromdate');
    $to = filter_input(INPUT_POST, 'todate');
    $saveid = filter_input(INPUT_POST, 'saveid');

    $message = '';

    if ($saveid) {
        $saveName = R::getCell('select savename from save where saveid = ' . $saveid);
        $message .= 'الخزنة: ' . $saveName;

        $sellbillqs = ' and sellbill.sellbillsaveid = ' . $saveid;
        $retsellbillqs = ' and returnsellbill.returnsellbillsaveid = ' . $saveid;
        $sellbillretqs = ' and sellbillandrutern.sellbillsaveid = ' . $saveid;
        $expensesqs = ' and expenses.saveid = ' . $saveid;


        if ($from) {
            $message .= ' - التاريخ من: ' . $from;

            $expensesqs .= ' and expenses.expensesdate >= "' . $from . '"';

            $from = $from . " 00:00:00";
            $sellbillqs .= ' and sellbill.sellbilldate >=  "' . $from . '"';
            $retsellbillqs .= ' and returnsellbill.returnsellbilldate >=  "' . $from . '"';
            $sellbillretqs .= ' and sellbillandrutern.sellbilldate >=  "' . $from . '"';
        }
        if ($to) {
            $message .= ' - التاريخ إلى: ' . $to;

            $expensesqs .= ' and expenses.expensesdate <= "' . $to . '"';

            $to = $to . ' 23:59:59';
            $sellbillqs .= ' and sellbill.sellbilldate <=  "' . $to . '"';
            $retsellbillqs .= ' and returnsellbill.returnsellbilldate <=  "' . $to . '"';
            $sellbillretqs .= ' and sellbillandrutern.sellbilldate <=  "' . $to . '"';
        }
        $allSellBills = R::getCell('select SUM(sellbilltotalpayed) FROM sellbill where conditions = 0 ' . $sellbillqs);
        $allRetSellBills = R::getCell('select SUM(returnsellbilltotalpayed) FROM returnsellbill where conditions = 0 ' . $retsellbillqs);
        $allSellRetBills = R::getCell('select SUM(sellbilltotalpayed) FROM sellbillandrutern where conditions = 0 ' . $sellbillretqs);

        $totalSells  = (float) ($allSellBills + $allSellRetBills - abs($allRetSellBills));
        $smarty->assign('totalSells', $totalSells);

        $allExpenses = R::getAll('select expensestype.expensestypename, SUM(expensesValue) totalEx from expenses join expensestype ON expensestype.expensestypeid = expenses.expensestypeid 
        where expenses.conditions = 0 ' . $expensesqs . ' group by expenses.expensestypeid');

        $smarty->assign('allExpenses', $allExpenses);

        $smarty->assign('message', $message);
        $smarty->assign('show', 1);
        $smarty->display("savereportview/show.html");
        $smarty->display("footer.html");
    } else {
        header('location:?');
    }
}
