<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
require("includes/function.php");

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$vouch = $_GET['vouchno'];
$type = $_GET['type'];
$para = $_GET['para'];

$cond = '';
$lcond = '';
$formname = "INVOICE";
$maindb = 'salemain';
$trandb = 'saletran';
$root = $dbname;
$zroot = $_SESSION["zdir"];

$getqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt  FROM `$maindb` WHERE vouchno='$vouch' ORDER BY `vouchno`") or die(mysqli_error($conn));


$qry = mysqli_fetch_array($getqry);


$getcompdt = mysqli_query($conn, "SELECT * FROM $zroot.`compmast` WHERE `code`= '$qry[company]'") or die(mysqli_error($conn));
$fetcompdt = mysqli_fetch_array($getcompdt);

include("includes/linkshead.php");
?>
<link href="MyStyle/inv_format.css" rel="stylesheet">
<style>
    @media screen,
    print {
        body {
            font-size: 13px;
            margin-left: 20px;
            margin-right: 20px !important;
        }

        .table>tbody>tr>td {
            padding: 0px;
        }

        .f-s {
            font-size: 12px;
        }

        .sklogo {
            position: relative;
        }

        .sklogo:after {
            content: "";
            display: block;
            width: 100%;
            height: 100%;
            position: absolute;
            top: 5px;
            left: 7px;
            background-image: url(bnxbg.png);
            background-size: 210px 100px;
            background-position: -10px -10px;
            background-repeat: no-repeat;
            opacity: 0.7;
        }

        .smfont23 {
            font-size: 13px;
        }

        .fontnew {
            font-size: 13px;
            color: black;
        }

        .tablesk>thead>tr>th,
        .tablesk>tbody>tr>th,
        .tablesk>tfoot>tr>th,
        .tablesk>thead>tr>td,
        .tablesk>tbody>tr>td,
        .tablesk>tfoot>tr>td {
            border: 1px solid black !important;
            color: black;
        }

        .tablesk>tbody>tr>td,
        .tablesk>tfoot>tr>td {
            font-size: 14px;
        }

    }

    .f12 {
        font-size: 14px;
    }

    .f10 {
        font-size: 13px;
    }

    .bold {
        font-weight: bold;
    }

    .skdiv {
        width: 50%;
        margin: auto;
    }

    img {
        width: 50%;
        margin: auto;
    }



    .header-center-2 {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 18px;
        max-width: 900px;
        margin: 0 auto;
        padding: 18px;
        border: 1px solid #000;
        /* border-radius:6px; */
    }

    .header-center-2 .logo {
        flex: 0 0 120px;
        display: flex;
        justify-content: center;
    }

    .header-center-2 .logo img {
        width: 120px;
        height: 120px;
        object-fit: contain;
        display: block;
    }

    .header-center-2 .details {
        width: 560px;
        /* fixed width so centering inside it looks balanced */
        text-align: center;
        /* center inside the details block */
    }

    @media (max-width:700px) {
        .header-center-2 {
            flex-direction: column;
        }

        .header-center-2 .details {
            width: 100%;
            padding: 0 10px;
            text-align: center;
        }
    }
</style>
</head>

