<?php



/* 

 * Author       : Jai K

 * Purpose      : Model for Billing Abishegam

 * Created On   : 2016-07-30 18:40

 */



class Bill_Settings_Model extends CI_Model {

    

    function __construct() {

        parent::__construct();

        

    }

    

    function get($tbl_name, $args = array()){

        $where = $args['where'];

        

        if( !empty( $args['select'] ) ) :

            $this->db->select( $args['select'] );

        endif;

        

        if( !empty( $args['order_by'] ) ) :

            $this->db->order_by( $args['order_by'] );

        endif;

        

        $q = $this->db->get_where( $tbl_name, $where );

        return $q->result_array();

    }

    

    function manipulate(){

        $now  = date("Y-m-d H:i:s");

        $user_data = get_user_data();

        $post = $this->input->post();

          

        $general = get_general_setting();

        

        //echo '<pre>'; print_r( $post ); die;

        //echo '<pre>Files: '; print_r( $_FILES ); die;

        

        /*

         * Basics

         */

        

        $slugs = array( 'rec_msg', 'ack_sms_msg', 'ack_mail_msg', 'video_url', 'pcrt_afternoon', 'pcrt_evening', 'kart_morning', 'pole_setting', 'port_val', 'pole_msg1', 'pole_msg2');

        $this->db->select('id, slug');

        $this->db->where_in( 'slug', $slugs );

        $q = $this->db->get_where( TBL_BILL_SETT, array('status !=' => 2));

        $basics = $q->result_array();

        //echo '<pre>'; print_r( $basics ); die;

        $slugs = array_flip($slugs);

        foreach($basics as $k => $basic) :

            $slugs[$basic['slug']] = $basic['id'];

        endforeach;

        foreach($slugs as $slug => $id) : 
			if($slug == 'pcrt_afternoon'):
        		$this->db->delete(TBL_BILL_SETT, array('slug' => 'pcrt_afternoon' ));
				// Update
				$args['slug'] 		= 'pcrt_afternoon';
				$args['status'] 	= 1;
				$args['name'] 		= "Print Prasadam Afternoon Time";
				$args['value']		= $post['pcrt_afternoon'];
				$args['created_on'] = $now;
				$args['created_by'] = $user_data['user_id'];
				$args['updated_by'] = $user_data['user_id'];
				
				$this->db->insert( TBL_BILL_SETT, $args );
			elseif($slug == 'pole_setting'):
				$this->db->delete(TBL_BILL_SETT, array('slug' => 'pole_setting' ));
				
				if( !isset( $post['display_pole_set']) ) :
                    // Insert
                    $args['slug'] 		= 'pole_setting';
                   	$args['status'] 	=  0;
					$args['name'] 		= "Pole Setting";
					$args['value']		= "Pole Setting Unchecked";
					$args['created_on'] = $now;
					$args['created_by'] = $user_data['user_id'];
                    $args['updated_by'] = $user_data['user_id'];
					
					$this->db->insert( TBL_BILL_SETT, $args);
                else :
                    // Update
                    $args['slug'] 		= 'pole_setting';
                   	$args['status'] 	= 1;
					$args['name'] 		= "Pole Setting";
					$args['value']		= "Pole Setting Checked";
					$args['created_on'] = $now;
					$args['created_by'] = $user_data['user_id'];
                    $args['updated_by'] = $user_data['user_id'];
					
					$this->db->insert( TBL_BILL_SETT, $args);					
                endif;
			elseif($slug == 'port_val'):
				$this->db->delete(TBL_BILL_SETT, array('slug' => 'port_val' ));
				// Update
				$args['slug'] 		= 'port_val';
				$args['status'] 	= 1;
				$args['name'] 		= "Port Text";
				$args['value']		= $post['port'];
				$args['created_on'] = $now;
				$args['created_by'] = $user_data['user_id'];
				$args['updated_by'] = $user_data['user_id'];
				
				$this->db->insert( TBL_BILL_SETT, $args );
			elseif($slug == 'pole_msg1'):
				$this->db->delete(TBL_BILL_SETT, array('slug' => 'pole_msg1' ));
				// Update
				$args['slug'] 		= 'pole_msg1';
				$args['status'] 	= 1;
				$args['name'] 		= "Pole Message 1";
				$args['value']		= $post['port_msg1'];
				$args['created_on'] = $now;
				$args['created_by'] = $user_data['user_id'];
				$args['updated_by'] = $user_data['user_id'];
				
				$this->db->insert( TBL_BILL_SETT, $args );	
			elseif($slug == 'pole_msg2'):
				$this->db->delete(TBL_BILL_SETT, array('slug' => 'pole_msg2' ));
				// Update
				$args['slug'] 		= 'pole_msg2';
				$args['status'] 	= 1;
				$args['name'] 		= "Pole Message 2";
				$args['value']		= $post['port_msg2'];
				$args['created_on'] = $now;
				$args['created_by'] = $user_data['user_id'];
				$args['updated_by'] = $user_data['user_id'];
				
				$this->db->insert( TBL_BILL_SETT, $args );	
				//echo $this->db->last_query(); //die;	
		    elseif($slug == 'pcrt_evening'):
				$this->db->delete(TBL_BILL_SETT, array('slug' => 'pcrt_evening' ));
				// Update
				$args['slug'] 		= 'pcrt_evening';
				$args['status'] 	= 1;
				$args['name'] 		= "Print Prasadam Evening Time";
				$args['value']		= $post['pcrt_evening'];
				$args['created_on'] = $now;
				$args['created_by'] = $user_data['user_id'];
				$args['updated_by'] = $user_data['user_id'];
				
				$this->db->insert( TBL_BILL_SETT, $args );		
				//echo $this->db->last_query(); //die;	
				
		    elseif($slug == 'kart_morning'):
				$this->db->delete(TBL_BILL_SETT, array('slug' => 'kart_morning' ));
				// Update
				$args['slug'] 		= 'kart_morning';
				$args['status'] 	= 1;
				$args['name'] 		= "Print Kattali Archanai Time";
				$args['value']		= $post['kart_morning'];
				$args['created_on'] = $now;
				$args['created_by'] = $user_data['user_id'];
				$args['updated_by'] = $user_data['user_id'];
				
				$this->db->insert( TBL_BILL_SETT, $args );		
				//echo $this->db->last_query(); //die;			
		    else :
				if( empty( $id ) ) :
	
					// Record not exists
	
					$this->db->insert( TBL_BILL_SETT, array('slug' => $slug, 'created_on' => $now, 'created_by' => $user_data['user_id']) );
					
				endif;
          
		    $args = array(
                'status'    => 1
            );
			
            if( $slug == 'rec_msg' || $slug == 'ack_sms_msg' || $slug == 'ack_mail_msg' ) :

                /*

                * Messages

                */

                $value = !empty( $post['swap_inp_'.$slug] ) ? $post['swap_inp_'.$slug] . htmlspecialchars( $post[$slug] ) : 'ENG' . $post[$slug];

            else :

                if( $slug == 'video_url' ) :

                    $args['status'] = $post['video_chk'] == 'on' ? '1' : '0';

                endif;

                

                $value = $post[$slug];

            endif;

            

            $args['value'] = $value;



            $this->db->update( TBL_BILL_SETT, $args, array('slug' => $slug) );

            $this->db->last_query();
		  endif;
        endforeach;
        /*

         * Events

         */

         

        if($general->scan_availability == 1) :

            $now_time = date("H:i:s");

            foreach( $post['event_name'] as $k => $evt_name ) :

                if( empty( $evt_name ) ) :

                    continue;

                endif;

                

                $lang = !empty( $post['swap_inp_event_name-'.$k.'-'] ) ? $post['swap_inp_event_name-'.$k.'-'] : 'ENG';

                $args = array(

                    'name'          => $lang. $evt_name,

                    /*'value'         => json_encode(array(

                            'start' => $post['event_start_date'][$k],

                            'end'   => $post['event_end_date'][$k],

                            'period'=> $post['event_period'][$k],

                        )),*/

                    

                    'value'         => $post['event_period'][$k],

                );

                

                $e  = explode( '-', $post['event_start_date'][$k] );

                $args['created_on'] = $e[2] . '-' . $e[1] .'-'. $e[0].' '. $now_time;

                

                $e  = explode( '-', $post['event_end_date'][$k] );

                $args['updated_on'] = $e[2] . '-' . $e[1] .'-'. $e[0].' ' .( $args['value'] == '1' ? '00:00:01' : '12:00:01' );

            

                if( empty( $post['event_id'][$k] ) ) :

                    // Insert

                    $args['slug'] = 'event';

                    $args['created_by'] = $user_data['user_id'];



                    $this->db->insert( TBL_BILL_SETT, $args );

                else :

                    // Update

                    $args['status'] = $post['event_chk'][$k] == 'on' ? 1 : 0;

                    $args['updated_by'] = $user_data['user_id'];

                    

                    $this->db->update( TBL_BILL_SETT, $args, array('id' => $post['event_id'][$k] ) );

                endif;

                

                

            endforeach;

            

            /*

             * Advertisements

             */

            if( !empty( $_FILES ) ) :

                

                $upload_data = uploads(array(

                    'field'         => 'ad_file',

                    'upload_path'   => DIR_ADVERTISEMENT,

                ));

            

            endif;

                

            foreach( $post['ad_name'] as $k => $ad_name ) :

                if( empty( $ad_name ) ) :

                    continue;

                endif;

                

                $args = array(

                    'name'  => $ad_name,

                );



                $value = $upload_data['ad_file']['file_arr'][$k];

                if( !empty( $value ) ) :

                    $args['value'] = $value;

                    

                    // Remove prev. file

                    unlink( DIR_ADVERTISEMENT . $post['ad_old'][$k] );

                endif;



                if( empty( $post['ad_id'][$k] ) ) :

                    // Insert

                    $args['slug']       = 'advertisement';

                    $args['created_on'] = $now;

                    $args['created_by'] = $user_data['user_id'];



                    $this->db->insert( TBL_BILL_SETT, $args );

                else :

                    // Update

                    $args['status'] = $post['ad_chk'][$k] == 'on' ? 1 : 0;

                    $args['updated_on'] = $now;

                    $args['updated_by'] = $user_data['user_id'];



                    $this->db->update( TBL_BILL_SETT, $args, array('id' => $post['ad_id'][$k] ) );

                endif;



            endforeach;

                

            

            

            if( !empty( $post['ad_rm'] ) ) :

                $args = array(

                    'status'    => 2,

                    'updated_on'=> $now,

                    'updated_by'=> $user_data['user_id']    

                );

            

                foreach( $post['ad_rm'] as $ad_id => $file_name ) :

                    // Remove File

                    unlink( DIR_ADVERTISEMENT . $file_name );

                    

                    $this->db->update( TBL_BILL_SETT, $args, array('id' => $ad_id ) );

                endforeach;

                        

            endif;

            

            /*

             * Update Records for all deleted rows

             */

            if( !empty( $post['rm_id'] ) ) : // Comma separated id's

                $args = array(

                    'status'    => 2,

                    'updated_by'=> $user_data['user_id']    

                );

            

                $this->db->where_in('id', $post['rm_id'] ); 

                $this->db->update( TBL_BILL_SETT, $args, array() );

                

            endif;

        endif;

  }

    

    function ajax(){

        $post = $this->input->post();

        $return = array();

        

        switch( $post['act'] ) :

            case 'events':

                // Get all Events

                $return = $this->get(TBL_BILL_SETT, array('where' => array( 'status !=' => 2, 'slug' => 'event' ) ) );

                

            break;    

        endswitch;

        

        return $return;

    }

    

}