generated from liutk/owl-admin-base
82 lines
2.9 KiB
PHP
82 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Admin\Filters\EmployeeSignFilter;
|
|
use App\Models\EmployeeSign;
|
|
use App\Models\EmployeeSignLog;
|
|
use App\Models\EmployeeRest;
|
|
use App\Models\Employee;
|
|
use App\Enums\SignType;
|
|
use App\Enums\SignStatus;
|
|
use App\Enums\SignTime;
|
|
|
|
class EmployeeSignService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['employee', 'store'];
|
|
|
|
protected string $modelName = EmployeeSign::class;
|
|
|
|
protected string $modelFilterName = EmployeeSignFilter::class;
|
|
|
|
/**
|
|
* 整理昨天的打卡流水, 生成对应的打卡记录
|
|
*/
|
|
public function signResult()
|
|
{
|
|
$date = now()->subDay();
|
|
$start = $date->copy()->startOfDay();
|
|
$end = $date->copy()->endOfDay();
|
|
// 打卡日志
|
|
$list = EmployeeSignLog::whereBetween('time', [$start, $end])->get();
|
|
// 休息的员工
|
|
$restEmployeeIds = EmployeeRest::whereBetWeen('date', [$start, $end])->pluck('employee_id');
|
|
// 需要打卡的员工
|
|
$employees = Employee::where('store_id', '>', 0)->whereNotIn('id', $restEmployeeIds)->get();
|
|
foreach ($employees as $employee) {
|
|
$logs = $list->where('employee_id', $employee->id);
|
|
// 状态: 两个打卡=正常, 一次打卡 = 缺卡, 两次未打=旷工
|
|
$status = 0;
|
|
// 外勤打卡-事由
|
|
$remarks = null;
|
|
// 上班打卡
|
|
$firstTime = null;
|
|
if ($item = $logs->where('sign_time', SignTime::Morning)->sortBy('time')->first()) {
|
|
$firstTime = $item->time;
|
|
$status ++;
|
|
if ($item->sign_type == SignType::Outside) {
|
|
$remarks = $item->remarks;
|
|
}
|
|
}
|
|
// 下班打卡
|
|
$lastTime = null;
|
|
if ($item = $logs->where('sign_time', SignTime::Afternoon)->sortByDesc('time')->first()) {
|
|
$lastTime = $item->time;
|
|
$status ++;
|
|
if ($item->sign_type == SignType::Outside) {
|
|
$remarks = $item->remarks;
|
|
}
|
|
}
|
|
// 打卡类型
|
|
$type = SignType::None;
|
|
if ($status > 0) {
|
|
$type = $logs->where('sign_type', SignType::Outside)->count() > 0 ? SignType::Outside : SignType::Normal;
|
|
}
|
|
$attributes = [
|
|
'date' => $date,
|
|
'store_id' => $employee->store_id,
|
|
'sign_type' => $type,
|
|
'first_time' => $firstTime,
|
|
'last_time' => $lastTime,
|
|
'sign_status' => match($status) {
|
|
0 => SignStatus::Absent,
|
|
1 => SignStatus::Lose,
|
|
2 => SignStatus::Normal,
|
|
},
|
|
'remarks' => $remarks,
|
|
];
|
|
$employee->signs()->create($attributes);
|
|
}
|
|
}
|
|
}
|