<?php
	require_once'session.php';
	require_once'../connection.php';
	require_once'../functions.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
            
            $InvDate    = $_GET['InvDate'];
            $InvAmt     = $_GET['InvAmt'];
            $ClinteId   = $_GET['ClinteId'];
            $TimePeriod = $_GET['TimePeriod'];
            $LLPfirmId  = $_GET['firm_id'];
            
            $quLPPFirm   = "SELECT * FROM firm WHERE Id='$LLPfirmId'";
            $reLPPFirm   = mysqli_query($con,$quLPPFirm);
            $dataLPPFirm = mysqli_fetch_array($reLPPFirm);
            $LLP_Firm    = $dataLPPFirm['FirmName'];
            $LLP_Gst     = $dataLPPFirm['GstNo'];
            $LLP_Addr    = $dataLPPFirm['Address'];
            
            $quClinte   = "SELECT * FROM api_user WHERE Userid='$ClinteId'";
            $reClinte   = mysqli_query($con,$quClinte);
            $dataClinte = mysqli_fetch_array($reClinte);
            $ClinteGst  = $dataClinte['Gst'];
            $ClinteFirm = $dataClinte['Firm'];
            $ClinteAddr = $dataClinte['GstAddr'];
            $GstZone    = substr($ClinteGst, 0, 2);
            $TaxblVal   = $InvAmt*100/118;
            if($GstZone=="09")
            {
                $TaxValue  = $TaxblVal*9/100;
                $CGSTnSGST = number_format($TaxValue,2,'.','');
            }
            else
            {
                $TaxValue = $TaxblVal*18/100;
                $IGST     = number_format($TaxValue,2,'.','');
            }
            
            $TaxblValue = number_format($TaxblVal,2,'.','');
            $Month = substr($TimePeriod,0,-4);
            $MONTH = strtoupper($Month);
            $YEAR  = substr($TimePeriod,-4);
            
            $quDupInv    = "SELECT Id FROM Gst_Invoice WHERE Userid='$ClinteId' AND Month='$MONTH' AND Year='$YEAR' AND firm_id='$LLPfirmId'";
            $reDupInv    = mysqli_query($con,$quDupInv);
            $countDupInv = mysqli_num_rows($reDupInv);
            if($countDupInv==1)
            {
                echo "<center><h2 style='color:red;'>The Invoice For Last Month Generated Already !</h2></center>";
                exit();
            }
            
            $quGetInvoiceNo   = "SELECT InvoiceNo FROM Gst_Invoice WHERE firm_id='$LLPfirmId' ORDER BY Id DESC";
            $reGetInvoiceNo   = mysqli_query($con,$quGetInvoiceNo);
            $dataGetInvoiceNo = mysqli_fetch_array($reGetInvoiceNo);
            $OldInvoiceNo     = $dataGetInvoiceNo['InvoiceNo'];
            $InvoiceNo        = $OldInvoiceNo+1;
        
        ?>
        <div id="main">
            <div id="invoiceNo">
                <p>Invoice No.: <?php echo $InvoiceNo; ?></p>
                <p>Date: <?php echo $InvDate ?></p>
            </div>
            <div id="month">
                <p>Month : <?php echo $MONTH; ?></p>
                <p>Year : <?php echo $YEAR; ?></p>
            </div>
            <div id="seller">
                <H2>Seller</H2>
                <H3><?php echo $LLP_Firm; ?></H3>
                <p><?php echo $LLP_Addr; ?></p>
                <P>GSTIN: <?php echo $LLP_Gst; ?></p>
            </div>
            <div id="buyer">
                <H2>Buyer</H2>
                <H3><?php echo $ClinteFirm; ?></H3>
                <p><?php echo $ClinteAddr;  ?></P>
                <P>GSTIN: <?php echo $ClinteGst; ?></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")
				{
				   	echo "<tr>";
    					echo "<td>E-TopUp Voucher</td>";
    					echo "<td>9984</td>";
    					echo "<td>".$InvAmt."</td>";
    					echo "<td>".$TaxblValue."</td>";
    					echo "<td>".$CGSTnSGST."</td>";
    					echo "<td>".$CGSTnSGST."</td>";
    					echo "<td></td>";
    				echo "</tr>";
				}
				else
				{
				    echo "<tr>";
    					echo "<td>E-TopUp Voucher</td>";
    					echo "<td>9984</td>";
    					echo "<td>".$InvAmt."</td>";
    					echo "<td>".$TaxblValue."</td>";
    					echo "<td></td>";
    					echo "<td></td>";
    					echo "<td>".$IGST."</td>";
    				echo "</tr>";
				}
				if($InvAmt>0)
				{
				    $InsertInvoice="INSERT INTO Gst_Invoice(InvoiceNo,Name,Userid,Month,Year,InvoiceValue,TaxableValue,firm_id) 
				    VALUES('$InvoiceNo','$ClinteFirm','$ClinteId','$MONTH','$YEAR','$InvAmt','$TaxblValue','$LLPfirmId')";
				    mysqli_query($con,$InsertInvoice);
				}
                ?>
                </table>
            </div>
        </div>
    </body>
</html>