Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 35
LeaveLogHelper
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 35
 getLeaveLogs
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 updateLeaveLog
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 15
 getCurrentLeaveBalance
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 18
<?php
namespace App\Helpers;
use App\Models\Leave\LeaveEntitlement;
use App\Models\Leave\LeaveLogs;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class LeaveLogHelper{
    public function getLeaveLogs($userId){
        $leaveLogs = LeaveLogs::where("user_id","=",$userId)->get();
        return $leaveLogs;
    }
    public function updateLeaveLog($amount,$operation,$userId,$leaveId,$comments,$updated_by){
        $currentLeaveBalance = $this->getCurrentLeaveBalance($userId);
        $newLeaveLog = new LeaveLogs;
        //For leave application
        if($operation=="ADDED"){
                $newLeaveLog->old_balance = $currentLeaveBalance - $amount;
                $newLeaveLog->updated_balance = $currentLeaveBalance;
        }else{
            $newLeaveLog->old_balance = $currentLeaveBalance + $amount;
            $newLeaveLog->updated_balance = $currentLeaveBalance;
        }
        $newLeaveLog->amount = $amount;
        $newLeaveLog->operation = $operation;
        $newLeaveLog->user_id = $userId;
        $newLeaveLog->leave_id = $leaveId;
        $newLeaveLog->comments = $comments;
        $newLeaveLog->updated_by_user = $updated_by;
        $newLeaveLog->save();
    }
    public function getCurrentLeaveBalance($userId){
        $leaves = DB::table('leaves')->whereNull('deleted_at')->get();
        $leaveBalances = [];
        foreach ($leaves as $leave) {
            $leaveEntitlementQuery = array(
                'user_id' => $userId,
                'year' => Carbon::now()->year,
                'leave_id' => $leave->id,
            );
           
            $leaveEntitlementArr = LeaveEntitlement::where($leaveEntitlementQuery)->get();
            $leaveBalance = null;
            foreach ($leaveEntitlementArr as $leaveEntitlement) {
                $leaveBalance = $leaveEntitlement->leave_balance;
            }
            if ($leaveBalance == null) {
                $leaveBalance = 0;
            }
            $leaveBalances[] = array(
                'leave_balance' => $leaveBalance
            );
            
        }
       $totalLeaveBalance = 0;
       foreach($leaveBalances as $leaveBalance) {
           $totalLeaveBalance += $leaveBalance['leave_balance'];
       }
       return $totalLeaveBalance;
    }
}