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

$maindb = "actran";

$file = "cust_ledger_pdf.php";

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

$para = $_GET["para"];
$party = $_GET["party"];

$formname = "";
if ($para == "customer") {
    $formname = "Customer Ledger";
    $bgcolor = "122, 78, 217";
} elseif ($para == "supplier") {
    $formname = "Supplier Ledger";
    $bgcolor = "150, 54, 52";
} elseif ($para == "expense") {
    $formname = "Expense Ledger";
    $bgcolor = "165, 56, 96";
} elseif ($para == "loan") {
    $formname = "Loan Ledger";
    $bgcolor = "11, 76, 120";
}

$dtypecond = "";
$mscopecond = "";

$mfrmdate = $_GET["frmdt"];
$mtodate = $_GET["todt"];

if (!empty($party)) {
    $partycond = " AND `accode`='$party' ";
}

if (empty($mfrmdate)) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}

if (empty($mtodate)) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

$maccode = $party;

$getpartyname = mysqli_query($conn, "SELECT `name`, openbal, dc FROM `accmast` WHERE `accode`='$maccode'") or die(mysqli_error($conn));
$zname = mysqli_fetch_object($getpartyname);
$mname = $zname->name;
$mopbal = $zname->openbal;
$mopdc = $zname->dc;

if ($mopdc == "D") {
    $mopbal = $mopbal * -1;
}

$getbal = mysqli_query($conn, "SELECT SUM(amount) as oamt FROM `actran` WHERE accode='$party' AND  date < '$mfrmdate'") or die(mysqli_error($conn));
$getamt = mysqli_fetch_array($getbal);
$oamt = $getamt["oamt"];

$mobal = $mopbal + $oamt;

$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `date` >= '$mfrmdate' 
                                        AND `date` <= '$mtodate' and accode = '$party' order by date, srl ") or die(mysqli_error($conn));

$get = mysqli_num_rows($getpmain);

$mobal = $mopbal + $oamt;

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

class pdf extends fpdf
{
    function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
    }
}

class PDF_Dash extends FPDF
{
    function SetDash($black = false, $white = false)
    {
        if ($black && $white) {
            $s = sprintf('[%.3f %.3f] 0 d', $black, $white);
        } else {
            $s = '[] 0 d'; // Solid line
        }
        $this->_out($s);
    }
}

$pdf = new PDF_Dash('P', 'mm', 'A4');
$pdf->AddPage();
$ht = 6;

$pdf->SetFont('Arial', 'B', 15);
$pdf->SetTextColor(255, 255, 255);

if ($para == "customer") {
    $pdf->SetFillColor(122, 78, 217);
} elseif ($para == "supplier") {
    $pdf->SetFillColor(150, 54, 52);
} elseif ($para == "expense") {
    $pdf->SetFillColor(165, 56, 96);
} elseif ($para == "loan") {
    $pdf->SetFillColor(11, 76, 120);
}

$pdf->Cell(195, $ht, $mname, 1, 1, 'L', 1);
$pdf->ln(3);

$pdf->SetFont('Arial', 'B', 10);
$pdf->SetTextColor(255, 255, 255);
$pdf->SetFillColor(141, 153, 174);

if ($para == "customer") {
    $btnlabel = "Sale";
    $btnval = "S";
    $btnlabel2 = "Receipt";
    $btnval2 = "R";
    $btnlabel3 = "Claim";
    $btnval3 = "SC";
} elseif ($para == "supplier") {
    $btnlabel = "Purchase";
    $btnval = "PU";
    $btnlabel2 = "Payment";
    $btnval2 = "PY";
    $btnlabel3 = "Claim";
    $btnval3 = "PC";
}

$pdf->SetTextColor(255, 255, 255);
$pdf->SetFillColor(141, 153, 174);

$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(8, $ht, 'Sr', 0, 0, '', true);
$pdf->Cell(18, $ht, 'Date', 0, 0, '', true);
$pdf->Cell(12, $ht, 'Source', 0, 0, '', true);
$pdf->Cell(14, $ht, 'Voucher', 0, 0, '', true);
$pdf->Cell(20, $ht, 'Transaction', 0, 0, '', true);
$pdf->Cell(93, $ht, 'Description', 0, 0, '', true);
$pdf->Cell(30, $ht, 'Amount', 0, 0, 'R', true);
$pdf->ln();

