<?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);
$type = $_GET['type'];

$cond = '';
$lcond = '';
$formname = "PROFORMA INVOICE";
$maindb = 'profexmain';
$trandb = 'profextran';
$keyfld = 'vouchno';
$vouchno = $_GET["vouchno"];
$zroot = $_SESSION["zdir"];
$fseries = $_SESSION["series_" . $dbname];

$gmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(invdate, '%d-%m-%Y')as invdt,DATE_FORMAT(sbdate, '%d-%m-%Y')as sbdate,DATE_FORMAT(orddate, '%d-%m-%Y')as orddt  FROM `$maindb` WHERE $keyfld = '$vouchno'  ") or die(mysqli_error($conn));
$fmain = mysqli_fetch_array($gmain);

$getconsg = mysqli_query($conn, "SELECT `accode`,`name`,`add1`,`add2`,`add3`, `tel`,`mobile`,`mobile2` FROM `accmast` WHERE `accode`= '$fmain[consignee]' ") or die(mysqli_error($conn));
$consg = mysqli_fetch_array($getconsg);

$getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$fmain[partyname]' and grpcode = '010'") or die(mysqli_error($conn));
$party = mysqli_fetch_array($getparty);

$getcomp = mysqli_query($conn, "SELECT code,init,compname,hoadd1,hoadd2,hoadd3,hocity,hopin FROM $zroot.`company` ")or die(mysqli_error($conn));
$comp = mysqli_fetch_array($getcomp);
include("includes/linkshead.php");
?>
<link href="MyStyle/rep_format.css" rel="stylesheet">
<style>
    body{
        font-size: 13px;
    }
    .table > tbody > tr > td{
        padding: 0px;
    }
    .f-s{
        font-size: 12px;
    }
    .border{
        border:1px solid black;
    }
    .bor{
        border-left: 1px solid black;
        border-right: 1px solid black;
    }

    .fontnew{
        font-family: 'Verdana';
        font-size : 11px;
        font : 'bold';

    }
    .smsfont{
        font-family: 'Verdana';
        font-size : 13px;
        /*font : 'bold';*/
    }
    table{
        width:100%;
    }
    table td {
        font-size: 12px;
        padding: 5px;
    }

