| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- namespace App\Service;
- use App\Model\Device;
- use App\Model\Employee;
- use App\Model\MonthlyDdOrder;
- use App\Model\MonthlyDdOrderDetails;
- use Illuminate\Support\Facades\DB;
- class DeviceDepreciationService extends Service
- {
- public function monthlyDdOrderEdit($data,$user){
- list($status,$msg) = $this->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')->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'];
- $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){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- try {
- DB::beginTransaction();
- $time = time();
- 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['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){
- if(empty($data['month'])) return [false, '月份不能为空'];
- $data['month'] = $this->changeDateToDate($data['month']);
- $data['top_depart_id'] = $user['top_depart_id'];
- 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 : '',
- 'depreciation_amount' => $item->depreciation_amount,
- ];
- }
- return $res; // 返回 [main_id => [detail_row, detail_row]]
- }
- }
|