<?php

require("library/fpdf/fpdf.php");

class pdf extends fpdf
{

    function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }
}

$ln = 0;
$pagerow = 25;
$pdf = new pdf('P', 'mm', 'A4');
$pdf->AliasNbPages();
$pdf->AddPage();

$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];
$getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_error($conn));
$comp = mysqli_fetch_array($getcomp);

$pdf->SetFont('Arial', 'B', 12);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFillColor(152, 249, 240);

$addy = $_REQUEST["dtype"];
$mfdt = $_REQUEST["frmdt"];
$mtdt = $_REQUEST["todt"];

if ($addy == 1) {
    $rtype = "Summary";
} elseif ($addy == 2) {
    $rtype = "Detail";
} elseif ($addy == '0') {
    $rtype = 'Monthly Summary';
}

$pdf->Cell(190, 6, strtoupper($comp['compname']), 'LRT', 0, 'L', 1);
$pdf->Ln();
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(190, 6, $comp['hoadd1'] . $comp['hoadd2'] . $comp['hoadd3'], 'LR', 0, '', 1);
$pdf->Ln();
$pdf->SetTextColor(255, 0, 0);
$pdf->Cell(120, 6, "$formname - " . $rtype, 'L', 0, '', 1);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(70, 6, "Report Date : " . Date("d-m-Y"), 'R', 0, 'R', 1);
$pdf->Ln();
$pdf->Cell(190, 6, 'From Date -' . format_date($mfdt) . "To Date -" . format_date($mtdt) , 'LRB', 0, '', 1);
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 9);
$pdf->SetFillColor(255, 255, 255);
$ht = 6;

////////////////////////////////  PDF  SUMMARRY PART 1221223     /////////////////////////////////////

if ($dtype < 2) {
    $pdf->SetFillColor(158, 135, 89);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(70, $ht, 'Account Name', 1, 0, '', 1);
    $pdf->Cell(30, $ht, 'Opening', 1, 0, 'R', 1);
    $pdf->Cell(30, $ht, 'Debit', 1, 0, 'R', 1);
    $pdf->Cell(30, $ht, 'Credit', 1, 0, 'R', 1);
    $pdf->Cell(30, $ht, 'Balance', 1, 0, 'R', 1);
    $pdf->ln($ht);
}


