PersonWorkService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DailyPwOrder;
  4. use App\Model\DailyPwOrderDetails;
  5. use App\Model\Employee;
  6. use App\Model\Item;
  7. use App\Model\MonthlyPwOrder;
  8. use App\Model\MonthlyPwOrderDetails;
  9. use Illuminate\Support\Facades\DB;
  10. class PersonWorkService extends Service
  11. {
  12. // 人员月工时单-------------------------------------------
  13. public function monthlyPwOrderEdit($data,$user){
  14. list($status,$msg) = $this->monthlyPwOrderRule($data, $user, false);
  15. if(!$status) return [$status,$msg];
  16. try {
  17. DB::beginTransaction();
  18. $model = MonthlyPwOrder::where('id',$data['id'])->first();
  19. // $model->month = $data['month'] ?? 0;
  20. // $model->save();
  21. $time = time();
  22. MonthlyPwOrderDetails::where('del_time',0)
  23. ->where('main_id', $model->id)
  24. ->update(['del_time' => $time]);
  25. $this->saveDetail($model->id, $time, $data);
  26. DB::commit();
  27. }catch (\Exception $exception){
  28. DB::rollBack();
  29. return [false,$exception->getMessage()];
  30. }
  31. return [true, ''];
  32. }
  33. public function monthlyPwOrderAdd($data,$user){
  34. list($status,$msg) = $this->monthlyPwOrderRule($data, $user);
  35. if(!$status) return [$status,$msg];
  36. try {
  37. DB::beginTransaction();
  38. $model = new MonthlyPwOrder();
  39. $model->code = $this->generateBillNo([
  40. 'top_depart_id' => $user['top_depart_id'],
  41. 'type' => MonthlyPwOrder::Order_type,
  42. 'period' => date("Ym", $data['month'])
  43. ]);
  44. $model->month = $data['month'] ?? 0;
  45. $model->crt_id = $user['id'];
  46. $model->top_depart_id = $data['top_depart_id'];
  47. $model->save();
  48. $this->saveDetail($model->id, time(), $data);
  49. DB::commit();
  50. }catch (\Exception $exception){
  51. DB::rollBack();
  52. return [false,$exception->getMessage()];
  53. }
  54. return [true, ''];
  55. }
  56. private function saveDetail($id, $time, $data){
  57. if(! empty($data['details'])){
  58. $unit = [];
  59. foreach ($data['details'] as $value){
  60. $unit[] = [
  61. 'main_id' => $id,
  62. 'employee_id' => $value['employee_id'],
  63. 'total_days' => $value['total_days'],
  64. 'rd_total_days' => $value['rd_total_days'],
  65. 'total_hours' => $value['total_hours'],
  66. 'rd_total_hours' => $value['rd_total_hours'],
  67. 'crt_time' => $time,
  68. 'top_depart_id' => $value['top_depart_id'],
  69. ];
  70. }
  71. if(! empty($unit)) MonthlyPwOrderDetails::insert($unit);
  72. }
  73. }
  74. private function getDetail($id){
  75. $data = MonthlyPwOrderDetails::where('del_time',0)
  76. ->where('main_id', $id)
  77. ->select('employee_id', 'total_days', 'rd_total_days', 'total_hours', 'rd_total_hours')
  78. ->get()->toArray();
  79. $id = array_column($data,'employee_id');
  80. $map = Employee::whereIn('id', $id)->select('title','id','number')->get()->toArray();
  81. $map = array_column($map,null,'id');
  82. foreach ($data as $key => $value){
  83. $tmp = $map[$value['employee_id']] ?? [];
  84. $merge = [];
  85. $merge['employee_title'] = $tmp['title'];
  86. $merge['employee_number'] = $tmp['number'];
  87. $data[$key] = array_merge($value, $merge);
  88. }
  89. $detail = [
  90. 'details' => $data,
  91. ];
  92. foreach ($detail as $key => $value) {
  93. if (empty($value)) {
  94. $detail[$key] = (object)[]; // 转成 stdClass 对象
  95. }
  96. }
  97. return $detail;
  98. }
  99. public function monthlyPwOrderDel($data){
  100. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  101. try {
  102. DB::beginTransaction();
  103. $time = time();
  104. MonthlyPwOrder::where('del_time',0)
  105. ->whereIn('id',$data['id'])
  106. ->update(['del_time' => $time]);
  107. MonthlyPwOrderDetails::where('del_time',0)
  108. ->whereIn('main_id', $data['id'])
  109. ->update(['del_time' => $time]);
  110. DB::commit();
  111. }catch (\Exception $exception){
  112. DB::rollBack();
  113. return [false,$exception->getMessage()];
  114. }
  115. return [true, ''];
  116. }
  117. public function monthlyPwOrderDetail($data, $user){
  118. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  119. $customer = MonthlyPwOrder::where('del_time',0)
  120. ->where('id',$data['id'])
  121. ->first();
  122. if(empty($customer)) return [false,'人员月度工时单不存在或已被删除'];
  123. $customer = $customer->toArray();
  124. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  125. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  126. $customer['month'] = $customer['month'] ? date("Y-m",$customer['month']): '';
  127. $details = $this->getDetail($data['id']);
  128. $customer = array_merge($customer, $details);
  129. return [true, $customer];
  130. }
  131. public function monthlyPwOrderCommon($data,$user, $field = []){
  132. if(empty($field)) $field = MonthlyPwOrder::$field;
  133. $model = MonthlyPwOrder::Clear($user,$data);
  134. $model = $model->where('del_time',0)
  135. ->select($field)
  136. ->orderby('id', 'desc');
  137. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  138. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  139. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  140. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  141. $model->where('crt_time','>=',$return[0]);
  142. $model->where('crt_time','<=',$return[1]);
  143. }
  144. return $model;
  145. }
  146. public function monthlyPwOrderList($data,$user){
  147. $model = $this->monthlyPwOrderCommon($data, $user);
  148. $list = $this->limit($model,'',$data);
  149. $list = $this->fillData($list);
  150. return [true, $list];
  151. }
  152. public function monthlyPwOrderRule(&$data, $user, $is_add = true)
  153. {
  154. if (empty($data['month'])) return [false, '月份不能为空'];
  155. $data['month'] = $this->changeDateToDate($data['month']);
  156. $data['top_depart_id'] = $user['top_depart_id'];
  157. if (empty($data['details'])) return [false, '人员月度工时单明细不能为空'];
  158. //获取系统计算的考勤基准数据 ---
  159. $empIds = array_column($data['details'], 'employee_id');
  160. list($status, $systemStats) = (new EmployeeService())->getEmployeesMonthStats($empIds, $data['month'], $user);
  161. if (!$status) return [false, $systemStats]; // 如果日历未设置,直接拦截
  162. foreach ($data['details'] as $key => $value) {
  163. if (empty($value['employee_id'])) return [false, '人员不能为空'];
  164. $empId = $value['employee_id'];
  165. // 基础数字格式检查
  166. foreach (['total_days', 'rd_total_days', 'total_hours', 'rd_total_hours'] as $field) {
  167. $precision = 2;
  168. $res = $this->checkNumber($value[$field], $precision, 'non-negative');
  169. if (!$res['valid']) return [false, $value['employee_title'] . "的" . $field . ":" . $res['error']];
  170. }
  171. // --- 业务逻辑校验:出勤天数与工时合法性 ---
  172. $sysData = $systemStats[$empId] ?? null;
  173. if ($sysData) {
  174. // 1. 研发天数不能大于出勤总天数
  175. if ($value['rd_total_days'] > $value['total_days']) {
  176. return [false, "第" . ($key + 1) . "行:研发出勤天数不能大于出勤总天数"];
  177. }
  178. // 2. 研发工时不能大于出勤总工时
  179. if ($value['rd_total_hours'] > $value['total_hours']) {
  180. return [false, "第" . ($key + 1) . "行:研发总工时不能大于出勤总工时"];
  181. }
  182. // 4. 校验出勤总天数是否超过了系统计算的上限
  183. if ($value['total_days'] != $sysData['attendance_days']) {
  184. return [false, "人员[{$empId}]填写的出勤总天数({$value['total_days']})不等于系统核算的天数({$sysData['attendance_days']})"];
  185. }
  186. //校验出勤总工时是否超过了系统计算的上限
  187. if ($value['total_hours'] != $sysData['final_work_hour']) {
  188. return [false, "人员[{$empId}]填写的出勤总工时({$value['total_hours']})不等于系统核算的工时({$sysData['final_work_hour']})"];
  189. }
  190. }
  191. $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
  192. }
  193. // --- 查重与唯一性校验 ---
  194. list($status, $msg) = $this->checkArrayRepeat($data['details'], 'employee_id', '人员');
  195. if (!$status) return [false, $msg];
  196. $query = MonthlyPwOrder::where('top_depart_id', $data['top_depart_id'])
  197. ->where('month', $data['month'])
  198. ->where('del_time', 0);
  199. if (!$is_add) {
  200. if (empty($data['id'])) return [false, 'ID不能为空'];
  201. $query->where('id', '<>', $data['id']);
  202. }
  203. if ($query->exists()) {
  204. return [false, date("Y-m", $data['month']) . '已存在人员月度研发工时单'];
  205. }
  206. return [true, ''];
  207. }
  208. public function monthlyPwOrderRule1(&$data, $user, $is_add = true){
  209. if(empty($data['month'])) return [false, '月份不能为空'];
  210. $data['month'] = $this->changeDateToDate($data['month']);
  211. $data['top_depart_id'] = $user['top_depart_id'];
  212. if(empty($data['details'])) return [false, '人员月度工时单明细不能为空'];
  213. foreach ($data['details'] as $key => $value){
  214. if(empty($value['employee_id'])) return [false, '人员不能为空'];
  215. $res = $this->checkNumber($value['total_days'],0,'non-negative');
  216. if(! $res['valid']) return [false,'出勤总天数:' . $res['error']];
  217. $res = $this->checkNumber($value['rd_total_days'],0,'non-negative');
  218. if(! $res['valid']) return [false,'研发出勤总天数:' . $res['error']];
  219. $res = $this->checkNumber($value['total_hours'],2,'non-negative');
  220. if(! $res['valid']) return [false,'出勤总工时:' . $res['error']];
  221. $res = $this->checkNumber($value['rd_total_hours'],2,'non-negative');
  222. if(! $res['valid']) return [false,'研发总工时:' . $res['error']];
  223. $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
  224. }
  225. list($status, $msg) = $this->checkArrayRepeat($data['details'],'employee_id','人员');
  226. if(! $status) return [false, $msg];
  227. if($is_add){
  228. $bool = MonthlyPwOrder::where('top_depart_id', $data['top_depart_id'])
  229. ->where('month', $data['month'])
  230. ->where('del_time',0)
  231. ->exists();
  232. }else{
  233. if(empty($data['id'])) return [false,'ID不能为空'];
  234. $bool = MonthlyPwOrder::where('top_depart_id', $data['top_depart_id'])
  235. ->where('month', $data['month'])
  236. ->where('id','<>',$data['id'])
  237. ->where('del_time',0)
  238. ->exists();
  239. }
  240. if($bool) return [false, date("Y-m", $data['month']) . '已存在人员月度研发工时单'];
  241. return [true, ''];
  242. }
  243. public function fillData($data){
  244. if(empty($data['data'])) return $data;
  245. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  246. foreach ($data['data'] as $key => $value){
  247. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  248. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  249. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  250. }
  251. return $data;
  252. }
  253. public function fillDataForExport($data, $column, &$return)
  254. {
  255. if(empty($data)) return;
  256. $mainIds = array_column($data, 'id');
  257. // 获取详情映射 [main_id => [details...]]
  258. $detailsMap = $this->getDetailsMap($mainIds);
  259. // 默认空行模板
  260. $defaultRow = array_fill_keys($column, '');
  261. foreach ($data as $main) {
  262. $mainId = $main['id'];
  263. $details = $detailsMap[$mainId] ?? [];
  264. // 提取主表信息
  265. $mainInfo = [
  266. 'code' => $main['code'],
  267. 'month' => $main['month'] ? date('Y-m', $main['month']) : '',
  268. ];
  269. if (empty($details)) {
  270. // 如果没有详情,至少导出一行主表信息(可选)
  271. $return[] = array_merge($defaultRow, $mainInfo);
  272. } else {
  273. // 核心:遍历详情,每一行详情都合并主表信息
  274. foreach ($details as $sub) {
  275. // 合并主表字段 + 详情字段
  276. $fullRow = array_merge($mainInfo, $sub);
  277. // 过滤掉不在导出列里的字段,并补充缺失列
  278. $return[] = array_merge($defaultRow, array_intersect_key($fullRow, $defaultRow));
  279. }
  280. }
  281. }
  282. }
  283. public function getDetailsMap($main_ids)
  284. {
  285. // 获取详情
  286. $details = MonthlyPwOrderDetails::where('del_time', 0)
  287. ->whereIn('main_id', $main_ids)
  288. ->get();
  289. // 获取人员信息
  290. $empIds = $details->pluck('employee_id')->unique();
  291. $empMap = Employee::whereIn('id', $empIds)->get()->keyBy('id');
  292. $res = [];
  293. foreach ($details as $item) {
  294. $tmpEmp = $empMap[$item->employee_id] ?? null;
  295. // 组装每一行详情需要展示的字段
  296. $res[$item->main_id][] = [
  297. 'employee_number' => $tmpEmp ? $tmpEmp->number : '',
  298. 'employee_title' => $tmpEmp ? $tmpEmp->title : '',
  299. 'total_days' => $item->total_days,
  300. 'rd_total_days' => $item->rd_total_days,
  301. 'total_hours' => $item->total_hours,
  302. 'rd_total_hours' => $item->rd_total_hours,
  303. ];
  304. }
  305. return $res; // 返回 [main_id => [detail_row, detail_row]]
  306. }
  307. // 人员日工时单 ------------------------------------------------
  308. public function dailyPwOrderEdit($data,$user){
  309. list($status,$msg) = $this->dailyPwOrderRule($data, $user, false);
  310. if(!$status) return [$status,$msg];
  311. try {
  312. DB::beginTransaction();
  313. $model = DailyPwOrder::where('id',$data['id'])->first();
  314. $model->item_id = $data['item_id'] ?? 0;
  315. $model->save();
  316. $time = time();
  317. DailyPwOrderDetails::where('del_time',0)
  318. ->where('main_id', $model->id)
  319. ->update(['del_time' => $time]);
  320. $this->saveDetailDaily($model->id, $time, $data);
  321. DB::commit();
  322. }catch (\Exception $exception){
  323. DB::rollBack();
  324. return [false,$exception->getMessage()];
  325. }
  326. return [true, ''];
  327. }
  328. public function dailyPwOrderAdd($data,$user){
  329. list($status,$msg) = $this->dailyPwOrderRule($data, $user);
  330. if(!$status) return [$status,$msg];
  331. try {
  332. DB::beginTransaction();
  333. $model = new DailyPwOrder();
  334. $model->code = $this->generateBillNo([
  335. 'top_depart_id' => $user['top_depart_id'],
  336. 'type' => DailyPwOrder::Order_type,
  337. 'period' => date("Ym", $data['order_time'])
  338. ]);
  339. $model->order_time = $data['order_time'] ?? 0;
  340. $model->item_id = $data['item_id'] ?? 0;
  341. $model->crt_id = $user['id'];
  342. $model->top_depart_id = $data['top_depart_id'];
  343. $model->save();
  344. $this->saveDetailDaily($model->id, time(), $data);
  345. DB::commit();
  346. }catch (\Exception $exception){
  347. DB::rollBack();
  348. return [false,$exception->getMessage()];
  349. }
  350. return [true, ''];
  351. }
  352. private function saveDetailDaily($id, $time, $data){
  353. if(! empty($data['details'])){
  354. $unit = [];
  355. foreach ($data['details'] as $value){
  356. $unit[] = [
  357. 'main_id' => $id,
  358. 'employee_id' => $value['employee_id'],
  359. 'start_time_hour' => $value['start_time_hour'],
  360. 'start_time_min' => $value['start_time_min'],
  361. 'end_time_hour' => $value['end_time_hour'],
  362. 'end_time_min' => $value['end_time_min'],
  363. 'total_work_min' => $value['total_work_min'],
  364. 'crt_time' => $time,
  365. 'top_depart_id' => $value['top_depart_id'],
  366. ];
  367. }
  368. if(! empty($unit)) DailyPwOrderDetails::insert($unit);
  369. }
  370. }
  371. private function getDetailDaily($id){
  372. $data = DailyPwOrderDetails::where('del_time',0)
  373. ->where('main_id', $id)
  374. ->select('employee_id', 'start_time_hour', 'start_time_min', 'end_time_hour', 'end_time_min', 'total_work_min')
  375. ->get()->toArray();
  376. $id = array_column($data,'employee_id');
  377. $map = Employee::whereIn('id', $id)
  378. ->select('title','id','number')
  379. ->get()
  380. ->keyBy('id')
  381. ->toArray();
  382. foreach ($data as $key => $value){
  383. $tmp = $map[$value['employee_id']] ?? [];
  384. $merge = [];
  385. $merge['employee_title'] = $tmp['title'];
  386. $merge['employee_number'] = $tmp['number'];
  387. $data[$key] = array_merge($value, $merge);
  388. }
  389. $detail = [
  390. 'details' => $data,
  391. ];
  392. foreach ($detail as $key => $value) {
  393. if (empty($value)) {
  394. $detail[$key] = (object)[]; // 转成 stdClass 对象
  395. }
  396. }
  397. return $detail;
  398. }
  399. public function dailyPwOrderDel($data){
  400. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  401. try {
  402. DB::beginTransaction();
  403. $time = time();
  404. DailyPwOrder::where('del_time',0)
  405. ->whereIn('id',$data['id'])
  406. ->update(['del_time' => $time]);
  407. DailyPwOrderDetails::where('del_time',0)
  408. ->whereIn('main_id', $data['id'])
  409. ->update(['del_time' => $time]);
  410. DB::commit();
  411. }catch (\Exception $exception){
  412. DB::rollBack();
  413. return [false,$exception->getMessage()];
  414. }
  415. return [true, ''];
  416. }
  417. public function dailyPwOrderDetail($data, $user){
  418. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  419. $customer = DailyPwOrder::where('del_time',0)
  420. ->where('id',$data['id'])
  421. ->first();
  422. if(empty($customer)) return [false,'人员日工时单不存在或已被删除'];
  423. $customer = $customer->toArray();
  424. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  425. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  426. $item = Item::where('id', $customer['item_id'])->first();
  427. $customer['item_title'] = $item->title;
  428. $customer['item_code'] = $item->code;
  429. $customer['order_time'] = $customer['order_time'] ? date("Y-m-d",$customer['order_time']): '';
  430. $details = $this->getDetailDaily($data['id']);
  431. $customer = array_merge($customer, $details);
  432. return [true, $customer];
  433. }
  434. public function dailyPwOrderCommon($data,$user, $field = []){
  435. if(empty($field)) $field = DailyPwOrder::$field;
  436. $model = DailyPwOrder::Clear($user,$data);
  437. $model = $model->where('del_time',0)
  438. ->select($field)
  439. ->orderby('id', 'desc');
  440. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  441. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  442. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  443. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  444. $model->where('crt_time','>=',$return[0]);
  445. $model->where('crt_time','<=',$return[1]);
  446. }
  447. return $model;
  448. }
  449. public function dailyPwOrderList($data,$user){
  450. $model = $this->dailyPwOrderCommon($data, $user);
  451. $list = $this->limit($model,'',$data);
  452. $list = $this->fillDataDaily($list);
  453. return [true, $list];
  454. }
  455. public function dailyPwOrderRule(&$data, $user, $is_add = true){
  456. if(empty($data['order_time'])) return [false, '单据日期不能为空'];
  457. $data['order_time'] = $this->changeDateToDate($data['order_time']);
  458. $orderTime = $data['order_time'];
  459. $itemId = $data['item_id'] ?? 0;
  460. if(empty($itemId)) return [false, '项目不能为空'];
  461. $bool = Item::where('del_time',0)->where('id', $itemId)->exists();
  462. if(!$bool) return [false, '项目不存在或已被删除'];
  463. $data['top_depart_id'] = $user['top_depart_id'];
  464. if(empty($data['details'])) return [false, '人员日工时单明细不能为空'];
  465. // --- 1. 批量预获取人员信息,用于报错提示 ---
  466. $allEmpIds = array_filter(array_unique(array_column($data['details'], 'employee_id')));
  467. // 如果需要工号+姓名,建议这样获取:
  468. $empDisplayMap = Employee::whereIn('id', $allEmpIds)
  469. ->get(['id', 'number', 'title'])
  470. ->mapWithKeys(function($item){
  471. return [$item->id => "[{$item->number}]{$item->title}"];
  472. })->toArray();
  473. // 2. 本次提交内部重叠记录器
  474. $internalOverlap = [];
  475. foreach ($data['details'] as $key => $value){
  476. $empId = $value['employee_id'] ?? 0;
  477. if(empty($empId)) return [false, '人员不能为空'];
  478. $empName = $empDisplayMap[$empId] ?? "ID:{$empId}";
  479. $res = $this->checkNumber($value['start_time_hour'], 0, 'non-negative');
  480. if(!$res['valid']) return [false, "人员{$empName}开始点:" . $res['error']];
  481. if($value['start_time_hour'] > 23) return [false, false, "人员{$empName}开始点不合法"];
  482. $res = $this->checkNumber($value['start_time_min'], 0, 'non-negative');
  483. if(!$res['valid']) return [false, "人员{$empName}开始分:" . $res['error']];
  484. if($value['start_time_min'] > 60) return [false, false, "人员{$empName}开始点不合法"];
  485. $res = $this->checkNumber($value['end_time_hour'], 0, 'non-negative');
  486. if(!$res['valid']) return [false, "人员{$empName}结束点:" . $res['error']];
  487. if($value['end_time_hour'] > 24) return [false, false, "人员{$empName}结束点不合法"];
  488. $res = $this->checkNumber($value['end_time_min'], 0, 'non-negative');
  489. if(!$res['valid']) return [false, "人员{$empName}结束分:" . $res['error']];
  490. if($value['end_time_min'] > 60) return [false, false, "人员{$empName}结束分不合法"];
  491. $currentStart = $value['start_time_hour'] * 60 + $value['start_time_min'];
  492. $currentEnd = $value['end_time_hour'] * 60 + $value['end_time_min'];
  493. if ($currentStart >= $currentEnd) {
  494. return [false, "人员{$empName}:开始时间必须早于结束时间"];
  495. }
  496. // --- 3. 内部重叠校验(防止一次提交多行重复) ---
  497. if (isset($internalOverlap[$empId])) {
  498. foreach ($internalOverlap[$empId] as $period) {
  499. if ($currentStart < $period['e'] && $period['s'] < $currentEnd) {
  500. return [false, "人员{$empName}在本次提交的多行明细中时间段重叠"];
  501. }
  502. }
  503. }
  504. $internalOverlap[$empId][] = ['s' => $currentStart, 'e' => $currentEnd];
  505. $query = DB::table('daily_pw_order_details as d')
  506. ->join('daily_pw_order as m', 'd.main_id', '=', 'm.id')
  507. ->where('m.top_depart_id', $data['top_depart_id'])
  508. ->where('m.order_time', $orderTime)
  509. ->where('m.item_id', $itemId)
  510. ->where('d.employee_id', $empId)
  511. ->where('m.del_time', 0)
  512. ->where('d.del_time', 0);
  513. if (!$is_add && !empty($data['id'])) {
  514. $query->where('m.id', '<>', $data['id']);
  515. }
  516. $existingPeriods = $query->select('d.start_time_hour', 'd.start_time_min', 'd.end_time_hour', 'd.end_time_min')->get();
  517. foreach ($existingPeriods as $p) {
  518. $exStart = $p->start_time_hour * 60 + $p->start_time_min;
  519. $exEnd = $p->end_time_hour * 60 + $p->end_time_min;
  520. if ($currentStart < $exEnd && $exStart < $currentEnd) {
  521. return [false, "人员{$empName}在该项目该日已有其他工时单创建重叠的时间段数据"];
  522. }
  523. }
  524. $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
  525. }
  526. if(!$is_add){
  527. if(empty($data['id'])) return [false,'ID不能为空'];
  528. $bool = DailyPwOrder::where('top_depart_id', $data['top_depart_id'])
  529. ->where('id',$data['id'])
  530. ->where('del_time',0)
  531. ->exists();
  532. if(!$bool) return [false, '人员日工时单不存在或已被删除'];
  533. }
  534. return [true, ''];
  535. }
  536. public function fillDataDaily($data){
  537. if(empty($data['data'])) return $data;
  538. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  539. $item = (new ItemService())->getItemMap(array_unique(array_column($data['data'],'item_id')));
  540. foreach ($data['data'] as $key => $value){
  541. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  542. $data['data'][$key]['order_time'] = $value['order_time'] ? date('Y-m-d',$value['order_time']) : '';
  543. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  544. $item_tmp = $item[$value['item_id']] ?? [];
  545. $data['data'][$key]['item_title'] = $item_tmp['title'] ?? '';
  546. $data['data'][$key]['item_code'] = $item_tmp['code'] ?? '';
  547. }
  548. return $data;
  549. }
  550. public function fillDataForExportDaily($data, $column, &$return)
  551. {
  552. if (empty($data)) return;
  553. $mainIds = array_column($data, 'id');
  554. // 1. 获取详情及所有关联档案(项目、人员)的映射
  555. $detailsMap = $this->getDailyDetailsMap($mainIds, $data);
  556. foreach ($data as $main) {
  557. $mainId = $main['id'];
  558. $details = $detailsMap[$mainId] ?? [];
  559. // 2. 提取并格式化主表共有信息
  560. $mainInfo = [
  561. 'code' => $main['code'],
  562. 'order_time' => !empty($main['order_time']) ? date('Y-m-d', $main['order_time']) : '',
  563. ];
  564. if (empty($details)) {
  565. // 无明细时只导出一行主表信息
  566. $tempRow = [];
  567. foreach ($column as $col) {
  568. $tempRow[] = $mainInfo[$col] ?? '';
  569. }
  570. $return[] = $tempRow;
  571. } else {
  572. // 3. 平铺:将详情里的项目信息、人员信息与主表信息合并
  573. foreach ($details as $sub) {
  574. $fullRowData = array_merge($mainInfo, $sub);
  575. $tempRow = [];
  576. foreach ($column as $col) {
  577. $tempRow[] = $fullRowData[$col] ?? '';
  578. }
  579. $return[] = $tempRow;
  580. }
  581. }
  582. }
  583. }
  584. public function getDailyDetailsMap($mainIds, $mainData)
  585. {
  586. // 1. 获取所有子表记录
  587. $details = DB::table('daily_pw_order_details')
  588. ->where('del_time', 0)
  589. ->whereIn('main_id', $mainIds)
  590. ->get();
  591. // 2. 提取所有关联 ID
  592. $empIds = $details->pluck('employee_id')->unique();
  593. $itemIds = array_unique(array_column($mainData, 'item_id')); // 从主表数组提取项目ID
  594. // 3. 批量获取档案 Map
  595. $empMap = DB::table('employee')
  596. ->whereIn('id', $empIds)
  597. ->get(['id', 'title', 'number'])
  598. ->keyBy('id');
  599. $itemMap = DB::table('item')
  600. ->whereIn('id', $itemIds)
  601. ->get(['id', 'title', 'code'])
  602. ->keyBy('id');
  603. // 4. 将主表的项目信息预先挂载到主表 ID 下,方便后续合并
  604. $mainItemInfo = [];
  605. foreach ($mainData as $m) {
  606. $proj = $itemMap[$m['item_id']] ?? null;
  607. $mainItemInfo[$m['id']] = [
  608. 'item_code' => $proj ? $proj->code : '',
  609. 'item_title' => $proj ? $proj->title : '',
  610. ];
  611. }
  612. $res = [];
  613. if ($details->isEmpty()) {
  614. // 如果没有详情,把项目信息返回去,确保主表能导出行
  615. foreach ($mainItemInfo as $mId => $info) {
  616. $res[$mId] = [];
  617. }
  618. return $res;
  619. }
  620. foreach ($details as $item) {
  621. $emp = $empMap[$item->employee_id] ?? null;
  622. // 组装明细行数据
  623. $detailRow = [
  624. // 人员信息
  625. 'employee_number' => $emp ? $emp->number : '',
  626. 'employee_title' => $emp ? $emp->title : '',
  627. // 时间信息
  628. 'start_time' => sprintf('%02d:%02d', $item->start_time_hour, $item->start_time_min),
  629. 'end_time' => sprintf('%02d:%02d', $item->end_time_hour, $item->end_time_min),
  630. // 将主表的项目信息也塞进每一行详情里实现平铺
  631. 'item_code' => $mainItemInfo[$item->main_id]['item_code'] ?? '',
  632. 'item_title' => $mainItemInfo[$item->main_id]['item_title'] ?? '',
  633. ];
  634. $res[$item->main_id][] = $detailRow;
  635. }
  636. return $res;
  637. }
  638. public function dailyPwOrderCreate($data,$user){
  639. $data['top_depart_id'] = $user['top_depart_id'];
  640. if(empty($data['month'])) return [false, '月份不能为空'];
  641. $monthStart = $this->changeDateToDate($data['month']);
  642. $data['month'] = $monthStart;
  643. $monthEnd = strtotime('+1 month', $monthStart) - 1;
  644. }
  645. }