123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class UserRetiredSalary extends Model
- {
- // 工资发放
- public function sendSalary($data){
- $types = [];
- if($data['types']){
- $types = Db::name('user_subsidies')->where('id','in',$data['types'])->where('del',0)->select();
- }
- $users = $data['users'];
- $curTime = date('Y-m-d H:i:s');
- $arr = [];
- foreach ($users as $k=>$v){
- $data = [
- 'salary_record_id' => $data['salary_record_id'],
- 'user_id' => $v['userId'],
- 'bz' => $v['bz'],
- 'zysdbt' => $v['zysdbt'],
- 'fb' => $v['fb'],
- 'wybt' => $v['wybt'],
- ];
- $salary = $v['bz'] + $v['zysdbt'] + $v['fb'] + $v['wybt'];
- $btjson = [];
- foreach ($v as $key=>$val){
- if(strpos($key,'subsidies') === 0){
- foreach ($types as $kk=>$vv){
- if($vv['id'] == str_ireplace('subsidies','',$key)){
- $btjson[] = [
- 'id' => $vv['id'],
- 'cate' => $vv['cate'],
- 'title' => $vv['type'],
- 'money' => $val
- ];
- if($vv['cate'] == 1){ // 补贴
- $salary += $val;
- }else{
- $salary -= $val;
- }
- }
- }
- }
- }
- $data['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
- $data['salary'] = $salary;
- $data['create_time'] = $curTime;
- $arr[] = $data;
- }
- $ret = Db::name('user_retired_salary')->insertAll($arr);
- if($ret != count($arr)){
- return false;
- }
- return true;
- }
- public function sendOneSalary($user,$data,$id){
- $types = [];
- if($data['types']){
- $types = Db::name('user_subsidies')->where('id','in',$data['types'])->where('del',0)->select();
- }
- $curTime = date('Y-m-d H:i:s');
- $sdata = [
- 'salary_record_id' => $data['id'],
- 'user_id' => $user['userId'],
- 'bz' => $user['bz'],
- 'zysdbt' => $user['zysdbt'],
- 'fb' => $user['fb'],
- 'wybt' => $user['wybt'],
- ];
- $salary = $user['bz'] + $user['zysdbt'] + $user['fb'] + $user['wybt'];
- $btjson = [];
- foreach ($user as $key=>$val){
- if(strpos($key,'subsidies') === 0){
- foreach ($types as $kk=>$vv){
- if($vv['id'] == str_ireplace('subsidies','',$key)){
- $val = (float)$val > 0?(float)$val:0; // 不填或小于0的值默认成0
- $btjson[] = [
- 'id' => $vv['id'],
- 'cate' => $vv['cate'],
- 'title' => $vv['type'],
- 'money' => $val
- ];
- if($vv['cate'] == 1){ // 补贴
- $salary += $val;
- }else{
- $salary -= $val;
- }
- }
- }
- }
- }
- $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
- $sdata['salary'] = $salary;
- $sdata['create_time'] = $curTime;
- $res = Db::name('user_retired_salary')->where('id',$id)->update($sdata);
- if($res === false){
- return false;
- }
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $data = $info->toArray();
- $data['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
- return $data;
- }
- public function lists($page,$size,$name,$srid){
- if($name != ''){
- $map[] = ['ui.name','like','%'.$name.'%'];
- }
- $map[] = ['ur.salary_record_id','=',$srid];
- $lists = Db::name('user_retired_salary')
- ->alias('ur')
- ->join('user_info ui','ui.user_id = ur.user_id')
- ->where($map)
- ->field('ur.*,ui.name')
- ->page($page,$size)
- ->order('ui.sorts2 asc,ur.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $btjson = $v['btjson']?json_decode($v['btjson'],true):[];
- foreach ($btjson as $kk=>$vv){
- $lists[$k]['subsidies'.$vv['id']] = $vv['money'];
- }
- }
- $total = Db::name('user_retired_salary')
- ->alias('ur')
- ->join('user_info ui','ui.user_id = ur.user_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- }
|