<?php
	session_start();
	if($_SESSION['adm'])
	{
		$userid= $_SESSION['adm'];
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php



?>
<html>
    <head>
        <style>
            body{margin:0; padding:0;}
            #main{background:; width:21cm; height:29.7cm; margin:auto; padding:auto; border:1px solid gray;}
            
            #invoiceNo{background:white; width:8cm; height:; margin:10px; padding:0 20px; position:; float:left; text-align:left; font-size:18px;}
            #month{background:white; width:8cm; height:; margin:10px; padding:0 20px; position:; float:right; text-align:right; font-size:18px;}
            
            #seller{background:white; width:9.8cm; height:; margin-left:10px; padding:5px; position:; float:left; text-align:left;}
            
            #buyer{background:white; width:9.8cm; height:; margin:0 10 0 10px; padding:5px; position:; float:right; text-align:right;}
            
            #tbl{background:white; width:20.5cm; height:; margin:10px; padding:0; position:; float:left;}
            table{width:20.5cm; margin:10 10px;  font-size:18px; border-collapse:collapse: }
            table td{text-align:center;}
            #blankTR{height:50px;}
            #note{background:white; width:870px; height:100px; margin:10px; padding:0; position:; float:left;}
        </style>
    </head>
    <body>
        <?php
            include'connection.php';
            
            $User=$_GET['uid'];
            
            date_default_timezone_set("asia/Kolkata");
			$datetime= Date("Y-m-d - h:i:s A");
			$time= Date("h:i:s A");
			$date= Date("Y-m-d");
			
            $Year= substr($date,0,4);
            $Month= substr($date,5,2);
            
            if($Month==01)
            {
                $InvoiceYear=$Year-1;
                $InvoiceMonth=12;
            }
            else
            {
                $InvoiceYear=$Year;
                $InvoiceMonth=$Month-1;
            }
            $lenth=strlen($InvoiceMonth);
            
            if($lenth==1)
            {
                $InvoiceMonth="0".$InvoiceMonth;
            }
            $months=array("01"=>"JANUARY", "02"=>"FEBRUARY", "03"=>"MARCH", "04"=>"APRIL", "05"=>"MAY", "06"=>"JUNE", "07"=>"JULY", "08"=>"AUGUST", "09"=>"SEPTEMBER", "10"=>"OCTOBER", "11"=>"NOVEMBER", "12"=>"DEEMBER");
            $MonthName=$months[$InvoiceMonth];
            
            if($MonthName=="JANUARY" || $MonthName=="MARCH" || $MonthName=="MAY" || $MonthName=="JULY" || $MonthName=="AUGUST" || $MonthName=="OCTOBER" || $MonthName=="DECEMBER")
            {
                $lastDate=31;
            }
            elseif($MonthName=="FEBRUARY")
            {
                $lastDate=29;
            }
            else
            {
                $lastDate=30;
            }
            
            $quGetInvoive="select * from Gst_Invoice where Userid='$User' and Month='$MonthName' and Year='$InvoiceYear'";
            $reGetInvoice=mysqli_query($con,$quGetInvoive);
            $CountInvoiceRow=mysqli_num_rows($reGetInvoice);
            if($CountInvoiceRow==1)
            {
                echo "This Invoice Generated Already !";
                exit();
            }
            
            $fdate="$InvoiceYear-$InvoiceMonth-01";
            $ldate="$InvoiceYear-$InvoiceMonth-31";
            
            $quGstDetal="select * from gst where Userid='$User'";
            $reGstDetal=mysqli_query($con,$quGstDetal);
            $dataGstdetal=mysqli_fetch_array($reGstDetal);
            
            $GstName=$dataGstdetal['GstName'];
            $Address=$dataGstdetal['Address'];
            $PanNo=$dataGstdetal['Pan'];
            $GstNo=$dataGstdetal['Gst'];
            
            $quGetInvoiceNo="select InvoiceNo from Gst_Invoice order by Id desc";
            $reGetInvoiceNo=mysqli_query($con,$quGetInvoiceNo);
            $dataGetInvoiceNo=mysqli_fetch_array($reGetInvoiceNo);
            $OldInvoiceNo=$dataGetInvoiceNo['InvoiceNo'];
            $NewInvoiveNo=$OldInvoiceNo+1;
        ?>
        <div id="main">
            <div id="invoiceNo">
                <p>Invoice No.: <?php echo $NewInvoiveNo; ?></p>
                <p>Date: <?php echo $ldate; ?></p>
            </div>
            <div id="month">
                <p>Invoice Month: <?php echo $MonthName; ?></p>
                <p>Invoice Year: <?php echo $InvoiceYear; ?></p>
            </div>
            <div id="seller">
                <H2>Seller</H2>
                <H3>LONG LIFE PRO DIGITAL SERVICES</H3>
                <p>NEAR CHARTHAWAL BUS STAND, THANA BHAWAN, SHAMLI, UTTAR PARDESH, 247777</p>
                <P>PAN: CGUPM0676E</p>
                <P>GSTIN: 09CGUPM0676E2ZW</p>
            </div>
            <div id="buyer">
                <H2>Buyer</H2>
                <H3><?php echo $GstName; ?></H3>
                <p><?php echo $Address;  ?></P>
                <P>PAN: <?php echo $PanNo; ?></p>
                <P>GSTIN: <?php echo $GstNo; ?></p>
            </div>
            <div id="tbl">
                <table border="0" cellpadding="5">
                    <tr>
                        <th>Service</th>
                        <th>HSN</th>
                        <th>Quantity</th>
                        <th>Credit Used</th>
                        <th>Taxable Value</th>
                        <th>CGST (9%)</th>
                        <th>SGST (9%)</th>
                        <th>IGST (18%)</th>
                    </tr>
                <?php
                    
			$sl=0;
			
            $TotalPrepaidCount=0;
			$TotalPostpaidCount=0;
			$TotalRoundTotalRechPreDebitAmount=0;
			$TotalRoundGstPaybleAmount=0;
			$TotalRoundGST=0;
			$TotalRoundIGST=0;
			    
			    $quPrepaidOpr="select * from operator where Serviceid='1' or Serviceid='2'";
                $rePrepaidOpr=mysqli_query($con,$quPrepaidOpr);
                while($dataPrepaidOpr=mysqli_fetch_array($rePrepaidOpr))
                {
                    $PrepaidCount=0;
			        
                    $PrepaidOpr=$dataPrepaidOpr['Operator'];
                    
			        $TotalRechPreDebitAmount=0;
                    $QUPrepaidTxn="select * from recharge where Status='SUCCESS' and Companyname='$PrepaidOpr' and Userid='$User' and Rechargedate>='$fdate' and Rechargedate<='$ldate'";
                    $RePrepaidTxn=mysqli_query($con,$QUPrepaidTxn);
                    while($DataPrepaidTxn=mysqli_fetch_array($RePrepaidTxn))
                    {   
                        $PrepaidAmount=$DataPrepaidTxn['Amount'];
                        $PrepaidCount=$PrepaidCount+$PrepaidAmount;
                        
                        $RechPreDebitAmount=$DataPrepaidTxn['Debitamount'];
                        $TotalRechPreDebitAmount=$TotalRechPreDebitAmount+$RechPreDebitAmount;
                    }
                    
                    if($PrepaidCount>0)
                    {
                        $RoundTotalRechPreDebitAmount=number_format($TotalRechPreDebitAmount,2,'.','');
                        $GstPaybleAmount=$RoundTotalRechPreDebitAmount*100/118;
                        $RoundGstPaybleAmount=number_format($GstPaybleAmount,2,'.','');
                        $GST=$RoundGstPaybleAmount*9/100;
                        $RoundGST=number_format($GST,2,'.','');
                        
                        $IGST=$RoundGstPaybleAmount*18/100;
                        $RoundIGST=number_format($IGST,2,'.','');
			    	    echo "<tr>";
    					    echo "<td>".$PrepaidOpr."</td>";
    					    echo "<td>9984</td>";
    					    echo "<td>".$PrepaidCount."</td>";
    					    echo "<td>".$RoundTotalRechPreDebitAmount."</td>";
    					    echo "<td>".$RoundGstPaybleAmount."</td>";
    					    echo "<td></td>";
    					    echo "<td></td>";
    					    echo "<td>".$RoundIGST."</td>";
				        echo "</tr>";
				    
			            $TotalPrepaidCount = $TotalPrepaidCount+$PrepaidCount;
			            $TotalRoundTotalRechPreDebitAmount=$TotalRoundTotalRechPreDebitAmount+$RoundTotalRechPreDebitAmount;
			            $TotalRoundGstPaybleAmount=$TotalRoundGstPaybleAmount+$RoundGstPaybleAmount;
			            $TotalRoundGST=$TotalRoundGST+$RoundGST;
			            $TotalRoundIGST=$TotalRoundIGST+$RoundIGST;
                    }
			    }
			    
		    	echo "<tr id='blankTR'>";
    					echo "<td></td>";
    					echo "<td></td>";
    					echo "<td></td>";
    					echo "<td></td>";
    					echo "<td></td>";
    					echo "<td></td>";
    					echo "<td></td>";
    					echo "<td></td>";
				echo "</tr>";
		    	echo "<tr>";
    					echo "<td>Total Prepaid</td>";
    					echo "<td>----</td>";
    					echo "<td>".$TotalPrepaidCount."</td>";
    					echo "<td>".$TotalRoundTotalRechPreDebitAmount."</td>";
    					echo "<td>".$TotalRoundGstPaybleAmount."</td>";
    					echo "<td></td>";
    					echo "<td></td>";
    					echo "<td>".$TotalRoundIGST."</td>";
				echo "</tr>";
				if($PrepaidAmount>0)
				{
				    $quGetName="select Name from retailer where Userid='$User'";
				    $reGetName=mysqli_query($con,$quGetName);
				    $dataGetName=mysqli_fetch_array($reGetName);
				    $UsrName=$dataGetName['Name'];
				    
				    $InsertInvoice="insert into Gst_Invoice(InvoiceNo,Name,Userid,Month,Year)values('$NewInvoiveNo','$UsrName','$User','$MonthName','$InvoiceYear')";
				    mysqli_query($con,$InsertInvoice);
				}
                ?>
                </table>
            </div>
        </div>
    </body>
</html>