<?php
	session_start();
	if($_SESSION['adm'])
	{
		$userid= $_SESSION['adm'];
	}
	else
	{
		header('location:../login.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'];
            $invoiceAmount=$_GET['invoiceAmount'];
            $invoiceDate=$_GET['invoiceDate'];
            $firm_id=$_GET['firm_id'];
            /*
            $extraAmount=2500000;
            $invoiceAmount=$invoiceAmount+$extraAmount;
            */
            $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'];
            $GstZone=substr($GstNo, 0, 2);
            
            $taxableValue = $invoiceAmount*100/118;
            $taxableValue=number_format($taxableValue,2,'.','');
            
            $IGST = $taxableValue*18/100;
            $IGST=number_format($IGST,2,'.','');
            
            $SGSTCGST = $taxableValue*9/100;
            $SGSTCGST=number_format($SGSTCGST,2,'.','');
            
            $Year= substr($invoiceDate,0,4);
            $Month= substr($invoiceDate,5,2);
        /*
            if($Month==010)
            {
                $InvoiceYear=$Year-1;
                $InvoiceMonth=12;
            }
            else
            {
                $InvoiceYear=$Year;
                $InvoiceMonth=$Month;
            }
        */
                $InvoiceYear=$Year;
                $InvoiceMonth=$Month;
            $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"=>"DECEMBER");
            $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' AND firm_id='$firm_id'";
            $reGetInvoice=mysqli_query($con,$quGetInvoive);
            $CountInvoiceRow=mysqli_num_rows($reGetInvoice);
            if($CountInvoiceRow==1)
            {
                echo "<center><h2 style='color:red;'>The Invoice For Last Month Generated Already !</h2></center>";
                exit();
            }
            
            $fdate="$InvoiceYear-$InvoiceMonth-01";
            $ldate="$InvoiceYear-$InvoiceMonth-$lastDate";
            
            $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 WHERE firm_id='$firm_id' ORDER BY Id DESC";
            $reGetInvoiceNo=mysqli_query($con,$quGetInvoiceNo);
            $dataGetInvoiceNo=mysqli_fetch_array($reGetInvoiceNo);
            $OldInvoiceNo=$dataGetInvoiceNo['InvoiceNo'];
            $NInvoiveNo=$OldInvoiceNo+1;
            if($NInvoiveNo<10)
            {
                $NewInvoiveNo="0".$NInvoiveNo;
            }
            else
            {
                $NewInvoiveNo=$NInvoiveNo; 
            }
            
            //$NewInvoiveNo="21";
            
            $InvoiveNo=$InvoiceYear."/".$MonthName."/".$NewInvoiveNo;
            
            $quMyGst="SELECT * FROM firm WHERE Id='$firm_id'";
            $reMyGst=mysqli_query($con,$quMyGst);
            $dataMyGst=mysqli_fetch_array($reMyGst);
            
            
        ?>
        <div id="main">
            <div id="invoiceNo">
                <p>Invoice No.: <?php echo $InvoiveNo; ?></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><?php echo $dataMyGst['FirmName']; ?></H3>
                <p><?php echo $dataMyGst['Address']; ?></p>
                <P>PAN: <?php echo $dataMyGst['PanNo']; ?></p>
                <P>GSTIN: <?php echo $dataMyGst['GstNo']; ?></p>
            </div>
            <?PHP
                /*
                $GstName="MANSOORI ENTERPRISES";
                $Address="0, MOHAMMDIGANJ, JALALABAD, Shamli, Uttar Pardesh, 247772";
                $PanNo="DZGPA4566M";
                $GstNo="09DZGPA4566M1ZX";
                $GstZone="09";
                */
            ?>
            <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>Discription</th>
                        <th>HSN</th>
                        <th>Invoice Value</th>
                        <th>Taxable Value</th>
                        <th>CGST (9%)</th>
                        <th>SGST (9%)</th>
                        <th>IGST (18%)</th>
                    </tr>
                <?php
                 
					    
		    	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>";
				
				if($GstZone=="09")
				{
				    /*
				    $invoiceAmount=50000;
				    $taxableValue=42372.9;
				    $SGSTCGST=3813.55;
				    */
				    
    		    	echo "<tr>";
    					echo "<td>E-TopUp Voucher</td>";
    					echo "<td>9984</td>";
    					echo "<td>".$invoiceAmount."</td>";
    					echo "<td>".$taxableValue."</td>";
    					echo "<td>".$SGSTCGST."</td>";
    					echo "<td>".$SGSTCGST."</td>";
    					echo "<td></td>";
    				echo "</tr>";
				}
				else
				{
				    /*
				    $invoiceAmount=2300000;
				    $taxableValue=1949152.5;
				    $IGST=350847.5;
				    */
				    
    		    	echo "<tr>";
    					echo "<td>E-TopUp Voucher</td>";
    					echo "<td>9984</td>";
    					echo "<td>".$invoiceAmount."</td>";
    					echo "<td>".$taxableValue."</td>";
    					echo "<td></td>";
    					echo "<td></td>";
    					echo "<td>".$IGST."</td>";
    				echo "</tr>";
				}
				if($invoiceAmount>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,InvoiceValue,TaxableValue,firm_id) 
				    VALUES('$NewInvoiveNo','$UsrName','$User','$MonthName','$InvoiceYear','$invoiceAmount','$taxableValue','$firm_id')";
				    mysqli_query($con,$InsertInvoice);
				}
                ?>
                </table>
            </div>
        </div>
    </body>
</html>