<?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
// GOES HERE ....................
include("../public/include_dao.php");





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



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

  Controller Name :- transferdatabaseCTRL اضافة قاعدة بيانات ونقل الجداول

  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
$smarty->display("header.html");


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




    //here the smarty templates
    $smarty->display("transferdatabaseview/add.html");
} elseif ($do == "add") {

    //here the permssion check
    include_once("../public/authentication.php");

    //to use the variable out side the funcion
    createDatabase();

    //table data count
    $smarty->display("transferdatabaseview/showtable.html");

    $smarty->assign("customTransferdatabase", 1);
} elseif ($do == "inserttabledata") {

    //here the permssion check
    include_once("../public/authentication.php");

    //to use the variable out side the funcion
    inserttabledata();

    $smarty->display("transferdatabaseview/exexcute.html");
} elseif ($do == "exexcute") {

    //here the permssion check
    include_once("../public/authentication.php");

    //to use the variable out side the funcion

    try {
        exexcute();

        $smarty->display("transferdatabaseview/done.html");
    } catch (Exception $e) {

        $smarty->display("transferdatabaseview/done.html");
    }
} elseif ($do == "changedb") {
    changedb();

    //send the user to the main page
    header("location:index.php");
}






$smarty->assign('customValidation', 1);


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

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

function changedb() {
    $databaseName = $_POST['databaseName'];

    $_SESSION['dbname'] = $databaseName;
}

function getDatabaseName() {

    // Read in entire file
    $databsesNames = file_get_contents("../views/default/archiveview/dbNames.txt");

    //print($databsesNames);
    $databases = array_reverse(explode(",", $databsesNames));
    $smarty->assign("databases", $databases);
}

function createDatabase() {

    global $smarty;

    $newdatabaseName = $_POST['newdatabaseName'];
    $newUserName = $_POST['newUserName'];
    $newPassword = $_POST['newPassword'];

    $olddatabaseName = $_POST['olddatabaseName'];
    $oldUserName = $_POST['oldUserName'];
    $oldPassword = $_POST['oldPassword'];


    //Current Database
    $currentDatabaselink = mysql_connect('localhost', 'root', '123456') or die(mysql_error()); // connect to database


    $currentDatabaseSql = mysql_query('SHOW TABLES FROM  ' . $_SESSION['dbname']) or die(mysql_error());



    //new Database
    $newDatabaseLink = mysql_connect('localhost', $newUserName, $newPassword) or die(mysql_error()); // connect to database

    if (!mysql_select_db($newdatabaseName)) {
        $createDatabaseSql = "CREATE DATABASE  " . $newdatabaseName . "";
        $result1 = mysql_query($createDatabaseSql) or die(mysql_error());
        file_put_contents("../views/default/archiveview/dbNames.txt", "," . $newdatabaseName, FILE_APPEND | LOCK_EX);
        //create table in newdatabase
        while ($row = mysql_fetch_row($currentDatabaseSql)) {
            mysql_query('DROP TABLE IF EXISTS ' . $newdatabaseName . '.' . $row[0] . '') or die(mysql_error());

            mysql_query('CREATE TABLE ' . $newdatabaseName . '.' . $row[0] . ' LIKE ' . $_SESSION['dbname'] . '.' . $row[0] . '') or die(mysql_error());
        }

        //create Main tables
        properties("initialValues/pro.sql", $newdatabaseName, $newUserName, $newPassword);
        usergroup($newdatabaseName, $newUserName, $newPassword);
        relusergroupproperties($newdatabaseName, $newUserName, $newPassword);
        billproperty("initialValues/billproperty.sql", $newdatabaseName, $newUserName, $newPassword);
        billname($newdatabaseName, $newUserName, $newPassword);
        unit($newdatabaseName, $newUserName, $newPassword);

        menuurl("initialValues/menu.sql", $newdatabaseName, $newUserName, $newPassword);
    } else {
        mysql_select_db($newdatabaseName, $newDatabaseLink);
    }




    //old Database

    $oldDatabaseLink = mysql_connect('localhost', $oldUserName, $oldPassword) or die(mysql_error()); // connect to database
//
    $oldDatabaseSql = mysql_query('SHOW TABLES FROM  ' . $olddatabaseName) or die(mysql_error());


    $k = 1;
    while ($row = mysql_fetch_row($oldDatabaseSql)) {



        if ($row[0] == "back_buy_bill_tbl") {


            $smarty->assign("tableArabicName" . $k, "مردودات المشتريات");

            $smarty->assign("tableName" . $k, $row[0]);


            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "returnbuybill");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "back_buy_bill_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);


            $k++;
        } elseif ($row[0] == "back_sell_bill_tbl") {
            $smarty->assign("tableArabicName" . $k, "مردودات المبيعات");

            $smarty->assign("tableName" . $k, $row[0]);


            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "returnsellbill");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "back_sell_bill_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);


            $k++;
        } elseif ($row[0] == "bank_account_tbl") {
            $smarty->assign("tableArabicName" . $k, "حسابات البنوك");

            $smarty->assign("tableName" . $k, $row[0]);


            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "bankaccount");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "bank_account_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);


            $k++;
        }


        //elseif($row[0] == "bank_deposite_tbl")
//			{
//				$smarty->assign("tableArabicName".$k, "ايداع الشيكات");
//
//				$result = mysql_query("SELECT * FROM ". $newdatabaseName.".checkdeposit");
//		  		$datacount= mysql_num_rows($result);
//				$smarty->assign("tableDataCount".$k, $datacount);
//
//
//				$smarty->assign("tableName".$k, $row[0]);
//
//				$k++;
//			}
//
//
//
//			elseif($row[0] == "bank_sa7b_tbl")
//			{
//				$smarty->assign("tableArabicName".$k, "سحب الشيكات");
//
//				$result = mysql_query("SELECT * FROM ". $newdatabaseName.".checkwithdrawal");
//		  		$datacount= mysql_num_rows($result);
//				$smarty->assign("tableDataCount".$k, $datacount);
//
//				$smarty->assign("tableName".$k, $row[0]);
//
//				$k++;
//
//			}
        elseif ($row[0] == "buy_bill_tbl") {
            $smarty->assign("tableArabicName" . $k, "المشتريات");

            $smarty->assign("tableName" . $k, $row[0]);


            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "buybill");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "buy_bill_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "cat_tbl") {
            $smarty->assign("tableArabicName" . $k, "تصنيفات المنتجات");

            $smarty->assign("tableName" . $k, $row[0]);


            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "productcat");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "cat_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        }


        //elseif($row[0] == "**changinstores_tbl")
//			{
//
//			}
//
//
//			elseif($row[0] == "**client1_report_tbl")
//			{
//
//			}
        elseif ($row[0] == "clients_tbl") {
            $smarty->assign("tableArabicName" . $k, "العملاء");

            $smarty->assign("tableName" . $k, $row[0]);


            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "client");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "clients_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        }



//			elseif($row[0] == "client_report_tbl")
//			{
//			}
//			elseif($row[0] == "**discount_product_tmp_tbl")
//			{
//			}
        //elseif($row[0] == "**expenses_report_tbl")
//			{
//
//			}
        elseif ($row[0] == "expenses_tbl") {
            $smarty->assign("tableArabicName" . $k, "المصروفات");

            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "expenses");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "expenses_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "expenses_type") {
            $smarty->assign("tableArabicName" . $k, "انواع المصروفات");

            $smarty->assign("tableName" . $k, $row[0]);


            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "expensestype");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "expenses_type");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "kem_tbl") {
            $smarty->assign("tableArabicName" . $k, "الكمبيالات");

            $smarty->assign("tableName" . $k, $row[0]);


            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "kempiala");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "kem_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "owners") {
            $smarty->assign("tableArabicName" . $k, "الشركاء");

            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "partner");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "owners");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "product_setting_tbl") {
            $smarty->assign("tableArabicName" . $k, "اعدادات المنتج داخل المخزن");
            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "productsetting");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "product_setting_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "product_tbl") {
            $smarty->assign("tableArabicName" . $k, "المنتجات");

            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "product");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "product_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "prog_setting_tbl") {
            $smarty->assign("tableArabicName" . $k, "اعدادت البرنامج");

            $smarty->assign("tableName" . $k, $row[0]);


            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "programsettings");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "prog_setting_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        }



        //	elseif($row[0] == "**save")
//			{
//
//			}
        elseif ($row[0] == "saves_tbl") {
            $smarty->assign("tableArabicName" . $k, "الخزن");

            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "save");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "saves_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "save_report_tbl") {
            $smarty->assign("tableArabicName" . $k, "يومية الخزن");

            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "savedaily");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "save_report_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "sell_bill_tbl") {
            $smarty->assign("tableArabicName" . $k, "المبيعات");
            $smarty->assign("tableName" . $k, $row[0]);


            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "sellbill");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "sell_bill_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "store_details") {
            $smarty->assign("tableArabicName" . $k, "حركة المخازن");

            $smarty->assign("tableName" . $k, $row[0]);


            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "storereport");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "store_details");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "store_details_tbl") {
            $smarty->assign("tableArabicName" . $k, "تفاصيل البضاعة بالمخازن");
            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "storedetail");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "store_details_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "store_tbl") {
            $smarty->assign("tableArabicName" . $k, "المخازن");

            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "store");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "store_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "store_transfer_tbl") {
            $smarty->assign("tableArabicName" . $k, "التحويل بين المخازن");

            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "storemovement");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "store_transfer_details_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        }



//			elseif($row[0] == "**store_transfer_tmp_tbl")
//			{
//			}
//			elseif($row[0] == "supplier_report_tbl")
//			{
//			}
        elseif ($row[0] == "supplier_tbl") {
            $smarty->assign("tableArabicName" . $k, "الموردين");

            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "supplier");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "supplier_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        }


        //	elseif($row[0] == "type_tbl")
//			{
//				$smarty->assign("tableArabicName".$k, "وحدات القياس");
//
//				$result = mysql_query("SELECT * FROM ". $newdatabaseName.".unit");
//		  		$datacount= mysql_num_rows($result);
//				$smarty->assign("tableDataCount".$k, $datacount);
//
//				$smarty->assign("tableName".$k, $row[0]);
//
//				$k++;
//			}
        elseif ($row[0] == "users_tbl") {
            $smarty->assign("tableArabicName" . $k, "المستخدمين");

            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "user");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "users_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "withdrawals") {
            $smarty->assign("tableArabicName" . $k, "مسحوبات الشركاء");

            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "partnerwithdrawal");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "withdrawals");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        } elseif ($row[0] == "notics_report_tbl") {
            $smarty->assign("tableArabicName" . $k, "الاشعارات");

            $smarty->assign("tableName" . $k, $row[0]);

            //new table data
            $newDatacount = getTableDataRow($newdatabaseName, $newDatabaseLink, "notics");
            $smarty->assign("tableDataCount" . $k, $newDatacount);


            //old table data
            $oldDatacount = getTableDataRow($olddatabaseName, $oldDatabaseLink, "notics_report_tbl");
            $smarty->assign("oldDatacount" . $k, $oldDatacount);

            $k++;
        }
    }



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

    $smarty->assign("newdatabaseName", $newdatabaseName);
    $smarty->assign("newUserName", $newUserName);
    $smarty->assign("newPassword", $newPassword);

    $smarty->assign("olddatabaseName", $olddatabaseName);
    $smarty->assign("oldUserName", $oldUserName);
    $smarty->assign("oldPassword", $oldPassword);
}

