<?php

session_start();
ob_start();


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

//Assets
require_once('../models/dao/AssetsDAO.class.php');
require_once('../models/dto/Asset.class.php');
require_once('../models/mysql/AssetsMySqlDAO.class.php');
require_once('../models/mysql/ext/AssetsMySqlExtDAO.class.php');
//Productcat
require_once('../models/dao/ProductcatDAO.class.php');
require_once('../models/dto/Productcat.class.php');
require_once('../models/mysql/ProductcatMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductcatMySqlExtDAO.class.php');

//Assetscat
require_once('../models/dao/AssetscatDAO.class.php');
require_once('../models/dto/Assetscat.class.php');
require_once('../models/mysql/AssetscatMySqlDAO.class.php');
require_once('../models/mysql/ext/AssetscatMySqlExtDAO.class.php');

//Bank
require_once('../models/dao/BankDAO.class.php');
require_once('../models/dto/Bank.class.php');
require_once('../models/mysql/BankMySqlDAO.class.php');
require_once('../models/mysql/ext/BankMySqlExtDAO.class.php');

//Bankaccount
require_once('../models/dao/BankaccountDAO.class.php');
require_once('../models/dto/Bankaccount.class.php');
require_once('../models/mysql/BankaccountMySqlDAO.class.php');
require_once('../models/mysql/ext/BankaccountMySqlExtDAO.class.php');

//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.class.php');

//Expensestype
require_once('../models/dao/ExpensestypeDAO.class.php');
require_once('../models/dto/Expensestype.class.php');
require_once('../models/mysql/ExpensestypeMySqlDAO.class.php');
require_once('../models/mysql/ext/ExpensestypeMySqlExtDAO.class.php');

//Partner
require_once('../models/dao/PartnerDAO.class.php');
require_once('../models/dto/Partner.class.php');
require_once('../models/mysql/PartnerMySqlDAO.class.php');
require_once('../models/mysql/ext/PartnerMySqlExtDAO.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');

//Store
require_once('../models/dao/StoreDAO.class.php');
require_once('../models/dto/Store.class.php');
require_once('../models/mysql/StoreMySqlDAO.class.php');
require_once('../models/mysql/ext/StoreMySqlExtDAO.class.php');

//Supplier
require_once('../models/dao/SupplierDAO.class.php');
require_once('../models/dto/Supplier.class.php');
require_once('../models/mysql/SupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierMySqlExtDAO.class.php');

//Unit
require_once('../models/dao/UnitDAO.class.php');
require_once('../models/dto/Unit.class.php');
require_once('../models/mysql/UnitMySqlDAO.class.php');
require_once('../models/mysql/ext/UnitMySqlExtDAO.class.php');

//Usergroup
require_once('../models/dao/UsergroupDAO.class.php');
require_once('../models/dto/Usergroup.class.php');
require_once('../models/mysql/UsergroupMySqlDAO.class.php');
require_once('../models/mysql/ext/UsergroupMySqlExtDAO.class.php');
//Product
require_once('../models/dao/ProductDAO.class.php');
require_once('../models/dto/Product.class.php');
require_once('../models/mysql/ProductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductMySqlExtDAO.class.php');
//Expenses
require_once('../models/dao/ExpensesDAO.class.php');
require_once('../models/dto/Expense.class.php');
require_once('../models/mysql/ExpensesMySqlDAO.class.php');
require_once('../models/mysql/ext/ExpensesMySqlExtDAO.class.php');

//Billproperty
require_once('../models/dao/BillpropertyDAO.class.php');
require_once('../models/dto/Billproperty.class.php');
require_once('../models/mysql/BillpropertyMySqlDAO.class.php');
require_once('../models/mysql/ext/BillpropertyMySqlExtDAO.class.php');
//Billname
require_once('../models/dao/BillnameDAO.class.php');
require_once('../models/dto/Billname.class.php');
require_once('../models/mysql/BillnameMySqlDAO.class.php');
require_once('../models/mysql/ext/BillnameMySqlExtDAO.class.php');


//Shippercompanies
require_once('../models/dao/ShippercompaniesDAO.class.php');
require_once('../models/dto/Shippercompanie.class.php');
require_once('../models/mysql/ShippercompaniesMySqlDAO.class.php');
require_once('../models/mysql/ext/ShippercompaniesMySqlExtDAO.class.php');


//Policy
require_once('../models/dao/PolicyDAO.class.php');
require_once('../models/dto/Policy.class.php');
require_once('../models/mysql/PolicyMySqlDAO.class.php');
require_once('../models/mysql/ext/PolicyMySqlExtDAO.class.php');


