<?php

session_start();
ob_start();

//the global file operation
//include("../public/impOpreation.php");
//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");


//here the db files that include in the file
// GOES HERE ....................
include("../public/include_dao.php");



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




//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
//Savedaily

$mySavedailyRecord = new SavedailyMySqlDAO();



//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("transferdatabaseviewnora/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("transferdatabaseviewnora/showtable.html");
} elseif ($do == "inserttabledata") {

    //here the permssion check
    //include_once("../public/authentication.php");
    //to use the variable out side the funcion
    inserttabledata();

    $smarty->display("transferdatabaseviewnora/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("transferdatabaseviewnora/done.html");
//		   $url = "transferdatabaseController.php?do=add";
//
//			$smarty->assign('urldirect', $url);
//			 $note ="تمت العملية بنجاح";
//
//			$smarty->assign('msgnote', $note);
//			$smarty->display("notes2.html");
    }
    //catch(Exception $e)
    {
        //$smarty->display("transferdatabaseviewnora/done.html");
//			 $url = "transferdatabaseController.php?do=add";
//
//			$smarty->assign('urldirect', $url);
//			 $note ="حدث خطأ اثناء اجراء العملية";
//
//			$smarty->assign('msgnote', $note);
//			$smarty->display("notes2.html");
    }
}






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


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

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

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

    print_r("");
    $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());

        //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());
        }
    } 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] == "**agz_tbl") {
            $smarty->assign("tableArabicName" . $k, "يومية الخزينة");

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".savedaily");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".savedaily");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

            $k++;
        } elseif ($row[0] == "**backup_info_tbl") {
            $smarty->assign("tableArabicName" . $k, "الاصول");

            $result = mysql_query("SELECT * FROM  " . $newdatabaseName . ".savedaily");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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


            $result = mysql_query("SELECT * FROM  " . $newdatabaseName . ".returnbuybill");

            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".returnsellbill");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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


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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".bankaccount");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

            $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 . ".returnbuybill");
            $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, "المشتريات");

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".buybill");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".productcat");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".storemovement");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".supplierdebtchange");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".client");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".clientdebtchange");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

            $k++;
        } elseif ($row[0] == "**discount_product_tmp_tbl") {
            $smarty->assign("tableArabicName" . $k, "مديونية العملاء");

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".returnbuybill");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".returnbuybill");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".expenses");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".expensestype");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".kempiala");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".partner");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);


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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".returnbuybill");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".productsetting");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".returnbuybill");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".product");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".returnbuybill");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".returnbuybill");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".save");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".savedaily");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".sellbill");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".storereport");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

            $k++;
        }



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

          $result = mysql_query("SELECT * FROM ". $newdatabaseName.".storedetail");
          $datacount= mysql_num_rows($result);
          $smarty->assign("tableDataCount".$k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".store");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".storemovement");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".returnbuybill");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".supplierdebtchange");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".supplier");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

            $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, "المستخدمين");

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".user");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

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

            $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".partnerwithdrawal");
            $datacount = mysql_num_rows($result);
            $smarty->assign("tableDataCount" . $k, $datacount);

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

            $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'];

    $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() {


    $tableName = $_POST['tablename'];

    print_r($tableName);

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

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


    //new Database
    try {

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

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


    if ($tableName == "**agz_tbl") {

    } elseif ($tableName == "**backups_tbl") {

    } elseif ($tableName == "**backup_info_tbl") {

    } elseif ($tableName == "back_buy_bill_tbl") {

        returnBuyBill();
    } elseif ($tableName == "back_sell_bill_tbl") {
        returnSellBill();
    } elseif ($tableName == "bank_account_tbl") {
        $smarty->assign("tableArabicName" . $k, "حسابات البنوك");

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".bankaccount");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

        $smarty->assign("tableName" . $k, $row[0]);
    } elseif ($tableName == "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]);
    } elseif ($tableName == "**bank_sa7b_tbl") {
        $smarty->assign("tableArabicName" . $k, "خصائص الفواتير");

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".returnbuybill");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

        $smarty->assign("tableName" . $k, $row[0]);
    } elseif ($tableName == "buy_bill_tbl") {
        $smarty->assign("tableArabicName" . $k, "المشتريات");

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".buybill");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".productcat");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".storemovement");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".supplierdebtchange");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

          $result = mysql_query("SELECT * FROM ". $newdatabaseName.".client");
          $datacount= mysql_num_rows($result);
          $smarty->assign("tableDataCount".$k, $datacount);

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

    //**************************nora********************************
    elseif ($tableName == "client_report_tbl") {

        clientdebtchange($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    } 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 == "product_setting_tbl") {

        productsetting($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    } elseif ($tableName == "**product_supplier_tbl") {

    }

    /*     * ******************sara******************** */ elseif ($tableName == "product_tbl") {
        $smarty->assign("tableArabicName" . $k, "المنتجات");

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".product");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".returnbuybill");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".returnbuybill");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".save");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".savedaily");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

    /*     * ***************** nora ************************ */ elseif ($tableName == "sell_bill_tbl") {
        sellbill($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    } elseif ($tableName == "store_details") {

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



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

      $result = mysql_query("SELECT * FROM ". $newdatabaseName.".storedetail");
      $datacount= mysql_num_rows($result);
      $smarty->assign("tableDataCount".$k, $datacount);

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


      } */ elseif ($tableName == "store_tbl") {
        /* $smarty->assign("tableArabicName".$k, "المخازن");

          $result = mysql_query("SELECT * FROM ". $newdatabaseName.".store");
          $datacount= mysql_num_rows($result);
          $smarty->assign("tableDataCount".$k, $datacount);

          $smarty->assign("tableName".$k, $row[0]); */
        store($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    } elseif ($tableName == "store_transfer_tbl") {
        storemovement($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);
    }


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

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".supplierdebtchange");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".supplier");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

        $smarty->assign("tableName" . $k, $row[0]);
    } elseif ($tableName == "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]);
    } elseif ($tableName == "users_tbl") {
        $smarty->assign("tableArabicName" . $k, "المستخدمين");

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".user");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

        $result = mysql_query("SELECT * FROM " . $newdatabaseName . ".partnerwithdrawal");
        $datacount = mysql_num_rows($result);
        $smarty->assign("tableDataCount" . $k, $datacount);

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