$ctr = 0;
$totamt = 0;
$gtot = 0;

$vdate1 = strtotime($mfrmdate);
$vdate = date('d-m-Y', $vdate1);

$mdescrip = "Opening Bal.";

$ctr++;
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFillColor(255, 255, 255);
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(8, $ht, "$ctr", 'T', 0, '', true);
$pdf->Cell(18, $ht, "$vdate", 'T', 0, '', true);
$pdf->Cell(12, $ht, "", 'T', 0, '', true);
$pdf->Cell(14, $ht, "", 'T', 0, '', true);
$pdf->Cell(20, $ht, "$mdescrip", 'T', 0, '', true);
// $pdf->Cell(20, $ht, "", 'T', 0, '', true);
$pdf->Cell(93, $ht, "", 'T', 0, '', true);
$pdf->Cell(30, $ht, "$mobal", 'T', 0, 'R', true);
$pdf->ln();

$gtot = $gtot + $mobal;

$pdf->Cell(8, $ht, "", 'T', 0, '', true);
$pdf->Cell(18, $ht, "", 'T', 0, '', true);
$pdf->Cell(12, $ht, "", 'T', 0, '', true);
$pdf->Cell(14, $ht, "", 'T', 0, '', true);
$pdf->Cell(20, $ht, "", 'T', 0, '', true);
$pdf->Cell(93, $ht, "Balance", 'T', 0, 'R', true);
$pdf->SetFillColor(255, 255, 210);
$pdf->Cell(30, $ht, "$gtot", 'T', 0, 'R', true);
$pdf->ln();

