<?php
error_reporting(E_ERROR | E_PARSE);
session_start();

include '../../class/ClassForm.php';
$inv_query = "select distinct * from domestic_invS where id='".$_GET['id']."'";
$result = $ClassForm->Fetch_Result_Set($inv_query);
$row = mysqli_fetch_assoc($result);

$inv_temQuery = "select distinct * from inv_template where id='".$row['template_id']."'";
$teml_res = $ClassForm->Fetch_Result_Set($inv_temQuery);
$temRow = mysqli_fetch_assoc($teml_res);

$biller_query = "select * from biller_details where inv_id = '".$row['inv_no']."'";
$bill_resule = $ClassForm->Fetch_Result_Set($biller_query);
$billRow = mysqli_fetch_assoc($bill_resule);

$tax_Query = "select * from domestic_taxdetails where inv_id ='".$_GET['id']."'";
$tax_resule = $ClassForm->Fetch_Result_Set($tax_Query);
$taxRow = mysqli_fetch_assoc($tax_resule);

$ser_Query = "select * from domestic_service where inv_id ='".$_GET['id']."'";
$ser_resule = $ClassForm->Fetch_Result_Set($ser_Query);
//$serv_Row = Fetch_Result_Set($ser_resule);

?>


<div class="col-12">
    <div class="col-4">
        <a class="btn btn-info" onclick="Print_Datas('#invoice')">
            Print DC </a>
    </div>
    <div class="col-4">

    </div>
    <div class="col-4">

    </div>
