<?session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.php'; </script>";
 return;
}
include("../opendb.php");

$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
if(isset($_POST['upd']))
{
    $ff=$_REQUEST['ff'];
    if($ff !="none")
    {
        $fl=$_FILES['ff']['name'];
        $file_dir=".";
        $tmp_name=$_FILES['ff']['tmp_name'];
        move_uploaded_file($tmp_name,"txtfiles/".$fl) or die(' err:could not copy file'.$fl);
        //print "File copied :".$fl;
    }
    $fp=fopen("txtfiles/".$fl,"r") or die("Cant open file");
    $ctr=1;
    $j=1;
    $invno_arr[]='';
    $invno_prv='';
    /*while(!feof($fp))
    {
        
        
     $line=fgets($fp,1024);
     
     $spline=explode("\t",$line);
    if($j==1)
    {
      $no_col=sizeof($spline);
      for($x=0;$x<=$no_col;$x++)
      { 
           if($spline[$x]=='Invoice No.')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline['$invno_no1']);
    
    if($invno1!=$invno_prv)
    {
        $query="select invno from dispatchinvoice where invno='$invno1'";
        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
            $invno_arr[$ctr++]=$invno1;
        }
    }
        $invno_prv=$invno1;
        
    }$j++;
  
        
        
        
        
    }*/
    
   
    print "<table>";
    $fp=fopen("txtfiles/".$fl,"r") or die("Cant open file");
    $i=1;
    $i=1;
      $invno_prv1='';
    while(!feof($fp))
    {
        
        
        
        
        
    $line=fgets($fp,1024);
    $spline=explode("\t",$line);
    if($i==1)
    {
        $no_col=sizeof($spline);
        for($x=0;$x<=$no_col+1;$x++)
        {
          
            if($spline[$x]=='Invoice No.')
            {
                $invno_no = $x;
            }
            if($spline[$x]=='Invoice Date')
            {
                $invdate_no = $x;
            }
            if(trim($spline[$x])=='Customer')
            {
                $customer_no = $x;
            }
            if(trim($spline[$x])=='Location')
            {
                $location_no = $x;
            }
            if(trim($spline[$x])=='Executive')
            {
                $executive_no = $x;
            }
            if(trim($spline[$x])=='Inv. Amount')
            {
                $invamt_no = $x;
            }
            if($spline[$x]=='Dispatch Date')
            {
                $dispdate_no = $x;
            }
            if($spline[$x]=='Box')
            {
                $box_no = $x;
            }
            if($spline[$x]=='Prim.')
            {
                $prim_no = $x;
            }
            if($spline[$x]=='Prim. Transporter')
            {
                $transport_no = $x;
            }
            if($spline[$x]=='Sec. Transporter')
            {
                $sectransport_no = $x;
            }
            if($spline[$x]=='Vehicle No.')
            {
                $vehicle_no = $x;
            }
            if($spline[$x]=='LR No.')
            {
                $lr_no = $x;
            }
            if($spline[$x]=='LR Date')
            {
                $lrdate_no = $x;
            }
            if($spline[$x]=='LR Amount')
            {
                $lramt_no = $x;
            }
        }
    }
    if($i>1)
    {
        
        $invno = trim($spline[$invno_no]);
        
        $invdate = trim($spline[$invdate_no]);
        $invdate = str_replace('/', '-', $invdate);
        $invdate = implode('-', array_reverse(explode('-', $invdate)));
        $customer = trim($spline[$customer_no]);
        $location = trim($spline[$location_no]);
        $executive = trim($spline[$executive_no]);
        $dispdate = str_replace('/', '-', trim($spline[$dispdate_no]));
        $dispdate = implode('-', array_reverse(explode('-', $dispdate)));
        
        $invamt = trim($spline[$invamt_no]);
        
        
        $box = trim($spline[$box_no]);
        $prim = trim($spline[$prim_no]);
        $transport = trim($spline[$transport_no]);
        $sectransport = trim($spline[$sectransport_no]);
        $vehicle = trim($spline[$vehicle_no]);
        $lr = trim($spline[$lr_no]);
        $lrdate = str_replace('/', '-', trim($spline[$lrdate_no]));
        $lrdate = implode('-', array_reverse(explode('-', $lrdate)));
        
        $lramt = trim($spline[$lramt_no]);
        
             //$invdate = date("Y-m-d", strtotime($invdate));
         
        
        
        // $invoicevalue = trim($invoicevalue,'"');
          //$invoicevalue = str_replace(',', '', $invoicevalue);
       
          // $suplplantname=mysqli_escape_string($suplplantname);
          
         
      
        $k=$i-1;
        //print $grno;
        if(strlen($invno)>0)
        {
            if($invno!='')
            {
                //if(array_search($invno,$invno_arr)>0)
                //{
                $query=mysqli_query($db,"select invno from dispatchinvoice where invno='$invno'");
                $row=mysqli_fetch_row($query);
                if($row[0]>0)
                {
                    $query21=mysqli_query($db,"select lrno from dispatchinvoice where invno='$invno'");
                    $row21=mysqli_fetch_row($query21);
                    if(strlen($row21[0])>0)
                    {
                        print " <tr><td><font color='red'>$invno</td><td><font color='red'>Already updated.....(No Changes made)</td></tr>";
                    }
                    else
                    {
                        print " <tr><td>$invno</td><td>Dispatch Details already updated;";
                        if(strlen($lr)>0)
                        {
                            $sql1="update dispatchinvoice set lrno='$lr',lrdate='$lrdate',lramount='$lramt',trans_name2='$sectransport' where invno='$invno'";    
                            mysqli_query($db,$sql1) or die('Insert Err:'.mysqli_error());
                            print " Now LR details also updated..</td></tr>";
                        }
                        else
                        {
                            print " No LR Details found.....</td></tr>";
                        }
                        //print $i.". ".$sql1."<br>";
                    }
                }
                else
                {
                    $query11=mysqli_query($db,"select invdate,customer,sum(tot),engineer from spareinvoice where invno='$invno' and brcode= '$brcode' group by invno");
                    $row11=mysqli_fetch_row($query11);
                    if(strlen($row11[0])>0)
                    {
                        if(strlen($lramt)<=0)
                            $lramt=0;
                        $sql="insert into dispatchinvoice (invno,invdate,customer,tot,brcode,engineer,disp_date,box,trans_name,veh_no,lrno,lrdate,lramount,trans_name2,veh_no2)  
        values('$invno','$row11[0]','$row11[1]','$row11[2]','$brcode','$row11[3]','$dispdate','$box','$transport','$vehicle','$lr','$lrdate','$lramt','$sectransport','')";
                        print $i.". ".$sql."<br>";
                        //print "<font color='blue'>Successfuly Inserted.....!!!--".$invno."<br></font>";
                        mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error());
                        //$cname="";
                        print " <tr><td>$invno</td><td>Dispatch Details Updated Successfully....";
                        if(strlen($lr)>0)
                        {
                            print "; LR Details also updated..</td></tr>";
                        }
                        else
                        {
                            print "</td></tr>";
                        }
                    }
                    else
                    {
                        print " <tr><td>$invno</td><td>Invalid Invoice number</td></tr>";
                    }
                }
            }
        }
    }
    $invpr=$invno;
    $i++;

        
        
        
        
        
        
        
        
        
    }
    $path="txtfiles/".$fl;
	//echo $path;
    if(unlink($path))
	{
		echo "File Deleted"; 
	}
    print "<script> alert('Done');</script>";
}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
//etr{background-color:c1d799}
.mnu{margin-left:5}
</style>
<center>
<br><br><br><br><br><br>
<form action='<?print $PHP_SELF?>' method='post' ENCTYPE="multipart/form-data">
<table border=0 width=60% cellspacing=1 cellpadding=1 bgcolor='darkbrown'>
<tr><td align=center bgcolor=white>
<table >
<th colspan=2 bgColor=c1d799>Upload Dispatch Data
<tr><td>File </td><td><input type=file name='ff'></td></tr>
<tr bgColor='lightsteelblue'><td align=center colspan=2 ><input type=submit value='Upload' name='upd'></td></tr>
</table>
</td></tr>
</table>
</form>
<font color='blue'><b>Please use the below format for file uploading</b></font>

<table border=1 width=60% cellspacing=1 cellpadding=0 bgcolor=''>
<tr><th>Branch</th><th>Invoice No.</th><th>Invoice Date</th><th>Customer</th><th bgcolor='yellow'>Dispatch Date</th><th bgcolor='yellow'>Box</th><th bgcolor='yellow'>Prim. Transporter</th><th bgcolor='yellow'>Vehicle No.</th><th bgcolor='lightgreen'>Sec. Transporter</th><th bgcolor='lightgreen'>LR No.</th><th bgcolor='lightgreen'>LR Date</th><th bgcolor='lightgreen'>LR Amount</th><th>Dummy</th>
</tr>
<tr><td>BLR001</td><td>XXXXXX</td><td>XX-XX-XXXX</td><td>XXXXXXXX</td><td bgcolor='yellow'>XX-XX-XXXX</td><td bgcolor='yellow'>X</td><td bgcolor='yellow'>XXXXX</td><td bgcolor='yellow'>XXXXX</td><td bgcolor='lightgreen'>XXXXXX</td><td bgcolor='lightgreen'>XXXXX</td><td bgcolor='lightgreen'>XX-XX-XXXX</td><td bgcolor='lightgreen'>XXX</td><td></td>
</tr>

</table >
