<?php
 
defined('BASEPATH') OR exit('No direct script access allowed');

class Transfer extends Admin_Controller {
  
	 public function __construct(){
	      parent::__construct();
	      $this->load->library('form_validation');
	      $this->load->model('Transfer_model');
	 }
  
  
	  public function index()
	  {
		  $this->load->view('theme/transfer/send_money');
	  }
	  
	  public function manage()
	  { 
		  $data=array();
		  $data['table_data']=$this->Transfer_model->get_all();
		  $this->load->view('theme/transfer/manage_transfer',$data);
	  }
	  
	  public function transfer_invoice_list(){
	    $data=array();
		$data['invoices']=$this->Transfer_model->get_all_invoice();
	    $this->load->view('theme/transfer/invoice_list',$data);
	  }
	  
	   public function view_invoice($id=""){
	    $data=array();
		$data['invoice']=$this->Transfer_model->get_by($id);
	    $this->load->view('theme/transfer/invoice',$data);
	  }
	  
	  public function save($id="")
	  {
  
		$data=array();
		$data['sending_type']=$this->input->post('sending_type',false);
		$data['sending_code']=$this->input->post('sending_code',false);
		$data['sending_date']=$this->input->post('sending_date',false);
		$data['sender_fullname']=$this->input->post('sender_fullname',false);
		$data['sender_city']=$this->input->post('sender_city',false);
		$data['sender_passport']=$this->input->post('sender_passport',false);
		$data['passport_exp_date']=$this->input->post('passport_exp_date',false);
		$data['sending_amount']=$this->input->post('sending_amount',false);
		$data['cost']=$this->input->post('cost',false);
		$data['total_amount']=$this->input->post('total_amount',false);
		$data['telephone']=$this->input->post('telephone',false);
		$data['secret_question']=$this->input->post('secret_question',false);
		$data['secret_answer']=$this->input->post('secret_answer',false);
		$data['recipent_fullname']=$this->input->post('recipent_fullname',false);
		$data['recipent_phone']=$this->input->post('recipent_phone',false);
		$data['recipent_city']=$this->input->post('recipent_city',false);
		$data['recipent_CNI']=$this->input->post('recipent_CNI',false);
		$data['org_address']=$this->input->post('org_address',false);
		$data['distribution_number']=$this->input->post('distribution_number',false);
		$data['name_of_project']=$this->input->post('name_of_project',false);
		$data['agent_id']=$this->input->post('agent_id',false);
		$data['status']="sending";
		$data['trash']="no";
		$data['offline']="no";
		if($this->config->item('offline')=='YES'){
		$data['offline']="yes";
		}
		
		  // Set up the form
		  if($data['sending_type']=="individual"){
		  $rules = $this->Transfer_model->rules_in;
		  }else{
		  $rules = $this->Transfer_model->rules_org;
		  }
		  
		  $this->form_validation->set_rules($rules);
  
		  //Process The Form
		  //Insert
		  if($id==""){
		  if ($this->form_validation->run() == TRUE) {
			  if($this->db->insert("sending_money",$data)){
			  $id=$this->db->insert_id();
			  $this->session->set_flashdata('success', 'Payment Send Sucessfully');
			  redirect('Transfer/view_invoice/'.$id, 'refresh');
		  }else{
			  $this->session->set_flashdata('error', 'Saved Failed !');
			  redirect('Transfer', 'refresh');
		  }
		  }else{
			  $this->load->view('theme/transfer/send_money');
		  }
		
		}else if($id!=""){
		  //Update
		  if ($this->form_validation->run() == TRUE) {
		     $this->db->where('id',$id);
		     if($this->db->update("sending_money",$data)){
			  $this->session->set_flashdata('success', 'Data Updated Sucessfully');
			  redirect('Transfer/edit/'.$id, 'refresh');
		      }else{
			  $this->session->set_flashdata('error', 'Update Failed !');
			  redirect('Transfer/edit/'.$id, 'refresh');
		      }
		  }else{
			$data=array();
			$data['edit_data']=$this->Transfer_model->get_by($id);
			$this->load->view('theme/transfer/send_money',$data);
		  }
		
		}		
	  }
	  
	  public function edit($id=NULL){
	  if($id==NULL){
	    show_404();
	  }
	  $data=array();
	  $data['edit_data']=$this->Transfer_model->get_by($id);
	  $this->load->view('theme/transfer/send_money',$data);
	  
	  }
	  
	  public function calculate_fee($amount=""){
	  echo calculate_fee($amount);	  
	  }
	  
	  public function remove_invoice($id=NULL)
	  {
	  if(user_type()!="admin"){
	   redirect('Dashboard', 'refresh');
	  }
	  $data=array();
	  $data['trash']='yes';
	  $this->db->where('id',$id);
	  $this->db->update('sending_money',$data);
	  $this->session->set_flashdata('success', 'Invoice Remove Sucessfully');
	  $data=array();
	  $data['invoices']=$this->Transfer_model->get_all_invoice();
	  $this->load->view('theme/transfer/invoice_list',$data);	
	  }
	  
	  public function cancle_transfer($id=NULL)
	  {
	  if(user_type()!="admin"){
	   redirect('Dashboard', 'refresh');
	  }
	  $data=array();
	  $data['status']='cancle';
	  $this->db->where('id',$id);
	  $this->db->update('sending_money',$data);
	  $this->session->set_flashdata('success', 'Transfer Cancle Sucessfully');
      redirect('Transfer/manage', 'refresh');
	  	
	  }
	  
	  /*
	  public function remove($id=NULL)
	  {
	  $this->db->delete('sending_money', array('id' => $id));
	  $this->session->set_flashdata('success', 'Remove Sucessfully');
	  redirect('Transfer/manage', 'refresh');	
	  }
	  */
  }