<?php

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

//global varable
global $showoutside;

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

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


//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.class.php');

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

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

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

  Controller Name :-

  OPERTATION in Controller

  1- show report
  2- search with

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

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

//here goes the instances and general variables
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();


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

//check and use the condetion that suite this action
if (empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");

    $triptypeid = (int) $_POST['triptypeid'];
    $tripid = (int) $_POST['tripid'];
    $pathid = (int) $_POST['pathid'];
    $tripdateid = (int) $_POST['tripdateid'];
    $busid = (int) $_POST['busid'];
    $driverid = (int) $_POST['driverid'];

    $from = $_POST['from'];
    $to = $_POST['to'];

    $querystring = '';
    if ($triptypeid > 0) {
        $querystring .= " and travelTripTypeId= " . $triptypeid;
    }
    if ($tripid > 0) {
        $querystring .= " and travelTripId= " . $tripid;
    }
    if ($pathid > 0) {
        $querystring .= " and travelPathId= " . $pathid;
    }
    if ($tripdateid > 0) {
        $querystring .= " and travelTripDateId= " . $tripdateid;
    }
    if ($busid > 0) {
        $querystring .= " and travelBusId= " . $busid;
    }
    if ($driverid > 0) {
        $querystring .= " and travelDriverId= " . $driverid;
    }

    $querystring2 = $querystring3 = $querystring;
    if (isset($from) && !empty($from)) {
        $querystring3 .= " and expensesdate>= '" . $from . "' ";
        $from = $from . " 00:00:00";
        $querystring .= " and sellbilldate>= '" . $from . "' ";
        $querystring2 .= " and clientdebtchangedate>= '" . $from . "' ";
    }
    if (isset($to) && !empty($to)) {
        $querystring3 .= " and expensesdate<= '" . $to . "' ";
        $to = $to . " 23:59:59";
        $querystring .= " and sellbilldate<= '" . $to . "' ";
        $querystring2 .= " and clientdebtchangedate<= '" . $to . "' ";
    }

    ##default search is today
    if ($querystring == "") {
        //get today result
        $from = date('y-m-d') . " 00:00:00";
        $to = date('y-m-d') . " 23:59:59";
        $querystring .= " and sellbilldate>= '" . $from . "' and sellbilldate<= '" . $to . "' ";
    }
    if ($querystring2 == "") {
        //get today result
        $from = date('y-m-d') . " 00:00:00";
        $to = date('y-m-d') . " 23:59:59";
        $querystring2 .= " and clientdebtchangedate>= '" . $from . "' and clientdebtchangedate<= '" . $to . "' ";
    }
    if ($querystring3 == "") {
        //get today result
        $from = $to = date('y-m-d');
        $querystring3 .= " and expensesdate>= '" . $from . "' and expensesdate<= '" . $to . "' ";
    }

    ## incomes #################################################################
    //1- payed in bills
    $billData = R::getAll('select sellbillid,sellbilldate,sellbilltotalpayed from sellbill where 1 ' . $querystring);
    $smarty->assign("billData", $billData);
    //2- payments on bill
    $billDataForPayments = R::getAll('select sellbillid from sellbill where 1 ' . $querystring2);
    $queryStringPayment = '';
    if (isset($from) && !empty($from)) {
        $from = $from . " 00:00:00";
        $queryStringPayment .= " and clientdebtchangedate>= '" . $from . "' ";
    }
    if (isset($to) && !empty($to)) {
        $to = $to . " 23:59:59";
        $queryStringPayment .= " and clientdebtchangedate<= '" . $to . "' ";
    }
    $billIds = array();
    foreach ($billDataForPayments as $value) {
        $billIds[] = $value['sellbillid'];
    }
    if (!empty($billIds)) {
        $billIds = !empty($billIds) ? array_unique($billIds) : [0];
        $queryStringPayment .= " and billid in(" . implode(',', $billIds) . ") ";
    }


    ##default search is today
    if ($querystring2 == "" && $queryStringPayment == "") {
        //get today result
        $from = date('y-m-d') . " 00:00:00";
        $to = date('y-m-d') . " 23:59:59";
        $queryStringPayment .= " and clientdebtchangedate>= '" . $from . "' and clientdebtchangedate<= '" . $to . "' ";
    }

    $paymentData = array();
    if ($queryStringPayment != "") {
        $paymentData = R::getAll('SELECT clientdebtchangeid,clientdebtchangeamount , clientdebtchangedate , billid , paytype,dailyentryid
                FROM clientdebtchange
                WHERE tablename = "clientPayedDeptSellBillsController.php" ' . $queryStringPayment);
    }
    $smarty->assign("paymentData", $paymentData);
    ## expenses ################################################################
    $expenses = R::getAll('select expensesid,expensesname,expensesdate,expensesValue from expenses where 1' . $querystring3);
    $smarty->assign("expenses", $expenses);
    //here the smarty templates
    $smarty->display("tripsReportview/show.html");
} elseif ($do == "referrers") {

    $referrerid = (int) $_POST['referrerid'];
    $from = $_POST['from'];
    $to = $_POST['to'];

    $querystring = '';
    if ($referrerid > 0) {
        $querystring .= " and sellbilldetail.travelBrokerId= " . $referrerid;
        if (isset($from) && !empty($from)) {
            $from = $from . " 00:00:00";
            $querystring .= " and sellbilldate>= '" . $from . "' ";
        }
        if (isset($to) && !empty($to)) {
            $to = $to . " 23:59:59";
            $querystring .= " and sellbilldate<= '" . $to . "' ";
        }

        ##
        $billData = R::getAll('select sellbill.sellbillid,sellbilldate,sellbillaftertotalbill,sellbilldetailtotalprice,sellbilltotalpayed,travelTripId,sellbillclientid
                               from sellbill
                               join sellbilldetail on sellbilldetail.sellbillid = sellbill.sellbillid
                               where 1 ' . $querystring);
        list($clientIDs, $tripIDs) = [[], []];
        foreach ($billData as $value) {
            $clientIDs[] = $value['sellbillclientid'];
            $tripIDs[] = $value['travelTripId'];
        }
        $clientIDs = !empty($clientIDs) ? array_unique($clientIDs) : [0];
        $tripIDs = !empty($tripIDs) ? array_unique($tripIDs) : [0];


        ##client
        $clientDataArr = R::getAll('select clientid,clientname from client where clientid in(' . implode(',', $clientIDs) . ') ');
        $clientDataArr = customArrayIndexOne($clientDataArr, 'clientid');

        ##trip
        $return_arr = array();
        $response = CURL_Request(array(), getenv('africanoUrl') . '/trip/filter/' . implode(',', $tripIDs), 1, 0, getenv('africanoToken'));
        $receivedata = json_decode($response);
        foreach ($receivedata->data as $data) {
            $row_array = array();
            $row_array['id'] = $data->id;
            $row_array['name'] = $data->name;
            array_push($return_arr, $row_array);
        }
        $tripDataArr = customArrayIndexOne($return_arr, 'id');

        $billDataTemp = array();
        foreach ($billData as $value) {
            $value['clientname'] = $clientDataArr[$value['sellbillclientid']]['clientname'];
            $value['tripname'] = $tripDataArr[$value['travelTripId']]['name'];
            $billDataTemp[] = $value;
        }
        $billData = $billDataTemp;
        $smarty->assign("billData", $billData);
    }
    $smarty->display("tripsReportview/referrers.html");
}
//here the global templates
$smarty->display("footer.html");

/* ===============================
  function in this CONTROLLER
  ================================ */
