DeviceDepreciationService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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','type')->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. $merge['type_title'] = Device::$type[$tmp['type']] ?? "";
  82. $data[$key] = array_merge($value, $merge);
  83. }
  84. $detail = [
  85. 'details' => $data,
  86. ];
  87. //foreach ($detail as $key => $value) {
  88. // if (empty($value)) {
  89. //$detail[$key] = (object)[]; // 转成 stdClass 对象
  90. //}
  91. //}
  92. return $detail;
  93. }
  94. public function monthlyDdOrderDel($data){
  95. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  96. try {
  97. DB::beginTransaction();
  98. $time = time();
  99. monthlyDdOrder::where('del_time',0)
  100. ->whereIn('id',$data['id'])
  101. ->update(['del_time' => $time]);
  102. monthlyDdOrderDetails::where('del_time',0)
  103. ->whereIn('main_id', $data['id'])
  104. ->update(['del_time' => $time]);
  105. DB::commit();
  106. }catch (\Exception $exception){
  107. DB::rollBack();
  108. return [false,$exception->getMessage()];
  109. }
  110. return [true, ''];
  111. }
  112. public function monthlyDdOrderDetail($data, $user){
  113. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  114. $customer = monthlyDdOrder::where('del_time',0)
  115. ->where('id',$data['id'])
  116. ->first();
  117. if(empty($customer)) return [false,'设备月度折旧单不存在或已被删除'];
  118. $customer = $customer->toArray();
  119. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  120. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  121. $customer['month'] = $customer['month'] ? date("Y-m",$customer['month']): '';
  122. $details = $this->getDetail($data['id']);
  123. $customer = array_merge($customer, $details);
  124. return [true, $customer];
  125. }
  126. public function monthlyDdOrderCommon($data,$user, $field = []){
  127. if(empty($field)) $field = monthlyDdOrder::$field;
  128. $model = monthlyDdOrder::Clear($user,$data);
  129. $model = $model->where('del_time',0)
  130. ->select($field)
  131. ->orderby('id', 'desc');
  132. if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
  133. $return = $this->changeDateToTimeStampAboutRange($data['time']);
  134. $model->where('month','>=',$return[0]);
  135. $model->where('month','<=',$return[1]);
  136. }
  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 monthlyDdOrderList($data,$user){
  147. $model = $this->monthlyDdOrderCommon($data, $user);
  148. $list = $this->limit($model,'',$data);
  149. $list = $this->fillData($list);
  150. return [true, $list];
  151. }
  152. public function monthlyDdOrderRule(&$data, $user, $is_add = true){
  153. if(empty($data['month'])) return [false, '月份不能为空'];
  154. $data['month'] = $this->changeDateToDate($data['month']);
  155. $data['top_depart_id'] = $user['top_depart_id'];
  156. if(empty($data['details'])) return [false, '设备月度工时单明细不能为空'];
  157. foreach ($data['details'] as $key => $value){
  158. if(empty($value['device_id'])) return [false, '设备不能为空'];
  159. $res = $this->checkNumber($value['depreciation_amount'],2,'non-negative');
  160. if(! $res['valid']) return [false,'月折旧额:' . $res['error']];
  161. $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
  162. }
  163. list($status, $msg) = $this->checkArrayRepeat($data['details'],'device_id','设备');
  164. if(! $status) return [false, $msg];
  165. if($is_add){
  166. $bool = monthlyDdOrder::where('top_depart_id', $data['top_depart_id'])
  167. ->where('month', $data['month'])
  168. ->where('del_time',0)
  169. ->exists();
  170. }else{
  171. if(empty($data['id'])) return [false,'ID不能为空'];
  172. $bool = monthlyDdOrder::where('top_depart_id', $data['top_depart_id'])
  173. ->where('month', $data['month'])
  174. ->where('id','<>',$data['id'])
  175. ->where('del_time',0)
  176. ->exists();
  177. }
  178. if($bool) return [false, date("Y-m", $data['month']) . '已存在设备月度折旧单'];
  179. return [true, ''];
  180. }
  181. public function fillData($data){
  182. if(empty($data['data'])) return $data;
  183. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  184. foreach ($data['data'] as $key => $value){
  185. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  186. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  187. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  188. }
  189. return $data;
  190. }
  191. public function fillDataForExport($data, $column, &$return)
  192. {
  193. if(empty($data)) return;
  194. $mainIds = array_column($data, 'id');
  195. // 获取详情映射 [main_id => [details...]]
  196. $detailsMap = $this->getDetailsMap($mainIds);
  197. // 默认空行模板
  198. $defaultRow = array_fill_keys($column, '');
  199. foreach ($data as $main) {
  200. $mainId = $main['id'];
  201. $details = $detailsMap[$mainId] ?? [];
  202. // 提取主表信息
  203. $mainInfo = [
  204. 'code' => $main['code'],
  205. 'month' => $main['month'] ? date('Y-m', $main['month']) : '',
  206. ];
  207. if (empty($details)) {
  208. // 如果没有详情,至少导出一行主表信息(可选)
  209. $return[] = array_merge($defaultRow, $mainInfo);
  210. } else {
  211. // 核心:遍历详情,每一行详情都合并主表信息
  212. foreach ($details as $sub) {
  213. // 合并主表字段 + 详情字段
  214. $fullRow = array_merge($mainInfo, $sub);
  215. // 过滤掉不在导出列里的字段,并补充缺失列
  216. $return[] = array_merge($defaultRow, array_intersect_key($fullRow, $defaultRow));
  217. }
  218. }
  219. }
  220. }
  221. public function getDetailsMap($main_ids)
  222. {
  223. // 获取详情
  224. $details = monthlyDdOrderDetails::where('del_time', 0)
  225. ->whereIn('main_id', $main_ids)
  226. ->get();
  227. // 获取设备信息
  228. $empIds = $details->pluck('device_id')->unique();
  229. $empMap = Device::whereIn('id', $empIds)->get()->keyBy('id');
  230. $res = [];
  231. foreach ($details as $item) {
  232. $tmpEmp = $empMap[$item->device_id] ?? null;
  233. // 组装每一行详情需要展示的字段
  234. $res[$item->main_id][] = [
  235. 'device_code' => $tmpEmp ? $tmpEmp->code : '',
  236. 'device_title' => $tmpEmp ? $tmpEmp->title : '',
  237. 'depreciation_amount' => $item->depreciation_amount,
  238. ];
  239. }
  240. return $res; // 返回 [main_id => [detail_row, detail_row]]
  241. }
  242. }