DeviceDepreciationService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\ProcessDeviceZjTask;
  4. use App\Model\Device;
  5. use App\Model\Employee;
  6. use App\Model\MonthlyDdOrder;
  7. use App\Model\MonthlyDdOrderDetails;
  8. use Illuminate\Support\Facades\DB;
  9. class DeviceDepreciationService extends Service
  10. {
  11. public function monthlyDdOrderEdit($data,$user){
  12. list($status,$msg) = $this->monthlyDdOrderRule($data, $user, false);
  13. if(!$status) return [$status,$msg];
  14. try {
  15. DB::beginTransaction();
  16. $model = monthlyDdOrder::where('id',$data['id'])->first();
  17. // $model->month = $data['month'] ?? 0;
  18. // $model->save();
  19. $time = time();
  20. monthlyDdOrderDetails::where('del_time',0)
  21. ->where('main_id', $model->id)
  22. ->update(['del_time' => $time]);
  23. $this->saveDetail($model->id, $time, $data);
  24. DB::commit();
  25. //触发折旧计算
  26. $this->triggerJob($model->id);
  27. }catch (\Exception $exception){
  28. DB::rollBack();
  29. return [false,$exception->getMessage()];
  30. }
  31. return [true, ''];
  32. }
  33. public function monthlyDdOrderAdd($data,$user){
  34. list($status,$msg) = $this->monthlyDdOrderRule($data, $user);
  35. if(!$status) return [$status,$msg];
  36. try {
  37. DB::beginTransaction();
  38. $model = new monthlyDdOrder();
  39. $model->code = $this->generateBillNo([
  40. 'top_depart_id' => $user['top_depart_id'],
  41. 'type' => monthlyDdOrder::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. //触发折旧计算
  51. $this->triggerJob($model->id);
  52. }catch (\Exception $exception){
  53. DB::rollBack();
  54. return [false,$exception->getMessage()];
  55. }
  56. return [true, ''];
  57. }
  58. private function saveDetail($id, $time, $data){
  59. if(! empty($data['details'])){
  60. $unit = [];
  61. foreach ($data['details'] as $value){
  62. $unit[] = [
  63. 'main_id' => $id,
  64. 'device_id' => $value['device_id'],
  65. 'depreciation_amount' => $value['depreciation_amount'],
  66. 'crt_time' => $time,
  67. 'top_depart_id' => $value['top_depart_id'],
  68. ];
  69. }
  70. if(! empty($unit)) monthlyDdOrderDetails::insert($unit);
  71. }
  72. }
  73. private function getDetail($id){
  74. $data = monthlyDdOrderDetails::where('del_time',0)
  75. ->where('main_id', $id)
  76. ->select('device_id', 'depreciation_amount')
  77. ->get()->toArray();
  78. $id = array_column($data,'device_id');
  79. $map = Device::whereIn('id', $id)->select('title','id','code','type')->get()->toArray();
  80. $map = array_column($map,null,'id');
  81. foreach ($data as $key => $value){
  82. $tmp = $map[$value['device_id']] ?? [];
  83. $merge = [];
  84. $merge['device_title'] = $tmp['title'];
  85. $merge['device_code'] = $tmp['code'];
  86. $merge['type_title'] = Device::$type[$tmp['type']] ?? "";
  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 monthlyDdOrderDel($data, $user){
  100. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  101. try {
  102. DB::beginTransaction();
  103. $time = time();
  104. $month = monthlyDdOrder::where('del_time',0)
  105. ->whereIn('id',$data['id'])
  106. ->pluck('month')
  107. ->toArray();
  108. //归档
  109. list($status, $msg) = ArchiveService::isArchive($month, $user);
  110. if(! $status) return [false, $msg];
  111. monthlyDdOrder::where('del_time',0)
  112. ->whereIn('id',$data['id'])
  113. ->update(['del_time' => $time]);
  114. monthlyDdOrderDetails::where('del_time',0)
  115. ->whereIn('main_id', $data['id'])
  116. ->update(['del_time' => $time]);
  117. DB::commit();
  118. //触发折旧计算
  119. $this->triggerJob($data['id']);
  120. }catch (\Exception $exception){
  121. DB::rollBack();
  122. return [false,$exception->getMessage()];
  123. }
  124. return [true, ''];
  125. }
  126. public function triggerJob($id){
  127. if(! is_array($id)) $id = [$id];
  128. ProcessDeviceZjTask::dispatch($id)->onQueue("count_device_zj");
  129. }
  130. public function monthlyDdOrderDetail($data, $user){
  131. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  132. $customer = monthlyDdOrder::where('del_time',0)
  133. ->where('id',$data['id'])
  134. ->first();
  135. if(empty($customer)) return [false,'设备月度折旧单不存在或已被删除'];
  136. $customer = $customer->toArray();
  137. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  138. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  139. $map = ArchiveService::fillIsArchive($customer['month'], $user);
  140. $customer['is_archive'] = $map[$customer['month']] ?? false;
  141. $customer['month'] = $customer['month'] ? date("Y-m",$customer['month']): '';
  142. $details = $this->getDetail($data['id']);
  143. $customer = array_merge($customer, $details);
  144. return [true, $customer];
  145. }
  146. public function monthlyDdOrderCommon($data,$user, $field = []){
  147. if(empty($field)) $field = monthlyDdOrder::$field;
  148. $model = monthlyDdOrder::Clear($user,$data);
  149. $model = $model->where('del_time',0)
  150. ->select($field)
  151. ->orderby('id', 'desc');
  152. if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
  153. $return = $this->changeDateToTimeStampAboutRange($data['time']);
  154. $model->where('month','>=',$return[0]);
  155. $model->where('month','<=',$return[1]);
  156. }
  157. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  158. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  159. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  160. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  161. $model->where('crt_time','>=',$return[0]);
  162. $model->where('crt_time','<=',$return[1]);
  163. }
  164. return $model;
  165. }
  166. public function monthlyDdOrderList($data,$user){
  167. $model = $this->monthlyDdOrderCommon($data, $user);
  168. $list = $this->limit($model,'',$data);
  169. $list = $this->fillData($list, $user);
  170. return [true, $list];
  171. }
  172. public function monthlyDdOrderRule(&$data, $user, $is_add = true){
  173. $data['top_depart_id'] = $user['top_depart_id'];
  174. if(empty($data['month'])) return [false, '月份不能为空'];
  175. $data['month'] = $this->changeDateToDate($data['month']);
  176. //归档
  177. list($status, $msg) = ArchiveService::isArchive($data['month'], $user);
  178. if(! $status) return [false, $msg];
  179. if(empty($data['details'])) return [false, '设备月度工时单明细不能为空'];
  180. foreach ($data['details'] as $key => $value){
  181. if(empty($value['device_id'])) return [false, '设备不能为空'];
  182. $res = $this->checkNumber($value['depreciation_amount'],2,'non-negative');
  183. if(! $res['valid']) return [false,'月折旧额:' . $res['error']];
  184. $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
  185. }
  186. list($status, $msg) = $this->checkArrayRepeat($data['details'],'device_id','设备');
  187. if(! $status) return [false, $msg];
  188. if($is_add){
  189. $bool = monthlyDdOrder::where('top_depart_id', $data['top_depart_id'])
  190. ->where('month', $data['month'])
  191. ->where('del_time',0)
  192. ->exists();
  193. }else{
  194. if(empty($data['id'])) return [false,'ID不能为空'];
  195. $bool = monthlyDdOrder::where('top_depart_id', $data['top_depart_id'])
  196. ->where('month', $data['month'])
  197. ->where('id','<>',$data['id'])
  198. ->where('del_time',0)
  199. ->exists();
  200. }
  201. if($bool) return [false, date("Y-m", $data['month']) . '已存在设备月度折旧单'];
  202. return [true, ''];
  203. }
  204. public function fillData($data, $user){
  205. if(empty($data['data'])) return $data;
  206. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  207. $map = ArchiveService::fillIsArchive(array_unique(array_column($data['data'],'month')), $user);
  208. foreach ($data['data'] as $key => $value){
  209. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  210. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  211. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  212. $data['data'][$key]['is_archive'] = $map[$value['month']] ?? false;
  213. }
  214. return $data;
  215. }
  216. public function fillDataForExport($data, $column, &$return)
  217. {
  218. if(empty($data)) return;
  219. $mainIds = array_column($data, 'id');
  220. // 获取详情映射 [main_id => [details...]]
  221. $detailsMap = $this->getDetailsMap($mainIds);
  222. // 默认空行模板
  223. $defaultRow = array_fill_keys($column, '');
  224. foreach ($data as $main) {
  225. $mainId = $main['id'];
  226. $details = $detailsMap[$mainId] ?? [];
  227. // 提取主表信息
  228. $mainInfo = [
  229. 'code' => $main['code'],
  230. 'month' => $main['month'] ? date('Y-m', $main['month']) : '',
  231. ];
  232. if (empty($details)) {
  233. // 如果没有详情,至少导出一行主表信息(可选)
  234. $return[] = array_merge($defaultRow, $mainInfo);
  235. } else {
  236. // 核心:遍历详情,每一行详情都合并主表信息
  237. foreach ($details as $sub) {
  238. // 合并主表字段 + 详情字段
  239. $fullRow = array_merge($mainInfo, $sub);
  240. // 过滤掉不在导出列里的字段,并补充缺失列
  241. $return[] = array_merge($defaultRow, array_intersect_key($fullRow, $defaultRow));
  242. }
  243. }
  244. }
  245. }
  246. public function getDetailsMap($main_ids)
  247. {
  248. // 获取详情
  249. $details = monthlyDdOrderDetails::where('del_time', 0)
  250. ->whereIn('main_id', $main_ids)
  251. ->get();
  252. // 获取设备信息
  253. $empIds = $details->pluck('device_id')->unique();
  254. $empMap = Device::whereIn('id', $empIds)->get()->keyBy('id');
  255. $res = [];
  256. foreach ($details as $item) {
  257. $tmpEmp = $empMap[$item->device_id] ?? null;
  258. // 组装每一行详情需要展示的字段
  259. $res[$item->main_id][] = [
  260. 'device_code' => $tmpEmp ? $tmpEmp->code : '',
  261. 'device_title' => $tmpEmp ? $tmpEmp->title : '',
  262. 'depreciation_amount' => $item->depreciation_amount,
  263. ];
  264. }
  265. return $res; // 返回 [main_id => [detail_row, detail_row]]
  266. }
  267. }