<body>
    <!-- onload="window.print();" -->
    <?php
    $tdhg = "14px";

    $logoPath = $fetcompdt['image'];
    $logoExists = !empty($logoPath) && file_exists($logoPath);
    ?>
    <div class="page">

        <div class="" style="width:95%;margin-left:15px;margin-top:7px;">

            <div class="header-center-2">
                <?php if ($logoExists): ?>
                    <div class="logo">
                        <img src="<?= $fetcompdt['image']; ?>" alt="logo">
                    </div>
                <?php endif; ?>



                <div class="details">
                    <h1 style="margin:0; font-size:3rem;"><?= $fetcompdt['name']; ?></h1>

                    <div style="margin-top:6px;">
                        <?= $fetcompdt['add1'] . ' ' . $fetcompdt['add2'] . ' ' . $fetcompdt['add3']; ?><br>
                        <?= $fetcompdt['city'] . ' - ' . $fetcompdt['pin']; ?>
                    </div>

                    <div style="margin-top:6px;">
                        <?php if (!empty($fetcompdt['email'])) { ?>
                            Email: <?= $fetcompdt['email']; ?> &nbsp;
                        <?php } ?>
                        <?php if (!empty($fetcompdt['tel'])) { ?>
                            |&nbsp; Tel: <?= $fetcompdt['tel']; ?> &nbsp;
                        <?php } ?>
                        <?php if (!empty($fetcompdt['website'])) { ?>
                            |&nbsp; Website: <?= $fetcompdt['website']; ?>
                        <?php } ?>
                    </div>

                    <div style="margin-top:6px;">VAT No. <?= $fetcompdt['vatno']; ?></div>
                    <div style="margin-top:10px;font-weight:bold"><strong><?= $formname; ?></strong></div>
                </div>
                <?php if ($logoExists): ?>
                    <div class="logo">
                        <!-- <img src="<?= $fetcompdt['image']; ?>" alt="logo"> -->
                    </div>
                <?php endif; ?>
            </div>

            <div class="border">
                <div class="row">
                    <?php

                    $getpartyname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$qry[party]' AND `grpcode`='010'") or die(mysqli_error($conn));
                    $fparty = mysqli_fetch_array($getpartyname);
                    $partyname = $fparty['name'];
                    $mobile = $fparty["mobile"];
                    $add1 = $fparty['add1'];
                    $add2 = $fparty['add2'] . " " . $fparty['add3'];

                    if ($qry["gstno"] != "") {
                        $partygstno = " GST No.: " . $qry["gstno"];
                    }
                    ?>
                    <table width="100%">

                        <tr>
                            <td style="width:40%; border-right: 1px solid black;" class="text-left skpadd">
                                <label class="fontnew " style="text-align: left;font-weight:900">Invoice To :&nbsp;&nbsp;&nbsp;
                                    <span style="font-size: 16px;"><? echo $partyname; ?></span></label>
                            </td>
                            <td style="width:10%" class="text-left skpadd">
                                <label class="fontnew" style="text-align: left;font-size : 14px;"><b>&nbsp; Invoice No. &nbsp; </b></label>
                            </td>
                            <td class="skpadd" style="width:20%">
                                <label class="fontnew" style="font-size: 14px;font-family: 'verdana';font-weight:900">:
                                    <?= $qry['invno']; ?></label>
                            </td>
                        </tr>

                        <tr>
                            <td class="skpadd" style="width:40%;border-right: 1px solid black">
                                <?php if ($add1 != '') { ?>
                                    <label class="fontnew"><span
                                            style="font-size: 16px;"><? echo $add1 . ' ' . $add2; ?></span></label>
                                <?php } ?>
                            </td>
                            <td style="width:15%" class="text-left skpadd">
                                <label class="fontnew" style="text-align: left;font-size : 14px;">&nbsp;Date
                                    &nbsp;&nbsp;</label>
                            </td>
                            <td class="text-left skpadd" style="width:15%">
                                <label class="fontnew" style="font-size : 14px;">:
                                    <b><?= $qry['mydt']; ?></b></label>
                            </td>
                        </tr>

                        <tr>
                            <td class="skpadd" style="width:40%;border-right: 1px solid black">
                                <?php if ($add3 != '') { ?>
                                    <label class="fontnew"><? echo $add3 ?></label>
                                <?php } ?>
                            </td>
                            <td style="width:15%" class="text-left skpadd">
                                <label class="fontnew" style="text-align: right;font-size : 12px;"></label>
                            </td>
                            <td class="skpadd"
                                style="width:15%; font-family: 'Verdana';font-size : 12px;font : 'bold';"><label
                                    class="fontnew"></label></td>
                        </tr>

                        <tr>
                            <td class="skpadd" style="width:70%;border-right: 1px solid black">
                                <label class="fontnew" style="text-align: right;font-size : 14px;">Mobile No.
                                </label>
                                <label class="fontnew">:&nbsp;<span
                                        style="font-size: 16px;"><?= $mobile; ?></span></label>
                            </td>
                            <td style="width:15%" class="text-left skpadd">
                            </td>
                            <td class="skpadd"
                                style="width:15%; font-family: 'Verdana';font-size : 12px;font : 'bold';">
                            </td>
                        </tr>

                    </table>
                </div>

            </div>

            <div>
                <div class="row">
                    <div class="col-sm-12 pad">
                        <table class="tablesk table-bordered" style="width: 100%">
                            <tr>
                                <th class="border bold" style="width:5%;  text-align: center;">Sr</th>
                                <th class="border bold" style="width:46%; text-align: center;">Product</th>
                                <th class="border bold" style="width:10%; text-align: right;">Qty.</th>
                                <th class="border bold" style="width:6%; text-align: center;">Unit</th>
                                <th class="border bold" style="width:10%; text-align: right;">Rate</th>
                                <th class="border bold" style="width:10%; text-align: right;">Amount</th>
                            </tr>
                            <?
                            $ctab = 0;
                            $ctr = 0;
                            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$qry[vouchno]' ORDER BY `srno`") or die(mysqli_error($conn));
                            while ($tran = mysqli_fetch_array($gettran)) {
                                $getitem = mysqli_query($conn, "SELECT `name` FROM `product` WHERE code = '$tran[itemcode]'") or die(mysqli_error($conn));
                                $geti = mysqli_fetch_array($getitem);

                                $getunit = mysqli_query($conn, "SELECT * FROM $zroot.`unit` WHERE code = '$tran[unit]'") or die(mysqli_error($conn));
                                $fetunit = mysqli_fetch_array($getunit);

                                if ($tran["igstrate"] > 0) {
                                    $gstperc = $tran["igstrate"];
                                } else {
                                    $gstperc = $tran["cgstrate"] * 2;
                                }

                                $frame = $tran['frame'];
                                $frame1 = substr($frame, 0, 6);
                                $frame2 = substr($frame, 6, 15);

                                $ctr++;
                                ?>
                                <tr style="height: 21px">
                                    <td class="text-center" style="padding: 1px"><? echo $ctr; ?></td>
                                    <td style="padding: 1px"><? echo $geti['name'] ?></td>
                                    <td class="text-right" style="padding: 1px"><? echo $tran['qty']; ?></td>
                                    <td class="text-center" style="padding: 1px"><? echo $fetunit['name'] ?></td>
                                    <td class="text-right" style="padding: 1px"><? echo $tran['rate']; ?></td>
                                    <td class="text-right" style="padding: 1px"><? echo $tran['amount']; ?></td>
                                </tr>
                                <?
                                $ctab = $ctab + 1;
                                $grqty = $grqty + $tran['qty'];
                                $totamt = $totamt + $tran['amount'];
                            }

                            $cnt = 20;
                            if ($ctab < $cnt) {
                                for ($i = $ctab; $i < $cnt; $i++) {
                                    ?>
                                    <tr style="height: 21px">
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                    </tr>
                                    <?php
                                }
                            }
                            ?>

                        </table>

                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-12 pad">
                    <table width="100%" class="border">
                        <tr style="height:25px;">
                            <td style="width:5%;" class="border" colspan=""></td>
                            <td style="width:46%;" class="border text-right f10 bold">Qty.</td>
                            <td style="width:10%;" class="border text-right f10 bold">
                                <?php echo number_format($grqty, 2) ?>
                            </td>
                            <td style="width:16%;" class="border text-right f10 bold">Total</td>
                            <td style="width:10%;" class="border text-right f10 bold">
                                <?php echo number_format($totamt, 2) ?>
                            </td>
                        </tr>

                        <?php if ($qry['totdisc'] > 0) { ?>
                            <tr style="height:25px;">
                                <td class="border" colspan="3"></td>
                                <td class="border text-right f11 bold">Less Discount</td>
                                <td class="border text-right f11 bold"><?php echo "&pound; " . number_format($qry['totdisc'], 2) ?>
                                </td>
                            </tr>
                        <?php } ?>

                        <?php if ($qry['courchgs'] > 0) { ?>
                            <tr style="height:25px;">
                                <td class="border" colspan="3"></td>
                                <td class="border text-right f11 bold">Courier Charges</td>
                                <td class="border text-right f11 bold"><?php echo "&pound; " . number_format($qry['courchgs'], 2) ?>
                                </td>
                            </tr>
                        <?php } ?>

                        <?php if ($qry['totvat'] > 0) { ?>
                            <tr style="height:25px;">
                                <td class="border" colspan="3"></td>
                                <td class="border text-right f11 bold">Add VAT</td>
                                <td class="border text-right f11 bold"><?php echo "&pound; " .  number_format($qry['totvat'], 2) ?>
                                </td>
                            </tr>

                        <?php } ?>

                        <tr style="height:25px;">
                            <td class="border" colspan="3"></td>
                            <td class="border text-right f12 bold">Bill Amount</td>
                            <td class="border text-right f12 bold">
                                <?php echo "&pound; " . number_format($qry['billamt'], 2) ?>
                            </td>
                        </tr>


                        <tr style="height:25px;">
                            <td class="border f12" colspan="3"><?php echo convertToPoundsAndPence($qry['billamt']); ?>
                                Only
                            </td>
                            <?php if ($qry['totadv'] > 0) { ?>

                                <td class="border text-right f11 bold">Advance</td>
                                <td class="border text-right f11 bold"><?php echo number_format($qry['totadv'], 2) ?>
                                </td>
                            <?php } else {
                                ?>
                                <td class="border" colspan="2"></td>

                            <?php } ?>

                        </tr>

                        <?php if ($qry['totadv'] > 0) { ?>
                            <tr style="height:25px;">

                                <td class="border" colspan="3"></td>

                                <td class="border text-right f11 bold">Balance</td>
                                <td class="border text-right f11 bold">
                                    <?php echo convertAmountToWords($qry['totbal'], 2) ?>
                                </td>
                            </tr>
                        <?php } ?>

                        <tr style="height:25px;">
                            <td class="border f10" colspan="5">Narration : <?= $qry['rem1']; ?></td>
                        </tr>

                        <tr style="height:25px;">
                            <td class="border" colspan="5"> <?= $qry['rem2']; ?></td>
                        </tr>

                        <tr style="height:25px;">
                            <td colspan="5">
                                <table border="solid 1px" style="border-collapse: collapse; width:50%">
                                    <tbody style="border-collapse: collapse;">

                                        <tr style="height:25px;">
                                            <td style="width:25%;font-weight:bold;">Bank Name</td>
                                            <td style="width:75%;"><?= $fetcompdt['bankname'] ?></td>
                                        </tr>

                                        <tr style="height:25px;">
                                            <td style="width:25%;font-weight:bold;">Account No.</td>
                                            <td style="width:75%;"><?= $fetcompdt['acno'] ?></td>
                                        </tr>
                                        
                                        <tr style="height:25px;">
                                            <td style="width:25%;font-weight:bold;">Sort Code</td>
                                            <td style="width:75%;"><?= $fetcompdt['sortcode'] ?></td>
                                        </tr>
                                        
                                        <tr style="height:25px;">
                                            <td style="width:25%;font-weight:bold;">IBAN No.</td>
                                            <td style="width:75%;"><?= $fetcompdt['ibanno'] ?></td>
                                        </tr>
                                        
                                        <tr style="height:25px;">
                                            <td style="width:25%;font-weight:bold;">Reg. No.</td>
                                            <td style="width:75%;"><?= $fetcompdt['regnno'] ?></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>

                    </table>

                </div>
            </div>
        </div>
        <?php include('includes/linksfoot.php'); ?>