<? 
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("test.php");
include("../opendb.php");
include ("../test.php");

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;
       */
       
       $imagename=$_FILES['ff']['name'];
        $filepath="txtfiles/";
        $tmp_name=$_FILES['ff']['tmp_name'];
        move_uploaded_file($tmp_name, $filepath . $imagename) or die(' err:could not copy file');
       
       
       
       
       
       
   }
    $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
    $ctr=1;
    $j=1;
    $invno_arr[]='';
    $invno_prv='';
    while(!feof($fp))
    {
        $line=fgets($fp,4096);
        $spline=explode("\t",$line);
        //print $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 sap_einvoice where invno='$invno1' and brcode='$brcode'";
                $rs=mysqli_query($db,$query);
                if(mysqli_num_rows($rs)>0)
                {
                    mysqli_query($db,"delete from sap_einvoice where invno ='$invno1'");
                    $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,4096);
        $spline=explode("\t",$line);
        //print_r($spline);
        if($i==1)
        {
            $no_col=sizeof($spline);
            for($x=0;$x<=$no_col;$x++)
            {
     
            if($spline[$x]=='Supply Type') $supply_type_no =$x;	
            if($spline[$x]=='Document type') $document_type_no =$x;	
            if($spline[$x]=='Document number') $document_number_no =$x;	
            if($spline[$x]=='Document dateDD/MM/YYYY') $document_date_no =$x;	
            if($spline[$x]=='Buyer gstin') $buyer_gstin_no =$x;	
            if($spline[$x]=='Buyer legal name') $buyer_legal_name_no =$x;	
            if($spline[$x]=='Buyer trade name') $buyer_trade_name_no =$x;	
            if($spline[$x]=='Buyer POS') $buyer_pos_no =$x;	
            if($spline[$x]=='Buyer Addr1') $buyer_addr1_no =$x;	
            if($spline[$x]=='Buyer Addr2') $buyer_addr2_no =$x;	
            if($spline[$x]=='Buyer Location') $buyer_location_no =$x;	
            if($spline[$x]=='Buyer Pincod') $buyer_pincod_no =$x;	
            if($spline[$x]=='Buyer State') $buyer_state_no =$x;	
            if($spline[$x]=='Buyer Phone Number') $buyer_phone_number_no =$x;	
            if($spline[$x]=='Buyer Email id') $buyer_email_id_no =$x;	
            if($spline[$x]=='Dispatch Name') $dispatch_name_no =$x;	
            if($spline[$x]=='Dispacth Addr2') $dispacth_addr2_no =$x;	
            if($spline[$x]=='Dispatch Location') $dispatch_location_no =$x;	
            if($spline[$x]=='Dispatch Pincode') $dispatch_pincode_no =$x;	
            if($spline[$x]=='Dispactch State') $dispactch_state_no =$x;	
            if($spline[$x]=='Shipping gstin') $shipping_gstin_no =$x;	
            if($spline[$x]=='Shipping Legal Name') $shipping_legal_name_no =$x;	
            if($spline[$x]=='Shipping Trade Name') $shipping_trade_name_no =$x;	
            if($spline[$x]=='Shipping Addr1') $shipping_addr1_no =$x;	
            if($spline[$x]=='Shipping Addr2') $shipping_addr2_no =$x;	
            if($spline[$x]=='Shipping location') $shipping_location_no =$x;	
            if($spline[$x]=='Shipping Pincod') $shipping_pincod_no =$x;	
            if($spline[$x]=='Shipping State') $shipping_state_no =$x;	
            if($spline[$x]=='sl.No.') $sl_no_no =$x;	
            if($spline[$x]=='Product Description') $product_description_no =$x;	
            if($spline[$x]=='Is_service') $is_service_no =$x;	
            if($spline[$x]=='HSN code') $hsn_code_no =$x;	
            if($spline[$x]=='Bar Code') $bar_code_no =$x;	
            if($spline[$x]=='Quantity') $quantity_no =$x;	
            if($spline[$x]=='free Quantitiy') $free_quantitiy_no =$x;	
            if($spline[$x]=='Unit') $unit_no =$x;	
            if($spline[$x]=='unit price') $unit_price_no =$x;	
            if($spline[$x]=='Gross Amount') $gross_amount_no =$x;	
            if($spline[$x]=='Discount') $discount_no =$x;	
            if($spline[$x]=='Pre Tax Value') $pre_tax_value_no =$x;	
            if($spline[$x]=='Taxable Value') $taxable_value_no =$x;	
            if($spline[$x]=='Gst Rate (%)') $gst_rate_no =$x;	
            if($spline[$x]=='sgst Amount') $sgst_amount_no =$x;	
            if($spline[$x]=='cgst amount') $cgst_amount_no =$x;	
            if($spline[$x]=='igst amount') $igst_amount_no =$x;	
            if($spline[$x]=='Cess Rate') $cess_rate_no =$x;	
            if($spline[$x]=='Cess Amt adval') $cess_amt_adval_no =$x;	
            if($spline[$x]=='cess Nonadval Amt') $cess_nonadval_amt_no =$x;	
            if($spline[$x]=='Stae Cess rate') $stae_cess_rate_no =$x;	
            if($spline[$x]=='Stae cess adval Amt') $stae_cess_adval_amt_no =$x;	
            if($spline[$x]=='stae Cess Non adval Amt') $stae_cess_non_adval_amt_no =$x;	
            if($spline[$x]=='other charges') $other_charges_no =$x;	
            if($spline[$x]=='Item Total') $item_total_no =$x;
            if($spline[$x]=='Total taxable value') $total_taxable_value_no =$x;	
            if($spline[$x]=='sgst amt') $sgst_amt_no =$x;	
            if($spline[$x]=='cgst amt') $cgst_amt_no =$x;	
            if($spline[$x]=='igst amt') $igst_amt_no =$x;	
            if($spline[$x]=='cess amt') $cess_amt_no =$x;	
            if($spline[$x]=='Stae Cess amt') $stae_cess_amt_no =$x;	
            if($spline[$x]=='Discount') $discount_no =$x;	
            if($spline[$x]=='Other Changes') $other_changes_no =$x;	
            if($spline[$x]=='Round off') $round_off_no =$x;	
            if($spline[$x]=='total invoice Value') $total_invoice_value_no =$x;
             
            }
        }
        if($i>1)
        {
            $invno = $spline[$document_number_no];
            $invno =  $spline[$document_number_no];
            $suptype =  $spline[$supply_type_no];
            $doctype =  $spline[$document_type_no];
            $entrydate =  $spline[$document_date_no];
            $buygstn =  $spline[$buyer_gstin_no];
            $customer =  $spline[$buyer_legal_name_no];
            $acname =  $spline[$buyer_trade_name_no];
            $buypos =  $spline[$buyer_pos_no];
            $buyaddr1 =  $spline[$buyer_addr1_no];
            $buyaddr2 =  $spline[$buyer_addr2_no];
            $buyloc =  $spline[$buyer_location_no];
            $buypin =  $spline[$buyer_pincod_no];
            $buystate =  $spline[$buyer_state_no];
            $buyphon =  $spline[$buyer_phone_number_no];
            $buyemail =  $spline[$buyer_email_id_no];
            $shipgstn =  $spline[$shipping_gstin_no];
            $shilglnm =  $spline[$shipping_legal_name_no];
            $shiptrdnm =  $spline[$shipping_trade_name_no];
            $shipaddr1 =  $spline[$shipping_addr1_no];
            $shipaddr2 =  $spline[$shipping_addr2_no];
            $shiploc =  $spline[$shipping_location_no];
            $shippin =  $spline[$shipping_pincod_no];
            $shipstate =  $spline[$shipping_state_no];
            $proddesc =  $spline[$product_description_no];
            $srvflg =  $spline[$is_service_no];
            $qty =  $spline[$quantity_no];
            $price =  $spline[$unit_price_no];
            
            $price = str_replace('"', '', $price);
            $price = str_replace(',', '', $price);
            
            $disc =  $spline[$discount_no];
            
            $aprice =  $spline[$taxable_value_no];
            
            $aprice = str_replace('"', '', $aprice);
            $aprice = str_replace(',', '', $aprice);
            
            $hsncode =  $spline[$hsn_code_no];
            $cessp =  $spline[$cess_rate_no];
           
            $cessamt =  $spline[$cess_amt_adval_no];
            $cessamt = str_replace(',', '', $cessamt);            
            $cessamt = str_replace('"', '', $cessamt);             
            
            $gstp =  $spline[$gst_rate_no];
           
            $cgstamt =  $spline[$cgst_amount_no];
            $cgstamt = str_replace('"', '', $cgstamt);
            $cgstamt = str_replace(',', '', $cgstamt);
             
            $sgstamt =  $spline[$sgst_amount_no];
            $sgstamt = str_replace('"', '', $sgstamt);
            $sgstamt = str_replace(',', '', $sgstamt);
           
            
            $igstamt =  $spline[$igst_amount_no];
            $igstamt = str_replace('"', '', $igstamt);
            $igstamt = str_replace(',', '', $igstamt);            
            $other_charges =  $spline[$other_charges_no];
             
            
            $other_charges = str_replace('"', '', $other_charges);
            $other_charges = str_replace(',', '', $other_charges); 
             
             
            $sl_no =  $spline[$sl_no_no];
            $item_total =  $spline[$item_total_no];
            $item_total = str_replace('"', '', $item_total);
            $item_total = str_replace(',', '', $item_total);
            $total_taxable_value =  $spline[$total_taxable_value_no];
            $total_taxable_value = str_replace('"', '', $total_taxable_value);
            $total_taxable_value = str_replace(',', '', $total_taxable_value);
            $cgst_amt =  $spline[$sgst_amt_no];
            $cgst_amt = str_replace('"', '', $cgst_amt);
            $cgst_amt = str_replace(',', '', $cgst_amt);
            $sgst_amt =  $spline[$cgst_amt_no];
            $sgst_amt = str_replace('"', '', $sgst_amt);
            $sgst_amt = str_replace(',', '', $sgst_amt);
            $igst_amt =  $spline[$igst_amt_no];
            $igst_amt = str_replace('"', '', $igst_amt);
            $igst_amt = str_replace(',', '', $igst_amt);            
            $cess_amt =  $spline[$cess_amt_no];
            $cess_amt = str_replace(',', '', $cess_amt);            
            $cess_amt = str_replace('"', '', $cess_amt);            
            $other_changes =  $spline[$other_changes_no];
            $other_changes = str_replace('"', '', $other_changes);
            $other_changes = str_replace(',', '', $other_changes);
            $total_invoice_value =  $spline[$total_invoice_value_no];
            $total_invoice_value = str_replace('"', '', $total_invoice_value);
            $total_invoice_value = str_replace(',', '', $total_invoice_value);            $buyphon = str_replace("'", "", $buyphon); 
    
            //echo $invno;
            $entrydate = str_replace('/', '-', $entrydate);
            $entrydate = implode('-', array_reverse(explode('-', $entrydate)));
            $k=$i-1;
            
            if($suptype=='B2B' && $doctype=='Tax Invoice')
            {
                if(array_search($invno,$invno_arr)>0)
                {
                    print "<font color='red'>Found !!! Data overwritten.....!!!--".$invno."<br></font>";
                }
                    $mbrcode1 = substr($invno,0,4);
                    /*$query="select brcode from branch where brman='$mbrcode1'";
                    $rs=mysqli_query($db,$query);
                    $brrow=mysqli_fetch_array($rs);
                    $mbrcode = $brrow[0];*/
                    if(($mbrcode1=='3751')||($mbrcode1=='3752'))
                    {
                        $sql="insert into sap_einvoice (invno,	suptype,	doctype,	entrydate,	buygstn,	customer,	acname,	buypos,	buyaddr1,	buyaddr2,	buyloc,	buypin,	buystate,	buyphon,	buyemail,	shipgstn,	shilglnm,	shiptrdnm,	shipaddr1,	shipaddr2,	shiploc,	shippin,	shipstate,	proddesc,	srvflg,	qty,	price,	disc,	aprice,	hsncode,	cessp,	cessamt,	gstp,	cgstamt,	sgstamt,	igstamt,	other_charges,	sl_no,	item_total,	total_taxable_value,	cgst_amt,	sgst_amt,	igst_amt,	cess_amt,	other_changes,	total_invoice_value,brcode) values ('$invno',	'$suptype',	'$doctype',	'$entrydate',	'$buygstn',	'$customer',	'$acname',	'$buypos',	'$buyaddr1',	'$buyaddr2',	'$buyloc',	'$buypin',	'$buystate',	'$buyphon',	'$buyemail',	'$shipgstn',	'$shilglnm',	'$shiptrdnm',	'$shipaddr1',	'$shipaddr2',	'$shiploc',	'$shippin',	'$shipstate',	'$proddesc',	'$srvflg',	'$qty',	'$price',	'$disc',	'$aprice',	'$hsncode',	'$cessp',	'$cessamt',	'$gstp',	'$cgstamt',	'$sgstamt',	'$igstamt',	'$other_charges',	'$sl_no',	'$item_total',	'$total_taxable_value',	'$cgst_amt',	'$sgst_amt',	'$igst_amt',	'$cess_amt',	'$other_changes',	'$total_invoice_value','PSN236')";
                        //echo $sql;
                        //echo "<br>";
                        mysqli_query($db,$sql);
                    }
                
            }
        }
        $i++;
    }
    print "<script> alert('Successfully Uploaded....!!!');document.location='sap_spareinvoiceupload.php';</script>";
}
?>
<center>
<form action='' method='POST' ENCTYPE="multipart/form-data">
<table cellspacing=1 cellpadding=2 class='form'>
<tr height=21><td colspan=2 align=center bgcolor='steelblue'><FONT COLOR=white><B>Upload Text File</td></tr>
</td></tr>




<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>
</form>


</td></tr></table>

