Location: PHPKode > projects > IRMA - Asset Inventory and Job Tracking > irma-1.0.6/includes/purchase-orders/functions.php
<?php

include_once(INCDIR . "/includes_vendors.php");

function searchFormPurchaseOrders() {
    // Print Search Form
    
    GLOBAL $cfg_install, $layout, $lang;

    $options['vendor_id']     = $lang['purchase_orders']['VENDOR'];
    $options['order_number']  = $lang['purchase_orders']['ORDER_NUMBER'];
    $options['modified']      = $lang['purchase_orders']['MODIFIED'];
    $options['sent']          = $lang['purchase_orders']['SENT'];
    $options['received']      = $lang['purchase_orders']['RECEIVED'];
    $options['p.comments']    = $lang['purchase_orders']['COMMENTS'];

    print <<<EOF
<div align="center">

<a href="{$cfg_install['root']}/purchase-orders/info.php">
  <b>{$lang['purchase_orders']['ADD']}...</b>
</a>
<hr noshade>

<form method="GET" action="{$cfg_install['root']}/purchase-orders/search.php">
<table border="0" width="90%">
  <tr>
    <th colspan="2"><b>{$lang['search']['SEARCH']}:</b></th>
  </tr>
  <tr>
    <td align=center>
      <select name="field" size="1">
EOF;

    asort($options);
    reset($options);
    foreach ($options as $key => $value) {
        echo "<option value=\"$key\">$value\n";
    }

    print <<<EOF
      </select>
      &nbsp;
      {$lang['search']['NOTUSED']}
      &nbsp;
      <select name="phrasetype">
        <option value="contains">{$lang['search']['CONTAINS']}</option>
        <option value="contains">{$lang['search']['IS']}</option>
      </select>
      <input type="text" size="10" name="contains">
      &nbsp{$lang['search']['SORTBY']}
      &nbsp;
      <select name="sort" size="1">
EOF;

    reset($options);
    foreach ($options as $key => $value) {
        print <<<EOF
        <option value="{$key}">$value</option>

EOF;
    }
    
    print <<<EOF
      </select>
      <input type="submit" value="{$lang['buttons']['SEARCH']}">
    </td>
  </tr>
</table>
</form>
</div>
EOF;

}

function showPurchaseOrdersShort($target, $vendor_id) {

    GLOBAL $lang, $cfg_install;

    $db = new DB;

    $sql = <<<EOF
SELECT 
    purchase_order_id, order_number, comments, sent, received
FROM 
   purchase_order
WHERE
   vendor_id = {$vendor_id}
EOF;

    print <<<EOF
<div align="center">
<table border="0" cellspacing="0" cellpadding="3">
<tr>
  <th>{$lang['purchase_orders']['ORDER_NUMBER']}</th>
  <th>{$lang['purchase_orders']['COMMENTS']}</th>
  <th>{$lang['purchase_orders']['SENT']}</th>
  <th>{$lang['purchase_orders']['RECEIVED']}</th>
</tr>

EOF;

    $rs = $db->query($sql);
    $count = $db->numrows($rs);

    for ($i = 0; $i < $count; $i++) {
        $id = $db->result($rs, $i, "purchase_order_id");
        $number = $db->result($rs, $i, "order_number");
        $comments = $db->result($rs, $i, "comments");
        $sent = $db->result($rs, $i, "sent");
        $received = $db->result($rs, $i, "received");

        print <<<EOF
<tr>
  <td>
    <a href="{$cfg_install['root']}/purchase-orders/info.php?ID={$id}">
    {$number}
    </a>
  </td>
  <td>{$comments}&nbsp;</td>
  <td>{$sent}</td>
  <td>{$received}&nbsp;</td>
</tr>

EOF;

    }

    print <<<EOF
</table>
</div>

EOF;

}

