<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use PDF;
use Carbon\Carbon;
class SalaireController extends Controller
{
    function ajouter($id)
    {
        /*if(session('user')== null) {
            return redirect('/');
        }*/
        $idemploye = $id;
        session(['idemploye'=> $idemploye]);
        $employe = DB::table('employe')->where('id', $idemploye)->first();
        $nbreabsence = DB::table('absence')->where('employe_id', $idemploye)->where('etat', 'En cours')->get()->count();
        $salaire = DB::table('salaires')->where('employe_id', $idemploye)->orderBydesc('id')->get();
        //$emp=DB::table('employe')->get();
        return view('salaires/nouveau',[
            'id' => $idemploye,
            'employe' => $employe,
            'nbreabsence' => $nbreabsence,
            'salaire' => $salaire,
        ]);
    }
	function ajouter2($id)
    {
        /*if(session('user')== null) {
            return redirect('/');
        }*/
        $idemploye = $id;
        session(['idemploye'=> $idemploye]);
        $employe = DB::table('employe')->where('id', $idemploye)->first();
        $nbreabsence = DB::table('absence')->where('employe_id', $idemploye)->where('etat', 'En cours')->get()->count();
        $salaire = DB::table('salaires')->where('employe_id', $idemploye)->orderBydesc('id')->get();
        //$emp=DB::table('employe')->get();
        return view('salaires/nouveau2',[
            'id' => $idemploye,
            'employe' => $employe,
            'nbreabsence' => $nbreabsence,
            'salaire' => $salaire,
        ]);
    }

    function liste($id) {
        /*if(session('user')== null) {
            return redirect('/');
        }*/
        $salaires = DB::table('salaires')->where('employe_id', $id)->orderBydesc('id')->get();
        return view('salaires/liste',[
            'salaire' => $salaires,
        ]);
    }
	function listejourn() {
        /*if(session('user')== null) {
            return redirect('/');
        }*/
        $salaires = DB::table('salaires')->where('typeemploye','Journalier')->orderBydesc('id')->get();
        return view('salaires/liste',[
            'salaire' => $salaires,
        ]);
    }
	function listeperm() {
        /*if(session('user')== null) {
            return redirect('/');
        }*/
        $salaires = DB::table('salaires')->where('typeemploye','Salarié')->orderBydesc('id')->get();
        return view('salaires/liste2',[
            'salaire' => $salaires,
        ]);
    }
	
	//LES HISTORIQUES
	
	function listejourn2() {
        $date_debut = implode('-', array_reverse(explode('/', request('date_debut'))));
        $dd = request('date_debut');
        $date_fin = implode('-', array_reverse(explode('/', request('date_fin'))));
        $df = request('date_fin');
        $salaires = DB::table('salaires')->where('typeemploye','Journalier')->where('date_debut',$date_debut)->where('date_fin',$date_fin)->orderBydesc('id')->get();
        return view('salaires/historique-journalier',[
            'df' => $df,
            'dd' => $dd,
            'salaire' => $salaires,
        ]);
    }
	function listeperm2() {
        $date_debut = implode('-', array_reverse(explode('/', request('date_debut'))));
        $dd = request('date_debut');
        $date_fin = implode('-', array_reverse(explode('/', request('date_fin'))));
        $df = request('date_fin');
        $salaires = DB::table('salaires')->where('typeemploye','Salarié')->where('date_debut',$date_debut)->where('date_fin',$date_fin)->orderBydesc('id')->get();
        return view('salaires/historique-permanent',[
            'salaire' => $salaires,
            'df' => $df,
            'dd' => $dd,
        ]);
    }
	// FIN HISTORIQUES

    function VoirSuppression ($id) {
        $salaires= DB::table('salaires')->where('id',$id)->first();
        return [
            'salaire' => $salaires,
        ];
    }
    function supprimer () {

        $id=request('id');
        DB::table('salaires')->where('id', $id)->delete();
        //return redirect()->route('liste-des-salaires-journalier', ['id' => session('idemploye')])->with('status', 'Suppression effectuée avec succes');
        return redirect()->route('liste-des-salaires-journalier')->with('status', 'Suppression effectuée avec succes');
    }