</div>
<div class="col-12 domInv" id="invoice">
    <div class="row">
        <div class="col-12 inv_title text-center">
            TAX INVOICE
        </div>
    </div>
    <div class="row " style="padding-bottom:5px;">
        <div class="col-12">
            <div class="row ">
                <div class="col-4 float-left logo247">
                    [24]<span>7<span>
                </div>
                <div class="col-4 Seller text-left">
                    <br />
                    <div class="col-12 sel_title">
                    <?php echo $temRow['cmpny_name']; ?>
                    </div>
                    <div class="col-12 address"><?php echo $temRow['cmpny_addr1'] ?>
                    </div>
                    <div class="col-12 address">
                    <?php echo $temRow['cmpny_addr2']; ?>
                    </div>
                    <div class="col-12 address"><?php echo $temRow['cmpny_addr3']; ?></div>
                    <div class="col-12 address"><?php echo $temRow['cmpny_addr4']; ?></div>
                    <!-- <div class="col-12 address">Bangalore – 560087.</div> -->
                    <div class="col-12 gstin">
                        GSTIN : <span><?php echo $temRow['cmpny_gstin'] ?></span>
                    </div>
                    <div class="col-12 pan">
                        PAN : <span><?php echo $temRow['cmpny_pan'] ?></span>
                    </div>
                </div>
                <div class="col-4 qrCode float-right">
                    <img src="qr/temp/.png" id="qrcode" name="qrcode"
                        title="QrCode Not Found" />
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-12 ">
            <div class="row BillerDetails">
                <div class="header">Billed to</div>
                <div class="col-6">
                    <div class="row">
                        <div class="col-12 billerInfo">
                            <div class="row">
                                <div class="col-12 Billername"><?php echo $billRow['biller_Name'] ?></div>
                                <div class="col-12 Address"><?php echo $billRow['biller_Addr1'] ?></div>
                                <div class="col-12 Address"><?php echo $billRow['biller_Addr2'] ?></div>
                                <div class="col-12 Address"><?php echo $billRow['biller_Addr3'] ?></div>
                                <div class="col-12 Address"><?php echo $billRow['biller_Addr4'] ?></div>
                                <!-- <div class="col-12 Address">State Code : 29</div> -->
                                <div class="col-12 gstin">GSTIN/Unique code : <?php echo $billRow['biller_GSTIN'] ?></div>
                                <div class="col-12 pan">PAN - <?php echo $billRow['biller_PAN'] ?></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-6 BillerDetails_Right">
                    <div class="col-12 _right">
                        <table class="invDetailTable" style="">
                            <tr>
                                <td>
                                    <span class="lbl">Invoice No:</span><br />
                                    <?php echo $row['inv_no'] ?>
                                </td>
                                <td>
                                    <span class="lbl">Invoice Date:</span><br />
                                    <?php echo $row['inv_date'] ?>
                                </td>
                            </tr>
                            <tr>
                                <td class="lbl">Place of Supply </td>
                                <td><?php echo $row['inv_pos'] ?></td>
                            </tr>
                            <tr>
                                <td class="lbl">State Code</td>
                                <td><?php echo $row['inv_sc'] ?>
                                </td>
                            </tr>
                            <tr>
                                <td class="lbl">PO Reference</td>
                                <td>
                                <?php echo $row['inv_poref'] ?>
                                </td>
                            </tr>
                            <tr>
                                <td></td>
                                <td>
                                    <br /><br />
                                </td>
                            </tr>
                        </table>
                    </div>

                </div>
            </div>
        </div>
    </div>
    <div class="row irn">
        <div class="col-12 " style="font-weight:bold">
            <span>IRN : <span>
        </div>
    </div>
    <div class="row">
        <table class="pr_addressTable col-lg-12">
            <thead>
                <tr class="text-center">
                    <th width="100">SL. NO</th>
                    <th colspan="7">Particulars</th>
                    <th width="200">HSN /Service Chapter Heading (Tariff)</th>
                    <th width="150">AMOUNT Rs.</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $i = 0;
    while($serv_Row = mysqli_fetch_assoc($ser_resule))
    {
        $i++;
        ?>
                <tr>
                    <td class="text-center"><?php echo $i; ?></td>
                    <td colspan="7"> <?php echo $serv_Row['particulars'] ?></td>
                    <td class="text-center"> <?php echo $serv_Row['s_code'] ?> </td>
                    <td class="text-right "> <?php echo $serv_Row['amount'] ?> </td>
                </tr>
        <?php
    }
                ?>
               
                <tr>
                    <td colspan="8" rowspan="6">
                        <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
                    </td>
                </tr>
                <tr>
                    <!-- <td colspan="8" >
                            
                        </td> -->
                    <td>NET TAXABLE VALUE</td>
                    <td class=" text-right valxy"><?php echo $taxRow['taxable_amt']; ?></td>
                </tr>
                <tr>
                    <td>CGST @ <?php echo $taxRow['cgst_per']; ?>%</td>
                    <td class=" text-right valxy"><?php echo $taxRow['cgst_val']; ?></td>
                </tr>
                <tr>
                    <td>SGST @ <?php echo $taxRow['sgst_per']; ?>%</td>
                    <td class="text-right "><?php echo $taxRow['sgst_val']; ?></td>
                </tr>
                <tr>
                    <td>IGST @ <?php echo $taxRow['igst_per']; ?>%</td>
                    <td class=" text-right "><?php echo $taxRow['igst_val']; ?></td>
                </tr>
                <tr>
                    <td>Round off</td>
                    <td class=" text-right "><?php echo $taxRow['roindOff'] ;?></td>
                </tr>
                <tr>
                    <td colspan="8" class="rsTowrd">
                        amount in words
                    </td>
                    <td>Value</td>
                    <td class="_val money text-right"><?php echo $taxRow['total']; ?></td>
                </tr>
                <tr>
                    <td colspan="8" class="text-center">
                        Whether Tax payable under Reverse Charge : No
                    </td>
                    <td colspan="2"></td>

                </tr>
            </tbody>
        </table>
    </div>
    <br />
    <div class="row">
        <div class="col-12 text-right" style="font-weight:bold;padding-right:20px;">
            For 24/7 Customer Pvt Ltd
        </div>
    </div>
    <div class="row">
        <div class="col-8 text-center" style="font-weight:bold;padding:10px;">
            All transactions covered by this Invoice are subject to the terms and <br />
            conditions which have been agreed by the parties to the transaction.<br />
            <br />
            <br /><br />
        </div>
        <div class="col-2"></div>
    </div>
    <div class="row">
        <div class="col-12 text-right" style="font-weight:bold;padding-right:20px;">
            Authorized Signatory
        </div>
    </div>
    <div class="row">
        <div class="col-12">
            <div class="row " style="padding:20px;">
                <div class="col-12 paymen_intro" style="font-family:Book Antiqua;font-size:12pt;">
                    <div class="row" style="padding:10px;">
                        <div class="col-12" style="color:blue;font-weight:bold;padding:5px">Payment Instructions
                        </div>
                        <div class="col-12" style="padding:5px;">1. Payment should be made to 24/7 Customer Pvt Ltd;
                        </div>
                        <div class="col-12" style="padding:5px;">2. The remittance should specifically indicate that
                            payment is being made for this invoice.</div>
                        <div class="col-12" style="padding:5px;">3. Wire Transfer: Axis Bank Ltd, Main Branch,
                            Esquire Centre, No 9, M G Road; Bangalore-560 001</div>
                        <div class="col-12 text-center">Bank Account Number: 009010200016667; IFSC/RTGS number:
                            UTIB0000009</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row" style="border:solid 1px;">
        <div class="col-12 text-center"
            style="padding-left:25px; padding-right:25px;font-family:Book Antiqua;font-size:10;">
            <?php echo $temRow['cmpny_footer']; ?>
        </div>
    </div>
