123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class UserSalaryRecord extends Model
- {
- // 工资发放
- public function sendSalary($data){
- $month = $data['month'];
- $types = $data['types'];
- $types = $types?explode(',',$types):[];
- $users = Db::name('user_salary')
- ->where('status',1)
- ->where('company_id',$data['company_id'])
- ->field('create_time,update_time,remark,status',true)
- ->select();
- $curTime = date('Y-m-d H:i:s');
- $bls = Db::name('user_bl')->select();
- foreach ($users as $k=>$v){
- $udata = $this->getUserBase($v['user_id']);
- $age = $udata['age'];
- $gender = $udata['gender'];
- $rdata = [
- 'user_id' => $v['user_id'],
- 'company_id' => $data['company_id'],
- 'type' => $udata['type'],
- 'psid' => $udata['psid'],
- 'pwlevel' => $udata['pwlevel'],
- 'month_performance' => $udata['month_performance'],
- 'wages' => $udata['wages'],
- 'post_salary' => $udata['post_salary'],
- 'salary_record_id' => $data['salary_record_id'],
- 'create_time' => $curTime,
- 'month' => $month,
- 'fixed_performance' => $v['fixed_performance'],
- 'child' => $v['child'],
- 'mssr' => $v['mssr'],
- 'qtbt' => $v['qtbt'],
- ];
- $sbgjj = $this->getSbGjj($v['user_id'],$data['company_id'],$bls);
- $rdata['sbgjj'] = json_encode($sbgjj);
- $butie = $this->getButie($v['user_id'],$data['company_id'],$udata['psid'],$age,$gender,$types,$v,$month);
- $rdata['btjson'] = $butie['btjson'];
- $rdata['should_total'] = $rdata['wages'] + $rdata['post_salary'] + $rdata['month_performance'] + $rdata['fixed_performance'] + $butie['total'] + $v['qtbt'];
- $taxdata = $this->getTax($v['user_id'],$data['company_id'],$month,$rdata,$sbgjj,$v);
- $rdata['tax'] = $taxdata['tax'];
- $rdata['salary'] = $rdata['should_total'] - $taxdata['tax'] - $rdata['child'] - $sbgjj['gjjp'] - $sbgjj['qynjp'] - $sbgjj['ptotal'];
- $rdata['salary'] = $rdata['salary'] > 0?$rdata['salary']:0;
- $rid = Db::name('user_salary_record')->insertGetId($rdata);
- if(!$rid){
- return false;
- }
- $rtax = $taxdata['data'];
- $rtax['record_id'] = $rid;
- $ret = Db::name('user_salary_record_tax')->insert($rtax);
- if(!$ret){
- return false;
- }
- }
- return true;
- }
- private function getTax($userId,$companyId,$month,$rdata,$sbgjj,$user){
- $special = Db::name('user_special')->where('user_id',$userId)->find();
- $ysmoney = $rdata['should_total'] - $rdata['mssr']; // 应发-减免税额 = 应税收入
- $jc = 5000;
- $tax = 0;
- //获取上月累计
- $mtime = strtotime($month.'-01');
- $my = date('Y',$mtime);
- $sy = date('Y',$mtime - 2*24*60*60);
- $data = [
- 'sr' => 0,
- 'jc' => 0,
- 'nj' => 0,
- 'gjj' => 0,
- 'ylobx' => 0,
- 'sybx' => 0,
- 'ylibx' => 0,
- 'znjy' => 0,
- 'jxjy' => 0,
- 'zfdklx' => 0,
- 'zfzj' => 0,
- 'sylr' => 0,
- 'donations' => 0,
- 'ykjse' => 0,
- 'syjkbx' => 0,
- 'ysylbx' => 0,
- 'other' => 0,
- 'min_tax' => 0
- ]; // 累计收入
- $smonth = date('Y-m',$mtime - 2*24*60*60);
- // 先查tax记录
- $info = Db::name('user_salary_record_tax')
- ->alias('t')
- ->join('user_salary_record r','r.id = t.record_id')
- ->where('r.user_id',$userId)
- ->where('r.month',$smonth)
- ->field('t.*')
- ->find();
- if(!$info){
- $info = Db::name('user_total')
- ->where('year',$sy)
- ->where('user_id',$userId)
- ->find();
- }
- if($info){
- $data = [
- 'sr' => $info['sr'],
- 'jc' => $info['jc'],
- 'nj' => $info['nj'],
- 'gjj' => $info['gjj'],
- 'ylobx' => $info['ylobx'],
- 'sybx' => $info['sybx'],
- 'ylibx' => $info['ylibx'],
- 'znjy' => $info['znjy'],
- 'jxjy' => $info['jxjy'],
- 'zfdklx' => $info['zfdklx'],
- 'zfzj' => $info['zfzj'],
- 'sylr' => $info['sylr'],
- 'donations' => $info['donations'],
- 'ykjse' => $info['ykjse'],
- 'syjkbx' => $info['syjkbx'],
- 'ysylbx' => $info['ysylbx'],
- 'other' => $info['other'],
- 'min_tax' => $info['min_tax']
- ];
- }
- if($companyId == 1){ // 算上个月的税
- // 累计预扣预缴应纳税所得额
- $ljkc = $data['jc'] + $data['nj'] + $data['gjj'] + $data['ylobx'] + $data['sybx'] + $data['ylibx'] + $data['znjy'] + $data['jxjy']
- + $data['zfdklx'] + $data['zfzj'] + $data['sylr'] + $data['donations'] + $data['syjkbx'] + $data['ysylbx'] + $data['other'];
- $tax1 = $ljkc*0.03 - 0;
- $tax2 = $ljkc*0.1 - 2520;
- $tax3 = $ljkc*0.2 - 16920;
- $tax4 = $ljkc*0.25 - 31920;
- $tax5 = $ljkc*0.3 - 52920;
- $tax6 = $ljkc*0.35 - 85920;
- $tax7 = $ljkc*0.45 - 181920;
- $tax = round(max([$tax1,$tax2,$tax3,$tax4,$tax5,$tax6,$tax7]),2);
- $tax = round($tax - $tax*$data['min_tax'],2);
- }
- $newdata = [
- 'sr' => $data['sr'] + $ysmoney,
- 'jc' => $data['jc'] + $jc,
- 'nj' => $data['nj'] + $sbgjj['qynjp'],
- 'gjj' => $data['gjj'] + $sbgjj['gjjp'],
- 'ylobx' => $data['ylobx'] + $sbgjj['ylap'],
- 'sybx' => $data['sybx'] + $sbgjj['syep'],
- 'ylibx' => $data['ylibx'] + $sbgjj['ylip'],
- 'znjy' => $data['znjy'],
- 'jxjy' => $data['jxjy'],
- 'zfdklx' => $data['zfdklx'],
- 'zfzj' => $data['zfzj'],
- 'sylr' => $data['sylr'],
- 'donations' => $data['donations'] + $user['donations'],
- 'ykjse' => $data['ykjse'],
- 'syjkbx' => $user['syjkbx'],
- 'ysylbx' => $user['ysylbx'],
- 'other' => $user['other'],
- 'min_tax' => $user['min_tax']
- ];
- if($special){
- $newdata['znjy'] += $special['znjy'];
- $newdata['jxjy'] += $special['jxjy'];
- $newdata['zfdklx'] += $special['zfdklx'];
- $newdata['zfzj'] += $special['zfzj'];
- $newdata['sylr'] += $special['sylr'];
- }
- if($companyId == 2){ // 新华算本月税
- // 累计预扣预缴应纳税所得额
- $ljkc = $newdata['jc'] + $newdata['nj'] + $newdata['gjj'] + $newdata['ylobx'] + $newdata['sybx'] + $newdata['ylibx'] + $newdata['znjy'] + $newdata['jxjy']
- + $newdata['zfdklx'] + $newdata['zfzj'] + $newdata['sylr'] + $newdata['donations'] + $newdata['syjkbx'] + $newdata['ysylbx'] + $newdata['other'];
- $tax1 = $ljkc*0.03 - 0;
- $tax2 = $ljkc*0.1 - 2520;
- $tax3 = $ljkc*0.2 - 16920;
- $tax4 = $ljkc*0.25 - 31920;
- $tax5 = $ljkc*0.3 - 52920;
- $tax6 = $ljkc*0.35 - 85920;
- $tax7 = $ljkc*0.45 - 181920;
- $tax = round(max([$tax1,$tax2,$tax3,$tax4,$tax5,$tax6,$tax7]),2);
- $tax = round($tax - $tax*$newdata['min_tax'],2);
- }
- $tax = $tax>0?$tax:0;
- $newdata['ykjse'] += $tax;
- return ['tax'=>$tax,'data' => $newdata];
- }
- private function getButie($userId,$companyId,$post,$age,$gender,$types,$user,$month){
- $data = [];
- $data[] = [
- 'name' => '其他补贴',
- 'money' => $user['qtbt']
- ];
- foreach ($types as $val){
- switch ($val){
- case '1': // 防暑降温费
- $var = $this->getBtByName('fsjwf',$userId,$post,$age,$gender,$user,$month);
- $data[] = [
- 'name' => '防暑降温费',
- 'money' => $var
- ];
- break;
- case '2': // 禁食猪肉补贴
- $var = $this->getBtByName('jszrbt',$userId,$post,$age,$gender,$user,$month);
- $data[] = [
- 'name' => '禁食猪肉补贴',
- 'money' => $var
- ];
- break;
- }
- }
- $total = 0;
- foreach ($data as $k=>$v){
- $total += $v['money'];
- }
- return ['total'=>$total,'btjson' => json_encode($data)];
- }
- private function getBtByName($name,$userId,$post,$age,$gender,$user,$month){
- $info = Db::name('user_subsidies')->where('name',$name)->find();
- if(!$info){
- return 0;
- }
- $val = 0;
- if($info['type'] == 1){
- $val = $info['money'];
- } else {
- $dd = json_encode($info['values'],true);
- foreach ($dd as $k=>$v){
- if($v['id'] == $post){
- if($v['money2']){
- if($age < 15){
- $val = $info['money1'];
- } else if ($age >= 15 && $age < 25){
- $val = $info['money2'];
- }else{
- $val = $info['money3'];
- }
- }else{
- $val = $info['money1'];
- }
- break;
- }
- }
- }
- // 检查用户是否有补贴
- $btjson = $user['btjson'];
- if($btjson){
- $btjson = json_decode($btjson,true);
- foreach ($btjson as $k=>$v){
- if($v['name'] == $name){
- switch ($v['type']){
- case "1":
- if($v['end']&&$v['end'] >= $month){
- $val = (int) $v['money'] > 0 ?$v['money']:0;
- }
- break;
- case "2":
- if($v['end']&&$v['end'] < $month){
- $val = 0;
- }
- break;
- case "3":
- if($v['end']&&$v['end'] == 0){
- $val = 0;
- }
- break;
- }
- }
- }
- }
- return $val;
- }
- // 社保公积金
- private function getSbGjj($userId,$companyId,$bls){
- $base = Db::name('user_base')->where('user_id',$userId)->where('company_id',$companyId)->find();
- $data = [
- 'qynj' => 0,
- 'gjj' => 0,
- 'sb' => 0,
- 'syyl' => 0,
- 'gssy' => 0,
- 'ylnj' => 0,
- 'ylap' => 0,
- 'ylac' => 0,
- 'syep' => 0,
- 'syec' => 0,
- 'ylip' => 0,
- 'ylic' => 0,
- 'gsc' => 0,
- 'syuc' => 0,
- 'ptotal' => 0,
- 'ctotal' => 0,
- 'sbtotal' => 0,
- 'qynjp' => 0,
- 'qynjc' => 0,
- 'qynjtotal' => 0,
- 'gjjp' => 0,
- 'gjjc' => 0,
- 'gjjtotal' => 0,
- ];
- if(!$base){
- return $data;
- }
- $data['qynj'] = $base['qynj'];
- $data['gjj'] = $base['gjj'];
- $data['sb'] = $base['sb'];
- $data['syyl'] = $base['syyl'];
- $data['gssy'] = $base['gssy'];
- $data['ylnj'] = $base['ylnj'];
- // 计算各项扣除
- foreach ($bls as $key=>$val){
- if($val['company_id'] == $base['company_id']){
- switch ($val['name']){
- case 'gjj':
- $data['gjjp'] = calculate_money($base['gjj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
- $data['gjjc'] = calculate_money($base['gjj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- $data['gjjtotal'] = round($data['gjjp'] + $data['gjjc'],2);
- break;
- case 'qynj':
- if($val['company_id'] == 1){
- $data['qynjp'] = calculate_money($base['ylnj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
- $data['qynjc'] = calculate_money($base['ylnj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- } else {
- $data['qynjp'] = calculate_money($base['qynj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
- $data['qynjc'] = calculate_money($base['qynj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- }
- $data['qynjtotal'] = round($data['qynjp'] + $data['qynjc'],2);
- break;
- case 'yla':
- if($val['company_id'] == 1){
- $data['sylap'] = calculate_money($base['ylnj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
- $data['sylac'] = calculate_money($base['ylnj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- } else {
- $data['sylap'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
- $data['sylac'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- }
- $data['ptotal'] += $data['sylap'];
- $data['ctotal'] += $data['sylac'];
- break;
- case 'sye':
- if($val['company_id'] == 1){
- $data['syep'] = calculate_money($base['gssy'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
- $data['syec'] = calculate_money($base['gssy'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- } else {
- $data['syep'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
- $data['syec'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- }
- $data['ptotal'] += $data['syep'];
- $data['ctotal'] += $data['syec'];
- break;
- case 'yli':
- if($val['company_id'] == 1){
- $data['ylip'] = calculate_money($base['syyl'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
- $data['ylic'] = calculate_money($base['syyl'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- } else {
- $data['ylip'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
- $data['ylic'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- }
- $data['ptotal'] += $data['ylip'];
- $data['ctotal'] += $data['ylic'];
- break;
- case 'gs':
- if($val['company_id'] == 1){
- $data['gsc'] = calculate_money($base['gssy'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- } else {
- $data['gsc'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- }
- $data['ctotal'] += $data['gsc'];
- break;
- case 'syu':
- if($val['company_id'] == 1){
- $data['syuc'] = calculate_money($base['gssy'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- } else {
- $data['syuc'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- }
- $data['ctotal'] += $data['syuc'];
- break;
- }
- }
- }
- $data['ctotal'] = round($data['ctotal'],2);
- $data['ptotal'] = round($data['ptotal'],2);
- $data['sbtotal'] = $data['ptotal'] + $data['ctotal'];
- return $data;
- }
- // 获取用户基本工资
- private function getUserBase($userId){
- $uinfo = Db::name('user_info')->where('user_id',$userId)->field('pwlevel,psid,join_time,gender,company_id,type')->find();
- $post = Db::name('post_salary')->where('id',$uinfo['psid'])->find();
- $data['post_salary'] = $post?$post['money']:0;
- $data['month_performance'] = $post?$post['money']:0;
- $wages = Db::name('pay_wages')->where('company_id',$uinfo['company_id'])->where('type',$uinfo['type'])->value('content');
- $data['wages'] = 0;
- if($wages){
- $wages = json_decode($wages,true);
- foreach ($wages as $kk=>$vv){
- if($vv['level'] == $uinfo['pwlevel']){
- $data['wages'] = $vv['money'];
- break;
- }
- }
- }
- $data['age'] = 0;
- $curYear = date('Y');
- if($uinfo['join_time']){
- $joinYear = date('Y',strtotime($uinfo['join_time']));
- }
- $data['age'] = $curYear - $joinYear;
- $data['gender'] = $uinfo['gender'];
- $data['type'] = $uinfo['type'];
- $data['pwlevel'] = $uinfo['pwlevel'];
- $data['psid'] = $uinfo['psid'];
- return $data;
- }
- 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_salary_record')
- ->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();
- foreach ($lists as $k=>$v){
- $lists[$k]['sbgjj'] = json_decode($v['sbgjj'],true);
- $btjson = json_decode($v['btjson'],true);
- $lists[$k]['btjson'] = $btjson?$btjson:[];
- $bttotal = 0;
- if($btjson){
- foreach ($btjson as $kk=>$vv){
- $bttotal += $vv['money'];
- }
- }
- $lists[$k]['bttotal'] = round($bttotal,2);
- $lists[$k]['ptotal'] = round($lists[$k]['sbgjj']['ptotal'],2);
- }
- $total = Db::name('user_salary_record')
- ->alias('ur')
- ->join('user_info ui','ui.user_id = ur.user_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- }
|