SalaryRecord.php 188 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class SalaryRecord extends Model
  8. {
  9. // 工资发放
  10. public function add(){
  11. $data = [
  12. 'type' => input('type/d',0),
  13. 'month' => input('month','','trim'),
  14. 'types' => input('types','','trim'),
  15. 'remark' => input('remark','','trim'),
  16. 'user_id' => input('userId/d',0),
  17. 'users' => input('users','','trim')
  18. ];
  19. $orgId = input('orgId/d',0);
  20. $result = validate('SalaryRecord')->check($data,[],'');
  21. if(true !== $result){
  22. HelpHander::error(validate('SalaryRecord')->getError());
  23. }
  24. // 检查是否已设置审批流程
  25. $apply = model('SalaryApply')->all2($data['type']);
  26. if(!$apply){
  27. HelpHander::error('该类型工资的发放未设置审批节点');
  28. }
  29. $curinfo = $apply[0];
  30. $data['apply'] = json_encode($apply);
  31. $data['cur_node'] = $curinfo['id'];
  32. Db::startTrans();
  33. try{
  34. $users = json_decode($data['users'],true);
  35. unset($data['users']);
  36. $sdata = $data;
  37. $sdata['nums'] = count($users);
  38. $sdata['create_time'] = date('Y-m-d H:i:s');
  39. $salaryId = Db::name('SalaryRecord')->insertGetId($sdata);
  40. if(!$salaryId){
  41. \exception('操作失败');
  42. }
  43. $data['users'] = $users;
  44. $data['salary_record_id'] = $salaryId;
  45. if($data['type'] == 5){ //1=中心编制 2=新华聘用 3=中心劳务 4=新华劳务 5=中心退休
  46. $ret = model('UserRetiredSalary')->sendSalary($data);
  47. }else if(in_array($data['type'],[1,2,3,4])){
  48. $ret = model('SalaryRecord')->sendSalary($data);
  49. } else{ // 年绩效 6=中心编制 7=新华聘用 8=中心劳务 9=新华劳务
  50. $ret = model('SalaryRecord')->sendSalaryJx($data);
  51. }
  52. if(!$ret){
  53. \exception('操作失败');
  54. }
  55. // 生成审核记录
  56. $uids = explode(',',$curinfo['userids']);
  57. foreach ($uids as $k=>$v){
  58. $sd = [
  59. 'salary_record_id' => $salaryId,
  60. 'user_id' => $v,
  61. 'status' => 0,
  62. 'create_time' => date('Y-m-d H:i:s'),
  63. 'salary_apply_id' => $curinfo['id'],
  64. 'sign' => $curinfo['sign'],
  65. ];
  66. $ret = Db::name('salary_apply_record')->insertGetId($sd);
  67. if(!$ret){
  68. \exception('操作失败');
  69. }
  70. model('Message')->add(3,$salaryId,12,$v,$orgId,'工资流程待您的审核确认');
  71. }
  72. if(!$ret){
  73. \exception('操作失败');
  74. }
  75. Db::commit();
  76. }catch (Exception $e){
  77. Db::rollback();
  78. HelpHander::error($e->getMessage());
  79. }
  80. return true;
  81. }
  82. // 重新发工资
  83. public function resend(){
  84. $salaryRecordId = input('salaryRecordId/d',0);
  85. $id = input('id/d',0);
  86. $users = input('users','','trim');
  87. $userId = input('userId/d',0);
  88. $info = Db::name('salary_record')->where('id',$salaryRecordId)->where('del',0)->find();
  89. if(!$info){
  90. HelpHander::error('记录不存在');
  91. }
  92. if($info['status'] != 0 && $info['status'] != 3){ //审核中或已打回
  93. HelpHander::error('无权限操作');
  94. }
  95. if($info['status'] == 3 && $info['user_id'] != $userId){
  96. HelpHander::error('无权限操作');
  97. }
  98. if($info['status'] == 0){
  99. $sar = Db::name('salary_apply_record')
  100. ->where('salary_apply_id',$info['cur_node'])
  101. ->where('salary_record_id',$salaryRecordId)
  102. ->where('user_id',$userId)
  103. ->where('status',0)
  104. ->where('del',0)
  105. ->find();
  106. if(!$sar){
  107. HelpHander::error('无权限修改3');
  108. }
  109. }
  110. $users = json_decode($users,true);
  111. if(!$users){
  112. HelpHander::error('参数错误');
  113. }
  114. Db::startTrans();
  115. try{
  116. if($info['type'] === 5){
  117. $ret = model('UserRetiredSalary')->sendOneSalary($users[0],$info,$id);
  118. }else if(in_array($info['type'],[1,2,3,4])){
  119. $ret = model('SalaryRecord')->sendOneSalary($users[0],$info,$id);
  120. }else{
  121. $ret = model('SalaryRecord')->sendOneSalaryJx($users[0],$info,$id);
  122. }
  123. if(!$ret){
  124. \exception('操作失败');
  125. }
  126. Db::commit();
  127. }catch (Exception $e){
  128. Db::rollback();
  129. HelpHander::error($e->getMessage());
  130. }
  131. return true;
  132. }
  133. // 删除工资项
  134. public function delsalary(){
  135. $salaryRecordId = input('salaryRecordId/d',0);
  136. $tids = input('tids','','trim');
  137. if(!$tids){
  138. HelpHander::error('未选择要删除的工资项');
  139. }
  140. $tids = array_unique(explode(',',$tids));
  141. $userId = input('userId/d',0);
  142. $orgId = input('orgId/d',0);
  143. $info = Db::name('salary_record')->where('id',$salaryRecordId)->where('del',0)->find();
  144. if(!$info){
  145. HelpHander::error('记录不存在');
  146. }
  147. if($info['status'] != 0 && $info['status'] != 3){ //审核中或已打回
  148. HelpHander::error('无权限操作');
  149. }
  150. if(!$info['types']){
  151. HelpHander::error('无可删除工资项');
  152. }
  153. if($info['status'] == 0){
  154. $sar = Db::name('salary_apply_record')
  155. ->where('salary_apply_id',$info['cur_node'])
  156. ->where('salary_record_id',$salaryRecordId)
  157. ->where('user_id',$userId)
  158. ->where('status',0)
  159. ->where('del',0)
  160. ->find();
  161. if(!$sar){
  162. HelpHander::error('无权限修改');
  163. }
  164. }
  165. $ntypes = [];
  166. $otypes = explode(',',$info['types']);
  167. foreach ($otypes as $v){
  168. if(!in_array($v,$tids)){
  169. $ntypes[] = $v;
  170. }
  171. }
  172. $info['types'] = !empty($ntypes)?implode(',',$ntypes):'';
  173. Db::startTrans();
  174. try{
  175. // 更新工资项
  176. $ret = Db::name('salary_record')->where('id',$salaryRecordId)->update([
  177. 'types' => $info['types'],
  178. ]);
  179. if(!$ret){
  180. \exception('操作失败');
  181. }
  182. // 重新计算每个人的工资
  183. $logs = Db::name('salary_record_log')->where('salary_record_id',$salaryRecordId)->field('id,user_id')->select();
  184. foreach ($logs as $k=>$v){
  185. $rdata = array_change_line_to_hump($this->reloadAdd($salaryRecordId,$v['user_id'],$orgId,0));
  186. $types = $rdata['types'];
  187. $users = $rdata['list'];
  188. foreach ($users as $uk=>$uv){
  189. foreach ($types as $tk=>$tv){
  190. if(in_array($tv['id'],$tids)){
  191. unset($users[$uk]['subsidies'.$tv['id']]);
  192. }
  193. }
  194. }
  195. if($info['type'] === 5){
  196. $ret = model('UserRetiredSalary')->sendOneSalary($users[0],$info,$v['id']);
  197. }else if(in_array($info['type'],[1,2,3,4])){
  198. $ret = model('SalaryRecord')->sendOneSalary($users[0],$info,$v['id']);
  199. }else{
  200. $ret = model('SalaryRecord')->sendOneSalaryJx($users[0],$info,$v['id']);
  201. }
  202. if(!$ret){
  203. \exception('操作失败');
  204. }
  205. }
  206. Db::commit();
  207. }catch (Exception $e){
  208. Db::rollback();
  209. HelpHander::error($e->getMessage());
  210. }
  211. return true;
  212. }
  213. // 添加工资项
  214. public function addsalary(){
  215. $salaryRecordId = input('salaryRecordId/d',0);
  216. $tids = input('tids','','trim');
  217. if(!$tids){
  218. HelpHander::error('未选择要添加的工资项');
  219. }
  220. $tids = array_unique(explode(',',$tids));
  221. $userId = input('userId/d',0);
  222. $orgId = input('orgId/d',0);
  223. $info = Db::name('salary_record')->where('id',$salaryRecordId)->where('del',0)->find();
  224. if(!$info){
  225. HelpHander::error('记录不存在');
  226. }
  227. if($info['status'] != 0 && $info['status'] != 3){ //审核中或已打回
  228. HelpHander::error('无权限操作');
  229. }
  230. if($info['status'] == 0){
  231. $sar = Db::name('salary_apply_record')
  232. ->where('salary_apply_id',$info['cur_node'])
  233. ->where('salary_record_id',$salaryRecordId)
  234. ->where('user_id',$userId)
  235. ->where('status',0)
  236. ->where('del',0)
  237. ->find();
  238. if(!$sar){
  239. HelpHander::error('无权限修改');
  240. }
  241. }
  242. $postusers = input('users','','trim');
  243. $postusers = json_decode($postusers,true);
  244. if(!$postusers){
  245. HelpHander::error('参数错误');
  246. }
  247. $otypes = explode(',',$info['types']);
  248. if(array_intersect($tids,$otypes)){ // 有交集说明参数错误
  249. HelpHander::error('部分工资项已存在,不能添加');
  250. }
  251. $ntypes = $otypes;
  252. foreach ($tids as $v){
  253. if(!in_array($v,$otypes)){
  254. $ntypes[] = $v;
  255. }
  256. }
  257. $info['types'] = !empty($ntypes)?implode(',',$ntypes):'';
  258. Db::startTrans();
  259. try{
  260. // 更新工资项
  261. $ret = Db::name('salary_record')->where('id',$salaryRecordId)->update([
  262. 'types' => $info['types'],
  263. ]);
  264. if(!$ret){
  265. \exception('操作失败');
  266. }
  267. // 重新计算每个人的工资
  268. $logs = Db::name('salary_record_log')->where('salary_record_id',$salaryRecordId)->field('id,user_id')->select();
  269. foreach ($logs as $k=>$v){
  270. $rdata = array_change_line_to_hump($this->reloadAdd($salaryRecordId,$v['user_id'],$orgId,0));
  271. $users = $rdata['list'];
  272. foreach ($users as $uk=>$uv){
  273. $userSn = think_encrypt($uv['userId']);
  274. foreach ($tids as $tk=>$tv){
  275. foreach ($postusers as $puk=>$puv){
  276. if($userSn == $puv['userSn']){
  277. $users[$uk]['subsidies'.$tv] = !empty($puv['subsidies'.$tv])?$puv['subsidies'.$tv]:0;
  278. break;
  279. }
  280. }
  281. }
  282. }
  283. if($info['type'] === 5){
  284. $ret = model('UserRetiredSalary')->sendOneSalary($users[0],$info,$v['id']);
  285. }else if(in_array($info['type'],[1,2,3,4])){
  286. $ret = model('SalaryRecord')->sendOneSalary($users[0],$info,$v['id']);
  287. }else{
  288. $ret = model('SalaryRecord')->sendOneSalaryJx($users[0],$info,$v['id']);
  289. }
  290. if(!$ret){
  291. \exception('操作失败');
  292. }
  293. }
  294. Db::commit();
  295. }catch (Exception $e){
  296. Db::rollback();
  297. HelpHander::error($e->getMessage());
  298. }
  299. return true;
  300. }
  301. // 修改工资项
  302. public function modifysalary(){
  303. $salaryRecordId = input('salaryRecordId/d',0);
  304. $tids = input('tids','','trim');
  305. if(!$tids){
  306. HelpHander::error('未选择要修改的工资项');
  307. }
  308. $tids = array_unique(explode(',',$tids));
  309. $userId = input('userId/d',0);
  310. $orgId = input('orgId/d',0);
  311. $info = Db::name('salary_record')->where('id',$salaryRecordId)->where('del',0)->find();
  312. if(!$info){
  313. HelpHander::error('记录不存在');
  314. }
  315. if($info['status'] != 0 && $info['status'] != 3){ //审核中或已打回
  316. HelpHander::error('无权限操作');
  317. }
  318. if($info['status'] == 0){
  319. $sar = Db::name('salary_apply_record')
  320. ->where('salary_apply_id',$info['cur_node'])
  321. ->where('salary_record_id',$salaryRecordId)
  322. ->where('user_id',$userId)
  323. ->where('status',0)
  324. ->where('del',0)
  325. ->find();
  326. if(!$sar){
  327. HelpHander::error('无权限修改');
  328. }
  329. }
  330. $postusers = input('users','','trim');
  331. $postusers = json_decode($postusers,true);
  332. if(!$postusers){
  333. HelpHander::error('参数错误');
  334. }
  335. $otypes = explode(',',$info['types']);
  336. if(array_intersect($tids,$otypes) != $tids){ // 不是子集
  337. HelpHander::error('修改的工资项不存在');
  338. }
  339. Db::startTrans();
  340. try{
  341. // 重新计算每个人的工资
  342. $logs = Db::name('salary_record_log')->where('salary_record_id',$salaryRecordId)->field('id,user_id')->select();
  343. foreach ($logs as $k=>$v){
  344. $rdata = array_change_line_to_hump($this->reloadAdd($salaryRecordId,$v['user_id'],$orgId,0));
  345. $users = $rdata['list'];
  346. foreach ($users as $uk=>$uv){
  347. $userSn = think_encrypt($uv['userId']);
  348. foreach ($tids as $tk=>$tv){
  349. foreach ($postusers as $puk=>$puv){
  350. if($userSn == $puv['userSn']){
  351. $users[$uk]['subsidies'.$tv] = !empty($puv['subsidies'.$tv])?$puv['subsidies'.$tv]:0;
  352. break;
  353. }
  354. }
  355. }
  356. }
  357. if($info['type'] === 5){
  358. $ret = model('UserRetiredSalary')->sendOneSalary($users[0],$info,$v['id']);
  359. }else if(in_array($info['type'],[1,2,3,4])){
  360. $ret = model('SalaryRecord')->sendOneSalary($users[0],$info,$v['id']);
  361. }else{
  362. $ret = model('SalaryRecord')->sendOneSalaryJx($users[0],$info,$v['id']);
  363. }
  364. if(!$ret){
  365. \exception('操作失败');
  366. }
  367. }
  368. Db::commit();
  369. }catch (Exception $e){
  370. Db::rollback();
  371. HelpHander::error($e->getMessage());
  372. }
  373. return true;
  374. }
  375. // 重新计算个税
  376. public function resendtax(){
  377. $id = input('id/d',0);
  378. $userId = input('userId/d',0);
  379. $log = Db::name('salary_record_log')->where('id',$id)->find();
  380. if(!$log){
  381. HelpHander::error('记录不存在');
  382. }
  383. $salaryRecordId = $log['salary_record_id'];
  384. $info = Db::name('salary_record')->where('id',$salaryRecordId)->where('del',0)->find();
  385. if(!$info){
  386. HelpHander::error('记录不存在');
  387. }
  388. if($info['status'] != 0){
  389. HelpHander::error('无权限操作');
  390. }
  391. $sar = Db::name('salary_apply_record')
  392. ->where('salary_apply_id',$info['cur_node'])
  393. ->where('salary_record_id',$salaryRecordId)
  394. ->where('user_id',$userId)
  395. ->where('status',0)
  396. ->where('del',0)
  397. ->find();
  398. if(!$sar){
  399. HelpHander::error('无权限修改');
  400. }
  401. if(!in_array($info['type'],[1,2])){
  402. HelpHander::error('无权限修改');
  403. }
  404. $sbdata = Db::name('salary_record_sb')->where('user_id',$log['user_id'])->where('salary_record_id',$salaryRecordId)->find();
  405. $jbsalary = $log['gwgz'] + $log['xjgz'] + $log['jcjx'] + $log['yjx'];
  406. $srid = Db::name('salary_record')
  407. ->alias('sr')
  408. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  409. ->where('sr.month',$info['month'])
  410. ->where('srs.user_id',$log['user_id'])
  411. ->where('sr.type',$info['type'])
  412. ->where('sr.del',0)
  413. ->order('sr.id desc')
  414. ->value('sr.id');
  415. $spdata = $this->getSalaryTax($log['should_total'],$sbdata,$log['user_id'],$log['company_id'],$info['month'],$info['type'],$jbsalary,$srid); // 计算个税
  416. $tax = $spdata['tax'];
  417. if($tax == $log['tax']){
  418. return true;
  419. }
  420. Db::startTrans();
  421. try{
  422. $salary = $log['salary'] - ($tax - $log['tax']);
  423. $sdata = [
  424. 'tax' => $tax,
  425. 'salary' => $salary,
  426. ];
  427. $ret = Db::name('salary_record_log')->where('id',$id)->update($sdata);
  428. if(!$ret){
  429. \exception('操作失败');
  430. }
  431. $spdata['salary_record_id'] = $salaryRecordId;
  432. $spdata['user_id'] = $log['user_id'];
  433. $m = intval(date('m',strtotime($info['month'].'-01')));
  434. if($m == 1 && $spdata['type'] != 1){ // 一月的第一次发工资
  435. $spdata['tax'] = 0;
  436. }
  437. $ret = Db::name('salary_record_special')
  438. ->where('salary_record_id',$salaryRecordId)
  439. ->where('user_id',$log['user_id'])
  440. ->strict(false)
  441. ->update($spdata);
  442. if(!$ret){
  443. \exception('操作失败');
  444. }
  445. Db::commit();
  446. }catch (Exception $e){
  447. Db::rollback();
  448. HelpHander::error($e->getMessage());
  449. }
  450. return true;
  451. }
  452. private function sendOneSalary($user,$data,$id){
  453. $user['psmoney'] = (float)$user['psmoney'];
  454. $user['pwmoney'] = (float)$user['pwmoney'];
  455. $user['fixedPerformance'] = (float)$user['fixedPerformance'];
  456. $user['psmonth'] = (float)$user['psmonth'];
  457. $types = [];
  458. if($data['types']){
  459. $types = Db::name('user_subsidies')
  460. ->where('id','in',$data['types'])
  461. ->where('del',0)
  462. ->order('sorts asc,id asc')
  463. ->select();
  464. }
  465. if($data['type'] == 3||$data['type'] == 4){ // 劳务费
  466. $sdata = [
  467. 'salary_record_id' => $data['id'],
  468. 'user_id' => $user['userId'],
  469. 'gwgz' => $user['psmoney'],
  470. 'xjgz' => $user['pwmoney'],
  471. 'jcjx' => $user['fixedPerformance'],
  472. 'yjx' => $user['psmonth'],
  473. 'pwlevel' => $user['pwlevel'],
  474. 'psid' => $user['psid'],
  475. 'type' => $user['type'],
  476. 'company_id' => $user['companyId'],
  477. 'child' => 0
  478. ];
  479. $should_total = $user['psmoney'] + $user['pwmoney'] + $user['fixedPerformance'] + $user['psmonth'];
  480. $jbsalary = $should_total;
  481. $btjson = [];
  482. $kcsalary = 0;
  483. foreach ($user as $key=>$val){
  484. if(strpos($key,'subsidies') === 0){
  485. foreach ($types as $kk=>$vv){
  486. if($vv['id'] == str_ireplace('subsidies','',$key)){
  487. // $val = (float)$val > 0?(float)$val:0; // 不填或小于0的值默认成0
  488. $val = (float)$val; // 支持负值
  489. $btjson[] = [
  490. 'id' => $vv['id'],
  491. 'cate' => $vv['cate'],
  492. 'title' => $vv['title'],
  493. 'money' => $val
  494. ];
  495. if($vv['cate'] == 1){ // 补贴
  496. $should_total += $val;
  497. }else{
  498. $kcsalary += $val;
  499. }
  500. }
  501. }
  502. }
  503. }
  504. $sdata['should_total'] = $should_total;
  505. $sdata['tax'] = $this->getLaborTax($should_total,$user['userId']);
  506. $sdata['salary'] = round($should_total - $sdata['tax'] - $sdata['child'] - $kcsalary,2);
  507. if($sdata['salary'] < 0){
  508. return false;
  509. }
  510. $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
  511. $res = Db::name('salary_record_log')->where('id',$id)->update($sdata);
  512. if($res === false){
  513. return false;
  514. }
  515. }else if($data['type'] == 1||$data['type'] == 2){ // 新华聘算社保及累计数
  516. $sdata = [
  517. 'salary_record_id' => $data['id'],
  518. 'user_id' => $user['userId'],
  519. 'gwgz' => $user['psmoney'],
  520. 'xjgz' => $user['pwmoney'],
  521. 'jcjx' => $user['fixedPerformance'],
  522. 'yjx' => $user['psmonth'],
  523. 'pwlevel' => $user['pwlevel'],
  524. 'psid' => $user['psid'],
  525. 'type' => $user['type'],
  526. 'company_id' => $user['companyId'],
  527. 'child' => 0
  528. ];
  529. $should_total = $user['psmoney'] + $user['pwmoney'] + $user['fixedPerformance'] + $user['psmonth'];
  530. $jbsalary = $should_total; // 基本工资,如果不发基本工资,专项附件不累计,如果发基本工资,按正常工资走
  531. $btjson = [];
  532. $kcsalary = 0;
  533. $bkdata = [
  534. 'ggj' => 0, // 补扣公积金
  535. 'qynj' => 0, // 补扣企业年金
  536. 'yla' => 0, // 补扣养老保险
  537. 'yli' => 0, // 补扣医疗保险
  538. 'sye' => 0 // 补扣失业保险
  539. ];
  540. foreach ($user as $key=>$val){
  541. if(strpos($key,'subsidies') === 0){
  542. foreach ($types as $kk=>$vv){
  543. if($vv['id'] == str_ireplace('subsidies','',$key)){
  544. // $val = (float)$val > 0?(float)$val:0; // 不填或小于0的值默认成0
  545. $val = (float)$val; // 支持负值
  546. $btjson[] = [
  547. 'id' => $vv['id'],
  548. 'cate' => $vv['cate'],
  549. 'title' => $vv['title'],
  550. 'money' => $val,
  551. 'sbtype' => $vv['sbtype']
  552. ];
  553. $vvl = 0;
  554. if($vv['cate'] == 1){ // 补贴
  555. $should_total += $val;
  556. $vvl = $val;
  557. }else{
  558. $kcsalary += $val;
  559. $vvl = -$val;
  560. }
  561. // 是否关联社保公积金
  562. // 1=公积金 2=企业年金 3=养老保险 4=医疗保险 5=失业保险
  563. if($vv['sbtype'] == 1){
  564. $bkdata['ggj'] += $vvl;
  565. }else if($vv['sbtype'] == 2){
  566. $bkdata['qynj'] += $vvl;
  567. }else if($vv['sbtype'] == 3){
  568. $bkdata['yla'] += $vvl;
  569. }else if($vv['sbtype'] == 4){
  570. $bkdata['yli'] += $vvl;
  571. }else if($vv['sbtype'] == 5){
  572. $bkdata['sye'] += $vvl;
  573. }
  574. }
  575. }
  576. }
  577. }
  578. $sbdata = $this->getShebao($user['userId'],$user['companyId'],$user,$jbsalary);
  579. $sbdata['salary_record_id'] = $data['id'];
  580. $sbdata['user_id'] = $user['userId'];
  581. // 社保扣除金额
  582. $sbkc = $sbdata['gjjp'] + $sbdata['qynjp'] +$sbdata['ylip'] +$sbdata['syep'] +$sbdata['ylap'];
  583. $sdata['should_total'] = $should_total;
  584. $sdata['sbgjj'] = $sbkc;
  585. $srid = Db::name('salary_record')
  586. ->alias('sr')
  587. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  588. ->where('sr.month',$data['month'])
  589. ->where('srs.user_id',$user['userId'])
  590. ->where('sr.type',$data['type'])
  591. ->where('sr.del',0)
  592. ->order('sr.id desc')
  593. ->value('sr.id');
  594. $spdata = $this->getSalaryTax($should_total,$sbdata,$user['userId'],$user['companyId'],$data['month'],$data['type'],$jbsalary,$srid,$bkdata); // 计算个税
  595. $spdata['salary_record_id'] = $data['id'];
  596. $spdata['user_id'] = $user['userId'];
  597. $tax = $spdata['tax'];
  598. $m = intval(date('m',strtotime($data['month'].'-01')));
  599. if($m == 1 && $spdata['type'] != 1){ // 一月的第一次发工资
  600. $spdata['tax'] = 0;
  601. }else if($m == 1 && $spdata['type'] == 1){ // 补交不算税
  602. /*$count = Db::name('salary_record')
  603. ->alias('sr')
  604. ->join('salary_record_log srl','srl.salary_record_id = sr.id')
  605. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  606. ->where('sr.month',$data['month'])
  607. ->where('sr.id','<>',$id)
  608. ->where('srs.user_id',$data['userId'])
  609. ->where('sr.type',$data['type'])
  610. ->where('sr.del',0)
  611. ->order('sr.id desc')
  612. ->count();
  613. if($count == 1){
  614. $tax = Db::name('salary_record')
  615. ->alias('sr')
  616. ->join('salary_record_log srl','srl.salary_record_id = sr.id')
  617. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  618. ->where('sr.month',$data['month'])
  619. ->where('srs.user_id',$data['userId'])
  620. ->where('sr.type',$data['type'])
  621. ->where('sr.del',0)
  622. ->order('sr.id desc')
  623. ->value('srl.tax');
  624. $spdata['tax'] = $tax;
  625. }*/
  626. }
  627. unset($sdata['type']);
  628. $sparr[] = $spdata;
  629. if($jbsalary > 0){
  630. $sdata['salary'] = round($should_total - $tax - $sbkc - $kcsalary,2);
  631. }else{
  632. $sdata['salary'] = round($should_total - $tax - $kcsalary,2);
  633. }
  634. if($sdata['salary'] < 0){
  635. return false;
  636. }
  637. $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
  638. $sdata['tax'] = $tax;
  639. $res = Db::name('salary_record_log')->where('id',$id)->update($sdata);
  640. if($res === false){
  641. return false;
  642. }
  643. $res2 = Db::name('salary_record_sb')
  644. ->where('user_id',$user['userId'])
  645. ->where('salary_record_id',$data['id'])
  646. ->update($sbdata);
  647. if($res2 === false){
  648. return false;
  649. }
  650. $res3 = Db::name('salary_record_special')
  651. ->where('user_id',$user['userId'])
  652. ->where('salary_record_id',$data['id'])
  653. ->strict(false)
  654. ->update($spdata);
  655. if($res3 === false){
  656. return false;
  657. }
  658. }
  659. return true;
  660. }
  661. private function sendOneSalaryJx($user,$data,$id){
  662. // 判断下记录是否存在
  663. $ret = Db::name('salary_record_log')
  664. ->where('id',$id)
  665. ->where('salary_record_id',$data['id'])
  666. ->where('user_id',$user['userId'])
  667. ->find();
  668. if(!$ret){
  669. return false;
  670. }
  671. $sdata = [
  672. 'salary_record_id' => $data['id'],
  673. 'user_id' => $user['userId'],
  674. 'njx' => floatval($user['psyear']) > 0?floatval($user['psyear']):0,
  675. 'pwlevel' => $user['pwlevel'],
  676. 'psid' => $user['psid'],
  677. 'type' => $user['type'],
  678. 'company_id' => $user['companyId'],
  679. 'should_total' => floatval($user['shouldTotal']) > 0?floatval($user['shouldTotal']):0,
  680. 'salary' => floatval($user['salary']) > 0?floatval($user['salary']):0,
  681. 'tax' => floatval($user['tax']) > 0?floatval($user['tax']):0,
  682. ];
  683. if($data['type'] == 6||$data['type'] == 7){
  684. $sdata['level'] = trim($user['level'])?trim($user['level']):'';
  685. $sdata['factor'] = floatval($user['factor']) > 0?floatval($user['factor']):0;
  686. $sdata['months'] = intval($user['months']) > 0?intval($user['months']):0;
  687. $sdata['taxrate'] = floatval($user['taxrate']) > 0?floatval($user['taxrate']):0;
  688. }
  689. $res = Db::name('salary_record_log')->where('id',$id)->update($sdata);
  690. if($res === false){
  691. return false;
  692. }
  693. return true;
  694. }
  695. private function sendSalary($data){
  696. $types = [];
  697. if($data['types']){
  698. $types = Db::name('user_subsidies')
  699. ->where('id','in',$data['types'])
  700. ->where('del',0)
  701. ->order('sorts asc,id asc')
  702. ->select();
  703. }
  704. $users = $data['users'];
  705. foreach ($users as $k=>$v){
  706. $users[$k]['psmoney'] = (float)$v['psmoney'];
  707. $users[$k]['pwmoney'] = (float)$v['pwmoney'];
  708. $users[$k]['fixedPerformance'] = (float)$v['fixedPerformance'];
  709. $users[$k]['psmonth'] = (float)$v['psmonth'];
  710. }
  711. if($data['type'] == 3||$data['type'] == 4){ // 劳务费
  712. $arr = [];
  713. foreach ($users as $k=>$v){
  714. $sdata = [
  715. 'salary_record_id' => $data['salary_record_id'],
  716. 'user_id' => $v['userId'],
  717. 'gwgz' => $v['psmoney'],
  718. 'xjgz' => $v['pwmoney'],
  719. 'jcjx' => $v['fixedPerformance'],
  720. 'yjx' => $v['psmonth'],
  721. 'pwlevel' => $v['pwlevel'],
  722. 'psid' => $v['psid'],
  723. 'type' => $v['type'],
  724. 'company_id' => $v['companyId'],
  725. 'child' => $v['child']
  726. ];
  727. $should_total = $v['psmoney'] + $v['pwmoney'] + $v['fixedPerformance'] + $v['psmonth'];
  728. $btjson = [];
  729. $kcsalary = 0;
  730. foreach ($v as $key=>$val){
  731. if(strpos($key,'subsidies') === 0){
  732. foreach ($types as $kk=>$vv){
  733. if($vv['id'] == str_ireplace('subsidies','',$key)){
  734. // $val = (float)$val > 0?(float)$val:0; // 不填或小于0的值默认成0
  735. $val = (float)$val; // 支持负值
  736. $btjson[] = [
  737. 'id' => $vv['id'],
  738. 'cate' => $vv['cate'],
  739. 'title' => $vv['title'],
  740. 'money' => $val
  741. ];
  742. if($vv['cate'] == 1){ // 补贴
  743. $should_total += $val;
  744. }else{
  745. $kcsalary += $val;
  746. }
  747. }
  748. }
  749. }
  750. }
  751. $sdata['should_total'] = $should_total;
  752. $sdata['tax'] = $this->getLaborTax($should_total,$v['userId']);
  753. $sdata['salary'] = round($should_total - $sdata['tax'] - $kcsalary,2);
  754. if($sdata['salary'] < 0){
  755. return false;
  756. }
  757. $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
  758. $arr[] = $sdata;
  759. if(count($arr) == 300){
  760. $res = Db::name('salary_record_log')->insertAll($arr);
  761. if($res != count($arr)){
  762. return false;
  763. }
  764. }
  765. }
  766. if($arr){
  767. $res = Db::name('salary_record_log')->insertAll($arr);
  768. if($res != count($arr)){
  769. return false;
  770. }
  771. }
  772. }else if($data['type'] == 1||$data['type'] == 2){ // 新华聘算社保及累计数
  773. $arr = [];
  774. $sbarr = [];
  775. $sparr = [];
  776. foreach ($users as $k=>$v){
  777. $sdata = [
  778. 'salary_record_id' => $data['salary_record_id'],
  779. 'user_id' => $v['userId'],
  780. 'gwgz' => $v['psmoney'],
  781. 'xjgz' => $v['pwmoney'],
  782. 'jcjx' => $v['fixedPerformance'],
  783. 'yjx' => $v['psmonth'],
  784. 'pwlevel' => $v['pwlevel'],
  785. 'psid' => $v['psid'],
  786. 'type' => $v['pwlevel'],
  787. 'company_id' => $v['companyId'],
  788. 'child' => $v['child']
  789. ];
  790. $should_total = $v['psmoney'] + $v['pwmoney'] + $v['fixedPerformance'] + $v['psmonth'];
  791. $jbsalary = $should_total; // 基本工资,如果不发基本工资,专项附件不累计,如果发基本工资,按正常工资走
  792. $btjson = [];
  793. $kcsalary = 0;
  794. $bkdata = [
  795. 'ggj' => 0, // 补扣公积金
  796. 'qynj' => 0, // 补扣企业年金
  797. 'yla' => 0, // 补扣养老保险
  798. 'yli' => 0, // 补扣医疗保险
  799. 'sye' => 0 // 补扣失业保险
  800. ];
  801. foreach ($v as $key=>$val){
  802. if(strpos($key,'subsidies') === 0){
  803. foreach ($types as $kk=>$vv){
  804. if($vv['id'] == str_ireplace('subsidies','',$key)){
  805. // $val = (float)$val > 0?(float)$val:0; // 不填或小于0的值默认成0
  806. $val = (float)$val; // 支持负值
  807. $btjson[] = [
  808. 'id' => $vv['id'],
  809. 'cate' => $vv['cate'],
  810. 'title' => $vv['title'],
  811. 'money' => $val,
  812. 'sbtype' => $vv['sbtype']
  813. ];
  814. $vvl = 0;
  815. if($vv['cate'] == 1){ // 补贴
  816. $should_total += $val;
  817. $vvl = $val;
  818. }else{
  819. $kcsalary += $val;
  820. $vvl = -$val;
  821. }
  822. // 是否关联社保公积金
  823. // 1=公积金 2=企业年金 3=养老保险 4=医疗保险 5=失业保险
  824. if($vv['sbtype'] == 1){
  825. $bkdata['ggj'] += $vvl;
  826. }else if($vv['sbtype'] == 2){
  827. $bkdata['qynj'] += $vvl;
  828. }else if($vv['sbtype'] == 3){
  829. $bkdata['yla'] += $vvl;
  830. }else if($vv['sbtype'] == 4){
  831. $bkdata['yli'] += $vvl;
  832. }else if($vv['sbtype'] == 5){
  833. $bkdata['sye'] += $vvl;
  834. }
  835. }
  836. }
  837. }
  838. }
  839. if($should_total <= 0){
  840. return false;
  841. }
  842. $sbdata = $this->getShebao($v['userId'],$v['companyId'],$v,$jbsalary);
  843. $sbdata['salary_record_id'] = $data['salary_record_id'];
  844. $sbdata['user_id'] = $v['userId'];
  845. $sbarr[] = $sbdata;
  846. // 社保扣除金额
  847. $sbkc = $sbdata['gjjp'] + $sbdata['qynjp'] +$sbdata['ylip'] +$sbdata['syep'] +$sbdata['ylap'];
  848. $sdata['should_total'] = $should_total;
  849. $sdata['sbgjj'] = $sbkc;
  850. $spdata = $this->getSalaryTax($should_total,$sbdata,$v['userId'],$v['companyId'],$data['month'],$data['type'],$jbsalary,0,$bkdata); // 计算个税
  851. trace($spdata);
  852. $spdata['salary_record_id'] = $data['salary_record_id'];
  853. $spdata['user_id'] = $v['userId'];
  854. $tax = $spdata['tax'];
  855. $m = intval(date('m',strtotime($data['month'].'-01')));
  856. if($m == 1 && $spdata['type'] != 1){ // 一月的第一次发工资
  857. $spdata['tax'] = 0;
  858. }else if($m == 1 && $spdata['type'] == 1){ // 补交不算税
  859. /*$count = Db::name('salary_record')
  860. ->alias('sr')
  861. ->join('salary_record_log srl','srl.salary_record_id = sr.id')
  862. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  863. ->where('sr.month',$data['month'])
  864. ->where('srs.user_id',$v['userId'])
  865. ->where('sr.type',$data['type'])
  866. ->where('sr.del',0)
  867. ->order('sr.id desc')
  868. ->count();
  869. if($count == 1){
  870. $tax = Db::name('salary_record')
  871. ->alias('sr')
  872. ->join('salary_record_log srl','srl.salary_record_id = sr.id')
  873. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  874. ->where('sr.month',$data['month'])
  875. ->where('srs.user_id',$v['userId'])
  876. ->where('sr.type',$data['type'])
  877. ->where('sr.del',0)
  878. ->order('sr.id desc')
  879. ->value('srl.tax');
  880. $spdata['tax'] = $tax;
  881. }*/
  882. }
  883. unset($spdata['type']);
  884. $sparr[] = $spdata;
  885. if($jbsalary > 0){
  886. $sdata['salary'] = round($should_total - $tax - $sbkc - $kcsalary,2);
  887. }else{
  888. $sdata['salary'] = round($should_total - $tax - $kcsalary,2);
  889. }
  890. if($sdata['salary'] < 0){ // 实发金额不能小于0
  891. return false;
  892. }
  893. $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
  894. $sdata['tax'] = $tax;
  895. $arr[] = $sdata;
  896. if(count($arr) == 300){
  897. $res = Db::name('salary_record_log')->insertAll($arr);
  898. if($res != count($arr)){
  899. return false;
  900. }
  901. }
  902. if(count($sbarr) == 300){
  903. $res = Db::name('salary_record_sb')->insertAll($sbarr);
  904. if($res != count($sbarr)){
  905. return false;
  906. }
  907. }
  908. if(count($sparr) == 300){
  909. $res = Db::name('salary_record_special')->insertAll($sparr);
  910. if($res != count($sparr)){
  911. return false;
  912. }
  913. }
  914. }
  915. if($arr){
  916. $res = Db::name('salary_record_log')->insertAll($arr);
  917. if($res != count($arr)){
  918. return false;
  919. }
  920. }
  921. if($sbarr){
  922. $res = Db::name('salary_record_sb')->insertAll($sbarr);
  923. if($res != count($sbarr)){
  924. return false;
  925. }
  926. }
  927. if($sparr){
  928. $res = Db::name('salary_record_special')->insertAll($sparr);
  929. if($res != count($sparr)){
  930. return false;
  931. }
  932. }
  933. trace($sparr);
  934. }
  935. return true;
  936. }
  937. private function sendSalaryJx($data){
  938. $users = $data['users'];
  939. $arr = [];
  940. $uids = [];
  941. foreach ($users as $k=>$v){
  942. if(in_array($v['userId'],$uids)){
  943. continue;
  944. }
  945. $uids[] = $v['userId'];
  946. $sdata = [
  947. 'salary_record_id' => $data['salary_record_id'],
  948. 'user_id' => $v['userId'],
  949. 'njx' => floatval($v['psyear']) > 0?floatval($v['psyear']):0,
  950. 'pwlevel' => $v['pwlevel'],
  951. 'psid' => $v['psid'],
  952. 'type' => $v['type'],
  953. 'company_id' => $v['companyId'],
  954. 'should_total' => floatval($v['shouldTotal']) > 0?floatval($v['shouldTotal']):0,
  955. 'salary' => floatval($v['salary']) > 0?floatval($v['salary']):0,
  956. 'tax' => floatval($v['tax']) > 0?floatval($v['tax']):0,
  957. ];
  958. if($data['type'] == 6||$data['type'] == 7){
  959. $sdata['level'] = trim($v['level'])?trim($v['level']):'';
  960. $sdata['factor'] = floatval($v['factor']) > 0?floatval($v['factor']):0;
  961. $sdata['months'] = intval($v['months']) > 0?intval($v['months']):0;
  962. $sdata['taxrate'] = floatval($v['taxrate']) > 0?floatval($v['taxrate']):0;
  963. }
  964. $arr[] = $sdata;
  965. if(count($arr) == 300){
  966. $res = Db::name('salary_record_log')->insertAll($arr);
  967. if($res != count($arr)){
  968. return false;
  969. }
  970. }
  971. }
  972. if($arr){
  973. $res = Db::name('salary_record_log')->insertAll($arr);
  974. if($res != count($arr)){
  975. return false;
  976. }
  977. }
  978. return true;
  979. }
  980. // 新华聘用税/编制个税
  981. private function getSalaryTax($should_total,$sbdata,$userId,$companyId,$month,$type,$jbsalary,$from=0,$bkdata=[]){// from 0=新增 0<重新计算
  982. $data = [
  983. 'salary' => 0,
  984. 'mssr' => 0,
  985. 'sr' => 0,
  986. 'ljsr' => 0,
  987. 'ljjc' => 0,
  988. 'ljgjj' => 0,
  989. 'ljylobx' => 0,
  990. 'ljsybx' => 0,
  991. 'ljylibx' => 0,
  992. 'ljnj' => 0,
  993. 'ljznjy' => 0,
  994. 'ljjxjy' => 0,
  995. 'ljzfdklx' => 0,
  996. 'ljzfzj' => 0,
  997. 'ljsylr' => 0,
  998. 'syjkbx' => 0,
  999. 'ysylbx' => 0,
  1000. 'grbk' => 0,
  1001. 'jzjbjsb' => 0,
  1002. 'ljgzds' => 0,
  1003. 'qtkc' => 0,
  1004. 'ljsqljxmhj' => 0,
  1005. 'ljykjse' => 0,
  1006. 'tax' => 0,
  1007. 'zykcjze' => 0,
  1008. 'jmsebl' => 0,
  1009. 'extratax' => 0,
  1010. 'ljyyezh' => 0,
  1011. 'ljgrylj' => 0,
  1012. ];
  1013. $lastinfo = [];
  1014. if($from > 0){
  1015. // 每月发多次工资,先找本月是否已经发放过工资,没有的话获取上个月的记录,在没有的话获取初始值
  1016. $lastinfo = Db::name('salary_record')
  1017. ->alias('sr')
  1018. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  1019. ->where('sr.month',$month)
  1020. ->where('srs.user_id',$userId)
  1021. ->where('sr.id','<>',$from)
  1022. ->where('sr.type',$type)
  1023. ->where('sr.del',0)
  1024. ->field('srs.*')
  1025. ->order('sr.id desc')
  1026. ->find();
  1027. }else{
  1028. // 每月发多次工资,先找本月是否已经发放过工资,没有的话获取上个月的记录,在没有的话获取初始值
  1029. $lastinfo = Db::name('salary_record')
  1030. ->alias('sr')
  1031. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  1032. ->where('sr.month',$month)
  1033. ->where('srs.user_id',$userId)
  1034. ->where('sr.type',$type)
  1035. ->where('sr.del',0)
  1036. ->field('srs.*')
  1037. ->order('sr.id desc')
  1038. ->find();
  1039. }
  1040. $stype = 0; // 无累计
  1041. if(!$lastinfo){
  1042. $lastmonth = date('Y-m',strtotime($month.'-01') - 24*60*60); // 上个月
  1043. $lastinfo = Db::name('salary_record')
  1044. ->alias('sr')
  1045. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  1046. ->where('sr.month',$lastmonth)
  1047. ->where('srs.user_id',$userId)
  1048. ->where('sr.type',$type)
  1049. ->where('sr.del',0)
  1050. ->field('srs.*')
  1051. ->order('sr.id desc')
  1052. ->find();
  1053. if($lastinfo){
  1054. $stype = 2; // 有上月累计
  1055. }
  1056. }else{
  1057. $stype = 1; // 有本月累计
  1058. }
  1059. if(!$lastinfo){
  1060. $lastinfo = Db::name('user_total')->where('user_id',$userId)->find();
  1061. if($lastinfo){
  1062. $stype = 3; // 有初始累计
  1063. }
  1064. }
  1065. if($lastinfo){
  1066. $data = [
  1067. 'salary' => $lastinfo['salary'],
  1068. 'mssr' => $lastinfo['mssr'],
  1069. 'sr' => $lastinfo['sr'],
  1070. 'ljsr' => $lastinfo['ljsr'],
  1071. 'ljjc' => $lastinfo['ljjc'],
  1072. 'ljgjj' => $lastinfo['ljgjj'],
  1073. 'ljylobx' => $lastinfo['ljylobx'],
  1074. 'ljsybx' => $lastinfo['ljsybx'],
  1075. 'ljylibx' => $lastinfo['ljylibx'],
  1076. 'ljnj' => $lastinfo['ljnj'],
  1077. 'ljznjy' => $lastinfo['ljznjy'],
  1078. 'ljjxjy' => $lastinfo['ljjxjy'],
  1079. 'ljzfdklx' => $lastinfo['ljzfdklx'],
  1080. 'ljzfzj' => $lastinfo['ljzfzj'],
  1081. 'ljsylr' => $lastinfo['ljsylr'],
  1082. 'ljyyezh' => $lastinfo['ljyyezh'],
  1083. 'syjkbx' => $lastinfo['syjkbx'],
  1084. 'ysylbx' => $lastinfo['ysylbx'],
  1085. 'grbk' => $lastinfo['grbk'],
  1086. 'jzjbjsb' => $lastinfo['jzjbjsb'],
  1087. 'ljgzds' => $lastinfo['ljgzds'],
  1088. 'qtkc' => $lastinfo['qtkc'],
  1089. 'ljsqljxmhj' => $lastinfo['ljsqljxmhj'],
  1090. 'ljykjse' => $lastinfo['ljykjse'],
  1091. 'tax' => $lastinfo['tax'],
  1092. 'zykcjze' => $lastinfo['zykcjze'],
  1093. 'jmsebl' => $lastinfo['jmsebl'],
  1094. 'ljgrylj' => $lastinfo['ljgrylj'],
  1095. 'extratax' => isset($lastinfo['extratax'])?$lastinfo['extratax']:0
  1096. ];
  1097. }
  1098. $extratax = $data['extratax']; // 额外税差额
  1099. unset($data['extratax']);
  1100. // 使用上月数据计算个税
  1101. $ljkc = $data['ljsr'] - $data['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
  1102. if($ljkc > 0){
  1103. $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
  1104. $jmse = round($tax*$data['jmsebl'],2); // 减免税额
  1105. $ljykjse = $data['ljykjse'] + $data['tax'];
  1106. $tax = round($tax - $jmse - $ljykjse,2);
  1107. $tax = $tax>0?$tax:0;
  1108. $data['ljykjse'] = $ljykjse;
  1109. }else{
  1110. $tax = 0;
  1111. }
  1112. $data['tax'] = round($tax + $extratax,2);
  1113. $data['salary'] = $should_total;
  1114. $mssr = 0;
  1115. $m = intval(date('m',strtotime($month.'-01')));
  1116. if($jbsalary > 0){ // 发基本工资
  1117. // 加累计值
  1118. $mssr = 0;
  1119. $special = Db::name('user_special')->where('user_id',$userId)->find();
  1120. if($special){}
  1121. $mssr = $special?$special['mssr']:0;
  1122. if($m > 1){
  1123. $data['ljjc'] += 5000; // 减除费用每月只能使用1次
  1124. $data['ljgjj'] += $sbdata['gjjp'] - (!empty($bkdata)?$bkdata['ggj']:0);
  1125. $data['ljylobx'] += $sbdata['ylap'] - (!empty($bkdata)?$bkdata['yla']:0);
  1126. $data['ljsybx'] += $sbdata['syep'] - (!empty($bkdata)?$bkdata['sye']:0);
  1127. $data['ljylibx'] += $sbdata['ylip'] - (!empty($bkdata)?$bkdata['yli']:0);
  1128. $data['ljnj'] += $sbdata['qynjp'] - (!empty($bkdata)?$bkdata['qynj']:0);
  1129. $data['ljznjy'] += $special?$special['znjy']:0;
  1130. $data['ljjxjy'] += $special?$special['jxjy']:0;
  1131. $data['ljzfdklx'] += $special?$special['zfdklx']:0;
  1132. $data['ljzfzj'] += $special?$special['zfzj']:0;
  1133. $data['ljsylr'] += $special?$special['sylr']:0;
  1134. $data['ljyyezh'] += $special?$special['yyezh']:0;
  1135. $data['ljgzds'] += $special?$special['gzds']:0;
  1136. $data['syjkbx'] += $special?$special['syjkbx']:0;
  1137. $data['ysylbx'] += $special?$special['ysylbx']:0;
  1138. $data['grbk'] += $special?$special['grbk']:0;
  1139. $data['jzjbjsb'] += $special?$special['nzjds']:0;
  1140. $data['qtkc'] += $special?$special['qtkc']:0;
  1141. $data['zykcjze'] += $special?$special['zykcjze']:0;
  1142. $data['ljgrylj'] += $special?$special['grylj']:0;
  1143. }else{
  1144. $data['ljjc'] = 5000;
  1145. $data['ljgjj'] = $sbdata['gjjp'] - (!empty($bkdata)?$bkdata['ggj']:0);
  1146. $data['ljylobx'] = $sbdata['ylap'] - (!empty($bkdata)?$bkdata['yla']:0);
  1147. $data['ljsybx'] = $sbdata['syep'] - (!empty($bkdata)?$bkdata['sye']:0);
  1148. $data['ljylibx'] = $sbdata['ylip'] - (!empty($bkdata)?$bkdata['yli']:0);
  1149. $data['ljnj'] = $sbdata['qynjp'] - (!empty($bkdata)?$bkdata['qynj']:0);
  1150. $data['ljznjy'] = $special?$special['znjy']:0;
  1151. $data['ljjxjy'] = $special?$special['jxjy']:0;
  1152. $data['ljzfdklx'] = $special?$special['zfdklx']:0;
  1153. $data['ljzfzj'] = $special?$special['zfzj']:0;
  1154. $data['ljsylr'] = $special?$special['sylr']:0;
  1155. $data['ljgzds'] = $special?$special['gzds']:0;
  1156. $data['ljyyezh'] = $special?$special['yyezh']:0;
  1157. $data['ljykjse'] = 0;
  1158. $data['ljsr'] = 0;
  1159. $data['syjkbx'] = $special?$special['syjkbx']:0;
  1160. $data['ysylbx'] = $special?$special['ysylbx']:0;
  1161. $data['grbk'] = $special?$special['grbk']:0;
  1162. $data['jzjbjsb'] = $special?$special['nzjds']:0;
  1163. $data['qtkc'] = $special?$special['qtkc']:0;
  1164. $data['zykcjze'] = $special?$special['zykcjze']:0;
  1165. $data['ljgrylj'] = $special?$special['grylj']:0;
  1166. }
  1167. $data['jmsebl'] = $special?$special['jmsebl']:0;
  1168. $data['mssr'] = $special?$special['mssr']:0;
  1169. }else{
  1170. if($m == 1 && $stype != 1){
  1171. $data['ljjc'] = 0;
  1172. $data['ljgjj'] = 0;
  1173. $data['ljylobx'] = 0;
  1174. $data['ljsybx'] = 0;
  1175. $data['ljylibx'] = 0;
  1176. $data['ljnj'] = 0;
  1177. $data['ljznjy'] = 0;
  1178. $data['ljjxjy'] = 0;
  1179. $data['ljzfdklx'] = 0;
  1180. $data['ljzfzj'] = 0;
  1181. $data['ljsylr'] = 0;
  1182. $data['ljgzds'] = 0;
  1183. $data['ljykjse'] = 0;
  1184. $data['ljsr'] = 0;
  1185. $data['syjkbx'] = 0;
  1186. $data['ysylbx'] = 0;
  1187. $data['grbk'] = 0;
  1188. $data['jzjbjsb'] = 0;
  1189. $data['qtkc'] = 0;
  1190. $data['zykcjze'] = 0;
  1191. $data['ljsqljxmhj'] = 0;
  1192. $data['jmsebl'] = 0;
  1193. $data['ljyyezh'] = 0;
  1194. $data['ljgrylj'] = 0;
  1195. }
  1196. $data['tax'] = 0; // 补助不算个税,只累计值
  1197. }
  1198. $data['mssr'] = $mssr;
  1199. $data['sr'] = $should_total - $mssr;
  1200. $data['ljsr'] += $data['sr'];
  1201. $data['type'] = $stype;
  1202. $data['ljsqljxmhj'] = $data['ljjc'] + $data['ljgjj'] + $data['ljyyezh'] + $data['ljgrylj'] + $data['ljylobx'] + $data['ljsybx'] + $data['ljylibx'] + $data['ljnj'] + $data['ljznjy']
  1203. + $data['ljjxjy'] + $data['ljzfdklx'] + $data['ljzfzj'] + $data['ljsylr'] + $data['ljgzds'] + $data['syjkbx'] + $data['ysylbx']
  1204. + $data['grbk'] + $data['jzjbjsb'] + $data['zykcjze'] + $data['qtkc'];
  1205. return $data;
  1206. }
  1207. // 社保公积金
  1208. private function getShebao($userId,$companyId,$info,$jbsalary){
  1209. $bls = Db::name('user_bl')->select(); // 社保比例
  1210. $base = Db::name('user_base')->where('user_id',$userId)->find();
  1211. $data = [
  1212. 'qynj' => 0,
  1213. 'gjj' => 0,
  1214. 'sb' => 0,
  1215. 'syyl' => 0,
  1216. 'gssy' => 0,
  1217. 'ylnj' => 0,
  1218. 'ylap' => 0,
  1219. 'ylac' => 0,
  1220. 'syep' => 0,
  1221. 'syec' => 0,
  1222. 'ylip' => 0,
  1223. 'ylic' => 0,
  1224. 'gsc' => 0,
  1225. 'qynjp' => 0,
  1226. 'qynjc' => 0,
  1227. 'gjjp' => 0,
  1228. 'gjjc' => 0,
  1229. ];
  1230. if(!$base||$jbsalary == 0){ //不是发基本工资就不扣缴社保
  1231. return $data;
  1232. }
  1233. $data['qynj'] = $base['qynj'];
  1234. $data['gjj'] = $base['gjj'];
  1235. $data['sb'] = $base['sb'];
  1236. $data['syyl'] = $base['syyl'];
  1237. $data['gssy'] = $base['gssy'];
  1238. $data['ylnj'] = $base['ylnj'];
  1239. // 计算各项扣除
  1240. foreach ($bls as $key=>$val){
  1241. if($val['company_id'] == $companyId){
  1242. switch ($val['name']){
  1243. case 'gjj': // 住房公积金,企业和个人只保留整数,四舍五入
  1244. $data['gjjp'] = round(calculate_money($base['gjj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']));
  1245. $data['gjjc'] = round(calculate_money($base['gjj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']));
  1246. break;
  1247. case 'qynj':
  1248. if ($val['company_id'] == 1) {
  1249. $data['qynjp'] = calculate_money($base['ylnj'], $val['p_bl'], $val['p_bl_type'], $val['p_bl_extra'], $val['p_free_bl']);
  1250. $data['qynjc'] = calculate_money($base['ylnj'], $val['c_bl'], $val['c_bl_type'], $val['c_bl_extra'], $val['c_free_bl']);
  1251. } else {
  1252. $data['qynjp'] = calculate_money($base['qynj'], $val['p_bl'], $val['p_bl_type'], $val['p_bl_extra'], $val['p_free_bl']);
  1253. $data['qynjc'] = calculate_money($base['qynj'], $val['c_bl'], $val['c_bl_type'], $val['c_bl_extra'], $val['c_free_bl']);
  1254. }
  1255. break;
  1256. case 'yla':
  1257. if($info['yla']) {
  1258. if($val['company_id'] == 1){
  1259. $data['ylap'] = calculate_money($base['ylnj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  1260. $data['ylac'] = calculate_money($base['ylnj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  1261. } else {
  1262. $data['ylap'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  1263. $data['ylac'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  1264. }
  1265. }
  1266. break;
  1267. case 'sye':
  1268. if($val['company_id'] == 1){
  1269. $data['syep'] = calculate_money($base['gssy'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  1270. $data['syec'] = calculate_money($base['gssy'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  1271. } else {
  1272. $data['syep'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  1273. $data['syec'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  1274. }
  1275. break;
  1276. case 'yli':
  1277. if($val['company_id'] == 1){
  1278. $data['ylip'] = calculate_money($base['syyl'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  1279. $data['ylic'] = calculate_money($base['syyl'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  1280. } else {
  1281. $data['ylip'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  1282. $data['ylic'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  1283. }
  1284. break;
  1285. case 'gs':
  1286. if($val['company_id'] == 1){
  1287. $data['gsc'] = calculate_money($base['gssy'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  1288. } else {
  1289. $data['gsc'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  1290. }
  1291. break;
  1292. }
  1293. }
  1294. }
  1295. if(!$info['gjj']){
  1296. $data['gjjp'] = 0;
  1297. $data['gjjc'] = 0;
  1298. }
  1299. if(!$info['qynj']){
  1300. $data['qynjp'] = 0;
  1301. $data['gjjc'] = 0;
  1302. }
  1303. if(!$info['yla']){
  1304. $data['ylap'] = 0;
  1305. $data['ylac'] = 0;
  1306. }
  1307. if(!$info['sye']){
  1308. $data['syep'] = 0;
  1309. $data['syec'] = 0;
  1310. }
  1311. if(!$info['yli']){
  1312. $data['ylip'] = 0;
  1313. $data['ylic'] = 0;
  1314. }
  1315. if(!$info['gs']){
  1316. $data['gsc'] = 0;
  1317. }
  1318. return $data;
  1319. }
  1320. // 获取劳务费个税
  1321. public function getLaborTax($salary,$userId){
  1322. // =ROUND(MAX((MIN((G4)*0.8,(G4)-800)*{0;20;30;40}%-1000*{0;0;2;7}),0),2) //计算公式
  1323. if($salary*0.8 <= $salary - 800){
  1324. $salary = $salary*0.8;
  1325. }else{
  1326. $salary = $salary - 800;
  1327. }
  1328. $arr = [
  1329. $salary*0 - 0,
  1330. $salary*0.2 - 0,
  1331. $salary*0.3 - 2000,
  1332. $salary*0.4 - 7000,
  1333. 0
  1334. ];
  1335. $tax = max($arr);
  1336. if($tax > 0){
  1337. $bl = Db::name('user_jmlabor')->where('user_id',$userId)->value('jmsebl');
  1338. $bl = $bl?$bl:0;
  1339. if($bl > 0){
  1340. $tax = (1 - $bl)*$tax;
  1341. }
  1342. }
  1343. $tax = round($tax,2);
  1344. return $tax;
  1345. }
  1346. public function preAdd($data,$orgId){
  1347. $uids = model('User')->getAdminUsers();
  1348. if($uids){
  1349. $map[] = ['u.id','not in',$uids];
  1350. }
  1351. $map[] = ['u.del','=',0];
  1352. $map[] = ['r.type','=',3];
  1353. $map[] = ['r.org_id','=',$orgId];
  1354. if($data['type'] == 6){
  1355. $map[] = ['ui.pay_type','=',1];
  1356. } else if($data['type'] == 7){
  1357. $map[] = ['ui.pay_type','=',2];
  1358. } else if($data['type'] == 8){
  1359. $map[] = ['ui.pay_type','=',3];
  1360. } else if($data['type'] == 9){
  1361. $map[] = ['ui.pay_type','=',4];
  1362. } else {
  1363. $map[] = ['ui.pay_type','=',$data['type']];
  1364. }
  1365. $lists = Db::name('user_info')
  1366. ->alias('ui')
  1367. ->join('user u','ui.user_id = u.id')
  1368. ->join('user_roles ur','ur.user_id = u.id')
  1369. ->join('roles r','ur.roles_id = r.id')
  1370. ->where($map)
  1371. ->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')
  1372. ->order('ui.sorts3 asc,u.id desc')
  1373. ->select();
  1374. if(!$lists){
  1375. HelpHander::error('该工资类型下无可发放工资的人员');
  1376. }
  1377. $types = [];
  1378. if($data['types']){
  1379. $types = Db::name('user_subsidies')
  1380. ->where('id','in',$data['types'])
  1381. ->where('del',0)
  1382. ->order('sorts asc,id asc')
  1383. ->select();
  1384. }
  1385. foreach ($lists as $k=>$v){
  1386. $lists[$k]['user_sn'] = think_encrypt($v['user_id']);
  1387. if(in_array($data['type'],[6,7,8,9])){
  1388. $lists[$k]['level'] = '';
  1389. $lists[$k]['factor'] = '';
  1390. $lists[$k]['months'] = '';
  1391. $lists[$k]['taxrate'] = '';
  1392. $lists[$k]['tax'] = '';
  1393. $lists[$k]['salary'] = '';
  1394. $lists[$k]['should_total'] = '';
  1395. $lists[$k]['psyear'] = '';
  1396. }
  1397. $lists[$k]['pwstatus'] = 0; // 薪级变化状态
  1398. $lists[$k]['work_age'] = $v['work_date']?calculate_age($v['work_date']):0; // 工龄
  1399. if($data['type'] == 5){
  1400. $retired = Db::name('user_retired')->where('user_id',$v['user_id'])->find();
  1401. $lists[$k]['bz'] = $retired?$retired['bz']:0;
  1402. $lists[$k]['zysdbt'] = $retired?$retired['zysdbt']:0;
  1403. $lists[$k]['fb'] = $retired?$retired['fb']:0;
  1404. $lists[$k]['wybt'] = $retired?$retired['wybt']:0;
  1405. }else{
  1406. $lists[$k]['psname'] = '';
  1407. $lists[$k]['psmonth'] = 0;
  1408. // $lists[$k]['psyear'] = 0;
  1409. $lists[$k]['psmoney'] = 0;
  1410. if($lists[$k]['psid'] > 0){
  1411. $psinfo = Db::name('post_salary')->where('id',$v['psid'])->find();
  1412. if($psinfo){
  1413. $lists[$k]['psname'] = $psinfo['job'];
  1414. $lists[$k]['psmoney'] = $psinfo['money'];
  1415. // $lists[$k]['psyear'] = $psinfo['year_performance'];
  1416. $lists[$k]['psmonth'] = $psinfo['month_performance'];
  1417. if($lists[$k]['work_age'] > 25 && $psinfo['month_performance2'] > 0){
  1418. $lists[$k]['psmonth'] = $psinfo['month_performance2'];
  1419. }
  1420. // if($lists[$k]['work_age'] > 25 && $psinfo['year_performance2'] > 0){
  1421. // $lists[$k]['psyear'] = $psinfo['year_performance2'];
  1422. // }
  1423. }
  1424. }
  1425. $lists[$k]['pwmoney'] = 0;
  1426. if($v['type'] > 0 && $v['company_id'] > 0){
  1427. $pay_wages = Db::name('pay_wages')
  1428. ->where('company_id',$v['company_id'])
  1429. ->where('type',$v['type'])
  1430. ->value('content');
  1431. $pay_wages = $pay_wages?json_decode($pay_wages,true):[];
  1432. foreach ($pay_wages as $kk=>$vv){
  1433. if($v['pwlevel'] == $vv['level']){
  1434. $lists[$k]['pwmoney'] = $vv['money'];
  1435. break;
  1436. }
  1437. }
  1438. }
  1439. if($data['type'] == 1||$data['type'] == 2){
  1440. $lists[$k]['gjj'] = true;
  1441. $lists[$k]['yla'] = true;
  1442. $lists[$k]['sye'] = true;
  1443. $lists[$k]['yli'] = true;
  1444. $lists[$k]['qynj'] = true;
  1445. $lists[$k]['gs'] = true;
  1446. }
  1447. }
  1448. $lastmonth = date('Y-m',strtotime($data['month'].'-01') - 1000);
  1449. $btjson = null;
  1450. $log = null;
  1451. if($data['type'] != 5 && $data['continue'] == 1){ //延续上月,获取上月补贴,只获取基本工资的补贴项
  1452. $log = Db::name('salary_record_log')
  1453. ->alias('srl')
  1454. ->join('salary_record sr','sr.id = srl.salary_record_id')
  1455. ->where('sr.month',$data['month'])
  1456. ->where('srl.user_id',$v['user_id'])
  1457. ->where('sr.status','in',[1,2])
  1458. ->where('srl.gwgz','>',0)
  1459. ->where('sr.del',0)
  1460. ->field('srl.*')
  1461. ->order('srl.id desc')
  1462. ->find();
  1463. if(!$log){
  1464. $log = Db::name('salary_record_log')
  1465. ->alias('srl')
  1466. ->join('salary_record sr','sr.id = srl.salary_record_id')
  1467. ->where('sr.month',$lastmonth)
  1468. ->where('srl.user_id',$v['user_id'])
  1469. ->where('sr.status','in',[1,2])
  1470. ->where('srl.gwgz','>',0)
  1471. ->where('sr.del',0)
  1472. ->field('srl.*')
  1473. ->order('srl.id desc')
  1474. ->find();
  1475. }
  1476. if(!empty($log) && ($data['type'] == 3||$data['type'] == 4)){ // 劳务基本工资项也要继承上次的
  1477. $lists[$k]['psmoney'] = $log['gwgz'];
  1478. $lists[$k]['pwmoney'] = $log['xjgz'];
  1479. $lists[$k]['fixed_performance'] = $log['jcjx'];
  1480. $lists[$k]['psmonth'] = $log['yjx'];
  1481. }
  1482. if(!empty($log) && ($v['pwlevel'] != $log['pwlevel'])){ // 有薪级变化
  1483. $lists[$k]['pwstatus'] = 1;
  1484. }
  1485. $btjson = !empty($log)?$log['btjson']:null;
  1486. /*$btjson = Db::name('salary_record_log')
  1487. ->alias('srl')
  1488. ->join('salary_record sr','sr.id = srl.salary_record_id')
  1489. ->where('sr.month',$data['month'])
  1490. ->where('srl.user_id',$v['user_id'])
  1491. ->where('sr.status','in',[1,2])
  1492. ->where('srl.gwgz','>',0)
  1493. ->where('sr.del',0)
  1494. ->value('btjson');
  1495. if(!$btjson){
  1496. $btjson = Db::name('salary_record_log')
  1497. ->alias('srl')
  1498. ->join('salary_record sr','sr.id = srl.salary_record_id')
  1499. ->where('sr.month',$lastmonth)
  1500. ->where('srl.user_id',$v['user_id'])
  1501. ->where('sr.status','in',[1,2])
  1502. ->where('srl.gwgz','>',0)
  1503. ->where('sr.del',0)
  1504. ->value('btjson');
  1505. }*/
  1506. }else if($data['type'] == 5 && $data['continue'] == 1){
  1507. $btjson = Db::name('user_retired_salary')
  1508. ->alias('srl')
  1509. ->join('salary_record sr','sr.id = srl.salary_record_id')
  1510. ->where('sr.month',$data['month'])
  1511. ->where('srl.user_id',$v['user_id'])
  1512. ->where('sr.status','in',[1,2])
  1513. ->where('sr.del',0)
  1514. ->order('srl.id desc')
  1515. ->value('btjson');
  1516. if(!$btjson){
  1517. $btjson = Db::name('user_retired_salary')
  1518. ->alias('srl')
  1519. ->join('salary_record sr','sr.id = srl.salary_record_id')
  1520. ->where('sr.month',$lastmonth)
  1521. ->where('srl.user_id',$v['user_id'])
  1522. ->where('sr.status','in',[1,2])
  1523. ->where('sr.del',0)
  1524. ->order('srl.id desc')
  1525. ->value('btjson');
  1526. }
  1527. }
  1528. $btjson = !empty($btjson)?json_decode($btjson,true):[];
  1529. foreach ($types as $kk=>$vv){
  1530. if(empty($btjson)){
  1531. $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
  1532. }else{
  1533. $flag = 1;
  1534. foreach ($btjson as $key=>$val){
  1535. if($val['id'] == $vv['id']){
  1536. $flag = 0;
  1537. $lists[$k]['subsidies'.$vv['id']] = $val['money'];
  1538. }
  1539. }
  1540. if($flag == 1){ // 无相同的分类
  1541. $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
  1542. }
  1543. }
  1544. }
  1545. }
  1546. return ['types' => $types,'list' => $lists];
  1547. }
  1548. // 重新计算
  1549. public function reloadAdd($salaryRecordId,$uid,$orgId,$verification=1){
  1550. $data = Db::name('salary_record')->where('id',$salaryRecordId)->find();
  1551. // 判断下记录是否存在
  1552. $ret = Db::name('salary_record_log')->where('salary_record_id',$salaryRecordId)->where('user_id',$uid)->find();
  1553. if($verification == 1 && !$ret){
  1554. HelpHander::error('记录不存在');
  1555. }
  1556. $map[] = ['u.id','=',$uid];
  1557. $map[] = ['u.del','=',0];
  1558. $map[] = ['r.type','=',3];
  1559. $map[] = ['r.org_id','=',$orgId];
  1560. // 不再判断类型,已发放时为准
  1561. /*if($data['type'] == 1||$data['type'] == 6){
  1562. $map[] = ['ui.pay_type','=',1];
  1563. }else if($data['type'] == 2||$data['type'] == 7){
  1564. $map[] = ['ui.pay_type','=',2];
  1565. }else if($data['type'] == 3||$data['type'] == 8){
  1566. $map[] = ['ui.pay_type','=',3];
  1567. }else if($data['type'] == 4||$data['type'] == 9){
  1568. $map[] = ['ui.pay_type','=',4];
  1569. }else{
  1570. $map[] = ['ui.pay_type','=',$data['type']];
  1571. }*/
  1572. $lists = Db::name('user_info')
  1573. ->alias('ui')
  1574. ->join('user u','ui.user_id = u.id')
  1575. ->join('user_roles ur','ur.user_id = u.id')
  1576. ->join('roles r','ur.roles_id = r.id')
  1577. ->where($map)
  1578. ->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')
  1579. ->order('ui.sorts3 asc,u.id desc')
  1580. ->select();
  1581. if($verification == 1 && !$lists){
  1582. HelpHander::error('该工资类型下无可发放工资的人员');
  1583. }
  1584. $types = [];
  1585. if($data['types']){
  1586. $types = Db::name('user_subsidies')->where('id','in',$data['types'])->where('del',0)->order('sorts asc')->select();
  1587. }
  1588. foreach ($lists as $k=>$v){
  1589. if(in_array($data['type'],[6,7,8,9])){
  1590. $log = Db::name('salary_record_log')
  1591. ->where('salary_record_id',$salaryRecordId)
  1592. ->where('user_id',$v['user_id'])
  1593. ->find();
  1594. $lists[$k]['level'] = $log?$log['level']:'';
  1595. $lists[$k]['factor'] = $log?$log['factor']:'';
  1596. $lists[$k]['months'] = $log?$log['months']:'';
  1597. $lists[$k]['taxrate'] = $log?$log['taxrate']:'';
  1598. $lists[$k]['tax'] = $log?$log['tax']:'';
  1599. $lists[$k]['salary'] = $log?$log['salary']:'';
  1600. $lists[$k]['should_total'] = $log?$log['should_total']:'';
  1601. $lists[$k]['psyear'] = $log?$log['njx']:'';
  1602. }
  1603. $lists[$k]['work_age'] = $v['work_date']?calculate_age($v['work_date']):0; // 工龄
  1604. if($data['type'] == 5){
  1605. $retired = Db::name('user_retired')->where('user_id',$v['user_id'])->find();
  1606. $lists[$k]['bz'] = $retired?$retired['bz']:0;
  1607. $lists[$k]['zysdbt'] = $retired?$retired['zysdbt']:0;
  1608. $lists[$k]['fb'] = $retired?$retired['fb']:0;
  1609. $lists[$k]['wybt'] = $retired?$retired['wybt']:0;
  1610. }else{
  1611. $lists[$k]['psname'] = '';
  1612. $lists[$k]['psmonth'] = 0;
  1613. $lists[$k]['psmoney'] = 0;
  1614. if($lists[$k]['psid'] > 0){
  1615. $psinfo = Db::name('post_salary')->where('id',$v['psid'])->find();
  1616. if($psinfo){
  1617. $lists[$k]['psname'] = $psinfo['job'];
  1618. $lists[$k]['psmoney'] = $psinfo['money'];
  1619. $lists[$k]['psmonth'] = $psinfo['month_performance'];
  1620. if($lists[$k]['work_age'] > 25 && $psinfo['month_performance2'] > 0){
  1621. $lists[$k]['psmonth'] = $psinfo['month_performance2'];
  1622. }
  1623. }
  1624. }
  1625. $lists[$k]['pwmoney'] = 0;
  1626. if($v['type'] > 0 && $v['company_id'] > 0){
  1627. $pay_wages = Db::name('pay_wages')
  1628. ->where('company_id',$v['company_id'])
  1629. ->where('type',$v['type'])
  1630. ->value('content');
  1631. $pay_wages = $pay_wages?json_decode($pay_wages,true):[];
  1632. foreach ($pay_wages as $kk=>$vv){
  1633. if($v['pwlevel'] == $vv['level']){
  1634. $lists[$k]['pwmoney'] = $vv['money'];
  1635. break;
  1636. }
  1637. }
  1638. }
  1639. if($data['type'] == 1||$data['type'] == 2){
  1640. $lists[$k]['gjj'] = true;
  1641. $lists[$k]['yla'] = true;
  1642. $lists[$k]['sye'] = true;
  1643. $lists[$k]['yli'] = true;
  1644. $lists[$k]['qynj'] = true;
  1645. $lists[$k]['gs'] = true;
  1646. // 检查开关
  1647. $sbinfo = Db::name('salary_record_sb')->where('salary_record_id',$salaryRecordId)->where('user_id',$v['user_id'])->find();
  1648. if($sbinfo){
  1649. if($sbinfo['gjjp'] <= 0){
  1650. $lists[$k]['gjj'] = false;
  1651. }
  1652. if($sbinfo['ylap'] <= 0){
  1653. $lists[$k]['yla'] = false;
  1654. }
  1655. if($sbinfo['syep'] <= 0){
  1656. $lists[$k]['sye'] = false;
  1657. }
  1658. if($sbinfo['ylip'] <= 0){
  1659. $lists[$k]['yli'] = false;
  1660. }
  1661. if($sbinfo['gsc'] <= 0){
  1662. $lists[$k]['gs'] = false;
  1663. }
  1664. if($sbinfo['qynjp'] <= 0){
  1665. $lists[$k]['qynj'] = false;
  1666. }
  1667. }else{
  1668. $lists[$k]['gjj'] = false;
  1669. $lists[$k]['yla'] = false;
  1670. $lists[$k]['sye'] = false;
  1671. $lists[$k]['yli'] = false;
  1672. $lists[$k]['qynj'] = false;
  1673. $lists[$k]['gs'] = false;
  1674. }
  1675. }
  1676. }
  1677. $btjson = null;
  1678. $log = null;
  1679. if($data['type'] != 5){
  1680. $log = Db::name('salary_record_log')
  1681. ->where('salary_record_id',$salaryRecordId)
  1682. ->where('user_id',$uid)
  1683. ->find();
  1684. if(!empty($log)){
  1685. $lists[$k]['psmoney'] = $log['gwgz'];
  1686. $lists[$k]['pwmoney'] = $log['xjgz'];
  1687. $lists[$k]['fixed_performance'] = $log['jcjx'];
  1688. $lists[$k]['psmonth'] = $log['yjx'];
  1689. }
  1690. $btjson = !empty($log)?$log['btjson']:null;
  1691. }else{
  1692. $btjson = Db::name('user_retired_salary')
  1693. ->where('salary_record_id',$salaryRecordId)
  1694. ->where('user_id',$uid)
  1695. ->value('btjson');
  1696. }
  1697. $btjson = !empty($btjson)?json_decode($btjson,true):[];
  1698. foreach ($types as $kk=>$vv){
  1699. if(empty($btjson)){
  1700. $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
  1701. }else{
  1702. $flag = 1;
  1703. foreach ($btjson as $key=>$val){
  1704. if($val['id'] == $vv['id']){
  1705. $flag = 0;
  1706. $lists[$k]['subsidies'.$vv['id']] = $val['money'];
  1707. }
  1708. }
  1709. if($flag == 1){ // 无相同的分类
  1710. $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
  1711. }
  1712. }
  1713. }
  1714. // foreach ($types as $kk=>$vv){
  1715. // $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
  1716. // }
  1717. }
  1718. return ['types' => $types,'list' => $lists];
  1719. }
  1720. private function getBtByName($info,$psid,$age,$payType,$gender){
  1721. $val = 0;
  1722. if($payType == 1){ // 中心在编
  1723. if($info['type'] == 1){
  1724. if($info['gender'] == 0){
  1725. $val = $info['money'];
  1726. }else if($info['gender'] != 0&&$info['gender'] == $gender){
  1727. $val = $info['money'];
  1728. }
  1729. }else if($info['type'] == 3){ // 检测上个月的记录
  1730. $val = 0;
  1731. } else {
  1732. $dd = json_decode($info['values'],true);
  1733. $dd = $dd?$dd:[];
  1734. foreach ($dd as $k=>$v){
  1735. if($v['id'] == $psid){
  1736. if($v['money2']){
  1737. if($age < 15){
  1738. $val = $v['money1'];
  1739. } else if ($age >= 15 && $age < 25){
  1740. $val = $v['money2'];
  1741. }else{
  1742. $val = $v['money3'];
  1743. }
  1744. }else{
  1745. $val = $v['money1'];
  1746. }
  1747. break;
  1748. }
  1749. }
  1750. if($info['gender'] != 0&&$info['gender'] != $gender){
  1751. $val = 0;
  1752. }
  1753. }
  1754. }else{
  1755. if($info['type'] == 1){
  1756. if($info['gender'] == 0){
  1757. $val = $info['money'];
  1758. }else if($info['gender'] != 0&&$info['gender'] == $gender){
  1759. $val = $info['money'];
  1760. }
  1761. }else if($info['type'] == 3){ // 检测上个月的记录
  1762. $val = 0;
  1763. }
  1764. }
  1765. return $val;
  1766. }
  1767. public function info($id){
  1768. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  1769. if(!$info){
  1770. HelpHander::error('数据不存在');
  1771. }
  1772. $types = [];
  1773. $tids = [];
  1774. if($info['types']){
  1775. $tids = explode(',',$info['types']);
  1776. $types = Db::name('user_subsidies')->where('id','in',$info['types'])->select();
  1777. }
  1778. $info['types'] = $types;
  1779. $alltypes = Db::name('user_subsidies')->where('del',0)->where('pay_type',$info['type'])->select();
  1780. $addtypes = [];
  1781. foreach ($alltypes as $v){
  1782. if(!in_array($v['id'],$tids)){
  1783. $addtypes[] = $v;
  1784. }
  1785. }
  1786. $info['addtypes'] = $addtypes;
  1787. $info['sarlist'] = [];
  1788. if($info['status'] == 0){ // 审核中
  1789. $sarlist = Db::name('salary_apply_record')
  1790. ->alias('sar')
  1791. ->join('salary_apply sa','sa.id = sar.salary_apply_id')
  1792. ->where('sar.salary_apply_id',$info['cur_node'])
  1793. ->where('sar.salary_record_id',$info['id'])
  1794. ->where('sar.del',0)
  1795. ->field('sa.cate,sar.*')
  1796. ->order('sar.id asc')
  1797. ->select();
  1798. foreach ($sarlist as $k=>$v){
  1799. $sarlist[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  1800. }
  1801. $info['sarlist'] = $sarlist;
  1802. }
  1803. // 计算实发总金额
  1804. if($info['type'] == 5){
  1805. $totalSalary = Db::name('user_retired_salary')->where('salary_record_id',$id)->sum('salary');
  1806. }else{
  1807. $totalSalary = Db::name('salary_record_log')->where('salary_record_id',$id)->sum('salary');
  1808. }
  1809. $info['total_salary'] = $totalSalary;
  1810. //获取已审批人员
  1811. $alist = Db::name('salary_apply_record')
  1812. ->alias('sar')
  1813. ->join('salary_record sr','sr.id = sar.salary_record_id')
  1814. ->join('user_info ui','ui.user_id = sar.user_id')
  1815. ->where('sar.salary_record_id',$id)
  1816. ->where('sar.status',1)
  1817. ->where('sar.del',0)
  1818. ->field('sar.*,ui.name as user_name')
  1819. ->order('sar.id desc')
  1820. ->select();
  1821. $info['alist'] = $alist?$alist:[];
  1822. return $info;
  1823. }
  1824. public function lists($page,$size,$month,$type){
  1825. if($month != ''){
  1826. $map[] = ['month','=',$month];
  1827. }
  1828. if($type >= 0){
  1829. $map[] = ['type','=',$type];
  1830. }
  1831. $map[] = ['del','=',0];
  1832. $map = empty($map)?true:$map;
  1833. $lists = Db::name('salary_record')
  1834. ->where($map)
  1835. ->page($page,$size)
  1836. ->order('id desc')
  1837. ->select();
  1838. foreach ($lists as $k=>$v){
  1839. // 计算实发总金额
  1840. if($v['type'] == 5){
  1841. $totalSalary = Db::name('user_retired_salary')->where('salary_record_id',$v['id'])->sum('salary');
  1842. }else{
  1843. $totalSalary = Db::name('salary_record_log')->where('salary_record_id',$v['id'])->sum('salary');
  1844. }
  1845. $lists[$k]['total_salary'] = $totalSalary;
  1846. }
  1847. $total = Db::name('salary_record')
  1848. ->where($map)->count();
  1849. $data = [
  1850. 'total' => $total,
  1851. 'list' => $lists?$lists:[]
  1852. ];
  1853. return $data;
  1854. }
  1855. public function payinfo($id){
  1856. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  1857. if(!$info){
  1858. HelpHander::error('数据不存在');
  1859. }
  1860. $info['user_name'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  1861. if($info['status'] == 1||$info['status'] == 2){
  1862. $last_time = Db::name('salary_apply_record')
  1863. ->where('salary_record_id',$info['id'])
  1864. ->where('status',1)
  1865. ->where('del',0)
  1866. ->order('id desc')
  1867. ->value('update_time');
  1868. $info['last_time'] = date('Y年m月d日',strtotime($last_time));
  1869. if($info['id'] > 407){
  1870. $rsinfo = Db::name('salary_apply_record')
  1871. ->alias('sar')
  1872. // ->join('salary_apply sa','sa.id = sar.salary_apply_id')
  1873. ->join('user_info ui','ui.user_id = sar.user_id')
  1874. ->where('sar.salary_record_id',$info['id'])
  1875. ->where('sar.status',1)
  1876. ->where('sar.sign',2)
  1877. ->where('sar.del',0)
  1878. ->order('sar.id asc')
  1879. ->value('ui.name');
  1880. $info['rsl_user_name'] = $rsinfo?$rsinfo:'';
  1881. $cwinfo = Db::name('salary_apply_record')
  1882. ->alias('sar')
  1883. // ->join('salary_apply sa','sa.id = sar.salary_apply_id')
  1884. ->join('user_info ui','ui.user_id = sar.user_id')
  1885. ->where('sar.sign',1)
  1886. ->where('sar.status',1)
  1887. ->where('sar.del',0)
  1888. ->where('sar.salary_record_id',$info['id'])
  1889. ->order('sar.id asc')
  1890. ->value('ui.name');
  1891. $info['kj_user_name'] = $cwinfo?$cwinfo:'';
  1892. $cwlinfo = Db::name('salary_apply_record')
  1893. ->alias('sar')
  1894. // ->join('salary_apply sa','sa.id = sar.salary_apply_id')
  1895. ->join('user_info ui','ui.user_id = sar.user_id')
  1896. ->where('sar.salary_record_id',$info['id'])
  1897. ->where('sar.sign',3)
  1898. ->where('sar.status',1)
  1899. ->where('sar.del',0)
  1900. ->order('sar.id asc')
  1901. ->value('ui.name');
  1902. $info['cwl_user_name'] = $cwlinfo?$cwlinfo:'';
  1903. }else{
  1904. $rsinfo = Db::name('salary_apply_record')
  1905. ->alias('sar')
  1906. ->join('salary_apply sa','sa.id = sar.salary_apply_id')
  1907. ->join('user_info ui','ui.user_id = sar.user_id')
  1908. ->where('sar.salary_record_id',$info['id'])
  1909. ->where('sar.status',1)
  1910. ->where('sa.cate',2)
  1911. ->where('sar.del',0)
  1912. ->order('sar.id asc')
  1913. ->value('ui.name');
  1914. $info['rsl_user_name'] = $rsinfo?$rsinfo:'';
  1915. $cwinfo = Db::name('salary_apply_record')
  1916. ->alias('sar')
  1917. ->join('salary_apply sa','sa.id = sar.salary_apply_id')
  1918. ->join('user_info ui','ui.user_id = sar.user_id')
  1919. ->where('sa.cate',3)
  1920. ->where('sar.status',1)
  1921. ->where('sar.del',0)
  1922. ->where('sar.salary_record_id',$info['id'])
  1923. ->order('sar.id asc')
  1924. ->value('ui.name');
  1925. $info['kj_user_name'] = $cwinfo?$cwinfo:'';
  1926. $cwlinfo = Db::name('salary_apply_record')
  1927. ->alias('sar')
  1928. ->join('salary_apply sa','sa.id = sar.salary_apply_id')
  1929. ->join('user_info ui','ui.user_id = sar.user_id')
  1930. ->where('sar.salary_record_id',$info['id'])
  1931. ->where('sa.cate',4)
  1932. ->where('sar.status',1)
  1933. ->where('sar.del',0)
  1934. ->order('sar.id asc')
  1935. ->value('ui.name');
  1936. $info['cwl_user_name'] = $cwlinfo?$cwlinfo:'';
  1937. if($info['type'] == 1||$info['type'] == 3||$info['type'] == 5){
  1938. $info['kj_user_name'] = '郑琳';
  1939. }else{
  1940. $info['kj_user_name'] = '汪涵';
  1941. }
  1942. $info['cwl_user_name'] = '许倩鸿';
  1943. }
  1944. }else{
  1945. $info['last_time'] = '';
  1946. $info['kj_user_name'] = '';
  1947. $info['rsl_user_name'] = '';
  1948. $info['cwl_user_name'] = '';
  1949. }
  1950. if($info['type'] == 5){
  1951. $totalSalary = Db::name('user_retired_salary')->where('salary_record_id',$id)->sum('salary');
  1952. }else{
  1953. $totalSalary = Db::name('salary_record_log')->where('salary_record_id',$id)->sum('salary');
  1954. }
  1955. $use = "";
  1956. switch ($info['type']){
  1957. case 1:
  1958. $use = "发放中心编制工资";
  1959. break;
  1960. case 2:
  1961. $use = "发放新华聘用工资";
  1962. break;
  1963. case 3:
  1964. $use = "发放中心劳务工资";
  1965. break;
  1966. case 4:
  1967. $use = "发放新华劳务工资";
  1968. break;
  1969. case 5:
  1970. $use = "发放中心退休工资";
  1971. break;
  1972. case 6:
  1973. $use = "发放中心编制工资";
  1974. break;
  1975. case 7:
  1976. $use = "发放新华聘用年绩效";
  1977. break;
  1978. case 8:
  1979. $use = "发放中心劳务年绩效";
  1980. break;
  1981. case 9:
  1982. $use = "发放新华劳务年绩效";
  1983. break;
  1984. }
  1985. $info['use'] = $use;
  1986. // 获取上个月
  1987. $lastmonth = get_lastmonth_by_day($info['month'].'-01');
  1988. // 社保的汇缴是滞后的,即3月请款单上应汇缴的是2月的社保
  1989. $lastmonthtxt = date('Y年m月',strtotime($lastmonth.'-01'));
  1990. if($info['type'] == 1){
  1991. $month = date('Y年m月',strtotime($info['month'].'-01'));
  1992. // $lastinfo = Db::name('salary_record_sb')
  1993. // ->alias('a')
  1994. // ->join('salary_record b','a.salary_record_id = b.id')
  1995. // ->where('b.month',$lastmonth)
  1996. // ->where('a.ylnj','>',0)
  1997. // ->field('b.*')
  1998. // ->find();
  1999. // if($lastinfo){
  2000. // $info['ylause'] = "基建中心".$lastmonthtxt.$lastinfo['nums']."人职业年金汇缴";
  2001. // $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylac');
  2002. // $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylap');
  2003. // }else{
  2004. // $info['ylause'] = "基建中心".$lastmonthtxt."0人职业年金汇缴";
  2005. // $hjsalary1 = 0;
  2006. // $hjsalary2 = 0;
  2007. // }
  2008. $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylac');
  2009. $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylap');
  2010. $info['ylause'] = "基建中心".$lastmonthtxt.$info['nums']."人养老保险汇缴";
  2011. $info['ylasalary'] = round($hjsalary1+$hjsalary2,2);
  2012. $info['ylacompany'] = "中央国家机关养老保险管理中心";
  2013. $info['njcompany'] = "中国建设银行中央国家机关事业单位职业年金基金归集财产";
  2014. $info['njuse'] = "基建中心".$lastmonthtxt.$info['nums']."人职业年金汇缴";
  2015. $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('qynjc');
  2016. $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('qynjp');
  2017. $info['njsalary'] = round($hjsalary1+$hjsalary2,2);
  2018. // if($lastinfo){
  2019. // $info['sbuse'] = "基建中心".$lastmonthtxt.$lastinfo['nums']."人医疗(含生育)、失业、工伤保险汇缴";
  2020. // $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylic');
  2021. // $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylip');
  2022. // $hjsalary3 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('syec');
  2023. // $hjsalary4 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('syep');
  2024. // $hjsalary5 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('gsc');
  2025. // }else{
  2026. // $info['sbuse'] = "基建中心".$lastmonthtxt."0人医疗(含生育)、失业、工伤保险汇缴";
  2027. // $hjsalary1 = 0;
  2028. // $hjsalary2 = 0;
  2029. // $hjsalary3 = 0;
  2030. // $hjsalary4 = 0;
  2031. // $hjsalary5 = 0;
  2032. // }
  2033. $info['sbuse'] = "基建中心".$lastmonthtxt.$info['nums']."人医疗(含生育)、失业、工伤保险汇缴";
  2034. $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylic');
  2035. $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylip');
  2036. $hjsalary3 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('syec');
  2037. $hjsalary4 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('syep');
  2038. $hjsalary5 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gsc');
  2039. $info['sbcompany'] = "国家税务总局北京市西城区税务局";
  2040. // $info['sbuse'] = "基建中心".$month.$info['nums']."人医疗(含生育)、失业、工伤保险汇缴";
  2041. $info['sbsalary'] = round($hjsalary1+$hjsalary2+$hjsalary3+$hjsalary4+$hjsalary5,2);
  2042. $info['gjjcompany'] = "中央国家机关住房资金管理中心";
  2043. $info['gjjuse'] = "基建中心".$month.$info['nums']."人住房公积金汇缴";
  2044. $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gjjc');
  2045. $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gjjp');
  2046. $info['gjjsalary'] = round($hjsalary1+$hjsalary2,2);
  2047. }else if($info['type'] == 2){
  2048. $month = date('Y年m月',strtotime($info['month'].'-01'));
  2049. $info['gjjcompany'] = "中央国家机关住房资金管理中心";
  2050. $info['gjjuse'] = "新华公司".$month.$info['nums']."人住房公积金汇缴";
  2051. $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gjjc');
  2052. $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gjjp');
  2053. $info['gjjsalary'] = round($hjsalary1+$hjsalary2,2);
  2054. $info['njcompany'] = "中国银行建信养老养颐乐安享集合计划受托财产";
  2055. $info['njuse'] = "新华公司".$month.$info['nums']."人企业年金汇缴";
  2056. $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('qynjc');
  2057. $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('qynjp');
  2058. $info['njsalary'] = round($hjsalary1+$hjsalary2,2);
  2059. // $lastinfo = Db::name('salary_record_sb')
  2060. // ->alias('a')
  2061. // ->join('salary_record b','a.salary_record_id = b.id')
  2062. // ->where('b.month',$lastmonth)
  2063. // ->where('a.sb','>',0)
  2064. // ->field('b.*')
  2065. // ->find();
  2066. // if($lastinfo){
  2067. // $info['sbuse'] = "新华公司".$lastmonthtxt.$lastinfo['nums']."人养老、医疗(含生育)、失业、工伤保险汇缴";
  2068. // $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylic');
  2069. // $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylip');
  2070. // $hjsalary3 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('syec');
  2071. // $hjsalary4 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('syep');
  2072. // $hjsalary5 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('gsc');
  2073. // $hjsalary6 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylac');
  2074. // $hjsalary7 = Db::name('salary_record_sb')->where('salary_record_id',$lastinfo['id'])->sum('ylap');
  2075. // }else{
  2076. // $info['sbuse'] = "新华公司".$lastmonthtxt."0人养老、医疗(含生育)、失业、工伤保险汇缴";
  2077. // $hjsalary1 = 0;
  2078. // $hjsalary2 = 0;
  2079. // $hjsalary3 = 0;
  2080. // $hjsalary4 = 0;
  2081. // $hjsalary5 = 0;
  2082. // $hjsalary6 = 0;
  2083. // $hjsalary7 = 0;
  2084. // }
  2085. $info['sbuse'] = "新华公司".$lastmonthtxt.$info['nums']."人养老、医疗(含生育)、失业、工伤保险汇缴";
  2086. $hjsalary1 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylic');
  2087. $hjsalary2 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylip');
  2088. $hjsalary3 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('syec');
  2089. $hjsalary4 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('syep');
  2090. $hjsalary5 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('gsc');
  2091. $hjsalary6 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylac');
  2092. $hjsalary7 = Db::name('salary_record_sb')->where('salary_record_id',$id)->sum('ylap');
  2093. $info['sbcompany'] = "国家税务总局北京市西城区税务局";
  2094. // $info['sbuse'] = "基建中心".$month.$info['nums']."人养老、医疗(含生育)、失业、工伤保险汇缴";
  2095. $info['sbsalary'] = round($hjsalary1+$hjsalary2+$hjsalary3+$hjsalary4+$hjsalary5+$hjsalary6+$hjsalary7,2);
  2096. }
  2097. $info['total_salary'] = $totalSalary;
  2098. return $info;
  2099. }
  2100. public function del($id){
  2101. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  2102. if(!$info){
  2103. HelpHander::error('数据不存在');
  2104. }
  2105. // if($info['status'] == 2){
  2106. // HelpHander::error('该状态不能删除');
  2107. // }
  2108. $ret = Db::name('salary_record')->where('id',$id)->setField('del',1);
  2109. if(!$ret){
  2110. HelpHander::error('操作失败');
  2111. }
  2112. return true;
  2113. }
  2114. public function send($id){
  2115. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  2116. if(!$info){
  2117. HelpHander::error('记录不存在');
  2118. }
  2119. if($info['status'] != 3){
  2120. HelpHander::error('该状态不能重新发放');
  2121. }
  2122. $ret = Db::name('salary_record')->where('id',$id)->setField('status',0);
  2123. if(!$ret){
  2124. HelpHander::error('操作失败');
  2125. }
  2126. return true;
  2127. }
  2128. // 工资列表
  2129. public function listLog($page,$size,$name,$id){
  2130. $info = $this->info($id);
  2131. if($info['type'] == 5){ // 退休
  2132. $data = model('UserRetiredSalary')->lists($page,$size,$name,$info['id']);
  2133. } else {
  2134. if($name != ''){
  2135. $map[] = ['ui.name','like','%'.$name.'%'];
  2136. }
  2137. $map[] = ['ur.salary_record_id','=',$info['id']];
  2138. $lists = Db::name('salary_record_log')
  2139. ->alias('ur')
  2140. ->join('user_info ui','ui.user_id = ur.user_id')
  2141. ->where($map)
  2142. ->field('ur.*,ui.name')
  2143. ->page($page,$size)
  2144. ->order('ui.sorts3 asc,ur.id desc')
  2145. ->select();
  2146. $lists = $lists?$lists:[];
  2147. foreach ($lists as $k=>$v){
  2148. $lists[$k]['user_sn'] = think_encrypt($v['user_id']);
  2149. if($info['type'] == 1||$info['type'] == 2){
  2150. $sb = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->find();
  2151. $lists[$k]['gjjp'] = $sb?$sb['gjjp']:0;
  2152. $lists[$k]['qynjp'] = $sb?$sb['qynjp']:0;
  2153. $lists[$k]['ylap'] = $sb?$sb['ylap']:0;
  2154. $lists[$k]['syep'] = $sb?$sb['syep']:0;
  2155. $lists[$k]['ylip'] = $sb?$sb['ylip']:0;
  2156. }
  2157. $btjson = $v['btjson']?json_decode($v['btjson'],true):[];
  2158. $kcmoney = 0;
  2159. foreach ($btjson as $kk=>$vv){
  2160. $lists[$k]['subsidies'.$vv['id']] = $vv['money'];
  2161. if($vv['cate'] == 2){ // 扣除项
  2162. $kcmoney += $vv['money'];
  2163. }
  2164. }
  2165. $lists[$k]['dktotal'] = round($v['sbgjj'] + $v['tax'] + $kcmoney,2);
  2166. $depjob = Db::name('user_job')
  2167. ->alias('uj')
  2168. ->join('job j','j.id = uj.job_id')
  2169. ->join('dep d','d.id = j.dep_id')
  2170. ->where('d.enable',1)
  2171. ->where('uj.user_id',$v['user_id'])
  2172. ->field('d.name as dep_name,j.name as job_name')
  2173. ->find();
  2174. $lists[$k]['dep_name'] = $depjob?$depjob['dep_name']:'';
  2175. //职级
  2176. $lists[$k]['job_name'] = Db::name('post_salary')->where('id',$v['psid'])->value('title');
  2177. }
  2178. $total = Db::name('salary_record_log')
  2179. ->alias('ur')
  2180. ->join('user_info ui','ui.user_id = ur.user_id')
  2181. ->where($map)->count();
  2182. $data = [
  2183. 'total' => $total,
  2184. 'list' => $lists?$lists:[]
  2185. ];
  2186. }
  2187. $data['info'] = $info;
  2188. return $data;
  2189. }
  2190. // 个税累计列表
  2191. public function listSpecial($page,$size,$name,$id){
  2192. $info = $this->info($id);
  2193. if($info){
  2194. if($name != ''){
  2195. $map[] = ['ui.name','like','%'.$name.'%'];
  2196. }
  2197. $map[] = ['ur.salary_record_id','=',$info['id']];
  2198. $lists = Db::name('salary_record_special')
  2199. ->alias('ur')
  2200. ->join('user_info ui','ui.user_id = ur.user_id')
  2201. ->where($map)
  2202. ->field('ur.*,ui.name,ui.pwlevel')
  2203. ->page($page,$size)
  2204. ->order('ui.sorts3 asc,ur.id desc')
  2205. ->select();
  2206. $lists = $lists?$lists:[];
  2207. foreach ($lists as $k=>$v){
  2208. $ljkc = $v['ljsr'] - $v['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
  2209. $lists[$k]['ljkc'] = round($ljkc,2);
  2210. $lists[$k]['ljyykyjsr'] = 0;
  2211. $lists[$k]['jmsr'] = 0;
  2212. $ljykjse = $v['ljykjse'] + $v['tax'];
  2213. $lists[$k]['ljykjse'] = round($ljykjse,2);
  2214. if($ljkc > 0){
  2215. $tax = $this->getYkyjTax($ljkc); // 累计应预扣预缴税额
  2216. $lists[$k]['ljyykyjsr'] = round($tax,2);
  2217. $jmse = round($tax*$v['jmsebl'],2); // 减免税额
  2218. $lists[$k]['jmsr'] = $jmse;
  2219. $tax = round($tax - $jmse - $ljykjse,2);
  2220. $tax = $tax>0?$tax:0;
  2221. }else{
  2222. $tax = 0;
  2223. }
  2224. $lists[$k]['nextTax'] = $tax;
  2225. }
  2226. $total = Db::name('salary_record_special')
  2227. ->alias('ur')
  2228. ->join('user_info ui','ui.user_id = ur.user_id')
  2229. ->where($map)->count();
  2230. $data = [
  2231. 'total' => $total,
  2232. 'list' => $lists?$lists:[]
  2233. ];
  2234. }
  2235. $data['info'] = $info;
  2236. return $data;
  2237. }
  2238. // 导出
  2239. public function exportListLog($name,$id){
  2240. $info = $this->info($id);
  2241. if($info['type'] == 5){ // 退休
  2242. $data = model('UserRetiredSalary')->lists(1,100000,$name,$info['id']);
  2243. } else {
  2244. if($name != ''){
  2245. $map[] = ['ui.name','like','%'.$name.'%'];
  2246. }
  2247. $map[] = ['ur.salary_record_id','=',$info['id']];
  2248. $lists = Db::name('salary_record_log')
  2249. ->alias('ur')
  2250. ->join('user_info ui','ui.user_id = ur.user_id')
  2251. ->where($map)
  2252. ->field('ur.*,ui.name,ui.pwlevel')
  2253. ->order('ui.sorts3 asc,ur.id desc')
  2254. ->select();
  2255. $lists = $lists?$lists:[];
  2256. foreach ($lists as $k=>$v){
  2257. if($info['type'] == 1||$info['type'] == 2){
  2258. $sb = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->find();
  2259. $lists[$k]['gjjp'] = $sb?$sb['gjjp']:0;
  2260. $lists[$k]['qynjp'] = $sb?$sb['qynjp']:0;
  2261. $lists[$k]['ylap'] = $sb?$sb['ylap']:0;
  2262. $lists[$k]['syep'] = $sb?$sb['syep']:0;
  2263. $lists[$k]['ylip'] = $sb?$sb['ylip']:0;
  2264. }
  2265. $btjson = $v['btjson']?json_decode($v['btjson'],true):[];
  2266. $kcmoney = 0;
  2267. foreach ($btjson as $kk=>$vv){
  2268. $lists[$k]['subsidies'.$vv['id']] = $vv['money'];
  2269. if($vv['cate'] == 2){
  2270. $kcmoney += $vv['money'];
  2271. }
  2272. }
  2273. $lists[$k]['dktotal'] = $v['sbgjj'] + $v['tax'] + $kcmoney;
  2274. $depjob = Db::name('user_job')
  2275. ->alias('uj')
  2276. ->join('job j','j.id = uj.job_id')
  2277. ->join('dep d','d.id = j.dep_id')
  2278. ->where('uj.user_id',$v['user_id'])
  2279. ->field('d.name as dep_name,j.name as job_name')
  2280. ->find();
  2281. $lists[$k]['dep_name'] = $depjob?$depjob['dep_name']:'';
  2282. //职级
  2283. $lists[$k]['job_name'] = Db::name('post_salary')->where('id',$v['psid'])->value('title');
  2284. }
  2285. if($lists){
  2286. $darr = $lists[0];
  2287. foreach ($darr as $k=>$v){
  2288. $darr[$k] = '';
  2289. if($k == 'name'){
  2290. $darr[$k] = '合计';
  2291. }
  2292. if($info['type'] == 5){
  2293. $arrs = ['bz','zysdbt','fb','wybt'];
  2294. if(in_array($k,$arrs)){
  2295. $val = 0;
  2296. foreach ($lists as $kk=>$vv){
  2297. $val += $vv[$k];
  2298. }
  2299. $darr[$k] = round($val,2);
  2300. }
  2301. }else{
  2302. $arrs = ['gwgz','xjgz','jcjx','yjx','should_total','child','dktotal','njx','tax'];
  2303. if(in_array($k,$arrs)){
  2304. $val = 0;
  2305. foreach ($lists as $kk=>$vv){
  2306. $val += $vv[$k];
  2307. }
  2308. $darr[$k] = round($val,2);
  2309. }
  2310. }
  2311. $arrs = ['salary'];
  2312. foreach ($info['types'] as $kkk=>$vvv){
  2313. $arrs[] = "subsidies".$vvv['id'];
  2314. }
  2315. if(in_array($k,$arrs)){
  2316. $val = 0;
  2317. foreach ($lists as $kk=>$vv){
  2318. $val += $vv[$k];
  2319. }
  2320. $darr[$k] = round($val,2);
  2321. }
  2322. if($info['type'] == 1||$info['type'] == 2){
  2323. $arrs = ['gjjp','qynjp','ylap','ylip','syep','tax'];
  2324. if(in_array($k,$arrs)){
  2325. $val = 0;
  2326. foreach ($lists as $kk=>$vv){
  2327. $val += $vv[$k];
  2328. }
  2329. $darr[$k] = round($val,2);
  2330. }
  2331. }
  2332. if($info['type'] == 3||$info['type'] == 4){
  2333. $arrs = ['tax'];
  2334. if(in_array($k,$arrs)){
  2335. $val = 0;
  2336. foreach ($lists as $kk=>$vv){
  2337. $val += $vv[$k];
  2338. }
  2339. $darr[$k] = round($val,2);
  2340. }
  2341. }
  2342. }
  2343. $lists[] = $darr;
  2344. }
  2345. //转成数字格式
  2346. $extra = ['dep_name','name','job_name'];
  2347. foreach ($lists as $k=>$v){
  2348. foreach ($v as $kk=>$vv){
  2349. if(!in_array($kk,$extra)){
  2350. $v[$kk] = (float)$vv;
  2351. }
  2352. }
  2353. $lists[$k] = $v;
  2354. }
  2355. $data = [
  2356. 'total' => count($lists),
  2357. 'list' => $lists?$lists:[]
  2358. ];
  2359. }
  2360. $columns = [
  2361. ["title" => "姓名","key" => "name"]
  2362. ];
  2363. if($info['type'] == 5){
  2364. $columns[] = ["title" => "补助","key" => "bz"];
  2365. $columns[] = ["title" => "中央适当补贴","key" => "zysdbt"];
  2366. $columns[] = ["title" => "房补","key" => "fb"];
  2367. $columns[] = ["title" => "物业补贴","key" => "wybt"];
  2368. }else if(in_array($info['type'],[1,2,3,4])){
  2369. $columns[] = ["title" => "部门","key" => "depName"];
  2370. $columns[] = ["title" => "职级","key" => "jobName"];
  2371. $columns[] = ["title" => "薪级","key" => "pwlevel"];
  2372. $columns[] = ["title" => "岗位工资","key" => "gwgz"];
  2373. $columns[] = ["title" => "薪级工资","key" => "xjgz"];
  2374. $columns[] = ["title" => "基础绩效","key" => "jcjx"];
  2375. $columns[] = ["title" => "月绩效","key" => "yjx"];
  2376. }else if(in_array($info['type'],[6,7])){
  2377. $columns[] = ["title" => "部门","key" => "depName"];
  2378. $columns[] = ["title" => "职级","key" => "jobName"];
  2379. $columns[] = ["title" => "薪级","key" => "pwlevel"];
  2380. $columns[] = ["title" => "年绩效","key" => "njx"];
  2381. $columns[] = ["title" => "等次","key" => "level"];
  2382. $columns[] = ["title" => "系数","key" => "factor"];
  2383. $columns[] = ["title" => "发放月数","key" => "months"];
  2384. }else if(in_array($info['type'],[8,9])){
  2385. $columns[] = ["title" => "部门","key" => "depName"];
  2386. $columns[] = ["title" => "职级","key" => "jobName"];
  2387. $columns[] = ["title" => "薪级","key" => "pwlevel"];
  2388. $columns[] = ["title" => "年绩效","key" => "njx"];
  2389. }
  2390. foreach ($info['types'] as $k=>$v){
  2391. if($v['cate'] == 1){
  2392. $columns[] = ["title" => $v['title'],"key" => "subsidies".$v['id']];
  2393. }
  2394. }
  2395. if($info['type'] != 5){
  2396. $columns[] = ["title" => "应发工资","key" => "shouldTotal"];
  2397. }
  2398. foreach ($info['types'] as $k=>$v){
  2399. if($v['cate'] != 1){
  2400. $columns[] = ["title" => $v['title'],"key" => "subsidies".$v['id']];
  2401. }
  2402. }
  2403. if($info['type'] == 1||$info['type'] == 2){
  2404. // $columns[] = ["title" => "社保公积金代扣","key" => "sbgjj"];
  2405. $columns[] = ["title" => "公积金","key" => "gjjp"];
  2406. if($info['type'] == 1){
  2407. $columns[] = ["title" => "职业年金","key" => "qynjp"];
  2408. }else{
  2409. $columns[] = ["title" => "企业年金","key" => "qynjp"];
  2410. }
  2411. $columns[] = ["title" => "养老保险","key" => "ylap"];
  2412. $columns[] = ["title" => "医疗保险","key" => "ylip"];
  2413. $columns[] = ["title" => "失业保险","key" => "syep"];
  2414. }
  2415. if(in_array($info['type'],[6,7])){
  2416. $columns[] = ["title" => "适用税率","key" => "taxrate"];
  2417. }
  2418. if($info['type'] != 5){
  2419. $columns[] = ["title" => "个税","key" => "tax"];
  2420. }
  2421. if($info['type'] < 5){
  2422. $columns[] = ["title" => "代扣合计","key" => "dktotal"];
  2423. }
  2424. $columns[] = ["title" => "实发工资","key" => "salary"];
  2425. $data = [
  2426. 'columns' => $columns,
  2427. 'list' => $data['list']
  2428. ];
  2429. return $data;
  2430. }
  2431. public function mySalary($page,$size,$month,$userId){
  2432. if($month != ''){
  2433. $map[] = ['month','=',$month];
  2434. }
  2435. $map[] = ['user_id','=',$userId];
  2436. $lists = Db::name('common_salary')
  2437. ->where($map)
  2438. ->page($page,$size)
  2439. ->order('month desc,id desc')
  2440. ->select();
  2441. foreach ($lists as $k=>$v){
  2442. if($v['type'] == 1){
  2443. $lists[$k]['type_name'] = '中心编制';
  2444. }else if($v['type'] == 2){
  2445. $lists[$k]['type_name'] = '新华聘用';
  2446. }else if($v['type'] == 3){
  2447. $lists[$k]['type_name'] = '中心劳务';
  2448. }else if($v['type'] == 4){
  2449. $lists[$k]['type_name'] = '新华劳务';
  2450. }else if($v['type'] == 6){
  2451. $lists[$k]['type_name'] = '中心编制年绩效';
  2452. }else if($v['type'] == 7){
  2453. $lists[$k]['type_name'] = '新华聘用年绩效';
  2454. }else if($v['type'] == 8){
  2455. $lists[$k]['type_name'] = '中心劳务年绩效';
  2456. }else if($v['type'] == 9){
  2457. $lists[$k]['type_name'] = '新华劳务年绩效';
  2458. }else{
  2459. $lists[$k]['type_name'] = '中心退休';
  2460. }
  2461. }
  2462. $total = Db::name('common_salary')
  2463. ->where($map)->count();
  2464. $data = [
  2465. 'total' => $total,
  2466. 'list' => $lists?$lists:[]
  2467. ];
  2468. return $data;
  2469. }
  2470. public function salaryDetail($id,$userId){
  2471. $info = Db::name('common_salary')->where('id',$id)->where('user_id',$userId)->find();
  2472. if(!$info){
  2473. HelpHander::error('记录不存在');
  2474. }
  2475. if($info['type'] == 5){ // 退休
  2476. $data = Db::name('user_retired_salary')->where('salary_record_id',$id)->where('user_id',$userId)->find();
  2477. }else{ // 其他
  2478. $data = Db::name('salary_record_log')->where('salary_record_id',$id)->where('user_id',$userId)->find();
  2479. }
  2480. if(!$data){
  2481. HelpHander::error('工资不存在');
  2482. }
  2483. $data['pay_type'] = $info['type'];
  2484. $data['btjson'] = $data['btjson']?json_decode($data['btjson'],true):[];
  2485. $btmoney = $kcmoney = 0;
  2486. foreach ($data['btjson'] as $k=>$v){
  2487. if($v['cate'] == 1){ // 补贴
  2488. $btmoney += floatval($v['money']);
  2489. }else{
  2490. $kcmoney += floatval($v['money']);
  2491. }
  2492. }
  2493. $data['btmoney'] = round($btmoney,2);
  2494. $data['kcmoney'] = round($kcmoney,2);
  2495. $data['gjj'] = '0';
  2496. $data['yla'] = '0';
  2497. $data['qynj'] = '0';
  2498. $data['yli'] = '0';
  2499. $data['sye'] = '0';
  2500. $sbdata = Db::name('salary_record_sb')->where('salary_record_id',$id)->where('user_id',$userId)->find();
  2501. if($sbdata){
  2502. $data['gjj'] = $sbdata['gjjp'];
  2503. $data['yla'] = $sbdata['ylap'];
  2504. $data['qynj'] = $sbdata['qynjp'];
  2505. $data['yli'] = $sbdata['ylip'];
  2506. $data['sye'] = $sbdata['syep'];
  2507. }
  2508. return $data;
  2509. }
  2510. public function sbinfo($id,$uid){
  2511. $info = Db::name('salary_record_sb')
  2512. ->alias('srs')
  2513. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2514. ->where('srs.salary_record_id',$id)
  2515. ->where('srs.user_id',$uid)
  2516. ->field('sr.type,srs.*')
  2517. ->find();
  2518. if(!$info){
  2519. HelpHander::error('数据不存在');
  2520. }
  2521. return $info;
  2522. }
  2523. public function taxinfo($id,$uid){
  2524. $info = Db::name('salary_record_special')
  2525. ->alias('srs')
  2526. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2527. ->where('srs.salary_record_id',$id)
  2528. ->where('srs.user_id',$uid)
  2529. ->field('sr.type,srs.*')
  2530. ->find();
  2531. if(!$info){
  2532. HelpHander::error('数据不存在');
  2533. }
  2534. return $info;
  2535. }
  2536. // 同意 或签
  2537. public function agree($id,$userId,$orgId){
  2538. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  2539. if(!$info){
  2540. HelpHander::error('记录不存在');
  2541. }
  2542. if($info['status'] != 0){
  2543. HelpHander::error('无权限操作');
  2544. }
  2545. $uinfo = Db::name('salary_apply_record')
  2546. ->where('salary_record_id',$id)
  2547. ->where('user_id',$userId)
  2548. ->where('salary_apply_id',$info['cur_node'])
  2549. ->where('status',0)
  2550. ->where('del',0)
  2551. ->find();
  2552. if(!$uinfo){
  2553. HelpHander::error('无权限操作');
  2554. }
  2555. $apply = json_decode($info['apply'],true);
  2556. Db::startTrans();
  2557. try{
  2558. $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')]);
  2559. if(!$res){
  2560. \exception('操作失败');
  2561. }
  2562. if($info['cur_node'] == $apply[count($apply)-1]['id']){ // 最后一个节点
  2563. $res2 = Db::name('salary_record')->where('id',$id)->setField('status',1);
  2564. if(!$res2){
  2565. \exception('操作失败');
  2566. }
  2567. }else{
  2568. // 获取下一节点
  2569. $next = [];
  2570. foreach ($apply as $k=>$v){
  2571. if($v['id'] == $info['cur_node']){
  2572. $next = $apply[$k+1];
  2573. break;
  2574. }
  2575. }
  2576. $res2 = Db::name('salary_record')->where('id',$id)->setField('cur_node',$next['id']);
  2577. if(!$res2){
  2578. \exception('操作失败');
  2579. }
  2580. // 生成审核记录
  2581. $uids = explode(',',$next['userids']);
  2582. foreach ($uids as $k=>$v){
  2583. $sd = [
  2584. 'salary_record_id' => $id,
  2585. 'user_id' => $v,
  2586. 'status' => 0,
  2587. 'create_time' => date('Y-m-d H:i:s'),
  2588. 'salary_apply_id' => $next['id'],
  2589. 'sign' => $next['sign']
  2590. ];
  2591. $ret = Db::name('salary_apply_record')->insertGetId($sd);
  2592. if(!$ret){
  2593. \exception('操作失败');
  2594. }
  2595. model('Message')->add(3,$id,12,$v,$orgId,'工资流程待您的审核确认');
  2596. }
  2597. }
  2598. Db::commit();
  2599. }catch (Exception $e){
  2600. Db::rollback();
  2601. $this->error = '操作失败';
  2602. return false;
  2603. }
  2604. return true;
  2605. }
  2606. // 打回
  2607. public function disagree($id,$userId,$orgId){
  2608. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  2609. if(!$info){
  2610. HelpHander::error('记录不存在');
  2611. }
  2612. if($info['status'] != 0){
  2613. HelpHander::error('无权限操作');
  2614. }
  2615. $uinfo = Db::name('salary_apply_record')
  2616. ->where('salary_record_id',$id)
  2617. ->where('user_id',$userId)
  2618. ->where('salary_apply_id',$info['cur_node'])
  2619. ->where('status',0)
  2620. ->where('del',0)
  2621. ->find();
  2622. if(!$uinfo){
  2623. HelpHander::error('无权限操作');
  2624. }
  2625. Db::startTrans();
  2626. try{
  2627. $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')]);
  2628. if(!$res){
  2629. \exception('操作失败');
  2630. }
  2631. $res2 = Db::name('salary_record')->where('id',$id)->setField('status',3);
  2632. if(!$res2){
  2633. \exception('操作失败');
  2634. }
  2635. Db::commit();
  2636. }catch (Exception $e){
  2637. Db::rollback();
  2638. $this->error = '操作失败';
  2639. return false;
  2640. }
  2641. return true;
  2642. }
  2643. public function reapply($id,$userId,$orgId){
  2644. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  2645. if(!$info){
  2646. HelpHander::error('记录不存在');
  2647. }
  2648. if($info['status'] != 3 || $info['user_id'] != $userId){
  2649. HelpHander::error('无权限操作');
  2650. }
  2651. Db::startTrans();
  2652. try{
  2653. // 删除原审批记录
  2654. Db::name('salary_apply_record')->where('salary_record_id',$id)->update(['del'=>1,'update_time'=>date('Y-m-d H:i:s')]);
  2655. $apply = json_decode($info['apply'],true);
  2656. $curinfo = $apply[0];
  2657. $res2 = Db::name('salary_record')->where('id',$id)->update(['status'=>0,'cur_node'=>$curinfo['id']]);
  2658. if(!$res2){
  2659. \exception('操作失败');
  2660. }
  2661. // 生成审核记录
  2662. $uids = explode(',',$curinfo['userids']);
  2663. foreach ($uids as $k=>$v){
  2664. $sd = [
  2665. 'salary_record_id' => $id,
  2666. 'user_id' => $v,
  2667. 'status' => 0,
  2668. 'create_time' => date('Y-m-d H:i:s'),
  2669. 'salary_apply_id' => $curinfo['id'],
  2670. 'sign' => $curinfo['sign'],
  2671. ];
  2672. $ret = Db::name('salary_apply_record')->insertGetId($sd);
  2673. if(!$ret){
  2674. \exception('操作失败');
  2675. }
  2676. model('Message')->add(3,$id,12,$v,$orgId,'工资流程待您的审核确认');
  2677. }
  2678. Db::commit();
  2679. }catch (Exception $e){
  2680. Db::rollback();
  2681. $this->error = '操作失败';
  2682. return false;
  2683. }
  2684. return true;
  2685. }
  2686. // 会签版本
  2687. public function agreeold($id,$userId,$orgId){
  2688. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  2689. if(!$info){
  2690. HelpHander::error('记录不存在');
  2691. }
  2692. if($info['status'] != 0){
  2693. HelpHander::error('无权限操作');
  2694. }
  2695. $uinfo = Db::name('salary_apply_record')
  2696. ->where('salary_record_id',$id)
  2697. ->where('user_id',$userId)
  2698. ->where('salary_apply_id',$info['cur_node'])
  2699. ->where('status',0)
  2700. ->where('del',0)
  2701. ->find();
  2702. if(!$uinfo){
  2703. HelpHander::error('无权限操作');
  2704. }
  2705. // 检查是否有除自己之外的未处理的人
  2706. $others = Db::name('salary_apply_record')
  2707. ->where('salary_record_id',$id)
  2708. ->where('user_id','<>',$userId)
  2709. ->where('salary_apply_id',$info['cur_node'])
  2710. ->where('del',0)
  2711. ->where('status',0)
  2712. ->find();
  2713. $apply = json_decode($info['apply'],true);
  2714. Db::startTrans();
  2715. try{
  2716. $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')]);
  2717. if(!$res){
  2718. \exception('操作失败');
  2719. }
  2720. if(!$others){ // 没有未处理的人了,走下一节点
  2721. if($info['cur_node'] == $apply[count($apply)-1]['id']){ // 最后一个节点
  2722. $res2 = Db::name('salary_record')->where('id',$id)->setField('status',1);
  2723. if(!$res2){
  2724. \exception('操作失败');
  2725. }
  2726. }else{
  2727. // 获取下一节点
  2728. $next = [];
  2729. foreach ($apply as $k=>$v){
  2730. if($v['id'] == $info['cur_node']){
  2731. $next = $apply[$k+1];
  2732. break;
  2733. }
  2734. }
  2735. $res2 = Db::name('salary_record')->where('id',$id)->setField('cur_node',$next['id']);
  2736. if(!$res2){
  2737. \exception('操作失败');
  2738. }
  2739. // 生成审核记录
  2740. $uids = explode(',',$next['userids']);
  2741. foreach ($uids as $k=>$v){
  2742. $sd = [
  2743. 'salary_record_id' => $id,
  2744. 'user_id' => $v,
  2745. 'status' => 0,
  2746. 'create_time' => date('Y-m-d H:i:s'),
  2747. 'salary_apply_id' => $next['id']
  2748. ];
  2749. $ret = Db::name('salary_apply_record')->insertGetId($sd);
  2750. if(!$ret){
  2751. \exception('操作失败');
  2752. }
  2753. model('Message')->add(3,$id,12,$v,$orgId,'工资流程待您的审核确认');
  2754. }
  2755. }
  2756. }
  2757. Db::commit();
  2758. }catch (Exception $e){
  2759. Db::rollback();
  2760. return false;
  2761. }
  2762. return true;
  2763. }
  2764. public function confirm($id,$userId){
  2765. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  2766. if(!$info){
  2767. HelpHander::error('记录不存在');
  2768. }
  2769. if($info['status'] != 1){
  2770. HelpHander::error('无权限操作');
  2771. }
  2772. $res = Db::name('salary_record')->where('id',$id)->update([
  2773. 'cw_time' => date('Y-m-d H:i:s'),
  2774. 'cw_user_id' => $userId,
  2775. 'status' => 2
  2776. ]);
  2777. if(!$res){
  2778. HelpHander::error('操作失败');
  2779. }
  2780. return true;
  2781. }
  2782. // 工资统计
  2783. public function financeSalaryStructure($type,$cate){
  2784. $map[] = ['b.type','=',$cate];
  2785. $map[] = ['b.status','=',2];
  2786. $map[] = ['b.del','=',0];
  2787. if($type == 1){
  2788. $map[] = ['b.month','=',date('Y-m')];
  2789. }else {// 当年
  2790. $year = date('Y');
  2791. $startMonth = $year . '-01';
  2792. $endMonth = ($year + 1) . '-01';
  2793. $map[] = ['b.month','>=',$startMonth];
  2794. $map[] = ['b.month','<',$endMonth];
  2795. }
  2796. if($cate == 5){ // 中心养老
  2797. $total = Db::name('user_retired_salary')
  2798. ->alias('a')
  2799. ->join('salary_record b','b.id = a.salary_record_id')
  2800. ->where($map)
  2801. ->sum('salary');
  2802. }else{// 其他
  2803. $total = Db::name('salary_record_log')
  2804. ->alias('a')
  2805. ->join('salary_record b','b.id = a.salary_record_id')
  2806. ->where($map)
  2807. ->sum('salary');
  2808. }
  2809. return $total;
  2810. }
  2811. public function taxlist($page,$size,$month,$name,$type){
  2812. if($month != ''){
  2813. $map[] = ['sr.month','=',$month];
  2814. }
  2815. if($name != ''){
  2816. $map[] = ['ui.name','=',$name];
  2817. }
  2818. if(in_array($type,[1,2])){
  2819. $map[] = ['sr.type','=',$type];
  2820. }
  2821. $map[] = ['sr.del','=',0];
  2822. $map = empty($map)?true:$map;
  2823. $lists = Db::name('salary_record_special')
  2824. ->alias('srs')
  2825. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2826. ->join('user_info ui','ui.user_id = srs.user_id')
  2827. ->where($map)
  2828. ->page($page,$size)
  2829. ->order('sr.id desc,ui.sorts3 asc')
  2830. ->field('srs.*,ui.name,sr.month')
  2831. ->select();
  2832. foreach ($lists as $k=>$v){
  2833. $ljkc = $v['ljsr'] - $v['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
  2834. $lists[$k]['ljkc'] = round($ljkc,2);
  2835. $lists[$k]['ljyykyjsr'] = 0;
  2836. $lists[$k]['jmsr'] = 0;
  2837. $ljykjse = $v['ljykjse'] + $v['tax'];
  2838. $lists[$k]['ljykjse'] = round($ljykjse,2);
  2839. if($ljkc > 0){
  2840. $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
  2841. $lists[$k]['ljyykyjsr'] = round($tax,2);
  2842. $jmse = round($tax*$v['jmsebl'],2); // 减免税额
  2843. $lists[$k]['jmsr'] = $jmse;
  2844. $tax = round($tax - $jmse - $ljykjse,2);
  2845. $tax = $tax>0?$tax:0;
  2846. }else{
  2847. $tax = 0;
  2848. }
  2849. $lists[$k]['nextTax'] = $tax;
  2850. }
  2851. $total = Db::name('salary_record_special')
  2852. ->alias('srs')
  2853. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2854. ->join('user_info ui','ui.user_id = srs.user_id')
  2855. ->where($map)->count();
  2856. $data = [
  2857. 'total' => $total,
  2858. 'list' => $lists?$lists:[]
  2859. ];
  2860. return $data;
  2861. }
  2862. // 个税累计列表
  2863. public function ntaxlist($month,$name,$type){
  2864. if($month){
  2865. $map[] = ['sr.month','=',$month];
  2866. }else{
  2867. return [];
  2868. }
  2869. if($name != ''){
  2870. $map[] = ['ui.name','=',$name];
  2871. }
  2872. if(in_array($type,[1,2])){
  2873. $map[] = ['sr.type','=',$type];
  2874. }
  2875. $map[] = ['sr.del','=',0];
  2876. $map = empty($map)?true:$map;
  2877. $lists = Db::name('salary_record_special')
  2878. ->alias('srs')
  2879. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2880. ->join('user_info ui','ui.user_id = srs.user_id')
  2881. ->where($map)
  2882. ->group('sr.month,srs.user_id')
  2883. ->field('srs.*,ui.name,sr.month')
  2884. ->order('sr.id desc,ui.sorts3 asc')
  2885. ->select();
  2886. foreach ($lists as $k=>$v){
  2887. $children = Db::name('salary_record_special')
  2888. ->alias('srs')
  2889. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2890. ->join('user_info ui','ui.user_id = srs.user_id')
  2891. ->where('sr.month',$v['month'])
  2892. ->where('srs.user_id',$v['user_id'])
  2893. ->where('sr.del',0)
  2894. ->field('srs.*,ui.name,sr.month')
  2895. ->order('srs.id desc')
  2896. ->select();
  2897. $nsalary = 0;
  2898. $nmssr = 0;
  2899. $nsr = 0;
  2900. foreach ($children as $kk=>$vv){
  2901. $ljkc = $vv['ljsr'] - $vv['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
  2902. $children[$kk]['ljkc'] = round($ljkc,2);
  2903. $children[$kk]['ljyykyjsr'] = 0;
  2904. $children[$kk]['jmsr'] = 0;
  2905. $ljykjse = $vv['ljykjse'] + $vv['tax'];
  2906. $children[$kk]['ljykjse'] = round($ljykjse,2);
  2907. if($ljkc > 0){
  2908. $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
  2909. $children[$kk]['ljyykyjsr'] = round($tax,2);
  2910. $jmse = round($tax*$vv['jmsebl'],2); // 减免税额
  2911. $children[$kk]['jmsr'] = $jmse;
  2912. $tax = round($tax - $jmse - $ljykjse,2);
  2913. $tax = $tax>0?$tax:0;
  2914. }else{
  2915. $tax = 0;
  2916. }
  2917. $children[$kk]['nextTax'] = $tax;
  2918. $children[$kk]['child'] = 1;
  2919. $nsalary += $vv['salary'];
  2920. $nmssr += $vv['mssr'];
  2921. $nsr += $vv['sr'];
  2922. $children[$kk]['nsalary'] = $vv['salary'];
  2923. $children[$kk]['nmssr'] = $vv['mssr'];
  2924. $children[$kk]['nsr'] = $vv['sr'];
  2925. $children[$kk]['unique'] = 'C'.$vv['id'];
  2926. }
  2927. $lists[$k] = $children[0];
  2928. $lists[$k]['child'] = 0;
  2929. $lists[$k]['nsalary'] = round($nsalary,2);
  2930. $lists[$k]['nmssr'] = round($nmssr,2);
  2931. $lists[$k]['nsr'] = round($nsr,2);
  2932. $lists[$k]['unique'] = $children[0]['id'];
  2933. if(count($children) >= 2){
  2934. $lists[$k]['children'] = $children;
  2935. }
  2936. }
  2937. return $lists?$lists:[];
  2938. }
  2939. public function ntaxexport($month,$name,$type){
  2940. if($month != ''){
  2941. $map[] = ['sr.month','=',$month];
  2942. }
  2943. if($name != ''){
  2944. $map[] = ['ui.name','=',$name];
  2945. }
  2946. if(in_array($type,[1,2])){
  2947. $map[] = ['sr.type','=',$type];
  2948. }
  2949. $map[] = ['sr.del','=',0];
  2950. $map = empty($map)?true:$map;
  2951. $lists = Db::name('salary_record_special')
  2952. ->alias('srs')
  2953. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2954. ->join('user_info ui','ui.user_id = srs.user_id')
  2955. ->where($map)
  2956. ->group('sr.month,srs.user_id')
  2957. ->field('srs.*,ui.name,sr.month')
  2958. ->order('sr.id desc,ui.sorts3 asc')
  2959. ->select();
  2960. foreach ($lists as $k=>$v){
  2961. $children = Db::name('salary_record_special')
  2962. ->alias('srs')
  2963. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2964. ->join('user_info ui','ui.user_id = srs.user_id')
  2965. ->where('sr.month',$v['month'])
  2966. ->where('srs.user_id',$v['user_id'])
  2967. ->where('sr.del',0)
  2968. ->field('srs.*,ui.name,sr.month')
  2969. ->order('srs.id desc')
  2970. ->select();
  2971. $nsalary = 0;
  2972. $nmssr = 0;
  2973. $nsr = 0;
  2974. foreach ($children as $kk=>$vv){
  2975. $ljkc = $vv['ljsr'] - $vv['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
  2976. $children[$kk]['ljkc'] = round($ljkc,2);
  2977. $children[$kk]['ljyykyjsr'] = 0;
  2978. $children[$kk]['jmsr'] = 0;
  2979. $ljykjse = $vv['ljykjse'] + $vv['tax'];
  2980. $children[$kk]['ljykjse'] = round($ljykjse,2);
  2981. if($ljkc > 0){
  2982. $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
  2983. $children[$kk]['ljyykyjsr'] = round($tax,2);
  2984. $jmse = round($tax*$vv['jmsebl'],2); // 减免税额
  2985. $children[$kk]['jmsr'] = $jmse;
  2986. $tax = round($tax - $jmse - $ljykjse,2);
  2987. $tax = $tax>0?$tax:0;
  2988. }else{
  2989. $tax = 0;
  2990. }
  2991. $children[$kk]['nextTax'] = $tax;
  2992. $children[$kk]['child'] = 1;
  2993. $nsalary += $vv['salary'];
  2994. $nmssr += $vv['mssr'];
  2995. $nsr += $vv['sr'];
  2996. $children[$kk]['nsalary'] = $vv['salary'];
  2997. $children[$kk]['nmssr'] = $vv['mssr'];
  2998. $children[$kk]['nsr'] = $vv['sr'];
  2999. $children[$kk]['unique'] = 'C'.$vv['id'];
  3000. }
  3001. $lists[$k] = $children[0];
  3002. $lists[$k]['child'] = 0;
  3003. $lists[$k]['nsalary'] = round($nsalary,2);
  3004. $lists[$k]['nmssr'] = round($nmssr,2);
  3005. $lists[$k]['nsr'] = round($nsr,2);
  3006. $lists[$k]['unique'] = $children[0]['id'];
  3007. if(count($children) >= 2){
  3008. $lists[$k]['children'] = $children;
  3009. }
  3010. }
  3011. //转成数字格式
  3012. $extra = ['month','name'];
  3013. foreach ($lists as $k=>$v){
  3014. foreach ($v as $kk=>$vv){
  3015. if(!in_array($kk,$extra)){
  3016. $v[$kk] = (float)$vv;
  3017. }
  3018. }
  3019. $lists[$k] = $v;
  3020. }
  3021. $columns[] = ["title" => "月份","key" => "month"];
  3022. $columns[] = ["title" => "姓名","key" => "name"];
  3023. $columns[] = ["title" => "应发工资","key" => "nsalary"];
  3024. $columns[] = ["title" => "免税收入","key" => "nmssr"];
  3025. $columns[] = ["title" => "应税收入","key" => "nsr"];
  3026. $columns[] = ["title" => "累计应税收入","key" => "ljsr"];
  3027. $columns[] = ["title" => "累计减除费用","key" => "ljjc"];
  3028. $columns[] = ["title" => "累计公积金","key" => "ljgjj"];
  3029. $columns[] = ["title" => "累计养老保险","key" => "ljylobx"];
  3030. $columns[] = ["title" => "累计失业保险","key" => "ljsybx"];
  3031. $columns[] = ["title" => "累计医疗保险","key" => "ljylibx"];
  3032. $columns[] = ["title" => "累计职业年金","key" => "ljnj"];
  3033. $columns[] = ["title" => "累计子女教育","key" => "ljznjy"];
  3034. $columns[] = ["title" => "累计继续教育","key" => "ljjxjy"];
  3035. $columns[] = ["title" => "累计住房贷款利息","key" => "ljzfdklx"];
  3036. $columns[] = ["title" => "累计住房租金","key" => "ljzfzj"];
  3037. $columns[] = ["title" => "累计赡养老人","key" => "ljsylr"];
  3038. $columns[] = ["title" => "商业健康保险","key" => "syjkbx"];
  3039. $columns[] = ["title" => "延税养老保险","key" => "ysylbx"];
  3040. $columns[] = ["title" => "个人补扣/补交社保年金","key" => "grbk"];
  3041. $columns[] = ["title" => "年终奖补交社保年金","key" => "jzjbjsb"];
  3042. $columns[] = ["title" => "工资中补交社保年金","key" => "ljgzds"];
  3043. $columns[] = ["title" => "累计准予扣除的捐赠额","key" => "zykcjze"];
  3044. $columns[] = ["title" => "累计其他扣除","key" => "qtkc"];
  3045. $columns[] = ["title" => "税前扣除项目合计","key" => "ljsqljxmhj"];
  3046. $columns[] = ["title" => "累计预扣预缴应纳税所得额","key" => "ljkc"];
  3047. $columns[] = ["title" => "累计应预扣预缴税额","key" => "ljyykyjsr"];
  3048. $columns[] = ["title" => "减免税额","key" => "jmsr"];
  3049. $columns[] = ["title" => "累计已扣缴税额","key" => "ljykjse"];
  3050. $columns[] = ["title" => "下月应扣个人所得税税额","key" => "nextTax"];
  3051. $data = [
  3052. 'columns' => $columns,
  3053. 'list' => $lists
  3054. ];
  3055. return $data;
  3056. }
  3057. // 根据累计预扣预缴应纳税所得额获取累计应预扣预缴税额
  3058. public function getYkyjTax($ljkc){
  3059. $tax1 = $ljkc*0.03 - 0;
  3060. $tax2 = $ljkc*0.1 - 2520;
  3061. $tax3 = $ljkc*0.2 - 16920;
  3062. $tax4 = $ljkc*0.25 - 31920;
  3063. $tax5 = $ljkc*0.3 - 52920;
  3064. $tax6 = $ljkc*0.35 - 85920;
  3065. $tax7 = $ljkc*0.45 - 181920;
  3066. $tax = round(max([$tax1,$tax2,$tax3,$tax4,$tax5,$tax6,$tax7]),2); // 累计应预扣预缴税额
  3067. return $tax;
  3068. }
  3069. public function taxEdit(){
  3070. $id = input('id/d',0);
  3071. $info = Db::name('salary_record_special')->where('id',$id)->find();
  3072. if(!$info){
  3073. HelpHander::error('记录不存在');
  3074. }
  3075. $oldljsr = $info['ljsr'] - $info['sr'];
  3076. $data = [
  3077. 'id' => input('id/d',0),
  3078. 'salary' => input('salary/f',0),
  3079. 'mssr' => input('mssr/f',0),
  3080. // 'sr' => input('sr/f',0),
  3081. // 'ljsr' => input('ljsr/f',0),
  3082. 'ljjc' => input('ljjc/f',0),
  3083. 'ljgjj' => input('ljgjj/f',0),
  3084. 'ljylobx' => input('ljylobx/f',0),
  3085. 'ljylibx' => input('ljylibx/f',0),
  3086. 'ljsybx' => input('ljsybx/f',0),
  3087. 'ljnj' => input('ljnj/f',0),
  3088. 'ljznjy' => input('ljznjy/f',0),
  3089. 'ljjxjy' => input('ljjxjy/f',0),
  3090. 'ljzfdklx' => input('ljzfdklx/f',0),
  3091. 'ljzfzj' => input('ljzfzj/f',0),
  3092. 'ljsylr' => input('ljsylr/f',0),
  3093. 'ljyyezh' => input('ljyyezh/f',0),
  3094. 'syjkbx' => input('syjkbx/f',0),
  3095. 'ysylbx' => input('ysylbx/f',0),
  3096. 'grbk' => input('grbk/f',0),
  3097. 'jzjbjsb' => input('jzjbjsb/f',0),
  3098. 'ljgzds' => input('ljgzds/f',0),
  3099. 'qtkc' => input('qtkc/f',0),
  3100. // 'ljsqljxmhj' => input('ljsqljxmhj/f',0),
  3101. 'ljykjse' => input('ljykjse/f',0),
  3102. 'tax' => input('tax/f',0),
  3103. 'zykcjze' => input('zykcjze/f',0),
  3104. 'jmsebl' => input('jmsebl/f',0),
  3105. 'ljgrylj' => input('ljgrylj/f',0),
  3106. ];
  3107. $data['sr'] = round($data['salary'] - $data['mssr'],2);
  3108. $data['ljsr'] = round($oldljsr + $data['sr'],2);
  3109. $data['ljsqljxmhj'] = $data['ljjc'] + $data['ljgjj'] + $data['ljyyezh'] + $data['ljgrylj'] + $data['ljylobx'] + $data['ljsybx'] + $data['ljylibx'] + $data['ljnj'] + $data['ljznjy']
  3110. + $data['ljjxjy'] + $data['ljzfdklx'] + $data['ljzfzj'] + $data['ljsylr'] + $data['ljgzds'] + $data['syjkbx'] + $data['ysylbx']
  3111. + $data['grbk'] + $data['jzjbjsb'] + $data['zykcjze'] + $data['qtkc'];
  3112. $data['ljsqljxmhj'] = round($data['ljsqljxmhj'],2);
  3113. $ret = Db::name('salary_record_special')->where('id',$data['id'])->update($data);
  3114. if(!$ret){
  3115. HelpHander::error('操作失败');
  3116. }
  3117. $userId = input('userId/d',0);
  3118. model('ActionLog')->add(19,'修改个税累计',$userId,json_encode($data));
  3119. return true;
  3120. }
  3121. // 社保公积金
  3122. public function sblist($month,$name,$type){
  3123. if($month){
  3124. $map[] = ['sr.month','=',$month];
  3125. }else{
  3126. return [];
  3127. }
  3128. if($name != ''){
  3129. $map[] = ['ui.name','=',$name];
  3130. }
  3131. if(in_array($type,[1,2])){
  3132. $map[] = ['sr.type','=',$type];
  3133. }
  3134. $map[] = ['sr.del','=',0];
  3135. $map = empty($map)?true:$map;
  3136. $lists = Db::name('salary_record_sb')
  3137. ->alias('srs')
  3138. ->join('salary_record sr','sr.id = srs.salary_record_id')
  3139. ->join('user_info ui','ui.user_id = srs.user_id')
  3140. ->where($map)
  3141. // ->page($page,$size)
  3142. ->order('sr.id desc,ui.sorts3 asc')
  3143. ->field('ui.name,sr.month,srs.id,srs.user_id,sum(srs.qynj) as qynj,sum(srs.gjj) as gjj,
  3144. sum(srs.sb) as sb,sum(srs.syyl) as syyl,sum(srs.gssy) as gssy,sum(srs.ylnj) as ylnj,
  3145. sum(srs.gjjp) as gjjp,sum(srs.gjjc) as gjjc,sum(srs.ylap) as ylap,sum(srs.ylac) as ylac,
  3146. sum(srs.syep) as syep,sum(srs.syec) as syec,sum(srs.ylip) as ylip,sum(srs.ylic) as ylic,
  3147. sum(srs.gsc) as gsc,sum(srs.qynjp) as qynjp,sum(srs.qynjc) as qynjc')
  3148. ->group('sr.month,srs.user_id')
  3149. ->select();
  3150. foreach ($lists as $k=>$v){
  3151. $lists[$k]['sbtotalp'] = round($v['ylap'] + $v['syep'] + $v['ylip'],2);
  3152. $lists[$k]['sbtotalc'] = round($v['ylac'] + $v['syec'] + $v['ylic'] + $v['gsc'],2);
  3153. $lists[$k]['sbtotal'] = round($lists[$k]['sbtotalp'] + $lists[$k]['sbtotalc'],2);
  3154. $lists[$k]['gjjtotal'] = round($lists[$k]['gjjp'] + $lists[$k]['gjjc'],2);
  3155. $lists[$k]['qynjtotal'] = round($lists[$k]['qynjp'] + $lists[$k]['qynjc'],2);
  3156. }
  3157. // $total = Db::name('salary_record_sb')
  3158. // ->alias('srs')
  3159. // ->join('salary_record sr','sr.id = srs.salary_record_id')
  3160. // ->join('user_info ui','ui.user_id = srs.user_id')
  3161. // ->where($map)
  3162. // ->group('sr.month,srs.user_id')
  3163. // ->count();
  3164. // $data = [
  3165. // 'total' => $total,
  3166. // 'list' => $lists?$lists:[]
  3167. // ];
  3168. return $lists?$lists:[];
  3169. }
  3170. public function sbexport($month,$name,$type){
  3171. if($month != ''){
  3172. $map[] = ['sr.month','=',$month];
  3173. }
  3174. if($name != ''){
  3175. $map[] = ['ui.name','=',$name];
  3176. }
  3177. if(in_array($type,[1,2])){
  3178. $map[] = ['sr.type','=',$type];
  3179. }
  3180. $map[] = ['sr.del','=',0];
  3181. $map = empty($map)?true:$map;
  3182. $lists = Db::name('salary_record_sb')
  3183. ->alias('srs')
  3184. ->join('salary_record sr','sr.id = srs.salary_record_id')
  3185. ->join('user_info ui','ui.user_id = srs.user_id')
  3186. ->where($map)
  3187. ->order('sr.id desc,ui.sorts3 asc')
  3188. ->field('ui.name,sr.month,srs.id,srs.user_id,sum(srs.qynj) as qynj,sum(srs.gjj) as gjj,
  3189. sum(srs.sb) as sb,sum(srs.syyl) as syyl,sum(srs.gssy) as gssy,sum(srs.ylnj) as ylnj,
  3190. sum(srs.gjjp) as gjjp,sum(srs.gjjc) as gjjc,sum(srs.ylap) as ylap,sum(srs.ylac) as ylac,
  3191. sum(srs.syep) as syep,sum(srs.syec) as syec,sum(srs.ylip) as ylip,sum(srs.ylic) as ylic,
  3192. sum(srs.gsc) as gsc,sum(srs.qynjp) as qynjp,sum(srs.qynjc) as qynjc')
  3193. ->group('sr.month,srs.user_id')
  3194. ->distinct(true)
  3195. ->select();
  3196. foreach ($lists as $k=>$v){
  3197. $lists[$k]['sbtotalp'] = round($v['ylap'] + $v['syep'] + $v['ylip'],2);
  3198. $lists[$k]['sbtotalc'] = round($v['ylac'] + $v['syec'] + $v['ylic'] + $v['gsc'],2);
  3199. $lists[$k]['sbtotal'] = round($lists[$k]['sbtotalp'] + $lists[$k]['sbtotalc'],2);
  3200. $lists[$k]['gjjtotal'] = round($lists[$k]['gjjp'] + $lists[$k]['gjjc'],2);
  3201. $lists[$k]['qynjtotal'] = round($lists[$k]['qynjp'] + $lists[$k]['qynjc'],2);
  3202. }
  3203. //转成数字格式
  3204. $extra = ['month','name'];
  3205. foreach ($lists as $k=>$v){
  3206. foreach ($v as $kk=>$vv){
  3207. if(!in_array($kk,$extra)){
  3208. $v[$kk] = (float)$vv;
  3209. }
  3210. }
  3211. $lists[$k] = $v;
  3212. }
  3213. $columns[] = ["title" => "月份","key" => "month"];
  3214. $columns[] = ["title" => "姓名","key" => "name"];
  3215. if($type == 2){
  3216. $columns[] = ["title" => "企业年金基数","key" => "qynj"];
  3217. }
  3218. $columns[] = ["title" => "公积金缴纳基数","key" => "gjj"];
  3219. if($type == 2){
  3220. $columns[] = ["title" => "社保缴纳基数","key" => "sn"];
  3221. }else{
  3222. $columns[] = ["title" => "生育/医疗基数","key" => "syyl"];
  3223. $columns[] = ["title" => "工伤/失业基数","key" => "gssy"];
  3224. $columns[] = ["title" => "养老/年金基数","key" => "ylnj"];
  3225. }
  3226. $columns[] = ["title" => "养老保险(个人)","key" => "ylap"];
  3227. $columns[] = ["title" => "失业保险(个人)","key" => "syep"];
  3228. $columns[] = ["title" => "医疗保险(个人)","key" => "ylip"];
  3229. $columns[] = ["title" => "个人小计(社保)","key" => "sbtotalp"];
  3230. $columns[] = ["title" => "养老保险(单位)","key" => "ylac"];
  3231. $columns[] = ["title" => "失业保险(单位)","key" => "syec"];
  3232. $columns[] = ["title" => "医疗保险(单位)","key" => "ylic"];
  3233. $columns[] = ["title" => "工伤保险(单位)","key" => "gsc"];
  3234. $columns[] = ["title" => "单位小计(社保)","key" => "sbtotalc"];
  3235. $columns[] = ["title" => "社保合计","key" => "sbtotal"];
  3236. $columns[] = ["title" => "公积金(个人)","key" => "gjjp"];
  3237. $columns[] = ["title" => "公积金(单位)","key" => "gjjc"];
  3238. $columns[] = ["title" => "公积金合计","key" => "gjjtotal"];
  3239. if($type == 2){
  3240. $columns[] = ["title" => "企业年金(个人)","key" => "qynjp"];
  3241. $columns[] = ["title" => "企业年金(单位)","key" => "qynjc"];
  3242. $columns[] = ["title" => "企业年金合计","key" => "qynjtotal"];
  3243. }else{
  3244. $columns[] = ["title" => "职业年金(个人)","key" => "qynjp"];
  3245. $columns[] = ["title" => "职业年金(单位)","key" => "qynjc"];
  3246. $columns[] = ["title" => "职业年金合计","key" => "qynjtotal"];
  3247. }
  3248. $data = [
  3249. 'columns' => $columns,
  3250. 'list' => $lists
  3251. ];
  3252. return $data;
  3253. }
  3254. public function unapply($userId){
  3255. $lists = Db::name('salary_apply_record')
  3256. ->alias('sar')
  3257. ->join('salary_record sr','sr.id = sar.salary_record_id and sr.cur_node = sar.salary_apply_id')
  3258. ->where('sar.user_id',$userId)
  3259. ->where('sr.del',0)
  3260. ->where('sr.status',0)
  3261. ->where('sar.del',0)
  3262. ->field('sr.id,sr.month,sr.type,sar.create_time')
  3263. ->select();
  3264. $nlist = [];
  3265. foreach ($lists as $k=>$v){
  3266. $arr = [
  3267. 'id' => $v['id'],
  3268. 'create_time' => $v['create_time']
  3269. ];
  3270. if($v['type'] == 1){
  3271. $arr['title'] = $v['month'].'-中心编制';
  3272. }else if($v['type'] == 2){
  3273. $arr['title'] = $v['month'].'-新华聘用';
  3274. }else if($v['type'] == 3){
  3275. $arr['title'] = $v['month'].'-中心劳务';
  3276. }else if($v['type'] == 4){
  3277. $arr['title'] = $v['month'].'-新华劳务';
  3278. }else if($v['type'] == 5){
  3279. $arr['title'] = $v['month'].'-中心养老';
  3280. }else if($v['type'] == 6){ //6=中心编制 7=新华聘用 8=中心劳务 9=新华劳务
  3281. $arr['title'] = $v['month'].'-中心编制年绩效';
  3282. }else if($v['type'] == 7){
  3283. $arr['title'] = $v['month'].'-新华聘用年绩效';
  3284. }else if($v['type'] == 8){
  3285. $arr['title'] = $v['month'].'-中心劳务年绩效';
  3286. }else if($v['type'] == 9){
  3287. $arr['title'] = $v['month'].'-新华劳务年绩效';
  3288. }else{
  3289. $arr['title'] = $v['month'].'-';
  3290. }
  3291. $nlist[] = $arr;
  3292. }
  3293. return $nlist;
  3294. }
  3295. // 个人明细
  3296. public function userdetail($page,$size,$type,$year){
  3297. // 先取出本年发放工资人数
  3298. if($type != 5){
  3299. $users = Db::name('salary_record_log')
  3300. ->alias('srl')
  3301. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3302. ->join('user_info ui','ui.user_id = srl.user_id')
  3303. ->where('sr.type',$type)
  3304. ->where('sr.month','like',$year.'%')
  3305. ->where('sr.status',2)
  3306. ->where('sr.del',0)
  3307. ->group('srl.user_id')
  3308. ->page($page,$size)
  3309. ->field('ui.name,srl.user_id,sum(gwgz) as gwgz,sum(xjgz) as xjgz,sum(jcjx) as jcjx,sum(yjx) as yjx,
  3310. sum(tax) as tax,sum(salary) as salary,sum(should_total) as should_total,sum(sbgjj) as sbgjj')
  3311. ->distinct(true)
  3312. ->order('ui.sorts3 asc')
  3313. ->select();
  3314. return $users;
  3315. }else{
  3316. }
  3317. }
  3318. public function userYear($type,$year,$name,$month = 0){
  3319. $map[] = ['sr.status','=',2];
  3320. $map[] = ['sr.del','=',0];
  3321. $map2 = $map;
  3322. if($month > 0){
  3323. if($month < 10){
  3324. $map[] = ['sr.month','=',$year.'-0'.$month];
  3325. }else{
  3326. $map[] = ['sr.month','=',$year.'-'.$month];
  3327. }
  3328. }else{
  3329. $map[] = ['sr.month','like',$year.'%'];
  3330. }
  3331. $map[] = ['sr.type','=',$type];
  3332. $map1 = $map;
  3333. if($name){
  3334. $map[] = ['ui.name','like','%'.$name.'%'];
  3335. }
  3336. $ntype = $type + 5; // 年绩效类型
  3337. $map2[] = ['sr.type','=',$ntype];
  3338. if($month > 0){
  3339. if($month < 10){
  3340. $map2[] = ['sr.month','=',$year.'-0'.$month];
  3341. }else{
  3342. $map2[] = ['sr.month','=',$year.'-'.$month];
  3343. }
  3344. }else{
  3345. $map2[] = ['sr.month','like',$year.'%'];
  3346. }
  3347. if(in_array($type,[1,2])){
  3348. $lists = Db::name('salary_record_sb')
  3349. ->alias('srs')
  3350. ->join('user_info ui','ui.user_id = srs.user_id')
  3351. ->join('salary_record sr','sr.id = srs.salary_record_id')
  3352. ->where($map)
  3353. ->field('ui.name,srs.user_id,srs.salary_record_id')
  3354. ->group('srs.user_id')
  3355. ->distinct(true)
  3356. ->order('ui.sorts3 asc')
  3357. ->select();
  3358. $lists = $lists?$lists:[];
  3359. foreach ($lists as $k=>$v){
  3360. $lists[$k]['should_total'] = Db::name('salary_record_log')
  3361. ->alias('srl')
  3362. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3363. ->where($map1)
  3364. ->where('srl.user_id',$v['user_id'])
  3365. ->sum('srl.should_total');
  3366. $lists[$k]['ylac'] = Db::name('salary_record_sb')
  3367. ->alias('srl')
  3368. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3369. ->where($map1)
  3370. ->where('srl.user_id',$v['user_id'])
  3371. ->sum('srl.ylac');
  3372. $lists[$k]['ylic'] = Db::name('salary_record_sb')
  3373. ->alias('srl')
  3374. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3375. ->where($map1)
  3376. ->where('srl.user_id',$v['user_id'])
  3377. ->sum('srl.ylic');
  3378. $lists[$k]['gsc'] = Db::name('salary_record_sb')
  3379. ->alias('srl')
  3380. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3381. ->where($map1)
  3382. ->where('srl.user_id',$v['user_id'])
  3383. ->sum('srl.gsc');
  3384. $lists[$k]['syec'] = Db::name('salary_record_sb')
  3385. ->alias('srl')
  3386. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3387. ->where($map1)
  3388. ->where('srl.user_id',$v['user_id'])
  3389. ->sum('srl.syec');
  3390. $lists[$k]['gjjc'] = Db::name('salary_record_sb')
  3391. ->alias('srl')
  3392. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3393. ->where($map1)
  3394. ->where('srl.user_id',$v['user_id'])
  3395. ->sum('srl.gjjc');
  3396. $lists[$k]['qynjc'] = Db::name('salary_record_sb')
  3397. ->alias('srl')
  3398. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3399. ->where($map1)
  3400. ->where('srl.user_id',$v['user_id'])
  3401. ->sum('srl.qynjc');
  3402. $lists[$k]['njx'] = Db::name('salary_record_log')
  3403. ->alias('srl')
  3404. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3405. ->where($map2)
  3406. ->where('srl.user_id',$v['user_id'])
  3407. ->sum('srl.should_total');
  3408. $lists[$k]['key'] = $k+1;
  3409. $lists[$k]['sb_total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['syec'],2);
  3410. $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);
  3411. }
  3412. }else{
  3413. $lists = Db::name('salary_record_log')
  3414. ->alias('srs')
  3415. ->join('user_info ui','ui.user_id = srs.user_id')
  3416. ->join('salary_record sr','sr.id = srs.salary_record_id')
  3417. ->where($map)
  3418. ->field('ui.name,srs.user_id,srs.salary_record_id')
  3419. ->group('srs.user_id')
  3420. ->distinct(true)
  3421. ->order('ui.sorts3 asc')
  3422. ->select();
  3423. $lists = $lists?$lists:[];;
  3424. foreach ($lists as $k=>$v){
  3425. $lists[$k]['should_total'] = Db::name('salary_record_log')
  3426. ->alias('srl')
  3427. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3428. ->where($map1)
  3429. ->where('srl.user_id',$v['user_id'])
  3430. ->sum('srl.should_total');
  3431. $lists[$k]['njx'] = Db::name('salary_record_log')
  3432. ->alias('srl')
  3433. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3434. ->where($map2)
  3435. ->where('srl.user_id',$v['user_id'])
  3436. ->sum('srl.should_total');
  3437. $lists[$k]['key'] = $k+1;
  3438. $lists[$k]['total'] = round($lists[$k]['should_total'] + $lists[$k]['njx'] + $lists[$k]['should_total'],2);
  3439. }
  3440. }
  3441. return $lists;
  3442. }
  3443. public function yearExport($type,$year,$name,$month = 0){
  3444. $map[] = ['sr.status','=',2];
  3445. $map[] = ['sr.del','=',0];
  3446. $map2 = $map;
  3447. $map[] = ['sr.type','=',$type];
  3448. if($month > 0){
  3449. if($month < 10){
  3450. $map[] = ['sr.month','=',$year.'-0'.$month];
  3451. }else{
  3452. $map[] = ['sr.month','=',$year.'-'.$month];
  3453. }
  3454. }else{
  3455. $map[] = ['sr.month','like',$year.'%'];
  3456. }
  3457. $map1 = $map;
  3458. if($name){
  3459. $map[] = ['ui.name','like','%'.$name.'%'];
  3460. }
  3461. $ntype = $type + 5; // 年绩效类型
  3462. $map2[] = ['sr.type','=',$ntype];
  3463. // $map2[] = ['sr.month','like','%'.($year+1).'%'];
  3464. if($month > 0){
  3465. if($month < 10){
  3466. $map2[] = ['sr.month','=',$year.'-0'.$month];
  3467. }else{
  3468. $map2[] = ['sr.month','=',$year.'-'.$month];
  3469. }
  3470. }else{
  3471. $map2[] = ['sr.month','like',$year.'%'];
  3472. }
  3473. if(in_array($type,[1,2])){
  3474. $lists = Db::name('salary_record_sb')
  3475. ->alias('srs')
  3476. ->join('user_info ui','ui.user_id = srs.user_id')
  3477. ->join('salary_record sr','sr.id = srs.salary_record_id')
  3478. ->where($map)
  3479. ->field('ui.name,srs.user_id,srs.salary_record_id')
  3480. ->group('srs.user_id')
  3481. ->distinct(true)
  3482. ->order('ui.sorts3 asc')
  3483. ->select();
  3484. $lists = $lists?$lists:[];
  3485. $shoud_total = 0;
  3486. $ylac = 0;
  3487. $ylic = 0;
  3488. $gsc = 0;
  3489. $syec = 0;
  3490. $gjjc = 0;
  3491. $qynjc = 0;
  3492. $sb_total = 0;
  3493. $njx = 0;
  3494. $total = 0;
  3495. foreach ($lists as $k=>$v){
  3496. $lists[$k]['should_total'] = Db::name('salary_record_log')
  3497. ->alias('srl')
  3498. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3499. ->where($map1)
  3500. ->where('srl.user_id',$v['user_id'])
  3501. ->sum('srl.should_total');
  3502. $lists[$k]['ylac'] = Db::name('salary_record_sb')
  3503. ->alias('srl')
  3504. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3505. ->where($map1)
  3506. ->where('srl.user_id',$v['user_id'])
  3507. ->sum('srl.ylac');
  3508. //
  3509. $lists[$k]['ylic'] = Db::name('salary_record_sb')
  3510. ->alias('srl')
  3511. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3512. ->where($map1)
  3513. ->where('srl.user_id',$v['user_id'])
  3514. ->sum('srl.ylic');
  3515. $lists[$k]['gsc'] = Db::name('salary_record_sb')
  3516. ->alias('srl')
  3517. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3518. ->where($map1)
  3519. ->where('srl.user_id',$v['user_id'])
  3520. ->sum('srl.gsc');
  3521. $lists[$k]['syec'] = Db::name('salary_record_sb')
  3522. ->alias('srl')
  3523. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3524. ->where($map1)
  3525. ->where('srl.user_id',$v['user_id'])
  3526. ->sum('srl.syec');
  3527. $lists[$k]['gjjc'] = Db::name('salary_record_sb')
  3528. ->alias('srl')
  3529. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3530. ->where('sr.type',$type)
  3531. ->where('sr.month','like',$year.'%')
  3532. ->where('sr.status',2)
  3533. ->where('sr.del',0)
  3534. ->where('srl.user_id',$v['user_id'])
  3535. ->sum('srl.gjjc');
  3536. $lists[$k]['qynjc'] = Db::name('salary_record_sb')
  3537. ->alias('srl')
  3538. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3539. ->where($map1)
  3540. ->where('srl.user_id',$v['user_id'])
  3541. ->sum('srl.qynjc');
  3542. $lists[$k]['njx'] = Db::name('salary_record_log')
  3543. ->alias('srl')
  3544. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3545. ->where($map2)
  3546. ->where('srl.user_id',$v['user_id'])
  3547. ->sum('srl.should_total');
  3548. $lists[$k]['key'] = $k+1;
  3549. $lists[$k]['sb_total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['syec'],2);
  3550. $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);
  3551. $shoud_total += $lists[$k]['should_total'];
  3552. $ylac += $lists[$k]['ylac'];
  3553. $ylic += $lists[$k]['ylic'];
  3554. $gsc += $lists[$k]['gsc'];
  3555. $syec += $lists[$k]['syec'];
  3556. $gjjc += $lists[$k]['gjjc'];
  3557. $qynjc += $lists[$k]['qynjc'];
  3558. $sb_total += $lists[$k]['sb_total'];
  3559. $njx += $lists[$k]['njx'];
  3560. $total += $lists[$k]['total'];
  3561. }
  3562. $lists[] = [
  3563. 'salary_record_id' => 0,
  3564. 'user_id' => 0,
  3565. 'name' => '',
  3566. 'key' => '合计',
  3567. 'should_total' => $shoud_total,
  3568. 'ylac' => $ylac,
  3569. 'ylic' => $ylic,
  3570. 'gsc' => $gsc,
  3571. 'syec' => $syec,
  3572. 'gjjc' => $gjjc,
  3573. 'qynjc' => $qynjc,
  3574. 'sb_total' => $sb_total,
  3575. 'njx' => $njx,
  3576. 'total' => $total,
  3577. ];
  3578. }else{
  3579. $lists = Db::name('salary_record_log')
  3580. ->alias('srs')
  3581. ->join('user_info ui','ui.user_id = srs.user_id')
  3582. ->join('salary_record sr','sr.id = srs.salary_record_id')
  3583. ->where($map)
  3584. ->field('ui.name,srs.user_id,srs.salary_record_id')
  3585. ->group('srs.user_id')
  3586. ->distinct(true)
  3587. ->order('ui.sorts3 asc')
  3588. ->select();
  3589. $lists = $lists?$lists:[];
  3590. $shoud_total = 0;
  3591. $njx = 0;
  3592. $total = 0;
  3593. foreach ($lists as $k=>$v){
  3594. $lists[$k]['should_total'] = Db::name('salary_record_log')
  3595. ->alias('srl')
  3596. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3597. ->where($map1)
  3598. ->where('srl.user_id',$v['user_id'])
  3599. ->sum('srl.should_total');
  3600. $lists[$k]['njx'] = Db::name('salary_record_log')
  3601. ->alias('srl')
  3602. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3603. ->where($map2)
  3604. ->where('srl.user_id',$v['user_id'])
  3605. ->sum('srl.should_total');
  3606. $lists[$k]['key'] = $k+1;
  3607. $lists[$k]['total'] = round($lists[$k]['should_total']+$lists[$k]['njx'] + $lists[$k]['should_total'],2);
  3608. $shoud_total += $lists[$k]['should_total'];
  3609. $total += $lists[$k]['total'];
  3610. $njx += $lists[$k]['njx'];
  3611. }
  3612. $lists[] = [
  3613. 'salary_record_id' => 0,
  3614. 'user_id' => 0,
  3615. 'name' => '',
  3616. 'key' => '合计',
  3617. 'njx' => $njx,
  3618. 'should_total' => $shoud_total,
  3619. 'total' => $total,
  3620. ];
  3621. }
  3622. $columns[] = ["title" => "序号","key" => "key"];
  3623. $columns[] = ["title" => "姓名","key" => "name"];
  3624. $columns[] = ["title" => "应发工资","key" => "shouldTotal"];
  3625. if(in_array($type,[1,2])){
  3626. $columns[] = ["title" => "养老保险(单位)","key" => "ylac"];
  3627. $columns[] = ["title" => "失业保险(单位)","key" => "syec"];
  3628. $columns[] = ["title" => "医疗保险(单位)","key" => "ylic"];
  3629. $columns[] = ["title" => "工伤保险(单位)","key" => "gsc"];
  3630. $columns[] = ["title" => "社保小计","key" => "sbTotal"];
  3631. $columns[] = ["title" => "住房公积金(单位)","key" => "gjjc"];
  3632. if($type == 2){
  3633. $columns[] = ["title" => "企业年金(单位)","key" => "qynjc"];
  3634. }else{
  3635. $columns[] = ["title" => "职业年金(单位)","key" => "qynjc"];
  3636. }
  3637. }
  3638. $columns[] = ["title" => "应发年绩效","key" => "njx"];
  3639. $columns[] = ["title" => "用工成本合计","key" => "total"];
  3640. $data = [
  3641. 'columns' => $columns,
  3642. 'list' => $lists
  3643. ];
  3644. return $data;
  3645. }
  3646. public function workerYear($type,$year,$name){
  3647. // 先取出所有补助项
  3648. // 取出所有人并算出人员的全年合计
  3649. // 取出每个人的记录明细
  3650. $types = Db::name('salary_record')->where('month','like','%'.$year.'%')->where('status',2)->where('del',0)->where('type',$type)->column('types');
  3651. $typeids = [];
  3652. foreach ($types as $k=>$v){
  3653. if($v){
  3654. $typeids = array_merge($typeids,explode(',',$v));
  3655. }
  3656. }
  3657. $typeids = array_unique($typeids);
  3658. if($typeids){
  3659. $tlist = Db::name('user_subsidies')->where('id','in',$typeids)->field('id,title,type,pay_type,cate')->order('sorts asc,id asc')->select();
  3660. }else{
  3661. $tlist = [];
  3662. }
  3663. $map[] = ['sr.status','=',2];
  3664. $map[] = ['sr.del','=',0];
  3665. $map2 = $map;
  3666. $map[] = ['sr.type','=',$type];
  3667. $map[] = ['sr.month','like','%'.$year.'%'];
  3668. $map1 = $map;
  3669. if($name){
  3670. $map[] = ['ui.name','like','%'.$name.'%'];
  3671. }
  3672. $map2[] = ['sr.type','=',$type+5];
  3673. $map2[] = ['sr.month','like','%'.($year+1).'%'];
  3674. $lists = Db::name('salary_record_log')
  3675. ->alias('srs')
  3676. ->join('user_info ui','ui.user_id = srs.user_id')
  3677. ->join('salary_record sr','sr.id = srs.salary_record_id')
  3678. ->where($map)
  3679. ->field('ui.name,srs.user_id,ui.id,max(srs.pwlevel) as pwlevel,srs.psid')
  3680. ->group('srs.user_id')
  3681. ->distinct(true)
  3682. ->order('ui.sorts3 asc,srs.id desc')
  3683. ->select();
  3684. $lists = $lists?$lists:[];
  3685. foreach ($lists as $k=>$v){
  3686. $data = [
  3687. 'gwgz' => 0,
  3688. 'xjgz' => 0,
  3689. 'jcjx' => 0,
  3690. 'yjx' => 0,
  3691. 'tax' => 0,
  3692. 'salary' => 0,
  3693. 'should_total' => 0,
  3694. 'sbgjj' => 0,
  3695. 'dktotal' => 0,
  3696. 'njxyf' => 0,
  3697. 'njxtax' => 0,
  3698. 'njx' => 0,
  3699. 'alltotal' => 0
  3700. ];
  3701. $data['njxyf'] = Db::name('salary_record_log')
  3702. ->alias('srl')
  3703. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3704. ->where($map2)
  3705. ->where('srl.user_id',$v['user_id'])
  3706. ->sum('srl.should_total');
  3707. $data['njx'] = Db::name('salary_record_log')
  3708. ->alias('srl')
  3709. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3710. ->where($map2)
  3711. ->where('srl.user_id',$v['user_id'])
  3712. ->sum('srl.salary');
  3713. $data['njxtax'] = Db::name('salary_record_log')
  3714. ->alias('srl')
  3715. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3716. ->where($map2)
  3717. ->where('srl.user_id',$v['user_id'])
  3718. ->sum('srl.tax');
  3719. // 积累值
  3720. foreach ($tlist as $key=>$val){
  3721. $data['subsidies'.$val['id']] = 0;
  3722. }
  3723. // 获取本年的详细信息
  3724. $depjob = Db::name('user_job')
  3725. ->alias('uj')
  3726. ->join('job j','j.id = uj.job_id')
  3727. ->join('dep d','d.id = j.dep_id')
  3728. ->where('uj.user_id',$v['user_id'])
  3729. ->where('d.enable',1)
  3730. ->field('d.name as dep_name,j.name as job_name')
  3731. ->find();
  3732. $lists[$k]['dep_name'] = $depjob?$depjob['dep_name']:'';
  3733. //职级
  3734. // $lists[$k]['job_name'] = Db::name('post_salary')->where('id',$v['psid'])->value('title');
  3735. $lists[$k]['job_name'] = '';
  3736. $children = Db::name('salary_record_log')
  3737. ->alias('srs')
  3738. ->join('user_info ui','ui.user_id = srs.user_id')
  3739. ->join('salary_record sr','sr.id = srs.salary_record_id')
  3740. ->where($map1)
  3741. ->where('srs.user_id',$v['user_id'])
  3742. ->field('srs.*,ui.name,sr.month')
  3743. ->order('sr.month asc,sr.id asc')
  3744. ->select();
  3745. $children = $children?$children:[];
  3746. foreach ($children as $kk=>$vv){
  3747. $data['gwgz'] += $vv['gwgz'];
  3748. $data['xjgz'] += $vv['xjgz'];
  3749. $data['jcjx'] += $vv['jcjx'];
  3750. $data['yjx'] += $vv['yjx'];
  3751. $data['tax'] += $vv['tax'];
  3752. $data['salary'] += $vv['salary'];
  3753. $data['should_total'] += $vv['should_total'];
  3754. $data['sbgjj'] += $vv['sbgjj'];
  3755. $btjson = json_decode($vv['btjson'],true);
  3756. $btjson = $btjson?$btjson:[];
  3757. foreach ($tlist as $key=>$val){
  3758. $children[$kk]['subsidies'.$val['id']] = 0;
  3759. foreach ($btjson as $bk=>$bv){
  3760. if($val['id'] == $bv['id']){
  3761. $data['subsidies'.$val['id']] += $bv['money'];
  3762. $children[$kk]['subsidies'.$val['id']] = $bv['money'];
  3763. }
  3764. }
  3765. }
  3766. $kcmoney = 0;
  3767. foreach ($btjson as $kkk=>$vvv){
  3768. if($vvv['cate'] == 2){
  3769. $kcmoney += $vvv['money'];
  3770. }
  3771. }
  3772. $children[$kk]['dktotal'] = round($vv['sbgjj'] + $vv['tax'] + $kcmoney,2);
  3773. $children[$kk]['dep_name'] = $lists[$k]['dep_name'];
  3774. $children[$kk]['job_name'] = Db::name('post_salary')->where('id',$vv['psid'])->value('title');
  3775. // $children[$kk]['job_name'] = $lists[$k]['job_name'];
  3776. $lists[$k]['job_name'] = $children[$kk]['job_name'];
  3777. $children[$kk]['njxyf'] = 0;
  3778. $children[$kk]['njxtax'] = 0;
  3779. $children[$kk]['njx'] = 0;
  3780. $children[$kk]['alltotal'] = $children[$kk]['salary'];
  3781. $data['dktotal'] += $children[$kk]['dktotal'];
  3782. }
  3783. $lists[$k]['children'] = $children?$children:[];
  3784. $lists[$k]['month'] = '合计';
  3785. // 处理精度问题
  3786. $data['gwgz'] = round($data['gwgz'],2);
  3787. $data['xjgz'] = round($data['xjgz'],2);
  3788. $data['jcjx'] = round($data['jcjx'],2);
  3789. $data['yjx'] = round($data['yjx'],2);
  3790. $data['tax'] = round($data['tax'],2);
  3791. $data['salary'] = round($data['salary'],2);
  3792. $data['should_total'] = round($data['should_total'],2);
  3793. $data['sbgjj'] = round($data['sbgjj'],2);
  3794. $data['dktotal'] = round($data['dktotal'],2);
  3795. foreach ($tlist as $key=>$val){
  3796. $data['subsidies'.$val['id']] = round($data['subsidies'.$val['id']],2);
  3797. }
  3798. $data['alltotal'] = round($data['salary'] + $data['njx'],2);
  3799. $lists[$k] = array_merge($lists[$k],$data);
  3800. }
  3801. return ['types' => $tlist,'lists' => $lists];
  3802. }
  3803. public function yearWorkerExport($type,$year,$name){
  3804. // 先取出所有补助项
  3805. // 取出所有人并算出人员的全年合计
  3806. // 取出每个人的记录明细
  3807. $types = Db::name('salary_record')->where('month','like','%'.$year.'%')->where('status',2)->where('del',0)->where('type',$type)->column('types');
  3808. $typeids = [];
  3809. foreach ($types as $k=>$v){
  3810. if($v){
  3811. $typeids = array_merge($typeids,explode(',',$v));
  3812. }
  3813. }
  3814. $typeids = array_unique($typeids);
  3815. if($typeids){
  3816. $tlist = Db::name('user_subsidies')->where('id','in',$typeids)->field('id,title,type,pay_type,cate')->order('sorts asc,id asc')->select();
  3817. }else{
  3818. $tlist = [];
  3819. }
  3820. $map[] = ['sr.status','=',2];
  3821. $map[] = ['sr.del','=',0];
  3822. $map2 = $map;
  3823. $map[] = ['sr.type','=',$type];
  3824. $map[] = ['sr.month','like','%'.$year.'%'];
  3825. $map1 = $map;
  3826. if($name){
  3827. $map[] = ['ui.name','like','%'.$name.'%'];
  3828. }
  3829. $map2[] = ['sr.type','=',$type+5];
  3830. $map2[] = ['sr.month','like','%'.($year+1).'%'];
  3831. $lists = Db::name('salary_record_log')
  3832. ->alias('srs')
  3833. ->join('user_info ui','ui.user_id = srs.user_id')
  3834. ->join('salary_record sr','sr.id = srs.salary_record_id')
  3835. ->where($map)
  3836. ->field('ui.name,srs.user_id,ui.id,srs.pwlevel,srs.psid')
  3837. ->group('srs.user_id')
  3838. ->distinct(true)
  3839. ->order('ui.sorts3 asc')
  3840. ->select();
  3841. $lists = $lists?$lists:[];
  3842. foreach ($lists as $k=>$v){
  3843. $data = [
  3844. 'gwgz' => 0,
  3845. 'xjgz' => 0,
  3846. 'jcjx' => 0,
  3847. 'yjx' => 0,
  3848. 'tax' => 0,
  3849. 'salary' => 0,
  3850. 'should_total' => 0,
  3851. 'sbgjj' => 0,
  3852. 'dktotal' => 0,
  3853. 'njxyf' => 0,
  3854. 'njxtax' => 0,
  3855. 'njx' => 0,
  3856. 'alltotal' => 0
  3857. ];
  3858. $data['njxyf'] = Db::name('salary_record_log')
  3859. ->alias('srl')
  3860. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3861. ->where($map2)
  3862. ->where('srl.user_id',$v['user_id'])
  3863. ->sum('srl.should_total');
  3864. $data['njx'] = Db::name('salary_record_log')
  3865. ->alias('srl')
  3866. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3867. ->where($map2)
  3868. ->where('srl.user_id',$v['user_id'])
  3869. ->sum('srl.salary');
  3870. $data['njxtax'] = Db::name('salary_record_log')
  3871. ->alias('srl')
  3872. ->join('salary_record sr','sr.id = srl.salary_record_id')
  3873. ->where($map2)
  3874. ->where('srl.user_id',$v['user_id'])
  3875. ->sum('srl.tax');
  3876. // 积累值
  3877. foreach ($tlist as $key=>$val){
  3878. $data['subsidies'.$val['id']] = 0;
  3879. }
  3880. // 获取本年的详细信息
  3881. $depjob = Db::name('user_job')
  3882. ->alias('uj')
  3883. ->join('job j','j.id = uj.job_id')
  3884. ->join('dep d','d.id = j.dep_id')
  3885. ->where('uj.user_id',$v['user_id'])
  3886. ->where('d.enable',1)
  3887. ->field('d.name as dep_name,j.name as job_name')
  3888. ->find();
  3889. $lists[$k]['dep_name'] = $depjob?$depjob['dep_name']:'';
  3890. //职级
  3891. // $lists[$k]['job_name'] = Db::name('post_salary')->where('id',$v['psid'])->value('title');
  3892. $lists[$k]['job_name'] = '';
  3893. $children = Db::name('salary_record_log')
  3894. ->alias('srs')
  3895. ->join('user_info ui','ui.user_id = srs.user_id')
  3896. ->join('salary_record sr','sr.id = srs.salary_record_id')
  3897. ->where($map1)
  3898. ->where('srs.user_id',$v['user_id'])
  3899. ->field('srs.*,ui.name,sr.month')
  3900. ->order('sr.month asc,sr.id asc')
  3901. ->select();
  3902. $children = $children?$children:[];
  3903. foreach ($children as $kk=>$vv){
  3904. $data['gwgz'] += $vv['gwgz'];
  3905. $data['xjgz'] += $vv['xjgz'];
  3906. $data['jcjx'] += $vv['jcjx'];
  3907. $data['yjx'] += $vv['yjx'];
  3908. $data['tax'] += $vv['tax'];
  3909. $data['salary'] += $vv['salary'];
  3910. $data['should_total'] += $vv['should_total'];
  3911. $data['sbgjj'] += $vv['sbgjj'];
  3912. $btjson = json_decode($vv['btjson'],true);
  3913. $btjson = $btjson?$btjson:[];
  3914. foreach ($tlist as $key=>$val){
  3915. $children[$kk]['subsidies'.$val['id']] = 0;
  3916. foreach ($btjson as $bk=>$bv){
  3917. if($val['id'] == $bv['id']){
  3918. $data['subsidies'.$val['id']] += $bv['money'];
  3919. $children[$kk]['subsidies'.$val['id']] = $bv['money'];
  3920. }
  3921. }
  3922. }
  3923. $kcmoney = 0;
  3924. foreach ($btjson as $kkk=>$vvv){
  3925. if($vvv['cate'] == 2){
  3926. $kcmoney += $vvv['money'];
  3927. }
  3928. }
  3929. $children[$kk]['dktotal'] = round($vv['sbgjj'] + $vv['tax'] + $kcmoney,2);
  3930. $children[$kk]['dep_name'] = $lists[$k]['dep_name'];
  3931. $children[$kk]['job_name'] = Db::name('post_salary')->where('id',$vv['psid'])->value('title');
  3932. // $children[$kk]['job_name'] = $lists[$k]['job_name'];
  3933. $lists[$k]['job_name'] = $children[$kk]['job_name'];
  3934. $children[$kk]['njxyf'] = 0;
  3935. $children[$kk]['njxtax'] = 0;
  3936. $children[$kk]['njx'] = 0;
  3937. $children[$kk]['alltotal'] = $children[$kk]['salary'];
  3938. $data['dktotal'] += $children[$kk]['dktotal'];
  3939. }
  3940. $lists[$k]['children'] = $children?$children:[];
  3941. $lists[$k]['month'] = '合计';
  3942. // 处理精度问题
  3943. $data['gwgz'] = round($data['gwgz'],2);
  3944. $data['xjgz'] = round($data['xjgz'],2);
  3945. $data['jcjx'] = round($data['jcjx'],2);
  3946. $data['yjx'] = round($data['yjx'],2);
  3947. $data['tax'] = round($data['tax'],2);
  3948. $data['salary'] = round($data['salary'],2);
  3949. $data['should_total'] = round($data['should_total'],2);
  3950. $data['sbgjj'] = round($data['sbgjj'],2);
  3951. $data['dktotal'] = round($data['dktotal'],2);
  3952. foreach ($tlist as $key=>$val){
  3953. $data['subsidies'.$val['id']] = round($data['subsidies'.$val['id']],2);
  3954. }
  3955. $data['alltotal'] = round($data['salary'] + $data['njx'],2);
  3956. $lists[$k] = array_merge($lists[$k],$data);
  3957. }
  3958. $newList = [];
  3959. foreach ($lists as $k=>$v){
  3960. $children = $v['children'];
  3961. unset($v['children']);
  3962. $newList[] = $v;
  3963. foreach ($children as $kk=>$vv){
  3964. $newList[] = $vv;
  3965. }
  3966. }
  3967. $columns[] = ["title" => "姓名","key" => "name"];
  3968. $columns[] = ["title" => "月份","key" => "month"];
  3969. $columns[] = ["title" => "部门","key" => "depName"];
  3970. $columns[] = ["title" => "职级","key" => "jobName"];
  3971. $columns[] = ["title" => "薪级","key" => "pwlevel"];
  3972. $columns[] = ["title" => "岗位工资","key" => "gwgz"];
  3973. $columns[] = ["title" => "薪级工资","key" => "xjgz"];
  3974. foreach ($tlist as $k=>$v){
  3975. if($v['cate'] == 1){
  3976. $columns[] = ["title" => $v['title'],"key" => "subsidies".$v['id']];
  3977. }
  3978. }
  3979. $columns[] = ["title" => "基础绩效","key" => "jcjx"];
  3980. $columns[] = ["title" => "月绩效","key" => "yjx"];
  3981. $columns[] = ["title" => "应发工资","key" => "shouldTotal"];
  3982. foreach ($tlist as $k=>$v){
  3983. if($v['cate'] == 2){
  3984. $columns[] = ["title" => $v['title'],"key" => "subsidies".$v['id']];
  3985. }
  3986. }
  3987. $columns[] = ["title" => "社保公积金代扣","key" => "sbgjj"];
  3988. $columns[] = ["title" => "个税","key" => "tax"];
  3989. $columns[] = ["title" => "代扣合计","key" => "dktotal"];
  3990. $columns[] = ["title" => "实发工资","key" => "salary"];
  3991. $columns[] = ["title" => "应发年绩效","key" => "njxyf"];
  3992. $columns[] = ["title" => "年绩效个税","key" => "njxtax"];
  3993. $columns[] = ["title" => "实发年绩效","key" => "njx"];
  3994. $columns[] = ["title" => "总实发","key" => "alltotal"];
  3995. $data = [
  3996. 'columns' => $columns,
  3997. 'list' => $newList
  3998. ];
  3999. return $data;
  4000. }
  4001. public function userWorkerYear($type,$year,$userId){
  4002. // 先取出所有补助项
  4003. // 取出所有人并算出人员的全年合计
  4004. // 取出每个人的记录明细
  4005. if($type != 5){
  4006. $types = Db::name('salary_record')
  4007. ->alias('a')
  4008. ->join('salary_record_log b','b.salary_record_id = a.id')
  4009. ->where('a.month','like',$year.'%')
  4010. ->where('a.status',2)
  4011. ->where('a.del',0)
  4012. ->where('a.type',$type)
  4013. ->where('b.user_id',$userId)
  4014. ->column('a.types');
  4015. }else{
  4016. $types = Db::name('salary_record')
  4017. ->alias('a')
  4018. ->join('user_retired_salary b','b.salary_record_id = a.id')
  4019. ->where('a.month','like','%'.$year.'%')
  4020. ->where('a.status',2)
  4021. ->where('a.del',0)
  4022. ->where('a.type',$type)
  4023. ->where('b.user_id',$userId)
  4024. ->column('a.types');
  4025. }
  4026. $types = $types?$types:[];
  4027. $typeids = [];
  4028. foreach ($types as $k=>$v){
  4029. if($v){
  4030. $typeids = array_merge($typeids,explode(',',$v));
  4031. }
  4032. }
  4033. $typeids = array_unique($typeids);
  4034. if($typeids){
  4035. $tlist = Db::name('user_subsidies')->where('id','in',$typeids)->field('id,title,type,pay_type,cate')->order('sorts asc,id asc')->select();
  4036. }else{
  4037. $tlist = [];
  4038. }
  4039. $map[] = ['sr.type','=',$type];
  4040. $map[] = ['sr.month','like',$year.'%'];
  4041. $map[] = ['sr.status','=',2];
  4042. $map[] = ['sr.del','=',0];
  4043. $map[] = ['srs.user_id','=',$userId];
  4044. $map1 = $map;
  4045. $lists = Db::name('salary_record_log')
  4046. ->alias('srs')
  4047. ->join('user_info ui','ui.user_id = srs.user_id')
  4048. ->join('salary_record sr','sr.id = srs.salary_record_id')
  4049. ->where($map)
  4050. ->field('ui.name,srs.user_id,ui.id,srs.pwlevel,srs.psid,srs.njx')
  4051. ->group('srs.user_id')
  4052. ->distinct(true)
  4053. ->order('ui.sorts3 asc')
  4054. ->select();
  4055. $lists = $lists?$lists:[];
  4056. foreach ($lists as $k=>$v){
  4057. if($type != 5){
  4058. $data = [
  4059. 'njx' => 0,
  4060. 'gwgz' => 0,
  4061. 'xjgz' => 0,
  4062. 'jcjx' => 0,
  4063. 'yjx' => 0,
  4064. 'tax' => 0,
  4065. 'salary' => 0,
  4066. 'should_total' => 0,
  4067. 'sbgjj' => 0,
  4068. 'dktotal' => 0,
  4069. 'level' => '',
  4070. 'factor' => '',
  4071. 'months' => '',
  4072. 'taxrate' => '',
  4073. 'gjjp' => 0,
  4074. 'qynjp' => 0,
  4075. 'ylap' => 0,
  4076. 'syep' => 0,
  4077. 'ylip' => 0,
  4078. ];
  4079. }else{
  4080. $data = [
  4081. 'bz' => 0,
  4082. 'zysdbt' => 0,
  4083. 'fb' => 0,
  4084. 'wybt' => 0,
  4085. 'salary' => 0,
  4086. 'dktotal' => 0,
  4087. ];
  4088. }
  4089. // 积累值
  4090. foreach ($tlist as $key=>$val){
  4091. $data['subsidies'.$val['id']] = 0;
  4092. }
  4093. // 获取本年的详细信息
  4094. $depjob = Db::name('user_job')
  4095. ->alias('uj')
  4096. ->join('job j','j.id = uj.job_id')
  4097. ->join('dep d','d.id = j.dep_id')
  4098. ->where('uj.user_id',$v['user_id'])
  4099. ->where('d.enable',1)
  4100. ->field('d.name as dep_name,j.name as job_name')
  4101. ->find();
  4102. $lists[$k]['dep_name'] = $depjob?$depjob['dep_name']:'';
  4103. //职级
  4104. // $lists[$k]['job_name'] = Db::name('post_salary')->where('id',$v['psid'])->value('title');
  4105. $lists[$k]['job_name'] = '';
  4106. $children = Db::name('salary_record_log')
  4107. ->alias('srs')
  4108. ->join('user_info ui','ui.user_id = srs.user_id')
  4109. ->join('salary_record sr','sr.id = srs.salary_record_id')
  4110. ->where($map1)
  4111. ->where('srs.user_id',$v['user_id'])
  4112. ->field('srs.*,ui.name,sr.month')
  4113. ->order('sr.month asc,sr.id asc')
  4114. ->select();
  4115. $children = $children?$children:[];
  4116. foreach ($children as $kk=>$vv){
  4117. if($type != 5){
  4118. if(in_array($type,[1,2,3,4])){
  4119. $data['gwgz'] += $vv['gwgz'];
  4120. $data['xjgz'] += $vv['xjgz'];
  4121. $data['jcjx'] += $vv['jcjx'];
  4122. $data['yjx'] += $vv['yjx'];
  4123. }else if(in_array($type,[6,7,8,9])){
  4124. $data['njx'] += $vv['njx'];
  4125. }
  4126. $data['tax'] += $vv['tax'];
  4127. $data['should_total'] += $vv['should_total'];
  4128. $data['sbgjj'] += $vv['sbgjj'];
  4129. if($type == 1||$type == 2){
  4130. $sb = Db::name('salary_record_sb')->where('salary_record_id',$vv['salary_record_id'])->where('user_id',$vv['user_id'])->find();
  4131. $children[$kk]['gjjp'] = $sb?$sb['gjjp']:0;
  4132. $children[$kk]['qynjp'] = $sb?$sb['qynjp']:0;
  4133. $children[$kk]['ylap'] = $sb?$sb['ylap']:0;
  4134. $children[$kk]['syep'] = $sb?$sb['syep']:0;
  4135. $children[$kk]['ylip'] = $sb?$sb['ylip']:0;
  4136. $data['gjjp'] += $children[$kk]['gjjp'];
  4137. $data['qynjp'] += $children[$kk]['qynjp'];
  4138. $data['ylap'] += $children[$kk]['ylap'];
  4139. $data['syep'] += $children[$kk]['syep'];
  4140. $data['ylip'] += $children[$kk]['ylip'];
  4141. }
  4142. }else{
  4143. $data['bz'] += $vv['bz'];
  4144. $data['zysdbt'] += $vv['zysdbt'];
  4145. $data['fb'] += $vv['fb'];
  4146. $data['wybt'] += $vv['wybt'];
  4147. }
  4148. $data['salary'] += $vv['salary'];
  4149. $btjson = json_decode($vv['btjson'],true);
  4150. $btjson = $btjson?$btjson:[];
  4151. foreach ($tlist as $key=>$val){
  4152. $children[$kk]['subsidies'.$val['id']] = 0;
  4153. foreach ($btjson as $bk=>$bv){
  4154. if($val['id'] == $bv['id']){
  4155. $data['subsidies'.$val['id']] += $bv['money'];
  4156. $children[$kk]['subsidies'.$val['id']] = $bv['money'];
  4157. }
  4158. }
  4159. }
  4160. $kcmoney = 0;
  4161. foreach ($btjson as $kkk=>$vvv){
  4162. if($vvv['cate'] == 2){
  4163. $kcmoney += $vvv['money'];
  4164. }
  4165. }
  4166. if($type != 5){
  4167. $children[$kk]['dktotal'] = round($vv['sbgjj'] + $vv['tax'] + $kcmoney,2);
  4168. }else{
  4169. $children[$kk]['dktotal'] = round($kcmoney,2);
  4170. }
  4171. $children[$kk]['dep_name'] = $lists[$k]['dep_name'];
  4172. //职级
  4173. $children[$kk]['job_name'] = Db::name('post_salary')->where('id',$vv['psid'])->value('title');
  4174. // $children[$kk]['job_name'] = $lists[$k]['job_name'];
  4175. $lists[$k]['job_name'] = $children[$kk]['job_name'];
  4176. $data['dktotal'] += $children[$kk]['dktotal'];
  4177. }
  4178. $lists[$k]['children'] = $children?$children:[];
  4179. $lists[$k]['month'] = '合计';
  4180. // 处理精度问题
  4181. if($type != 5){
  4182. $data['gwgz'] = round($data['gwgz'],2);
  4183. $data['xjgz'] = round($data['xjgz'],2);
  4184. $data['jcjx'] = round($data['jcjx'],2);
  4185. $data['yjx'] = round($data['yjx'],2);
  4186. $data['tax'] = round($data['tax'],2);
  4187. $data['should_total'] = round($data['should_total'],2);
  4188. $data['sbgjj'] = round($data['sbgjj'],2);
  4189. $data['gjjp'] = round($data['gjjp'],2);
  4190. $data['qynjp'] = round($data['qynjp'],2);
  4191. $data['ylap'] = round($data['ylap'],2);
  4192. $data['syep'] = round($data['syep'],2);
  4193. $data['ylip'] = round($data['ylip'],2);
  4194. }else{
  4195. $data['bz'] = round($data['bz'],2);
  4196. $data['zysdbt'] = round($data['zysdbt'],2);
  4197. $data['fb'] = round($data['fb'],2);
  4198. $data['wybt'] = round($data['wybt'],2);
  4199. }
  4200. $data['salary'] = round($data['salary'],2);
  4201. $data['dktotal'] = round($data['dktotal'],2);
  4202. foreach ($tlist as $key=>$val){
  4203. $data['subsidies'.$val['id']] = round($data['subsidies'.$val['id']],2);
  4204. }
  4205. $lists[$k] = array_merge($lists[$k],$data);
  4206. }
  4207. return ['types' => $tlist,'lists' => $lists];
  4208. }
  4209. // 获取用户发放工资的所有类型
  4210. public function getUserSalayrTypes($userId){
  4211. $types1 = Db::name('salary_record')
  4212. ->alias('a')
  4213. ->join('salary_record_log b','b.salary_record_id = a.id')
  4214. ->where('a.status',2)
  4215. ->where('a.del',0)
  4216. ->where('b.user_id',$userId)
  4217. ->group('a.type')
  4218. ->distinct(true)
  4219. ->column('a.type');
  4220. $types1 = $types1?$types1:[];
  4221. $types2 = Db::name('salary_record')
  4222. ->alias('a')
  4223. ->join('user_retired_salary b','b.salary_record_id = a.id')
  4224. ->where('a.status',2)
  4225. ->where('a.del',0)
  4226. ->where('b.user_id',$userId)
  4227. ->group('a.type')
  4228. ->order('a.type asc')
  4229. ->distinct(true)
  4230. ->column('a.type');
  4231. $types2 = $types2?$types2:[];
  4232. $types = array_merge($types1,$types2);
  4233. $list = [];
  4234. foreach ($types as $v){
  4235. $a['id'] = $v;
  4236. if($v == 1){
  4237. $a['title'] = '中心编制';
  4238. } else if ($v == 2){
  4239. $a['title'] = '新华聘用';
  4240. } else if ($v == 3){
  4241. $a['title'] = '中心劳务';
  4242. } else if ($v == 4){
  4243. $a['title'] = '新华劳务';
  4244. }else if($v == 6){
  4245. $a['title'] = '中心编制年绩效';
  4246. } else if ($v == 7){
  4247. $a['title'] = '新华聘用年绩效';
  4248. } else if ($v == 8){
  4249. $a['title'] = '中心劳务年绩效';
  4250. } else if ($v == 9){
  4251. $a['title'] = '新华劳务年绩效';
  4252. }else{
  4253. $a['title'] = '中心退休';
  4254. }
  4255. $list[] = $a;
  4256. }
  4257. return $list;
  4258. }
  4259. }