<?php

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

class Export extends Admin_Controller
{

    public function __construct()
    {
        parent::__construct();
		 if(user_type()!="admin"){
		     redirect('Dashboard', 'refresh');
		  }
    }

    public function index()
    {
	    if($this->config->item('offline')=='NO'){
		redirect('Export/import','refresh');
		}
	    $data=array();
		$data['transfer_data']=$this->get_offline_transfer();
		$data['payment_data']=$this->get_offline_payment();
        $this->load->view('theme/export/export',$data);
    }
	
	public function import()
    {   
	    if($this->config->item('offline')=='YES'){
		redirect('Export','refresh');
		}
        $this->load->view('theme/export/import');
    }
	
	public function process_import()
    {   
	    $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'sql';
        $config['remove_spaces'] = true;

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
          
            $data=array();
			$data['error'] = $this->upload->display_errors();
            $this->load->view('theme/export/import', $data);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $file_name=$data['upload_data']['file_name'];
			
			$this->load->helper('file');
            $schema = read_file('./uploads/'.$file_name);			
			$query      = rtrim(trim($schema), "\n;");
			$query_list = explode(";", $query);
			
			foreach ($query_list as $query){
				$this->db->query($query);
			}
			
			unlink('./uploads/'.$file_name);
			
			
            $this->session->set_flashdata('success', 'Data Imported Sucessfully');
			redirect('Export/import', 'refresh');
        }
    }
	
	public function get_offline_transfer(){
		$query=$this->db->query("SELECT * FROM sending_money WHERE offline='yes'");
		return $query->result();

	}
	
	public function get_offline_payment(){
		$query=$this->db->query("SELECT * FROM payment WHERE offline='yes'");
		return $query->result();
	}
	
	
	public function export_data(){
	  $transfer=$this->get_offline_transfer();
	  $sql="INSERT INTO sending_money(sending_type, sending_code, sending_date, sender_fullname, sender_city, sender_passport, passport_exp_date, sending_amount, cost, total_amount, telephone, secret_question, secret_answer, recipent_fullname, recipent_phone, recipent_city, org_address, distribution_number, name_of_project, agent_id, status, trash, offline) VALUES";
	    
	  foreach($transfer as $key){
	   $sql.="\n(";
	   $sql.="'".$key->sending_type."',";
	   $sql.="'".$key->sending_code."',";
	   $sql.="'".$key->sending_date."',";
	   $sql.="'".$key->sender_fullname."',";
	   $sql.="'".$key->sender_city."',";
	   $sql.="'".$key->sender_passport."',";
	   $sql.="'".$key->passport_exp_date."',";
	   $sql.="'".$key->sending_amount."',";
	   $sql.="'".$key->cost."',";
	   $sql.="'".$key->total_amount."',";
	   $sql.="'".$key->telephone."',";
	   $sql.="'".$key->secret_question."',";
	   $sql.="'".$key->secret_answer."',";
	   $sql.="'".$key->recipent_fullname."',";
	   $sql.="'".$key->recipent_phone."',";
	   $sql.="'".$key->recipent_city."',";
	   $sql.="'".$key->org_address."',";
	   $sql.="'".$key->distribution_number."',";
	   $sql.="'".$key->name_of_project."',";
	   $sql.="'".$key->agent_id."',";
	   $sql.="'".$key->status."',";
	   $sql.="'".$key->trash."',";
	   $sql.="'no'";
	   $sql.="),";
	  }
	  $sql=substr_replace($sql, "", -1);
	  $sql.=";";
	  
	  $sql.="\n\n\n";

	  $payment=$this->get_offline_payment();
	  $sql.="INSERT INTO payment(payment_type, sending_code, date_of_sending, sender_fullname, shipping_city, sender_phone, payable_amount, secret_question, secret_answer, recipent_fullname, recipent_passport, passport_exp_date, payment_city, Recipient_CNI, Recipient_Phone, Recipient_address, Distribution_Number, Name_of_project, paying_date, agent_id, trash, offline) VALUES";
	  
	  foreach($payment as $key){
	   $sql.="\n(";
	   $sql.="'".$key->payment_type."',";
	   $sql.="'".$key->sending_code."',";
	   $sql.="'".$key->date_of_sending."',";
	   $sql.="'".$key->sender_fullname."',";
	   $sql.="'".$key->shipping_city."',";
	   $sql.="'".$key->sender_phone."',";
	   $sql.="'".$key->payable_amount."',";
	   $sql.="'".$key->secret_question."',";
	   $sql.="'".$key->secret_answer."',";
	   $sql.="'".$key->recipent_fullname."',";
	   $sql.="'".$key->recipent_passport."',";
	   $sql.="'".$key->passport_exp_date."',";
	   $sql.="'".$key->payment_city."',";
	   $sql.="'".$key->Recipient_CNI."',";   
	   $sql.="'".$key->Recipient_Phone."',";
	   $sql.="'".$key->Recipient_address."',";
	   $sql.="'".$key->Distribution_Number."',";   
	   $sql.="'".$key->Name_of_project."',";   
	   $sql.="'".$key->paying_date."',";
	   $sql.="'".$key->agent_id."',";
	   $sql.="'".$key->trash."',";
	   $sql.="'no'";
	   $sql.="),";
	  }
	  $sql=substr_replace($sql, "", -1);
	  $sql.=";";
	  
	  $data=array();
	  $data['offline']='no';
	  $this->db->where('offline','yes');
	  $this->db->update('sending_money',$data);
	  
	  $this->db->where('offline','yes');
	  $this->db->update('payment',$data);
	    
	  $this->load->helper('download');
      $name = 'export.sql';
      force_download($name, $sql);
	  //echo "<pre>".$sql."</pre>";
	}

   
}