<?php

include("../public/include_dao.php");

//print_r('jjjjjjjjjjjjjjjjjjjjjjjjjj'.$_SESSION['dbname']);
//Productcat
require_once('../models/dao/ProductcatDAO.class.php');
require_once('../models/dto/Productcat.class.php');
require_once('../models/mysql/ProductcatMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductcatMySqlExtDAO.class.php');
//Product
require_once('../models/dao/ProductDAO.class.php');
require_once('../models/dto/Product.class.php');
require_once('../models/mysql/ProductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductMySqlExtDAO.class.php');
//Storedetail
require_once('../models/dao/StoredetailDAO.class.php');
require_once('../models/dto/Storedetail.class.php');
require_once('../models/mysql/StoredetailMySqlDAO.class.php');
require_once('../models/mysql/ext/StoredetailMySqlExtDAO.class.php');




//Productcat
$Productcat = new Productcat();
$ProductcatDAO = new ProductcatMySqlDAO();
$ProductcatEX = new ProductcatMySqlExtDAO();


//Product
$Product = new Product();
$ProductEX = new ProductMySqlExtDAO();
$ProductDAO = new ProductMySqlDAO();


//Storedetail

$Storedetail = new Storedetail();
$StoredetailDAO = new StoredetailMySqlDAO();
$StoredetailEX = new StoredetailMySqlExtDAO();
//Current Database
$currentDatabaselink = mysql_connect('localhost', 'root', '') or die(mysql_error()); // connect to database


mysql_select_db('kity', $currentDatabaselink);






$createDatabaseSql = " select * from productcat ";
$result1 = mysql_query($createDatabaseSql) or die(mysql_error());
//create table in newdatabase
while ($row = mysql_fetch_row($result1)) {

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


    mysql_select_db('kity', $currentDatabaselink);
    //	print_r('catid='.$row[0].'<br>');

    $getproductcatSql = ' select * from product where productCatId= ' . $row[0];
    $productresult = mysql_query($getproductcatSql) or die(mysql_error());


    //print_r('sql ='.($getproductcatSql).'<br>');
    //print_r('productresult='.count($productresult).'<br>');

    $Productcat->productCatName = $row[1];
    $Productcat->productCatDate = $row[2];
    $Productcat->productCatDescription = $row[3];
    $Productcat->productCatParent = $row[4];
    $Productcat->userId = $row[5];
    $Productcat->conditions = $row[6];
    $Productcat->selldiscount = $row[7];
    $Productcat->buydiscount = $row[8];
    $Productcat->discounttype = $row[9];
    $Productcat->isOptic = 0;
    $Productcat->inMenu = 0;



    $catid = $ProductcatDAO->insert($Productcat);

    $i = 0;
    while ($row2 = mysql_fetch_row($productresult)) {


        $Product->productName = $row2[1];
        $Product->productDescription = $row2[2];
        $Product->productCatId = $catid;
        $Product->productBuyPrice = $row2[4];
        $Product->productSellAllPrice = $row2[5];
        $Product->productSellUnitPrice = $row2[6];
        $Product->productSellHalfPrice = $row2[7];
        $Product->productDate = $row2[8];
        $Product->conditions = $row2[9];
        $Product->userId = $row2[10];
        $Product->limitamount = $row2[11];
        $Product->parcode = $row2[12];
        $Product->type = 0;
        $Product->expireDate = 100;
        $Product->sellingtype = 1;
        $Product->dailyentryId = 0;
        $Product->isService = 0;
        $Product->isOptic = 0;



        if ($row2[1] != '') {
            /* print_r('<pre>');
              print_r($Product);
              print_r('<pre>'); */
            $i ++;
            $Productid = $ProductDAO->insert($Product);

            /*
              $Storedetail->productid=$Productid;
              $Storedetail->storeid=1;
              $Storedetail->productquantity=0;
              $Storedetail->userid=1;
              $Storedetail->storedetaildate=date("Y-m-d");
             */

            //print_r($Storedetail);
            //$Storedetaiid=$StoredetailDAO->insert($Storedetail);



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


            mysql_select_db('kity', $currentDatabaselink);
            //	print_r('catid='.$row[0].'<br>');

            $getproductdetalesSql = ' select * from storedetail where productid=' . $row2[0] . ' ';


            //print_r('<br>'.$getproductdetalesSql.'<br>');
            $productdetaleresult = mysql_query($getproductdetalesSql) or die(mysql_error());
            //print_r('productdetaleresult'.count($productdetaleresult).'<br>');
            while ($row3 = mysql_fetch_row($productdetaleresult)) {
                //print_r('storeid'.$row3['storeid'].'<br>');

                $Storedetail->productid = $Productid;
                $Storedetail->storeid = $row3[2];
                $Storedetail->productquantity = $row3[3];
                $Storedetail->userid = $row3[4];
                $Storedetail->storedetaildate = $row3[5];


                print_r($Storedetail);
                $Storedetaiid = $StoredetailDAO->insert($Storedetail);

                if ($Storedetaiid) {
                    //print_r('Productid='.$Productid.'===Storedetaiid='.$Storedetaiid.'<br>');
                    //    print_r('Storedetaiid='.$Storedetaiid.'<br>');
                } else {
                    //print_r('Productidxxxxxxxxxxx======='.$Productid.'<br>');
                }
            }
        }
    }

    //print_r('===============================================<br>');
}


echo "data  are  saved == " . $i;
//create Main tables
?>