if ($dtype < 2) {
    mysqli_select_db($conn, $zdb);
    $totbal = $totobal = $totdeb = $totcrd = 0;

    $ctrsum = 0;
    while ($getac = mysqli_fetch_array($acc)) {
        if ($dtype < 2) {
            $maccode = $getac["accode"];
            $acname = $getac["name"];
            $opamt = $getac["opamt"];
            $dramt = $getac["dramt"];
            $cramt = $getac["cramt"];
            $blamt = $getac["blamt"];

            $mopbal = $getac["openbal"];
            $mopdc = $getac["dc"];
            if ($mopdc == "D") {
                $mopbal = $mopbal * -1;
            }

            ############PARTY NAME####################

            $obal = $mopbal + $opamt;
            if ($obal < 0) {
                $odc = "DR";
            } else {
                $odc = "CR";
            }

            $tramtdr = $dramt;
            $tramtdr = abs($tramtdr);

            $tramtcr = $cramt;
            $tramtcr = abs($tramtcr);

            $mbal = $obal - $tramtdr + $tramtcr;

            if ($mbal < 0) {
                $mdc = "DR";
            } else {
                $mdc = "CR";
            }

            $show = 1;
            if ($prnzero == 1) {
                $show = 1;
            } else {
                if ($mbal != 0) {
                    $show = 1;
                } else {
                    $show = 0;
                }
            }

            $obal1 = abs($obal);
            $mbal1 = abs($mbal);

            if ($show == 1) {

                $skacname = str_replace(",", " ", $acname);

                $skmbal1 = round($mbal1, 2);

                if ($dtype == 0) {
                    $colr = "0, 0, 255";
                } else if ($dtype == 2) {
                    $colr = "0, 0, 0";
                }
                $colorArray = explode(',', $colr);
                $pdf->SetTextColor($colorArray[0], $colorArray[1], $colorArray[2]);
                $pdf->SetFillColor(255, 255, 255);

                $pdf->SetFont('Arial', 'B', 9);
                $pdf->Cell(70, $ht, $acname, 1, 0, 'L', 1);
                $pdf->Cell(30, $ht, $obal1 . " " . $odc, 1, 0, 'R', 1);
                $pdf->Cell(30, $ht, $tramtdr, 1, 0, 'R', 1);
                $pdf->Cell(30, $ht, $tramtcr, 1, 0, 'R', 1);

                $pdf->setFillColor(152, 249, 240);
                $pdf->setTextColor(0, 0, 0);
                $pdf->Cell(30, $ht, $skmbal1 . " " . $mdc, 1, 0, 'R', 1);
                $pdf->ln($ht);
                $pdf->setFillColor(255, 255, 255);
                $pdf->setTextColor(0, 0, 255);

                $totobal = $totobal + $obal;

                $totdeb = $totdeb + $tramtdr;
                $totcrd = $totcrd + $tramtcr;

                $totbal = $totbal + $mbal;
            }

            if ($totbal < 0) {
                $mdctot = "DR";
            } else {
                $mdctot = "CR";
            }

            if ($dtype == 0) {
                mysqli_query($conn, "drop temporary table if exists mondata") or die(mysqli_error($conn));
                mysqli_query($conn, "create temporary table mondata SELECT *, month(date) as mon  FROM `$trandb` WHERE 
                                                    `accode`='$maccode' and date >= '$mfrmdate' and date <= '$mtodate' and retdate = '0000-00-00'") or die(mysqli_error($conn));

                $getmon = mysqli_query($conn, "SELECT SUM(amount) as monamt, date, mon  FROM `mondata` group by mon order by date") or die(mysqli_error($conn));
                if (mysqli_num_rows($getmon) > 0) {


                    $pdf->setFillColor(153, 153, 153);
                    $pdf->setTextColor(255, 255, 255);

                    $pdf->Cell(70, $ht, 'Month', 1, 0, '', 1);
                    $pdf->Cell(30, $ht, 'Opening', 1, 0, 'R', 1);
                    $pdf->Cell(30, $ht, 'Debit', 1, 0, 'R', 1);
                    $pdf->Cell(30, $ht, 'Credit', 1, 0, 'R', 1);
                    $pdf->Cell(30, $ht, 'Balance', 1, 0, 'R', 1);
                    $pdf->ln($ht);

                    $pdf->setFillColor(255, 255, 255);
                    $pdf->setTextColor(0, 0, 0);

                    while ($fetmon = mysqli_fetch_array($getmon)) {
                        $mon = $fetmon['mon'];
                        $monamt = $fetmon['monamt'];

                        if ($mon < 10) {
                            $mond = '0' . $mon;
                        } else {
                            $mond = $mon;
                        }

                        $monopn = 0;

                        if ($mon < 4) {
                            $date1 = $zeyear . '-' . $mond . '-01';
                        } else {
                            $date1 = $zsyear . '-' . $mond . '-01';
                        }

                        $getmn = mysqli_query($conn, "SELECT descrip  FROM $zroot.`months` WHERE `code`='$mon' ") or die(mysqli_error($conn));
                        $fetmn = mysqli_fetch_array($getmn);
                        $monname = $fetmn['descrip'];

                        $getopn = mysqli_query($conn, "SELECT SUM(amount) as oamt FROM `mondata` WHERE date < '$date1' ") or die(mysqli_error($conn));
                        $fetopn = mysqli_fetch_array($getopn);
                        $opnamt = $fetopn["oamt"];
                        $monopn = $mopbal + $opnamt;

                        if ($monopn < 0) {
                            $mdco = "DR";
                        } else {
                            $mdco = "CR";
                        }

                        $getdeb = mysqli_query($conn, "SELECT SUM(amount) as mondeb FROM `mondata` WHERE amount < 0 and 
                                                       month(date) = '$mon' ") or die(mysqli_error($conn));
                        $fetdeb = mysqli_fetch_array($getdeb);
                        $mondeb = $fetdeb['mondeb'];

                        $getcrd = mysqli_query($conn, "SELECT SUM(amount) as moncrd FROM `mondata` WHERE amount > 0 and 
                                                       month(date) = '$mon' ") or die(mysqli_error($conn));
                        $fetcrd = mysqli_fetch_array($getcrd);
                        $moncrd = $fetcrd['moncrd'];

                        $monbal = $monopn + $mondeb + $moncrd;

                        if ($monbal < 0) {
                            $mdcmon = "DR";
                        } else {
                            $mdcmon = "CR";
                        }

                        $monopn1 = abs($monopn);
                        $mondeb1 = abs($mondeb);
                        $moncrd1 = abs($moncrd);
                        $monbal1 = abs($monbal);

                        $pdf->Cell(70, $ht, $monname, 1, 0, '', 1);
                        $pdf->Cell(30, $ht, $monopn1 . " " . $mdco, 1, 0, 'R', 1);
                        $pdf->Cell(30, $ht, $mondeb1, 1, 0, 'R', 1);
                        $pdf->Cell(30, $ht, $moncrd1, 1, 0, 'R', 1);
                        $pdf->Cell(30, $ht, $monbal1 . " " . $mdcmon, 1, 0, 'R', 1);
                        $pdf->ln();
                    }
                    $pdf->Cell(100, $ht, " ", 1, 0, '', 1);
                    $pdf->Cell(90, $ht, " ", 1, 0, '', 1);
                    $pdf->ln();
                }
            }
        }
    }
    $totobals = $totobal;
    if ($totobals < 0) {
        $todc = 'DR';
    } else {
        $todc = 'CR';
    }

    $ntotobals = abs($totobals);

    if ($totbal < 0) {
        $tbdc = 'DR';
    } else {
        $tbdc = 'CR';
    }
    $ntotbals = abs($totbal);
    $pdf->Cell(70, $ht, "Total", 1, 0, 'R', 1);
    $pdf->setTextcolor(255, 0, 0);
    $pdf->Cell(30, $ht, $ntotobals . " " . $todc, 1, 0, 'R', 1);
    $pdf->Cell(30, $ht, $totdeb, 1, 0, 'R', 1);
    $pdf->Cell(30, $ht, $totcrd, 1, 0, 'R', 1);
    $pdf->Cell(30, $ht, $ntotbals . " " . $tbdc, 1, 0, 'R', 1);
    $pdf->setTextcolor(0, 0, 0);
}



///////////////////  PDF  DETAIL  PART   //////////////////////////// 999999999999999999999

if ($dtype == 2) {
    $taccode = -1;
    $ftime = 0;

    while ($getac = mysqli_fetch_array($acc)) {


        $blamt = $getac["blamt"];

        if ($prnzero == 1) {

        } else {
            if ($blamt == 0) {
                continue;
            }
        }
        $vdate = $getac["mydt"];
        $msource = $getac["source"];
        $rt = $getac["rt"];
        $mbankinit = $getac["bankinit"];
        $mvouch = $getac["vouchno"];
        $mamount = $getac["amount"];
        $mgrpcode = $getac["grpcode"];
        $maccode = $getac["accode"];
        $macname = $getac["name"];
        $mopbal = $getac["openbal"];
        $mopdc = $getac["dc"];
        $opamt = $getac["opamt"];
        if ($mopdc == "D") {
            $mopbal = $mopbal * -1;
        }
        $amt = $opamt;
        $mobal = $mopbal + $amt;

        $mdebit = 0;
        $mcredit = 0;
        if ($mobal < 0) {
            $mdc = "DR";
            $mdebit = abs($mobal);
        } else {
            $mdc = "CR";
            $mcredit = abs($mobal);
            //                                    $ctot = $ctot + $mcredit;
        }

        if ($maccode != $taccode) {
            if ($ftime > 0) {


                // $contents .= "" . ",";
                // $contents .= "" . ",";
                // $contents .= "Total" . ",";
                // $contents .= "Opening - " .  round($opening, 2) . ",";
                // $contents .= round($dtot, 2) . ",";
                // $contents .= round($ctot, 2) . ",";
                // if ($totbal < 0) {
                //     $contents .=  round(abs($totbal), 2) . $mdctot . "\n";
                // } else {
                //     $contents .= round($totbal, 2)  . $mdctot . "\n";
                // }
            }

            $mdescrip = "Opening Balance";
            $totbal = $mobal;
            if ($totbal < 0) {
                $mdctot = "DR";
            } else {
                $mdctot = "CR";
            }


            $pdf->setFillColor(38, 166, 154);
            $pdf->setTextColor(255, 255, 255);
            $pdf->Cell(164, $ht, $macname, 1, 0, 'L', 1);
            $pdf->Cell(26, $ht, $mbal . $mdc, 1, 0, 'R', 1);
            $pdf->ln();

            $pdf->setFillColor(47, 190, 243);
            $pdf->Cell(20, $ht, "Date", 1, 0, '', 1);
            $pdf->Cell(15, $ht, "Source", 1, 0, '', 1);
            $pdf->Cell(15, $ht, "Voucher", 1, 0, '', 1);
            $pdf->Cell(68, $ht, "Description", 1, 0, '', 1);
            // $pdf->Cell(30, $ht, "Salesman", 1, 0, '', 1);
            $pdf->Cell(23, $ht, "Debit", 1, 0, 'R', 1);
            $pdf->Cell(23, $ht, "Credit", 1, 0, 'R', 1);
            $pdf->Cell(26, $ht, "Balance", 1, 0, 'R', 1);
            $pdf->ln($ht);

            $totbal1 = abs($totbal);
            $pdf->setFillColor(255, 255, 255);
            $pdf->setTextColor(0, 0, 0);

            $pdf->Cell(20, $ht, $vdate, 1, 0, '', 1);
            $pdf->Cell(15, $ht, "", 1, 0, '', 1);
            $pdf->Cell(15, $ht, $mvouch, 1, 0, '', 1);
            $pdf->Cell(68, $ht, $mdescrip, 1, 0, '', 1);
            // $pdf->Cell(30, $ht, $msalesman, 1, 0, '', 1);
            $pdf->Cell(23, $ht, $mdebit, 1, 0, 'R', 1);
            $pdf->Cell(23, $ht, $mcredit, 1, 0, 'R', 1);
            $pdf->Cell(26, $ht, $totbal1 . " " . $mdctot, 1, 0, 'R', 1);
            $pdf->ln($ht);

            if ($mcredit > 0) {
                $opening = $mcredit;
            } else {
                $opening = $mdebit;
            }
            $taccode = $maccode;
            $ftime++;
            //                                    $dtot = $ctot = $totbal = 0;
        }

        $totdiscprov = 0;
        $mdebit = 0;
        $mcredit = 0;

        if ($rt > 0) {
            $mamount = $mamount * -1;
        }

        if ($mamount < 0) {
            $mdc = "DR";
            $mdebit = abs($mamount);
            $dtot = $dtot + $mdebit;
            $debcrdcond = ' and a.credit > 0 ';
        } else {
            $mdc = "CR";
            $mcredit = abs($mamount);
            $ctot = $ctot + $mcredit;
            $debcrdcond = ' and a.debit > 0 ';
        }
        $totbal = $totbal + $mamount;
        if ($totbal < 0) {
            $mdctot = "DR";
        } else {
            $mdctot = "CR";
        }
        $mdescrip = "";
        $mchqdtl = "";
        $mnarr = "";

        switch ($msource) {
            // case "PU";
            //     $head = "Purchase";
            //     $url = "purchase.php?vouchno=$mvouch&method=ledger";
            //     $totdiscprov = $totdiscprov + $mamount;
            //     if ($prnnarr == 1) {
            //         $nqry1 = mysqli_query($conn, "SELECT billno, DATE_FORMAT(`billdate`, '%d-%m-%Y') as mbilldate,
            //                                    `remark1`, `remark2` FROM `purmain`
            //                                   WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
            //         $nqry = mysqli_fetch_array($nqry1);
            //         $mbill = $nqry["billno"];
            //         $mbilldt = $nqry["mbilldate"];
            //         $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
            //         $mchqdtl = "Bill No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
            //     }
            //     break;
            case "PUEX";
                $head = "Purchase Expense";
                $url = "purchase_exp.php?mode=edit&type=R&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $mbill = $getac["puex_billno"];
                    $mbilldt = $getac["puex_billdate"];
                    $mnarr = trim($getac["puex_rem1"]) . " " . trim($getac["puex_rem2"]);
                    $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "PUPK";
                $head = "Purchase Packing";
                $url = "purchase_mi.php?mode=edit&type=P&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {

                    $nqry1 = mysqli_query($conn, "SELECT invno as billno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                               `rem1`, `rem2` FROM `rawmain`
                                              WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);
                    $mbill = $nqry["billno"];
                    $mbilldt = $nqry["mbilldate"];
                    $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                    $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "PURW";
                $head = "Purchase Raw Material";
                $url = "purchase_mi.php?mode=edit&type=R&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {

                    $nqry1 = mysqli_query($conn, "SELECT invno as billno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                               `rem1`, `rem2` FROM `rawmain`
                                              WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);
                    $mbill = $nqry["billno"];
                    $mbilldt = $nqry["mbilldate"];
                    $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                    $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "PRPK";
                $head = "Purchase Return - Packing Material";
                $url = "debnote_mi.php?mode=edit&type=P&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                               `rem1`, `rem2` FROM `debmain`
                                              WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);
                    $mbill = $nqry["invno"];
                    $mbilldt = $nqry["mbilldate"];
                    $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                    $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "PRRW";
                $head = "Purchase Return - Raw Material";
                $url = "debnote_mi.php?mode=edit&type=R&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                               `rem1`, `rem2` FROM `debmain`
                                              WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);
                    $mbill = $nqry["invno"];
                    $mbilldt = $nqry["mbilldate"];
                    $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                    $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "PREX";
                $head = "Purchase Return - Expense";
                $url = "debnote_mi.php?mode=edit&type=E&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                               `rem1`, `rem2` FROM `debmain`
                                              WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);
                    $mbill = $nqry["invno"];
                    $mbilldt = $nqry["mbilldate"];
                    $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                    $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "SL";
                $head = "Sale";
                $url = "sale_ti.php?mode=edit&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $mbill = $getac['sale_billno'];
                    $mbilldt = $getac["sale_billdate"];
                    $mnarr = trim($getac["sale_rem1"]) . " " . trim($getac["sale_rem2"]);
                    $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "SLON";
                $head = "Sale Online";
                $url = "sale_onl.php?mode=edit&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $mbill = $getac['slonl_billno'];
                    $mbilldt = $getac["slonl_billdate"];
                    $mnarr = trim($getac["slonl_rem1"]) . " " . trim($getac["slonl_rem2"]);
                    $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "SLDE";
                $head = "Sale Deemed";
                $url = "sale_de.php?mode=edit&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                    `rem1`, `rem2` FROM `deexmain` 
                                                     WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);
                    $mbill = $nqry["invno"];
                    $mbilldt = $nqry["mbilldate"];

                    $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                    $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "SLO";
                $head = "Sale Other";
                $url = "saleoth.php?mode=edit&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                    `rem1`, `rem2` FROM `slotmain` 
                                                     WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);
                    $mbill = $nqry["invno"];
                    $mbilldt = $nqry["mbilldate"];

                    $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                    $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "SLEX";
                $head = "Sale Export";
                $url = "sale_ex.php?mode=edit&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $nqry1 = mysqli_query($conn, "SELECT invoice, DATE_FORMAT(`invdate`, '%d-%m-%Y') as mbilldate,
                                                    `rem1`, `rem2` FROM `salemainexp` 
                                                     WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);
                    $mbill = $nqry["invno"];
                    $mbilldt = $nqry["mbilldate"];

                    $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                    $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "SR";
                $head = "Sale Return";
                $url = "crednote.php?mode=edit&credtype=G&vouchno=$mvouch&method=ledger";
                break;
            case "BR";
                if ($rt == 0) {
                    $head = "Bank Receipt ( $mbankinit )";
                } else {
                    $head = "Bank Receipt ( $mbankinit ) - Cheque Returned";
                }
                $url = "bankrecp.php?mode=edit&vouchno=$mvouch&method=ledger";

                if ($prnnarr == 1) {
                    $mbill = $getac["bankrecp_chqno"];
                    $mbilldt = $getac["bankrecp_date"];
                    $mnarr = $getac["bankrecp_rem1"] . " " . $getac["bankrecp_rem2"];

                    $mchqdtl = "Cheque. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "BP";
                if ($rt == 0) {
                    $head = "Bank Payment ( $mbankinit )";
                } else {
                    $head = "Bank Payment ( $mbankinit ) - Cheque Returned";
                }
                $url = "bankpay.php?mode=edit&vouchno=$mvouch&method=ledger";

                if ($prnnarr == 1) {
                    $mbill = $getac["bankpay_chqno"];
                    $mbilldt = $getac["bankpay_date"];
                    $mnarr = $getac["bankpay_rem1"] . " " . $getac["bankpay_rem2"];
                    $mchqdtl = "Cheque. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                }
                break;
            case "CR";
                $head = "Cash Receipt";
                $url = "cashrecp.php?mode=edit&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $mnarr = $getac["cashrecp_rem1"] . " " . $getac["cashrecp_rem2"];
                }
                break;
            case "CP";
                $head = "Cash Payment";
                $url = "cashpay.php?mode=edit&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $mnarr = $getac["cashpay_rem1"] . " " . $getac["cashpay_rem2"];
                }
                break;
            case "TR";
                $head = "Contra / Transfer";
                $url = "contra.php?mode=edit&vouchno=$mvouch&method=ledger";
                if ($prnnarr == 1) {
                    $mnarr = $getac["contra_rem1"] . " " . $getac["contra_rem2"];
                }
                break;
            case "JV";
                $head = "Journal Voucher";
                $url = "jv.php?mode=edit&vouchno=$mvouch&method=ledger";

                $tran1 = mysqli_query($conn, "SELECT a.accode, b.name FROM `jvtran` a ,accmast b
                                                  WHERE a.vouchno='$mvouch' $debcrdcond  and a.accode = b.accode order by a.srno limit 1 ") or die(mysqli_error($conn));

                $fet1 = mysqli_fetch_array($tran1);
                $jname = $fet1["name"];
                if ($jname != '') {
                    $head = "$jname";
                }
                if ($prnnarr == 1) {
                    $mnarr = $getac["jv_rem1"] . "" . $getac['jv_rem2'];
                }
                break;
            case "DG";
                $head = "Discount Given";
                break;
            case "DR";
                $head = "Discount Received";
                break;
            default:
                $head = "Other";
        }


        $pdf->SetFont('Arial', 'B', 8);

        $pdf->Cell(20, $ht, $vdate, 1, 0, '', 1);
        $pdf->Cell(15, $ht, $msource, 1, 0, '', 1);
        $pdf->Cell(15, $ht, $mvouch, 1, 0, '', 1);
        $pdf->Cell(68, $ht, $head, 1, 0, '', 1);
        $pdf->Cell(23, $ht, number_format($mdebit, 2), 1, 0, 'R', 1);
        $pdf->Cell(23, $ht, number_format($mcredit, 2), 1, 0, 'R', 1);
        $pdf->SetFont('Arial', 'B', 8);

        $pdf->Cell(26, $ht, number_format(abs($totbal), 2) . "" . $mdctot, 1, 0, 'R', 1);
        $pdf->ln($ht);

        if ($prnnarr == 1) {
            if (!empty($mchqdtl)) {
                if (!empty($mchqdtl)) {
                    $pdf->Cell(20, $ht, "", 1, 0, '', 1);
                    $pdf->Cell(15, $ht, "", 1, 0, '', 1);
                    $pdf->Cell(15, $ht, "", 1, 0, '', 1);
                    $pdf->SetFont('Arial', '', 8);
                    $pdf->Cell(68, $ht, $mchqdtl, 1, 0, '', 1);
                    $pdf->Cell(23, $ht, "", 1, 0, '', 1);
                    $pdf->Cell(23, $ht, "", 1, 0, '', 1);
                    $pdf->Cell(26, $ht, "", 1, 0, '', 1);
                    $pdf->ln($ht);
                }

                if (!empty($mnarr)) {
                    $pdf->Cell(20, $ht, "", 1, 0, '', 1);
                    $pdf->Cell(15, $ht, "", 1, 0, '', 1);
                    $pdf->Cell(15, $ht, "", 1, 0, '', 1);
                    $pdf->SetFont('Arial', '', 8);
                    $pdf->Cell(68, $ht, $mnarr, 1, 0, '', 1);
                    $pdf->SetFont('Arial', 'B', 10);
                    $pdf->Cell(23, $ht, "", 1, 0, '', 1);
                    $pdf->Cell(23, $ht, "", 1, 0, '', 1);
                    $pdf->Cell(26, $ht, "", 1, 0, '', 1);
                    $pdf->ln($ht);
                }
            }
        }
    }


    $pdf->Cell(90, $ht, "Opening : " . round($opening, 2), 1, 0, 'R', 1);
    $pdf->Cell(28, $ht, "Total", 1, 0, 'R', 1);
    $pdf->Cell(23, $ht, round($dtot, 2), 1, 0, 'R', 1);
    $pdf->Cell(23, $ht, round($ctot, 2), 1, 0, 'R', 1);

    $pdf->SetFont('Arial', 'B', 9);
    if ($totbal < 0) {
        $color = "255, 0, 0";
        $chgcolr = explode(',', $color);
        $pdf->SetTextColor($chgcolr[0], $chgcolr[1], $chgcolr[2]);
        $pdf->Cell(26, $ht, round(abs($totbal), 2) . " " . $mdctot, 1, 0, 'R', 1);
    } else {
        $color = "0, 128, 0";
        $changecolor = explode(',', $color);
        $pdf->SetTextColor($changecolor[0], $changecolor[1], $changecolor[2]);
        $pdf->Cell(26, $ht, round($totbal, 2) . " " . $mdctot, 1, 0, 'R', 1);
    }
    $pdf->SetTextColor(0, 0, 0);
    $pdf->ln();
}


