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

class Rincian_gaji_karyawan extends MX_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->page->use_directory();
		$this->load->model('model_laporan_penggajian');	
	}

	public function index(){
		$this->page->view('rincian_gaji_karyawan_form', array(
			'pdf'			=> $this->page->base_url('/download/'),
			'formImportExcel'	=> $this->page->base_url('/formImportExcel'),
		));
	}
	public function formImportExcel() {
		$this->page->view('presensi_form_excel', array (
			'back'			=> $this->agent->referrer(),
			'action'		=> $this->page->base_url("/importExcel"),
		));
	}

	public function importExcel() {
		$this->load->library('Excel_reader');

		$target = basename($_FILES['import_excel']['name']) ;
		move_uploaded_file($_FILES['import_excel']['tmp_name'], $target);
    
		$data = new Spreadsheet_Excel_Reader($_FILES['import_excel']['name'],false);
    
//    menghitung jumlah baris file xls
		$baris = $data->rowcount($sheet_index=0);
    
//    import data excel mulai baris ke-2 (karena tabel xls ada header pada baris 1)
		for ($i=2; $i<=$baris; $i++)
		{
//       membaca data (kolom ke-1 sd terakhir)
			
			$nama	= $data->val($i, 1);
			$tanggal		= $data->val($i, 2);
			$pendapatan		= $data->val($i, 3);
			$potongan		= $data->val($i, 4);
			$gaji_bersih		= $data->val($i, 5);
			$id    = $data->val($i, 6);
			 $id_karyawan	= $this->db->get_where('karyawan', array('nama' => $nama))->row();
			// if (empty($id_karyawan)) {
			// 	$insertkaryawan = $this->db->insert('presensi',['nama'=>$id_karyawan]);
			// 	$id_karyawan = $insertkaryawan;
			// }

//      setelah data dibaca, masukkan ke tabel pegawai sql
			$hasil=array();
			if (!empty(@$id_karyawan)) {
				$dtsoal = [
					'id' => $nama,
					'id_karyawan'	=> $id_karyawan->nama,
					'tgl_gaji'		=> date('Y-m-d',strtotime($tanggal)),
					'pendapatan'		=> $pendapatan,
					'potongan'	=> $potongan,
					'gaji_bersih'		=> $gaji_bersih,
				];
				$hasil = $this->db->insert('transaksi_gaji',$dtsoal);
			}
		}
    
		if(!$hasil){
//          jika import gagal
			$this->session->set_flashdata('info','Data berhasil diimpor.');
		}else{
//          jika impor berhasil
			$this->session->set_flashdata('info','Data berhasil diimpor.');
		}
    
//    hapus file xls yang udah dibaca
		unlink($_FILES['import_excel']['name']);

		redirect($this->page->base_url());
	}
	
	public function options_slipgaji(){
		$slipgaji = $this->db->order_by('id')->get('transaksi_gaji');
		return options($slipgaji, 'id', NULL, 'id');
	}
	
	public function ajax_rinciangaji_options(){
		$no_slip = $this->input->post('no_slip');
		$karyawan = $this->model_laporan_penggajian->show_noslip_options($no_slip);
		echo $karyawan->id_karyawan.'#'.$karyawan->nama.'#'.$karyawan->alamat.'#'.$karyawan->jabatan.'#'.$karyawan->pendapatan.'#'.$karyawan->potongan.'#'.$karyawan->gaji_bersih;
	}
	
	public function ajax_list_perkiraan_gaji(){
		$no_slip = $this->input->post('no_slip');
		$list_gaji = $this->model_laporan_penggajian->show_list_gaji($no_slip);
		echo json_encode($list_gaji);
	}
	
	public function pdf($no_slip) {
		$this->load->library('pdf');
		$content = $this->load->view('rincian_gaji_karyawan_pdf', array (
			'karyawan' 		=> $this->model_laporan_penggajian->show_noslip_options($no_slip),
			'list_gaji'  	=> $this->model_laporan_penggajian->show_list_gaji($no_slip)
		), TRUE);
		$this->pdf->create($content, 'rincian_gaji');
	}
	
	public function download() {
		$no_slip = $this->input->post('no_slip');
		$this->load->helper('download');
		$data = file_get_contents(site_url('/laporan/rincian_gaji_karyawan/pdf/'.$no_slip));
		force_download('rincian_gaji_'.$no_slip.'.pdf', $data); 
	}
	
	
}

/* End of file Rincian_gaji_karyawan.php */
/* Location: ./application/modules/laporan/controllers/Rincian_gaji_karyawan.php */