<?php
ini_set('display_errors', 1);
error_reporting(E_ERROR);

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];
$success = false;
$zyear = right($zdb, 4);
$compcd = $_SESSION["comp_" . $dbname];

//$para = $_REQUEST['para'];

//$series = $_GET["series"];
$zroot = $_SESSION["zdir"];
$date = date("Y-m-d");
$ewayno = ($_REQUEST["ewayno"]) ? $_REQUEST["ewayno"] : "";
//$result = mysqli_query($conn, "SELECT compmast FROM `invmain` WHERE ewaybillno='$ewayno'") or die(mysqli_error($conn));
//$fetr = mysqli_fetch_array($result);

$actcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code = '$compcd'") or die(mysqli_error($conn));
$compn = mysqli_fetch_array($actcomp);
$ginv = $tinv = $cinv = "";
$gstno = $compn['gst'];
//browse($gstno);
/* ================= API CALL ================= */
//browse($ewayno);
$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://ewayapi.mygstcafe.com/managed/v1.03/GetEwayBill?ewbNo=$ewayno",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array(
        "GSTIN:$gstno",
        "CustomerName:$compn[pname]",
        "Branch: $compn[city]",
        "Username: $compn[apiuser]",
        "Password: $compn[apipass]",
        'CustomerId: ASP10142',
        'APIId: eqGWltJk-PG1V-fcFG-UrED-RBJOgxRe',
        'APISecret: eqGWltJkPG1VfcFG',
        'Source: API',
        'Environment-type: Production'
    ),
));

$response = curl_exec($curl);
curl_close($curl);
print_r($response);
$data = json_decode($response, true);
$d = $data['data'];

/* ================= QR ================= */
$qr = "https://api.qrserver.com/v1/create-qr-code/?size=130x130&data=" . $d['ewbNo'] . "/" . $d['fromGstin'] . "/" . $d['ewayBillDate'];

/* ================= BARCODE ================= */
$barcode = "https://barcode.tec-it.com/barcode.ashx?data=" . $d['ewbNo'] . "&code=Code128&dpi=96";
?>