function inserttabledata() {

    global $smarty;

    $tableName = $_POST['tablename'];

    $newdatabaseName = $_POST['newdatabaseName'];
    $newUserName = $_POST['newUserName'];
    $newPassword = $_POST['newPassword'];

    $olddatabaseName = $_POST['olddatabaseName'];
    $oldUserName = $_POST['oldUserName'];
    $oldPassword = $_POST['oldPassword'];



    //if($tableName == "**agz_tbl")
//			{
//
//			}
//			elseif($tableName == "**backups_tbl")
//			{
//			}
//			elseif($tableName == "**backup_info_tbl")
//			{
//			}

    if ($tableName == "back_buy_bill_tbl") {

        $smarty->assign("tableArabicName", "مردودات المشتريات");
    } elseif ($tableName == "back_sell_bill_tbl") {
        $smarty->assign("tableArabicName", "مردودات المبيعات");
    } elseif ($tableName == "bank_account_tbl") {
        $smarty->assign("tableArabicName", "حسابات البنوك");
    }


    //elseif($tableName == "bank_deposite_tbl")
//			{
//
//				$smarty->assign("tableArabicName", "ايداع شيكات");
//
//			}
//
//
//			elseif($tableName == "bank_sa7b_tbl")
//			{
//				$smarty->assign("tableArabicName", "سحب شيكات");
//			}
    elseif ($tableName == "buy_bill_tbl") {
        $smarty->assign("tableArabicName", "المشتريات");
    } elseif ($tableName == "cat_tbl") {
        $smarty->assign("tableArabicName", "تصنيفات المنتجات");
    }


    //elseif($tableName == "**changinstores_tbl")
//			{
//
//			}
//
//
//			elseif($tableName == "**client1_report_tbl")
//			{
//
//			}
//
    elseif ($tableName == "clients_tbl") {
        $smarty->assign("tableArabicName", "العملاء");
    }



    //elseif($tableName == "client_report_tbl")
//			{
//				$smarty->assign("tableArabicName", "مديونية العملاء");
//			}
    //elseif($tableName == "**discount_product_tmp_tbl")
//			{
//
//			}
    //	elseif($tableName == "**expenses_report_tbl")
//			{
//
//
//			}
    elseif ($tableName == "expenses_tbl") {
        $smarty->assign("tableArabicName", "المصروفات");
    } elseif ($tableName == "expenses_type") {
        $smarty->assign("tableArabicName", "انواع المصروفات");
    }

    ////////
    elseif ($tableName == "kem_tbl") {

        $smarty->assign("tableArabicName", "الكمبيالات");
    } elseif ($tableName == "owners") {
        $smarty->assign("tableArabicName", "الشركاء");
    }


    //elseif($tableName == "**producttemp_tbl")
//			{
//
//			}
//
    elseif ($tableName == "product_setting_tbl") {

        $smarty->assign("tableArabicName", "اعدادات المنتج داخل المخزن");
    }


    //elseif($tableName == "**product_supplier_tbl")
//			{
//
//			}
//
    elseif ($tableName == "product_tbl") {

        $smarty->assign("tableArabicName", "المنتجات");
    } elseif ($tableName == "prog_setting_tbl") {

        $smarty->assign("tableArabicName", "اعدادات البرنامج");
    }


    //elseif($tableName == "**save")
//			{
//
//			}
    elseif ($tableName == "saves_tbl") {
        $smarty->assign("tableArabicName", "الخزن");
    } elseif ($tableName == "save_report_tbl") {
        $smarty->assign("tableArabicName", "يومية الخزن");
    } elseif ($tableName == "sell_bill_tbl") {
        $smarty->assign("tableArabicName", "المبيعات");
    } elseif ($tableName == "store_details") {
        $smarty->assign("tableArabicName", "حركة المخازن");
    } elseif ($tableName == "store_details_tbl") {
        $smarty->assign("tableArabicName", "تفاصيل البضاعة بالمخازن");
    } elseif ($tableName == "store_tbl") {
        $smarty->assign("tableArabicName", "المخازن");
    } elseif ($tableName == "store_transfer_tbl") {
        $smarty->assign("tableArabicName", "النقل بين المخازن");
    }



    //elseif($tableName == "**store_transfer_tmp_tbl")
//			{
//
//
//			}
    //elseif($tableName == "supplier_report_tbl")
//			{
//				$smarty->assign("tableArabicName", "مديونية الموردين");
//			}
    elseif ($tableName == "supplier_tbl") {
        $smarty->assign("tableArabicName", "الموردين");
    }


    //	elseif($tableName == "type_tbl")
//			{
//				$smarty->assign("tableArabicName", "وحدات القياس");
//			}
    elseif ($tableName == "users_tbl") {
        $smarty->assign("tableArabicName", "المستخدمين");
    } elseif ($tableName == "withdrawals") {
        $smarty->assign("tableArabicName", "مسحوبات الشركاء");
    }


    $smarty->assign("tablename", $tableName);


    $smarty->assign("newdatabaseName", $newdatabaseName);
    $smarty->assign("newUserName", $newUserName);
    $smarty->assign("newPassword", $newPassword);

    $smarty->assign("olddatabaseName", $olddatabaseName);
    $smarty->assign("oldUserName", $oldUserName);
    $smarty->assign("oldPassword", $oldPassword);
}

