<?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
// GOES HERE ....................
include("../public/include_dao.php");

//user
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');

//Returnsellbill
require_once('../models/dao/ReturnsellbillDAO.class.php');
require_once('../models/dto/Returnsellbill.class.php');
require_once('../models/mysql/ReturnsellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbillMySqlExtDAO.class.php');


//Sellbill
require_once('../models/dao/SellbillDAO.class.php');
require_once('../models/dto/Sellbill.class.php');
require_once('../models/mysql/SellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillMySqlExtDAO.class.php');

//Sellbillandrutern
require_once('../models/dao/SellbillandruternDAO.class.php');
require_once('../models/dto/Sellbillandrutern.class.php');
require_once('../models/mysql/SellbillandruternMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillandruternMySqlExtDAO.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'];

$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");

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

  Controller Name :- userCTRL

  OPERTATION in Controller

  1-display add form
  2- add in user tble
  3-display sucess or error
  4-display show form
  5-delete from user tbl
  6-edit in user data

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


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



//here goes the instances and general variables
//user
$myUser = new User();
$myUserRecord = new UserMySqlDAO();
$myUserEx = new UserMySqlExtDAO();

//Returnsellbill
$Returnsellbill = new Returnsellbill();
$ReturnsellbillDAO = new ReturnsellbillMySqlDAO();
$ReturnsellbillEX = new ReturnsellbillMySqlExtDAO();


//Sellbillandrutern
$Sellbillandrutern = new Sellbillandrutern();
$SellbillandruternDAO = new SellbillandruternMySqlDAO();
$SellbillandruternEX = new SellbillandruternMySqlExtDAO();


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

//Sellbill
$Sellbill = new Sellbill();
$SellbillDAO = new SellbillMySqlDAO();
$SellbillEX = new SellbillMySqlExtDAO();



//check and use the condetion that suite this action
if (empty($do)) {
    include_once("../public/authentication.php");
    $smarty->display("usersratesreportsview/show.html");
} elseif ($do == "show") {
    include_once("../public/authentication.php");


    $allsellier = loadseller();
    $smarty->assign('allsellier', $allsellier);
    showAll();

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


    $smarty->display("usersratesreportsview/show.html");
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
$smarty->assign("customUser", 1);
//here the global templates
$smarty->display("footer.html");

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

// show the form
function showAll() {
    //to use the variable out side the funcion

    global $myUserRecord;
    global $myUserEx;

    global $SellbillandruternEX;
    global $SellbillEX;
    global $ReturnsellbillEX;

    global $smarty;

    $sellerid = $_POST["sellerid"];
    $startDate = $_POST["startDate"];
    $endDate = $_POST["endDate"];
    $priceType = filter_input(INPUT_POST, 'priceType');
    if ($priceType == -1) {
        $priceType = '0,1,2';
    }
    $totalPiecesNo = 0;
    if ((!$sellerid || $sellerid == -1) && !$startDate && !$endDate) {

        $userData = $myUserEx->queryWithLevel(3);
        $usersum = (count($userData));
        $awardrate = 0;
        $awardrategomla=0;
        $awardratehalf=0;


         $azmy = array();
        $existId = array();
        $selldata = $SellbillEX->queryAllforrateallWithoutSeller();
        foreach ($selldata as $key => $value) {

            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }
//            var_dump($value);
            $myclient->username = $value->username;
            $myclient->totalsellbill += count($value);
            $myclient->totalQuan += $value->sellQuantity;
            $myclient->sellbilltotalbill += $value->sellbillaftertotalbill;
            $myclient->billtotalpayed += $value->sellbilltotalpayed;
            if ($key == -1) {
                array_push($azmy , $myclient);
            }
        }
        $Returnselldata = $ReturnsellbillEX->queryAllforrateallWithoutSeller();
        foreach ($Returnselldata as $key => $value) {

            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }

            $myclient->username = $value->username;
            $myclient->totalsellbill -= count($value);
            $myclient->totalQuan -= $value->returnsellQuantity;
            $myclient->sellbilltotalbill -= $value->returnsellbillaftertotalbill;
            $myclient->billtotalpayed -= $value->returnsellbilltotalpayed;

            if ($key == -1) {
                array_push($azmy, $myclient);
            }
        }

        $Sellbillandruterndata = $SellbillandruternEX->queryAllforrateallWithoutSeller();
        foreach ($Sellbillandruterndata as $key => $value) {
            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }
            if ($value->selltype == 0) {
                $rtnsellbill = array();
                array_push($rtnsellbill,$value);
                $totsellbill = count($rtnsellbill);

            } else {
                $rtnrturn = array();
                array_push($rtnsellbill,$value);
                $totrtnsellbill = count($rtnrturn);

            }
            $myclient->username = $value->username;
            $myclient->totalsellbill += $totsellbill - $totrtnsellbill;
            if ($value->sellbillaftertotalbill >= 0) {
                $myclient->totalQuan += $value->sellQuantity - $value->returnsellQuantity;
                $myclient->sellbilltotalbill += $value->sellbillaftertotalbill;
                $myclient->billtotalpayed += $value->sellbilltotalpayed;
            } else {
                $myclient->totalQuan -= $value->sellQuantity - $value->returnsellQuantity;
                $myclient->sellbilltotalbill -= $value->sellbillaftertotalbill;
                $myclient->billtotalbill -= $value->sellbilltotalpayed;
            }


            if ($key == -1) {
                array_push($azmy, $myclient);
            }
        }
