1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class SalaryRecord extends Model
- {
- // 工资发放
- public function add(){
- $data = [
- 'type' => input('type/d',0),
- 'month' => input('month','','trim'),
- 'types' => input('types','','trim'),
- 'remark' => input('remark','','trim'),
- 'user_id' => input('userId/d',0),
- 'users' => input('users','','trim')
- ];
- $orgId = input('orgId/d',0);
- $result = validate('SalaryRecord')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('SalaryRecord')->getError());
- }
- // 检查是否已设置审批流程
- $apply = model('SalaryApply')->all2($data['type']);
- if(!$apply){
- HelpHander::error('该类型工资的发放未设置审批节点');
- }
- $curinfo = $apply[0];
- $data['apply'] = json_encode($apply);
- $data['cur_node'] = $curinfo['id'];
- Db::startTrans();
- try{
- $users = json_decode($data['users'],true);
- unset($data['users']);
- $sdata = $data;
- $sdata['nums'] = count($users);
- $sdata['create_time'] = date('Y-m-d H:i:s');
- $salaryId = Db::name('SalaryRecord')->insertGetId($sdata);
- if(!$salaryId){
- \exception('操作失败');
- }
- $data['users'] = $users;
- $data['salary_record_id'] = $salaryId;
- if($data['type'] == 5){ //1=中心编制 2=新华聘用 3=中心劳务 4=新华劳务 5=中心退休
- $ret = model('UserRetiredSalary')->sendSalary($data);
- }else{
- $ret = model('SalaryRecord')->sendSalary($data);
- }
- if(!$ret){
- \exception('操作失败');
- }
- // 生成审核记录
- $uids = explode(',',$curinfo['userids']);
- foreach ($uids as $k=>$v){
- $sd = [
- 'salary_record_id' => $salaryId,
- 'user_id' => $v,
- 'status' => 0,
- 'create_time' => date('Y-m-d H:i:s'),
- 'salary_apply_id' => $curinfo['id']
- ];
- $ret = Db::name('salary_apply_record')->insertGetId($sd);
- if(!$ret){
- \exception('操作失败');
- }
- model('Message')->add(3,$salaryId,12,$v,$orgId,'工资流程待您的审核确认');
- }
- if(!$ret){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- // 重新发工资
- public function resend(){
- $salaryRecordId = input('salaryRecordId/d',0);
- $id = input('id/d',0);
- $users = input('users','','trim');
- $userId = input('userId/d',0);
- $info = Db::name('salary_record')->where('id',$salaryRecordId)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 0){
- HelpHander::error('无权限操作');
- }
- $sar = Db::name('salary_apply_record')
- ->where('salary_apply_id',$info['cur_node'])
- ->where('salary_record_id',$salaryRecordId)
- ->where('user_id',$userId)
- ->where('status',0)
- ->where('del',0)
- ->find();
- if(!$sar){
- HelpHander::error('无权限修改');
- }
- $users = json_decode($users,true);
- if(!$users){
- HelpHander::error('参数错误');
- }
- Db::startTrans();
- try{
- if($info['type'] === 5){
- $ret = model('UserRetiredSalary')->sendOneSalary($users[0],$info,$id);
- }else{
- $ret = model('SalaryRecord')->sendOneSalary($users[0],$info,$id);
- }
- if(!$ret){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- private function sendOneSalary($user,$data,$id){
- $user['psmoney'] = (float)$user['psmoney'];
- $user['pwmoney'] = (float)$user['pwmoney'];
- $user['fixedPerformance'] = (float)$user['fixedPerformance'];
- $user['psmonth'] = (float)$user['psmonth'];
- $types = [];
- if($data['types']){
- $types = Db::name('user_subsidies')
- ->where('id','in',$data['types'])
- ->where('del',0)
- ->order('sorts asc,id asc')
- ->select();
- }
- if($data['type'] == 3||$data['type'] == 4){ // 劳务费
- $sdata = [
- 'salary_record_id' => $data['id'],
- 'user_id' => $user['userId'],
- 'gwgz' => $user['psmoney'],
- 'xjgz' => $user['pwmoney'],
- 'jcjx' => $user['fixedPerformance'],
- 'yjx' => $user['psmonth'],
- 'pwlevel' => $user['pwlevel'],
- 'psid' => $user['psid'],
- 'type' => $user['type'],
- 'company_id' => $user['companyId'],
- 'child' => 0
- ];
- $should_total = $user['psmoney'] + $user['pwmoney'] + $user['fixedPerformance'] + $user['psmonth'];
- $jbsalary = $should_total;
- $btjson = [];
- $kcsalary = 0;
- 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['title'],
- 'money' => $val
- ];
- if($vv['cate'] == 1){ // 补贴
- $should_total += $val;
- }else{
- $kcsalary += $val;
- }
- }
- }
- }
- }
- $sdata['should_total'] = $should_total;
- $sdata['tax'] = $this->getLaborTax($should_total);
- $sdata['salary'] = round($should_total - $sdata['tax'] - $sdata['child'] - $kcsalary,2);
- if($sdata['salary'] < 0){
- return false;
- }
- $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
- $res = Db::name('salary_record_log')->where('id',$id)->update($sdata);
- if($res === false){
- return false;
- }
- }else if($data['type'] == 1||$data['type'] == 2){ // 新华聘算社保及累计数
- $sdata = [
- 'salary_record_id' => $data['id'],
- 'user_id' => $user['userId'],
- 'gwgz' => $user['psmoney'],
- 'xjgz' => $user['pwmoney'],
- 'jcjx' => $user['fixedPerformance'],
- 'yjx' => $user['psmonth'],
- 'pwlevel' => $user['pwlevel'],
- 'psid' => $user['psid'],
- 'type' => $user['type'],
- 'company_id' => $user['companyId'],
- 'child' => 0
- ];
- $should_total = $user['psmoney'] + $user['pwmoney'] + $user['fixedPerformance'] + $user['psmonth'];
- $jbsalary = $should_total; // 基本工资,如果不发基本工资,专项附件不累计,如果发基本工资,按正常工资走
- $btjson = [];
- $kcsalary = 0;
- 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['title'],
- 'money' => $val
- ];
- if($vv['cate'] == 1){ // 补贴
- $should_total += $val;
- }else{
- $kcsalary += $val;
- }
- }
- }
- }
- }
- $sbdata = $this->getShebao($user['userId'],$user['companyId'],$user,$jbsalary);
- $sbdata['salary_record_id'] = $data['id'];
- $sbdata['user_id'] = $user['userId'];
- // 社保扣除金额
- $sbkc = $sbdata['gjjp'] + $sbdata['qynjp'] +$sbdata['ylip'] +$sbdata['syep'] +$sbdata['ylap'];
- $sdata['should_total'] = $should_total;
- $sdata['sbgjj'] = $sbkc;
- $srid = Db::name('salary_record')
- ->alias('sr')
- ->join('salary_record_special srs','srs.salary_record_id = sr.id')
- ->where('sr.month',$data['month'])
- ->where('srs.user_id',$user['userId'])
- ->where('sr.type',$data['type'])
- ->where('sr.del',0)
- ->order('sr.id desc')
- ->value('sr.id');
- $spdata = $this->getSalaryTax($should_total,$sbdata,$user['userId'],$user['companyId'],$data['month'],$data['type'],$jbsalary,$srid); // 计算个税
- $spdata['salary_record_id'] = $data['id'];
- $spdata['user_id'] = $user['userId'];
- $tax = $spdata['tax'];
- $m = intval(date('m',strtotime($data['month'].'-01')));
- if($m == 1 && $spdata['type'] != 1){ // 一月的第一次发工资
- $spdata['tax'] = 0;
- }else if($m == 1 && $spdata['type'] == 1){ // 补交不算税
- /*$count = Db::name('salary_record')
- ->alias('sr')
- ->join('salary_record_log srl','srl.salary_record_id = sr.id')
- ->join('salary_record_special srs','srs.salary_record_id = sr.id')
- ->where('sr.month',$data['month'])
- ->where('sr.id','<>',$id)
- ->where('srs.user_id',$data['userId'])
- ->where('sr.type',$data['type'])
- ->where('sr.del',0)
- ->order('sr.id desc')
- ->count();
- if($count == 1){
- $tax = Db::name('salary_record')
- ->alias('sr')
- ->join('salary_record_log srl','srl.salary_record_id = sr.id')
- ->join('salary_record_special srs','srs.salary_record_id = sr.id')
- ->where('sr.month',$data['month'])
- ->where('srs.user_id',$data['userId'])
- ->where('sr.type',$data['type'])
- ->where('sr.del',0)
- ->order('sr.id desc')
- ->value('srl.tax');
- $spdata['tax'] = $tax;
- }*/
- }
- unset($sdata['type']);
- $sparr[] = $spdata;
- if($jbsalary > 0){
- $sdata['salary'] = round($should_total - $tax - $sbkc - $kcsalary,2);
- }else{
- $sdata['salary'] = round($should_total - $tax - $kcsalary,2);
- }
- if($sdata['salary'] < 0){
- return false;
- }
- $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
- $sdata['tax'] = $tax;
- $res = Db::name('salary_record_log')->where('id',$id)->update($sdata);
- if($res === false){
- return false;
- }
- $res2 = Db::name('salary_record_sb')
- ->where('user_id',$user['userId'])
- ->where('salary_record_id',$data['id'])
- ->update($sbdata);
- if($res2 === false){
- return false;
- }
- $res3 = Db::name('salary_record_special')
- ->where('user_id',$user['userId'])
- ->where('salary_record_id',$data['id'])
- ->strict(false)
- ->update($spdata);
- if($res3 === false){
- return false;
- }
- }
- return true;
- }
- private function sendSalary($data){
- $types = [];
- if($data['types']){
- $types = Db::name('user_subsidies')
- ->where('id','in',$data['types'])
- ->where('del',0)
- ->order('sorts asc,id asc')
- ->select();
- }
- $users = $data['users'];
- foreach ($users as $k=>$v){
- $users[$k]['psmoney'] = (float)$v['psmoney'];
- $users[$k]['pwmoney'] = (float)$v['pwmoney'];
- $users[$k]['fixedPerformance'] = (float)$v['fixedPerformance'];
- $users[$k]['psmonth'] = (float)$v['psmonth'];
- }
- if($data['type'] == 3||$data['type'] == 4){ // 劳务费
- $arr = [];
- foreach ($users as $k=>$v){
- $sdata = [
- 'salary_record_id' => $data['salary_record_id'],
- 'user_id' => $v['userId'],
- 'gwgz' => $v['psmoney'],
- 'xjgz' => $v['pwmoney'],
- 'jcjx' => $v['fixedPerformance'],
- 'yjx' => $v['psmonth'],
- 'pwlevel' => $v['pwlevel'],
- 'psid' => $v['psid'],
- 'type' => $v['type'],
- 'company_id' => $v['companyId'],
- 'child' => $v['child']
- ];
- $should_total = $v['psmoney'] + $v['pwmoney'] + $v['fixedPerformance'] + $v['psmonth'];
- $btjson = [];
- $kcsalary = 0;
- foreach ($v 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['title'],
- 'money' => $val
- ];
- if($vv['cate'] == 1){ // 补贴
- $should_total += $val;
- }else{
- $kcsalary += $val;
- }
- }
- }
- }
- }
- $sdata['should_total'] = $should_total;
- $sdata['tax'] = $this->getLaborTax($should_total);
- $sdata['salary'] = round($should_total - $sdata['tax'] - $kcsalary,2);
- if($sdata['salary'] < 0){
- return false;
- }
- $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
- $arr[] = $sdata;
- if(count($arr) == 300){
- $res = Db::name('salary_record_log')->insertAll($arr);
- if($res != count($arr)){
- return false;
- }
- }
- }
- if($arr){
- $res = Db::name('salary_record_log')->insertAll($arr);
- if($res != count($arr)){
- return false;
- }
- }
- }else if($data['type'] == 1||$data['type'] == 2){ // 新华聘算社保及累计数
- $arr = [];
- $sbarr = [];
- $sparr = [];
- foreach ($users as $k=>$v){
- $sdata = [
- 'salary_record_id' => $data['salary_record_id'],
- 'user_id' => $v['userId'],
- 'gwgz' => $v['psmoney'],
- 'xjgz' => $v['pwmoney'],
- 'jcjx' => $v['fixedPerformance'],
- 'yjx' => $v['psmonth'],
- 'pwlevel' => $v['pwlevel'],
- 'psid' => $v['psid'],
- 'type' => $v['pwlevel'],
- 'company_id' => $v['companyId'],
- 'child' => $v['child']
- ];
- $should_total = $v['psmoney'] + $v['pwmoney'] + $v['fixedPerformance'] + $v['psmonth'];
- $jbsalary = $should_total; // 基本工资,如果不发基本工资,专项附件不累计,如果发基本工资,按正常工资走
- $btjson = [];
- $kcsalary = 0;
- foreach ($v 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['title'],
- 'money' => $val
- ];
- if($vv['cate'] == 1){ // 补贴
- $should_total += $val;
- }else{
- $kcsalary += $val;
- }
- }
- }
- }
- }
- if($should_total <= 0){
- return false;
- }
- $sbdata = $this->getShebao($v['userId'],$v['companyId'],$v,$jbsalary);
- $sbdata['salary_record_id'] = $data['salary_record_id'];
- $sbdata['user_id'] = $v['userId'];
- $sbarr[] = $sbdata;
- // 社保扣除金额
- $sbkc = $sbdata['gjjp'] + $sbdata['qynjp'] +$sbdata['ylip'] +$sbdata['syep'] +$sbdata['ylap'];
- $sdata['should_total'] = $should_total;
- $sdata['sbgjj'] = $sbkc;
- $spdata = $this->getSalaryTax($should_total,$sbdata,$v['userId'],$v['companyId'],$data['month'],$data['type'],$jbsalary); // 计算个税
- $spdata['salary_record_id'] = $data['salary_record_id'];
- $spdata['user_id'] = $v['userId'];
- $tax = $spdata['tax'];
- $m = intval(date('m',strtotime($data['month'].'-01')));
- if($m == 1 && $spdata['type'] != 1){ // 一月的第一次发工资
- $spdata['tax'] = 0;
- }else if($m == 1 && $spdata['type'] == 1){ // 补交不算税
- /*$count = Db::name('salary_record')
- ->alias('sr')
- ->join('salary_record_log srl','srl.salary_record_id = sr.id')
- ->join('salary_record_special srs','srs.salary_record_id = sr.id')
- ->where('sr.month',$data['month'])
- ->where('srs.user_id',$v['userId'])
- ->where('sr.type',$data['type'])
- ->where('sr.del',0)
- ->order('sr.id desc')
- ->count();
- if($count == 1){
- $tax = Db::name('salary_record')
- ->alias('sr')
- ->join('salary_record_log srl','srl.salary_record_id = sr.id')
- ->join('salary_record_special srs','srs.salary_record_id = sr.id')
- ->where('sr.month',$data['month'])
- ->where('srs.user_id',$v['userId'])
- ->where('sr.type',$data['type'])
- ->where('sr.del',0)
- ->order('sr.id desc')
- ->value('srl.tax');
- $spdata['tax'] = $tax;
- }*/
- }
- unset($spdata['type']);
- $sparr[] = $spdata;
- if($jbsalary > 0){
- $sdata['salary'] = round($should_total - $tax - $sbkc - $kcsalary,2);
- }else{
- $sdata['salary'] = round($should_total - $tax - $kcsalary,2);
- }
- if($sdata['salary'] < 0){ // 实发金额不能小于0
- return false;
- }
- $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
- $sdata['tax'] = $tax;
- $arr[] = $sdata;
- if(count($arr) == 300){
- $res = Db::name('salary_record_log')->insertAll($arr);
- if($res != count($arr)){
- return false;
- }
- }
- if(count($sbarr) == 300){
- $res = Db::name('salary_record_sb')->insertAll($sbarr);
- if($res != count($sbarr)){
- return false;
- }
- }
- if(count($sparr) == 300){
- $res = Db::name('salary_record_special')->insertAll($sparr);
- if($res != count($sparr)){
- return false;
- }
- }
- }
-
- if($arr){
- $res = Db::name('salary_record_log')->insertAll($arr);
- if($res != count($arr)){
- return false;
- }
- }
- if($sbarr){
- $res = Db::name('salary_record_sb')->insertAll($sbarr);
- if($res != count($sbarr)){
- return false;
- }
- }
- if($sparr){
- $res = Db::name('salary_record_special')->insertAll($sparr);
- if($res != count($sparr)){
- return false;
- }
- }
- }
- return true;
- }
- // 新华聘用税/编制个税
- private function getSalaryTax($should_total,$sbdata,$userId,$companyId,$month,$type,$jbsalary,$from=0){// from 0=新增 0<重新计算
- $data = [
- 'salary' => 0,
- 'mssr' => 0,
- 'sr' => 0,
- 'ljsr' => 0,
- 'ljjc' => 0,
- 'ljgjj' => 0,
- 'ljylobx' => 0,
- 'ljsybx' => 0,
- 'ljylibx' => 0,
- 'ljnj' => 0,
- 'ljznjy' => 0,
- 'ljjxjy' => 0,
- 'ljzfdklx' => 0,
- 'ljzfzj' => 0,
- 'ljsylr' => 0,
- 'syjkbx' => 0,
- 'ysylbx' => 0,
- 'grbk' => 0,
- 'jzjbjsb' => 0,
- 'ljgzds' => 0,
- 'qtkc' => 0,
- 'ljsqljxmhj' => 0,
- 'ljykjse' => 0,
- 'tax' => 0,
- 'zykcjze' => 0,
- 'jmsebl' => 0,
- 'extratax' => 0,
- ];
- $lastinfo = [];
- if($from > 0){
- // 每月发多次工资,先找本月是否已经发放过工资,没有的话获取上个月的记录,在没有的话获取初始值
- $lastinfo = Db::name('salary_record')
- ->alias('sr')
- ->join('salary_record_special srs','srs.salary_record_id = sr.id')
- ->where('sr.month',$month)
- ->where('srs.user_id',$userId)
- ->where('sr.id','<>',$from)
- ->where('sr.type',$type)
- ->where('sr.del',0)
- ->field('srs.*')
- ->order('sr.id desc')
- ->find();
- }else{
- // 每月发多次工资,先找本月是否已经发放过工资,没有的话获取上个月的记录,在没有的话获取初始值
- $lastinfo = Db::name('salary_record')
- ->alias('sr')
- ->join('salary_record_special srs','srs.salary_record_id = sr.id')
- ->where('sr.month',$month)
- ->where('srs.user_id',$userId)
- ->where('sr.type',$type)
- ->where('sr.del',0)
- ->field('srs.*')
- ->order('sr.id desc')
- ->find();
- }
- $stype = 0; // 无累计
- if(!$lastinfo){
- $lastmonth = date('Y-m',strtotime($month.'-01') - 24*60*60); // 上个月
- $lastinfo = Db::name('salary_record')
- ->alias('sr')
- ->join('salary_record_special srs','srs.salary_record_id = sr.id')
- ->where('sr.month',$lastmonth)
- ->where('srs.user_id',$userId)
- ->where('sr.type',$type)
- ->where('sr.del',0)
- ->field('srs.*')
- ->order('sr.id desc')
- ->find();
- if($lastinfo){
- $stype = 2; // 有上月累计
- }
- }else{
- $stype = 1; // 有本月累计
- }
- if(!$lastinfo){
- $lastinfo = Db::name('user_total')->where('user_id',$userId)->find();
- if($lastinfo){
- $stype = 3; // 有初始累计
- }
- }
- if($lastinfo){
- $data = [
- 'salary' => $lastinfo['salary'],
- 'mssr' => $lastinfo['mssr'],
- 'sr' => $lastinfo['sr'],
- 'ljsr' => $lastinfo['ljsr'],
- 'ljjc' => $lastinfo['ljjc'],
- 'ljgjj' => $lastinfo['ljgjj'],
- 'ljylobx' => $lastinfo['ljylobx'],
- 'ljsybx' => $lastinfo['ljsybx'],
- 'ljylibx' => $lastinfo['ljylibx'],
- 'ljnj' => $lastinfo['ljnj'],
- 'ljznjy' => $lastinfo['ljznjy'],
- 'ljjxjy' => $lastinfo['ljjxjy'],
- 'ljzfdklx' => $lastinfo['ljzfdklx'],
- 'ljzfzj' => $lastinfo['ljzfzj'],
- 'ljsylr' => $lastinfo['ljsylr'],
- 'syjkbx' => $lastinfo['syjkbx'],
- 'ysylbx' => $lastinfo['ysylbx'],
- 'grbk' => $lastinfo['grbk'],
- 'jzjbjsb' => $lastinfo['jzjbjsb'],
- 'ljgzds' => $lastinfo['ljgzds'],
- 'qtkc' => $lastinfo['qtkc'],
- 'ljsqljxmhj' => $lastinfo['ljsqljxmhj'],
- 'ljykjse' => $lastinfo['ljykjse'],
- 'tax' => $lastinfo['tax'],
- 'zykcjze' => $lastinfo['zykcjze'],
- 'jmsebl' => $lastinfo['jmsebl'],
- 'extratax' => isset($lastinfo['extratax'])?$lastinfo['extratax']:0
- ];
- }
- $extratax = $data['extratax']; // 额外税差额
- unset($data['extratax']);
- // 使用上月数据计算个税
- $ljkc = $data['ljsr'] - $data['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
- if($ljkc > 0){
- $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
- $jmse = round($tax*$data['jmsebl'],2); // 减免税额
- $ljykjse = $data['ljykjse'] + $data['tax'];
- $tax = round($tax - $jmse - $ljykjse,2);
- $tax = $tax>0?$tax:0;
- $data['ljykjse'] = $ljykjse;
- }else{
- $tax = 0;
- }
- $data['tax'] = round($tax + $extratax,2);
- $data['salary'] = $should_total;
- $mssr = 0;
- $m = intval(date('m',strtotime($month.'-01')));
- if($jbsalary > 0){ // 发基本工资
- // 加累计值
- $mssr = 0;
- $special = Db::name('user_special')->where('user_id',$userId)->find();
- if($special){
- $mssr = $special['mssr'];
- if($m > 1){
- $data['ljjc'] += 5000; // 减除费用每月只能使用1次
- $data['ljgjj'] += $sbdata['gjjp'];
- $data['ljylobx'] += $sbdata['ylap'];
- $data['ljsybx'] += $sbdata['syep'];
- $data['ljylibx'] += $sbdata['ylip'];
- $data['ljnj'] += $sbdata['qynjp'];
- $data['ljznjy'] += $special['znjy'];
- $data['ljjxjy'] += $special['jxjy'];
- $data['ljzfdklx'] += $special['zfdklx'];
- $data['ljzfzj'] += $special['zfzj'];
- $data['ljsylr'] += $special['sylr'];
- $data['ljgzds'] += $special['gzds'];
- $data['syjkbx'] += $special['syjkbx'];
- $data['ysylbx'] += $special['ysylbx'];
- $data['grbk'] += $special['grbk'];
- $data['jzjbjsb'] += $special['nzjds'];
- $data['qtkc'] += $special['qtkc'];
- $data['zykcjze'] += $special['zykcjze'];
- }else{
- $data['ljjc'] = 5000;
- $data['ljgjj'] = $sbdata['gjjp'];
- $data['ljylobx'] = $sbdata['ylap'];
- $data['ljsybx'] = $sbdata['syep'];
- $data['ljylibx'] = $sbdata['ylip'];
- $data['ljnj'] = $sbdata['qynjp'];
- $data['ljznjy'] = $special['znjy'];
- $data['ljjxjy'] = $special['jxjy'];
- $data['ljzfdklx'] = $special['zfdklx'];
- $data['ljzfzj'] = $special['zfzj'];
- $data['ljsylr'] = $special['sylr'];
- $data['ljgzds'] = $special['gzds'];
- $data['ljykjse'] = 0;
- $data['ljsr'] = 0;
- $data['syjkbx'] = $special['syjkbx'];
- $data['ysylbx'] = $special['ysylbx'];
- $data['grbk'] = $special['grbk'];
- $data['jzjbjsb'] = $special['nzjds'];
- $data['qtkc'] = $special['qtkc'];
- $data['zykcjze'] = $special['zykcjze'];
- }
- $data['jmsebl'] = $special['jmsebl'];
- $data['mssr'] = $special['mssr'];
- }
- }else{
- if($m == 1 && $stype != 1){
- $data['ljjc'] = 0;
- $data['ljgjj'] = 0;
- $data['ljylobx'] = 0;
- $data['ljsybx'] = 0;
- $data['ljylibx'] = 0;
- $data['ljnj'] = 0;
- $data['ljznjy'] = 0;
- $data['ljjxjy'] = 0;
- $data['ljzfdklx'] = 0;
- $data['ljzfzj'] = 0;
- $data['ljsylr'] = 0;
- $data['ljgzds'] = 0;
- $data['ljykjse'] = 0;
- $data['ljsr'] = 0;
- $data['syjkbx'] = 0;
- $data['ysylbx'] = 0;
- $data['grbk'] = 0;
- $data['jzjbjsb'] = 0;
- $data['qtkc'] = 0;
- $data['zykcjze'] = 0;
- $data['ljsqljxmhj'] = 0;
- $data['jmsebl'] = 0;
- }
- $data['tax'] = 0; // 补助不算个税,只累计值
- }
- $data['mssr'] = $mssr;
- $data['sr'] = $should_total - $mssr;
- $data['ljsr'] += $data['sr'];
- $data['type'] = $stype;
- $data['ljsqljxmhj'] = $data['ljjc'] + $data['ljgjj'] + $data['ljylobx'] + $data['ljsybx'] + $data['ljylibx'] + $data['ljnj'] + $data['ljznjy']
- + $data['ljjxjy'] + $data['ljzfdklx'] + $data['ljzfzj'] + $data['ljsylr'] + $data['ljgzds'] + $data['syjkbx'] + $data['ysylbx']
- + $data['grbk'] + $data['jzjbjsb'] + $data['zykcjze'] + $data['qtkc'];
- return $data;
- }
- // 社保公积金
- private function getShebao($userId,$companyId,$info,$jbsalary){
- $bls = Db::name('user_bl')->select(); // 社保比例
- $base = Db::name('user_base')->where('user_id',$userId)->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,
- 'qynjp' => 0,
- 'qynjc' => 0,
- 'gjjp' => 0,
- 'gjjc' => 0,
- ];
- if(!$base||$jbsalary == 0){ //不是发基本工资就不扣缴社保
- 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'] == $companyId){
- switch ($val['name']){
- case 'gjj': // 住房公积金,企业和个人只保留整数,四舍五入
- $data['gjjp'] = round(calculate_money($base['gjj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']));
- $data['gjjc'] = round(calculate_money($base['gjj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']));
- 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']);
- }
- break;
- case 'yla':
- if($info['yla']) {
- if($val['company_id'] == 1){
- $data['ylap'] = calculate_money($base['ylnj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
- $data['ylac'] = calculate_money($base['ylnj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- } else {
- $data['ylap'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
- $data['ylac'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
- }
- }
- 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']);
- }
- 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']);
- }
- 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']);
- }
- break;
- }
- }
- }
- if(!$info['gjj']){
- $data['gjjp'] = 0;
- }
- if(!$info['qynj']){
- $data['qynjp'] = 0;
- }
- if(!$info['yla']){
- $data['ylap'] = 0;
- }
- if(!$info['sye']){
- $data['syep'] = 0;
- }
- if(!$info['yli']){
- $data['ylip'] = 0;
- }
- return $data;
- }
- // 获取劳务费个税
- public function getLaborTax($salary){
- // =ROUND(MAX((MIN((G4)*0.8,(G4)-800)*{0;20;30;40}%-1000*{0;0;2;7}),0),2) //计算公式
- if($salary*0.8 <= $salary - 800){
- $salary = $salary*0.8;
- }else{
- $salary = $salary - 800;
- }
- $arr = [
- $salary*0 - 0,
- $salary*0.2 - 0,
- $salary*0.3 - 2000,
- $salary*0.4 - 7000,
- 0
- ];
- $tax = round(max($arr),2);
- /*$taxs = Db::name('user_labor_tax')->select();
- // 获取减除费用
- $jcmoney = 0;
- $bl = 0;
- $ssmoney = 0;
- foreach ($taxs as $k=>$v){
- if($v['type'] == 1){
- if($v['max'] > 0){
- if($salary > $v['min'] && $salary <= $v['max']){
- $jcmoney = $v['bl'] == 1?$salary*$v['val']:$v['val'];
- }
- }else{
- if($salary > $v['min']){
- $jcmoney = $v['bl'] == 1?$salary*$v['val']:$v['val'];
- }
- }
- } else {
- if($v['max'] > 0){
- if($salary > $v['min'] && $salary <= $v['max']){
- $bl = $v['val'];
- $ssmoney = $v['val2'];
- }
- }else{
- if($salary > $v['min']){
- $bl = $v['val'];
- $ssmoney = $v['val2'];
- }
- }
- }
- }
- $sfsalary = round(($salary - $jcmoney)*$bl - $ssmoney,2);*/
- return $tax;
- }
- public function preAdd($data,$orgId){
- $uids = model('User')->getAdminUsers();
- if($uids){
- $map[] = ['u.id','not in',$uids];
- }
- $map[] = ['u.del','=',0];
- $map[] = ['r.type','=',3];
- $map[] = ['r.org_id','=',$orgId];
- $map[] = ['ui.pay_type','=',$data['type']];
- $lists = Db::name('user_info')
- ->alias('ui')
- ->join('user u','ui.user_id = u.id')
- ->join('user_roles ur','ur.user_id = u.id')
- ->join('roles r','ur.roles_id = r.id')
- ->where($map)
- ->field('u.phone,ui.company_id,ui.user_id,ui.name,ui.gender,ui.work_date,ui.company_id,ui.pwlevel,ui.type,ui.psid,ui.fixed_performance,ui.child')
- ->order('ui.sorts3 asc,u.id desc')
- ->select();
- if(!$lists){
- HelpHander::error('该工资类型下无可发放工资的人员');
- }
- $types = [];
- if($data['types']){
- $types = Db::name('user_subsidies')
- ->where('id','in',$data['types'])
- ->where('del',0)
- ->order('sorts asc,id asc')
- ->select();
- }
- foreach ($lists as $k=>$v){
- $lists[$k]['pwstatus'] = 0; // 薪级变化状态
- $lists[$k]['work_age'] = $v['work_date']?calculate_age($v['work_date']):0; // 工龄
- if($data['type'] == 5){
- $retired = Db::name('user_retired')->where('user_id',$v['user_id'])->find();
- $lists[$k]['bz'] = $retired?$retired['bz']:0;
- $lists[$k]['zysdbt'] = $retired?$retired['zysdbt']:0;
- $lists[$k]['fb'] = $retired?$retired['fb']:0;
- $lists[$k]['wybt'] = $retired?$retired['wybt']:0;
- }else{
- $lists[$k]['psname'] = '';
- $lists[$k]['psmonth'] = 0;
- $lists[$k]['psmoney'] = 0;
- if($lists[$k]['psid'] > 0){
- $psinfo = Db::name('post_salary')->where('id',$v['psid'])->find();
- if($psinfo){
- $lists[$k]['psname'] = $psinfo['job'];
- $lists[$k]['psmoney'] = $psinfo['money'];
- $lists[$k]['psmonth'] = $psinfo['month_performance'];
- if($lists[$k]['work_age'] > 25 && $psinfo['month_performance2'] > 0){
- $lists[$k]['psmonth'] = $psinfo['month_performance2'];
- }
- }
- }
- $lists[$k]['pwmoney'] = 0;
- if($v['type'] > 0 && $v['company_id'] > 0){
- $pay_wages = Db::name('pay_wages')
- ->where('company_id',$v['company_id'])
- ->where('type',$v['type'])
- ->value('content');
- $pay_wages = $pay_wages?json_decode($pay_wages,true):[];
- foreach ($pay_wages as $kk=>$vv){
- if($v['pwlevel'] == $vv['level']){
- $lists[$k]['pwmoney'] = $vv['money'];
- break;
- }
- }
- }
- if($data['type'] == 1||$data['type'] == 2){
- $lists[$k]['gjj'] = true;
- $lists[$k]['yla'] = true;
- $lists[$k]['sye'] = true;
- $lists[$k]['yli'] = true;
- $lists[$k]['qynj'] = true;
- }
- }
- $lastmonth = date('Y-m',strtotime($data['month'].'-01') - 1000);
- $btjson = null;
- $log = null;
- if($data['type'] != 5 && $data['continue'] == 1){ //延续上月,获取上月补贴,只获取基本工资的补贴项
- $log = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where('sr.month',$data['month'])
- ->where('srl.user_id',$v['user_id'])
- ->where('sr.status','in',[1,2])
- ->where('srl.gwgz','>',0)
- ->where('sr.del',0)
- ->field('srl.*')
- ->order('srl.id desc')
- ->find();
- if(!$log){
- $log = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where('sr.month',$lastmonth)
- ->where('srl.user_id',$v['user_id'])
- ->where('sr.status','in',[1,2])
- ->where('srl.gwgz','>',0)
- ->where('sr.del',0)
- ->field('srl.*')
- ->order('srl.id desc')
- ->find();
- }
- if(!empty($log) && ($data['type'] == 3||$data['type'] == 4)){ // 劳务基本工资项也要继承上次的
- $lists[$k]['psmoney'] = $log['gwgz'];
- $lists[$k]['pwmoney'] = $log['xjgz'];
- $lists[$k]['fixed_performance'] = $log['jcjx'];
- $lists[$k]['psmonth'] = $log['yjx'];
- }
- if(!empty($log) && ($v['pwlevel'] != $log['pwlevel'])){ // 有薪级变化
- $lists[$k]['pwstatus'] = 1;
- }
- $btjson = !empty($log)?$log['btjson']:null;
- /*$btjson = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where('sr.month',$data['month'])
- ->where('srl.user_id',$v['user_id'])
- ->where('sr.status','in',[1,2])
- ->where('srl.gwgz','>',0)
- ->where('sr.del',0)
- ->value('btjson');
- if(!$btjson){
- $btjson = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where('sr.month',$lastmonth)
- ->where('srl.user_id',$v['user_id'])
- ->where('sr.status','in',[1,2])
- ->where('srl.gwgz','>',0)
- ->where('sr.del',0)
- ->value('btjson');
- }*/
- }else if($data['type'] == 5 && $data['continue'] == 1){
- $btjson = Db::name('user_retired_salary')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where('sr.month',$data['month'])
- ->where('srl.user_id',$v['user_id'])
- ->where('sr.status','in',[1,2])
- ->where('sr.del',0)
- ->order('srl.id desc')
- ->value('btjson');
- if(!$btjson){
- $btjson = Db::name('user_retired_salary')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where('sr.month',$lastmonth)
- ->where('srl.user_id',$v['user_id'])
- ->where('sr.status','in',[1,2])
- ->where('sr.del',0)
- ->order('srl.id desc')
- ->value('btjson');
- }
- }
- $btjson = !empty($btjson)?json_decode($btjson,true):[];
- foreach ($types as $kk=>$vv){
- if(empty($btjson)){
- $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
- }else{
- $flag = 1;
- foreach ($btjson as $key=>$val){
- if($val['id'] == $vv['id']){
- $flag = 0;
- $lists[$k]['subsidies'.$vv['id']] = $val['money'];
- }
- }
- if($flag == 1){ // 无相同的分类
- $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
- }
- }
- }
- }
- return ['types' => $types,'list' => $lists];
- }
- // 重新计算
- public function reloadAdd($salaryRecordId,$uid,$orgId){
- $data = Db::name('salary_record')->where('id',$salaryRecordId)->find();
- $map[] = ['u.id','=',$uid];
- $map[] = ['u.del','=',0];
- $map[] = ['r.type','=',3];
- $map[] = ['r.org_id','=',$orgId];
- $map[] = ['ui.pay_type','=',$data['type']];
- $lists = Db::name('user_info')
- ->alias('ui')
- ->join('user u','ui.user_id = u.id')
- ->join('user_roles ur','ur.user_id = u.id')
- ->join('roles r','ur.roles_id = r.id')
- ->where($map)
- ->field('u.phone,ui.company_id,ui.user_id,ui.name,ui.gender,ui.work_date,ui.company_id,ui.pwlevel,ui.type,ui.psid,ui.fixed_performance,ui.child')
- ->order('ui.sorts3 asc,u.id desc')
- ->select();
- if(!$lists){
- HelpHander::error('该工资类型下无可发放工资的人员');
- }
- $types = [];
- if($data['types']){
- $types = Db::name('user_subsidies')->where('id','in',$data['types'])->where('del',0)->order('sorts asc')->select();
- }
- foreach ($lists as $k=>$v){
- $lists[$k]['work_age'] = $v['work_date']?calculate_age($v['work_date']):0; // 工龄
- if($data['type'] == 5){
- $retired = Db::name('user_retired')->where('user_id',$v['user_id'])->find();
- $lists[$k]['bz'] = $retired?$retired['bz']:0;
- $lists[$k]['zysdbt'] = $retired?$retired['zysdbt']:0;
- $lists[$k]['fb'] = $retired?$retired['fb']:0;
- $lists[$k]['wybt'] = $retired?$retired['wybt']:0;
- }else{
- $lists[$k]['psname'] = '';
- $lists[$k]['psmonth'] = 0;
- $lists[$k]['psmoney'] = 0;
- if($lists[$k]['psid'] > 0){
- $psinfo = Db::name('post_salary')->where('id',$v['psid'])->find();
- if($psinfo){
- $lists[$k]['psname'] = $psinfo['job'];
- $lists[$k]['psmoney'] = $psinfo['money'];
- $lists[$k]['psmonth'] = $psinfo['month_performance'];
- if($lists[$k]['work_age'] > 25 && $psinfo['month_performance2'] > 0){
- $lists[$k]['psmonth'] = $psinfo['month_performance2'];
- }
- }
- }
- $lists[$k]['pwmoney'] = 0;
- if($v['type'] > 0 && $v['company_id'] > 0){
- $pay_wages = Db::name('pay_wages')
- ->where('company_id',$v['company_id'])
- ->where('type',$v['type'])
- ->value('content');
- $pay_wages = $pay_wages?json_decode($pay_wages,true):[];
- foreach ($pay_wages as $kk=>$vv){
- if($v['pwlevel'] == $vv['level']){
- $lists[$k]['pwmoney'] = $vv['money'];
- break;
- }
- }
- }
- if($data['type'] == 1||$data['type'] == 2){
- $lists[$k]['gjj'] = true;
- $lists[$k]['yla'] = true;
- $lists[$k]['sye'] = true;
- $lists[$k]['yli'] = true;
- $lists[$k]['qynj'] = true;
- }
- }
- $btjson = null;
- $log = null;
- if($data['type'] != 5){
- $log = Db::name('salary_record_log')
- ->where('salary_record_id',$salaryRecordId)
- ->where('user_id',$uid)
- ->find();
- if(!empty($log)){
- $lists[$k]['psmoney'] = $log['gwgz'];
- $lists[$k]['pwmoney'] = $log['xjgz'];
- $lists[$k]['fixed_performance'] = $log['jcjx'];
- $lists[$k]['psmonth'] = $log['yjx'];
- }
- $btjson = !empty($log)?$log['btjson']:null;
- }else{
- $btjson = Db::name('user_retired_salary')
- ->where('salary_record_id',$salaryRecordId)
- ->where('user_id',$uid)
- ->value('btjson');
- }
- $btjson = !empty($btjson)?json_decode($btjson,true):[];
- foreach ($types as $kk=>$vv){
- if(empty($btjson)){
- $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
- }else{
- $flag = 1;
- foreach ($btjson as $key=>$val){
- if($val['id'] == $vv['id']){
- $flag = 0;
- $lists[$k]['subsidies'.$vv['id']] = $val['money'];
- }
- }
- if($flag == 1){ // 无相同的分类
- $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
- }
- }
- }
- // foreach ($types as $kk=>$vv){
- // $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
- // }
- }
- return ['types' => $types,'list' => $lists];
- }
- private function getBtByName($info,$psid,$age,$payType,$gender){
- $val = 0;
- if($payType == 1){ // 中心在编
- if($info['type'] == 1){
- if($info['gender'] == 0){
- $val = $info['money'];
- }else if($info['gender'] != 0&&$info['gender'] == $gender){
- $val = $info['money'];
- }
- }else if($info['type'] == 3){ // 检测上个月的记录
- $val = 0;
- } else {
- $dd = json_decode($info['values'],true);
- $dd = $dd?$dd:[];
- foreach ($dd as $k=>$v){
- if($v['id'] == $psid){
- if($v['money2']){
- if($age < 15){
- $val = $v['money1'];
- } else if ($age >= 15 && $age < 25){
- $val = $v['money2'];
- }else{
- $val = $v['money3'];
- }
- }else{
- $val = $v['money1'];
- }
- break;
- }
- }
- if($info['gender'] != 0&&$info['gender'] != $gender){
- $val = 0;
- }
- }
- }else{
- if($info['type'] == 1){
- if($info['gender'] == 0){
- $val = $info['money'];
- }else if($info['gender'] != 0&&$info['gender'] == $gender){
- $val = $info['money'];
- }
- }else if($info['type'] == 3){ // 检测上个月的记录
- $val = 0;
- }
- }
- return $val;
- }
- public function info($id){
- $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $types = [];
- if($info['types']){
- $types = Db::name('user_subsidies')->where('id','in',$info['types'])->select();
- }
- $info['types'] = $types;
- $info['sarlist'] = [];
- if($info['status'] == 0){ // 审核中
- $sarlist = Db::name('salary_apply_record')
- ->alias('sar')
- ->join('salary_apply sa','sa.id = sar.salary_apply_id')
- ->where('sar.salary_apply_id',$info['cur_node'])
- ->where('sar.salary_record_id',$info['id'])
- ->where('sar.del',0)
- ->field('sa.cate,sar.*')
- ->order('sar.id asc')
- ->select();
- foreach ($sarlist as $k=>$v){
- $sarlist[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
- }
- $info['sarlist'] = $sarlist;
- }
- // 计算实发总金额
- if($info['type'] == 5){
- $totalSalary = Db::name('user_retired_salary')->where('salary_record_id',$id)->sum('salary');
- }else{
- $totalSalary = Db::name('salary_record_log')->where('salary_record_id',$id)->sum('salary');
- }
- $info['total_salary'] = $totalSalary;
- //获取已审批人员
- $alist = Db::name('salary_apply_record')
- ->alias('sar')
- ->join('salary_record sr','sr.id = sar.salary_record_id')
- ->join('user_info ui','ui.user_id = sar.user_id')
- ->where('sar.salary_record_id',$id)
- ->where('sar.status',1)
- ->where('sar.del',0)
- ->field('sar.*,ui.name as user_name')
- ->order('sar.id desc')
- ->select();
- $info['alist'] = $alist?$alist:[];
- return $info;
- }
- public function lists($page,$size,$month,$type){
- if($month != ''){
- $map[] = ['month','=',$month];
- }
- if($type >= 0){
- $map[] = ['type','=',$type];
- }
- $map[] = ['del','=',0];
- $map = empty($map)?true:$map;
- $lists = Db::name('salary_record')
- ->where($map)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- foreach ($lists as $k=>$v){
- // 计算实发总金额
- if($v['type'] == 5){
- $totalSalary = Db::name('user_retired_salary')->where('salary_record_id',$v['id'])->sum('salary');
- }else{
- $totalSalary = Db::name('salary_record_log')->where('salary_record_id',$v['id'])->sum('salary');
- }
- $lists[$k]['total_salary'] = $totalSalary;
- }
- $total = Db::name('salary_record')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- public function payinfo($id){
- $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $info['user_name'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
- if($info['status'] == 1||$info['status'] == 2){
- $last_time = Db::name('salary_apply_record')
- ->where('salary_record_id',$info['id'])
- ->where('status',1)
- ->where('del',0)
- ->order('id desc')
- ->value('update_time');
- $info['last_time'] = date('Y年m月d日',strtotime($last_time));
- $rsinfo = Db::name('salary_apply_record')
- ->alias('sar')
- ->join('salary_apply sa','sa.id = sar.salary_apply_id')
- ->join('user_info ui','ui.user_id = sar.user_id')
- ->where('sar.del',0)
- ->where('sa.cate',2)
- ->value('ui.name');
- $info['rsl_user_name'] = $rsinfo?$rsinfo:'';
- $cwinfo = Db::name('salary_apply_record')
- ->alias('sar')
- ->join('salary_apply sa','sa.id = sar.salary_apply_id')
- ->join('user_info ui','ui.user_id = sar.user_id')
- ->where('sar.del',0)
- ->where('sa.cate',3)
- ->value('ui.name');
- $info['kj_user_name'] = $cwinfo?$cwinfo:'';
- $cwlinfo = Db::name('salary_apply_record')
- ->alias('sar')
- ->join('salary_apply sa','sa.id = sar.salary_apply_id')
- ->join('user_info ui','ui.user_id = sar.user_id')
- ->where('sar.del',0)
- ->where('sa.cate',4)
- ->value('ui.name');
- $info['cwl_user_name'] = $cwlinfo?$cwlinfo:'';
- }else{
- $info['last_time'] = '';
- $info['kj_user_name'] = '';
- $info['rsl_user_name'] = '';
- $info['cwl_user_name'] = '';
- }
- if($info['type'] == 5){
- $totalSalary = Db::name('user_retired_salary')->where('salary_record_id',$id)->sum('salary');
- }else{
- $totalSalary = Db::name('salary_record_log')->where('salary_record_id',$id)->sum('salary');
- }
- $info['total_salary'] = $totalSalary;
- return $info;
- }
- public function del($id){
- $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- // if($info['status'] == 2){
- // HelpHander::error('该状态不能删除');
- // }
- $ret = Db::name('salary_record')->where('id',$id)->setField('del',1);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- public function send($id){
- $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 3){
- HelpHander::error('该状态不能重新发放');
- }
- $ret = Db::name('salary_record')->where('id',$id)->setField('status',0);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- // 工资列表
- public function listLog($page,$size,$name,$id){
- $info = $this->info($id);
- if($info['type'] == 5){ // 退休
- $data = model('UserRetiredSalary')->lists($page,$size,$name,$info['id']);
- } else {
- if($name != ''){
- $map[] = ['ui.name','like','%'.$name.'%'];
- }
- $map[] = ['ur.salary_record_id','=',$info['id']];
- $lists = Db::name('salary_record_log')
- ->alias('ur')
- ->join('user_info ui','ui.user_id = ur.user_id')
- ->where($map)
- ->field('ur.*,ui.name')
- ->page($page,$size)
- ->order('ui.sorts3 asc,ur.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $btjson = $v['btjson']?json_decode($v['btjson'],true):[];
- $kcmoney = 0;
- foreach ($btjson as $kk=>$vv){
- $lists[$k]['subsidies'.$vv['id']] = $vv['money'];
- if($vv['cate'] == 2){ // 扣除项
- $kcmoney += $vv['money'];
- }
- }
- $lists[$k]['dktotal'] = round($v['sbgjj'] + $v['tax'] + $kcmoney,2);
- $depjob = Db::name('user_job')
- ->alias('uj')
- ->join('job j','j.id = uj.job_id')
- ->join('dep d','d.id = j.dep_id')
- ->where('uj.user_id',$v['user_id'])
- ->field('d.name as dep_name,j.name as job_name')
- ->find();
- $lists[$k]['dep_name'] = $depjob?$depjob['dep_name']:'';
- //职级
- $lists[$k]['job_name'] = Db::name('post_salary')->where('id',$v['psid'])->value('title');
- }
- $total = Db::name('salary_record_log')
- ->alias('ur')
- ->join('user_info ui','ui.user_id = ur.user_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- }
- $data['info'] = $info;
- return $data;
- }
- // 个税累计列表
- public function listSpecial($page,$size,$name,$id){
- $info = $this->info($id);
- if($info){
- if($name != ''){
- $map[] = ['ui.name','like','%'.$name.'%'];
- }
- $map[] = ['ur.salary_record_id','=',$info['id']];
- $lists = Db::name('salary_record_special')
- ->alias('ur')
- ->join('user_info ui','ui.user_id = ur.user_id')
- ->where($map)
- ->field('ur.*,ui.name,ui.pwlevel')
- ->page($page,$size)
- ->order('ui.sorts3 asc,ur.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $ljkc = $v['ljsr'] - $v['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
- $lists[$k]['ljkc'] = round($ljkc,2);
- $lists[$k]['ljyykyjsr'] = 0;
- $lists[$k]['jmsr'] = 0;
- $ljykjse = $v['ljykjse'] + $v['tax'];
- $lists[$k]['ljykjse'] = round($ljykjse,2);
- if($ljkc > 0){
- $tax = $this->getYkyjTax($ljkc); // 累计应预扣预缴税额
- $lists[$k]['ljyykyjsr'] = round($tax,2);
- $jmse = round($tax*$v['jmsebl'],2); // 减免税额
- $lists[$k]['jmsr'] = $jmse;
- $tax = round($tax - $jmse - $ljykjse,2);
- $tax = $tax>0?$tax:0;
- }else{
- $tax = 0;
- }
- $lists[$k]['nextTax'] = $tax;
- }
- $total = Db::name('salary_record_special')
- ->alias('ur')
- ->join('user_info ui','ui.user_id = ur.user_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- }
- $data['info'] = $info;
- return $data;
- }
- // 导出
- public function exportListLog($name,$id){
- $info = $this->info($id);
- if($info['type'] == 5){ // 退休
- $data = model('UserRetiredSalary')->lists(1,100000,$name,$info['id']);
- } else {
- if($name != ''){
- $map[] = ['ui.name','like','%'.$name.'%'];
- }
- $map[] = ['ur.salary_record_id','=',$info['id']];
- $lists = Db::name('salary_record_log')
- ->alias('ur')
- ->join('user_info ui','ui.user_id = ur.user_id')
- ->where($map)
- ->field('ur.*,ui.name,ui.pwlevel')
- ->order('ui.sorts3 asc,ur.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $btjson = $v['btjson']?json_decode($v['btjson'],true):[];
- $kcmoney = 0;
- foreach ($btjson as $kk=>$vv){
- $lists[$k]['subsidies'.$vv['id']] = $vv['money'];
- if($vv['cate'] == 2){
- $kcmoney += $vv['money'];
- }
- }
- $lists[$k]['dktotal'] = $v['sbgjj'] + $v['tax'] + $kcmoney;
- $depjob = Db::name('user_job')
- ->alias('uj')
- ->join('job j','j.id = uj.job_id')
- ->join('dep d','d.id = j.dep_id')
- ->where('uj.user_id',$v['user_id'])
- ->field('d.name as dep_name,j.name as job_name')
- ->find();
- $lists[$k]['dep_name'] = $depjob?$depjob['dep_name']:'';
- //职级
- $lists[$k]['job_name'] = Db::name('post_salary')->where('id',$v['psid'])->value('title');
- }
- if($lists){
- $darr = $lists[0];
- foreach ($darr as $k=>$v){
- $darr[$k] = '';
- if($k == 'name'){
- $darr[$k] = '合计';
- }
- if($info['type'] == 5){
- $arrs = ['bz','zysdbt','fb','wybt'];
- if(in_array($k,$arrs)){
- $val = 0;
- foreach ($lists as $kk=>$vv){
- $val += $vv[$k];
- }
- $darr[$k] = round($val,2);
- }
- }else{
- $arrs = ['gwgz','xjgz','jcjx','yjx','should_total','child','dktotal'];
- if(in_array($k,$arrs)){
- $val = 0;
- foreach ($lists as $kk=>$vv){
- $val += $vv[$k];
- }
- $darr[$k] = round($val,2);
- }
- }
- $arrs = ['salary'];
- foreach ($info['types'] as $kkk=>$vvv){
- $arrs[] = "subsidies".$vvv['id'];
- }
- if(in_array($k,$arrs)){
- $val = 0;
- foreach ($lists as $kk=>$vv){
- $val += $vv[$k];
- }
- $darr[$k] = round($val,2);
- }
- if($info['type'] == 1||$info['type'] == 2){
- $arrs = ['sbgjj','tax'];
- if(in_array($k,$arrs)){
- $val = 0;
- foreach ($lists as $kk=>$vv){
- $val += $vv[$k];
- }
- $darr[$k] = round($val,2);
- }
- }
- if($info['type'] == 3||$info['type'] == 4){
- $arrs = ['tax'];
- if(in_array($k,$arrs)){
- $val = 0;
- foreach ($lists as $kk=>$vv){
- $val += $vv[$k];
- }
- $darr[$k] = round($val,2);
- }
- }
- }
- $lists[] = $darr;
- }
- //转成数字格式
- $extra = ['dep_name','name','job_name'];
- foreach ($lists as $k=>$v){
- foreach ($v as $kk=>$vv){
- if(!in_array($kk,$extra)){
- $v[$kk] = (float)$vv;
- }
- }
- $lists[$k] = $v;
- }
- $data = [
- 'total' => count($lists),
- 'list' => $lists?$lists:[]
- ];
- }
- $columns = [
- ["title" => "姓名","key" => "name"]
- ];
- if($info['type'] == 5){
- $columns[] = ["title" => "补助","key" => "bz"];
- $columns[] = ["title" => "中央适当补贴","key" => "zysdbt"];
- $columns[] = ["title" => "房补","key" => "fb"];
- $columns[] = ["title" => "物业补贴","key" => "wybt"];
- }else{
- $columns[] = ["title" => "部门","key" => "depName"];
- $columns[] = ["title" => "职级","key" => "jobName"];
- $columns[] = ["title" => "薪级","key" => "pwlevel"];
- $columns[] = ["title" => "岗位工资","key" => "gwgz"];
- $columns[] = ["title" => "薪级工资","key" => "xjgz"];
- $columns[] = ["title" => "基础绩效","key" => "jcjx"];
- $columns[] = ["title" => "月绩效","key" => "yjx"];
- }
- foreach ($info['types'] as $k=>$v){
- if($v['cate'] == 1){
- $columns[] = ["title" => $v['title'],"key" => "subsidies".$v['id']];
- }
- }
- if($info['type'] != 5){
- $columns[] = ["title" => "应发工资","key" => "shouldTotal"];
- }
- foreach ($info['types'] as $k=>$v){
- if($v['cate'] != 1){
- $columns[] = ["title" => $v['title'],"key" => "subsidies".$v['id']];
- }
- }
- if($info['type'] == 1||$info['type'] == 2){
- $columns[] = ["title" => "社保公积金代扣","key" => "sbgjj"];
- }
- if($info['type'] == 3||$info['type'] == 4){
- $columns[] = ["title" => "个税","key" => "tax"];
- }
- if($info['type'] == 1||$info['type'] == 2){
- $columns[] = ["title" => "个税","key" => "tax"];
- }
- if($info['type'] != 5){
- $columns[] = ["title" => "代扣合计","key" => "dktotal"];
- }
- $columns[] = ["title" => "实发工资","key" => "salary"];
- $data = [
- 'columns' => $columns,
- 'list' => $data['list']
- ];
- return $data;
- }
- public function mySalary($page,$size,$month,$userId){
- if($month != ''){
- $map[] = ['month','=',$month];
- }
- $map[] = ['user_id','=',$userId];
- $lists = Db::name('common_salary')
- ->where($map)
- ->page($page,$size)
- ->order('month desc,id desc')
- ->select();
- foreach ($lists as $k=>$v){
- if($v['type'] == 1){
- $lists[$k]['type_name'] = '中心编制';
- }else if($v['type'] == 2){
- $lists[$k]['type_name'] = '新华聘用';
- }else if($v['type'] == 3){
- $lists[$k]['type_name'] = '中心劳务';
- }else if($v['type'] == 4){
- $lists[$k]['type_name'] = '新华劳务';
- }else{
- $lists[$k]['type_name'] = '中心退休';
- }
- }
- $total = Db::name('common_salary')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- public function salaryDetail($id,$userId){
- $info = Db::name('common_salary')->where('id',$id)->where('user_id',$userId)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['type'] == 5){ // 退休
- $data = Db::name('user_retired_salary')->where('salary_record_id',$id)->where('user_id',$userId)->find();
- }else{ // 其他
- $data = Db::name('salary_record_log')->where('salary_record_id',$id)->where('user_id',$userId)->find();
- }
- if(!$data){
- HelpHander::error('工资不存在');
- }
- $data['pay_type'] = $info['type'];
- $data['btjson'] = $data['btjson']?json_decode($data['btjson'],true):[];
- $data['gjj'] = '0';
- $data['yla'] = '0';
- $data['qynj'] = '0';
- $data['yli'] = '0';
- $data['sye'] = '0';
- $sbdata = Db::name('salary_record_sb')->where('salary_record_id',$id)->where('user_id',$userId)->find();
- if($sbdata){
- $data['gjj'] = $sbdata['gjjp'];
- $data['yla'] = $sbdata['ylap'];
- $data['qynj'] = $sbdata['qynjp'];
- $data['yli'] = $sbdata['ylip'];
- $data['sye'] = $sbdata['syep'];
- }
- return $data;
- }
- public function sbinfo($id,$uid){
- $info = Db::name('salary_record_sb')
- ->alias('srs')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where('srs.salary_record_id',$id)
- ->where('srs.user_id',$uid)
- ->field('sr.type,srs.*')
- ->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- return $info;
- }
- public function taxinfo($id,$uid){
- $info = Db::name('salary_record_special')
- ->alias('srs')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where('srs.salary_record_id',$id)
- ->where('srs.user_id',$uid)
- ->field('sr.type,srs.*')
- ->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- return $info;
- }
- // 同意 或签
- public function agree($id,$userId,$orgId){
- $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 0){
- HelpHander::error('无权限操作');
- }
- $uinfo = Db::name('salary_apply_record')
- ->where('salary_record_id',$id)
- ->where('user_id',$userId)
- ->where('salary_apply_id',$info['cur_node'])
- ->where('status',0)
- ->find();
- if(!$uinfo){
- HelpHander::error('无权限操作');
- }
- $apply = json_decode($info['apply'],true);
- Db::startTrans();
- try{
- $res = Db::name('salary_apply_record')->where('id',$uinfo['id'])->update(['status'=>1,'update_time'=>date('Y-m-d H:i:s')]);
- if(!$res){
- \exception('操作失败');
- }
- if($info['cur_node'] == $apply[count($apply)-1]['id']){ // 最后一个节点
- $res2 = Db::name('salary_record')->where('id',$id)->setField('status',1);
- if(!$res2){
- \exception('操作失败');
- }
- }else{
- // 获取下一节点
- $next = [];
- foreach ($apply as $k=>$v){
- if($v['id'] == $info['cur_node']){
- $next = $apply[$k+1];
- break;
- }
- }
- $res2 = Db::name('salary_record')->where('id',$id)->setField('cur_node',$next['id']);
- if(!$res2){
- \exception('操作失败');
- }
- // 生成审核记录
- $uids = explode(',',$next['userids']);
- foreach ($uids as $k=>$v){
- $sd = [
- 'salary_record_id' => $id,
- 'user_id' => $v,
- 'status' => 0,
- 'create_time' => date('Y-m-d H:i:s'),
- 'salary_apply_id' => $next['id']
- ];
- $ret = Db::name('salary_apply_record')->insertGetId($sd);
- if(!$ret){
- \exception('操作失败');
- }
- model('Message')->add(3,$id,12,$v,$orgId,'工资流程待您的审核确认');
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error = '操作失败';
- return false;
- }
- return true;
- }
- // 打回
- public function disagree($id,$userId,$orgId){
- $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 0){
- HelpHander::error('无权限操作');
- }
- $uinfo = Db::name('salary_apply_record')
- ->where('salary_record_id',$id)
- ->where('user_id',$userId)
- ->where('salary_apply_id',$info['cur_node'])
- ->where('status',0)
- ->find();
- if(!$uinfo){
- HelpHander::error('无权限操作');
- }
- Db::startTrans();
- try{
- $res = Db::name('salary_apply_record')->where('id',$uinfo['id'])->update(['status'=>2,'update_time'=>date('Y-m-d H:i:s')]);
- if(!$res){
- \exception('操作失败');
- }
- $res2 = Db::name('salary_record')->where('id',$id)->setField('status',3);
- if(!$res2){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error = '操作失败';
- return false;
- }
- return true;
- }
- // 会签版本
- public function agreeold($id,$userId,$orgId){
- $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 0){
- HelpHander::error('无权限操作');
- }
- $uinfo = Db::name('salary_apply_record')
- ->where('salary_record_id',$id)
- ->where('user_id',$userId)
- ->where('salary_apply_id',$info['cur_node'])
- ->where('status',0)
- ->find();
- if(!$uinfo){
- HelpHander::error('无权限操作');
- }
- // 检查是否有除自己之外的未处理的人
- $others = Db::name('salary_apply_record')
- ->where('salary_record_id',$id)
- ->where('user_id','<>',$userId)
- ->where('salary_apply_id',$info['cur_node'])
- ->where('status',0)
- ->find();
- $apply = json_decode($info['apply'],true);
- Db::startTrans();
- try{
- $res = Db::name('salary_apply_record')->where('id',$uinfo['id'])->update(['status'=>1,'update_time'=>date('Y-m-d H:i:s')]);
- if(!$res){
- \exception('操作失败');
- }
- if(!$others){ // 没有未处理的人了,走下一节点
- if($info['cur_node'] == $apply[count($apply)-1]['id']){ // 最后一个节点
- $res2 = Db::name('salary_record')->where('id',$id)->setField('status',1);
- if(!$res2){
- \exception('操作失败');
- }
- }else{
- // 获取下一节点
- $next = [];
- foreach ($apply as $k=>$v){
- if($v['id'] == $info['cur_node']){
- $next = $apply[$k+1];
- break;
- }
- }
- $res2 = Db::name('salary_record')->where('id',$id)->setField('cur_node',$next['id']);
- if(!$res2){
- \exception('操作失败');
- }
- // 生成审核记录
- $uids = explode(',',$next['userids']);
- foreach ($uids as $k=>$v){
- $sd = [
- 'salary_record_id' => $id,
- 'user_id' => $v,
- 'status' => 0,
- 'create_time' => date('Y-m-d H:i:s'),
- 'salary_apply_id' => $next['id']
- ];
- $ret = Db::name('salary_apply_record')->insertGetId($sd);
- if(!$ret){
- \exception('操作失败');
- }
- model('Message')->add(3,$id,12,$v,$orgId,'工资流程待您的审核确认');
- }
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- return false;
- }
- return true;
- }
- public function confirm($id,$userId){
- $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 1){
- HelpHander::error('无权限操作');
- }
- $res = Db::name('salary_record')->where('id',$id)->update([
- 'cw_time' => date('Y-m-d H:i:s'),
- 'cw_user_id' => $userId,
- 'status' => 2
- ]);
- if(!$res){
- HelpHander::error('操作失败');
- }
- return true;
- }
- // 工资统计
- public function financeSalaryStructure($type,$cate){
- $map[] = ['b.type','=',$cate];
- $map[] = ['b.status','=',2];
- $map[] = ['b.del','=',0];
- if($type == 1){
- $map[] = ['b.month','=',date('Y-m')];
- }else {// 当年
- $year = date('Y');
- $startMonth = $year . '-01';
- $endMonth = ($year + 1) . '-01';
- $map[] = ['b.month','>=',$startMonth];
- $map[] = ['b.month','<',$endMonth];
- }
- if($cate == 5){ // 中心养老
- $total = Db::name('user_retired_salary')
- ->alias('a')
- ->join('salary_record b','b.id = a.salary_record_id')
- ->where($map)
- ->sum('salary');
- }else{// 其他
- $total = Db::name('salary_record_log')
- ->alias('a')
- ->join('salary_record b','b.id = a.salary_record_id')
- ->where($map)
- ->sum('salary');
- }
- return $total;
- }
- public function taxlist($page,$size,$month,$name,$type){
- if($month != ''){
- $map[] = ['sr.month','=',$month];
- }
- if($name != ''){
- $map[] = ['ui.name','=',$name];
- }
- if(in_array($type,[1,2])){
- $map[] = ['sr.type','=',$type];
- }
- $map[] = ['sr.del','=',0];
- $map = empty($map)?true:$map;
- $lists = Db::name('salary_record_special')
- ->alias('srs')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->where($map)
- ->page($page,$size)
- ->order('sr.id desc,ui.sorts3 asc')
- ->field('srs.*,ui.name,sr.month')
- ->select();
- foreach ($lists as $k=>$v){
- $ljkc = $v['ljsr'] - $v['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
- $lists[$k]['ljkc'] = round($ljkc,2);
- $lists[$k]['ljyykyjsr'] = 0;
- $lists[$k]['jmsr'] = 0;
- $ljykjse = $v['ljykjse'] + $v['tax'];
- $lists[$k]['ljykjse'] = round($ljykjse,2);
- if($ljkc > 0){
- $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
- $lists[$k]['ljyykyjsr'] = round($tax,2);
- $jmse = round($tax*$v['jmsebl'],2); // 减免税额
- $lists[$k]['jmsr'] = $jmse;
- $tax = round($tax - $jmse - $ljykjse,2);
- $tax = $tax>0?$tax:0;
- }else{
- $tax = 0;
- }
- $lists[$k]['nextTax'] = $tax;
- }
- $total = Db::name('salary_record_special')
- ->alias('srs')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- // 个税累计列表
- public function ntaxlist($month,$name,$type){
- if($month){
- $map[] = ['sr.month','=',$month];
- }else{
- return [];
- }
- if($name != ''){
- $map[] = ['ui.name','=',$name];
- }
- if(in_array($type,[1,2])){
- $map[] = ['sr.type','=',$type];
- }
- $map[] = ['sr.del','=',0];
- $map = empty($map)?true:$map;
- $lists = Db::name('salary_record_special')
- ->alias('srs')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->where($map)
- ->group('sr.month,srs.user_id')
- ->field('srs.*,ui.name,sr.month')
- ->order('sr.id desc,ui.sorts3 asc')
- ->select();
- foreach ($lists as $k=>$v){
- $children = Db::name('salary_record_special')
- ->alias('srs')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->where('sr.month',$v['month'])
- ->where('srs.user_id',$v['user_id'])
- ->where('sr.del',0)
- ->field('srs.*,ui.name,sr.month')
- ->order('srs.id desc')
- ->select();
- $nsalary = 0;
- $nmssr = 0;
- $nsr = 0;
- foreach ($children as $kk=>$vv){
- $ljkc = $vv['ljsr'] - $vv['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
- $children[$kk]['ljkc'] = round($ljkc,2);
- $children[$kk]['ljyykyjsr'] = 0;
- $children[$kk]['jmsr'] = 0;
- $ljykjse = $vv['ljykjse'] + $vv['tax'];
- $children[$kk]['ljykjse'] = round($ljykjse,2);
- if($ljkc > 0){
- $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
- $children[$kk]['ljyykyjsr'] = round($tax,2);
- $jmse = round($tax*$vv['jmsebl'],2); // 减免税额
- $children[$kk]['jmsr'] = $jmse;
- $tax = round($tax - $jmse - $ljykjse,2);
- $tax = $tax>0?$tax:0;
- }else{
- $tax = 0;
- }
- $children[$kk]['nextTax'] = $tax;
- $children[$kk]['child'] = 1;
- $nsalary += $vv['salary'];
- $nmssr += $vv['mssr'];
- $nsr += $vv['sr'];
- $children[$kk]['nsalary'] = $vv['salary'];
- $children[$kk]['nmssr'] = $vv['mssr'];
- $children[$kk]['nsr'] = $vv['sr'];
- $children[$kk]['unique'] = 'C'.$vv['id'];
- }
- $lists[$k] = $children[0];
- $lists[$k]['child'] = 0;
- $lists[$k]['nsalary'] = round($nsalary,2);
- $lists[$k]['nmssr'] = round($nmssr,2);
- $lists[$k]['nsr'] = round($nsr,2);
- $lists[$k]['unique'] = $children[0]['id'];
- if(count($children) >= 2){
- $lists[$k]['children'] = $children;
- }
- }
- return $lists?$lists:[];
- }
- public function ntaxexport($month,$name,$type){
- if($month != ''){
- $map[] = ['sr.month','=',$month];
- }
- if($name != ''){
- $map[] = ['ui.name','=',$name];
- }
- if(in_array($type,[1,2])){
- $map[] = ['sr.type','=',$type];
- }
- $map[] = ['sr.del','=',0];
- $map = empty($map)?true:$map;
- $lists = Db::name('salary_record_special')
- ->alias('srs')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->where($map)
- ->group('sr.month,srs.user_id')
- ->field('srs.*,ui.name,sr.month')
- ->order('sr.id desc,ui.sorts3 asc')
- ->select();
- foreach ($lists as $k=>$v){
- $children = Db::name('salary_record_special')
- ->alias('srs')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->where('sr.month',$v['month'])
- ->where('srs.user_id',$v['user_id'])
- ->where('sr.del',0)
- ->field('srs.*,ui.name,sr.month')
- ->order('srs.id desc')
- ->select();
- $nsalary = 0;
- $nmssr = 0;
- $nsr = 0;
- foreach ($children as $kk=>$vv){
- $ljkc = $vv['ljsr'] - $vv['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
- $children[$kk]['ljkc'] = round($ljkc,2);
- $children[$kk]['ljyykyjsr'] = 0;
- $children[$kk]['jmsr'] = 0;
- $ljykjse = $vv['ljykjse'] + $vv['tax'];
- $children[$kk]['ljykjse'] = round($ljykjse,2);
- if($ljkc > 0){
- $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
- $children[$kk]['ljyykyjsr'] = round($tax,2);
- $jmse = round($tax*$vv['jmsebl'],2); // 减免税额
- $children[$kk]['jmsr'] = $jmse;
- $tax = round($tax - $jmse - $ljykjse,2);
- $tax = $tax>0?$tax:0;
- }else{
- $tax = 0;
- }
- $children[$kk]['nextTax'] = $tax;
- $children[$kk]['child'] = 1;
- $nsalary += $vv['salary'];
- $nmssr += $vv['mssr'];
- $nsr += $vv['sr'];
- $children[$kk]['nsalary'] = $vv['salary'];
- $children[$kk]['nmssr'] = $vv['mssr'];
- $children[$kk]['nsr'] = $vv['sr'];
- $children[$kk]['unique'] = 'C'.$vv['id'];
- }
- $lists[$k] = $children[0];
- $lists[$k]['child'] = 0;
- $lists[$k]['nsalary'] = round($nsalary,2);
- $lists[$k]['nmssr'] = round($nmssr,2);
- $lists[$k]['nsr'] = round($nsr,2);
- $lists[$k]['unique'] = $children[0]['id'];
- if(count($children) >= 2){
- $lists[$k]['children'] = $children;
- }
- }
- //转成数字格式
- $extra = ['month','name'];
- foreach ($lists as $k=>$v){
- foreach ($v as $kk=>$vv){
- if(!in_array($kk,$extra)){
- $v[$kk] = (float)$vv;
- }
- }
- $lists[$k] = $v;
- }
- $columns[] = ["title" => "月份","key" => "month"];
- $columns[] = ["title" => "姓名","key" => "name"];
- $columns[] = ["title" => "应发工资","key" => "nsalary"];
- $columns[] = ["title" => "免税收入","key" => "nmssr"];
- $columns[] = ["title" => "应税收入","key" => "nsr"];
- $columns[] = ["title" => "累计应税收入","key" => "ljsr"];
- $columns[] = ["title" => "累计减除费用","key" => "ljjc"];
- $columns[] = ["title" => "累计公积金","key" => "ljgjj"];
- $columns[] = ["title" => "累计养老保险","key" => "ljylobx"];
- $columns[] = ["title" => "累计失业保险","key" => "ljsybx"];
- $columns[] = ["title" => "累计医疗保险","key" => "ljylibx"];
- $columns[] = ["title" => "累计职业年金","key" => "ljnj"];
- $columns[] = ["title" => "累计子女教育","key" => "ljznjy"];
- $columns[] = ["title" => "累计继续教育","key" => "ljjxjy"];
- $columns[] = ["title" => "累计住房贷款利息","key" => "ljzfdklx"];
- $columns[] = ["title" => "累计住房租金","key" => "ljzfzj"];
- $columns[] = ["title" => "累计赡养老人","key" => "ljsylr"];
- $columns[] = ["title" => "商业健康保险","key" => "syjkbx"];
- $columns[] = ["title" => "延税养老保险","key" => "ysylbx"];
- $columns[] = ["title" => "个人补扣/补交社保年金","key" => "grbk"];
- $columns[] = ["title" => "年终奖补交社保年金","key" => "jzjbjsb"];
- $columns[] = ["title" => "工资中补交社保年金","key" => "ljgzds"];
- $columns[] = ["title" => "累计准予扣除的捐赠额","key" => "zykcjze"];
- $columns[] = ["title" => "累计其他扣除","key" => "qtkc"];
- $columns[] = ["title" => "税前扣除项目合计","key" => "ljsqljxmhj"];
- $columns[] = ["title" => "累计预扣预缴应纳税所得额","key" => "ljkc"];
- $columns[] = ["title" => "累计应预扣预缴税额","key" => "ljyykyjsr"];
- $columns[] = ["title" => "减免税额","key" => "jmsr"];
- $columns[] = ["title" => "累计已扣缴税额","key" => "ljykjse"];
- $columns[] = ["title" => "下月应扣个人所得税税额","key" => "nextTax"];
- $data = [
- 'columns' => $columns,
- 'list' => $lists
- ];
- return $data;
- }
- // 根据累计预扣预缴应纳税所得额获取累计应预扣预缴税额
- public function getYkyjTax($ljkc){
- $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); // 累计应预扣预缴税额
- return $tax;
- }
- public function taxEdit(){
- $id = input('id/d',0);
- $info = Db::name('salary_record_special')->where('id',$id)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- $oldljsr = $info['ljsr'] - $info['sr'];
- $data = [
- 'id' => input('id/d',0),
- 'salary' => input('salary/f',0),
- 'mssr' => input('mssr/f',0),
- // 'sr' => input('sr/f',0),
- // 'ljsr' => input('ljsr/f',0),
- 'ljjc' => input('ljjc/f',0),
- 'ljgjj' => input('ljgjj/f',0),
- 'ljylobx' => input('ljylobx/f',0),
- 'ljylibx' => input('ljylibx/f',0),
- 'ljsybx' => input('ljsybx/f',0),
- 'ljnj' => input('ljnj/f',0),
- 'ljznjy' => input('ljznjy/f',0),
- 'ljjxjy' => input('ljjxjy/f',0),
- 'ljzfdklx' => input('ljzfdklx/f',0),
- 'ljzfzj' => input('ljzfzj/f',0),
- 'ljsylr' => input('ljsylr/f',0),
- 'syjkbx' => input('syjkbx/f',0),
- 'ysylbx' => input('ysylbx/f',0),
- 'grbk' => input('grbk/f',0),
- 'jzjbjsb' => input('jzjbjsb/f',0),
- 'ljgzds' => input('ljgzds/f',0),
- 'qtkc' => input('qtkc/f',0),
- // 'ljsqljxmhj' => input('ljsqljxmhj/f',0),
- 'ljykjse' => input('ljykjse/f',0),
- 'tax' => input('tax/f',0),
- 'zykcjze' => input('zykcjze/f',0),
- 'jmsebl' => input('jmsebl/f',0),
- ];
- $data['sr'] = round($data['salary'] - $data['mssr'],2);
- $data['ljsr'] = round($oldljsr + $data['sr'],2);
- $data['ljsqljxmhj'] = $data['ljjc'] + $data['ljgjj'] + $data['ljylobx'] + $data['ljsybx'] + $data['ljylibx'] + $data['ljnj'] + $data['ljznjy']
- + $data['ljjxjy'] + $data['ljzfdklx'] + $data['ljzfzj'] + $data['ljsylr'] + $data['ljgzds'] + $data['syjkbx'] + $data['ysylbx']
- + $data['grbk'] + $data['jzjbjsb'] + $data['zykcjze'] + $data['qtkc'];
- $data['ljsqljxmhj'] = round($data['ljsqljxmhj'],2);
- $ret = Db::name('salary_record_special')->where('id',$data['id'])->update($data);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- // 社保公积金
- public function sblist($month,$name,$type){
- if($month){
- $map[] = ['sr.month','=',$month];
- }else{
- return [];
- }
- if($name != ''){
- $map[] = ['ui.name','=',$name];
- }
- if(in_array($type,[1,2])){
- $map[] = ['sr.type','=',$type];
- }
- $map[] = ['sr.del','=',0];
- $map = empty($map)?true:$map;
- $lists = Db::name('salary_record_sb')
- ->alias('srs')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->where($map)
- // ->page($page,$size)
- ->order('sr.id desc,ui.sorts3 asc')
- ->field('ui.name,sr.month,srs.id,srs.user_id,sum(srs.qynj) as qynj,sum(srs.gjj) as gjj,
- sum(srs.sb) as sb,sum(srs.syyl) as syyl,sum(srs.gssy) as gssy,sum(srs.ylnj) as ylnj,
- sum(srs.gjjp) as gjjp,sum(srs.gjjc) as gjjc,sum(srs.ylap) as ylap,sum(srs.ylac) as ylac,
- sum(srs.syep) as syep,sum(srs.syec) as syec,sum(srs.ylip) as ylip,sum(srs.ylic) as ylic,
- sum(srs.gsc) as gsc,sum(srs.qynjp) as qynjp,sum(srs.qynjc) as qynjc')
- ->group('sr.month,srs.user_id')
- ->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['sbtotalp'] = round($v['ylap'] + $v['syep'] + $v['ylip'],2);
- $lists[$k]['sbtotalc'] = round($v['ylac'] + $v['syec'] + $v['ylic'] + $v['gsc'],2);
- $lists[$k]['sbtotal'] = round($lists[$k]['sbtotalp'] + $lists[$k]['sbtotalc'],2);
- $lists[$k]['gjjtotal'] = round($lists[$k]['gjjp'] + $lists[$k]['gjjc'],2);
- $lists[$k]['qynjtotal'] = round($lists[$k]['qynjp'] + $lists[$k]['qynjc'],2);
- }
- // $total = Db::name('salary_record_sb')
- // ->alias('srs')
- // ->join('salary_record sr','sr.id = srs.salary_record_id')
- // ->join('user_info ui','ui.user_id = srs.user_id')
- // ->where($map)
- // ->group('sr.month,srs.user_id')
- // ->count();
- // $data = [
- // 'total' => $total,
- // 'list' => $lists?$lists:[]
- // ];
- return $lists?$lists:[];
- }
- public function sbexport($month,$name,$type){
- if($month != ''){
- $map[] = ['sr.month','=',$month];
- }
- if($name != ''){
- $map[] = ['ui.name','=',$name];
- }
- if(in_array($type,[1,2])){
- $map[] = ['sr.type','=',$type];
- }
- $map[] = ['sr.del','=',0];
- $map = empty($map)?true:$map;
- $lists = Db::name('salary_record_sb')
- ->alias('srs')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->where($map)
- ->order('sr.id desc,ui.sorts3 asc')
- ->field('ui.name,sr.month,srs.id,srs.user_id,sum(srs.qynj) as qynj,sum(srs.gjj) as gjj,
- sum(srs.sb) as sb,sum(srs.syyl) as syyl,sum(srs.gssy) as gssy,sum(srs.ylnj) as ylnj,
- sum(srs.gjjp) as gjjp,sum(srs.gjjc) as gjjc,sum(srs.ylap) as ylap,sum(srs.ylac) as ylac,
- sum(srs.syep) as syep,sum(srs.syec) as syec,sum(srs.ylip) as ylip,sum(srs.ylic) as ylic,
- sum(srs.gsc) as gsc,sum(srs.qynjp) as qynjp,sum(srs.qynjc) as qynjc')
- ->group('sr.month,srs.user_id')
- ->distinct(true)
- ->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['sbtotalp'] = round($v['ylap'] + $v['syep'] + $v['ylip'],2);
- $lists[$k]['sbtotalc'] = round($v['ylac'] + $v['syec'] + $v['ylic'] + $v['gsc'],2);
- $lists[$k]['sbtotal'] = round($lists[$k]['sbtotalp'] + $lists[$k]['sbtotalc'],2);
- $lists[$k]['gjjtotal'] = round($lists[$k]['gjjp'] + $lists[$k]['gjjc'],2);
- $lists[$k]['qynjtotal'] = round($lists[$k]['qynjp'] + $lists[$k]['qynjc'],2);
- }
- //转成数字格式
- $extra = ['month','name'];
- foreach ($lists as $k=>$v){
- foreach ($v as $kk=>$vv){
- if(!in_array($kk,$extra)){
- $v[$kk] = (float)$vv;
- }
- }
- $lists[$k] = $v;
- }
- $columns[] = ["title" => "月份","key" => "month"];
- $columns[] = ["title" => "姓名","key" => "name"];
- if($type == 2){
- $columns[] = ["title" => "企业年金基数","key" => "qynj"];
- }
- $columns[] = ["title" => "公积金缴纳基数","key" => "gjj"];
- if($type == 2){
- $columns[] = ["title" => "社保缴纳基数","key" => "sn"];
- }else{
- $columns[] = ["title" => "生育/医疗基数","key" => "syyl"];
- $columns[] = ["title" => "工伤/失业基数","key" => "gssy"];
- $columns[] = ["title" => "养老/年金基数","key" => "ylnj"];
- }
- $columns[] = ["title" => "养老保险(个人)","key" => "ylap"];
- $columns[] = ["title" => "失业保险(个人)","key" => "syep"];
- $columns[] = ["title" => "医疗保险(个人)","key" => "ylip"];
- $columns[] = ["title" => "个人小计(社保)","key" => "ylac"];
- $columns[] = ["title" => "养老保险(单位)","key" => "ylac"];
- $columns[] = ["title" => "失业保险(单位)","key" => "syec"];
- $columns[] = ["title" => "医疗保险(单位)","key" => "ylic"];
- $columns[] = ["title" => "工伤保险(单位)","key" => "gsc"];
- $columns[] = ["title" => "单位小计(社保)","key" => "sbtotalc"];
- $columns[] = ["title" => "社保合计","key" => "sbtotal"];
- $columns[] = ["title" => "公积金(个人)","key" => "gjjp"];
- $columns[] = ["title" => "公积金(单位)","key" => "gjjc"];
- $columns[] = ["title" => "公积金合计","key" => "gjjtotal"];
- if($type == 2){
- $columns[] = ["title" => "企业年金(个人)","key" => "qynjp"];
- $columns[] = ["title" => "企业年金(单位)","key" => "qynjc"];
- $columns[] = ["title" => "企业年金合计","key" => "qynjtotal"];
- }else{
- $columns[] = ["title" => "职业年金(个人)","key" => "qynjp"];
- $columns[] = ["title" => "职业年金(单位)","key" => "qynjc"];
- $columns[] = ["title" => "职业年金合计","key" => "qynjtotal"];
- }
- $data = [
- 'columns' => $columns,
- 'list' => $lists
- ];
- return $data;
- }
- public function unapply($userId){
- $lists = Db::name('salary_apply_record')
- ->alias('sar')
- ->join('salary_record sr','sr.id = sar.salary_record_id and sr.cur_node = sar.salary_apply_id')
- ->where('sar.user_id',$userId)
- ->where('sr.del',0)
- ->where('sr.status',0)
- ->field('sr.id,sr.month,sr.type,sar.create_time')
- ->select();
- $nlist = [];
- foreach ($lists as $k=>$v){
- $arr = [
- 'id' => $v['id'],
- 'create_time' => $v['create_time']
- ];
- if($v['type'] == 1){
- $arr['title'] = $v['month'].'-中心编制';
- }else if($v['type'] == 2){
- $arr['title'] = $v['month'].'-新华聘用';
- }else if($v['type'] == 3){
- $arr['title'] = $v['month'].'-中心劳务';
- }else if($v['type'] == 4){
- $arr['title'] = $v['month'].'-新华劳务';
- }else{
- $arr['title'] = $v['month'].'-中心养老';
- }
- $nlist[] = $arr;
- }
- return $nlist;
- }
- // 个人明细
- public function userdetail($page,$size,$type,$year){
- // 先取出本年发放工资人数
- if($type != 5){
- $users = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->join('user_info ui','ui.user_id = srl.user_id')
- ->where('sr.type',$type)
- ->where('sr.month','like',$year.'%')
- ->where('sr.status',2)
- ->where('sr.del',0)
- ->group('srl.user_id')
- ->page($page,$size)
- ->field('ui.name,srl.user_id,sum(gwgz) as gwgz,sum(xjgz) as xjgz,sum(jcjx) as jcjx,sum(yjx) as yjx,
- sum(tax) as tax,sum(salary) as salary,sum(should_total) as should_total,sum(sbgjj) as sbgjj')
- ->distinct(true)
- ->order('ui.sorts3 asc')
- ->select();
- return $users;
- }else{
- }
- }
- public function userYear($type,$year){
- $lists = Db::name('salary_record_sb')
- ->alias('srs')
- // ->join('salary_record_log srl','srl.salary_record_id = srs.salary_record_id')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where('sr.type',$type)
- ->where('sr.month','like',$year.'%')
- ->where('sr.status',2)
- ->where('sr.del',0)
- // ->field('ui.name,srl.user_id,sum(srl.should_total) as should_total,sum(srs.ylac) as ylac,sum(srs.ylic) as ylic,sum(srs.gsc) as gsc,
- // sum(srs.syec) as syec,sum(srs.gjjc) as gjjc,sum(srs.qynjc) as qynjc')
- ->field('ui.name,srs.user_id,srs.salary_record_id')
- ->group('srs.user_id')
- ->distinct(true)
- ->order('ui.sorts3 asc')
- ->select();
- $lists = $lists?$lists:[];;
- foreach ($lists as $k=>$v){
- $lists[$k]['should_total'] = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where('sr.type',$type)
- ->where('sr.month','like',$year.'%')
- ->where('sr.status',2)
- ->where('sr.del',0)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.should_total');
- $lists[$k]['ylac'] = Db::name('salary_record_sb')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where('sr.type',$type)
- ->where('sr.month','like',$year.'%')
- ->where('sr.status',2)
- ->where('sr.del',0)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.ylac');
- $lists[$k]['ylic'] = Db::name('salary_record_sb')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where('sr.type',$type)
- ->where('sr.month','like',$year.'%')
- ->where('sr.status',2)
- ->where('sr.del',0)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.ylic');
- // $lists[$k]['ylic'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('ylic');
- $lists[$k]['gsc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('gsc');
- $lists[$k]['syec'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('syec');
- $lists[$k]['gjjc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('gjjc');
- $lists[$k]['qynjc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('qynjc');
- $lists[$k]['key'] = $k+1;
- $lists[$k]['sb_total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['ylac'],2);
- $lists[$k]['total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['ylac'] + $lists[$k]['gjjc'] + $lists[$k]['qynjc'],2);
- }
- return $lists;
- }
- public function yearExport($type,$year){
- $lists = Db::name('salary_record_sb')
- ->alias('srs')
- // ->join('salary_record_log srl','srl.salary_record_id = srs.salary_record_id')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where('sr.type',$type)
- ->where('sr.month','like',$year.'%')
- ->where('sr.status',2)
- ->where('sr.del',0)
- // ->field('ui.name,srl.user_id,sum(srl.should_total) as should_total,sum(srs.ylac) as ylac,sum(srs.ylic) as ylic,sum(srs.gsc) as gsc,
- // sum(srs.syec) as syec,sum(srs.gjjc) as gjjc,sum(srs.qynjc) as qynjc')
- ->field('ui.name,srs.user_id,srs.salary_record_id')
- ->group('srs.user_id')
- ->distinct(true)
- ->order('ui.sorts3 asc')
- ->select();
- $lists = $lists?$lists:[];;
- foreach ($lists as $k=>$v){
- $lists[$k]['should_total'] = Db::name('salary_record_log')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('should_total');
- $lists[$k]['ylac'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('ylac');
- $lists[$k]['ylic'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('ylic');
- $lists[$k]['gsc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('gsc');
- $lists[$k]['syec'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('syec');
- $lists[$k]['gjjc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('gjjc');
- $lists[$k]['qynjc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('qynjc');
- $lists[$k]['key'] = $k+1;
- $lists[$k]['sb_total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['ylac'],2);
- $lists[$k]['total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['ylac'] + $lists[$k]['gjjc'] + $lists[$k]['qynjc'],2);
- }
- $columns[] = ["title" => "序号","key" => "key"];
- $columns[] = ["title" => "姓名","key" => "name"];
- $columns[] = ["title" => "应发工资","key" => "shouldTotal"];
- $columns[] = ["title" => "养老保险(单位)","key" => "ylac"];
- $columns[] = ["title" => "失业保险(单位)","key" => "syec"];
- $columns[] = ["title" => "医疗保险(单位)","key" => "ylic"];
- $columns[] = ["title" => "工伤保险(单位)","key" => "gsc"];
- $columns[] = ["title" => "社保小计","key" => "sbTotal"];
- $columns[] = ["title" => "住房公积金(单位)","key" => "gjjc"];
- if($type == 2){
- $columns[] = ["title" => "企业年金(单位)","key" => "qynjc"];
- }else{
- $columns[] = ["title" => "职业年金(单位)","key" => "qynjc"];
- }
- $columns[] = ["title" => "用工成本合计","key" => "total"];
- $data = [
- 'columns' => $columns,
- 'list' => $lists
- ];
- return $data;
- }
- }
|