123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class PartyFee extends Model
- {
- public function add(){
- $year = input('year','','trim');
- if(!$year){
- HelpHander::error('未选择年份');
- }
- // 检查该年份是否已生成过党费
- $ret = Db::name('party_fee')->where('year',$year)->find();
- if($ret){
- HelpHander::error('该年份已生成过党费');
- }
- $recordIds = Db::name('salary_record')->where('month','like',$year.'-%')->where('type','in',[1,2])->where('status',2)->where('del',0)->column('id');
- if(!$recordIds){
- HelpHander::error('该年份工资还没有已发放的工资');
- }
- $duids = Db::name('user_info')->where('political','in',['中共党员','中共预备党员'])->column('user_id'); // 党员
- $map[] = ['salary_record_id','in',$recordIds];
- if($duids){
- $map[] = ['user_id','in',$duids];
- }else{
- $map[] = ['user_id','=',0];
- }
- $userIds = Db::name('salary_record_log')->where($map)->column('user_id');
- if(!$userIds){
- HelpHander::error('没有可生成党费的人员或未发放工资');
- }
- $userIds = array_unique($userIds);
- $curTime = date('Y-m-d H:i:s');
- Db::startTrans();
- try{
- $btids = Db::name('user_subsidies')->where('prop',2)->column('id');
- $btids = $btids?$btids:[];
- $arr = [];
- foreach ($userIds as $uid){
- $d = [
- 'year' => $year,
- 'user_id' => $uid,
- 'should_total' => 0,
- 'yjx' => 0,
- 'tax' => 0,
- 'sbgjj' => 0,
- 'bt' => 0,
- 'cardinal' => 0,
- 'bl' => 0,
- 'fee' => 0,
- 'create_time' => $curTime
- ];
- $logs = Db::name('salary_record_log')->where('salary_record_id','in',$recordIds)->where('gwgz','>',0)->where('user_id',$uid)->select();
- $logs = $logs?$logs:[];
- foreach ($logs as $kk=>$vv){
- $d['should_total'] += $vv['should_total'];
- $d['yjx'] += $vv['yjx'];
- $d['tax'] += $vv['tax'];
- $d['sbgjj'] += $vv['sbgjj'];
- $btjson = $vv['btjson']?json_decode($vv['btjson'],true):[];
- $bt = 0;
- foreach ($btjson as $bk=>$bv){
- if($bv['cate'] == 1 && in_array($bv['id'],$btids)){
- $bt += floatval($bv['money']);
- }
- }
- $d['bt'] += $bt;
- }
- $d['should_total'] = round($d['should_total'],2);
- $d['yjx'] = round($d['yjx'],2);
- $d['tax'] = round($d['tax'],2);
- $d['sbgjj'] = round($d['sbgjj'],2);
- $d['bt'] = round($d['bt'],2);
- $d['cardinal'] = round($d['should_total'] - $d['yjx'] - $d['tax'] - $d['sbgjj'] - $d['bt'],2);
- $d['cardinal'] = $d['cardinal'] > 0?$d['cardinal']:0;
- $bl = 0;
- if($d['cardinal'] > 0){
- $cardinal = $d['cardinal']/count($logs);
- if($cardinal <= 3000){
- $bl = 0.005;
- }else if($cardinal > 3000 && $cardinal <= 5000){
- $bl = 0.01;
- }else if($cardinal > 5000 && $cardinal <= 10000){
- $bl = 0.015;
- }else{
- $bl = 0.02;
- }
- }
- $d['bl'] = $bl;
- $d['fee'] = round($d['cardinal']*$bl,2);
- $arr[] = $d;
- if(count($arr) == 30){
- $ret = Db::name('party_fee')->insertAll($arr);
- if(!$ret || count($arr) != $ret){
- \exception('保存失败');
- }
- $arr = [];
- }
- }
- if(count($arr) > 0){
- $ret = Db::name('party_fee')->insertAll($arr);
- if(!$ret || count($arr) != $ret){
- \exception('保存失败');
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- trace($e->getMessage());
- HelpHander::error('操作失败'.$e->getMessage());
- }
- return true;
- }
- public function oldadd(){
- $month = input('month','','trim');
- $year = input('year','','trim');
- $type = input('type/d',0);
- if(!in_array($type,[1,2])){
- HelpHander::error('参数错误');
- }
- if($type == 1 && !$month){
- HelpHander::error('未选择月份');
- }
- if($type == 2 && !$year){
- HelpHander::error('未选择年份');
- }
- if($type == 1){
- $slists = Db::name('salary_record')->where('month',$month)->where('type','in',[1,2])->where('status',2)->where('del',0)->select();
- if(!$slists){
- HelpHander::error('该月份没有已发放的工资');
- }
- }else{
- $slists = Db::name('salary_record')->where('month','like',$year.'-%')->where('type','in',[1,2])->where('status',2)->where('del',0)->select();
- if(!$slists){
- HelpHander::error('该年份工资还没有已发放的工资');
- }
- }
- $duids = Db::name('user_info')->where('political','in',['中共党员','中共预备党员'])->column('user_id'); // 党员
- Db::startTrans();
- try{
- foreach ($slists as $kk=>$vv){
- $map = [];
- $uids = Db::name('party_fee')->where('month',$vv['month'])->column('user_id');
- $map[] = ['gwgz','>',0];
- $map[] = ['salary_record_id','=',$vv['id']];
- if($uids){
- $map[] = ['user_id','not in',$uids];
- }
- if($duids){
- $map[] = ['user_id','in',$duids];
- }
- $lists = Db::name('salary_record_log')->where($map)->select();
- if($lists){
- $arr = [];
- $curTime = date('Y-m-d H:i:s');
- foreach ($lists as $k=>$v){
- $btjson = $v['btjson']?json_decode($v['btjson'],true):[];
- $kcmoney = 0;
- foreach ($btjson as $kkk=>$vvv){
- $lists[$k]['subsidies'.$vvv['id']] = $vvv['money'];
- if($vvv['cate'] == 2){ // 扣除项
- $kcmoney += $vvv['money'];
- }
- }
- $dktotal = round($v['sbgjj'] + $v['tax'] + $kcmoney,2);
- $dktotal = $dktotal>0?$dktotal:0;
- $cardinal = round($v['gwgz'] + $v['xjgz'] + $v['jcjx'] - $dktotal,2);
- $cardinal = $cardinal>0?$cardinal:0;
- $bl = 0;
- if($cardinal <= 3000){
- $bl = 0.005;
- }else if($cardinal > 3000 && $cardinal <= 5000){
- $bl = 0.01;
- }else if($cardinal > 5000 && $cardinal <= 10000){
- $bl = 0.015;
- }else{
- $bl = 0.02;
- }
- $dd = [
- 'user_id' => $v['user_id'],
- 'month' => $vv['month'],
- 'gwgz' => $v['gwgz'],
- 'xjgz' => $v['xjgz'],
- 'jcjx' => $v['jcjx'],
- 'dktotal' => $dktotal,
- 'cardinal' => $cardinal,
- 'bl' => $bl,
- 'fee' => round($cardinal*$bl,2),
- 'create_time' => $curTime
- ];
- $arr[] = $dd;
- if(count($arr) == 30){
- $ret = Db::name('party_fee')->insertAll($arr);
- if(!$ret || count($arr) != $ret){
- \exception('保存失败');
- }
- $arr = [];
- }
- }
- if(count($arr) > 0){
- $ret = Db::name('party_fee')->insertAll($arr);
- if(!$ret || count($arr) != $ret){
- \exception('保存失败');
- }
- }
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- trace($e->getMessage());
- HelpHander::error('操作失败'.$e->getMessage());
- }
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $data = $info->toArray();
- return $data;
- }
- public function del($id){
- $ret = $this->delete($id);
- if(!$ret){
- HelpHander::error('删除失败');
- }
- return true;
- }
- public function lists($title,$year){
- $map[] = ['a.year','=',$year];
- if($title){
- $map[] = ['b.name','like','%'.$title.'%'];
- }
- $lists = Db::name('party_fee')
- ->alias('a')
- ->join('user_info b','a.user_id = b.user_id')
- ->where($map)
- ->order('b.sorts3 asc,b.user_id asc')
- ->field('a.*,b.name')
- ->select();
- $lists = $lists?$lists:[];
- $all = [
- 'name' => '合计',
- 'year' => '',
- 'should_total' => 0,
- 'yjx' => 0,
- 'tax' => 0,
- 'sbgjj' => 0,
- 'bt' => 0,
- 'cardinal' => 0,
- 'bl' => '',
- 'fee' => 0
- ];
- foreach ($lists as $k=>$v){
- $all['should_total'] += $v['should_total'];
- $all['yjx'] += $v['yjx'];
- $all['tax'] += $v['tax'];
- $all['sbgjj'] += $v['sbgjj'];
- $all['bt'] += $v['bt'];
- $all['cardinal'] += $v['cardinal'];
- $all['fee'] += $v['fee'];
- }
- $all['should_total'] = round($all['should_total'],2);
- $all['yjx'] = round($all['yjx'],2);
- $all['tax'] = round($all['tax'],2);
- $all['sbgjj'] = round($all['sbgjj'],2);
- $all['bt'] = round($all['bt'],2);
- $all['cardinal'] = round($all['cardinal'],2);
- $all['fee'] = round($all['fee'],2);
- $lists[] = $all;
- $columns = [
- ["title" => "姓名","key" => "name"],
- ["title" => "应发工资","key" => "shouldTotal"],
- ["title" => "奖励性绩效","key" => "yjx"],
- ["title" => "个人所得税","key" => "tax"],
- ["title" => "五险二金","key" => "sbgjj"],
- ["title" => "津贴补贴和改革性补贴","key" => "bt"],
- ["title" => "党费基数","key" => "cardinal"],
- ["title" => "党费","key" => "fee"],
- ];
- $data = [
- 'columns' => $columns,
- 'list' => $lists
- ];
- return $data;
- }
- public function ulists($userId,$year){
- $map[] = ['a.year','=',$year];
- $map[] = ['a.user_id','=',$userId];
- $lists = Db::name('party_fee')
- ->alias('a')
- ->join('user_info b','a.user_id = b.user_id')
- ->where($map)
- ->order('b.sorts3 asc,b.user_id asc')
- ->field('a.*,b.name')
- ->select();
- $lists = $lists?$lists:[];
- $columns = [
- // ["title" => "姓名","key" => "name"],
- ["title" => "应发工资","key" => "shouldTotal"],
- ["title" => "奖励性绩效","key" => "yjx"],
- ["title" => "个人所得税","key" => "tax"],
- ["title" => "五险二金","key" => "sbgjj"],
- ["title" => "津贴补贴和改革性补贴","key" => "bt"],
- ["title" => "党费基数","key" => "cardinal"],
- ["title" => "党费","key" => "fee"],
- ];
- $data = [
- 'columns' => $columns,
- 'list' => $lists
- ];
- return $data;
- }
- }
|