function exexcute() {
    global $smarty;

    $newdatabaseName = $_POST['newdatabaseName'];
    $newUserName = $_POST['newUserName'];
    $newPassword = $_POST['newPassword'];

    $olddatabaseName = $_POST['olddatabaseName'];
    $oldUserName = $_POST['oldUserName'];
    $oldPassword = $_POST['oldPassword'];

    $smarty->assign("newdatabaseName", $newdatabaseName);
    $smarty->assign("newUserName", $newUserName);
    $smarty->assign("newPassword", $newPassword);

    $smarty->assign("olddatabaseName", $olddatabaseName);
    $smarty->assign("oldUserName", $oldUserName);
    $smarty->assign("oldPassword", $oldPassword);

    try {
        //new Database
        $newDatabaseLink = mysql_connect('localhost', $newUserName, $newPassword, $newdatabaseName) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }

    try {
        //old Database
        $oldDatabaseLink = mysql_connect('localhost', $oldUserName, $oldPassword, $olddatabaseName) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }


    $tableName = $_POST['tablename'];



    if ($tableName == "back_buy_bill_tbl") {

        returnBuyBill($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    } elseif ($tableName == "back_sell_bill_tbl") {
        returnSellBill($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    } elseif ($tableName == "bank_account_tbl") {
        bankaccount($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    }


    //elseif($tableName == "bank_deposite_tbl")
//			{
//
//				checkdeposit($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
//
//			}
//
//
//			elseif($tableName == "bank_sa7b_tbl")
//			{
//				checkwithdrawal($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
//			}
    elseif ($tableName == "buy_bill_tbl") {
        buyBill($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    } elseif ($tableName == "cat_tbl") {
        productCategory($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    }


//			elseif($tableName == "**changinstores_tbl")
//			{
//
//			}
//			elseif($tableName == "**client1_report_tbl")
//			{
//
//			}
    elseif ($tableName == "clients_tbl") {
        client($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    }

//			elseif($tableName == "client_report_tbl")
//			{
//			}


    /* elseif($tableName == "**discount_product_tmp_tbl")
      {

      } */


    //	elseif($tableName == "**expenses_report_tbl")
//			{
//
//
//			}
    elseif ($tableName == "expenses_tbl") {
        expenses($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    } elseif ($tableName == "expenses_type") {
        expensestype($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    }

    ////////
    elseif ($tableName == "kem_tbl") {

        kempiala($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    } elseif ($tableName == "owners") {
        partner($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    }


    /* 	elseif($tableName == "**producttemp_tbl")
      {

      }

     */ elseif ($tableName == "product_setting_tbl") {


        productsetting($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    }



    /* 	elseif($tableName == "**product_supplier_tbl")
      {

      }
     */ elseif ($tableName == "product_tbl") {

        product($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    } elseif ($tableName == "prog_setting_tbl") {

        programsettings($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    }



    //elseif($tableName == "**save")
//			{
//
//			}
    elseif ($tableName == "saves_tbl") {
        save($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    } elseif ($tableName == "save_report_tbl") {
        savedaily($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    } elseif ($tableName == "sell_bill_tbl") {

        sellbill($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    } elseif ($tableName == "store_details") {
        storereport($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    } elseif ($tableName == "store_details_tbl") {

        storedetail($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    } elseif ($tableName == "store_tbl") {
        store($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    } elseif ($tableName == "store_transfer_tbl") {

        storemovement($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    }



//			elseif($tableName == "**store_transfer_tmp_tbl")
//			{
//			}
    //elseif($tableName == "supplier_report_tbl")
//			{
//			}
    elseif ($tableName == "supplier_tbl") {
        supplier($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    }


    //elseif($tableName == "type_tbl")
//			{
//				productUnit($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
//			}
    elseif ($tableName == "users_tbl") {
        user($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    } elseif ($tableName == "withdrawals") {
        withdrawals($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
    }
}

function returnBuyBill($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {

    global $smarty;

    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');



    try {

        //select data from back_buy_bill_tbl

        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".back_buy_bill_tbl", $oldDatabaseLink) or die(mysql_error());



        while ($row = mysql_fetch_array($result)) {


            //returnbuybillsupplierid
            if (is_null($row['back_buy_billSupplierId'])) {
                $row['back_buy_billSupplierId'] = 0;
            }

            //returnbuybilldirectpayment
            $returnbuybilldirectpayment;

            if ($row['back_buy_billSupplierId'] == 0) {
                $returnbuybilldirectpayment = 0;
            } else {
                $returnbuybilldirectpayment = 1;
            }


            //returnbuybilltotaldeptbefor
            if (is_null($row['back_buy_billTotalDeptBefor'])) {
                $row['back_buy_billTotalDeptBefor'] = 0;
            }


            //returnbuybilltotaldeptafter
            if (is_null($row['back_buy_billTotalDeptAfter'])) {
                $row['back_buy_billTotalDeptAfter'] = 0;
            }

            //returnbuybilldiscount
            if (is_null($row['back_buy_billDiscount'])) {
                $row['back_buy_billDiscount'] = 0;
            }

            //returnbuybilltotalbill
            if (is_null($row['back_buy_billTotalBill'])) {
                $row['back_buy_billTotalBill'] = 0;
            }

            //returnbuybillaftertotalbill
            if (is_null($row['back_buy_billAfterTotalBill'])) {
                $row['back_buy_billAfterTotalBill'] = 0;
            }

            //returnbuybilltotalpayed
            if (is_null($row['back_buy_billTotalPayed'])) {
                $row['back_buy_billTotalPayed'] = 0;
            }

            //returnbuybillfinalbill
            if (is_null($row['back_buy_billFinalBill'])) {
                $row['back_buy_billFinalBill'] = 0;
            }


            //returnbuybillstoreid
            if (is_null($row['back_buy_billStoreId'])) {
                $row['back_buy_billStoreId'] = 0;
            }

            //returnbuybillsysdate
            if (is_null($row['back_buy_billSysDate'])) {
                $row['back_buy_billSysDate'] = '';
            }

            //returnbuybillsaveid
            if (is_null($row['back_buy_billSaveId'])) {
                $row['back_buy_billSaveId'] = 0;
            }



            //insert data into returnbuybill tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".returnbuybill


				 (returnbuybillid, returnbuybillSerial, returnbuybilldate, returnbuybillsupplierid, returnbuybilltotaldeptbefor, returnbuybilltotaldeptafter, returnbuybilldiscount, returnbuybilldiscountrype, returnbuybilltotalbill, returnbuybillaftertotalbill, returnbuybilltotalpayed, returnbuybillfinalbill, returnbuybillstoreid, returnbuybillsysdate, userid, conditions, returnbuybillclientname, returnbuybillsaveid, billnameid, returnbuybilldirectpayment)

				VALUES

				(" . $row['ID'] . ", '" . $row['back_buy_billSerial'] . "', '" . $row['back_buy_billDate'] . "', '" . $row['back_buy_billSupplierId'] . "',  '" . $row['back_buy_billTotalDeptBefor'] . "', '" . $row['back_buy_billTotalDeptAfter'] . "', '" . $row['back_buy_billDiscount'] . "', '" . $row['back_buy_billDiscountType'] . "', '" . $row['back_buy_billTotalBill'] . "', '" . $row['back_buy_billAfterTotalBill'] . "', '" . $row['back_buy_billTotalPayed'] . "', '" . $row['back_buy_billFinalBill'] . "',  '" . $row['back_buy_billStoreId'] . "', '" . $row['back_buy_billSysDate'] . "', 1, '" . $row['back_buy_billTypeDeleted'] . "', '', '" . $row['back_buy_billSaveId'] . "',  2,  " . $returnbuybilldirectpayment . ")"
                            , $newDatabaseLink) or die(mysql_error());
        }
    } catch (Exception $e) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".returnbuybill");
    }



    $smarty->assign("rowCount", mysql_affected_rows());
    $smarty->assign("tablename", "مردودات المشتريات");

    returnBuyBillDetail($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
}

function returnBuyBillDetail($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;

    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');



    try {

        //select data from back_buy_bill_details_tbl

        $returnbuybilldetailResult = mysql_query("SELECT * FROM  " . $olddatabaseName . ".back_buy_bill_details_tbl", $oldDatabaseLink) or die(mysql_error());


        while ($returnbuybilldetailRow = mysql_fetch_array($returnbuybilldetailResult)) {

            //returnbuybillid
            if (is_null($returnbuybilldetailRow['back_buy_bill_Id'])) {
                $returnbuybilldetailRow['back_buy_bill_Id'] = 0;
            }

            //returnbuybilldetailcatid
            if (is_null($returnbuybilldetailRow['back_buy_billCatId'])) {
                $returnbuybilldetailRow['back_buy_billCatId'] = 0;
            }

            //back_buy_billProductId
            if (is_null($returnbuybilldetailRow['back_buy_billProductId'])) {
                $returnbuybilldetailRow['back_buy_billProductId'] = 0;
            }

            //back_buy_billProductId
            if (is_null($returnbuybilldetailRow['back_buy_billQuantity'])) {
                $returnbuybilldetailRow['back_buy_billQuantity'] = 0;
            }

            //back_buy_billProductId
            if (is_null($returnbuybilldetailRow['back_buy_billPrice'])) {
                $returnbuybilldetailRow['back_buy_billPrice'] = 0;
            }

            //back_buy_billProductId
            if (is_null($returnbuybilldetailRow['back_buy_billTotalPrice'])) {
                $returnbuybilldetailRow['back_buy_billTotalPrice'] = 0;
            }


            //back_buy_billProductId
            if (is_null($returnbuybilldetailRow['backbuy_billDate'])) {
                $returnbuybilldetailRow['backbuy_billDate'] = '';
            }



            //insert data into returnbuybilldetail tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".returnbuybilldetail


				 (returnbuybilldetailid, Parcode, returnbuybillid, returnbuybilldetailcatid, returnbuybilldetailproductid, returnbuybilldetailquantity, returnbuybilldetailprice, returnbuybilldetailtotalprice, discountvalue, note, productunitid)

				VALUES

				(" . $returnbuybilldetailRow['ID'] . ", '', '" . $returnbuybilldetailRow['back_buy_bill_Id'] . "', '" . $returnbuybilldetailRow['back_buy_billCatId'] . "', '" . $returnbuybilldetailRow['back_buy_billProductId'] . "',  '" . $returnbuybilldetailRow['back_buy_billQuantity'] . "', '" . $row['back_buy_billPrice'] . "', '" . $returnbuybilldetailRow['back_buy_billTotalPrice'] . "', '" . $returnbuybilldetailRow['product_discount'] . "', '','" . $returnbuybilldetailRow['back_buy_billProductId'] . "')"
                            , $newDatabaseLink) or die(mysql_error());
        }
    } catch (Exception $e) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".returnbuybill");
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".returnbuybilldetail");
    }
}

function returnSellBill($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;


    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    try {


        //select data from back_sell_bill_tbl

        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".back_sell_bill_tbl", $oldDatabaseLink) or die(mysql_error());






        while ($row = mysql_fetch_array($result)) {

            //back_sell_billClientId
            if (is_null($row['back_sell_billClientId'])) {
                $row['back_sell_billClientId'] = 0;
            }


            //returnsellbildirectpayment
            $returnsellbildirectpayment;
            if ($row['back_sell_billClientId'] == 0) {
                $returnsellbildirectpayment = 0;
            } else {
                $returnsellbildirectpayment = 1;
            }

            //back_sell_billTotalDeptBefor
            if (is_null($row['back_sell_billTotalDeptBefor'])) {
                $row['back_sell_billTotalDeptBefor'] = 0;
            }

            //back_sell_billTotalDeptAfter
            if (is_null($row['back_sell_billTotalDeptAfter'])) {
                $row['back_sell_billTotalDeptAfter'] = 0;
            }


            //back_sell_billDiscount
            if (is_null($row['back_sell_billDiscount'])) {
                $row['back_sell_billDiscount'] = 0;
            }

            //back_sell_billTotalBill
            if (is_null($row['back_sell_billTotalBill'])) {
                $row['back_sell_billTotalBill'] = 0;
            }

            //back_sell_billAfterTotalBill
            if (is_null($row['back_sell_billAfterTotalBill'])) {
                $row['back_sell_billAfterTotalBill'] = 0;
            }

            //back_sell_billTotalPayed
            if (is_null($row['back_sell_billTotalPayed'])) {
                $row['back_sell_billTotalPayed'] = 0;
            }

            //back_sell_billFinalBill
            if (is_null($row['back_sell_billFinalBill'])) {
                $row['back_sell_billFinalBill'] = 0;
            }

            //back_sell_billStoreId
            if (is_null($row['back_sell_billStoreId'])) {
                $row['back_sell_billStoreId'] = 0;
            }


            //back_sell_billSysDate
            if (is_null($row['back_sell_billSysDate'])) {
                $row['back_sell_billSysDate'] = '';
            }

            //back_sell_billSaveId
            if (is_null($row['back_sell_billSaveId'])) {
                $row['back_sell_billSaveId'] = 0;
            }


            if (is_null($row['seller'])) {
                $row['seller'] = 0;
            }


            //insert data into returnsellbill tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".returnsellbill


				 (returnsellbillid, returnsellbillserial, returnsellbilldate, returnsellbillclientid, returnsellbilltotaldeptbefor, returnsellbilltotaldeptafter, returnsellbilldiscount, returnsellbilldiscounttype, returnsellbilltotalbill, returnsellbillaftertotalbill, returnsellbilltotalpayed, returnsellbillfinalbill, returnsellbillstoreid, returnsellbillsysdate, userid, conditions, returnsellbillclientname, returnsellbillsaveid, billnameid, returnsellbildirectpayment, sellerid, tempclientName)

				VALUES

				(" . $row['ID'] . ", '" . $row['back_sell_billSerial'] . "', '" . $row['back_sell_billDate'] . "', '" . $row['back_sell_billClientId'] . "',  '" . $row['back_sell_billTotalDeptBefor'] . "', '" . $row['back_sell_billTotalDeptAfter'] . "', '" . $row['back_sell_billDiscount'] . "', '" . $row['back_sell_billDiscountType'] . "', '" . $row['back_sell_billTotalBill'] . "', '" . $row['back_sell_billAfterTotalBill'] . "', '" . $row['back_sell_billTotalPayed'] . "', '" . $row['back_sell_billFinalBill'] . "',  '" . $row['back_sell_billStoreId'] . "', '" . $row['back_sell_billSysDate'] . "', 1, '" . $row['back_sell_billTypeDeleted'] . "', '', '" . $row['back_sell_billSaveId'] . "',  4,  " . $returnsellbildirectpayment . ", " . $row['seller'] . ", '')"
                            , $newDatabaseLink) or die(mysql_error());
        }
    } catch (Exception $e) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".returnsellbill");
    }


    $smarty->assign("rowCount", mysql_affected_rows());
    $smarty->assign("tablename", "مردودات المبيعات");


    returnSellBilldetail($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink);
}

function returnSellBilldetail($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;


    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    try {


        //select data from back_sell_bill_details_tbl


        $returnsellbilldetailResult = mysql_query("SELECT * FROM  " . $olddatabaseName . ".back_sell_bill_details_tbl", $oldDatabaseLink) or die(mysql_error());


        while ($returnsellbilldetailRow = mysql_fetch_array($returnsellbilldetailResult)) {
            //back_sell_bill_Id
            if (is_null($returnsellbilldetailRow['back_sell_bill_Id'])) {
                $returnsellbilldetailRow['back_sell_bill_Id'] = 0;
            }

            //back_sell_billCatId
            if (is_null($returnsellbilldetailRow['back_sell_billCatId'])) {
                $returnsellbilldetailRow['back_sell_billCatId'] = 0;
            }

            //back_sell_billProductId
            if (is_null($returnsellbilldetailRow['back_sell_billProductId'])) {
                $returnsellbilldetailRow['back_sell_billProductId'] = 0;
            }

            //back_sell_billQuantity
            if (is_null($returnsellbilldetailRow['back_sell_billQuantity'])) {
                $returnsellbilldetailRow['back_sell_billQuantity'] = 0;
            }

            //back_sell_billPrice
            if (is_null($returnsellbilldetailRow['back_sell_billPrice'])) {
                $returnsellbilldetailRow['back_sell_billPrice'] = 0;
            }


            //back_sell_billTotalPrice
            if (is_null($returnsellbilldetailRow['back_sell_billTotalPrice'])) {
                $returnsellbilldetailRow['back_sell_billTotalPrice'] = 0;
            }



            //insert data into returnsellbilldetail tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".returnsellbilldetail


					 (returnsellbilldetailid, buyprice, parcode, returnsellbillid, returnsellbilldetailcatid, returnsellbilldetailproductid, returnsellbilldetailquantity, returnsellbilldetailprice, returnsellbilldetailtotalprice, conditions, note, pricetype, discountvalue, productunitid, buydiscount, buydiscounttype)

					VALUES

					(" . $returnsellbilldetailRow['ID'] . ",
					'" . $returnsellbilldetailRow['buyprice'] . "',
					 '',
					 '" . $returnsellbilldetailRow['back_sell_bill_Id'] . "',
					 '" . $returnsellbilldetailRow['back_sell_billCatId'] . "',
					 '" . $returnsellbilldetailRow['back_sell_billProductId'] . "',
					 '" . $returnsellbilldetailRow['back_sell_billQuantity'] . "',
					 '" . $returnsellbilldetailRow['back_sell_billPrice'] . "',
					 '" . $returnsellbilldetailRow['back_sell_billTotalPrice'] . "',
					  0,
					  '',
					  1,
					  '" . $returnsellbilldetailRow['product_discount'] . "',
					  '" . $returnsellbilldetailRow['back_sell_billProductId'] . "',
					   0,
					   0)"
                            , $newDatabaseLink) or die(mysql_error());
        }
    } catch (Exception $e) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".returnsellbill");
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".returnsellbilldetail");
    }
}

function bankaccount($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {

    global $smarty;

    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    try {

        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".bank_account_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }


    while ($row = mysql_fetch_array($result)) {

        try {

            //insert in bank tbl
            mysql_query("INSERT INTO " . $newdatabaseName . ".bank


				 (bankid, bankname, bankdetails, userid, conditions, bankdate)

				VALUES

				(" . $row['id'] . ", '" . $row['bankName'] . "', '" . $row['notes'] . "', 1,  '" . $row['type_deleted'] . "', '" . $row['date'] . "')"
                            , $newDatabaseLink) or die(mysql_error());


            //select data from bank tbl
            $bankResult = mysql_query("SELECT * FROM  " . $newdatabaseName . ".bank order by bankid desc limit 1", $newDatabaseLink) or die(mysql_error());


            $bankRow = mysql_fetch_row($bankResult);

            $bankId = $bankRow['bankid'];



            //insert in bankaccount tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".bankaccount

				 (accountname, comment, accountnumber, accountbeginingbalance, accountdate, accountcurrencytype, bankid, userid, conditions)

				VALUES

				('" . $row['accName'] . "', '" . $row['notes'] . "', '" . $row['accNo'] . "',  '" . $row['start_val'] . "', '" . $row['date'] . "', ''," . $bankId . " , 1, '" . $row['type_deleted'] . "')"
                            , $newDatabaseLink) or die(mysql_error());
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".bank");
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".bankaccount");
        }
    }


    $smarty->assign("rowCount", mysql_affected_rows());
    $smarty->assign("tablename", "حسابات البنوك");
}

function checkdeposit($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {

    global $smarty;

    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    try {

        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".bank_deposite_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }


    while ($row = mysql_fetch_array($result)) {

        try {

            //select data from bank tbl

            $bankaccountResult = mysql_query("SELECT * FROM  " . $newdatabaseName . ".bankaccount where bankid = " . $row['accId'] . " limit 1", $newDatabaseLink) or die(mysql_error());


            $bankaccountRow = mysql_fetch_row($bankaccountResult);

            $accountId = $bankaccountRow['accountid'];


            //insert in checkdeposit tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".checkdeposit


				 (checkdepositid, bankaccountid, clientid, checkdepositnumber, checkdeposituser, checkdepositamount, checkdepositdate, checkdepositnote, userid, conditions, accountbeforevalue)

				VALUES

				(" . $row['id'] . ", " . $accountId . ", 1,'" . $row['checkno'] . "',  '" . $row['mostafedName'] . "', '" . $row['value_deposite'] . "',  '" . $row['deposite_date'] . "',  '" . $row['notes'] . "',  1,  '" . $row['type_deleted'] . "',  '" . $row['value_before'] . "')"
                            , $newDatabaseLink) or die(mysql_error());
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".checkdeposit");
        }
    }


    $smarty->assign("rowCount", mysql_affected_rows());
    $smarty->assign("tablename", "ايداع الشيكات");
}

function checkwithdrawal($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;

    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    try {

        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".bank_sa7b_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }


    while ($row = mysql_fetch_array($result)) {

        try {

            //select data from bank tbl

            $bankaccountResult = mysql_query("SELECT * FROM  " . $newdatabaseName . ".bankaccount where bankid = " . $row['accId'] . " limit 1", $newDatabaseLink) or die(mysql_error());


            $bankaccountRow = mysql_fetch_row($bankaccountResult);

            $accountId = $bankaccountRow['accountid'];


            //insert in checkdeposit tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".checkwithdrawal


				 (checkwithdrawalid, bankaccountid, supplierid, accountbeforevalue, checkwithdrawaluser, checkwithdrawalamount, checkwithdrawaldate, checkwithdrawalnote, userid, conditions, checkwithdrawalnumber)

				VALUES

				(" . $row['id'] . ", " . $accountId . ", 1,'" . $row['value_before'] . "',  '" . $row['mostafedName'] . "', '" . $row['value_sa7b'] . "', '" . $row['sa7b_date'] . "',  '" . $row['notes'] . "',  1,  '" . $row['type_deleted'] . "',  '" . $row['checkno'] . "')"
                            , $newDatabaseLink) or die(mysql_error());
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".checkwithdrawal");
        }
    }


    $smarty->assign("rowCount", mysql_affected_rows());
    $smarty->assign("tablename", "سحب الشيكات");
}

function product($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;

    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');


    try {


        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".product_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }


    while ($row = mysql_fetch_array($result)) {

        //productName
        if (is_null($row['productName'])) {
            $row['productName'] = '';
        }

        //productDesc
        if (is_null($row['productDesc'])) {
            $row['productDesc'] = '';
        }

        //productCatId
        if (is_null($row['productCatId'])) {
            $row['productCatId'] = 0;
        }

        //productTotSellPrice
        if (is_null($row['productTotSellPrice'])) {
            $row['productTotSellPrice'] = 0;
        }


        //productSingleSellPrice
        if (is_null($row['productSingleSellPrice'])) {
            $row['productSingleSellPrice'] = 0;
        }

        //productTotSingleSellPrice
        if (is_null($row['productTotSingleSellPrice'])) {
            $row['productTotSingleSellPrice'] = 0;
        }


        //productDate
        if (is_null($row['productDate'])) {
            $row['productDate'] = '';
        }

        //productLimitNo
        if (is_null($row['productLimitNo'])) {
            $row['productLimitNo'] = 0;
        }


        //parcode
        if (is_null($row['parcode'])) {
            $row['parcode'] = '';
        }


        try {

            //insert in product tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".product


				 (productId, productName, productDescription, productCatId, productBuyPrice, productSellAllPrice, productSellUnitPrice, productSellHalfPrice, productDate, conditions, userId, limitamount, parcode)

				VALUES

				(" . $row['ID'] . ", '" . $row['productName'] . "','" . $row['productDesc'] . "',  '" . $row['productCatId'] . "', '" . $row['buyprice'] . "', '" . $row['productTotSellPrice'] . "',  '" . $row['productSingleSellPrice'] . "', '" . $row['productTotSingleSellPrice'] . "',  '" . $row['productDate'] . "',  '" . $row['productTypeDeleted'] . "', 1, '" . $row['productLimitNo'] . "', '" . $row['parcode'] . "')"
                            , $newDatabaseLink) or die(mysql_error());



            mysql_query("INSERT INTO " . $newdatabaseName . ".productunit

	(productunitid, unitid, productid, productnumber, productunitdate, userid, conditions)

				VALUES

				(" . $row['ID'] . ", 1, " . $row['ID'] . ", 1,'" . date("Y-m-d") . "', 1, 0)"
                            , $newDatabaseLink) or die(mysql_error());
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".product");
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".productunit");
        }
    }


    $smarty->assign("rowCount", mysql_affected_rows());
    $smarty->assign("tablename", "المنتجات");
}

function programsettings($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;

    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');


    try {

        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".prog_setting_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }



    while ($row = mysql_fetch_array($result)) {

        //prog_settingCompany
        if (is_null($row['prog_settingCompany'])) {
            $row['prog_settingCompany'] = '';
        }


        try {

            //insert in product tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".programsettings


				 (programsettingsid, companyname, address, specializtion, phone, faxnumber, email, website, logo, roundnumbers, saveprinting, deptprinting, printingpapersize, userid)

				VALUES

				(" . $row['ID'] . ", '" . $row['prog_settingCompany'] . "','" . $row['prog_settingAddress'] . "',  '" . $row['prog_settingSpecialize'] . "', '" . $row['prog_settingTelNo'] . "', '" . $row['prog_settingFaxNo'] . "',  '" . $row['prog_settingEmail'] . "', '" . $row['prog_settingwebsite'] . "',  '" . $row['link3'] . "',  '" . $row['round_numbers'] . "', '" . $row['link1'] . "', '" . $row['link2'] . "', 0, 1)"
                            , $newDatabaseLink) or die(mysql_error());
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".programsettings");
        }
    }


    $smarty->assign("rowCount", mysql_affected_rows());
    $smarty->assign("tablename", "اعدادت البرنامج");
}

function save($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;

    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');


    try {

        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".saves_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }



    while ($row = mysql_fetch_array($result)) {

        //saveName
        if (is_null($row['saveName'])) {
            $row['saveName'] = '';
        }

        //saveValue
        if (is_null($row['saveValue'])) {
            $row['saveValue'] = 0;
        }


        //saveDesc
        if (is_null($row['saveDesc'])) {
            $row['saveDesc'] = '';
        }


        //saveDate
        if (is_null($row['saveDate'])) {
            $row['saveDate'] = '';
        }
        try {

            //insert in product tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".save


				 (saveid, savename, savecurrentvalue, savedetails, savedate, conditions, userid)

				VALUES

				(" . $row['ID'] . ",
				'" . $row['saveName'] . "',
				'" . $row['saveValue'] . "',
				'" . $row['saveDesc'] . "',
				'" . $row['saveDate'] . "',
				'" . $row['saveTypeDeleted'] . "',
				 1)"
                            , $newDatabaseLink) or die(mysql_error());
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".save");
        }
    }


    $smarty->assign("rowCount", mysql_affected_rows());
    $smarty->assign("tablename", "الخزن");
}

function savedaily($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;

    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    try {
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".save_report_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }

    while ($row = mysql_fetch_array($result)) {
        if ($row['saveModel'] == "sellbill") {
            $row['saveModel'] = "sellbillController.php";
        }

        if ($row['saveModel'] == "bill") {
            $row['saveModel'] = "buyBillController.php";
        }

        if ($row['saveModel'] == "sellbillback") {
            $row['saveModel'] = "returnsellbillController.php";
        }

        if ($row['saveModel'] == "supplierdept") {
            $row['saveModel'] = "supplierPayedDeptController.php";
        }

        if ($row['saveModel'] == "clientdebt") {
            $row['saveModel'] = "clientPayedDeptController.php";
        }

        if ($row['saveModel'] == "Expenseadd") {
            $row['saveModel'] = "expensesController.php";
        }

        if ($row['saveModel'] == "billback") {
            $row['saveModel'] = "returnBuyBillController.php";
        }

        if ($row['saveModel'] == "withdrawal") {
            $row['saveModel'] = "partnerwithdrawalController.php";
        }

        if ($row['saveModel'] == "ment") {
            $row['saveModel'] = "settlementsaveController.php";
        }

        try {
            //savePersonID
            if (is_null($row['savePersonID'])) {
                $row['savePersonID'] = 0;
            }

            //savedailychangetype
            if ($row['SaveOpKind'] == 0) {
                $savedailychangetype = 1;
            } else {
                $savedailychangetype = 0;
            }

            //insert in savedaily tbl
            mysql_query("INSERT INTO " . $newdatabaseName . ".savedaily
				 (savedailyid, savedailysavebefore, savedailychangeamount, savedailychangetype, saveid, processname, savedailymodelid, savedailysaveafter, savedailydate, userid, tablename)

				VALUES

				(" . $row['saveID'] . ",
				'" . $row['saveBefor'] . "',
				'" . $row['saveMoney'] . "',
				'" . $savedailychangetype . "',
				1,
				'" . $row['saveOpreation'] . "',
				'" . $row['saveModelID'] . "',
				'" . $row['saveAfter'] . "',
				'" . $row['saveDate'] . "',
				1,
				'" . $row['saveModel'] . "')"
                            , $newDatabaseLink) or die(mysql_error());
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".savedaily");
        }
    }


    $smarty->assign("rowCount", mysql_affected_rows());
    $smarty->assign("tablename", "يومية الخزن");
}

function user($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;

    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');


    try {

        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".users_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }



    while ($row = mysql_fetch_array($result)) {
        try {

            //UserName
            if (is_null($row['UserName'])) {
                $row['UserName'] = '';
            }

            //usersPassword
            if (is_null($row['usersPassword'])) {
                $row['usersPassword'] = '';
            }


            //usersName
            if (is_null($row['usersName'])) {
                $row['usersName'] = '';
            }


            //usersDate
            if (is_null($row['usersDate'])) {
                $row['usersDate'] = '';
            }



            //insert in product tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".user


				 (userid, username, password, usergroupid, employeename, currentdate, conditions, saveid)

				VALUES

				(" . $row['ID'] . ",
				'" . $row['UserName'] . "',
				'" . $row['usersPassword'] . "',
				1,
				'" . $row['usersName'] . "',
				'" . $row['usersDate'] . "',
				'" . $row['usersTypeDeleted'] . "',
				1)"
                            , $newDatabaseLink) or die(mysql_error());
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".user");
        }
    }


    $smarty->assign("rowCount", mysql_affected_rows());
    $smarty->assign("tablename", "المستخدمين");
}

//Yasmeen functions
function buyBill($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    $count = 0;
    try {
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".buy_bill_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }

    while ($row = mysql_fetch_array($result)) {
        $buybilldirectpayment;
        $buy_billOptionalSerial;
        if ($row['buy_billSupplierId'] == 0) {
            $buybilldirectpayment = 0;
        } else {
            $buybilldirectpayment = 1;
        }

        if ($row['buy_billOptionalSerial'] != "" || !empty($row['buy_billOptionalSerial'])) {
            $serial = $row['buy_billOptionalSerial'];
        } else {
            $serial = $row['buy_billSerial'];
        }



        mysql_query("INSERT INTO " . $newdatabaseName . ".`buybill`
			(buybillid, `buybillSerial` , `buybilldate` , `buybillsupplierid` , `buybilltotaldeptbefor` , `buybilltotaldeptafter` , `buybilldiscount` , `buybilldiscountrype` , `buybilltotalbill` , `buybillaftertotalbill` , `buybilltotalpayed` , `buybillfinalbill` , `buybillstoreid` , `buybillsysdate` , `userid` , `conditions` , `buybillclientname` , `buybillsaveid` , `buybilldirectpayment` , `billnameid` )

			VALUES
			('" . $row['ID'] . "', '" . $row['buy_billSerial'] . "', '" . $row['buy_billDate'] . "', '" . $row['buy_billSupplierId'] . "',  '" . $row['buy_billTotalDeptBefor'] . "', '" . $row['buy_billTotalDeptAfter'] . "', '" . $row['buy_billDiscount'] . "', '" . $row['buy_billDiscountType'] . "', '" . $row['buy_billTotalBill'] . "', '" . $row['buy_billAfterTotalBill'] . "', '" . $row['buy_billTotalPayed'] . "', '" . $row['buy_billFinalBill'] . "',  '" . $row['buy_billStoreId'] . "', '" . $row['buy_billSysDate'] . "', 1, '" . $row['buy_billTypeDeleted'] . "', '', '" . $row['buy_billSaveId'] . "', " . $buybilldirectpayment . ", 1)"
                , $newDatabaseLink);

        if ($error = mysql_error()) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`buybill`");
            die('Error, select query failed with:' . $error);
        }

        $count++;
    }

    $smarty->assign("rowCount", $count);


    //////////////////////////////////////////////////////

    $result2 = mysql_query("SELECT * FROM  " . $olddatabaseName . ".buy_bill_details_tbl", $oldDatabaseLink);
    if ($error = mysql_error()) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`buybill`");
        die('Error, insert query failed with:' . $error);
    }

    while ($row = mysql_fetch_array($result2)) {
        mysql_query("INSERT INTO " . $newdatabaseName . ".`buybilldetail`
			( `Parcode` , `buybilldetailid` , `buybillid` , `buybilldetailcatid` , `buybilldetailproductid` , `buybilldetailquantity` , `buybilldetailprice` , `buybilldetailtotalprice` , `discountvalue` , `note` , `buybilldetaildate` , `productunitid` )

			VALUES
			(' ', '" . $row['ID'] . "', '" . $row['buy_bill_Id'] . "',  '0', '" . $row['buy_billProductId'] . "', '" . $row['buy_billQuantity'] . "', '" . $row['buy_billPrice'] . "', '" . $row['buy_billTotalPrice'] . "', '" . $row['product_discount'] . "', '',  '" . $row['buy_billDate'] . "', '" . $row['buy_billProductId'] . "')"
                , $newDatabaseLink);

        if ($error = mysql_error()) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`buybill`");
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`buybilldetail`");
            die('Error, insert query failed with:' . $error);
        }
        $count++;
    }


    $smarty->assign("tablename", "فاتورة الشراء");
}