//Newdbbackup
require_once('../models/dao/NewdbbackupDAO.class.php');
require_once('../models/dto/Newdbbackup.class.php');
require_once('../models/mysql/NewdbbackupMySqlDAO.class.php');
require_once('../models/mysql/ext/NewdbbackupMySqlExtDAO.class.php');
//Sparepart
require_once('../models/dao/SparepartDAO.class.php');
require_once('../models/dto/Sparepart.class.php');
require_once('../models/mysql/SparepartMySqlDAO.class.php');
require_once('../models/mysql/ext/SparepartMySqlExtDAO.class.php');
//Workshop
require_once('../models/dao/WorkshopDAO.class.php');
require_once('../models/dto/Workshop.class.php');
require_once('../models/mysql/WorkshopMySqlDAO.class.php');
require_once('../models/mysql/ext/WorkshopMySqlExtDAO.class.php');
//Branch
require_once('../models/dao/BranchDAO.class.php');
require_once('../models/dto/Branch.class.php');
require_once('../models/mysql/BranchMySqlDAO.class.php');
require_once('../models/mysql/ext/BranchMySqlExtDAO.class.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');

require_once('../models/dao/ClientareaDAO.class.php');
require_once('../models/dto/Clientarea.class.php');
require_once('../models/mysql/ClientareaMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientareaMySqlExtDAO.class.php');
//Restauranthall
require_once('../models/dao/RestauranthallDAO.class.php');
require_once('../models/dto/Restauranthall.class.php');
require_once('../models/mysql/RestauranthallMySqlDAO.class.php');
require_once('../models/mysql/ext/RestauranthallMySqlExtDAO.class.php');

require_once('../models/dao/RestauranttableDAO.class.php');
require_once('../models/dto/Restauranttable.class.php');
require_once('../models/mysql/RestauranttableMySqlDAO.class.php');
require_once('../models/mysql/ext/RestauranttableMySqlExtDAO.class.php');
//
require_once('../models/dao/SizecolorDAO.class.php');
require_once('../models/dto/Sizecolor.class.php');
require_once('../models/mysql/SizecolorMySqlDAO.class.php');
require_once('../models/mysql/ext/SizecolorMySqlExtDAO.class.php');
//typeClient
require_once('../models/dao/TypeClientDAO.class.php');
require_once('../models/dto/TypeClient.class.php');
require_once('../models/mysql/TypeClientMySqlDAO.class.php');
require_once('../models/mysql/ext/TypeClientMySqlExtDAO.class.php');
//Employeeattendancesystem
require_once('../models/dao/EmployeeattendancesystemDAO.class.php');
require_once('../models/dto/Employeeattendancesystem.class.php');
require_once('../models/mysql/EmployeeattendancesystemMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeeattendancesystemMySqlExtDAO.class.php');
//Employeegroup
require_once('../models/dao/EmployeegroupDAO.class.php');
require_once('../models/dto/Employeegroup.class.php');
require_once('../models/mysql/EmployeegroupMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeegroupMySqlExtDAO.class.php');
//Employeesubgroup
require_once('../models/dao/EmployeesubgroupDAO.class.php');
require_once('../models/dto/Employeesubgroup.class.php');
require_once('../models/mysql/EmployeesubgroupMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeesubgroupMySqlExtDAO.class.php');
//Restaurantplaystation
require_once('../models/dao/RestaurantplaystationDAO.class.php');
require_once('../models/dto/Restaurantplaystation.class.php');
require_once('../models/mysql/RestaurantplaystationMySqlDAO.class.php');
require_once('../models/mysql/ext/RestaurantplaystationMySqlExtDAO.class.php');
//
require_once('../models/dao/PlaystationcardDAO.class.php');
require_once('../models/dto/Playstationcard.class.php');
require_once('../models/mysql/PlaystationcardMySqlDAO.class.php');
require_once('../models/mysql/ext/PlaystationcardMySqlExtDAO.class.php');

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


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

  Controller Name :- assetsCTRL

  OPERTATION in Controller

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

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


//here goes the instances and general variables
//
$myAssets = new Asset();
$myAssetsRecord = new AssetsMySqlDAO();
$myAssetsEx = new AssetsMySqlExtDAO();

//Productcat
$productCat = new Productcat();
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();

//Assetscat
$myAssetscatRecord = new AssetscatMySqlDAO();


//Costcenter
$CostcenterDAO = new CostcenterMySqlDAO();