</div>

<style>
.domInv {
    border: solid 2px;
    border-radius: 5px;
}

.inv_title {
    color: blue;
    font-family: Times New Roman;
    font-weight: bold;
    font-size: 20pt;
    padding: 10px;
}

.logo247>span {
    color: orange;
}

.logo247 {
    font-family: Arial;
    font-weight: bold;
    font-size: 72pt;
    /* Bangalore – 560087. */
}

.Seller>.sel_title {
    color: blue;
    font-family: Times New Roman;
    font-weight: bold;
    font-size: 18pt;
    padding: 10px;
}

.Seller>.address {

    font-family: Times New Roman;
    /* font-weight : bold; */
    font-size: 14pt;
}

.gstin,
.pan {
    color: blue;
    font-family: Times New Roman;
    font-weight: bold;
    font-size: 12pt;

}

.qrCode>img {
    width: 300px !important;
    height: 300px !important;
    /* background-image: url('qr/temp/test95ed2d0bad8d676e73cb2ecefeaf5fe3.png'); */
    background-size: cover;
}

.BillerDetails {
    border: solid 1px;
    /* border-radius : 10px; */
    padding: 10px;
}

.billerInfo {
    border: solid 1px;
    border-radius: 5px;

}

.BillerDetails>.header {
    position: absolute;
    margin-top: -25px;
    margin-left: 10px;
    color: blue;
    background: white;
    border-radius: 10px;
    padding: 2px 10px;
    font-family: Bookman Old Style;
    font-weight: bold;
    font-size: 12t;
}

.Billername {
    color: blue;
    font-family: Times New Roman;
    font-weight: bold;
    font-size: 12pt;
}

.billerInfo>.row>.Address {
    font-family: Times New Roman;
    /* font-weight: bold; */
    font-size: 12pt;
}

.BillerDetails_Right {}

._right {
    /* border: solid 1px; */
    /* padding: 5px; */
}

.invDetailTable {
    border-collapse: collapse;
    width: 100%;
    height: 100%;
    font-size: 12pt;
}

.lbl {
    font-size: 12pt;
    font-weight: bold;
}

.invDetailTable,
th,
td {
    border: 1px solid black;
}

span .lbl {
    font-family: Times New Roman;
    font-weight: bold;
    font-size: 10pt;
}

.irn {
    border: 1px solid black;
}

.enMoney::before {
    content: "";
}

.money {
    padding-right: 10px;
}

.paymen_intro {
    border: solid 1px;
    border-radius: 10px;
}
</style>
<script>
$(document).ready(function() {

    setTimeout(() => {
        $tot = $('._val').html();        
        $('.rsTowrd').text('Amount In Words: '+NumberToWords($tot));
        }, 500);
    
    // $('.money').ready(function() {
    //     $('.money').each(function() {
    //         var item = $(this).text();
    //         var num = Number(item).toLocaleString('en-IN');
    //         if (Number(item) < 0) {
    //             num = num.replace('-', '');
    //             $(this).addClass('negMoney');
    //         } else {
    //             $(this).addClass('enMoney');
    //         }

    //         if (num.indexOf('.') < 0) {
    //             num = num + '.00';
    //         }

    //         $(this).text(num);
    //     });
    //     setTimeout(() => {
    //         commaAmmound();
    //     }, 5000);

    // });

});


