<?php

include("db.php");

$year = $_POST['year'];

// slelect old database = = = == = 
    $dabas_file = fopen("database_name.txt","r+");
	$preivousDatabase = fgets($dabas_file);

 // $db->query("drop database $preivousDatabase");

  $db->query("create database exchangecurrency$year");
  //insert into  database name table =================
  $database="exchangecurrency$year";   
   $localhost='localhost';
   $user='root';
   $password='';
   $db=new mysqli($localhost,$user,$password,$database);
   $db->set_charset("utf8");
  
   //create table customer= 
   "CREATE TABLE $database.customers LIKE $preivousDatabase.customers";
$db->query("CREATE TABLE $database.customers LIKE $preivousDatabase.customers");
$db->query("INSERT INTO customers  SELECT * FROM $preivousDatabase.customers");
//end table customer = = == = =
//create assetopeningbalance  = 
//create customeropeningbalance  = 
$db->query("CREATE TABLE customeropeningbalance LIKE $preivousDatabase.customeropeningbalance");
//end table customeropeningbalance = = == = =

   //create customer balance  = 
$db->query("CREATE TABLE customerbalance LIKE $preivousDatabase.customerbalance");
$db->query("INSERT INTO customerbalance  SELECT * FROM $preivousDatabase.customerbalance");
//end table customer = = == = =

 $db->query("INSERT INTO customeropeningbalance (customerId,currencyId,amount,dollarAmount) SELECT customerId,currencyId,amount,dollarAmount from  $database.customerbalance ");
//create asset   = 
$db->query("CREATE TABLE assets LIKE $preivousDatabase.assets");
$db->query("INSERT INTO assets  SELECT * FROM $preivousDatabase.assets");
//end table asset = = == = =
$db->query("CREATE TABLE assetopeningbalance LIKE $preivousDatabase.assetopeningbalance");
$db->query("INSERT INTO assetopeningbalance (currencyUnit,amount,totalDollar) SELECT currencies_id,amount,dollarAmount from  $database.assets");

//end table assetopeningbalance = = == = =
//create admin  = 
$db->query("CREATE TABLE admin LIKE $preivousDatabase.admin");
$db->query("INSERT INTO admin  SELECT * FROM $preivousDatabase.admin");
//end table asset = = == = =

//create creatt  = 
$db->query("CREATE TABLE crrencyrate LIKE $preivousDatabase.crrencyrate");



//create banking  = 
$db->query("CREATE TABLE banking LIKE $preivousDatabase.banking");
//end table banking = = == = =

//create currencies  = 
$db->query("CREATE TABLE currencies LIKE $preivousDatabase.currencies");
$db->query("INSERT INTO currencies  SELECT * FROM $preivousDatabase.currencies");

//end table currencies = = == = =



//create customeropeningbalance  = 
$db->query("CREATE TABLE customerssell LIKE $preivousDatabase.customerssell");
//end table customeropeningbalance = = == = =

//create daily  = 
$db->query("CREATE TABLE daily LIKE $preivousDatabase.daily");
//end table customeropeningbalance = = == = =

//create daily  = 
$db->query("CREATE TABLE dailyasset LIKE $preivousDatabase.dailyasset");
//end table customeropeningbalance = = == = =

//create daily  = 
$db->query("CREATE TABLE dailyprofit LIKE $preivousDatabase.dailyprofit");
//end table customeropeningbalance = = == = =

//create daily  = daily_transactions
$db->query("CREATE TABLE daily_transactions LIKE $preivousDatabase.daily_transactions");
//end table customeropeningbalance = = == = =

//create daily  = daily_transactions
$db->query("CREATE TABLE estimate LIKE $preivousDatabase.estimate");
//end table customeropeningbalance = = == = =
//create daily  = daily_transactions
$db->query("CREATE TABLE expensesname LIKE $preivousDatabase.expensesname");
//end table customeropeningbalance = = == = =
//create daily  = daily_transactions
$db->query("CREATE TABLE incomename LIKE $preivousDatabase.incomename");
//end table customeropeningbalance = = == = =

//create daily  = daily_transactions
$db->query("CREATE TABLE journal LIKE $preivousDatabase.journal");
//end table customeropeningbalance = = == = =
//create daily  = daily_transactions
$db->query("CREATE TABLE others LIKE $preivousDatabase.others");
//end table customeropeningbalance = = == = =

//create daily  = daily_transactions
$db->query("CREATE TABLE transaction LIKE $preivousDatabase.transaction");
//end table customeropeningbalance = = == = =

  $db->query("insert into exchangecurrency1396.database_name(name,year) value('$database',$year)");
  
  //write to file====
  
  $file = fopen("database_name.txt","w+");
  $rite = fwrite($file,"exchangecurrency$year");
   fclose($rite);
   //write on report file
   $file1 = fopen("report/database_name.txt","w+");
  $rite1 = fwrite($file1,"exchangecurrency$year");
   fclose($rite1);
   
   //write on edit file
    $file2 = fopen("report/database_name.txt","w+");
  $rite2 = fwrite($file2,"exchangecurrency$year");
   fclose($rite2);
   
   //write on delete file
    $file3 = fopen("delete/database_name.txt","w+");
  $rite3 = fwrite($file3,"exchangecurrency$year");
   fclose($rite3);
   
   

  header("location:welcome.php?year=$year");

?>

