<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Billing_gold_silver_offering extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('billing/gold_silver_offering_model', '', TRUE);
        $this->load->model('login_details_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index($id = 6) {
        $data = array();
        $data['results'] = $this->gold_silver_offering_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 .' 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_gold_silver_offering/print_list/';
        #$data['title'] = 'Gold/Silver Offering';
        
        $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/gold_silver_offering/list');
    } 
    
    public function add($type_id= '') {
        $data = array();
        $actions = $this->actions();
        $service_menu = get_billing_controller($actions['controller']);
        $data['fn_types'] = $this->gold_silver_offering_model->get( TBL_SERVICE, array( 'menu_name' => $type_id ) );
        $data['service_type_id'] = $type_id;
        $data['payments'] = $this->gold_silver_offering_model->payment_mode_lists();
        $data['god_list'] = $this->gold_silver_offering_model->god_list();
        
        $data['form_action'] = $actions['insert'];
        $data['title'] = $service_menu->service_name;
        $this->render($data, 'billing/gold_silver_offering/add');
    }
    
    private function get_gold_silver_url_key($name, $id = '') {
        $url_key = get_url_key($name);
        if ($this->gold_silver_offering_model->gold_silver_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_gold_silver_url_key($name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function insert() {
        $services = $this->gold_silver_offering_model->service_type_exists($this->input->post('fn_type'));
        $gst_details = $this->login_details_model->get_amount_by_service_id($this->input->post('fn_type'));
        
        $actions = $this->actions();
        $serviceMenu = get_billing_controller($actions['controller']);
        
        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'),
            'name' => $this->input->post('name'),
            'tamil_name' => htmlspecialchars( $this->input->post('tamil_name') ),
            'deity_id' => $this->input->post('deity_id'),
            'contact_no' => $this->input->post('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'),
            #'status' => $this->input->post('status'),
            'gst' => $gst,
            'gst_value' => $gst_amount,
            'url_key' => $this->get_gold_silver_url_key($this->input->post('name')),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $insert_id = $this->gold_silver_offering_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->gold_silver_offering_model->update($update, $insert_id);
           
            $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'  => 6,
                        '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->gold_silver_offering_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;
            
            $god_name = $this->gold_silver_offering_model->get_deity_by_id($this->input->post('deity_id'));
            $args = array(
                'type'      => '16', // 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,
				'pay_ref_id'    => $insert_id,
                'created_on' => date('Y-m-d H:i:s'),
                
                'data'      => array(
                    // Type = 1 && 3 
                    'name'      => array($this->input->post('name')  .','.  $this->input->post('tamil_name')),
                    'deity'      => $god_name->name . ((!empty($god_name->tamil_name)) ? ' /  <span style="font-family: amudham; font-size: 24px;">' . $god_name->tamil_name . '</span>': '' ),
                    'contact_no'=> $this->input->post('contact_no'),
                    'email'     => $this->input->post('ct_email'),
                    'address'   => $this->input->post('ct_address'),
                    'desc'      => $this->input->post('cs_desc'),
                ),
                
            );
            
            print_rec_A4($args);            
            /*** End ***/
        }
        else
            $this->session->set_flashdata('error', 'Billing for ' . $serviceMenu->service_name . ' is not added!');   
        
        redirect('billing_gold_silver_offering', '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['offering'] = $this->gold_silver_offering_model->get_by_id($id);
        $data['paid_amount'] = $this->gold_silver_offering_model->total_paid_amount($data['offering']->type_id,$data['offering']->id);
        $data['fn_types'] = $this->gold_silver_offering_model->get( TBL_SERVICE, array( 'menu_name' => $data['offering']->type_id ) );
        $data['payments'] = $this->gold_silver_offering_model->payment_mode_lists();
        $data['god_list'] = $this->gold_silver_offering_model->god_list();
        
        $data['title'] = $service_menu->service_name;
        $this->render($data, 'billing/gold_silver_offering/edit');
    }
    
    public  function update($id){
        if (empty($id)) {
            show_400_error();
        }
        
        $offering = $this->gold_silver_offering_model->get_by_id($id);
        $gst_details = $this->login_details_model->get_amount_by_service_id($this->input->post('fn_type'));
        
        $actions = $this->actions();
        $service_menu = get_billing_controller($actions['controller']);
        
        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'),
            'name' => $this->input->post('name'),
            'tamil_name' => htmlspecialchars( $this->input->post('tamil_name') ),
            'deity_id' => $this->input->post('deity_id'),
            'contact_no' => $this->input->post('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'),
            #'status' => $this->input->post('status'),
            'gst' => $gst,
            'gst_value' => $gst_amount,
            'url_key' => $this->get_gold_silver_url_key($this->input->post('name')),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        $affected_rows = $this->gold_silver_offering_model->update($update,$id);
        
        if($affected_rows > 0) {
           $this->session->set_flashdata('success', 'Billing for ' . $service_menu->service_name . ' is updated successfully');
           
            $paid_hidden_id = $this->input->post('paid_hidden_id');
            if(!empty($paid_hidden_id)) :
                $paidtype = $this->gold_silver_offering_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 != '') {
                   $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'  => 6,
                        'pay_ref_id'    => $id,
                        'pay_type_id'   => $key,
                        'pay_amount'    => $value,
                        'pay_details'   => $pay_details,
                        'status'        => 1,
                        'created_by'    => $this->user_data['user_id'],
                        'created_on'    => $offering->created_on
                    );
                    $this->gold_silver_offering_model->insert_payment($insert);
                }
            }
            
        } else 
            $this->session->set_flashdata('error', 'Billing for ' . $service_menu->service_name . ' is not updated!');
        
        redirect( 'billing_gold_silver_offering', 'refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $actions = $this->actions();
        $service_menu = get_billing_controller($actions['controller']);
        
        $data['offering'] = $this->gold_silver_offering_model->get_by_id($id);
        $data['paid_amount'] = $this->gold_silver_offering_model->total_paid_amount($data['offering']->type_id,$data['offering']->id);
        $data['fn_types'] = $this->gold_silver_offering_model->get( TBL_SERVICE, array( 'menu_name' => $data['offering']->type_id ) );
        $data['payments'] = $this->gold_silver_offering_model->get_paid_type_by_id($data['offering']->type_id,$id);
        
        $data['title'] = $service_menu->service_name;
        $this->render($data, 'billing/gold_silver_offering/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->gold_silver_offering_model->update($update,$id) > 0) {
            $this->session->set_flashdata('success', 'Billing for ' . $service_menu->service_name . ' is deleted successfully');
            $offering = $this->gold_silver_offering_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->gold_silver_offering_model->update_paid_type($update,$offering->type_id,$id);
        }
        
        else 
            $this->session->set_flashdata('error', 'Billing for ' . $service_menu->service_name . ' is not deleted!');
        
        redirect( 'billing_gold_silver_offering/index/' . $menu_id, 'refresh');
    }
    
    public function get_amount_by_service_id() {
        $data =  $this->gold_silver_offering_model->get_amount_by_service_id($_POST['service_type']);
        echo json_encode($data);
        exit(0);
    }
    
    public function get_data_by_service_type(){
        $data['get_data'] =  $this->gold_silver_offering_model->get_data_by_service_type($_POST['service_type']);
        $data['get_contact'] =  $this->gold_silver_offering_model->get_contact_by_service_type($_POST['service_type']);
        echo json_encode($data);
        exit(0);
    }
    
    public function ajax() {
        $data =  $this->gold_silver_offering_model->ajax();
        echo json_encode($data);
        exit(0);  
    }
    
    public function print_list($id,$menu_id) {
        $offering = $this->gold_silver_offering_model->get_by_id($id);
       // echo '<pre>';print_r($offering);die;
        /*** To Print Receipt - Start ***/
        $service_menu = $this->login_details_model->get_service_menu_by_id($offering->type_id);
        $gst_details = $this->login_details_model->get_amount_by_service_id($offering->service_id);
        $prefix = get_general_setting()->prefix;
        
        $args = array(
            'type'      => '16', // Default = 1 // Normal (Double print), '2' => Archanai,   '3' = Hall Booking | Wedding Hall
            'rec_no'    => (($prefix == 1) ? $gst_details->prefix : '') . $offering->receipt_no,
            'title'     => $service_menu->service_name . ' - ' . $gst_details->name,
            'amount'    => array($offering->amount),
            'gst'       => $gst_details->gst,
            'status'    => $offering->status,
            'created_on' => $offering->created_on,
            
            'data'      => array(
                // Type = 1 && 3 
                'name'      => array($offering->name .','.  $offering->tamil_name),
				'deity'     => $offering->god_name,
                'contact_no'=> $offering->contact_no,
                'email'     => $offering->email,
                'address'   => $offering->address,
                'desc'      => $offering->desc,
            ),
            
        );
        
        if(print_rec_A4($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_gold_silver_offering/index/' . $menu_id, 'refresh');
    }
    
} ?>