function productCategory($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    $count = 0;
    try {
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".cat_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }

    while ($row = mysql_fetch_array($result)) {


        mysql_query("INSERT INTO " . $newdatabaseName . ".`productcat`
				(productCatId, `productCatName` , `productCatDate` , `productCatDescription` , `productCatParent` , `userId` , `conditions` , `selldiscount` , `buydiscount` , `discounttype` )

				VALUES
				('" . $row['ID'] . "', '" . $row['catName'] . "', '" . $row['catDate'] . "', '" . $row['catDesc'] . "',  '0', '1', '" . $row['catTypeDeleted'] . "', '" . $row['sell'] . "', '" . $row['buy'] . "', '" . $row['type'] . "')"
                , $newDatabaseLink);

        if ($error = mysql_error()) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`productcat`");
            die('Error, insert query failed with:' . $error);
        }

        $count++;
    }
    $smarty->assign("rowCount", $count);

    $smarty->assign("tablename", "تصنيفات المنتجات");
}

function client($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    $count = 0;
    try {
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".clients_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }

    while ($row = mysql_fetch_array($result)) {

        mysql_query("INSERT INTO " . $newdatabaseName . ".`client`
				 ( `clientid` , `clientname` , `clientaddress` , `clientphone` , `clientmobile` , `clientdebt` , `clientdetails` , `conditions` , `clientdate` , `userid` )

				VALUES
				('" . $row['ID'] . "',
				 '" . $row['clientName'] . "',
				 '" . $row['clientAddress'] . "',
				 '" . $row['clientTelephone'] . "',
				 '' ,
				 '" . $row['clientTotalDept'] . "',
				 '" . $row['clientComment'] . "',
				 '" . $row['clientTypeDeleted'] . "',
				 '" . $row['clientDate'] . "',
				 '1')"
                , $newDatabaseLink);

        if ($error = mysql_error()) {
            //print("truncate");
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`client`");
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`clientdebtchange`");
            die('Error, insert query failed with:' . $error);
        }
        $count++;
    }

    $smarty->assign("rowCount", $count);

    ////////////////////////////////
    $result2 = mysql_query("SELECT * FROM  " . $olddatabaseName . ".client1_report_tbl", $oldDatabaseLink);
    if ($error = mysql_error()) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`client`");
        die('Error, select query failed with:' . $error);
    }

    while ($row = mysql_fetch_array($result2)) {
        $type;
        if ($row['SR_Dept_Befor'] > $row['SR_Dept_After']) {
            $type = 1;
        } else {
            $type = 0;
        }


        if ($row['SR_Model'] == "sell") {
            //$description = "فتورة بيع";
            $description = "اضافة فاتورة مبيعات";
            $row['SR_Model'] = "sellbillController.php";
        }


        if ($row['SR_Model'] == "sellback") {
            //$description=" فتورة مردودات مبيعات";
            $description = "اضافة فاتورة مردوات مبيعات";
            $row['SR_Model'] = "returnsellbillController.php";
        }


        if ($row['SR_Model'] == "back_and_sell") {
            //$description = "مبيعات ومردودات مبيعات";
            $description = "اضافة فاتورة مبيعات ومردودات";
            $row['SR_Model'] = "sellbillandruternController.php";
        }


        if ($row['SR_Model'] == "paydept") {
            $description = " سداد دين";
            $row['SR_Model'] = "clientPayedDeptController.php";
        }


        if ($row['SR_Model'] == "kem") {
            $description = "كمبيالة";
            $row['SR_Model'] = "kempialaController.php";
        }


        if ($row['SR_Model'] == "kemnon") {
            $description = "الغاء كمبيالة";
            $row['SR_Model'] = "kempialaController.php";
        }

        if ($row['SR_Model'] == "") {
            $row['SR_Model'] = "settlementsaveController.php";
        }

        mysql_query("INSERT INTO " . $newdatabaseName . ".clientdebtchange
			 (clientdebtchangeid, clientid, clientdebtchangebefore, clientdebtchangeamount, clientdebtchangetype, processname, clientdebtchangemodelid, clientdebtchangeafter, clientdebtchangedate, userid, tablename, comment, totalOperationCost)

			VALUES
			('" . $row['SR_ID'] . "',
			'" . $row['SR_SupplierID'] . "',
			'" . $row['SR_Dept_Befor'] . "',
			'" . $row['SR_Price_Payed'] . "',
			 '" . $type . "',
			 '" . $description . "',
			 '" . $row['SR_Model_ID'] . "',
			 '" . $row['SR_Dept_After'] . "',
			 '" . $row['SR_Date'] . "',
			 '1',
			 '" . $row['SR_Model'] . "',
			 ' ',
			 '0')"
                , $newDatabaseLink);


        if ($error = mysql_error()) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`client`");
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`clientdebtchange`");
            die('Error, insert query failed with:' . $error);
        }
        $count++;
    }


    $smarty->assign("tablename", "العملاء");
}