</style>
</head>
<body>

    <div class="page">
        <div class="subpage" style="padding-top: 16px;">
            <div class="row">
                <div class="col-lg-12 col-md-12 col-sm-12 text-right">
                    <button onclick="window.print();return false;" class="btn btn-primary noPrint" style="text-align: right">Print</button>
                </div>
            </div><br>
            <h4 align="center"> <b> <u> <?= $formname ?> </b> </u> </h4>
            <table>
                <tr>
                    <td width="50%" class="border">
                        <table class=" ">
                            <tr><td>Exporter</td></tr>
                            <tr><td><b><?php echo $comp['compname']; ?></b></td></tr>
                            <tr><td><b><?php echo $comp['hoadd1']; ?></b></td></tr>
                            <tr><td><b><?php echo $comp['hoadd2']; ?></b></td></tr>
                            <tr><td><b><?php echo $comp['hoadd3']; ?></b></td></tr>
                        </table>
                    </td>
                    <td width="50%" class="border">
                        <table class=" " width="100%">
                            <tr>
                                <td width="50%" style="border-right : 1px solid black">Invoice No.</td>                               
                                <td width="50%">Exporters Ref..</td>
                            </tr>
                            <tr style="border-bottom:1px solid black">

                                <td width="50%" style="border-right : 1px solid black"><b><?php Echo $fmain['invoice'] ?></b><br>Date:&nbsp;&nbsp;&nbsp;<b><?php echo $fmain['invdt']; ?></td>
                                <td width="50%"><b><?php ?></b></td>
                            </tr>
                            <tr style="border-bottom:1px solid black">
                                <td colspan="2" width="50%">Buyers Order No & Date.<?php echo $fmain['orderno'] . " " . $fmain['orddt'] ?></td>
                            </tr>

                            <tr>
                                <td colspan="2" width="50%">Other Reference.</td>
                            </tr>
                        </table>                    
                    </td>
                </tr>
                <tr>
                    <td class="border">
                        <table>
                            <tr><td>Consignee</td></tr>
                            <tr><td><b><?php echo $consg['name']; ?></b></td></tr>
                            <?php if ($consg['add1'] != '') { ?><tr><td>Address</td></tr><?php } ?>
                            <?php if ($consg['add1'] != '') { ?><tr><td><b><?php
                                            echo $consg['add1'];
                                        }
                                        ?></b></td></tr>
                            <?php if ($consg['add2'] != '') { ?><tr><td><b><?php
                                            echo $consg['add2'];
                                        }
                                        ?></b></td></tr>
                            <?php if ($consg['add3'] != '') { ?><tr><td><b><?php
                                            echo $consg['add3'];
                                        }
                                        ?></b></td></tr>
                            <?php if ($consg['mobile'] != '') { ?><tr><td>Telephone : <?php
                                        echo $consg['mobile'] . "/" . $consg['mobile2'];
                                    }
                                    ?></td></tr>
                            <?php if ($consg['fax'] != '') { ?><tr><td>Fax : </td></tr><?php } ?>
                        </table>
                    </td>


                    <td class="border">
                        <table>
                            <tr><td>NOTIFY : </td></tr>
                            <tr><td colspan="2"><b><?php echo $party['name'] ?></b></td></tr>

                            <?php if ($consg['add1'] != '') { ?>

                                <tr><td colspan="2">Address</td></tr><?php } ?>
                            <?php if ($consg['add1'] != '') { ?>

                                <tr><td colspan="2"><b><?php
                                            echo $party[add1];
                                        }
                                        ?></b></td></tr>

                            <?php if ($consg['add2'] != '') { ?>
                                <tr><td colspan="2"><b><?php
                                            echo $party['add2'];
                                        }
                                        ?></b></td></tr>
                            <?php if ($consg['add3'] != '') { ?>
                                <tr><td colspan="2"><b><?php
                                            echo $party['add3'];
                                        }
                                        ?></b></td></tr>
                            <?php if ($party['mobile'] != '') { ?>
                                <tr><td colspan="2">Telephone : <?php
                                        echo $party['mobile'] . "/" . $party['mobile2'];
                                    }
                                    ?></td></tr>
                            <?php if ($party['fax'] != '') { ?>
                                <tr style="border-bottom:1px solid black"><td colspan="2">Fax : <?php echo $party['fax'] ?></td></tr>
                            <?php } ?>

                            <tr>
                                <td width="50" style="border-right:1px solid black">
                                    <table>
                                        <tr><td>Country of Origin of Goods</td></tr>
                                        <tr><td><b><?php echo $fmain['portofdischarge'] ?></b></td></tr>
                                    </table>
                                </td>
                                <td width="50%">
                                    <table>
                                        <tr><td>Country of Final Destinations</td></tr>
                                        <tr><td><b><?php echo $fmain['portofloading'] ?></b></td></tr>
                                    </table>

                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            <table>
                <tr>
                    <td class="border" width="25%">
                        <table>
                            <tr>
                                <td style="border-bottom: 1px solid black">Vessel/ Flight No.<br><hr style="margin-bottom: 0px;margin-top:0px "><b><?php echo $fmain["vessel"] ?></b></td>
                            </tr>
                            <tr>
                                <td>Port Of Discharge : <b><?php echo $fmain["portofdischarge"] ?></b></td>
                            </tr>
                        </table>
                    </td>
                    <td class="border" width="25%">
                        <table>

                            <tr>
                                <td>Final Destination : <b><?php echo $fmain["finaldestination"]; ?></b></td>
                            </tr>
                        </table>
                    </td>
                    <td class="border" width="50%">
                        <table>
                            <tr><td>Term of Delivery And Payment.&nbsp;&nbsp;&nbsp;&nbsp;<b><?php echo $fmain["topay"]; ?></b></td></tr>
                            <tr><td><?php if ($fmain['canc_flag'] != '') {
                                ?>
                                        <img src="images/cancel.jpg" alt="Cancelled Bill" style="width:100%;height:100%">
                                    <?php } ?>
                                </td></tr>
                            <tr><td></td></tr>
                            <tr><td></td></tr>
                            <tr><td></td></tr>
                            <tr><td></td></tr>
                        </table>
                    </td>
                </tr>
            </table>

            <table class="border" style="width:100%;" style="border-collapse: collapse;">
                <tr>
                    <td style="width:50%;border:none">
                        <table class="border" style="width:100%;" style="border-collapse: collapse;">
                            <tr>
                                <td class="border" width="50%"><b>Marks & Nos. Container No.</b></td>
                                <td class="border" width="50%"><b>No. & Kind of Packaging</b></td>  
                            </tr>
                            <?php
                            $ctr = 0;
                            $totqty = 0;
                            $gdesp = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE vouchno='$fmain[vouchno]'") or die(mysqli_error($conn));
                            while ($fdesp = mysqli_fetch_array($gdesp)) {
                                $ctr++;
                                $marksnos = "marksnos" . "$ctr";
                                $kindpack = "kindpack" . "$ctr";

                                echo "<tr>";
                                echo "<td class='border'>$fdesp[$marksnos]</td>";
                                echo "<td class='border'>$fdesp[$kindpack]</td>";
                                echo "</tr>";
                            }
                            ?>
                        </table>
                    </td>
                    <td style="width:50%;">
                    </td>
                </tr>
            </table>
            <table class="border" style="width:100%;">
                <tr>
                    <td class="border" width="5%"><b>Sr.</b></td>
                    <td class="border" colspan="2" width="50%"><b>Description of Goods</b></td>
                    <td class="border" width="10%"><b>HSN Code</b></td>
                    <td class="border text-right" width="6%"><b>Dzn.Qty</b></td>
                    <td class="border text-right" width="6%"><b>Quantity</b></td>
                    <td class="border text-right" width="11%"><b>Rate</b></td>
                    <td class="border text-right" width="12%"><b>Amount</b></td>
                </tr>
                <?php
                $ctr = 0;
                $totqty = 0;
                $qry = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno='$fmain[vouchno]' ORDER BY srno") or die(mysqli_error($conn));
                while ($row = mysqli_fetch_array($qry)) {




                    $icode = $row['itemcode'];
                    $dzn = $row['dzn'];
                    $qty = $row['qty'];
                    $rate = $row['rate'];
                    $unit = $row['unit'];
                    $amount = $row['amount'];
                    $hsncode = $row['hsncode'];


                    $gitemname = mysqli_query($conn, "SELECT `code`,`name` FROM `product` WHERE `prodcode`='$row[itemcode]'")or die(mysqli_error($conn));
                    $fitemname = mysqli_fetch_array($gitemname);
                    $itemname = $fitemname['name'];

                    $ctr++;
                    echo "<tr>";
                    echo "<td class='border'>$ctr</td>";
                    echo "<td class='border' colspan='2'>$itemname</td>";
                    echo "<td class='border'>$hsncode</td>";
                    echo "<td class='border  text-right'>$dzn</td>";
                    echo "<td class='border  text-right'>$qty</td>";
                    echo "<td class='border text-right'>$rate</td>";
                    echo "<td class='border text-right'>$amount</td>";
                    echo "</tr>";

                    $totqty = $totqty + $dzn;
                    $totamt = $totamt + $amount;
                }
                ?>

                <tr>
                    <td class='border text-right' colspan='4'><b>Total</b></td>
                    <td class='border  text-right'><b><?php echo $totqty; ?></td>
                    <td class='border text-right'></td>
                    <td class='border text-right'></td>
                    <td class='border text-right'><b><?php echo $totamt; ?></b></td>
                </tr>
                <tr>
                    <td colspan="3" class="border">Net Weight : <?php echo $fmain['netweight'] ?></td>
                    <td colspan="5" class="border"></td>
                </tr>
                <tr>
                    <td colspan="3" class="border">Gross Weight : <?php echo $fmain['grossweight'] ?></td>
                    <td colspan="5" class=""></td>
                </tr>
                <tr>
                    <td colspan="3" class="border">Total CTNS : <?php echo $fmain['tctns'] ?></td>
                    <td colspan="5" class=""></td>
                </tr>
                <tr>
                    <td colspan="3" class="border">Dimension (IN CMS) : <?php echo $fmain['dimension'] ?></td>
                    <td colspan="5" class=""></td>
                </tr>
                <tr>
                    <td colspan="3" class="border">BL No. : <?php echo $fmain['blno'] ?></td>
                    <td colspan="5" class=""></td>
                </tr>
                <tr>
                    <td colspan="3" class="border">SB No. : <?php echo $fmain['sbno'] ?>&nbsp;DT : <?php echo $fmain[sbdate]; ?></td>
                    <td colspan="5" style="border-bottom: 1px solid black"></td>
                </tr>

                <tr style="border-bottom: 1px solid black">
                    <?php
                    if ($fmain['curr'] == 'R') {
                        $curr = 'Rupees';
                    } elseif ($fmain['curr'] == 'E') {
                        $curr = 'Euro';
                    } elseif ($fmain['curr'] == 'P') {
                        $curr = 'Pound';
                    } elseif ($fmain['curr'] == 'U') {
                        $curr = 'USD';
                    }
                    ?>
                    <td colspan="7">Amount Chargeable (in words) <b> : <?php echo $curr . ' ' . rsword($totamt) ?></b></td>
                    <td><?php ?></td>
                </tr>
                <tr>
                    <td colspan="4"><b>Declaration : </b>We declare that this invoice shows the actual price of the goods described and that all
                        Particulars are true and correct.</td>
                    <td colspan="4" class="border">Signature & Date :<br><br> </td>
                </tr>

            </table>


        </div>
    </div>

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