<?
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'.mysql_error());
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
      
      $fag10=0;
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]=='Document number')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline[$invno_no1]);
    
    
    if($invno1!=$invno_prv)
    {
        $query="select invno from spareinvoice where invno='$invno1' and brcode='$brcode'";
        print $query;
        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
            $invno_arr[$ctr++]=$invno1;
        }
        else
        {
          $query="select invno from salesretnewjc where invno='$invno1' and brcode='$brcode'";
          print $query;
        $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]=='Document number')
          $invno_no = $x;
         if($spline[$x]=='Document type')
          $invtype_no = $x;
         if($spline[$x]=='Document dateDD/MM/YYYY')
          $invdate_no = $x;
         if($spline[$x]=='SO No.')
          $so_no = $x;
            if($spline[$x]=='Dispacth Addr1')
            $vendor_no = $x;
         if($spline[$x]=='Buyer legal name')
          $cname_no = $x;
         if($spline[$x]=='Product Description')
          $itemno_no = $x;
         if($spline[$x]=='Product Description')
          $itemdesc_no = $x;
         if($spline[$x]=='Quantity')
          $qty_no = $x;
         if($spline[$x]=='unit price')
          $lstprice_no = $x;
         if($spline[$x]=='Discount')
          $disc_no = $x;
         if($spline[$x]=='Taxable Value')
          $tottax_no = $x;
         if($spline[$x]=='other charges')
          $hnd_no = $x;
         if($spline[$x]=='Gst Rate (%)')
          $tax_no = $x;
         if($spline[$x]=='VAT Amount')
          $vat_no = $x;
          if(trim($spline[$x])=='Item Total')
          $total_no = $x;
       
       if($spline[$x]=='Buyer gstin')
            $gst_no = $x;
       
          if($spline[$x]=='Is_service')
          $itemtype_no = $x;
          if($spline[$x]=='Dispatch Name')
          $dlco_no = $x;
         if($spline[$x]=='Invoice cancelled')
          $invcan_no = $x;
          if($spline[$x]=='Other charges')
          $oth_no = $x;
          
          
          if($spline[$x]=='igst amount')
          $igst_amount_no = $x;
          
          if($spline[$x]=='sgst Amount')
          $sgst_amount_no = $x;
         
          if($spline[$x]=='cgst amount')
          $cgst_amount_no = $x;
          if($spline[$x]=='HSN code')
          $hsn_no = $x;
          
          if($spline[$x]=='Document number')
          $sapinv_no = $x;
          
          if($spline[$x]=='Total Tax Amount')
          $tottaxa_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);
          $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]);
         
         
          $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]);
          $hsn = trim($spline[$hsn_no]);
      $sapinv = trim($spline[$sapinv_no]);
      $tottaxa_amount = trim($spline[$tottaxa_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);
          $hsn = trim($hsn,'"');
          $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);
          
          $tottaxa_amount = str_replace(',', '', $tottaxa_amount);
            $tottaxa_amount = trim($tottaxa_amount,'"');
         if($cgst_amount==0)
         {
            $igst_rate=$tax;
              $cgst_rate=0;
           $sgst_rate=0;
           $vatorcst='INTERSTATE';
         }
          if($cgst_amount>0)
          {
            $cgst_rate=$tax/2;
           $sgst_rate=$tax/2;
          $igst_rate=0;
          $vatorcst='INTRASTATE';
          }
            //if($tot<='')
            //$tot=0;
          $vendor_code = trim($spline[$vendor_no]);
          
          $gst_code = trim($spline[$gst_no]);
          
