<?php

class Platnosci_pl_controller extends Controller {

	function Platnosci_pl_controller()
	{
		parent::Controller();
		 $this->load->library('session');		
		$this->load->library('platnosci_pl');	
		$this->load->helper(array('form', 'url','html'));
		$this->load->model('platnosci_pl_model','',TRUE);		
	}
	
	function status($zwrot=null)
	{		
		switch($zwrot){
		   case 'ok':
		   		if(isset($this->session->userdata['user_zalogowany']) and isset($this->session->userdata['di']))
				{	
					$this->load->model('baza','',TRUE);
					$user_login_data = $this->baza->get_user_login($this->session->userdata['user_zalogowany'], $this->session->userdata['di']);
					
					if($user_login_data!=null)	
					{							 
						$data['content']  = $this->load->view('admin/platnosci_pl_ok',null,true);								 
						$this->load->view('/layout/default',$data);				
					}
					else
					{
						redirect('/admin/logout','location');	
					}		
				}
				else
				{
					redirect('/admin/','location');		
				} 
		    break;  
		   case 'blad':
		    		if(isset($this->session->userdata['user_zalogowany']) and isset($this->session->userdata['di']))
					{	
						$this->load->model('baza','',TRUE);
						$user_login_data = $this->baza->get_user_login($this->session->userdata['user_zalogowany'], $this->session->userdata['di']);
						
						if($user_login_data!=null)	
						{							 
							$data['content']  = $this->load->view('admin/platnosci_pl_blad',null,true);								 
							$this->load->view('/layout/default',$data);				
						}
						else
						{
							redirect('/admin/logout','location');	
						}		
					}
					else
					{
						redirect('/admin/','location');		
					} 	    
		    
		    break;       
		   case 'raport':
		        
		    $this->load->library('platnosci_pl');
		    $this->platnosci_pl->dodajLog(print_r($_POST,TRUE));   
		    $raport = $this->platnosci_pl->pobierzRaport();
		    $this->platnosci_pl->dodajLog(print_r($raport,TRUE));
		    
		    if (is_array($raport))
		    {
		     if ($raport['status'] == '99')
		     {
		          $id_faktury = $this->platnosci_pl->pobierzIdFaktury($raport['session_id']);		     
		    	  $this->load->model('baza_payments','',TRUE);
			  	  $this->baza_payments->zaplacono_platnosci_pl($id_faktury);		
		     }
		    }
		    print("OK");
		    break;
		}
	}
	
	function oplac_fakture_za_doladowanie()
	{
	
	}
	
	function oplac_fakture($id_faktury=null)
	{
		if(isset($this->session->userdata['user_zalogowany']) and isset($this->session->userdata['di']))
		{	
			$this->load->model('baza','',TRUE);
			$user_login_data = $this->baza->get_user_login($this->session->userdata['user_zalogowany'], $this->session->userdata['di']);
			$user_data = $this->baza->get_user_data($this->session->userdata['di']);
			
			if($user_login_data!=null and $user_data!=null)	
			{
				$this->load->model('baza_payments','',TRUE);
				$data['user']  = $user_login_data[0]['name'];
				$data_f['faktury']  = $this->baza_payments->get_facture_platnosci_pl($this->session->userdata['di'],$id_faktury);		

				if(empty($data_f['faktury'])) redirect('/admin/','location');	
				
				  $this->config->load('platnosci_pl');
				  $testowe = $this->config->item('testowe_platnosci','platnosci_pl');
				  $typ_platnosci = $this->config->item('typ_platnosci','platnosci_pl');
				 
				  $this->load->library('platnosci_pl');  
				  $this->platnosci_pl->ustaw_pay_type($typ_platnosci);
				  
				  if ($testowe)
				  {
				    $this->platnosci_pl->ustaw_pay_type('t');
				  }				 
				  			  
				  $platnosci_pl_form_data['kodowanie'] = $this->platnosci_pl->kodowanie();
				  $platnosci_pl_form_data['procedura'] = $this->platnosci_pl->procedura_new();
				  $platnosci_pl_form_data['pay_type'] = $this->platnosci_pl->pay_type();
				  $platnosci_pl_form_data['first_name'] = trim($user_data[0]['company_name']);
				  $platnosci_pl_form_data['last_name'] =  trim($user_data[0]['contact_name']);
				  $platnosci_pl_form_data['email'] = trim($user_data[0]['email']);
				  $platnosci_pl_form_data['pos_id'] = $this->platnosci_pl->pos_id();
				  $platnosci_pl_form_data['pos_auth_key'] = $this->platnosci_pl->pos_auth_key();
				  $platnosci_pl_form_data['session_id'] = time();
				  $platnosci_pl_form_data['amount'] = $data_f['faktury'][0]['brutto']*100; 
				  $platnosci_pl_form_data['desc'] = 'Płatność za FV '.$data_f['faktury'][0]['nr'].'/SEO/'.$data_f['faktury'][0]['month'].'/'.$data_f['faktury'][0]['year'];
				  $platnosci_pl_form_data['desc2'] = $data_f['faktury'][0]['id'];// id faktury w systemie dla ktorej jest dokonywana platnosc na platnosc.pl
				  $platnosci_pl_form_data['client_ip'] = $_SERVER['REMOTE_ADDR'];
				  $platnosci_pl_form_data['ts'] = time();

				  $platnosci_pl_form_data['sig'] = $this->platnosci_pl->sig(
				   $platnosci_pl_form_data['session_id'],
				   $platnosci_pl_form_data['amount'],
				   $platnosci_pl_form_data['desc'],
				   $platnosci_pl_form_data['desc2'],
				   "",
				   "",
				   $platnosci_pl_form_data['first_name'],
				   $platnosci_pl_form_data['last_name'],
				   "",
				   "",
				   "",
				   "",
				   "",
				   "",
				   "",
				   $platnosci_pl_form_data['email'],
				   "",
				   "",
				   $platnosci_pl_form_data['client_ip'],
				   $platnosci_pl_form_data['ts']
				  );
				  
				 $transaction_id = $this->platnosci_pl->dodajNowaTransakcjeDoSystemu($platnosci_pl_form_data['session_id']);				  
				 $formularz_kup_www_data['platnosci_pl_form_data'] = $platnosci_pl_form_data; 
				 $data['content']  = $this->load->view('admin/platnosci_pl_form',$formularz_kup_www_data,true);
				 $this->load->view('/layout/default',$data);				
			}
			else
			{
				redirect('/admin/logout','location');	
			}		
		}
		else
		{
			redirect('/admin/','location');		
		}
	 }
}
?>