예제 #1
0
function view()
{
    extract($_REQUEST);
    $fields = array("stkcod" => "");
    extract($fields, EXTR_SKIP);
    # Select the stock category
    db_connect();
    $cats = "<select name='catid'>";
    $sql = "SELECT catid,cat,catcod FROM stockcat WHERE div = '" . USER_DIV . "' ORDER BY cat ASC";
    $catRslt = db_exec($sql);
    if (pg_numrows($catRslt) < 1) {
        return "<li>There are no stock categories in Cubit.</li>";
    } else {
        while ($cat = pg_fetch_array($catRslt)) {
            $cats .= "<option value='{$cat['catid']}'>({$cat['catcod']}) {$cat['cat']}</option>";
        }
    }
    $cats .= "</select>";
    $vat = getSetting("SELAMT_VAT") == 'inc' ? "Including VAT" : "Excluding VAT";
    # Select classification
    $class = "<select name='clasid' style='width: 167'>";
    $sql = "SELECT * FROM stockclass WHERE div = '" . USER_DIV . "' ORDER BY classname ASC";
    $clasRslt = db_exec($sql);
    if (pg_numrows($clasRslt) < 1) {
        return "<li>There are no Classifications in Cubit.</li>";
    } else {
        while ($clas = pg_fetch_array($clasRslt)) {
            $class .= "<option value='{$clas['clasid']}'>{$clas['classname']}</option>";
        }
    }
    $class .= "</select>";
    db_connect();
    $get_defwh = "SELECT * FROM set WHERE label = 'DEF_WH' LIMIT 1";
    $run_defwh = db_exec($get_defwh) or errDie("Unable to get default store information");
    if (pg_numrows($run_defwh) < 1) {
        $defwhid = "";
    } else {
        $darr = pg_fetch_array($run_defwh);
        $defwhid = $darr['value'];
    }
    # Select the stock warehouse
    db_conn("exten");
    $whs = "<select name='whid'>";
    $sql = "SELECT whid,whname,whno FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li>There are no stock stores in Cubit.</li>";
    } else {
        while ($wh = pg_fetch_array($whRslt)) {
            if ($wh['whid'] == $defwhid) {
                $whs .= "<option value='{$wh['whid']}' selected>({$wh['whno']}) {$wh['whname']}</option>";
            } else {
                $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
            }
        }
    }
    $whs .= "</select>";
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get data.");
    $vatcodes = "<select name='vatcode'>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['del'] == "Yes") {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']} {$vd['description']}</option>";
    }
    $vatcodes .= "</select>";
    $Sl = "SELECT supid,supname FROM suppliers ORDER BY supname";
    $Ri = db_exec($Sl);
    $supplier1 = 0;
    $supplier2 = 0;
    $supplier3 = 0;
    $suppliers1 = "\n\t\t<select name='supplier1'>\n\t\t\t<option value='0'>Select Supplier 1</option>";
    $suppliers2 = "\n\t\t<select name='supplier2'>\n\t\t\t<option value='0'>Select Supplier 2</option>";
    $suppliers3 = "\n\t\t<select name='supplier3'>\n\t\t\t<option value='0'>Select Supplier 3</option>";
    while ($sd = pg_fetch_array($Ri)) {
        if ($sd['supid'] == $supplier1) {
            $sel1 = "selected";
        } else {
            $sel1 = "";
        }
        $suppliers1 .= "<option value='{$sd['supid']}' {$sel1}>{$sd['supname']}</option>";
        if ($sd['supid'] == $supplier2) {
            $sel2 = "selected";
        } else {
            $sel2 = "";
        }
        $suppliers2 .= "<option value='{$sd['supid']}' {$sel2}>{$sd['supname']}</option>";
        if ($sd['supid'] == $supplier3) {
            $sel3 = "selected";
        } else {
            $sel3 = "";
        }
        $suppliers3 .= "<option value='{$sd['supid']}' {$sel3}>{$sd['supname']}</option>";
    }
    $suppliers1 .= "</select>";
    $suppliers2 .= "</select>";
    $suppliers3 .= "</select>";
    $warranty_ar = array("year" => "Year/s", "month" => "Month/s", "day" => "Day/s");
    $warranty_sel = "<select name='warranty_range'>";
    foreach ($warranty_ar as $key => $title) {
        $warranty_sel .= "<option value='{$key}'>{$title}</option>";
    }
    $warranty_sel .= "</select>";
    /* adding from supplier stock */
    if (isset($supid) && isset($supstkcod)) {
        $supadd = "\n\t\t\t<input type='hidden' name='supid' value='{$supid}' />\n\t\t\t<input type='hidden' name='supstkcod' value='{$supstkcod}' />";
    } else {
        $supadd = "";
    }
    // Layout
    $view = "\n\t\t<h3>Add Stock</h3>\n\t\t<form action='" . SELF . "' method=post name='form'>\n\t\t<input type='hidden' name='key' value='confirm'>\n\t\t{$supadd}\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>Field</th>\n\t\t\t\t\t\t\t<th>Value</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>" . REQ . "Select Store</td>\n\t\t\t\t\t\t\t<td>{$whs}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>" . REQ . "Stock code</td>\n\t\t\t\t\t\t\t<td><input type='text' size='20' name='stkcod' value='{$stkcod}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Stock description</td>\n\t\t\t\t\t\t\t<td><textarea cols='18' rows='5' name='stkdes'></textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Type</td>\n\t\t\t\t\t\t\t<td valign='center'>\n\t\t\t\t\t\t\t\t<input type='radio' name='stktp' value='stk' checked=yes>Stock<b> | </b>\n\t\t\t\t\t\t\t\t<input type='radio' name='stktp' value='lab'> Services/Labour\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Serialized</td>\n\t\t\t\t\t\t\t<td valign='center'>\n\t\t\t\t\t\t\t\t<input type='radio' name='serd' value='yes' >Yes<b> | </b>\n\t\t\t\t\t\t\t\t<input type='radio' name='serd' value='no' checked='yes'> No</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>RFID type of tag</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' name='rfidtype' value='Gen2'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>RFID (869.4 to 928 Mhz UHF)</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' name='rfidfreq' value='869.4'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>RFID Tag read rate</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' name='rfidrate' value='2'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>" . REQ . "Select Category [<a href='javascript: popupSized(\"stockcat-add.php?" . frmupdate_make("list", "form", "catid") . "\", \"stock\", 380, 400);'>Add New</a>]</td>\n\t\t\t\t\t\t\t<td>{$cats}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>" . REQ . "Classification [<a href='javascript: popupSized(\"stockclass-add.php?" . frmupdate_make("list", "form", "clasid") . "\", \"stock\", 380, 400);'>Add New</a>]</td>\n\t\t\t\t\t\t\t<td>{$class}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Warranty</td>\n\t\t\t\t\t\t\t<td><input type='text' name='warranty' size=2>{$warranty_sel}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Upload Stock Image</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<input type='radio' name='change_image' value='yes'>Yes<b> | </b>\n\t\t\t\t\t\t\t\t<input type='radio' name='change_image' value='no' checked>No\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>Field</th>\n\t\t\t\t\t\t\t<th>Value</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Buying Unit of measure</td>\n\t\t\t\t\t\t\t<td><input type='text' size='7' name='buom'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Selling Unit of measure</td>\n\t\t\t\t\t\t\t<td><input type='text' size='7' name='suom'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Selling Units per Buying unit</td>\n\t\t\t\t\t\t\t<td><input type='text' size='5' name='rate'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Location</td>\n\t\t\t\t\t\t\t<td>Shelf <input type='text' size='5' name='shelf'> Row <input type='text' size='5' name='row'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Level</td>\n\t\t\t\t\t\t\t<td>Minimum <input type='text' size='5' name='minlvl'> Maximum <input type='text' size='5' name='maxlvl'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Selling price per unit</td>\n\t\t\t\t\t\t\t<td>" . CUR . " <input type='text' size='7' name='selamt'> {$vat}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Bar Code</td>\n\t\t\t\t\t\t\t<td><input type='text' size='20' name='bar'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Code</td>\n\t\t\t\t\t\t\t<td>{$vatcodes}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Markup Percentage</td>\n\t\t\t\t\t\t\t<td><input type='text' size='10' name='markup'> %</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier1</td>\n\t\t\t\t\t\t\t<td>{$suppliers1}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier2</td>\n\t\t\t\t\t\t\t<td>{$suppliers2}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier3</td>\n\t\t\t\t\t\t\t<td>{$suppliers3}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td></td>\n\t\t\t\t<td valign='center'><input type='submit' value='Confirm &raquo'></td>\n\t\t\t</tr>\n\t\t</table>\n\t\t</form>\n\t\t<p>\n\t\t<table " . TMPL_tblDflts . " width='100'>\n\t\t\t<tr>\n\t\t\t\t<th>Quick Links</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><a href='stock-view.php'>View Stock</a></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t</tr>\n\t\t</table>";
    return $view;
}
function view()
{
    extract($_REQUEST);
    $fields = array();
    $fields["id"] = 0;
    extract($fields, EXTR_SKIP);
    // Retrieve team
    $sql = "SELECT * FROM crm.team_owners WHERE user_id='" . USER_ID . "'";
    $to_rslt = db_exec($sql) or errDie("Unable to retrieve team owners.");
    $perm_ar = array();
    while ($to_data = pg_fetch_array($to_rslt)) {
        $perm_ar[] = "team_id='{$to_data['team_id']}'";
    }
    $perm_sql = "";
    if (count($perm_ar)) {
        $perm_sql = " OR " . implode(" OR ", $perm_ar);
    } else {
        $perm_sql = "";
    }
    // Retrieve main todo's
    $sql = "SELECT * FROM cubit.todo_main WHERE (user_id='" . USER_ID . "' {$perm_sql})\n\tORDER BY title ASC";
    $tm_rslt = db_exec($sql) or errDie("Unable to retrieve main todos.");
    // Keep track of the total amount of todo items
    $total_todo = 0;
    // Create main todo dropdown
    $tm_sel = "\n\t\t<select name='id' onchange='javascript:document.form.submit()' style='width: 100%'>\n\t\t\t<option value='0'>[None]</option>";
    while ($tm_data = pg_fetch_array($tm_rslt)) {
        $sql = "SELECT count(id) FROM todo_sub WHERE main_id='{$tm_data['id']}'\n\t\tAND done='0'";
        $count_rslt = db_exec($sql) or errDie("Unable to retrieve todo count.");
        $count = pg_fetch_result($count_rslt, 0);
        // Add to the grand total
        $total_todo += $count;
        if ($id == $tm_data["id"]) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $tm_sel .= "\n\t\t\t<option value='{$tm_data['id']}' {$sel}>\n\t\t\t\t{$tm_data['title']} ({$count})\n\t\t\t</option>";
    }
    $tm_sel .= "</select>";
    // Create the list of Todo items.
    if ($id) {
        // Retrieve tasks already added
        $sql = "SELECT *,extract('epoch' FROM datetime) as e_time FROM cubit.todo_sub\n \t\tWHERE done='0' AND main_id='{$id}' ORDER BY datetime DESC";
        $ts_rslt = db_exec($sql) or errDie("Unable to retrieve sub todos.");
        // Inputs for new todo items
        $ts_out = "\n\t\t\t<tr class='odd'>\n\t\t\t\t<td>" . mkDateSelect("ndate") . "</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='text' name='nhour' size='2'  value='" . date("G") . "'\n\t\t\t\t\tstyle='text-align: center'/> :\n\t\t\t\t\t<input type='text' name='nminute' size='2' value='" . date("i") . "'\n\t\t\t\t\tstyle='text-align: center' />\n\t\t\t\t</td>\n\t\t\t\t<td><input type='text' name='ndesc' /></td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t</tr>";
        $i = 0;
        while ($ts_data = pg_fetch_array($ts_rslt)) {
            $i++;
            $class = $i % 2 ? "odd" : "even";
            $date = date("d-m-Y", $ts_data["e_time"]);
            $time = date("G:i", $ts_data["e_time"]);
            $ts_out .= "\n\t\t\t\t<tr class='{$class}'>\n\t\t\t\t\t<td>{$date}</td>\n\t\t\t\t\t<td>{$time}</td>\n\t\t\t\t\t<td>{$ts_data['description']}</td>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<input type='checkbox' name='done' value='{$ts_data['id']}'\n\t\t\t\t\t\tonchange='javascript:document.form2.submit()'/>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>";
        }
        $num_todo = pg_num_rows($ts_rslt);
    } else {
        $ts_out = "\n\t\t\t<tr class='odd'>\n\t\t\t\t<td colspan='5'><li>Please Select a Main Todo Item</li></td>\n\t\t\t</tr>";
        $num_todo = 0;
    }
    $long_date = date("D") . " " . date("d") . date("S") . " " . date("M") . " " . date("Y");
    $OUTPUT = "\n\t\t<h3>Todo List</h3>\n\t\t<form method='post' action='" . SELF . "' name='form'>\n\t\t\t<input type='hidden' name='key' value='update' />\n\t\t<table cellpadding='2' cellspacing='0' class='shtable'>\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'>Main Todo ({$total_todo})</th>\n\t\t\t</tr>\n\t\t\t<tr class='even'>\n\t\t\t\t<td>{$tm_sel}</td>\n\t\t\t\t<td>\n\t\t\t\t\t<a href='javascript:popupOpen(\"todo_main_save.php?" . frmupdate_make("list", "form", "id") . "\")'>\n\t\t\t\t\t\tAdd Main Todo\n\t\t\t\t\t</a>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'>Todo ({$num_todo}) {$long_date}</th>\n\t\t\t</tr>\n\t\t</table>\n\t\t</form>\n\n\t\t<p></p>\n\n\t\t<form method='post' action='" . SELF . "' name='form2'>\n\t\t\t<input type='hidden' name='id' value='{$id}' />\n\t\t\t<input type='hidden' name='key' value='update' />\n\t\t<table cellpadding='2' cellspacing='0' class='shtable'>\n\t\t\t<tr>\n\t\t\t\t<th>Date</th>\n\t\t\t\t<th>Time</th>\n\t\t\t\t<th>Description</th>\n\t\t\t\t<th>Done</th>\n\t\t\t</tr>\n\t\t\t{$ts_out}\n\t\t</table>\n\n\t\t<p></p>\n\n\t\t<input type='submit' value='Update &raquo' />\n\n\t\t</form>";
    return $OUTPUT;
}
function enter($errors = "")
{
    global $_POST;
    extract($_POST);
    require_lib("validate");
    $v = new validate();
    $fields["search_cus"] = "";
    $fields["stkid"] = "";
    $fields["cusnum"] = "";
    $fields["stkcod"] = "";
    $fields["stkname"] = "";
    $fields["serno"] = "";
    $fields["description"] = "";
    $fields["conditions"] = "";
    $fields["notes"] = "";
    foreach ($fields as $var_name => $value) {
        if (!isset(${$var_name})) {
            ${$var_name} = $value;
        }
    }
    if (empty($conditions)) {
        // Retrieve the workshop conditions from Cubit.
        db_conn("cubit");
        $sql = "SELECT value FROM workshop_settings WHERE div='" . USER_DIV . "' AND setting='workshop_conditions'";
        $wssRslt = db_exec($sql) or errDie("Unable to retrieve workshop settings from Cubit.");
        $conditions = pg_fetch_result($wssRslt, 0);
    }
    if (isset($notes)) {
        $v->isOk($notes, "string", 1, 1024, "Invalid notes.");
    } else {
        $notes = "";
    }
    // Stock code dropdown
    $stkdn = "<select name=stkid style='width:180px'>\n\t  <option value='0'>Please select</th>";
    db_conn("cubit");
    $sql = "SELECT * FROM stock WHERE div='" . USER_DIV . "' ORDER BY stkcod ASC";
    $stkRslt = db_exec($sql) or errDie("Unable to retrieve the stock from Cubit.");
    while ($stkData = pg_fetch_array($stkRslt)) {
        if ($stkid == $stkData["stkid"]) {
            $selected = "selected";
        } else {
            $selected = "";
        }
        $stkdn .= "<option value='{$stkData['stkid']}' {$selected}>{$stkData['stkcod']}</option>";
    }
    $stkdn .= "</select>";
    // Customer dropdown
    $cusdn = "<select name='cusnum' style='width:180px'>";
    db_conn("cubit");
    $sql = "SELECT * FROM customers WHERE surname LIKE '%{$search_cus}%' AND div='" . USER_DIV . "' ORDER BY surname ASC";
    $cusRslt = db_exec($sql) or errDie("Unable to retrieve customers from Cubit.");
    if (pg_num_rows($cusRslt) == 0) {
        $cusdn .= "<option value='0'>No customers found</option>";
    }
    while ($cusData = pg_fetch_array($cusRslt)) {
        if ($cusnum == $cusData["cusnum"]) {
            $selected = "selected";
        } else {
            $selected = "";
        }
        $cusdn .= "<option value='{$cusData['cusnum']}' {$selected}>{$cusData['surname']} {$cusData['init']}</option>";
    }
    $cusdn .= "</select>";
    $OUTPUT = "\n\t\t\t\t\t<h3>Add to workshop</h3>\n\t\t\t\t\t{$errors}\n\t\t\t\t\t<form method='POST' action='" . SELF . "' name='frm_ws'>\n\t\t\t\t\t\t<input type='hidden' name='key' value='confirm'>\n\t\t\t\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan=2>Add</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>" . REQ . "Customer</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<center><input type='button' value='Search' onClick='popupSized(\"customers-view.php?action=select&" . frmupdate_make("text", "frm_ws", "cusnum", "cusnum_only") . "\", \"workshop_custsearch\", 800, 400);'></center><br>\n\t\t\t\t\t\t\t\t{$cusdn}\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>" . REQ . "Stock Code/Name</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t{$stkdn}<br>\n\t\t\t\t\t\t\t\t<input type='text' name='stkname' value='{$stkname}' style='width:180px'>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Serial number</td>\n\t\t\t\t\t\t\t<td><input type='text' name='serno' value='{$serno}' style='width:180px'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>" . REQ . "Description</td>\n\t\t\t\t\t\t\t<td><input type='text' name='description' value='{$description}' style='width:180px'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>" . REQ . "Workshop Conditions</td>\n\t\t\t\t\t\t\t<td><textarea name='conditions' rows='5' style='width:180px'>{$conditions}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Notes</td>\n\t\t\t\t\t\t\t<td><textarea name='notes' rows='5' style='width:180px'>{$notes}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td colspan='2' align='right'>\n\t\t\t\t\t\t\t\t<input type='submit' value='Confirm &raquo'>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>";
    return $OUTPUT;
}
function enterAppointment()
{
    global $_GET;
    // if a selected user was specified, but you do not have write permission to his diary, remove
    // the diary selection and update buttons
    if (isset($_GET["ap_diaryowner"]) && $_GET["ap_diaryowner"] != USER_NAME) {
        db_conn("cubit");
        $sql = "SELECT * FROM diary_privileges\n\t\t\tWHERE diary_owner='{$_GET['ap_diaryowner']}' AND priv_owner='" . USER_NAME . "' AND privilege='W'";
        $rslt = db_exec($sql) or errDie("Error checking diary permissions (REMFLD).");
        if (pg_num_rows($rslt) > 0) {
            $NOT_WRITEABLE = false;
        } else {
            $NOT_WRITEABLE = true;
        }
    } else {
        $NOT_WRITEABLE = false;
    }
    // start of form
    $OUTPUT = "<center>\n\t\t<table width=100% height=100%>\n\t\t\t<tr>\n\t\t\t\t<td valign=top align=center>";
    if (!$NOT_WRITEABLE) {
        $OUTPUT .= "<form action='diary-appointment.php' method=POST name='form'>";
    }
    // generate lists for start time selections
    $select_day = "";
    for ($i = 1; $i <= 31; $i++) {
        if (isset($_GET["ap_day"]) && $_GET["ap_day"] == $i) {
            $selected = "selected";
        } else {
            $selected = "";
        }
        $select_day .= "<option value={$i} {$selected}>{$i}</option>";
    }
    $select_month = "";
    for ($i = 1; $i <= 12; $i++) {
        if (isset($_GET["ap_month"]) && $_GET["ap_month"] == $i) {
            $selected = "selected";
        } else {
            $selected = "";
        }
        $select_month .= "<option value={$i} {$selected}>" . date("M", mktime(0, 0, 0, $i, 1, 2000)) . "</option>";
    }
    $select_year = "";
    for ($i = date("Y"); $i <= 2050; $i++) {
        if (isset($_GET["ap_year"]) && $_GET["ap_year"] == $i) {
            $selected = "selected";
        } else {
            $selected = "";
        }
        $select_year .= "<option value={$i} {$selected}>{$i}</option>";
    }
    $select_start_time = "";
    for ($i = 6; $i <= 21; $i++) {
        $selected1 = "";
        $selected2 = "";
        if (isset($_GET["ap_start_time"])) {
            if ($_GET["ap_start_time"] == "{$i}:00") {
                $selected1 = "selected";
            } else {
                if ($_GET["ap_start_time"] == "{$i}:30") {
                    $selected2 = "selected";
                }
            }
        }
        $select_start_time .= "<option value='{$i}:00' {$selected1}>{$i}:00</option>";
        $select_start_time .= "<option value='{$i}:30' {$selected2}>{$i}:30</option>";
    }
    // generate lists for end time selection
    $select_end_time = "";
    for ($i = 6; $i <= 22; $i++) {
        $selected1 = "";
        $selected2 = "";
        if (isset($_GET["ap_end_time"])) {
            if ($_GET["ap_end_time"] == "{$i}:00") {
                $selected1 = "selected";
            } else {
                if ($_GET["ap_end_time"] == "{$i}:30") {
                    $selected2 = "selected";
                }
            }
        } else {
            if (isset($_GET["ap_start_time"])) {
                if ($_GET["ap_start_time"] == $i - 1 . ":30") {
                    $selected1 = "selected";
                } else {
                    if ($_GET["ap_start_time"] == $i . ":00") {
                        $selected2 = "selected";
                    }
                }
            }
        }
        $select_end_time .= "<option value='{$i}:00' {$selected1}>{$i}:00</option>";
        // only add this on if it not past 22:00
        if ($i < 22) {
            $select_end_time .= "<option value='{$i}:30' {$selected2}>{$i}:30</option>";
        }
    }
    // lists for repetitions dates
    $select_repet_day = "";
    for ($i = 1; $i <= 31; $i++) {
        if (isset($_GET["ap_repet_day"]) && $_GET["ap_repet_day"] == $i) {
            $selected = "selected";
        } else {
            if (isset($_GET["ap_day"]) && $_GET["ap_day"] == $i) {
                $selected = "selected";
            } else {
                $selected = "";
            }
        }
        $select_repet_day .= "<option value={$i} {$selected}>{$i}</option>";
    }
    $select_repet_month = "";
    for ($i = 1; $i <= 12; $i++) {
        if (isset($_GET["ap_repet_month"]) && $_GET["ap_repet_month"] == $i) {
            $selected = "selected";
        } else {
            if (isset($_GET["ap_month"]) && $_GET["ap_month"] == $i) {
                $selected = "selected";
            } else {
                $selected = "";
            }
        }
        $select_repet_month .= "<option value={$i} {$selected}>" . date("M", mktime(0, 0, 0, $i, 1, 2000)) . "</option>";
    }
    $select_repet_year = "";
    for ($i = date("Y"); $i <= 2050; $i++) {
        if (isset($_GET["ap_repet_year"]) && $_GET["ap_repet_year"] == $i) {
            $selected = "selected";
        } else {
            if (isset($_GET["ap_year"]) && $_GET["ap_year"] == $i) {
                $selected = "selected";
            } else {
                $selected = "";
            }
        }
        $select_repet_year .= "<option value={$i} {$selected}>{$i}</option>";
    }
    // list of diaries person may edit
    if ($NOT_WRITEABLE) {
        $diary_list = "{$_GET['ap_diaryowner']}";
    } else {
        db_conn("cubit");
        $sql = "SELECT '" . USER_NAME . "' AS diary_owner\n\t\t\tUNION\n\t\t\tSELECT diary_owner FROM diary_privileges WHERE privilege = 'W' AND priv_owner = '" . USER_NAME . "'";
        $rslt = db_exec($sql) or errDie("Error reading diaries you may write to.");
        $diary_list = "<select name='ap_diaryowner'>";
        while ($row = pg_fetch_array($rslt)) {
            if (isset($_GET["ap_diaryowner"]) && $_GET["ap_diaryowner"] == $row["diary_owner"]) {
                $selected = "selected";
            } elseif (!isset($_GET["ap_diaryowner"]) && $row["diary_owner"] == USER_NAME) {
                $selected = "selected";
            } else {
                $selected = "";
            }
            $diary_list .= "<option value='{$row['0']}' {$selected}>{$row['0']}</option>";
        }
        $diary_list .= "</select>";
    }
    // list of categories, default selection: appointments
    $rslt = db_exec("SELECT category_id,category_name FROM diary_categories");
    $category_list = "";
    // check if there was any categories, if not add them, and get the results again
    if (pg_num_rows($rslt) <= 0) {
        db_exec("INSERT INTO diary_categories (category_name) VALUES('Reminder')") or errDie("Error inserting category");
        db_exec("INSERT INTO diary_categories (category_name) VALUES('Call')") or errDie("Error inserting category");
        db_exec("INSERT INTO diary_categories (category_name) VALUES('Meeting')") or errDie("Error inserting category");
        db_exec("INSERT INTO diary_categories (category_name) VALUES('Birthday')") or errDie("Error inserting category");
        db_exec("INSERT INTO diary_categories (category_name) VALUES('Training')") or errDie("Error inserting category");
        db_exec("INSERT INTO diary_categories (category_name) VALUES('Event')") or errDie("Error inserting category");
        $rslt = db_exec("SELECT category_id,category_name FROM diary_categories");
    }
    while ($row = pg_fetch_row($rslt)) {
        if (isset($_GET["ap_category"]) && $_GET["ap_category"] == $row[0]) {
            $selected = "selected";
        } else {
            if (!isset($_GET["ap_category"]) && $row[1] == 'Appointments') {
                $selected = "selected";
            } else {
                $selected = "";
            }
        }
        $category_list .= "<option value={$row['0']} {$selected}>{$row['1']}</option>";
    }
    // notify list
    $select_notify = "";
    for ($i = 0; $i <= 14; $i++) {
        if (isset($_GET["ap_notify"]) && $_GET["ap_notify"] == $i) {
            $selected = "selected";
        } else {
            if (!isset($_GET["ap_notify"]) && $i == 0) {
                $selected = "selected";
            } else {
                $selected = "";
            }
        }
        if ($i == 0) {
            // no notify
            $select_notify .= "<option value='{$i}' {$selected}>Dont Notify</option>";
        } else {
            $select_notify .= "<option value='{$i}' {$selected}>{$i} days before</option>";
        }
    }
    // selection restore for Repetitions
    if (isset($_GET["ap_repet"])) {
        $_GET["ap_repet"] == 'N' ? $rep_selected0 = "checked" : ($rep_selected0 = "");
        $_GET["ap_repet"] == 'D' ? $rep_selected1 = "checked" : ($rep_selected1 = "");
        $_GET["ap_repet"] == 'W' ? $rep_selected2 = "checked" : ($rep_selected2 = "");
        $_GET["ap_repet"] == 'M' ? $rep_selected3 = "checked" : ($rep_selected3 = "");
        $_GET["ap_repet"] == 'Y' ? $rep_selected4 = "checked" : ($rep_selected4 = "");
    } else {
        $rep_selected0 = "checked";
        $rep_selected1 = "";
        $rep_selected2 = "";
        $rep_selected3 = "";
        $rep_selected4 = "";
    }
    // format variables so they are checked or filled again
    isset($_GET["ap_entireday"]) ? $sel_entireday = "checked" : ($sel_entireday = "");
    isset($_GET["ap_private"]) ? $sel_private = "checked" : ($sel_private = "");
    isset($_GET["ap_repet_forever"]) ? $sel_repet_forever = "checked" : ($sel_repet_forever = "");
    isset($_GET["ap_title"]) ? $ap_title = $_GET["ap_title"] : ($ap_title = "");
    isset($_GET["ap_location"]) ? $ap_location = $_GET["ap_location"] : ($ap_location = "");
    isset($_GET["ap_homepage"]) ? $ap_homepage = $_GET["ap_homepage"] : ($ap_homepage = "");
    isset($_GET["ap_description"]) ? $ap_description = htmlspecialchars($_GET["ap_description"]) : ($ap_description = "");
    isset($_GET["ap_required"]) ? $ap_required = $_GET["ap_required"] : ($ap_required = "");
    isset($_GET["ap_notrequired"]) ? $ap_notrequired = $_GET["ap_notrequired"] : ($ap_notrequired = "");
    isset($_GET["ap_optional"]) ? $ap_optional = $_GET["ap_optional"] : ($ap_optional = "");
    isset($_GET["ap_leadid"]) ? $ap_leadid = $_GET["ap_leadid"] : ($ap_leadid = "");
    // start date
    $OUTPUT .= "<table width=100% cellpadding='2' cellspacing='0' class='shtable'>\n\t\t\t<tr class='even'>\n\t\t\t\t<td>Date:</td>\n\t\t\t\t<td>\n\t\t\t\t\t<select name='ap_day'>{$select_day}</select> &nbsp; &nbsp;\n\t\t\t\t\t<select name='ap_month'>{$select_month}</select> &nbsp; &nbsp;\n\t\t\t\t\t<select name='ap_year'>{$select_year}</select> &nbsp; &nbsp;\n\t\t\t\t</td>\n\t\t\t\t<td nowrap><input type=checkbox {$sel_entireday} name='ap_entireday'>Entire Day</td>\n\t\t\t</tr>\n\t\t\t<tr class='odd'>\n\t\t\t\t<td>Time:</td>\n\t\t\t\t<td>\n\t\t\t\t\tFrom &nbsp; &nbsp; <select name='ap_start_time'>{$select_start_time}</select> &nbsp; &nbsp;\n\t\t\t\t\tto &nbsp; &nbsp; <select name='ap_end_time'>{$select_end_time}</select>\n\t\t\t\t</td>\n\t\t\t\t<td nowrap><input type=checkbox {$sel_private} name='ap_private'>Private</td>\n\t\t\t</tr>\n\t\t\t</table>\n\t\t\t<p></p>";
    // Retrieve contacts from the database
    db_conn("cubit");
    $sql = "SELECT * FROM cons WHERE by='" . USER_NAME . "'";
    $rslt = db_exec($sql) or errDie("Unable to retrieve leads from Cubit.");
    if (!pg_num_rows($rslt)) {
        $lead_sel = "<input type='hidden' name='lead_id' value='0'>";
        $lead_sel .= "<b>[No contacts found]</b>";
    } else {
        $lead_sel = "<select name='lead_id' style='width: 150'>";
        $lead_sel .= "<option value='0'>[None]</option>";
        while ($lead_data = pg_fetch_array($rslt)) {
            if ($lead_data["id"] == $ap_leadid) {
                $selected = "selected";
            } else {
                $selected = "";
            }
            $lead_sel .= "<option value='{$lead_data['id']}'>{$lead_data['name']} {$lead_data['surname']}</option>";
        }
        $lead_sel .= "</select>";
    }
    // Create the location dropdown
    $sql = "SELECT * FROM cubit.diary_locations";
    $loc_rslt = db_exec($sql) or errDie("Unable to retrieve locations.");
    $loc_sel = "<select name='loc_id'>\n\t\t<option value='0'>[None]</option>";
    while ($loc_data = pg_fetch_array($loc_rslt)) {
        if ($loc_id == $loc_data["id"]) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $loc_sel .= "<option value='{$loc_data['id']}' {$sel}>\n\t\t\t{$loc_data['location']}\n\t\t</option>";
    }
    $loc_sel .= "</select>";
    // information fields
    $OUTPUT .= "\n\t\t\t<table width=100% cellpadding='2' cellspacing='0' class='shtable'>\n\t\t\t\t<tr class='even'>\n\t\t\t\t\t<td>Title:</td>\n\t\t\t\t\t<td><input type=text name=ap_title style='width: 150' value='{$ap_title}'>{$lead_sel}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='odd'>\n\t\t\t\t\t<td>Location:</td>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t{$loc_sel}\n\t\t\t\t\t\t<a href='javascript:popupOpen(\"location_save.php?" . frmupdate_make("list", "form", "loc_id") . "\")'>\n\t\t\t\t\t\t\tAdd Location\n\t\t\t\t\t\t</a>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='even'>\n\t\t\t\t\t<td colspan=2>\n\t\t\t\t\t\tDescription:<br>\n\t\t\t\t\t\t<textarea name=ap_description rows=5 cols=60>{$ap_description}</textarea>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t\t<p></p>";
    // repetitions
    $OUTPUT .= "<table width=100% cellspacing='0' cellpadding='2' class='shtable'>\n\t\t\t<tr class='even'>\n\t\t\t\t<td>Repetitions:</td>\n\t\t\t\t<td><input type=radio name='ap_repet' value=N {$rep_selected0}>None</td>\n\t\t\t\t<td><input type=radio name='ap_repet' value=D {$rep_selected1}>Daily</td>\n\t\t\t\t<td><input type=radio name='ap_repet' value=W {$rep_selected2}>Weekly</td>\n\t\t\t\t<td><input type=radio name='ap_repet' value=M {$rep_selected3}>Monthly</td>\n\t\t\t\t<td><input type=radio name='ap_repet' value=Y {$rep_selected4}>Yearly</td>\n\t\t\t</tr>\n\t\t\t<tr class='odd'>\n\t\t\t\t<td>Until:</td>\n\t\t\t\t<td><select name='ap_repet_day'>{$select_repet_day}</select></td>\n\t\t\t\t<td><select name='ap_repet_month'>{$select_repet_month}</select></td>\n\t\t\t\t<td><select name='ap_repet_year'>{$select_repet_year}</select></td>\n\t\t\t\t<td colspan=2><input type=checkbox {$sel_repet_forever} name='ap_repet_forever'>Forever</td>\n\t\t\t</tr>\n\t\t\t</table>\n\t\t\t<p></p>";
    // other user info (NOT YET IMPLEMENTED)
    $OUTPUT .= "\n\t\t\t<input type=hidden name='ap_required' value=''>\n\t\t\t<input type=hidden name='ap_notrequired' value=''>\n\t\t\t<input type=hidden name='ap_optional' value=''>\n\t\t\t";
    /*$OUTPUT.="<table width=100%>
    			<tr>
    				<td>Required</td>
    				<td><input type=text name='ap_required' size=50 value='$ap_required'></td>
    			</tr>
    			<tr>
    				<td>Not Required</td>
    				<td><input type=text name='ap_notrequired' size=50 value='$ap_notrequired'></td>
    			</tr>
    			<tr>
    				<td>Optional</td>
    				<td><input type=text name='ap_optional' size=50 value='$ap_optional'></td>
    			</tr>
    		</table><hr>";*/
    // categories and whos diary
    $OUTPUT .= "<table width=100% cellspacing='0' cellpadding='2' class='shtable'>\n\t\t<tr class='even'>\n\t\t\t<td valign=top>Category:</td>\n\t\t\t<td><select name='ap_category'>{$category_list}</select></td>\n\t\t\t<td valign=top nowrap>Who's Diary:</td>\n\t\t\t<td valign=top>{$diary_list}</td>\n\t\t</tr>\n\t\t<tr class='odd'>\n\t\t\t<td valign=top>Notify Time:</td>\n\t\t\t<td><select name='ap_notify'>{$select_notify}</select> days before</td>\n\t\t\t<td colspan=2>&nbsp;</td>\n\t\t</tr>\n\t</table>";
    if (!$NOT_WRITEABLE) {
        // attach the appropriate buttons
        if (isset($_GET["key"]) && ($_GET["key"] == "view" || $_GET["key"] == "modify")) {
            // attach modify button
            $OUTPUT .= "\n\t\t\t\t<center><table><tr><td>\n\t\t\t\t<input type=hidden name=key value=modify>\n\t\t\t\t<input type=hidden name='entry_id' value='{$_GET['entry_id']}'>\n\t\t\t\t<input type=submit name=submit value='Modify Appointment'>\n\t\t\t</form></td></tr>";
            // attach delete button
            if (isset($_GET["entry_id"])) {
                $OUTPUT .= "<tr><td><form action='diary-appointment.php' method=post>\n\t\t\t\t\t\t<input type=hidden name=key value=delete>\n\t\t\t\t\t\t<input type=hidden name='entry_id' value='{$_GET['entry_id']}'>\n\t\t\t\t\t\t<input type=submit name=submit value='Delete Appointment'>\n\t\t\t\t\t</form></td>\n\t\t\t\t</tr></table></center>";
            }
            define("DOC_TITLE", "{$ap_title}");
        } else {
            $OUTPUT .= "\t\t<br>\n\t\t\t\t<input type=hidden name=key value=create>\n\t\t\t\t<input type=submit name=submit value='Create Appointment'>\n\t\t\t</form>";
            define("DOC_TITLE", "New Appointment");
        }
    }
    // end of form
    $OUTPUT .= "</td>\n\t\t\t</tr>\n\t\t</table>\n\t</center>";
    return $OUTPUT;
}