<?php
include('../headertop.php');
include('../db.php');

$disp = '';
$pagetitle = 'Add New Expense';

date_default_timezone_set('Asia/Kolkata');

if(!isset($_POST['bulkexpensesubmit']))
{
echo '<meta http-equiv="refresh" content="0;URL=bulkexpense.php">';
exit;
}

$project = $_POST['project'];
$csvfile = $_FILES['csvfile']['tmp_name'];

$file = fopen($csvfile, 'r');
$correct = array();
while (($line = fgetcsv($file)) !== FALSE) {
  //$line is an array of the csv elements
  $wt = $line[4];
  $q = "select * from work_type where work_type like '$wt%'";
  $res = mysqli_query($dbcon,$q);
  //if($row['work_type'].""==$line[4])
  if(mysqli_num_rows($res)>=1)
  {
  $row = mysqli_fetch_array($res);
  $datee = date("Y-m-d", strtotime($line[1]));
  $particulars = addslashes($line[2]);
  $amount = str_replace(',','',$line[3]);
  
$dateref = date("dmY", strtotime($line[1]));
$qp = "select project_name, substring(project_name,1,20) as proj from project where id='$project'";
$rp = mysqli_query($dbcon,$qp) or die(mysqli_error($dbcon));
$rwp = mysqli_fetch_array($rp);
$rwf = substr($row['work_type'],0,19);
$reference = $rwp['proj'].$dateref.$rwf;
  
$qins[] = "insert into expenses(date,work_type,particulars,amount,reference,project)  values('$datee','".$row['id']."','$particulars','$amount','$reference','$project')";
   //print_r($line);
  //echo "<br>";
  }
  else
  {
  if($line[1]!='Date' and $line[3]!='Amount')
  {
  $datec = date("Y-m-d", strtotime($line[1]));
  $particulars = addslashes($line[2]);
  $amount = str_replace(',','',$line[3]);
  
  $qins2[] = "insert into expenses(date,work_type,particulars,amount,project)  values('$datec','To be corrected','$particulars','$amount','$project')";
 //$correct[] = array('date'=>$datec, 'particulars'=>$line[2],'amount'=>$line[3],'work_type'=>$row['work_type']);
  }//end of if
  //echo 'DB: '.$row['work_type'];
  //echo ' CSV: '.$line[4]."<br>";
  }//end of else
}
fclose($file);

mysqli_autocommit($dbcon, false);

foreach($qins as $query)
{
mysqli_query($dbcon,$query) or die(mysqli_error($dbcon));
}

foreach($qins2 as $query2)
{
mysqli_query($dbcon,$query2) or die(mysqli_error($dbcon));
}

mysqli_commit($dbcon);

$sm = "Expenses information saved ";
echo '<meta http-equiv="refresh" content="0;URL=bulkexpense.php?sm='.$sm.'">';


/*

$q2 = "select * from work_type";
$res2 = mysqli_query($dbcon,$q2);
while($row2 = mysqli_fetch_array($res2))
{
$wtarray[$row2['id']] = $row2['work_type'];
}

echo '<br> Pls correct Work Type for these following ('.count($wtarray).') Entries:<br>
<form method="post" action="readexpenses2.php"><table style="width:70%">
<tr><th>Date</th><th>Particulars</th><th>Amount</th><th>Work Type</th></tr>';
foreach($correct as $k=>$v)
{
$nd = 'correct2['.$k.'][date]';
$nw = 'correct2['.$k.'][work_type]';
$np = 'correct2['.$k.'][particulars]';
$na = 'correct2['.$k.'][amount]';
echo '<tr><td><input type="text" name="'.$nd.'" value="'.$correct[$k]['date'].'"></td>
<td><input type="text" name="'.$np.'" value="'.$correct[$k]['particulars'].'"></td>
<td><input type="text" name="'.$na.'" value="'.$correct[$k]['amount'].'"></td>
<Td><select name="correct2[$k][work_type]"><option value=""></option>';

foreach($wtarray as $i=>$w)
{
echo '<option value="'.$i.'">'.$w.'</option>';
}
echo '</select></td></tr>';
reset($wtarray);
}//end of foreach for $correct
echo '<tr><td colspan="4"><input type="submit" value="Submit corrections"></td></tr>';
echo '</table></form>';
*/