if (isset($_POST["subpdf"])) {
    $pdf->output("Ledger.pdf", "I");
} else {
    $myyear = $_SESSION[$myyear];
    $z1 = explode('_', $zroot);

    $accname1 = str_replace("/", "-", $accname);

    //    $myyear = $_SESSION[$myyear];
    $z1 = explode('_', $zroot);

    $nm = $z1[1];

    $nomFacture = $_SERVER['DOCUMENT_ROOT'] . "/admin/ledgerpdf/$accname1.pdf";

    $urlsend = "https://apps2ai.com/galaxycargo/manage/admin/ledgerpdf/$accname1.pdf";

    $pdf->Output("F", "$nomFacture");

    $filevoucher = "LEDGERPDF-" . $accname1;
    $filename = "LEDGERPDF-" . $accname1 . ".pdf";

    $mobileno = $mobile;

    // $mobileno = '919082336408';

    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();
        }

        // ---- Background work continues here ----
        $logFile = "/tmp/whatsapp_send_" . $fetdata['vouchno'] . ".log";

        $cmd = "curl -s -X POST " .
            "\"https://web.secureleads.in/api/send?number=$mobileno&type=media&message=Shipment%20Detail%20$filevoucher%20&media_url=$msgtech&filename=$filename&instance_id=$instid&access_token=$accstkn\" " .
            "-H 'Cookie: stackpost_session=m1ip03t51dk5rlgaa5blakqmva57k3nd' " .
            "> $logFile 2>&1 &";

        exec($cmd);

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