function write($frm)
{
    extract($_REQUEST);
    if ($frm->validate("write")) {
        return confirm($frm);
    }
    pglib_transaction("BEGIN");
    db_conn('cubit');
    $user = USER_NAME;
    $Sql = "SELECT * FROM assets WHERE (dep_month='yes' AND remaction IS NULL)";
    $Rslt = db_exec($Sql) or errDie("Unable to access database.");
    $cc = "";
    $todate = mkdate($date_year, $date_month, $date_day);
    $ttime = mktimefd($todate);
    $refnum = getrefnum($todate);
    while ($led = pg_fetch_array($Rslt)) {
        if (empty($led["autodepr_date"])) {
            $led["autodepr_date"] = $led["date"];
        }
        explodeDate($led["autodepr_date"], $date_year, $date_month, $date_day);
        $ftime = mktime(0, 0, 0, $date_month, $date_day, $date_year);
        $depmonths = 0;
        while ($ftime < $ttime) {
            ++$depmonths;
            $ftime = mktime(0, 0, 0, $date_month + $depmonths, $date_day, $date_year);
        }
        if ($depmonths == 0) {
            continue;
        }
        $depperc = $led["dep_perc"];
        $ml_perc = $depperc * ($depmonths % 12 / 12);
        $years = ($depmonths - $depmonths % 12) / 12;
        $baseamt = $led["amount"] - $led["accdep"];
        $depamt = 0;
        /* yearly depreciations */
        for ($i = 1; $i <= $years; ++$i) {
            $depamt += ($baseamt - $depamt) * ($depperc / 100);
        }
        /* monthly depreciation */
        $depamt += ($baseamt - $depamt) * ($ml_perc / 100);
        $sql = "SELECT * FROM assetgrp WHERE grpid = '{$led['grpid']}' AND div = '" . USER_DIV . "'";
        $grpRslt = db_exec($sql);
        $grp = pg_fetch_array($grpRslt);
        writetrans($grp['depacc'], $grp['accdacc'], $todate, $refnum, $depamt, "{$led['des']} Depreciation");
        db_connect();
        $sql = "UPDATE assets SET accdep = (accdep + '{$depamt}'), autodepr_date='{$todate}'\r\n\t\t\t\tWHERE (id='{$led['id']}' AND div = '" . USER_DIV . "')";
        db_exec($sql) or errdie("Could not update assets table.");
        $snetval = $baseamt - $depamt;
        $sdate = date("Y-m-d");
        $sql = "INSERT INTO assetledger(assetid, asset, date, depamt, netval, div) \r\n\t\t\t\tVALUES ('{$led['id']}', '{$led['des']}', '{$todate}', '{$depamt}', '{$snetval}', '" . USER_DIV . "')";
        db_exec($sql) or errdie("Could not write to asset ledger.");
        $cc .= "CostCenter('ct', 'Asset Depreciation', '{$todate}', '{$led['des']} Depreciation', '{$depamt}', '');";
    }
    pglib_transaction("COMMIT");
    $write = "\r\n\t<script> \r\n\t{$cc}\r\n\t</script>\r\n\t<table " . TMPL_tblDflts . " width='50%'>\r\n\t\t<tr>\r\n\t\t\t<th>Auto Asset Depreciation</th>\r\n\t\t</tr>\r\n\t\t<tr class='datacell'>\r\n\t\t\t<td>Asset Depreciation has calculated and recorded.</td>\r\n\t\t</tr>\r\n\t</table>";
    return $write;
}
Exemplo n.º 2
0
function genslip($empnum, $fromdate, $todate)
{
    $Sl = "SELECT * FROM employees WHERE empnum='{$empnum}'";
    $Ry = db_exec($Sl) or errDie("Unable to select employees from database.");
    if (pg_numrows($Ry) < 1) {
        $Sl = "SELECT * FROM lemployees WHERE empnum='{$empnum}'";
        $Ry = db_exec($Sl) or errDie("Unable to select employees from database.");
    }
    $emp = pg_fetch_array($Ry);
    $pay['showex'] = "Yes";
    $date = $todate;
    //$pw = "saldate>='$fromdate' AND saldate<='$todate'";
    $from_month = extractMonth($fromdate);
    $to_month = extractMonth($todate);
    if ($to_month < $from_month) {
        $pw = "month::int>='{$from_month}' OR month::int <= '{$to_month}'";
    } else {
        $pw = "month::int>='{$from_month}' AND month::int <= '{$to_month}'";
    }
    $pw = "({$pw}) AND (saldate>='{$fromdate}' AND saldate<='{$todate}')";
    /* paye balance */
    $sql = "SELECT sum(paye) AS sum FROM salpaid WHERE ({$pw}) AND empnum='{$empnum}' AND cyear='" . EMP_YEAR . "'";
    $Ry = db_exec($sql) or errDie("Unable to get paye");
    $pdata = pg_fetch_array($Ry);
    $paid = $pdata['sum'];
    $sql = "SELECT sum(paye) AS sum FROM salr WHERE ({$pw}) AND empnum='{$empnum}' AND cyear='" . EMP_YEAR . "'";
    $Ry = db_exec($sql) or errDie("Unable to get paye");
    $pdata = pg_fetch_array($Ry);
    $upaid = $pdata['sum'];
    $tottax = sprint($paid - $upaid);
    /* salary balance */
    $sql = "SELECT sum(salary) FROM salpaid WHERE ({$pw}) AND empnum='{$empnum}' AND cyear='" . EMP_YEAR . "'";
    $Ry = db_exec($sql) or errDie("Unable to get paye");
    $pdata = pg_fetch_array($Ry);
    $sql = "SELECT sum(salary) FROM salr WHERE ({$pw}) AND empnum='{$empnum}' AND cyear='" . EMP_YEAR . "'";
    $Ry = db_exec($sql) or errDie("Unable to get paye");
    $prdata = pg_fetch_array($Ry);
    $pay['salary'] = $pdata['sum'] - $prdata["sum"];
    $emp['basic_sal'] = sprint($emp['basic_sal']);
    /* pay slip ids */
    $psids = array();
    $sql = "\n\t\tSELECT id, novert, hovert FROM cubit.salpaid WHERE ({$pw}) AND empnum='{$empnum}' AND cyear='" . EMP_YEAR . "' \n\t\tUNION \n\t\tSELECT -id, -novert, -hovert FROM cubit.salr WHERE ({$pw}) AND empnum='{$empnum}' AND cyear='" . EMP_YEAR . "'";
    $rslt = db_exec($sql) or errDie("Error reading payslip ids");
    $novert = 0;
    $hovert = 0;
    while ($row = pg_fetch_assoc($rslt)) {
        $psids[] = "payslip='{$row['id']}'";
        $novert += $row["novert"];
        $hovert += $row["hovert"];
    }
    if (count($psids) <= 0) {
        $psids[] = "true";
    }
    $pwc = "(" . implode(" OR ", $psids) . ")";
    $dates = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<td width='50%'>Date</td>\n\t\t\t\t<td width='50%'>{$date}</td>\n\t\t\t</tr>\n\t\t</table>";
    $i = 0;
    $epw = "date >= '{$fromdate}' AND date <= '{$todate}'";
    $fepw = "fdate >= '{$fromdate}' AND fdate <= '{$todate}'";
    $incomes = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<td width='80%' align='center'>Description</td>\n\t\t\t\t<td align='center'>Amount</td>\n\t\t\t</tr>";
    db_conn('cubit');
    $sql = "SELECT DISTINCT description, type FROM emp_inc WHERE ({$pwc}) AND emp='{$empnum}' AND ({$epw}) ORDER BY description";
    $Ri = db_exec($sql) or errDie("Unable to get data.");
    $tot_incomes = 0;
    while ($data = pg_fetch_array($Ri)) {
        $sql = "SELECT sum(amount) FROM emp_inc WHERE description='{$data['description']}' AND ({$pwc}) AND emp='{$empnum}' AND ({$epw})";
        $Rl = db_exec($sql) or errDie("Unable to get data.");
        $sdata = pg_fetch_array($Rl);
        $incomes .= "\n\t\t\t<tr>\n\t\t\t\t<td>{$data['description']}</td>\n\t\t\t\t<td align='right'>" . CUR . " {$sdata['sum']}</td>\n\t\t\t</tr>";
        $i++;
        if ($data["description"] != "Fringe Benefits Total") {
            $tot_incomes = $tot_incomes + $sdata['sum'];
        }
        if ($data["type"] == "INCO") {
            if ($novert > 0) {
                $incomes .= "\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td colspan='2'>&nbsp;&nbsp;&nbsp; {$novert} Hours Normal Overtime</td>\n\t\t\t\t\t</tr>";
                ++$i;
            }
            if ($hovert > 0) {
                $incomes .= "\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td colspan='2'>&nbsp;&nbsp;&nbsp; {$hovert} Hours Holiday Overtime</td>\n\t\t\t\t\t</tr>";
                ++$i;
            }
        }
    }
    while ($i < 7) {
        $incomes .= "<tr><td><br></td></tr>";
        $i++;
    }
    $incomes .= "</table>";
    $i = 0;
    $benefits = "<table " . TMPL_tblDflts . " width='100%'>";
    while ($i < 4) {
        $benefits .= "<tr><td><br></td></tr>";
        $i++;
    }
    $benefits .= "</table>";
    $i = 0;
    $comp_parts = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<td align='center'>Description</td>\n\t\t\t\t<td align='center'>Amount</td>\n\t\t\t</tr>";
    $Sl = "SELECT DISTINCT(description) FROM emp_com WHERE ({$pwc}) AND emp='{$empnum}'  AND description !='SDL' AND ({$epw}) ORDER BY description";
    $Ri = db_exec($Sl) or errDie("Unable to get data.");
    while ($data = pg_fetch_array($Ri)) {
        $Sl = "SELECT SUM(amount) FROM emp_com WHERE description='{$data['description']}' AND ({$pwc}) AND emp='{$empnum}' AND description !='SDL' AND ({$epw})";
        $Rl = db_exec($Sl) or errDie("Unable to get data.");
        $sdata = pg_fetch_array($Rl);
        $comp_parts .= "\n\t\t\t<tr>\n\t\t\t\t<td width='80%'>{$data['description']}</td>\n\t\t\t\t<td width='20%' align='right'>" . CUR . " {$sdata['sum']}</td>\n\t\t\t</tr>";
        $i++;
    }
    while ($i < 7) {
        $comp_parts .= "<tr><td><br></td></tr>";
        $i++;
    }
    $comp_parts .= "</table>";
    $i = 0;
    $deductions = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<td width='90%' align='center'>Description</td>\n\t\t\t\t<td align='center'>Amount</td>\n\t\t\t</tr>";
    $Sl = "SELECT DISTINCT(description),type FROM emp_ded WHERE ({$pwc}) AND emp='{$empnum}' AND ({$epw}) ORDER BY description";
    $Ri = db_exec($Sl) or errDie("Unable to get data.");
    $tot_deductions = 0;
    while ($data = pg_fetch_array($Ri)) {
        $Sl = "SELECT SUM(amount) FROM emp_ded WHERE description='{$data['description']}' AND ({$pwc}) AND emp='{$empnum}' AND ({$epw})";
        $Rl = db_exec($Sl) or errDie("Unable to get data.");
        $sdata = pg_fetch_array($Rl);
        if ($data["type"] == "PAYE" && $emp["basic_sal_annum"] <= 65000) {
            $data["description"] = "SITE";
        }
        $deductions .= "\n\t\t\t<tr>\n\t\t\t\t<td>{$data['description']}</td>\n\t\t\t\t<td align='right' nowrap='t'>" . CUR . " {$sdata['sum']}</td>\n\t\t\t</tr>";
        $i++;
        $tot_deductions = $tot_deductions + $sdata['sum'];
    }
    while ($i < 6) {
        $deductions .= TBL_BR;
        $i++;
    }
    $deductions .= "</table>";
    $fringe = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<td width='90%' align=center>Description</td>\n\t\t\t\t<td align='center'>Amount</td>\n\t\t\t</tr>";
    $sql = "SELECT description,SUM(amount) AS amount FROM emp_frin WHERE ({$pwc}) AND emp='{$empnum}' AND ({$fepw}) GROUP BY description ORDER BY description";
    $rslt = db_exec($sql) or errDie("Unable to get data.");
    $i = 0;
    $tot_fringe = 0;
    while ($data = pg_fetch_array($rslt)) {
        $data["amount"] = sprint(abs($data["amount"]));
        $fringe .= "\n\t\t\t<tr>\n\t\t\t\t<td>{$data['description']}</td>\n\t\t\t\t<td align='right' nowrap='t'>" . CUR . " {$data['amount']}</td>\n\t\t\t</tr>";
        $i++;
        $tot_fringe += $data['amount'];
    }
    while ($i < 6) {
        $fringe .= TBL_BR;
        $i++;
    }
    $fringe .= "</table>";
    $exstras = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<td width='25%'>LEAVE DAYS DUE</td>\n\t\t\t\t<td width='25%'></td>\n\t\t\t\t<td width='25%'><b>NETT PAY</b></td>\n\t\t\t\t<td width='25%'><b>" . CUR . " " . sprint($pay["salary"]) . "</b></td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td width='25%'>Total Employee's Tax</td>\n\t\t\t\t<td width='25%'>{$tottax}</td>\n\t\t\t\t<td width='25%'></td>\n\t\t\t\t<td width='25%'></td>\n\t\t\t</tr>\n\t\t</table>";
    $pay["salary"] = sprint($pay["salary"]);
    db_conn('cubit');
    $period = "";
    $tot_incomes = sprint($tot_incomes);
    $tot_deductions = sprint($tot_deductions);
    vsprint($tot_fringe);
    $grossdata = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<td width='50%' align='center'><b>GROSS EARNINGS</b></td>\n\t\t\t\t<td width='50%' align='right'>" . CUR . " {$tot_incomes}</td>\n\t\t\t</tr>\n\t\t</table>";
    explodeDate($fromdate, $from_year, $from_month, $from_day);
    explodeDate($todate, $to_year, $to_month, $to_day);
    if ($from_year == $to_year && $from_month == $to_month) {
        $title = "Salary Advice";
        if ($emp["payprd"] == "m") {
            $salprd = getMonthName($to_month) . " " . getYearOfEmpMon($to_month);
        } else {
            if ($emp["payprd"] == "d" && $fromdate == $todate) {
                $salprd = $pay["week"] . getMonthName($to_month) . " " . getYearOfEmpMon($to_month);
            } else {
                if ($emp["payprd"] == "w") {
                    $stdate = mktime(0, 0, 0, $to_month, 1, getYearOfEmpMon($to_month));
                    $endate = mktime(0, 0, 0, $to_month + 1, -1, getYearOfEmpMon($to_month));
                    $paydate = mktimefd($todate);
                    $i = 1;
                    while ($stdate <= $endate) {
                        if (date("w", $stdate) == 5) {
                            if (date("W", $stdate) == date("W", $paydate_f) && date("W", $stdate) == date("W", $paydate_t)) {
                                $salprd = date("j", $stdate) . " " . getMonthName($to_month) . " " . getYearOfEmpMon($to_month);
                                break;
                            }
                            ++$i;
                        }
                        /* next day */
                        $stdate += 24 * 60 * 60;
                    }
                } else {
                    if ($emp["payprd"] == "f") {
                        $stdate = mktime(0, 0, 0, $to_month, 1, getYearOfEmpMon($to_month));
                        $endate = mktime(0, 0, 0, $to_month + 1, -1, getYearOfEmpMon($to_month));
                        $paydate_f = mktimefd($fromdate);
                        $paydate_t = mktimefd($todate);
                        $c = 0;
                        $fnd_week_f = 0;
                        $fnd_week_t = 0;
                        while ($stdate <= $endate) {
                            //date("W", $stdate) == date("W", $paydate)
                            if (date("w", $stdate) == 5) {
                                if (date("W", $stdate) == date("W", $paydate_f)) {
                                    $fnd_week_f = 1;
                                }
                                if (date("W", $stdate) == date("W", $paydate_t)) {
                                    $fnd_week_t = 1;
                                }
                                if ((++$c % 2 == 0 || $c == 5) && ($fnd_week_f || $fnd_week_t)) {
                                    if ($fnd_week_f == 1 && $paydate_f <= $stdate + 48 * 3600) {
                                        $fnd_week_f = 2;
                                        $salprd_f = date("j", $stdate) . " " . getMonthName($from_month) . " " . getYearOfEmpMon($from_month);
                                    }
                                    if ($fnd_week_t == 1 && $paydate_t <= $stdate + 48 * 3600) {
                                        $fnd_week_t = 2;
                                        $salprd_t = date("j", $stdate) . " " . getMonthName($to_month) . " " . getYearOfEmpMon($to_month);
                                    }
                                    /* now check that they are the same, and if so set the display */
                                    if ($fnd_week_f + ($fnd_week_t = 4)) {
                                        if ($salprd_f == $salprd_t) {
                                            $salprd = $salprd_f;
                                        }
                                        break;
                                    }
                                }
                            }
                            /* next day */
                            $stdate += 24 * 60 * 60;
                        }
                    }
                }
            }
        }
    }
    if (!isset($salprd)) {
        $salprd = $title = "{$fromdate} TO {$todate}";
    }
    $OUT = "\n\t\t<center>\n\t\t<h2>" . COMP_NAME . "<br>{$title}</h2>\n\t\t<table border=1 " . TMPL_tblDflts . " width='750'>\n\t\t\t<tr>\n\t\t\t\t<td width='50%' align='center'><b>Employee Details:</b></td>\n\t\t\t\t<td>{$dates}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>Name:</td>\n\t\t\t\t\t\t\t<td>{$emp['sname']}, {$emp['fnames']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>Number:</td>\n\t\t\t\t\t\t\t<td>{$emp['enum']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>ID:</td>\n\t\t\t\t\t\t\t<td>{$emp['idnum']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>Tax No:</td>\n\t\t\t\t\t\t\t<td>{$emp['taxref']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>Rate:</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$emp['basic_sal']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>Designation:</td>\n\t\t\t\t\t\t\t<td>{$emp['designation']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>Gender:</td>\n\t\t\t\t\t\t\t<td>{$emp['sex']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>Marital Status:</td>\n\t\t\t\t\t\t\t<td>{$emp['marital']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top'>\n            \t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td width='50%'>Period</td>\n\t\t\t\t\t\t\t<td width='50%'>{$salprd}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t\t<table border=1 " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td align='center' colspan='2'><b>Company Details:</b></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>Address:</td>\n\t\t\t\t\t\t\t<td>" . COMP_ADDRESS . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>Tel:</td>\n\t\t\t\t\t\t\t<td>" . COMP_TEL . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>Fax:</td>\n\t\t\t\t\t\t\t<td>" . COMP_FAX . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>Reg No:</td>\n\t\t\t\t\t\t\t<td>" . COMP_REGNO . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>PAYE Ref:</td>\n\t\t\t\t\t\t\t<td>" . COMP_PAYE . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='center'><b>COMPANY CONTRIBUTIONS</b></td>\n\t\t\t\t<td align='center'><b>INCOME</b></td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>{$comp_parts}</td>\n\t\t\t\t<td>{$incomes}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='center'></td>\n\t\t\t\t<td>{$grossdata}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='center'><b>DEDUCTIONS</b></td>\n\t\t\t\t<td align='center'><b>FRINGE BENEFITS</b></td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>{$deductions}</td>\n\t\t\t\t<td>{$fringe}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td width='50%'><b>TOTAL DEDUCTIONS</b></td>\n\t\t\t\t\t\t\t<td width='50%' align='right'>" . CUR . " {$tot_deductions}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td width='50%' nowrap='t'><b>TOTAL FRINGE BENEFITS</b></td>\n\t\t\t\t\t\t\t<td width='50%' align='right'>" . CUR . " {$tot_fringe}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$exstras}</td>\n\t\t\t</tr>\n\t\t</table>";
    return $OUT;
}
/**
 * Calculate PAYE on salary
 *
 * @param array &$emp employee db row
 * @param int $prd month/week for which salary is calculated
 * @param float $paye_salary taxable salary
 * @param int $tyear periods in year (weekly emp: 52, monthly: 12, etc...)
 * @param int $age employee age
 * @return float
 */
