PersonSalaryService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. 'base_salary' => $value['base_salary'],
  60. 'performance_salary' => $value['performance_salary'],
  61. 'bonus' => $value['bonus'],
  62. 'other' => $value['other'],
  63. 'social_insurance' => $value['social_insurance'],
  64. 'public_housing_fund' => $value['public_housing_fund'],
  65. 'crt_time' => $time,
  66. 'top_depart_id' => $value['top_depart_id'],
  67. ];
  68. }
  69. if(! empty($unit)) MonthlyPsOrderDetails::insert($unit);
  70. }
  71. }
  72. private function getDetail($id){
  73. $data = MonthlyPsOrderDetails::where('del_time',0)
  74. ->where('main_id', $id)
  75. ->select('employee_id', 'base_salary', 'social_insurance', 'public_housing_fund', 'performance_salary', 'bonus', 'other')
  76. ->get()->toArray();
  77. $id = array_column($data,'employee_id');
  78. $map = Employee::whereIn('id', $id)->select('title','id','number')->get()->toArray();
  79. $map = array_column($map,null,'id');
  80. foreach ($data as $key => $value){
  81. $tmp = $map[$value['employee_id']] ?? [];
  82. $merge = [];
  83. $merge['employee_title'] = $tmp['title'];
  84. $merge['employee_number'] = $tmp['number'];
  85. $data[$key] = array_merge($value, $merge);
  86. }
  87. $detail = [
  88. 'details' => $data,
  89. ];
  90. //foreach ($detail as $key => $value) {
  91. // if (empty($value)) {
  92. //$detail[$key] = (object)[]; // 转成 stdClass 对象
  93. //}
  94. //}
  95. return $detail;
  96. }
  97. public function MonthlyPsOrderDel($data, $user){
  98. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  99. try {
  100. DB::beginTransaction();
  101. $time = time();
  102. $month = MonthlyPsOrder::where('del_time',0)
  103. ->whereIn('id',$data['id'])
  104. ->pluck('month')
  105. ->toArray();
  106. //归档
  107. list($status, $msg) = ArchiveService::isArchive($month, $user);
  108. if(! $status) return [false, $msg];
  109. MonthlyPsOrder::where('del_time',0)
  110. ->whereIn('id',$data['id'])
  111. ->update(['del_time' => $time]);
  112. MonthlyPsOrderDetails::where('del_time',0)
  113. ->whereIn('main_id', $data['id'])
  114. ->update(['del_time' => $time]);
  115. DB::commit();
  116. }catch (\Exception $exception){
  117. DB::rollBack();
  118. return [false,$exception->getMessage()];
  119. }
  120. return [true, ''];
  121. }
  122. public function MonthlyPsOrderDetail($data, $user){
  123. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  124. $customer = MonthlyPsOrder::where('del_time',0)
  125. ->where('id',$data['id'])
  126. ->first();
  127. if(empty($customer)) return [false,'人员月度工资单不存在或已被删除'];
  128. $customer = $customer->toArray();
  129. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  130. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  131. $customer['month'] = $customer['month'] ? date("Y-m",$customer['month']): '';
  132. $details = $this->getDetail($data['id']);
  133. $customer = array_merge($customer, $details);
  134. return [true, $customer];
  135. }
  136. public function MonthlyPsOrderCommon($data,$user, $field = []){
  137. if(empty($field)) $field = MonthlyPsOrder::$field;
  138. $model = MonthlyPsOrder::Clear($user,$data);
  139. $model = $model->where('del_time',0)
  140. ->select($field)
  141. ->orderby('id', 'desc');
  142. if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
  143. $return = $this->changeDateToTimeStampAboutRange($data['time']);
  144. $model->where('month','>=',$return[0]);
  145. $model->where('month','<=',$return[1]);
  146. }
  147. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  148. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  149. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  150. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  151. $model->where('crt_time','>=',$return[0]);
  152. $model->where('crt_time','<=',$return[1]);
  153. }
  154. return $model;
  155. }
  156. public function MonthlyPsOrderList($data,$user){
  157. $model = $this->MonthlyPsOrderCommon($data, $user);
  158. $list = $this->limit($model,'',$data);
  159. $list = $this->fillData($list);
  160. return [true, $list];
  161. }
  162. public function MonthlyPsOrderRule(&$data, $user, $is_add = true){
  163. if(empty($data['month'])) return [false, '月份不能为空'];
  164. $data['month'] = $this->changeDateToDate($data['month']);
  165. $data['top_depart_id'] = $user['top_depart_id'];
  166. //归档
  167. list($status, $msg) = ArchiveService::isArchive($data['month'], $user);
  168. if(! $status) return [false, $msg];
  169. if(empty($data['details'])) return [false, '人员月度工时单明细不能为空'];
  170. foreach ($data['details'] as $key => $value){
  171. if(empty($value['employee_id'])) return [false, '人员不能为空'];
  172. $res = $this->checkNumber($value['base_salary'],2,'non-negative');
  173. if(! $res['valid']) return [false,'基本工资:' . $res['error']];
  174. $res = $this->checkNumber($value['performance_salary'],2,'non-negative');
  175. if(! $res['valid']) return [false,'绩效工资:' . $res['error']];
  176. $res = $this->checkNumber($value['social_insurance'],2,'non-negative');
  177. if(! $res['valid']) return [false,'社保:' . $res['error']];
  178. $res = $this->checkNumber($value['public_housing_fund'],2,'non-negative');
  179. if(! $res['valid']) return [false,'公积金:' . $res['error']];
  180. $res = $this->checkNumber($value['bonus'],2,'non-negative');
  181. if(! $res['valid']) return [false,'奖金工资:' . $res['error']];
  182. $res = $this->checkNumber($value['other'],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'],'employee_id','人员');
  187. if(! $status) return [false, $msg];
  188. if($is_add){
  189. $bool = MonthlyPsOrder::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 = MonthlyPsOrder::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){
  205. if(empty($data['data'])) return $data;
  206. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  207. foreach ($data['data'] as $key => $value){
  208. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  209. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  210. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  211. }
  212. return $data;
  213. }
  214. public function fillDataForExport($data, $column, &$return)
  215. {
  216. if(empty($data)) return;
  217. $mainIds = array_column($data, 'id');
  218. // 获取详情映射 [main_id => [details...]]
  219. $detailsMap = $this->getDetailsMap($mainIds);
  220. // 默认空行模板
  221. $defaultRow = array_fill_keys($column, '');
  222. foreach ($data as $main) {
  223. $mainId = $main['id'];
  224. $details = $detailsMap[$mainId] ?? [];
  225. // 提取主表信息
  226. $mainInfo = [
  227. 'code' => $main['code'],
  228. 'month' => $main['month'] ? date('Y-m', $main['month']) : '',
  229. ];
  230. if (empty($details)) {
  231. // 如果没有详情,至少导出一行主表信息(可选)
  232. $return[] = array_merge($defaultRow, $mainInfo);
  233. } else {
  234. // 核心:遍历详情,每一行详情都合并主表信息
  235. foreach ($details as $sub) {
  236. // 合并主表字段 + 详情字段
  237. $fullRow = array_merge($mainInfo, $sub);
  238. // 过滤掉不在导出列里的字段,并补充缺失列
  239. $return[] = array_merge($defaultRow, array_intersect_key($fullRow, $defaultRow));
  240. }
  241. }
  242. }
  243. }
  244. public function getDetailsMap($main_ids)
  245. {
  246. // 获取详情
  247. $details = MonthlyPsOrderDetails::where('del_time', 0)
  248. ->whereIn('main_id', $main_ids)
  249. ->get();
  250. // 获取人员信息
  251. $empIds = $details->pluck('employee_id')->unique();
  252. $empMap = Employee::whereIn('id', $empIds)->get()->keyBy('id');
  253. $res = [];
  254. foreach ($details as $item) {
  255. $tmpEmp = $empMap[$item->employee_id] ?? null;
  256. // 组装每一行详情需要展示的字段
  257. $res[$item->main_id][] = [
  258. 'employee_number' => $tmpEmp ? $tmpEmp->number : '',
  259. 'employee_title' => $tmpEmp ? $tmpEmp->title : '',
  260. 'base_salary' => $item->base_salary,
  261. 'social_insurance' => $item->social_insurance,
  262. 'public_housing_fund' => $item->public_housing_fund,
  263. 'performance_salary' => $item->performance_salary,
  264. 'bonus' => $item->bonus,
  265. 'other' => $item->other,
  266. ];
  267. }
  268. return $res; // 返回 [main_id => [detail_row, detail_row]]
  269. }
  270. }