<?php

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

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

$mobile = $_GET["mobileno"];
$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];

$getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE code = '$zcomp'") 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 -' . $mfdt . "To Date -" . $mtdt, 'LRB', 0, '', 1);
$pdf->Ln();

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

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


mysqli_select_db($conn, $zdb);
$totbal = $totobal = $totdeb = $totcrd = 0;

mysqli_query($conn, "drop temporary table if exists opdata") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table opdata SELECT accode, sum(amount) as amt  FROM `$trandb` WHERE 
                                                date < '$mfrmdate' and retdate = '0000-00-00' group by accode") or die(mysqli_error($conn));

mysqli_query($conn, "drop temporary table if exists drdata") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table drdata SELECT sum(amount) as amtdr, accode FROM `$trandb` WHERE 
                                                amount < 0 and date >= '$mfrmdate' and date <= '$mtodate' and retdate = '0000-00-00' group by accode") or die(mysqli_error($conn));

mysqli_query($conn, "drop temporary table if exists crdata") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table crdata SELECT sum(amount) as amtcr, accode FROM `$trandb` WHERE 
                                                amount > 0 and date >= '$mfrmdate' and date <= '$mtodate' and retdate = '0000-00-00' group by accode") or die(mysqli_error($conn));

if ($dtype == 2) {
    mysqli_query($conn, "drop temporary table if exists baldata") or die(mysqli_error($conn));
    mysqli_query($conn, "create temporary table baldata SELECT SUM(amount) as amt, accode FROM `$trandb` WHERE 
                                                      date <= '$mtodate' and `retdate` = '0000-00-00' ") or die(mysqli_error($conn));
}


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

    if ($dtype < 2) {
        $maccode = $getac["accode"];
        $acname = $getac["name"];

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

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

        $getop = mysqli_query($conn, "SELECT amt FROM `opdata` WHERE  `accode`='$maccode' ") or die(mysqli_error($conn));
        $fetop = mysqli_fetch_array($getop);
        $opamt = $fetop["amt"];
        $obal = $mopbal + $opamt;
        if ($obal < 0) {
            $odc = "DR";
        } else {
            $odc = "CR";
        }

        //                                $gettrdr = mysqli_query($conn, "SELECT SUM(amount) as amtdr FROM `mondata` WHERE  amount < 0 ") or die(mysqli_error($conn));
        $gettrdr = mysqli_query($conn, "SELECT amtdr FROM `drdata` WHERE  `accode`='$maccode' ") or die(mysqli_error($conn));
        $fettrdr = mysqli_fetch_array($gettrdr);
        $tramtdr = $fettrdr["amtdr"];
        $tramtdr = abs($tramtdr);

        //                                $gettrcr = mysqli_query($conn, "SELECT SUM(amount) as amtcr FROM `mondata` WHERE amount > 0 ") or die(mysqli_error($conn));
        $gettrcr = mysqli_query($conn, "SELECT amtcr FROM `crdata` WHERE `accode`='$maccode' ") or die(mysqli_error($conn));
        $fettrcr = mysqli_fetch_array($gettrcr);
        $tramtcr = $fettrcr["amtcr"];
        $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, 'L', 0);
                    $pdf->Cell(30, $ht, number_format($monopn1) . " " . $mdco, 1, 0, 'R', 0);
                    $pdf->Cell(30, $ht, number_format($mondeb1), 1, 0, 'R', 0);
                    $pdf->Cell(30, $ht, number_format($moncrd1), 1, 0, 'R', 0);
                    $pdf->Cell(30, $ht, number_format($monbal1) . " " . $mdcmon, 1, 0, 'R', 0);
                    $pdf->ln($ht);
                }

                $pdf->Cell(100, $ht, " ", 1, 0, '', 1);
                $pdf->Cell(90, $ht, " ", 1, 0, '', 1);
                $pdf->ln();
            }
        }
    } else {

        $totbal = 0;
        $dtot = 0;
        $ctot = 0;
        $maccode = $getac["accode"];
        $macname = $getac["name"];
        $mopbal = $getac["openbal"];
        $mopdc = $getac["dc"];
        if ($mopdc == "D") {
            $mopbal = $mopbal * -1;
        }
        $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb`
                                            WHERE accode = '$maccode' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                            and retdate = '0000-00-00' ORDER BY `date`, vouchno") or die(mysqli_error($conn));

        $curl = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb`
                                            WHERE accode = '$maccode' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                            and retdate = '0000-00-00' ORDER BY `date`, vouchno") or die(mysqli_error($conn));

        while ($murl = mysqli_fetch_object($curl)) {
            switch ($murl->source) {
                case "PU";
                    $head = "Purchase";
                    $url = "purchase.php?mode=edit&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "PUPK";
                    $head = "Purchase Packing";
                    $url = "purchase_mi.php?mode=edit&type=P&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "PURW";
                    $head = "Purchase Raw Material";
                    $url = "purchase_mi.php?mode=edit&type=P&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "PR";
                    $head = "Purchase Return";
                    $url = "debnote.php??mode=edit&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "SL";
                    $head = "Sale";
                    $url = "sale_ti.php?mode=edit&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "SLDE";
                    $head = "Sale Deemed";
                    $url = "sale_de.php?mode=edit&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "SLOT";
                    $head = "Sale Other";
                    $url = "saleoth.php?mode=edit&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "SLEX";
                    $head = "Sale Export";
                    $url = "sale_ex.php?mode=edit&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "SLON";
                    $head = "Sale Online";
                    $url = "sale_onl.php?mode=edit&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "SR";
                    $head = "Sale Return";
                    $url = "crednote.php?mode=edit&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "BR";
                    $url = "bankrecp.php?mode=edit&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "BP";
                    $head = "Bank Payment";
                    $url = "bankpay.php?mode=edit&$murl->vouchno&method = ledger";
                    break;
                case "CR";
                    $head = "Cash Receipt";
                    $url = "cashrecp.php?mode=edit&$murl->vouchno&method = ledger";
                    break;
                case "CP";
                    $head = "Cash Payment";
                    $url = "cashpay.php?mode=edit&$murl->vouchno&method = ledger";
                    break;
                case "TR";
                    $head = "Contra / Transfer";
                    $url = "contra.php?mode=edit&vouchno = $murl->vouchno&method = ledger";
                    break;
                case "JV";
                    $head = "Journal Voucher";
                    $url = "journal.php?mode=edit&$murl->vouchno&method = ledger";
                    break;
                case "DG";
                    $head = "Discount Given";
                    break;
                case "DR";
                    $head = "Discount Received";
                    break;
                default:
                    $head = "Other";
            }
        }

        $getbal = mysqli_query($conn, "SELECT amt FROM `opdata` WHERE accode='$maccode' ") or die(mysqli_error($conn));
        $getamt = mysqli_fetch_array($getbal);
        $amt = $getamt["amt"];
        $mobal = $mopbal + $amt;

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

        $vdate1 = strtotime($mfrmdate);
        $vdate = date('d-m-Y', $vdate1);
        $mvouch = "";
        $mdescrip = "Opening Balance";
        $totbal = $totbal + $mobal;
        if ($totbal < 0) {
            $mdctot = "DR";
        } else {
            $mdctot = "CR";
        }

        $getbal = mysqli_query($conn, "SELECT amt FROM `baldata` WHERE accode='$maccode' ") or die(mysqli_error($conn));
        $getamt = mysqli_fetch_array($getbal);
        $amt = $getamt["amt"];
        $mbal = $mopbal + $amt;
        if ($mbal < 0) {
            $mdc = "DR";
        } else {
            $mdc = "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;
        }

        while ($row = mysqli_fetch_array($ctran1)) {
            $vdate = $row["mydt"];
            $msource = $row["source"];
            $mvouch = $row["vouchno"];
            $mamount = $row["amount"];
            $mgrpcode = $getac["grpcode"];
            $mdiscprov = $getac["discount"];
            $totdiscprov = 0;
            $mdebit = 0;
            $mcredit = 0;
            if ($mamount < 0) {
                $mdc = "DR";
                $mdebit = abs($mamount);
                $dtot = $dtot + $mdebit;
            } else {
                $mdc = "CR";
                $mcredit = abs($mamount);
                $ctot = $ctot + $mcredit;
            }
            $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&mode=edit";
                    $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 "PUPK":
                    $head = "Purchase Packing";
                    $url = "purchase.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,
                                                                        `remark1` as rem1, `remark2` as 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.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 "PR":
                    $head = "Purchase Return";
                    $url = "debnote.php?vouchno=$mvouch&method=ledger";

                    $totdiscprov = $totdiscprov + $mamount;
                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%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) {
                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                                             rem1, rem2 FROM `salemain` 
                                                                              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 "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 "SLOT":
                    $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 "SLON":
                    $head = "Sale Online";
                    $url = "sale_onl.php?mode=edit&vouchno=$mvouch&method=ledger";

                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT invno, buyer, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                                             `rem1`, `rem2` FROM `slonlmain` 
                                                                              WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);
                        $mbill = $nqry["invno"];
                        $mbilldt = $nqry["mbilldate"];

                        $mnarr = trim($nqry["buyer"]) . " " . 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&vouchno=$mvouch&method=ledger";
                    break;

                case "BR":
                    $head = "Bank Receipt";
                    $url = "bankrecp.php?mode=edit&vouchno=$mvouch&method=ledger";
                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                             `remark1`, remark2 FROM `bankrecp`
                                                                              WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);
                        $mbill = $nqry["chqno"];

                        $mbilldt = $nqry["mbilldate"];
                        $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];

                        $mchqdtl = "Cheque. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                    }
                    break;

                case "BP":
                    $head = "Bank Payment";
                    $url = "bankpay.php?mode=edit&vouchno=$mvouch&method=ledger";
                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                        `remark1`, remark2 FROM `bankpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);
                        $mbill = $nqry["chqno"];
                        $mbilldt = $nqry["mbilldate"];
                        $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];
                        $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) {
                        $nqry1 = mysqli_query($conn, "SELECT `remark1`, remark2 FROM `cashrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);
                        $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];
                    }
                    break;

                case "CP":
                    $head = "Cash Payment";
                    $url = "cashpay.php?mode=edit&vouchno=$mvouch&method=ledger";
                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT `remark1`, remark2 FROM `cashpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);
                        $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];
                    }
                    break;

                case "TR":
                    $head = "Contra / Transfer";
                    $url = "contra.php?mode=edit&vouchno=$mvouch&method=ledger";
                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT `narr1`, narr2 FROM `contra`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);
                        $mnarr = $nqry["narr1"] . "" . $nqry['narr2'];
                    }
                    break;

                case "JV":
                    $head = "Journal Voucher";
                    $url = "jv.php?mode=edit&vouchno=$mvouch&method=ledger";
                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT `remark1`, remark2 FROM `jvmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);
                        $mnarr = $nqry["remark1"] . "" . $nqry['remark2'];
                    }
                    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)) {
                    $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 : " . number_format($opening, 2), 1, 0, 'R', 1);
        $pdf->Cell(28, $ht, "Total", 1, 0, 'R', 1);
        $pdf->Cell(23, $ht, number_format($dtot, 2), 1, 0, 'R', 1);
        $pdf->Cell(23, $ht, number_format($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, number_format(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, number_format($totbal, 2) . " " . $mdctot, 1, 0, 'R', 1);
        }
        $pdf->SetTextColor(0, 0, 0);
        $pdf->ln();
    }
}