    function generer (Request $request, $id){

        $employe= $id;
        $typeemploye= DB::table('employe')->where('id', $id)->first()->typeemploye;
        $date_debut=request('date_debut');
        $date_debut= explode('/', request('date_debut'));
        $date_debut= array_reverse($date_debut);
        $date_debut= implode('-', $date_debut);
        $date_fin=request('date_fin');
        $date_fin= explode('/', request('date_fin'));
        $date_fin= array_reverse($date_fin);
        $date_fin= implode('-', $date_fin);
        $salaire=request('salaire');
        $nbabsence = request('nbabsence');
		$today = date('Y-m-d');
        if(request('sal')){
            $sal=collect(request('sal'))->chunk(2);
        }

        $verification= DB::table('salaires')->where('employe_id',$employe)->whereBetween('date_debut', [$date_debut,$date_fin])->first();
        $verification2= DB::table('salaires')->where('employe_id',$employe)->whereBetween('date_fin', [$date_debut,$date_fin])->first();

        if($verification || $verification2){
            $message = [
                "title" => 'Attention',
                "type" => 'error',
                "response" => 'Cet enregistrement n\'est pas possible une date concerne un salaire déjà généré!',
            ];
            return $message;
        } else {
			$now =Carbon::now();
			$dd = Carbon::createFromFormat('Y-m-d', $date_debut)->toDateString();
			$df = Carbon::createFromFormat('Y-m-d', $date_fin)->toDateString();
			$dateActCarbon = Carbon::parse($dd);
			$diff1 = $dateActCarbon->diffInDays($df);
			$diff1 = $diff1 + 1;
			$nba = DB::table('absence')->where('employe_id', $employe)->whereBetween('date_absence', [$date_debut,$date_fin])->get()->count();
			$avance = DB::table('avance')->where('employe_id', $employe)->whereBetween('date_avance', [$date_debut,$date_fin])->sum('montant');
			$apayer1 = ($salaire * $diff1) - ($salaire * $nba);
			$apayer = $apayer1 - $avance;
            DB::table('salaires')->insert(
                [

                    'employe_id' => $employe,
                    'typeemploye' => $typeemploye,
                    'date_debut' => $date_debut,
                    'date_fin' => $date_fin,
                    'salaire' => $salaire,
                    'nbabsence' => $nba,
                    'apayer' => $apayer,
                    'avance' => $avance,
                    'nbjour' => $diff1,
                    'dateenreg' => $today,
                ]
            );
			
            $idLastsalaire = DB::table('salaires')->get()->last()->id;
            $i=0;
            if(request('sal')){
                foreach ($sal as $key=> $sals){

                    DB::table('primes')->insert(
                        [
                            'salaire_id' => $idLastsalaire,
                            'type' => $sals[0 +$i],
                            'montant' => $sals[1 +$i],
                        ]
                    );

                    $i += count($sals->toArray());

                }
            }

            $message = [
                "title" => 'success',
                "type" => 'success',
                "response" => 'Salaire enregistrée avec succes !',
            ];
            return $message;
        }
       }
	   //SALAIRE DES PERMANENTS
	   function generer2 (Request $request, $id){

        $employe= $id;
        $typeemploye= DB::table('employe')->where('id', $id)->first()->typeemploye;
        $date_debut=request('date_debut');
        $date_debut= explode('/', request('date_debut'));
        $date_debut= array_reverse($date_debut);
        $date_debut= implode('-', $date_debut);
        $date_fin=request('date_fin');
        $date_fin= explode('/', request('date_fin'));
        $date_fin= array_reverse($date_fin);
        $date_fin= implode('-', $date_fin);
        $salaire=request('salaire');
        $retenue=request('retenue');
        $mois = request('mois');
		$today = date('Y-m-d');
        if(request('sal')){
            $sal=collect(request('sal'))->chunk(2);
        }

        $verification= DB::table('salaires')->where('employe_id',$employe)->whereBetween('date_debut', [$date_debut,$date_fin])->first();
        $verification2= DB::table('salaires')->where('employe_id',$employe)->whereBetween('date_fin', [$date_debut,$date_fin])->first();

        if($verification || $verification2){
            $message = [
                "title" => 'Attention',
                "type" => 'error',
                "response" => 'Cet enregistrement n\'est pas possible une date concerne un salaire déjà généré!',
            ];
            return $message;
        } else {
            
            DB::table('salaires')->insert(
                [

                    'employe_id' => $employe,
                    'typeemploye' => $typeemploye,
                    'date_debut' => $date_debut,
                    'date_fin' => $date_fin,
                    'salaire' => $salaire,
                    'retenue' => $retenue,
                    'mois' => $mois,
                    'dateenreg' => $today,
                ]
            );
			
            $idLastsalaire = DB::table('salaires')->get()->last()->id;
            $i=0;
            if(request('sal')){
                foreach ($sal as $key=> $sals){

                    DB::table('primes')->insert(
                        [
                            'salaire_id' => $idLastsalaire,
                            'type' => $sals[0 +$i],
                            'montant' => $sals[1 +$i],
                        ]
                    );

                    $i += count($sals->toArray());

                }
            }
			$total_prime = DB::table('primes')->where('salaire_id', $idLastsalaire)->sum('montant');
			$avance = DB::table('avance')->where('employe_id', $employe)->whereBetween('date_avance', [$date_debut,$date_fin])->sum('montant');
			$net = ($salaire + $total_prime) - ($retenue + $avance);
			 DB::table('salaires')->where('id', $idLastsalaire)->update(
                        [
                            'avance' => $avance,
                            'total_prime' => $total_prime,
                            'net_a_payer' => $net,
                        ]
                    );
            $message = [
                "title" => 'success',
                "type" => 'success',
                "response" => 'Salaire enregistrée avec succes !',
            ];
            return $message;
        }
       }
	   public function voirRecuSalaire($id)
    {
        if(session('user')== null) {
            return redirect('/');
        }
        $today = date('d/m/Y');
        $salaire = DB::table('salaires')->where('id', $id)->first();
        //$primes = DB::table('primes')->where('salaire_id', $id)->get();
        //$totalprime = DB::table('primes')->where('salaire_id', $id)->get()->sum('montant');
        $employe = DB::table('employe')->where('id', $salaire->employe_id)->first();
        $salb = DB::table('employe')->where('id', $salaire->employe_id)->first()->salaire_base;
        
        $dds = DB::table('salaires')->where('id', $id)->first()->date_debut;
        $dfs = DB::table('salaires')->where('id', $id)->first()->date_fin;
        
        $absence = DB::table('absence')->where('employe_id', $salaire->employe_id)->whereBetween('date_absence', [$dds,$dfs])->get()->count();
        $retenue = $absence *  $salb;
        $datds=implode('/', array_reverse(explode('-', $dds)));
        $datfs=implode('/', array_reverse(explode('-', $dfs)));
        
        $pdf = PDF::loadView('salaires/recu_salaire', [
                 'salaire' => $salaire,
                 'employe' => $employe,
                 'datds' => $datds,
                 'datfs' => $datfs,
                 'absence' => $absence,
                 'retenue' => $retenue,
                 'today' => $today,
        ]);
        return $pdf->stream();
    }
	public function voirRecuSalaire2($id)
    {
        if(session('user')== null) {
            return redirect('/');
        }
        $prime = DB::table('primes')->where('salaire_id', $id)->get();
        $salaire = DB::table('salaires')->where('id', $id)->first();
        $employe = DB::table('employe')->where('id', $salaire->employe_id)->first();
        $salb = DB::table('employe')->where('id', $salaire->employe_id)->first()->salaire_base;
        
        $dds = DB::table('salaires')->where('id', $id)->first()->date_debut;
        $dfs = DB::table('salaires')->where('id', $id)->first()->date_fin;
        $datds=implode('-', array_reverse(explode('/', $dds)));
        $datfs=implode('-', array_reverse(explode('/', $dfs)));

        $pdf = PDF::loadView('salaires/recu_salaire2', [
                 'salaire' => $salaire,
                 'employe' => $employe,
                 'datds' => $datds,
                 'datfs' => $datfs,
                 'prime' => $prime,
        ]);
        return $pdf->stream();
    }
}
