<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );

  require('includes/application_top.php');

  require(DIR_WS_CLASSES . 'currencies.php');
  $currencies = new currencies();
  $server_date = getServerDate(true);	

  $orders_statuses = array();
  $orders_status_array = array();
  $orders_status_query = tep_db_query("select orders_status_id, orders_status_name from " . TABLE_ORDERS_STATUS . " where language_id = '" . (int)$languages_id . "'");
  while ($orders_status = tep_db_fetch_array($orders_status_query)) {
    $orders_statuses[] = array('id' => $orders_status['orders_status_id'],
                               'text' => $orders_status['orders_status_name']);
    $orders_status_array[$orders_status['orders_status_id']] = $orders_status['orders_status_name'];
  }

  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');

  if (tep_not_null($action)) {
    switch ($action) {
      case 'update_order':
        $oID = tep_db_prepare_input($HTTP_GET_VARS['oID']);
        $status = tep_db_prepare_input($HTTP_POST_VARS['status']);
        $comments = tep_db_prepare_input($HTTP_POST_VARS['comments']);

        $order_updated = false;
        $check_status_query = tep_db_query("select customers_name, customers_email_address, orders_status, date_purchased from " . TABLE_ORDERS . " where orders_id = '" . (int)$oID . "'");
        $check_status = tep_db_fetch_array($check_status_query);
// BOF: WebMakers.com Added: Downloads Controller
// always update date and time on order_status
// original        if ( ($check_status['orders_status'] != $status) || tep_not_null($comments)) {
			/*if ($status==1){
			  tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set events_type='P' where orders_id='" . tep_db_input($oID) . "' and products_type='E'");}
			else{
			  tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set events_type='R' where orders_id='" . tep_db_input($oID) . "' and products_type='E'");
			}*/
			 if($check_status['orders_status'] != $status)	
	  		 {
	  			if($check_status['orders_status']==1) //if pending
				{
		   			if($status==2 || $status==3)  // to processing or deliverd
	   		  		tep_db_query("update " . TABLE_ORDERS . " set date_paid = '" . getServerDate(true) . "' where orders_id = '" . (int)$oID . "'");
				}
	  		}
                   if ( ($check_status['orders_status'] != $status) || $comments != '' || ($status ==DOWNLOADS_ORDERS_STATUS_UPDATED_VALUE) ) {
          tep_db_query("update " . TABLE_ORDERS . " set orders_status = '" . tep_db_input($status) . "', last_modified ='" . $server_date . "' where orders_id = '" . (int)$oID . "'");
        $check_status_query2 = tep_db_query("select customers_name, customers_email_address, orders_status, date_purchased from " . TABLE_ORDERS . " where orders_id = '" . (int)$oID . "'");
        $check_status2 = tep_db_fetch_array($check_status_query2);
      if ( $check_status2['orders_status']==DOWNLOADS_ORDERS_STATUS_UPDATED_VALUE ) {
        tep_db_query("update " . TABLE_ORDERS_PRODUCTS_DOWNLOAD . " set download_maxdays = '" . tep_get_configuration_key_value('DOWNLOAD_MAX_DAYS') . "', download_count = '" . tep_get_configuration_key_value('DOWNLOAD_MAX_COUNT') . "' where orders_id = '" . (int)$oID . "'");
      }
// EOF: WebMakers.com Added: Downloads Controller

          $customer_notified = '0';
          if (isset($HTTP_POST_VARS['notify']) && ($HTTP_POST_VARS['notify'] == 'on')) {
            $notify_comments = '';
// BOF: WebMakers.com Added: Downloads Controller - Only tell of comments if there are comments
            if (isset($HTTP_POST_VARS['notify_comments']) && ($HTTP_POST_VARS['notify_comments'] == 'on')) {
              $notify_comments = sprintf(EMAIL_TEXT_COMMENTS_UPDATE, $comments) . "\n\n";
            }
// EOF: WebMakers.com Added: Downloads Controller
            $email = STORE_NAME . "\n" . EMAIL_SEPARATOR . "\n" . EMAIL_TEXT_ORDER_NUMBER . ' ' . $oID . "\n" . EMAIL_TEXT_INVOICE_URL . ' ' . tep_catalog_href_link(FILENAME_CATALOG_ACCOUNT_HISTORY_INFO, 'order_id=' . $oID, 'SSL') . "\n" . EMAIL_TEXT_DATE_ORDERED . ' ' . tep_date_long($check_status['date_purchased']) . "\n\n" . $notify_comments . sprintf(EMAIL_TEXT_STATUS_UPDATE, $orders_status_array[$status]);

            tep_mail($check_status['customers_name'], $check_status['customers_email_address'], EMAIL_TEXT_SUBJECT, $email, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
			
            $customer_notified = '1';
          }
			$user_name=tep_admin_name($login_id);
          tep_db_query("insert into " . TABLE_ORDERS_STATUS_HISTORY . " (orders_id, orders_status_id, date_added, customer_notified, comments,user_added) values ('" . (int)$oID . "', '" . tep_db_input($status) . "','" . $server_date . "', '" . tep_db_input($customer_notified) . "', '" . tep_db_input($comments)  . "','" . tep_db_input($user_name) . "')");

          $order_updated = true;
        }

        if ($order_updated == true) {
         $messageStack->add_session(SUCCESS_ORDER_UPDATED, 'success');
        } else {
          $messageStack->add_session(WARNING_ORDER_NOT_UPDATED, 'warning');
        }

        tep_redirect(tep_href_link(FILENAME_EDIT_ORDERS_SUCCESS, tep_get_all_get_params(array('action'))));
        break;
    }
  }

  if (isset($HTTP_GET_VARS['oID'])) {
    $oID = tep_db_prepare_input($HTTP_GET_VARS['oID']);

    $orders_query = tep_db_query("select orders_id from " . TABLE_ORDERS . " where orders_id = '" . (int)$oID . "'");
    $order_exists = true;
    if (!tep_db_num_rows($orders_query)) {
      $order_exists = false;
      $messageStack->add(sprintf(ERROR_ORDER_DOES_NOT_EXIST, $oID), 'error');
    }
  } else {
    tep_redirect(tep_href_link(FILENAME_ORDERS));
  }
// BOF: WebMakers.com Added: Additional info for Orders
// Look up things in orders
$the_extra_query= tep_db_query("select * from " . TABLE_ORDERS . " where orders_id = '" . (int)$oID . "'");
$the_extra= tep_db_fetch_array($the_extra_query);
$the_customers_id= $the_extra['customers_id'];
// Look up things in customers
$the_extra_query= tep_db_query("select * from " . TABLE_CUSTOMERS . " where customers_id = '" . $the_customers_id . "'");
$the_extra= tep_db_fetch_array($the_extra_query);
$the_customers_fax= $the_extra['customers_fax'];
// EOF: WebMakers.com Added: Additional info for Orders

  include(DIR_WS_CLASSES . 'order.php');
?>
<!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">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript"><!--
function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=650,height=500,screenX=150,screenY=150,top=150,left=150')
}
//--></script>
</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>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<?php
  if ($order_exists == true) {
    $order = new order($oID);
?>
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
			<tr>
			<td class="pageHeading"><font class=pageheading_alt><?php  echo TEXT_CREATE_ORDER;?> > </font><font class=pageheading><?php echo HEADING_TITLE;?></font></td>
		</tr>
        </table></td>
      </tr>
      <tr>
        <td><br><table width=100% border="0" cellspacing="0" cellpadding="2" class="datatablerowover">
          <tr>
            <td colspan="3" height="10"></td>
          </tr>
          <tr>
            <td valign="top">
			<table border="0" cellspacing="0" cellpadding="2"  >
              <tr>
                <td class="main" valign="top" nowrap="nowrap"><?php echo ENTRY_CUSTOMER_NAME; ?></td>
                <td class="main" nowrap="nowrap"><b><?php echo $order->customer['name']; ?></b></td>
				<td class="pageHeading" align="right" 
				<?php  if (ACCOUNT_SECOND_EMAIL=='true') 
							echo 'rowspan="3"';
						else
							echo 'rowspan="2"';?>
				 width=80% nowrap="nowrap">
			<?php 
			      if ($HTTP_GET_VARS['mode']!='new')
                     echo '<a href="' . tep_href_link("edit_orders.php", tep_get_all_get_params(array('action'))) . '">' . tep_image_button('button_edit.gif', IMAGE_EDIT) . '</a> &nbsp; '; 
                  echo '<a href="javascript:popupWindow(\'' .  (HTTP_SERVER . DIR_WS_ADMIN . FILENAME_ORDERS_INVOICE) . '?' . (tep_get_all_get_params(array('oID')) . 'oID=' . $HTTP_GET_VARS['oID']) . '\')">' .  tep_image_button('button_invoices.gif', IMAGE_ORDERS_INVOICE) . '</a>&nbsp;<a href="javascript:popupWindow(\'' .  (HTTP_SERVER . DIR_WS_ADMIN . FILENAME_ORDERS_PACKINGSLIP) . '?' . (tep_get_all_get_params(array('oID')) . 'oID=' . $HTTP_GET_VARS['oID']) . '\')">' . tep_image_button('button_packing_slips.gif', IMAGE_ORDERS_PACKINGSLIP) . '</a>' ;
				  
		    ?>
           </td>
              </tr>
              <tr>
                <td class="main" nowrap="nowrap"><?php echo ENTRY_EMAIL_ADDRESS; ?></td>
                <td class="main" nowrap="nowrap"><b><?php echo '<a href="mailto:' . $order->customer['email_address'] . '"><b>' . $order->customer['email_address'] . '</b></a>'; ?></b></td>
              </tr>
         <?php if (ACCOUNT_SECOND_EMAIL=='true') 
		 			{ if($order->customer['second_email_address'] != "") { ?>
              <tr>
                <td class="main" nowrap><?php echo ENTRY_SECOND_EMAIL_ADDRESS; ?></td>
                <td class="main"><b><?php echo '<a href="mailto:' . $order->customer['second_email_address'] . '"><b>' . $order->customer['second_email_address'] . '</b></a>'; ?></b></td>
              </tr>
         <?php  }
		 	} ?>
            </table></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td class="infoBoxHeading"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td><table border="0" cellspacing="0" cellpadding="2" width=100%>
<?php
// BOF: WebMakers.com Added: Show Order Info
?>
<!-- add Order # // -->
      <tr><td class="main"><table border="0" cellspacing="0" cellpadding="2" width=100%>
	<tr bgcolor=white>
	   <td class="main">
	   <?php tep_content_title_top(TEXT_CURRENT_STATUS);?></td></tr>
        <tr>
        <td class="main"><table border="0" cellspacing="0" cellpadding="3" width="100%">
          <tr>
            <td class="smallText" align="left" width="20%"><?php echo TABLE_HEADING_DATE_ADDED; ?></td>
            <td class="smallText" align="center" width="10%" nowrap><?php echo TABLE_HEADING_CUSTOMER_NOTIFIED; ?></td>
            <td class="smallText" align="center" width="10%"><?php echo TABLE_HEADING_STATUS; ?></td>
            <td class="smallText" align="left"  ><?php echo TABLE_HEADING_COMMENTS; ?></td>
			<td class="smallText" align="left" width="10%"><?php echo TABLE_HEADING_USER_ADDED; ?></td>
          </tr>
<?php
    $orders_history_query = tep_db_query("select orders_status_id, date_added, customer_notified, comments,user_added from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . (int)$oID . "' order by date_added");
    if (tep_db_num_rows($orders_history_query)) {
      while ($orders_history = tep_db_fetch_array($orders_history_query)) {
        echo '          <tr class="dataTableRowOver">' . "\n" .
             '            <td class="smallText" align="left">' . date(EVENTS_DATE_FORMAT . ' h:i:s',strtotime($orders_history['date_added'])) . '</td>' . "\n" .
             '            <td class="smallText" align="center">';
        if ($orders_history['customer_notified'] == '1') {
          echo tep_image(DIR_WS_ICONS . 'tick.gif', ICON_TICK) . "</td>\n";
        } else {
          echo tep_image(DIR_WS_ICONS . 'cross.gif', ICON_CROSS) . "</td>\n";
        }
        echo '            <td class="smallText" align="center">' . $orders_status_array[$orders_history['orders_status_id']] . '</td>' . "\n" .
             '            <td class="smallText" align="left">' . $orders_history['comments'] . '&nbsp;</td>' . "\n" .
			 '            <td class="smallText" align="left">' . $orders_history['user_added'] . '&nbsp;</td>' . "\n" .
             '          </tr>' . "\n";
      }
    } else {
        echo '          <tr>' . "\n" .
             '            <td class="smallText" colspan="5">' . TEXT_NO_ORDER_HISTORY . '</td>' . "\n" .
             '          </tr>' . "\n";
    }
?>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '3'); ?></td>
      </tr>
	  <?php tep_content_title_bottom();?>
      <tr bgcolor=white>
	<td class="main"><b><?php tep_content_title_top(TEXT_MODIFY_STATUS); ?></b></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '3'); ?></td>
      </tr>
	  <tr><td><table width="70%">
      <?php echo tep_draw_form('status', FILENAME_EDIT_ORDERS_SUCCESS, tep_get_all_get_params(array('action')) . 'action=update_order'); ?>
          <tr><td class="smallText" width="10%"><?php echo ENTRY_STATUS; ?><td colspan="2"><?php echo tep_draw_pull_down_menu('status', $orders_statuses, $order->info['orders_status']); ?></td></tr>
           <tr>
			  	<td class="smallText" nowrap><?php echo ENTRY_NOTIFY_CUSTOMER; ?></td><td><?php echo tep_draw_checkbox_field('notify', '', true); ?></td>
              	<td class="smallText" align="right"><?php echo ENTRY_NOTIFY_COMMENTS; ?><?php echo tep_draw_checkbox_field('notify_comments', '', true); ?></td>
			</tr>
	      <tr>
			<td class="smallText" valign="top" ><?php echo TABLE_HEADING_COMMENTS; ?></td>
			<td class="smallText" colspan="2"><?php echo tep_draw_textarea_field('comments', 'soft', '80', '5'); ?></td>
	      </tr>
		 </table></td></tr>
		 <?php tep_content_title_bottom();?>
      <tr>
        <td class="contentTitle"><?php echo tep_draw_separator('pixel_trans.gif', '1', '3'); ?></td>
      </tr>
      <tr><td valign="top" align=right><?php 
	  echo '<a href="' . tep_href_link(FILENAME_ORDERS, tep_get_all_get_params(array('action'))) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>&nbsp;'; 
	  echo tep_image_submit('button_update.gif', IMAGE_UPDATE); ?></td></tr>
      </table></td>
      </form></tr>
<?php } ?>
    </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'); ?>