function returnBuyBill() {

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

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


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


    //old Database
    $oldDatabaseLink = mysql_connect('localhost', $oldUserName, $oldPassword, $olddatabaseName) or die(mysql_error());


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


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

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

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


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

				VALUES

				('" . $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'] . "',  0,  " . $returnbuybilldirectpayment . ")"
                        , $newDatabaseLink) or die(mysql_error());
    }
}

function returnSellBill() {

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

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


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


    //old Database
    $oldDatabaseLink = mysql_connect('localhost', $oldUserName, $oldPassword, $olddatabaseName) or die(mysql_error());


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


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

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

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


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

				VALUES

				('" . $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'] . "',  0,  " . $returnsellbildirectpayment . ", " . $row['seller'] . ", '')"
                        , $newDatabaseLink) or die(mysql_error());
    }
}

function bankaccount() {

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

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


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


    //old Database
    $oldDatabaseLink = mysql_connect('localhost', $oldUserName, $oldPassword, $olddatabaseName) or die(mysql_error());


    $result = mysql_query("SELECT * FROM  " . $olddatabaseName . ".bank_account_tbl", $oldDatabaseLink);


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


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


				 (bankname, bankdetails, userid, conditions, bankdate)

				VALUES

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



        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'] . "', '', , 1, '" . $row['type_deleted'] . "')"
                        , $newDatabaseLink) or die(mysql_error());
    }
}

// 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', '1')"
                    , $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;
        }
    }
    sellbilldetail($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);

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

    $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'] . "',
				 '1',
				 '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", "اعدادات المنتج داخل المخزن");
}

/* old */

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)) {


        $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)) {


        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)) {


        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', '1')"
                    , $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)) {


        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)) {


        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)) {


        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;
        }
    }
    sellbilldetail($oldDatabaseLink, $olddatabaseName, $newDatabaseLink, $newdatabaseName);

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

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

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)) {


        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)) {
        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)) {



        $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)) {


            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)) {
        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", "اعدادات المنتج داخل المخزن");
}

?>