| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- <?php
- namespace app\service;
- use think\Db;
- use think\Exception;
- use think\Log;
- /**
- * 工资计算服务类
- */
- class SalaryCalculationService
- {
- /**
- * 执行工资计算主方法
- * @param array $params 计算参数
- * @return array 计算结果
- */
- public function calculateSalary(array $params): array
- {
- try {
- Log::info('开始执行工资计算', $params);
- // 参数验证
- $this->validateParams($params);
- $startDate = $params['start_date'];
- $endDate = $params['end_date'];
- $attendanceMonth = $params['date'];
- $sysId = $params['sys_id'] ?? '';
- // 1. 获取法定天数
- $legalDays = $this->getLegalDays($attendanceMonth);
- $params['days'] = $legalDays;
- // 2. 获取节假日数据
- $holidayData = $this->getHolidayData($params);
- // 3. 删除原有数据
- $this->clearOldData($startDate, $endDate);
- // 4. 查询并处理各个数据源
- $allData = [];
- // 4.1 处理设备_产量计酬数据
- $machineData = $this->processMachineData($params, $startDate, $endDate, $holidayData);
- $allData = array_merge($allData, $machineData);
- // 4.2 处理拆片工序数据
- $splitData = $this->processSplitData($params, $startDate, $endDate, $holidayData);
- $allData = array_merge($allData, $splitData);
- // 4.3 处理手工检验数据
- $manualData = $this->processManualData($params, $startDate, $endDate);
- $allData = array_merge($allData, $manualData);
- // 4.4 处理包装计件数据
- $packagingData = $this->processPackagingData($params, $startDate, $endDate);
- $allData = array_merge($allData, $packagingData);
- // 5. 计算工时占比和工资
- $calculatedData = $this->calculateWorkHoursAndWages($allData, $legalDays, $holidayData);
- // 6. 保存数据到数据库
- $insertCount = $this->saveSalaryData($calculatedData);
- Log::info('工资计算完成', [
- 'month' => $attendanceMonth,
- 'total_records' => count($calculatedData),
- 'insert_count' => $insertCount
- ]);
- return [
- 'success' => true,
- 'message' => '工资计算完成',
- 'data' => [
- 'month' => $attendanceMonth,
- 'total_records' => count($calculatedData),
- 'insert_count' => $insertCount,
- 'start_date' => $startDate,
- 'end_date' => $endDate
- ]
- ];
- } catch (Exception $e) {
- Log::error('工资计算失败: ' . $e->getMessage());
- return [
- 'success' => false,
- 'message' => '工资计算失败: ' . $e->getMessage()
- ];
- }
- }
- /**
- * 参数验证
- */
- protected function validateParams(array $params): void
- {
- if (empty($params['date']) || empty($params['start_date']) || empty($params['end_date'])) {
- throw new Exception('参数错误:日期参数不能为空');
- }
- // 确保开始日期和结束日期在考勤年月的范围内
- $startDateInRange = date('Ym', strtotime($params['start_date'])) === $params['date'];
- $endDateInRange = date('Ym', strtotime($params['end_date'])) === $params['date'];
- if (!$startDateInRange || !$endDateInRange) {
- throw new Exception('日期参数错误:开始日期或结束日期不在考勤月份内');
- }
- }
- /**
- * 获取法定天数
- */
- protected function getLegalDays(string $month): int
- {
- $clockingInDay = Db::name('人事_考勤资料')
- ->where('kqzl_ny', $month)
- ->field('法定天数')
- ->order('UniqId desc')
- ->find();
- if (empty($clockingInDay)) {
- throw new Exception('未找到该月的法定天数设置');
- }
- return (int)$clockingInDay['法定天数'];
- }
- /**
- * 获取节假日数据
- */
- protected function getHolidayData(array $params): array
- {
- $holidayData = [
- 'vacation_one' => [],
- 'vacation_two' => []
- ];
- // 处理法定假日1
- if (!empty($params['vacation_one_start']) && !empty($params['vacation_one_end'])) {
- $vacationOneStart = strtotime($params['vacation_one_start']);
- $vacationOneEnd = strtotime($params['vacation_one_end']);
- for ($i = $vacationOneStart; $i <= $vacationOneEnd; $i += 86400) {
- $holidayData['vacation_one'][] = date("Y-m-d", $i);
- }
- }
- // 处理法定假日2
- if (!empty($params['vacation_two_start']) && !empty($params['vacation_two_end'])) {
- $vacationTwoStart = strtotime($params['vacation_two_start']);
- $vacationTwoEnd = strtotime($params['vacation_two_end']);
- for ($i = $vacationTwoStart; $i <= $vacationTwoEnd; $i += 86400) {
- $holidayData['vacation_two'][] = date("Y-m-d", $i);
- }
- }
- return $holidayData;
- }
- /**
- * 清除旧数据
- */
- protected function clearOldData(string $startDate, string $endDate): int
- {
- return Db::name('绩效工资汇总')
- ->where('sczl_rq', 'between', [$startDate, $endDate])
- ->delete();
- }
- /**
- * 处理设备产量计酬数据
- */
- protected function processMachineData(array $params, string $startDate, string $endDate, array $holidayData): array
- {
- $where = ['a.sczl_rq' => ['between', [$startDate, $endDate]]];
- // 查询印刷印后车间的机台
- $sist = ['胶印车间', '凹丝印车间', '印后车间', '检验车间'];
- $jtbhs = Db::name('设备_基本资料')
- ->where('sys_sbID', '<>', '')
- ->where('使用部门', 'in', $sist)
- ->column('设备编号');
- if (empty($jtbhs)) {
- return [];
- }
- $fields = "a.sczl_gdbh,a.sczl_yjno,a.sczl_gxh,a.sczl_type as sczl_type,a.sczl_rq,a.sczl_jtbh,
- a.sczl_工价系数,a.sczl_ms,a.sczl_cl as 班组车头产量,a.sczl_Pgcl,a.sczl_zcfp,
- a.sczl_装版工时 as 装版工时,a.sczl_保养工时 as 保养工时,a.sczl_打样工时 as 打样工时,
- a.sczl_异常工时1 as 异常停机工时,a.sczl_设备运行工时 as 车头产量占用机时,
- 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,
- 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,
- a.sczl_废品率系数,a.UniqId,
- b.千件工价,b.日定额,b.补产标准,c.工价系数 as 工序难度系数,c.版距,c.印刷方式
- ,d1.员工姓名 as name1,d2.员工姓名 as name2,d3.员工姓名 as name3,d4.员工姓名 as name4,d5.员工姓名 as name5,d6.员工姓名 as name6,d7.员工姓名 as name7,d8.员工姓名 as name8
- ,d9.员工姓名 as name9,d10.员工姓名 as name10";
- $query = Db::name('设备_产量计酬')->alias('a')->field($fields);
- $query->join('dic_lzde b', 'a.sczl_dedh = b.sys_bh', 'LEFT');
- $query->join('工单_工艺资料 c', 'a.sczl_gdbh = c.Gy0_gdbh AND a.sczl_yjno = c.Gy0_yjno AND a.sczl_gxh = c.Gy0_gxh', 'LEFT');
- for ($i = 1; $i <= 10; $i++) {
- $field = 'a.sczl_bh' . $i;
- $alias = 'd' . $i;
- $query->join("人事_基本资料 $alias", "$field = {$alias}.员工编号 AND {$field} IS NOT NULL", 'LEFT');
- }
- $list = $query->where($where)
- ->where('a.sczl_jtbh', 'in', $jtbhs)
- ->order('a.sczl_rq')
- ->group('UniqId')
- ->select();
- $data = [];
- foreach ($list as $value) {
- $data = array_merge($data, $this->processMachineItem($value, $holidayData));
- }
- return $data;
- }
- /**
- * 处理单个设备产量计酬项
- */
- protected function processMachineItem(array $item, array $holidayData): array
- {
- $item['班组车头产量'] = $item['班组车头产量'] - $item['sczl_zcfp'];
- // 计算工序难度系数
- $gxRate = $this->calculateProcessDifficulty($item);
- // 调整产量(根据机台类型)
- $adjustedOutput = $this->adjustOutputByMachine($item, $gxRate);
- // 计算计件产量
- $byThePieceYield = $this->calculatePieceYield($item, $adjustedOutput, $gxRate);
- // 补产产量
- $afterProductionYield = $this->calculateSupplementYield($item);
- $data = [];
- for ($i = 1; $i < 11; $i++) {
- $bhKey = 'sczl_bh' . $i;
- $xmKey = 'name' . $i;
- $rateKey = 'sczl_rate' . $i;
- if (!empty($item[$bhKey]) && $item[$bhKey] != '0000') {
- $dataItem = $this->buildDataItem($item, [
- 'bh' => $item[$bhKey],
- 'xm' => $item[$xmKey],
- 'Rate' => $item[$rateKey],
- '工序难度系数' => $gxRate,
- '班组车头产量' => $adjustedOutput,
- '班组换算产量' => $afterProductionYield,
- '计件产量' => $byThePieceYield
- ]);
- $data[] = $dataItem;
- }
- }
- return $data;
- }
- /**
- * 计算工序难度系数
- */
- protected function calculateProcessDifficulty(array $item): float
- {
- $machineType = substr($item['sczl_jtbh'], 0, 2);
- if ($machineType == 'JP') { // 检品机
- return $item['sczl_废品率系数'] == 0 ? 1.0 : (float)$item['sczl_废品率系数'];
- } elseif ($machineType == 'WY' || $machineType == 'DW' ||
- substr($item['sczl_jtbh'], 0, 3) == 'YWY' ||
- substr($item['sczl_jtbh'], 0, 3) == 'YDW') { // 凹印机
- if ($item['sczl_工价系数'] == '0.000' || floatval($item['sczl_工价系数']) <= 0) {
- $gxRate = $item['工序难度系数'];
- return (floatval($item['工序难度系数']) <= 0 || empty($item['工序难度系数'])) ? 1.0 : (float)$item['工序难度系数'];
- } else {
- if (floatval($item['工序难度系数']) > 0) {
- return (float)number_format($item['sczl_工价系数'] * $item['工序难度系数'], 3);
- } else {
- return (float)$item['sczl_工价系数'];
- }
- }
- } else { // 其他机台
- $num = 1;
- if ($item['sczl_jtbh'] === 'YSY02#' || $item['sczl_jtbh'] === 'YSY08#' ||
- $item['sczl_jtbh'] === 'YSY10#' || $item['sczl_jtbh'] === 'SY03#') {
- $num = 1.1;
- }
- if ($item['sczl_工价系数'] == '0.000' || floatval($item['sczl_工价系数']) <= 0) {
- $gxRate = $item['工序难度系数'];
- $baseRate = (floatval($item['工序难度系数']) <= 0 || empty($item['工序难度系数'])) ? 1.0 : (float)$item['工序难度系数'];
- return $baseRate * $num;
- } else {
- if (floatval($item['工序难度系数']) > 0) {
- return (float)number_format($item['sczl_工价系数'] * $item['工序难度系数'] * $num, 3);
- } else {
- return (float)$item['sczl_工价系数'] * $num;
- }
- }
- }
- }
- /**
- * 根据机台类型调整产量
- */
- protected function adjustOutputByMachine(array $item, float $gxRate): float
- {
- $output = $item['班组车头产量'];
- $machineType = substr($item['sczl_jtbh'], 0, 2);
- if ($machineType == 'JP') { // 检品机
- $output = $output * $item['sczl_Pgcl'];
- } elseif (($machineType == 'WY' || $machineType == 'DW' ||
- substr($item['sczl_jtbh'], 0, 3) == 'YWY' ||
- substr($item['sczl_jtbh'], 0, 3) == 'YDW') &&
- str_contains($item['印刷方式'], '张') &&
- $item['版距'] > 0) { // 凹印机且是张式印刷
- $output = $output * ($item['版距'] / 1000);
- }
- return (float)$output;
- }
- /**
- * 计算计件产量
- */
- protected function calculatePieceYield(array $item, float $output, float $gxRate): float
- {
- $machineType = substr($item['sczl_jtbh'], 0, 2);
- if ($machineType == 'JP') {
- return round($output * $item['sczl_废品率系数']);
- } elseif ($machineType == 'WY' || $machineType == 'DW' ||
- substr($item['sczl_jtbh'], 0, 3) == 'YWY' ||
- substr($item['sczl_jtbh'], 0, 3) == 'YDW') {
- return str_replace(',', '', round($output * $gxRate));
- } else {
- return round($output * $gxRate);
- }
- }
- /**
- * 计算补产产量
- */
- protected function calculateSupplementYield(array $item): float
- {
- $totalHours = (float)($item['装版工时'] + $item['保养工时'] + $item['打样工时']);
- $supplementStandard = (float)$item['补产标准'];
- return $totalHours * $supplementStandard;
- }
- /**
- * 构建数据项
- */
- protected function buildDataItem(array $baseData, array $additionalData): array
- {
- $now = date('Y-m-d H:i:s');
- return array_merge([
- 'sczl_gdbh' => $baseData['sczl_gdbh'],
- 'sczl_yjno' => $baseData['sczl_yjno'],
- 'sczl_gxh' => $baseData['sczl_gxh'],
- 'sczl_type' => $baseData['sczl_type'],
- 'sczl_rq' => $baseData['sczl_rq'],
- 'sczl_jtbh' => $baseData['sczl_jtbh'],
- '班组车头产量' => 0,
- '工价系数' => '0.0000',
- '工序难度系数' => 1.0,
- '装版工时' => $baseData['装版工时'],
- '保养工时' => $baseData['保养工时'],
- '打样工时' => $baseData['打样工时'],
- '异常停机工时' => $baseData['异常停机工时'],
- '车头产量占用机时' => $baseData['车头产量占用机时'],
- '日定额' => (int)$baseData['日定额'],
- '千件工价' => $baseData['千件工价'],
- '补产标准' => $baseData['补产标准'],
- '班组换算产量' => 0,
- '计时补差额工资' => '0.00',
- 'bh' => '',
- 'xm' => '',
- 'Rate' => '1.0000',
- 'sczl_ms' => $baseData['sczl_ms'],
- '工时占比' => 0,
- '达标定额' => 0,
- '个人计件工资' => 0,
- '个人加班工资' => 0,
- 'UniqID' => 0,
- 'sys_ny' => '',
- 'sys_rq' => $now,
- 'sys_id' => '',
- '法定天数' => 0,
- '核算产量' => 0,
- ], $additionalData);
- }
- /**
- * 处理拆片工序数据(简略实现)
- */
- protected function processSplitData(array $params, string $startDate, string $endDate, array $holidayData): array
- {
- // 简略实现,您可以根据原有逻辑完善
- return [];
- }
- /**
- * 处理手工检验数据(简略实现)
- */
- protected function processManualData(array $params, string $startDate, string $endDate): array
- {
- // 简略实现,您可以根据原有逻辑完善
- return [];
- }
- /**
- * 处理包装计件数据(简略实现)
- */
- protected function processPackagingData(array $params, string $startDate, string $endDate): array
- {
- // 简略实现,您可以根据原有逻辑完善
- return [];
- }
- /**
- * 计算工时占比和工资
- */
- protected function calculateWorkHoursAndWages(array $data, int $legalDays, array $holidayData): array
- {
- // 1. 按人员和日期分组计算工时占比总和
- $bhTotals = [];
- foreach ($data as $row) {
- $dateKey = substr($row['sczl_rq'], 0, 10);
- $bhKey = $dateKey . '-' . $row['bh'];
- if (!isset($bhTotals[$bhKey])) {
- $bhTotals[$bhKey] = 0;
- }
- // 计算核算产量
- $accountingYield = $row['计件产量'] + $row['班组换算产量'];
- $row['核算产量'] = $accountingYield;
- // 计算工时占比
- if ($row['日定额'] > 0) {
- $row['工时占比'] = (float)number_format($accountingYield / $row['日定额'], 4);
- } else {
- $row['工时占比'] = 0;
- }
- $bhTotals[$bhKey] += $row['工时占比'];
- $data[$row['_index']]['工时占比'] = $row['工时占比'];
- $data[$row['_index']]['核算产量'] = $accountingYield;
- }
- // 2. 计算工资
- $result = [];
- foreach ($data as $row) {
- $dateKey = substr($row['sczl_rq'], 0, 10);
- $bhKey = $dateKey . '-' . $row['bh'];
- // 计算达标定额
- if ($row['工时占比'] > 0 && isset($bhTotals[$bhKey]) && $bhTotals[$bhKey] > 0) {
- $standardQuota = (float)number_format($row['工时占比'] / $bhTotals[$bhKey] * $row['日定额'], 2);
- } else {
- $standardQuota = (float)$row['班组车头产量'];
- }
- $row['达标定额'] = $standardQuota;
- // 判断是否是节假日
- $isHoliday = in_array($dateKey, $holidayData['vacation_one']) ||
- in_array($dateKey, $holidayData['vacation_two']);
- // 计算工资
- if ($isHoliday) {
- $row['个人计件工资'] = 0;
- $row['个人加班工资'] = $row['核算产量'] / 1000 * $row['千件工价'] * $row['Rate'] * 3;
- } else {
- // 这里需要根据原有的逻辑计算(您可以根据需要完善)
- $row['个人计件工资'] = $standardQuota / 1000 * $row['千件工价'] * $row['Rate'];
- $row['个人加班工资'] = ($row['核算产量'] - $standardQuota) / 1000 * $row['千件工价'] * $row['Rate'] * 1.5;
- }
- $result[] = $row;
- }
- return $result;
- }
- /**
- * 保存工资数据
- */
- protected function saveSalaryData(array $data): int
- {
- if (empty($data)) {
- return 0;
- }
- // 获取最大UniqID
- $maxUniqId = Db::name('绩效工资汇总')
- ->max('UniqID') ?? 0;
- // 准备插入数据
- $insertData = [];
- $currentUniqId = $maxUniqId + 1;
- foreach ($data as $item) {
- $insertData[] = [
- 'sczl_gdbh' => $item['sczl_gdbh'],
- 'sczl_yjno' => $item['sczl_yjno'],
- 'sczl_gxh' => $item['sczl_gxh'],
- 'sczl_type' => $item['sczl_type'],
- 'sczl_rq' => $item['sczl_rq'],
- 'sczl_jtbh' => $item['sczl_jtbh'],
- '班组车头产量' => $item['班组车头产量'],
- '工价系数' => $item['工价系数'],
- '工序难度系数' => $item['工序难度系数'],
- '装版工时' => $item['装版工时'],
- '保养工时' => $item['保养工时'],
- '打样工时' => $item['打样工时'],
- '异常停机工时' => $item['异常停机工时'],
- '车头产量占用机时' => $item['车头产量占用机时'],
- '日定额' => $item['日定额'],
- '千件工价' => $item['千件工价'],
- '补产标准' => $item['补产标准'],
- '班组换算产量' => $item['班组换算产量'],
- '计时补差额工资' => $item['计时补差额工资'],
- 'bh' => $item['bh'],
- 'xm' => $item['xm'],
- 'Rate' => $item['Rate'],
- 'sczl_ms' => $item['sczl_ms'],
- '工时占比' => $item['工时占比'],
- '达标定额' => $item['达标定额'],
- '个人计件工资' => $item['个人计件工资'],
- '个人加班工资' => $item['个人加班工资'],
- 'UniqID' => $currentUniqId++,
- 'sys_ny' => $item['sys_ny'],
- 'sys_rq' => $item['sys_rq'],
- 'sys_id' => $item['sys_id'],
- '法定天数' => $item['法定天数'],
- '核算产量' => $item['核算产量'] ?? 0,
- ];
- }
- // 批量插入
- $sql = Db::name('绩效工资汇总')->fetchSql(true)->insertAll($insertData);
- $result = Db::query($sql);
- return $result ? count($insertData) : 0;
- }
- }
|