function calculate_paye_old(&$emp, $prd, $paye_salary, $tyear, $age)
{
    $empnum = $emp["empnum"];
    //define("ACTUAL_EMP_YEAR",getYearOfEmpMon($prd));
    payedbg("fromsal: {$paye_salary}<br />");
    /* query expression for previous payments/paye */
    if ($emp["payprd"] == "w" || $emp["payprd"] == "f" || $emp["payprd"] == "d") {
        list($month, $week) = explode(":", $prd);
        if ($emp["payprd"] == "d") {
            $day = $week;
        } else {
            $day = 1;
        }
        $mw_b = "((month::int>='3' AND month::int<'{$month}')\n\t\t\t\tOR ('{$month}' < '3' AND (month::int<'{$month}' OR month::int>='3')))\n\t\t\t\tOR (month::int='{$month}' AND week<'{$week}')";
    } else {
        $month = $prd;
        $day = 1;
        $mw_b = "(month::int>='3' AND month::int<'{$month}')\n\t\t\t\tOR ('{$month}' < '3' AND (month::int<'{$month}' OR month::int>='3'))";
    }
    /* determine previously paid amounts */
    $sql = "SELECT 1 AS m, * FROM cubit.salamt_pay\n\t\t\tWHERE empnum='{$empnum}' AND cyear='" . EMP_YEAR . "' AND ({$mw_b})\n\t\t\tUNION\n\t\t\tSELECT -1 AS m, * FROM cubit.salamt_rev\n\t\t\tWHERE empnum='{$empnum}' AND cyear='" . EMP_YEAR . "' AND ({$mw_b})";
    $qry = new dbSql($sql);
    $qry->run();
    $prev_emp = $emp["cyear"] == EMP_YEAR && ($emp["prevemp_tax"] > 0 && $emp["prevemp_remun"] > 0);
    payedbg("prevemp: " . ($prev_emp ? "true" : "false") . "<br />");
    if ($prev_emp) {
        $totpaye = $emp["prevemp_tax"];
        $totgross = $paye_salary + $emp["prevemp_remun"];
    } else {
        $totpaye = 0;
        $totgross = $paye_salary;
    }
    $totnetgross = 0;
    while ($row = $qry->fetch_array()) {
        $totpaye += $row["paye"] * $row["m"];
        $totgross += $row["payegross"] * $row["m"];
        $totnetgross += $row["netgross"] * $row["m"];
    }
    /* calculate current year fraction */
    $fday = 1;
    $year_fmon = getYearOfEmpMon(3);
    $year_month = getYearOfEmpMon($month);
    $fmstart = mktime(0, 0, 0, 3, $fday, $year_fmon);
    $tmstart = mktime(0, 0, 0, $month, $day, $year_month);
    /* if weekly/fortnightly, find the first friday (effective first week)
    		and last day of selected weeks  */
    if ($emp["payprd"] == "w" || $emp["payprd"] == "f") {
        payedbg("week: {$week}<br />");
        /* effective first week */
        while (date("w", $fmstart) != 5) {
            $fmstart = mktime(0, 0, 0, 3, ++$fday, $year_fmon);
        }
        /* end of week, find first week first */
        while (date("w", $tmstart) != 5) {
            $tmstart = mktime(0, 0, 0, $month, ++$day, $year_month);
        }
        /* move the day by "week" number of weeks */
        $day += ($week - 1) * ($emp["payprd"] == "w" ? 1 : 2) * 7;
        $tmstart = mktime(0, 0, 0, $month, $day, $year_month);
        // hack(fortnightly): increase to end of 1st week
        if ($emp["payprd"] == "f") {
            $fmstart += 7 * 24 * 60 * 60;
            //$tmstart += 7 * 24 * 60 * 60;
        }
    }
    /* count the periods in the past */
    $curprd = prdage($fmstart, $tmstart, $emp["payprd"]);
    payedbg("curprd calc: " . mkdatet($fmstart) . " - " . mkdatet($tmstart) . BR);
    payedbg("curprd: {$curprd}<br />");
    /* determine start period from the $curprd */
    $finYearStart = getYearOfEmpMon(3);
    $styear = extractYear($emp["hiredate"]);
    $stmon = extractMonth($emp["hiredate"]);
    if ($styear < $finYearStart || $styear == $finYearStart && $stmon < 3) {
        $stprd = 1;
    } else {
        //$stprd = prdage(mktimefd($emp["hiredate"]), $tmstart, $emp["payprd"]);
        payedbg("stprd calc: " . date("Y-m-d", mktimefd(getYearOfEmpMon(3) . "-03-01")));
        payedbg(" - " . date("Y-m-d", mktimefd($emp["hiredate"])) . "<br />");
        $stprd = prdage(mktimefd(getYearOfEmpMon(3) . "-03-01"), mktimefd($emp["hiredate"]), $emp["payprd"]);
    }
    if ($prev_emp) {
        $working_prd = $curprd;
        $totprd = $tyear;
        //$totprd = $tyear - ($stprd - 0);
    } else {
        // num of prds working
        $working_prd = $curprd - ($stprd - 1);
        // total periods should be working this year
        $totprd = $tyear - ($stprd - 1);
    }
    payedbg("startprd: {$stprd}<br />");
    payedbg("totprd: {$totprd}<br />");
    payedbg("totgross: {$totgross}<br />");
    payedbg("totnetgross: {$totnetgross}<br />");
    payedbg("workprd: {$working_prd}<br />");
    //$working_prd = 2;
    //$totprd = 7;
    $paye_salary = $totgross / $working_prd;
    payedbg("payesal1: {$paye_salary}<br />");
    /* scale paye to amount of periods should be working */
    $paye_salary *= $totprd / $tyear;
    //$paye_salary *= $tyear / $curprd;
    payedbg("payesal2: {$paye_salary}<br />");
    $paye = bracket_calcpaye_old($age, $paye_salary, $tyear);
    if (($amt = sprint($paye * ($working_prd / $totprd) - $totpaye)) < 0) {
        $amt = 0;
    }
    return $amt;
}