我如何在laravel中计算每月明智的今天? [英] How can i count month wise present day in laravel?

查看:54
本文介绍了我如何在laravel中计算每月明智的今天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在选择月份和年份之后,我有一个月份和年份的列,该列将给出员工的总当日数,该总数减去该月份该员工所花的假期.

I have a column for months and years after choosing month and year it will give count of Total present days of employee which subtract leaves that employee took in that month.

这是我的控制器文件代码

here is my code of controller file

 public function employeeAttendance(Request $request)
    {
        $employeeName = User::all();
        $employeeLeave = LeaveManagement::all();

        $countMonth = $request->get('month');
        $countYear = $request->get('year');

        function countDays($year, $month, $ignore) 
        {
            $count = 0;
            $counter = mktime(0, 0, 0, $month, 1, $year);
            while (date("n", $counter) == $month) {
                if (in_array(date("w", $counter), $ignore) == false) 
                {
                    $count++;
                }
                $counter = strtotime("+1 day", $counter);
            }
            return $count;
        }

        $totalWorkingDays = countDays($countYear, $countMonth, array(0, 6));

        return view('pages.attendance', compact('employeeName', 'totalWorkingDays', 'employeeLeave', 'countMonth', 'countYear'));
    }

这是我的查看文件代码

<div class="col-md-5 align-self-center">
                    <h4 class="text-themecolor">{{__('Employee Attendance')}}</h4>
                </div>
            </div>

            <div class="card">
                <div class="card-body">
                <form action="{{ route('employee_attendance') }}" method="GET">
                    <select class="custom-select col-md-2" name="month">
                        <option value="">Select Month</option>
                        <option value="01">January</option>
                        <option value="02">February</option>
                        <option value="03">March</option>
                        <option value="04">April</option>
                        <option value="05">May</option>
                        <option value="06">June</option>
                        <option value="07">July</option>
                        <option value="08">August</option>
                        <option value="09">September</option>
                        <option value="10">October</option>
                        <option value="11">November</option>
                        <option value="12">December</option>
                    </select>
                    <select class="custom-select col-md-2" name="year">
                        <option value="">Select Year</option>
                        <?php
                          for ($year = 2000; $year <= 2050; $year++) 
                          {
                            $selected = (isset($getYear) && $getYear == $year) ? 'selected' : '';
                            echo "<option value=$year $selected>$year</option>";
                          }
                        ?>
                    </select>
                    <button class="btn btn-info" type="submit"><i class="fa fa-search "></i></button>

                    <span style="float:right" class = "btn btn-info" disabled>Total Working Days : {{ $totalWorkingDays }} </span>
                </form>

                    <div class="table-responsive m-t-40">
                        <table class="table table-bordered table-striped ">
                            <thead>
                                <tr>
                                    <th>Employee Name</th>
                                    <th>Present Day</th>
                                    <th>Casual Leave</th>
                                    <th>Medical Leave</th>
                                </tr>
                            </thead>
                            <tbody>

                                @foreach ($employeeName as $empName)
                                @php
                                    $casualCount = $medicalCount = 0;
                                @endphp
                                @foreach($employeeLeave as $empleave)
                                    @if($empName['personal_detail']['first_name'] == $empleave->name)
                                            @if($empleave->type == 'casual')
                                                @php $casualCount++; @endphp
                                            @endif

                                            @if($empleave->type == 'medical')
                                                @php $medicalCount++; @endphp
                                            @endif
                                    @endif
                                @endforeach

                                    @if($empName['username'] != 'admin')
                                        <tr>
                                            <td>
                                                {{$empName['personal_detail']['first_name']}}
                                            </td>
                                            <td>
                                            @if($totalWorkingDays == 0)
                                                {{ $totalWorkingDays }}
                                            @else
                                                @php 
                                                    $totalLeave = $casualCount + $medicalCount;
                                                    $presentDay = $totalWorkingDays - $totalLeave;
                                                @endphp
                                                {{ $presentDay }}
                                            @endif
                                            </td>
                                            <td>
                                                @if( $totalWorkingDays == 0 )
                                                    {{ $totalWorkingDays }}
                                                @elseif( $countMonth ==  date('m', strtotime($empleave->start)) && $countYear ==  date('Y', strtotime($empleave->start))) 
                                                        {{ $casualCount }}
                                                @endif
                                            </td>

                                            <td>
                                                @if( $totalWorkingDays == 0 )
                                                    {{ $totalWorkingDays }}
                                                @elseif( $countMonth ==  date('m', strtotime($empleave->start)) && $countYear ==  date('Y', strtotime($empleave->start))) 
                                                    {{ $medicalCount }}
                                                @endif
                                            </td>
                                        </tr>
                                    @endif
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

