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

$zdb = $_SESSION["zdbyear_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
$zcompany = $_SESSION["zcompany"];
$formname = "Cash Book";

$grid = "cashbook_in_out.php";
$cust = "accmast";
$ztit = $_SESSION["tit_" . $dbname];
$zroot = $_SESSION["zdir"];
$maindb = $cust;
$accdb = "actran";

$fseries = $_SESSION["series"];
$keycond = "";
$abal = 'accmtn';
$accbal = $abal . $ztit;
$compcond = '';

//if (isset($_POST["view"])) {
$a = 1;

$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);
$mname1 = trim($_REQUEST["accode"]);

$mfdt = $mfrmdate;
$mtdt = $mtodate;

// if ($mfrmdate > $mtodate) {
//     browse("To Date Cannot Be Less Than From Date");
//     header("refresh: 1; ledger_new.php");
//     exit();
//     $a = 0;
// }

// $maccode = '';
// if ($mname1 == '') {
//     browse("A/c. Name Cannot Be Empty");
//     header("refresh: 1; ledger_new.php");
//     exit();
//     $a = 0;
// } else {
//     $maccode = $mname1;
// }
// $paytypecond = '';
// if (!empty($_POST["paytype"])) {
//     $paytype = trim($_POST["paytype"]);
//     $paytypecond = "AND `paytype`='$partytype'";
// }


$zyear = right($zdb, 4);
//$mfrmdate = getzdates($zyear, 'S', $conn);
//$mtodate = getzdates($zyear, 'E', $conn);
mysqli_select_db($conn, $zdb);