// Lists PurchaseOrders
function showPurchaseOrdersList($target, $username, $field, 
                                $phrasetype, $contains,    
                                $sort, $order, $start) { 

    GLOBAL $cfg_install, $cfg_features, $lang;

    if ($sort == "") {
        $sort = "order_number";
    }

    $where = "(1 = 1)";

    // Build query
    if ($field != "") {
        if ($phrasetype == "contains") {
            $where = "({$field} LIKE '%{$contains}%')";
        } else {
            $where = "({$field} = '{$contains}')";
        }
    }

    if (! $start) {
        $start = 0;
    }

    if (! $order) {
        $order = "ASC";
    }

    $query = <<<EOF
SELECT 
    v.name as vendor,
    p.vendor_id as vendor_id,
    p.purchase_order_id as purchase_order_id,
    p.order_number as order_number,
    p.sent as sent,
    p.received as received,
    p.modified as modified,
    p.comments as comments
FROM 
   purchase_order p,
   vendor v
WHERE 
   p.vendor_id = v.vendor_id AND
   $where 
ORDER BY 
   $sort 
EOF;
    
    // Get it from database    
    $db = new DB;

    // Uncomment to view executed SQL
    // print "<h2>$query</h2>";

    if ($result = $db->query($query)) {

        $numrows = $db->numrows($result);

        // Limit the result, if no limit applies, use prior result
        if ($numrows > $cfg_features["list_limit"]) {

            $query_limit = "$query $order LIMIT $start, {$cfg_features['list_limit']}";
            $result_limit = $db->query($query_limit);
            $numrows_limit = $db->numrows($result_limit);

        } else {
            $numrows_limit = $numrows;
            $result_limit = $result;
        }

        $link = get_place();

        if ($numrows_limit > 0) {

            // Produce headline
            // Name
            print <<<EOF
  <center>

  <h4><a href="{$link}">Back</a></h4>

  <table border="0">
  <tr>

EOF;

            $headers = array('vendor', 'order_number', 'sent', 'received',
                             'modified', 'comments');

            foreach ($headers as $label) {

                if ($sort == $label) {
                    echo "&middot;&nbsp;";
                }

                $args = "field={$field}&phrasetype={$phrasetype}&" .
                        "contains={$contains}&sort={$label}&order=ASC&" .
                        "start={$label}";

                $ulabel = strtoupper($label);

                print <<<EOF
    <th>
      <a href="{$target}?{$args}">{$lang['purchase_orders'][$ulabel]}</a></th>
    </th>

EOF;

            }

                print <<<EOF
  </tr>

EOF;

            for ($i = 0; $i < $numrows_limit; $i++) {

                $id = $db->result($result_limit, $i, "purchase_order_id");
                $vendor = $db->result($result_limit, $i, "vendor");

                $purchase_order = new PurchaseOrder;
                $purchase_order->getfromDB($id);

                print <<<EOF
  <tr>
    <td>{$vendor}</td>
    <td>
      <b>
        <a href="{$cfg_install['root']}/purchase-orders/info.php?ID={$id}">
          {$purchase_order->order_number} ({$purchase_order->purchase_order_id})
        </a>
      </b>
    </td>
    <td>{$purchase_order->sent}</td>
    <td>{$purchase_order->received}</td>
    <td>{$purchase_order->modified}</td>
    <td>{$purchase_order->comments}</td>
  </tr>

EOF;
            }

            // Close Table
            print <<<EOF
</table>
</center>

EOF;

            // Pager
            $parameters="field=$field&phrasetype=$phrasetype&" .
                        "contains=$contains&sort=$sort";
            printPager($start, $numrows, $target, $parameters);

        } else {
            print <<<EOF
<center>
  <b>{$lang['purchase_orders']['NOTFOUND']}</b>
</center>
<hr noshade>
EOF;
            searchFormPurchaseOrders();
        }
    }
}