$cname=mysqli_escape_string($db,$cname);
       // $cust=mysqli_escape_string($db,$cname);
       print $cname;
       if(strlen($dlco)<4)
            $dlco=substr($invno,0,4);
            print $invno;
       $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 $invno_no."invno <br>";
      print $invtype_no."invtype<br>";print $invdate_no." invdate<br>";print $cname_no."cname <br>";
      print $itemno_no."itemno <br>";
      print $itemdesc_no." itemdes <br>";
      print $qty_no."qty <br>";print $lstprice_no."list <br>";print $tottax_no."tot <br>";print $igst_rate_no."igst <br>";print $igst_amount_no."igsta <br>";print $sgst_amount_no."ststa <br>";print $cgst_amount_no."cgsta <br>";print $cgst_rate_no."cgstp <br>";print $sgst_rate_no."sgstp <br>";
      //*************************************************************
        if($invno_no<='' || $invtype_no<='' || $invdate_no<='' || $cname_no<='' || $itemno_no<='' || $itemdesc_no<='' || $qty_no<='' || $lstprice_no<='' || $tottax_no<=''||$vendor_no<='')
        
        {
            print "<script>alert('You Are Selected a Wrong  File 2...!!!');</script>";
            return;
        }
       
      $k=$i-1;
      //echo "<br>";
      //echo $invdate."test";
     // echo "<br>";
      //echo $acf."test";
     // echo "<br>";
      //echo $act."test";
      //echo "<br>";
      
      if(strlen($invno)>0 && ($invdate>=$acf && $invdate<=$act) && ($invtype=='Credit Note' ))
      {
          
           /*if($tax=='')
          {
              $tax='14.50';
              $vat=$tottax*$tax/100;
              $tot=$vat+$tottax;
          }*/
          //$hnd=$hnd+$oth;
          $fcessa=0;
           $tcsa=0;
           if($hnd=='')
           $hnd=0;
          // $tottaxa_amount1=($cgst_amount+$sgst_amount+$igst_amount);  
           print $hnd;
          if(array_search($invno,$invno_arr)>0)
           {
               print "<font color='red'>Already Inserted.....!!!--".$invno."<br></font>";
           }
           else
           {
               print $invno;
                            if($itemtype=='YES')
                            {
                                $mitemtype='Labour';
                            }
                            else
                            {
                                $mitemtype='Spares';
                            }
                            //$tottaxa_amount1=$tottaxa_amount;
                            
                            
                            /*if($gst_code!='')
                            {
                             $tcsa=$tottaxa_amount1-($cgst_amount+$sgst_amount+$igst_amount);   
                            }
                            else
                            {
                            $fcessa=$tottaxa_amount1-($cgst_amount+$sgst_amount+$igst_amount);
                            }*/
                            if($fcessa=='')
                            {
                                $fcessa=0;
                            }
                            if($tcsa=='')
                            {
                                $tcsa=0;
                            }
                            
                            /*$fcessa=$tottaxa_amount1-($cgst_amount+$sgst_amount+$igst_amount);
                            if($fcessa=='')
                            {
                                $fcessa=0;
                            }
                            */
                            $qty=$qty*-1;
                            $tottax=$tottax*-1;
                            $tot=$tot*-1;
                            
                            $itemdesc=remove_special($itemdesc);
                            $itemno=remove_special($itemno);
        $sql="insert into salesretnewjc (invno,invtype,invdate,entrydate,saleordno,customer,partcode,partname,qty,price,disc,aprice,hand,tot,brcode,itemtype,msno,cgstp,cgstamt,sgstp,sgstamt,igstp,igstamt,fcessa,gstno,tcsa,vatorcst) 
        values('$invno','$invtype','$invdate','$invdate','$sono','$cname','$itemno','$itemdesc','$qty','$lstprice','$disc','$tottax','$hnd','$tot','$brcode','$mitemtype','$vendor_code','$cgst_rate','$cgst_amount','$sgst_rate','$sgst_amount','$igst_rate','$igst_amount','$fcessa','$gst_code','$tcsa','$vatorcst')";
        echo $sql;
        print "<font color='blue'>Successfuly Inserted.....!!!--".$invno."<br></font>";
        mysqli_query($db,$sql) or die('Insert Err:'.mysql_error());
        
           }
      }
    }
    $i++;
  }
  //print "<script> alert('Successfully Uploaded....!!!');document.location='upld_jobcardret.php';</script>";

}

function remove_special($str){
         $newstr = "";
         for ($ctr=0;$ctr<=strlen($str);$ctr++)
         {
             if(substr($str,$ctr,1)>=' ' && substr($str,$ctr,1)<='}')
                  $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 Jobcard Return</h1>
<tr ><td bgcolor='yellow'><font color='red'><blink>
 FIELD FROM EXCEL FILE</blink></font></td></tr>

<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>Download ZDEALER_EXTRACT report.Remove col BJ to end.enter plant code in Dispatch Name col and customer code in Dispacth Addr1 col as text. <br>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>