<?php
	require_once'session.php';
	require_once'../connection.php';
	require_once'../functions.php';
	
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .invoice-container {
            background-image: linear-gradient(#F8D6BD, white , #C6F8CF);
			width: 800px;
            margin: 0 auto;
            border: 1px solid #ccc;
            padding: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .header {
			background-image: linear-gradient(to top left, #8CA7F6, #1750F4 );
			box-shadow:6px 6px 14px -2px rgba(0,0,0,0.68);
            text-align:center;
            margin-bottom: 10px;
			padding:5px;
			border-radius:6px;
			color:#E5E7EB;
        }
        .header h1 {margin:10px 0 5px 0;}
        .header hr {width:50%;}
		.divInv{background:; width:100%; height:80px; margin:0; padding:0;}
		.p1{background:; width:40%; text-align:left; float:left; padding:0 20px 5px 20px;}
		.p2{background:; width:40%;text-align:right;  float:right; padding:0 20px 5px 20px;}
		
        .company-details{
            margin:20px 0 20px 0;
            overflow: hidden; /* Clearfix */
            width: 45%; /* Adjust as needed for layout */
            float: left;
            box-sizing: border-box;
            padding: 0 1%;
        }
        .customer-details div {
            margin:20px 0 20px 0;
            overflow: hidden; /* Clearfix */
            width: 45%; /* Adjust as needed for layout */
            float: right;
            box-sizing: border-box;
            padding: 0 1%;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        table th, table td {
            border: 1px solid #ccc;
            padding: 8px;
            text-align: left;
        }
        table th {
            background-color: #f2f2f2;
        }
        table td {
            background-color:white;
        }
        .total-section {
            text-align: right;
            margin-top: 20px;
        }
        .total-section p {
            margin: 5px 0;
        }
        .signature-section {
            margin-top: 50px;
            text-align: right;
        }
        .gstin {
            font-weight: bold;
        }
    </style>
</head>
<body><?php
            $CGSTnSGST = 0;
            $IGST      = 0;
            
            $InvDate    = $_GET['InvDate'];
            $InvAmt     = $_GET['InvAmt'];
            $ClinteId   = $_GET['ClinteId'];
            $TimePeriod = $_GET['TimePeriod'];
            $LLPfirmId  = $_GET['firm_id'];
            
            $dateRF = date_create($InvDate);
            $dateRF = date_format($dateRF,"d-m-Y");
            //$dateRF = "30-04-2026";
            
            $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;
            
            
			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);
			}
        
        ?>
        
    <div class="invoice-container">
        <div class="header">
            <h1><?php echo $LLP_Firm; ?></h1><hr>
			<div class="divInv">
				<p class="p1">Invoice No. : <?php echo $InvoiceNo; ?><br><br>Invoice Date. : <?php echo $dateRF ?></p>
				<p class="p2">Month : <?php echo $MONTH; ?><br><br>Year : <?php echo $YEAR; ?></p>
			</div>
        </div>

        <div class="company-details">
            <div>
                <p><strong>Sold By:</strong></p>
                <p><?php echo $LLP_Firm; ?></p>
                <p><?php echo $LLP_Addr; ?></p>
                <p>GSTIN: <?php echo $LLP_Gst; ?></p>
            </div>
        </div>

        <div class="customer-details">
            <div>
                <p><strong>Bill To:</strong></p>
                <p><?php echo $ClinteFirm; ?></p>
				<p><?php echo $ClinteAddr;  ?></p>
                <p>GSTIN: <?php echo $ClinteGst; ?></p>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <th>Sr.</th>
                    <th>Description</th>
                    <th>HSN</th>
                    <th>Taxable Value</th>
                    <th>CGST<br>(9%)</th>
                    <th>SGST<br>(9%)</th>
                    <th>IGST<br>(18%)</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>E-Topup Voucher</td>
                    <td>998413</td>
                    <td><?php echo $TaxblValue; ?></td>
                    <td><?php echo $CGSTnSGST; ?></td>
                    <td><?php echo $CGSTnSGST; ?></td>
                    <td><?php echo $IGST; ?></td>
                </tr>
                <tr>
                    <td>-</td>
                    <td>-</td>
                    <td>-</td>
                    <td>-</td>
                    <td>-</td>
                    <td>-</td>
                    <td>-</td>
                </tr>
                <!-- Add more rows for additional items -->
            </tbody>
        </table>

        <div class="total-section">
            <p><strong>Total Taxable Value:   </strong> <?php echo $TaxblValue; ?></p>
            <p><strong>Total CGST: </strong> <?php echo $CGSTnSGST; ?></p>
            <p><strong>Total SGST: </strong> <?php echo $CGSTnSGST; ?></p>
            <p><strong>Total IGST: </strong> <?php echo $IGST; ?></p>
            <p><strong>Grand Total:</strong> <?php echo $InvAmt; ?></p>
        </div>

        <div class="signature-section">
            <p>For <?php echo $LLP_Firm; ?></p>
            <br><br>
        </div>
    </div>
</body>
</html>