function supplier($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    $count = 0;
    try {
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".supplier_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }

    while ($row = mysql_fetch_array($result)) {
        mysql_query("INSERT INTO " . $newdatabaseName . ".`supplier`
				( `supplierid` , `suppliername` , `supplieraddress` , `supplierphone` , `suppliercurrentDebt` , `supplierdetails` , `conditions` , `userid` , `supplierdate` )

				VALUES
				('" . $row['ID'] . "',
				 '" . $row['supplierName'] . "',
				 '" . $row['supplierAddress'] . "',
				 '" . $row['supplierTelephone'] . "',
				 '" . $row['supplierTotalDept'] . "',
				 '" . $row['supplierComment'] . "',
				 '" . $row['supplierTypeDeleted'] . "',
				 '1',
				 '" . $row['supplierDate'] . "')"
                , $newDatabaseLink);

        if ($error = mysql_error()) {
            //print("truncate");
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`supplier`");
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`supplierdebtchange`");
            die('Error, insert query failed with:' . $error);
        }

        $count++;
    }

    $smarty->assign("rowCount", $count);

    ////////////////////////////////
    $result2 = mysql_query("SELECT * FROM  " . $olddatabaseName . ".supplier_report_tbl", $oldDatabaseLink);
    if ($error = mysql_error()) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`supplier`");
        die('Error, select query failed with:' . $error);
    }

    while ($row = mysql_fetch_array($result2)) {
        $type;
        if ($row['SR_Opreation'] == 0) {
            $type = 1;
        } else if ($row['SR_Opreation'] == 1) {
            $type = 0;
        }

        /////
        if ($row['SR_Model'] == "buy") {
            $description = "اضافة فاتورة مشتريات";
            $row['SR_Model'] = "buyBillController.php";
        }


        if ($row['SR_Model'] == "buyback") {
            $description = "اضافة فاتورة مردوات مشتريات";
            $row['SR_Model'] = "returnBuyBillController.php";
        }

        if ($row['SR_Model'] == "paydept") {
            $description = " سداد دين";
            $row['SR_Model'] = "supplierPayedDeptController.php";
        }

        if ($row['SR_Model'] == "supplier_3agz") {
            $description = "معالجة عجز مورد";
            $row['SR_Model'] = "supplierDeficitController.php";
        }

        if ($row['SR_Model'] == "kem") {
            $description = "كمبيالة";
            $row['SR_Model'] = "kempialaController.php";
        }


        if ($row['SR_Model'] == "kemnon") {
            $description = "الغاء كمبيالة";
            $row['SR_Model'] = "kempialaController.php";
        }
        /////

        mysql_query("INSERT INTO " . $newdatabaseName . ".supplierdebtchange
			 (supplierdebtchangeid, `supplierid` , `supplierdebtchangebefore` , `supplierdebtchangeamount` , `supplierdebtchangetype` , `processname` , `supplierdebtchangemodelid` , `supplierdebtchangeafter` , `supplierdebtchangedate` , `userid` , `tablename` , `comment` )

			VALUES
			('" . $row['SR_ID'] . "',
			'" . $row['SR_SupplierID'] . "',
			'" . $row['SR_Dept_Befor'] . "',
			'" . $row['SR_Price_Payed'] . "',
			'" . $type . "',
			'" . $description . "',
			'" . $row['SR_Model_ID'] . "',
			'" . $row['SR_Dept_After'] . "',
			'" . $row['SR_Date'] . "',
			'1',
			'" . $row['SR_Model'] . "',
			' ')"
                , $newDatabaseLink);


        if ($error = mysql_error()) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`supplier`");
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`supplierdebtchange`");
            die('Error, insert query failed with:' . $error);
        }
        $count++;
    }


    $smarty->assign("tablename", "الموردين");
}

