<?php
/*
  $Id: gv_queue.php,v 1.1.1.1 2002/11/28 20:56:44 wilt Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com
  Copyright (c) 2002 osCommerce

  Gift Voucher System v1.0
  Copyright (c) 2001,2002 Ian C Wilson
  http://www.phesis.org

  Released under the GNU General Public License
*/

  require('includes/application_top.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  $currencies = new currencies();

  if ($HTTP_GET_VARS['action']=='confirmrelease' && isset($HTTP_GET_VARS['gid'])) {
    $gv_query=tep_db_query("select release_flag from gv_queue where uid='".$HTTP_GET_VARS['gid']."'");
    $gv_result=tep_db_fetch_array($gv_query);
    if ($gv_result['release_flag']=='N') {  
      $gv_query=tep_db_query("select customer_id,amount from gv_queue where uid='".$HTTP_GET_VARS['gid']."'");
      if ($gv_resulta=tep_db_fetch_array($gv_query)) {
      $gv_amount=$gv_resulta['amount'];
      $gv_query=tep_db_query("select amount from gv_customer where customer_id='".$gv_resulta['customer_id']."'");
      $customer_gv=false;
      $total_gv_amount=0;
      if ($gv_result=tep_db_fetch_array($gv_query)) {
        $total_gv_amount=$gv_result['amount'];
        $customer_gv=true;
      }    
      $total_gv_amount=$total_gv_amount+$gv_amount;
//die($gv_amount.'   '.$total_gv_amount);
      if ($customer_gv) {
        $gv_update=tep_db_query("update gv_customer set amount='".$total_gv_amount."' where customer_id='".$gv_resulta['customer_id']."'");
      } else {
        $gv_insert=tep_db_query("insert into gv_customer (customer_id, amount) values ('".$gv_resulta['customer_id']."','".$total_gv_amount."')");
      }
       $gv_update=tep_db_query("update gv_queue set release_flag='Y' where uid='".$HTTP_GET_VARS['gid']."'");
      }
    }
  }
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent"><?php echo 'Customers Name'; ?></td>
                <td class="dataTableHeadingContent" align="center"><?php echo 'Order Id'; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo 'Voucher Value'; ?></td>
	          <td class="dataTableHeadingContent" align="right"><?php echo 'Date Purchased'; ?></td>		
                <td class="dataTableHeadingContent" align="center">&nbsp;<?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
<?php
  if ($HTTP_GET_VARS['page'] > 1) $rows = $HTTP_GET_VARS['page'] * 20 - 20;
  $gv_query_raw = "select c.customers_firstname, c.customers_lastname, gv.uid, gv.order_id, gv.amount, gv.date_created from " . TABLE_CUSTOMERS . " c, " . "gv_queue" . " gv " . " where c.customers_id = gv.customer_id and gv.release_flag='N'";

//  $gv_query_raw = "select p.products_id, pd.products_name, pd.products_viewed, l.name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_LANGUAGES . " l where p.products_id = pd.products_id and l.languages_id = pd.language_id order by pd.products_viewed DESC";
  $gv_split = new splitPageResults($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $gv_query_raw, $gv_query_numrows);
  $gv_query = tep_db_query($gv_query_raw);
  while ($gv_list = tep_db_fetch_array($gv_query)) {
    $rows++;

    if (strlen($rows) < 2) {
      $rows = '0' . $rows;
    }
    if (((!$HTTP_GET_VARS['gid']) || (@$HTTP_GET_VARS['gid'] == $gv_list['uid'])) && (!$gInfo)) {
    $gInfo = new objectInfo($gv_list);
    }
if ( (is_object($gInfo)) && ($gv_list['uid'] == $gInfo->uid) ) {
        echo '          <tr class="dataTableRowSelected" onmouseover="this.style.cursor=\'hand\'" onclick="document.location.href=\'' . tep_href_link('gv_queue.php', tep_get_all_get_params(array('gid', 'action')) . 'gid=' . $gInfo->uid . '&action=edit') . '\'">' . "\n";
      } else {
        echo '          <tr class="dataTableRow" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'hand\'" onmouseout="this.className=\'dataTableRow\'" onclick="document.location.href=\'' . tep_href_link('gv_queue.php', tep_get_all_get_params(array('gid', 'action')) . 'gid=' . $gv_list['uid']) . '\'">' . "\n";
      }
?>
                <td class="dataTableContent"><?php echo $gv_list['customers_firstname'] . ' ' . $gv_list['customers_lastname']; ?></td>
                <td class="dataTableContent" align="center"><?php echo $gv_list['order_id']; ?>&nbsp;</td>
                <td class="dataTableContent" align="right"><?php echo $currencies->format($gv_list['amount']); ?>&nbsp;</td>
                <td class="dataTableContent" align="right"><?php echo tep_datetime_short($gv_list['date_created']); ?>&nbsp;</td>
<?php
      if ($gv_list['uid'] == $gInfo->uid) {
?>
                <td class="smallText" align="center">&nbsp;<?php echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif', ''); ?>&nbsp;</td>
<?php
      } else {
?>
                <td class="smallText" align="center">&nbsp;<?php echo '<a href="' . tep_href_link(FILENAME_GV_QUEUE, tep_get_all_get_params(array('gid', 'action', 'x', 'y')) . 'gid=' . $gv_list['uid'], 'NONSSL') . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; ?>&nbsp;</td>
<?php
      }
?>              </tr>
<?php
  }
?>
            </table></td>
          </td>
          <tr>
            <td colspan="4"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr>
                <td class="smallText">&nbsp;<?php echo $gv_split->display_count($gv_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_PRODUCTS); ?>&nbsp;</td>
                <td align="right" class="smallText">&nbsp;<?php echo $gv_split->display_links($gv_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page']); ?>&nbsp;</td>
              </tr>
            </table></td>

          </tr>
        </table></td>
<?php
    switch ($HTTP_GET_VARS['action']) {
      case 'release':
?>   
    <td width="25%" valign="top">
<?php
    $heading = array();
    $heading[] = array('text' => '['.$gInfo->uid.'] '.tep_datetime_short($gInfo->date_created).' '.$currencies->format($gInfo->amount));
    $contents = array();
    $contents[] = array('align' => 'center','text' => '<a href="'.tep_href_link('gv_queue.php','action=confirmrelease&gid='.$gInfo->uid,'NONSSL').'">'.tep_image_button('button_confirm_red.gif','Confirm').'</a>'.
                                  '&nbsp;&nbsp;<a href="'.tep_href_link('gv_queue.php','action=cancel&gid='.$gInfo->uid,'NONSSL').'">'.tep_image_button('button_cancel.gif','Cancel').'</a>');
 $box = new box;
    echo $box->infoBox($heading, $contents);
      break;
      default:
?>   
    <td width="25%" valign="top">
<?php
    $heading = array();
    $heading[] = array('text' => '['.$gInfo->uid.'] '.tep_datetime_short($gInfo->date_created).' '.$currencies->format($gInfo->amount));
    $contents = array();
    $contents[] = array('align' => 'center','text' => '<a href="'.tep_href_link('gv_queue.php','action=release&gid='.$gInfo->uid,'NONSSL').'">'.tep_image_button('button_release.gif','Release Gift Voucher').'</a>');
 $box = new box;
    echo $box->infoBox($heading, $contents);
   }
?>
      </tr>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