function escapeRegExp(string) {
    return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

function replaceAll(str, term, replacement) {
    return str.replace(new RegExp(escapeRegExp(term), 'g'), replacement);
}

function commaAmmound() {
    let total = 0;
    $('.valxy').each(function() {
        var num = 0;
        var item = $(this).text();
        if (typeof item !== "undefined") {
            if (item.indexOf(',')) {
                num = parseFloat(replaceAll(item, ',', ''));
            }
            total = parseFloat(total) + parseFloat(num);
        }
    });    
    $('._val').text(total.toFixed(2));
    var words = NumberToWords(total.toFixed(2));
    $('.rsTowrd').text('Amount In Words: '+words);
};
</script>
<script>
function Print_Datas(id) {
    html2canvas($(id), {
        onrendered: function(canvas) {
            var data = canvas.toDataURL();
            var docDefinition = {
                content: [{
                    image: data,
                    width: 505,
                }, ],
                pageSize: 'A4',
                pageMargins: [45, 30, 20, 45],
            };
            pdfMake.createPdf(docDefinition).download(id + ".pdf");
        }
    });

    // html2canvas($('#invoice'), {
    //     onrendered: function(canvas) {         
    //         var imgData = canvas.toDataURL(
    //             'image/png');              
    //         var doc = new jsPDF('p', 'mm');
    //         doc.addImage(imgData, 'PNG', 10, 10);
    //         doc.save('sample-file.pdf');
    //     }
    // });

}

function NumberToWords(amount) {
    var words = new Array();
    words[0] = '';
    words[1] = 'One';
    words[2] = 'Two';
    words[3] = 'Three';
    words[4] = 'Four';
    words[5] = 'Five';
    words[6] = 'Six';
    words[7] = 'Seven';
    words[8] = 'Eight';
    words[9] = 'Nine';
    words[10] = 'Ten';
    words[11] = 'Eleven';
    words[12] = 'Twelve';
    words[13] = 'Thirteen';
    words[14] = 'Fourteen';
    words[15] = 'Fifteen';
    words[16] = 'Sixteen';
    words[17] = 'Seventeen';
    words[18] = 'Eighteen';
    words[19] = 'Nineteen';
    words[20] = 'Twenty';
    words[30] = 'Thirty';
    words[40] = 'Forty';
    words[50] = 'Fifty';
    words[60] = 'Sixty';
    words[70] = 'Seventy';
    words[80] = 'Eighty';
    words[90] = 'Ninety';
    amount = amount.toString();
    var atemp = amount.split(".");
    var number = atemp[0].split(",").join("");
    var n_length = number.length;
    var words_string = "";
    if (n_length <= 9) {
        var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
        var received_n_array = new Array();
        for (var i = 0; i < n_length; i++) {
            received_n_array[i] = number.substr(i, 1);
        }
        for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
            n_array[i] = received_n_array[j];
        }
        for (var i = 0, j = 1; i < 9; i++, j++) {
            if (i == 0 || i == 2 || i == 4 || i == 7) {
                if (n_array[i] == 1) {
                    n_array[j] = 10 + parseInt(n_array[j]);
                    n_array[i] = 0;
                }
            }
        }
        value = "";
        for (var i = 0; i < 9; i++) {
            if (i == 0 || i == 2 || i == 4 || i == 7) {
                value = n_array[i] * 10;
            } else {
                value = n_array[i];
            }
            if (value != 0) {
                words_string += words[value] + " ";
            }
            if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Crores ";
            }
            if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Lakhs ";
            }
            if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Thousand ";
            }
            if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
                words_string += "Hundred and ";
            } else if (i == 6 && value != 0) {
                words_string += "Hundred ";
            }
        }
        words_string = words_string.split("  ").join(" ");
    }
    return words_string;
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js" type="text/javascript"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.22/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>