GluSalaryCalculation.php 3.8 KB

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