<?php

include("../public/impOpreation.php");
//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file


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

include_once("../public/config.php");


//here the db files that include in the file
include("../public/include_dao.php");

//get the do the action
$do = $_GET['do'];

/* ======================

  Controller Name :- movementmanageCTRL

  OPERTATION in Controller

  1- show registered user
  2- admin accept their registration or not
  3-
  4-

  ======================== */

//here the global templates
$smarty->display("header.html");

//here goes the instances and general variables
//paymentnetworks
$network = new Paymentnetwork();
$networkDao = new PaymentnetworksMySqlDAO();
$networkExt = new PaymentnetworksMySqlExtDAO();
//Branch
$Branch = new Branch();
$BranchDAO = new BranchMySqlDAO();
$BranchEX = new BranchMySqlExtDAO();

$bills = new Bill();
$billsDao = new BillsMySqlDAO();
$billsExt = new BillsMySqlExtDAO();

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

//check and use the condetion that suite this action

if (empty($do)) {

    $allNetworks = $networkDao->queryByDeleted(0);
    $smarty->assign('allNetworks', $allNetworks);

    $allbranches = $BranchDAO->queryAll();
    $smarty->assign('allbranches', $allbranches);

    $allData = $billsExt->queryNetworkReportToday();
    foreach ($allData as $data) {
        $billTotal += $data->netbillvalue;
        $payedTotal += $data->cardvalue;
        $netTotal += ($data->cardvalue * $data->netdiscountpercent) / 100;
    }
    $smarty->assign('allData', $allData);
    $smarty->assign('billTotal', $billTotal);
    $smarty->assign('payedTotal', $payedTotal);
    $smarty->assign('netTotal', $netTotal);
    //here the smarty templates
    $smarty->display("networkreportview/show.html");
} elseif ($do == "show") {

    $allNetworks = $networkDao->queryByDeleted(0);
    $smarty->assign('allNetworks', $allNetworks);

    $allbranches = $BranchDAO->queryAll();
    $smarty->assign('allbranches', $allbranches);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $networkid = $_REQUEST['network'];
    $branchid = $_REQUEST['branch'];
    $reportType = $_REQUEST['reportType'];
    $query = '';
    if (!empty($startDate) && empty($endDate)) {
        $query .=' and Date(billdate) >= "' . $startDate . '" ';
    } elseif (empty($startDate) && !empty($endDate)) {
        $query .=' and Date(billdate) <= "' . $endDate . '" ';
    } elseif (!empty($startDate) && !empty($endDate)) {
        $query.= ' and Date(billdate) >= "' . $startDate . '" and Date(billdate) <= "' . $endDate . '" ';
    }
    if (!empty($networkid) && $networkid != '-1') {
        $query .= ' and paymentnetworkid = ' . $networkid . '';
    }
    if (!empty($branchid) && $branchid != '-1') {
        $query .=' and bills.branchid = ' . $branchid . '';
    }

    $billTotal = 0;
    $payedTotal = 0;
    $netTotal = 0;
    if ($reportType == 0) {
        $allData = $billsExt->queryNetworkReport($query);
        foreach ($allData as $data) {
            $billTotal += $data->netbillvalue;
            $payedTotal += $data->cardvalue;
            $netTotal += ($data->cardvalue * $data->netdiscountpercent) / 100;
        }
    } else {
        if ($networkid == 4) {
            $madaData = $billsExt->queryTotalNetworkReportMada($query);
            $madaData->totalNet = $madaData->totalCarry;
            $payedTotal += $madaData->totalCarry;
            if ($madaData->totalCarry < 5000)
                $dis = (7 * $madaData->totalCarry) / 1000;
            else
                $dis = 40;

            $madaData->totalCarry = $dis;
            $netTotal += $madaData->totalCarry;
            if ($madaData->totalCarry > 0)
                $smarty->assign('madaData', $madaData);
        } else {
            $allData = $billsExt->queryTotalNetworkReportButMada($query);
            foreach ($allData as $data) {
                $payedTotal += $data->totalNet;
                $netTotal += $data->totalCarry;
            }
        }
        if ($networkid == -1) {
            $madaData = $billsExt->queryTotalNetworkReportMada($query);
            $madaData->totalNet = $madaData->totalCarry;
            $payedTotal += $madaData->totalCarry;
            if ($madaData->totalCarry < 5000)
                $dis = (7 * $madaData->totalCarry) / 1000;
            else
                $dis = 40;

            $madaData->totalCarry = $dis;
            $netTotal += $madaData->totalCarry;
            if ($madaData->totalCarry > 0)
                $smarty->assign('madaData', $madaData);
        }
    }
    $smarty->assign('billTotal', $billTotal);
    $smarty->assign('payedTotal', $payedTotal);
    $smarty->assign('netTotal', $netTotal);

    $smarty->assign('type', $reportType);
    $smarty->assign('allData', $allData);
    //here the smarty templates
    $smarty->display("networkreportview/show.html");
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
//here the global templates
$smarty->display("footer.html");

/////////////////////////////////////////////////////////
?>