<!DOCTYPE html>
<html>
    <head>
        <title>E-Way Bill</title>

        <style>
            body {
                font-family: Arial, sans-serif;
                font-size: 12px;
                margin: 0;
            }

            .container {
                width: 800px;
                margin: auto;
                border: 1px solid #000;
            }

            .title {
                text-align: center;
                font-size: 22px;
                font-weight: bold;
                padding: 10px;
            }

            .qr {
                text-align: center;
                border-top: 1px solid #000;
                border-bottom: 1px solid #000;
                padding: 10px;
            }

            table {
                width: 100%;
                border-collapse: collapse;
            }

            td, th {
                border: 1px solid #000;
                padding: 6px;
            }

            .label {
                width: 35%;
                font-weight: bold;
                background: #f5f5f5;
            }

            .section {
                background: #eaeaea;
                font-weight: bold;
            }

            .center {
                text-align: center;
            }

            .barcode {
                text-align: center;
                padding: 15px;
            }

            .note {
                font-size: 10px;
                padding: 5px;
            }

            @media print {
                body {
                    margin: 0;
                }
                .container {
                    border: none;
                }
            }
        </style>
    </head>

    <body onload="window.print()">

        <div class="container">

            <div class="title">e-Way Bill</div>

            <div class="qr">
                <img src="<?= $qr ?>"><br>
            </div>

            <table>
                <tr>
                    <td class="label">E-Way Bill No:</td>
                    <td><?= $d['ewbNo'] ?></td>
                </tr>
                <tr>
                    <td class="label">E-Way Bill Date:</td>
                    <td><?= $d['ewayBillDate'] ?></td>
                </tr>
                <tr>
                    <td class="label">Generated By:</td>
                    <td><?= $d['fromGstin'] ?> - <?= $d['fromTrdName'] ?></td>
                </tr>
                <tr>
                    <td class="label">Valid From:</td>
                    <td><?= $d['ewayBillDate'] ?> [<?= $d['actualDist'] ?> Kms]</td>
                </tr>
                <tr>
                    <td class="label">Valid Until:</td>
                    <td><?= ($d['validUpto'] ?: 'Not Available') ?></td>
                </tr>
                <tr>
                    <td class="label">Portal:</td>
                    <td>1</td>
                </tr>
            </table>

            <!-- PART A -->
            <table>
                <tr><td colspan="2" class="section">Part - A</td></tr>

                <tr>
                    <td class="label">GSTIN of Supplier</td>
                    <td><?= $d['fromGstin'] ?>, <?= $d['fromTrdName'] ?></td>
                </tr>

                <tr>
                    <td class="label">Place of Dispatch</td>
                    <td><?= $d['fromPlace'] ?> - <?= $d['fromPincode'] ?></td>
                </tr>

                <tr>
                    <td class="label">GSTIN of Recipient</td>
                    <td><?= $d['toGstin'] ?>, <?= $d['toTrdName'] ?></td>
                </tr>

                <tr>
                    <td class="label">Place of Delivery</td>
                    <td><?= $d['toPlace'] ?> - <?= $d['toPincode'] ?></td>
                </tr>

                <tr>
                    <td class="label">Document No.</td>
                    <td><?= $d['docNo'] ?></td>
                </tr>

                <tr>
                    <td class="label">Document Date</td>
                    <td><?= $d['docDate'] ?></td>
                </tr>

                <tr>
                    <td class="label">Transaction Type</td>
                    <td><?= $d['transactionType'] ?></td>
                </tr>

                <tr>
                    <td class="label">Value of Goods</td>
                    <td><?= number_format($d['totalValue'], 2) ?></td>
                </tr>

                <tr>
                    <td class="label">Tax (CGST + SGST)</td>
                    <td><?= number_format($d['cgstValue'] + $d['sgstValue'], 2) ?></td>
                </tr>

                <tr>
                    <td class="label">Transporter</td>
                    <td><?= $d['transporterName'] ?> (<?= $d['transporterId'] ?>)</td>
                </tr>
            </table>

            <?php
            $vehicleNo = '-';
            $transMode = 'Road';

            if (!empty($d['VehiclListDetails'])) {

                $veh = $d['VehiclListDetails'][0];

                $vehicleNo = $veh['vehicleNo'];

                if ($veh['transMode'] == "1") {
                    $transMode = "Road";
                } elseif ($veh['transMode'] == "2") {
                    $transMode = "Rail";
                } elseif ($veh['transMode'] == "3") {
                    $transMode = "Air";
                } elseif ($veh['transMode'] == "4") {
                    $transMode = "Ship";
                }
            }
            ?>
            <!-- PART B -->
            <table>
                <tr><td colspan="8" class="section">Part - B</td></tr>

                <tr class="center">
                    <th>Mode</th>
                    <th>Vehicle / Trans Doc No & Dt.</th>
                    <th>From</th>
                    <th>Entered Date</th>
                    <th>Entered By</th>
                    <th>CEWB No</th>
                    <th>Multi Veh.Info</th>
                    <th>Portal</th>
                </tr>

                <tr class="center">
                    <!--<td>Road</td>-->
                    <!--<td>$d['vehicleNo']</td>-->
                    <td><?= $transMode ?></td>
                    <td><?= $vehicleNo ?></td>
                    <td><?= $d['fromPlace'] ?></td>
                    <td><?= $d['ewayBillDate'] ?></td>
                    <td><?= $d['fromGstin'] ?></td>
                    <td>-</td>
                    <td>-</td>
                    <td>1</td>
                </tr>
            </table>

            <!-- BARCODE -->
            <div class="barcode">
                <img src="<?= $barcode ?>"><br>
                <?= $d['ewbNo'] ?>
            </div>

            <div class="note">
                Note: If any discrepancy in information please try after sometimes.
            </div>

        </div>

    </body>
</html>