<?php namespace App\Http\Controllers;

use App\Models\Rekapprognosa;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
use Validator, Input, Redirect ; 


class RekapprognosaController extends Controller {

	protected $layout = "layouts.main";
	protected $data = array();	
	public $module = 'rekapprognosa';
	static $per_page	= '50';

	public function __construct()
	{
		
		parent::__construct();
		$this->model = new Rekapprognosa();
		$this->info = $this->model->makeInfo( $this->module);
		$this->access = array();
	
		$this->data = array_merge(array(
			'pageTitle'	=> 	$this->info['title'],
			'pageNote'	=>  $this->info['note'],
			'pageModule'=> 'rekapprognosa',
			'return'	=> self::returnUrl()
			
		),$this->data);


		
	}

	public function index( Request $request )
	{
		// Make Sure users Logged 
		if(!\Auth::check()) 
			return redirect('user/login')->with('status', 'error')->with('message','You are not login');
		$table = $this->model->getTable();
		if ($request->has('filter_pg') && !empty($request->input('filter_pg'))) {
			$filter_pg = $request->input('filter_pg');
			$results = \DB::table($table)
			->where('pg_penggiling', $filter_pg)
			->get();
		} else {
			$table = 'q_rekap_prognosa_total';
			$results = \DB::table($table)->get();
		}
		if ($request->has('filter_periode') && !empty($request->input('filter_periode'))) {
			$filter_periode = $request->input('filter_periode');
			$results = \DB::table($table)
			->where('periode', $filter_periode)
			->get();
		}

		if ($request->has('filter_pg') && !empty($request->input('filter_pg')) && $request->has('filter_periode') && !empty($request->input('filter_periode'))) {
			$filter_pg = $request->input('filter_pg');
			$filter_periode = $request->input('filter_periode');
			$results = \DB::table($table)
			->where('pg_penggiling', $filter_pg)
			->where('periode', $filter_periode)
			->get();
		}

		$today = \Carbon\Carbon::now();
		$year = $today->year;
		$month = $today->month;
		$startA = \Carbon\Carbon::create($year, $month, 1);
		$endA = \Carbon\Carbon::create($year, $month, 15);
		$startB = \Carbon\Carbon::create($year, $month, 16);
		$endB = \Carbon\Carbon::create($year, $month, $startB->daysInMonth);
		$isA = $today->between($startA, $endA);
		$isB = $today->between($startB, $endB);

		$setperiode = '';
		if ($isA) {
			$setperiode = date('n').'A';
		} elseif ($isB) {
			$setperiode = date('n').'B';
		}

		$this->data['periode'] = $request->input('filter_periode', '') ? $request->input('filter_periode') : $setperiode;
		$this->data['tableGrid'] = $this->info['config']['grid'];
		$this->data['rowData'] = $results;

		// Render into template
		return view( $this->module.'.index',$this->data);
	}	

	function rekap_prognosa_kebun( Request $request )
	{
		// Make Sure users Logged 
		if(!\Auth::check()) 
			return redirect('user/login')->with('status', 'error')->with('message','You are not login');
		
		$p_periode = $request->input('periode', '');
		$p_bulan_tanam = $request->input('periode', '');
		$filter_pg = $request->input('filter_pg', '');

		$results = \DB::select('CALL sp_rekap_prognosa_kebun(?, ?,?)', [$p_periode, $p_bulan_tanam,$filter_pg]);

		$html = '<table class="table table-bordered table-striped" id="dataTable2">';
		$html .= '<thead><tr>
			<th>NAMA KEBUN</th>
			<th>KATEGORI</th>
			<th>REAL SD</th>
			<th>SISA</th>
			<th>PROGNOSA</th>
			<th>RKAP</th>
			<th>TAKMAR</th>
			<th>% thd RKAP</th>
			<th>% thd TAKMAR</th>
		</tr></thead>';
		$html .= '<tbody>';

		$tebu = 0;
		$luas = 0;
		$hablur = 0;

		$tebu_real = 0;
		$luas_real = 0;
		$hablur_real = 0;

		$tebu_sisa = 0;
		$luas_sisa = 0;
		$hablur_sisa = 0;

		$nama_kebun = '';
		foreach ($results as $row) {
			
			if($row->kategori == 'hablur') {
				$hablur += $row->prognosa;
				$hablur_sisa += $row->sisa;
				$hablur_real += $row->real_sd;
			} elseif($row->kategori == 'luas') {
				$luas += $row->prognosa;
				$luas_real += $row->real_sd;
				$luas_sisa += $row->sisa;
			} elseif($row->kategori == 'tebu') {
				$tebu_real += $row->real_sd;
				$tebu += $row->prognosa;
				$tebu_sisa += $row->sisa;
			}

					if($row->kategori == 'protas') {
						$prognosa = ($luas != 0) ? $tebu / $luas : 0;
						$real_sd = ($luas_real != 0) ? $tebu_real / $luas_real : 0;
						$sisa = ($luas_sisa != 0) ? $tebu_sisa / $luas_sisa : 0;						
					}else if($row->kategori == 'rendemen') {
						$prognosa = ($tebu != 0) ? ($hablur / $tebu) * 100 : 0;
						$real_sd = ($tebu_real != 0) ? ($hablur_real / $tebu_real) * 100 : 0;
						$sisa = ($tebu_sisa != 0) ? ($hablur_sisa / $tebu_sisa) * 100 : 0;
					} else {
						$prognosa = $row->prognosa;
						$real_sd = $row->real_sd;
						$sisa = $row->sisa;
					} 
					$html .= '<tr>';
					$html .= '<td>' . htmlspecialchars($row->nama_kebun) . '</td>';
					$html .= '<td>' . htmlspecialchars($row->kategori) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars(number_format($real_sd,2)) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars(number_format($sisa,2)) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars(number_format($prognosa,2)) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars(number_format($row->rkap,2)) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars(number_format($row->takmar,2)) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars($row->rkap != 0 ? number_format(($prognosa / $row->rkap) * 100, 2) : '-') . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars($row->takmar != 0 ? number_format(($prognosa / $row->takmar) * 100, 2) : '-') . '</td>';
					$html .= '</tr>';

		
		}
		$html .= '</tbody>';
		$html .= '</table>';
		return $html;
	}