while ($pmain = mysqli_fetch_array($getpmain)) {
    $amount = $pmain['amount'];
    $msource = $pmain['source'];
    $mvouch = $pmain['vouchno'];

    $getname = mysqli_query($conn, "SELECT accode,name FROM `accmast` where accode='$pmain[accode]' order by name") or die(mysqli_error($conn));
    $fetname = mysqli_fetch_array($getname);

    $mnarr = "";
    $mchqdtl = "";
    $desc = "";

    switch ($msource) {

        case "PU";
            $head = "Purchase";
            $url = "purchase.php?vouchno=$mvouch&mode=edit";
            $totdiscprov = $totdiscprov + $mamount;

            if ($prnnarr == 1) {
                $nqry1 = mysqli_query($conn, "SELECT vouchno as billno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                                    `remark` FROM `purchase`
                                                                    WHERE vouchno='$mvouch' ") or die(mysqli_error($conn));
                $nqry = mysqli_fetch_array($nqry1);
                $mbill = $nqry["billno"];
                $mbilldt = $nqry["mbilldate"];
                $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                $mchqdtl = "Bill No. : " . trim($mbill) . " Dated : " . $mbilldt;
            }

            break;

        case "SL";
            $head = "Sale";
            $url = "sale.php?vouchno=$mvouch&mode=edit";
            //  if ($prnnarr == 1) {
            $nqry1 = mysqli_query($conn, "SELECT vouchno as invno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                                `remark` FROM `sale`
                                                                WHERE vouchno='$mvouch' ") or die(mysqli_error($conn));
            $nqry = mysqli_fetch_array($nqry1);
            $mbill = $nqry["invno"];
            $mbilldt = $nqry["mbilldate"];

            $mnarr = trim($nqry["remark"]);
            $mchqdtl = "Bill No. : " . trim($mbill) . " Dated : " . $mbilldt;
            // }
            break;

        case "SCL";
            $head = "Sale Claim";
            $url = "slclaim.php?vouchno=$mvouch&mode=edit";

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

            $mnarr = trim($nqry["remark"]);
            $mchqdtl = "Bill No. : " . trim($mbill) . " Dated : " . $mbilldt;
            // }
            break;

        case "PCL";
            $head = "Purchase Claim";
            $url = "puclaim.php?vouchno=$mvouch&mode=edit";

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

            $mnarr = trim($nqry["remark"]);
            $mchqdtl = "Bill No. : " . trim($mbill) . " Dated : " . $mbilldt;
            // }
            break;

        case "BR";
            $head = "Bank Receipt";
            $url = "bankrecp.php?vouchno=$mvouch&mode=edit";
            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["narration"];

                $mchqdtl = "Bill No. : " . trim($mbill) . " Dated : " . $mbilldt;
            }
            break;

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

                $mchqdtl = "Bill No. : " . trim($mbill) . " Dated : " . $mbilldt;
            }
            break;

        case "CR";
            $head = "Receipt";
            $url = "cashrecp.php?vouchno=$mvouch&mode=edit";
            if ($prnnarr == 1) {
                $nqry1 = mysqli_query($conn, "SELECT a.*, b.name  FROM `cashrecp` a, accmast b
                                                                    WHERE a.vouchno='$mvouch' and a.bankname = b.accode ") or die(mysqli_error($conn));
                $nqry = mysqli_fetch_array($nqry1);

                $desc = $nqry['name'];
                $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
            }
            break;

        case "CP";
            $head = "Payment";
            $url = "cashpay.php?vouchno=$mvouch&mode=edit";
            if ($prnnarr == 1) {
                $nqry1 = mysqli_query($conn, "SELECT a.*,b.name  FROM `cashpay` a, accmast b
                                                                    WHERE a.vouchno='$mvouch' and a.bankname = b.accode ") or die(mysqli_error($conn));
                $nqry = mysqli_fetch_array($nqry1);
                $desc = $nqry['name'];

                $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
            }
            break;

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

        case "JV";
            $head = "JV Entry";
            $url = "jventry.php?vouchno=$mvouch&mode=edit";

            if ($amount < 0) {
                $getcash = mysqli_query($conn, "Select a.remark1 as `narration`, b.name  from `jventry` a, accmast b where 
                                                                               a.`vouchno`='$mvouch' and a.party = b.accode ") or die(mysqli_error($conn));
            } else {
                $getcash = mysqli_query($conn, "Select a.remark1 as `narration`, b.name from `jventry` a, accmast b where 
                                                                               a.`vouchno`='$mvouch' and a.bankname = b.accode ") or die(mysqli_error($conn));
            }
            $fetcash = mysqli_fetch_array($getcash);
            $mchqdtl = $fetcash['name'];

            if ($prnnarr == 1) {
                $mnarr = $fetcash["narration"];
            }
            break;

        case "DG";
            $head = "Discount Given";
            break;

        case "DR";
            $head = "Discount Received";
            break;

        default:
            $head = "Other";
    }
    $pdf->SetTextColor(0, 0, 0);


    $ctr++;
    $pdf->Cell(8, $ht, "$ctr", 'T', 0, '');
    $pdf->Cell(18, $ht, "$pmain[mydt]", 'T', 0, '');
    $pdf->Cell(12, $ht, "$msource", 'T', 0, '');
    $pdf->Cell(14, $ht, "$pmain[vouchno]", 'T', 0, '');
    $pdf->Cell(20, $ht, "$head", 'T', 0, '');
    // $pdf->Cell(93, $ht, "$desc $mchqdtl", 'T', 0, '');
    $pdf->Cell(93, $ht, "", 'T', 0, '');
    $pdf->Cell(30, $ht, "$amount", 'T', 0, 'R');
    $pdf->ln();

    $gtot = $gtot + $amount;

    $pdf->Cell(8, $ht, "", 'TB', 0, '');
    $pdf->Cell(18, $ht, "", 'TB', 0, '');
    $pdf->Cell(12, $ht, "", 'TB', 0, '');
    $pdf->Cell(14, $ht, "", 'TB', 0, '');
    $pdf->Cell(20, $ht, "", 'TB', 0, '');
    if ($mnarr != '') {
        $pdf->Cell(81, $ht, "Remark : $mnarr", 'T', 0, '');
    } else {
        $pdf->Cell(81, $ht, "", 'TB', 0, '');
    }
    $pdf->Cell(12, $ht, "Balance", 'TB', 0, 'R');
    $pdf->SetFillColor(255, 255, 210);
    $pdf->Cell(30, $ht, "$gtot", 'TB', 0, 'R', 1);
    $pdf->ln();
}
// $pdf->SetFillColor(141, 153, 174);
// $pdf->Cell(195, $ht, "", 1, 0, '', true);
$pdf->ln();

$pdf->Output("$mname". date("d-m-Y") . ".pdf", "I");