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

class Developer_api extends CI_Controller { 

	public function __construct()
	{
		parent::__construct();
		header('X-Robots-Tag: noindex');
	}	
	function get_encrypt(){
		$sn_document = $this->input->get('doc_id');
		if(empty($sn_document)){
			exit('Unauthorized access!');
		}
		echo ci_encrypt($sn_document);
		//echo ci_decrypt($sn_document);
	}
	function memo()
	{
		$sn_document = $this->input->get('doc_id');
		$sn_document = ci_decrypt($sn_document);
		if(empty($sn_document)){
			exit('Unauthorized access!');
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$records = $this->memoInvoiceRecord($sn_document,'3');
		$records_sd = $this->specialDeal();
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		if(empty($records)){
			exit('Unauthorized access!');
		}
		$data['records'] = $records;
		$data['records_sd'] = $records_sd;

		$this->load->view('memo_invoice/memo',$data);
	}	
	function invoice()
	{
		$sn_document = $this->input->get('doc_id');
		$sn_document = ci_decrypt($sn_document);
		if(empty($sn_document)){
			exit('Unauthorized access!');
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$records = $this->memoInvoiceRecord($sn_document,'5');
		$records_sd = $this->specialDeal();
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		if(empty($records)){
			exit('Unauthorized access!');
		}
		$data['records'] = $records;
		$data['records_sd'] = $records_sd;
		
		$this->load->view('memo_invoice/invoice',$data);
	}	
	function invoice_packing()
	{
		$sn_document = $this->input->get('doc_id');
		$sn_document = ci_decrypt($sn_document);
		if(empty($sn_document)){
			exit('Unauthorized access!');
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$records = $this->memoInvoiceRecord($sn_document,'5');
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		if(empty($records)){
			exit('Unauthorized access!');
		}
		$data['records'] = $records;

		$this->load->view('memo_invoice/invoice_packing',$data);
	}
	function open_memo()
	{
		$cd_ac = $this->input->get('doc_id');
		$cd_ac = ci_decrypt($cd_ac);
		if(empty($cd_ac)){
			exit('Unauthorized access!');
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$records = $this->openMemo($cd_ac);
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		if(empty($records)){
			exit('Unauthorized access!');
		}
		$data['records'] = $records;

		$this->load->view('memo_invoice/open_memo',$data);
	}

	private function memoInvoiceRecord($sn_document,$fl_document)
	{
		$field['sn_document'] = $sn_document;
		$field['fl_document'] = $fl_document;
		$records = array();
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$token = $this->get_token();
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$curl = curl_init();
		curl_setopt_array($curl, array(
		  CURLOPT_URL => APIURL.'/memo-invoice',
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 30,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "GET",
		  CURLOPT_POSTFIELDS => http_build_query($field),
		  CURLOPT_HTTPHEADER => array(
		    "Accept: */*",
		    "Authorization: Bearer ".$token,
		    "Content-Type: application/x-www-form-urlencoded",			    
		  ),
		));
		$response = curl_exec($curl);
		$err = curl_error($curl);
		curl_close($curl);

		if (empty($err)){
			$response = json_decode($response);
			$records = $response->record;
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

		return $records;
	}
	private function specialDeal()
	{
		$records = array();
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$token = $this->get_token();
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$curl = curl_init();
		curl_setopt_array($curl, array(
		  CURLOPT_URL => APIURL.'/special-deal',
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 30,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "GET",
		  CURLOPT_HTTPHEADER => array(
		    "Accept: */*",
		    "Authorization: Bearer ".$token			    
		  ),
		));
		$response = curl_exec($curl);

		$err = curl_error($curl);
		curl_close($curl);
		if (empty($err)){
			$response = json_decode($response);
			$records = $response->record;
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		return $records;
	}
	private function openMemo($cd_ac)
	{
		$field['cd_ac'] = $cd_ac;
		$records = array();
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$token = $this->get_token();
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$curl = curl_init();
		curl_setopt_array($curl, array(
		  CURLOPT_URL => APIURL.'/open-memo',
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 30,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "GET",
		  CURLOPT_POSTFIELDS => http_build_query($field),
		  CURLOPT_HTTPHEADER => array(
		    "Accept: */*",
		    "Authorization: Bearer ".$token,
		    "Content-Type: application/x-www-form-urlencoded",			    
		  ),
		));
		$response = curl_exec($curl);

		$err = curl_error($curl);
		curl_close($curl);
		if (empty($err)){
			$response = json_decode($response);
			$records = $response->record;
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

		return $records;
	}
	private function get_token() 
	{		
		$token = $this->session->tempdata('memo_token');
		if(empty($token)) 
		{
			$curl = curl_init();
			curl_setopt_array($curl, array(
			CURLOPT_URL => APIURL."/get-token",
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => "",
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 30,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => "POST",
			CURLOPT_POSTFIELDS => "username=".APIUSER."&password=".APIPASSWORD."",
			CURLOPT_HTTPHEADER => array(
				  "Accept: */*",
				  "Content-Type: application/x-www-form-urlencoded",
				),
			));
			$response = curl_exec($curl);
			$err = curl_error($curl);
			curl_close($curl);
			if (empty($err)){
				$response = json_decode($response, true);
				$token = $response['token'];
				$this->session->set_tempdata('memo_token',$token,3000);
			}	
		}	

		return $token;
	}

}