function productUnit($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');
    $count = 0;
    try {
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".type_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }

    while ($row = mysql_fetch_array($result)) {
        mysql_query("INSERT INTO " . $newdatabaseName . ".`unit`
				( `unitId` , `unitName` , `unitSymbol` , `unitDescription` , `unitDate` , `conditions` , `userId`)

				VALUES
				('" . $row['ID'] . "',
				 '" . $row['typeName'] . "',
				 '" . $row['typeSymbol'] . "',
				 '" . $row['typeDesc'] . "',
				 '" . $row['typeDate'] . "',
				 '" . $row['typeTypeDeleted'] . "',
				 '1')", $newDatabaseLink);

        if ($error = mysql_error()) {
            //print("truncate");
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`unit`");
            die('Error, insert query failed with:' . $error);
        }
        $count++;
    }

    /////////// check if product table has data ///////////////////
    $product_result = mysql_query("SELECT * FROM " . $newdatabaseName . ".product", $newDatabaseLink);
    $num_of_products = mysql_num_rows($product_result);
    if (!empty($num_of_products) && $num_of_products > 0) {
        $old_product_result = mysql_query("SELECT * FROM " . $olddatabaseName . ".product_tbl", $oldDatabaseLink);
        while ($row2 = mysql_fetch_array($old_product_result)) {
            ////////////////////////////////
            if (isset($row2['productTypeId']) && !empty($row2['productTypeId']) && $row2['productTypeId'] != NULL) {
                mysql_query("INSERT INTO " . $newdatabaseName . ".productunit
					 (`unitid` , `productid` , `productnumber` , `productunitdate` , `userid` , `conditions`)
					VALUES
					('" . $row2['productTypeId'] . "',
					'" . $row2['ID'] . "',
					'1',
					'" . $row2['productDate'] . "',
					'1',
					'0')", $newDatabaseLink);

                if ($error = mysql_error()) {
                    mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`productunit`");
                    die('Error, insert query failed with:' . $error);
                }

                $count++;
            }
        }
    }

    $smarty->assign("rowCount", $count);
    $smarty->assign("tablename", "وحدات القياس");
}

function withdrawals($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    $count = 0;
    try {
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".withdrawals", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }

    while ($row = mysql_fetch_array($result)) {

        mysql_query("INSERT INTO " . $newdatabaseName . ".`partnerwithdrawal`
				 ( `partnerwithdrawalid` , `partnerid` , `partnerwithdrawalvalue` , `comment` , `partnerwithdrawaldate` , `userid` , `saveid` , `conditions` )

				VALUES
				('" . $row['id'] . "',
				'" . $row['toWhom'] . "',
				'" . $row['money'] . "',
				'" . $row['comment'] . "',
				'" . $row['enter_date'] . "',
				'1',
				'" . $row['save_id'] . "',
				'0')", $newDatabaseLink) or die(mysql_error());

        if ($error = mysql_error()) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`partnerwithdrawal`");
            die('Error, insert query failed with:' . $error);
        }

        $count++;
    }
    $smarty->assign("rowCount", $count);

    $smarty->assign("tablename", "مسحوبات الشركاء");
}

// nora function
//clientdebtchange




/* * *********************************************** */

// nora function
//clientdebtchange

function clientdebtchange($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName) {
    global $smarty;

    try {
        mysql_query("SET NAMES 'utf8'");
        mysql_query('SET CHARACTER SET utf8');
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".client_report_tbl", $oldDatabaseLink);
    } catch (Exception $e) {
        return 0;
    }

    $i = 0;
    while ($row = mysql_fetch_array($result)) {

        if (is_null($row['client_id'])) {
            $row['client_id'] = 0;
        }


        if (is_null($row['dept_date'])) {
            $row['dept_date'] = 0;
        }


        if (is_null($row['prev_dept'])) {
            $row['prev_dept'] = 0;
        }

        if (is_null($row['current_dept'])) {
            $row['current_dept'] = 0;
        }

        if (is_null($row['dept_after'])) {
            $row['dept_after'] = 0;
        }

        if (is_null($row['total_bill'])) {
            $row['total_bill'] = 0;
        }

        if (is_null($row['payed_money'])) {
            $row['payed_money'] = 0;
        }


        if (is_null($row['process_no'])) {
            $row['process_no'] = 0;
        }
        if (is_null($row['user_name'])) {
            $row['user_name'] = 0;
        }

        if (is_null($row['mortag3_id'])) {
            $row['mortag3_id'] = 0;
        }



        $type;
        if ($row['prev_dept'] > $row['dept_after']) {
            $type = 1;
        } else {
            $type = 0;
        }

        try {
            $i++;
            mysql_query("SET NAMES 'utf8'");
            mysql_query('SET CHARACTER SET utf8');
            mysql_query("INSERT INTO " . $newdatabaseName . ".clientdebtchange


				 (clientdebtchangeid,clientid, clientdebtchangebefore, clientdebtchangeamount, clientdebtchangetype, processname, clientdebtchangemodelid, clientdebtchangeafter, clientdebtchangedate, userid, tablename, comment)

				VALUES

				('" . $row['id'] . "','" . $row['client_id'] . "', '" . $row['prev_dept'] . "', '" . $row['current_dept'] . "',  '" . $type . "', '" . $row['describation'] . "', '" . $row['process_no'] . "', '" . $row['dept_after'] . "', '" . $row['dept_date'] . "', '1','0','0')"
                    , $newDatabaseLink);
        } catch (Exception $e) {
            //mysql_query("delete * FROM  ". $newdatabaseName.".clientdebtchange", $newDatabaseLink);
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".`clientdebtchange`");
            return 0;
        }
    }

    $smarty->assign("rowCount", $i);

    $smarty->assign("tablename", "تقرير مديونيه العميل");
}

//expenses
function expenses($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName) {
    global $smarty;


    try {
        mysql_query("SET NAMES 'utf8'");
        mysql_query('SET CHARACTER SET utf8');
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".expenses_tbl", $oldDatabaseLink);
    } catch (Exception $e) {
        return 0;
    }
    $i = 0;
    while ($row = mysql_fetch_array($result)) {

        if (is_null($row['exp_name'])) {
            $row['exp_name'] = "";
        }

        if (is_null($row['exp_type_id'])) {
            $row['exp_type_id'] = 0;
        }


        if (is_null($row['value'])) {
            $row['value'] = 0;
        }


        if (is_null($row['date'])) {
            $row['0000-00-00'] = 0;
        }


        if (is_null($row['comment'])) {
            $row['comment'] = "";
        }

        if (is_null($row['type_deleted'])) {
            $row['type_deleted'] = 0;
        }

        try {
            mysql_query("SET NAMES 'utf8'");
            mysql_query('SET CHARACTER SET utf8');
            mysql_query("INSERT INTO " . $newdatabaseName . ".expenses


				 (expensesid,expensestypeid, expensesname, expensesdetails, expensesValue, expensesdate, userid, conditions)

				VALUES

				('" . $row['id'] . "','" . $row['exp_type_id'] . "', '" . $row['exp_name'] . "', '" . $row['comment'] . "',  '" . $row['value'] . "', '" . $row['date'] . "', '1', '0')"
                    , $newDatabaseLink);

            $i++;
        } catch (Exception $e) {
            mysql_query("delete * FROM  " . $newdatabaseName . ".expenses", $newDatabaseLink);
        }
    }



    $smarty->assign("rowCount", $i);

    $smarty->assign("tablename", "المصروفات");
}

//expensestype

function expensestype($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName) {
    global $smarty;


    try {
        mysql_query("SET NAMES 'utf8'");
        mysql_query('SET CHARACTER SET utf8');
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".expenses_type", $oldDatabaseLink);
    } catch (Exception $e) {
        return 0;
    }
    $i = 0;
    while ($row = mysql_fetch_array($result)) {

        if (is_null($row['name'])) {
            $row['name'] = "";
        }

        if (is_null($row['comment'])) {
            $row['comment'] = "";
        }


        if (is_null($row['delete_reason'])) {
            $row['delete_reason'] = "";
        }

        try {
            mysql_query("SET NAMES 'utf8'");
            mysql_query('SET CHARACTER SET utf8');
            mysql_query("INSERT INTO " . $newdatabaseName . ".expensestype


					 (expensestypeid,expensestypename, expensestypedetails, expensestypedate, conditions, userid, parent)

					VALUES

					('" . $row['id'] . "','" . $row['name'] . "', '" . $row['comment'] . "', '0000-00-00',  '0', '1', '0')"
                    , $newDatabaseLink);

            $i++;
        } catch (Exception $e) {
            mysql_query("delete * FROM  " . $newdatabaseName . ".expensestype", $newDatabaseLink);
            return 0;
        }
    }

    $smarty->assign("rowCount", $i);

    $smarty->assign("tablename", "تصنيفات المصروفات");
}

//kempiala
function kempiala($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName) {
    global $smarty;


    try {
        mysql_query("SET NAMES 'utf8'");
        mysql_query('SET CHARACTER SET utf8');
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".kem_tbl", $oldDatabaseLink);
    } catch (Exception $e) {
        return 0;
    }
    $i = 0;
    while ($row = mysql_fetch_array($result)) {
        if (is_null($row['supplierID'])) {
            $row['supplierID'] = "";
        }

        if (is_null($row['sellingdate'])) {
            $row['sellingdate'] = "0000-00-00";
        }


        try {
            mysql_query("SET NAMES 'utf8'");
            mysql_query('SET CHARACTER SET utf8');
            mysql_query("INSERT INTO " . $newdatabaseName . ".kempiala


				 (id,clientID, recievingdate, value, clientcomment, selling,expiredate,supplierID,sellingdate,conditions, suppliercomment)

				VALUES

				('" . $row['id'] . "','" . $row['clientID'] . "', '" . $row['date'] . "', '" . $row['value'] . "','" . $row['comment'] . "', '" . $row['done'] . "','" . $row['acualdate'] . "','" . $row['supplierID'] . "','" . $row['sellingdate'] . "',0,'" . $row['del'] . "')"
                    , $newDatabaseLink);

            $i++;
        } catch (Exception $e) {
            mysql_query("delete * FROM  " . $newdatabaseName . ".kempiala", $newDatabaseLink);
            return 0;
        }
    }

    $smarty->assign("rowCount", $i);

    $smarty->assign("tablename", "الكمبيالات");
}

//partner
function partner($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName) {
    global $smarty;


    try {
        mysql_query("SET NAMES 'utf8'");
        mysql_query('SET CHARACTER SET utf8');
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".owners", $oldDatabaseLink);
    } catch (Exception $e) {
        return 0;
    }
    $i = 0;
    while ($row = mysql_fetch_array($result)) {

        if (is_null($row['owner_delete_reason'])) {
            $row['owner_delete_reason'] = "";
        }

        try {
            mysql_query("SET NAMES 'utf8'");
            mysql_query('SET CHARACTER SET utf8');
            mysql_query("INSERT INTO " . $newdatabaseName . ".partner


				 (partnerid,partnername, partnerdate, partnerpercentage, partnermoney, conditions,comment,userid)

				VALUES

				('" . $row['ID'] . "','" . $row['owner_name'] . "', '" . $row['owner_date'] . "', '0','0','" . $row['owner_delete'] . "','','1')"
                    , $newDatabaseLink);


            $i++;
        } catch (Exception $e) {
            mysql_query("delete * FROM  " . $newdatabaseName . ".partner", $newDatabaseLink);
            return 0;
        }
    }

    $smarty->assign("rowCount", $i);

    $smarty->assign("tablename", "الكمبيالات");
}

