<?php

//the global file operation
include("../public/impOpreation.php");
include_once("../library/uploadImages.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");

//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 :- clientController

  OPERTATION in Controller

  1- display add form
  2- add in client tbl
  3- display show and tempdelete
  4- edit client
  5- update client
  5.1- update client tbl with clientid

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

//paymentnetworks
$insurancecompany = new Insurancecompanie();
$insuranceCompanyDAO = new InsurancecompaniesMySqlDAO();
$insuranceCompanyEX = new InsurancecompaniesMySqlExtDAO();

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



$today = date("Y-m-d");


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

    //here the smarty templates
    $smarty->display("insuranceview/add.html");
}
####
elseif ($do == "add") {
    include_once("../public/authentication.php");

    try {
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}
####
elseif ($do == "show") {
    //here the permssion check
    include_once("../public/authentication.php");

    //$insurancecompany = $insuranceCompanyDAO->queryByDeleted(0);
    $insurancecompany = $insuranceCompanyDAO->queryAll();
    foreach ($insurancecompany as $comp) {
        if ($comp->contractend < date('Y-m-d') && $comp->deleted != 1) {
            $comp->deleted = 2;
        }
    }
    $smarty->assign('insuranceCompanies', $insurancecompany);
    $smarty->display("insuranceview/show.html");
}
####
elseif ($do == "delete") {
    include_once("../public/authentication.php");

    $companyId = filter_input(INPUT_GET, "id");
    $insurancecompany = $insuranceCompanyDAO->load($companyId);
    $insurancecompany->deleted = 1;
    try {
        $insuranceCompanyDAO->update($insurancecompany);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}

####
elseif ($do == "edit") {//edit  client
    include_once("../public/authentication.php");
    $companyId = filter_input(INPUT_GET, "id");

    $insurancecompany = $insuranceCompanyDAO->load($companyId);
    $smarty->assign('company', $insurancecompany);

    $smarty->display("insuranceview/edit.html");
}
####
elseif ($do == "update") { //edit update
    include_once("../public/authentication.php");
    try {
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}
####
elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
}
####
elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}



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

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

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

// add in client tbl
function add() {
    global $insurancecompany;
    global $insuranceCompanyDAO;

    $companyName = filter_input(INPUT_POST, "companyName");
    $discountPercent = filter_input(INPUT_POST, "discountPercent");
    $contractStart = filter_input(INPUT_POST, "contractStart");
    $contractEnd = filter_input(INPUT_POST, "contractEnd");
    $instruction = filter_input(INPUT_POST, "instruction");


    $handle = new upload($_FILES['contractPhoto']);
    $path = "../upload/insuranceCompany";
    $contractPhoto = uploadImages2($handle, $path);

    $insurancecompany->name = $companyName;
    $insurancecompany->discountpercent = $discountPercent;
    $insurancecompany->contractstart = $contractStart;
    $insurancecompany->contractend = $contractEnd;
    $insurancecompany->contractphoto = $contractPhoto;
    $insurancecompany->instruction = $instruction;
    $insurancecompany->userid = $_SESSION['userid'];
    $insurancecompany->deleted = 0;

    $insuranceCompanyDAO->insert($insurancecompany);
}

//show
function show() {
    global $clientDAO;
    global $clientExt;
    global $today;
    global $smarty;

    $alldata = $clientDAO->queryAll();
    $smarty->assign("alldata", $alldata);

    $shownData = $clientExt->queryAllForShow();

    $sum;
    foreach ($shownData as $data) {
        if ($data->conditions == 0) {
            $sum += $data->clientdebt;
        }
    }
    $smarty->assign('sum', $sum);

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


function update() {
    global $insurancecompany;
    global $insuranceCompanyDAO;

    $companyId = filter_input(INPUT_POST, "companyId");
    $companyName = filter_input(INPUT_POST, "companyName");
    $discountPercent = filter_input(INPUT_POST, "discountPercent");
    $contractStart = filter_input(INPUT_POST, "contractStart");
    $contractEnd = filter_input(INPUT_POST, "contractEnd");
    $instruction = filter_input(INPUT_POST, "instruction");

    $insurancecompany = $insuranceCompanyDAO->load($companyId);

    if ($_FILES['contractPhoto']['name']) {
        $handle = new upload($_FILES['contractPhoto']);
        $path = "../upload/insuranceCompany";
        $contractPhoto = uploadImages2($handle, $path);
        $insurancecompany->contractphoto = $contractPhoto;
    }

    $insurancecompany->name = $companyName;
    $insurancecompany->discountpercent = $discountPercent;
    $insurancecompany->contractstart = $contractStart;
    $insurancecompany->contractend = $contractEnd;
    $insurancecompany->instruction = $instruction;
    $insurancecompany->userid = $_SESSION['userid'];

    $insuranceCompanyDAO->update($insurancecompany);
}
?>