File: /home/mostafedeg/public_html/erp/controllers/saveController.php
<?php
//the global file operation
include("../public/impOpreation.php");
include_once("initiateStaticSessionCommingWithCurl.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");
//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');
//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');
require_once('../models/dao/CurrencyDAO.class.php');
require_once('../models/dto/Currency.class.php');
require_once('../models/mysql/CurrencyMySqlDAO.class.php');
require_once('../models/mysql/ext/CurrencyMySqlExtDAO.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');
//Breadcrumb
require_once("../library/breadcrumb.php");
include_once("dailyentryfun.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 :- saveCTRL اضافة وتعديل وعرض الخزن
OPERTATION in Controller
1-display add form
2- add in Save tbl
3-insert into savedaily tbl
4-display sucess or error
5-display show form
6-edit in Save data
======================== */
//here the global templates
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
} else {
$smarty->display("header.html");
}
//here goes the instances and general variables
$mySave = new Save();
$mySaveRecord = new SaveMySqlDAO();
$mySaveEx = new SaveMySqlExtDAO();
//Accountstree
$accountsTree = new Accountstree();
$accountsTreeDAO = new AccountstreeMySqlDAO();
$accountsTreeEX = new AccountstreeMySqlExtDAO();
//Savedaily
$mySavedaily = new Savedaily();
$mySavedailyRecord = new SavedailyMySqlDAO();
//Currency
$currency = new Currency();
$currencyDAO = new CurrencyMySqlDAO();
$currencyEX = new CurrencyMySqlExtDAO();
//Dailyentry
$dailyEntry = new Dailyentry();
//Dailyentrycreditor دائن
$dailyEntryCreditor = new Dailyentrycreditor();
//Dailyentrydebtor مدين
$dailyEntryDebtor = new Dailyentrydebtor();
//Breadcrumb
$breadcrumbObject = new Breadcrumb();
$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//check and use the condetion that suite this action
if (empty($do)) {
//here the permssion check
include_once("../public/authentication.php");
//breadcrumb
$breadcrumbObject->add('الادارة><a href="saveController.php?do=show"> الخزن </a> > اضافة خزنة', 'saveController.php', 0);
//Now output the navigation.
$breadCrumb = $breadcrumbObject->output();
$smarty->assign("breadCrumb", $breadCrumb);
$allCurrency = $currencyDAO->queryByConditions(0);
$smarty->assign("allCurrency", $allCurrency);
$allSaveType = R::getAll('select name,id from savetype where del != 2');
$smarty->assign("allSaveType", $allSaveType);
//here the smarty templates
$smarty->display("saveview/add.html");
} elseif ($do == "add") {
//here the permssion check
include_once("../public/authentication.php");
try {
// here the function that do the action
$flag = add();
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
switch ($flag) {
case 1:
$data = array('status' => 2, 'message' => 'تم ادخال اسم الخزنة من قبل', 'message_en' => 'This save has been added before');
break;
case 2:
$data = array('status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success');
break;
case 3:
$data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
break;
}
echo json_encode($data);
} else {
if ($flag == 1) {
$smarty->assign('msg', 'تم ادخال اسم الخزنة من قبل');
$smarty->display('saveview/add.html');
} else {
header("location:?do=sucess");
}
}
} catch (Exception $e) {
header("location:?do=error");
}
} elseif ($do == "show") {
//here the permssion check
include_once("../public/authentication.php");
$youtubes = $youtubeLinkDAO->queryAll();
$smarty->assign("youtubes", $youtubes);
$smarty->assign("savetypeids", $_POST['savetypeids']);
$allSaveType = R::getAll('select name,id from savetype where del != 2');
$smarty->assign("allSaveType", $allSaveType);
// here the function that do the action
$saveData = show();
$smarty->assign("saveData", $saveData);
//breadcrumb
$breadcrumbObject->add('الادارة><a href="saveController.php?do=show"> الخزن </a> > عرض الخزن', 'saveController.php', 0);
//Now output the navigation.
$breadCrumb = $breadcrumbObject->output();
$smarty->assign("breadCrumb", $breadCrumb);
//here the smarty templates
$smarty->display("saveview/show.html");
$smarty->assign("customCheck", 1);
} elseif ($do == "delete") {
//here the permssion check
include_once("../public/authentication.php");
try {
// here the function that do the action
$flag = delete();
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
$data = array('status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success');
echo json_encode($data);
} else {
header("location:?do=sucess");
}
} catch (Exception $e) {
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
$data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
echo json_encode($data);
} else {
header("location:?do=error");
}
}
} elseif ($do == "deleteFinaly") {
//here the permssion check
include_once("../public/authentication.php");
try {
// here the function that do the action
deleteFinaly();
header("location:?do=sucess");
} catch (Exception $e) {
header("location:?do=error");
}
} elseif ($do == "editprint") {
//here the permssion check
include_once("../public/authentication.php");
$loadData = editprint();
$smarty->assign("loadData", $loadData);
//breadcrumb
$breadcrumbObject->add('الادارة><a href="saveController.php?do=show"> الخزن </a> > عرض خزنة', 'saveController.php', 0);
//Now output the navigation.
$breadCrumb = $breadcrumbObject->output();
$smarty->assign("breadCrumb", $breadCrumb);
//here the smarty templates
$smarty->display("saveview/editprint.html");
$smarty->assign("customPrint", 1);
} elseif ($do == "edit") {
//here the permssion check
include_once("../public/authentication.php");
$loadData = edit();
$smarty->assign("loadData", $loadData);
$allSaveType = R::getAll('select name,id from savetype where del != 2');
$smarty->assign("allSaveType", $allSaveType);
//breadcrumb
$breadcrumbObject->add('الادارة><a href="saveController.php?do=show"> الخزن </a> > تعديل خزنة', 'saveController.php', 0);
//Now output the navigation.
$breadCrumb = $breadcrumbObject->output();
$smarty->assign("breadCrumb", $breadCrumb);
//here the smarty templates
$smarty->display("saveview/edit.html");
} elseif ($do == "update") {
//here the permssion check
include_once("../public/authentication.php");
try {
// here the function that do the action
$flag = update();
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
switch ($flag) {
case 1:
$data = array('status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success');
break;
case 2:
$data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
break;
}
echo json_encode($data);
} else {
header("location:?do=sucess");
}
} catch (Exception $e) {
header("location:?do=error");
}
} elseif ($do == "sortby") {
$id = $_POST['id'];
$sortby = $_POST['sortby'];
R::exec("UPDATE `save` SET `sortby`= $sortby WHERE saveid = $id ");
} elseif ($do == "sucess") {
//here the smarty templates
$smarty->display("succes.html");
} elseif ($do == "error") {
//here the smarty templates
$smarty->display("error.html");
}
$smarty->assign("customSave", 1);
$smarty->assign("customValidation", 1);
//here the global templates
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
} else {
$smarty->display("footer.html");
}
/* ===============================
function in this CONTROLLER
================================ */
// add in Save tbl
function add()
{
//to use the variable out side the funcion
global $mySave;
global $mySaveRecord;
//Dailyentry
global $dailyEntry;
//Dailyentrycreditor دائن
global $dailyEntryCreditor;
//Dailyentrydebtor مدين
global $dailyEntryDebtor;
//Transaction
$mytransactions = new Transaction();
//post variable
$saveName = $_POST['savename'];
$savecurrentValue = $_POST['savecurrentvalue'];
$saveDetails = $_POST['savedetails'];
$currencyId = $_POST['currencyId'];
$sortby = (int) $_POST['sortby'];
$saveTreeParentType = (int) $_POST['saveTreeParentType'];
$savetypeid = $_POST['savetypeid'];
//insert data in Save tbl
$mySave->savedate = date('Y-m-d H:i:s');
$mySave->conditions = 0;
$mySave->savecurrentvalue = $savecurrentValue;
$mySave->savename = $saveName;
$mySave->currencyId = $currencyId;
$mySave->sortby = $sortby;
$mySave->userid = $_SESSION['userid'];
$mySave->saveTreeParentType = $saveTreeParentType;
$mySave->webApiId = (int) $_POST['webApiId'];
$mySave->savetypeid = $savetypeid;
$dailyEntryDebtorArray = array();
$dailyEntryCreditorArray = array();
$dailyEntry->dDateTime = date('Y-m-d H:i:s');
$dailyEntry->entryComment = 'إضافة في ' . $saveName;
$dailyEntryDebtor->value = $savecurrentValue;
$dailyEntryCreditor->value = $savecurrentValue;
$dailyEntryCreditor->accountstreeid = 121;
$checkName = $mySaveRecord->queryBySavename($saveName);
if (!empty($checkName)) {
return 1;
}
try {
$saveId = $mySaveRecord->insert($mySave, $saveDetails);
//insert data in savedaily tbl
insertSavedaily(0, $savecurrentValue, 0, $saveId, "تم اضافة خزنة باسم [ " . $saveName . " ] ", $saveId, $savecurrentValue, "saveController.php");
$parent = 40;
if ($saveTreeParentType == 1) {
$parent = 408;
}
//add tree element
$treeId = addTreeElement($saveName, $parent, 3, 0, 1, '', 0, 0);
$mySave->treeId = $treeId;
$mySaveRecord->update($mySave, $saveDetails);
//add in daily entry
$dailyEntryDebtor->accountstreeid = $treeId;
array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
array_push($dailyEntryCreditorArray, $dailyEntryCreditor);
insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 1);
$mytransactions->commit();
$message = 'تمت العملية بنجاح';
return 2;
} catch (Exception $ex) {
$mytransactions->rollback();
$message = 'عفوا لقد حدث خطأ';
return 3;
}
}
// show the form
function show()
{
//to use the variable out side the funcion
global $mySaveEx;
//select all data from Save tbl
//$saveData = $mySaveEx->queryAllWithOrder();
$savetypeids = $_POST['savetypeids'];
$queryString = '';
if($savetypeids){
$queryString .= ' and savetypeid in (0,'.implode(",", $savetypeids).') ';
}
if ($_SESSION['searchinonesave'] == 0) {
if ($_SESSION['saveids'] == 0) {
$saveData = $mySaveEx->queryAllEX2($queryString);
} else {
$queryString .= ' and saveid in (' . $_SESSION['saveids'] . ')';
$saveData = $mySaveEx->queryAllEX2($queryString);
}
} else {
$queryString .= ' and saveid = ' . $_SESSION['saveid'] . ' ';
$saveData = $mySaveEx->queryAllEX2($queryString);
}
return $saveData;
}
//update condition in Save tbl
function delete()
{
//to use the variable out side the funcion
global $mySave;
global $mySaveEx;
//get variable
$condition = $_GET['condition'];
$saveId = $_GET['saveid'];
if ($condition == 1) {
$mySave->conditions = 0;
} else {
$mySave->conditions = 1;
}
//update tempdelete in Save tbl
$mySave->saveid = $saveId;
$mySave->savedate = date('Y-m-d H:i:s');
$mySave->userid = $_SESSION['userid'];
$mySaveEx->updateDele($mySave);
}
//finaly deleted from Save tbl
function deleteFinaly()
{
//to use the variable out side the funcion
global $mySaveRecord;
//get variable
$saveId = $_GET['saveid'];
$saveData = $mySaveRecord->load($saveId);
//delete from Save tbl
$mySaveRecord->delete($saveId);
delTreeElementById($saveData->treeId);
}
// edit in table
function edit()
{
//to use the variable out side the funcion
global $mySaveRecord;
global $currencyDAO;
//get variable
$saveId = $_GET['saveid'];
//load data by saveid
$loadData = $mySaveRecord->load($saveId);
$currency = $currencyDAO->load($loadData->currencyId);
$loadData->currencyName = $currency->name;
return $loadData;
}
// edit in table
function editprint()
{
//to use the variable out side the funcion
global $mySaveRecord;
global $currencyDAO;
//get variable
$saveId = $_GET['id'];
//load data by saveid
$loadData = $mySaveRecord->load($saveId);
$currency = $currencyDAO->load($loadData->currencyId);
$loadData->currencyName = $currency->name;
return $loadData;
}
// update table
function update()
{
//to use the variable out side the funcion
global $mySave;
global $mySaveRecord;
global $accountsTreeDAO;
global $accountsTree;
//Transaction
$utransactions = new Transaction();
//post variable
$saveName = $_POST['savename'];
$savecurrentValue = $_POST['savecurrentvalue'];
$saveDetails = $_POST['savedetails'];
$saveDate = $_POST['savedate'];
$conditions = $_POST['conditions'];
$saveId = $_POST['saveid'];
$sortby = $_POST['sortby'];
$saveTreeParentType = (int) $_POST['saveTreeParentType'];
$savetypeid = $_POST['savetypeid'];
//insert data in Save tbl
$mySave = $mySaveRecord->load($saveId);
$mySave->savedate = $saveDate;
$mySave->conditions = $conditions;
$mySave->savecurrentvalue = $savecurrentValue;
$mySave->savename = $saveName;
$mySave->userid = $_SESSION['userid'];
$mySave->saveid = $saveId;
$mySave->sortby = $sortby;
$mySave->saveTreeParentType = $saveTreeParentType;
$mySave->savetypeid = $savetypeid;
$treeId = $mySave->treeId;
$oldTree = $accountsTreeDAO->load($treeId);
$oldTree->name = $saveName;
$oldTree->customName = $saveName;
$parent = 40;
if ($saveTreeParentType == 1) {
$parent = 408;
}
$oldTree->parent = $parent;
try {
$mySaveRecord->update($mySave, $saveDetails);
editTreeElement($oldTree);
$utransactions->commit();
$message = 'تمت العملية بنجاح';
$flag = 1;
} catch (Exception $ex) {
$utransactions->rollback();
$message = 'عفوا لقد حدث خطأ';
$flag = 2;
}
return $flag;
}
// insert in Savedaily tbl
function insertSavedaily($savedailysavebefore, $savedailychangeamount, $savedailychangetype, $saveid, $processname, $savedailymodelid, $savedailysaveafter, $tablename)
{
//to use the variable out side the funcion
global $mySavedaily;
global $mySavedailyRecord;
//insert in Savedaily tbl
$mySavedaily->savedailydate = date("Y-m-d H:i:s");
$mySavedaily->userid = $_SESSION['userid'];
$mySavedaily->savedailysavebefore = $savedailysavebefore;
$mySavedaily->savedailychangeamount = $savedailychangeamount;
$mySavedaily->savedailychangetype = $savedailychangetype;
$mySavedaily->saveid = $saveid;
$mySavedaily->processname = $processname;
$mySavedaily->savedailymodelid = $savedailymodelid;
$mySavedaily->savedailysaveafter = $savedailysaveafter;
$mySavedaily->tablename = $tablename;
$mySavedailyRecord->insert($mySavedaily);
}