//sellbill
function sellbill($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName) {
    global $smarty;


    try {
        mysql_query("SET NAMES 'utf8'");
        mysql_query('SET CHARACTER SET utf8');
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".sell_bill_tbl", $oldDatabaseLink);
    } catch (Exception $e) {
        return 0;
    }
    $i = 0;
    while ($row = mysql_fetch_array($result)) {
        if (is_null($row['sell_billDate'])) {
            $row['sell_billDate'] = "0000-00-00";
        }

        if (is_null($row['sell_billSerial'])) {
            $row['sell_billSerial'] = "";
        }

        if (is_null($row['sell_billOptionalSerial'])) {
            $row['sell_billOptionalSerial'] = "";
        }


        if (is_null($row['sell_billClientId'])) {
            $row['sell_billClientId'] = 0;
        }


        if (is_null($row['sell_billTotalDeptBefor'])) {
            $row['sell_billTotalDeptBefor'] = 0;
        }


        if (is_null($row['sell_billTotalDeptAfter'])) {
            $row['sell_billTotalDeptAfter'] = 0;
        }

        if (is_null($row['sell_billDiscount'])) {
            $row['sell_billDiscount'] = 0;
        }


        if (is_null($row['sell_billDiscountType'])) {
            $row['sell_billDiscountType'] = 0;
        }


        if (is_null($row['sell_billTotalBill'])) {
            $row['sell_billTotalBill'] = 0;
        }

        if (is_null($row['sell_billAfterTotalBill'])) {
            $row['sell_billAfterTotalBill'] = 0;
        }


        if (is_null($row['sell_billTotalPayed'])) {
            $row['sell_billTotalPayed'] = 0;
        }


        if (is_null($row['sell_billFinalBill'])) {
            $row['sell_billFinalBill'] = 0;
        }


        if (is_null($row['sell_billStoreId'])) {
            $row['sell_billStoreId'] = 0;
        }


        if (is_null($row['sell_billSysDate'])) {
            $row['sell_billSysDate'] = "0000-00-00";
        }

        if (is_null($row['sell_billUserName'])) {
            $row['sell_billUserName'] = "";
        }

        if (is_null($row['sell_billTypeDeleted'])) {
            $row['sell_billTypeDeleted'] = 0;
        }

        if (is_null($row['sell_billDeleteReason'])) {
            $row['sell_billDeleteReason'] = "";
        }

        if (is_null($row['sell_billSaveId'])) {
            $row['sell_billSaveId'] = 1;
        }

        if (is_null($row['seller'])) {
            $row['seller'] = 0;
        }




        try {
            mysql_query("SET NAMES 'utf8'");
            mysql_query('SET CHARACTER SET utf8');
            mysql_query("INSERT INTO " . $newdatabaseName . ".sellbill


				 (sellbillid,sellbilldate, sellbillserial, sellbillclientid, sellbilltotaldeptbefor, sellbilltotaldeptafter,sellbilldiscount,sellbilldiscounttype,sellbilltotalbill,sellbillaftertotalbill,sellbilltotalpayed,sellbillfinalbill,sellbillstoreid,sellbillsysdate,userid,conditions,sellbillclientname,
sellbillsaveid,sellbildirectpayment,billnameid,sellerid,tempclientName)

				VALUES

				('" . $row['ID'] . "',
				'" . $row['sell_billDate'] . "',
				 '" . $row['sell_billSerial'] . "',
				 '" . $row['sell_billClientId'] . "',
				 '" . $row['sell_billTotalDeptBefor'] . "',
				 '" . $row['sell_billTotalDeptAfter'] . "',
				 '" . $row['sell_billDiscount'] . "',
				 '" . $row['sell_billDiscountType'] . "',
				 '" . $row['sell_billTotalBill'] . "',
				 '" . $row['sell_billAfterTotalBill'] . "',
				 '" . $row['sell_billTotalPayed'] . "',
				 '" . $row['sell_billFinalBill'] . "',
				 '" . $row['sell_billStoreId'] . "',
				 '" . $row['sell_billSysDate'] . "',
				 '" . $row['seller'] . "',
				 '" . $row['sell_billTypeDeleted'] . "',
				 '" . $row['sell_billOptionalSerial'] . "',
				 '" . $row['sell_billSaveId'] . "',
				 '" . $row['sell_bilDirectPayment'] . "',
				 3,
				 '" . $row['seller'] . "',
				 '" . $row['sell_billOptionalSerial'] . "')"
                            , $newDatabaseLink) or die(mysql_error());


            $i++;
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE   " . $newdatabaseName . ".sellbill", $newDatabaseLink);
            return 0;
        }
    }



    $smarty->assign("rowCount", $i);

    sellbilldetail($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);



    $smarty->assign("tablename", "فاتوره المبيعات");
}

//sellbilldetail
function sellbilldetail($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName) {


    global $smarty;


    try {
        mysql_query("SET NAMES 'utf8'");
        mysql_query('SET CHARACTER SET utf8');
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".sell_bill_details_tbl", $oldDatabaseLink);
    } catch (Exception $e) {
        return 0;
    }
    $i = 0;
    while ($row = mysql_fetch_array($result)) {
        if (is_null($row['sell_bill_Id'])) {
            $row['sell_bill_Id'] = 0;
        }

        if (is_null($row['sell_billCatId'])) {
            $row['sell_billCatId'] = 0;
        }


        if (is_null($row['sell_billProductId'])) {
            $row['sell_billProductId'] = 0;
        }


        if (is_null($row['sell_billQuantity'])) {
            $row['sell_billQuantity'] = 0;
        }


        if (is_null($row['sell_billPrice'])) {
            $row['sell_billPrice'] = 0;
        }



        if (is_null($row['sell_billTotalPrice'])) {
            $row['sell_billTotalPrice'] = 0;
        }



        if (is_null($row['sell_billPriceType'])) {
            $row['sell_billPriceType'] = 0;
        }




        if (is_null($row['sell_billDate'])) {
            $row['store_detailsDate'] = "0000-00-00";
        }


        if (is_null($row['buyprice'])) {
            $row['buyprice'] = "0";
        }




        if (is_null($row['product_discount'])) {
            $row['product_discount'] = "0";
        }



        try {
            mysql_query("SET NAMES 'utf8'");
            mysql_query('SET CHARACTER SET utf8');
            mysql_query("INSERT INTO " . $newdatabaseName . ".sellbilldetail


				 (buyprice,parcode, sellbilldetailid, sellbillid, sellbilldetailcatid, sellbilldetailproductid,sellbilldetailquantity,sellbilldetailprice,sellbilldetailtotalprice,discountvalue,note,pricetype,sellbilldetaildate,productunitid,buydiscount,buydiscounttype)

				VALUES

				('" . $row['buyprice'] . "',
				'',
				 '" . $row['ID'] . "',
				 '" . $row['sell_bill_Id'] . "',
				 '" . $row['sell_billCatId'] . "',
				 '" . $row['sell_billProductId'] . "',
				 '" . $row['sell_billQuantity'] . "',
				 '" . $row['sell_billPrice'] . "',
				 '" . $row['sell_billTotalPrice'] . "',
				 '" . $row['product_discount'] . "',
				  '',
		         '" . $row['sell_billPriceType'] . "',
				  '" . $row['sell_billDate'] . "',
				  '" . $row['sell_billProductId'] . "',
				 '0',
				 '0')"
                            , $newDatabaseLink) or die(mysql_error());


            $i++;
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE   " . $newdatabaseName . ".sellbilldetail", $newDatabaseLink);
            return 0;
        }
    }
}

//storereport
function storereport($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName) {
    global $smarty;


    try {
        mysql_query("SET NAMES 'utf8'");
        mysql_query('SET CHARACTER SET utf8');
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".store_details", $oldDatabaseLink);
    } catch (Exception $e) {
        return 0;
    }


    $i = 0;
    while ($row = mysql_fetch_array($result)) {



        try {

            //mysql_query("SET NAMES 'utf8'");
            //mysql_query('SET CHARACTER SET utf8');
            mysql_query("INSERT INTO " . $newdatabaseName . ".storereport


					 (storereportid,productid, storeid, productquantity, storereporttype, storereportmodelid,processname,tablename,productbefore,productafter,userid,storereportdate)
					VALUES
					('" . $row['id'] . "',
					'" . $row['productid'] . "',
					 '" . $row['storeid'] . "',
					 '" . $row['productquantity'] . "',
					 '" . $row['opertation'] . "',
					 '" . $row['modelid'] . "',
					 '" . $row['model'] . "',
					 '',
					 '" . $row['productquantitybefor'] . "',
					 '" . $row['productquantityafter'] . "',
					 '1',
					 '" . $row['date'] . "'
				)"
                            , $newDatabaseLink) or die(mysql_error());


            $i++;
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE   " . $newdatabaseName . ".storereport", $newDatabaseLink);
            return 0;
        }
    }

    $smarty->assign("rowCount", $i);

    $smarty->assign("tablename", "تقرير المخازن ");
}

//store
function store($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName) {
    global $smarty;


    try {
        mysql_query("SET NAMES 'utf8'");
        mysql_query('SET CHARACTER SET utf8');
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".store_tbl", $oldDatabaseLink);
    } catch (Exception $e) {
        return 0;
    }


    $i = 0;
    while ($row = mysql_fetch_array($result)) {

        if (is_null($row['storeName'])) {
            $row['storeName'] = "";
        }


        if (is_null($row['storeAddress'])) {
            $row['storeAddress'] = "";
        }


        if (is_null($row['storeComment'])) {
            $row['storeComment'] = "";
        }



        if (is_null($row['storeDate'])) {
            $row['storeDate'] = "0000-00-00";
        }


        if (is_null($row['storeUserName'])) {
            $row['storeUserName'] = "";
        }

        if (is_null($row['storeDeleteReason'])) {
            $row['storeDeleteReason'] = "";
        }


        try {

            mysql_query("SET NAMES 'utf8'");
            mysql_query('SET CHARACTER SET utf8');
            mysql_query("INSERT INTO " . $newdatabaseName . ".store


					 (storeId, storeName, storeDate, storeDescription, conditions,userId)
					VALUES
					('" . $row['ID'] . "',
					 '" . $row['storeName'] . "',
					 '" . $row['storeDate'] . "',
					 '" . $row['storeAddress'] . "',
					 '" . $row['storeTypeDeleted'] . "',
					 '1'
				)"
                            , $newDatabaseLink) or die(mysql_error());


            $i++;
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE   " . $newdatabaseName . ".store", $newDatabaseLink);
            return 0;
        }
    }

    $smarty->assign("rowCount", $i);

    $smarty->assign("tablename", "المخازن");
}

//storemovement
function storemovement($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName) {
    global $smarty;


    try {
        mysql_query("SET NAMES 'utf8'");
        mysql_query('SET CHARACTER SET utf8');
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".store_transfer_tbl", $oldDatabaseLink);
    } catch (Exception $e) {
        return 0;
    }

    //print_r("SELECT * FROM  ". $olddatabaseName.".store_transfer_tbl<br>");
    $i = 0;
    while ($row2 = mysql_fetch_array($result)) {


        if (is_null($row2['store_transferStoreFrom'])) {
            $row2['store_transferStoreFrom'] = 0;
        }

        if (is_null($row2['store_transferStoreTo'])) {
            $row2['store_transferStoreTo'] = 0;
        }

        if (is_null($row2['store_transferDate'])) {
            $row2['store_transferDate'] = "0000-00-00";
        }

        if (is_null($row2['store_transferUserId'])) {
            $row2['store_transferUserId'] = 0;
        }


        $result2 = mysql_query("SELECT * FROM  " . $olddatabaseName . ".store_transfer_details_tbl  where store_transfer_detailsTransferId = " . $row2['ID'], $oldDatabaseLink);

        //	print_r("SELECT * FROM  ". $olddatabaseName.".store_transfer_details_tbl  where store_transfer_detailsTransferId = ".$row2['ID']."<br>");

        while ($row = mysql_fetch_array($result2)) {


            if (is_null($row['store_transfer_detailsTransferId'])) {
                $row['store_transfer_detailsTransferId'] = 0;
            }

            if (is_null($row['store_transfer_detailsProductId'])) {
                $row['store_transfer_detailsProductId'] = 0;
            }


            if (is_null($row['store_transfer_detailsQty'])) {
                $row['store_transfer_detailsQty'] = 0;
            }

            try {


                mysql_query("SET NAMES 'utf8'");
                mysql_query('SET CHARACTER SET utf8');

                mysql_query("INSERT INTO " . $newdatabaseName . ".storemovement


					 (transferproductid, storeidfrom, storeidto,productid, transferproductamount, transferproductdate,userid,conditions)
					VALUES
					('" . $row['ID'] . "',
					 '" . $row2['store_transferStoreFrom'] . "',
					 '" . $row2['store_transferStoreTo'] . "',
					 '" . $row['store_transfer_detailsProductId'] . "',
					 '" . $row['store_transfer_detailsQty'] . "',
					 '" . $row2['store_transferDate'] . "',
					  '" . $row2['store_transferUserId'] . "',
					   '0'

				)"
                                , $newDatabaseLink) or die(mysql_error());



                $i++;
            } catch (Exception $e) {
                mysql_query("TRUNCATE TABLE   " . $newdatabaseName . ".storemovement", $newDatabaseLink);
                return 0;
            }
        }
    }

    $smarty->assign("rowCount", $i);

    $smarty->assign("tablename", "النقل بين المخازن");
}

