<?php
include_once('inc.function.php');

$MESSAGE_FAILURE =   '<blockquote>
                      <h1>
                          <div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-remove-sign"></span> It seems that your session is expired, please reselect appropriate date and pax.</div>
                      </h1>
                      <br>
                      <br>
                    </blockquote>';

if(empty($_SESSION['ITEM_SELECTED'])) { header('Location: booking.php'); }
if(empty($_SESSION['START_DATE']) || empty($_SESSION['END_DATE']) || empty($_SESSION['PAX'])) { $_SESSION['MESSAGE'] = $MESSAGE_FAILURE; header('Location: index.php'); die(); }


if($_SESSION['EXISTING'] == 1) {
    header('Location: booking_submission_process2.php');
    die();
} 

include_once('confdb.php');
include_once('db.functions.php');

    $row = DB::queryFirstRow("SELECT * FROM `sequence_no` WHERE SEQ_TYPE = %s", 'CUSTOMERS' );
    $CUST_ID = intval($row['SEQ_NO'])+1;
    $CUST_SEQ_NO = sequence(intval($row['CUST_SEQ_NO'])+1);
    $CUST_REF_NO = 'PICC/CUST/'.$_SESSION['EVENT_SEGMENT'].'/'.date('Y').'/'.$CUST_SEQ_NO;
    $_SESSION['CUST_REF_NO'] = $CUST_REF_NO;


$CUST_NAME = strtoupper($_SESSION['CLIENT_NAME']);
$CUST_TEL_1_CODE = $_SESSION['CUST_COUNTRY_CALLING_CODE'];
$CUST_TEL_1 = $_SESSION['CLIENT_TELEPHONE'];
$CUST_TEL_2_CODE = $_SESSION['CUST_COUNTRY_CALLING_CODE'];
$CUST_TEL_2 = $_SESSION['CLIENT_TELEPHONE2'];
$CUST_FAX_1_CODE = $_SESSION['CUST_COUNTRY_CALLING_CODE'];
$CUST_FAX_1 = $_SESSION['CLIENT_FAX'];
$CUST_EMAIL = $_SESSION['CLIENT_EMAIL'];
$PAX = $_SESSION['PAX'];
$START_DATE = $_SESSION['START_DATE'];
$END_DATE = $_SESSION['END_DATE'];
$EVENT_START_DATE = $_SESSION['START_DATE'];
$EVENT_END_DATE = $_SESSION['END_DATE'];
$CUST_ADD_1 = $_SESSION['ADDRESS_1'];
$CUST_ADD_2 = $_SESSION['ADDRESS_2'];
$CUST_COUNTRY = $_SESSION['CUST_COUNTRY'];
$CUST_STATE = $_SESSION['CUST_STATE'];
$CUST_CITY = $_SESSION['CUST_CITY'];
$CUST_POSTCODE = $_SESSION['CUST_POSTCODE'];
$EVENT_TITLE = strtoupper($_SESSION['EVENT_NAME']);
$EVENT_REF = $_SESSION['EVENT_TYPE'];
$CUST_TYPE = $_SESSION['EVENT_SEGMENT']; 
$CUST_COMPANY = strtoupper($_SESSION['COMPANY_NAME']); 
$CUST_BIZ_SECTOR = $_SESSION['EVENT_INDUSTRY'];
$CUST_REG_YEAR = date('Y');
$EVENT_ID = date('Y').$CUST_ID.date('mdhi');
$CUST_REF_EVENT = substr($EVENT_ID,-2);
$EVENT_REMARKS = $_SESSION['EVENT_REMARKS'];

DB::insert('customers',array( 
    'CUST_ID'=> $CUST_ID,
    'CUST_SEQ_NO'=> $CUST_SEQ_NO,
    'CUST_REF_NO'=> $CUST_REF_NO,
    'CUST_NAME'=> $CUST_NAME,
    'CUST_TYPE'=> $CUST_TYPE,
    'CUST_BIZ_SECTOR'=> $CUST_BIZ_SECTOR,
    'CUST_ADD_1'=> $CUST_ADD_1,
    'CUST_ADD_2'=> $CUST_ADD_2,
    'CUST_CITY'=> $CUST_CITY,
    'CUST_POSTCODE'=> $CUST_POSTCODE,
    'CUST_STATE'=> $CUST_STATE,
    'CUST_TEL_1_CODE'=> $CUST_TEL_1_CODE,
    'CUST_TEL_1'=> $CUST_TEL_1,
    'CUST_TEL_2_CODE'=> $CUST_TEL_2_CODE,
    'CUST_TEL_2'=> $CUST_TEL_2,
    'CUST_FAX_1_CODE'=> $CUST_FAX_1_CODE,
    'CUST_FAX_1'=> $CUST_FAX_1,
    'CUST_EMAIL'=> $CUST_EMAIL,
    'CUST_COMPANY'=> $CUST_COMPANY,
    'CUST_REG_YEAR'=> $CUST_REG_YEAR,
    'CUST_COUNTRY'=> $CUST_COUNTRY,
    'CUST_USERNAME'=> $CUST_EMAIL,
    'CUST_PASSWORD'=> $CUST_TEL_1 
));


