<?php

/* 
 * Author       : Jai K
 * Purpose      : The controller for Billing - Hall Booking
 * Created On   : 2016-08-08 18:32
 */

class Billing_Hall_Booking extends REF_Controller {
    public $controller;
    
    function __construct() {
        parent::__construct();
        
        $segments = $this->uri->segments;
        $this->controller = $segments[1];
        
        
        $this->load->model('billing/hall_booking_model');
        $this->load->model('login_details_model');
    }
    
    function index( $id ) {
        
        check_permission();
        $actions = $this->actions();
        
        $data = array(
            'title'          => 'Hall Booked',
            'controller'     => $this->controller,
            'actions'        => $actions,
            
            'success'        => $this->session->flashdata('success'),
            'error'          => $this->session->flashdata('error'),
        );
        
        
        $data['add_btn']    = 'Create New Booking';
        $data['view_link']  = $actions['view'] . '/';
        $data['add_link']   = $actions['add'] . '/'.$id;
        $data['edit_link']  = $actions['edit'] . '/';
        $data['delete_link']= $actions['delete'] . '/';
        $data['print']      = base_url() . 'billing_hall_booking/print_list/';
        
        $data['results'] = $this->hall_booking_model->get_all();
        //echo "<pre>Data: "; print_r( $data['results'] ); die;
        
        $data['menu_id'] = $id;
        $service_menu = get_billing_controller($actions['controller']);
        $data['title'] = $service_menu->service_name;
        $data['service_menu_id'] = array('service_menu_id' => $service_menu->id, 'menu_type' => '4');
        // Load Views
        $this->render( $data, 'billing/hall-booking/index' );
    }
    
    function add( $id ) {
        $this->manipulate( $id );
    }
    
    function edit( $id ) {
        $this->manipulate( $id, array( 'page' => 'edit' ) );
    }
    
    function view( $id ) {
        $this->manipulate( $id, array( 'page' => 'view' ) );
    }
    
    function export( $page = 'pdf', $id ) {
        $this->manipulate( $id, array( 'page' => $page ) );
    }
    
    function manipulate( $id, $args = array() ) {
        $actions = $this->actions();
        $post = $this->input->post();
        $session_data = $this->session->userdata(TBL_PREFIX . 'user_logged_in');
        $token = $this->session->userdata( 'rtoken-'.$session_data['user_id'] );
        
        $page = $page_file_name = !empty( $args['page'] ) ? $args['page'] : 'add';
        $page_file_name         = $page == 'pdf' ? 'view' : $page_file_name;
        
        $data = array(
            'id'                => $id,
            'title'             => 'Hall Booking',
            'is_'.$page         => TRUE,
            'controller'        => $this->controller,
            'actions'           => $actions,
            'form_action'       => base_url() . $this->controller .'/'.$page.'/'.$id,
            'success'           => $this->session->flashdata('success'),
            'error'             => $this->session->flashdata('error'),
        );
        
        if( $page != 'add' ) :
            // ID => Page ID
            
            $data['results'] = $this->hall_booking_model->get_all( $id );
            $data['payments'] = $this->hall_booking_model->get_payments( $id );
        endif;
        
        $service_id = $page == 'add' ? $id : $data['results'][0]['service_id'];
        if( $page != 'add' ) :
            $service = $this->hall_booking_model->get( TBL_SERVICE, array( 'id' => $service_id ) );
            $data['service'] = $service[0];
            $menu_id = $service[0]['menu_name'];
        else :
            $menu_id = $service_id;
        endif;
        
        if( $page == 'add' || $page == 'edit' ) :
            // ID => Menu id
            $data['token'] = $this->auto_generation_code( rand( 999, 9999 ) );
            $this->session->set_userdata( 'rtoken-'.$session_data['user_id'], $data['token'] );
            
            
            $data['ptypes']     = $this->hall_booking_model->get( TBL_PAY_TYPE );
            $data['fn_types']   = $this->hall_booking_model->get( TBL_SERVICE, array( 'menu_name' => $menu_id ) );
        
        endif;
        
        //echo "<pre>"; print_r( $post ); echo '------------'. $token;die;
        
        if( !empty( $post['submit'] ) && $post['token'] == $token ) :
            // insert
            $insert_id = $this->hall_booking_model->manipulate();

            $msg = 'Hall is booked successfully.';
            if( $page != 'add' ) :
                $msg = 'Booking is updated successfully.';
            endif;
            $this->session->set_flashdata('success', $msg);


            // Redirect
            redirect( $this->controller . '/index/' . $menu_id );
            //redirect( $data['form_action'] );
        endif;

        
        //  echo "<pre>"; print_r( $data );  die;
        // Load Views
        $this->render( $data, 'billing/hall-booking/'.$page_file_name );
    }
    
