<?

/* ************************************************************************************* */
// Main

// include libraries.......................................................................

require_once('../includes.php');
require_once('../models/tradeModel.php');

$mytradeModel=new tradeModel;


$textfile = "firuz.txt";
$rcount = 0;
$mcount = 0;


// read file into array....................................................................

$trades = file($textfile);		// read into array (line by line)


// go through array........................................................................
// 1st pass

echo "First Pass..<br>";

foreach ($trades as $mytrade) {
  //echo $mytrade."<br>";
  $rcount++;
  $data[$rcount] = explode(",\"", $mytrade);
  $data[$rcount][1]=str_replace("'","\'",$data[$rcount][1]);
  $data[$rcount][1]=str_replace('""',"~",$data[$rcount][1]);
  $data[$rcount][1]=str_replace('"',"",$data[$rcount][1]);
  $data[$rcount][1]=str_replace("~",'\"',$data[$rcount][1]);
  $data[$rcount][2]=str_replace("\"","",$data[$rcount][2]);
  $data[$rcount][3]=str_replace("\"","",$data[$rcount][3]);
  $data[$rcount][4]=str_replace("\"","",$data[$rcount][4]);
  $data[$rcount][5]=str_replace("\"","",$data[$rcount][5]);
  $data[$rcount][6]=str_replace("\"","",$data[$rcount][6]);
  $data[$rcount][7]=str_replace("\"","",$data[$rcount][7]);
  $data[$rcount][8]=str_replace("\"","",$data[$rcount][8]);
  $data[$rcount][9]=str_replace("\"","",$data[$rcount][9]);
  $data[$rcount][10]=str_replace("'","\'",$data[$rcount][10]);
  $data[$rcount][10]=str_replace('""',"~",$data[$rcount][10]);
  $data[$rcount][10]=str_replace('"',"",$data[$rcount][10]);
  $data[$rcount][10]=str_replace("~",'\"',$data[$rcount][10]);
  $data[$rcount][11]=str_replace("\"","",$data[$rcount][11]);
  $data[$rcount][12]=str_replace("\"","",$data[$rcount][12]);
  $data[$rcount][13]=str_replace("\"","",$data[$rcount][13]);
  $data[$rcount][14]=str_replace("\"","",$data[$rcount][14]);
  if ($data[$rcount][2]=="Active") { $status=1; } else { $status=0; }
  //echo $rcount.") Level: ".$data[$rcount][4]." - Desc: ".$data[$rcount][1]." - (Status: ".$status.")";
  if ($data[$rcount][4]=="first") {
    echo $rcount.") Level: ".$data[$rcount][4]." - Desc: ".$data[$rcount][1]." - (Status: ".$status.")";
    echo " (MATCH!)";
    $res = inserttrade("", "", 1, $data[$rcount][10], $data[$rcount][1], $data[$rcount][3], $data[$rcount][9], 0, "", 0, $status);
    if ($res==true) $mcount++;
    echo "<br>";
  }
  //echo "<br>";
}
//reset($trades);


// Closing
echo "<br>";
echo $rcount." records examined!<br>";
echo $mcount." records inserted!<br>";


// copy data..

$master = $data;


// go through array........................................................................
// 2nd pass

$rcount = 0;
$mcount = 0;

echo "<br><br>";
echo "Second Pass..<br>";

foreach ($data as $mydata) {
  $rcount++;
  if ($mydata[2]=="Active") { $status=1; } else { $status=0; }
  if ($mydata[4]=="second") {
    echo $rcount.") Level: ".$mydata[4]." - Desc: ".$mydata[1]." - (Status: ".$status.")";
    echo " (MATCH!)";
    $parent = $mytradeModel->getdata_pkeybycode($mydata[5]);
    $res = inserttrade("", $parent, 2, $mydata[10], $mydata[1], $mydata[3], $mydata[9], 0, "", 0, $status);
    if ($res==true) $mcount++;
    echo "<br>";
  }
}
reset($data);


// Closing
echo "<br>";
echo $rcount." records examined!<br>";
echo $mcount." records inserted!<br>";


// go through array........................................................................
// 3rd pass

$rcount = 0;
$mcount = 0;

echo "<br><br>";
echo "Third Pass..<br>";

foreach ($data as $mydata) {
  $rcount++;
  if ($mydata[2]=="Active") { $status=1; } else { $status=0; }
  if ($mydata[4]=="third") {
    echo $rcount.") Level: ".$mydata[4]." - Desc: ".$mydata[1]." - (Status: ".$status.")";
    echo " (MATCH!)";
    $parent = $mytradeModel->getdata_pkeybycode($mydata[6]);
    $res = inserttrade("", $parent, 3, $mydata[10], $mydata[1], $mydata[3], $mydata[9], 0, "", 0, $status);
    if ($res==true) $mcount++;
    echo "<br>";
  }
}
reset($data);


// Closing
echo "<br>";
echo $rcount." records examined!<br>";
echo $mcount." records inserted!<br>";


// go through array........................................................................
// 4th pass

$rcount = 0;
$mcount = 0;

echo "<br><br>";
echo "Fourth Pass..<br>";

foreach ($data as $mydata) {
  $rcount++;
  if ($mydata[2]=="Active") { $status=1; } else { $status=0; }
  if ($mydata[4]=="fourth") {
    echo $rcount.") Level: ".$mydata[4]." - Desc: ".$mydata[1]." - (Status: ".$status.")";
    echo " (MATCH!)";
    $parent = $mytradeModel->getdata_pkeybycode($mydata[7]);
    $res = inserttrade("", $parent, 4, $mydata[10], $mydata[1], $mydata[3], $mydata[9], 0, "", 0, $status);
    if ($res==true) $mcount++;
    echo "<br>";
  }
}
reset($data);


// Closing
echo "<br>";
echo $rcount." records examined!<br>";
echo $mcount." records inserted!<br>";


// go through array........................................................................
// 5th pass

$rcount = 0;
$mcount = 0;

echo "<br><br>";
echo "Fifth Pass..<br>";

foreach ($data as $mydata) {
  $rcount++;
  if ($mydata[2]=="Active") { $status=1; } else { $status=0; }
  if ($mydata[4]=="fifth") {
    echo $rcount.") Level: ".$mydata[4]." - Desc: ".$mydata[1]." - (Status: ".$status.")";
    echo " (MATCH!)";
    $parent = $mytradeModel->getdata_pkeybycode($mydata[8]);
    $res = inserttrade("", $parent, 5, $mydata[10], $mydata[1], $mydata[3], $mydata[9], 0, "", 0, $status);
    if ($res==true) $mcount++;
    echo "<br>";
  }
}
reset($data);


// Closing
echo "<br>";
echo $rcount." records examined!<br>";
echo $mcount." records inserted!<br>";


/* ************************************************************************************* */
// Functions

// insert page records into database


function inserttrade($p_tradepkey, $p_tradeparentpkey, $p_tradelevel, $p_tradecode, $p_tradedescription, $p_tradetype, $p_tradeunit, $p_tradesubcontype, $p_traderemarks, $p_tradereceivedsetting, $p_tradestatus) {
  global $mytradeModel;
  $opresult=$mytradeModel->insertdata_trade($p_tradepkey, $p_tradeparentpkey, $p_tradelevel, $p_tradecode, $p_tradedescription, $p_tradetype, $p_tradeunit, $p_tradesubcontype, $p_traderemarks, $p_tradereceivedsetting, $p_tradestatus);
  if ($opresult!=false) {
    return true;
  } else {
    return false;
  }
}


?>
