GluSalaryCalculation.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. /**
  5. * 糊盒工资计算
  6. */
  7. class GluSalaryCalculation extends Api
  8. {
  9. protected $noNeedLogin = ['*'];
  10. protected $noNeedRight = ['*'];
  11. public function salaryCalculation()
  12. {
  13. if(!$this->request->isGet()){
  14. $this->error('请求错误');
  15. }
  16. $params = $this->request->param();
  17. if (!isset($params['month']) || empty($params['month'])) {
  18. $this->error('参数错误');
  19. }
  20. db('糊盒工资汇总')->where('sczl_rq','like',$params['month'].'%')->delete();
  21. $where = ['a.sczl_rq' => ['between', [$params['startDay'].' 00:00:00', $params['endDay'].' 23:59:59']]];
  22. //糊盒计件产量
  23. $filds = "a.sczl_gdbh as 工单编号,a.sczl_gxmc as 工序名称,a.sczl_cl as 产量,a.sczl_rq as 日期,
  24. a.sczl_dedh as 定额代号,a.工价系数,a.保养工时,a.装版工时,a.异常工时,a.设备运行工时,a.role,a.sczl_jtbh,b.Gd_cpdh,b.Gd_cpmc,a.price";
  25. $list = db('设备_糊盒报工资料')
  26. ->alias('a')
  27. ->join('工单_基本资料 b', 'a.sczl_gdbh = b.Gd_gdbh ','left')
  28. ->field($filds)
  29. ->where($where)
  30. ->group('a.Uid')
  31. ->select();
  32. $data = [];
  33. foreach ($list as $k=>$v){
  34. $result = $this->salaryCalculationClass($v['role']);
  35. $salary = $v['产量'] * $v['price'];
  36. for ($i = 1; $i <= 30; $i++) {
  37. $bh = $result['bh'.$i];
  38. $name = $result['员工姓名'.$i];
  39. $rate = $result['rate'.$i];
  40. if (!empty($bh) && $bh !== '') {
  41. $wage = $salary * $rate;
  42. $data[] = [
  43. 'sczl_gdbh' => $v['工单编号'],
  44. 'sczl_gxmc' => $v['工序名称'],
  45. 'sczl_rq' => $v['日期'],
  46. 'sczl_cl' => $v['产量'],
  47. 'sczl_dedh' => $v['定额代号'],
  48. '工价系数' => $v['工价系数'],
  49. '保养工时' => $v['保养工时'],
  50. '装版工时' => $v['装版工时'],
  51. '异常工时' => $v['异常工时'],
  52. '设备运行工时' => $v['设备运行工时'],
  53. 'sczl_jtbh' => $v['sczl_jtbh'],
  54. 'cpdh' => $v['Gd_cpdh'],
  55. 'cpmc' => $v['Gd_cpmc'],
  56. 'bh' => $bh,
  57. 'rate' => $rate,
  58. 'name' => $name,
  59. 'salary' => $wage,
  60. 'price' => $v['price'],
  61. 'sys_id' => $params['sys_id'],
  62. 'sys_rq' => date('Y-m-d H:i:s',time())
  63. ];
  64. }
  65. }
  66. }
  67. $sql = db('糊盒工资汇总')->fetchSql(true)->insertAll($data);
  68. $res = db()->query($sql);
  69. if ($res !== false){
  70. $this->success('成功');
  71. }else{
  72. $this->error('失败');
  73. }
  74. }
  75. protected function salaryCalculationClass($role)
  76. {
  77. // 构建查询对象
  78. $query = db('糊盒报工班组')->alias('a')->where('a.id', $role);
  79. // 构建字段列表
  80. $fieldParts = [];
  81. // 添加 bh 和 rate 字段
  82. for ($i = 1; $i <= 30; $i++) {
  83. $fieldParts[] = "a.bh{$i}";
  84. $fieldParts[] = "a.rate{$i}";
  85. }
  86. // 添加 JOIN 和员工姓名字段
  87. for ($i = 1; $i <= 30; $i++) {
  88. $table = 'b' . $i;
  89. $fieldParts[] = "{$table}.员工姓名 as 员工姓名{$i}";
  90. $query->join("人事_基本资料 {$table}", "a.bh{$i} = {$table}.员工编号", 'left');
  91. }
  92. // 设置字段
  93. $query->field(implode(',', $fieldParts));
  94. return $query->find();
  95. }
  96. }