DB::update('sequence_no',array( 
  'SEQ_NO'=> $CUST_ID,
  'CUST_SEQ_NO'=> $CUST_SEQ_NO
),"SEQ_ID=%i",1);


foreach ( $_SESSION['ITEM_SELECTED'] AS $row_session ) 
{  
                        $FAC_ID = $row_session['ITEM_ID'];
                        $ITEM_DESCRIPTION = $row_session['ITEM_DESCRIPTION'];
                        $SETUP_LAYOUT = $row_session['SETUP_LAYOUT'];
                        $SETUP_CAPACITY = $row_session['SETUP_CAPACITY'];

                        $FACILITY_REF = $row_session['ITEM_ID'];

                            $result_rate = DB::query("SELECT * FROM `config_hall` WHERE FAC_ID = %i",$FAC_ID);
                            foreach ($result_rate as $row_rate) 
                            {
                                switch($_SESSION['EVENT_SEGMENT'])
                                {
                                  case 'C':
                                            $ITEM_UNIT_COST = $row_rate['CORPORATE_RATE'];
                                            break;
                                  case 'I':
                                            $ITEM_UNIT_COST = $row_rate['CORPORATE_RATE'];
                                            break;
                                  case 'G':
                                            $ITEM_UNIT_COST = $row_rate['GOVERNMENT_RATE'];
                                            break;
                                  case 'T':
                                            $ITEM_UNIT_COST = $row_rate['CORPORATE_RATE'];
                                            break;
                                }
                            }


            DB::insert('bookings',array( 
                'EVENT_ID'=> $EVENT_ID, 
                'INTERNAL_BOOKING' => 1,
                'QUOTATION_NO'=> $CUST_REF_NO, 
                'CUSTOMER_REF'=> $CUST_ID, 
                'CUST_SEQ_NO'=> $CUST_SEQ_NO, 
                'CUST_TYPE'=> $CUST_TYPE, 
                'INDUSTRY'=> $CUST_BIZ_SECTOR,
                'FACILITY_REF'=> $FACILITY_REF, 
                'EVENT_REF'=> $EVENT_REF, 
                'EVENT_REMARKS'=> $EVENT_REMARKS, 
                'PAX'=> $PAX, 
                'START_DATE'=> $START_DATE, 
                'END_DATE'=> $END_DATE, 
                'EVENT_START_DATE'=> $EVENT_START_DATE, 
                'EVENT_END_DATE'=> $EVENT_END_DATE, 
                'EVENT_TITLE'=> $EVENT_TITLE, 
                'BOOKING_STATUS_REF'=> 'BS'
            ));

            $bookings_id = DB::insertId();

            DB::insert('bookings_quotation',array( 
                'EVENT_ID'=> $EVENT_ID,  
                'BOOKING_ID'=> $bookings_id,  
                'QUOTATION_NO'=> $CUST_REF_NO,  
                'QUOTATION_VERSION'=> 1, 
                'BOOKING_REF'=> $CUST_ID,  
                'BOOKING_SEQ_NO'=> $CUST_SEQ_NO,  
                'ITEM_TYPE'=> 1,  
                'ITEM_ID'=> $FAC_ID,  
                'ITEM_DESCRIPTION'=> $ITEM_DESCRIPTION,  
                'ITEM_UNIT_COST'=> $ITEM_UNIT_COST, 
                'ITEM_QUANTITY'=> 1,  
                'SETUP_LAYOUT'=> $SETUP_LAYOUT, 
                'SETUP_CAPACITY'=> $SETUP_CAPACITY,  
                'ITEM_GROSS_TOTAL'=> $ITEM_UNIT_COST,  
                'ITEM_DISCOUNT_TOTAL'=> 0,  
                'ITEM_NET_TOTAL'=> $ITEM_UNIT_COST,  
                'QUOTATION_STATUS'=> 'NB'
            ));

}
                  
header('Location: booking_complete.php');

?>