<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Billing_ubayam extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('billing/ubayam_model', '', TRUE);
        $this->load->model('login_details_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index($id = 17) {
        $data = array();
        $data['results'] = $this->ubayam_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 Ubayam Details';
        $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_ubayam/print_list/';
        $data['title'] = 'Ubayam';
        
        $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/ubayam/list');
    }
    
    public function add($type_id= '') {
        $data = array();
        $actions = $this->actions();
        $data['devotees'] = $this->ubayam_model->get( TBL_DEVOTEE, '', array('order_by' => 'name') );
        $data['deity'] = $this->ubayam_model->get( TBL_GOD_NAME, '', array('order_by' => 'name') );
        $data['fn_types'] = $this->ubayam_model->get( TBL_SERVICE, array( 'menu_name' => $type_id ) );
        $data['stars'] = $this->ubayam_model->get( TBL_STAR, '', array('order_by' => 'name') );
        $data['service_type_id'] = $type_id;
        $data['ptypes'] = $this->ubayam_model->get( TBL_PAY_TYPE );
        $data['prasadams'] = $this->ubayam_model->get( TBL_PRASADAM, '', array('order_by' => 'name') );
        
        $data['form_action'] = $actions['insert'];
        $data['title'] = 'Ubayam';
        $this->render($data, 'billing/ubayam/add');
    }
    
    private function get_ubayam_url_key($name, $id = '') {
        $url_key = get_url_key($name);
        if ($this->ubayam_model->ubayam_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_ubayam_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_ubayam_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_UBAYAM, $wh);
        $insert['receipt_no'] = $receipt_no;   
        
        //$services = $this->ubayam_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->ubayam_model->insert($insert);
        
        if ($insert_id > 0) {
            $this->session->set_flashdata('success', 'Billing for Ubayam Details 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->ubayam_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(
                        'ubayam_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_ubayam_url_key($prasadam[$k]),
                        'created_by'    => $this->user_data['user_id'],
                        'created_on'    => date('Y-m-d H:i:s')
                    );
                    $this->ubayam_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'  => 17,
                            '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->ubayam_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;
                $ubayam = $this->ubayam_model->get_by_id($insert_id);
                $prasadam = $this->ubayam_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') .','. $ubayam->star1_name .','.  $this->input->post('ct_dev_name_1_tamil') .','. $ubayam->star_tamil_name1, $this->input->post('ct_dev_name_2') .','. $ubayam->star2_name .','.  $this->input->post('ct_dev_name_2_tamil') .','. $ubayam->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>'     => $ubayam->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 Ubayam Details is not added!');        

        if(empty($this->input->post('book-now')))
            redirect('billing_ubayam', 'refresh');

        else
            redirect('billing_ubayam/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['ubayam'] = $this->ubayam_model->get_by_id($id);
        $data['paid_amount'] = $this->ubayam_model->total_paid_amount($data['ubayam']->type_id,$data['ubayam']->id);
        $data['devotees'] = $this->ubayam_model->get( TBL_DEVOTEE, '', array('order_by' => 'name') );
        $data['deity'] = $this->ubayam_model->get( TBL_GOD_NAME, '', array('order_by' => 'name') );
        $data['fn_types'] = $this->ubayam_model->get( TBL_SERVICE, array( 'menu_name' => $data['ubayam']->type_id ) );
        $data['stars'] = $this->ubayam_model->get( TBL_STAR, '', array('order_by' => 'name') );
        $data['ptypes'] = $this->ubayam_model->get( TBL_PAY_TYPE );
        $data['prasadams'] = $this->ubayam_model->get( TBL_PRASADAM, '', array('order_by' => 'name') );
        $data['items'] = $this->ubayam_model->get_prasadam_json($id);
        $data['title'] = 'Ubayam';
        $this->render($data, 'billing/ubayam/edit');
    }
    
    public  function update($id){
        if (empty($id)) {
            show_400_error();
        }
       
        $ubayam = $this->ubayam_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_ubayam_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 = $ubayam->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_UBAYAM, $wh);
            $update['receipt_no'] = $receipt_no;   
            $created_date = date('Y-m-d H:i:s');
            
            
        else :
            $created_date = $ubayam->created_on;
        endif;
        
        $affected_rows = $this->ubayam_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(
                            'ubayam_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_ubayam_url_key($prasadam[$k]),
                            'created_by'    => $this->user_data['user_id'],
                            'created_on'    => date('Y-m-d H:i:s')
                        );
                        $this->ubayam_model->insert_prasadam($insert);
                    } else {
                        $update = array(
                            'ubayam_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_ubayam_url_key($prasadam[$k]),
                            'created_by'    => $this->user_data['user_id'],
                            'created_on'    => date('Y-m-d H:i:s')
                        );
                        $this->ubayam_model->update_prasadam_by_id($update,$pr_id[$k]);
                    }
                }
            }
            
            if(empty($this->input->post('book-now'))) {
                #$paidtype = $this->ubayam_model->delete_paid_type($this->input->post('type_id'),$id);
                $this->session->set_flashdata('success', 'Billing for Ubayam Details is updated successfully');
                
                $paid_hidden_id = $this->input->post('paid_hidden_id');
                if(!empty($paid_hidden_id)) :
                    $paidtype = $this->ubayam_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'  => 17,
                            '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->ubayam_model->insert_payment($insert);
                    }
                }
                
                if(($this->input->post('hidden_amount') < $this->input->post('cs_amount')) || $ubayam->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->ubayam_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') .','. $ubayam->star1_name  .','.  $this->input->post('ct_dev_name_1_tamil') .','. $ubayam->star_tamil_name1, $this->input->post('ct_dev_name_2') .','. $ubayam->star2_name .','.  $this->input->post('ct_dev_name_2_tamil') .','. $ubayam->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->ubayam_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 Ubayam Details 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->ubayam_model->update_prasadam_by_id($update,$rm_id);
                endforeach;
            endif;
           
        } else 
            $this->session->set_flashdata('error', 'Billing for Ubayam Details is not updated!');
        
        if(empty($this->input->post('book-now')))
            redirect('billing_ubayam', 'refresh');

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

