
<?php 
session_start();
ob_start();


function convert_number_to_words($number)
{

    $hyphen      = '-';
    $conjunction = ' and ';
    $separator   = ', ';
    $negative    = 'negative ';
    $decimal     = ' point ';
    $dictionary  = array(
        0                   => 'Zero',
        1                   => 'One',
        2                   => 'Two',
        3                   => 'Three',
        4                   => 'Four',
        5                   => 'Five',
        6                   => 'Six',
        7                   => 'Seven',
        8                   => 'Eight',
        9                   => 'Nine',
        10                  => 'Ten',
        11                  => 'Eleven',
        12                  => 'Twelve',
        13                  => 'Thirteen',
        14                  => 'Fourteen',
        15                  => 'Fifteen',
        16                  => 'Sixteen',
        17                  => 'Seventeen',
        18                  => 'Eighteen',
        19                  => 'Nineteen',
        20                  => 'Twenty',
        30                  => 'Thirty',
        40                  => 'Fourty',
        50                  => 'Fifty',
        60                  => 'Sixty',
        70                  => 'Seventy',
        80                  => 'Eighty',
        90                  => 'Ninety',
        100                 => 'Hundred',
        1000                => 'Thousand',
        1000000             => 'Million',
        1000000000          => 'Billion',
        1000000000000       => 'Trillion',
        1000000000000000    => 'Quadrillion',
        1000000000000000000 => 'Quintillion'
    );

    if (!is_numeric($number)) {
        return false;
    }

    if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
        // overflow
        trigger_error(
            'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
            E_USER_WARNING
        );
        return false;
    }

    if ($number < 0) {
        return $negative . convert_number_to_words(abs($number));
    }

    $string = $fraction = null;

    if (strpos($number, '.') !== false) {
        list($number, $fraction) = explode('.', $number);
    }

    switch (true) {
        case $number < 21:
            $string = $dictionary[$number];
            break;
        case $number < 100:
            $tens   = ((int) ($number / 10)) * 10;
            $units  = $number % 10;
            $string = $dictionary[$tens];
            if ($units) {
                $string .= $hyphen . $dictionary[$units];
            }
            break;
        case $number < 1000:
            $hundreds  = $number / 100;
            $remainder = $number % 100;
            $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
            if ($remainder) {
                $string .= $conjunction . convert_number_to_words($remainder);
            }
            break;
        default:
            $baseUnit = pow(1000, floor(log($number, 1000)));
            $numBaseUnits = (int) ($number / $baseUnit);
            $remainder = $number % $baseUnit;
            $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
            if ($remainder) {
                $string .= $remainder < 100 ? $conjunction : $separator;
                $string .= convert_number_to_words($remainder);
            }
            break;
    }

    if (null !== $fraction && is_numeric($fraction)) {
        $string .= $decimal;
        $words = array();
        foreach (str_split((string) $fraction) as $number) {
            $words[] = $dictionary[$number];
        }
        $string .= implode(' ', $words);
    }

    return $string;
}



include("../../../pidbco/pidbjoin.php");

$receipts = strip_tags(base64_decode($_GET['receipts']));
$sq = mysqli_query($con, "SELECT * FROM `p_receipts` WHERE `rid` = '$receipts' ");
$rs = mysqli_fetch_array($sq);

$comid = $rs['rcust'];
$sq1 = mysqli_query($con,"SELECT * FROM `p_customers` WHERE `cid`='$comid' ");
$rs1 = mysqli_fetch_array($sq1); 