function showPurchaseOrdersForm ($target, $id) {

    GLOBAL $cfg_install, $lang;

    $purchase_order = new PurchaseOrder;
    $vendor = new Vendor;

    $header = "";

    $vendor_id = "";

    if ($id == "") {
        $header = $lang["purchase_orders"]['ADD'].":";
        $vendor_id = $_REQUEST['vendor_id'];
    } else {
        $purchase_order->getfromDB($id);
        $vendor_id = $purchase_order->vendor_id;
        $vendor->getfromDB($vendor_id);
        $header = "{$vendor->name}: {$purchase_order->order_number} (${id})";
    }        

    $link = get_place();

    print <<<EOF
<div align="center">

<h4><a href="{$link}">Back to Results</a></h4>

<form method="POST" action="{$target}">
<table border="0" cellpadding="0">
  <tr>
    <th><b>{$header}</b></th>
  </tr>
  <tr>
    <td valign="top">
      <!-- Begin form display table -->
      <table cellpadding="3" cellspacing="0" border="0">
      <tr>
        <td width="50%" align="right">{$lang['purchase_orders']['ORDER_NUMBER']}: </td>
        <td>
            <input type="text" name="order_number" 
                   value="{$purchase_order->order_number}" size="20">
        </td>
      </tr>
      <tr>
        <td align="right">{$lang['purchase_orders']['SENT']}: </td>
        <td><input type="text" name="sent" 
                   value="{$purchase_order->sent}" size="10"> 
                   (YYYY-MM-DD)</td>
      </tr>
      <tr>
        <td align="right">{$lang['purchase_orders']['RECEIVED']}: </td>
        <td><input type="text" name="received" 
                   value="{$purchase_order->received}" size="10"> 
                   (YYYY-MM-DD)</td>
      </tr>
      <tr>
        <td align="right">{$lang['purchase_orders']['MODIFIED']}: </td>
        <td>{$purchase_order->modified}</td>
      </tr>
      <tr>
        <th align="center" colspan="2">{$lang['purchase_orders']['COMMENTS']}
        </th>
      </tr>
      <tr>
        <td align="center" colspan="2">
          <textarea cols="50" rows="4" name="comments" 
                    wrap="soft">{$purchase_order->comments}</textarea>
        </td>
      </tr>
EOF;
    
    if ($id == "") {

        print <<<EOF
       <tr>
         <td valign="top" colspan="2">
           <center>
             <input type="hidden" name="vendor_id" value="{$vendor_id}">
             <input type="submit" name="add" value="{$lang['buttons']['ADD']}">
           </center>
         </td>
       </tr>

EOF;

    } else {

        print <<<EOF
       <tr>
         <td align="center" valign="top">
           <input type="hidden" name="vendor_id" 
                  value="{$vendor_id}">
           <input type="hidden" name="purchase_order_id" 
                  value="{$purchase_order->purchase_order_id}">
           <input type="submit" name="update" 
                  value="{$lang['buttons']['UPDATE']}">
           </form>
         </td>
         <td align="center" valign="top">
           <form action="{$target}" method="POST">
             <input type="hidden" name="vendor_id" 
                    value="{$vendor_id}">
             <input type="hidden" name="purchase_order_id" 
                    value="{$purchase_order->purchase_order_id}">
             <input type="submit" name="delete" 
                    value="{$lang['buttons']['DELETE']}">
           </form>
         </td>
       </tr>
       </table>
       <!-- End form display table -->

EOF;


    }

    print <<<EOF
     </td>
   </tr>
</table>
</form>
</div>
<br/>
EOF;

       if ($id != "") {
           showLineItemsForm($target, $id);
       }
}


// Update a purchase_order in the database
function updatePurchaseOrder($input) {

    $purchase_order = new PurchaseOrder;
    $purchase_order->getFromDB($input['purchase_order_id']);

    // set new date and make sure it gets updated
    $fields = array();
    $fields[0]= "modified";
    $purchase_order->modified = date("Y-m-d H:i:s");
    
    // Fill the update-array with changes
    for ($i=0; $i < count($input); $i++) {
        list($key,$val) = each($input);
        if ($purchase_order->$key != $input[$key]) {
            $purchase_order->$key = $input[$key];
            $fields[$i+1] = $key;
        }
    }

    $purchase_order->updateInDB($fields);

}

// Add PurchaseOrder, nasty hack until we get PHP4-array-functions
function addPurchaseOrder($input) {

    $purchase_order = new PurchaseOrder;
    
    // fill array for update
    for ($i=0; $i < count($input); $i++) {
        list($key, $val) = each($input);
        $purchase_order->$key = $input[$key];
    }

    $purchase_order->addToDB();

}

function deletePurchaseOrder($id) {
    
    $purchase_order = new PurchaseOrder;
    $purchase_order->deleteFromDB($id);
    
}     