    function delete( $id, $menu_id ) {
        $hidden_del_reason = $this->input->post('hidden_del_reason');
        $this->hall_booking_model->delete( $id, $hidden_del_reason );
        
        // Redirect
        $this->session->set_flashdata('success', 'The record is deleted successfully');
        redirect( $this->controller . '/index/' . $menu_id );
        
    }
    
    function ajax() {
        $results =  $this->hall_booking_model->ajax();
       
        echo json_encode( $results );
    }
    
    public function print_list($id,$menu_id) {
        $hall_booking = $this->hall_booking_model->get_by_id($id);
        
        /*
         * Print
         */
        $service_menu = $this->login_details_model->get_service_menu_by_id(7);
        $gst_details = $this->login_details_model->get_amount_by_service_id($hall_booking->service_id);
        $prefix = get_general_setting()->prefix;
        $paidNow = number_format(($hall_booking->bh_amount - $hall_booking->bh_paid_amount),2,'.','');
        $args = array(
            'type'      => '3', // Default = 1 // Normal (Double print), '2' => Kattalai Archanai,   '3' = Hall Booking | Wedding Hall, '4' => 'Madapalli', '5' => Archanai, '6' => Common
            'rec_no'    => (($prefix == 1) ? $gst_details->prefix : '') . $hall_booking->bh_rec_no,
            'title'     => $service_menu->service_name . ' - ' . $gst_details->name,
            'amount'    => array($hall_booking->bh_amount, $hall_booking->bh_paid_amount, $paidNow),
            'gst'       => $gst_details->gst,
            'copy'      => TRUE,
            'status'    => $hall_booking->status,
            'printType' => FALSE,
            
            'data'      => array( 
                
                
                // Type = 1 && 3 && 4
                'name'      => array($hall_booking->bh_name . ( !empty( $hall_booking->bh_tamil_name ) ? ' / '.tamil( $hall_booking->bh_tamil_name ) : '') ),
                'contact_no'=> $hall_booking->bh_contact_no,
                'email'     => $hall_booking->bh_email,
                
                // Type = 1 && 3
                'address'   => $hall_booking->bh_address,
                'desc'      => $hall_booking->bh_desc,
                
                
                // Type = 3 
                'date'      => date('d/m/Y',strtotime($hall_booking->bh_from_date)),
                'time'      => date('H:i',strtotime($hall_booking->bh_from_date)) . ( !empty( $hall_booking->bh_to_date ) ? ' - '.date('H:i',strtotime($hall_booking->bh_to_date)) : '' ),
                'period'    => $hall_booking->bh_period == '1' ? 'Morning / காலை' : 'Evening / மாலை',
                'event_type'=> $hall_booking->bh_evt_type,    
                
            ),
            
            
        );
        
        if(print_rec($args))
            $this->session->set_flashdata('success', 'Receipt has been printed successfully');
        
        else 
            $this->session->set_flashdata('error', 'Receipt has not been printed!');
            
        /*** End ***/
        
        redirect( 'billing_hall_booking/index/' . $menu_id, 'refresh');
    }

    
    
    
} // class    

