コード例 #1
0
function confirm()
{
    extract($_REQUEST);
    require_lib("validate");
    $v = new validate();
    $v->isOk($asset_id, "num", 1, 9, "Invalid asset id.");
    $v->isOk($ex_year, "num", 4, 4, "Invalid expected date (year)");
    $v->isOk($ex_month, "num", 1, 2, "Invalid expected date (month)");
    $v->isOk($ex_day, "num", 1, 2, "Invalid expected date (day)");
    $v->isOk($description, "string", 0, 255, "Invalid description.");
    if (isHired($asset_id, date("Y-m-d"))) {
        $v->addError(0, "Asset is currently hired out.");
    }
    if ($v->isError()) {
        return enter($v->genErrors());
    }
    // Retrieve the name and serial of the asset
    $sql = "SELECT des, serial FROM cubit.assets WHERE id='{$asset_id}'";
    $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
    $asset_data = pg_fetch_array($asset_rslt);
    $asset_out = "({$asset_data['serial']}) {$asset_data['des']}";
    $ex_date = dateFmt($ex_year, $ex_month, $ex_day);
    $e_from = time();
    $e_to = getDTEpoch("{$ex_date} 23:59:59");
    if (!isSerialized($asset_id)) {
        $qty_out = "<tr class='" . bg_class() . "'>\r\n\t\t\t<td>Qty</td>\r\n\t\t\t<td><input type='text' name='qty' value='{$qty}' /></td>\r\n\t\t</tr>";
    } else {
        $booked = 0;
        for ($i = $e_from; $i < $e_to; $i += DAYS) {
            if (isBooked($asset_id, date("Y-m-d", $i))) {
                $booked = 1;
                break;
            }
        }
        if ($booked) {
            $msg = "<li class='err'><b>WARNING</b>: The asset has been booked</li>";
        }
    }
    $OUTPUT = "<h3>Book Asset to Workshop</h3>\r\n\t<form method='post' action='" . SELF . "'>\r\n\t<input type='hidden' name='key' value='write' />\r\n\t<input type='hidden' name='asset_id' value='{$asset_id}' />\r\n\t<input type='hidden' name='ex_year' value='{$ex_year}' />\r\n\t<input type='hidden' name='ex_month' value='{$ex_month}' />\r\n\t<input type='hidden' name='ex_day' value='{$ex_day}' />\r\n\t<input type='hidden' name='description' value='{$description}' />\r\n\t<input type='hidden' name='notes' value='{$notes}' />\r\n\t<input type='hidden' name='qty' value='{$qty}' />\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<td colspan='2'>{$msg}</td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<th colspan='2'>Confirm</th>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>Asset</td>\r\n\t\t\t<td>{$asset_out}</td>\r\n\t\t</tr>\r\n\t\t{$qty_out}\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>Expected Back Date</td>\r\n\t\t\t<td>{$ex_date}</td>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>Description</td>\r\n\t\t\t<td>{$description}</td>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>Notes</td>\r\n\t\t\t<td>{$notes}</td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<td colspan='2' align='center'>\r\n\t\t\t\t<input type='submit' name='key' value='&laquo Correction' />\r\n\t\t\t\t<input type='submit' value='Write &raquo' />\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t</table>\r\n\t</form>";
    return $OUTPUT;
}
コード例 #2
0
function update($_POST)
{
    extract($_POST);
    $collect_ar = array();
    if (!empty($client_collect)) {
        $collect_ar[] = "Client Collect";
    }
    if (!empty($collect)) {
        $collect_ar[] = "Collect";
    }
    if (!empty($deliver)) {
        $collect_ar[] = "Deliver";
    }
    $collection = implode(", ", $collect_ar);
    // 	if ((in_array("Collect", $collect_ar) && in_array("Client Collect", $collect_ar))
    // 		|| (count($collect_ar) == 3)) {
    // 		return "<li class='err'>Invalid collection options selected.</li>";
    // 	}
    if (count($collect_ar) > 1 && in_array("Client Collect", $collect_ar)) {
        return "<li class='err'>Invalid collection options selected.</li>";
    }
    $temp_assets = explode(",", $temp_assets);
    pglib_transaction("BEGIN");
    if (isset($nhalf_day) && $nhalf_day == "checked") {
        $nhalf_day = 1;
    } else {
        $nhalf_day = 0;
    }
    if (isset($nweekends) && $nweekends == "checked") {
        $nweekends = 1;
    } else {
        $nweekends = 0;
    }
    $sql = "UPDATE hire.hire_invoices SET comm='{$comm}' WHERE invid='{$invid}'";
    $comm_rslt = db_exec($sql) or errDie("Unable to retrieve invoice.");
    foreach ($temp_assets as $key => $value) {
        $sql = "SELECT * FROM cubit.assets WHERE id='{$key}'";
        $asset_rslt = db_exec($sql) or errDie("Unable to retrieve asset.");
        $asset_data = pg_fetch_array($asset_rslt);
    }
    if (isset($amount)) {
        foreach ($amount as $key => $value) {
            // 			if (empty($monthly)) {
            // 				$amount[$key] = "";
            // 			}
            if (!user_is_admin(USER_ID)) {
                $amount[$key] = "";
            }
            if (!isset($half_day[$key]) || empty($half_day[$key])) {
                $half_day[$key] = 0;
            }
            if (!isset($weekends[$key]) || empty($weekends[$key])) {
                $weekends[$key] = 0;
            } else {
                $weekends[$key] = 1;
            }
            if (empty($amount[$key]) && $amount != "0") {
                if ($basis[$key] == "per_day") {
                    $hifrm = "{$mfrm_year[$key]}-{$mfrm_month[$key]}-{$mfrm_day[$key]}";
                    $hito = "{$mto_year[$key]}-{$mto_month[$key]}-{$mto_day[$key]}";
                    $hours = "0";
                    /* calculate amount */
                    $ftime = getDTEpoch("{$hifrm} 0:00:00");
                    $ttime = getDTEpoch("{$hito} 0:00:00");
                    $days = 0;
                    $weeks = 0;
                    while ($ftime <= $ttime) {
                        if (date("w", $ftime) == 0 && isset($weekends[$key]) && $weekends[$key]) {
                            $days += 0.6;
                        } else {
                            ++$days;
                        }
                        $ftime += 24 * 60 * 60;
                    }
                    if (is_numeric($rain_days[$key])) {
                        $days -= $rain_days[$key];
                    }
                    $timeunits = $days;
                } else {
                    if ($basis[$key] == "per_hour") {
                        $hifrm = $hito = mkdate($pinv_year, $pinv_month, $pinv_day);
                        $timeunits = $hours;
                        $weeks = 0;
                        if (empty($hours) || !is_numeric($hours)) {
                            return "\r\n\t\t\t\t\t\t\t<li class='err'>\r\n\t\t\t\t\t\t\t\t<b>ERROR</b>: Invalid amount of hours.\r\n\t\t\t\t\t\t\t</li>";
                        }
                    } else {
                        if ($nbasis == "per_week") {
                            $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day);
                            $timeunits = $weeks;
                            $hours = 0;
                            if (empty($weeks) || !is_numeric($weeks)) {
                                return "\r\n\t\t\t\t\t\t\t<li class='err'>\r\n\t\t\t\t\t\t\t\t<b>ERROR</b>: Invalid amount of weeks.\r\n\t\t\t\t\t\t\t</li>";
                            }
                        }
                    }
                }
                if ($half_day[$key]) {
                    $amount[$key] = $qty[$key] * $timeunits * (basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key]) - basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key] + basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key] / 2;
                } else {
                    $amount[$key] = $qty[$key] * $timeunits * basisPrice($cusnum, $asset_id[$key], $basis[$key]);
                }
            }
            if ($amount[$key] == 0) {
                $amount[$key] = 0;
                $blank_amount = 1;
            } else {
                $blank_amount = 0;
            }
            $sql = "UPDATE hire.hire_invitems SET amt='{$amount[$key]}',\r\n\t\t\t\t\t\thalf_day='{$half_day[$key]}', weekends='{$weekends[$key]}'\r\n\t\t\t\t\tWHERE id='{$key}'";
            db_exec($sql) or errDie("Unable to update item amount.");
            $sql = "UPDATE hire.reprint_invitems SET amt='{$amount[$key]}',\r\n\t\t\t\t\t\thalf_day='{$half_day[$key]}', weekends='{$weekends[$key]}'\r\n\t\t\t\t\tWHERE item_id='{$key}'";
            db_exec($sql) or errDie("Unable to update return item amount.");
            if ($blank_amount) {
                $amount[$key] = "";
            }
            //$hifrm = "$hifrm_year[$key]-$hifrm_month[$key]-$hifrm_day[$key]";
            //$hito = "$hito_year[$key]-$hito_month[$key]-$hito_day[$key]";
            if (!isset($remove[$key])) {
                $sql = "SELECT basis FROM hire.hire_invitems WHERE id='{$key}'";
                $item_rslt = db_exec($sql) or errDie("Unable to retrieve basis.");
                $mbasis = pg_fetch_result($item_rslt, 0);
                /* determine time units */
                if ($mbasis == "per_day") {
                    $mfrm = mkdate($mfrm_year[$key], $mfrm_month[$key], $mfrm_day[$key]);
                    $mto = mkdate($mto_year[$key], $mto_month[$key], $mto_day[$key]);
                    /* calculate amount */
                    $ftime = mktime(0, 0, 0, $mfrm_month[$key], $mfrm_day[$key], $mfrm_year[$key]);
                    $ttime = mktime(0, 0, 0, $mto_month[$key], $mto_day[$key], $mto_year[$key]);
                    $days = 0;
                    if (empty($weeks)) {
                        $weeks = 0;
                    }
                    if (empty($hours)) {
                        $hours = 0;
                    }
                    while ($ftime <= $ttime) {
                        if (date("w", $ftime) == 0 && isset($weekends[$key]) && $weekends[$key]) {
                            $days += 0.6;
                        } else {
                            ++$days;
                        }
                        $ftime += 24 * 60 * 60;
                    }
                    $timeunits = $days;
                    $sql = "UPDATE hire.hire_invitems\r\n\t\t\t\t\t\t\tSET from_date='{$mfrm}', to_date='{$mto}'\r\n\t\t\t\t\t\t\tWHERE id='{$key}'";
                    db_exec($sql) or errDie("Unable to update items.");
                    $sql = "UPDATE hire.reprint_invitems\r\n\t\t\t\t\t\t\tSET from_date='{$mfrm}', to_date='{$mto}'\r\n\t\t\t\t\t\t\tWHERE item_id='{$key}'";
                    db_exec($sql) or errDie("Unable to update reprint items.");
                }
            } else {
                // Delete the old items
                $sql = "DELETE FROM hire.hire_invitems WHERE id='{$key}'";
                db_exec($sql) or errDie("Unable to remove old items.");
                $sql = "DELETE FROM hire.reprint_invitems WHERE item_id='{$key}'";
                db_exec($sql) or errDie("Unable to remove old reprint items.");
                //.Remove if the item has been hired as well
                $sql = "DELETE FROM hire.assets_hired WHERE item_id='{$key}'";
                db_exec($sql) or errDie("Unable to remove items from hired log.");
            }
        }
    }
    $sql = "SELECT * FROM hire.hire_invoices WHERE invid='{$invid}'";
    $hi_rslt = db_exec($sql) or errDie("Unable to retrieve invoice.");
    $invb = pg_fetch_array($hi_rslt);
    // Insert new items
    if ($nasset_id != "0" || $nbasis != "0") {
        if ($nasset_id == "0") {
            return "<li class='err'><b>ERROR</b>: No asset selected.</li>";
        }
        /* get asset id */
        list($serialqty, $nasset_id) = explode(":", $nasset_id);
        /* disabled items don't get passed through */
        if ($serialqty == "s" || !isset($nqty)) {
            $nqty = "1";
        } else {
            $sql = "SELECT serial2 FROM cubit.assets WHERE id='{$nasset_id}'";
            $dqty_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
            $dqty = pg_fetch_result($dqty_rslt, 0);
            if ($dqty - $nqty < 0) {
                return "<li class='err'><b>ERROR</b>: Invalid quantity. Only &nbsp; <b>{$dqty}</b> &nbsp; available.</li>";
            }
        }
        if (empty($nqty) || !is_numeric($nqty)) {
            return "<li class='err'><b>ERROR</b>: Invalid quantity</li>";
        }
        /* determine time units */
        if ($nbasis == "per_day") {
            $nhifrm = mkdate($nhifrm_year, $nhifrm_month, $nhifrm_day);
            $nhito = mkdate($nhito_year, $nhito_month, $nhito_day);
            $hours = "0";
            /* calculate amount */
            $ftime = mktime(0, 0, 0, $nhifrm_month, $nhifrm_day, $nhifrm_year);
            $ttime = mktime(0, 0, 0, $nhito_month, $nhito_day, $nhito_year);
            $days = 0;
            $weeks = 0;
            while ($ftime <= $ttime) {
                if (date("w", $ftime) == 0 && isset($nweekends) && $nweekends) {
                    $days += 0.6;
                } else {
                    ++$days;
                }
                $ftime += 24 * 60 * 60;
            }
            $timeunits = $days;
        } else {
            if ($nbasis == "per_hour") {
                $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day);
                $timeunits = $hours;
                $weeks = 0;
                if (empty($hours) || !is_numeric($hours)) {
                    return "<li class='err'><b>ERROR</b>: Invalid amount of hours.</li>";
                }
            } else {
                if ($nbasis == "per_week") {
                    $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day);
                    $timeunits = $weeks;
                    $hours = 0;
                    if (empty($weeks) || !is_numeric($weeks)) {
                        return "<li class='err'><b>ERROR</b>: Invalid amount of weeks.</li>";
                    }
                } else {
                    return "<li class='err'><b>ERROR</b>: No basis selected.</li>";
                }
            }
        }
        /* calculate amount according to hire settings, quantity and time units */
        if ($nhalf_day) {
            $camt = $nqty * $timeunits * basisPrice($cusnum, $nasset_id, $nbasis) - basisPrice($cusnum, $nasset_id, $nbasis) + basisPrice($cusnum, $nasset_id, $nbasis) / 2;
        } else {
            $camt = $nqty * $timeunits * basisPrice($cusnum, $nasset_id, $nbasis);
        }
        /* insert item */
        $sql = "SELECT asset_id FROM hire.hire_invitems WHERE invid='{$invid}' AND asset_id='{$nasset_id}'";
        $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
        // No duplicate assets
        if (!pg_num_rows($asset_rslt)) {
            if (isHired($nasset_id)) {
                return "\r\n\t\t\t\t<li class='err'>\r\n\t\t\t\t\t<b>ERROR</b>: Asset has already hired out.\r\n\t\t\t\t</li>";
            }
            $sql = "\r\n\t\t\t\tINSERT INTO hire.hire_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, basis, hours, weeks, \r\n\t\t\t\t\tcollection, half_day, weekends\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$nasset_id}', '{$nqty}', '{$camt}', '{$nhifrm}', '{$nhito}', '{$nbasis}', '{$hours}', '{$weeks}', \r\n\t\t\t\t\t'{$collection}', '{$nhalf_day}', '{$nweekends}'\r\n\t\t\t\t)";
            db_exec($sql) or errDie("Unable to create new invoice item.");
            $item_id = pglib_lastid("hire.hire_invitems", "id");
            $sql = "\r\n\t\t\t\tINSERT INTO hire.reprint_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, basis, hours, weeks, \r\n\t\t\t\t\tcollection, half_day, weekends, item_id\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$nasset_id}', '{$nqty}', '{$camt}', '{$nhifrm}', '{$nhito}', '{$nbasis}', '{$hours}', '{$weeks}', \r\n\t\t\t\t\t'{$collection}', '{$nhalf_day}', '{$nweekends}', '{$item_id}'\r\n\t\t\t\t)";
            db_exec($sql) or errDie("Unable to create reprint invoice item.");
        }
    }
    if ($monthly == "true") {
        $sql = "DELETE FROM hire.monthly_invitems WHERE invid='{$invid}'";
        db_exec($sql) or errDie("Unable to remove monthly items.");
    } else {
        $sql = "SELECT * FROM hire.hire_invitems WHERE invid='{$invid}'";
        $mii_rslt = db_exec($sql) or errDie("Unable to retrieve inv items.");
        $sql = "DELETE FROM hire.monthly_invitems WHERE invid='{$invid}'";
        db_exec($sql) or errDie("Unable to remove monthly items.");
        while ($item = pg_fetch_array($mii_rslt)) {
            $sql = "\r\n\t\t\t\tINSERT INTO hire.monthly_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, \r\n\t\t\t\t\tbasis, hours, weeks, collection, half_day, \r\n\t\t\t\t\tweekends, item_id\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$item['invid']}', '{$item['asset_id']}', '{$item['qty']}', '{$item['amt']}', '{$item['from_date']}', '{$item['to_date']}', \r\n\t\t\t\t\t'{$item['basis']}', '{$item['hours']}', '{$item['weeks']}', '{$item['collection']}', '{$item['half_day']}', \r\n\t\t\t\t\t'{$item['weekends']}', '{$item['id']}'\r\n\t\t\t\t)";
            db_exec($sql) or errDie("Unable to create monthly items.");
        }
    }
    $sql = "SELECT * FROM hire.reprint_invoices WHERE invid='{$invid}'";
    $ri_rslt = db_exec($sql) or errDie("Unable to retrieve reprints.");
    // Create a new entry, or update
    if (pg_num_rows($ri_rslt)) {
        $sql = "\r\n\t\t\tUPDATE hire.reprint_invoices \r\n\t\t\tSET deptid='{$invb['deptid']}', cusnum='{$invb['cusnum']}', deptname='{$invb['deptname']}', cusacc='{$invb['cusacc']}', \r\n\t\t\t\tcusname='{$invb['cusname']}', surname='{$invb['surname']}', cusaddr='{$invb['cusaddr']}', cusvatno='{$invb['cusvatno']}', \r\n\t\t\t\tcordno='{$invb['cordno']}', ordno='{$invb['ordno']}', chrgvat='{$invb['chrgvat']}', terms='{$invb['terms']}', \r\n\t\t\t\ttraddisc='{$invb['traddisc']}', salespn='{$invb['salespn']}', odate='{$invb['odate']}', delchrg='{$invb['delchrg']}', \r\n\t\t\t\tsubtot='{$invb['subtot']}', vat='{$invb['vat']}', total='{$invb['total']}', balance='{$invb['balance']}', \r\n\t\t\t\tcomm='{$invb['comm']}', printed='{$invb['printed']}', done='{$invb['done']}', div='{$invb['div']}', \r\n\t\t\t\tusername='******'username']}', rounding='{$invb['rounding']}', delvat='{$invb['delvat']}', vatnum='{$invb['vatnum']}', \r\n\t\t\t\tpcash='{$invb['pcash']}', pcheque='{$invb['pcheque']}', pcc='{$invb['pcc']}', pcredit='{$invb['pcredit']}' \r\n\t\t\tWHERE invid='{$invid}'";
        db_exec($sql) or errDie("Unable to update reprint.");
    } else {
        $sql = "\r\n\t\t\tINSERT INTO hire.reprint_invoices(\r\n\t\t\t\tinvid, invnum, deptid, cusnum, deptname, cusacc, \r\n\t\t\t\tcusname, surname, cusaddr, cusvatno, cordno, ordno, \r\n\t\t\t\tchrgvat, terms, traddisc, salespn, odate, delchrg, \r\n\t\t\t\tsubtot, vat, total, balance, comm, printed, done, div, \r\n\t\t\t\tusername, rounding, delvat, vatnum, pcash, pcheque, \r\n\t\t\t\tpcc, pcredit\r\n\t\t\t) VALUES (\r\n\t\t\t\t'{$invid}', '{$invb['invnum']}', '{$invb['deptid']}', '{$invb['cusnum']}', '{$invb['deptname']}', '{$invb['cusacc']}', \r\n\t\t\t\t'{$invb['cusname']}', '{$invb['surname']}', '{$invb['cusaddr']}', '{$invb['cusvatno']}', '{$invb['cordno']}', '{$invb['ordno']}', \r\n\t\t\t\t'{$invb['chrgvat']}', '{$invb['terms']}', '{$invb['traddisc']}', '{$invb['salespn']}', '{$invb['odate']}', '{$invb['delchrg']}', \r\n\t\t\t\t'{$invb['subtot']}', '{$invb['vat']}' , '{$invb['total']}', '{$invb['balance']}', '{$invb['comm']}', 'y', 'y', '" . USER_DIV . "', \r\n\t\t\t\t'" . USER_NAME . "', '{$invb['rounding']}', '{$invb['delvat']}', '{$invb['vatnum']}', '{$invb['pcash']}', '{$invb['pcheque']}', \r\n\t\t\t\t'{$invb['pcc']}', '{$invb['pcredit']}'\r\n\t\t\t)";
        db_exec($sql) or errDie("Unable to add reprint.");
    }
    $sql = "SELECT * FROM hire.monthly_invoices\r\n\t\t\tWHERE invid='{$invid}' OR invnum='{$invb['invnum']}'";
    $mi_rslt = db_exec($sql) or errDie("Unable to retrieve monthly.");
    // Should we create a new entry
    if (pg_num_rows($mi_rslt)) {
        $sql = "\r\n\t\t\tUPDATE hire.monthly_invoices \r\n\t\t\tSET deptid='{$invb['deptid']}', cusnum='{$invb['cusnum']}', deptname='{$invb['deptname']}', cusacc='{$invb['cusacc']}', \r\n\t\t\t\tcusname='{$invb['cusname']}', surname='{$invb['surname']}', cusaddr='{$invb['cusaddr']}', cusvatno='{$invb['cusvatno']}', \r\n\t\t\t\tcordno='{$invb['cordno']}', ordno='{$invb['ordno']}', chrgvat='{$invb['chrgvat']}', terms='{$invb['terms']}', \r\n\t\t\t\ttraddisc='{$invb['traddisc']}', salespn='{$invb['salespn']}', odate='{$invb['odate']}', delchrg='{$invb['delchrg']}', \r\n\t\t\t\tsubtot='{$invb['subtot']}', vat='{$invb['vat']}', total='{$invb['total']}', balance='{$invb['balance']}', \r\n\t\t\t\tcomm='{$invb['comm']}', printed='{$invb['printed']}', done='{$invb['done']}', div='{$invb['div']}', \r\n\t\t\t\tusername='******'username']}', rounding='{$invb['rounding']}', delvat='{$invb['delvat']}', vatnum='{$invb['vatnum']}', \r\n\t\t\t\tpcash='{$invb['pcash']}', pcheque='{$invb['pcheque']}', pcc='{$invb['pcc']}', pcredit='{$invb['pcredit']}', \r\n\t\t\t\thire_invid='{$invid}' \r\n\t\t\tWHERE invid='{$invb['invid']}'";
    } elseif (empty($monthly)) {
        $sql = "\r\n\t\t\t\tINSERT INTO hire.monthly_invoices (\r\n\t\t\t\t\tinvid, invnum, deptid, cusnum, deptname, cusacc, \r\n\t\t\t\t\tcusname, surname, cusaddr, cusvatno, cordno, \r\n\t\t\t\t\tordno, chrgvat, terms, traddisc, salespn, odate, \r\n\t\t\t\t\tdelchrg, subtot, vat, total, balance, comm, \r\n\t\t\t\t\tprinted, done, div, username, rounding, delvat, vatnum, \r\n\t\t\t\t\tpcash, pcheque, pcc, pcredit, invoiced_month, hire_invid\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$invb['invnum']}', '{$invb['deptid']}', '{$invb['cusnum']}', '{$invb['deptname']}', '{$invb['cusacc']}', \r\n\t\t\t\t\t'{$invb['cusname']}', '{$invb['surname']}', '{$invb['cusaddr']}', '{$invb['cusvatno']}', '{$invb['cordno']}', \r\n\t\t\t\t\t'{$invb['ordno']}', '{$invb['chrgvat']}', '{$invb['terms']}', '{$invb['traddisc']}', '{$invb['salespn']}', '{$invb['odate']}', \r\n\t\t\t\t\t'{$invb['delchrg']}', '{$invb['subtot']}', '{$invb['vat']}' , '{$invb['total']}', '{$invb['balance']}', '{$invb['comm']}', \r\n\t\t\t\t\t'y', 'y', '" . USER_DIV . "', '" . USER_NAME . "', '{$invb['rounding']}', '{$invb['delvat']}', '{$invb['vatnum']}', \r\n\t\t\t\t\t'{$invb['pcash']}', '{$invb['pcheque']}', '{$invb['pcc']}', '{$invb['pcredit']}', '" . date("m") . "', '{$invid}'\r\n\t\t\t\t)";
    }
    db_exec($sql) or errDie("Unable to store monthly invoice.");
    pglib_transaction("COMMIT");
    if (isset($upBtn)) {
        if ($upBtn == "Return") {
            return returnHire();
        } elseif ($upBtn == "Invoice") {
            return invoiceHire();
        }
    }
    return false;
}
コード例 #3
0
function update($_POST)
{
    extract($_POST);
    $collect_ar = array();
    if (!empty($client_collect)) {
        $collect_ar[] = "Client Collect";
    }
    if (!empty($collect)) {
        $collect_ar[] = "Collect";
    }
    if (!empty($deliver)) {
        $collect_ar[] = "Deliver";
    }
    $collection = implode(", ", $collect_ar);
    // 	if ((in_array("Collect", $collect_ar) && in_array("Client Collect", $collect_ar))
    // 		|| (count($collect_ar) == 3)) {
    // 		return "<li class='err'>Invalid collection options selected.</li>";
    // 	}
    if (count($collect_ar) > 1 && in_array("Client Collect", $collect_ar)) {
        return "<li class='err'>Invalid collection options selected.</li>";
    }
    $temp_assets = explode(",", $temp_assets);
    pglib_transaction("BEGIN");
    // Stock Sales
    if (isset($snremove) && is_array($snremove)) {
        foreach ($snremove as $id => $value) {
            $sql = "DELETE FROM hire.hire_stock_items WHERE id='{$id}'";
            db_exec($sql) or errDie("Unable to remove stock item.");
        }
    }
    if (isset($snstock) && $snstock > 0) {
        if (!is_numeric($snqty)) {
            $snqty = 1;
        }
        #get vatperc for this item
        $get_vatp = "SELECT vat_amount FROM vatcodes WHERE id = '{$snvatcode}' LIMIT 1";
        $run_vatp = db_exec($get_vatp) or errDie("Unable to get vat code information.");
        if (pg_numrows($run_vatp) < 1) {
            $vatperc = 0;
        } else {
            $vatperc = pg_fetch_result($run_vatp, 0, 0);
        }
        //		$sql = "SELECT selamt FROM cubit.stock WHERE stkid='$snstock'";
        //		$price_rslt = db_exec($sql) or errDie("Unable to retrieve price.");
        //		$price = pg_fetch_result($price_rslt, 0);
        $price = get_excl_stock($snstock) * $snqty;
        $excl_price = get_excl_stock($snstock) * $snqty;
        $vatamount = sprint($excl_price / 100 * $vatperc);
        $price += 0;
        $price += sprint($excl_price / 100 * $vatperc);
        $snamt = $price * $snqty;
        #hack, because stores functionality isnt working ...
        $get_store = "SELECT whid FROM stock WHERE stkid = '{$snstock}' LIMIT 1";
        $run_store = db_exec($get_store) or errDie("Unable to get stock store information.");
        $snstore = pg_fetch_result($run_store, 0, 0);
        $sql = "\r\n\t\t\tINSERT INTO hire.hire_stock_items (\r\n\t\t\t\twhid, invid, stkid, qty, vatcode, unitcost, \r\n\t\t\t\tamount, excl_amount, vatamount\r\n\t\t\t) VALUES (\r\n\t\t\t\t'{$snstore}', '{$invid}', '{$snstock}', '{$snqty}', '{$snvatcode}', '" . sprint($price / $snqty) . "', \r\n\t\t\t\t'" . sprint($snamt / $snqty) . "', '{$excl_price}', '{$vatamount}'\r\n\t\t\t)";
        db_exec($sql) or errDie("Unable to add stock.");
        $sql = "UPDATE cubit.stock SET alloc=(alloc+'{$snqty}') WHERE stkid='{$snstock}'";
        db_exec($sql) or errDie("Unable to update stock allocation.");
    }
    if (isset($nhalf_day) && $nhalf_day == "checked") {
        $nhalf_day = 1;
    } else {
        $nhalf_day = 0;
    }
    if (isset($nweekends) && $nweekends == "checked") {
        $nweekends = 1;
    } else {
        $nweekends = 0;
    }
    $sql = "UPDATE hire.hire_invoices SET comm='{$comm}' WHERE invid='{$invid}'";
    $comm_rslt = db_exec($sql) or errDie("Unable to retrieve invoice.");
    foreach ($temp_assets as $key => $value) {
        $sql = "SELECT * FROM cubit.assets WHERE id='{$key}'";
        $asset_rslt = db_exec($sql) or errDie("Unable to retrieve asset.");
        $asset_data = pg_fetch_array($asset_rslt);
    }
    if (isset($amount)) {
        foreach ($amount as $key => $value) {
            // 			if (empty($monthly)) {
            // 				$amount[$key] = "";
            // 			}
            #redundant code ... breaks new hire if you click update multiple times (sets item amount = 0 after second update)
            //			if (!user_is_admin(USER_ID)) {
            //				$amount[$key] = "";
            //			}
            if (!isset($half_day[$key]) || empty($half_day[$key])) {
                $half_day[$key] = 0;
            }
            if (!isset($weekends[$key]) || empty($weekends[$key])) {
                $weekends[$key] = 0;
            } else {
                $weekends[$key] = 1;
            }
            if (!isset($total_days[$key]) || empty($total_days[$key])) {
                $epoch_from = strtotime($from_date[$key]);
                $epoch_to = strtotime($to_date[$key]);
                $total_days[$key] = ($epoch_to - $epoch_from) / (60 * 60 * 24) + 1;
                $total_days[$key] = ceil($total_days[$key]);
            }
            $sql = "SELECT qty, total_days, half_day FROM hire.hire_invitems WHERE id='{$key}'";
            $old_rslt = db_exec($sql) or errDie("Unable to retrieve old qty.");
            list($oldqty, $olddays, $oldhalf_day) = pg_fetch_array($old_rslt);
            if (!isset($oldqty)) {
                $oldqty = 1;
            }
            if (!isset($qty[$key])) {
                $oldqty = 1;
                $qty[$key] = 1;
            }
            if ($half_day[$key]) {
                $half_day[$key] = 1;
                $recalc = FALSE;
            } else {
                $half_day[$key] = 0;
            }
            if (empty($amount) && $amount != 0 || $oldqty != $qty[$key] || $olddays != $total_days[$key] || (!isset($amount[$key]) or empty($amount[$key])) || $half_day == 1) {
                if ($basis[$key] == "per_day") {
                    $to_time = strtotime($from_date[$key]) + $total_days[$key] * (60 * 60 * 24);
                    $hifrm = "{$mfrm_year[$key]}-{$mfrm_month[$key]}-{$mfrm_day[$key]}";
                    $hito = date("Y-m-d", $to_time);
                    $hours = "0";
                    /* calculate amount */
                    $ftime = getDTEpoch("{$hifrm} 0:00:00");
                    $ttime = getDTEpoch("{$hito} 0:00:00");
                    $days = 0;
                    $weeks = 0;
                    $months = 0;
                    while ($ftime <= $ttime) {
                        if (date("w", $ftime) == 0 && isset($weekends[$key]) && $weekends[$key]) {
                            $days += 0.6;
                        } else {
                            ++$days;
                        }
                        $ftime += 24 * 60 * 60;
                    }
                    $timeunits = $total_days[$key];
                } else {
                    if ($basis[$key] == "per_hour") {
                        $hifrm = $hito = mkdate($pinv_year, $pinv_month, $pinv_day);
                        $timeunits = $hours;
                        $weeks = 0;
                        $months = 0;
                        if (empty($hours) || !is_numeric($hours)) {
                            return "<li class='err'><b>ERROR</b>: Invalid amount of hours.</li>";
                        }
                    } else {
                        if ($nbasis == "per_week") {
                            $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day);
                            $timeunits = $weeks;
                            $hours = 0;
                            $months = 0;
                            if (empty($weeks) || !is_numeric($weeks)) {
                                return "<li class='err'><b>ERROR</b>: Invalid amount of weeks.</li>";
                            }
                        } else {
                            if ($nbasis == "per_month") {
                                $nhifrm = $nhito = mkDate($pinv_year, $pinv_month, $pinv_day);
                                $timeunits = $months;
                                $weeks = 0;
                                $hours = 0;
                                if (empty($months) || !is_numeric($months)) {
                                    return "<li class='err'><b>ERROR</b>: Invalid amount of months.</li>";
                                }
                            }
                        }
                    }
                }
                if ($half_day[$key]) {
                    //					$amount[$key] = ($qty[$key] * $timeunits * (basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key]) - (basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key]) + ((basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key])) / halfday_rate());
                    $amount[$key] = sprint($qty[$key] * ($timeunits * basisPrice($cusnum, $asset_id[$key], $basis[$key]) - basisPrice($cusnum, $asset_id[$key], $basis[$key]) + basisPrice($cusnum, $asset_id[$key], $basis[$key]) / halfday_rate()));
                } else {
                    $amount[$key] = $qty[$key] * $timeunits * basisPrice($cusnum, $asset_id[$key], $basis[$key]);
                }
            }
            if ($amount[$key] == 0) {
                $amount[$key] = 0;
                $blank_amount = 1;
            } else {
                $blank_amount = 0;
            }
            $sql = "\r\n\t\t\t\tUPDATE hire.hire_invitems \r\n\t\t\t\tSET amt='{$amount[$key]}', half_day='{$half_day[$key]}', weekends='{$weekends[$key]}', \r\n\t\t\t\t\ttotal_days='{$total_days[$key]}', qty='{$qty[$key]}' \r\n\t\t\t\tWHERE id='{$key}'";
            db_exec($sql) or errDie("Unable to update item amount.");
            $sql = "\r\n\t\t\t\tUPDATE hire.reprint_invitems \r\n\t\t\t\tSET amt='{$amount[$key]}', half_day='{$half_day[$key]}', weekends='{$weekends[$key]}' \r\n\t\t\t\tWHERE item_id='{$key}'";
            db_exec($sql) or errDie("Unable to update return item amount.");
            if ($blank_amount) {
                $amount[$key] = "";
            }
            //$hifrm = "$hifrm_year[$key]-$hifrm_month[$key]-$hifrm_day[$key]";
            //$hito = "$hito_year[$key]-$hito_month[$key]-$hito_day[$key]";
            if (!isset($remove[$key])) {
                $sql = "SELECT basis FROM hire.hire_invitems WHERE id='{$key}'";
                $item_rslt = db_exec($sql) or errDie("Unable to retrieve basis.");
                $mbasis = pg_fetch_result($item_rslt, 0);
                /* determine time units */
                if ($mbasis == "per_day") {
                    $mfrm = mkdate($mfrm_year[$key], $mfrm_month[$key], $mfrm_day[$key]);
                    $mto = mkdate($mto_year[$key], $mto_month[$key], $mto_day[$key]);
                    /* calculate amount */
                    $ftime = mktime(0, 0, 0, $mfrm_month[$key], $mfrm_day[$key], $mfrm_year[$key]);
                    $ttime = mktime(0, 0, 0, $mto_month[$key], $mto_day[$key], $mto_year[$key]);
                    $days = 0;
                    if (empty($weeks)) {
                        $weeks = 0;
                    }
                    if (empty($hours)) {
                        $hours = 0;
                    }
                    if (empty($months)) {
                        $months = 0;
                    }
                    while ($ftime <= $ttime) {
                        if (date("w", $ftime) == 0 && isset($weekends[$key]) && $weekends[$key]) {
                            $days += 0.6;
                        } else {
                            ++$days;
                        }
                        $ftime += 24 * 60 * 60;
                    }
                    $timeunits = $days;
                    $sql = "\r\n\t\t\t\t\t\tUPDATE hire.hire_invitems \r\n\t\t\t\t\t\tSET from_date='{$mfrm}', to_date='{$mto}' \r\n\t\t\t\t\t\tWHERE id='{$key}'";
                    db_exec($sql) or errDie("Unable to update items.");
                    $sql = "\r\n\t\t\t\t\t\tUPDATE hire.reprint_invitems \r\n\t\t\t\t\t\tSET from_date='{$mfrm}', to_date='{$mto}' \r\n\t\t\t\t\t\tWHERE item_id='{$key}'";
                    db_exec($sql) or errDie("Unable to update reprint items.");
                }
            } else {
                // Delete the old items
                $sql = "DELETE FROM hire.hire_invitems WHERE id='{$key}'";
                db_exec($sql) or errDie("Unable to remove old items.");
                $sql = "DELETE FROM hire.reprint_invitems WHERE item_id='{$key}'";
                db_exec($sql) or errDie("Unable to remove old reprint items.");
                //.Remove if the item has been hired as well
                $sql = "DELETE FROM hire.assets_hired WHERE item_id='{$key}'";
                db_exec($sql) or errDie("Unable to remove items from hired log.");
            }
        }
    }
    $sql = "SELECT * FROM hire.hire_invoices WHERE invid='{$invid}'";
    $hi_rslt = db_exec($sql) or errDie("Unable to retrieve invoice.");
    $invb = pg_fetch_array($hi_rslt);
    // Default basis
    if (!empty($nasset_id) && $nbasis == "0") {
        list($serialqty, $tasset_id) = explode(":", $nasset_id);
        $nbasis = default_basis($tasset_id);
    }
    // Insert new items
    if ($nasset_id != "0") {
        if ($nasset_id == "0") {
            return "<li class='err'><b>ERROR</b>: No asset selected.</li>";
        }
        /* get asset id */
        list($serialqty, $nasset_id) = explode(":", $nasset_id);
        /* disabled items don't get passed through */
        if ($serialqty == "s" || !isset($nqty)) {
            $nqty = "1";
        } else {
            $sql = "SELECT serial2 FROM cubit.assets WHERE id='{$nasset_id}'";
            $dqty_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
            $dqty = pg_fetch_result($dqty_rslt, 0);
            if ($dqty - $nqty < 0) {
                return "<li class='err'><b>ERROR</b>: Invalid quantity. Only &nbsp; <b>{$dqty}</b> &nbsp; available.</li>";
            }
        }
        if (empty($nqty) || !is_numeric($nqty)) {
            return "<li class='err'><b>ERROR</b>: Invalid quantity</li>";
        }
        // Default basis
        if ($nbasis == "0") {
            $nbasis = default_basis($nasset_id);
        }
        /* determine time units */
        if ($nbasis == "per_day") {
            $nhifrm = mkdate($nhifrm_year, $nhifrm_month, $nhifrm_day);
            $nhito = mkdate($nhito_year, $nhito_month, $nhito_day);
            $hours = "0";
            /* calculate amount */
            $ftime = mktime(0, 0, 0, $nhifrm_month, $nhifrm_day, $nhifrm_year);
            $ttime = mktime(0, 0, 0, $nhito_month, $nhito_day, $nhito_year);
            $days = 0;
            $weeks = 0;
            $months = 0;
            while ($ftime <= $ttime) {
                if (date("w", $ftime) == 0 && isset($nweekends) && $nweekends) {
                    $days += 0.6;
                } else {
                    ++$days;
                }
                $ftime += 24 * 60 * 60;
            }
            $timeunits = $days;
        } else {
            if ($nbasis == "per_hour") {
                $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day);
                $timeunits = $hours;
                $weeks = 0;
                $months = 0;
                if (empty($hours) || !is_numeric($hours)) {
                    return "<li class='err'><b>ERROR</b>: Invalid amount of hours.</li>";
                }
            } else {
                if ($nbasis == "per_week") {
                    $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day);
                    $timeunits = $weeks;
                    $hours = 0;
                    $months = 0;
                    if (empty($weeks) || !is_numeric($weeks)) {
                        return "<li class='err'><b>ERROR</b>: Invalid amount of weeks.</li>";
                    }
                } else {
                    if ($nbasis == "per_month") {
                        $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day);
                        $timeunits = $months;
                        $hours = 0;
                        $weeks = 0;
                        if (empty($months) || !is_numeric($months)) {
                            return "<li class='err'><b>ERROR</b>: Invalid amount of months.</li>";
                        }
                    } else {
                        return "<li class='err'><b>ERROR</b>: No basis selected.</li>";
                    }
                }
            }
        }
        /* calculate amount according to hire settings, quantity and time units */
        if ($nhalf_day) {
            $camt = $nqty * $timeunits * basisPrice($cusnum, $nasset_id, $nbasis) - basisPrice($cusnum, $nasset_id, $nbasis) + basisPrice($cusnum, $nasset_id, $nbasis) / halfday_rate();
        } else {
            $camt = $nqty * $timeunits * basisPrice($cusnum, $nasset_id, $nbasis);
        }
        /* insert item */
        $sql = "SELECT asset_id FROM hire.hire_invitems\r\n\t\t\t\tWHERE invid='{$invid}' AND asset_id='{$nasset_id}'";
        $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
        // No duplicate assets
        if (!pg_num_rows($asset_rslt) || !isSerialized($nasset_id)) {
            if (isHired($nasset_id)) {
                return "\r\n\t\t\t\t<li class='err'>\r\n\t\t\t\t\t<b>ERROR</b>: Asset has already hired out.\r\n\t\t\t\t</li>";
            }
            $sql = "\r\n\t\t\t\tINSERT INTO hire.hire_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, basis, hours, weeks, \r\n\t\t\t\t\tmonths, collection, half_day, weekends\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$nasset_id}', '{$nqty}', '{$camt}', '{$nhifrm}', '{$nhito}', '{$nbasis}', '{$hours}', '{$weeks}', \r\n\t\t\t\t\t'{$months}', '{$collection}', '{$nhalf_day}', '{$nweekends}'\r\n\t\t\t\t)";
            db_exec($sql) or errDie("Unable to create new invoice item.");
            $item_id = pglib_lastid("hire.hire_invitems", "id");
            $sql = "\r\n\t\t\t\tINSERT INTO hire.reprint_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, basis, hours, weeks, \r\n\t\t\t\t\tmonths, collection, half_day, weekends, item_id\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$nasset_id}', '{$nqty}', '{$camt}', '{$nhifrm}', '{$nhito}', '{$nbasis}', '{$hours}', '{$weeks}', \r\n\t\t\t\t\t'{$months}', '{$collection}', '{$nhalf_day}', '{$nweekends}', '{$item_id}'\r\n\t\t\t\t)";
            db_exec($sql) or errDie("Unable to create reprint invoice item.");
        }
    }
    if ($monthly == "true") {
        $sql = "DELETE FROM hire.monthly_invitems WHERE invid='{$invid}'";
        db_exec($sql) or errDie("Unable to remove monthly items.");
    } else {
        $sql = "SELECT * FROM hire.hire_invitems WHERE invid='{$invid}'";
        $mii_rslt = db_exec($sql) or errDie("Unable to retrieve inv items.");
        $sql = "DELETE FROM hire.monthly_invitems WHERE invid='{$invid}'";
        db_exec($sql) or errDie("Unable to remove monthly items.");
        while ($item = pg_fetch_array($mii_rslt)) {
            $sql = "\r\n\t\t\t\tINSERT INTO hire.monthly_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, \r\n\t\t\t\t\tbasis, hours, weeks, months, collection, half_day, \r\n\t\t\t\t\tweekends, item_id, invnum\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$item['invid']}', '{$item['asset_id']}', '{$item['qty']}', '{$item['amt']}', '{$item['from_date']}', '{$item['to_date']}', \r\n\t\t\t\t\t'{$item['basis']}', '{$item['hours']}', '{$item['weeks']}', '{$item['months']}', '{$item['collection']}', '{$item['half_day']}',\r\n\t\t\t\t\t'{$item['weekends']}', '{$item['id']}', '{$invb['invnum']}'\r\n\t\t\t\t)";
            db_exec($sql) or errDie("Unable to create monthly items.");
        }
    }
    $sql = "SELECT * FROM hire.reprint_invoices WHERE invid='{$invid}'";
    $ri_rslt = db_exec($sql) or errDie("Unable to retrieve reprints.");
    // Create a new entry, or update
    if (pg_num_rows($ri_rslt)) {
        $sql = "\r\n\t\t\tUPDATE hire.reprint_invoices \r\n\t\t\tSET deptid='{$invb['deptid']}', cusnum='{$invb['cusnum']}', deptname='{$invb['deptname']}', cusacc='{$invb['cusacc']}',\r\n\t\t\t\tcusname='{$invb['cusname']}', surname='{$invb['surname']}', cusaddr='{$invb['cusaddr']}', cusvatno='{$invb['cusvatno']}', \r\n\t\t\t\tcordno='{$invb['cordno']}', ordno='{$invb['ordno']}', chrgvat='{$invb['chrgvat']}', terms='{$invb['terms']}', \r\n\t\t\t\ttraddisc='{$invb['traddisc']}', salespn='{$invb['salespn']}', odate='{$invb['odate']}', delchrg='{$delchrg}', \r\n\t\t\t\tsubtot='{$invb['subtot']}', vat='{$invb['vat']}', total='{$invb['total']}', balance='{$invb['balance']}', \r\n\t\t\t\tcomm='{$invb['comm']}', printed='{$invb['printed']}', done='{$invb['done']}', div='{$invb['div']}', \r\n\t\t\t\tusername='******'username']}', rounding='{$invb['rounding']}', delvat='{$invb['delvat']}', vatnum='{$invb['vatnum']}', \r\n\t\t\t\tpcash='{$invb['pcash']}', pcheque='{$invb['pcheque']}', pcc='{$invb['pcc']}', pcredit='{$invb['pcredit']}' \r\n\t\t\tWHERE invid='{$invid}'";
        db_exec($sql) or errDie("Unable to update reprint.");
    } else {
        $sql = "\r\n\t\t\tINSERT INTO hire.reprint_invoices (\r\n\t\t\t\tinvid, invnum, deptid, cusnum, deptname, cusacc, \r\n\t\t\t\tcusname, surname, cusaddr, cusvatno, cordno, ordno, \r\n\t\t\t\tchrgvat, terms, traddisc, salespn, odate, delchrg, \r\n\t\t\t\tsubtot, vat, total, balance, comm, printed, done, div, \r\n\t\t\t\tusername, rounding, delvat, vatnum, pcash, pcheque, \r\n\t\t\t\tpcc, pcredit\r\n\t\t\t) VALUES (\r\n\t\t\t\t'{$invid}', '{$invb['invnum']}', '{$invb['deptid']}', '{$invb['cusnum']}', '{$invb['deptname']}', '{$invb['cusacc']}', \r\n\t\t\t\t'{$invb['cusname']}', '{$invb['surname']}', '{$invb['cusaddr']}', '{$invb['cusvatno']}', '{$invb['cordno']}', '{$invb['ordno']}',\r\n\t\t\t\t'{$invb['chrgvat']}', '{$invb['terms']}', '{$invb['traddisc']}', '{$invb['salespn']}', '{$invb['odate']}', '{$invb['delchrg']}', \r\n\t\t\t\t'{$invb['subtot']}', '{$invb['vat']}' , '{$invb['total']}', '{$invb['balance']}', '{$invb['comm']}', 'y', 'y', '" . USER_DIV . "', \r\n\t\t\t\t'" . USER_NAME . "', '{$invb['rounding']}', '{$invb['delvat']}', '{$invb['vatnum']}', '{$invb['pcash']}', '{$invb['pcheque']}', \r\n\t\t\t\t'{$invb['pcc']}', '{$invb['pcredit']}'\r\n\t\t\t)";
        db_exec($sql) or errDie("Unable to add reprint.");
    }
    $sql = "SELECT * FROM hire.monthly_invoices WHERE invid='{$invid}' OR invnum='{$invb['invnum']}' AND invnum!=0";
    $mi_rslt = db_exec($sql) or errDie("Unable to retrieve monthly.");
    // Should we create a new entry
    if (pg_num_rows($mi_rslt)) {
        $sql = "\r\n\t\t\tUPDATE hire.monthly_invoices \r\n\t\t\tSET deptid='{$invb['deptid']}', cusnum='{$invb['cusnum']}', deptname='{$invb['deptname']}', cusacc='{$invb['cusacc']}', \r\n\t\t\t\tcusname='{$invb['cusname']}', surname='{$invb['surname']}', cusaddr='{$invb['cusaddr']}', cusvatno='{$invb['cusvatno']}', \r\n\t\t\t\tcordno='{$invb['cordno']}', ordno='{$invb['ordno']}', chrgvat='{$invb['chrgvat']}', terms='{$invb['terms']}', \r\n\t\t\t\ttraddisc='{$invb['traddisc']}', salespn='{$invb['salespn']}', odate='{$invb['odate']}', delchrg='{$invb['delchrg']}', \r\n\t\t\t\tsubtot='{$invb['subtot']}', vat='{$invb['vat']}', total='{$invb['total']}', balance='{$invb['balance']}', \r\n\t\t\t\tcomm='{$invb['comm']}', printed='{$invb['printed']}', done='{$invb['done']}', div='{$invb['div']}', \r\n\t\t\t\tusername='******'username']}', rounding='{$invb['rounding']}', delvat='{$invb['delvat']}', vatnum='{$invb['vatnum']}', \r\n\t\t\t\tpcash='{$invb['pcash']}', pcheque='{$invb['pcheque']}', pcc='{$invb['pcc']}', pcredit='{$invb['pcredit']}', \r\n\t\t\t\thire_invid='{$invid}' \r\n\t\t\tWHERE invid='{$invb['invid']}'";
    } elseif (empty($monthly)) {
        $sql = "\r\n\t\t\t\tINSERT INTO hire.monthly_invoices (\r\n\t\t\t\t\tinvid, invnum, deptid, cusnum, deptname, cusacc, \r\n\t\t\t\t\tcusname, surname, cusaddr, cusvatno, cordno, \r\n\t\t\t\t\tordno, chrgvat, terms, traddisc, salespn, \r\n\t\t\t\t\todate, delchrg, subtot, vat, total, balance, \r\n\t\t\t\t\tcomm, printed, done, div, username, rounding, delvat, \r\n\t\t\t\t\tvatnum, pcash, pcheque, pcc, pcredit, invoiced_month, \r\n\t\t\t\t\thire_invid\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$invb['invnum']}', '{$invb['deptid']}', '{$invb['cusnum']}', '{$invb['deptname']}', '{$invb['cusacc']}', \r\n\t\t\t\t\t'{$invb['cusname']}', '{$invb['surname']}', '{$invb['cusaddr']}', '{$invb['cusvatno']}', '{$invb['cordno']}', \r\n\t\t\t\t\t'{$invb['ordno']}', '{$invb['chrgvat']}', '{$invb['terms']}', '{$invb['traddisc']}', '{$invb['salespn']}', \r\n\t\t\t\t\t'{$invb['odate']}', '{$invb['delchrg']}', '{$invb['subtot']}', '{$invb['vat']}', '{$invb['total']}', '{$invb['balance']}', \r\n\t\t\t\t\t'{$invb['comm']}', 'y', 'y', '" . USER_DIV . "', '" . USER_NAME . "', '{$invb['rounding']}', '{$invb['delvat']}', \r\n\t\t\t\t\t'{$invb['vatnum']}', '{$invb['pcash']}', '{$invb['pcheque']}', '{$invb['pcc']}', '{$invb['pcredit']}', '" . date("m") . "', \r\n\t\t\t\t\t'{$invb['invid']}' \r\n\t\t\t\t)";
    }
    db_exec($sql) or errDie("Unable to store monthly invoice.");
    pglib_transaction("COMMIT");
    if (!isSerialized($nasset_id) && (!isset($_SESSION["ns"]) || !$_SESSION["ns"])) {
        $_SESSION["ns"] = 1;
    } else {
        $_SESSION["ns"] = 0;
    }
    if (isset($upBtn)) {
        if ($upBtn == "Return") {
            return returnHire();
        } elseif ($upBtn == "Invoice") {
            return invoiceHire();
        }
    }
    return false;
}
コード例 #4
0
function availability($from_date, $to_date, $asset_id)
{
    $e_from = getDTEpoch("{$from_date} 0:00:00");
    $e_to = getDTEpoch("{$to_date} 23:59:59");
    $row = "";
    for ($i = $e_from; $i < $e_to; $i += SECONDS_IN_DAY) {
        for ($j = 0; $j < 5; $j++) {
            if (inWorkshop($asset_id, date("Y-m-d", $i))) {
                $color = WORKSHOP;
            } elseif (isHired($asset_id, date("Y-m-d", $i))) {
                $color = HIRED_OUT;
            } elseif (isBooked($asset_id, date("Y-m-d", $i))) {
                $color = BOOKED;
            } else {
                $color = AVAILABLE;
            }
            $row .= "<td bgcolor='{$color}'></td>";
        }
    }
    return $row;
}
コード例 #5
0
function isHiredd($asset_id, $date = false)
{
    if (!$date) {
        $date = date("Y-m-d");
    }
    $sql = "SELECT hire_invitems.id, hours, weeks, serial, serial2,\r\n\t\t\t\tprinted, done, extract('epoch' FROM from_date) AS e_from,\r\n\t\t\t\textract('epoch' FROM to_date) AS e_to, return_time\r\n\t\t\tFROM hire.hire_invitems\r\n\t\t\t\tLEFT JOIN hire.hire_invoices\r\n\t\t\t\t\tON hire_invitems.invid = hire_invoices.invid\r\n\t\t\t\tLEFT JOIN cubit.assets\r\n\t\t\t\t\tON hire_invitems.asset_id = assets.id\r\n\t\t\t\tLEFT JOIN hire.assets_hired\r\n\t\t\t\t\tON hire_invitems.id = assets_hired.item_id\r\n\t\t\tWHERE hire_invitems.asset_id='{$asset_id}'";
    $item_rslt = db_exec($sql) or errDie("Unable to retrieve items.");
    // Check if item in workshop
    if (inWorkshop($asset_id, $date)) {
        return true;
    }
    while ($item_data = pg_fetch_array($item_rslt)) {
        if (!isSerialized($asset_id) && $item_data["serial2"] > 0) {
            return false;
        }
        if ($item_data["printed"] == "n" || $item_data["done"] == "n") {
            continue;
        }
        if (!empty($item_data["hours"])) {
            $to_date = hiredDate($item_data["id"], "U") + HOURS * $item_data["hours"];
        } elseif (!empty($item_data["weeks"])) {
            $to_date = hiredDate($item_data["id"], "U") + WEEKS * $item_data["weeks"];
        } else {
            $to_date = $item_data["e_to"];
        }
        $date = getDTEpoch("{$date} 0:00:00");
        if ($date >= $item_data["e_from"] && !$item_data["return_time"] && $date <= time()) {
            return true;
        }
        if ($date >= $item_data["e_from"] && $date <= $to_date) {
            return true;
        }
    }
    return false;
}
コード例 #6
0
function write()
{
    extract($_REQUEST);
    require_lib("validate");
    $v = new validate();
    $v->isOk($asset_id, "num", 1, 9, "Invalid asset selection.");
    $v->isOk($cust_id, "num", 1, 9, "Invalid customer selection.");
    $v->isOk($from_year, "num", 4, 4, "Invalid from date (year).");
    $v->isOk($from_month, "num", 1, 2, "Invalid from date (month).");
    $v->isOk($from_day, "num", 1, 2, "Invalid from date (day).");
    $v->isOk($to_year, "num", 4, 4, "Invalid to date (year).");
    $v->isOk($to_month, "num", 1, 2, "Invalid to date (month).");
    $v->isOk($to_day, "num", 1, 2, "Invalid to date (day).");
    $v->isOk($units, "num", 1, 80, "Invalid units.");
    // Booking date
    $from_date = dateFmt($from_year, $from_month, $from_day);
    $to_date = dateFmt($to_year, $to_month, $to_day);
    $e_from = getDTEpoch("{$from_date} 0:00:00");
    $e_to = getDTEpoch("{$to_date} 23:59:59");
    for ($i = $e_from; $i < $e_to; $i += DAYS) {
        $date = date("Y-m-d", $i);
        if (unitsAvailable($asset_id, $date) - $units < 0) {
            $available = unitsAvailable($asset_id, $date);
            $v->addError(0, "Only {$available} units available on {$date}.");
        }
    }
    if ($v->isError()) {
        return enter($v->genErrors());
    }
    if ($page_option == "edit") {
        $sql = "\r\n\t\tUPDATE hire.bookings SET asset_id='{$asset_id}', cust_id='{$cust_id}',\r\n\t\t\tfrom_date='{$from_date}', to_date='{$to_date}', units='{$units}'\r\n\t\tWHERE id='{$id}'";
    } else {
        $sql = "\r\n\t\tINSERT INTO hire.bookings (asset_id, cust_id, from_date, to_date, units)\r\n\t\tVALUES ('{$asset_id}', '{$cust_id}', '{$from_date}', '{$to_date}', '{$units}')";
    }
    db_exec($sql) or errDie("Unable to save booking.");
    $OUTPUT = "<h3>" . ucfirst($page_option) . " Booking</h3>\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th>Write</th>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td><li>Successfully saved booking.</li></td>\r\n\t\t</tr>\r\n\t</table>";
    return $OUTPUT;
}
コード例 #7
0
function utilisationPerc($asset_id, $from_date, $to_date)
{
    $DAYS = 60 * 60 * 24;
    $from_time = getDTEpoch("{$from_date} 0:00:00");
    $to_time = getDTEpoch("{$to_date} 23:59:59");
    $total_days = ($to_time - $from_time) / $DAYS;
    $hired_days = utilisationDays($asset_id, $from_date, $to_date);
    $percentage = $hired_days / $total_days * 100;
    return sprint($percentage);
}
コード例 #8
0
function updateAmounts($item_id)
{
    pglib_transaction("BEGIN");
    $sql = "SELECT *, cusnum\r\n\t\t\tFROM hire.hire_invitems\r\n\t\t\t\tLEFT JOIN hire.hire_invoices\r\n\t\t\t\t\tON hire_invitems.invid=hire_invoices.invid\r\n\t\t\tWHERE id='{$item_id}'";
    $item_rslt = db_exec($sql) or errDie("Unable to retrieve hire items.");
    $item = pg_fetch_array($item_rslt);
    if ($item["basis"] == "per_day") {
        $from_time = getDTEpoch("{$item['from_date']} 0:00:00");
        $to_time = getDTEpoch("{$item['to_date']} 23:59:59");
        $days = 0;
        $weeks = 0;
        while ($from_time <= $to_time) {
            if (date("w", $from_time) == 0 && $item["weekends"]) {
                $days += 0.6;
            } else {
                ++$days;
            }
            $from_time += 60 * 60 * 24;
        }
        $timeunits = ceil($days);
    } elseif ($item["basis"] == "per_week") {
        $timeunits = $item["weeks"];
    } elseif ($item["basis"] == "per_hour") {
        $timeunits = $item["hours"];
    }
    $amount = $item["qty"] * $timeunits * basisPrice($item["cusnum"], $item["asset_id"], $item["basis"]);
    if ($item["basis"] = "per_day") {
        $total_days = ",total_days='{$timeunits}'";
    } else {
        $total_days = "0";
    }
    $sql = "UPDATE hire.hire_invitems SET amt='{$amount}' {$total_days} WHERE id='{$item_id}'";
    db_exec($sql) or errDie("Unable to update hire items.");
    // Update totals
    $sql = "\r\n\tSELECT sum(amt) FROM hire.hire_invitems\r\n\tWHERE invid='{$item['invid']}'";
    $subtot_rslt = db_exec($sql) or errDie("Unable to retrieve subtotal.");
    $subtot = pg_fetch_result($subtot_rslt, 0);
    // Trade discount
    $sql = "SELECT traddisc FROM hire.hire_invoices WHERE invid='{$item['invid']}'";
    $disc_rslt = db_exec($sql) or errDie("Unable to retrieve discount.");
    $disc = pg_fetch_result($disc_rslt, 0);
    $traddiscamt = $subtot / 100 * $disc;
    $subtot = $subtot - $traddiscamt;
    $vat = $subtot / 100 * 14;
    $total = $subtot + $vat;
    $sql = "\r\n\tUPDATE hire.hire_invoices SET total='{$total}', vat='{$vat}',\r\n\t\tdiscount='{$traddiscamt}' \r\n\tWHERE invid='{$item['invid']}'";
    db_exec($sql) or errDie("Unable to update amounts.");
    pglib_transaction("COMMIT");
    return $amount;
}
コード例 #9
0
function averageSalesQty($stkid, $from_date, $to_date, $prd)
{
    $DAYS = 60 * 60 * 24;
    $WEEKS = $DAYS * 7;
    $MONTHS = $DAYS * 30;
    $times = getDTEpoch("{$to_date} 23:59:59") - getDTEpoch("{$from_date} 0:00:00");
    $days = $times / $DAYS;
    if ($days < $MONTHS) {
        $MONTHS = $days;
    }
    $total_days = 0;
    $sql = "SELECT sum(qty) FROM cubit.inv_items\n\t\t\t\tLEFT JOIN cubit.invoices\n\t\t\t\t\tON inv_items.invid = invoices.invid\n\t\t\tWHERE odate BETWEEN '{$from_date}' AND '{$to_date}' AND stkid='{$stkid}'";
    $inv_rslt = db_exec($sql) or errDie("Unable to retrieve invoices.");
    $inv_qty = pg_fetch_result($inv_rslt, 0);
    if (empty($inv_qty)) {
        $inv_qty = 0;
    }
    $from = explode("-", $from_date);
    $to = explode("-", $to_date);
    $from_time = mktime(0, 0, 0, $from[1], $from[2], $from[0]);
    $to_time = mktime(0, 0, 0, $to[1], $to[2], $to[0]);
    switch ($prd) {
        case "daily":
            $total_time = $from_time - $to_time;
            $total_days = (int) ($total_time / $DAYS);
            break;
        case "weekly":
            $total_time = $from_time - $to_time;
            $total_days = (int) ($total_time / $WEEKS);
            break;
        case "monthly":
            $total_time = $from_time - $to_time;
            $total_days = (int) ($total_time / $MONTHS);
            break;
    }
    $total_days = abs($total_days);
    if ($inv_qty && $total_days) {
        $average = ceil($inv_qty / $total_days);
    } else {
        $average = 0;
    }
    return $average;
}