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

class LaporanExcel extends CI_Controller {

	public function __construct()
    {
		
        parent::__construct();
        $this->load->model("UserModel");
        $this->load->model("LaporanModel");
        $this->load->model("KategoriModel");
        $this->load->model("JenisModel");

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

	public function index()
	{
			
			//cek ada laporan tahunan atau tidak
			$countlaporan = $this->LaporanModel->getLaporanCount();
			if($countlaporan>0){
				$year = $this->LaporanModel->getLastYearLaporan()->tahun;
				$data['tahun'] = $year;
				$lap_bulan = $this->LaporanModel->getBulanLaporanByYear($year);

				$omset_jen1=0;
				$omset_jen2=0;
				$omset_jen3=0;
				
				

				foreach($lap_bulan as $bulan){
					$umkm_jen1=0;
					$umkm_jen2=0;
					$umkm_jen3=0;

					$umkm_jen1_ket1=0;
					$umkm_jen1_ket2=0;
					$umkm_jen1_ket3=0;
					$umkm_jen2_ket1=0;
					$umkm_jen2_ket2=0;
					$umkm_jen2_ket3=0;
					$umkm_jen3_ket1=0;
					$umkm_jen3_ket2=0;
					$umkm_jen3_ket3=0;
					
					$aset_jen1=0;
					$aset_jen2=0;
					$aset_jen3=0;
					$naker_jen1_l=0;
					$naker_jen1_p=0;
					$naker_jen2_l=0;
					$naker_jen2_p=0;
					$naker_jen3_l=0;
					$naker_jen3_p=0;
					$lap_kelurahan = $this->LaporanModel->getKelurahanByBulanAndYear($bulan->bulan,$year);
					
					foreach($lap_kelurahan as $kelurahan){
						$laporan = $this->LaporanModel->getLaporanByBulanByYearByKelurahan($bulan->bulan,$year,$kelurahan->kelurahan);
						$umkm_jen1= $umkm_jen1+$laporan->umkm_jen1_kat1+$laporan->umkm_jen1_kat2+$laporan->umkm_jen1_kat3;
						$umkm_jen2= $umkm_jen2+$laporan->umkm_jen2_kat1+$laporan->umkm_jen2_kat2+$laporan->umkm_jen2_kat3;
						$umkm_jen3= $umkm_jen3+$laporan->umkm_jen3_kat1+$laporan->umkm_jen3_kat2+$laporan->umkm_jen3_kat3;
						$omset_jen1= $omset_jen1+$laporan->omset_jenis1;
						$omset_jen2= $omset_jen2+$laporan->omset_jenis2;
						$omset_jen3= $omset_jen3+$laporan->omset_jenis3;
						$aset_jen1= $aset_jen1+$laporan->aset_jenis1;
						$aset_jen2= $aset_jen2+$laporan->aset_jenis2;
						$aset_jen3= $aset_jen3+$laporan->aset_jenis3;
						$naker_jen1_l = $naker_jen1_l + $laporan->karyawan_jen1_l;
						$naker_jen1_p = $naker_jen1_p + $laporan->karyawan_jen1_p;
						$naker_jen2_l = $naker_jen2_l + $laporan->karyawan_jen2_l;
						$naker_jen2_p = $naker_jen2_p + $laporan->karyawan_jen2_p;
						$naker_jen3_l = $naker_jen3_l + $laporan->karyawan_jen3_l;
						$naker_jen3_p = $naker_jen3_p + $laporan->karyawan_jen3_p;

						$umkm_jen1_ket1=$umkm_jen1_ket1+$laporan->umkm_jen1_kat1;
						$umkm_jen1_ket2=$umkm_jen1_ket2+$laporan->umkm_jen1_kat2;
						$umkm_jen1_ket3=$umkm_jen1_ket3+$laporan->umkm_jen1_kat3;
						$umkm_jen2_ket1=$umkm_jen2_ket1+$laporan->umkm_jen2_kat1;
						$umkm_jen2_ket2=$umkm_jen2_ket2+$laporan->umkm_jen2_kat2;
						$umkm_jen2_ket3=$umkm_jen2_ket3+$laporan->umkm_jen2_kat3;
						$umkm_jen3_ket1=$umkm_jen3_ket1+$laporan->umkm_jen3_kat1;
						$umkm_jen3_ket2=$umkm_jen3_ket2+$laporan->umkm_jen3_kat2;
						$umkm_jen3_ket3=$umkm_jen3_ket3+$laporan->umkm_jen3_kat3;
						
					}
					
					
				}

				$data['umkm_jen1_ket1']=$umkm_jen1_ket1;
				$data['umkm_jen1_ket2']=$umkm_jen1_ket2;
				$data['umkm_jen1_ket3']=$umkm_jen1_ket3;
				$data['umkm_jen2_ket1']=$umkm_jen2_ket1;
				$data['umkm_jen2_ket2']=$umkm_jen2_ket2;
				$data['umkm_jen2_ket3']=$umkm_jen2_ket3;
				$data['umkm_jen3_ket1']=$umkm_jen3_ket1;
				$data['umkm_jen3_ket2']=$umkm_jen3_ket2;
				$data['umkm_jen3_ket3']=$umkm_jen3_ket3;

				
				$omset_bln_jen1= $omset_jen1/12;
				$omset_bln_jen2= $omset_jen2/12;
				$omset_bln_jen3= $omset_jen3/12;

				

				$data['umkm_jen1']=$umkm_jen1;
				$data['umkm_jen2']=$umkm_jen2;
				$data['umkm_jen3']=$umkm_jen3;

				$data['omset_bln_jen1'] = $omset_bln_jen1;
				$data['omset_bln_jen2'] = $omset_bln_jen2;
				$data['omset_bln_jen3'] = $omset_bln_jen3;

				$data['aset_jen1'] = $aset_jen1;
				$data['aset_jen2'] = $aset_jen2;
				$data['aset_jen3'] = $aset_jen3;
				$data['naker_jen1_l'] = $naker_jen1_l;
				$data['naker_jen1_p'] = $naker_jen1_p;
				$data['naker_jen2_l'] = $naker_jen2_l;
				$data['naker_jen2_p'] = $naker_jen2_p;
				$data['naker_jen3_l'] = $naker_jen3_l;
				$data['naker_jen3_p'] = $naker_jen3_p;

				//komulatif
				$data['omset_1'] = $omset_bln_jen1+$omset_bln_jen2+$omset_bln_jen3;
				$data['aset_1'] = $aset_jen1+$aset_jen2+$aset_jen3;
				$data['umkm_1'] = $umkm_jen1+$umkm_jen2+$umkm_jen3;
				$naker_1 = $naker_jen1_l+$naker_jen2_l+$naker_jen3_l;
				$data['naker_1'] = $naker_1+$naker_jen1_p+$naker_jen2_p+$naker_jen3_p;
				$data['mikro_1'] = $umkm_jen1_ket1+$umkm_jen2_ket1+$umkm_jen3_ket1;
				$data['kecil_1'] = $umkm_jen1_ket2+$umkm_jen2_ket2+$umkm_jen3_ket2;
				$data['menengah_1'] = $umkm_jen1_ket3+$umkm_jen2_ket3+$umkm_jen3_ket3;
				
				
				//cek ada laporan tahun sebelumnya atau tidak
				$countlaporanbefore = $this->LaporanModel->getLaporanBeforeCount();
				if($countlaporanbefore>0){
					$yearbefore = $this->LaporanModel->getLastYearLaporanBefore()->tahun;
					$data['tahun_0'] = $yearbefore;

					$lap_bulan_0 = $this->LaporanModel->getBulanLaporanByYear($yearbefore);

					$omset_jen1_0=0;
					$omset_jen2_0=0;
					$omset_jen3_0=0;
					
					

					foreach($lap_bulan_0 as $bulan_0){
						$umkm_jen1_0=0;
						$umkm_jen2_0=0;
						$umkm_jen3_0=0;

						$umkm_jen1_ket1_0=0;
						$umkm_jen1_ket2_0=0;
						$umkm_jen1_ket3_0=0;
						$umkm_jen2_ket1_0=0;
						$umkm_jen2_ket2_0=0;
						$umkm_jen2_ket3_0=0;
						$umkm_jen3_ket1_0=0;
						$umkm_jen3_ket2_0=0;
						$umkm_jen3_ket3_0=0;
						
						$aset_jen1_0=0;
						$aset_jen2_0=0;
						$aset_jen3_0=0;
						$naker_jen1_l_0=0;
						$naker_jen1_p_0=0;
						$naker_jen2_l_0=0;
						$naker_jen2_p_0=0;
						$naker_jen3_l_0=0;
						$naker_jen3_p_0=0;
						$lap_kelurahan_0 = $this->LaporanModel->getKelurahanByBulanAndYear($bulan_0->bulan,$yearbefore);
						
						foreach($lap_kelurahan_0 as $kelurahan_0){
							$laporan_0 = $this->LaporanModel->getLaporanByBulanByYearByKelurahan($bulan_0->bulan,$yearbefore,$kelurahan_0->kelurahan);
							$umkm_jen1_0= $umkm_jen1_0+$laporan_0->umkm_jen1_kat1+$laporan_0->umkm_jen1_kat2+$laporan_0->umkm_jen1_kat3;
							$umkm_jen2_0= $umkm_jen2_0+$laporan_0->umkm_jen2_kat1+$laporan_0->umkm_jen2_kat2+$laporan_0->umkm_jen2_kat3;
							$umkm_jen3_0= $umkm_jen3_0+$laporan_0->umkm_jen3_kat1+$laporan_0->umkm_jen3_kat2+$laporan_0->umkm_jen3_kat3;
							$omset_jen1_0= $omset_jen1_0+$laporan_0->omset_jenis1;
							$omset_jen2_0= $omset_jen2_0+$laporan_0->omset_jenis2;
							$omset_jen3_0= $omset_jen3_0+$laporan_0->omset_jenis3;
							$aset_jen1_0= $aset_jen1_0+$laporan_0->aset_jenis1;
							$aset_jen2_0= $aset_jen2_0+$laporan_0->aset_jenis2;
							$aset_jen3_0= $aset_jen3_0+$laporan_0->aset_jenis3;
							$naker_jen1_l_0 = $naker_jen1_l_0 + $laporan_0->karyawan_jen1_l;
							$naker_jen1_p_0 = $naker_jen1_p_0 + $laporan_0->karyawan_jen1_p;
							$naker_jen2_l_0 = $naker_jen2_l_0 + $laporan_0->karyawan_jen2_l;
							$naker_jen2_p_0 = $naker_jen2_p_0 + $laporan_0->karyawan_jen2_p;
							$naker_jen3_l_0 = $naker_jen3_l_0 + $laporan_0->karyawan_jen3_l;
							$naker_jen3_p_0 = $naker_jen3_p_0 + $laporan_0->karyawan_jen3_p;

							$umkm_jen1_ket1_0=$umkm_jen1_ket1_0+$laporan_0->umkm_jen1_kat1;
							$umkm_jen1_ket2_0=$umkm_jen1_ket2_0+$laporan_0->umkm_jen1_kat2;
							$umkm_jen1_ket3_0=$umkm_jen1_ket3_0+$laporan_0->umkm_jen1_kat3;
							$umkm_jen2_ket1_0=$umkm_jen2_ket1_0+$laporan_0->umkm_jen2_kat1;
							$umkm_jen2_ket2_0=$umkm_jen2_ket2_0+$laporan_0->umkm_jen2_kat2;
							$umkm_jen2_ket3_0=$umkm_jen2_ket3_0+$laporan_0->umkm_jen2_kat3;
							$umkm_jen3_ket1_0=$umkm_jen3_ket1_0+$laporan_0->umkm_jen3_kat1;
							$umkm_jen3_ket2_0=$umkm_jen3_ket2_0+$laporan_0->umkm_jen3_kat2;
							$umkm_jen3_ket3_0=$umkm_jen3_ket3_0+$laporan_0->umkm_jen3_kat3;
							
						}
						
						
					}

					$data['umkm_jen1_ket1_0']=$umkm_jen1_ket1_0;
					$data['umkm_jen1_ket2_0']=$umkm_jen1_ket2_0;
					$data['umkm_jen1_ket3_0']=$umkm_jen1_ket3_0;
					$data['umkm_jen2_ket1_0']=$umkm_jen2_ket1_0;
					$data['umkm_jen2_ket2_0']=$umkm_jen2_ket2_0;
					$data['umkm_jen2_ket3_0']=$umkm_jen2_ket3_0;
					$data['umkm_jen3_ket1_0']=$umkm_jen3_ket1_0;
					$data['umkm_jen3_ket2_0']=$umkm_jen3_ket2_0;
					$data['umkm_jen3_ket3_0']=$umkm_jen3_ket3_0;

					
					$omset_bln_jen1_0= $omset_jen1_0/12;
					$omset_bln_jen2_0= $omset_jen2_0/12;
					$omset_bln_jen3_0= $omset_jen3_0/12;

					

					$data['umkm_jen1_0']=$umkm_jen1_0;
					$data['umkm_jen2_0']=$umkm_jen2_0;
					$data['umkm_jen3_0']=$umkm_jen3_0;

					$data['omset_bln_jen1_0'] = $omset_bln_jen1_0;
					$data['omset_bln_jen2_0'] = $omset_bln_jen2_0;
					$data['omset_bln_jen3_0'] = $omset_bln_jen3_0;
					$data['aset_jen1_0'] = $aset_jen1_0;
					$data['aset_jen2_0'] = $aset_jen2_0;
					$data['aset_jen3_0'] = $aset_jen3_0;
					$data['naker_jen1_l_0'] = $naker_jen1_l_0;
					$data['naker_jen1_p_0'] = $naker_jen1_p_0;
					$data['naker_jen2_l_0'] = $naker_jen2_l_0;
					$data['naker_jen2_p_0'] = $naker_jen2_p_0;
					$data['naker_jen3_l_0'] = $naker_jen3_l_0;
					$data['naker_jen3_p_0'] = $naker_jen3_p_0;

					//komulatif
				$data['omset_0'] = $omset_bln_jen1_0+$omset_bln_jen2_0+$omset_bln_jen3_0;
				$data['aset_0'] = $aset_jen1_0+$aset_jen2_0+$aset_jen3_0;
				$data['umkm_0'] = $umkm_jen1_0+$umkm_jen2_0+$umkm_jen3_0;
				$naker_0 = $naker_jen1_l_0+$naker_jen2_l_0+$naker_jen3_l_0;
				$data['naker_0'] = $naker_0+$naker_jen1_p_0+$naker_jen2_p_0+$naker_jen3_p_0;
				$data['mikro_0'] = $umkm_jen1_ket1_0+$umkm_jen2_ket1_0+$umkm_jen3_ket1_0;
				$data['kecil_0'] = $umkm_jen1_ket2_0+$umkm_jen2_ket2_0+$umkm_jen3_ket2_0;
				$data['menengah_0'] = $umkm_jen1_ket3_0+$umkm_jen2_ket3_0+$umkm_jen3_ket3_0;

					
					
				}
				
				$data['msg'] = 'found';
			} else {
				$data['msg'] = 'empty';
			}
			

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

	
}