//Bank
$myBankRecord = new BankMySqlDAO();

//Bankaccount
$myBankaccountRecord = new BankaccountMySqlDAO();

//Client
$myClientRecord = new ClientMySqlDAO();

//Expensestype
$myExpensestypeRecord = new ExpensestypeMySqlDAO();

//Partner
$myPartnerRecord = new PartnerMySqlDAO();

//Save
$mySaveRecord = new SaveMySqlDAO();

//Store
$myStoreRecord = new StoreMySqlDAO();

//Supplier
$mySupplierRecord = new SupplierMySqlDAO();

//Unit
$myUnitRecord = new UnitMySqlDAO();

//Usergroup
$myUsergroupRecord = new UsergroupMySqlDAO();
//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();
//Expenses
$ExpenseDAO = new ExpensesMySqlDAO();
$Expense = new Expense();
$ExpenseExt = new ExpensesMySqlExtDAO();

//Billproperty
$myBillpropertyRecord = new BillpropertyMySqlDAO();
$myBillpropertyEx = new BillpropertyMySqlExtDAO();

//Billname
$Billname = new Billname();
$BillnameDAO = new BillnameMySqlDAO();
$BillnameEX = new BillnameMySqlExtDAO();


//Shippercompanies
$myShippercompaniesRecord = new ShippercompaniesMySqlDAO();

//Policy
$myPolicyRecord = new PolicyMySqlDAO();


//Newdbbackup
$myNewdbbackupRecord = new NewdbbackupMySqlDAO();

//Sparepart
$sparePart = new Sparepart();
$sparePartDAO = new SparepartMySqlDAO();
$sparePartExt = new SparepartMySqlExtDAO();
//Workshop
$workshop = new Workshop();
$workshopDAO = new WorkshopMySqlDAO();
$workshopEX = new WorkshopMySqlExtDAO();
//Branch
$branchDAO = new BranchMySqlDAO();
//User
$userDAO = new UserMySqlDAO();

//Clientarea
$clientAreaDAO = new ClientareaMySqlDAO();
//
$restaurantHallDAO = new RestauranthallMySqlDAO();
//
$restaurantTableDAO = new RestauranttableMySqlDAO();
//
$sizeColorDAO = new SizecolorMySqlDAO();
//
$TypeClientDAO = new TypeClientMySqlDAO();
//
$employeeAttendanceSystemDAO = new EmployeeattendancesystemMySqlDAO();
//
$employeeGroupDAO = new EmployeegroupMySqlDAO();
//
$employeeSubGroupDAO = new EmployeesubgroupMySqlDAO();
//
$restaurantPlaystationDAO = new RestaurantplaystationMySqlDAO();
//
$playstationCardDAO = new PlaystationcardMySqlDAO();

