<?php

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

class Payment extends CI_Controller {

public function __construct(){
  parent::__construct();
  $this->load->library('form_validation');
  $this->load->model('Payment_model');
}


public function index()
{
  $this->load->view('theme/payment/payment');
}

public function manage()
{ 
  $data=array();
  $data['table_data']=$this->Payment_model->get_all();
  $this->load->view('theme/payment/manage_payment',$data);
}

     public function payment_invoice_list(){
	    $data=array();
		$data['invoices']=$this->Payment_model->get_all_invoice();
	    $this->load->view('theme/payment/invoice_list',$data);
	  }
	  
	   public function view_invoice($id=""){
	    $data=array();
		$data['invoice']=$this->Payment_model->get_by($id);
	    $this->load->view('theme/payment/invoice',$data);
	  }

public function process_payment($id="")
{

$data=array();
$data['payment_type']=$this->input->post('payment_type',false);
$data['sending_code']=$this->input->post('sending_code',false);
$data['date_of_sending']=$this->input->post('date_of_sending',false);
$data['sender_fullname']=$this->input->post('sender_fullname',false);
$data['shipping_city']=$this->input->post('shipping_city',false);
$data['sender_phone']=$this->input->post('sender_phone',false);
$data['payable_amount']=$this->input->post('payable_amount',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_passport']=$this->input->post('recipent_passport',false);
$data['passport_exp_date']=$this->input->post('passport_exp_date',false);
$data['payment_city']=$this->input->post('payment_city',false);
$data['Recipient_CNI']=$this->input->post('Recipient_CNI',false);
$data['Recipient_Phone']=$this->input->post('Recipient_Phone',false);
$data['Recipient_address']=$this->input->post('Recipient_address',false);
$data['Distribution_Number']=$this->input->post('Distribution_Number',false);
$data['Name_of_project']=$this->input->post('Name_of_project',false);
$data['paying_date']=$this->input->post('paying_date',false);
$data['agent_id']=$this->input->post('agent_id',false);
$data['trash']="no";
$data['offline']="no";
if($this->config->item('offline')=='YES'){
$data['offline']="yes";
}

  // Set up the form
  if($data['payment_type']=="individual"){
   $rules = $this->Payment_model->rules_in;
  }else{
   $rules = $this->Payment_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("payment",$data)){
	  $id=$this->db->insert_id();
	  $data=array();
	  $data['status']="paid";
	  $this->db->where('sending_code',$this->input->post('sending_code',false));
	  $this->db->update('sending_money',$data);
	  $this->session->set_flashdata('success', 'Payment Sucessfully');
	  redirect('Payment/view_invoice/'.$id, 'refresh');
  }else{
	  $this->session->set_flashdata('error', 'Saved Failed !');
	  redirect('Payment', 'refresh');
  }
  }else{
	  $this->load->view('theme/payment/payment');
  }

}else if($id!=""){
  //Update
  if ($this->form_validation->run() == TRUE) {
	 $this->db->where('id',$id);
	 if($this->db->update("payment",$data)){
	  $this->session->set_flashdata('success', 'Data Updated Sucessfully');
	  redirect('Payment/edit/'.$id, 'refresh');
	  }else{
	  $this->session->set_flashdata('error', 'Update Failed !');
	  redirect('Payment/edit/'.$id, 'refresh');
	  }
  }else{
	$data=array();
	$data['edit_data']=$this->Payment_model->get_by($id);
	$this->load->view('theme/payment/payment',$data);
  }

}		
}

public function edit($id=NULL){
if($id==NULL){
show_404();
}
$data=array();
$data['edit_data']=$this->Payment_model->get_by($id);
$this->load->view('theme/payment/payment',$data);

}

public function varify_payment($code,$type){
echo json_encode(varify_payment($code,$type));
}

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('payment',$data);
  $this->session->set_flashdata('success', 'Invoice Remove Sucessfully');
  $data=array();
  $data['invoices']=$this->Transfer_model->get_all_invoice();
  $this->load->view('theme/payment/invoice_list',$data);	
  }

/*
public function remove($id=NULL)
{
$this->db->delete('payment', array('id' => $id));
$this->session->set_flashdata('success', 'Remove Sucessfully');
redirect('Payment/manage', 'refresh');	
}*/

}