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

class Keluar extends CI_Controller
{

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

	public function index()
	{
		$data['notiket'] = $this->Mkeluar->getnotiket();
		// $this->load->view('header');
		$this->load->view('formkeluar', $data);
	}

	private function rupiah($angka)
	{

		$hasil_rupiah = "Rp " . number_format($angka, 2, ',', '.');
		return $hasil_rupiah;
	}

	private function tgl_indo($tanggal)
	{
		$bulan = array(
			1 =>   'Januari',
			'Februari',
			'Maret',
			'April',
			'Mei',
			'Juni',
			'Juli',
			'Agustus',
			'September',
			'Oktober',
			'November',
			'Desember'
		);
		$pecahkan = explode('-', $tanggal);
		return $pecahkan[2] . ' ' . $bulan[(int)$pecahkan[1]] . ' ' . $pecahkan[0];
	}

	private function kirimpesan($pesan = '', $nomor = '')
	{
		$curl = curl_init();

		curl_setopt_array($curl, array(
			CURLOPT_URL => 'https://api.fonnte.com/send',
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'POST',
			CURLOPT_POSTFIELDS => array(
				'target' => $nomor,
				'message' => $pesan,
				'countryCode' => '62', //optional
			),
			CURLOPT_HTTPHEADER => array(
				'Authorization: 4MAmMTi7mNLmA#ybfA8C' //change TOKEN to your actual token
			),
		));

		curl_exec($curl);

		curl_close($curl);
	}

	public function savedata()
	{
		$data_setting = $this->db->select("harga_buah,potongan_ramp")->get("setting")->row();
		if ($this->input->post('type') == 1) {
			$timbangan = $this->input->post('timbangan');
			$notiket = $this->input->post('notiket');
			$nopolisi = $this->input->post('nopolisi');
			$namasupir = $this->input->post('namasupir');
			$namamuatan = $this->input->post('namamuatan');
			$berattotal = $this->input->post('berattotal');
			$beratkosong = $this->input->post('beratkosong');
			$beratmuatan = $this->input->post('beratmuatan');
			// $baseFromJavascript = $this->input->post('base64image');
			// $base_to_php = explode(',', $baseFromJavascript);
			// $data = base64_decode($base_to_php[1]);
			// $imagename = 'imgK_'.date('m-d-Y_hia').'.png';
			// $filepath = "./uploads/".$imagename; 
			// file_put_contents($filepath,$data);
			$data_all = array(
				'timbangan' => $timbangan,
				'notiket' => $notiket,
				'nopolisi' => $nopolisi,
				'namasupir' => $namasupir,
				'namamuatan' => $namamuatan,
				'berattotal' => $berattotal,
				'beratkosong' => $beratkosong,
				'beratmuatan' => $beratmuatan,
				'harga_tbs' => $data_setting->harga_buah,
				'potongan' => $data_setting->potongan_ramp
				// 'createdate' => date("Y-m-d"),
				// 'image'=>$imagename,
			);
			$data = $this->Mkeluar->saverecords($data_all);
			echo json_encode(array(
				"statusCode" => 200,
				"id" => $data,
			));
		}
	}

	public function cekdetailtiket()
	{
		$notiket	= $this->input->post('kode');
		$data = $this->Mkeluar->cekdetailtiket($notiket);

		header('Content-Type: application/json');
		echo json_encode($data);
	}

	public function printtiket($id = '')
	{
		$id = $id;
		$this->load->library('pdfgenerator');
		$data['keluar'] = $this->Mkeluar->getdatacetak($id)->row();
		$data["setting"] = $this->db->get("setting")->row();
		$file_pdf = 'kwitansi_' . $data['keluar']->notiket;
		$paper = 'A4';
		$orientation = "landscape";
		// $html = $this->load->view('printtiketkeluar', $data, true);
		// $this->pdfgenerator->generate($html, $file_pdf, $paper, $orientation);

		$tgl = explode(" ", $data['keluar']->createdate);
		$tgl_indo = $this->tgl_indo($tgl[0]);
		$potongan = ceil($data['keluar']->beratmuatan * $data['keluar']->potongan / 100);
		$netto = $data['keluar']->beratmuatan - $potongan;
		$bayar = $this->rupiah($netto * $data['keluar']->harga_tbs);
		$pesan = "*Informasi pembelian TBS:* \n"
			. "Tanggal: {$tgl_indo} \n"
			. "No. Tiket: {$data['keluar']->notiket} \n"
			. "No. Kendaraan: {$data['keluar']->nopolisi} \n"
			. "Nama Supir: {$data['keluar']->namasupir} \n"
			. "Netto + potongan: {$netto} \n"
			. "Harga TBS: {$data['keluar']->harga_tbs} \n"
			. "Total Bayar: $bayar \n";
		if ($data["setting"]->service_wa == '1') {
			$this->kirimpesan($pesan, $data["setting"]->nomor_hp);
		}

		$this->load->view('printtiketkeluar', $data);
	}
}

/* End of file Keluar.php */
/* Location: ./application/controllers/Keluar.php */