123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626 |
- <?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 if(in_array($data['type'],[1,2,3,4])){
- $ret = model('SalaryRecord')->sendSalary($data);
- } else{ // 年绩效 6=中心编制 7=新华聘用 8=中心劳务 9=新华劳务
- $ret = model('SalaryRecord')->sendSalaryJx($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'],
- 'sign' => $curinfo['sign'],
- ];
- $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 && $info['status'] != 3){ //审核中或已打回
- HelpHander::error('无权限操作');
- }
- if($info['status'] == 3 && $info['user_id'] != $userId){
- HelpHander::error('无权限操作');
- }
- if($info['status'] == 0){
- $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('无权限修改3');
- }
- }
- $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 if(in_array($info['type'],[1,2,3,4])){
- $ret = model('SalaryRecord')->sendOneSalary($users[0],$info,$id);
- }else{
- $ret = model('SalaryRecord')->sendOneSalaryJx($users[0],$info,$id);
- }
- if(!$ret){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- // 删除工资项
- public function delsalary(){
- $salaryRecordId = input('salaryRecordId/d',0);
- $tids = input('tids','','trim');
- if(!$tids){
- HelpHander::error('未选择要删除的工资项');
- }
- $tids = array_unique(explode(',',$tids));
- $userId = input('userId/d',0);
- $orgId = input('orgId/d',0);
- $info = Db::name('salary_record')->where('id',$salaryRecordId)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 0 && $info['status'] != 3){ //审核中或已打回
- HelpHander::error('无权限操作');
- }
- if(!$info['types']){
- HelpHander::error('无可删除工资项');
- }
- if($info['status'] == 0){
- $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('无权限修改');
- }
- }
- $ntypes = [];
- $otypes = explode(',',$info['types']);
- foreach ($otypes as $v){
- if(!in_array($v,$tids)){
- $ntypes[] = $v;
- }
- }
- $info['types'] = !empty($ntypes)?implode(',',$ntypes):'';
- Db::startTrans();
- try{
- // 更新工资项
- $ret = Db::name('salary_record')->where('id',$salaryRecordId)->update([
- 'types' => $info['types'],
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- // 重新计算每个人的工资
- $logs = Db::name('salary_record_log')->where('salary_record_id',$salaryRecordId)->field('id,user_id')->select();
- foreach ($logs as $k=>$v){
- $rdata = array_change_line_to_hump($this->reloadAdd($salaryRecordId,$v['user_id'],$orgId,0));
- $types = $rdata['types'];
- $users = $rdata['list'];
- foreach ($users as $uk=>$uv){
- foreach ($types as $tk=>$tv){
- if(in_array($tv['id'],$tids)){
- unset($users[$uk]['subsidies'.$tv['id']]);
- }
- }
- }
- if($info['type'] === 5){
- $ret = model('UserRetiredSalary')->sendOneSalary($users[0],$info,$v['id']);
- }else if(in_array($info['type'],[1,2,3,4])){
- $ret = model('SalaryRecord')->sendOneSalary($users[0],$info,$v['id']);
- }else{
- $ret = model('SalaryRecord')->sendOneSalaryJx($users[0],$info,$v['id']);
- }
- if(!$ret){
- \exception('操作失败');
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- // 添加工资项
- public function addsalary(){
- $salaryRecordId = input('salaryRecordId/d',0);
- $tids = input('tids','','trim');
- if(!$tids){
- HelpHander::error('未选择要添加的工资项');
- }
- $tids = array_unique(explode(',',$tids));
- $userId = input('userId/d',0);
- $orgId = input('orgId/d',0);
- $info = Db::name('salary_record')->where('id',$salaryRecordId)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 0 && $info['status'] != 3){ //审核中或已打回
- HelpHander::error('无权限操作');
- }
- if($info['status'] == 0){
- $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('无权限修改');
- }
- }
- $postusers = input('users','','trim');
- $postusers = json_decode($postusers,true);
- if(!$postusers){
- HelpHander::error('参数错误');
- }
- $otypes = explode(',',$info['types']);
- if(array_intersect($tids,$otypes)){ // 有交集说明参数错误
- HelpHander::error('部分工资项已存在,不能添加');
- }
- $ntypes = $otypes;
- foreach ($tids as $v){
- if(!in_array($v,$otypes)){
- $ntypes[] = $v;
- }
- }
- $info['types'] = !empty($ntypes)?implode(',',$ntypes):'';
- Db::startTrans();
- try{
- // 更新工资项
- $ret = Db::name('salary_record')->where('id',$salaryRecordId)->update([
- 'types' => $info['types'],
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- // 重新计算每个人的工资
- $logs = Db::name('salary_record_log')->where('salary_record_id',$salaryRecordId)->field('id,user_id')->select();
- foreach ($logs as $k=>$v){
- $rdata = array_change_line_to_hump($this->reloadAdd($salaryRecordId,$v['user_id'],$orgId,0));
- $users = $rdata['list'];
- foreach ($users as $uk=>$uv){
- $userSn = think_encrypt($uv['userId']);
- foreach ($tids as $tk=>$tv){
- foreach ($postusers as $puk=>$puv){
- if($userSn == $puv['userSn']){
- $users[$uk]['subsidies'.$tv] = !empty($puv['subsidies'.$tv])?$puv['subsidies'.$tv]:0;
- break;
- }
- }
- }
- }
- if($info['type'] === 5){
- $ret = model('UserRetiredSalary')->sendOneSalary($users[0],$info,$v['id']);
- }else if(in_array($info['type'],[1,2,3,4])){
- $ret = model('SalaryRecord')->sendOneSalary($users[0],$info,$v['id']);
- }else{
- $ret = model('SalaryRecord')->sendOneSalaryJx($users[0],$info,$v['id']);
- }
- if(!$ret){
- \exception('操作失败');
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- // 修改工资项
- public function modifysalary(){
- $salaryRecordId = input('salaryRecordId/d',0);
- $tids = input('tids','','trim');
- if(!$tids){
- HelpHander::error('未选择要修改的工资项');
- }
- $tids = array_unique(explode(',',$tids));
- $userId = input('userId/d',0);
- $orgId = input('orgId/d',0);
- $info = Db::name('salary_record')->where('id',$salaryRecordId)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 0 && $info['status'] != 3){ //审核中或已打回
- HelpHander::error('无权限操作');
- }
- if($info['status'] == 0){
- $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('无权限修改');
- }
- }
- $postusers = input('users','','trim');
- $postusers = json_decode($postusers,true);
- if(!$postusers){
- HelpHander::error('参数错误');
- }
- $otypes = explode(',',$info['types']);
- if(array_intersect($tids,$otypes) != $tids){ // 不是子集
- HelpHander::error('修改的工资项不存在');
- }
- Db::startTrans();
- try{
- // 重新计算每个人的工资
- $logs = Db::name('salary_record_log')->where('salary_record_id',$salaryRecordId)->field('id,user_id')->select();
- foreach ($logs as $k=>$v){
- $rdata = array_change_line_to_hump($this->reloadAdd($salaryRecordId,$v['user_id'],$orgId,0));
- $users = $rdata['list'];
- foreach ($users as $uk=>$uv){
- $userSn = think_encrypt($uv['userId']);
- foreach ($tids as $tk=>$tv){
- foreach ($postusers as $puk=>$puv){
- if($userSn == $puv['userSn']){
- $users[$uk]['subsidies'.$tv] = !empty($puv['subsidies'.$tv])?$puv['subsidies'.$tv]:0;
- break;
- }
- }
- }
- }
- if($info['type'] === 5){
- $ret = model('UserRetiredSalary')->sendOneSalary($users[0],$info,$v['id']);
- }else if(in_array($info['type'],[1,2,3,4])){
- $ret = model('SalaryRecord')->sendOneSalary($users[0],$info,$v['id']);
- }else{
- $ret = model('SalaryRecord')->sendOneSalaryJx($users[0],$info,$v['id']);
- }
- if(!$ret){
- \exception('操作失败');
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- // 重新计算个税
- public function resendtax(){
- $id = input('id/d',0);
- $userId = input('userId/d',0);
- $log = Db::name('salary_record_log')->where('id',$id)->find();
- if(!$log){
- HelpHander::error('记录不存在');
- }
- $salaryRecordId = $log['salary_record_id'];
- $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('无权限修改');
- }
- if(!in_array($info['type'],[1,2])){
- HelpHander::error('无权限修改');
- }
- $sbdata = Db::name('salary_record_sb')->where('user_id',$log['user_id'])->where('salary_record_id',$salaryRecordId)->find();
- $jbsalary = $log['gwgz'] + $log['xjgz'] + $log['jcjx'] + $log['yjx'];
- $srid = Db::name('salary_record')
- ->alias('sr')
- ->join('salary_record_special srs','srs.salary_record_id = sr.id')
- ->where('sr.month',$info['month'])
- ->where('srs.user_id',$log['user_id'])
- ->where('sr.type',$info['type'])
- ->where('sr.del',0)
- ->order('sr.id desc')
- ->value('sr.id');
- $spdata = $this->getSalaryTax($log['should_total'],$sbdata,$log['user_id'],$log['company_id'],$info['month'],$info['type'],$jbsalary,$srid); // 计算个税
- $tax = $spdata['tax'];
- if($tax == $log['tax']){
- return true;
- }
- Db::startTrans();
- try{
- $salary = $log['salary'] - ($tax - $log['tax']);
- $sdata = [
- 'tax' => $tax,
- 'salary' => $salary,
- ];
- $ret = Db::name('salary_record_log')->where('id',$id)->update($sdata);
- if(!$ret){
- \exception('操作失败');
- }
- $spdata['salary_record_id'] = $salaryRecordId;
- $spdata['user_id'] = $log['user_id'];
- $m = intval(date('m',strtotime($info['month'].'-01')));
- if($m == 1 && $spdata['type'] != 1){ // 一月的第一次发工资
- $spdata['tax'] = 0;
- }
- $ret = Db::name('salary_record_special')
- ->where('salary_record_id',$salaryRecordId)
- ->where('user_id',$log['user_id'])
- ->strict(false)
- ->update($spdata);
- 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
- $val = (float)$val; // 支持负值
- $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,$user['userId']);
- $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;
- $bkdata = [
- 'ggj' => 0, // 补扣公积金
- 'qynj' => 0, // 补扣企业年金
- 'yla' => 0, // 补扣养老保险
- 'yli' => 0, // 补扣医疗保险
- 'sye' => 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
- $val = (float)$val; // 支持负值
- $btjson[] = [
- 'id' => $vv['id'],
- 'cate' => $vv['cate'],
- 'title' => $vv['title'],
- 'money' => $val,
- 'sbtype' => $vv['sbtype']
- ];
- $vvl = 0;
- if($vv['cate'] == 1){ // 补贴
- $should_total += $val;
- $vvl = $val;
- }else{
- $kcsalary += $val;
- $vvl = -$val;
- }
- // 是否关联社保公积金
- // 1=公积金 2=企业年金 3=养老保险 4=医疗保险 5=失业保险
- if($vv['sbtype'] == 1){
- $bkdata['ggj'] += $vvl;
- }else if($vv['sbtype'] == 2){
- $bkdata['qynj'] += $vvl;
- }else if($vv['sbtype'] == 3){
- $bkdata['yla'] += $vvl;
- }else if($vv['sbtype'] == 4){
- $bkdata['yli'] += $vvl;
- }else if($vv['sbtype'] == 5){
- $bkdata['sye'] += $vvl;
- }
- }
- }
- }
- }
- $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,$bkdata); // 计算个税
- $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 sendOneSalaryJx($user,$data,$id){
- // 判断下记录是否存在
- $ret = Db::name('salary_record_log')
- ->where('id',$id)
- ->where('salary_record_id',$data['id'])
- ->where('user_id',$user['userId'])
- ->find();
- if(!$ret){
- return false;
- }
- $sdata = [
- 'salary_record_id' => $data['id'],
- 'user_id' => $user['userId'],
- 'njx' => floatval($user['psyear']) > 0?floatval($user['psyear']):0,
- 'pwlevel' => $user['pwlevel'],
- 'psid' => $user['psid'],
- 'type' => $user['type'],
- 'company_id' => $user['companyId'],
- 'should_total' => floatval($user['shouldTotal']) > 0?floatval($user['shouldTotal']):0,
- 'salary' => floatval($user['salary']) > 0?floatval($user['salary']):0,
- 'tax' => floatval($user['tax']) > 0?floatval($user['tax']):0,
- ];
- if($data['type'] == 6||$data['type'] == 7){
- $sdata['level'] = trim($user['level'])?trim($user['level']):'';
- $sdata['factor'] = floatval($user['factor']) > 0?floatval($user['factor']):0;
- $sdata['months'] = intval($user['months']) > 0?intval($user['months']):0;
- $sdata['taxrate'] = floatval($user['taxrate']) > 0?floatval($user['taxrate']):0;
- }
- $res = Db::name('salary_record_log')->where('id',$id)->update($sdata);
- if($res === 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
- $val = (float)$val; // 支持负值
- $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,$v['userId']);
- $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;
- $bkdata = [
- 'ggj' => 0, // 补扣公积金
- 'qynj' => 0, // 补扣企业年金
- 'yla' => 0, // 补扣养老保险
- 'yli' => 0, // 补扣医疗保险
- 'sye' => 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
- $val = (float)$val; // 支持负值
- $btjson[] = [
- 'id' => $vv['id'],
- 'cate' => $vv['cate'],
- 'title' => $vv['title'],
- 'money' => $val,
- 'sbtype' => $vv['sbtype']
- ];
- $vvl = 0;
- if($vv['cate'] == 1){ // 补贴
- $should_total += $val;
- $vvl = $val;
- }else{
- $kcsalary += $val;
- $vvl = -$val;
- }
- // 是否关联社保公积金
- // 1=公积金 2=企业年金 3=养老保险 4=医疗保险 5=失业保险
- if($vv['sbtype'] == 1){
- $bkdata['ggj'] += $vvl;
- }else if($vv['sbtype'] == 2){
- $bkdata['qynj'] += $vvl;
- }else if($vv['sbtype'] == 3){
- $bkdata['yla'] += $vvl;
- }else if($vv['sbtype'] == 4){
- $bkdata['yli'] += $vvl;
- }else if($vv['sbtype'] == 5){
- $bkdata['sye'] += $vvl;
- }
- }
- }
- }
- }
- 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,0,$bkdata); // 计算个税
- trace($spdata);
- $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;
- }
- }
- trace($sparr);
- }
- return true;
- }
- private function sendSalaryJx($data){
- $users = $data['users'];
- $arr = [];
- $uids = [];
- foreach ($users as $k=>$v){
- if(in_array($v['userId'],$uids)){
- continue;
- }
- $uids[] = $v['userId'];
- $sdata = [
- 'salary_record_id' => $data['salary_record_id'],
- 'user_id' => $v['userId'],
- 'njx' => floatval($v['psyear']) > 0?floatval($v['psyear']):0,
- 'pwlevel' => $v['pwlevel'],
- 'psid' => $v['psid'],
- 'type' => $v['type'],
- 'company_id' => $v['companyId'],
- 'should_total' => floatval($v['shouldTotal']) > 0?floatval($v['shouldTotal']):0,
- 'salary' => floatval($v['salary']) > 0?floatval($v['salary']):0,
- 'tax' => floatval($v['tax']) > 0?floatval($v['tax']):0,
- ];
- if($data['type'] == 6||$data['type'] == 7){
- $sdata['level'] = trim($v['level'])?trim($v['level']):'';
- $sdata['factor'] = floatval($v['factor']) > 0?floatval($v['factor']):0;
- $sdata['months'] = intval($v['months']) > 0?intval($v['months']):0;
- $sdata['taxrate'] = floatval($v['taxrate']) > 0?floatval($v['taxrate']):0;
- }
- $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;
- }
- }
- return true;
- }
- // 新华聘用税/编制个税
- private function getSalaryTax($should_total,$sbdata,$userId,$companyId,$month,$type,$jbsalary,$from=0,$bkdata=[]){// 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,
- 'ljyyezh' => 0,
- 'ljgrylj' => 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'],
- 'ljyyezh' => $lastinfo['ljyyezh'],
- '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'],
- 'ljgrylj' => $lastinfo['ljgrylj'],
- '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?$special['mssr']:0;
- if($m > 1){
- $data['ljjc'] += 5000; // 减除费用每月只能使用1次
- $data['ljgjj'] += $sbdata['gjjp'] - (!empty($bkdata)?$bkdata['ggj']:0);
- $data['ljylobx'] += $sbdata['ylap'] - (!empty($bkdata)?$bkdata['yla']:0);
- $data['ljsybx'] += $sbdata['syep'] - (!empty($bkdata)?$bkdata['sye']:0);
- $data['ljylibx'] += $sbdata['ylip'] - (!empty($bkdata)?$bkdata['yli']:0);
- $data['ljnj'] += $sbdata['qynjp'] - (!empty($bkdata)?$bkdata['qynj']:0);
- $data['ljznjy'] += $special?$special['znjy']:0;
- $data['ljjxjy'] += $special?$special['jxjy']:0;
- $data['ljzfdklx'] += $special?$special['zfdklx']:0;
- $data['ljzfzj'] += $special?$special['zfzj']:0;
- $data['ljsylr'] += $special?$special['sylr']:0;
- $data['ljyyezh'] += $special?$special['yyezh']:0;
- $data['ljgzds'] += $special?$special['gzds']:0;
- $data['syjkbx'] += $special?$special['syjkbx']:0;
- $data['ysylbx'] += $special?$special['ysylbx']:0;
- $data['grbk'] += $special?$special['grbk']:0;
- $data['jzjbjsb'] += $special?$special['nzjds']:0;
- $data['qtkc'] += $special?$special['qtkc']:0;
- $data['zykcjze'] += $special?$special['zykcjze']:0;
- $data['ljgrylj'] += $special?$special['grylj']:0;
- }else{
- $data['ljjc'] = 5000;
- $data['ljgjj'] = $sbdata['gjjp'] - (!empty($bkdata)?$bkdata['ggj']:0);
- $data['ljylobx'] = $sbdata['ylap'] - (!empty($bkdata)?$bkdata['yla']:0);
- $data['ljsybx'] = $sbdata['syep'] - (!empty($bkdata)?$bkdata['sye']:0);
- $data['ljylibx'] = $sbdata['ylip'] - (!empty($bkdata)?$bkdata['yli']:0);
- $data['ljnj'] = $sbdata['qynjp'] - (!empty($bkdata)?$bkdata['qynj']:0);
- $data['ljznjy'] = $special?$special['znjy']:0;
- $data['ljjxjy'] = $special?$special['jxjy']:0;
- $data['ljzfdklx'] = $special?$special['zfdklx']:0;
- $data['ljzfzj'] = $special?$special['zfzj']:0;
- $data['ljsylr'] = $special?$special['sylr']:0;
- $data['ljgzds'] = $special?$special['gzds']:0;
- $data['ljyyezh'] = $special?$special['yyezh']:0;
- $data['ljykjse'] = 0;
- $data['ljsr'] = 0;
- $data['syjkbx'] = $special?$special['syjkbx']:0;
- $data['ysylbx'] = $special?$special['ysylbx']:0;
- $data['grbk'] = $special?$special['grbk']:0;
- $data['jzjbjsb'] = $special?$special['nzjds']:0;
- $data['qtkc'] = $special?$special['qtkc']:0;
- $data['zykcjze'] = $special?$special['zykcjze']:0;
- $data['ljgrylj'] = $special?$special['grylj']:0;
- }
- $data['jmsebl'] = $special?$special['jmsebl']:0;
- $data['mssr'] = $special?$special['mssr']:0;
- }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['ljyyezh'] = 0;
- $data['ljgrylj'] = 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['ljyyezh'] + $data['ljgrylj'] + $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;
- $data['gjjc'] = 0;
- }
- if(!$info['qynj']){
- $data['qynjp'] = 0;
- $data['gjjc'] = 0;
- }
- if(!$info['yla']){
- $data['ylap'] = 0;
- $data['ylac'] = 0;
- }
- if(!$info['sye']){
- $data['syep'] = 0;
- $data['syec'] = 0;
- }
- if(!$info['yli']){
- $data['ylip'] = 0;
- $data['ylic'] = 0;
- }
- if(!$info['gs']){
- $data['gsc'] = 0;
- }
- return $data;
- }
- // 获取劳务费个税
- public function getLaborTax($salary,$userId){
- // =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 = max($arr);
- if($tax > 0){
- $bl = Db::name('user_jmlabor')->where('user_id',$userId)->value('jmsebl');
- $bl = $bl?$bl:0;
- if($bl > 0){
- $tax = (1 - $bl)*$tax;
- }
- }
- $tax = round($tax,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];
- if($data['type'] == 6){
- $map[] = ['ui.pay_type','=',1];
- } else if($data['type'] == 7){
- $map[] = ['ui.pay_type','=',2];
- } else if($data['type'] == 8){
- $map[] = ['ui.pay_type','=',3];
- } else if($data['type'] == 9){
- $map[] = ['ui.pay_type','=',4];
- } else {
- $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]['user_sn'] = think_encrypt($v['user_id']);
- if(in_array($data['type'],[6,7,8,9])){
- $lists[$k]['level'] = '';
- $lists[$k]['factor'] = '';
- $lists[$k]['months'] = '';
- $lists[$k]['taxrate'] = '';
- $lists[$k]['tax'] = '';
- $lists[$k]['salary'] = '';
- $lists[$k]['should_total'] = '';
- $lists[$k]['psyear'] = '';
- }
- $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]['psyear'] = 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]['psyear'] = $psinfo['year_performance'];
- $lists[$k]['psmonth'] = $psinfo['month_performance'];
- if($lists[$k]['work_age'] > 25 && $psinfo['month_performance2'] > 0){
- $lists[$k]['psmonth'] = $psinfo['month_performance2'];
- }
- // if($lists[$k]['work_age'] > 25 && $psinfo['year_performance2'] > 0){
- // $lists[$k]['psyear'] = $psinfo['year_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;
- $lists[$k]['gs'] = 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,$verification=1){
- $data = Db::name('salary_record')->where('id',$salaryRecordId)->find();
- // 判断下记录是否存在
- $ret = Db::name('salary_record_log')->where('salary_record_id',$salaryRecordId)->where('user_id',$uid)->find();
- if($verification == 1 && !$ret){
- HelpHander::error('记录不存在');
- }
- $map[] = ['u.id','=',$uid];
- $map[] = ['u.del','=',0];
- $map[] = ['r.type','=',3];
- $map[] = ['r.org_id','=',$orgId];
- // 不再判断类型,已发放时为准
- /*if($data['type'] == 1||$data['type'] == 6){
- $map[] = ['ui.pay_type','=',1];
- }else if($data['type'] == 2||$data['type'] == 7){
- $map[] = ['ui.pay_type','=',2];
- }else if($data['type'] == 3||$data['type'] == 8){
- $map[] = ['ui.pay_type','=',3];
- }else if($data['type'] == 4||$data['type'] == 9){
- $map[] = ['ui.pay_type','=',4];
- }else{
- $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($verification == 1 && !$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){
- if(in_array($data['type'],[6,7,8,9])){
- $log = Db::name('salary_record_log')
- ->where('salary_record_id',$salaryRecordId)
- ->where('user_id',$v['user_id'])
- ->find();
- $lists[$k]['level'] = $log?$log['level']:'';
- $lists[$k]['factor'] = $log?$log['factor']:'';
- $lists[$k]['months'] = $log?$log['months']:'';
- $lists[$k]['taxrate'] = $log?$log['taxrate']:'';
- $lists[$k]['tax'] = $log?$log['tax']:'';
- $lists[$k]['salary'] = $log?$log['salary']:'';
- $lists[$k]['should_total'] = $log?$log['should_total']:'';
- $lists[$k]['psyear'] = $log?$log['njx']:'';
- }
- $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;
- $lists[$k]['gs'] = true;
- // 检查开关
- $sbinfo = Db::name('salary_record_sb')->where('salary_record_id',$salaryRecordId)->where('user_id',$v['user_id'])->find();
- if($sbinfo){
- if($sbinfo['gjjp'] <= 0){
- $lists[$k]['gjj'] = false;
- }
- if($sbinfo['ylap'] <= 0){
- $lists[$k]['yla'] = false;
- }
- if($sbinfo['syep'] <= 0){
- $lists[$k]['sye'] = false;
- }
- if($sbinfo['ylip'] <= 0){
- $lists[$k]['yli'] = false;
- }
- if($sbinfo['gsc'] <= 0){
- $lists[$k]['gs'] = false;
- }
- if($sbinfo['qynjp'] <= 0){
- $lists[$k]['qynj'] = false;
- }
- }else{
- $lists[$k]['gjj'] = false;
- $lists[$k]['yla'] = false;
- $lists[$k]['sye'] = false;
- $lists[$k]['yli'] = false;
- $lists[$k]['qynj'] = false;
- $lists[$k]['gs'] = false;
- }
- }
- }
- $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 = [];
- $tids = [];
- if($info['types']){
- $tids = explode(',',$info['types']);
- $types = Db::name('user_subsidies')->where('id','in',$info['types'])->select();
- }
- $info['types'] = $types;
- $alltypes = Db::name('user_subsidies')->where('del',0)->where('pay_type',$info['type'])->select();
- $addtypes = [];
- foreach ($alltypes as $v){
- if(!in_array($v['id'],$tids)){
- $addtypes[] = $v;
- }
- }
- $info['addtypes'] = $addtypes;
- $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));
- if($info['id'] > 407){
- $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.salary_record_id',$info['id'])
- ->where('sar.status',1)
- ->where('sar.sign',2)
- ->where('sar.del',0)
- ->order('sar.id asc')
- ->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.sign',1)
- ->where('sar.status',1)
- ->where('sar.del',0)
- ->where('sar.salary_record_id',$info['id'])
- ->order('sar.id asc')
- ->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.salary_record_id',$info['id'])
- ->where('sar.sign',3)
- ->where('sar.status',1)
- ->where('sar.del',0)
- ->order('sar.id asc')
- ->value('ui.name');
- $info['cwl_user_name'] = $cwlinfo?$cwlinfo:'';
- }else{
- $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.salary_record_id',$info['id'])
- ->where('sar.status',1)
- ->where('sa.cate',2)
- ->where('sar.del',0)
- ->order('sar.id asc')
- ->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('sa.cate',3)
- ->where('sar.status',1)
- ->where('sar.del',0)
- ->where('sar.salary_record_id',$info['id'])
- ->order('sar.id asc')
- ->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.salary_record_id',$info['id'])
- ->where('sa.cate',4)
- ->where('sar.status',1)
- ->where('sar.del',0)
- ->order('sar.id asc')
- ->value('ui.name');
- $info['cwl_user_name'] = $cwlinfo?$cwlinfo:'';
- if($info['type'] == 1||$info['type'] == 3||$info['type'] == 5){
- $info['kj_user_name'] = '郑琳';
- }else{
- $info['kj_user_name'] = '汪涵';
- }
- $info['cwl_user_name'] = '许倩鸿';
- }
- }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');
- }
- $use = "";
- switch ($info['type']){
- case 1:
- $use = "发放中心编制工资";
- break;
- case 2:
- $use = "发放新华聘用工资";
- break;
- case 3:
- $use = "发放中心劳务工资";
- break;
- case 4:
- $use = "发放新华劳务工资";
- break;
- case 5:
- $use = "发放中心退休工资";
- break;
- case 6:
- $use = "发放中心编制工资";
- break;
- case 7:
- $use = "发放新华聘用年绩效";
- break;
- case 8:
- $use = "发放中心劳务年绩效";
- break;
- case 9:
- $use = "发放新华劳务年绩效";
- break;
- }
- $info['use'] = $use;
- // 获取上个月
- $lastmonth = get_lastmonth_by_day($info['month'].'-01');
- // 社保的汇缴是滞后的,即3月请款单上应汇缴的是2月的社保
- $lastmonthtxt = date('Y年m月',strtotime($lastmonth.'-01'));
- if($info['type'] == 1){
- $month = date('Y年m月',strtotime($info['month'].'-01'));
- // $lastinfo = Db::name('salary_record_sb')
- // ->alias('a')
- // ->join('salary_record b','a.salary_record_id = b.id')
- // ->where('b.month',$lastmonth)
- // ->where('a.ylnj','>',0)
- // ->field('b.*')
- // ->find();
- // if($lastinfo){
- // $info['ylause'] = "基建中心".$lastmonthtxt.$lastinfo['nums']."人职业年金汇缴";
- // $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylac');
- // $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylap');
- // }else{
- // $info['ylause'] = "基建中心".$lastmonthtxt."0人职业年金汇缴";
- // $hjsalary1 = 0;
- // $hjsalary2 = 0;
- // }
- $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylac');
- $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylap');
- $info['ylause'] = "基建中心".$lastmonthtxt.$info['nums']."人养老保险汇缴";
- $info['ylasalary'] = round($hjsalary1+$hjsalary2,2);
- $info['ylacompany'] = "中央国家机关养老保险管理中心";
- $info['njcompany'] = "中国建设银行中央国家机关事业单位职业年金基金归集财产";
- $info['njuse'] = "基建中心".$lastmonthtxt.$info['nums']."人职业年金汇缴";
- $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('qynjc');
- $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('qynjp');
- $info['njsalary'] = round($hjsalary1+$hjsalary2,2);
- // if($lastinfo){
- // $info['sbuse'] = "基建中心".$lastmonthtxt.$lastinfo['nums']."人医疗(含生育)、失业、工伤保险汇缴";
- // $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylic');
- // $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylip');
- // $hjsalary3 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('syec');
- // $hjsalary4 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('syep');
- // $hjsalary5 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('gsc');
- // }else{
- // $info['sbuse'] = "基建中心".$lastmonthtxt."0人医疗(含生育)、失业、工伤保险汇缴";
- // $hjsalary1 = 0;
- // $hjsalary2 = 0;
- // $hjsalary3 = 0;
- // $hjsalary4 = 0;
- // $hjsalary5 = 0;
- // }
- $info['sbuse'] = "基建中心".$lastmonthtxt.$info['nums']."人医疗(含生育)、失业、工伤保险汇缴";
- $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylic');
- $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylip');
- $hjsalary3 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('syec');
- $hjsalary4 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('syep');
- $hjsalary5 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gsc');
- $info['sbcompany'] = "国家税务总局北京市西城区税务局";
- // $info['sbuse'] = "基建中心".$month.$info['nums']."人医疗(含生育)、失业、工伤保险汇缴";
- $info['sbsalary'] = round($hjsalary1+$hjsalary2+$hjsalary3+$hjsalary4+$hjsalary5,2);
- $info['gjjcompany'] = "中央国家机关住房资金管理中心";
- $info['gjjuse'] = "基建中心".$month.$info['nums']."人住房公积金汇缴";
- $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gjjc');
- $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gjjp');
- $info['gjjsalary'] = round($hjsalary1+$hjsalary2,2);
- }else if($info['type'] == 2){
- $month = date('Y年m月',strtotime($info['month'].'-01'));
- $info['gjjcompany'] = "中央国家机关住房资金管理中心";
- $info['gjjuse'] = "新华公司".$month.$info['nums']."人住房公积金汇缴";
- $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gjjc');
- $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gjjp');
- $info['gjjsalary'] = round($hjsalary1+$hjsalary2,2);
- $info['njcompany'] = "中国银行建信养老养颐乐安享集合计划受托财产";
- $info['njuse'] = "新华公司".$month.$info['nums']."人企业年金汇缴";
- $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('qynjc');
- $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('qynjp');
- $info['njsalary'] = round($hjsalary1+$hjsalary2,2);
- // $lastinfo = Db::name('salary_record_sb')
- // ->alias('a')
- // ->join('salary_record b','a.salary_record_id = b.id')
- // ->where('b.month',$lastmonth)
- // ->where('a.sb','>',0)
- // ->field('b.*')
- // ->find();
- // if($lastinfo){
- // $info['sbuse'] = "新华公司".$lastmonthtxt.$lastinfo['nums']."人养老、医疗(含生育)、失业、工伤保险汇缴";
- // $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylic');
- // $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylip');
- // $hjsalary3 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('syec');
- // $hjsalary4 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('syep');
- // $hjsalary5 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('gsc');
- // $hjsalary6 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylac');
- // $hjsalary7 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylap');
- // }else{
- // $info['sbuse'] = "新华公司".$lastmonthtxt."0人养老、医疗(含生育)、失业、工伤保险汇缴";
- // $hjsalary1 = 0;
- // $hjsalary2 = 0;
- // $hjsalary3 = 0;
- // $hjsalary4 = 0;
- // $hjsalary5 = 0;
- // $hjsalary6 = 0;
- // $hjsalary7 = 0;
- // }
- $info['sbuse'] = "新华公司".$lastmonthtxt.$info['nums']."人养老、医疗(含生育)、失业、工伤保险汇缴";
- $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylic');
- $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylip');
- $hjsalary3 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('syec');
- $hjsalary4 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('syep');
- $hjsalary5 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gsc');
- $hjsalary6 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylac');
- $hjsalary7 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylap');
- $info['sbcompany'] = "国家税务总局北京市西城区税务局";
- // $info['sbuse'] = "基建中心".$month.$info['nums']."人养老、医疗(含生育)、失业、工伤保险汇缴";
- $info['sbsalary'] = round($hjsalary1+$hjsalary2+$hjsalary3+$hjsalary4+$hjsalary5+$hjsalary6+$hjsalary7,2);
- }
- $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){
- $lists[$k]['user_sn'] = think_encrypt($v['user_id']);
- if($info['type'] == 1||$info['type'] == 2){
- $sb = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->find();
- $lists[$k]['gjjp'] = $sb?$sb['gjjp']:0;
- $lists[$k]['qynjp'] = $sb?$sb['qynjp']:0;
- $lists[$k]['ylap'] = $sb?$sb['ylap']:0;
- $lists[$k]['syep'] = $sb?$sb['syep']:0;
- $lists[$k]['ylip'] = $sb?$sb['ylip']:0;
- }
- $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('d.enable',1)
- ->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){
- if($info['type'] == 1||$info['type'] == 2){
- $sb = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->find();
- $lists[$k]['gjjp'] = $sb?$sb['gjjp']:0;
- $lists[$k]['qynjp'] = $sb?$sb['qynjp']:0;
- $lists[$k]['ylap'] = $sb?$sb['ylap']:0;
- $lists[$k]['syep'] = $sb?$sb['syep']:0;
- $lists[$k]['ylip'] = $sb?$sb['ylip']:0;
- }
- $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','njx','tax'];
- 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 = ['gjjp','qynjp','ylap','ylip','syep','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 if(in_array($info['type'],[1,2,3,4])){
- $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"];
- }else if(in_array($info['type'],[6,7])){
- $columns[] = ["title" => "部门","key" => "depName"];
- $columns[] = ["title" => "职级","key" => "jobName"];
- $columns[] = ["title" => "薪级","key" => "pwlevel"];
- $columns[] = ["title" => "年绩效","key" => "njx"];
- $columns[] = ["title" => "等次","key" => "level"];
- $columns[] = ["title" => "系数","key" => "factor"];
- $columns[] = ["title" => "发放月数","key" => "months"];
- }else if(in_array($info['type'],[8,9])){
- $columns[] = ["title" => "部门","key" => "depName"];
- $columns[] = ["title" => "职级","key" => "jobName"];
- $columns[] = ["title" => "薪级","key" => "pwlevel"];
- $columns[] = ["title" => "年绩效","key" => "njx"];
- }
- 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"];
- $columns[] = ["title" => "公积金","key" => "gjjp"];
- if($info['type'] == 1){
- $columns[] = ["title" => "职业年金","key" => "qynjp"];
- }else{
- $columns[] = ["title" => "企业年金","key" => "qynjp"];
- }
- $columns[] = ["title" => "养老保险","key" => "ylap"];
- $columns[] = ["title" => "医疗保险","key" => "ylip"];
- $columns[] = ["title" => "失业保险","key" => "syep"];
- }
- if(in_array($info['type'],[6,7])){
- $columns[] = ["title" => "适用税率","key" => "taxrate"];
- }
- if($info['type'] != 5){
- $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 if($v['type'] == 6){
- $lists[$k]['type_name'] = '中心编制年绩效';
- }else if($v['type'] == 7){
- $lists[$k]['type_name'] = '新华聘用年绩效';
- }else if($v['type'] == 8){
- $lists[$k]['type_name'] = '中心劳务年绩效';
- }else if($v['type'] == 9){
- $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):[];
- $btmoney = $kcmoney = 0;
- foreach ($data['btjson'] as $k=>$v){
- if($v['cate'] == 1){ // 补贴
- $btmoney += floatval($v['money']);
- }else{
- $kcmoney += floatval($v['money']);
- }
- }
- $data['btmoney'] = round($btmoney,2);
- $data['kcmoney'] = round($kcmoney,2);
- $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)
- ->where('del',0)
- ->find();
- if(!$uinfo){
- HelpHander::error('无权限操作');
- }
- $apply = json_decode($info['apply'],true);
- Db::startTrans();
- try{
- $res = Db::name('salary_apply_record')->where('del',0)->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'],
- 'sign' => $next['sign']
- ];
- $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)
- ->where('del',0)
- ->find();
- if(!$uinfo){
- HelpHander::error('无权限操作');
- }
- Db::startTrans();
- try{
- $res = Db::name('salary_apply_record')->where('del',0)->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 reapply($id,$userId,$orgId){
- $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 3 || $info['user_id'] != $userId){
- HelpHander::error('无权限操作');
- }
- Db::startTrans();
- try{
- // 删除原审批记录
- Db::name('salary_apply_record')->where('salary_record_id',$id)->update(['del'=>1,'update_time'=>date('Y-m-d H:i:s')]);
- $apply = json_decode($info['apply'],true);
- $curinfo = $apply[0];
- $res2 = Db::name('salary_record')->where('id',$id)->update(['status'=>0,'cur_node'=>$curinfo['id']]);
- if(!$res2){
- \exception('操作失败');
- }
- // 生成审核记录
- $uids = explode(',',$curinfo['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' => $curinfo['id'],
- 'sign' => $curinfo['sign'],
- ];
- $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 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)
- ->where('del',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('del',0)
- ->where('status',0)
- ->find();
- $apply = json_decode($info['apply'],true);
- Db::startTrans();
- try{
- $res = Db::name('salary_apply_record')->where('del',0)->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),
- 'ljyyezh' => input('ljyyezh/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),
- 'ljgrylj' => input('ljgrylj/f',0),
- ];
- $data['sr'] = round($data['salary'] - $data['mssr'],2);
- $data['ljsr'] = round($oldljsr + $data['sr'],2);
- $data['ljsqljxmhj'] = $data['ljjc'] + $data['ljgjj'] + $data['ljyyezh'] + $data['ljgrylj'] + $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('操作失败');
- }
- $userId = input('userId/d',0);
- model('ActionLog')->add(19,'修改个税累计',$userId,json_encode($data));
- 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" => "sbtotalp"];
- $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)
- ->where('sar.del',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 if($v['type'] == 5){
- $arr['title'] = $v['month'].'-中心养老';
- }else if($v['type'] == 6){ //6=中心编制 7=新华聘用 8=中心劳务 9=新华劳务
- $arr['title'] = $v['month'].'-中心编制年绩效';
- }else if($v['type'] == 7){
- $arr['title'] = $v['month'].'-新华聘用年绩效';
- }else if($v['type'] == 8){
- $arr['title'] = $v['month'].'-中心劳务年绩效';
- }else if($v['type'] == 9){
- $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,$name,$month = 0){
- $map[] = ['sr.status','=',2];
- $map[] = ['sr.del','=',0];
- $map2 = $map;
- if($month > 0){
- if($month < 10){
- $map[] = ['sr.month','=',$year.'-0'.$month];
- }else{
- $map[] = ['sr.month','=',$year.'-'.$month];
- }
- }else{
- $map[] = ['sr.month','like',$year.'%'];
- }
- $map[] = ['sr.type','=',$type];
- $map1 = $map;
- if($name){
- $map[] = ['ui.name','like','%'.$name.'%'];
- }
- $ntype = $type + 5; // 年绩效类型
- $map2[] = ['sr.type','=',$ntype];
- if($month > 0){
- if($month < 10){
- $map2[] = ['sr.month','=',$year.'-0'.$month];
- }else{
- $map2[] = ['sr.month','=',$year.'-'.$month];
- }
- }else{
- $map2[] = ['sr.month','like',$year.'%'];
- }
- if(in_array($type,[1,2])){
- $lists = Db::name('salary_record_sb')
- ->alias('srs')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where($map)
- ->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($map1)
- ->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($map1)
- ->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($map1)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.ylic');
- $lists[$k]['gsc'] = Db::name('salary_record_sb')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map1)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.gsc');
- $lists[$k]['syec'] = Db::name('salary_record_sb')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map1)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.syec');
- $lists[$k]['gjjc'] = Db::name('salary_record_sb')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map1)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.gjjc');
- $lists[$k]['qynjc'] = Db::name('salary_record_sb')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map1)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.qynjc');
- $lists[$k]['njx'] = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map2)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.should_total');
- $lists[$k]['key'] = $k+1;
- $lists[$k]['sb_total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['syec'],2);
- $lists[$k]['total'] = round($lists[$k]['njx'] + $lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['syec'] + $lists[$k]['gjjc'] + $lists[$k]['qynjc'] + $lists[$k]['should_total'],2);
- }
- }else{
- $lists = Db::name('salary_record_log')
- ->alias('srs')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where($map)
- ->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($map1)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.should_total');
- $lists[$k]['njx'] = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map2)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.should_total');
- $lists[$k]['key'] = $k+1;
- $lists[$k]['total'] = round($lists[$k]['should_total'] + $lists[$k]['njx'] + $lists[$k]['should_total'],2);
- }
- }
- return $lists;
- }
- public function yearExport($type,$year,$name,$month = 0){
- $map[] = ['sr.status','=',2];
- $map[] = ['sr.del','=',0];
- $map2 = $map;
- $map[] = ['sr.type','=',$type];
- if($month > 0){
- if($month < 10){
- $map[] = ['sr.month','=',$year.'-0'.$month];
- }else{
- $map[] = ['sr.month','=',$year.'-'.$month];
- }
- }else{
- $map[] = ['sr.month','like',$year.'%'];
- }
- $map1 = $map;
- if($name){
- $map[] = ['ui.name','like','%'.$name.'%'];
- }
- $ntype = $type + 5; // 年绩效类型
- $map2[] = ['sr.type','=',$ntype];
- // $map2[] = ['sr.month','like','%'.($year+1).'%'];
- if($month > 0){
- if($month < 10){
- $map2[] = ['sr.month','=',$year.'-0'.$month];
- }else{
- $map2[] = ['sr.month','=',$year.'-'.$month];
- }
- }else{
- $map2[] = ['sr.month','like',$year.'%'];
- }
- if(in_array($type,[1,2])){
- $lists = Db::name('salary_record_sb')
- ->alias('srs')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where($map)
- ->field('ui.name,srs.user_id,srs.salary_record_id')
- ->group('srs.user_id')
- ->distinct(true)
- ->order('ui.sorts3 asc')
- ->select();
- $lists = $lists?$lists:[];
- $shoud_total = 0;
- $ylac = 0;
- $ylic = 0;
- $gsc = 0;
- $syec = 0;
- $gjjc = 0;
- $qynjc = 0;
- $sb_total = 0;
- $njx = 0;
- $total = 0;
- 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($map1)
- ->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($map1)
- ->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($map1)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.ylic');
- $lists[$k]['gsc'] = Db::name('salary_record_sb')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map1)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.gsc');
- $lists[$k]['syec'] = Db::name('salary_record_sb')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map1)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.syec');
- $lists[$k]['gjjc'] = 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.gjjc');
- $lists[$k]['qynjc'] = Db::name('salary_record_sb')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map1)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.qynjc');
- $lists[$k]['njx'] = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map2)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.should_total');
- $lists[$k]['key'] = $k+1;
- $lists[$k]['sb_total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['syec'],2);
- $lists[$k]['total'] = round($lists[$k]['njx'] + $lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['syec'] + $lists[$k]['gjjc'] + $lists[$k]['qynjc'] + $lists[$k]['should_total'],2);
- $shoud_total += $lists[$k]['should_total'];
- $ylac += $lists[$k]['ylac'];
- $ylic += $lists[$k]['ylic'];
- $gsc += $lists[$k]['gsc'];
- $syec += $lists[$k]['syec'];
- $gjjc += $lists[$k]['gjjc'];
- $qynjc += $lists[$k]['qynjc'];
- $sb_total += $lists[$k]['sb_total'];
- $njx += $lists[$k]['njx'];
- $total += $lists[$k]['total'];
- }
- $lists[] = [
- 'salary_record_id' => 0,
- 'user_id' => 0,
- 'name' => '',
- 'key' => '合计',
- 'should_total' => $shoud_total,
- 'ylac' => $ylac,
- 'ylic' => $ylic,
- 'gsc' => $gsc,
- 'syec' => $syec,
- 'gjjc' => $gjjc,
- 'qynjc' => $qynjc,
- 'sb_total' => $sb_total,
- 'njx' => $njx,
- 'total' => $total,
- ];
- }else{
- $lists = Db::name('salary_record_log')
- ->alias('srs')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where($map)
- ->field('ui.name,srs.user_id,srs.salary_record_id')
- ->group('srs.user_id')
- ->distinct(true)
- ->order('ui.sorts3 asc')
- ->select();
- $lists = $lists?$lists:[];
- $shoud_total = 0;
- $njx = 0;
- $total = 0;
- 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($map1)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.should_total');
- $lists[$k]['njx'] = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map2)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.should_total');
- $lists[$k]['key'] = $k+1;
- $lists[$k]['total'] = round($lists[$k]['should_total']+$lists[$k]['njx'] + $lists[$k]['should_total'],2);
- $shoud_total += $lists[$k]['should_total'];
- $total += $lists[$k]['total'];
- $njx += $lists[$k]['njx'];
- }
- $lists[] = [
- 'salary_record_id' => 0,
- 'user_id' => 0,
- 'name' => '',
- 'key' => '合计',
- 'njx' => $njx,
- 'should_total' => $shoud_total,
- 'total' => $total,
- ];
- }
- $columns[] = ["title" => "序号","key" => "key"];
- $columns[] = ["title" => "姓名","key" => "name"];
- $columns[] = ["title" => "应发工资","key" => "shouldTotal"];
- if(in_array($type,[1,2])){
- $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" => "njx"];
- $columns[] = ["title" => "用工成本合计","key" => "total"];
- $data = [
- 'columns' => $columns,
- 'list' => $lists
- ];
- return $data;
- }
- public function workerYear($type,$year,$name){
- // 先取出所有补助项
- // 取出所有人并算出人员的全年合计
- // 取出每个人的记录明细
- $types = Db::name('salary_record')->where('month','like','%'.$year.'%')->where('status',2)->where('del',0)->where('type',$type)->column('types');
- $typeids = [];
- foreach ($types as $k=>$v){
- if($v){
- $typeids = array_merge($typeids,explode(',',$v));
- }
- }
- $typeids = array_unique($typeids);
- if($typeids){
- $tlist = Db::name('user_subsidies')->where('id','in',$typeids)->field('id,title,type,pay_type,cate')->order('sorts asc,id asc')->select();
- }else{
- $tlist = [];
- }
- $map[] = ['sr.status','=',2];
- $map[] = ['sr.del','=',0];
- $map2 = $map;
- $map[] = ['sr.type','=',$type];
- $map[] = ['sr.month','like','%'.$year.'%'];
- $map1 = $map;
- if($name){
- $map[] = ['ui.name','like','%'.$name.'%'];
- }
- $map2[] = ['sr.type','=',$type+5];
- $map2[] = ['sr.month','like','%'.($year+1).'%'];
- $lists = Db::name('salary_record_log')
- ->alias('srs')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where($map)
- ->field('ui.name,srs.user_id,ui.id,max(srs.pwlevel) as pwlevel,srs.psid')
- ->group('srs.user_id')
- ->distinct(true)
- ->order('ui.sorts3 asc,srs.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $data = [
- 'gwgz' => 0,
- 'xjgz' => 0,
- 'jcjx' => 0,
- 'yjx' => 0,
- 'tax' => 0,
- 'salary' => 0,
- 'should_total' => 0,
- 'sbgjj' => 0,
- 'dktotal' => 0,
- 'njxyf' => 0,
- 'njxtax' => 0,
- 'njx' => 0,
- 'alltotal' => 0
- ];
- $data['njxyf'] = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map2)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.should_total');
- $data['njx'] = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map2)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.salary');
- $data['njxtax'] = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map2)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.tax');
- // 积累值
- foreach ($tlist as $key=>$val){
- $data['subsidies'.$val['id']] = 0;
- }
- // 获取本年的详细信息
- $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'])
- ->where('d.enable',1)
- ->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');
- $lists[$k]['job_name'] = '';
- $children = Db::name('salary_record_log')
- ->alias('srs')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where($map1)
- ->where('srs.user_id',$v['user_id'])
- ->field('srs.*,ui.name,sr.month')
- ->order('sr.month asc,sr.id asc')
- ->select();
- $children = $children?$children:[];
- foreach ($children as $kk=>$vv){
- $data['gwgz'] += $vv['gwgz'];
- $data['xjgz'] += $vv['xjgz'];
- $data['jcjx'] += $vv['jcjx'];
- $data['yjx'] += $vv['yjx'];
- $data['tax'] += $vv['tax'];
- $data['salary'] += $vv['salary'];
- $data['should_total'] += $vv['should_total'];
- $data['sbgjj'] += $vv['sbgjj'];
- $btjson = json_decode($vv['btjson'],true);
- $btjson = $btjson?$btjson:[];
- foreach ($tlist as $key=>$val){
- $children[$kk]['subsidies'.$val['id']] = 0;
- foreach ($btjson as $bk=>$bv){
- if($val['id'] == $bv['id']){
- $data['subsidies'.$val['id']] += $bv['money'];
- $children[$kk]['subsidies'.$val['id']] = $bv['money'];
- }
- }
- }
- $kcmoney = 0;
- foreach ($btjson as $kkk=>$vvv){
- if($vvv['cate'] == 2){
- $kcmoney += $vvv['money'];
- }
- }
- $children[$kk]['dktotal'] = round($vv['sbgjj'] + $vv['tax'] + $kcmoney,2);
- $children[$kk]['dep_name'] = $lists[$k]['dep_name'];
- $children[$kk]['job_name'] = Db::name('post_salary')->where('id',$vv['psid'])->value('title');
- // $children[$kk]['job_name'] = $lists[$k]['job_name'];
- $lists[$k]['job_name'] = $children[$kk]['job_name'];
- $children[$kk]['njxyf'] = 0;
- $children[$kk]['njxtax'] = 0;
- $children[$kk]['njx'] = 0;
- $children[$kk]['alltotal'] = $children[$kk]['salary'];
- $data['dktotal'] += $children[$kk]['dktotal'];
- }
- $lists[$k]['children'] = $children?$children:[];
- $lists[$k]['month'] = '合计';
- // 处理精度问题
- $data['gwgz'] = round($data['gwgz'],2);
- $data['xjgz'] = round($data['xjgz'],2);
- $data['jcjx'] = round($data['jcjx'],2);
- $data['yjx'] = round($data['yjx'],2);
- $data['tax'] = round($data['tax'],2);
- $data['salary'] = round($data['salary'],2);
- $data['should_total'] = round($data['should_total'],2);
- $data['sbgjj'] = round($data['sbgjj'],2);
- $data['dktotal'] = round($data['dktotal'],2);
- foreach ($tlist as $key=>$val){
- $data['subsidies'.$val['id']] = round($data['subsidies'.$val['id']],2);
- }
- $data['alltotal'] = round($data['salary'] + $data['njx'],2);
- $lists[$k] = array_merge($lists[$k],$data);
- }
- return ['types' => $tlist,'lists' => $lists];
- }
- public function yearWorkerExport($type,$year,$name){
- // 先取出所有补助项
- // 取出所有人并算出人员的全年合计
- // 取出每个人的记录明细
- $types = Db::name('salary_record')->where('month','like','%'.$year.'%')->where('status',2)->where('del',0)->where('type',$type)->column('types');
- $typeids = [];
- foreach ($types as $k=>$v){
- if($v){
- $typeids = array_merge($typeids,explode(',',$v));
- }
- }
- $typeids = array_unique($typeids);
- if($typeids){
- $tlist = Db::name('user_subsidies')->where('id','in',$typeids)->field('id,title,type,pay_type,cate')->order('sorts asc,id asc')->select();
- }else{
- $tlist = [];
- }
- $map[] = ['sr.status','=',2];
- $map[] = ['sr.del','=',0];
- $map2 = $map;
- $map[] = ['sr.type','=',$type];
- $map[] = ['sr.month','like','%'.$year.'%'];
- $map1 = $map;
- if($name){
- $map[] = ['ui.name','like','%'.$name.'%'];
- }
- $map2[] = ['sr.type','=',$type+5];
- $map2[] = ['sr.month','like','%'.($year+1).'%'];
- $lists = Db::name('salary_record_log')
- ->alias('srs')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where($map)
- ->field('ui.name,srs.user_id,ui.id,srs.pwlevel,srs.psid')
- ->group('srs.user_id')
- ->distinct(true)
- ->order('ui.sorts3 asc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $data = [
- 'gwgz' => 0,
- 'xjgz' => 0,
- 'jcjx' => 0,
- 'yjx' => 0,
- 'tax' => 0,
- 'salary' => 0,
- 'should_total' => 0,
- 'sbgjj' => 0,
- 'dktotal' => 0,
- 'njxyf' => 0,
- 'njxtax' => 0,
- 'njx' => 0,
- 'alltotal' => 0
- ];
- $data['njxyf'] = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map2)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.should_total');
- $data['njx'] = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map2)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.salary');
- $data['njxtax'] = Db::name('salary_record_log')
- ->alias('srl')
- ->join('salary_record sr','sr.id = srl.salary_record_id')
- ->where($map2)
- ->where('srl.user_id',$v['user_id'])
- ->sum('srl.tax');
- // 积累值
- foreach ($tlist as $key=>$val){
- $data['subsidies'.$val['id']] = 0;
- }
- // 获取本年的详细信息
- $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'])
- ->where('d.enable',1)
- ->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');
- $lists[$k]['job_name'] = '';
- $children = Db::name('salary_record_log')
- ->alias('srs')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where($map1)
- ->where('srs.user_id',$v['user_id'])
- ->field('srs.*,ui.name,sr.month')
- ->order('sr.month asc,sr.id asc')
- ->select();
- $children = $children?$children:[];
- foreach ($children as $kk=>$vv){
- $data['gwgz'] += $vv['gwgz'];
- $data['xjgz'] += $vv['xjgz'];
- $data['jcjx'] += $vv['jcjx'];
- $data['yjx'] += $vv['yjx'];
- $data['tax'] += $vv['tax'];
- $data['salary'] += $vv['salary'];
- $data['should_total'] += $vv['should_total'];
- $data['sbgjj'] += $vv['sbgjj'];
- $btjson = json_decode($vv['btjson'],true);
- $btjson = $btjson?$btjson:[];
- foreach ($tlist as $key=>$val){
- $children[$kk]['subsidies'.$val['id']] = 0;
- foreach ($btjson as $bk=>$bv){
- if($val['id'] == $bv['id']){
- $data['subsidies'.$val['id']] += $bv['money'];
- $children[$kk]['subsidies'.$val['id']] = $bv['money'];
- }
- }
- }
- $kcmoney = 0;
- foreach ($btjson as $kkk=>$vvv){
- if($vvv['cate'] == 2){
- $kcmoney += $vvv['money'];
- }
- }
- $children[$kk]['dktotal'] = round($vv['sbgjj'] + $vv['tax'] + $kcmoney,2);
- $children[$kk]['dep_name'] = $lists[$k]['dep_name'];
- $children[$kk]['job_name'] = Db::name('post_salary')->where('id',$vv['psid'])->value('title');
- // $children[$kk]['job_name'] = $lists[$k]['job_name'];
- $lists[$k]['job_name'] = $children[$kk]['job_name'];
- $children[$kk]['njxyf'] = 0;
- $children[$kk]['njxtax'] = 0;
- $children[$kk]['njx'] = 0;
- $children[$kk]['alltotal'] = $children[$kk]['salary'];
- $data['dktotal'] += $children[$kk]['dktotal'];
- }
- $lists[$k]['children'] = $children?$children:[];
- $lists[$k]['month'] = '合计';
- // 处理精度问题
- $data['gwgz'] = round($data['gwgz'],2);
- $data['xjgz'] = round($data['xjgz'],2);
- $data['jcjx'] = round($data['jcjx'],2);
- $data['yjx'] = round($data['yjx'],2);
- $data['tax'] = round($data['tax'],2);
- $data['salary'] = round($data['salary'],2);
- $data['should_total'] = round($data['should_total'],2);
- $data['sbgjj'] = round($data['sbgjj'],2);
- $data['dktotal'] = round($data['dktotal'],2);
- foreach ($tlist as $key=>$val){
- $data['subsidies'.$val['id']] = round($data['subsidies'.$val['id']],2);
- }
- $data['alltotal'] = round($data['salary'] + $data['njx'],2);
- $lists[$k] = array_merge($lists[$k],$data);
- }
- $newList = [];
- foreach ($lists as $k=>$v){
- $children = $v['children'];
- unset($v['children']);
- $newList[] = $v;
- foreach ($children as $kk=>$vv){
- $newList[] = $vv;
- }
- }
- $columns[] = ["title" => "姓名","key" => "name"];
- $columns[] = ["title" => "月份","key" => "month"];
- $columns[] = ["title" => "部门","key" => "depName"];
- $columns[] = ["title" => "职级","key" => "jobName"];
- $columns[] = ["title" => "薪级","key" => "pwlevel"];
- $columns[] = ["title" => "岗位工资","key" => "gwgz"];
- $columns[] = ["title" => "薪级工资","key" => "xjgz"];
- foreach ($tlist as $k=>$v){
- if($v['cate'] == 1){
- $columns[] = ["title" => $v['title'],"key" => "subsidies".$v['id']];
- }
- }
- $columns[] = ["title" => "基础绩效","key" => "jcjx"];
- $columns[] = ["title" => "月绩效","key" => "yjx"];
- $columns[] = ["title" => "应发工资","key" => "shouldTotal"];
- foreach ($tlist as $k=>$v){
- if($v['cate'] == 2){
- $columns[] = ["title" => $v['title'],"key" => "subsidies".$v['id']];
- }
- }
- $columns[] = ["title" => "社保公积金代扣","key" => "sbgjj"];
- $columns[] = ["title" => "个税","key" => "tax"];
- $columns[] = ["title" => "代扣合计","key" => "dktotal"];
- $columns[] = ["title" => "实发工资","key" => "salary"];
- $columns[] = ["title" => "应发年绩效","key" => "njxyf"];
- $columns[] = ["title" => "年绩效个税","key" => "njxtax"];
- $columns[] = ["title" => "实发年绩效","key" => "njx"];
- $columns[] = ["title" => "总实发","key" => "alltotal"];
- $data = [
- 'columns' => $columns,
- 'list' => $newList
- ];
- return $data;
- }
- public function userWorkerYear($type,$year,$userId){
- // 先取出所有补助项
- // 取出所有人并算出人员的全年合计
- // 取出每个人的记录明细
- if($type != 5){
- $types = Db::name('salary_record')
- ->alias('a')
- ->join('salary_record_log b','b.salary_record_id = a.id')
- ->where('a.month','like',$year.'%')
- ->where('a.status',2)
- ->where('a.del',0)
- ->where('a.type',$type)
- ->where('b.user_id',$userId)
- ->column('a.types');
- }else{
- $types = Db::name('salary_record')
- ->alias('a')
- ->join('user_retired_salary b','b.salary_record_id = a.id')
- ->where('a.month','like','%'.$year.'%')
- ->where('a.status',2)
- ->where('a.del',0)
- ->where('a.type',$type)
- ->where('b.user_id',$userId)
- ->column('a.types');
- }
- $types = $types?$types:[];
- $typeids = [];
- foreach ($types as $k=>$v){
- if($v){
- $typeids = array_merge($typeids,explode(',',$v));
- }
- }
- $typeids = array_unique($typeids);
- if($typeids){
- $tlist = Db::name('user_subsidies')->where('id','in',$typeids)->field('id,title,type,pay_type,cate')->order('sorts asc,id asc')->select();
- }else{
- $tlist = [];
- }
- $map[] = ['sr.type','=',$type];
- $map[] = ['sr.month','like',$year.'%'];
- $map[] = ['sr.status','=',2];
- $map[] = ['sr.del','=',0];
- $map[] = ['srs.user_id','=',$userId];
- $map1 = $map;
- $lists = Db::name('salary_record_log')
- ->alias('srs')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where($map)
- ->field('ui.name,srs.user_id,ui.id,srs.pwlevel,srs.psid,srs.njx')
- ->group('srs.user_id')
- ->distinct(true)
- ->order('ui.sorts3 asc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- if($type != 5){
- $data = [
- 'njx' => 0,
- 'gwgz' => 0,
- 'xjgz' => 0,
- 'jcjx' => 0,
- 'yjx' => 0,
- 'tax' => 0,
- 'salary' => 0,
- 'should_total' => 0,
- 'sbgjj' => 0,
- 'dktotal' => 0,
- 'level' => '',
- 'factor' => '',
- 'months' => '',
- 'taxrate' => '',
- 'gjjp' => 0,
- 'qynjp' => 0,
- 'ylap' => 0,
- 'syep' => 0,
- 'ylip' => 0,
- ];
- }else{
- $data = [
- 'bz' => 0,
- 'zysdbt' => 0,
- 'fb' => 0,
- 'wybt' => 0,
- 'salary' => 0,
- 'dktotal' => 0,
- ];
- }
- // 积累值
- foreach ($tlist as $key=>$val){
- $data['subsidies'.$val['id']] = 0;
- }
- // 获取本年的详细信息
- $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'])
- ->where('d.enable',1)
- ->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');
- $lists[$k]['job_name'] = '';
- $children = Db::name('salary_record_log')
- ->alias('srs')
- ->join('user_info ui','ui.user_id = srs.user_id')
- ->join('salary_record sr','sr.id = srs.salary_record_id')
- ->where($map1)
- ->where('srs.user_id',$v['user_id'])
- ->field('srs.*,ui.name,sr.month')
- ->order('sr.month asc,sr.id asc')
- ->select();
- $children = $children?$children:[];
- foreach ($children as $kk=>$vv){
- if($type != 5){
- if(in_array($type,[1,2,3,4])){
- $data['gwgz'] += $vv['gwgz'];
- $data['xjgz'] += $vv['xjgz'];
- $data['jcjx'] += $vv['jcjx'];
- $data['yjx'] += $vv['yjx'];
- }else if(in_array($type,[6,7,8,9])){
- $data['njx'] += $vv['njx'];
- }
- $data['tax'] += $vv['tax'];
- $data['should_total'] += $vv['should_total'];
- $data['sbgjj'] += $vv['sbgjj'];
- if($type == 1||$type == 2){
- $sb = Db::name('salary_record_sb')->where('salary_record_id',$vv['salary_record_id'])->where('user_id',$vv['user_id'])->find();
- $children[$kk]['gjjp'] = $sb?$sb['gjjp']:0;
- $children[$kk]['qynjp'] = $sb?$sb['qynjp']:0;
- $children[$kk]['ylap'] = $sb?$sb['ylap']:0;
- $children[$kk]['syep'] = $sb?$sb['syep']:0;
- $children[$kk]['ylip'] = $sb?$sb['ylip']:0;
- $data['gjjp'] += $children[$kk]['gjjp'];
- $data['qynjp'] += $children[$kk]['qynjp'];
- $data['ylap'] += $children[$kk]['ylap'];
- $data['syep'] += $children[$kk]['syep'];
- $data['ylip'] += $children[$kk]['ylip'];
- }
- }else{
- $data['bz'] += $vv['bz'];
- $data['zysdbt'] += $vv['zysdbt'];
- $data['fb'] += $vv['fb'];
- $data['wybt'] += $vv['wybt'];
- }
- $data['salary'] += $vv['salary'];
- $btjson = json_decode($vv['btjson'],true);
- $btjson = $btjson?$btjson:[];
- foreach ($tlist as $key=>$val){
- $children[$kk]['subsidies'.$val['id']] = 0;
- foreach ($btjson as $bk=>$bv){
- if($val['id'] == $bv['id']){
- $data['subsidies'.$val['id']] += $bv['money'];
- $children[$kk]['subsidies'.$val['id']] = $bv['money'];
- }
- }
- }
- $kcmoney = 0;
- foreach ($btjson as $kkk=>$vvv){
- if($vvv['cate'] == 2){
- $kcmoney += $vvv['money'];
- }
- }
- if($type != 5){
- $children[$kk]['dktotal'] = round($vv['sbgjj'] + $vv['tax'] + $kcmoney,2);
- }else{
- $children[$kk]['dktotal'] = round($kcmoney,2);
- }
- $children[$kk]['dep_name'] = $lists[$k]['dep_name'];
- //职级
- $children[$kk]['job_name'] = Db::name('post_salary')->where('id',$vv['psid'])->value('title');
- // $children[$kk]['job_name'] = $lists[$k]['job_name'];
- $lists[$k]['job_name'] = $children[$kk]['job_name'];
- $data['dktotal'] += $children[$kk]['dktotal'];
- }
- $lists[$k]['children'] = $children?$children:[];
- $lists[$k]['month'] = '合计';
- // 处理精度问题
- if($type != 5){
- $data['gwgz'] = round($data['gwgz'],2);
- $data['xjgz'] = round($data['xjgz'],2);
- $data['jcjx'] = round($data['jcjx'],2);
- $data['yjx'] = round($data['yjx'],2);
- $data['tax'] = round($data['tax'],2);
- $data['should_total'] = round($data['should_total'],2);
- $data['sbgjj'] = round($data['sbgjj'],2);
- $data['gjjp'] = round($data['gjjp'],2);
- $data['qynjp'] = round($data['qynjp'],2);
- $data['ylap'] = round($data['ylap'],2);
- $data['syep'] = round($data['syep'],2);
- $data['ylip'] = round($data['ylip'],2);
- }else{
- $data['bz'] = round($data['bz'],2);
- $data['zysdbt'] = round($data['zysdbt'],2);
- $data['fb'] = round($data['fb'],2);
- $data['wybt'] = round($data['wybt'],2);
- }
- $data['salary'] = round($data['salary'],2);
- $data['dktotal'] = round($data['dktotal'],2);
- foreach ($tlist as $key=>$val){
- $data['subsidies'.$val['id']] = round($data['subsidies'.$val['id']],2);
- }
- $lists[$k] = array_merge($lists[$k],$data);
- }
- return ['types' => $tlist,'lists' => $lists];
- }
- // 获取用户发放工资的所有类型
- public function getUserSalayrTypes($userId){
- $types1 = Db::name('salary_record')
- ->alias('a')
- ->join('salary_record_log b','b.salary_record_id = a.id')
- ->where('a.status',2)
- ->where('a.del',0)
- ->where('b.user_id',$userId)
- ->group('a.type')
- ->distinct(true)
- ->column('a.type');
- $types1 = $types1?$types1:[];
- $types2 = Db::name('salary_record')
- ->alias('a')
- ->join('user_retired_salary b','b.salary_record_id = a.id')
- ->where('a.status',2)
- ->where('a.del',0)
- ->where('b.user_id',$userId)
- ->group('a.type')
- ->order('a.type asc')
- ->distinct(true)
- ->column('a.type');
- $types2 = $types2?$types2:[];
- $types = array_merge($types1,$types2);
- $list = [];
- foreach ($types as $v){
- $a['id'] = $v;
- if($v == 1){
- $a['title'] = '中心编制';
- } else if ($v == 2){
- $a['title'] = '新华聘用';
- } else if ($v == 3){
- $a['title'] = '中心劳务';
- } else if ($v == 4){
- $a['title'] = '新华劳务';
- }else if($v == 6){
- $a['title'] = '中心编制年绩效';
- } else if ($v == 7){
- $a['title'] = '新华聘用年绩效';
- } else if ($v == 8){
- $a['title'] = '中心劳务年绩效';
- } else if ($v == 9){
- $a['title'] = '新华劳务年绩效';
- }else{
- $a['title'] = '中心退休';
- }
- $list[] = $a;
- }
- return $list;
- }
- }
|