monthlyDdOrderRule($data, $user, false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = monthlyDdOrder::where('id',$data['id'])->first(); // $model->month = $data['month'] ?? 0; // $model->save(); $time = time(); monthlyDdOrderDetails::where('del_time',0) ->where('main_id', $model->id) ->update(['del_time' => $time]); $this->saveDetail($model->id, $time, $data); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } public function monthlyDdOrderAdd($data,$user){ list($status,$msg) = $this->monthlyDdOrderRule($data, $user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new monthlyDdOrder(); $model->code = $this->generateBillNo([ 'top_depart_id' => $user['top_depart_id'], 'type' => monthlyDdOrder::Order_type, 'period' => date("Ym", $data['month']) ]); $model->month = $data['month'] ?? 0; $model->crt_id = $user['id']; $model->top_depart_id = $data['top_depart_id']; $model->save(); $this->saveDetail($model->id, time(), $data); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } private function saveDetail($id, $time, $data){ if(! empty($data['details'])){ $unit = []; foreach ($data['details'] as $value){ $unit[] = [ 'main_id' => $id, 'device_id' => $value['device_id'], 'depreciation_amount' => $value['depreciation_amount'], 'crt_time' => $time, 'top_depart_id' => $value['top_depart_id'], ]; } if(! empty($unit)) monthlyDdOrderDetails::insert($unit); } } private function getDetail($id){ $data = monthlyDdOrderDetails::where('del_time',0) ->where('main_id', $id) ->select('device_id', 'depreciation_amount') ->get()->toArray(); $id = array_column($data,'device_id'); $map = Device::whereIn('id', $id)->select('title','id','code','type')->get()->toArray(); $map = array_column($map,null,'id'); foreach ($data as $key => $value){ $tmp = $map[$value['device_id']] ?? []; $merge = []; $merge['device_title'] = $tmp['title']; $merge['device_code'] = $tmp['code']; $merge['type_title'] = Device::$type[$tmp['type']] ?? ""; $data[$key] = array_merge($value, $merge); } $detail = [ 'details' => $data, ]; //foreach ($detail as $key => $value) { // if (empty($value)) { //$detail[$key] = (object)[]; // 转成 stdClass 对象 //} //} return $detail; } public function monthlyDdOrderDel($data, $user){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; try { DB::beginTransaction(); $time = time(); $month = monthlyDdOrder::where('del_time',0) ->whereIn('id',$data['id']) ->pluck('month') ->toArray(); //归档 list($status, $msg) = ArchiveService::isArchive($month, $user); if(! $status) return [false, $msg]; monthlyDdOrder::where('del_time',0) ->whereIn('id',$data['id']) ->update(['del_time' => $time]); monthlyDdOrderDetails::where('del_time',0) ->whereIn('main_id', $data['id']) ->update(['del_time' => $time]); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } public function monthlyDdOrderDetail($data, $user){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; $customer = monthlyDdOrder::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($customer)) return [false,'设备月度折旧单不存在或已被删除']; $customer = $customer->toArray(); $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title'); $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): ''; $customer['month'] = $customer['month'] ? date("Y-m",$customer['month']): ''; $details = $this->getDetail($data['id']); $customer = array_merge($customer, $details); return [true, $customer]; } public function monthlyDdOrderCommon($data,$user, $field = []){ if(empty($field)) $field = monthlyDdOrder::$field; $model = monthlyDdOrder::Clear($user,$data); $model = $model->where('del_time',0) ->select($field) ->orderby('id', 'desc'); if(! empty($data['time'][0]) && ! empty($data['time'][1])) { $return = $this->changeDateToTimeStampAboutRange($data['time']); $model->where('month','>=',$return[0]); $model->where('month','<=',$return[1]); } if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%'); if(! empty($data['id'])) $model->whereIn('id', $data['id']); if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) { $return = $this->changeDateToTimeStampAboutRange($data['crt_time']); $model->where('crt_time','>=',$return[0]); $model->where('crt_time','<=',$return[1]); } return $model; } public function monthlyDdOrderList($data,$user){ $model = $this->monthlyDdOrderCommon($data, $user); $list = $this->limit($model,'',$data); $list = $this->fillData($list); return [true, $list]; } public function monthlyDdOrderRule(&$data, $user, $is_add = true){ $data['top_depart_id'] = $user['top_depart_id']; if(empty($data['month'])) return [false, '月份不能为空']; $data['month'] = $this->changeDateToDate($data['month']); //归档 list($status, $msg) = ArchiveService::isArchive($data['month'], $user); if(! $status) return [false, $msg]; if(empty($data['details'])) return [false, '设备月度工时单明细不能为空']; foreach ($data['details'] as $key => $value){ if(empty($value['device_id'])) return [false, '设备不能为空']; $res = $this->checkNumber($value['depreciation_amount'],2,'non-negative'); if(! $res['valid']) return [false,'月折旧额:' . $res['error']]; $data['details'][$key]['top_depart_id'] = $data['top_depart_id']; } list($status, $msg) = $this->checkArrayRepeat($data['details'],'device_id','设备'); if(! $status) return [false, $msg]; if($is_add){ $bool = monthlyDdOrder::where('top_depart_id', $data['top_depart_id']) ->where('month', $data['month']) ->where('del_time',0) ->exists(); }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = monthlyDdOrder::where('top_depart_id', $data['top_depart_id']) ->where('month', $data['month']) ->where('id','<>',$data['id']) ->where('del_time',0) ->exists(); } if($bool) return [false, date("Y-m", $data['month']) . '已存在设备月度折旧单']; return [true, '']; } public function fillData($data){ if(empty($data['data'])) return $data; $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id'))); foreach ($data['data'] as $key => $value){ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : ''; $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : ''; $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? ''; } return $data; } public function fillDataForExport($data, $column, &$return) { if(empty($data)) return; $mainIds = array_column($data, 'id'); // 获取详情映射 [main_id => [details...]] $detailsMap = $this->getDetailsMap($mainIds); // 默认空行模板 $defaultRow = array_fill_keys($column, ''); foreach ($data as $main) { $mainId = $main['id']; $details = $detailsMap[$mainId] ?? []; // 提取主表信息 $mainInfo = [ 'code' => $main['code'], 'month' => $main['month'] ? date('Y-m', $main['month']) : '', ]; if (empty($details)) { // 如果没有详情,至少导出一行主表信息(可选) $return[] = array_merge($defaultRow, $mainInfo); } else { // 核心:遍历详情,每一行详情都合并主表信息 foreach ($details as $sub) { // 合并主表字段 + 详情字段 $fullRow = array_merge($mainInfo, $sub); // 过滤掉不在导出列里的字段,并补充缺失列 $return[] = array_merge($defaultRow, array_intersect_key($fullRow, $defaultRow)); } } } } public function getDetailsMap($main_ids) { // 获取详情 $details = monthlyDdOrderDetails::where('del_time', 0) ->whereIn('main_id', $main_ids) ->get(); // 获取设备信息 $empIds = $details->pluck('device_id')->unique(); $empMap = Device::whereIn('id', $empIds)->get()->keyBy('id'); $res = []; foreach ($details as $item) { $tmpEmp = $empMap[$item->device_id] ?? null; // 组装每一行详情需要展示的字段 $res[$item->main_id][] = [ 'device_code' => $tmpEmp ? $tmpEmp->code : '', 'device_title' => $tmpEmp ? $tmpEmp->title : '', 'depreciation_amount' => $item->depreciation_amount, ]; } return $res; // 返回 [main_id => [detail_row, detail_row]] } }