SalaryCalculationService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <?php
  2. namespace app\service;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Log;
  6. /**
  7. * 工资计算服务类
  8. */
  9. class SalaryCalculationService
  10. {
  11. /**
  12. * 执行工资计算主方法
  13. * @param array $params 计算参数
  14. * @return array 计算结果
  15. */
  16. public function calculateSalary(array $params): array
  17. {
  18. try {
  19. Log::info('开始执行工资计算', $params);
  20. // 参数验证
  21. $this->validateParams($params);
  22. $startDate = $params['start_date'];
  23. $endDate = $params['end_date'];
  24. $attendanceMonth = $params['date'];
  25. $sysId = $params['sys_id'] ?? '';
  26. // 1. 获取法定天数
  27. $legalDays = $this->getLegalDays($attendanceMonth);
  28. $params['days'] = $legalDays;
  29. // 2. 获取节假日数据
  30. $holidayData = $this->getHolidayData($params);
  31. // 3. 删除原有数据
  32. $this->clearOldData($startDate, $endDate);
  33. // 4. 查询并处理各个数据源
  34. $allData = [];
  35. // 4.1 处理设备_产量计酬数据
  36. $machineData = $this->processMachineData($params, $startDate, $endDate, $holidayData);
  37. $allData = array_merge($allData, $machineData);
  38. // 4.2 处理拆片工序数据
  39. $splitData = $this->processSplitData($params, $startDate, $endDate, $holidayData);
  40. $allData = array_merge($allData, $splitData);
  41. // 4.3 处理手工检验数据
  42. $manualData = $this->processManualData($params, $startDate, $endDate);
  43. $allData = array_merge($allData, $manualData);
  44. // 4.4 处理包装计件数据
  45. $packagingData = $this->processPackagingData($params, $startDate, $endDate);
  46. $allData = array_merge($allData, $packagingData);
  47. // 5. 计算工时占比和工资
  48. $calculatedData = $this->calculateWorkHoursAndWages($allData, $legalDays, $holidayData);
  49. // 6. 保存数据到数据库
  50. $insertCount = $this->saveSalaryData($calculatedData);
  51. Log::info('工资计算完成', [
  52. 'month' => $attendanceMonth,
  53. 'total_records' => count($calculatedData),
  54. 'insert_count' => $insertCount
  55. ]);
  56. return [
  57. 'success' => true,
  58. 'message' => '工资计算完成',
  59. 'data' => [
  60. 'month' => $attendanceMonth,
  61. 'total_records' => count($calculatedData),
  62. 'insert_count' => $insertCount,
  63. 'start_date' => $startDate,
  64. 'end_date' => $endDate
  65. ]
  66. ];
  67. } catch (Exception $e) {
  68. Log::error('工资计算失败: ' . $e->getMessage());
  69. return [
  70. 'success' => false,
  71. 'message' => '工资计算失败: ' . $e->getMessage()
  72. ];
  73. }
  74. }
  75. /**
  76. * 参数验证
  77. */
  78. protected function validateParams(array $params): void
  79. {
  80. if (empty($params['date']) || empty($params['start_date']) || empty($params['end_date'])) {
  81. throw new Exception('参数错误:日期参数不能为空');
  82. }
  83. // 确保开始日期和结束日期在考勤年月的范围内
  84. $startDateInRange = date('Ym', strtotime($params['start_date'])) === $params['date'];
  85. $endDateInRange = date('Ym', strtotime($params['end_date'])) === $params['date'];
  86. if (!$startDateInRange || !$endDateInRange) {
  87. throw new Exception('日期参数错误:开始日期或结束日期不在考勤月份内');
  88. }
  89. }
  90. /**
  91. * 获取法定天数
  92. */
  93. protected function getLegalDays(string $month): int
  94. {
  95. $clockingInDay = Db::name('人事_考勤资料')
  96. ->where('kqzl_ny', $month)
  97. ->field('法定天数')
  98. ->order('UniqId desc')
  99. ->find();
  100. if (empty($clockingInDay)) {
  101. throw new Exception('未找到该月的法定天数设置');
  102. }
  103. return (int)$clockingInDay['法定天数'];
  104. }
  105. /**
  106. * 获取节假日数据
  107. */
  108. protected function getHolidayData(array $params): array
  109. {
  110. $holidayData = [
  111. 'vacation_one' => [],
  112. 'vacation_two' => []
  113. ];
  114. // 处理法定假日1
  115. if (!empty($params['vacation_one_start']) && !empty($params['vacation_one_end'])) {
  116. $vacationOneStart = strtotime($params['vacation_one_start']);
  117. $vacationOneEnd = strtotime($params['vacation_one_end']);
  118. for ($i = $vacationOneStart; $i <= $vacationOneEnd; $i += 86400) {
  119. $holidayData['vacation_one'][] = date("Y-m-d", $i);
  120. }
  121. }
  122. // 处理法定假日2
  123. if (!empty($params['vacation_two_start']) && !empty($params['vacation_two_end'])) {
  124. $vacationTwoStart = strtotime($params['vacation_two_start']);
  125. $vacationTwoEnd = strtotime($params['vacation_two_end']);
  126. for ($i = $vacationTwoStart; $i <= $vacationTwoEnd; $i += 86400) {
  127. $holidayData['vacation_two'][] = date("Y-m-d", $i);
  128. }
  129. }
  130. return $holidayData;
  131. }
  132. /**
  133. * 清除旧数据
  134. */
  135. protected function clearOldData(string $startDate, string $endDate): int
  136. {
  137. return Db::name('绩效工资汇总')
  138. ->where('sczl_rq', 'between', [$startDate, $endDate])
  139. ->delete();
  140. }
  141. /**
  142. * 处理设备产量计酬数据
  143. */
  144. protected function processMachineData(array $params, string $startDate, string $endDate, array $holidayData): array
  145. {
  146. $where = ['a.sczl_rq' => ['between', [$startDate, $endDate]]];
  147. // 查询印刷印后车间的机台
  148. $sist = ['胶印车间', '凹丝印车间', '印后车间', '检验车间'];
  149. $jtbhs = Db::name('设备_基本资料')
  150. ->where('sys_sbID', '<>', '')
  151. ->where('使用部门', 'in', $sist)
  152. ->column('设备编号');
  153. if (empty($jtbhs)) {
  154. return [];
  155. }
  156. $fields = "a.sczl_gdbh,a.sczl_yjno,a.sczl_gxh,a.sczl_type as sczl_type,a.sczl_rq,a.sczl_jtbh,
  157. a.sczl_工价系数,a.sczl_ms,a.sczl_cl as 班组车头产量,a.sczl_Pgcl,a.sczl_zcfp,
  158. a.sczl_装版工时 as 装版工时,a.sczl_保养工时 as 保养工时,a.sczl_打样工时 as 打样工时,
  159. a.sczl_异常工时1 as 异常停机工时,a.sczl_设备运行工时 as 车头产量占用机时,
  160. a.sczl_bh1,a.sczl_bh2,a.sczl_bh3,a.sczl_bh4,a.sczl_bh5,a.sczl_bh6,a.sczl_bh7,a.sczl_bh8,a.sczl_bh9,a.sczl_bh10,
  161. a.sczl_rate1,a.sczl_rate2,a.sczl_rate3,a.sczl_rate4,a.sczl_rate5,a.sczl_rate6,a.sczl_rate7,a.sczl_rate8,a.sczl_rate9,a.sczl_rate10,
  162. a.sczl_废品率系数,a.UniqId,
  163. b.千件工价,b.日定额,b.补产标准,c.工价系数 as 工序难度系数,c.版距,c.印刷方式
  164. ,d1.员工姓名 as name1,d2.员工姓名 as name2,d3.员工姓名 as name3,d4.员工姓名 as name4,d5.员工姓名 as name5,d6.员工姓名 as name6,d7.员工姓名 as name7,d8.员工姓名 as name8
  165. ,d9.员工姓名 as name9,d10.员工姓名 as name10";
  166. $query = Db::name('设备_产量计酬')->alias('a')->field($fields);
  167. $query->join('dic_lzde b', 'a.sczl_dedh = b.sys_bh', 'LEFT');
  168. $query->join('工单_工艺资料 c', 'a.sczl_gdbh = c.Gy0_gdbh AND a.sczl_yjno = c.Gy0_yjno AND a.sczl_gxh = c.Gy0_gxh', 'LEFT');
  169. for ($i = 1; $i <= 10; $i++) {
  170. $field = 'a.sczl_bh' . $i;
  171. $alias = 'd' . $i;
  172. $query->join("人事_基本资料 $alias", "$field = {$alias}.员工编号 AND {$field} IS NOT NULL", 'LEFT');
  173. }
  174. $list = $query->where($where)
  175. ->where('a.sczl_jtbh', 'in', $jtbhs)
  176. ->order('a.sczl_rq')
  177. ->group('UniqId')
  178. ->select();
  179. $data = [];
  180. foreach ($list as $value) {
  181. $data = array_merge($data, $this->processMachineItem($value, $holidayData));
  182. }
  183. return $data;
  184. }
  185. /**
  186. * 处理单个设备产量计酬项
  187. */
  188. protected function processMachineItem(array $item, array $holidayData): array
  189. {
  190. $item['班组车头产量'] = $item['班组车头产量'] - $item['sczl_zcfp'];
  191. // 计算工序难度系数
  192. $gxRate = $this->calculateProcessDifficulty($item);
  193. // 调整产量(根据机台类型)
  194. $adjustedOutput = $this->adjustOutputByMachine($item, $gxRate);
  195. // 计算计件产量
  196. $byThePieceYield = $this->calculatePieceYield($item, $adjustedOutput, $gxRate);
  197. // 补产产量
  198. $afterProductionYield = $this->calculateSupplementYield($item);
  199. $data = [];
  200. for ($i = 1; $i < 11; $i++) {
  201. $bhKey = 'sczl_bh' . $i;
  202. $xmKey = 'name' . $i;
  203. $rateKey = 'sczl_rate' . $i;
  204. if (!empty($item[$bhKey]) && $item[$bhKey] != '0000') {
  205. $dataItem = $this->buildDataItem($item, [
  206. 'bh' => $item[$bhKey],
  207. 'xm' => $item[$xmKey],
  208. 'Rate' => $item[$rateKey],
  209. '工序难度系数' => $gxRate,
  210. '班组车头产量' => $adjustedOutput,
  211. '班组换算产量' => $afterProductionYield,
  212. '计件产量' => $byThePieceYield
  213. ]);
  214. $data[] = $dataItem;
  215. }
  216. }
  217. return $data;
  218. }
  219. /**
  220. * 计算工序难度系数
  221. */
  222. protected function calculateProcessDifficulty(array $item): float
  223. {
  224. $machineType = substr($item['sczl_jtbh'], 0, 2);
  225. if ($machineType == 'JP') { // 检品机
  226. return $item['sczl_废品率系数'] == 0 ? 1.0 : (float)$item['sczl_废品率系数'];
  227. } elseif ($machineType == 'WY' || $machineType == 'DW' ||
  228. substr($item['sczl_jtbh'], 0, 3) == 'YWY' ||
  229. substr($item['sczl_jtbh'], 0, 3) == 'YDW') { // 凹印机
  230. if ($item['sczl_工价系数'] == '0.000' || floatval($item['sczl_工价系数']) <= 0) {
  231. $gxRate = $item['工序难度系数'];
  232. return (floatval($item['工序难度系数']) <= 0 || empty($item['工序难度系数'])) ? 1.0 : (float)$item['工序难度系数'];
  233. } else {
  234. if (floatval($item['工序难度系数']) > 0) {
  235. return (float)number_format($item['sczl_工价系数'] * $item['工序难度系数'], 3);
  236. } else {
  237. return (float)$item['sczl_工价系数'];
  238. }
  239. }
  240. } else { // 其他机台
  241. $num = 1;
  242. if ($item['sczl_jtbh'] === 'YSY02#' || $item['sczl_jtbh'] === 'YSY08#' ||
  243. $item['sczl_jtbh'] === 'YSY10#' || $item['sczl_jtbh'] === 'SY03#') {
  244. $num = 1.1;
  245. }
  246. if ($item['sczl_工价系数'] == '0.000' || floatval($item['sczl_工价系数']) <= 0) {
  247. $gxRate = $item['工序难度系数'];
  248. $baseRate = (floatval($item['工序难度系数']) <= 0 || empty($item['工序难度系数'])) ? 1.0 : (float)$item['工序难度系数'];
  249. return $baseRate * $num;
  250. } else {
  251. if (floatval($item['工序难度系数']) > 0) {
  252. return (float)number_format($item['sczl_工价系数'] * $item['工序难度系数'] * $num, 3);
  253. } else {
  254. return (float)$item['sczl_工价系数'] * $num;
  255. }
  256. }
  257. }
  258. }
  259. /**
  260. * 根据机台类型调整产量
  261. */
  262. protected function adjustOutputByMachine(array $item, float $gxRate): float
  263. {
  264. $output = $item['班组车头产量'];
  265. $machineType = substr($item['sczl_jtbh'], 0, 2);
  266. if ($machineType == 'JP') { // 检品机
  267. $output = $output * $item['sczl_Pgcl'];
  268. } elseif (($machineType == 'WY' || $machineType == 'DW' ||
  269. substr($item['sczl_jtbh'], 0, 3) == 'YWY' ||
  270. substr($item['sczl_jtbh'], 0, 3) == 'YDW') &&
  271. str_contains($item['印刷方式'], '张') &&
  272. $item['版距'] > 0) { // 凹印机且是张式印刷
  273. $output = $output * ($item['版距'] / 1000);
  274. }
  275. return (float)$output;
  276. }
  277. /**
  278. * 计算计件产量
  279. */
  280. protected function calculatePieceYield(array $item, float $output, float $gxRate): float
  281. {
  282. $machineType = substr($item['sczl_jtbh'], 0, 2);
  283. if ($machineType == 'JP') {
  284. return round($output * $item['sczl_废品率系数']);
  285. } elseif ($machineType == 'WY' || $machineType == 'DW' ||
  286. substr($item['sczl_jtbh'], 0, 3) == 'YWY' ||
  287. substr($item['sczl_jtbh'], 0, 3) == 'YDW') {
  288. return str_replace(',', '', round($output * $gxRate));
  289. } else {
  290. return round($output * $gxRate);
  291. }
  292. }
  293. /**
  294. * 计算补产产量
  295. */
  296. protected function calculateSupplementYield(array $item): float
  297. {
  298. $totalHours = (float)($item['装版工时'] + $item['保养工时'] + $item['打样工时']);
  299. $supplementStandard = (float)$item['补产标准'];
  300. return $totalHours * $supplementStandard;
  301. }
  302. /**
  303. * 构建数据项
  304. */
  305. protected function buildDataItem(array $baseData, array $additionalData): array
  306. {
  307. $now = date('Y-m-d H:i:s');
  308. return array_merge([
  309. 'sczl_gdbh' => $baseData['sczl_gdbh'],
  310. 'sczl_yjno' => $baseData['sczl_yjno'],
  311. 'sczl_gxh' => $baseData['sczl_gxh'],
  312. 'sczl_type' => $baseData['sczl_type'],
  313. 'sczl_rq' => $baseData['sczl_rq'],
  314. 'sczl_jtbh' => $baseData['sczl_jtbh'],
  315. '班组车头产量' => 0,
  316. '工价系数' => '0.0000',
  317. '工序难度系数' => 1.0,
  318. '装版工时' => $baseData['装版工时'],
  319. '保养工时' => $baseData['保养工时'],
  320. '打样工时' => $baseData['打样工时'],
  321. '异常停机工时' => $baseData['异常停机工时'],
  322. '车头产量占用机时' => $baseData['车头产量占用机时'],
  323. '日定额' => (int)$baseData['日定额'],
  324. '千件工价' => $baseData['千件工价'],
  325. '补产标准' => $baseData['补产标准'],
  326. '班组换算产量' => 0,
  327. '计时补差额工资' => '0.00',
  328. 'bh' => '',
  329. 'xm' => '',
  330. 'Rate' => '1.0000',
  331. 'sczl_ms' => $baseData['sczl_ms'],
  332. '工时占比' => 0,
  333. '达标定额' => 0,
  334. '个人计件工资' => 0,
  335. '个人加班工资' => 0,
  336. 'UniqID' => 0,
  337. 'sys_ny' => '',
  338. 'sys_rq' => $now,
  339. 'sys_id' => '',
  340. '法定天数' => 0,
  341. '核算产量' => 0,
  342. ], $additionalData);
  343. }
  344. /**
  345. * 处理拆片工序数据(简略实现)
  346. */
  347. protected function processSplitData(array $params, string $startDate, string $endDate, array $holidayData): array
  348. {
  349. // 简略实现,您可以根据原有逻辑完善
  350. return [];
  351. }
  352. /**
  353. * 处理手工检验数据(简略实现)
  354. */
  355. protected function processManualData(array $params, string $startDate, string $endDate): array
  356. {
  357. // 简略实现,您可以根据原有逻辑完善
  358. return [];
  359. }
  360. /**
  361. * 处理包装计件数据(简略实现)
  362. */
  363. protected function processPackagingData(array $params, string $startDate, string $endDate): array
  364. {
  365. // 简略实现,您可以根据原有逻辑完善
  366. return [];
  367. }
  368. /**
  369. * 计算工时占比和工资
  370. */
  371. protected function calculateWorkHoursAndWages(array $data, int $legalDays, array $holidayData): array
  372. {
  373. // 1. 按人员和日期分组计算工时占比总和
  374. $bhTotals = [];
  375. foreach ($data as $row) {
  376. $dateKey = substr($row['sczl_rq'], 0, 10);
  377. $bhKey = $dateKey . '-' . $row['bh'];
  378. if (!isset($bhTotals[$bhKey])) {
  379. $bhTotals[$bhKey] = 0;
  380. }
  381. // 计算核算产量
  382. $accountingYield = $row['计件产量'] + $row['班组换算产量'];
  383. $row['核算产量'] = $accountingYield;
  384. // 计算工时占比
  385. if ($row['日定额'] > 0) {
  386. $row['工时占比'] = (float)number_format($accountingYield / $row['日定额'], 4);
  387. } else {
  388. $row['工时占比'] = 0;
  389. }
  390. $bhTotals[$bhKey] += $row['工时占比'];
  391. $data[$row['_index']]['工时占比'] = $row['工时占比'];
  392. $data[$row['_index']]['核算产量'] = $accountingYield;
  393. }
  394. // 2. 计算工资
  395. $result = [];
  396. foreach ($data as $row) {
  397. $dateKey = substr($row['sczl_rq'], 0, 10);
  398. $bhKey = $dateKey . '-' . $row['bh'];
  399. // 计算达标定额
  400. if ($row['工时占比'] > 0 && isset($bhTotals[$bhKey]) && $bhTotals[$bhKey] > 0) {
  401. $standardQuota = (float)number_format($row['工时占比'] / $bhTotals[$bhKey] * $row['日定额'], 2);
  402. } else {
  403. $standardQuota = (float)$row['班组车头产量'];
  404. }
  405. $row['达标定额'] = $standardQuota;
  406. // 判断是否是节假日
  407. $isHoliday = in_array($dateKey, $holidayData['vacation_one']) ||
  408. in_array($dateKey, $holidayData['vacation_two']);
  409. // 计算工资
  410. if ($isHoliday) {
  411. $row['个人计件工资'] = 0;
  412. $row['个人加班工资'] = $row['核算产量'] / 1000 * $row['千件工价'] * $row['Rate'] * 3;
  413. } else {
  414. // 这里需要根据原有的逻辑计算(您可以根据需要完善)
  415. $row['个人计件工资'] = $standardQuota / 1000 * $row['千件工价'] * $row['Rate'];
  416. $row['个人加班工资'] = ($row['核算产量'] - $standardQuota) / 1000 * $row['千件工价'] * $row['Rate'] * 1.5;
  417. }
  418. $result[] = $row;
  419. }
  420. return $result;
  421. }
  422. /**
  423. * 保存工资数据
  424. */
  425. protected function saveSalaryData(array $data): int
  426. {
  427. if (empty($data)) {
  428. return 0;
  429. }
  430. // 获取最大UniqID
  431. $maxUniqId = Db::name('绩效工资汇总')
  432. ->max('UniqID') ?? 0;
  433. // 准备插入数据
  434. $insertData = [];
  435. $currentUniqId = $maxUniqId + 1;
  436. foreach ($data as $item) {
  437. $insertData[] = [
  438. 'sczl_gdbh' => $item['sczl_gdbh'],
  439. 'sczl_yjno' => $item['sczl_yjno'],
  440. 'sczl_gxh' => $item['sczl_gxh'],
  441. 'sczl_type' => $item['sczl_type'],
  442. 'sczl_rq' => $item['sczl_rq'],
  443. 'sczl_jtbh' => $item['sczl_jtbh'],
  444. '班组车头产量' => $item['班组车头产量'],
  445. '工价系数' => $item['工价系数'],
  446. '工序难度系数' => $item['工序难度系数'],
  447. '装版工时' => $item['装版工时'],
  448. '保养工时' => $item['保养工时'],
  449. '打样工时' => $item['打样工时'],
  450. '异常停机工时' => $item['异常停机工时'],
  451. '车头产量占用机时' => $item['车头产量占用机时'],
  452. '日定额' => $item['日定额'],
  453. '千件工价' => $item['千件工价'],
  454. '补产标准' => $item['补产标准'],
  455. '班组换算产量' => $item['班组换算产量'],
  456. '计时补差额工资' => $item['计时补差额工资'],
  457. 'bh' => $item['bh'],
  458. 'xm' => $item['xm'],
  459. 'Rate' => $item['Rate'],
  460. 'sczl_ms' => $item['sczl_ms'],
  461. '工时占比' => $item['工时占比'],
  462. '达标定额' => $item['达标定额'],
  463. '个人计件工资' => $item['个人计件工资'],
  464. '个人加班工资' => $item['个人加班工资'],
  465. 'UniqID' => $currentUniqId++,
  466. 'sys_ny' => $item['sys_ny'],
  467. 'sys_rq' => $item['sys_rq'],
  468. 'sys_id' => $item['sys_id'],
  469. '法定天数' => $item['法定天数'],
  470. '核算产量' => $item['核算产量'] ?? 0,
  471. ];
  472. }
  473. // 批量插入
  474. $sql = Db::name('绩效工资汇总')->fetchSql(true)->insertAll($insertData);
  475. $result = Db::query($sql);
  476. return $result ? count($insertData) : 0;
  477. }
  478. }