	function rekap_prognosa_rayon( Request $request )
	{
		// Make Sure users Logged 
		if(!\Auth::check()) 
			return redirect('user/login')->with('status', 'error')->with('message','You are not login');
		
		$p_periode = $request->input('periode', '');
		$p_bulan_tanam = $request->input('periode', '');
		$filter_pg = $request->input('filter_pg', '');

		$results = \DB::select('CALL sp_rekap_prognosa_rayon(?, ?,?)', [$p_periode, $p_bulan_tanam,$filter_pg]);

		$html = '<table class="table table-bordered table-striped" id="dataTable3">';
		
		$html .= '<thead><tr>
			<th>NAMA RAYON</th>
			<th>KATEGORI</th>
			<th>REAL SD</th>
			<th>SISA</th>
			<th>PROGNOSA</th>
			<th>RKAP</th>
			<th>TAKMAR</th>
			<th>% thd RKAP</th>
			<th>% thd TAKMAR</th>
		</tr></thead>';

		$html .= '<tbody>';

		$tebu = 0;
		$luas = 0;
		$hablur = 0;

		$tebu_real = 0;
		$luas_real = 0;
		$hablur_real = 0;

		$tebu_sisa = 0;
		$luas_sisa = 0;
		$hablur_sisa = 0;

		$nama_rayon = '';
		foreach ($results as $row) {
			
			if($row->kategori == 'hablur') {
				$hablur += $row->prognosa;
				$hablur_sisa += $row->sisa;
				$hablur_real += $row->real_sd;
			} elseif($row->kategori == 'luas') {
				$luas += $row->prognosa;
				$luas_real += $row->real_sd;
				$luas_sisa += $row->sisa;
			} elseif($row->kategori == 'tebu') {
				$tebu_real += $row->real_sd;
				$tebu += $row->prognosa;
				$tebu_sisa += $row->sisa;
			}

					if($row->kategori == 'protas') {
						$prognosa = ($luas != 0) ? $tebu / $luas : 0;
						$real_sd = ($luas_real != 0) ? $tebu_real / $luas_real : 0;
						$sisa = ($luas_sisa != 0) ? $tebu_sisa / $luas_sisa : 0;						
					}else if($row->kategori == 'rendemen') {
						$prognosa = ($tebu != 0) ? ($hablur / $tebu) * 100 : 0;
						$real_sd = ($tebu_real != 0) ? ($hablur_real / $tebu_real) * 100 : 0;
						$sisa = ($tebu_sisa != 0) ? ($hablur_sisa / $tebu_sisa) * 100 : 0;
					} else {
						$prognosa = $row->prognosa;
						$real_sd = $row->real_sd;
						$sisa = $row->sisa;
					} 
					$html .= '<tr>';
					$html .= '<td>' . htmlspecialchars($row->nama_rayon) . '</td>';
					$html .= '<td>' . htmlspecialchars($row->kategori) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars(number_format($real_sd,2)) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars(number_format($sisa,2)) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars(number_format($prognosa,2)) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars(number_format($row->rkap,2)) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars(number_format($row->takmar,2)) . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars($row->rkap != 0 ? number_format(($prognosa / $row->rkap) * 100, 2) : '-') . '</td>';
					$html .= '<td style="text-align:right;">' . htmlspecialchars($row->takmar != 0 ? number_format(($prognosa / $row->takmar) * 100, 2) : '-') . '</td>';
					$html .= '</tr>';

		
		}

		$html .= '</table>';
		$html .= '</tbody>';
		return $html;
	}
	function show( Request $request , $id ) 
	{
		/* Handle import , export and view */
		$task =$id ;
		switch( $task)
		{

			case 'export':
				return $this->getExport( $request );
			case 'rekap_prognosa_kebun':
				return $this->rekap_prognosa_kebun($request);	
			case 'rekap_prognosa_rayon':
				return $this->rekap_prognosa_rayon($request);	
				break;
	
		}
	}
}