GluSalaryCalculationService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <?php
  2. namespace app\service;
  3. /**
  4. * 糊盒工资计算服务
  5. */
  6. class GluSalaryCalculationService
  7. {
  8. /**
  9. * 执行糊盒工资计算
  10. * @param array $params
  11. * @return array
  12. */
  13. public function execute(array $params)
  14. {
  15. try {
  16. if (empty($params['month']) || empty($params['startDay']) || empty($params['endDay'])) {
  17. return ['success' => false, 'message' => '参数错误'];
  18. }
  19. $where = ['a.sczl_rq' => ['between', [$params['startDay'] . ' 00:00:00', $params['endDay'] . ' 23:59:59']]];
  20. $filds = "a.sczl_gdbh as 工单编号,a.sczl_gxmc as 工序名称,a.sczl_cl as 产量,a.sczl_rq as 日期,
  21. a.sczl_dedh as 定额代号,d.日定额 as 日定额,a.工价系数,a.保养工时,a.装版工时,a.异常工时,a.设备运行工时,a.role,a.sczl_jtbh,b.Gd_cpdh,b.Gd_cpmc,a.price";
  22. $list = db('设备_糊盒报工资料')
  23. ->alias('a')
  24. ->join('工单_基本资料 b', 'a.sczl_gdbh = b.Gd_gdbh ', 'left')
  25. ->join('dic_lzde d', 'a.sczl_dedh = d.sys_bh', 'left')
  26. ->field($filds)
  27. ->where($where)
  28. ->group('a.Uid')
  29. ->select();
  30. $pieceData = [];
  31. foreach ($list as $v) {
  32. $result = $this->salaryCalculationClass($v['role']);
  33. if ($this->isManualMachine($v['sczl_jtbh'])) {
  34. $wageList = $this->calculateManualPieceWages($v, $result);
  35. } else {
  36. $wageList = $this->calculateRegularPieceWages($v, $result);
  37. }
  38. foreach ($wageList as $wageItem) {
  39. $pieceData[] = $this->buildSalarySummaryRow([
  40. 'sczl_gdbh' => $v['工单编号'],
  41. 'sczl_gxmc' => $v['工序名称'],
  42. 'sczl_rq' => $v['日期'],
  43. 'sczl_cl' => $v['产量'],
  44. 'sczl_dedh' => $v['定额代号'],
  45. '工价系数' => $v['工价系数'],
  46. '保养工时' => $v['保养工时'],
  47. '装版工时' => $v['装版工时'],
  48. '异常工时' => $v['异常工时'],
  49. '设备运行工时' => $v['设备运行工时'],
  50. 'sczl_jtbh' => $v['sczl_jtbh'],
  51. 'cpdh' => $v['Gd_cpdh'],
  52. 'cpmc' => $v['Gd_cpmc'],
  53. 'bh' => $wageItem['bh'],
  54. 'rate' => $wageItem['rate'],
  55. 'name' => $wageItem['name'],
  56. 'salary' => $wageItem['wage'],
  57. 'price' => $v['price'],
  58. 'sys_id' => $params['sys_id'] ?? '',
  59. ]);
  60. }
  61. }
  62. $teamres = $this->teamTimekeepCalculation($params['month']);
  63. $timekeepres = $this->calculateTimekeepSalary($params['month']);
  64. $timekeepData = $this->mergeTimekeepSalaryData($teamres, $timekeepres);
  65. $timekeepInsert = [];
  66. foreach ($timekeepData as $item) {
  67. $remark = $item['remark'] ?? '';
  68. if (!empty($item['type'])) {
  69. $remark = $remark !== '' ? $item['type'] . ',' . $remark : $item['type'];
  70. }
  71. $timekeepInsert[] = $this->buildSalarySummaryRow([
  72. 'sczl_rq' => $item['sczl_rq'] . ' 00:00:00',
  73. 'bh' => $item['bh'],
  74. 'name' => $item['姓名'],
  75. 'time_salary' => $item['salary'],
  76. 'time_price' => $item['price'],
  77. 'time_duration' => $item['duration'],
  78. 'remark' => $remark,
  79. 'sys_id' => $params['sys_id'] ?? '',
  80. ]);
  81. }
  82. $insertData = array_merge($pieceData, $timekeepInsert);
  83. if (empty($insertData)) {
  84. return ['success' => false, 'message' => '无可保存的工资数据'];
  85. }
  86. db('糊盒工资汇总')->where('sczl_rq', 'like', $params['month'] . '%')->delete();
  87. $sql = db('糊盒工资汇总')->fetchSql(true)->insertAll($insertData);
  88. $res = db()->query($sql);
  89. if ($res === false) {
  90. return ['success' => false, 'message' => '工资数据保存失败'];
  91. }
  92. return [
  93. 'success' => true,
  94. 'message' => '成功',
  95. 'piece_count' => count($pieceData),
  96. 'timekeep_count' => count($timekeepInsert),
  97. 'total_count' => count($insertData),
  98. ];
  99. } catch (\Exception $e) {
  100. return ['success' => false, 'message' => '糊盒工资计算失败: ' . $e->getMessage()];
  101. }
  102. }
  103. protected function salaryCalculationClass($role)
  104. {
  105. // 构建查询对象
  106. $query = db('糊盒报工班组')->alias('a')->where('a.id', $role);
  107. // 构建字段列表
  108. $fieldParts = [];
  109. // 添加 bh 和 rate 字段
  110. for ($i = 1; $i <= 30; $i++) {
  111. $fieldParts[] = "a.bh{$i}";
  112. $fieldParts[] = "a.rate{$i}";
  113. }
  114. // 添加 JOIN 和员工姓名字段
  115. for ($i = 1; $i <= 30; $i++) {
  116. $table = 'b' . $i;
  117. $fieldParts[] = "{$table}.员工姓名 as 员工姓名{$i}";
  118. $query->join("人事_基本资料 {$table}", "a.bh{$i} = {$table}.员工编号", 'left');
  119. }
  120. // 设置字段
  121. $query->field(implode(',', $fieldParts));
  122. return $query->find();
  123. }
  124. /**
  125. * 判断是否手工机台
  126. * @param string $jtbh
  127. * @return bool
  128. */
  129. protected function isManualMachine($jtbh)
  130. {
  131. return stripos((string)$jtbh, 'SG') !== false;
  132. }
  133. /**
  134. * 普通机台计件工资
  135. * @param array $report
  136. * @param array $classResult
  137. * @return array
  138. */
  139. protected function calculateRegularPieceWages(array $report, array $classResult)
  140. {
  141. $wageList = [];
  142. $salary = (float)$report['产量'] * (float)$report['price'];
  143. for ($i = 1; $i <= 30; $i++) {
  144. $bh = trim($classResult['bh' . $i] ?? '');
  145. if ($bh === '' || $bh === '0000') {
  146. continue;
  147. }
  148. $wageList[] = [
  149. 'bh' => $bh,
  150. 'name' => $classResult['员工姓名' . $i] ?? '',
  151. 'rate' => $classResult['rate' . $i] ?? '',
  152. 'wage' => floatval(number_format($salary * (float)$classResult['rate' . $i], 2, '.', '')),
  153. ];
  154. }
  155. return $wageList;
  156. }
  157. /**
  158. * 手工机台计件超产工资
  159. * @param array $report
  160. * @param array $classResult
  161. * @return array
  162. */
  163. protected function calculateManualPieceWages(array $report, array $classResult)
  164. {
  165. $employees = [];
  166. for ($i = 1; $i <= 30; $i++) {
  167. $bh = trim($classResult['bh' . $i] ?? '');
  168. if ($bh === '' || $bh === '0000') {
  169. continue;
  170. }
  171. $employees[] = [
  172. 'bh' => $bh,
  173. 'name' => $classResult['员工姓名' . $i] ?? '',
  174. 'rate' => $classResult['rate' . $i] ?? '',
  175. ];
  176. }
  177. if (empty($employees)) {
  178. return [];
  179. }
  180. $hireDates = db('人事_基本资料')
  181. ->whereIn('员工编号', array_column($employees, 'bh'))
  182. ->column('聘用日期', '员工编号');
  183. $totalOutput = (float)$report['产量'];
  184. $dailyQuota = (float)$report['日定额'];
  185. if ($dailyQuota <= 0) {
  186. $dailyQuota = (float)$report['定额代号'];
  187. }
  188. $unitPrice = (float)$report['price'];
  189. $reportDate = $report['日期'];
  190. $totalCount = count($employees);
  191. $countGt3 = 0;
  192. $count8to15 = 0;
  193. $countGt15 = 0;
  194. $allPiece = true;
  195. foreach ($employees as &$employee) {
  196. $workDays = $this->getEmployeeTenureDays($employee['bh'], $reportDate, $hireDates);
  197. $employee['work_days'] = $workDays;
  198. if ($workDays > 3) {
  199. $countGt3++;
  200. }
  201. if ($workDays >= 8 && $workDays <= 15) {
  202. $count8to15++;
  203. }
  204. if ($workDays > 15) {
  205. $countGt15++;
  206. }
  207. if ($workDays <= 7) {
  208. $allPiece = false;
  209. }
  210. }
  211. unset($employee);
  212. $wageList = [];
  213. if ($allPiece) {
  214. $overPerPerson = $totalCount > 0
  215. ? max(0, ($totalOutput - $dailyQuota * $totalCount) * $unitPrice / $totalCount)
  216. : 0;
  217. foreach ($employees as $employee) {
  218. $wageList[] = [
  219. 'bh' => $employee['bh'],
  220. 'name' => $employee['name'],
  221. 'rate' => $employee['rate'],
  222. 'wage' => floatval(number_format($overPerPerson, 2, '.', '')),
  223. ];
  224. }
  225. return $wageList;
  226. }
  227. $value1 = $countGt3 > 0
  228. ? max(0, ($totalOutput - $dailyQuota * $countGt3) * $unitPrice)
  229. : 0;
  230. $halfShare = $countGt3 > 0 ? $value1 / $countGt3 / 2 : 0;
  231. $value2 = max(0, $value1 - $halfShare * $count8to15);
  232. $gt15Share = $countGt15 > 0 ? $value2 / $countGt15 : 0;
  233. foreach ($employees as $employee) {
  234. $workDays = $employee['work_days'];
  235. if ($workDays <= 7) {
  236. $wage = 0;
  237. } elseif ($workDays >= 8 && $workDays <= 15) {
  238. $wage = $halfShare;
  239. } elseif ($workDays > 15) {
  240. $wage = $gt15Share;
  241. } else {
  242. $wage = 0;
  243. }
  244. $wageList[] = [
  245. 'bh' => $employee['bh'],
  246. 'name' => $employee['name'],
  247. 'rate' => $employee['rate'],
  248. 'wage' => floatval(number_format($wage, 2, '.', '')),
  249. ];
  250. }
  251. return $wageList;
  252. }
  253. /**
  254. * 计算员工上班天数
  255. * @param string $bh
  256. * @param string $reportDate
  257. * @param array $hireDates
  258. * @return int
  259. */
  260. protected function getEmployeeTenureDays($bh, $reportDate, array $hireDates)
  261. {
  262. if (empty($hireDates[$bh])) {
  263. return 0;
  264. }
  265. $hireTime = strtotime(date('Y-m-d', strtotime($hireDates[$bh])));
  266. $reportTime = strtotime(date('Y-m-d', strtotime($reportDate)));
  267. if ($hireTime === false || $reportTime === false || $reportTime < $hireTime) {
  268. return 0;
  269. }
  270. return (int)floor(($reportTime - $hireTime) / 86400) + 1;
  271. }
  272. /**
  273. * 班组计时工资计算
  274. * @param string $month 月份
  275. * @return array 班组计时工资数据
  276. */
  277. protected function teamTimekeepCalculation($month)
  278. {
  279. $bhFields = [];
  280. for ($i = 1; $i <= 30; $i++) {
  281. $bhFields[] = 'bh' . $i;
  282. }
  283. $list = db('糊盒班组计时')
  284. ->field(array_merge(['sczl_rq', 'type', 'duration', 'price', 'remark'], $bhFields))
  285. ->where('sczl_rq', 'like', $month . '%')
  286. ->select();
  287. if (empty($list)) {
  288. return [];
  289. }
  290. $employeeIds = [];
  291. foreach ($list as $row) {
  292. for ($i = 1; $i <= 30; $i++) {
  293. $bh = isset($row['bh' . $i]) ? trim($row['bh' . $i]) : '';
  294. if ($bh !== '' && $bh !== '0000') {
  295. $employeeIds[$bh] = true;
  296. }
  297. }
  298. }
  299. $employeeNames = [];
  300. if (!empty($employeeIds)) {
  301. $employees = db('人事_基本资料')
  302. ->whereIn('员工编号', array_keys($employeeIds))
  303. ->field('员工编号, rtrim(员工姓名) as 姓名')
  304. ->select();
  305. foreach ($employees as $employee) {
  306. $employeeNames[$employee['员工编号']] = $employee['姓名'];
  307. }
  308. }
  309. $data = [];
  310. foreach ($list as $row) {
  311. $employees = [];
  312. for ($i = 1; $i <= 30; $i++) {
  313. $bh = isset($row['bh' . $i]) ? trim($row['bh' . $i]) : '';
  314. if ($bh !== '' && $bh !== '0000') {
  315. $employees[] = $bh;
  316. }
  317. }
  318. if (empty($employees)) {
  319. continue;
  320. }
  321. $employeeCount = count($employees);
  322. $salaryPerEmployee = ((float)$row['duration'] * (float)$row['price']) / $employeeCount;
  323. $ymd = date('Ymd', strtotime($row['sczl_rq']));
  324. foreach ($employees as $bh) {
  325. $name = $employeeNames[$bh] ?? '';
  326. $key = $ymd . '-' . $bh . '-' . $name . '-' . $row['price'];
  327. $durationPerEmployee = (float)$row['duration'] / $employeeCount;
  328. if (isset($data[$key])) {
  329. $data[$key]['duration'] = floatval(number_format((float)$data[$key]['duration'] + $durationPerEmployee, 2, '.', ''));
  330. $data[$key]['salary'] = floatval(number_format($data[$key]['salary'] + $salaryPerEmployee, 2, '.', ''));
  331. if ($row['type'] !== '' && strpos($data[$key]['type'], $row['type']) === false) {
  332. $data[$key]['type'] = trim($data[$key]['type'] . ',' . $row['type'], ',');
  333. }
  334. if ($row['remark'] !== '' && strpos($data[$key]['remark'], $row['remark']) === false) {
  335. $data[$key]['remark'] = trim($data[$key]['remark'] . ',' . $row['remark'], ',');
  336. }
  337. continue;
  338. }
  339. $data[$key] = [
  340. 'sczl_rq' => date('Y-m-d', strtotime($row['sczl_rq'])),
  341. 'type' => $row['type'],
  342. 'duration' => floatval(number_format($durationPerEmployee, 2, '.', '')),
  343. 'price' => $row['price'],
  344. 'remark' => $row['remark'],
  345. 'bh' => $bh,
  346. '姓名' => $name,
  347. 'salary' => floatval(number_format($salaryPerEmployee, 2, '.', '')),
  348. ];
  349. }
  350. }
  351. return $data;
  352. }
  353. /**
  354. * 计算糊盒计时工资
  355. * @param string $month 月份
  356. * @return array 糊盒计时工资数据
  357. */
  358. protected function calculateTimekeepSalary($month)
  359. {
  360. $list = db('糊盒报工机时')
  361. ->field(['wgjs_rq', 'wgjs_bh1', 'wgjs_js1', 'wgjs_yy1', 'wgjs_je1',
  362. 'wgjs_bh2', 'wgjs_js2', 'wgjs_yy2', 'wgjs_je2',
  363. 'wgjs_bh3', 'wgjs_js3', 'wgjs_yy3', 'wgjs_je3',
  364. 'wgjs_bh4', 'wgjs_js4', 'wgjs_yy4', 'wgjs_je4',
  365. 'wgjs_bh5', 'wgjs_js5', 'wgjs_yy5', 'wgjs_je5',
  366. 'wgjs_bh6', 'wgjs_js6', 'wgjs_yy6', 'wgjs_je6'])
  367. ->where('wgjs_rq', 'like', $month . '%')
  368. ->select();
  369. if (empty($list)) {
  370. return [];
  371. }
  372. $employeeIds = [];
  373. foreach ($list as $row) {
  374. for ($i = 1; $i <= 6; $i++) {
  375. $bh = isset($row['wgjs_bh' . $i]) ? trim($row['wgjs_bh' . $i]) : '';
  376. if ($bh !== '') {
  377. $employeeIds[$bh] = true;
  378. }
  379. }
  380. }
  381. $employeeNames = [];
  382. if (!empty($employeeIds)) {
  383. $employees = db('人事_基本资料')
  384. ->whereIn('员工编号', array_keys($employeeIds))
  385. ->field('员工编号, rtrim(员工姓名) as 姓名')
  386. ->select();
  387. foreach ($employees as $employee) {
  388. $employeeNames[$employee['员工编号']] = $employee['姓名'];
  389. }
  390. }
  391. $data = [];
  392. foreach ($list as $row) {
  393. $ymd = date('Ymd', strtotime($row['wgjs_rq']));
  394. $sczl_rq = date('Y-m-d', strtotime($row['wgjs_rq']));
  395. for ($i = 1; $i <= 6; $i++) {
  396. $bh = isset($row['wgjs_bh' . $i]) ? trim($row['wgjs_bh' . $i]) : '';
  397. if ($bh === '') {
  398. continue;
  399. }
  400. $duration = isset($row['wgjs_js' . $i]) ? (float)$row['wgjs_js' . $i] : 0;
  401. $type = isset($row['wgjs_yy' . $i]) ? trim($row['wgjs_yy' . $i]) : '';
  402. $salary = isset($row['wgjs_je' . $i]) ? (float)$row['wgjs_je' . $i] : 0;
  403. $price = '';
  404. $name = $employeeNames[$bh] ?? '';
  405. $key = $ymd . '-' . $bh . '-' . $name . '-' . $price;
  406. if (isset($data[$key])) {
  407. $data[$key]['duration'] = floatval(number_format((float)$data[$key]['duration'] + $duration, 2, '.', ''));
  408. $data[$key]['salary'] = floatval(number_format((float)$data[$key]['salary'] + $salary, 2, '.', ''));
  409. if ($type !== '' && strpos($data[$key]['type'], $type) === false) {
  410. $data[$key]['type'] = trim($data[$key]['type'] . ',' . $type, ',');
  411. }
  412. continue;
  413. }
  414. $data[$key] = [
  415. 'sczl_rq' => $sczl_rq,
  416. 'type' => $type,
  417. 'duration' => floatval(number_format($duration, 2, '.', '')),
  418. 'price' => '',
  419. 'remark' => '',
  420. 'bh' => $bh,
  421. '姓名' => $name,
  422. 'salary' => floatval(number_format($salary, 2, '.', '')),
  423. ];
  424. }
  425. }
  426. return $data;
  427. }
  428. /**
  429. * 合并计时工资数据
  430. * @param array $data1
  431. * @param array $data2
  432. * @return array
  433. */
  434. protected function mergeTimekeepSalaryData(array $data1, array $data2)
  435. {
  436. $data = $data1;
  437. foreach ($data2 as $key => $item) {
  438. if (!isset($data[$key])) {
  439. $data[$key] = $item;
  440. continue;
  441. }
  442. $data[$key]['duration'] = floatval(number_format((float)$data[$key]['duration'] + (float)$item['duration'], 2, '.', ''));
  443. $data[$key]['salary'] = floatval(number_format((float)$data[$key]['salary'] + (float)$item['salary'], 2, '.', ''));
  444. if (!empty($item['type']) && strpos($data[$key]['type'], $item['type']) === false) {
  445. $data[$key]['type'] = trim($data[$key]['type'] . ',' . $item['type'], ',');
  446. }
  447. if (!empty($item['remark']) && strpos($data[$key]['remark'], $item['remark']) === false) {
  448. $data[$key]['remark'] = trim($data[$key]['remark'] . ',' . $item['remark'], ',');
  449. }
  450. }
  451. return $data;
  452. }
  453. /**
  454. * 构建工资汇总统一字段结构
  455. * @param array $fields
  456. * @return array
  457. */
  458. protected function buildSalarySummaryRow(array $fields = [])
  459. {
  460. $defaults = [
  461. 'sczl_gdbh' => '',
  462. 'sczl_gxmc' => '',
  463. 'sczl_rq' => '',
  464. 'sczl_dedh' => '',
  465. 'sczl_cl' => 0,
  466. '工价系数' => '',
  467. '保养工时' => '',
  468. '装版工时' => '',
  469. '异常工时' => '',
  470. '设备运行工时' => '',
  471. 'sczl_jtbh' => '',
  472. 'cpdh' => '',
  473. 'cpmc' => '',
  474. 'bh' => '',
  475. 'rate' => '',
  476. 'name' => '',
  477. 'salary' => 0,
  478. 'time_salary' => 0,
  479. 'price' => 0,
  480. 'time_price' => 0,
  481. 'time_duration' => 0,
  482. 'remark' => '',
  483. 'sys_id' => '',
  484. 'sys_rq' => date('Y-m-d H:i:s'),
  485. ];
  486. $row = array_merge($defaults, $fields);
  487. $decimalFields = ['sczl_cl', 'salary', 'time_salary', 'price', 'time_price', 'time_duration'];
  488. foreach ($decimalFields as $field) {
  489. if (!isset($row[$field]) || $row[$field] === '' || $row[$field] === null) {
  490. $row[$field] = 0;
  491. } else {
  492. $row[$field] = is_numeric($row[$field]) ? (float)$row[$field] : 0;
  493. }
  494. }
  495. return $row;
  496. }
  497. }