<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Billing_festival_function extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('billing/festival_function_model', '', TRUE);
        $this->load->model('login_details_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index($id = 5) {
        $data = array();
        $data['results'] = $this->festival_function_model->list_all();
        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');
        
        $actions = $this->actions();
        $service_menu = get_billing_controller($actions['controller']);
        $data['add_btn']    = 'Create New ' . $service_menu->service_name;
        $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_festival_function/print_list/';
        $data['title'] = $service_menu->service_name;
        
        $data['menu_id'] = $id;
        $data['title'] = $service_menu->service_name;
        $data['service_menu_id'] = array('service_menu_id' => $service_menu->id, 'menu_type' => '4');
        $this->render($data, 'billing/festival_function/list');
    }
    
    public function add($type_id= '') {
        $data = array();
        $actions = $this->actions();
        $service_menu = get_billing_controller($actions['controller']);
        $data['devotees'] = $this->festival_function_model->get( TBL_DEVOTEE, '', array('order_by' => 'name') );
        $data['deity'] = $this->festival_function_model->get( TBL_GOD_NAME,'', array('order_by' => 'name') );
        $data['fn_types'] = $this->festival_function_model->get( TBL_SERVICE, array( 'menu_name' => $type_id ) );
        $data['stars'] = $this->festival_function_model->get( TBL_STAR, '', array('order_by' => 'name') );
        $data['service_type_id'] = $type_id;
        $data['ptypes'] = $this->festival_function_model->get( TBL_PAY_TYPE );
        $data['prasadams'] = $this->festival_function_model->get( TBL_PRASADAM,'', array('order_by' => 'name') );
        
        $data['form_action'] = $actions['insert'];
        $data['title'] = $service_menu->service_name;
        $this->render($data, 'billing/festival_function/add');
    }
    
    private function get_festival_url_key($name, $id = '') {
        $url_key = get_url_key($name);
        if ($this->festival_function_model->festival_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_festival_url_key($name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function insert() {
        $actions = $this->actions();
        $serviceMenu = get_billing_controller($actions['controller']);
        
        $gst_details = $this->login_details_model->get_amount_by_service_id($this->input->post('fn_type'));
        
        if($gst_details->gst == 1) {
            $gst = get_general_setting()->gst; 
            $gst_amount = ($gst / 100) * ($this->input->post('cs_amount'));
        } else {
            $gst = '0.00';
            $gst_amount = '0.00';
        }
        
        $insert = array(
            'type_id' => $this->input->post('type_id'),
            'service_id' => $this->input->post('fn_type'),
            'date' => date('Y-m-d',strtotime($this->input->post('fn_date'))),
            'from_time' => date('H:i',strtotime($this->input->post('fn_from_time'))),
            'to_time' => date('H:i',strtotime($this->input->post('fn_to_time'))),
            'deity_id' => $this->input->post('fn_deity'),
            'period' => $this->input->post('fn_period'),
            'devotee_name1' => $this->input->post('ct_dev_name_1'),
            'tamil_name1' => htmlspecialchars( $this->input->post('ct_dev_name_1_tamil') ),
            'devotee_name2' => $this->input->post('ct_dev_name_2'),
            'tamil_name2' => htmlspecialchars( $this->input->post('ct_dev_name_2_tamil') ),
            'star1' => $this->input->post('ct_star_1'),
            'star2' => $this->input->post('ct_star_2'),
            'contact_no' => $this->input->post('ct_contact_no'),
            'email' => $this->input->post('ct_email'),
            'address' => $this->input->post('ct_address'),
            'desc' => $this->input->post('cs_desc'),
            'amount' => $this->input->post('cs_amount'),
            'gst' => $gst,
            'gst_value' => $gst_amount,
            'book_status' => (!empty($this->input->post('book-now'))) ? 2 : 1,
            'booked_date' => date('Y-m-d H:i:s'),
            'url_key' => $this->get_festival_url_key($this->input->post('ct_dev_name_1')),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $bk_status = $insert['book_status'];
        /*
        * Get last Receipt ID
        */
        $wh = array(
            'service_id'    => $insert['service_id'] ,
            'book_status'   => $bk_status
        );

        $receipt_no = generate_rec_no(TBL_BILL_FESTIVAL_FUNCTION, $wh);
        $insert['receipt_no'] = $receipt_no;   
        
        
        //$services = $this->festival_function_model->service_type_exists($this->input->post('fn_type'), $bk_status);
        
        if(!empty($this->input->post('book-now'))) {
            $insert['updated_on'] = date('Y-m-d H:i:s');
        }
        
        $insert_id = $this->festival_function_model->insert($insert);
        
        if ($insert_id > 0) {
            $this->session->set_flashdata('success', 'Billing for ' . $serviceMenu->service_name . ' is added successfully');
            /*if($services->num_rows() > 0) {
                $last_receipt_no = $services->row()->receipt_no;
                $last_no = (int)$last_receipt_no;
                $receipt_no = (($last_no + 1)  > 9999) ? ($last_no + 1)  : sprintf('0%03d', ($last_no + 1) );
            } else
                $receipt_no = '0001';*/
            
            /*$update = array(
                'receipt_no' => $receipt_no
            );
            $this->festival_function_model->update($update, $insert_id);*/
            
            $deity = $this->input->post('pr_deity');
            $qty = $this->input->post('pr_qty');
            $price = $this->input->post('pr_price');
            $prasadam = $this->input->post('pr_prasadam');
            if(!empty($deity)) {
                foreach( $deity as $k => $deity_id ) {
                    $sub_total = $qty[$k] * $price[$k];
                    $insert = array(
                        'festival_id'   => $insert_id,
                        'deity_id'      => $deity_id,
                        'prasadam'      => $prasadam[$k],
                        'quantity'      => $qty[$k],
                        'price'         => $price[$k],
                        'sub_total'     => $sub_total,
                        'status'        => 1,
                        'url_key'       => $this->get_festival_url_key($prasadam[$k]),
                        'created_by'    => $this->user_data['user_id'],
                        'created_on'    => date('Y-m-d H:i:s')
                    );
                    $this->festival_function_model->insert_prasadam($insert);
                }
            }
            
            if(empty($this->input->post('book-now'))) {
                $pay_type = $this->input->post('pay_type');
                
                foreach( $pay_type as $key => $value ) {
                    if($value != '') {
                        
                        $this->db->select('slug');
                        $q = $this->db->get_where( TBL_PAY_TYPE, array('id' => $key) );
                        $prow = $q->row_array();
                   
                        $pay_details = array();
                        if( $prow['slug'] == 'cheque' && !empty($value) ) :
                            // Cheque
                            $e = explode('-', $this->input->post('chq_date'));
                            $pay_details = array(
                                'name'   => $this->input->post('chq_bnk_name'),
                                'no'   => $this->input->post('chq_no'),
                                'date'   => $e[2].'-'.$e[1].'-'.$e[0],
                            );
                        endif;
                   
                        $pay_details = json_encode( $pay_details );
                   
                        $insert = array(
                            'pay_ref_type'  => 5,
                            'pay_ref_id'    => $insert_id,
                            'pay_type_id'   => $key,
                            'pay_amount'    => $value,
                            'pay_details'   => $pay_details,
                            'status'        => 1,
                            'created_by'    => $this->user_data['user_id'],
                            'created_on'    => date('Y-m-d H:i:s')
                        );
                        $this->festival_function_model->insert_payment($insert);
                    }
                }
                
                /*** To Print Receipt - Start ***/
                $service_menu = $this->login_details_model->get_service_menu_by_id($this->input->post('type_id'));
                $prefix = get_general_setting()->prefix;
                $festival = $this->festival_function_model->get_by_id($insert_id);
                $prasadam = $this->festival_function_model->get_prasadam_by_id($insert_id);
                
                $gods = $prasadams= $print_body = array();
                foreach( $prasadam as $k => $details ) :
                
                    if( !array_key_exists( $details->deity_id, $gods ) ) :
                        $q  =$this->db->get_where( TBL_GOD_NAME, array('id' => $details->deity_id) );
                        $gods[$details->deity_id] = $q->row_array();
                    endif;
                    
                    if( !array_key_exists( $details->prasadam, $prasadams ) ) :
                        $q  =$this->db->get_where( TBL_PRASADAM, array('id' => $details->prasadam) );
                        $prasadams[$details->prasadam] = $q->row_array();
                    endif;
                    
                    $print_body[] = array(
                        (!empty($details->deity_id) ? ($gods[$details->deity_id]['name'] . ((!empty($gods[$details->deity_id]['tamil_name'])) ?  ' / <span class="linear-lbl lang-tamil">' . $gods[$details->deity_id]['tamil_name'] .'</span>' : '')) : '<center> - </center>'),
                        $prasadams[$details->prasadam]['name'] . ((!empty($prasadams[$details->prasadam]['tamil_name'])) ?  ' / <span class="linear-lbl lang-tamil">' . $prasadams[$details->prasadam]['tamil_name'] .'</span>' : ''),
                        '<div style="text-align: right;">'.$details->quantity . ' x ' . $details->price .'</div>',
                        '<div style="text-align: right;">'.number_format( $details->quantity*$details->price, 2 ).'</div>',
                    );
                endforeach;
                
                $args = array(
                    'type'      => '1', // Default = 1 // Normal (Double print), '2' => Archanai,   '3' = Hall Booking | Wedding Hall
                    'rec_no'    => (($prefix == 1) ? $gst_details->prefix : '') . $receipt_no,
                    'title'     => $service_menu->service_name . ' - ' . $gst_details->name,
                    'amount'    => array($this->input->post('cs_amount')),
                    'gst'       => $gst_details->gst,
                    'printType' => TRUE,
                    
                    'data'      => array(
                        // Type = 1 && 3 
                        'name'      => array($this->input->post('ct_dev_name_1') .','. $festival->star1_name .','. $this->input->post('ct_dev_name_1_tamil') .','. $festival->star_tamil_name1, $this->input->post('ct_dev_name_2') .','. $festival->star2_name .','. $this->input->post('ct_dev_name_2_tamil') .','. $festival->star_tamil_name2),
                        'contact_no'=> $this->input->post('ct_contact_no'),
                        'email'     => $this->input->post('ct_email'),
                        'address'   => $this->input->post('ct_address'),
                        'desc'      => $this->input->post('cs_desc'),
                        
                        'section'  => array(
                            array(
                                'Date<div class="sub-lbl"> தேதி</div>' => date('d/m/Y',strtotime($this->input->post('fn_date'))),
                                'Time<div class="linear-lbl"> நேரம்</div>'      => date('H:i',strtotime($this->input->post('fn_from_time'))) . ' - ' . date('H:i',strtotime($this->input->post('fn_to_time'))),
                                'Period<div class="linear-lbl"> காலம்</div>'    => ($this->input->post('fn_period') == 1) ? 'Morning / காலை' : 'Evening / மாலை',
                                'Deity<div class="linear-lbl"> தெய்வம்</div>'     => $festival->god_name,
                            ),
                        ),
                        
                        'tables' => array(
                            'head'  => array('God Name<div class="sub-lbl">கடவுள் பெயர்<div>', 'Prasadam Name<div class="sub-lbl">பிரசாதம்<div>','Qty x Unit Price($)<div class="sub-lbl">எண்ணிக்கை x ஒன்றின் விலை<div>', 'Amount($)<div class="sub-lbl">தொகை<div>'),
                            'body'  => $print_body,
                        ),
                    ),
                    
                );
                
                print_rec($args);      
                /*** End ***/
            }
        }
        else
            $this->session->set_flashdata('error', 'Billing for ' . $serviceMenu->service_name . ' is not added!');   
        
        if(empty($this->input->post('book-now')))
            redirect('billing_festival_function', 'refresh');

        else
            redirect('billing_festival_function/add/' . $this->input->post('type_id'), 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $actions = $this->actions();
        $service_menu = get_billing_controller($actions['controller']);
        $data['form_action'] = $actions['update'] . '/' . $id;
        
        $data['festival'] = $this->festival_function_model->get_by_id($id);
        $data['paid_amount'] = $this->festival_function_model->total_paid_amount($data['festival']->type_id,$data['festival']->id);
        $data['devotees'] = $this->festival_function_model->get( TBL_DEVOTEE, '', array('order_by' => 'name') );
        $data['deity'] = $this->festival_function_model->get( TBL_GOD_NAME,'', array('order_by' => 'name') );
        $data['fn_types'] = $this->festival_function_model->get( TBL_SERVICE, array( 'menu_name' => $data['festival']->type_id ) );
        $data['stars'] = $this->festival_function_model->get( TBL_STAR, '', array('order_by' => 'name') );
        $data['ptypes'] = $this->festival_function_model->get( TBL_PAY_TYPE );
        $data['prasadams'] = $this->festival_function_model->get( TBL_PRASADAM, '', array('order_by' => 'name') );
        $data['items'] = $this->festival_function_model->get_prasadam_json($id);
        $data['title'] = $service_menu->service_name;
        $this->render($data, 'billing/festival_function/edit');
    }
    
    public function update($id){
        if (empty($id)) {
            show_400_error();
        }
        $actions = $this->actions();
        $serviceMenu = get_billing_controller($actions['controller']);
        
        $festival = $this->festival_function_model->get_by_id($id);
        $gst_details = $this->login_details_model->get_amount_by_service_id($this->input->post('fn_type'));
        
        if($gst_details->gst == 1) {
            $gst = get_general_setting()->gst; 
            $gst_amount = ($gst / 100) * ($this->input->post('cs_amount'));
        } else {
            $gst = '0.00';
            $gst_amount = '0.00';
        }
        
        $update = array(
            'type_id' => $this->input->post('type_id'),
            'service_id' => $this->input->post('fn_type'),
            'date' => date('Y-m-d',strtotime($this->input->post('fn_date'))),
            'from_time' => date('H:i',strtotime($this->input->post('fn_from_time'))),
            'to_time' => date('H:i',strtotime($this->input->post('fn_to_time'))),
            'deity_id' => $this->input->post('fn_deity'),
            'period' => $this->input->post('fn_period'),
            'devotee_name1' => $this->input->post('ct_dev_name_1'),
            'tamil_name1' => htmlspecialchars( $this->input->post('ct_dev_name_1_tamil') ),
            'devotee_name2' => $this->input->post('ct_dev_name_2'),
            'tamil_name2' => htmlspecialchars( $this->input->post('ct_dev_name_2_tamil') ),
            'star1' => $this->input->post('ct_star_1'),
            'star2' => $this->input->post('ct_star_2'),
            'contact_no' => $this->input->post('ct_contact_no'),
            'email' => $this->input->post('ct_email'),
            'address' => $this->input->post('ct_address'),
            'desc' => $this->input->post('cs_desc'),
            'amount' => $this->input->post('cs_amount'),
            'gst' => $gst,
            'gst_value' => $gst_amount,
            'book_status' => (!empty($this->input->post('book-now'))) ? 2 : 1,
            'url_key' => $this->get_festival_url_key($this->input->post('ct_dev_name_1')),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        $receipt_no = $festival->receipt_no;
        $bk_status = $update['book_status'];
        
        if( empty( $receipt_no ) || ($bk_status == "1" && $this->input->post('book_status') == "2" ) ) :
            /*
            * Get last Receipt ID
            */
            $wh = array(
                'service_id'    => $update['service_id'],
                'book_status'   => $bk_status
            );
            
            $receipt_no = generate_rec_no(TBL_BILL_FESTIVAL_FUNCTION, $wh);
            $update['receipt_no'] = $receipt_no;   
            $created_date = date('Y-m-d H:i:s');
            
        else :
            $created_date = $festival->created_on;
        endif;
        
        $affected_rows = $this->festival_function_model->update($update,$id);
        
        if($affected_rows > 0) {
            
            $deity = $this->input->post('pr_deity');
            $qty = $this->input->post('pr_qty');
            $price = $this->input->post('pr_price');
            $prasadam = $this->input->post('pr_prasadam');
            $pr_id = $this->input->post('pr_id');
            
            if(!empty($deity)) {
                foreach( $deity as $k => $deity_id ) {
                    $sub_total = $qty[$k] * $price[$k];
                    if(empty($pr_id[$k])) {
                        $insert = array(
                            'festival_id'   => $id,
                            'deity_id'      => $deity_id,
                            'prasadam'      => $prasadam[$k],
                            'quantity'      => $qty[$k],
                            'price'         => $price[$k],
                            'sub_total'     => $sub_total,
                            'status'        => 1,
                            'url_key'       => $this->get_festival_url_key($prasadam[$k]),
                            'created_by'    => $this->user_data['user_id'],
                            'created_on'    => date('Y-m-d H:i:s')
                        );
                        $this->festival_function_model->insert_prasadam($insert);
                    } else {
                        $update = array(
                            'festival_id'   => $id,
                            'deity_id'      => $deity_id,
                            'prasadam'      => $prasadam[$k],
                            'quantity'      => $qty[$k],
                            'price'         => $price[$k],
                            'sub_total'     => $sub_total,
                            'status'        => 1,
                            'url_key'       => $this->get_festival_url_key($prasadam[$k]),
                            'created_by'    => $this->user_data['user_id'],
                            'created_on'    => date('Y-m-d H:i:s')
                        );
                        $this->festival_function_model->update_prasadam_by_id($update,$pr_id[$k]);
                    }
                }
            }
            
            if((empty($this->input->post('book-now')))) { 
                #$paidtype = $this->festival_function_model->delete_paid_type($this->input->post('type_id'),$id);
                $this->session->set_flashdata('success', 'Billing for ' . $serviceMenu->service_name . ' is updated successfully');
                   
                    $paid_hidden_id = $this->input->post('paid_hidden_id');
                    if(!empty($paid_hidden_id)) {
                        foreach($paid_hidden_id as $k=>$value) :
                            $paidtype = $this->festival_function_model->delete_payment_by_paid_id($value);
                        endforeach;
                    }
                
                    $pay_type = $this->input->post('pay_type');
                    foreach( $pay_type as $key => $value ) {
                        if($value > 0 && $value != '') {
                            $this->db->select('slug');
                            $q = $this->db->get_where( TBL_PAY_TYPE, array('id' => $key) );
                            $prow = $q->row_array();
                           
                            $pay_details = array();
                            if( $prow['slug'] == 'cheque' && !empty($value) ) :
                               // Cheque
                               $e = explode('-', $this->input->post('chq_date'));
                               $pay_details = array(
                                   'name'   => $this->input->post('chq_bnk_name'),
                                   'no'   => $this->input->post('chq_no'),
                                   'date'   => $e[2].'-'.$e[1].'-'.$e[0],
                               );
                            endif;
                           
                            $pay_details = json_encode( $pay_details );
                            
                            $insert = array(
                                'pay_ref_type'  => 5,
                                'pay_ref_id'    => $id,
                                'pay_type_id'   => $key,
                                'pay_amount'    => trim($value),
                                'pay_details'   => $pay_details,
                                'status'        => 1,
                                'created_by'    => $this->user_data['user_id'],
                                'created_on'    => $created_date
                            );
                            $this->festival_function_model->insert_payment($insert);
                        }
                    }
                    
                if($this->input->post('hidden_amount') < $this->input->post('cs_amount') || $festival->book_status == 2) {    
                    /*** To Print Receipt - Start ***/
                    $service_menu = $this->login_details_model->get_service_menu_by_id($this->input->post('type_id'));
                    $prefix = get_general_setting()->prefix;
                    
                    $prasadam = $this->festival_function_model->get_prasadam_by_id($id);
                
                    $gods = $prasadams= $print_body = array();
                    foreach( $prasadam as $k => $details ) :
                    
                        if( !array_key_exists( $details->deity_id, $gods ) ) :
                            $q  =$this->db->get_where( TBL_GOD_NAME, array('id' => $details->deity_id) );
                            $gods[$details->deity_id] = $q->row_array();
                        endif;
                        
                        if( !array_key_exists( $details->prasadam, $prasadams ) ) :
                            $q  =$this->db->get_where( TBL_PRASADAM, array('id' => $details->prasadam) );
                            $prasadams[$details->prasadam] = $q->row_array();
                        endif;
                        
                        $print_body[] = array(
                            (!empty($details->deity_id) ? ($gods[$details->deity_id]['name'] . ((!empty($gods[$details->deity_id]['tamil_name'])) ?  ' / <span class="linear-lbl lang-tamil">' . $gods[$details->deity_id]['tamil_name'] .'</span>' : '')) : '<center> - </center>'),
                            $prasadams[$details->prasadam]['name'] . ((!empty($prasadams[$details->prasadam]['tamil_name'])) ?  ' / <span class="linear-lbl lang-tamil">' . $prasadams[$details->prasadam]['tamil_name'] .'</span>' : ''),
                            '<div style="text-align: right;">'.$details->quantity . ' x ' . $details->price .'</div>',
                            '<div style="text-align: right;">'.number_format( $details->quantity*$details->price, 2 ).'</div>',
                        );
                    endforeach;
                    
                    $args = array(
                        'type'      => '1', // Default = 1 // Normal (Double print), '2' => Archanai,   '3' = Hall Booking | Wedding Hall
                        'rec_no'    => (($prefix == 1) ? $gst_details->prefix : '') . $receipt_no,
                        'title'     => $service_menu->service_name . ' - ' . $gst_details->name,
                        /* 'amount'    => array($this->input->post('cs_amount')), */
                        'amount'    => array($this->input->post('paid_amount')),
                        'gst'       => $gst_details->gst,
                        'printType' => TRUE,
                        
                        'data'      => array(
                            // Type = 1 && 3 
                            'name'      => array($this->input->post('ct_dev_name_1') . ','. $festival->star1_name  .','.  $this->input->post('ct_dev_name_1_tamil') .','. $festival->star_tamil_name1, $this->input->post('ct_dev_name_2') .','. $festival->star2_name .','.  $this->input->post('ct_dev_name_2_tamil') .','. $festival->star_tamil_name2),
                            'contact_no'=> $this->input->post('ct_contact_no'),
                            'email'     => $this->input->post('ct_email'),
                            'address'   => $this->input->post('ct_address'),
                            'desc'      => $this->input->post('cs_desc'),
                            
                            'section'  => array(
                                array(
                                    'Date<div class="sub-lbl"> தேதி</div>' => date('d/m/Y',strtotime($this->input->post('fn_date'))),
                                    'Time<div class="linear-lbl"> நேரம்</div>'      => date('H:i',strtotime($this->input->post('fn_from_time'))) . ' - ' . date('H:i',strtotime($this->input->post('fn_to_time'))),
                                    'Period<div class="linear-lbl"> காலம்</div>'    => ($this->input->post('fn_period') == 1) ? 'Morning / காலை' : 'Evening / மாலை',
                                    'Deity<div class="linear-lbl"> தெய்வம்</div>'     => ((!empty($this->input->post('fn_deity'))) ? $this->festival_function_model->get_deity_name($this->input->post('fn_deity'))->name : ''),
                                ),
                            ),
                            
                            'tables' => array(
                                'head'  => array('God Name<div class="sub-lbl">கடவுள் பெயர்<div>', 'Prasadam Name<div class="sub-lbl">பிரசாதம்<div>','Qty x Unit Price($)<div class="sub-lbl">எண்ணிக்கை x ஒன்றின் விலை<div>', 'Amount($)<div class="sub-lbl">தொகை<div>'),
                                'body'  => $print_body,
                            ),
                        ),
                        
                    );
                    
                    print_rec($args);            
                    /*** End ***/
                }
                
                /*** To Insert Refund Amount - Start ***/
                /* if($this->input->post('hidden_amount') > $this->input->post('cs_amount')) {
                    $insert = array(
                        'menu_id' => $this->input->post('type_id'),
                        'service_id' => $this->input->post('fn_type'),
                        'auto_id' => $id,
                        'pay_type_id' => 1,
                        'amount' => ($this->input->post('hidden_amount') - $this->input->post('cs_amount')),
                        'created_on' => date('Y-m-d H:i:s'),
                        'created_by' => $this->user_data['user_id']
                    );
                    $this->login_details_model->insert_refund_amount($insert);
                    
                } */
                /*** End ***/
            
           } else {
               $this->session->set_flashdata('success', 'Billing for ' . $serviceMenu->service_name . ' is booked successfully');
           }
            
            /*
             * Items
             */
            // Set status for removed Items
            if( !empty($this->input->post('removed-items')) ) :
                $e = explode("-", trim($this->input->post('removed-items'), '-'));
                foreach( $e as $k => $rm_id ) :
                    $update = array('status' => 2);
                    $this->festival_function_model->update_prasadam_by_id($update,$rm_id);
                endforeach;
            endif;
            
        } else 
            $this->session->set_flashdata('error', 'Billing for ' . $serviceMenu->service_name . ' is not updated!');
        
        if(empty($this->input->post('book-now')))
            redirect('billing_festival_function', 'refresh');

        else
            redirect('billing_festival_function/add/' . $this->input->post('type_id'), 'refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $actions = $this->actions();
        $service_menu = get_billing_controller($actions['controller']);
        $data['festival'] = $this->festival_function_model->get_by_id($id);
        $data['paid_amount'] = $this->festival_function_model->total_paid_amount($data['festival']->type_id,$data['festival']->id);
        $data['prasadams'] = $this->festival_function_model->get_prasadam_by_id($id);
        $data['ptypes'] = $this->festival_function_model->get_paid_type_by_id($data['festival']->type_id,$id);
        $data['title'] = $service_menu->service_name;
        $this->render($data, 'billing/festival_function/view');
    }
    
    public function delete($id = '', $menu_id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $actions = $this->actions();
        $service_menu = get_billing_controller($actions['controller']);
        
        $update = array ( 
            'amount' => 0,
            'del_reason' => $this->input->post('hidden_del_reason'),
            'status' => 2,
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );

        if ($this->festival_function_model->update($update,$id) > 0) {
            $this->session->set_flashdata('success', 'Billing for ' . $service_menu->service_name . ' is deleted successfully');
            $festival = $this->festival_function_model->get_by_id($id);
            
            $update = array ( 
                'pay_amount' => 0,
                'updated_by' => $this->user_data['user_id'],
                'updated_on' => date('Y-m-d H:i:s')
            );
            $this->festival_function_model->update_paid_type($update,$festival->type_id,$id);
            $this->festival_function_model->update_refund(array('status' => 2),$festival->type_id,$id);
            
            $update = array ( 
                'sub_total' => 0,
                'status' => 2,
                'updated_by' => $this->user_data['user_id'],
                'updated_on' => date('Y-m-d H:i:s')
            );
            $this->festival_function_model->update_prasadam($update,$id);
        }
           
        else 
            $this->session->set_flashdata('error', 'Billing for ' . $service_menu->service_name . ' is not deleted!');
        
        redirect( 'billing_festival_function/index/' . $menu_id, 'refresh');
    }
    
    public function ajax() {
        $results =  $this->festival_function_model->ajax();
        echo json_encode( $results );
    }
    
    public function get_prasadam_price() {
        $data = $this->festival_function_model->get_prasadam_price($_POST['id']);
        echo json_encode($data);
        exit(0);
    }
    
    public function print_list($id,$menu_id) {
        $festival = $this->festival_function_model->get_by_id($id);
        $prasadam = $this->festival_function_model->get_prasadam_by_id($id);
        
        /*** To Print Receipt - Start ***/
        $service_menu = $this->login_details_model->get_service_menu_by_id($festival->type_id);
        $gst_details = $this->login_details_model->get_amount_by_service_id($festival->service_id);
        $prefix = get_general_setting()->prefix;
                
        $gods = $prasadams= $print_body = array();
        foreach( $prasadam as $k => $details ) :
        
            if( !array_key_exists( $details->deity_id, $gods ) ) :
                $q  =$this->db->get_where( TBL_GOD_NAME, array('id' => $details->deity_id) );
                $gods[$details->deity_id] = $q->row_array();
            endif;
            
            if( !array_key_exists( $details->prasadam, $prasadams ) ) :
                $q  =$this->db->get_where( TBL_PRASADAM, array('id' => $details->prasadam) );
                $prasadams[$details->prasadam] = $q->row_array();
            endif;
            
            $print_body[] = array(
                (!empty($details->deity_id) ? ($gods[$details->deity_id]['name'] . ((!empty($gods[$details->deity_id]['tamil_name'])) ?  ' / <span class="linear-lbl lang-tamil">' . $gods[$details->deity_id]['tamil_name'] .'</span>' : '')) : '<center> - </center>'),
                $prasadams[$details->prasadam]['name'] . ((!empty($prasadams[$details->prasadam]['tamil_name'])) ?  ' / <span class="linear-lbl lang-tamil">' . $prasadams[$details->prasadam]['tamil_name'] .'</span>' : ''),
                '<div style="text-align: right;">'.$details->quantity . ' x ' . $details->price .'</div>',
                '<div style="text-align: right;">'.number_format( $details->quantity*$details->price, 2 ).'</div>',
            );
        endforeach;
        
        $args = array(
            'type'      => '1', // Default = 1 // Normal (Double print), '2' => Archanai,   '3' = Hall Booking | Wedding Hall
            'rec_no'    => (($prefix == 1) ? $gst_details->prefix : '') . $festival->receipt_no,
            'title'     => $service_menu->service_name . ' - ' . $gst_details->name,
            'amount'    => array($festival->amount),
            'gst'       => $gst_details->gst,
            'status'    => $festival->status,
            'printType' => TRUE,
            
            'data'      => array(
                // Type = 1 && 3 
                'name'      => array($festival->devotee_name1 . ','. $festival->star1_name  .','.  $festival->tamil_name1 .','. $festival->star_tamil_name1, $festival->devotee_name2 .','. $festival->star2_name  .','. $festival->tamil_name2 .','. $festival->star_tamil_name2),
                'contact_no'=> $festival->contact_no,
                'email'     => $festival->email,
                'address'   => $festival->address,
                'desc'      => $festival->desc,
                
                'section'  => array(
                    array(
                        'Date<div class="sub-lbl"> தேதி</div>' => date('d/m/Y',strtotime($festival->date)),
                        'Time<div class="linear-lbl"> நேரம்</div>'      => date('H:i',strtotime($festival->from_time)) . ' - ' . date('H:i',strtotime($festival->to_time)),
                        'Period<div class="linear-lbl"> காலம்</div>'    => ($festival->period == 1) ? 'Morning / காலை' : 'Evening / மாலை',
                        'Deity<div class="linear-lbl"> தெய்வம்</div>'     => $festival->god_name,
                    ),
                ),
                
                'tables' => array(
                    'head'  => array('God Name<div class="sub-lbl">கடவுள் பெயர்<div>', 'Prasadam Name<div class="sub-lbl">பிரசாதம்<div>','Qty x Unit Price($)<div class="sub-lbl">எண்ணிக்கை x ஒன்றின் விலை<div>', 'Amount($)<div class="sub-lbl">தொகை<div>'),
                    'body'  => $print_body,
                ),
            ),
            
        );
        
        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_festival_function/index/' . $menu_id, 'refresh');
    }
} ?>

