function display()
{
    extract($_REQUEST);
    $fields = array();
    $fields["from_year"] = date("Y");
    $fields["from_month"] = date("m");
    $fields["from_day"] = date("d");
    $fields["to_year"] = date("Y");
    $fields["to_month"] = date("m");
    $fields["to_day"] = date("d");
    extract($fields, EXTR_SKIP);
    $OUTPUT = "<center>\r\n\t<h3>Equipment to be Returned</h3>\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th colspan='4'>Date Range</th>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>" . mkDateSelect("from", $from_year, $from_month, $from_day) . "</td>\r\n\t\t\t<td><b> To </b></td>\r\n\t\t\t<td>" . mkDateSelect("to", $to_year, $to_month, $to_day) . "</td>\r\n\t\t\t<td>\r\n\t\t\t\t<input type='submit' value='Continue' style='text-weight: bold' />\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t</table>";
    // Retrieve equipment to be returned
    $sql = "SELECT *,\r\n\t\t\t\t(SELECT des FROM cubit.assets WHERE id=asset_id) AS des,\r\n\t\t\t\t(SELECT serial FROM cubit.assets WHERE id=asset_id) AS serial\r\n\t\t\tFROM hire.hire_invitems\r\n\t\t\tORDER BY des ASC";
    $items_rslt = db_exec($sql) or errDie("Unable to retrieve items.");
    $items_out = "";
    while ($items_data = pg_fetch_array($items_rslt)) {
        $sql = "SELECT * FROM hire.hire_invoices WHERE invid='{$items_data['invid']}'";
        $inv_rslt = db_exec($sql) or errDie("Unable to retrieve invoice.");
        $inv_data = pg_fetch_array($inv_rslt);
        $sql = "SELECT * FROM cubit.customers WHERE cusnum='{$inv_data['cusnum']}'";
        $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customer.");
        $cust_data = pg_fetch_array($cust_rslt);
        if (!hiredDate($items_data["id"])) {
            continue;
        }
        $items_out .= "<tr class='" . bg_class() . "'>\r\n\t\t\t<td>" . hiredDate($items_data["id"]) . "</td>\r\n\t\t\t<td>" . returnDate($items_data["id"]) . "</td>\r\n\t\t\t<td>{$items_data['des']}</td>\r\n\t\t\t<td>" . getSerial($items_data["asset_id"]) . "</td>\r\n\t\t\t<td>{$items_data['qty']}</td>\r\n\t\t\t<td>{$cust_data['surname']} {$cust_data['cusname']}</td>\r\n\t\t\t<td>\r\n\t\t\t\t<a href='hire-invoice-new.php?invid={$items_data['invid']}'>\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</tr>";
    }
    $OUTPUT .= "<p></p>\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th>Hired</th>\r\n\t\t\t<th>To Be Returned</th>\r\n\t\t\t<th>Plant</th>\r\n\t\t\t<th>Serial</th>\r\n\t\t\t<th>Qty</th>\r\n\t\t\t<th>Customer</th>\r\n\t\t\t<th>Options</th>\r\n\t\t</tr>\r\n\t\t{$items_out}\r\n\t</table>\r\n\t</center>";
    return $OUTPUT;
}
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";
}
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;
}
function returnDate($item_id, $date_fmt = "d-m-Y")
{
    $sql = "SELECT * FROM hire.hire_invitems WHERE id='{$item_id}'";
    $item_rslt = db_exec($sql) or errDie("Unable to retrieve item.");
    $item_data = pg_fetch_array($item_rslt);
    if (!pg_num_rows($item_rslt)) {
        $sql = "SELECT * FROM hire.reprint_invitems WHERE item_id='{$item_id}'";
        $item_rslt = db_exec($sql) or errDie("Unable to retrieve item.");
        $item_data = pg_fetch_array($item_rslt);
    }
    if (!isset($item_data['asset_id'])) {
        $item_data['asset_id'] = "0";
    }
    $sql = "SELECT *,\r\n\t\t\t\textract('epoch' FROM hired_time) AS e_hired\r\n\t\t\tFROM hire.assets_hired WHERE asset_id='{$item_data['asset_id']}'";
    $ah_rslt = db_exec($sql) or errDie("Unable to retrieve hire date.");
    $ah_data = pg_fetch_array($ah_rslt);
    if (!empty($item_data["to_date"])) {
        $to_date = getDTEpoch("{$item_data['to_date']} 23:59:59");
    }
    if (!empty($item_data["hours"])) {
        $to_date = hiredDate($item_id, "U") + HOURS * $item_data["hours"];
    }
    if (!empty($item_data["weeks"])) {
        $to_date = hiredDate($item_id, "U") + WEEKS * $item_data["weeks"];
    }
    if (!isset($to_date)) {
        $to_date = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
    }
    return date($date_fmt, $to_date);
}