//Checkcontroller
//check and use the condetion that suite this action
if (empty($do)) {

    $flag = '0';
    $name = $_GET['name'];
    $tablename = $_GET['tablename'];
    $catid = $_GET['catid'];
    if (!isset($catid) && $catid == '-1') {
        $catid = 0;
    }
    checkName($name, $tablename, $catid);
} elseif ($do == "checkNameWithId") {
    $flag = '0';
    $name = $_GET['name'];
    $id = $_GET['id'];
    $tablename = $_GET['tablename'];
    $catid = 0;

    if (isset($name) && $name != "" && isset($tablename) && $tablename != "" && isset($id) && $id != "") {

        switch ($tablename) {
            case "productcat":
                $data = $productCatDAO->load($id);
                $oldName = $data->productCatName;
                break;
            case "costcenter":
                $data = $CostcenterDAO->load($id);
                $oldName = $data->name;
                break;
            case "assets":
                $data = $myAssetsRecord->load($id);
                $oldName = $data->assetsName;
                break;
            case "assetscat":
                $data = $myAssetscatRecord->load($id);
                $oldName = $data->cattitel;
                break;
            case "bank":
                $data = $myBankRecord->load($id);
                $oldName = $data->bankname;
                break;
            case "bankaccount":
                $data = $myBankaccountRecord->load($id);
                $oldName = $data->accountname;
                break;
            case "client":
                $data = $myClientRecord->load($id);
                $oldName = $data->clientname;
                break;
            case "clientcode":
                $data = $myClientRecord->load($id);
                $oldName = $data->clientcode;
                break;
            case "expensestype":
                $data = $myExpensestypeRecord->load($id);
                $oldName = $data->expensestypename;
                break;
            case "partner":
                $data = $myPartnerRecord->load($id);
                $oldName = $data->partnername;
                break;
            case "save":
                $data = $mySaveRecord->load($id);
                $oldName = $data->savename;
                break;
            case "store":
                $data = $myStoreRecord->load($id);
                $oldName = $data->storeName;
                break;
            case "supplier":
                $data = $mySupplierRecord->load($id);
                $oldName = $data->suppliername;
                break;
            case "unit":
                $data = $myUnitRecord->load($id);
                $oldName = $data->unitName;
                break;
            case "usergroup":
                $data = $myUsergroupRecord->load($id);
                $oldName = $data->usergroupname;
                break;
            case "product":
                $data = $productExt->loadEX($id);
                $oldName = $data->productName;
                $catid = $data->productCatId;
                break;
            case "expenses":
                $data = $ExpenseDAO->load($id);
                $oldName = $data->expensesname;
                break;
            case "billproperty":
                $data = $myBillpropertyRecord->load($id);
                $oldName = $data->billpropertyname;
                break;
            case "billname":
                $data = $BillnameDAO->load($id);
                $oldName = $data->name;
                break;
            case "shippercompanies":
                $data = $myShippercompaniesRecord->load($id);
                $oldName = $data->companyName;
                break;
            case "policy":
                $data = $myPolicyRecord->load($id);
                $oldName = $data->policyName;
                break;
            case "newdbbackup":
                $data = $myNewdbbackupRecord->load($id);
                $oldName = $data->backupname;
                break;
            case "sparepart":
                $data = $sparePartDAO->load($id);
                $oldName = $data->sparepartname;
                break;
            case "workshop":
                $data = $workshopDAO->load($id);
                $oldName = $data->name;
                break;
            case "branch":
                $data = $branchDAO->load($id);
                $oldName = $data->branchName;
                break;
            case "user":
                $data = $userDAO->load($id);
                $oldName = $data->username;
                break;
            case "clientarea":
                $data = $clientAreaDAO->load($id);
                $oldName = $data->name;
                break;
            case "restauranthall":
                $data = $restaurantHallDAO->load($id);
                $oldName = $data->name;
                break;
            case "restauranttable":
                $data = $restaurantTableDAO->load($id);
                $oldName = $data->name;
                break;
            case "sizecolor":
                $data = $sizeColorDAO->load($id);
                $oldName = $data->name;
                break;
            case "typeclient":
                $data = $TypeClientDAO->load($id);
                $oldName = $data->typeName;
                break;
            case "employeeAttendanceSystems":
                $data = $employeeAttendanceSystemDAO->load($id);
                $oldName = $data->name;
                break;
            case "employeegroups":
                $data = $employeeGroupDAO->load($id);
                $oldName = $data->name;
                break;
            case "employeesubgroup":
                $data = $employeeSubGroupDAO->load($id);
                $oldName = $data->name;
                break;
            case "restaurantplaystation":
                $data = $restaurantPlaystationDAO->load($id);
                $oldName = $data->name;
                break;
            case "playStationCard":
                $data = $playstationCardDAO->load($id);
                $oldName = $data->name;
                break;

            default:
                $data = array();
                break;
        }

        if (isset($data) > 0) {
            if ($name != $oldName) {

                if ($tablename == "product") {
                    //$catid = $_GET['catid'];
                    //echo "id catid: ".$catid;
                    if (isset($catid) && $catid != 0) {
                        //header("location:?name="+$name+"&tablename=product&catid="+$catid);
                        checkName($name, $tablename, $catid);
                    }
                } else {
                    checkName($name, $tablename, 0);
                }
            } else {
                echo $flag;
            }
        }
    }
}
/* ===============================
  function in this CONTROLLER
  ================================ */