//if ($a == 1) {
$recqry = mysqli_query($conn, " SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') AS vouchdt  FROM `$accdb`
                                where amount < 0 and accode = '$maccode' and chqret != 'Y'
                                -- and date >= '$mfrmdate' and date <= '$mtodate' 
                                order by date, source, vouchno ") or die(mysqli_error($conn));
$rcount = mysqli_num_rows($recqry);

$payqry = mysqli_query($conn, " SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') AS vouchdt  FROM `$accdb`
                                where amount > 0 and accode = '$maccode' and chqret != 'Y'
                                -- and date >= '$mfrmdate' and date <= '$mtodate' 
                                order by date, source, vouchno ") or die(mysqli_error($conn));
$lcount = mysqli_num_rows($payqry);

$getopen = mysqli_query($conn, "SELECT grpcode, accode, name, openbal, dc  FROM `$maindb` WHERE `accode` = '$maccode'  ") or die(mysqli_error($conn));

$ctr = 0;
$totrecp = 0;
$fetopen = mysqli_fetch_array($getopen);
$mname = $fetopen['name'];

$opdc = $fetopen["dc"];

if ($opdc == 'D') {
    $opb = $fetopen["openbal"] * -1;
} else {
    $opb = $fetopen["openbal"];
}
$opbt = $opb;

$mopen = $opbt;

// $getacc = mysqli_query($conn, "SELECT SUM(amount) AS `openamt` FROM `$accdb` WHERE `accode` = '$maccode' and `date` < '$mfrmdate' and chqret != 'Y' ") or die(mysqli_error($conn));
// $fetacc = mysqli_fetch_array($getacc);
// $openacc = $fetacc['openamt'];

$finopen = $mopen + $openacc;
$finop = abs($finopen);

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();
$font = 'Arial';
$cellWidth = 97;
$ht = 8;

$pdf->SetDrawColor(220, 220, 220); // white border

$pdf->SetFont($font, 'B', 15);
$pdf->SetTextColor(255, 255, 255); // White text
$pdf->SetFillColor(141, 153, 174); // Background color

$pdf->SetFont($font, 'B', 12);
$pdf->Cell($cellWidth, $ht, "Received", 1, 0, 'L', 1);
$pdf->Cell($cellWidth, $ht, "Paid", 1, 1, 'L', 1);

$pdf->ln(0);

$pdf->SetFont($font, 'B', 10);
$pdf->SetFillColor(64, 145, 108);  // Green header
$pdf->SetTextColor(255, 255, 255); // White text

$pdf->Cell(20, $ht, 'Date', 1, 0, 'L', 1);
$pdf->Cell(15, $ht, 'Source', 1, 0, 'L', 1);
$pdf->Cell(16, $ht, 'Voucher', 1, 0, 'L', 1);
$pdf->Cell(31, $ht, 'Description', 1, 0, 'L', 1);
$pdf->Cell(15, $ht, 'Amount', 1, 0, 'R', 1);


// Header Row under Paid
$pdf->Cell(20, $ht, 'Date', 1, 0, 'L', 1);
$pdf->Cell(15, $ht, 'Source', 1, 0, 'C', 1);
$pdf->Cell(16, $ht, 'Voucher', 1, 0, 'C', 1);
$pdf->Cell(31, $ht, 'Description', 1, 0, 'C', 1);
$pdf->Cell(15, $ht, 'Amount', 1, 1, 'C', 1);
$pdf->Ln(0);
// $pdf->ln();

$openflag = 0;
if ($finopen <= 0) {
    $ctr++;
    // Received side data
    // $pdf->SetFont($font, '', 10);
    // $pdf->SetTextColor(0);
    // $pdf->Cell(20, $ht, '', 1, 0, 'L');
    // $pdf->Cell(15, $ht, '', 1, 0, 'L');
    // $pdf->Cell(16, $ht, '', 1, 0, 'L');
    // $pdf->Cell(31, $ht, '', 1, 0, 'L');
    // $pdf->Cell(15, $ht, number_format($finop, 0), 1, 0, 'R');


    $totrecp += $finop;
    $openflag = 1;
} else {
    if ($lop == 1) {
        // Received side data
        // $pdf->SetFont($font, '', 10);
        // $pdf->SetTextColor(0);
        // $pdf->Cell(20, $ht, '', 1, 0, 'L');
        // $pdf->Cell(15, $ht, '', 1, 0, 'L');
        // $pdf->Cell(16, $ht, '', 1, 0, 'L');
        // $pdf->Cell(31, $ht, '', 1, 0, 'L');
        // $pdf->Cell(15, $ht, '', 1, 0, 'R');
        // $pdf->ln();
    }
}

$ctr = 0;
while ($receipt = mysqli_fetch_array($recqry)) {
    $source = $receipt['source'];
    $vouchno = $receipt['vouchno'];
    $vouchdt = $receipt['vouchdt'];
    $grpcode = $receipt['grpcode'];
    $recamt = abs($receipt['amount']);
    $amt = $receipt['amount'];

    $colr = "";
    $acname = '';

    if ($source == 'PYIN') {
        $url = "payin.php?vouchno=$vouchno&mode=edit";
        $url2 = "payin_fr.php?vouchno=$vouchno";
        $desc = "Payment In";

        if ($grpcode == '002') {
            $getcash = mysqli_query($conn, "SELECT party as accode from `cashrecp` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
        } else {
            $getcash = mysqli_query($conn, "Select bankname as accode from `cashrecp` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
        }
        $fetcash = mysqli_fetch_array($getcash);
        $accode = $fetcash['accode'];

        $getacc = mysqli_query($conn, "Select name from `accmast` where `accode`='$accode' ") or die(mysqli_error($conn));
        $fetacc = mysqli_fetch_array($getacc);
        $desc = $fetacc['name'];
    }

    if ($source == 'CR') {
        $url = "cashrecp.php?vouchno=$vouchno&mode=edit";
        $url2 = "cashrecp_fr.php?vouchno=$vouchno";
        $desc = "Receipt";

        if ($grpcode == '002') {
            $getcash = mysqli_query($conn, "Select party as accode from `cashrecp` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
        } else {
            $getcash = mysqli_query($conn, "Select bankname as accode from `cashrecp` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
        }
        $fetcash = mysqli_fetch_array($getcash);
        $accode = $fetcash['accode'];

        $getacc = mysqli_query($conn, "Select name from `accmast` where `accode`='$accode' ") or die(mysqli_error($conn));
        $fetacc = mysqli_fetch_array($getacc);
        $desc = $fetacc['name'];
    }

    if ($source == 'TR') {
        $url = "contra.php?vouchno=$vouchno&mode=edit";
        $url2 = "contrec.php?vouchno=$vouchno";
        $desc = "Contra Entry";

        $getcash = mysqli_query($conn, "Select b.name from `contra` a, accmast b 
                                                                                       where a.`vouchno`='$vouchno' and a.frombc = b.accode ") or die(mysqli_error($conn));
        $fetcash = mysqli_fetch_array($getcash);
        $desc = $fetcash['name'];
    }

    if ($source == 'JV') {
        $url = "jventry.php?vouchno=$vouchno&mode=edit";
        $url2 = "jventry.php?vouchno=$vouchno";
        $desc = "JV Entry";

        if ($amt > 0) {
            $getcash = mysqli_query($conn, "Select b.name from `jventry` a, accmast b where 
                                                                                           a.`vouchno`='$vouchno' and a.party = b.accode ") or die(mysqli_error($conn));
        } else {
            $getcash = mysqli_query($conn, "Select b.name from `jventry` a, accmast b where 
                                                                                           a.`vouchno`='$vouchno' and a.bankname = b.accode ") or die(mysqli_error($conn));
        }
        $fetcash = mysqli_fetch_array($getcash);
        $desc = $fetcash['name'];
    }

    $ctr++;
    // Received side data
    // $pdf->SetFont($font, '', 10);
    // $pdf->SetTextColor(0);
    // $pdf->Cell(20, $ht, $vouchdt, 1, 0, 'L');
    // $pdf->Cell(15, $ht, $source, 1, 0, 'L');
    // $pdf->Cell(16, $ht, $couchno, 1, 0, 'L');
    // $pdf->Cell(31, $ht, $desc, 1, 0, 'L');
    // $pdf->Cell(15, $ht, $recamt, 1, 0, 'R');
    // $pdf->ln();

    $totrecp += $recamt;
}

if ($rcount < $lcount) {
    $ext = $lcount - $rcount;
    for ($i = 0; $i < $ext; $i++) {
        // Received side data
        // $pdf->SetFont($font, '', 10);
        // $pdf->SetTextColor(0);
        // $pdf->Cell(20, $ht, '', 1, 0, 'L');
        // $pdf->Cell(15, $ht, '', 1, 0, 'L');
        // $pdf->Cell(16, $ht, '', 1, 0, 'L');
        // $pdf->Cell(31, $ht, '', 1, 0, 'L');
        // $pdf->Cell(15, $ht, '', 1, 0, 'R');
    }
}
// Received side data
// $pdf->SetFont($font, '', 10);
// $pdf->SetTextColor(0);
// $pdf->Cell(20, $ht, '', 1, 0, 'L');
// $pdf->Cell(15, $ht, '', 1, 0, 'L');
// $pdf->Cell(16, $ht, '', 1, 0, 'L');
// $pdf->Cell(31, $ht, '', 1, 0, 'L');
// $pdf->Cell(15, $ht, $totrecp, 1, 0, 'R');
// $pdf->ln();

$lop = 0;
if ($finopen > 0) {
    $ctr++;

    // $pdf->SetFont($font, '', 10);
    // $pdf->SetTextColor(0);
    // $pdf->Cell(20, $ht, '', 1, 0, 'L');
    // $pdf->Cell(15, $ht, '', 1, 0, 'L');
    // $pdf->Cell(16, $ht, 'Opening', 1, 0, 'L');
    // $pdf->Cell(31, $ht, '', 1, 0, 'L');
    // $pdf->Cell(15, $ht, $finop, 1, 0, 'R');
    // $pdf->ln();

    $lop = 1;
    $totpay += $finop;
}

if ($openflag > 0) {

    // $pdf->SetFont($font, '', 10);
    // $pdf->SetTextColor(0);
    // $pdf->Cell(20, $ht, '', 1, 0, 'L');
    // $pdf->Cell(15, $ht, '', 1, 0, 'L');
    // $pdf->Cell(16, $ht, '', 1, 0, 'L');
    // $pdf->Cell(31, $ht, '', 1, 0, 'L');
    // $pdf->Cell(15, $ht, '', 1, 0, 'R');
    // $pdf->ln();
}

?>
<?php
$ctr2 = 0;
$totpay = 0;

while ($payment = mysqli_fetch_array($payqry)) {
    $source = $payment['source'];
    $vouchno = $payment['vouchno'];
    $vouchdt = $payment['vouchdt'];
    $payamt = abs($payment['amount']);
    $amt = $payment['amount'];
    $trcolr = "";

    if ($source == 'PYOU') {
        $url = "payout.php?vouchno=$vouchno&mode=edit";
        $url2 = "payout_fr.php?vouchno=$vouchno";
        $desc = "Payment Out";

        $mtransacdb = 'cashpay';

        $getcash = mysqli_query($conn, "Select party as accode from `cashpay` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
        $fetcash = mysqli_fetch_array($getcash);
        $accode = $fetcash['accode'];

        $getacc = mysqli_query($conn, "Select name from `accmast` where `accode`='$accode' ") or die(mysqli_error($conn));
        $fetacc = mysqli_fetch_array($getacc);
        $desc = $fetacc['name'];

        //                                                        $trn2 = mysqli_query($conn, "SELECT SUM(`setoff`) AS `totadj`, nodays FROM `billsett` WHERE 
        //                                                                                    `billno` = '$gdtl[vouchno]' AND 
        //                                                                                    `party` = '$gdtl[party]' LIMIT 0,1 ") or die(mysqli_error($conn));
        //
        //                                                        $tranqry2 = mysqli_fetch_assoc($trn2);
        //                                                        $totadj = $tranqry2["totadj"];
        //
        //                                                        if ($totadj != $gdtl['netamt']) {
        //                                                            $trcolr = "color:red";
        //                                                        }
    }


    if ($source == 'CP') {
        $url = "cashpay.php?vouchno=$vouchno&mode=edit";
        $url2 = "cashpay_fr.php?vouchno=$vouchno";
        $desc = "Payment";

        if ($grpcode == '002') {
            $getcash = mysqli_query($conn, "Select party as accode from `cashpay` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
        } else {
            $getcash = mysqli_query($conn, "Select bankname as accode from `cashpay` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
        }
        $fetcash = mysqli_fetch_array($getcash);
        $accode = $fetcash['accode'];

        $getacc = mysqli_query($conn, "Select name from `accmast` where `accode`='$accode' ") or die(mysqli_error($conn));
        $fetacc = mysqli_fetch_array($getacc);
        $desc = $fetacc['name'];
    }

    if ($source == 'TR') {
        $url = "contra.php?vouchno=$vouchno&mode=edit";
        $url2 = "contrec.php?vouchno=$vouchno";
        $desc = "Contra Entry";

        $getcash = mysqli_query($conn, "Select b.name from `contra` a, accmast b 
                                                                                       where a.`vouchno`='$vouchno' and a.tobc = b.accode ") or die(mysqli_error($conn));
        $fetcash = mysqli_fetch_array($getcash);
        $desc = $fetcash['name'];
    }

    if ($source == 'JV') {
        $url = "jventry.php?vouchno=$vouchno&mode=edit";
        $url2 = "jventry.php?vouchno=$vouchno";
        $desc = "JV Entry";

        if ($amt > 0) {
            $getcash = mysqli_query($conn, "Select b.name from `jventry` a, accmast b where 
                                                                                           a.`vouchno`='$vouchno' and a.party = b.accode ") or die(mysqli_error($conn));
        } else {
            $getcash = mysqli_query($conn, "Select b.name from `jventry` a, accmast b where 
                                                                                           a.`vouchno`='$vouchno' and a.bankname = b.accode ") or die(mysqli_error($conn));
        }
        $fetcash = mysqli_fetch_array($getcash);
        $desc = $fetcash['name'];
    }

    $ctr2++;
    // $pdf->SetFont($font, '', 10);
    // $pdf->SetTextColor(0);
    // $pdf->Cell(20, $ht, $vouchdt, 1, 0, 'L');
    // $pdf->Cell(15, $ht, $source, 1, 0, 'L');
    // $pdf->Cell(16, $ht, $couchno, 1, 0, 'L');
    // $pdf->Cell(31, $ht, $desc, 1, 0, 'L');
    // $pdf->Cell(15, $ht, $recamt, 1, 0, 'R');
    // $pdf->Ln();

    $totpay += $payamt;
}

if ($lcount < $rcount) {
    $ext = $rcount - $lcount;
    for ($i = 0; $i < $ext; $i++) {
        // Received side data
        $pdf->SetFont($font, '', 10);
        $pdf->SetTextColor(0);
        $pdf->Cell(20, $ht, '', 1, 0, 'L');
        $pdf->Cell(15, $ht, '', 1, 0, 'L');
        $pdf->Cell(16, $ht, '', 1, 0, 'L');
        $pdf->Cell(31, $ht, '', 1, 0, 'L');
        $pdf->Cell(15, $ht, '', 1, 0, 'R');
    }
}

// Received side data
// $pdf->SetTextColor(0);
// $pdf->SetFont($font, '', 10);
// $pdf->SetFillColor(224, 255, 234); // light green
// $pdf->Cell(20, $ht, '', 1, 0, 'L');
// $pdf->Cell(15, $ht, '', 1, 0, 'L');
// $pdf->Cell(16, $ht, '', 1, 0, 'L');
// $pdf->Cell(82, $ht, 'Total', 1, 0, 'R', 1);
// $pdf->Cell(15, $ht, $totpay, 1, 0, 'R', 1);
// $pdf->ln();

$diff = $totrecp - $totpay;

if ($diff > 0) {
    $ctr2++;
    $pdf->SetFillColor(64, 145, 108);  // Green
    $pdf->SetTextColor(255);
    $pdf->Cell(20 + 15 + 16 + 31, $ht, '', 1, 0, 'R', 1); // blank on left
    $pdf->Cell(15, $ht, '', 1, 0, 'R', 1); // blank amount

    $pdf->Cell(20 + 15 + 16 + 31, $ht, 'Closing', 1, 0, 'R', 1);
    $pdf->Cell(15, $ht, $diff, 1, 1, 'R', 1);
    // $pdf->Ln(0);
} else {

    $pdf->SetFillColor(64, 145, 108);  // Green
    $pdf->SetTextColor(255);
    $pdf->Cell(20 + 15 + 16 + 31, $ht, '', 1, 0, 'R', 1); // blank on left
    $pdf->Cell(15, $ht, '', 1, 0, 'R', 1); // blank amount

    $pdf->Cell(20 + 15 + 16 + 31, $ht, 'Closing', 1, 0, 'R', 1);
    $pdf->Cell(15, $ht, abs($diff), 1, 1, 'R', 1);
    $pdf->Ln(0);
}
$pdf->Output(".pdf", "I");
