function display($msg = "")
{
    extract($_REQUEST);
    $fields = array();
    $fields["search"] = "~:BLANK:~";
    extract($fields, EXTR_SKIP);
    removeDead();
    $sql = "SELECT invid FROM hire.hire_invoices WHERE done='y' AND printed='y'";
    $inv_rslt = db_exec($sql) or errDie("Unable to retrieve hire notes.");
    while ($inv_data = pg_fetch_array($inv_rslt)) {
        updateTotals($inv_data["invid"]);
    }
    // Retrieve invoices
    if ($search == "~:BLANK:~") {
        // We don't want any results at first
        $sql = "SELECT * FROM hire.hire_invoices WHERE cusnum='-1.23'";
        $search = "";
    } else {
        if (!empty($search)) {
            $sql = "SELECT * FROM hire.hire_invoices WHERE done='y'\r\n\t\t\t\t\tAND invnum ILIKE '%{$search}%'\r\n\t\t\t\tUNION\r\n\t\t\t\tSELECT * FROM hire.hire_invoices WHERE done='y'\r\n\t\t\t\t\tAND cusname ILIKE '%{$search}%'\r\n\t\t\t\tORDER BY invnum DESC";
        } else {
            $sql = "SELECT * FROM hire.hire_invoices\r\n\t\t\t\t\tWHERE done='y'\r\n\t\t\t\t\tORDER BY invnum DESC";
        }
    }
    $inv_rslt = db_exec($sql) or errDie("Unable to retrieve invoices.");
    $total = 0;
    $hires_out = "";
    while ($inv_data = pg_fetch_array($inv_rslt)) {
        // Retrieve customer
        $sql = "SELECT * FROM cubit.customers WHERE cusnum='{$inv_data['cusnum']}'";
        $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customers.");
        $cust_data = pg_fetch_array($cust_rslt);
        $hires_out .= "<tr class='" . bg_class() . "'>\r\n\t\t\t<td>\r\n\t\t\t\t<a href='javascript:printer(\"hire/hire-invoice-new.php?invid={$inv_data['invid']}&reprint=1\")'>\r\n\t\t\t\t\tH{$inv_data['invnum']}" . rev($inv_data["invid"]) . "\r\n\t\t\t\t</a>\r\n\t\t\t</td>\r\n\t\t\t<td>{$cust_data['surname']}</td>\r\n\t\t\t<td>" . CUR . "{$inv_data['total']}</td>\r\n\t\t\t<td>\r\n\t\t\t\t<a href='hire-invoice-new.php?invid={$inv_data['invid']}&reprint=1'>\r\n\t\t\t\t\tView Hire\r\n\t\t\t\t</a>\r\n\t\t\t</td>\r\n\t\t\t<td>\r\n\t\t\t<a href='javascript:printer(\"hire/hire_note_reprint.php?invid={$inv_data['invid']}\")'>Reprint</a>\r\n\t\t\t</td>\r\n\t\t\t<!--<td><a href='hires_return.php?invid={$inv_data['invid']}'>Return</a></td>-->\r\n\t\t\t<!--<td><a href='hires_swap.php?invid={$inv_data['invid']}'>Swap</a></td>-->\r\n\t\t</tr>";
        $total += $inv_data["total"];
    }
    if (empty($hires_out)) {
        $hires_out = "<tr class='" . bg_class() . "'>\r\n\t\t\t<td colspan='5'>Please enter a hire no or customer name</td>\r\n\t\t</tr>";
    }
    if (isset($added)) {
        $msg = "<li class='err'>Item/s successfully hired out</li>";
    }
    $OUTPUT = "<h3>View Hire</h3>\r\n\t<form method='post' action='" . SELF . "'>\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th colspan='2'>Enter Hire No or Client Name</th>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td><input type='text' name='search' value='{$search}' /></td>\r\n\t\t\t<td><input type='submit' value='Filter' /></td>\r\n\t\t</tr>\r\n\t</table>\r\n\t</form>\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<td colspan='5'>{$msg}</td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<th>Hire No</td>\r\n\t\t\t<th>Customer</th>\r\n\t\t\t<th>Total</th>\r\n\t\t\t<th colspan='2'>Options</th>\r\n\t\t</tr>\r\n\t\t{$hires_out}\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td colspan='2'><strong>Total</strong></td>\r\n\t\t\t<td>" . sprint($total) . "</td>\r\n\t\t\t<td colspan='4'>&nbsp;</td>\r\n\t\t</tr>\r\n\t</table>";
    return $OUTPUT;
}
function details($_POST, $error = "")
{
    extract($_REQUEST);
    $fields = array();
    $fields["deptid"] = 2;
    $fields["cusnum"] = 0;
    $fields["telno"] = "";
    $fields["cordno"] = "";
    $fields["des"] = "";
    $fields["pinv_day"] = date("d");
    $fields["pinv_month"] = date("m");
    $fields["pinv_year"] = date("Y");
    $fields["vatinc_yes"] = "checked";
    $fields["vatinc_no"] = "";
    $fields["vat14"] = AT14;
    $fields["vat"] = "0.00";
    $fields["total"] = "0.00";
    $fields["rounding"] = "";
    $fields["nhifrm_year"] = date("Y");
    $fields["nhifrm_month"] = date("m");
    $fields["nhifrm_day"] = date("d");
    $fields["nhito_year"] = date("Y");
    $fields["nhito_month"] = date("m");
    $fields["nhito_day"] = date("d");
    $fields["client_collect"] = "";
    $fields["collect"] = "";
    $fields["deliver"] = "";
    $fields["deposit_amt"] = "0.00";
    $fields["deposit_type"] = "CSH";
    $fields["custom_txt"] = "";
    $fields["monthly"] = false;
    $fields["bk_asset"] = 0;
    // 30 Asset
    $fields["bk_id"] = 0;
    $fields["reprint"] = 0;
    extract($fields, EXTR_SKIP);
    if (isset($bk_from)) {
        list($nhifrm_year, $nhifrm_month, $nhifrm_day) = explode("-", $bk_from);
    }
    if (isset($bk_to)) {
        list($nhito_year, $nhito_month, $nhito_day) = explode("-", $bk_to);
    }
    $subtot = 0;
    if (isset($hirenewBtn)) {
        newHire($_POST);
    }
    // Get us an invoice id
    if (!isset($invid)) {
        $invid = create_dummy($deptid);
    } else {
        $sql = "SELECT cusnum FROM hire.hire_invoices WHERE invid='{$invid}'";
        $cn_rslt = db_exec($sql) or errDie("Unable to retrieve invoices.");
        $cusnum = pg_fetch_result($cn_rslt, 0);
        updateTotals($invid);
    }
    $ind_ccol = "";
    $ind_col = "";
    $ind_del = "";
    $collect_ar = array();
    if (!empty($client_collect)) {
        $collect_ar[] = "Client Collect";
    }
    if (!empty($collect)) {
        $collect_ar[] = "Collect";
    }
    if (!empty($deliver)) {
        $collect_ar[] = "Deliver";
    }
    if (empty($client_collect) && empty($collect) && empty($deliver)) {
        $client_collect = "checked";
        $collect_ar[] = "Client Collect";
    }
    $collection = implode(", ", $collect_ar);
    if (empty($monthly)) {
        $sql = "SELECT *, extract('epoch' FROM expected) AS e_exp, extract('epoch' FROM to_date) AS e_to FROM hire.hire_invitems WHERE invid='{$invid}'";
        $item_rslt = db_exec($sql) or errDie("Unable to retrieve items.");
        while ($item_data = pg_fetch_array($item_rslt)) {
            if (!empty($item_data["expected"])) {
                if ($item_data["e_to"] > time()) {
                    $item_data["expected"] = date("Y-m-t", $item_data["e_to"]);
                } else {
                    if ($item_data["e_exp"] < time()) {
                        $item_data["expected"] = date("Y-m-t");
                    }
                }
                $sql = "\r\n\t\t\t\t\tUPDATE hire.hire_invitems \r\n\t\t\t\t\tSET from_date='{$item_data['to_date']}', to_date='{$item_data['expected']}', expected=NULL \r\n\t\t\t\t\tWHERE id='{$item_data['id']}'";
                db_exec($sql) or errDie("Unable to update invoice.");
            }
        }
    }
    // Retrieve the actual invoice
    $sql = "SELECT * FROM hire.hire_invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $inv_rslt = db_exec($sql) or errDie("Unable to get invoice information");
    $inv_data = pg_fetch_array($inv_rslt);
    if ($cusnum == 0) {
        $cusnum = $inv_data["cusnum"];
    }
    if (empty($cordno)) {
        $cordno = $inv_data["cordno"];
    }
    $pinv_date = explode("-", $inv_data["odate"]);
    $pinv_year = $pinv_date[0];
    $pinv_month = $pinv_date[1];
    $pinv_day = $pinv_date[2];
    // Create the dropdowns ---------------------------------------------------
    // Retrieve departments
    $sql = "SELECT * FROM exten.departments ORDER BY deptname ASC";
    $dept_rslt = db_exec($sql) or errDie("Unable to retrieve departments.");
    // Create departments dropdown
    $dept_sel = "<select name='deptid' style='width: 100%'>";
    while ($dept_data = pg_fetch_array($dept_rslt)) {
        $dept_sel .= "<option value='{$dept_data['deptid']}'>{$dept_data['deptname']}</option>";
    }
    $dept_sel .= "</select>";
    // Check customer basis
    if ($cusnum > 0) {
        checkCustBasis($cusnum);
    }
    // Retrieve customers
    $sql = "SELECT * FROM cubit.customers ORDER BY surname ASC";
    $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customers.");
    // Create customers dropdown
    if (empty($cusnum)) {
        $cust_sel = "\r\n\t\t\t<select name='cusnum' style='width: 100%' onchange='javascript:document.form.submit()'>\r\n\t\t\t\t<option value='0'>[None]</option>";
        while ($cust_data = pg_fetch_array($cust_rslt)) {
            $sel = fsel(isset($cusnum) && $cusnum == $cust_data["cusnum"]);
            $cust_sel .= "<option value='{$cust_data['cusnum']}' {$sel}>{$cust_data['surname']}</option>";
        }
        $cust_sel .= "</select>";
    } else {
        $sql = "SELECT * FROM cubit.customers WHERE cusnum='{$cusnum}'";
        $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customer.");
        $cust_data = pg_fetch_array($cust_rslt);
        $cust_sel = $cust_data["surname"];
    }
    // Retrieve sales people
    $sql = "SELECT * FROM exten.salespeople ORDER BY salesp ASC";
    $salesp_rslt = db_exec($sql) or errDie("Unable to retrieve sales people.");
    // Create sales people dropdown
    $salesp_sel = "<select name='salespid' style='width: 100%'>";
    while ($salesp_data = pg_fetch_array($salesp_rslt)) {
        $salesp_sel .= "<option value='{$salesp_data['salespid']}'>{$salesp_data['salesp']}</option>";
    }
    $salesp_sel .= "</select>";
    // Deposit Options
    $deposit_list = array("CSH" => "Cash", "CHQ" => "Cheque", "CRD" => "Credit Card");
    // Create the deposit dropdown
    $deposit_sel = "<select name='deposit_type'>";
    foreach ($deposit_list as $key => $value) {
        if ($inv_data["deposit_type"] == $key) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $deposit_sel .= "<option value='{$key}' {$sel}>{$value}</option>";
    }
    $deposit_sel .= "</select>";
    // Items Display -------------------------------------------------------
    $basis_list = array("per_day" => "Per Day", "per_hour" => "Per Hour", "per_week" => "Per Week");
    // Retrieve items
    $sql = "SELECT * FROM hire.hire_invitems WHERE invid='{$invid}' ORDER BY id ASC";
    $items_rslt = db_exec($sql) or errDie("Unable to retrieve items.");
    $items_out = "";
    $temp_assets = array();
    while ($items_data = pg_fetch_array($items_rslt)) {
        $i = $items_data["id"];
        // Create the basis display
        $basis_disp = $basis_list[$items_data["basis"]];
        // Retrieve assets
        $sql = "SELECT * FROM cubit.assets WHERE id='{$items_data['asset_id']}'";
        $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
        $ad = pg_fetch_array($asset_rslt);
        // Retrieve asset group
        $sql = "SELECT * FROM cubit.assetgrp WHERE grpid='{$ad['grpid']}'";
        $agrp_rslt = db_exec($sql) or errDie("Unable to retrieve asset group.");
        $agrp_data = pg_fetch_array($agrp_rslt);
        if ($agrp_data["grpname"] == "Temporary Asset") {
            $temp_assets[] = $agrp_data[$ad["id"]];
        }
        // 		if ($ad["serial"] == "CUBIT::-QTY-") {
        // 			$asset_disp = "$ad[des]";
        // 		} else {
        // 			$asset_disp = "$ad[des] ($ad[serial])";
        // 		}
        $asset_disp = $ad["des"] . " " . getSerial($ad["id"], 1);
        $subtot += $items_data["amt"];
        //*$items_data["qty"];
        if ($items_data["basis"] == "per_hour") {
            $from_disp = "Hours: {$items_data['hours']}";
            $to_disp = "";
        } else {
            if ($items_data["basis"] == "per_day") {
                $mfrm_date = $items_data["from_date"];
                $mfrm_date = explode("-", $mfrm_date);
                $mfrm_year[$i] = $mfrm_date[0];
                $mfrm_month[$i] = $mfrm_date[1];
                $mfrm_day[$i] = $mfrm_date[2];
                $mto_date = $items_data["to_date"];
                $mto_date = explode("-", $mto_date);
                $mto_year[$i] = $mto_date[0];
                $mto_month[$i] = $mto_date[1];
                $mto_day[$i] = $mto_date[2];
                $from_disp = mkDateSelectA("mfrm", $i, $mfrm_year[$i], $mfrm_month[$i], $mfrm_day[$i]);
                $to_disp = mkDateSelectA("mto", $i, $mto_year[$i], $mto_month[$i], $mto_day[$i]);
                // 				$from_disp = "
                // 				<input type='hidden' name='mfrm_year[$i]' value='$mfrm_year[$i]' />
                // 				<input type='hidden' name='mfrm_month[$i]' value='$mfrm_month[$i]' />
                // 				<input type='hidden' name='mfrm_day[$i]' value='$mfrm_day[$i]' />
                // 				$mfrm_day[$i]-$mfrm_month[$i]-$mfrm_year[$i]";
                // 				$to_disp = "
                // 				<input type='hidden' name='mto_year[$i]' value='$mto_year[$i]' />
                // 				<input type='hidden' name='mto_month[$i]' value='$mto_month[$i]' />
                // 				<input type='hidden' name='mto_day[$i]' value='$mto_day[$i]' />
                // 				$mto_day[$i]-$mto_month[$i]-$mto_year[$i]";
                $from_date[$i] = "{$mfrm_year[$i]}-{$mfrm_month[$i]}-{$mfrm_day[$i]}";
                $to_date[$i] = "{$mto_year[$i]}-{$mto_month[$i]}-{$mto_day[$i]}";
                $hidden_date = "\r\n\t\t\t\t\t<input type='hidden' name='from_date[{$i}]' value='{$from_date[$i]}' />\r\n\t\t\t\t\t<input type='hidden' name='to_date[{$i}]' value='{$to_date[$i]}' />";
                // 				$from_disp = "$items_data[from_date]";
                // 				$to_disp = "$items_data[to_date]";
            } else {
                if ($items_data["basis"] == "per_week") {
                    $from_disp = "Weeks: {$items_data['weeks']}";
                    $to_disp = "";
                }
            }
        }
        if (!isset($return[$i])) {
            $return[$i] = "";
        }
        if (!isset($hidden_date)) {
            $hidden_date = "";
        }
        if (!isset($rain_days[$i])) {
            $rain_days[$i] = 0;
        }
        if ($items_data["basis"] == "per_day") {
            $rd_disp = "<input type='hidden' name='rain_days[{$i}]' \t\t\t\r\n\t\t\t\t\t\tvalue='{$rain_days[$i]}' size='3' style='text align: center' />";
            if ($items_data["half_day"]) {
                //				$hd_disp = "<input type='checkbox' name='half_day[$i]' value='1' checked /> Half Day</b>";
                $hd_disp = "<input type='hidden' name='half_day[{$i}]' value='1' />";
            } else {
                $hd_disp = "<input type='hidden' name='half_day[{$i}]' value='0' />";
                //				$hd_disp = "<input type='checkbox' name='half_day[$i]' value='1' /> Half Day";
            }
        } else {
            $hd_disp = "";
            $rd_disp = "<input type='hidden' name='rain_days[{$i}]' value='0' />";
        }
        if ($items_data["weekends"]) {
            $weekends[$i] = "checked";
        } else {
            $weekends[$i] = "";
        }
        // Items should not be removed once processed, use reprint to check
        // if this hire note has already been processed.
        if (isset($reprint) && $reprint || !empty($monthly)) {
            $rem_cbox = "";
        } else {
            $rem_cbox = "<td><input type='checkbox' name='remove[{$i}]'></td>";
        }
        if (isset($monthly) && $monthly) {
            if ($items_data["basis"] == "per_day") {
                $ret_cbox = "<td><input type='checkbox' name='return[{$i}]' value='checked' {$return[$i]}></td>";
            } else {
                $ret_cbox = "<td>&nbsp;</td>";
            }
        } else {
            $ret_cbox = "<td><input type='checkbox' name='return[{$i}]' value='checked' {$return[$i]}></td>";
        }
        $amt = sprint($items_data["amt"]);
        if (user_is_admin(USER_ID)) {
            $amount_out = "<input type='text' name='amount[{$i}]' value='{$amt}' size='7' />";
        } else {
            $amount_out = "<input type='hidden' name='amount[{$i}]' value='{$amt}' />{$amt}";
        }
        $items_out .= "\r\n\t\t\t<input type='hidden' name='asset_id[{$i}]' value='{$ad['id']}' />\r\n\t\t\t<input type='hidden' name='basis[{$i}]' value='{$items_data['basis']}' />\r\n\t\t\t<input type='hidden' name='qty[{$i}]' value='{$items_data['qty']}' />\r\n\t\t\t{$hidden_date}\r\n\t\t\t{$rd_disp}\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td>{$basis_disp}</td>\r\n\t\t\t\t<td>{$asset_disp}</td>\r\n\t\t\t\t<td align='center'>{$items_data['qty']}</td>\r\n\t\t\t\t<td align='center'>{$from_disp}</td>\r\n\t\t\t\t<td align='center'>{$to_disp} {$hd_disp}</td>\r\n\t<!--\t\t\t\r\n\t\t\t\t<td align='center'>{$rd_disp}</td>\r\n\t\t\t\t<td align='center'>{$items_data['collection']}</td>\r\n\t-->\r\n\t\t\t\t<td>{$amount_out}</td>\r\n\t\t\t\t{$rem_cbox}\r\n\t\t\t\t{$ret_cbox}\r\n\t\t\t</tr>";
    }
    $temp_assets = implode(",", $temp_assets);
    // New Items --------------------------------------------------------------
    // Avoid undefined variable items_out
    if (empty($items_out)) {
        $items_out = "";
    }
    // Buttons
    if (!empty($cusnum)) {
        if ($deposit_type == "CSH" && $deposit_amt != "0.00") {
            $deposit_open = "popupOpen(\"hire-invoice-print.php?key=cash_receipt&invid={$inv_data['invid']}\")";
        } else {
            $deposit_open = "";
        }
        $sql = "SELECT * FROM cubit.customers WHERE cusnum='{$cusnum}'";
        $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customers.");
        $cust_data = pg_fetch_array($cust_rslt);
        $telno = $cust_data["bustel"];
        $return_btn = "<input type='submit' name='upBtn' value='Return' />";
        if (isset($reprint) && $reprint) {
            $new_btn = "<input type='button' value='Reprint' onclick='javascript:printer(\"hire/hire_note_reprint.php?invid={$inv_data['invid']}\");{$deposit_open}'>";
            $purch_btn = "";
            //$purch_btn = "<input type='button' value='Payment' onclick='javascript:popupOpen(\"".SELF."?key=newpos&cusnum=$cusnum\");' />";
        } else {
            if (!$monthly) {
                $new_btn = "<input name='hirenewBtn' type='submit' value='Process' />";
            } else {
                $new_btn = "<input type='submit' name='upBtn' value='Invoice' />";
            }
            $purch_btn = "";
        }
        $hire_buttons = "\r\n\t\t\t<tr>\r\n\t\t\t\t<td>&nbsp;</td>\r\n\t\t\t\t<td align='center'>\r\n\t\t\t\t\t<input type='submit' name='upBtn' value='Update'>\r\n\t\t\t\t\t{$new_btn}\r\n\t\t\t\t\t{$return_btn}\r\n\t\t\t\t\t{$purch_btn}\r\n\t\t\t\t\t<!--<input type='button' value='Swap Hire' />-->\r\n\t\t\t\t</td>\r\n\t\t\t\t<td>&nbsp;</td>\r\n\t\t\t</tr>";
        $basevis = "visible";
        $credit_limit = CUR . sprint($cust_data["credlimit"] - $cust_data["balance"]);
        $cust_balance = CUR . $cust_data["balance"];
    } else {
        $hire_buttons = "";
        $basevis = "hidden";
        $credit_limit = "";
        $cust_balance = "";
    }
    // Retrieve assets
    $sql = "SELECT *  FROM cubit.assets ORDER BY des ASC";
    $nasset_rslt = db_exec($sql) or errDie("Unable to retrieve asset.");
    // Assets dropdown
    $nasset_sel = "\r\n\t\t<select name='nasset_id' style='visibility: {$basevis}; width: 120px' onchange='assetChange(this);'>\r\n\t\t\t<option value='0'>- SELECT PLANT -</option>";
    while ($ad = pg_fetch_array($nasset_rslt)) {
        $sql = "SELECT * FROM hire.hire_invitems WHERE asset_id='{$ad['id']}' AND invid='{$invid}'";
        $invitem_rslt = db_exec($sql) or errDie("Unable to retrieve items.");
        if (pg_num_rows($invitem_rslt)) {
            continue;
        }
        if (!empty($ad["remaction"])) {
            continue;
        }
        if (isHired($ad["id"], date("Y-m-d"))) {
            continue;
        }
        if (!isSerialized($ad["id"])) {
            $at = "q";
            //$asset_disp = "$ad[des] ($ad[serial2] available.)";
            $units_avail = unitsAvailable($ad["id"], date("Y-m-d"));
            $asset_disp = "{$ad['des']} {$units_avail} available.";
            if ($ad["serial2"] <= 0) {
                continue;
            }
        } else {
            $at = "s";
            $asset_disp = "{$ad['des']} ({$ad['serial']})";
        }
        if ($cust_bk = isBooked($ad["id"], date("Y-m-d"))) {
            $sql = "SELECT surname FROM cubit.customers WHERE cusnum='{$cust_bk}'";
            $surname_rslt = db_exec($sql) or errDie("Unable to retrieve customer.");
            $surname = pg_fetch_result($surname_rslt, 0);
            if (isSerialized($ad["id"])) {
                $asset_disp .= " Booked: {$surname}";
            } else {
                $units_booked = unitsBooked($ad["id"], date("Y-m-d"));
                $asset_disp .= " {$units_booked} Units Booked";
            }
        }
        if ($bk_asset == $ad["id"]) {
            $sel = "selected='selected'";
        } else {
            $sel = "";
        }
        $nasset_sel .= "<option value='{$at}:{$ad['id']}' {$sel}>{$asset_disp}</option>";
    }
    $nasset_sel .= "</select>";
    // Create basis dropdown
    $nbasis_sel = "\r\n\t\t<select name='nbasis' style='width: 100%; visibility: {$basevis};' onchange='basisChange(this);'>\r\n\t\t\t<option value='0'>- BASIS -</option>";
    foreach ($basis_list as $key => $value) {
        $nbasis_sel .= "<option value='{$key}'>{$value}</option>";
    }
    $nbasis_sel .= "</select>";
    // Create asset group dropdown
    $sql = "SELECT grpid, grpname FROM cubit.assetgrp ORDER BY grpname ASC";
    $grp_rslt = db_exec($sql) or errDie("Unable to retrieve groups.");
    if ($cusnum) {
        $OTS_OPT = onthespot_encode(SELF, "cust_selection", "deptid={$deptid}&cusnum={$cusnum}&invid={$invid}");
        // 		$cust_edit = "
        // 			<td nowrap>
        // 			<a href='javascript: popupSized(\"../cust-edit.php?cusnum=$cusnum&onthespot=$OTS_OPT\", \"edit_cust\", 700, 630);'>
        // 				Edit Customer Details
        // 			</a>
        // 			</td>";
        $cust_edit = "";
    } else {
        $cust_edit = "";
    }
    // Retrieve service date
    $sql = "SELECT * FROM hire.hire_invitems WHERE invid='{$invid}'";
    $invi_rslt = db_exec($sql) or errDie("Unable to retrieve item.");
    $sv_warn = "";
    while ($invi_data = pg_fetch_array($invi_rslt)) {
        $sql = "SELECT * FROM cubit.asset_svdates WHERE svdate<=CURRENT_DATE AND asset_id='{$invi_data['asset_id']}'";
        $sv_rslt = db_exec($sql) or errDie("Unable to retrieve service date.");
        $sv_data = pg_fetch_array($sv_rslt);
        // Retrieve asset
        if (pg_num_rows($sv_rslt)) {
            $sql = "SELECT * FROM cubit.assets WHERE id='{$sv_data['asset_id']}'";
            $asset_rslt = db_exec($sql) or errDie("Unable to retrieve asset.");
            $asset_data = pg_fetch_array($asset_rslt);
            $sv_warn .= "<li class='err'><b>SERVICING</b>: " . getSerial($asset_data["id"], 1) . "\r\n\t\t\t{$asset_data['des']} has a service date on {$sv_data['svdate']}.</li>";
        }
        if ($days = checkServicing($invi_data["asset_id"], 1)) {
            $sv_warn .= "<li class='err'><b>SERVICING</b>: {$asset_data['des']} needs servicing.</li>";
        }
    }
    // Check if we should use the default comments
    if (empty($inv_data["comm"])) {
        $sql = "SELECT value FROM cubit.settings WHERE constant='HIRE_COMMENTS'";
        $comment_rslt = db_exec($sql) or errDie("Unable to retrieve comments.");
        $inv_data["comm"] = pg_fetch_result($comment_rslt, 0);
    }
    // Site address
    $addr_sel = "";
    if ($cusnum) {
        // Retrieve branch address
        $sql = "SELECT branch_addr FROM hire.hire_invoices WHERE invid='{$invid}'";
        $addr_rslt = db_exec($sql) or errDie("Unable to retrieve branch address.");
        $branch_addr = pg_fetch_result($addr_rslt, 0);
        $sql = "SELECT id, branch_name FROM cubit.customer_branches WHERE cusnum='{$cusnum}'";
        $bran_rslt = db_exec($sql) or errDie("Unable to retrieve customer branch.");
        $addr_sel = "<select name='branch_addr' style='width: 100%' onchange='javascript:document.form.submit()'>";
        $addr_sel .= "<option value='0'>Physical Address</option>";
        while ($bran_data = pg_fetch_array($bran_rslt)) {
            if ($branch_addr == $bran_data["id"]) {
                $sel = "selected='selected'";
            } else {
                $sel = "";
            }
            $addr_sel .= "<option value='{$bran_data['id']}' {$sel}>{$bran_data['branch_name']}</option>";
        }
        $addr_sel .= "</select>";
        $addr_sel .= "<br />" . branchAddress($branch_addr, $cusnum);
    }
    $booked_items = getBookedItems($cusnum, date("Y-m-d"));
    foreach ($booked_items as $asset_id => $units_booked) {
        $sql = "SELECT des FROM cubit.assets WHERE id='{$asset_id}'";
        $bkdes_rslt = db_exec($sql) or errDie("Unable to retrieve bookings.");
        $bkdes = pg_fetch_result($bkdes_rslt, 0);
        $sv_warn .= "<li class='err'><b>BOOKING</b>: {$units_booked}x " . getSerial($asset_id, 1) . " {$bkdes} booked for this customer.</li>";
    }
    if ($monthly) {
        $ret_out = "Invoice";
    } else {
        $ret_out = "Return";
    }
    // Items should not be removed once processed, use reprint to check
    // if this hire note has already been processed or if its monthly.
    if (isset($reprint) && $reprint || !empty($monthly)) {
        $rem_th = "";
        $rem_nbsp = "";
    } else {
        $rem_th = "<th>Remove</th>";
        $rem_nbsp = "<td>&nbsp;</td>";
    }
    // Use the customer trad discount on default
    $sql = "SELECT traddisc FROM cubit.customers WHERE cusnum='{$cusnum}'";
    $cust_rslt = db_exec($sql) or errDie("Unable to retrieve discount.");
    $trade_discount = pg_fetch_result($cust_rslt, 0);
    // Determine if we got any items, if we do, we don't need to go for the
    // default value anymore, because the customer is already selected.
    $sql = "SELECT count(id) FROM hire.hire_invitems WHERE invid='{$invid}'";
    $count_rslt = db_exec($sql) or errDie("Unable to retrieve items.");
    $count = pg_fetch_result($count_rslt, 0);
    if ($count) {
        $trade_discount = $inv_data["traddisc"];
    }
    if (isset($bk_id) && $bk_id && !isset($bk_done)) {
        $sql = "\r\n\t\t\tSELECT serial FROM hire.bookings\r\n\t\t\t\tLEFT JOIN cubit.assets ON bookings.asset_id=assets.id\r\n\t\t\tWHERE bookings.id='{$bk_id}'";
        $bk_rslt = db_exec($sql) or errDie("Unable to retrieve booking.");
        $serialized = pg_fetch_result($bk_rslt, 0);
        if ($serialized == "Not Serialized") {
            $qty_disabled = "";
        } else {
            $qty_disabled = "disabled='t'";
        }
    } else {
        $qty_disabled = "disabled='t'";
    }
    // New Items
    $new_items_out = "\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>{$nbasis_sel}</td>\r\n\t\t\t<td>{$nasset_sel}</td>\r\n\t\t\t<td align='center'>\r\n\t\t\t\t<input type='text' id='nqty' name='nqty' value='' size='3' class='clear' {$qty_disabled} style='text-align:center' />\r\n\t\t\t</td>\r\n\t\t\t<td align='left' nowrap='t'>\r\n\t\t\t\t<div id='d_wks' style='height: 0px; visibility: hidden;'>\r\n\t\t\t\t\tWeeks: <input type='text' name='weeks' size='5' style='text-align: center;' />\r\n\t\t\t\t</div>\r\n\t\t\t\t<div id='d_hrs' style='height: 0px; visibility: hidden;'>\r\n\t\t\t\t\tHours: <input type='text' name='hours' size='5'\r\n\t\t\t\t\t\tstyle='text-align: center;' />\r\n\t\t\t\t</div>\r\n\t\t\t\t<div id='d_fdate' style='visibility: hidden;'>\r\n\t\t\t\t\t" . mkDateSelect("nhifrm", $nhifrm_year, $nhifrm_month, $nhifrm_day) . "\r\n\t\t\t\t</div>\r\n\t\t\t</td>\r\n\t\t\t<td align='left' nowrap='t'>\r\n\t\t\t\t<div id='d_tdate' style='visibility: hidden;'>\r\n\t\t\t\t\t" . mkDateSelect("nhito", $nhito_year, $nhito_month, $nhito_day) . "\r\n\t<!--\t\t\t\t\r\n\t\t\t\t\t<input type='checkbox' name='nhalf_day' value='checked' />\r\n\t\t\t\t\tHalf Day\r\n\t-->\t\t\t\r\n\t\t\t\t</div>\r\n\t\t\t</td>\r\n\t\t\t<td>&nbsp;</td>\r\n\t\t\t{$rem_nbsp}\r\n\t\t\t<td>&nbsp;</td>\r\n\t\t</tr>";
    /* -- Final Layout -- */
    $details = "\r\n\t\t<script>\r\n\t\t\tfunction basisChange(o) {\r\n\t\t\t\thrs = getObject('d_hrs');\r\n\t\t\t\tfd = getObject('d_fdate');\r\n\t\t\t\ttd = getObject('d_tdate');\r\n\t\t\t\twks = getObject('d_wks');\r\n\r\n\t\t\t\tswitch (o.value) {\r\n\t\t\t\t\tcase 'per_hour':\r\n\t\t\t\t\t\thrs.style.visibility = 'visible';\r\n\t\t\t\t\t\tfd.style.visibility = 'hidden';\r\n\t\t\t\t\t\ttd.style.visibility = 'hidden';\r\n\t\t\t\t\t\twks.style.visibility = 'hidden';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase 'per_day':\r\n\t\t\t\t\t\thrs.style.visibility = 'hidden';\r\n\t\t\t\t\t\tfd.style.visibility = 'visible';\r\n\t\t\t\t\t\ttd.style.visibility = 'visible';\r\n\t\t\t\t\t\twks.style.visibility = 'hidden';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase 'per_week':\r\n\t\t\t\t\t\thrs.style.visibility = 'hidden';\r\n\t\t\t\t\t\tfd.style.visibility = 'hidden';\r\n\t\t\t\t\t\ttd.style.visibility = 'hidden';\r\n\t\t\t\t\t\twks.style.visibility = 'visible';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tdefault:\r\n\t\t\t\t\t\thrs.style.visibility = 'hidden';\r\n\t\t\t\t\t\tfd.style.visibility = 'hidden';\r\n\t\t\t\t\t\ttd.style.visibility = 'hidden';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tfunction assetChange(o) {\r\n\t\t\t\tqo = getObject('nqty');\r\n\r\n\t\t\t\tswitch(o.value.substr(0, 1)) {\r\n\t\t\t\t\tcase 'q':\r\n\t\t\t\t\t\tqo.value = '';\r\n\t\t\t\t\t\tqo.disabled = false;\r\n\t\t\t\t\t\tqo.className = 'std';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase 's':\r\n\t\t\t\t\t\tqo.value = '1';\r\n\t\t\t\t\t\tqo.disabled = true;\r\n\t\t\t\t\t\tqo.className = 'clear';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tdefault:\r\n\t\t\t\t\t\tqo.value = '';\r\n\t\t\t\t\t\tqo.disabled = true;\r\n\t\t\t\t\t\tqo.className = 'clear'\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t</script>\r\n\t\t<style>\r\n\t\t\ttd, input, textarea, select { font-size: .75em; }\r\n\t\t</style>\r\n\t\t<center>\r\n\t\t<form method='POST' name='formName'>\r\n\t\t\t<input type='hidden' name='key' value='update'>\r\n\t\t</form>\r\n\t\t<form action='" . SELF . "' method='POST' name='form'>\r\n\t\t\t<input type='hidden' name='collection' value='{$collection}' />\r\n\t\t\t<input type='hidden' name='key' value='update'>\r\n\t\t\t<input type='hidden' name='invid' value='{$invid}' />\r\n\t\t\t<input type='hidden' name='temp_assets' value='{$temp_assets}' />\r\n\t\t\t<input type='hidden' name='monthly' value='{$monthly}' />\r\n\t\t\t<input type='hidden' name='cusnum' value='{$cusnum}' />\r\n\t\t\t<input type='hidden' name='chrgvat' value='no' />\r\n\t\t\t<input type='hidden' name='bk_id' value='{$bk_id}' />\r\n\t\t\t<input type='hidden' name='bk_done' value='1' />\r\n\t\t\t<input type='hidden' name='reprint' value='{$reprint}' />\r\n\t\t<table " . TMPL_tblDflts . " width='100%'>\r\n\t\t \t<tr>\r\n\t\t \t\t<td colspan='3' align='center'><h3>New Hire</h3></td>\r\n\t\t \t</tr>\r\n\t\t \t<tr>\r\n\t\t \t\t<td valign='top'>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<th colspan='2'> Customer Details </th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Department</td>\r\n\t\t\t\t\t\t\t<td valign='center'>{$dept_sel}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Customer</td>\r\n\t\t\t\t\t\t\t<td valign='center'>{$cust_sel}</td>\r\n\t\t\t\t\t\t\t{$cust_edit}\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Customer Telephone Number</td>\r\n\t\t\t\t\t\t\t<td valign='center'>\r\n\t\t\t\t\t\t\t\t<input type='text' size='20' name='telno' value='{$telno}'>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Customer Order number</td>\r\n\t\t\t\t\t\t\t<td valign='center'>\r\n\t\t\t\t\t\t\t\t<input type='text' size='10' name='cordno' value='{$cordno}'>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Available Credit</td>\r\n\t\t\t\t\t\t\t<td>{$credit_limit}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Balance</td>\r\n\t\t\t\t\t\t\t<td>{$cust_balance}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Customer Address</td>\r\n\t\t\t\t\t\t\t<td>{$addr_sel}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t<!--\r\n\t\t\t\t\t\t<tr><th colspan='2'>Point of Hire</th></tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Barcode</td>\r\n\t\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t\t<input type='text' size='13' name='bar' value=''>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td nowrap='t'>Search for description</td>\r\n\t\t\t\t\t\t\t<td><input type='text' size='13' name='des' value='{$des}'></td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td colspan='2' align='center'>\r\n\t\t\t\t\t\t\t\t<input type='submit' value='Search'>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Collection Method</td>\r\n\t\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t\t<input type='checkbox' name='client_collect' value='checked' {$client_collect} />\r\n\t\t\t\t\t\t\t\tClient Collect\r\n\t\t\t\t\t\t\t\t<br />\r\n\t\t\t\t\t\t\t\t<input type='checkbox' name='deliver' value='checked' {$deliver} />\r\n\t\t\t\t\t\t\t\tTo be Delivered\r\n\t\t\t\t\t\t\t\t<br />\r\n\t\t\t\t\t\t\t\t<input type='checkbox' name='collect' value='checked' {$collect} />\r\n\t\t\t\t\t\t\t\tTo be Collected\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t-->\r\n\t\t\t\t\t</table>\r\n\t\t\t\t<td valign='top' align='center' style='width: 100%;'>\r\n\t\t\t\t\t<img src='../compinfo/getimg.php' style='border: 1px solid #000' width='230' height='47' />\r\n\t\t\t\t</td>\r\n\t\t\t\t</td>\r\n\t\t\t\t<td valign='top' align='right'>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<th colspan=2>Hire Details</th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Hire No.</td>\r\n\t\t\t\t\t\t\t<td valign='center'>H{$inv_data['invnum']}" . rev($inv_data["invid"]) . "</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Sales Order No.</td>\r\n\t\t\t\t\t\t\t<td valign='center'>\r\n\t\t\t\t\t\t\t\t<input type='text' size='5' name='ordno' value='{$inv_data['ordno']}'>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Hire Date</td>\r\n\t\t\t\t\t\t\t<td valign='center' nowrap='t'>\r\n\t\t\t\t\t\t\t\t" . mkDateSelect("pinv", $pinv_year, $pinv_month, $pinv_day) . "\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Sales Person</td>\r\n\t\t\t\t\t\t\t<td>{$salesp_sel}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Trade Discount</td>\r\n\t\t\t\t\t\t\t<td valign='center'>\r\n\t\t\t\t\t\t\t\t<input type='text' size='5' name='traddisc'\r\n\t\t\t\t\t\t\t\tvalue='{$trade_discount}'>%\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td nowrap='t'>Delivery Charge</td>\r\n\t\t\t\t\t\t\t<td valign='center'>\r\n\t\t\t\t\t\t\t\t<input type='text' size='7' name='delchrg'\r\n\t\t\t\t\t\t\t\tvalue='{$inv_data['delchrg']}'>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<th colspan='2'>Payment Details </th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>User</td>\r\n\t\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t\t<input type='hidden' name='user' value='" . USER_NAME . "'>\r\n\t\t\t\t\t\t\t\t" . USER_NAME . "\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Deposit Type</td>\r\n\t\t\t\t\t\t\t<td>{$deposit_sel}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Deposit Amount</td>\r\n\t\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t\t<input type='text' name='deposit_amt'\r\n\t\t\t\t\t\t\t\tvalue='" . sprint($inv_data["deposit_amt"]) . "' size='7' />\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t</table>\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td colspan='3'>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'></tr>\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<th>Basis</th>\r\n\t\t\t\t\t\t\t<th>Item</th>\r\n\t\t\t\t\t\t\t<th>Qty</th>\r\n\t\t\t\t\t\t\t<th>Hire Date</th>\r\n\t\t\t\t\t\t\t<th>Expected Return</th>\r\n\t\t\t<!--\t\t\t\r\n\t\t\t\t\t\t\t<th>Rain Days</th>\r\n\t\t\t\t\t\t\t<th>Collection</th>\r\n\t\t\t-->\r\n\t\t\t\t\t\t\t<th>Amount</th>\r\n\t\t\t\t\t\t\t{$rem_th}\r\n\t\t\t\t\t\t\t<th>{$ret_out}</th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t{$items_out}\r\n\t\t\t\t\t\t{$new_items_out}\r\n\t\t\t\t\t</table>\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td width='70%' valign='top' colspan='2'>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<td colspan='10'>{$sv_warn}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<td rowspan='4' nowrap>" . mkQuickLinks(ql("javascript:popupOpen(\"../customers-new.php\")", "New Customer"), ql("../pos-invoice-new.php", "New POS Invoice"), ql("../nons-invoice-new.php", "New Non-Stock Invoice")) . "\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t\t<th>Comments</th>\r\n\t\t\t\t\t\t\t<th>Custom Text</th>\r\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width=40%>{$error}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td rowspan=4 align=center valign=top>\r\n\t\t\t\t\t\t\t\t<textarea name=comm cols=20 style='height: 100%'>{$inv_data['comm']}</textarea>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t\t<td rowspan='4' align='center' valign='top'>\r\n\t\t\t\t\t\t\t\t<textarea name='custom_txt' rows='4' cols='60' style='height: 100%'>{$custom_txt}</textarea>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t</table>\r\n\t\t\t\t</td>\r\n\t\t\t\t<td colspan='2' align='right' valign='top' width='30%'>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Delivery Charge</td>\r\n\t\t\t\t\t\t\t<td align=right>" . CUR . " {$inv_data['delivery']}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Trade Discount</td>\r\n\t\t\t\t\t\t\t<td align=right>" . CUR . " {$inv_data['discount']}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\r\n\t\t\t\t\t\t\t<td align=right>\r\n\t\t\t\t\t\t\t\t" . CUR . "<input type=hidden name='subtot' value='{$inv_data['subtot']}'>\r\n\t\t\t\t\t\t\t\t" . sprint($inv_data["subtot"]) . "\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td><b>VAT {$vat14}</b></td>\r\n\t\t\t\t\t\t\t<td align=right>" . CUR . " {$inv_data['vat']}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\r\n\t\t\t\t\t\t\t<td align=right>" . CUR . " {$inv_data['total']}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t{$rounding}\r\n\t\t\t\t\t</table>\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t{$hire_buttons}\r\n\t\t</table>\r\n\t\t<a name='bottom'>\r\n\t\t</form>\r\n\t\t</center>";
    return $details;
}
function details($_GET)
{
    $showvat = TRUE;
    # get vars
    extract($_GET);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($invid, "num", 1, 20, "Invalid invoice number.");
    # display errors, if any
    if ($v->isError()) {
        $err = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $err .= "<li class='err'>{$e['msg']}</li>";
        }
        $confirm .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    updateTotals($invid);
    # Get invoice info
    $sql = "SELECT *, extract('epoch' from timestamp) AS e_time\r\n\t\t\t\tFROM hire.reprint_invoices\r\n\t\t\t\tWHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<i class='err'>Not Found[1]</i>";
    }
    $inv = pg_fetch_array($invRslt);
    # get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$inv['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<i class='err'>Not Found[2]</i>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    /* --- Start some checks --- */
    # check if stock was selected(yes = put done button)
    $sql = "SELECT asset_id FROM hire.reprint_invitems WHERE invid = '{$inv['invid']}'";
    $crslt = db_exec($sql);
    if (pg_numrows($crslt) < 1) {
        $error = "<li class='err'> Error : Invoice number <b>{$invid}</b> has no items.</li>";
        return $error;
    }
    /* --- End some checks --- */
    /* --- Start Products Display --- */
    # Products layout
    $products = "";
    $disc = 0;
    # get selected stock in this invoice
    $sql = "SELECT * FROM hire.reprint_invitems  WHERE invid = '{$invid}'";
    $stkdRslt = db_exec($sql);
    $tcosamt = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # get selected stock in this warehouse
        db_connect();
        $sql = "SELECT * FROM assets WHERE id = '{$stkd['asset_id']}'";
        $stkRslt = db_exec($sql);
        $stk = pg_fetch_array($stkRslt);
        $sp = "&nbsp;&nbsp;&nbsp;&nbsp;";
        # keep track of discounts
        $disc += $stkd['disc'];
        if ($stkd['account'] != 0) {
            $stk['stkcod'] = $stkd['description'];
            $stk['stkdes'] = "";
        }
        $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'";
        $Ri = db_exec($Sl);
        $vd = pg_fetch_array($Ri);
        if (TAX_VAT != $vd['vat_amount'] and $vd['vat_amount'] != "0.00") {
            $showvat = FALSE;
        }
        # put in product
        $stkd['unitcost'] = $stkd['unitcost'] - $stkd['disc'];
        $return = returnDate($stkd["item_id"]);
        $hired = hiredDate($stkd["item_id"]);
        $products .= "\r\n\t\t\t\t<tr valign=top>\r\n\t\t\t\t\t<td>" . getSerial($stk["id"], 1) . " {$stk['des']} ({$stkd['collection']})</td>\r\n\t\t\t\t\t<td>{$stkd['qty']}</td>\r\n\t\t\t\t\t<td>{$hired}</td>\r\n\t\t\t\t\t<td>{$return}</td>\r\n\t\t\t\t\t<td align=right>" . sprint($stkd["amt"]) . "</td>\r\n\t\t\t\t</tr>";
        $client_collect = 0;
        $collect = 0;
        $deliver = 0;
        if (preg_match("/(Client Collect|collect)/", $stkd["collection"])) {
            $client_collect = 1;
        }
        if (preg_match("/(^Collect|, Collect)/", $stkd["collection"])) {
            $collect = 1;
        }
        if (preg_match("/Deliver/", $stkd["collection"])) {
            $deliver = 1;
        }
    }
    /* --- Start Some calculations --- */
    # subtotal
    $SUBTOT = sprint($inv['subtot']);
    # Calculate tradediscm
    $traddiscm = $inv["discount"];
    // 	if(strlen($inv['traddisc']) > 0){
    // 		$traddiscm = sprint((($inv['traddisc']/100) * $SUBTOT));
    // 	}else{
    // 		$traddiscm = "0.00";
    // 	}
    # Calculate subtotal
    $VATP = TAX_VAT;
    $SUBTOT = sprint($inv['subtot']);
    $VAT = sprint($inv['vat']);
    $TOTAL = sprint($inv['total']);
    $inv['delchrg'] = sprint($inv['delchrg']);
    # Update number of prints
    // 	$inv['prints']++;
    // 	db_conn($prd);
    // 	$Sql = "UPDATE hire.reprint_invitems SET prints = '$inv[prints]' WHERE invid = '$invid' AND div = '".USER_DIV."'";
    // 	$upRslt = db_exec($Sql) or errDie ("Unable to update invoice information");
    # todays date
    $date = date("d-m-Y");
    $sdate = date("Y-m-d");
    if (strlen($inv['comm']) > 0) {
        $Com = "\r\n\t\t\t\t<table>\r\n\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t<td>" . nl2br($inv['comm']) . "</td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t</table>";
    } else {
        $Com = "";
    }
    $time = date("G:i:s", $inv["e_time"]);
    if (isset($cccc)) {
        $cc = "<script> sCostCenter('dt', 'Sales', '{$date}', 'Invoice No.{$inv['invnum']} for Customer {$inv['cusname']} {$inv['surname']}', '" . ($TOTAL - $VAT) . "', 'Cost Of Sales for Invoice No.{$inv['invnum']}', '{$tcosamt}', ''); </script>";
    } else {
        $cc = "";
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM settings WHERE constant='PSALES'";
    $Ri = db_exec($Sl) or errDie("Unable to get settings.");
    $data = pg_fetch_array($Ri);
    if ($data['value'] == "Yes") {
        $sp = "<tr><td>Sales Person</td><td>{$inv['salespn']}</td></tr>";
    } else {
        $sp = "";
    }
    $Sl = "SELECT * FROM pc WHERE inv='{$inv['invnum']}'";
    $Ri = db_exec($Sl) or errDie("Unable to get data.");
    if (pg_num_rows($Ri) > 0) {
        $pd = pg_fetch_array($Ri);
        //$pc="<tr><td>Change</td><td align=right><b>".CUR." $pd[amount]</b></td></tr>";
        $pc = "";
        $change = $pd['amount'];
    } else {
        $pc = "";
        $change = 0;
    }
    $Sl = "SELECT * FROM payrec WHERE inv='{$inv['invnum']}' AND method='Cash'";
    $Ri = db_exec($Sl) or errDie("Unable to get data.");
    if (pg_num_rows($Ri) > 0) {
        $pd = pg_fetch_array($Ri);
        $pd['amount'] = sprint($pd['amount'] + $change);
        $pcash = "<!--<tr><td>Paid Cash</td><td align=right><b>" . CUR . " {$pd['amount']}</b></td></tr>-->";
    } else {
        $pcash = "";
    }
    $Sl = "SELECT * FROM payrec WHERE inv='{$inv['invnum']}' AND method='Cheque'";
    $Ri = db_exec($Sl) or errDie("Unable to get data.");
    if (pg_num_rows($Ri) > 0) {
        $pd = pg_fetch_array($Ri);
        $pcheque = "<tr><td>Paid Cheque</td><td align=right><b>" . CUR . " {$pd['amount']}</b></td></tr>";
    } else {
        $pcheque = "";
    }
    $Sl = "SELECT * FROM payrec WHERE inv='{$inv['invnum']}' AND method='Credit Card'";
    $Ri = db_exec($Sl) or errDie("Unable to get data.");
    if (pg_num_rows($Ri) > 0) {
        $pd = pg_fetch_array($Ri);
        $pcc = "<tr><td>Paid Credit Card</td><td align=right><b>" . CUR . " {$pd['amount']}</b></td></tr>";
    } else {
        $pcc = "";
    }
    $Sl = "SELECT * FROM payrec WHERE inv='{$inv['invnum']}' AND method='Credit'";
    $Ri = db_exec($Sl) or errDie("Unable to get data.");
    if (pg_num_rows($Ri) > 0) {
        $pd = pg_fetch_array($Ri);
        $pcc .= "<tr><td>On Credit</td><td align=right><b>" . CUR . " {$pd['amount']}</b></td></tr>";
    } else {
        $pcc .= "";
    }
    // 	$Sl="SELECT * FROM varrec WHERE inv='$inv[invnum]'";
    // 	$Ri=db_exec($Sl);
    //
    // 	if(pg_num_rows($Ri)>0) {
    // 		$rd=pg_fetch_array($Ri);
    //
    // 		$rounding="<tr><td>Rounding</td><td align=right>".CUR." $rd[amount]</td></tr>";
    // 	} else {
    // 		$rounding="";
    // 	}
    //
    if ($inv['rounding'] > 0) {
        $due = sprint($inv['total'] - $inv['rounding']);
        $rounding = "\r\n\t\t\t\t<tr>\r\n\t\t\t\t\t<td>Rounding</td>\r\n\t\t\t\t\t<td align='right'>" . CUR . " {$inv['rounding']}</td>\r\n\t\t\t\t</tr>\r\n\t\t\t\t<tr>\r\n\t\t\t\t\t<td>Amount Due</td>\r\n\t\t\t\t\t<td align='right'>" . CUR . " {$due}</td>\r\n\t\t\t\t</tr>";
    } else {
        $rounding = "";
    }
    $cusinfo = "";
    if ($inv['cusnum'] > 0) {
        db_conn('cubit');
        $Sl = "SELECT * FROM customers WHERE cusnum='{$inv['cusnum']}'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $cd = pg_fetch_array($Ri);
        $inv['cusname'] = $cd['surname'] . " (VAT No. {$cd['vatnum']})";
        $cusinfo .= "<p>" . hireAddress($inv["invid"]) . "</p>";
        $cusinfo .= "Tel: {$cd['bustel']}<br />";
        $cusinfo .= "Order No: {$inv['cordno']}";
    } else {
        if (strlen($inv['vatnum']) > 1) {
            $inv['cusname'] = "{$inv['cusname']} (VAT No. {$inv['vatnum']})<br />";
            $cusinfo .= "Order No: {$inv['cordno']}";
        }
    }
    db_conn('cubit');
    $Sl = "SELECT img2 FROM compinfo";
    $Ri = db_exec($Sl);
    $id = pg_fetch_array($Ri);
    if (strlen($id['img2']) > 0) {
        $logo = "<tr>\r\n\t\t\t<td valign='top' width='100%' align=center><img src='compinfo/getimg2.php' width='230' height='47'></td>\r\n\t\t</tr>";
    } else {
        $logo = "";
    }
    $sql = "SELECT value FROM cubit.settings WHERE constant='CONTRACT_TEXT'";
    $contract_rslt = db_exec($sql) or errDie("Unable to retrieve contract.");
    $contract_text = nl2br(base64_decode(pg_fetch_result($contract_rslt, 0)));
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    $sql = "SELECT text FROM hire.thanks_text";
    $thanks_rslt = db_exec($sql) or errDie("Unable to retrieve thank you text.");
    $thank_you = pg_fetch_result($thanks_rslt, 0);
    $details = "<center>\r\n\t<style>\r\n\t\th2 {\r\n\t\t\tfont-size: 2em;\r\n\t\t\tpadding: 0;\r\n\t\t\tmargin: 0;\r\n\t\t}\r\n\t\tth {\r\n\t\t\tfont-weight: bold;\r\n\t\t\ttext-align: left;\r\n\t\t}\r\n\t\t.print_input {\r\n\t\t\tfont-family: monospace;\r\n\t\t\tfont-weight: bold;\r\n\t\t}\r\n\t</style>\r\n\t<table cellpadding='0' cellspacing='1' border=1 width='97%'>\r\n\t<tr><td>\r\n\t\t<table cellpadding='5' cellspacing='0' width='100%'>\r\n\t\t\t<tr>\r\n\t\t\t\t<td><img src='../compinfo/getimg.php' width='230' height='47' /></td>\r\n\t\t\t\t<td align='right'><h2>HIRE NOTE</h2></td>\r\n\t\t\t</tr>\r\n\t\t</table>\r\n\t</tr></td>\r\n\t<tr><td>\r\n\t\t<table border='1' width='100%'>\r\n\t\t<tr>\r\n\t\t\t<td valign=top width='50%'>\r\n\t\t\t\t" . COMP_NAME . "<br />\r\n\t\t\t\t" . COMP_ADDRESS . "<br />\r\n\t\t\t\tTEL: " . COMP_TEL . "<br />\r\n\t\t\t\tFAX: " . COMP_FAX . "<br />\r\n\t\t\t\tRegistration Number: " . COMP_REGNO . "<br />\r\n\t\t\t\tVAT Registration Number: " . COMP_VATNO . "<br />\r\n\t\t\t</td>\r\n\t\t\t<td valign='top' width='50%'>\r\n\t\t\t\t{$inv['cusname']}<br />\r\n\t\t\t\t{$cusinfo}\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t\t</table>\r\n\t</td></tr>\r\n\t<tr><td>\r\n\t\t<table " . TMPL_tblDflts . " width='100%'>\r\n\t\t\t<tr>\r\n\t\t\t\t<td align='left' width='25%'>\r\n\t\t\t\t\tNote: H{$inv['invnum']}" . rrev($inv["invid"]) . "\r\n\t\t\t\t</td>\r\n\t\t\t\t<td align='center' width='25%'>CASHIER: {$inv['username']}</td>\r\n\t\t\t\t<td width='25%' align='center'>Time: {$time}</td>\r\n\t\t\t\t<td width='25%' align='right'>{$inv['odate']}</td>\r\n\t\t\t</tr>\r\n\t\t</table>\r\n\t</td></tr>\r\n\t<tr><td>\r\n\t<table cellpadding='5' cellspacing='0' border='0' width='100%' bordercolor='#000000'>\r\n\t\t<tr>\r\n\t\t\t<th>CODE</th>\r\n\t\t\t<th>QTY</th>\r\n\t\t\t<th>HIRE DATE</th>\r\n\t\t\t<th>RETURN</th>\r\n\t\t\t<th style='text-align: right'>TOTAL</th>\r\n\t\t<tr>\r\n\t\t{$products}\r\n\t</table>\r\n\t</td></tr>\r\n\t<tr>\r\n\t\t<td valign='top'>\r\n\t\t<table width='100%'>\r\n\t\t<tr><td valign='top'>\r\n\t\t<table cellpadding='2' cellspacing='0' width='80%'>\r\n\t\t\t<tr><td colspan='2'>{$thank_you}</td></tr>\r\n\t\t\t<tr><td>{$Com}</td></tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td>\r\n\t\t\t\t<font style='font-size: 0.75em'>" . nl2br($inv["custom_txt"]) . "<br />\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr><td>&nbsp;</td></tr>\r\n\t\t</table>\r\n\t\t</td>\r\n\t\t<td align=right valign='top'>\r\n\t\t<table cellpadding='2' cellspacing='0' width='50%'>\r\n\t\t\t<tr>\r\n\t\t\t\t<td>Delivery Charge</td>\r\n\t\t\t\t<td align='right' nowrap>" . CUR . " {$inv['delchrg']}</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td>Trade Discount</td>\r\n\t\t\t\t<td align='right' nowrap>" . CUR . " {$traddiscm}</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td>SUBTOTAL</td>\r\n\t\t\t\t<td align='right' nowrap>" . CUR . " {$SUBTOT}</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td>VAT {$vat14}</td>\r\n\t\t\t\t<td align='right' nowrap>" . CUR . " {$VAT}</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td nowrap>GRAND TOTAL</td>\r\n\t\t\t\t<td align='right' nowrap><b>" . CUR . " {$TOTAL}</b></td>\r\n\t\t\t</tr>\r\n\t\t\t{$sp}\r\n\t\t</table>\r\n\t</td></tr>\r\n\t\t<table cellpadding='2' cellspacing='0' width='100%' border='1'>\r\n\t\t\t<tr><td colspan='2'>\r\n\t\t\t<div style='font-size: .80em;'>" . stripslashes($contract_text) . "</div></div>\r\n\t\t\t</td></tr>\r\n\t\t\t<tr><td colspan='2'>&nbsp;</td></tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td>Full Names (Print) ____________________________</td>\r\n\t\t\t\t<td>Identity Number ____________________________</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr><td colspan='2'>&nbsp;</td></tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td>Signature - Customer ____________________________</td>\r\n\t\t\t\t<td>Signature - Authorized Agent ____________________________</td>\r\n\t\t\t</tr>\r\n\t\t</table>\r\n\t</td></tr>\r\n\t</td></tr>\r\n\t</table>\r\n\t</table>\r\n\t</center>";
    $OUTPUT = $details;
    require "../tmpl-print.php";
}