$rorder = $rs['rorder'];
$sq2 = mysqli_query($con, "SELECT * FROM `p_orders` WHERE `order_id`='$rorder' ");
$rs2 = mysqli_fetch_array($sq2);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Programers Receipt</title>
    <style>
        /* Print Configuration */
        @media print {
    @page {
        size: A5 landscape;
        margin: 0;
    }

    html, body {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        overflow: hidden;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .no-print {
        display: none !important;
    }

    .receipt-container {
        width: 100vw;
        height: 100vh;
        margin: 0;
        padding: 0;
        border: none;
        box-sizing: border-box;
        display: flex;
    }

    .receipt-border {
        width: 100%;
        height: 100%;
        border: 2px solid #333;
        border-radius: 12px;
        box-sizing: border-box;
    }
}


        /* General Styling */
        body {
            font-family: Arial, Helvetica, sans-serif;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .receipt-container {
            width: 210mm; /* A5 Landscape Width */
            height: 148mm; /* A5 Landscape Height */
            background: white;
            padding: 3mm;
            box-sizing: border-box;
            border: 1px solid #ccc; /* Visible border on screen */
            position: relative;
            display: flex;
            flex-direction: column;
        }

        /* Use a separate inner border to mimic the rounded rectangle in the image */
        .receipt-border {
            border: 2px solid #333;
            border-radius: 12px;
            height: 100%;
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }

        /* Header Section */
        .header {
            display: flex;
            justify-content: space-between;
            padding: 15px 20px;
        }

        .logo-section {
            width: 70%;
        }

        .logo-title {
            font-size: 28px;
            font-weight: bold;
            margin: 0;
            letter-spacing: 1px;
            display: flex;
            align-items: center;
        }

        .logo-globe {
            display: inline-block;
            position: relative;
        }
        
        .logo-globe::before {
             content: "🌍"; /* Emoji approximation or use an img tag */
             font-size: 24px;
             margin-right: 2px;
        }

        .tagline {
            font-size: 12px;
            font-weight: bold;
            margin-left: 10px;
            border-left: 2px solid #333;
            padding-left: 8px;
        }

        .address {
            font-size: 10px;
            margin-top: 8px;
            line-height: 1.4;
        }

        .meta-section {
            width: 25%;
            text-align: right;
            display: flex;
            flex-direction: column;
            align-items: flex-end;
        }

        .receipt-label {
            font-size: 16px;
            font-weight: bold;
            text-decoration: underline;
            margin-bottom: 10px;
            text-transform: uppercase;
        }

        .receipt-no {
            font-size: 14px;
            margin-bottom: 8px;
            font-weight: bold;
        }
        
        .red-text {
            color: #d00000;
            font-size: 18px;
        }

        .date-row {
            display: flex;
            align-items: center;
            font-size: 14px;
        }

        .box-input {
            border: 1px solid #333;
            width: 100px;
            height: 20px;
            margin-left: 5px;
        }

        /* Middle Section - Checkboxes */
        .services-section {
            border-top: 1px solid #333;
            border-bottom: 1px solid #333;
            padding: 8px 20px;
            display: flex;
            font-size: 11px;
            justify-content: space-between;
        }

        .col {
            display: flex;
            flex-direction: column;
            gap: 5px;
        }

        .checkbox-item {
            display: flex;
            align-items: center;
        }

        .checkbox-square {
            width: 12px;
            height: 12px;
            border: 1px solid #333;
            margin-right: 5px;
            display: inline-block;
        }

        .order-row {
            display: flex;
            align-items: center;
            margin-top: 5px;
        }
        
        .order-box {
            border: 1px solid #333;
            width: 80px;
            height: 18px;
            margin-left: 5px;
        }

        /* Form Lines Section */
        .form-section {
            padding: 15px 20px;
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            gap: 12px;
        }

        .line-row {
            display: flex;
            align-items: flex-end; /* Align text to bottom to match dotted line */
            font-size: 14px;
            width: 100%;
        }

        .label {
            white-space: nowrap;
            margin-right: 5px;
        }

        .dotted-fill {
            flex-grow: 1;
            border-bottom: 1px dotted #333;
            margin-bottom: 4px; /* Adjustment for baseline */
        }
        
        .short-dotted {
            width: 150px;
            border-bottom: 1px dotted #333;
            margin-bottom: 4px;
            margin-right: 10px;
        }

        /* Footer Section */
        .footer-section {
            padding: 0 20px 10px 20px;
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
        }

        .amount-box-container {
            border: 2px solid #333;
            border-radius: 8px;
            padding: 5px 10px;
            width: 200px;
            height: 40px;
            display: flex;
            align-items: center;
        }

        .amount-label {
            font-weight: bold;
            margin-right: 10px;
            font-size: 16px;
        }

        .signature-area {
            text-align: center;
            font-size: 12px;
        }

        .disclaimer {
            border-top: 1px solid #333;
            font-size: 9px;
            text-align: center;
            padding: 4px;
            width: 100%;
            box-sizing: border-box;
        }

        /* Screen only elements */
        .controls {
            position: fixed;
            top: 20px;
            right: 20px;
            background: white;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        }
        
        button {
            padding: 8px 16px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        button:hover {
            background: #0056b3;
        }

    </style>
</head>
<body>

    <div class="controls no-print">
        <button onclick="window.print()">Print Receipt</button>
    </div>

    <div class="receipt-container">
        <div class="receipt-border">
            
            <div class="header">
                <div class="logo-section">
                    <div class="logo-title">
                     <img src="images/pro-logo.png" alt="" class="logo-img" style="width: 50%;" >
                        <!-- <span class="logo-globe">PR</span>OGRAMERS <span class="tagline">expertise from enriched experience</span> -->
                    </div>
                    <div class="address">
                        4th Floor, Park House Building, Round North Thrissur, Kerala-680001<br>
                        Mobile: +91 9447151442 | 9446451442 | 9495951442<br>
                        www.programers.in | info@programers.in | www.programersglobal.com
                    </div>
                </div>
                <div class="meta-section">
                    <div class="receipt-label">RECEIPT</div>
                    <div class="receipt-no">No: <span class="red-text"><?php echo $rs['ridentity']; ?></span></div>
                    <div class="date-row">
                        Date: <div class="box-input"><?php echo $rs['rdate']; ?></div>
                    </div>
                </div>
            </div>

            <div class="services-section">
                <div class="col">
                    <div class="checkbox-item"><span class="checkbox-square"></span> Programers</div>
                    <div class="checkbox-item"><span class="checkbox-square"></span> Programers Web Technologies</div>
                </div>
                <div class="col">
                    <div class="checkbox-item"><span class="checkbox-square"></span> Programers International</div>
                    <div class="checkbox-item"><span class="checkbox-square"></span> Programers Software Solutions</div>
                </div>
                <div class="col">
                    <div class="checkbox-item"><span class="checkbox-square"></span> Programers Resources</div>
                    <div class="order-row">
                        ORDER NO: <div class="order-box"></div>
                    </div>
                </div>
            </div>

            <div class="form-section">
                <div class="line-row">
                    <span class="label">Received from Mr/Ms/M/s</span>
                    <span class="dotted-fill"><?php echo $rs1['cname']; ?></span>
                </div>
                
                <div class="line-row">
                    <span class="label">Address</span>
                    <span class="dotted-fill">&nbsp;<?php echo $rs1['cdistrict'].",".$rs1['cstate']; ?></span>
                </div>
                
                <div class="line-row">
                    <span class="dotted-fill"></span>
                </div>

                <div class="line-row">
                    <span class="label">The sum of Rupees ( in words )</span>
                    <span class="dotted-fill"><?php echo convert_number_to_words($rs['ramount']).' Rupees Only.';  ?></span>
                </div>

                <div class="line-row">
                    <span class="dotted-fill"></span>
                </div>

                <div class="line-row">
                    <span class="label">By Cash / Draft / Cheque No</span>
                    <span class="dotted-fill" style="flex-grow: 0.5;"><?php if($rs['ramnt_type'] == 'Cash') { ?> Cash <?php } else { 
                	?>cheque, No. <span style="font-style:italic; display:inline-block; border-bottom:dotted 1px #666666; padding:0px 5px; margin-right:10px;">
                	    <?php echo $rs['rcheque']; ?></span> Dated <span style="font-style:italic; display:inline-block; border-bottom:dotted 1px #666666; padding:0px 5px; margin-right:10px;"><?php echo $rs['rcheq_date']; ?></span> <?php } ?> </span>
                    <span class="label" style="margin-left: 10px;">Dated</span>
                    <span class="dotted-fill"><?php echo $rs['rcheq_date']; ?></span>
                </div>

                <div class="line-row">
                    <span class="label">Drawn on</span>
                    <span class="dotted-fill" style="flex-grow: 0.5;"></span>
                    <span class="label" style="margin-left: 10px;">Towards</span>
                    <span class="dotted-fill"><?php echo $rs2['bill_head']; ?></span>
                </div>
                
                 <div class="line-row">
                    <span class="dotted-fill"></span>
                </div>
            </div>

            <div class="footer-section">
                <div class="amount-box-container">
                    <span class="amount-label">Rs.<?php echo number_format($rs['ramount'],2); ?></span>
                </div>
                <div class="signature-area">
                    
                    <img src="images/signature.jpg" alt="" style="width:75px;" /><br />
                    (Authorised Signatory)
                </div>
            </div>

            <div class="disclaimer">
                * Receipt Subject to relation of Cheque, There will be no refunds given once payment has been made, except for duplicate payments
            </div>

        </div>
    </div>

</body>
</html>