<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Employee;
use App\Models\EmploymentDetail;
use App\Models\EmployeeEducation;
use App\Models\EmployeeDocument;
use App\Models\EducationLevel;
use App\Models\DocumentType;
use App\Models\Grade;
use App\Models\Designation;
use App\Models\PayrollHead;
use App\Models\GradeStructure;
use App\Models\PayrollStructure;
use App\Models\PayrollStructureDetail;
use App\Models\AccountHead;
use App\Models\PayrollHistory;
use App\Models\PayrollHistoryDetail;
use App\Models\PayrollTransection;
use App\Models\Journal;

class PayrollController extends Controller
{
    public function index()
    {
        $data['title'] = "Employee Payroll";
        $data['all']   = PayrollStructure::with(['employee','designationName.designation','grade','psHead.head','due'])->where('type','!=',4)->get();
        return view('admin.payroll.index',$data);
    }
    public function create()
    {
        $data['title'] = "Employee Bill";
        $data['all']   = PayrollStructure::with(['employee','designationName.designation','grade','psHead.head','due'])->where('type','!=',4)->get();
        return view('admin.payroll.create',$data);
    }
    public function billCreate(Request $request)
    {
        $c_date = date('Y-m-d H:i:s');
        $user_id = \Auth::user()->id;
        $input = $request->all();
        $condition = $input['emp_id'];
        foreach ($condition as $key => $condition) {
            
            $ph_create = new PayrollHistory;
            $ph_create->employee_id = $input['emp_id'][$key];
            $ph_create->date = $c_date;
            $ph_create->gross_amount = $input['salary'][$key];
            $ph_create->status= 1;
            $ph_create->added_by = $user_id;
            $ph_create->save();
            $gethead = PayrollStructure::with(['psHead'])->where('employee_id',$input['emp_id'][$key])->first();

            foreach ($gethead->psHead as $key => $row) {
                $psd = new PayrollHistoryDetail;
                $psd->ph_id = $ph_create->id;
                $psd->head_id = $row->head_id;
                $psd->amount = $row->amount;
                $psd->save();
                
            }
        }
        return redirect('admin/payroll')->with('msg','Employee Bill Created Successfully.');
    }
    
    public function prUpdate($id)
    {
        $data['title'] = "Employee Payroll";
        $data['single'] = PayrollStructure::with(['employee','designationName.designation','grade','psHead.head','payableDue'])->find($id);

        $data['media'] = AccountHead::where('type_id',1)->orderBy('name','ASC')->get();
        return view('admin.payroll.edit',$data);
    }

    
    public function update(Request $request, $id)
    {
        //dd($request->all());
        $user_id = \Auth::user()->id;
        $c_date = date('Y-m-d H:i:s');

        $input = $request->all();
        $condition = $input['prh_id'];
        foreach ($condition as $key => $condition) {
            
            $journal = new Journal;
            $journal->ref_id = 1;
            $journal->debit_account = 1;
            $journal->credit_account =$request->media;
            $journal->description =$request->description;
            $journal->amount =$input['dues'][$key];
            $journal->is_approaved =0;
            $journal->client_id =0;
            $journal->office_id =1;
            $journal->save();



            $prh_id = $input['prh_id'][$key];
            $ph_create = PayrollHistory::find($prh_id);
            $old_due = $ph_create->paid_amount;
            $now_paid = $input['dues'][$key] + $old_due;
            $ph_create->paid_amount = $now_paid;
            $ph_create->save();

            $prt =  new PayrollTransection;
            $prt->ph_id = $ph_create->id;
            $prt->journal_id = $journal->id;
            $prt->date = $c_date;
            $prt->save();

        }

        return redirect('admin/payroll')->with('msg','Employee Payment Successfully.');
    }

    
    public function destroy($id)
    {
        PayrollStructure::whereId($id)->delete();
        return redirect('admin/payroll_structure')->with('msg','Successfully Deleted!');
    }
    public function details($id)
    {
        $data['title'] = "Employee Payment History";
        $data['emp'] = Employee::find($id);
        $data['emp_details'] = EmploymentDetail::with(['designation','grade'])->where('employee_id',$id)->orderBy('id','DESC')->first();
        $data['all'] = PayrollHistory::where('employee_id',$id)->orderBy('id','DESC')->get();
        return view('admin.payroll.details',$data);
    }
    public function receipt($id , $pay_id)
    {
        $data['emp'] = Employee::find($id);
        $data['emp_details'] = EmploymentDetail::with(['designation','grade'])->where('employee_id',$id)->orderBy('id','DESC')->first();
        $data['pay'] = PayrollHistory::find($pay_id);
        $data['single'] = PayrollStructure::with(['employee','designationName.designation','grade','psHead.head','payableDue'])->where('employee_id',$id)->first();
        return view('admin.payroll.receipt',$data);
    }
}