function showLineItemsForm($target, $purchase_order_id) {

    GLOBAL $lang, $cfg_install;

    $db = new DB;

    $sql = <<<EOF
SELECT 
    line_item_id, purchase_order_id, type,
    description, product_number, quantity, quantity_received
FROM 
   line_item
WHERE
   purchase_order_id = {$purchase_order_id}
ORDER BY
   created, line_item_id
EOF;

    print <<<EOF
<div align="center">
<h4>{$lang['line_items']['ADD']}</h4>
<table width="85%" border="0" cellspacing="0" cellpadding="3">
<tr>
  <th>{$lang['line_items']['PRODUCT_NUMBER']}</th>
  <th>{$lang['line_items']['DESCRIPTION']}</th>
  <th>{$lang['line_items']['TYPE']}</th>
  <th>{$lang['line_items']['QUANTITY']}</th>
  <th>{$lang['line_items']['ACTION']}</th>
</tr>
<form action="{$target}" method="POST">
<tr>
  <td align="center"><input type="text" size="20" name="product_number" 
             class="tdsc" value=""></td>
  <td align="center"><textarea name="description" class="tdsc" 
              cols="30" rows="3"></textarea>
  </td>
  <td align="center">
    <select name="type">
      <option value="computers">Computer</option>
      <option value="monitors">Monitor</option>
      <option value="networking">Network Device</option>
      <option value="peripherals">Peripheral</option>
      <option value="software">Software</option>
    </select>
  </td>
  <td align="center">
    <input type="hidden" name="quantity_received" value="0">
    <input name="quantity" size="5">
  </td>
  <td class="tdsc" colspan="2">
        <br/>
        <input type="hidden" name="id" value="{$purchase_order_id}">
        <input type="hidden" name="action" value="add">
        <input type="hidden" name="o" value="line_item">
        <input type="submit" class="tdsc" name="button" 
               value="{$lang['buttons']['ADD']}">
      </form>
  </td>
</tr>
</table>
<h4>{$lang['line_items']['VIEWITEMS']}</h4>
<table width="85%" border="0" cellspacing="0" cellpadding="3">
<tr>
  <th>{$lang['line_items']['PRODUCT_NUMBER']}</th>
  <th>{$lang['line_items']['DESCRIPTION']}</th>
  <th>{$lang['line_items']['TYPE']}</th>
  <th>{$lang['line_items']['QUANTITY_RECEIVED']}</th>
  <th>{$lang['line_items']['QUANTITY']}</th>
  <th>{$lang['line_items']['ACTION']}</th>
  <th>{$lang['line_items']['ASSOCIATIONS']}</th>
</tr>

EOF;

    $rs = $db->query($sql);
    $count = $db->numrows($rs);

    for ($i = 0; $i < $count; $i++) {
        $id = $db->result($rs, $i, "line_item_id");
        $product_number = $db->result($rs, $i, "product_number");
        $description = stripslashes($db->result($rs, $i, "description"));
        $type = $db->result($rs, $i, "type");
        $quantity = $db->result($rs, $i, "quantity");
        $quantity_received = $db->result($rs, $i, "quantity_received");

        $class = 'class="greencell"';

        $received = $quantity_received;

        if ($quantity_received < $quantity) {
            $class = 'class="redcell"';
        }

        $types = array('computers'   => 'Computer',
                       'monitors'    => 'Monitor',
                       'networking'  => 'Network Device',
                       'peripherals' => 'Peripheral',
                       'software'    => 'Software');
                       # 'support' => 'Support Contract'

        $type_label = $types[$type];

        $select_ql = "";

        for ($q = 0; $q <= $quantity; $q++) {
            $sel = "";
            if ($quantity_received == $q) {
                $sel = "selected";
            }
            $select_ql .= <<<EOF
      <option value="{$q}" {$sel}>{$q}</option>

EOF;
        }

        print <<<EOF
<form action="$target" method="POST">
<tr>
  <td align="center">
    <input type="text" size="20" name="product_number" 
           class="tdsc" value="{$product_number}">&nbsp;</td>
  <td align="center">
    <textarea name="description" class="tdsc" 
              cols="30" rows="3">{$description}</textarea>
  </td>
  <td align="center">
    {$type_label}
  </td>
  <td align="center" {$class}>
    <select name="quantity_received">
{$select_ql}
    </select>
  </td>
  <td align="center">{$quantity}&nbsp;</td>
  <td align="center">
    <input type="hidden" name="id" value="${purchase_order_id}">
    <input type="hidden" name="line_item_id" value="${id}">
    <input type="hidden" name="o" value="line_item">
    <br/>
    <nobr>
    <select name="action" class="tdsc">
      <option value="delete">Delete</option>
      <option value="update" selected>Update</option>
    </select>
    <input type="submit" class="tdsc" name="button" 
           class="tdsc" value="{$lang['buttons']['GO']}">
    </nobr>
    </form>
  </td>
  <td align="center">
      <a target="_blank" 
         href="item-link.php?line_item_id={$id}&action=view">View</a>
  </td>
</tr>

EOF;

    }

    print <<<EOF
</table>
</div>

EOF;

}

