function slip($_POST, $pure = false)
{
    # get vars
    extract($_POST);
    $empnum += 0;
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($from_day)) {
        $v->isOk($from_day, "num", 1, 2, "Invalid from Date day.");
        $v->isOk($from_month, "num", 1, 2, "Invalid from Date month.");
        $v->isOk($from_year, "num", 1, 4, "Invalid from Date Year.");
        $v->isOk($to_day, "num", 1, 2, "Invalid to Date day.");
        $v->isOk($to_month, "num", 1, 2, "Invalid to Date month.");
        $v->isOk($to_year, "num", 1, 4, "Invalid to Date Year.");
        # mix dates
        $fromdate = $from_year . "-" . $from_month . "-" . $from_day;
        $todate = $to_year . "-" . $to_month . "-" . $to_day;
        if (!checkdate($from_month, $from_day, $from_year)) {
            $v->isOk($fromdate, "num", 1, 1, "Invalid from date.");
        }
        if (!checkdate($to_month, $to_day, $to_year)) {
            $v->isOk($todate, "num", 1, 1, "Invalid to date.");
        }
    } else {
        if (isset($mon)) {
            $v->isOk($mon, "num", 1, 2, "Invalid month selected.");
        }
    }
    $v->isOk($empnum, "num", 1, 14, "Invalid employee selected.");
    # display errors, if any
    if ($v->isError()) {
        $confirmCust = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirmCust .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirmCust .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirmCust;
    }
    #check what we have permission to
    $get_perm = "SELECT payroll_groups FROM users WHERE username = '******'USER_NAME']}' LIMIT 1";
    $run_perm = db_exec($get_perm) or errDie("Unable to get payroll groups permission information.");
    if (pg_numrows($run_perm) > 0) {
        $parr = pg_fetch_array($run_perm);
        if (strlen($parr['payroll_groups']) > 0) {
            $pay_grps = explode(",", $parr['payroll_groups']);
        } else {
            $pay_grps = array();
        }
    } else {
        $pay_grps = array();
    }
    if (isset($emp_group) and is_array($emp_group)) {
        $emp_groups = array();
        $emps = array();
        foreach ($emp_group as $each) {
            if (!in_array($each, $pay_grps)) {
                continue;
            }
            $emp_groups[] = $each;
            $get_emp = "SELECT empnum FROM employees WHERE emp_group = '{$each}'";
            $run_emp = db_exec($get_emp) or errDie("Unable to get employees information.");
            if (pg_numrows($run_emp) > 0) {
                while ($earr = pg_fetch_array($run_emp)) {
                    $emps[] = $earr['empnum'];
                }
            }
        }
    } else {
        #check for which groups we have perm
        $get_check = "SELECT payroll_groups FROM users WHERE username = '******'USER_NAME']}' LIMIT 1";
        $run_check = db_exec($get_check) or errDie("Unable to get employees group permissions.");
        if (pg_numrows($run_check) > 0) {
            $earr = pg_fetch_array($run_check);
            if (strlen($earr['payroll_groups']) > 0) {
                $eperms = explode(",", $earr['payroll_groups']);
                $egsearch = " AND (emp_group = '" . implode("' OR emp_group = '", $eperms) . "')";
            } else {
                $egsearch = "AND FALSE";
            }
        }
        $emp_groups[] = array(0 => '0');
        $get_emp = "SELECT empnum FROM employees WHERE true {$egsearch}";
        $run_emp = db_exec($get_emp) or errDie("Unable to get employees information.");
        while ($earr = pg_fetch_array($run_emp)) {
            $emps[] = $earr['empnum'];
        }
    }
    if (!isset($emps)) {
        $emps = array(0);
    }
    if (in_array('0', $emp_groups)) {
        $show_all = TRUE;
    } else {
        $show_all = FALSE;
    }
    $totgross = 0;
    $totcomm = 0;
    $totins = 0;
    $totuif = 0;
    $totpaye = 0;
    $totded = 0;
    $totsal = 0;
    if (!isset($salyear) or strlen($salyear) < 1) {
        $salyear = EMP_YEAR;
    }
    /* get employee details */
    db_connect();
    if (isset($from_day)) {
        $retfunc = "slctDate";
        if ($empnum != "0") {
            #if not all then use selected employee
            $empw = "empnum='{$empnum}' AND ";
        } else {
            #else use all payslips ... but only with emps in selected group
            if (!$show_all) {
                $empw = "";
                foreach ($emps as $each) {
                    $empw .= "empnum='{$each}' OR ";
                }
                $empw .= "empnum='{$each}'";
            }
        }
        if (substr($empw, -4) == "AND ") {
            $empw = substr($empw, 0, -4);
        }
        if (!isset($empw)) {
            $empw = "true";
        }
        $sql = "SELECT 'salp' AS paytype, * FROM salpaid\n\t\t\t\tWHERE ({$empw}) AND saldate >= '{$fromdate}' AND saldate <= '{$todate}' AND div = '" . USER_DIV . "' AND cyear='{$salyear}'\n\t\t\t\tUNION\n\t\t\t\tSELECT 'salr' AS paytype, * FROM salr\n\t\t\t\tWHERE ({$empw}) AND saldate >= '{$fromdate}' AND saldate <= '{$todate}' AND div = '" . USER_DIV . "' AND cyear='{$salyear}'\n\t\t\t\tORDER BY true_ids ASC";
    } else {
        if (isset($empnum)) {
            $retfunc = "slctEmployee";
            $sql = "SELECT 'salp' AS paytype, * FROM salpaid\n\t\t\t\tWHERE month='{$mon}' AND empnum='{$empnum}' AND div = '" . USER_DIV . "' AND cyear='{$salyear}'\n\t\t\t\tUNION\n\t\t\t\tSELECT 'salr' AS paytype, * FROM salr\n\t\t\t\tWHERE month='{$mon}' AND empnum='{$empnum}' AND div = '" . USER_DIV . "' AND cyear='{$salyear}'\n\t\t\t\tORDER BY true_ids ASC";
        } else {
            invalid_use();
        }
    }
    $pRslt = db_exec($sql) or errDie("Unable to select employee payments from database.");
    if (pg_numrows($pRslt) < 1) {
        return "<li class='err'> - Employee salaries matching the search criteria not found.</li>" . $retfunc();
    }
    $slip = "";
    if (pg_numrows($pRslt) > 0) {
        $empdata = array();
        $empcounter = array();
        while ($pay = pg_fetch_array($pRslt)) {
            $en = $pay["empnum"];
            $mwid = "{$pay['month']}:{$pay['week']}";
            if (!isset($empdata[$en])) {
                $empdata[$en] = array();
            }
            if (!isset($empdata[$en][$mwid])) {
                $empdata[$en][$mwid] = array("gross" => 0, "comm" => 0, "loanins" => 0, "uif" => 0, "paye" => 0, "salary" => 0, "saldate" => "", "payslip" => 0);
            }
            $ed =& $empdata[$en][$mwid];
            $gross = $pay['salary'] - $pay['totallow'] - $pay['comm'] + $pay['totded'] + $pay['uif'] + $pay['paye'] + $pay['loanins'];
            $ed["saldate"] = $pay["saldate"];
            if ($pay["paytype"] == "salp") {
                $ed["gross"] += $gross;
                $ed["comm"] += $pay["comm"];
                $ed["loanins"] += $pay["loanins"];
                $ed["uif"] += $pay["uif"];
                $ed["paye"] += $pay["paye"];
                // 				$ed["totded"] += $pay["totded"];
                $ed["salary"] += $pay["salary"];
                $ed["payslip"] = $pay["id"];
                $totgross += $gross;
                $totcomm += $pay['comm'];
                $totins += $pay['loanins'];
                $totuif += $pay['uif'];
                $totpaye += $pay['paye'];
                $totded += $pay['totded'];
                $totsal += $pay['salary'];
            } else {
                $ed["gross"] -= $gross;
                $ed["comm"] -= $pay["comm"];
                $ed["loanins"] -= $pay["loanins"];
                $ed["uif"] -= $pay["uif"];
                $ed["paye"] -= $pay["paye"];
                // 				$ed["totded"] -= $pay["totded"];
                $ed["salary"] -= $pay["salary"];
                $ed["payslip"] = "{$pay['id']}&rev=true";
                $totgross -= $gross;
                $totcomm -= $pay['comm'];
                $totins -= $pay['loanins'];
                $totuif -= $pay['uif'];
                $totpaye -= $pay['paye'];
                $totded -= $pay['totded'];
                $totsal -= $pay['salary'];
            }
            $get_deds = "SELECT distinct(type) FROM emp_ded WHERE payslip = '{$pay['id']}'";
            $run_deds = db_exec($get_deds) or errDie("Unable to get salary deduction information.");
            if (pg_numrows($run_deds) > 0) {
                // 				$deductions = "";
                // 				$ed["totded"] = array()
                $colspan = 0;
                while ($darr = pg_fetch_array($run_deds)) {
                    $darr['type'] += 0;
                    if ($darr['type'] > 0) {
                        print "adding a deduction<br>";
                        $get_amt = "SELECT amount, description FROM emp_ded WHERE payslip = '{$pay['id']}' AND type = '{$darr['type']}' LIMIT 1";
                        $run_amt = db_exec($get_amt) or errDie("Unable to get employee deduction amount.");
                        $deduction_heading .= "<th>" . pg_fetch_result($run_amt, 0, 1) . "</th>";
                        $ed["totded"][] = "<td nowrap>" . CUR . " " . sprint(pg_fetch_result($run_amt, 0, 0)) . "</td>";
                        // 						$deductions .= "<td nowrap>".CUR." ".sprint($darr['amount'])."</td>";
                        $colspan++;
                    }
                }
            } else {
                // 				$deductions = "";
                $colspan = 1;
            }
        }
        print "----------<br>";
        print "<pre>";
        var_dump($ed);
        print "</pre>";
        print "<br>>>>>>>>>>>>>>>>>>>>><br>";
        // print "<pre>";
        // var_dump ($
        foreach ($empdata as $empnum => $months) {
            foreach ($months as $monthweek => $sal) {
                list($month, $week) = explode(":", $monthweek);
                if (($emp = qryEmployee($empnum, "fnames, sname, basic_sal, payprd")) === false) {
                    $emp = qryLEmployee($empnum, "fnames, sname, basic_sal, payprd");
                }
                // not a date range but a single employee, store the name
                if (!isset($from_day)) {
                    $empname = "{$emp['fnames']} {$emp['sname']}";
                }
                /* create month week description */
                $mw_desc = getMonthName($month);
                // weekly
                if ($emp["payprd"] == "w") {
                    $mw_desc .= ", Week {$week}";
                    // fortnightly
                } else {
                    if ($emp["payprd"] == "f") {
                        if ($week == 1) {
                            $week = "1-2";
                        } else {
                            if ($week == 2) {
                                $week = "3-4";
                            } else {
                                $week = "5";
                            }
                        }
                        $mw_desc .= ", Week {$week}";
                    }
                }
                print "<pre>";
                var_dump($sal["totded"]);
                print "</pre>";
                $bgColor = bgcolorg();
                $slip .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$emp['fnames']} {$emp['sname']}</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["gross"]) . "</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["comm"]) . "</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["loanins"]) . "</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["uif"]) . "</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["paye"]) . "</td>\n\t\t\t\t\t\t" . implode("", $sal["totded"]) . "\n\t\t\t\t\t\t<td nowrap>" . CUR . " " . sprint($sal["salary"]) . "</td>\n\t\t\t\t\t\t<td nowrap>{$mw_desc}</td>\n\t\t\t\t\t\t<td nowrap>{$sal['saldate']}</td>";
                if (!$pure) {
                    $slip .= "\n\t\t\t\t\t\t<td><a href='payslip-view.php?empnum={$empnum}&id={$sal['payslip']}'>View</a></td>\n\t\t\t\t\t\t<td><a target='_blank' href='payslip-print.php?id={$sal['payslip']}'>Print</a></td>";
                }
                $slip .= "</tr>";
            }
        }
        # Format the totals
        $totgross = sprint($totgross);
        $totcomm = sprint($totcomm);
        $totins = sprint($totins);
        $totuif = sprint($totuif);
        $totpaye = sprint($totpaye);
        $totded = sprint($totded);
        $totsal = sprint($totsal);
        $slip .= "\n\t\t\t<tr class='bg-even'>\n\t\t\t\t<td><b>Total</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totgross}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totcomm}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totins}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totuif}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totpaye}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totded}</b></td>\n\t\t\t\t<td nowrap><b>" . CUR . " {$totsal}</b></td>\n\t\t\t\t<td colspan='4'></td>\n\t\t\t</tr>";
    } else {
        return "<li> - There are no salary payments for the selected month</li>";
    }
    if (isset($from_day)) {
        $title = "<h3>Salaries Paid {$fromdate} TO {$todate}</h3>";
    } else {
        $title = "<h3>Salaries for {$empname}</h3>";
    }
    $slip = "\n\t\t<center>\n\t\t{$title}\n\t\t<table " . TMPL_tblDflts . " width='70%'>\n\t\t\t<tr>\n\t\t\t\t<th>Employee</th>\n\t\t\t\t<th>Gross Salary</th>\n\t\t\t\t<th>Commission</th>\n\t\t\t\t<th>Low or interest free loan</th>\n\t\t\t\t<th>UIF</th>\n\t\t\t\t<th>PAYE</th>\n\t\t\t\t<th colspan='{$colspan}'>Deductions</th>\n\t\t\t\t<th>Nett Income</th>\n\t\t\t\t<th>Month/Week</th>\n\t\t\t\t<th>Payment Date</th>\n\t\t\t\t" . (!$pure ? "<th colspan='2'>Options</th>" : "") . "\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<th colspan='6'></th>\n\t\t\t\t{$deduction_heading}\n\t\t\t\t<th colspan='5'></th>\n\t\t\t</tr>\n\t\t\t{$slip}\n\t\t\t" . TBL_BR;
    if (!$pure) {
        $slip .= "\n\t\t\t\t<form action='" . SELF . "' method='POST'>\n\t\t\t\t" . array2form($_REQUEST) . "\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2'><input name=key type=submit value='Export to Spreadsheet'></td>\n\t\t\t\t</tr>\n\t\t\t</form>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee")) . "\n\t\t\t</td></tR>";
    }
    $slip .= "\n\t\t</table>\n\t\t</center>";
    return $slip;
}
Ejemplo n.º 2
0
function viewtran($_POST)
{
    global $MONPRD, $PRDMON;
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($accnt, "string", 1, 10, "Invalid Accounts Selection.");
    if ($accnt == 'slct') {
        if (isset($accids)) {
            foreach ($accids as $key => $accid) {
                $v->isOk($accid, "num", 1, 20, "Invalid Account number.");
            }
        } else {
            return "<li class='err'>Please select at least one account.</li>" . slctacc();
        }
    }
    if ($v->isError()) {
        $err = $v->genErrors();
        return $confirm;
    }
    if ($_POST["key"] == "export") {
        $pure = true;
    } else {
        $pure = false;
    }
    if ($accnt == 'all') {
        $accids = array();
        core_connect();
        $sql = "SELECT accid FROM accounts WHERE div = '" . USER_DIV . "'";
        $rs = db_exec($sql);
        if (pg_num_rows($rs) > 0) {
            while ($ac = pg_fetch_array($rs)) {
                $accids[] = $ac['accid'];
            }
        } else {
            return "<li calss='err'> There are no accounts yet in Cubit.</li>";
        }
    } else {
        if ($accnt == "allactive") {
            $accids = array();
            $sql = "SELECT accid FROM core.trial_bal\n\t\t\t\tWHERE (debit!=0 OR credit!=0) AND div='" . USER_DIV . "'\n\t\t\t\t\tAND period<='" . $MONPRD[PRD_DB] . "'\n\t\t\t\tGROUP BY accid";
            $qry = new dbSql($sql);
            $qry->run();
            while ($macc_data = $qry->fetch_array()) {
                $accids[] = $macc_data["accid"];
            }
        }
    }
    $hide = "";
    $trans = "";
    foreach ($accids as $key => $accid) {
        $accRs = get("core", "accname, accid, topacc, accnum", "accounts", "accid", $accid);
        $acc = pg_fetch_array($accRs);
        $trans .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='8'><b>{$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}</b></td>\n\t\t\t</tr>";
        db_conn("audit");
        $sql = "SELECT prd.*, map.period FROM audit.closedprd prd, core.prdmap map\n\t\t\t\tWHERE prd.prdnum=map.month\n\t\t\t\tORDER BY map.period::integer";
        $clsRs = db_exec($sql) or errDie("Could not get closed periods from audit DB", SELF);
        while ($cls = pg_fetch_array($clsRs)) {
            $prd = $cls['prdnum'];
            # Period name
            $prdname = prdname($prd);
            $trans .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='8' align='center'><h3>{$prdname}</h3></td>\n\t\t\t\t</tr>";
            $hide = "";
            if (isset($t)) {
                unset($t);
            }
            # Get balances
            $idRs = get($prd, "max(id), min(id)", "ledger", "acc", $accid);
            $id = pg_fetch_array($idRs);
            if ($id['min'] != 0) {
                $balRs = get($prd, "(cbalance-credit) as cbalance,(dbalance-debit) as dbalance", "ledger", "id", $id['min']);
                $bal = pg_fetch_array($balRs);
                $cbalRs = get($prd, "cbalance,dbalance", "ledger", "id", $id['max']);
                $cbal = pg_fetch_array($cbalRs);
            } else {
                if (!isset($t)) {
                    $trans .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td colspan='8' align='center'><li> There are no transactions in this period.</td>\n\t\t\t\t\t\t</tr>";
                }
                continue;
                $balRs = get("core", "credit as cbalance, debit as dbalance", "trial_bal", "accid", $accid);
                $bal = pg_fetch_array($balRs);
                $cbal['cbalance'] = 0;
                $cbal['dbalance'] = 0;
            }
            $t = "lemme ci";
            if ($bal['dbalance'] > $bal['cbalance']) {
                $bal['dbalance'] = sprint($bal['dbalance'] - $bal['cbalance']);
                $bal['cbalance'] = "";
                $balance = $bal['dbalance'];
                $fl = "DT";
            } elseif ($bal['cbalance'] > $bal['dbalance']) {
                $bal['cbalance'] = sprint($bal['cbalance'] - $bal['dbalance']);
                $bal['dbalance'] = "";
                $balance = $bal['cbalance'];
                $fl = "CT";
            } else {
                $bal['cbalance'] = "";
                $bal['dbalance'] = "";
                $balance = "0.00";
                $fl = "";
            }
            $balance = sprint($balance);
            $bal['cbalance'] = sprint($bal['cbalance']);
            $bal['dbalance'] = sprint($bal['dbalance']);
            // calculate which year the current period is in
            $prd_y = getFinYear() - 1;
            if ($prd < $PRDMON[1]) {
                ++$prd_y;
            }
            // make the date of the last day of the previous prd
            $bbf_date = date("t-M-Y", mktime(0, 0, 0, $prd - 1, 1, $prd_y));
            $trans .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2' align='right'>{$bbf_date}</td>\n\t\t\t\t\t<td>Br/Forwd</td><td>Brought Forward</td>\n\t\t\t\t\t<td align='right'>{$bal['dbalance']}</td>\n\t\t\t\t\t<td align='right'>{$bal['cbalance']}</td>\n\t\t\t\t\t<td align='right'>{$balance} {$fl}</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t</tr>";
            # --> Transaction reding comes here <--- #
            $dbal['debit'] = 0;
            $dbal['credit'] = 0;
            $tranRs = get($prd, "*", "ledger", "acc", $accid);
            while ($tran = pg_fetch_array($tranRs)) {
                $dbal['debit'] += $tran['debit'];
                $dbal['credit'] += $tran['credit'];
                # Current(Running) balance
                if ($tran['dbalance'] > $tran['cbalance']) {
                    $tran['dbalance'] = sprint($tran['dbalance'] - $tran['cbalance']);
                    $tran['cbalance'] = "";
                    $cbalance = $tran['dbalance'];
                    $cfl = "DT";
                } elseif ($tran['cbalance'] > $tran['dbalance']) {
                    $tran['cbalance'] = sprint($tran['cbalance'] - $tran['dbalance']);
                    $tran['dbalance'] = "";
                    $cbalance = $tran['cbalance'];
                    $cfl = "CT";
                } else {
                    $tran['cbalance'] = "";
                    $tran['dbalance'] = "";
                    $cbalance = "0.00";
                    $cfl = "";
                }
                # Format date
                $tran['edate'] = explode("-", $tran['edate']);
                $tran['edate'] = $tran['edate'][2] . "-" . $tran['edate'][1] . "-" . $tran['edate'][0];
                $tran['debit'] = sprint($tran['debit']);
                $tran['credit'] = sprint($tran['credit']);
                $trans .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><br></td>\n\t\t\t\t\t\t<td>{$tran['edate']}</td>\n\t\t\t\t\t\t<td>{$tran['eref']}</td>\n\t\t\t\t\t\t<td>{$tran['descript']}</td>\n\t\t\t\t\t\t<td align='right'>{$tran['debit']}</td>\n\t\t\t\t\t\t<td align='right'>{$tran['credit']}</td>\n\t\t\t\t\t\t<td align='right'>{$cbalance} {$cfl}</td>\n\t\t\t\t\t\t<td>{$tran['ctopacc']}/{$tran['caccnum']} - {$tran['caccname']}</td>\n\t\t\t\t\t</tr>";
            }
            # Total balance changes
            if ($dbal['debit'] > $dbal['credit']) {
                $dbal['debit'] = sprint($dbal['debit'] - $dbal['credit']);
                $dbal['credit'] = "";
            } elseif ($dbal['credit'] > $dbal['debit']) {
                $dbal['credit'] = sprint($dbal['credit'] - $dbal['debit']);
                $dbal['debit'] = "";
            } else {
                $dbal['credit'] = "0.00";
                $dbal['debit'] = "0.00";
            }
            $trans .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'><br></td>\n\t\t\t\t\t<td>A/C Total</td>\n\t\t\t\t\t<td>Total for period {$prdname} to Date :</td>\n\t\t\t\t\t<td align='right'>{$dbal['debit']}</td>\n\t\t\t\t\t<td align='right'>{$dbal['credit']}</td>\n\t\t\t\t\t<td align='right'></td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t</tr>";
        }
        $trans .= "<tr><td colspan='8'><br></td></tr>";
    }
    $OUT = "";
    if (!$pure) {
        $OUT .= "<center>";
    }
    $OUT .= "\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<td colspan='8' align='center'><h3>Year Review General Ledger</h3></td>\n\t\t\t</tr>";
    if (!$pure) {
        $OUT .= "\n\t\t<tr>\n\t\t\t<form action='" . SELF . "' method='post'>\n\t\t\t<input type='hidden' name='key' value='export' />\n\t\t\t<input type='hidden' name='prd' value='{$prd}' />\n\t\t\t<input type='hidden' name='accnt' value='{$accnt}' />\n\t\t\t" . array2form($accids, "accids") . "\n\t\t\t<td colspan='8' align='center'>\n\t\t\t\t<input type='submit' value='Export to Spreadsheet'>\n\t\t\t</td>\n\t\t\t</form>\n\t\t</tr>\n\t\t" . TBL_BR;
    }
    $OUT .= "\n\t<tr>\n\t\t<th>&nbsp;</th>\n\t\t<th>Date</th>\n\t\t<th>Reference</th>\n\t\t<th>Description</th>\n\t\t<th>Debit</th>\n\t\t<th>Credit</th>\n\t\t<th>Balance</th>\n\t\t<th>Contra Acc</th>\n\t</tr>\n\t{$trans}\n\t</table>";
    if (!$pure) {
        $OUT .= mkQuickLinks(ql("index-reports.php", "Financials"), ql("index-reports-journal.php", "Current Year Details General Ledger Reports"), ql("../core/acc-new2.php", "Add New Account"));
        $OUT .= "\n\t\t</center>";
    }
    return $OUT;
}
function viewtran($_POST)
{
    global $MONPRD, $PRDMON;
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($accnt, "string", 1, 10, "Invalid Accounts Selection.");
    if ($accnt == 'slct') {
        if (isset($accids)) {
            foreach ($accids as $key => $accid) {
                $v->isOk($accid, "num", 1, 20, "Invalid Account number.");
            }
        } else {
            return "<li class='err'>Please select at least one account.</li>" . slctacc();
        }
    }
    if ($v->isError()) {
        $err = $v->genErrors();
        return $confirm;
    }
    if ($_POST["key"] == "export") {
        $pure = true;
    } else {
        $pure = false;
    }
    #get list of which accounts to show
    if ($accnt == 'all') {
        $accids = array();
        core_connect();
        $sql = "SELECT accid FROM accounts WHERE div = '" . USER_DIV . "'";
        $rs = db_exec($sql);
        if (pg_num_rows($rs) > 0) {
            while ($ac = pg_fetch_array($rs)) {
                $accids[] = $ac['accid'];
            }
        } else {
            return "<li calss='err'> There are no accounts yet in Cubit.</li>";
        }
    } else {
        if ($accnt == "allactive") {
            $accids = array();
            //print "->$fin_year<-";
            //		if ($fin_year != "0"){
            //			$accsql = array ();
            //			for ($x=1;$x<13;$x++){
            //				$month = date ("F",mktime (0,0,0,$x,1,substr($fin_year,1)));
            //				$accsql[] = "SELECT debit,credit FROM $month";
            //			}
            //			$sql = implode (" UNION ",$accsql);
            //			db_conn($fin_year."_audit");
            //			$run_sql = db_exec($sql) or errDie ("Unable to get previous year information.");
            //			while ($rarr = pg_fetch_array ($run_sql)){
            //				$accids[] = $rarr['debit'];
            //				$accids[] = $rarr['credit'];
            //				$accids = array_unique($accids);
            //			}
            //
            //		}else {
            $sql = "SELECT accid FROM core.trial_bal\n\t\t\t\t\tWHERE (debit!=0 OR credit!=0) AND div='" . USER_DIV . "'\n\t\t\t\t\t\tAND period<='" . $MONPRD[PRD_DB] . "'\n\t\t\t\t\tGROUP BY accid";
            $qry = new dbSql($sql);
            $qry->run();
            while ($macc_data = $qry->fetch_array()) {
                $accids[] = $macc_data["accid"];
            }
            //		}
        }
    }
    $hide = "";
    $trans = "";
    foreach ($accids as $key => $accid) {
        $accRs = get("core", "accname, accid, topacc, accnum", "accounts", "accid", $accid);
        $acc = pg_fetch_array($accRs);
        $tran_flag = FALSE;
        $the_trans = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='8'><b>{$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}</b></td>\n\t\t\t</tr>";
        db_conn("audit");
        #go through SELECTED periods ...
        $cp = $fprd;
        $fs = 0;
        if ($fprd == $tprd + 1) {
            $f = true;
        } else {
            $f = false;
        }
        $balRs = get("core", "credit as cbalance, debit as dbalance", "trial_bal", "accid", $accid);
        $bal = pg_fetch_array($balRs);
        if ($bal['dbalance'] > $bal['cbalance']) {
            $bal['dbalance'] = sprint($bal['dbalance'] - $bal['cbalance']);
            $bal['cbalance'] = "";
            $balance = $bal['dbalance'];
            $fl = "DT";
        } elseif ($bal['cbalance'] > $bal['dbalance']) {
            $bal['cbalance'] = sprint($bal['cbalance'] - $bal['dbalance']);
            $bal['dbalance'] = "";
            $balance = $bal['cbalance'];
            $fl = "CT";
        } else {
            $bal['cbalance'] = "";
            $bal['dbalance'] = "";
            $balance = "0.00";
            $fl = "";
        }
        // calculate which year the current period is in
        $prd_y = getFinYear() - 1;
        //		if ($prd < $PRDMON[1]) {
        //			++$prd_y;
        //		}
        //"t-M-Y"
        // make the date of the last day of the previous prd
        $bbf_date = date("t F Y", mktime(0, 0, 0, $PRDMON[1] - 1, 1, $prd_y));
        if ($openbal == "show" and ($bal['dbalance'] > 0 or $bal['cbalance'] > 0)) {
            $tran_flag = TRUE;
            $the_trans .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td nowrap colspan='2' align='right'>{$bbf_date}</td>\n\t\t\t\t\t\t<td>Br/Forwd</td><td>Brought Forward</td>\n\t\t\t\t\t\t<td align='right'>{$bal['dbalance']}</td>\n\t\t\t\t\t\t<td align='right'>{$bal['cbalance']}</td>\n\t\t\t\t\t\t<td align='right'>{$balance} {$fl}</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t</tr>";
        }
        while ($cp != $tprd + 1 || $f) {
            $prd = $cp;
            $cp++;
            $fs++;
            if ($cp == 13) {
                $cp = 1;
            }
            if ($fs > 13) {
                break;
            }
            $f = false;
            # Period name
            $prdname = prdname($prd);
            $hide = "";
            if (isset($t)) {
                unset($t);
            }
            # Get balances
            $idRs = get($prd, "max(id), min(id)", "ledger", "acc", $accid);
            $id = pg_fetch_array($idRs);
            if ($id['min'] != 0) {
                #at least 1 entry found for this period ...
                $balRs = get($prd, "(cbalance-credit) as cbalance,(dbalance-debit) as dbalance", "ledger", "id", $id['min']);
                $bal = pg_fetch_array($balRs);
                $cbalRs = get($prd, "cbalance,dbalance", "ledger", "id", $id['max']);
                $cbal = pg_fetch_array($cbalRs);
            } else {
                if (!isset($t)) {
                    //					$the_trans .= "
                    //						<tr class='".bg_class()."'>
                    //							<td colspan='8' align='center'><li> There are no transactions in this period.</td>
                    //						</tr>";
                }
                continue;
                $balRs = get("core", "credit as cbalance, debit as dbalance", "trial_bal", "accid", $accid);
                $bal = pg_fetch_array($balRs);
                $cbal['cbalance'] = 0;
                $cbal['dbalance'] = 0;
            }
            $t = "lemme ci";
            if ($bal['dbalance'] > $bal['cbalance']) {
                $bal['dbalance'] = sprint($bal['dbalance'] - $bal['cbalance']);
                $bal['cbalance'] = "";
                $balance = $bal['dbalance'];
                $fl = "DT";
            } elseif ($bal['cbalance'] > $bal['dbalance']) {
                $bal['cbalance'] = sprint($bal['cbalance'] - $bal['dbalance']);
                $bal['dbalance'] = "";
                $balance = $bal['cbalance'];
                $fl = "CT";
            } else {
                $bal['cbalance'] = "";
                $bal['dbalance'] = "";
                $balance = "0.00";
                $fl = "";
            }
            $balance = sprint($balance);
            $bal['cbalance'] = sprint($bal['cbalance']);
            $bal['dbalance'] = sprint($bal['dbalance']);
            // calculate which year the current period is in
            $prd_y = getFinYear() - 1;
            if ($prd < $PRDMON[1]) {
                ++$prd_y;
            }
            # --> Transaction reding comes here <--- #
            $dbal['debit'] = 0;
            $dbal['credit'] = 0;
            #go through all the transactions
            $tranRs = get($prd, "*", "ledger", "acc", $accid, "ORDER BY id");
            while ($tran = pg_fetch_array($tranRs)) {
                $dbal['debit'] += $tran['debit'];
                $dbal['credit'] += $tran['credit'];
                # Current(Running) balance
                if ($tran['dbalance'] > $tran['cbalance']) {
                    $tran['dbalance'] = sprint($tran['dbalance'] - $tran['cbalance']);
                    $tran['cbalance'] = "";
                    $cbalance = $tran['dbalance'];
                    $cfl = "DT";
                } elseif ($tran['cbalance'] > $tran['dbalance']) {
                    $tran['cbalance'] = sprint($tran['cbalance'] - $tran['dbalance']);
                    $tran['dbalance'] = "";
                    $cbalance = $tran['cbalance'];
                    $cfl = "CT";
                } else {
                    $tran['cbalance'] = "";
                    $tran['dbalance'] = "";
                    $cbalance = "0.00";
                    $cfl = "";
                }
                # Format date
                $tran['edate'] = explode("-", $tran['edate']);
                $tran['edate'] = $tran['edate'][2] . "-" . $tran['edate'][1] . "-" . $tran['edate'][0];
                $tran['debit'] = sprint($tran['debit']);
                $tran['credit'] = sprint($tran['credit']);
                #only show the transaction if it actually DOES something ...
                if ($tran['debit'] != 0 or $tran['credit'] != 0) {
                    $tran_flag = TRUE;
                    if (strlen($tran['edate']) == 10) {
                        $darr = explode("-", $tran['edate']);
                        $tran['edate'] = date("t F Y", mktime(0, 0, 0, $darr[1], $darr[0], $darr[2]));
                    }
                    $the_trans .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><br></td>\n\t\t\t\t\t\t\t<td nowrap>{$tran['edate']}</td>\n\t\t\t\t\t\t\t<td>{$tran['eref']}</td>\n\t\t\t\t\t\t\t<td>{$tran['descript']}</td>\n\t\t\t\t\t\t\t<td nowrap align='right'>{$tran['debit']}</td>\n\t\t\t\t\t\t\t<td nowrap align='right'>{$tran['credit']}</td>\n\t\t\t\t\t\t\t<td nowrap align='right'>{$cbalance} {$cfl}</td>\n\t\t\t\t\t\t\t<td nowrap>{$tran['ctopacc']}/{$tran['caccnum']} - {$tran['caccname']}</td>\n\t\t\t\t\t\t</tr>";
                }
            }
            # Total balance changes
            if ($dbal['debit'] > $dbal['credit']) {
                $dbal['debit'] = sprint($dbal['debit'] - $dbal['credit']);
                $dbal['credit'] = "";
            } elseif ($dbal['credit'] > $dbal['debit']) {
                $dbal['credit'] = sprint($dbal['credit'] - $dbal['debit']);
                $dbal['debit'] = "";
            } else {
                $dbal['credit'] = "0.00";
                $dbal['debit'] = "0.00";
            }
            //			$trans .= "
            //				<tr class='".bg_class()."'>
            //					<td colspan='2'><br></td>
            //					<td>A/C Total</td>
            //					<td>Total for period $prdname to Date :</td>
            //					<td align='right'>$dbal[debit]</td>
            //					<td align='right'>$dbal[credit]</td>
            //					<td align='right'></td>
            //					<td> </td>
            //				</tr>";
            if ($tran_flag) {
                $trans .= $the_trans;
                $the_trans = "";
            }
        }
        if ($tran_flag) {
            $trans .= "<tr><td colspan='8'><br></td></tr>";
        }
    }
    $OUT = "";
    if (!$pure) {
        $OUT .= "<center>";
    }
    $OUT .= "\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<td colspan='8' align='center'><h3>General Ledger Report</h3></td>\n\t\t\t</tr>";
    if (!$pure) {
        $OUT .= "\n\t\t<tr>\n\t\t\t<form action='" . SELF . "' method='post'>\n\t\t\t\t<input type='hidden' name='key' value='export' />\n\t\t\t\t<input type='hidden' name='prd' value='{$prd}' />\n\t\t\t\t<input type='hidden' name='accnt' value='{$accnt}' />\n\t\t\t\t<input type='hidden' name='fprd' value='{$fprd}' />\n\t\t\t\t<input type='hidden' name='tprd' value='{$tprd}' />\n\t\t\t\t<input type='hidden' name='openbal' value='{$openbal}' />\n\t\t\t\t" . array2form($accids, "accids") . "\n\t\t\t\t<td colspan='8' align='center'>\n\t\t\t\t\t<input type='submit' value='Export to Spreadsheet'>\n\t\t\t\t</td>\n\t\t\t</form>\n\t\t</tr>\n\t\t" . TBL_BR;
    }
    $OUT .= "\n\t<tr>\n\t\t<th>&nbsp;</th>\n\t\t<th>Date</th>\n\t\t<th>Reference</th>\n\t\t<th>Description</th>\n\t\t<th>Debit</th>\n\t\t<th>Credit</th>\n\t\t<th>Balance</th>\n\t\t<th>Contra Acc</th>\n\t</tr>\n\t{$trans}\n\t</table>";
    if (!$pure) {
        $OUT .= mkQuickLinks(ql("index-reports.php", "Financials"), ql("index-reports-journal.php", "Current Year Details General Ledger Reports"), ql("../core/acc-new2.php", "Add New Account"));
        $OUT .= "\n\t\t</center>";
    }
    return $OUT;
}
Ejemplo n.º 4
0
/**
 * returns to an "return2step" session
 *
 * @param int $seq r2s session id
 */
function r2s_return($seq)
{
    if (!isset($_SESSION["R2S_PAGE_{$seq}"])) {
        return;
    }
    $page = $_SESSION["R2S_PAGE_{$seq}"];
    $post = unserialize($_SESSION["R2S_POST_{$seq}"]);
    $get = unserialize($_SESSION["R2S_GET_{$seq}"]);
    $gets = array();
    foreach ($get as $n => $v) {
        $gets[] = "{$n}={$v}";
    }
    $get = implode("&", $gets);
    $OUTPUT = "\n\t<body>\n\t<form name='r2sfrm{$seq}' method='post' action='{$page}?{$get}'>";
    $OUTPUT .= array2form($post);
    $OUTPUT .= "\n\t</form>\n\t<script>\n\t\tdocument.r2sfrm{$seq}.submit();\n\t</script>\n\t</body>";
    r2s_destroy($seq);
    require relpath("template.php");
}