function checkName($name, $tablename, $catid) {

    global $myAssetsRecord;
    global $myAssetscatRecord;
    global $productCatDAO;
    global $myBankRecord;
    global $myBankaccountRecord;
    global $myClientRecord;
    global $myExpensestypeRecord;
    global $myPartnerRecord;
    global $mySaveRecord;
    global $myStoreRecord;
    global $mySupplierRecord;
    global $myUnitRecord;
    global $myUsergroupRecord;
    global $productDAO;
    global $productExt;
    global $ExpenseDAO;
    global $myBillpropertyRecord;
    global $BillnameDAO;
    global $myShippercompaniesRecord;
    global $myPolicyRecord;
    global $myNewdbbackupRecord;
    global $workshopDAO;
    global $branchDAO;
    global $userDAO;
    global $CostcenterDAO;
    global $clientAreaDAO;
    global $restaurantHallDAO;
    global $restaurantTableDAO;
    global $sizeColorDAO;
    global $TypeClientDAO;
    global $employeeAttendanceSystemDAO;
    global $employeeSubGroupDAO;
    global $employeeGroupDAO;
    global $restaurantPlaystationDAO;
    global $playstationCardDAO;


    //print("name: " . $name . "  table: " . $tablename);

    $flag = '0';
    if (isset($name) && $name != "" && isset($tablename) && $tablename != "") {
        //print("name: ".$name."  table: ".$tablename);
        if (($tablename == "productcat" && (count($productCatDAO->queryByProductCatName($name)) > 0)) || ($tablename == "assets" && (count($myAssetsRecord->queryByAssetsName($name)) > 0)) || ($tablename == "assetscat" && (count($myAssetscatRecord->queryByCattitel($name)) > 0)) || ($tablename == "bank" && (count($myBankRecord->queryByBankname($name)) > 0)) || ($tablename == "bankaccount" && (count($myBankaccountRecord->queryByAccountname($name)) > 0)) || ($tablename == "client" && (count($myClientRecord->queryByClientname($name)) > 0)) || ($tablename == "clientcode" && (count($myClientRecord->queryByClientcode($name)) > 0)) || ($tablename == "expensestype" && (count($myExpensestypeRecord->queryByExpensestypename($name)) > 0)) || ($tablename == "partner" && (count($myPartnerRecord->queryByPartnername($name)) > 0)) || ($tablename == "save" && (count($mySaveRecord->queryBySavename($name)) > 0)) || ($tablename == "store" && (count($myStoreRecord->queryByStoreName($name)) > 0)) || ($tablename == "supplier" && (count($mySupplierRecord->queryBySuppliername($name)) > 0)) || ($tablename == "unit" && (count($myUnitRecord->queryByUnitName($name)) > 0)) || ($tablename == "usergroup" && (count($myUsergroupRecord->queryByUsergroupname($name)) > 0)) || ($tablename == "expenses" && (count($ExpenseDAO->queryByExpensesname($name)) > 0)) || ($tablename == "billproperty" && (count($myBillpropertyRecord->queryByBillpropertyname($name)) > 0)) || ($tablename == "billname" && (count($BillnameDAO->queryByName($name)) > 0)) || ($tablename == "shippercompanies" && (count($myShippercompaniesRecord->queryByCompanyName($name)) > 0)) || ($tablename == "policy" && (count($myPolicyRecord->queryByPolicyName($name)) > 0)) || ($tablename == "newdbbackup" && (count($myNewdbbackupRecord->queryByBackupname($name)) > 0) || ($tablename == "workshop" && (count($workshopDAO->queryByName($name)) > 0)) || ($tablename == "branch" && (count($branchDAO->queryByBranchName($name)) > 0)) || ($tablename == "user" && (count($userDAO->queryByUsername($name)) > 0)) || ($tablename == "costcenter" && (count($CostcenterDAO->queryByName($name)) > 0)) || ($tablename == "clientarea" && (count($clientAreaDAO->queryByName($name)) > 0)) || ($tablename == "restauranthall" && (count($restaurantHallDAO->queryByName($name)) > 0)) || ($tablename == "restauranttable" && (count($restaurantTableDAO->queryByName($name)) > 0)) || ($tablename == "sizecolor" && (count($sizeColorDAO->queryByName($name)) > 0)) || ($tablename == "typeclient" && (count($TypeClientDAO->queryByTypeName($name)) > 0)) || ($tablename == "employeeAttendanceSystems" && (count($employeeAttendanceSystemDAO->queryByName($name)) > 0))) || ($tablename == "employeegroups" && (count($employeeGroupDAO->queryByName($name)) > 0)) || ($tablename == "employeesubgroup" && (count($employeeSubGroupDAO->queryByName($name)) > 0)) || ($tablename == "restaurantplaystation" && (count($restaurantPlaystationDAO->queryByName($name)) > 0)) || ($tablename == "playStationCard" && (count($playstationCardDAO->queryByName($name)) > 0))) {
            $flag = '1';
        } elseif ($tablename == "product") {
            $catid = $_GET['catid'];
            //echo "catid: "+$catid;
            if (isset($catid) && $catid != 0) {

                if (count($productExt->checkForProductNameInOneCategory2($name, $catid)) > 0) {
                    $flag = '1';
                } else {
                    $flag = '0';
                }
            }
        } else {
            $flag = '0';
        }
    }

    echo $flag;
}

?>