<?php

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

ini_set("display_errors", 1);
error_reporting(E_ERROR);

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$zyear = $myyear;
$vouch = $_GET['vouchno'];
$cond = '';
$lcond = '';

$instid = $_SESSION["instid"];
$accstkn = $_SESSION["accstkn"];

$formname = "Tax Invoice";
$maindb = 'salemain';
$trandb = 'saletran';

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

$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where `code`='$zcomp'") or die(mysqli_error($conn));
$getcmpname = mysqli_fetch_array($cmpname);
$init = $getcmpname['init'];
$invoice = $init . "/" . $zyear . "/" . $getdata["vouchno"];

$getparty = mysqli_query($conn, "SELECT * From accmast WHERE `accode` = '$getdata[party]'") or die(mysqli_error($conn));
$showparty = mysqli_fetch_array($getparty);

$getsalesm = mysqli_query($conn, "SELECT `accode`,`name` From accmast WHERE `accode` = '$getdata[salesman]'") or die(mysqli_error($conn));
$showsalesm = mysqli_fetch_array($getsalesm);

$gettransp = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` WHERE `code`= '$getdata[transporter]'") or die(mysqli_error($conn));
$ltrans = mysqli_fetch_array($gettransp);
$oms = $getdata['oms'];

require_once("library/TCPDF/tcpdf.php");

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(10, 0, 0); // LEFT, TOP, RIGHT

$pdf->AddPage();

$fs = 11;
$ht = 5;
$hln = 8;

$pdf->SetDrawColor(0, 0, 0);     // black border
$pdf->SetFillColor(255, 255, 255); // light blue fill// 

$pdf->Rect(10, 4, 190, 39, 'DF'); // Draw & Fill// LOGO

if (file_exists("images/logo.png")) {
    $pdf->Image("images/logo.png", 10, 5, 30, 30);
}

// COMPANY NAME
// $pdf->SetY(3);
// $pdf->SetFont('freeserif', 'B', 24);
// $pdf->Cell(40, 5, "", 0, 0, 'C');
// $pdf->Cell(0, 5, $getcmpname['compname'], 0, 1, 'C');// TAGLINE
// COMPANY NAME BACKGROUND
$pdf->SetY(5);

// Black background
$pdf->SetFillColor(0, 0, 0);

// Orange/Yellow text
$pdf->SetTextColor(255, 180, 0);

$pdf->SetFont('freeserif', 'B', 26);

// leave logo space
$pdf->Cell(70, 12, '', 0, 0);
$pdf->Cell(100, 12, strtoupper($getcmpname['compname']), 0, 0, 'C', true);
$pdf->Cell(20, 12, '', 0, 0);
$pdf->Ln();
// Reset text color to black for remaining content
$pdf->SetTextColor(0, 0, 0);

$pdf->SetFont('freeserif', 'B', 11);
$pdf->Cell(70, 5, "", 0, 0, 'L');
$pdf->Cell(100, 5, 'TRADER | WHOLESALER | MANUFACTURER', 0, 0, 'C');
$pdf->Cell(20, 5, '', 0, 0);

$pdf->Ln();

$pdf->Cell(70, 5, "", 0, 0, 'L');
$pdf->Cell(100, 5, 'ALL KINDS OF FOOTWEAR', 0, 0, 'C'); // ADDRESS
$pdf->Cell(20, 5, '', 0, 0);
$pdf->Ln();

$pdf->SetFont('freeserif', '', 10);
$address = $getcmpname['hoadd1'] . ' ' . $getcmpname['hoadd2'] . ' ' . $getcmpname['hoadd3'] . ' ' . $getcmpname['hocity'] . ' ' . $getcmpname['hopin'];

$pdf->Cell(70, 5, "", 0, 0, 'L');
$pdf->Cell(100, 4, $address, 0, 0, 'C'); // CONTACT
$pdf->Cell(20, 5, '', 0, 0);
$pdf->Ln();
$contact = 'Email : ' . $getcmpname['email'] . '    Contact : ' . $getcmpname['htel'] . '    GST : ' . $getcmpname['gstno'];

$pdf->Cell(70, 5, "", 0, 0, 'L');
$pdf->Cell(100, 4, $contact, 0, 0, 'C');
$pdf->Cell(20, 5, '', 0, 0);
$pdf->Ln();

// TITLE//
$pdf->SetFont('freeserif', 'B', 14);
$pdf->Cell(70, 5, "", 0, 0, 'C');
$pdf->Cell(100, 4, $formname, 0, 0, 'C');
$pdf->Cell(20, 5, '', 0, 0);
$pdf->Ln();

$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(90, 5, "Bill To Party", 1, 0, 'C');
$pdf->Cell(1, 6, "", 0, 0, 'L');
$pdf->Cell(99, 5, "Ship To Party", 1, 0, 'C');
$pdf->Ln(6);

if ($getdata["nameprint"] != '') {
    $printname = $getdata["nameprint"];
    $printmobile = $getdata["printmobile"];
} else {
    $printname = $showparty['name'];
    $printmobile = $showparty['mobile'];
}
if ($getdata["shipgstno"] == $showparty['gstno'] || $getdata['shipgstno'] == '') {
    $gstno = $showparty['gstno'];
} else {
    $gstno = $getdata['shipgstno'];
}

$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(14, 4, "M/s. ", 'LT', 0, 'L');
$pdf->Cell(2, 4, ":", 'T', 0, 'L');
$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(74, 4, "$printname", 'TR', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(14, 4, "M/s.", 'TL', 0, 'L');
$pdf->Cell(2, 4, ":", 'T', 0, 'L');
$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(83, 4, "$getdata[shipname]", 'TR', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('freeserif', 10);
$pdf->Cell(14, 4, "Address", 'L', 0, 'L');
$pdf->Cell(2, 4, ":", '0', 0, 'L');
$pdf->SetFont('freeserif', 9);
$pdf->Cell(74, 4, "$showparty[add1]", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('freeserif', 10);
$pdf->Cell(14, 4, "Address", 0, 0, 'L');
$pdf->Cell(2, 4, ":", 0, 0, 'L');
$pdf->SetFont('freeserif', 9);
$pdf->Cell(83, 4, "$getdata[shipadd1]", 'R', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('freeserif', 10);
$pdf->Cell(16, 4, "", 'L', 0, 'L');
$pdf->SetFont('freeserif', 9);
$pdf->Cell(74, 4, "$showparty[add2]", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('freeserif', 10);
$pdf->Cell(16, 4, "", 0, 0, 'L');
$pdf->SetFont('freeserif', 9);
$pdf->Cell(83, 4, "$getdata[shipadd2]", 'R', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('freeserif', 10);
$pdf->Cell(16, 4, "", 'L', 0, 'L');
$pdf->SetFont('freeserif', 9);
$pdf->Cell(74, 4, "$showparty[add3] $showparty[city] $showparty[pin]", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('freeserif', 10);
$pdf->Cell(16, 4, "", 0, 0, 'L');
$pdf->SetFont('freeserif', 9);
$pdf->Cell(83, 4, "$getdata[shipadd3]", 'R', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('freeserif', 10);
$pdf->Cell(14, 4, "Mobile", 'L', 0, 'L');
$pdf->Cell(2, 4, ":", '0', 0, 'L');
$pdf->SetFont('freeserif', '', 10);
$pdf->Cell(74, 4, "$printmobile", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('freeserif', 10);
$pdf->Cell(14, 4, "Mobile", 'L', 0, 'L');
$pdf->Cell(2, 4, ":", '0', 0, 'L');
$pdf->SetFont('freeserif', '', 10);
$pdf->Cell(83, 4, "$printmobile", 'R', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('freeserif', 10);
$pdf->Cell(14, 4, "GST No.", 'L', 0, 'L');
$pdf->Cell(2, 4, ":", '0', 0, 'L');
$pdf->SetFont('freeserif', 'B', 9);
$pdf->Cell(74, 4, "$showparty[gstno]", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('freeserif', 10);
$pdf->Cell(14, 4, "GST No.", 'L', 0, 'L');
$pdf->Cell(2, 4, ":", '0', 0, 'L');
$pdf->SetFont('freeserif', 'B', 9);
$pdf->Cell(83, 4, "$gstno", 'R', 0, 'L');
$pdf->Ln(4);

if (strlen($showparty['pan']) < 12) {
    $adpan = "PAN";
} else {
    $adpan = "AADHAAR";
}

$pdf->SetFont('freeserif', 10);
$pdf->Cell(14, 4, "$adpan", 'L', 0, 'L');
$pdf->Cell(2, 4, ":", '0', 0, 'L');
$pdf->SetFont('freeserif', 'B', 9);
$pdf->Cell(74, 4, "$showparty[pan]", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('freeserif', 10);
$pdf->Cell(14, 4, "PAN", 'L', 0, 'L');
$pdf->Cell(2, 4, ":", '0', 0, 'L');
$pdf->SetFont('freeserif', 'B', 9);
$pdf->Cell(83, 4, "", 'R', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(14, 4, "State", 'LB', 0, 'L');
$pdf->Cell(2, 4, ":", 'B', 0, 'L');
$pdf->SetFont('freeserif', '', 10);
$pdf->Cell(34, 4, "$showparty[state]", 'B', 0, 'L');
$pdf->Cell(40, 4, "Code : $showparty[statecode]", 'RB', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(14, 4, "State", 'LB', 0, 'L');
$pdf->Cell(2, 4, ":", 'B', 0, 'L');
$pdf->SetFont('freeserif', '', 10);
$pdf->Cell(34, 4, "$showparty[state]", 'B', 0, 'L');
$pdf->Cell(49, 4, "Code : $showparty[statecode]", 'RB', 0, 'L');
$pdf->Ln(5);

$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(20, 5, "Invoice No.", 'L', 0, 'L');
$pdf->Cell(1, 5, ":", 0, 0, 'L');
$pdf->SetFont('freeserif', '', 10);
$pdf->Cell(69, 5, " $invoice", 'R', 0, 'L');
$pdf->Cell(1, 5, "", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(28, 5, "E-way Bill No.", 'L', 0, 'L');
$pdf->Cell(1, 5, ":", 0, 0, 'L');
$pdf->SetFont('freeserif', '', 10);
$pdf->Cell(70, 5, "$getdata[ewaybill]", 'R', 0, 'L');
$pdf->Ln(5);

$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(20, 5, "Date", 'L', 0, 'L');
$pdf->Cell(1, 5, ":", 0, 0, 'L');
$pdf->SetFont('freeserif', '', 10);
$pdf->Cell(69, 5, " $getdata[vouchdt]", 'R', 0, 'L');
$pdf->Cell(1, 5, "", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(28, 5, "Transporter", 'L', 0, 'L');
$pdf->Cell(1, 5, ":", 0, 0, 'L');
$pdf->SetFont('freeserif', '', 10);
$pdf->Cell(70, 5, "$ltrans[name]", 'R', 0, 'L');
$pdf->Ln(5);

if ($getdata['cashcred'] == "R") {
    $paytype = "Credit";
} else {
    $paytype = "Cash";
}
if ($getdata['area'] != "") {
    $pdf->SetFont('freeserif', 'B', 10);
    $pdf->Cell(20, 5, "Booking", 'L', 0, 'L');
    $pdf->Cell(1, 5, ":", 0, 0, 'L');
    $pdf->SetFont('freeserif', '', 10);
    $pdf->Cell(69, 5, " $getdata[area]  Paid By : $paytype", 'R', 0, 'L');
} else {
    $pdf->SetFont('freeserif', 'B', 10);
    $pdf->Cell(20, 5, "Paid By", 'L', 0, 'L');
    $pdf->Cell(1, 5, ":", 0, 0, 'L');
    $pdf->SetFont('freeserif', '', 10);
    $pdf->Cell(69, 5, " $paytype", 'R', 0, 'L');
}

$pdf->Cell(1, 5, "", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(28, 5, "Terms of Delivery", 'L', 0, 'L');
$pdf->Cell(1, 5, ":", 0, 0, 'L');
$pdf->SetFont('freeserif', '', 10);
$pdf->Cell(70, 5, "$getdata[remark1] $getdata[remark2]", 'R', 0, 'L');
$pdf->Ln(5);

$w = [5, 68, 18, 12, 15, 15, 20, 17, 20];
$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell($w[0], 5, "Sr.", 'BLT', 0, 'C');
$pdf->Cell($w[1], 5, "Item", 'LBT', 0, 'C');
$pdf->Cell($w[2], 5, "HSN Code", 'BLRT', 0, 'C');
$pdf->Cell($w[3], 5, "GST%", 'BRT', 0, 'R');
$pdf->Cell($w[4], 5, "Qty.", 'BRT', 0, 'R');
$pdf->Cell($w[5], 5, "Rate", 'BRT', 0, 'R');
$pdf->Cell($w[6], 5, "Amount", 'BRT', 0, 'R');
$pdf->Cell($w[7], 5, "Discount", 'BRT', 0, 'R');
$pdf->Cell($w[8], 5, "Taxable", 'BRT', 0, 'R');
$pdf->Ln(5);

$ctr = 0;
$ctab = 0;
$totqty = 0;
$gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$vouch' order by srno") or die(mysqli_error($conn));
while ($showtran = mysqli_fetch_array($gettran)) {
    $getitem = mysqli_query($conn, "SELECT `name`,`code` From `product` WHERE `code` = $showtran[item] ") or die(mysqli_error($conn));
    $showitem = mysqli_fetch_array($getitem);
    $gstperc = $showtran['cgstrate'] * 2;
    $gstamt = $showtran['cgstamt'] * 2;
    $nameprint = $showitem["nameprint"];
    $name = $showitem["name"];
    if ($nameprint != '') {
        $itemname = $nameprint;
    } else {
        $itemname = $name;
    }
    if ($showtran['igstrate'] > 0) {
        $gst = $showtran['igstrate'];
    } else {
        $gst = $gstperc;
    }
    $totqty = $totqty + $showtran['qty'];
    $ctr++;
    $ctab = $ctab + 1;
    $pdf->SetFont('freeserif', '', 10);
    $pdf->Cell($w[0], 6, "$showtran[srno]", 'BLT', 0, 'C');
    $pdf->Cell($w[1], 6, "$itemname", 'LBT', 0, 'L');
    $pdf->Cell($w[2], 6, "$showtran[hsncode]", 'BLRT', 0, 'C');
    $pdf->Cell($w[3], 6, "$gst", 'BRT', 0, 'R');
    $pdf->Cell($w[4], 6, "$showtran[qty]", 'BRT', 0, 'R');
    $pdf->Cell($w[5], 6, "$showtran[rate]", 'BRT', 0, 'R');
    $pdf->Cell($w[6], 6, "$showtran[amount]", 'BRT', 0, 'R');
    $pdf->Cell($w[7], 6, "$showtran[discount]", 'BRT', 0, 'R');
    $pdf->Cell($w[8], 6, "$showtran[subtot]", 'BRT', 0, 'R');
    $pdf->Ln();
}
$newctr = 12 - $a;
for ($i = 1; $i <= $newctr; $i++) {
    $pdf->Cell($w[0], 6, "", 'BLT', 0, 'C');
    $pdf->Cell($w[1], 6, "", 'LBT', 0, 'C');
    $pdf->Cell($w[2], 6, "", 'BLRT', 0, 'C');
    $pdf->Cell($w[3], 6, "", 'BRT', 0, 'R');
    $pdf->Cell($w[4], 6, "", 'BRT', 0, 'R');
    $pdf->Cell($w[5], 6, "", 'BRT', 0, 'R');
    $pdf->Cell($w[6], 6, "", 'BRT', 0, 'R');
    $pdf->Cell($w[7], 6, "", 'BRT', 0, 'R');
    $pdf->Cell($w[8], 6, "", 'BRT', 0, 'R');
    $pdf->Ln();
}
$pdf->SetFont('freeserif', '', 10);
$pdf->Cell(103, 5, "Total", 'BL', 0, 'R');
$pdf->Cell(15, 5, "$totqty", 'B', 0, 'R');
$pdf->Cell(35, 5, "$getdata[subtotal]", 'BR', 0, 'R');
$pdf->Cell(17, 5, "$getdata[litemdisc]", 'BR', 0, 'R');
$pdf->Cell(20, 5, "$getdata[tottaxamt]", 'BR', 0, 'R');
$pdf->Ln();
$h = 4;
$h = 4; // ===============================// GST VALUE FUNCTION// ===============================

function gstVal($val) {
    return ($val > 0) ? number_format($val, 2) : "";
}

// ===============================// HEADER ROW// ===============================
$pdf->Cell(27, $h, "A/c Name", 1, 0, "L");
$pdf->Cell(64, $h, $getcmpname['compname'], 1, 0, "L");
$pdf->Cell(19, $h, "%", 1, 0, 'R');
$pdf->Cell(20, $h, "CGST", 1, 0, 'R');
$pdf->Cell(20, $h, "SGST", 1, 0, 'R');
$pdf->Cell(20, $h, "IGST", 1, 0, 'R');
$pdf->Cell(20, $h, "", 'BTR', 0, 'R');
$pdf->Ln();

// ===============================// 5%// ===============================
$pdf->Cell(27, $h, "Bank Name", 1, 0, "L");
$pdf->Cell(64, $h, $getcmpname['bankname'], 1, 0, "L");
$pdf->Cell(19, $h, "5", 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['cgst5']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['sgst5']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['igst5']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['totgst5']), 'BTR', 0, 'R');
$pdf->Ln();

// ===============================// 12%// ===============================
$pdf->Cell(27, $h, "Branch Name", 1, 0, "L");
$pdf->Cell(64, $h, $getcmpname['branchname'], 1, 0, "L");
$pdf->Cell(19, $h, "12", 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['cgst12']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['sgst12']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['igst12']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['totgst12']), 'BTR', 0, 'R');
$pdf->Ln();

// ===============================// 18%// ===============================
$pdf->Cell(27, $h, "Account No.", 1, 0, "L");
$pdf->Cell(64, $h, $getcmpname['acno'], 1, 0, "L");
$pdf->Cell(19, $h, "18", 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['cgst18']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['sgst18']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['igst18']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['totgst18']), 'BTR', 0, 'R');
$pdf->Ln();

// ===============================// 28%// ===============================
$pdf->Cell(27, $h, "IFSC Code", 1, 0, "L");
$pdf->Cell(64, $h, $getcmpname['ifccode'], 1, 0, "L");
$pdf->Cell(19, $h, "28", 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['cgst28']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['sgst28']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['igst28']), 1, 0, 'R');
$pdf->Cell(20, $h, gstVal($getdata['totgst28']), 'BTR', 0, 'R');
$pdf->Ln();

$h = 5;

$amountinwords = rsword(round($getdata['netamt']));
$pdf->SetFont('freeserif', '', 10);
$pdf->Cell(130, $h, "Rupees " . $amountinwords, 'BLR', 0, 'L');
$pdf->SetFont('freeserif', 'B', 11);
$pdf->Cell(24, $h, "Bill Amount", 'TBL', 0, 'L');
$pdf->Cell(36, $h, number_format($getdata['netamt'], 2), 'TBR', 0, 'R');
$pdf->Ln();

$h = 4;         // row height
$pdf->SetFont('freeserif', 'B', 10);
$pdf->Cell(130, $h, "", 'LRT', 0, 'L');
$pdf->Cell(60, $h, "For  $getcmpname[compname]", 'RT', 0, 'C');
$pdf->Ln();

$pdf->SetFont('freeserif', '', 9);
$pdf->Cell(130, $h, " I / We hereby certify that my/our registration certificate under Rule 1 of GST invoice rules 2017 is", 'LR', 0, 'L');
$pdf->Cell(60, $h, "", 'R', 0, 'R');
$pdf->Ln();

$pdf->Cell(130, $h, " in force on which the sale of the goods specified in this tax invoice is made by me/us and that the", 'LR', 0, 'L');
$pdf->Cell(60, $h, "", 'R', 0, 'R');
$pdf->Ln();

$pdf->Cell(130, $h, " transaction of sale covered by this tax invoice has been effected by me / us and it shall be accounted", 'LR', 0, 'L');
$pdf->Cell(60, $h, "", 'R', 0, 'R');
$pdf->Ln();

$pdf->Cell(130, $h, " of in the turnover of sales while filling of return and the due tax, if any, payable on the sale has been", 'LR', 0, 'L');
$pdf->Cell(60, $h, "", 'R', 0, 'R');
$pdf->Ln();

$pdf->Cell(130, $h, " paid or shall be paid.", 'LR', 0, 'L');
$pdf->Cell(60, $h, "", 'R', 0, 'R');
$pdf->Ln();

$pdf->SetFont('freeserif', 'U', 9);
$pdf->Cell(130, 10, "Subject To Mumbai Jurisdiction", 'LRB', 0, 'L');
$pdf->SetFont('freeserif', '', 9);
$pdf->Cell(60, 10, "AUTHORIZED SIGNATORY", 'RB', 0, 'C');
$pdf->Ln();

ob_end_clean();

$para = $_REQUEST["para"];
//browse($para);

if ($para == "") {
    $pdf->Output("GSTBILL-" . $getdata['vouchno'] . ".pdf", "I");
} else {
    $myyear = $_SESSION[$myyear];

    $nomFacture = $_SERVER['DOCUMENT_ROOT'] . "/admin/gstbillpdf/$myyear/" . $getmdata['vouchno'] . ".pdf";
    clearstatcache();

    $urlsend = "https://mysoft365.com/sisgstnew/admin/gstbillpdf/$myyear/" . $getmdata['vouchno'] . ".pdf?v=" . time();

// Delete old PDF if it exists
    if (file_exists($nomFacture)) {
        @unlink($nomFacture);
    }

// Generate new PDF
    $pdf->Output($nomFacture, "F");

    $filevoucher = "GSTBILL-" . $getdata['vouchno'];
    $filename = "GSTBILL-" . $getdata['vouchno'] . ".pdf";

    $mobileno = $printmobile;

    // ALL WHATSAPP    
    // $mobileno = '8652372200'; // TEST NUMBER

    if ($mobileno != '') {
        $msgtech = urlencode($urlsend);
        // ✅ Show alert + close page after 1 second       
        echo "<script>             
                alert('Message Send to Customer');    
                setTimeout(function() {          
                window.close();     
                }, 1000); // 1 second delay    
            </script>";
        // Flush response so browser processes alert/close       

        ignore_user_abort(true);
        set_time_limit(0);

        if (function_exists('fastcgi_finish_request')) {
            fastcgi_finish_request();
        } else {
            ob_end_flush();
            flush();
        }
        $apiUrl = "https://web.secureleads.in/api/send?" . http_build_query([
                    'number' => '91' . $mobileno,
                    'type' => 'media',
                    'message' => $filevoucher,
                    'media_url' => $urlsend,
                    'filename' => $filename,
                    'instance_id' => $instid,
                    'access_token' => $accstkn
        ]);

        // Log URL for debugging
        file_put_contents(
                "/tmp/whatsapp_url.log",
                date('Y-m-d H:i:s') . " => " . $apiUrl . PHP_EOL,
                FILE_APPEND
        );

        // Response log file
        $logFile = "/tmp/whatsapp_send_Bill_" . $getmdata['vouchno'] . ".log";

        // Background CURL
        $cmd = sprintf(
                "curl -s -X POST %s -H %s > %s 2>&1 &",
                escapeshellarg($apiUrl),
                escapeshellarg("Cookie: stackpost_session=m1ip03t51dk5rlgaa5blakqmva57k3nd"),
                escapeshellarg($logFile)
        );

        exec($cmd);
    } else {
        echo "<script>     
                   alert('No. Mobile no. Found');                
                   window.close();              
                   </script>";
    }
}
?>