if ($dtype < 2) {
    $totobals = abs($totobal);
    if ($totobal < 0) {
        $todc = 'DR';
    } else {
        $todc = 'CR';
    }

    $totbals = abs($totbal);
    if ($totbal < 0) {
        $tbdc = 'DR';
    } else {
        $tbdc = 'CR';
    }

    $tcolr = 'color:red';

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


if (isset($_POST["subpdf"])) {
    $pdf->output("Ledger.pdf", "I");
} else {

    $myyear = $_SESSION[$myyear];

    $accname1 = preg_replace('/[^A-Za-z0-9_-]/', '_', $accname);

    $mobileno = $mobile;
    clearstatcache();

    $nomFacture = $_SERVER['DOCUMENT_ROOT'] . "/admin/ledgerpdf/" . $accname1 . ".pdf";
    $urlsend = "https://mysoft365.com/sisgstnew/admin/ledgerpdf/" . $accname1 . ".pdf?v=" . time();
    if (file_exists($nomFacture)) {
        @unlink($nomFacture);
    }
    $pdf->Output("F", $nomFacture);

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

    // ALL WHATSAPP    
    // $mobileno = '9082336408'; // 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();
        }

        // ---- Background work continues here ----     

        $logFile = "/tmp/whatsapp_send_LEDGER" . $accname1 . ".log";
        $cmd = "curl -s -X POST " .
                "\"https://web.secureleads.in/api/send?number=91$mobileno&type=media&message=$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>";
    }
}
?>