UserRetiredSalary.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class UserRetiredSalary extends Model
  8. {
  9. // 工资发放
  10. public function sendSalary($data){
  11. $types = [];
  12. if($data['types']){
  13. $types = Db::name('user_subsidies')->where('id','in',$data['types'])->where('del',0)->select();
  14. }
  15. $users = $data['users'];
  16. $curTime = date('Y-m-d H:i:s');
  17. $arr = [];
  18. foreach ($users as $k=>$v){
  19. $data = [
  20. 'salary_record_id' => $data['salary_record_id'],
  21. 'user_id' => $v['userId'],
  22. 'bz' => $v['bz'],
  23. 'zysdbt' => $v['zysdbt'],
  24. 'fb' => $v['fb'],
  25. 'wybt' => $v['wybt'],
  26. ];
  27. $salary = $v['bz'] + $v['zysdbt'] + $v['fb'] + $v['wybt'];
  28. $btjson = [];
  29. foreach ($v as $key=>$val){
  30. if(strpos($key,'subsidies') === 0){
  31. foreach ($types as $kk=>$vv){
  32. if($vv['id'] == str_ireplace('subsidies','',$key)){
  33. $btjson[] = [
  34. 'id' => $vv['id'],
  35. 'cate' => $vv['cate'],
  36. 'title' => $vv['type'],
  37. 'money' => $val
  38. ];
  39. if($vv['cate'] == 1){ // 补贴
  40. $salary += $val;
  41. }else{
  42. $salary -= $val;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. $data['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
  49. $data['salary'] = $salary;
  50. $data['create_time'] = $curTime;
  51. $arr[] = $data;
  52. }
  53. $ret = Db::name('user_retired_salary')->insertAll($arr);
  54. if($ret != count($arr)){
  55. return false;
  56. }
  57. return true;
  58. }
  59. public function sendOneSalary($user,$data,$id){
  60. $types = [];
  61. if($data['types']){
  62. $types = Db::name('user_subsidies')->where('id','in',$data['types'])->where('del',0)->select();
  63. }
  64. $curTime = date('Y-m-d H:i:s');
  65. $sdata = [
  66. 'salary_record_id' => $data['id'],
  67. 'user_id' => $user['userId'],
  68. 'bz' => $user['bz'],
  69. 'zysdbt' => $user['zysdbt'],
  70. 'fb' => $user['fb'],
  71. 'wybt' => $user['wybt'],
  72. ];
  73. $salary = $user['bz'] + $user['zysdbt'] + $user['fb'] + $user['wybt'];
  74. $btjson = [];
  75. foreach ($user as $key=>$val){
  76. if(strpos($key,'subsidies') === 0){
  77. foreach ($types as $kk=>$vv){
  78. if($vv['id'] == str_ireplace('subsidies','',$key)){
  79. $val = (float)$val > 0?(float)$val:0; // 不填或小于0的值默认成0
  80. $btjson[] = [
  81. 'id' => $vv['id'],
  82. 'cate' => $vv['cate'],
  83. 'title' => $vv['type'],
  84. 'money' => $val
  85. ];
  86. if($vv['cate'] == 1){ // 补贴
  87. $salary += $val;
  88. }else{
  89. $salary -= $val;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
  96. $sdata['salary'] = $salary;
  97. $sdata['create_time'] = $curTime;
  98. $res = Db::name('user_retired_salary')->where('id',$id)->update($sdata);
  99. if($res === false){
  100. return false;
  101. }
  102. return true;
  103. }
  104. public function info($id){
  105. $info = $this->where('id',$id)->find();
  106. if(!$info){
  107. HelpHander::error('数据不存在');
  108. }
  109. $data = $info->toArray();
  110. $data['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  111. return $data;
  112. }
  113. public function lists($page,$size,$name,$srid){
  114. if($name != ''){
  115. $map[] = ['ui.name','like','%'.$name.'%'];
  116. }
  117. $map[] = ['ur.salary_record_id','=',$srid];
  118. $lists = Db::name('user_retired_salary')
  119. ->alias('ur')
  120. ->join('user_info ui','ui.user_id = ur.user_id')
  121. ->where($map)
  122. ->field('ur.*,ui.name')
  123. ->page($page,$size)
  124. ->order('ui.sorts2 asc,ur.id desc')
  125. ->select();
  126. $lists = $lists?$lists:[];
  127. foreach ($lists as $k=>$v){
  128. $btjson = $v['btjson']?json_decode($v['btjson'],true):[];
  129. foreach ($btjson as $kk=>$vv){
  130. $lists[$k]['subsidies'.$vv['id']] = $vv['money'];
  131. }
  132. }
  133. $total = Db::name('user_retired_salary')
  134. ->alias('ur')
  135. ->join('user_info ui','ui.user_id = ur.user_id')
  136. ->where($map)->count();
  137. $data = [
  138. 'total' => $total,
  139. 'list' => $lists?$lists:[]
  140. ];
  141. return $data;
  142. }
  143. }