<?php

session_start();
ob_start();
//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");

//Savedaily
require_once('../models/dao/SavedailyDAO.class.php');
require_once('../models/dto/Savedaily.class.php');
require_once('../models/mysql/SavedailyMySqlDAO.class.php');
require_once('../models/mysql/ext/SavedailyMySqlExtDAO.class.php');

//Save
require_once('../models/dao/SaveDAO.class.php');
require_once('../models/dto/Save.class.php');
require_once('../models/mysql/SaveMySqlDAO.class.php');
require_once('../models/mysql/ext/SaveMySqlExtDAO.class.php');




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


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

  Controller Name :- savedailyAjaxCTRL

  OPERTATION in Controller


  1-display show form where search by saveid using ajax


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


//here goes the instances and general variables


$mySavedailyRecord = new SavedailyMySqlDAO();
$mySavedailyEx = new SavedailyMySqlExtDAO();

$mySaveRecord = new SaveMySqlDAO();
$mySaveEx = new SaveMySqlExtDAO();



//check and use the condetion that suite this action
if ($do == "show") {


    // here the function that do the action
    $saveData = loadSave();
    $smarty->assign("saveData", $saveData);


    $saveId = $_REQUEST['saveid'];
    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];


    if (isset($saveId) && $saveId != "-1" && empty($startDate) && empty($endDate)) {

        $savedailyData = showBysaveName();
        $sumValue = sumCreditById($saveId);
    } elseif (isset($startDate) && isset($endDate) && $startDate != "" && $endDate != "" && isset($saveId) && $saveId != "-1") {


        $savedailyData = showByDateAndSave();
        $sumValue = sumCreditById($saveId);
    }


    $smarty->assign('savedailyData', $savedailyData);
    $smarty->assign('sumValue', $sumValue);


    //here the smarty templates
    $smarty->display("savedailyview/savedaily.html");
} elseif ($do == "getSavesAjax") {
    $row_array = array();
    $return_arr = array();

    $name = $_GET['term']; //It could be product name or category name
    $limit = intval($_GET['page_limit']);

    $queryString = '';
    if ($_SESSION['searchinonesave'] == 0) {
        if ($_SESSION['saveids'] == 0) {
            //all saves
        } else {
            //user saves only
            $queryString = ' and saveid in (' . $_SESSION['saveids'] . ')';
        }
    } else {
        //user only save
        $queryString = ' and saveid in (' . $_SESSION['saveid'] . ')';
    }


    $saves = $mySaveEx->queryAllEX($name, $queryString);
    foreach ($saves as $row) {
        $row_array['id'] = $row->saveid;
        $row_array['text'] = $row->savename;
        array_push($return_arr, $row_array);
    }

//print_r($clients);
    echo json_encode($return_arr);
}





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

//select all save data
function loadSave() {

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

    //load all save data
    $saveData = $mySaveRecord->queryByConditions(0);

    return $saveData;
}

function sumCreditById($id) {
    //to use the variable out side the funcion
    global $mySaveRecord;

    //load data from save tbl by id
    $saveData = $mySaveRecord->load($id);


    //get the sum of value in save
    $savecurrentValue = $saveData->savecurrentvalue;
    $sumValue = $savecurrentValue;

    return $sumValue;
}

// show the form by save name
function showBysaveName() {
    //to use the variable out side the funcion
    global $mySavedailyEx;

    $saveId = $_REQUEST['saveid'];

    //select all data from Savedaily by saveid
    $savedailyData = $mySavedailyEx->queryAllSave($saveId);

    return $savedailyData;
}

// show the form by date
function showByDateAndSave() {
    //to use the variable out side the funcion
    global $mySavedailyEx;


    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $saveId = $_REQUEST['saveid'];

    //select all data from Savedaily by currentdate
    $savedailyData = $mySavedailyEx->queryWithDateAndSaveId($saveId, $startDate, $endDate);

    return $savedailyData;
}

?>