<?php

/* 
 * Author       : Jai K
 * Purpose      : The controller for Archanai
 * Created On   : 2016-08-22 12:19
 */

class Billing_Archanai extends REF_Controller {
    public $controller;
    
    function __construct() {
        parent::__construct();
        
        $segments = $this->uri->segments;
        $this->controller = $segments[1];
        
        $this->load->model('billing/archanai_model');
    }
    
    function scan(){
        // Archanai Page for Kiosk Scan
        $user_data = get_user_data();
        
        if( empty( $user_data['user_id'] ) ) :
            // Redirect to login
            redirect('kiosk/scan');
        endif;
        
        $user = _get( TBL_BILL_MEMBER_CARD, array('id' => $user_data['user_id'] ), array('select' => '*, star AS star_id', 'is_single' => TRUE) );
                            
        $data = array(
            'user'      => $user,
            'category_on' => $user_data['category_on'],
            'settings_design'=> get_design_setting(),
            
            'gods'      => $this->archanai_model->get( TBL_GOD_NAME ),
            'members'   => $this->archanai_model->get_members( $user['id'] ),
            'pay_id'    => $this->archanai_model->scan('payment'), // Enable "Membership Card Payment Type" 
        );
        
        if( $data['category_on'] ) :
            $data['categories'] = $this->archanai_model->get( TBL_CATEGORY, '', array('order_by' => 'category_order') );
        else :
            $data['items'] = $this->archanai_model->get_items();
        endif;
        
        //echo "<pre>"; print_r( $data ); die;
        $this->load->view( 'billing/archanai/scan', $data);
        
    }
    
    function add( $id='' ) {
        $this->manipulate( $id );
        
        
    }
    
    function manipulate( $id, $args = array() ) {
        $actions = $this->actions();
        $post = $this->input->post();
        $session_data = $this->session->userdata(TBL_PREFIX . 'user_logged_in');
        $token = $this->session->userdata( 'rtoken-'.$session_data['user_id'] );
        
        $page = $page_file_name = !empty( $args['page'] ) ? $args['page'] : 'add';
        $page_file_name         = $page == 'pdf' ? 'view' : $page_file_name;
        
        $data = array(
            'id'                => $id,
            'title'             => 'Archanai (Billing)',
            'is_'.$page         => TRUE,
            'controller'        => $this->controller,
            'actions'           => $actions,
            'form_action'       => base_url() . $this->controller .'/'.$page.'/'.$id,
            'success'           => $this->session->flashdata('success'),
            'error'             => $this->session->flashdata('error'),
            'stars'             => $this->archanai_model->get( TBL_STAR ), 
            'categories'        => $this->archanai_model->get( TBL_CATEGORY, '', array('order_by' => 'category_order') ),
            'gods'              => $this->archanai_model->get( TBL_GOD_NAME ),
        );
        
        if( $page == 'add' || $page == 'edit' ) :
            // ID => Menu id
            $data['token'] = $this->auto_generation_code( rand( 999, 9999 ) );
            $this->session->set_userdata( 'rtoken-'.$session_data['user_id'], $data['token'] );
            
            $data['ptypes']     = $this->archanai_model->get( TBL_PAY_TYPE );
            $data['items']      = $this->archanai_model->get_items_by_not_cat();
        endif;
        
        // Load Views
        $this->render( $data, 'billing/archanai/add' );
        /*$this->load->view( 'billing/header', $data );
        $this->load->view( 'billing/archanai/add', $data );
        $this->load->view( 'billing/footer', $data );*/
        
    }
    
    function ajax() {
        $results =  $this->archanai_model->ajax();
        
        echo json_encode( $results );
    }
    
    
	public function get_sanctum_name() {
        $god_name = $this->input->post("godnewval");
        $get_details = $this->archanai_model->get_sanctum_name($god_name);
        echo json_encode($get_details);
    }
    
	function archanai_bill_amount() {
		$display_pole_set = get_display_pole_data();
		$amount =  $_POST['amount'];
		//$this->session->set_flashdata('bill_paid_amount', $amount);
		//DISPLAY POLE TEXT CONTENT
		if($display_pole_set['pole_setting']['status'] == 1){
			exec("mode ".$display_pole_set['port_val']['value'].": BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");
			//$fp =fopen("C:/xampp/htdocs/smmt/application/views/billing/com.txt", "w+");
			$fp =fopen($display_pole_set['port_val']['value'], "w+");
			fwrite($fp,"\x0c");
			fwrite($fp, "ARCHANAI&ITEMS\n\rTOTAL : $".$_POST['amount']); //write string to serial
			//sleep(2);
			//fwrite($fp,"\x0c");
			//fwrite($fp,"*SREE RAMAR TEMPLE*\n\r **Have a nice day**");
			//fclose($fp);
		}
	}
	
	function archanai_balance_amount() {
		$display_pole_set = get_display_pole_data();
		$receive_amt =  $_POST['receive_amt'];
		$balance_amt =  $_POST['balance_amt'];
		//$this->session->set_flashdata('bill_paid_amount', $amount);
		//DISPLAY POLE TEXT CONTENT
		if($display_pole_set['pole_setting']['status'] == 1){
			exec("mode ".$display_pole_set['port_val']['value'].": BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");
			//$fp =fopen("C:/xampp/htdocs/smmt/application/views/billing/com.txt", "w+");
			$fp =fopen($display_pole_set['port_val']['value'], "w+");
			//fwrite($fp,"\x0c");
			//fwrite($fp, "$".$_POST['amount']); //write string to serial
			fwrite($fp,"\x0c");
			fwrite($fp,"RECEIVED: $".$receive_amt."\n\rCHANGE :$".$balance_amt);
			//sleep(2);
			//fwrite($fp,"\x0c");
			//fwrite($fp,"*SREE RAMAR TEMPLE*\n\r **Have a nice day**");
			//fclose($fp);
		}
	}
	
	public function get_day_devotees(){
		$post=$this->input->post();
		$act=$post['act'];
		if($act=='get_day_dev'):
			$days=date("l");
			$get_details=$this->archanai_model->get_day_devotees($days);	  
		endif;
		echo json_encode($get_details);
	}
	
	public function get_day_devotees_by_name(){   
		$post=$this->input->post();
		$ref_id=$post['ref_id'];
		$get_details=$this->archanai_model->get_day_devotees_by_name($ref_id);
		echo json_encode($get_details);
	}

	public function delete_dev(){
		$post		= $this->input->post();
		$ref_id		= $post['ref_id'];
		$this->db->where("ref_id",$ref_id);
		$details = $this->db->delete("thiru_day_devotees");
		if($details>0){
			$result = 1;
		}else{
			$result = 0;
		}
		echo json_encode($result);
	}
} // class    
?>