<?php
//the global file operation
include("../public/impOpreation.php");

//global varable
global $showoutside;

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");


$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

//here the db files that include in the file
// GOES HERE ....................
include("../public/include_dao.php");
$do = $_GET['do'];
$date = date("Y-m-d");
$youtubes = $youtubeLinkDAO->queryAll();
$smarty->assign("youtubes", $youtubes);

if (empty($do)) {
    $smarty->assign('date', $date);
    $smarty->display("header.html");
    $smarty->display("incomelistview/index.html");
    $smarty->display("footer.html");
} else if ($do == 'getdata') {
    $date = date("Y-m-d");
    $fromdate = $_POST['fromdate'];
    $todate   = $_POST['todate'];
    $smarty->assign('fromdate', $fromdate);
    $smarty->assign('todate', $todate);
   
    $sellbillfinalbilltotal = R::getCell("SELECT sum(sellbillaftertotalbill) FROM sellbill where sellbill.conditions = 0 and sellbill.sellbilldate  >= '$fromdate 00-00-00' and sellbill.sellbilldate <= '$todate 23-59-55' ");
     $returnsellbillaftertotalbill = R::getCell("SELECT sum(returnsellbillaftertotalbill) FROM returnsellbill where returnsellbill.conditions = 0 and returnsellbill.returnsellbillsysdate  >= '$fromdate 00-00-00' and returnsellbill.returnsellbillsysdate <= '$todate 23-59-55' ");
    $smarty->assign('sellbillfinalbilltotal', $sellbillfinalbilltotal);
    $smarty->assign('returnsellbillaftertotalbill',  abs($returnsellbillaftertotalbill));

    $income = R::getCell("SELECT sum(incomeValue) FROM income where income.conditions = 0
        AND income.incomeDate >= '$fromdate' AND income.incomeDate <= '$todate' ");
    $smarty->assign('income', $income);    

    $accountmovementplus = R::getCell("SELECT sum(accountmovementamount) FROM accountmovement  WHERE accountmovement.accountmovementdate >=  '$fromdate' AND  accountmovement.accountmovementdate <= '$todate' and accountmovementtype != 1 AND accountmovement.tablename = 'bankAccountDeficitController.php' ");
    $smarty->assign('accountmovementplus', $accountmovementplus);

    $accountmovementloss = R::getCell("SELECT sum(accountmovementamount) FROM accountmovement  WHERE accountmovement.accountmovementdate >=  '$fromdate' AND  accountmovement.accountmovementdate <= '$todate' and accountmovementtype = 1 AND accountmovement.tablename = 'bankAccountDeficitController.php' ");
    $smarty->assign('accountmovementloss', $accountmovementloss);           

$buybillfinalbilltotals = R::getCell("SELECT sum(buybillaftertotalbill) FROM buybill where buybill.conditions = 0
    and buybill.buybilldate  >= '$fromdate 00-00-00' and buybill.buybilldate <= '$todate 23-59-55' ");
$returnbuybill = R::getCell("SELECT sum(returnbuybillaftertotalbill) FROM returnbuybill where returnbuybill.conditions = 0
    and returnbuybill.returnbuybillsysdate  >= '$fromdate 00-00-00' and returnbuybill.returnbuybillsysdate <= '$todate 23-59-55' ");
$smarty->assign('returnbuybill', abs($returnbuybill));
$smarty->assign('buybillfinalbilltotals', $buybillfinalbilltotals);

$expenses = R::getCell("SELECT sum(expensesValue) FROM expenses join expensestype
                on expensestype.expensestypeid = expenses.expensestypeid
                where expenses.conditions = 0
                AND expenses.expensesdate  >= '$fromdate' AND expenses.expensesdate  <= '$todate' ");
$smarty->assign('expenses', $expenses);

$savestockstart = R::findOne('savestock',"savestock.savestockdate = '$fromdate'");
$smarty->assign('savestockstart', $savestockstart->savestocktotal);
if($todate < $date){
$savestockend = R::findOne('savestock',"savestock.savestockdate = '$todate'");
$smarty->assign('savestockend', $savestockend->savestocktotal);
}else{
$totalstock = R::getCell("SELECT sum(((buybilldetailprice * don) + (buybilldetailprice * don * payedtax) / 100)) as total 
FROM productserial left join product on product.productid = productserial.productid left join productcat on product.productCatId = productcat.productCatId 
left join buybilldetail on buybilldetail.buybilldetailid = productserial.buybilldetailid left join buybill on buybilldetail.buybillid = buybill.buybillid 
WHERE productserial.don > 0 and productserial.del = 0");
$smarty->assign('savestockend', $totalstock);
}

$supplierdebt = R::getCell("SELECT sum(suppliercurrentDebt) FROM supplier WHERE conditions = 0  ");
$smarty->assign('supplierdebt', $supplierdebt);
$clientdebt = R::getCell("SELECT sum(clientdebt) FROM client WHERE conditions = 0 ");
$smarty->assign('clientdebt', $clientdebt);

$smarty->display("incomelistview/getdata.html");
}



?>