<?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");

require_once('../models/dao/EmployeeDAO.class.php');
require_once('../models/dto/Employee.class.php');
require_once('../models/mysql/EmployeeMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeeMySqlExtDAO.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');

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

$smarty->display("header.html");


//create object  from employee
$employeeDAO = new EmployeeMySqlDAO();
$employee = new Employee();
$employeeEX = new EmployeeMySqlExtDAO();

$EmployeepersonnelDAO = new EmployeepersonnelMySqlDAO();
$Employeepersonnel = new Employeepersonnel();
$EmployeepersonnelEX = new EmployeepersonnelMySqlExtDAO();

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

//create object from user
//dispaly  add form
if (empty($do) || $do == "show") {
    include_once("../public/authentication.php");

    $queryString = '';
    if ($_SESSION['branchId'] > 0)
        $queryString = ' AND  branchid = ' . $_SESSION['branchId'];

    $allemployee = $employeeEX->queryAllSimple($queryString);
    $smarty->assign('allemployee', $allemployee);

    $empid = filter_input(INPUT_POST, "chooseEmp");
    $smarty->assign('empid', $empid);

    $fromdate = filter_input(INPUT_POST, "fromdate");
    $smarty->assign('fromdate', $fromdate);

    $todate = filter_input(INPUT_POST, "todate");
    $smarty->assign('todate', $todate);

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

    $queryString = "";
    if (!empty($empid)) {

        $employeeData = $employeeDAO->load($empid);

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

        $salary1 = $salary2 = $salary3 = $salary4 = $salary5 = $salary6 = $salary7 = $salary8 = '0';
        if ($fromdate && $todate) {
            $todate = $todate . ' 23:59:59';
            $fromdate = $fromdate . " 00:00:00";
            $prsonalEmployee = R::getAll('SELECT * FROM employeepersonnel WHERE employeeid = ' . $empid . ' and employeepersonneldate  >= "' . $fromdate . '" AND employeepersonneldate <= "' . $todate . '"');

            foreach ($prsonalEmployee as $persData) {
                if ($persData['employeepersonneltype'] == 1 && $persData['conditions'] == 0) {
                    $salary1 += $persData['employeepersonnelvalue']; // مكافاة
                } elseif ($persData['employeepersonneltype'] == 2 && $persData['conditions'] == 0) {
                    $salary2 += $persData['employeepersonnelvalue']; // خصم
                } elseif ($persData['employeepersonneltype'] == 3 && $persData['conditions'] == 0) {
                    $salary3 += $persData['employeepersonnelvalue']; // سحب
                } elseif ($persData['employeepersonneltype'] == 4 && $persData['conditions'] == 0) {
                    $salary4 += $persData['employeepersonnelvalue']; // سلفة
                } elseif ($persData['employeepersonneltype'] == 9 && $persData['conditions'] == 0) {
                    $salary5 += $persData['employeepersonnelvalue']; // غياب
                } elseif ($persData['employeepersonneltype'] == 5 && $persData['conditions'] == 0) {
                    $salary5 += $persData['employeepersonnelvalue']; // تأخير
                } elseif ($persData['employeepersonneltype'] == 7 && $persData['conditions'] == 0) {
                    $salary7 += $persData['employeepersonnelvalue']; // سداد سلفة من المرتب
                } elseif ($persData['employeepersonneltype'] == 11 && $persData['conditions'] == 0) {
                    $salary6 += $persData['employeepersonnelvalue']; // استئذان
                } elseif ($persData['employeepersonneltype'] == 14 && $persData['conditions'] == 0) {
                    $salary8 += $persData['employeepersonnelvalue']; // انصراف
                }
            }
        } else {
            $prsonalEmployee = $EmployeepersonnelDAO->queryByEmployeeid($empid);
            foreach ($prsonalEmployee as $persData) {
                if ($persData->employeepersonneltype == 1 && $persData->conditions == 0) {
                    $salary1 += $persData->employeepersonnelvalue; // مكافاة
                } elseif ($persData->employeepersonneltype == 2 && $persData->conditions == 0) {
                    $salary2 += $persData->employeepersonnelvalue; // خصم
                } elseif ($persData->employeepersonneltype == 3 && $persData->conditions == 0) {
                    $salary3 += $persData->employeepersonnelvalue; // سحب
                } elseif ($persData->employeepersonneltype == 4 && $persData->conditions == 0) {
                    $salary4 += $persData->employeepersonnelvalue; // سلفة
                } elseif ($persData->employeepersonneltype == 9 && $persData->conditions == 0) {
                    $salary5 += $persData->employeepersonnelvalue; // غياب
                } elseif ($persData->employeepersonneltype == 5 && $persData->conditions == 0) {
                    $salary5 += $persData->employeepersonnelvalue; // تأخير
                } elseif ($persData->employeepersonneltype == 7 && $persData->conditions == 0) {
                    $salary7 += $persData->employeepersonnelvalue; // سداد سلفة من المرتب
                } elseif ($persData->employeepersonneltype == 11 && $persData->conditions == 0) {
                    $salary6 += $persData->employeepersonnelvalue; //استئذان
                } elseif ($persData->employeepersonneltype == 14 && $persData->conditions == 0) {
                    $salary8 += $persData->employeepersonnelvalue; // انصراف
                }
            }
        }

        // print_r($salary1 . '<br>' . $salary2 . '<br>' . $salary3 . '<br>' . $salary4 . '<br>' . $salary5 . '<br>' . $salary7);
        $salary = array();
        array_push($salary, $salary1, $salary2, $salary3, $salary4, $salary5, $salary7, $salary6, $salary8);
        $smarty->assign('salary', $salary);
        $bonuses = $_POST['bonuses'];
        $smarty->assign('sum', $bonuses);
    }


    $smarty->assign("customMarchSalary", 1);

    $smarty->display("marchesSalaries/show.html"); //the template page
} elseif ($do == 'editprint') {
    $empSalary = $_POST['empSalary']; // المرتب الاساسى
    $fromdate = $_POST['fromdate']; // من تاريخ
    $todate = $_POST['todate']; // إلى تاريخ
    $bonuses = $_POST['bonuses']; // المكافأت
    $houseAllowance = $_POST['houseAllowance']; // بدل سكن
    $payAdvances = $_POST['payAdvances']; // دفع السلف
    $transportationAllowance = $_POST['transportationAllowance']; // بدل المواصلات
    $otherAllowance = $_POST['otherAllowance']; // بدلات اخرى
    $absence_delay = $_POST['absence_delay']; // تاخير-غياب
    $discounts = $_POST['discounts']; // الخصومات
    $withdrawals = $_POST['withdrawals']; // المسحوبات
    $predecessor = $_POST['predecessor']; // السلف
    $permission = $_POST['permission']; // الاستئذان
    $leave = $_POST['leave']; // الانصراف
    $employeId = filter_input(INPUT_POST, 'emplId');
    $employeeData = $employeeDAO->load($employeId);
    $empName = $employeeData->employeeName; // اسم الموظف
    $eradat = $bonuses + $houseAllowance + $payAdvances + $transportationAllowance + $otherAllowance + $empSalary; // جمع الايرادات
    $disc = $absence_delay + $discounts + $withdrawals + $predecessor + $permission + $leave; // جمع المصروفات
    $sub = $eradat - $disc; // صافى المرتب

    $prnData = array();
    array_push($prnData, $empSalary, $bonuses, $houseAllowance, $payAdvances, $transportationAllowance, $otherAllowance, $absence_delay, $discounts, $withdrawals, $predecessor, $sub, $empName, $eradat, $disc, $fromdate, $todate, $permission, $leave);


    $smarty->assign('prnData', $prnData);
    $smarty->display("marchesSalaries/editprint.html"); //the template page
}
//success
elseif ($_GET['do'] == "sucess") {
    $smarty->display("succes.html");
} elseif ($_GET['do'] == "error") {
    $smarty->display("error.html");
}
$smarty->assign("customValidation", 1);
$smarty->display("footer.html");