// Add LineItem, nasty hack until we get PHP4-array-functions
function addLineItem($purchase_order_id,
                     $product_number,
                     $description,
                     $type,
                     $quantity_received,
                     $quantity) {

    $line_item = new LineItem;
    
    $line_item->created = date("Y-m-d H:i:s");
    $line_item->purchase_order_id = $purchase_order_id;
    $line_item->product_number = $product_number;
    $line_item->description = $description;
    $line_item->type = $type;
    $line_item->quantity_received = $quantity_received;
    $line_item->quantity = $quantity;

    $line_item->addToDB();

}

// Update a line_item in the database
function updateLineItem($form) {

    $line_item = new LineItem;
    $line_item->getFromDB($line_item_id);

    $changed = array();

    // If type changes, all item links are
    // invalidated; delete them.

    if ($form['type'] != '') {
        if ($line_item->type != $form['type']) {
            deleteItemLinks($line_item_id);
        }
    }
    foreach ($form as $key => $value) {

        if ($line_item->$key != $value) {
            $line_item->$key = $value;
            array_push($changed, $key);
        }
    }

    $line_item->updateInDB($changed);

}

function deleteLineItem($id) {
    
    $line_item = new LineItem;
    $line_item->deleteFromDB($id);
    
}     

include_once(INCDIR . "/includes_computers.php");
include_once(INCDIR . "/includes_monitors.php");
include_once(INCDIR . "/includes_networking.php");
include_once(INCDIR . "/includes_peripherals.php");
include_once(INCDIR . "/includes_software.php");

function itemLinkHeader($product_number) {

    GLOBAL $cfg_install;

    print <<<EOF
<html>
<head>
  <title>Line Item {$production_number}</title>
  <link rel="stylesheet" type="text/css" 
        href="{$cfg_install['root']}/inc/styles.css">

<script type='text/javascript'>


function go() {

    var height = 500;
    var width = 500;      //Set width
    var name = "item"; //Set window name
    var top = 20;         //Set distance from top
    var left = 20;        //Set distance from bottom

    if (document.URL.indexOf('newwin') == -1)  {

      newurl = document.URL

      if (newurl.indexOf('?') == -1) {
          newurl = newurl + "?newwin=true";
      } else {
          newurl = newurl + "&newwin=true"; 
      }

      w = open(newurl,
               name, 
               "fullscreen=no,toolbar=no,status=no," +
               "menubar=no,scrollbars=yes,resizable=yes,directories=no," +
               "location=no,width=" + width + ",height=" + height + 
               ",left=" + left + ",top=" + top)
      self.close();
    }

    return true;

}

</script>

</head>
<body onLoad="javascript:void go()">

  <div align="center">
    <h2>Line Item {$product_number}</h2>
  </div>

EOF;

}

function itemLinkFooter() {

    print <<<EOF

  <br/>

  <div align="center">
    <input type="submit" onClick="javascript:window.close(); return true;"
           name="close" value="Close">
  </div>

</body>
</html>
EOF;

}

function deleteItemLinks($line_item_id) {

    $sql = "DELETE from item_link where line_item_id = {$line_item_id}";
    $db = new DB;
    $db->query($sql);

}

function addItemLink($line_item_id, $target_id) {

    $sql = <<<EOF
INSERT INTO 
    item_link (line_item_id, target_id)
VALUES 
    ($line_item_id, $target_id)
EOF;

    $db = new DB;
    $db->query($sql);

}

