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

class TambahAbsen extends CI_Controller {

	public function __construct()
    {
		
        parent::__construct();
        $this->load->model("UserModel");
		$this->load->model("AbsenModel");
		$this->load->model("AbsensiModel");
		$this->load->model("UmkmModel");
		$this->load->model("LaporanModel");

		if($this->UserModel->isNotLogin()) {
			redirect(site_url('login'));
		} elseif(!($this->UserModel->isAdmin())) {
			redirect(site_url('dashboard'));
		} 
		
    }

	public function index()
	{
		$data['tahun']=date('Y');
		if($this->input->post()){
			
			$post = $this->input->post();
			$data['tahun']=$post['tahun'];
			$cek_absen = $this->AbsenModel->getAbsenCountByBulanAndYear($post['bulan'],$post['tahun']);

			if($cek_absen>0){
				$data['msg']="error!";
				
			} else {
				$cek_laporan = $this->LaporanModel->getLaporanCountByYear($post['tahun']);
				if($cek_laporan>0){
					$data['error_tutup_tahun']=$post['tahun'];
				} else {

					if($post['bulan']!=='1'){
						$cek_last_month_absen = $this->AbsenModel->getLastMonthAbsenCountByBulanAndYear($post['bulan'],$post['tahun']);
						if($cek_last_month_absen<1){
	
							if($post['bulan']=='2'){
								$last_mont = 'Januari';
							} elseif($post['bulan']=='3'){
								$last_mont = 'Februari';
							} elseif($post['bulan']=='4'){
								$last_mont = 'Maret';
							} elseif($post['bulan']=='5'){
								$last_mont = 'April';
							} elseif($post['bulan']=='6'){
								$last_mont = 'Mei';
							} elseif($post['bulan']=='7'){
								$last_mont = 'Juni';
							} elseif($post['bulan']=='8'){
								$last_mont = 'Juli';
							} elseif($post['bulan']=='9'){
								$last_mont = 'Agustus';
							} elseif($post['bulan']=='10'){
								$last_mont = 'September';
							} elseif($post['bulan']=='11'){
								$last_mont = 'Oktober';
							} elseif($post['bulan']=='12'){
								$last_mont = 'November';
							} else {
								$last_mont = 'n/a';
							}
	
							$data['error_last_mont']=$last_mont;
	
						} else {
							$jumlahumkm = $this->UmkmModel->getUmkmCount();
							
							$listlastabsensi = $this->AbsensiModel->getLastMonthAbsensi($post['bulan'],$post['tahun']);
	
							$omset = 0;
							$aset = 0;
							$karyawan_l = 0;
							$karyawan_p = 0;
	
							foreach($listlastabsensi as $lastabsensi){
								$aset = $aset + $lastabsensi->aset;
								$karyawan_l = $karyawan_l + $lastabsensi->karyawan_l;
								$karyawan_p = $karyawan_p + $lastabsensi->karyawan_p;
							}
	
							if($this->AbsenModel->save($jumlahumkm,$omset,$aset,$karyawan_l,$karyawan_p)){
								$daftarumkm = $this->UmkmModel->getUmkm();
								$new_absen = $this->AbsenModel->getAbsenByBulanAndYear($post['bulan'],$post['tahun']);
								$id_absen = $new_absen->id_absen;
								$bulan_absen = $new_absen->bulan;
								$tahun_absen =  $new_absen->tahun;
	
								$this->AbsenModel->setOpenAbsen($id_absen);
	
								foreach($daftarumkm as $umkm){
									if($bulan_absen!=='1'){
										$latestabsensi = $this->AbsensiModel->getLatestAbsensi($post['bulan'],$post['tahun'],$umkm->id_umkm);
										$aset_absensi= $latestabsensi->aset;
										$karyawan_absensi_l = $latestabsensi->karyawan_l;
										$karyawan_absensi_p = $latestabsensi->karyawan_p;
									} else {
										$aset_absensi= $umkm->aset_awal;
										$karyawan_absensi_l= $umkm->karyawan_l;
										$karyawan_absensi_p= $umkm->karyawan_p;
									}
									$omset_absensi= 0;
									$this->AbsensiModel->save($id_absen,$umkm->id_umkm,$umkm->id_pemilik,$umkm->kecamatan,$umkm->kelurahan,$bulan_absen,$tahun_absen,$omset_absensi,$aset_absensi,$karyawan_absensi_l,$karyawan_absensi_p);
									
								}
	
								$data['msg']="success";
								redirect(site_url('admin/absensi'));
							} else {
								$data['msg']="error!";
							}
						}
					} else {
	
					$jumlahumkm = $this->UmkmModel->getUmkmCount();
					$listumkm = $this->UmkmModel->getUmkm();
	
					$omset = 0;
					$aset = 0;
					$karyawan_l = 0;
					$karyawan_p = 0;

					foreach($listumkm as $umkm){
						$aset = $aset + $umkm->aset_awal;
						$karyawan_l = $karyawan_l + $umkm->karyawan_l;
						$karyawan_p = $karyawan_p + $umkm->karyawan_p;
					}
	
					if($this->AbsenModel->save($jumlahumkm,$omset,$aset,$karyawan_l,$karyawan_p)){
						$daftarumkm = $this->UmkmModel->getUmkm();
						$new_absen = $this->AbsenModel->getAbsenByBulanAndYear($post['bulan'],$post['tahun']);
						$id_absen = $new_absen->id_absen;
						$bulan_absen = $new_absen->bulan;
						$tahun_absen =  $new_absen->tahun;
	
						$this->AbsenModel->setOpenAbsen($id_absen);
	
						foreach($daftarumkm as $umkm){
							if($bulan_absen!=='1'){
								$latestabsensi = $this->AbsensiModel->getLatestAbsensi($post['bulan'],$post['tahun'],$umkm->id_umkm);
								$aset_absensi= $latestabsensi->aset;
								$karyawan_absensi_l = $latestabsensi->karyawan_l;
								$karyawan_absensi_p = $latestabsensi->karyawan_p;
							} else {
								$aset_absensi= $umkm->aset_awal;
								$karyawan_absensi_l= $umkm->karyawan_l;
								$karyawan_absensi_p= $umkm->karyawan_p;
							}
							$omset_absensi= 0;
							$this->AbsensiModel->save($id_absen,$umkm->id_umkm,$umkm->id_pemilik,$umkm->kecamatan,$umkm->kelurahan,$bulan_absen,$tahun_absen,$omset_absensi,$aset_absensi,$karyawan_absensi_l,$karyawan_absensi_p);
							
						}
	
						$data['msg']="success";
						redirect(site_url('admin/absensi'));
					  } else {
						$data['msg']="error!";
					  }
	
					}
				}
				

			}
         
        }


			$data['user_logged'] = $this->session->userdata('user_logged');
			$this->load->view('admin/tambahabsen',$data);
		
	}

	
}