//            var_dump($azmy);


        /* print_r('totalsell'.$totalsell.'<br>');
          print_r('totalReturnsell'.$totalReturnsell.'<br>');
          print_r('totalSellbillandrutern'.$totalSellbillandrutern.'<br>'); */
        foreach ($userData as $myuserData) {
            $sellerid = $myuserData->userid;
            $userData = $myUserRecord->load($sellerid);

            //print_r($userData);
        $awardrate = ($awardrate+$awardrategomla+$awardratehalf) / $usersum;
            $awardrategomla += $userData->awardrategomla;
            $awardratehalf += $userData->awardratehalf;
            
            // assign your db results to the template
        }
        //print_r('awardrate'.$awardrate.'<br>');
        //print_r('userData'.count($userData).'<br>');
        $awardrate = $awardrate / $usersum;
        $message = "تقرير نسبه كل  البائعين لهذا الشهر ";
        $smarty->assign('message', $message);
        $smarty->assign('azmy', $azmy);

    } elseif ($sellerid != '-1' && isset($sellerid) && $sellerid != '0' && $startDate != '' && isset($startDate) && $endDate != '' && isset($endDate)) {



        $azmy = array();
        $existId = array();
        $selldata = $SellbillEX->queryAllforrateWithSellerNameDistinct($sellerid, $startDate, $endDate . ' 23:59:59', $priceType);
        foreach ($selldata as $key => $value) {

            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }
//            var_dump($value);
            $myclient->username = $value->username;
            $myclient->totalsellbill += count($value);
            $myclient->totalQuan += $value->sellQuantity;
            $myclient->sellbilltotalbill += $value->sellbillaftertotalbill;
            $myclient->billtotalpayed += $value->sellbilltotalpayed;
            if ($key == -1) {
                array_push($azmy , $myclient);
            }
        }
        $Returnselldata = $ReturnsellbillEX->queryAllforrateWithSellerName($sellerid, $startDate, $endDate . ' 23:59:59', $priceType);
        foreach ($Returnselldata as $key => $value) {

            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }
//            var_dump($value);

            $myclient->username = $value->username;
            $myclient->totalsellbill -= count($value);
            $myclient->totalQuan -= $value->returnsellQuantity;
            $myclient->sellbilltotalbill -= $value->returnsellbillaftertotalbill;
            $myclient->billtotalpayed -= $value->returnsellbilltotalpayed;

            if ($key == -1) {
                array_push($azmy, $myclient);
            }
        }

        $Sellbillandruterndata = $SellbillandruternEX->queryAllforrateWithSellerName($sellerid, $startDate, $endDate . ' 23:59:59', $priceType);
        foreach ($Sellbillandruterndata as $key => $value) {
            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }
            if ($value->selltype == 0) {
                $rtnsellbill = array();
                array_push($rtnsellbill,$value);
                $totsellbill = count($rtnsellbill);

            } else {
                $rtnrturn = array();
                array_push($rtnsellbill,$value);
                $totrtnsellbill = count($rtnrturn);

            }
            $myclient->username = $value->username;
            $myclient->totalsellbill += $totsellbill - $totrtnsellbill;
            if ($value->sellbillaftertotalbill >= 0) {
                $myclient->totalQuan += $value->sellQuantity - $value->returnsellQuantity;
                $myclient->sellbilltotalbill += $value->sellbillaftertotalbill;
                $myclient->billtotalpayed += $value->sellbilltotalpayed;
            } else {
                $myclient->totalQuan -= $value->sellQuantity - $value->returnsellQuantity;
                $myclient->sellbilltotalbill -= $value->sellbillaftertotalbill;
                $myclient->billtotalbill -= $value->sellbilltotalpayed;
            }


            if ($key == -1) {
                array_push($azmy, $myclient);
            }
        }

        /*  print_r('totalsell'.$totalsell.'<br>');
          print_r('totalReturnsell'.$totalReturnsell.'<br>');
          print_r('totalSellbillandrutern'.$totalSellbillandrutern.'<br>'); */

        $userData = $myUserRecord->load($sellerid);
        $message = "تقرير نسبه موظف من تاريخ :" . $startDate . " الى تاريخ : " . $endDate . " للموظف :" . $userData->employeename;
        $smarty->assign('message', $message);
        //print_r($userData);

        // assign your db results to the template
        $smarty->assign('azmy', $azmy);

    } elseif ($sellerid != '-1' && isset($sellerid) && $sellerid == '0' && $startDate != '' && isset($startDate) && $endDate != '' && isset($endDate)) {

        $userData = $myUserEx->queryWithLevel(3);
        $usersum = (count($userData));
        $awardrate = 0;
        $awardrategomla = 0;
        $awardratehalf = 0;



        $azmy = array();
        $existId = array();
        $selldata = $SellbillEX->queryAllforrateallWithSellerName($startDate, $endDate . ' 23:59:59', $priceType);
        foreach ($selldata as $key => $value) {

            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }
//            var_dump($value);
            $myclient->username = $value->username;
            $myclient->totalsellbill += count($value);
            $myclient->totalQuan += $value->sellQuantity;
            $myclient->sellbilltotalbill += $value->sellbillaftertotalbill;
            $myclient->billtotalpayed += $value->sellbilltotalpayed;
            if ($key == -1) {
                array_push($azmy , $myclient);
            }
        }

        $Returnselldata = $ReturnsellbillEX->queryAllforrateallWithSellerName($startDate, $endDate . ' 23:59:59', $priceType);
        foreach ($Returnselldata as $key => $value) {

            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }
//            var_dump($value);

            $myclient->username = $value->username;
            $myclient->totalsellbill -= count($value);
            $myclient->totalQuan -= $value->returnsellQuantity;
            $myclient->sellbilltotalbill -= $value->returnsellbillaftertotalbill;
            $myclient->billtotalpayed -= $value->returnsellbilltotalpayed;

            if ($key == -1) {
                array_push($azmy, $myclient);
            }
        }


        $Sellbillandruterndata = $SellbillandruternEX->queryAllforrateallWithSellerName($startDate, $endDate . ' 23:59:59', $priceType);
        foreach ($Sellbillandruterndata as $key => $value) {
            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }
            if ($value->selltype == 0) {
                $rtnsellbill = array();
                array_push($rtnsellbill,$value);
                $totsellbill = count($rtnsellbill);

            } else {
                $rtnrturn = array();
                array_push($rtnsellbill,$value);
                $totrtnsellbill = count($rtnrturn);

            }
            $myclient->username = $value->username;
            $myclient->totalsellbill += $totsellbill - $totrtnsellbill;
            if ($value->sellbillaftertotalbill >= 0) {
                $myclient->totalQuan += $value->sellQuantity - $value->returnsellQuantity;
                $myclient->sellbilltotalbill += $value->sellbillaftertotalbill;
                $myclient->billtotalpayed += $value->sellbilltotalpayed;
            } else {
                $myclient->totalQuan -= $value->sellQuantity - $value->returnsellQuantity;
                $myclient->sellbilltotalbill -= $value->sellbillaftertotalbill;
                $myclient->billtotalbill -= $value->sellbilltotalpayed;
            }


            if ($key == -1) {
                array_push($azmy, $myclient);
            }
        }


        /* print_r('totalsell'.$totalsell.'<br>');
          print_r('totalReturnsell'.$totalReturnsell.'<br>');
          print_r('totalSellbillandrutern'.$totalSellbillandrutern.'<br>'); */
        foreach ($userData as $myuserData) {
            $sellerid = $myuserData->userid;
            $userData = $myUserRecord->load($sellerid);

            //print_r($userData);
            $awardrate+=$userData->awardrate;
            $awardrategomla+=$userData->awardrategomla;
            $awardratehalf+=$userData->awardratehalf;
            
            // assign your db results to the template
        }
        //print_r('awardrate'.$awardrate.'<br>');
        //print_r('userData'.count($userData).'<br>');

        $awardrate = ($awardrate+$awardrategomla+$awardratehalf) / $usersum;
        $message = "تقرير نسبه كل  الموظفين من تاريخ :" . $startDate . " الى تاريخ : " . $endDate . "  ";
        $smarty->assign('message', $message);

        $smarty->assign('azmy', $azmy);
    } elseif ($sellerid != '-1' && isset($sellerid) && $sellerid != '0' && !isset($startDate) && !isset($endDate)) {

        $azmy = array();
        $existId = array();
        $selldata = $SellbillEX->queryAllforrateWithSellerNameDistinct($sellerid, $startDate, $endDate . ' 23:59:59', $priceType);
        foreach ($selldata as $key => $value) {

            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }
//            var_dump($value);
            $myclient->username = $value->username;
            $myclient->totalsellbill += count($value);
            $myclient->totalQuan += $value->sellQuantity;
            $myclient->sellbilltotalbill += $value->sellbillaftertotalbill;
            $myclient->billtotalpayed += $value->sellbilltotalpayed;
            if ($key == -1) {
                array_push($azmy , $myclient);
            }
        }
        $Returnselldata = $ReturnsellbillEX->queryAllforrateWithSellerName($sellerid, $startDate, $endDate, $priceType);
        foreach ($Returnselldata as $key => $value) {

            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }
//            var_dump($value);

            $myclient->username = $value->username;
            $myclient->totalsellbill -= count($value);
            $myclient->totalQuan -= $value->returnsellQuantity;
            $myclient->sellbilltotalbill -= $value->returnsellbillaftertotalbill;
            $myclient->billtotalpayed -= $value->returnsellbilltotalpayed;

            if ($key == -1) {
                array_push($azmy, $myclient);
            }
        }

        $Sellbillandruterndata = $SellbillandruternEX->queryAllforrateWithSellerName($sellerid, $startDate, $endDate, $priceType);
        foreach ($Sellbillandruterndata as $key => $value) {
            if (in_array($value->sellerid, $existId)) {
                $key = array_search($value->sellerid, $existId);
                $myclient = $azmy[$key];
            } else {
                $key = -1;
                $myclient = new User();
                $myclient->sellerid = $value->sellerid;
                array_push($existId, $value->sellerid);
            }
            if ($value->selltype == 0) {
                $rtnsellbill = array();
                array_push($rtnsellbill,$value);
                $totsellbill = count($rtnsellbill);

            } else {
                $rtnrturn = array();
                array_push($rtnsellbill,$value);
                $totrtnsellbill = count($rtnrturn);

            }
            $myclient->username = $value->username;
            $myclient->totalsellbill += $totsellbill - $totrtnsellbill;
            if ($value->sellbillaftertotalbill >= 0) {
                $myclient->totalQuan += $value->sellQuantity - $value->returnsellQuantity;
                $myclient->sellbilltotalbill += $value->sellbillaftertotalbill;
                $myclient->billtotalpayed += $value->sellbilltotalpayed;
            } else {
                $myclient->totalQuan -= $value->sellQuantity - $value->returnsellQuantity;
                $myclient->sellbilltotalbill -= $value->sellbillaftertotalbill;
                $myclient->billtotalbill -= $value->sellbilltotalpayed;
            }


            if ($key == -1) {
                array_push($azmy, $myclient);
            }
        }

        /* print_r('totalsell'.$totalsell.'<br>');
          print_r('totalReturnsell'.$totalReturnsell.'<br>');
          print_r('totalSellbillandrutern'.$totalSellbillandrutern.'<br>'); */

        $userData = $myUserRecord->load($sellerid);
        $message = "تقرير نسبه موظف من تاريخ :" . $startDate . " الى تاريخ : " . $endDate . " للموظف :" . $userData->employeename;
        $smarty->assign('message', $message);
        //print_r($userData);
        $awardrate = $userData->awardrate;
        $awardrategomla = $userData->awardrategomla;
        $awardratehalf = $userData->awardratehalf;
        

        $smarty->assign('azmy', $azmy);
    }
}

// select from user tbl
function loadseller() {

    //to use the variable out side the funcion
    global $myUserEx;

    // select all data from user
    $userData = $myUserEx->queryWithLevel(3);

    return $userData;
}

?>