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

$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error($db));
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
if(isset($_POST['upd']))
{
    $ff=$_REQUEST['ff'];
  if($ff !="none")
  {
       $imagename=$_FILES['ff']['name'];
       $filepath= "";
       $tmp = $_FILES['ff']['tmp_name'];
       move_uploaded_file($tmp, $filepath . $imagename) or die(' err:could not copy file');
       // print "File copied :".$fl;
   }
    $filepathval=$filepath.$imagename;
     $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
  $ctr=1;
  $j=1;
  $invno_arr[]='';
  $invno_prv='';
  $invno_arr2[]='';
  $invno_prv2='';

    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]=='DocumentNumber')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline[$invno_no1]);
    
    if($invno1!=$invno_prv)
    {
        $query="select invno from sparepurreteich where invno='$invno1' and brcode='$brcode'";
        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
            $invno_arr[$ctr++]=$invno1;
        }
        
    }
        $invno_prv=$invno1;
        
    }$j++;
  }
  
  
  
  //-------------------------------------------------------------
  $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
  
  $i=1;
  while(!feof($fp))
  {
     $line=fgets($fp,1024);
     $spline=explode("\t",$line);
              if($i==1)
    {
      $no_col=sizeof($spline);
      for($x=0;$x<=$no_col;$x++)
      {
        
         if($spline[$x]=='DocumentNumber')
          $invno_no = $x;
         if($spline[$x]=='DocumentType')
          $invtype_no = $x;
         if($spline[$x]=='DocumentDate')
          $invdate_no = $x;
         if($spline[$x]=='SO No.')
          $so_no = $x;
          
          if($spline[$x]=='SupplyType')
          $suptype_no = $x;
          
            if($spline[$x]=='CustomerCode')
            $vendor_no = $x;
         if($spline[$x]=='CustomerName')
          $cname_no = $x;
         if($spline[$x]=='ProductCode')
          $itemno_no = $x;
         if($spline[$x]=='ProductDescription')
          $itemdesc_no = $x;
         if($spline[$x]=='Quantity')
          $qty_no = $x;
         if($spline[$x]=='List price Per Unit')
          $lstprice_no = $x;
         if($spline[$x]=='Spares Discount')
          $disc_no = $x;
         if($spline[$x]=='TaxableValue')
          $tottax_no = $x;
         if($spline[$x]=='Handling charges')
          $hnd_no = $x;
         if($spline[$x]=='IntegratedTaxRate')
          $tax_no = $x;
         if($spline[$x]=='IntegratedTaxAmount')
          $vat_no = $x;
          if(trim($spline[$x])=='Grand Total')
          $total_no = $x;
       
       
          if($spline[$x]=='Material Type')
          $itemtype_no = $x;
          if($spline[$x]=='PlantCode')
          $dlco_no = $x;
         if($spline[$x]=='Invoice cancelled')
          $invcan_no = $x;
          if($spline[$x]=='Other charges')
          $oth_no = $x;
          
          if($spline[$x]=='IntegratedTaxRate')
          $igst_rate_no = $x;
          //print $spline[$x]."--".substr($spline[$x],0,20).strlen($spline[$x])."<br>";
          if(substr($spline[$x],0,20)=='IntegratedTaxAmount')
          $igst_amount_no = $x;
          if($spline[$x]=='StateUTTaxRate')
          $sgst_rate_no = $x;
          if($spline[$x]=='StateUTTaxAmount')
          $sgst_amount_no = $x;
          if($spline[$x]=='CentralTaxRate')
          $cgst_rate_no = $x;
          if($spline[$x]=='CentralTaxAmount')
          $cgst_amount_no = $x;
          
               }
    }
    if($i>1)
    {
         
          $invno = $spline[$invno_no];
          $invtype = trim($spline[$invtype_no]);
          $invdate = trim($spline[$invdate_no]);
          $invdate = str_replace('/', '-', $invdate);
         // $invdate = implode('-', array_reverse(explode('-', $invdate)));
          $sono = trim($spline[$so_no]);
          $cname = trim($spline[$cname_no]);
          $itemno = trim($spline[$itemno_no]);
          $itemdesc = trim($spline[$itemdesc_no]);
          $itemdesc =mysqli_escape_string($db,$itemdesc);
          $itemdesc  = remove_special($itemdesc);
          $qty = trim($spline[$qty_no]);
          $lstprice = trim($spline[$lstprice_no]);
          $tottax = trim($spline[$tottax_no]);
          $disc = trim($spline[$disc_no]);
          $hnd = trim($spline[$hnd_no]);
          $tax = trim($spline[$tax_no]);
          $vat = trim($spline[$vat_no]);
          $tot = trim($spline[$total_no]);
          $itemtype = trim($spline[$itemtype_no]);
          $dlco = trim($spline[$dlco_no]);
          $invcan = trim($spline[$invcan_no]);
          $oth = trim($spline[$oth_no]);
         $suptype = trim($spline[$suptype_no]);
         
          $igst_rate = trim($spline[$igst_rate_no]);
          $igst_amount = trim($spline[$igst_amount_no]);
          $sgst_rate = trim($spline[$sgst_rate_no]);
          $sgst_amount = trim($spline[$sgst_amount_no]);
          $cgst_rate = trim($spline[$cgst_rate_no]);
          $cgst_amount = trim($spline[$cgst_amount_no]);
          
          $cname = trim($cname, '"');
          $cname=str_replace("'","", $cname);
          $qty = trim($qty,'"');
          $qty = str_replace(',', '', $qty);
          $lstprice = trim($lstprice,'"');
          $lstprice = str_replace(',', '', $lstprice);
          $tottax = trim($tottax,'"');
          $tottax = str_replace(',', '', $tottax);
          $disc = trim($disc,'"');
          $disc = str_replace(',', '', $disc);
          $hnd = trim($hnd,'"');
          $hnd = str_replace(',', '', $hnd);
          $tax = trim($tax,'"');
          $tax = trim($tax,'%-');
          $tax = str_replace(',', '', $tax);
          $tot = trim($tot,'"');
          $tot = str_replace(',', '', $tot);
          $vat = trim($vat,'"');
          $vat = str_replace(',', '', $vat);
          $itemtype = str_replace(',', '', $itemtype);
          $dlco = str_replace(',', '', $dlco);
          $invcan = trim($invcan, '"');
          $invcan=str_replace("'","", $invcan);
          $oth = trim($oth,'"');
          $oth = str_replace(',', '', $oth);
          $suptype = trim($suptype, '"');
            $suptype = str_replace(',', '', $suptype);
          $igst_rate = trim($igst_rate,'"');
          $igst_rate = trim($igst_rate,'%-');
          $igst_rate = str_replace(',', '', $igst_rate);
          $igst_amount = trim($igst_amount,'"');
          $igst_amount = str_replace(',', '', $igst_amount);
          
          $cgst_rate = trim($cgst_rate,'"');
          $cgst_rate = trim($cgst_rate,'%-');
          $cgst_rate = str_replace(',', '', $cgst_rate);
          $cgst_amount = trim($cgst_amount,'"');
          $cgst_amount = str_replace(',', '', $cgst_amount);
          
          $sgst_rate = trim($sgst_rate,'"');
          $sgst_rate = trim($sgst_rate,'%-');
          $sgst_rate = str_replace(',', '', $sgst_rate);
          $sgst_amount = trim($sgst_amount,'"');
          $sgst_amount = str_replace(',', '', $sgst_amount);
print $invdate;
         if($igst_rate<='')
            $igst_rate=0;
          if($igst_amount<='')
            $igst_amount=0;
          if($cgst_rate<='')
            $cgst_rate=0;
          if($cgst_amount<='')
            $cgst_amount=0;
          if($sgst_rate<='')
            $sgst_rate=0;
          if($sgst_amount<='')
            $sgst_amount=0;
            //if($tot<='')
            //$tot=0;
          $vendor_code = trim($spline[$vendor_no]);
          
          

       $cname=$cname." "."(".$vendor_code.")";  
       $pq="select brman from branch where brman='$dlco' and brcode='$brcode'";
       //print $pq;
       if($dlco!='')
       {
        $query=mysqli_query($db,"select brman from branch where brman='$dlco' and brcode='$brcode'");
        if(mysqli_num_rows($query)<=0 )
        {
            print "<script>alert('You Are Selected a Wrong Branch...!!!');</script>";
            return;
        }
       }
       
       
        print"inv ". $invno_no."<br>";
        print "inv d ".$invdate_no."<br>";
        print " part ".$itemno_no."<br>";
        print "part nam ".$itemdesc_no."<br>";
        print "qty".$qty_no."<br>";
        print "type ".$invtype_no."<br>";
        print "cname ".$cname_no."<br>";
        
        //print "tax ".$tax_no."<br>";
       // print "tot ".$total_no."<br>";
        //print "grn ".$grno_no."<br>";*/
        
        if($invno_no<='' || $invtype_no<='' || $invdate_no<='' || $cname_no<='' || $itemno_no<='' || $itemdesc_no<='' || $qty_no<='' )
        
        {
            print "<script>alert('You Are Selected a Wrong  File 1...!!!');</script>";
            return;
        }
      $k=$i-1;
       //print $invno;
       $mflg_22 = 0;
       if($brcode == 3788 || $brcode == 3783)
            $mflg_22 = 1;
      if(strlen($invno)>0 && ($suptype!='CAN' ||$mflg_22 == 1)  && $invdate>=$acf  && $invdate<=$act && $brcode==$dlco)
      {
          //
          
           //print $invno;print "ok";
         /* if($tax==''||$tax=='0')
          {
              $tax='14.50';
              $vat=$tottax*$tax/100;
              $tot=$vat+$tottax;
          }*/
          //print $invno;
          //$hnd=$hnd+$oth;
          $hnd=0;
          if(array_search($invno,$invno_arr)>0)
           {
                //print "ok2";
               print "<font color='red'>Already Inserted.....!!!--".$invno."<br></font>";
           }
           else
           {
               //print "ok1";
                            if($itemtype=='ZLUB')
                                $mitemtype='Lubricant';
                            else
                                $mitemtype='Spares';
                                $lstprice=$tottax/$qty;
                                //$tot=$tottax+$vat;
                                $tot=$tottax+$cgst_amount+$sgst_amount+$igst_amount;
        if($suptype =='CAN')
            $sql="insert into sparepurreteichcancel (invno,invtype,invdate,entrydate,saleordno,customer,partcode,partname,qty,price,disc,aprice,hand,tot,brcode,itemtype,cgstp,cgstamt,sgstp,sgstamt,igstp,igstamt,custcode) 
        values('$invno','$invtype','$invdate','$invdate','$sono','$cname','$itemno','$itemdesc','$qty','$lstprice','0','$tottax','$hnd','$tot','$brcode','spares','$cgst_rate','$cgst_amount','$sgst_rate','$sgst_amount','$igst_rate','$igst_amount','$vendor_code')";
        else
        $sql="insert into sparepurreteich (invno,invtype,invdate,entrydate,saleordno,customer,partcode,partname,qty,price,disc,aprice,hand,tot,brcode,itemtype,cgstp,cgstamt,sgstp,sgstamt,igstp,igstamt,custcode) 
        values('$invno','$invtype','$invdate','$invdate','$sono','$cname','$itemno','$itemdesc','$qty','$lstprice','0','$tottax','$hnd','$tot','$brcode','spares','$cgst_rate','$cgst_amount','$sgst_rate','$sgst_amount','$igst_rate','$igst_amount','$vendor_code')";
         print $sql."<br>";
        mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
         print "<font color='blue'>Successfuly Inserted.....!!!--".$invno."<br></font>";
      
           }
      }
      
      
    }
    $i++;
  }
  print "<script> alert('Successfully Uploaded....!!!');document.location='upld_purreteich.php';</script>";

}

function remove_special($str){
     $newstr = "";
     for ($ctr=0;$ctr<=strlen($str);$ctr++)
     {
         if(substr($str,$ctr,1)>=' ' && substr($str,$ctr,1)<='}')
         {
             if(substr($str,$ctr,1)=="'")
                $ggg=0;
             else
                $newstr=$newstr.substr($str,$ctr,1);
         }
     }
     return $newstr;
}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'calibri';letter-spacing:1;}
tr{background-color:rgb(95, 197, 187);}
.mnu{margin-left:5}
</style>
<center>
<br><br><br><br><br><br>
<form action='' method='post' ENCTYPE="multipart/form-data">
<table border=1 width=60%>
<tr><td>
<table width=100%>
<th colspan=2><h1>Upload Eicher Purchase Return GST</h1>
<tr ><td bgcolor='yellow'><font color='red'></font></td></tr>

<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>Always download the spare purchase return eicher file <br>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>