DeviceDepreciationService.php 10 KB

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