<?php
/*
	 
	
	Freeway eCommerce 
	http://www.openfreeway.org
	Copyright (c) 2007 ZacWare
	
	osConcert, Online Seat Booking 
  	https://www.osconcert.com

  	Copyright (c) 2020 osConcert
	
	Released under the GNU General Public License 
*/

// Set flag that this is a parent file
	define( '_FEXEC', 1 );


  require('includes/application_top.php');

  if (!$FSESSION->is_registered('customer_id')) {
    $navigation->set_snapshot();
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
  
  $cart->reset(true);
  $FSESSION->remove('box_office_refund');
  
  if($_SESSION['customer_country_id']==999 ){

  require(DIR_WS_LANGUAGES . $FSESSION->language . '/bor_listings.php');
  
  // -----------------------------
// BOR EXTEND EXPIRY
// -----------------------------
if (
    isset($_POST['action']) &&
    $_POST['action'] == 'extend_bor' &&
    isset($_POST['orders_id']) &&
    isset($_POST['extend_days'])
) {
    $orders_id = (int)$_POST['orders_id'];
    $extend_days = (int)$_POST['extend_days'];

    // safety limits
    if ($orders_id > 0 && in_array($extend_days, array(1, 2, 4, 7, 14))) {

        $order_query = tep_db_query("
            SELECT orders_id, bor_datetime, bor_expiry, date_purchased, orders_status
            FROM " . TABLE_ORDERS . "
            WHERE orders_id = '" . (int)$orders_id . "'
            AND bor_datetime > 0
            AND orders_status = '" . MODULE_PAYMENT_BOR_ORDER_STATUS_ID . "'
            LIMIT 1
        ");

        if (tep_db_num_rows($order_query) > 0) {

            $order = tep_db_fetch_array($order_query);

            $old_bor_datetime = $order['bor_datetime'];

            // Extend from current expiry, not from today
            $new_bor_datetime = date(
                'Y-m-d H:i:s',
                strtotime($old_bor_datetime . ' +' . $extend_days . ' days')
            );

            // Recalculate total expiry days from original order date
            $date_purchased_ts = strtotime($order['date_purchased']);
            $new_expiry_ts = strtotime($new_bor_datetime);

            $new_bor_expiry = ceil(($new_expiry_ts - $date_purchased_ts) / 86400);

            if ($new_bor_expiry < 1) {
                $new_bor_expiry = $extend_days;
            }

            // Update order header
            tep_db_query("
                UPDATE " . TABLE_ORDERS . "
                SET 
                    bor_datetime = '" . tep_db_input($new_bor_datetime) . "',
                    bor_expiry = '" . (int)$new_bor_expiry . "'
                WHERE orders_id = '" . (int)$orders_id . "'
                LIMIT 1
            ");

            // Update reserved products/seats
            tep_db_query("
                UPDATE " . TABLE_ORDERS_PRODUCTS . "
                SET 
                    bor_datetime = '" . tep_db_input($new_bor_datetime) . "',
                    bor_expiry = '" . (int)$new_bor_expiry . "'
                WHERE orders_id = '" . (int)$orders_id . "'
            ");

            // Add history comment
            tep_db_query("
                INSERT INTO " . TABLE_ORDERS_STATUS_HISTORY . "
                (orders_id, orders_status_id, date_added, customer_notified, comments)
                VALUES (
                    '" . (int)$orders_id . "',
                    '" . MODULE_PAYMENT_BOR_ORDER_STATUS_ID . "',
                    now(),
                    '0',
                    'BOR expiry extended by Box Office. Old expiry: " . tep_db_input($old_bor_datetime) . ". New expiry: " . tep_db_input($new_bor_datetime) . ".'
                )
            ");
        }
    }

    tep_redirect(tep_href_link('bor_listings.php', 'edit=' . (int)$orders_id, 'SSL'));
}
  	  // delete old orders
	  
	  //get seatplan class
	  require(DIR_WS_CLASSES.'seatplan.php');
      $sp = new seatplan;
	  //cancel aged orders
	$time_now = date('Y-m-d H:i:s',getServerDate(false));

	  $bor_query = tep_db_query("select orders_id from " . TABLE_ORDERS . " 
									where bor_datetime <= '" . $time_now . "' 
									AND bor_datetime > 0
									AND orders_status = '".MODULE_PAYMENT_BOR_ORDER_STATUS_ID."'");
		if (tep_db_num_rows($bor_query) > 0 ) { 
		while ($bor_results = tep_db_fetch_array($bor_query)){
			$sp->canx_order($bor_results['orders_id'],TEXT_AUTO_RESTOCK);
		}
		}



  $content = 'bor_listings';
  $javascript = $content . '.js';

  require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);

  require(DIR_WS_INCLUDES . 'application_bottom.php');
  } else { exit(BOR_ERROR);}
?>