function deleteItemLink($item_link_id) {

    $sql = "DELETE from item_link where item_link_id = {$item_link_id}";
    $db = new DB;
    $db->query($sql);

}

function getItemLinkIdsForType($type) {

    $sql = <<<EOF
SELECT
    i.target_id as id
FROM
    item_link i,
    line_item l
WHERE
    l.type = '{$type}' AND
    l.line_item_id = i.line_item_id
EOF;

    $db = new DB;

    $rs = $db->query($sql);
    $count = $db->numrows($rs);

    $ids = array();

    for ($i = 0; $i < $count; $i++) {
        array_push($ids, $db->result($rs, $i, 'id'));
    }

    return $ids;

}

function getNumberAssociated($line_item_id) {

    $sql = <<<EOF
SELECT
    count(*) as count
FROM
    item_link
WHERE
    line_item_id = {$line_item_id}
EOF;

    $db = new DB;
    $rs = $db->query($sql);

    return $db->result($rs, 0, 'count');

}

function showItemLinks($target, $line_item_id) {


    $line_item = new LineItem;
    $line_item->getfromDB($line_item_id);

    itemLinkHeader($line_item->product_number);

    $quantity = $line_item->quantity;
    $quantity_received = $line_item->quantity_received;
    $type = $line_item->type;

    $associated = getNumberAssociated($line_item_id);

    $howmany = "{$associated} of {$quantity_received}";

    if ($associated == $quantity_received) {
        $howmany = 'all';
    }

    print <<<EOF
  <div align="center">
    <h4>You have associated {$howmany} received pieces</h4>
    <p>({$quantity} total pieces in this line item)</p>
    <table border="0" cellspacing="1" cellpadding="3">
    <tr>
      <th>Associated with</th>
      <th>Information</th>
      <th>Action</th>
    </tr>

EOF;

    $sql = <<<EOF
SELECT
    item_link_id,
    line_item_id,
    target_id
FROM
    item_link
WHERE
    line_item_id = {$line_item_id}
EOF;

    $db = new DB;
    $rs = $db->query($sql);
    $count = $db->numrows($rs);

    // Hold names of items already added so we don't
    // show them in the 'Create new Association' list

    $already_added = array();

    for ($i = 0; $i < $count; $i++) {

        $item_link_id = $db->result($rs, $i, 'item_link_id');
        $id = $db->result($rs, $i, 'target_id');

        $item = null;
        $name = "";
        $label = "";

        switch ($type) {
            case 'computers':
                $item = new Computer();
                $item->getfromDB($id, 0);
                $name = $item->fields['name'];
                $info = "Mod: {$item->fields['type']}<br/>" .
                        "SS1: {$item->fields['serial']}<br/>" .
                        "SS2: {$item->fields['otherserial']}<br/>" .
                        "Loc: {$item->fields['location']}";
                $label = "Computer";
            break;
            case 'networking':
                $item = new Netdevice();
                $item->getfromDB($id);
                $name = $item->fields['name'];
                $info = "Mod: {$item->fields['type']}<br/>" .
                        "SS1: {$item->fields['serial']}<br/>" .
                        "SS2: {$item->fields['otherserial']}<br/>" .
                        "Loc: {$item->fields['location']}";
                $label = "Network Device";
            break;
            case 'software':
                $item = new Software();
                $item->getfromDB($id);
                $name = $item->fields['name'];
                $info = "Ver: {$item->fields['version']}<br/>" .
                        "&nbsp;OS: {$item->fields['platform']}<br/>" .
                        "Loc: {$item->fields['location']}";
                $label = "Software";
            break;
            case 'monitors':
                $item = new Monitor();
                $item->getfromDB($id);
                $name = $item->fields['name'];
                $info = "Mod: {$item->fields['type']}<br/>" .
                        "SS1: {$item->fields['serial']}<br/>" .
                        "SS2: {$item->fields['otherserial']}<br/>" .
                        "Siz: {$item->fields['size']}&quot;<br/>" .
                        "Loc: {$item->fields['location']}";
                $label = "Monitor";
            break;
            case 'peripherals':
                $item = new Peripheral();
                $item->getfromDB($id);
                $name = $item->fields['name'];
                $info = "Mod: {$item->fields['type']}<br/>" .
                        "SS1: {$item->fields['serial']}<br/>" .
                        "SS2: {$item->fields['otherserial']}<br/>" .
                        "Loc: {$item->fields['location']}";
                $label = "Peripheral";
            break;
        }

        $already_added["{$id}:{$type}"] = 1;

        print <<<EOF
    <tr>
      <td class="tdsc"><b>{$label}:</b><br/>{$name}</td>
      <td class="tdsl">{$info}</td>
      <td class="tdsc">
        <br/>
        <form action="{$target}" method="GET">
          <input type="hidden" name="newwin" value="true">
          <input type="hidden" name="line_item_id" value="{$line_item_id}">
          <input type="hidden" name="item_link_id" value="{$item_link_id}">
          <input type="hidden" name="action" value="delete">
          <input type="submit" name="submit" 
                 class="tdsc" value="Delete">
        </form>
      </td>
    </tr>
EOF;
    }

    if ($count < 1) {

        print <<<EOF
    <tr><td class="tdsc" colspan="3">No assocations found</td</tr>
EOF;
    }

    // Show 'new association widget' if we haven't already
    // associated as many items as we have received


    $ids = join(',', getItemLinkIdsForType($type));

    if ($ids == "") {

        // None associated, so get all available
        if ($quantity_received > 0) {
            $ids = 'id > -1';
        }
    }

    $sql = <<<EOF
SELECT 
    * 
FROM 
    {$type} 
WHERE
    id NOT IN ($ids)
ORDER BY 
    location, name
EOF;

    $trs = $db->query($sql);

    $target_count = 0;

    if ($trs != null) {
        $target_count = $db->numrows($trs);
    }

    //  If we have associatable items and we haven't associated all items 
    //  in this line item, show an add association form.

    if ($count >= $quantity_received) {
        print <<<EOF
    <tr>
      <td colspan="3" align="center">Line item complete</th>
    </tr>

EOF;
    } else if ($target_count == 0) {

        $msg = 'No items of this type left to associate';

        if ($quantity_received == 0) {
            $msg = "Can't associate, no items received";
        }

        print <<<EOF
    <tr>
      <td colspan="3" align="center">{$msg}</td>
    </tr>

EOF;

    } else {

        print <<<EOF
    <tr>
      <th colspan="3">Create new Association</th>
    </tr>

EOF;

        $names = "";

        for ($i = 0; $i < $target_count; $i++) {

            $info = "";

            $name = $db->result($trs, $i, 'name');
            $location = $db->result($trs, $i, 'location');

            switch ($type) {
                case 'computers':
                case 'networking':
                case 'peripherals':
                    $target_type = $db->result($trs, $i, 'type');
                    $info = "{$location} - {$name} - {$target_type}";
                break;
                case 'monitors':
                    $target_type = $db->result($trs, $i, 'type');
                    $size = $db->result($trs, $i, 'size');
                    $info = "{$location} - {$name} - {$target_type} - {$size}&quot;";
                break;
                case 'software':
                    $platform = $db->result($trs, $i, 'platform');
                    $version = $db->result($trs, $i, 'version');
                    $info = "{$location} - {$name} - {$platform} - {$version}";
                break;
            }

            $id = $db->result($trs, $i, 'id');

            if ($already_added["{$id}:{$type}"] != 1) {

                $names .= <<<EOF
            <option value="{$id}">{$info}</option>

EOF;
            }

        }

        print <<<EOF
    <tr>
      <td colspan="3" align="center">
        <br/>
        <form name="create" method="GET" action="{$target}">
          <select name="target_id">
$names
          </select>
          <input type="hidden" name="newwin" value="true">
          <input type="hidden" name="type" value="{$type}">
          <input type="hidden" name="action" value="create">
          <input type="hidden" name="line_item_id" value="{$line_item_id}">
          <input type="submit" name="submit" class="tdsc" value="Add">
       </form>
      </td>
    </tr>

EOF;

    }

    print <<<EOF
    </table>
  </div>

EOF;

    itemLinkFooter();
}

?>
Return current item: IRMA - Asset Inventory and Job Tracking