//productsetting
function productsetting($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName) {
    global $smarty;


    try {
        mysql_query("SET NAMES 'utf8'");
        mysql_query('SET CHARACTER SET utf8');
        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".product_setting_tbl", $oldDatabaseLink);
    } catch (Exception $e) {
        return 0;
    }


    $i = 0;
    while ($row = mysql_fetch_array($result)) {

        if (is_null($row2['product_settingValue'])) {
            $row2['product_settingValue'] = 0;
        }



        if (is_null($row2['product_settingProductId'])) {
            $row2['product_settingProductId'] = 0;
        }





        try {

            mysql_query("SET NAMES 'utf8'");
            mysql_query('SET CHARACTER SET utf8');
            mysql_query("INSERT INTO " . $newdatabaseName . ".productsetting


					 (productsettingid, leastValueAtStore, storeid)
					VALUES
					('" . $row['ID'] . "',
					 '" . $row['product_settingValue'] . "',
					 '" . $row['product_settingProductId'] . "'

				)"
                            , $newDatabaseLink) or die(mysql_error());


            $i++;
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE   " . $newdatabaseName . ".productsetting", $newDatabaseLink);
            return 0;
        }
    }

    $smarty->assign("rowCount", $i);

    $smarty->assign("tablename", "اعدادات المنتج داخل المخزن");
}

function storedetail($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    global $smarty;

    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');


    try {

        $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".store_details_tbl", $oldDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        return 0;
    }



    while ($row = mysql_fetch_array($result)) {

        //store_detailsStoreId
        if (is_null($row['store_detailsStoreId'])) {
            $row['store_detailsStoreId'] = 0;
        }

        //store_detailsQty
        if (is_null($row['store_detailsQty'])) {
            $row['store_detailsQty'] = 0;
        }

        //store_detailsDate
        if (is_null($row['store_detailsDate'])) {
            $row['store_detailsDate'] = '';
        }


        try {

            //insert in product tbl

            mysql_query("INSERT INTO " . $newdatabaseName . ".storedetail


				 (storedetailid, productid, storeid, productquantity, userid, storedetaildate)

				VALUES

				(" . $row['ID'] . ",
				'" . $row['store_detailsProductId'] . "',
				'" . $row['store_detailsStoreId'] . "',
				'" . $row['store_detailsQty'] . "',
				1,
				'" . $row['store_detailsDate'] . "')"
                            , $newDatabaseLink) or die(mysql_error());
        } catch (Exception $e) {
            mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".storedetail");
        }
    }


    $smarty->assign("rowCount", mysql_affected_rows());
    $smarty->assign("tablename", "تفاصيل البضاعة بالمخازن");
}

////////////////////////////MAIN TABLES//////////////////////////

function properties($location, $newdatabaseName, $newUserName, $newPassword) {
    $con = mysql_connect("localhost", $newUserName, $newPassword);
    mysql_select_db($newdatabaseName, $con);

    $charset = mysql_client_encoding($con);
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    mysql_query("INSERT INTO " . $newdatabaseName . ".`properties` VALUES (1, 'Ø®ØµØ§Ø¦Øµ Ø¹Ø§Ù…Ù‡', 0, 1, '')");

    //load file
    $commands = file_get_contents($location);

    //delete comments
    $lines = explode("\n", $commands);
    $commands = '';
    foreach ($lines as $line) {
        $line = trim($line);
        if ($line && !strpos($line, '--')) {
            $commands .= $line . "\n";
        }
    }

    //convert to array
    $commands = explode(";", $commands);

    //run commands
    $total = $success = 0;
    foreach ($commands as $command) {
        if (trim($command)) {
            $success += (@mysql_query($command) == false ? 0 : 1);
            $total += 1;
        }
    }

    //return number of successful queries and total number of queries found
    return array(
        "success" => $success,
        "total" => $total
    );
}

function usergroup($newdatabaseName, $newUserName, $newPassword) {
    $newDatabaseLink = mysql_connect('localhost', $newUserName, $newPassword, $newdatabaseName) or die(mysql_error());


    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');


    try {
        mysql_query("INSERT INTO " . $newdatabaseName . ".usergroup

				 (usergroupid, usergroupname, usergroupdescription, currentdate, conditions, startpage, level)

				VALUES

				(1, 'admin', 'admin', '" . date("Y-m-d") . "', 0, 'index.php', 1)"
                        , $newDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".usergroup");
    }
}

function relusergroupproperties($newdatabaseName, $newUserName, $newPassword) {
    $newDatabaseLink = mysql_connect('localhost', $newUserName, $newPassword, $newdatabaseName) or die(mysql_error());


    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');


    try {
        $result = mysql_query("SELECT * FROM  " . $newdatabaseName . ".properties", $newDatabaseLink) or die(mysql_error());

        while ($row = mysql_fetch_array($result)) {
            //insert in relusergroupproperties tbl
            mysql_query("INSERT INTO " . $newdatabaseName . ".relusergroupproperties
					 (usergroupid, propertyid, propertyvalue)

					VALUES

					(1, '" . $row['propertyid'] . "',0 )"
                            , $newDatabaseLink) or die(mysql_error());
        }
    } catch (Exception $e) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".relusergroupproperties");
    }
}

function billproperty($location, $newdatabaseName, $newUserName, $newPassword) {
    $con = mysql_connect("localhost", $newUserName, $newPassword);
    mysql_select_db($newdatabaseName, $con);

    $charset = mysql_client_encoding($con);
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    mysql_query("INSERT INTO " . $newdatabaseName . ".`billproperty` VALUES (1, 'Ù…Ø±Ø¯ÙˆØ¯Ø§Øª', 'ÙŠÙˆØ¬Ø¯ Ù…Ø±Ø¯ÙˆØ¯Ø§Øª', 0)");

    //load file
    $commands = file_get_contents($location);

    //delete comments
    $lines = explode("\n", $commands);
    $commands = '';
    foreach ($lines as $line) {
        $line = trim($line);
        if ($line && !strpos($line, '--')) {
            $commands .= $line . "\n";
        }
    }

    //convert to array
    $commands = explode(";", $commands);

    //run commands
    $total = $success = 0;
    foreach ($commands as $command) {
        if (trim($command)) {
            $success += (@mysql_query($command) == false ? 0 : 1);
            $total += 1;
        }
    }

    //return number of successful queries and total number of queries found
    return array(
        "success" => $success,
        "total" => $total
    );
}

function billname($newdatabaseName, $newUserName, $newPassword) {
    $newDatabaseLink = mysql_connect('localhost', $newUserName, $newPassword, $newdatabaseName) or die(mysql_error());




    try {
        //insert in billname tbl for buybill

        mysql_query("INSERT INTO " . $newdatabaseName . ".billname

				 (billnameid, name, userid, conditions, billnamedate, billtype)

				VALUES

				(1, 'فاتورة مشتريات', 1, 0,  '" . date("Y-m-d") . "', 1)"
                        , $newDatabaseLink) or die(mysql_error());

        billsettings($newdatabaseName, $newUserName, $newPassword, 1);


        //insert in billname tbl for returnbuybill

        mysql_query("INSERT INTO " . $newdatabaseName . ".billname

				 (billnameid, name, userid, conditions, billnamedate, billtype)

				VALUES

				(2, 'فاتورة مردودات مشتريات', 1, 0,  '" . date("Y-m-d") . "', 1)"
                        , $newDatabaseLink) or die(mysql_error());
        billsettings($newdatabaseName, $newUserName, $newPassword, 2);


        //insert in billname tbl for sellbill

        mysql_query("INSERT INTO " . $newdatabaseName . ".billname

				 (billnameid, name, userid, conditions, billnamedate, billtype)

				VALUES

				(3, 'فاتورة  مبيعات', 1, 0,  '" . date("Y-m-d") . "', 0)"
                        , $newDatabaseLink) or die(mysql_error());
        billsettings($newdatabaseName, $newUserName, $newPassword, 3);


        //insert in billname tbl for returnsellbill

        mysql_query("INSERT INTO " . $newdatabaseName . ".billname

				 (billnameid, name, userid, conditions, billnamedate, billtype)

				VALUES

				(4, 'فاتورة مردودات مبيعات', 1, 0,  '" . date("Y-m-d") . "', 0)"
                        , $newDatabaseLink) or die(mysql_error());
        billsettings($newdatabaseName, $newUserName, $newPassword, 4);
    } catch (Exception $e) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".billname");
    }
}

function billsettings($newdatabaseName, $newUserName, $newPassword, $billnameid) {
    $newDatabaseLink = mysql_connect('localhost', $newUserName, $newPassword, $newdatabaseName) or die(mysql_error());


    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');


    try {
        $result = mysql_query("SELECT * FROM  " . $newdatabaseName . ".billproperty", $newDatabaseLink) or die(mysql_error());

        while ($row = mysql_fetch_array($result)) {
            if (($billnameid == 1 || $billnameid == 3) && $row['billpropertyid'] == 1) {
                $billsettingsvalue = 1;
            } else {
                $billsettingsvalue = 0;
            }


            //insert in billsettings tbl
            mysql_query("INSERT INTO " . $newdatabaseName . ".billsettings
		(billnameid, billpropertyid, billsettingsvalue)

		VALUES

		(" . $billnameid . ", '" . $row['billpropertyid'] . "'," . $billsettingsvalue . " )"
                            , $newDatabaseLink) or die(mysql_error());
        }
    } catch (Exception $e) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".billsettings");
    }
}

function menuurl($location, $newdatabaseName, $newUserName, $newPassword) {
    $con = mysql_connect("localhost", $newUserName, $newPassword);
    mysql_select_db($newdatabaseName, $con);

    $charset = mysql_client_encoding($con);
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    mysql_query("INSERT INTO `menuurl` VALUES (1, 'ÙØ§ØªÙˆØ± Ù…Ø¨ÙŠØ¹Ø§Øª', 'http://192.168.1.10/newstorex/controllers/sellbillController.php?do=addsellBill&billnameid=3', '926df9d40ae168abae0bd3e0202779d4.png', '1')");

    //load file
    $commands = file_get_contents($location);

    //delete comments
    $lines = explode("\n", $commands);
    $commands = '';
    foreach ($lines as $line) {
        $line = trim($line);
        if ($line && !strpos($line, '--')) {
            $commands .= $line . "\n";
        }
    }

    //convert to array
    $commands = explode(";", $commands);

    //run commands
    $total = $success = 0;
    foreach ($commands as $command) {
        if (trim($command)) {
            $success += (@mysql_query($command) == false ? 0 : 1);
            $total += 1;
        }
    }

    //return number of successful queries and total number of queries found
    return array(
        "success" => $success,
        "total" => $total
    );
}

function unit($newdatabaseName, $olddatabaseName, $newDatabaseLink, $oldDatabaseLink) {
    $newDatabaseLink = mysql_connect('localhost', $newUserName, $newPassword, $newdatabaseName) or die(mysql_error());



    try {

        //insert in billsettings tbl
        mysql_query("INSERT INTO " . $newdatabaseName . ".unit
			(unitId, unitName, unitSymbol, unitDescription, unitDate, conditions, userId)

			VALUES

			(1, 'قطعة', 'e', 'single unit', '" . date("Y-m-d") . "', 0, 1)"
                        , $newDatabaseLink) or die(mysql_error());
    } catch (Exception $e) {
        mysql_query("TRUNCATE TABLE " . $newdatabaseName . ".unit");
    }
}

function getTableDataRow($databaseN, $databaseL, $tableName) {

    $result = mysql_query("SELECT * FROM  " . $databaseN . "." . $tableName . "", $databaseL) or die(mysql_error());

    $datacount = mysql_num_rows($result);

    return $datacount;
}

?>