PersonSalaryService.php 11 KB

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