我希望,如果我选择"2019年3月",它将为我提供所有雇员在当月的当日姓名,并且如果他们在该月休了任何病假或不请假,那么我将在当日"列中将其减去.

I want that If I select March 2019 than it will give me all the employees name their present days in that month and if they took any medical or casual leave in that month than it will subtract from it in present day column

推荐答案

这是您的控制器功能

public function employeeAttendance(Request $request)
{
    $employeeName = User::all();
    $countMonth   = (!empty($request->get('month')) ? intval($request->get('month')) : 0);
    $countYear    = (!empty($request->get('year')) ? intval($request->get('year')) : 0);
    $firstDay     = new DateTime(date('F jS Y h:i:s A', strtotime('first day of ' . date('Y-m', strtotime("$countYear-$countYear")))));
    $lastDay      = new DateTime(date('F jS Y h:i:s A', strtotime('last day of ' . date('Y-m', strtotime("$countYear-$countYear")))));

    $employeeLeave = LeaveManagement::where('yourdatefield', '>=', $firstDay)
        ->where('yourdatefield', '<=', $lastDay)->get();
    $totalWorkingDays = 0;
    if (!empty($countMonth && $countYear)) {
        $totalWorkingDays = $this->countDays($countYear, $countMonth, array(0, 6));
    }
    $result = [];
    foreach ($employeeLeave as $empleave) {
        if ($empleave->type == 'casual') {
            $result[$empleave->name]['casual'][] = $empleave;
        }
        if ($empleave->type == 'medical') {
            $result[$empleave->name]['medical'][] = $empleave;
        }
    }
    return view('pages.attendance', compact('employeeName', 'totalWorkingDays', 'employeeLeave','result'));
}



public function countDays($year, $month, $ignore)
{
    $count   = 0;
    $counter = mktime(0, 0, 0, $month, 1, $year);
    while (date("n", $counter) == $month) {
        if (!in_array(date("w", $counter), $ignore)) {
            $count++;
        }
        $counter = strtotime("+1 day", $counter);
    }
    return $count;
}

我对forloop做了一些改动

I have changed forloop a little bit

for ($year = 2000; $year <= 2050; $year++) {
    $selected = (isset($getYear) && $getYear == $year) ? 'selected' : '';
    echo "<option value='$year' $selected>$year</option>";
}

请确保上一个问题中的所有内容均已妥善处理,检查是否可行.

Rest everything is taken care in previous question, check if it works.

编辑

在获取$employeeLeave

$result = [];
foreach ($employeeLeave as $empleave) {
    if ($empleave->type == 'casual') {
        $result[$empleave->name]['casual'][] = $empleave;
    }
    if ($empleave->type == 'medical') {
        $result[$empleave->name]['medical'][] = $empleave;
    }
}

发送$result到刀片.

如下更改您的html表结构,

Change your html table structure as below,

<table class="table table-bordered table-striped ">
    <thead>
        <tr>
            <th>Employee Name</th>
            <th>Present Day</th>
            <th>Casual Leave</th>
            <th>Medical Leave</th>
        </tr>
    </thead>
    <tbody>
        @foreach($result as $empname => $empleave)
            <tr>
                <td>{{ $empname }}</td>
                <td>{{ $totalWorkingDays }}</td>
                <td>{{ count($result[$empname]['casual']) }}</td>
                <td>{{ count($result[$empname]['medical']) }}</td>

            </tr>    
        @endforeach
    </tbody>
</table>

这篇关于我如何在laravel中计算每月明智的今天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