<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Billing_mega_function extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('billing/mega_function_model', '', TRUE);
        $this->load->model('login_details_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index($id = 10) {
        $data = array();
        $data['results'] = $this->mega_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();
        $data['add_btn']    = 'Create New Mega Function';
        $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_mega_function/print_list/';
        $data['title'] = 'Mega Function';
        
        $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');
        $this->render($data, 'billing/mega_function/list');
    }
    
    public function add($type_id= '') {
        $data = array();
        $actions = $this->actions();
        $data['devotees'] = $this->mega_function_model->get( TBL_DEVOTEE, '', array('order_by' => 'name') );
        $data['deity'] = $this->mega_function_model->get( TBL_GOD_NAME, '', array('order_by' => 'name') );
        $data['fn_types'] = $this->mega_function_model->get( TBL_SERVICE, array( 'menu_name' => $type_id ), array('order_by' => 'name') );
        $data['stars'] = $this->mega_function_model->get( TBL_STAR, '', array('order_by' => 'name') );
        $data['service_type_id'] = $type_id;
        $data['ptypes'] = $this->mega_function_model->get( TBL_PAY_TYPE );
        $data['prasadams'] = $this->mega_function_model->get( TBL_PRASADAM, '', array('order_by' => 'name') );
        
        $data['form_action'] = $actions['insert'];
        $data['title'] = 'Mega Function';
        $this->render($data, 'billing/mega_function/add');
    }
    
    private function get_mega_url_key($name, $id = '') {
        $url_key = get_url_key($name);
        if ($this->mega_function_model->mega_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_mega_url_key($name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function insert() {
        
        $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_mega_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_MEGA_FUNCTION, $wh);
        $insert['receipt_no'] = $receipt_no;   
        
        
        
        if(!empty($this->input->post('book-now'))) {
            $insert['updated_on'] = date('Y-m-d H:i:s');
        }
        
        $insert_id = $this->mega_function_model->insert($insert);
        
        if ($insert_id > 0) {
            $this->session->set_flashdata('success', 'Billing for Mega Function 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->mega_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(
                        'mega_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_mega_url_key($prasadam[$k]),
                        'created_by'    => $this->user_data['user_id'],
                        'created_on'    => date('Y-m-d H:i:s')
                    );
                    $this->mega_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'  => 10,
                            '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->mega_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;
                $mega = $this->mega_function_model->get_by_id($insert_id);
                $prasadam = $this->mega_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') .','. $mega->star1_name  .','.  $this->input->post('ct_dev_name_1_tamil') .','. $mega->star_tamil_name1 , $this->input->post('ct_dev_name_2') .','. $mega->star2_name  .','.  $this->input->post('ct_dev_name_2_tamil') .','. $mega->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>'     => $mega->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 Mega Function is not added!');
        
        if(empty($this->input->post('book-now')))
            redirect('billing_mega_function', 'refresh');

        else
            redirect('billing_mega_function/add/' . $this->input->post('type_id'), 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        
        $data['mega'] = $this->mega_function_model->get_by_id($id);
        $data['paid_amount'] = $this->mega_function_model->total_paid_amount($data['mega']->type_id,$data['mega']->id);
        $data['devotees'] = $this->mega_function_model->get( TBL_DEVOTEE, '', array('order_by' => 'name') );
        $data['deity'] = $this->mega_function_model->get( TBL_GOD_NAME, '', array('order_by' => 'name') );
        $data['fn_types'] = $this->mega_function_model->get( TBL_SERVICE, array( 'menu_name' => $data['mega']->type_id ), array('order_by' => 'name') );
        $data['stars'] = $this->mega_function_model->get( TBL_STAR, '', array('order_by' => 'name') );
        $data['ptypes'] = $this->mega_function_model->get( TBL_PAY_TYPE );
        $data['prasadams'] = $this->mega_function_model->get( TBL_PRASADAM, '', array('order_by' => 'name') );
        $data['items'] = $this->mega_function_model->get_prasadam_json($id);
        $data['title'] = 'Mega Function';
        $this->render($data, 'billing/mega_function/edit');
    }
    
    public  function update($id){
        if (empty($id)) {
            show_400_error();
        }
        
        $mega = $this->mega_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';
        }
        #echo '<pre>';print_r($_POST);die;
        $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_mega_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 = $mega->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_MEGA_FUNCTION, $wh);
            $update['receipt_no'] = $receipt_no;   
            $created_date = date('Y-m-d H:i:s');
            
        else :
            $created_date = $mega->created_on;
        endif;
        
        
        $affected_rows = $this->mega_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(
                            'mega_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_mega_url_key($prasadam[$k]),
                            'created_by'    => $this->user_data['user_id'],
                            'created_on'    => date('Y-m-d H:i:s')
                        );
                        $this->mega_function_model->insert_prasadam($insert);
                    } else {
                        $update = array(
                            'mega_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_mega_url_key($prasadam[$k]),
                            'created_by'    => $this->user_data['user_id'],
                            'created_on'    => date('Y-m-d H:i:s')
                        );
                        $this->mega_function_model->update_prasadam_by_id($update,$pr_id[$k]);
                    }
                }
            }
            
            if((empty($this->input->post('book-now')))) { 
                #$paidtype = $this->mega_function_model->delete_paid_type($this->input->post('type_id'),$id);
                $this->session->set_flashdata('success', 'Billing for Mega Function is updated successfully');
                
                $paid_hidden_id = $this->input->post('paid_hidden_id');
                if(!empty($paid_hidden_id)) :
                    $paidtype = $this->mega_function_model->delete_payment_by_paid_id($paid_hidden_id);
                endif;                
               
                $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'  => 10,
                            '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->mega_function_model->insert_payment($insert);
                    }
                }
            
            
                if($this->input->post('hidden_amount') < $this->input->post('cs_amount') || $mega->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->mega_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('paid_amount')),
                        'gst'       => $gst_details->gst,
                        'printType' => TRUE,
                        
                        'data'      => array(
                            // Type = 1 && 3 
                            'name'      => array($this->input->post('ct_dev_name_1') .','. $mega->star1_name .','.  $this->input->post('ct_dev_name_1_tamil') .','. $mega->star_tamil_name1 , $this->input->post('ct_dev_name_2') .','. $mega->star2_name  .','.  $this->input->post('ct_dev_name_2_tamil') .','. $mega->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->mega_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 Mega Function 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->mega_function_model->update_prasadam_by_id($update,$rm_id);
                endforeach;
            endif;
            
            
           
        } else 
            $this->session->set_flashdata('error', 'Billing for Mega Function is not updated!');
        
        if(empty($this->input->post('book-now')))
            redirect('billing_mega_function', 'refresh');

        else
            redirect('billing_mega_function/add/' . $this->input->post('type_id'), 'refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['mega'] = $this->mega_function_model->get_by_id($id);
        $data['paid_amount'] = $this->mega_function_model->total_paid_amount($data['mega']->type_id,$data['mega']->id);
        $data['prasadams'] = $this->mega_function_model->get_prasadam_by_id($id);
        $data['ptypes'] = $this->mega_function_model->get_paid_type_by_id($data['mega']->type_id,$id);
        $data['title'] = 'Mega Function';
        $this->render($data, 'billing/mega_function/view');
    }
    
    public function delete($id = '', $menu_id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $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->mega_function_model->update($update,$id) > 0) {
            $this->session->set_flashdata('success', 'Billing for Mega Function is deleted successfully');
            
            $mega = $this->mega_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->mega_function_model->update_paid_type($update,$mega->type_id,$id);
            $this->mega_function_model->update_refund(array('status' => 2),$mega->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->mega_function_model->update_prasadam($update,$id);
        }
           
        else 
            $this->session->set_flashdata('error', 'Billing for Mega Function is not deleted!');
        
        redirect( 'billing_mega_function/index/' . $menu_id, 'refresh');
    }
    
    public function ajax() {
        $results =  $this->mega_function_model->ajax();
        echo json_encode( $results );
    }
    
    public function get_prasadam_price() {
        $data = $this->mega_function_model->get_prasadam_price($_POST['id']);
        echo json_encode($data);
        exit(0);
    }
    
    public function print_list($id,$menu_id) {
        $mega = $this->mega_function_model->get_by_id($id);
        
        /*** To Print Receipt - Start ***/
        $service_menu = $this->login_details_model->get_service_menu_by_id($mega->type_id);
        $gst_details = $this->login_details_model->get_amount_by_service_id($mega->service_id);
        $prefix = get_general_setting()->prefix;
        
        $prasadam = $this->mega_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 : '') . $mega->receipt_no,
            'title'     => $service_menu->service_name . ' - ' . $gst_details->name,
            'amount'    => array($mega->amount),
            'gst'       => $gst_details->gst,
            'status'    => $mega->status,
            'printType' => TRUE,
            
            'data'      => array(
                // Type = 1 && 3 
                'name'      => array($mega->devotee_name1 .','. $mega->star1_name  .','.  $mega->tamil_name1 .','. $mega->star_tamil_name1 , $mega->devotee_name2 .','. $mega->star2_name  .','.  $mega->tamil_name2 .','. $mega->star_tamil_name2 ),
                'contact_no'=> $mega->contact_no,
                'email'     => $mega->email,
                'address'   => $mega->address,
                'desc'      => $mega->desc,
                
                'section'  => array(
                    array(
                        'Date<div class="sub-lbl"> தேதி</div>' => date('d/m/Y',strtotime($mega->date)),
                        'Time<div class="linear-lbl"> நேரம்</div>'      => date('H:i',strtotime($mega->from_time)) . ' - ' . date('H:i',strtotime($mega->to_time)),
                        'Period<div class="linear-lbl"> காலம்</div>'    => ($mega->period == 1) ? 'Morning / காலை' : 'Evening / மாலை',
                        'Deity<div class="linear-lbl"> தெய்வம்</div>'     => $mega->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_mega_function/index/' . $menu_id, 'refresh');
    }
    
} ?>

