SalaryRecord-old.php 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767
  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{
  48. $ret = model('SalaryRecord')->sendSalary($data);
  49. }
  50. if(!$ret){
  51. \exception('操作失败');
  52. }
  53. // 生成审核记录
  54. $uids = explode(',',$curinfo['userids']);
  55. foreach ($uids as $k=>$v){
  56. $sd = [
  57. 'salary_record_id' => $salaryId,
  58. 'user_id' => $v,
  59. 'status' => 0,
  60. 'create_time' => date('Y-m-d H:i:s'),
  61. 'salary_apply_id' => $curinfo['id']
  62. ];
  63. $ret = Db::name('salary_apply_record')->insertGetId($sd);
  64. if(!$ret){
  65. \exception('操作失败');
  66. }
  67. model('Message')->add(3,$salaryId,12,$v,$orgId,'工资流程待您的审核确认');
  68. }
  69. if(!$ret){
  70. \exception('操作失败');
  71. }
  72. Db::commit();
  73. }catch (Exception $e){
  74. Db::rollback();
  75. HelpHander::error($e->getMessage());
  76. }
  77. return true;
  78. }
  79. // 重新发工资
  80. public function resend(){
  81. $salaryRecordId = input('salaryRecordId/d',0);
  82. $id = input('id/d',0);
  83. $users = input('users','','trim');
  84. $userId = input('userId/d',0);
  85. $info = Db::name('salary_record')->where('id',$salaryRecordId)->where('del',0)->find();
  86. if(!$info){
  87. HelpHander::error('记录不存在');
  88. }
  89. if($info['status'] != 0){
  90. HelpHander::error('无权限操作');
  91. }
  92. $sar = Db::name('salary_apply_record')
  93. ->where('salary_apply_id',$info['cur_node'])
  94. ->where('salary_record_id',$salaryRecordId)
  95. ->where('user_id',$userId)
  96. ->where('status',0)
  97. ->where('del',0)
  98. ->find();
  99. if(!$sar){
  100. HelpHander::error('无权限修改');
  101. }
  102. $users = json_decode($users,true);
  103. if(!$users){
  104. HelpHander::error('参数错误');
  105. }
  106. Db::startTrans();
  107. try{
  108. if($info['type'] === 5){
  109. $ret = model('UserRetiredSalary')->sendOneSalary($users[0],$info,$id);
  110. }else{
  111. $ret = model('SalaryRecord')->sendOneSalary($users[0],$info,$id);
  112. }
  113. if(!$ret){
  114. \exception('操作失败');
  115. }
  116. Db::commit();
  117. }catch (Exception $e){
  118. Db::rollback();
  119. HelpHander::error($e->getMessage());
  120. }
  121. return true;
  122. }
  123. private function sendOneSalary($user,$data,$id){
  124. $user['psmoney'] = (float)$user['psmoney'];
  125. $user['pwmoney'] = (float)$user['pwmoney'];
  126. $user['fixedPerformance'] = (float)$user['fixedPerformance'];
  127. $user['psmonth'] = (float)$user['psmonth'];
  128. $types = [];
  129. if($data['types']){
  130. $types = Db::name('user_subsidies')
  131. ->where('id','in',$data['types'])
  132. ->where('del',0)
  133. ->order('sorts asc,id asc')
  134. ->select();
  135. }
  136. if($data['type'] == 3||$data['type'] == 4){ // 劳务费
  137. $sdata = [
  138. 'salary_record_id' => $data['id'],
  139. 'user_id' => $user['userId'],
  140. 'gwgz' => $user['psmoney'],
  141. 'xjgz' => $user['pwmoney'],
  142. 'jcjx' => $user['fixedPerformance'],
  143. 'yjx' => $user['psmonth'],
  144. 'pwlevel' => $user['pwlevel'],
  145. 'psid' => $user['psid'],
  146. 'type' => $user['type'],
  147. 'company_id' => $user['companyId'],
  148. 'child' => 0
  149. ];
  150. $should_total = $user['psmoney'] + $user['pwmoney'] + $user['fixedPerformance'] + $user['psmonth'];
  151. $jbsalary = $should_total;
  152. $btjson = [];
  153. $kcsalary = 0;
  154. foreach ($user as $key=>$val){
  155. if(strpos($key,'subsidies') === 0){
  156. foreach ($types as $kk=>$vv){
  157. if($vv['id'] == str_ireplace('subsidies','',$key)){
  158. $val = (float)$val > 0?(float)$val:0; // 不填或小于0的值默认成0
  159. $btjson[] = [
  160. 'id' => $vv['id'],
  161. 'cate' => $vv['cate'],
  162. 'title' => $vv['title'],
  163. 'money' => $val
  164. ];
  165. if($vv['cate'] == 1){ // 补贴
  166. $should_total += $val;
  167. }else{
  168. $kcsalary += $val;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. $sdata['should_total'] = $should_total;
  175. $sdata['tax'] = $this->getLaborTax($should_total);
  176. $sdata['salary'] = round($should_total - $sdata['tax'] - $sdata['child'] - $kcsalary,2);
  177. if($sdata['salary'] < 0){
  178. return false;
  179. }
  180. $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
  181. $res = Db::name('salary_record_log')->where('id',$id)->update($sdata);
  182. if($res === false){
  183. return false;
  184. }
  185. }else if($data['type'] == 1||$data['type'] == 2){ // 新华聘算社保及累计数
  186. $sdata = [
  187. 'salary_record_id' => $data['id'],
  188. 'user_id' => $user['userId'],
  189. 'gwgz' => $user['psmoney'],
  190. 'xjgz' => $user['pwmoney'],
  191. 'jcjx' => $user['fixedPerformance'],
  192. 'yjx' => $user['psmonth'],
  193. 'pwlevel' => $user['pwlevel'],
  194. 'psid' => $user['psid'],
  195. 'type' => $user['type'],
  196. 'company_id' => $user['companyId'],
  197. 'child' => 0
  198. ];
  199. $should_total = $user['psmoney'] + $user['pwmoney'] + $user['fixedPerformance'] + $user['psmonth'];
  200. $jbsalary = $should_total; // 基本工资,如果不发基本工资,专项附件不累计,如果发基本工资,按正常工资走
  201. $btjson = [];
  202. $kcsalary = 0;
  203. foreach ($user as $key=>$val){
  204. if(strpos($key,'subsidies') === 0){
  205. foreach ($types as $kk=>$vv){
  206. if($vv['id'] == str_ireplace('subsidies','',$key)){
  207. $val = (float)$val > 0?(float)$val:0; // 不填或小于0的值默认成0
  208. $btjson[] = [
  209. 'id' => $vv['id'],
  210. 'cate' => $vv['cate'],
  211. 'title' => $vv['title'],
  212. 'money' => $val
  213. ];
  214. if($vv['cate'] == 1){ // 补贴
  215. $should_total += $val;
  216. }else{
  217. $kcsalary += $val;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. $sbdata = $this->getShebao($user['userId'],$user['companyId'],$user,$jbsalary);
  224. $sbdata['salary_record_id'] = $data['id'];
  225. $sbdata['user_id'] = $user['userId'];
  226. // 社保扣除金额
  227. $sbkc = $sbdata['gjjp'] + $sbdata['qynjp'] +$sbdata['ylip'] +$sbdata['syep'] +$sbdata['ylap'];
  228. $sdata['should_total'] = $should_total;
  229. $sdata['sbgjj'] = $sbkc;
  230. $srid = Db::name('salary_record')
  231. ->alias('sr')
  232. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  233. ->where('sr.month',$data['month'])
  234. ->where('srs.user_id',$user['userId'])
  235. ->where('sr.type',$data['type'])
  236. ->where('sr.del',0)
  237. ->order('sr.id desc')
  238. ->value('sr.id');
  239. $spdata = $this->getSalaryTax($should_total,$sbdata,$user['userId'],$user['companyId'],$data['month'],$data['type'],$jbsalary,$srid); // 计算个税
  240. $spdata['salary_record_id'] = $data['id'];
  241. $spdata['user_id'] = $user['userId'];
  242. $tax = $spdata['tax'];
  243. $m = intval(date('m',strtotime($data['month'].'-01')));
  244. if($m == 1 && $spdata['type'] != 1){ // 一月的第一次发工资
  245. $spdata['tax'] = 0;
  246. }else if($m == 1 && $spdata['type'] == 1){ // 补交不算税
  247. /*$count = Db::name('salary_record')
  248. ->alias('sr')
  249. ->join('salary_record_log srl','srl.salary_record_id = sr.id')
  250. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  251. ->where('sr.month',$data['month'])
  252. ->where('sr.id','<>',$id)
  253. ->where('srs.user_id',$data['userId'])
  254. ->where('sr.type',$data['type'])
  255. ->where('sr.del',0)
  256. ->order('sr.id desc')
  257. ->count();
  258. if($count == 1){
  259. $tax = Db::name('salary_record')
  260. ->alias('sr')
  261. ->join('salary_record_log srl','srl.salary_record_id = sr.id')
  262. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  263. ->where('sr.month',$data['month'])
  264. ->where('srs.user_id',$data['userId'])
  265. ->where('sr.type',$data['type'])
  266. ->where('sr.del',0)
  267. ->order('sr.id desc')
  268. ->value('srl.tax');
  269. $spdata['tax'] = $tax;
  270. }*/
  271. }
  272. unset($sdata['type']);
  273. $sparr[] = $spdata;
  274. if($jbsalary > 0){
  275. $sdata['salary'] = round($should_total - $tax - $sbkc - $kcsalary,2);
  276. }else{
  277. $sdata['salary'] = round($should_total - $tax - $kcsalary,2);
  278. }
  279. if($sdata['salary'] < 0){
  280. return false;
  281. }
  282. $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
  283. $sdata['tax'] = $tax;
  284. $res = Db::name('salary_record_log')->where('id',$id)->update($sdata);
  285. if($res === false){
  286. return false;
  287. }
  288. $res2 = Db::name('salary_record_sb')
  289. ->where('user_id',$user['userId'])
  290. ->where('salary_record_id',$data['id'])
  291. ->update($sbdata);
  292. if($res2 === false){
  293. return false;
  294. }
  295. $res3 = Db::name('salary_record_special')
  296. ->where('user_id',$user['userId'])
  297. ->where('salary_record_id',$data['id'])
  298. ->strict(false)
  299. ->update($spdata);
  300. if($res3 === false){
  301. return false;
  302. }
  303. }
  304. return true;
  305. }
  306. private function sendSalary($data){
  307. $types = [];
  308. if($data['types']){
  309. $types = Db::name('user_subsidies')
  310. ->where('id','in',$data['types'])
  311. ->where('del',0)
  312. ->order('sorts asc,id asc')
  313. ->select();
  314. }
  315. $users = $data['users'];
  316. foreach ($users as $k=>$v){
  317. $users[$k]['psmoney'] = (float)$v['psmoney'];
  318. $users[$k]['pwmoney'] = (float)$v['pwmoney'];
  319. $users[$k]['fixedPerformance'] = (float)$v['fixedPerformance'];
  320. $users[$k]['psmonth'] = (float)$v['psmonth'];
  321. }
  322. if($data['type'] == 3||$data['type'] == 4){ // 劳务费
  323. $arr = [];
  324. foreach ($users as $k=>$v){
  325. $sdata = [
  326. 'salary_record_id' => $data['salary_record_id'],
  327. 'user_id' => $v['userId'],
  328. 'gwgz' => $v['psmoney'],
  329. 'xjgz' => $v['pwmoney'],
  330. 'jcjx' => $v['fixedPerformance'],
  331. 'yjx' => $v['psmonth'],
  332. 'pwlevel' => $v['pwlevel'],
  333. 'psid' => $v['psid'],
  334. 'type' => $v['type'],
  335. 'company_id' => $v['companyId'],
  336. 'child' => $v['child']
  337. ];
  338. $should_total = $v['psmoney'] + $v['pwmoney'] + $v['fixedPerformance'] + $v['psmonth'];
  339. $btjson = [];
  340. $kcsalary = 0;
  341. foreach ($v as $key=>$val){
  342. if(strpos($key,'subsidies') === 0){
  343. foreach ($types as $kk=>$vv){
  344. if($vv['id'] == str_ireplace('subsidies','',$key)){
  345. $val = (float)$val > 0?(float)$val:0; // 不填或小于0的值默认成0
  346. $btjson[] = [
  347. 'id' => $vv['id'],
  348. 'cate' => $vv['cate'],
  349. 'title' => $vv['title'],
  350. 'money' => $val
  351. ];
  352. if($vv['cate'] == 1){ // 补贴
  353. $should_total += $val;
  354. }else{
  355. $kcsalary += $val;
  356. }
  357. }
  358. }
  359. }
  360. }
  361. $sdata['should_total'] = $should_total;
  362. $sdata['tax'] = $this->getLaborTax($should_total);
  363. $sdata['salary'] = round($should_total - $sdata['tax'] - $kcsalary,2);
  364. if($sdata['salary'] < 0){
  365. return false;
  366. }
  367. $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
  368. $arr[] = $sdata;
  369. if(count($arr) == 300){
  370. $res = Db::name('salary_record_log')->insertAll($arr);
  371. if($res != count($arr)){
  372. return false;
  373. }
  374. }
  375. }
  376. if($arr){
  377. $res = Db::name('salary_record_log')->insertAll($arr);
  378. if($res != count($arr)){
  379. return false;
  380. }
  381. }
  382. }else if($data['type'] == 1||$data['type'] == 2){ // 新华聘算社保及累计数
  383. $arr = [];
  384. $sbarr = [];
  385. $sparr = [];
  386. foreach ($users as $k=>$v){
  387. $sdata = [
  388. 'salary_record_id' => $data['salary_record_id'],
  389. 'user_id' => $v['userId'],
  390. 'gwgz' => $v['psmoney'],
  391. 'xjgz' => $v['pwmoney'],
  392. 'jcjx' => $v['fixedPerformance'],
  393. 'yjx' => $v['psmonth'],
  394. 'pwlevel' => $v['pwlevel'],
  395. 'psid' => $v['psid'],
  396. 'type' => $v['pwlevel'],
  397. 'company_id' => $v['companyId'],
  398. 'child' => $v['child']
  399. ];
  400. $should_total = $v['psmoney'] + $v['pwmoney'] + $v['fixedPerformance'] + $v['psmonth'];
  401. $jbsalary = $should_total; // 基本工资,如果不发基本工资,专项附件不累计,如果发基本工资,按正常工资走
  402. $btjson = [];
  403. $kcsalary = 0;
  404. foreach ($v as $key=>$val){
  405. if(strpos($key,'subsidies') === 0){
  406. foreach ($types as $kk=>$vv){
  407. if($vv['id'] == str_ireplace('subsidies','',$key)){
  408. $val = (float)$val > 0?(float)$val:0; // 不填或小于0的值默认成0
  409. $btjson[] = [
  410. 'id' => $vv['id'],
  411. 'cate' => $vv['cate'],
  412. 'title' => $vv['title'],
  413. 'money' => $val
  414. ];
  415. if($vv['cate'] == 1){ // 补贴
  416. $should_total += $val;
  417. }else{
  418. $kcsalary += $val;
  419. }
  420. }
  421. }
  422. }
  423. }
  424. if($should_total <= 0){
  425. return false;
  426. }
  427. $sbdata = $this->getShebao($v['userId'],$v['companyId'],$v,$jbsalary);
  428. $sbdata['salary_record_id'] = $data['salary_record_id'];
  429. $sbdata['user_id'] = $v['userId'];
  430. $sbarr[] = $sbdata;
  431. // 社保扣除金额
  432. $sbkc = $sbdata['gjjp'] + $sbdata['qynjp'] +$sbdata['ylip'] +$sbdata['syep'] +$sbdata['ylap'];
  433. $sdata['should_total'] = $should_total;
  434. $sdata['sbgjj'] = $sbkc;
  435. $spdata = $this->getSalaryTax($should_total,$sbdata,$v['userId'],$v['companyId'],$data['month'],$data['type'],$jbsalary); // 计算个税
  436. $spdata['salary_record_id'] = $data['salary_record_id'];
  437. $spdata['user_id'] = $v['userId'];
  438. $tax = $spdata['tax'];
  439. $m = intval(date('m',strtotime($data['month'].'-01')));
  440. if($m == 1 && $spdata['type'] != 1){ // 一月的第一次发工资
  441. $spdata['tax'] = 0;
  442. }else if($m == 1 && $spdata['type'] == 1){ // 补交不算税
  443. /*$count = Db::name('salary_record')
  444. ->alias('sr')
  445. ->join('salary_record_log srl','srl.salary_record_id = sr.id')
  446. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  447. ->where('sr.month',$data['month'])
  448. ->where('srs.user_id',$v['userId'])
  449. ->where('sr.type',$data['type'])
  450. ->where('sr.del',0)
  451. ->order('sr.id desc')
  452. ->count();
  453. if($count == 1){
  454. $tax = Db::name('salary_record')
  455. ->alias('sr')
  456. ->join('salary_record_log srl','srl.salary_record_id = sr.id')
  457. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  458. ->where('sr.month',$data['month'])
  459. ->where('srs.user_id',$v['userId'])
  460. ->where('sr.type',$data['type'])
  461. ->where('sr.del',0)
  462. ->order('sr.id desc')
  463. ->value('srl.tax');
  464. $spdata['tax'] = $tax;
  465. }*/
  466. }
  467. unset($spdata['type']);
  468. $sparr[] = $spdata;
  469. if($jbsalary > 0){
  470. $sdata['salary'] = round($should_total - $tax - $sbkc - $kcsalary,2);
  471. }else{
  472. $sdata['salary'] = round($should_total - $tax - $kcsalary,2);
  473. }
  474. if($sdata['salary'] < 0){ // 实发金额不能小于0
  475. return false;
  476. }
  477. $sdata['btjson'] = json_encode($btjson,JSON_UNESCAPED_UNICODE);
  478. $sdata['tax'] = $tax;
  479. $arr[] = $sdata;
  480. if(count($arr) == 300){
  481. $res = Db::name('salary_record_log')->insertAll($arr);
  482. if($res != count($arr)){
  483. return false;
  484. }
  485. }
  486. if(count($sbarr) == 300){
  487. $res = Db::name('salary_record_sb')->insertAll($sbarr);
  488. if($res != count($sbarr)){
  489. return false;
  490. }
  491. }
  492. if(count($sparr) == 300){
  493. $res = Db::name('salary_record_special')->insertAll($sparr);
  494. if($res != count($sparr)){
  495. return false;
  496. }
  497. }
  498. }
  499. if($arr){
  500. $res = Db::name('salary_record_log')->insertAll($arr);
  501. if($res != count($arr)){
  502. return false;
  503. }
  504. }
  505. if($sbarr){
  506. $res = Db::name('salary_record_sb')->insertAll($sbarr);
  507. if($res != count($sbarr)){
  508. return false;
  509. }
  510. }
  511. if($sparr){
  512. $res = Db::name('salary_record_special')->insertAll($sparr);
  513. if($res != count($sparr)){
  514. return false;
  515. }
  516. }
  517. }
  518. return true;
  519. }
  520. // 新华聘用税/编制个税
  521. private function getSalaryTax($should_total,$sbdata,$userId,$companyId,$month,$type,$jbsalary,$from=0){// from 0=新增 0<重新计算
  522. $data = [
  523. 'salary' => 0,
  524. 'mssr' => 0,
  525. 'sr' => 0,
  526. 'ljsr' => 0,
  527. 'ljjc' => 0,
  528. 'ljgjj' => 0,
  529. 'ljylobx' => 0,
  530. 'ljsybx' => 0,
  531. 'ljylibx' => 0,
  532. 'ljnj' => 0,
  533. 'ljznjy' => 0,
  534. 'ljjxjy' => 0,
  535. 'ljzfdklx' => 0,
  536. 'ljzfzj' => 0,
  537. 'ljsylr' => 0,
  538. 'syjkbx' => 0,
  539. 'ysylbx' => 0,
  540. 'grbk' => 0,
  541. 'jzjbjsb' => 0,
  542. 'ljgzds' => 0,
  543. 'qtkc' => 0,
  544. 'ljsqljxmhj' => 0,
  545. 'ljykjse' => 0,
  546. 'tax' => 0,
  547. 'zykcjze' => 0,
  548. 'jmsebl' => 0,
  549. 'extratax' => 0,
  550. ];
  551. $lastinfo = [];
  552. if($from > 0){
  553. // 每月发多次工资,先找本月是否已经发放过工资,没有的话获取上个月的记录,在没有的话获取初始值
  554. $lastinfo = Db::name('salary_record')
  555. ->alias('sr')
  556. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  557. ->where('sr.month',$month)
  558. ->where('srs.user_id',$userId)
  559. ->where('sr.id','<>',$from)
  560. ->where('sr.type',$type)
  561. ->where('sr.del',0)
  562. ->field('srs.*')
  563. ->order('sr.id desc')
  564. ->find();
  565. }else{
  566. // 每月发多次工资,先找本月是否已经发放过工资,没有的话获取上个月的记录,在没有的话获取初始值
  567. $lastinfo = Db::name('salary_record')
  568. ->alias('sr')
  569. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  570. ->where('sr.month',$month)
  571. ->where('srs.user_id',$userId)
  572. ->where('sr.type',$type)
  573. ->where('sr.del',0)
  574. ->field('srs.*')
  575. ->order('sr.id desc')
  576. ->find();
  577. }
  578. $stype = 0; // 无累计
  579. if(!$lastinfo){
  580. $lastmonth = date('Y-m',strtotime($month.'-01') - 24*60*60); // 上个月
  581. $lastinfo = Db::name('salary_record')
  582. ->alias('sr')
  583. ->join('salary_record_special srs','srs.salary_record_id = sr.id')
  584. ->where('sr.month',$lastmonth)
  585. ->where('srs.user_id',$userId)
  586. ->where('sr.type',$type)
  587. ->where('sr.del',0)
  588. ->field('srs.*')
  589. ->order('sr.id desc')
  590. ->find();
  591. if($lastinfo){
  592. $stype = 2; // 有上月累计
  593. }
  594. }else{
  595. $stype = 1; // 有本月累计
  596. }
  597. if(!$lastinfo){
  598. $lastinfo = Db::name('user_total')->where('user_id',$userId)->find();
  599. if($lastinfo){
  600. $stype = 3; // 有初始累计
  601. }
  602. }
  603. if($lastinfo){
  604. $data = [
  605. 'salary' => $lastinfo['salary'],
  606. 'mssr' => $lastinfo['mssr'],
  607. 'sr' => $lastinfo['sr'],
  608. 'ljsr' => $lastinfo['ljsr'],
  609. 'ljjc' => $lastinfo['ljjc'],
  610. 'ljgjj' => $lastinfo['ljgjj'],
  611. 'ljylobx' => $lastinfo['ljylobx'],
  612. 'ljsybx' => $lastinfo['ljsybx'],
  613. 'ljylibx' => $lastinfo['ljylibx'],
  614. 'ljnj' => $lastinfo['ljnj'],
  615. 'ljznjy' => $lastinfo['ljznjy'],
  616. 'ljjxjy' => $lastinfo['ljjxjy'],
  617. 'ljzfdklx' => $lastinfo['ljzfdklx'],
  618. 'ljzfzj' => $lastinfo['ljzfzj'],
  619. 'ljsylr' => $lastinfo['ljsylr'],
  620. 'syjkbx' => $lastinfo['syjkbx'],
  621. 'ysylbx' => $lastinfo['ysylbx'],
  622. 'grbk' => $lastinfo['grbk'],
  623. 'jzjbjsb' => $lastinfo['jzjbjsb'],
  624. 'ljgzds' => $lastinfo['ljgzds'],
  625. 'qtkc' => $lastinfo['qtkc'],
  626. 'ljsqljxmhj' => $lastinfo['ljsqljxmhj'],
  627. 'ljykjse' => $lastinfo['ljykjse'],
  628. 'tax' => $lastinfo['tax'],
  629. 'zykcjze' => $lastinfo['zykcjze'],
  630. 'jmsebl' => $lastinfo['jmsebl'],
  631. 'extratax' => isset($lastinfo['extratax'])?$lastinfo['extratax']:0
  632. ];
  633. }
  634. $extratax = $data['extratax']; // 额外税差额
  635. unset($data['extratax']);
  636. // 使用上月数据计算个税
  637. $ljkc = $data['ljsr'] - $data['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
  638. if($ljkc > 0){
  639. $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
  640. $jmse = round($tax*$data['jmsebl'],2); // 减免税额
  641. $ljykjse = $data['ljykjse'] + $data['tax'];
  642. $tax = round($tax - $jmse - $ljykjse,2);
  643. $tax = $tax>0?$tax:0;
  644. $data['ljykjse'] = $ljykjse;
  645. }else{
  646. $tax = 0;
  647. }
  648. $data['tax'] = round($tax + $extratax,2);
  649. $data['salary'] = $should_total;
  650. $mssr = 0;
  651. $m = intval(date('m',strtotime($month.'-01')));
  652. if($jbsalary > 0){ // 发基本工资
  653. // 加累计值
  654. $mssr = 0;
  655. $special = Db::name('user_special')->where('user_id',$userId)->find();
  656. if($special){
  657. $mssr = $special['mssr'];
  658. if($m > 1){
  659. $data['ljjc'] += 5000; // 减除费用每月只能使用1次
  660. $data['ljgjj'] += $sbdata['gjjp'];
  661. $data['ljylobx'] += $sbdata['ylap'];
  662. $data['ljsybx'] += $sbdata['syep'];
  663. $data['ljylibx'] += $sbdata['ylip'];
  664. $data['ljnj'] += $sbdata['qynjp'];
  665. $data['ljznjy'] += $special['znjy'];
  666. $data['ljjxjy'] += $special['jxjy'];
  667. $data['ljzfdklx'] += $special['zfdklx'];
  668. $data['ljzfzj'] += $special['zfzj'];
  669. $data['ljsylr'] += $special['sylr'];
  670. $data['ljgzds'] += $special['gzds'];
  671. $data['syjkbx'] += $special['syjkbx'];
  672. $data['ysylbx'] += $special['ysylbx'];
  673. $data['grbk'] += $special['grbk'];
  674. $data['jzjbjsb'] += $special['nzjds'];
  675. $data['qtkc'] += $special['qtkc'];
  676. $data['zykcjze'] += $special['zykcjze'];
  677. }else{
  678. $data['ljjc'] = 5000;
  679. $data['ljgjj'] = $sbdata['gjjp'];
  680. $data['ljylobx'] = $sbdata['ylap'];
  681. $data['ljsybx'] = $sbdata['syep'];
  682. $data['ljylibx'] = $sbdata['ylip'];
  683. $data['ljnj'] = $sbdata['qynjp'];
  684. $data['ljznjy'] = $special['znjy'];
  685. $data['ljjxjy'] = $special['jxjy'];
  686. $data['ljzfdklx'] = $special['zfdklx'];
  687. $data['ljzfzj'] = $special['zfzj'];
  688. $data['ljsylr'] = $special['sylr'];
  689. $data['ljgzds'] = $special['gzds'];
  690. $data['ljykjse'] = 0;
  691. $data['ljsr'] = 0;
  692. $data['syjkbx'] = $special['syjkbx'];
  693. $data['ysylbx'] = $special['ysylbx'];
  694. $data['grbk'] = $special['grbk'];
  695. $data['jzjbjsb'] = $special['nzjds'];
  696. $data['qtkc'] = $special['qtkc'];
  697. $data['zykcjze'] = $special['zykcjze'];
  698. }
  699. $data['jmsebl'] = $special['jmsebl'];
  700. $data['mssr'] = $special['mssr'];
  701. }
  702. }else{
  703. if($m == 1 && $stype != 1){
  704. $data['ljjc'] = 0;
  705. $data['ljgjj'] = 0;
  706. $data['ljylobx'] = 0;
  707. $data['ljsybx'] = 0;
  708. $data['ljylibx'] = 0;
  709. $data['ljnj'] = 0;
  710. $data['ljznjy'] = 0;
  711. $data['ljjxjy'] = 0;
  712. $data['ljzfdklx'] = 0;
  713. $data['ljzfzj'] = 0;
  714. $data['ljsylr'] = 0;
  715. $data['ljgzds'] = 0;
  716. $data['ljykjse'] = 0;
  717. $data['ljsr'] = 0;
  718. $data['syjkbx'] = 0;
  719. $data['ysylbx'] = 0;
  720. $data['grbk'] = 0;
  721. $data['jzjbjsb'] = 0;
  722. $data['qtkc'] = 0;
  723. $data['zykcjze'] = 0;
  724. $data['ljsqljxmhj'] = 0;
  725. $data['jmsebl'] = 0;
  726. }
  727. $data['tax'] = 0; // 补助不算个税,只累计值
  728. }
  729. $data['mssr'] = $mssr;
  730. $data['sr'] = $should_total - $mssr;
  731. $data['ljsr'] += $data['sr'];
  732. $data['type'] = $stype;
  733. $data['ljsqljxmhj'] = $data['ljjc'] + $data['ljgjj'] + $data['ljylobx'] + $data['ljsybx'] + $data['ljylibx'] + $data['ljnj'] + $data['ljznjy']
  734. + $data['ljjxjy'] + $data['ljzfdklx'] + $data['ljzfzj'] + $data['ljsylr'] + $data['ljgzds'] + $data['syjkbx'] + $data['ysylbx']
  735. + $data['grbk'] + $data['jzjbjsb'] + $data['zykcjze'] + $data['qtkc'];
  736. return $data;
  737. }
  738. // 社保公积金
  739. private function getShebao($userId,$companyId,$info,$jbsalary){
  740. $bls = Db::name('user_bl')->select(); // 社保比例
  741. $base = Db::name('user_base')->where('user_id',$userId)->find();
  742. $data = [
  743. 'qynj' => 0,
  744. 'gjj' => 0,
  745. 'sb' => 0,
  746. 'syyl' => 0,
  747. 'gssy' => 0,
  748. 'ylnj' => 0,
  749. 'ylap' => 0,
  750. 'ylac' => 0,
  751. 'syep' => 0,
  752. 'syec' => 0,
  753. 'ylip' => 0,
  754. 'ylic' => 0,
  755. 'gsc' => 0,
  756. 'qynjp' => 0,
  757. 'qynjc' => 0,
  758. 'gjjp' => 0,
  759. 'gjjc' => 0,
  760. ];
  761. if(!$base||$jbsalary == 0){ //不是发基本工资就不扣缴社保
  762. return $data;
  763. }
  764. $data['qynj'] = $base['qynj'];
  765. $data['gjj'] = $base['gjj'];
  766. $data['sb'] = $base['sb'];
  767. $data['syyl'] = $base['syyl'];
  768. $data['gssy'] = $base['gssy'];
  769. $data['ylnj'] = $base['ylnj'];
  770. // 计算各项扣除
  771. foreach ($bls as $key=>$val){
  772. if($val['company_id'] == $companyId){
  773. switch ($val['name']){
  774. case 'gjj': // 住房公积金,企业和个人只保留整数,四舍五入
  775. $data['gjjp'] = round(calculate_money($base['gjj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']));
  776. $data['gjjc'] = round(calculate_money($base['gjj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']));
  777. break;
  778. case 'qynj':
  779. if ($val['company_id'] == 1) {
  780. $data['qynjp'] = calculate_money($base['ylnj'], $val['p_bl'], $val['p_bl_type'], $val['p_bl_extra'], $val['p_free_bl']);
  781. $data['qynjc'] = calculate_money($base['ylnj'], $val['c_bl'], $val['c_bl_type'], $val['c_bl_extra'], $val['c_free_bl']);
  782. } else {
  783. $data['qynjp'] = calculate_money($base['qynj'], $val['p_bl'], $val['p_bl_type'], $val['p_bl_extra'], $val['p_free_bl']);
  784. $data['qynjc'] = calculate_money($base['qynj'], $val['c_bl'], $val['c_bl_type'], $val['c_bl_extra'], $val['c_free_bl']);
  785. }
  786. break;
  787. case 'yla':
  788. if($info['yla']) {
  789. if($val['company_id'] == 1){
  790. $data['ylap'] = calculate_money($base['ylnj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  791. $data['ylac'] = calculate_money($base['ylnj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  792. } else {
  793. $data['ylap'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  794. $data['ylac'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  795. }
  796. }
  797. break;
  798. case 'sye':
  799. if($val['company_id'] == 1){
  800. $data['syep'] = calculate_money($base['gssy'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  801. $data['syec'] = calculate_money($base['gssy'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  802. } else {
  803. $data['syep'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  804. $data['syec'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  805. }
  806. break;
  807. case 'yli':
  808. if($val['company_id'] == 1){
  809. $data['ylip'] = calculate_money($base['syyl'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  810. $data['ylic'] = calculate_money($base['syyl'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  811. } else {
  812. $data['ylip'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  813. $data['ylic'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  814. }
  815. break;
  816. case 'gs':
  817. if($val['company_id'] == 1){
  818. $data['gsc'] = calculate_money($base['gssy'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  819. } else {
  820. $data['gsc'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  821. }
  822. break;
  823. }
  824. }
  825. }
  826. if(!$info['gjj']){
  827. $data['gjjp'] = 0;
  828. }
  829. if(!$info['qynj']){
  830. $data['qynjp'] = 0;
  831. }
  832. if(!$info['yla']){
  833. $data['ylap'] = 0;
  834. }
  835. if(!$info['sye']){
  836. $data['syep'] = 0;
  837. }
  838. if(!$info['yli']){
  839. $data['ylip'] = 0;
  840. }
  841. return $data;
  842. }
  843. // 获取劳务费个税
  844. public function getLaborTax($salary){
  845. // =ROUND(MAX((MIN((G4)*0.8,(G4)-800)*{0;20;30;40}%-1000*{0;0;2;7}),0),2) //计算公式
  846. if($salary*0.8 <= $salary - 800){
  847. $salary = $salary*0.8;
  848. }else{
  849. $salary = $salary - 800;
  850. }
  851. $arr = [
  852. $salary*0 - 0,
  853. $salary*0.2 - 0,
  854. $salary*0.3 - 2000,
  855. $salary*0.4 - 7000,
  856. 0
  857. ];
  858. $tax = round(max($arr),2);
  859. /*$taxs = Db::name('user_labor_tax')->select();
  860. // 获取减除费用
  861. $jcmoney = 0;
  862. $bl = 0;
  863. $ssmoney = 0;
  864. foreach ($taxs as $k=>$v){
  865. if($v['type'] == 1){
  866. if($v['max'] > 0){
  867. if($salary > $v['min'] && $salary <= $v['max']){
  868. $jcmoney = $v['bl'] == 1?$salary*$v['val']:$v['val'];
  869. }
  870. }else{
  871. if($salary > $v['min']){
  872. $jcmoney = $v['bl'] == 1?$salary*$v['val']:$v['val'];
  873. }
  874. }
  875. } else {
  876. if($v['max'] > 0){
  877. if($salary > $v['min'] && $salary <= $v['max']){
  878. $bl = $v['val'];
  879. $ssmoney = $v['val2'];
  880. }
  881. }else{
  882. if($salary > $v['min']){
  883. $bl = $v['val'];
  884. $ssmoney = $v['val2'];
  885. }
  886. }
  887. }
  888. }
  889. $sfsalary = round(($salary - $jcmoney)*$bl - $ssmoney,2);*/
  890. return $tax;
  891. }
  892. public function preAdd($data,$orgId){
  893. $uids = model('User')->getAdminUsers();
  894. if($uids){
  895. $map[] = ['u.id','not in',$uids];
  896. }
  897. $map[] = ['u.del','=',0];
  898. $map[] = ['r.type','=',3];
  899. $map[] = ['r.org_id','=',$orgId];
  900. $map[] = ['ui.pay_type','=',$data['type']];
  901. $lists = Db::name('user_info')
  902. ->alias('ui')
  903. ->join('user u','ui.user_id = u.id')
  904. ->join('user_roles ur','ur.user_id = u.id')
  905. ->join('roles r','ur.roles_id = r.id')
  906. ->where($map)
  907. ->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')
  908. ->order('ui.sorts3 asc,u.id desc')
  909. ->select();
  910. if(!$lists){
  911. HelpHander::error('该工资类型下无可发放工资的人员');
  912. }
  913. $types = [];
  914. if($data['types']){
  915. $types = Db::name('user_subsidies')
  916. ->where('id','in',$data['types'])
  917. ->where('del',0)
  918. ->order('sorts asc,id asc')
  919. ->select();
  920. }
  921. foreach ($lists as $k=>$v){
  922. $lists[$k]['pwstatus'] = 0; // 薪级变化状态
  923. $lists[$k]['work_age'] = $v['work_date']?calculate_age($v['work_date']):0; // 工龄
  924. if($data['type'] == 5){
  925. $retired = Db::name('user_retired')->where('user_id',$v['user_id'])->find();
  926. $lists[$k]['bz'] = $retired?$retired['bz']:0;
  927. $lists[$k]['zysdbt'] = $retired?$retired['zysdbt']:0;
  928. $lists[$k]['fb'] = $retired?$retired['fb']:0;
  929. $lists[$k]['wybt'] = $retired?$retired['wybt']:0;
  930. }else{
  931. $lists[$k]['psname'] = '';
  932. $lists[$k]['psmonth'] = 0;
  933. $lists[$k]['psmoney'] = 0;
  934. if($lists[$k]['psid'] > 0){
  935. $psinfo = Db::name('post_salary')->where('id',$v['psid'])->find();
  936. if($psinfo){
  937. $lists[$k]['psname'] = $psinfo['job'];
  938. $lists[$k]['psmoney'] = $psinfo['money'];
  939. $lists[$k]['psmonth'] = $psinfo['month_performance'];
  940. if($lists[$k]['work_age'] > 25 && $psinfo['month_performance2'] > 0){
  941. $lists[$k]['psmonth'] = $psinfo['month_performance2'];
  942. }
  943. }
  944. }
  945. $lists[$k]['pwmoney'] = 0;
  946. if($v['type'] > 0 && $v['company_id'] > 0){
  947. $pay_wages = Db::name('pay_wages')
  948. ->where('company_id',$v['company_id'])
  949. ->where('type',$v['type'])
  950. ->value('content');
  951. $pay_wages = $pay_wages?json_decode($pay_wages,true):[];
  952. foreach ($pay_wages as $kk=>$vv){
  953. if($v['pwlevel'] == $vv['level']){
  954. $lists[$k]['pwmoney'] = $vv['money'];
  955. break;
  956. }
  957. }
  958. }
  959. if($data['type'] == 1||$data['type'] == 2){
  960. $lists[$k]['gjj'] = true;
  961. $lists[$k]['yla'] = true;
  962. $lists[$k]['sye'] = true;
  963. $lists[$k]['yli'] = true;
  964. $lists[$k]['qynj'] = true;
  965. }
  966. }
  967. $lastmonth = date('Y-m',strtotime($data['month'].'-01') - 1000);
  968. $btjson = null;
  969. $log = null;
  970. if($data['type'] != 5 && $data['continue'] == 1){ //延续上月,获取上月补贴,只获取基本工资的补贴项
  971. $log = Db::name('salary_record_log')
  972. ->alias('srl')
  973. ->join('salary_record sr','sr.id = srl.salary_record_id')
  974. ->where('sr.month',$data['month'])
  975. ->where('srl.user_id',$v['user_id'])
  976. ->where('sr.status','in',[1,2])
  977. ->where('srl.gwgz','>',0)
  978. ->where('sr.del',0)
  979. ->field('srl.*')
  980. ->order('srl.id desc')
  981. ->find();
  982. if(!$log){
  983. $log = Db::name('salary_record_log')
  984. ->alias('srl')
  985. ->join('salary_record sr','sr.id = srl.salary_record_id')
  986. ->where('sr.month',$lastmonth)
  987. ->where('srl.user_id',$v['user_id'])
  988. ->where('sr.status','in',[1,2])
  989. ->where('srl.gwgz','>',0)
  990. ->where('sr.del',0)
  991. ->field('srl.*')
  992. ->order('srl.id desc')
  993. ->find();
  994. }
  995. if(!empty($log) && ($data['type'] == 3||$data['type'] == 4)){ // 劳务基本工资项也要继承上次的
  996. $lists[$k]['psmoney'] = $log['gwgz'];
  997. $lists[$k]['pwmoney'] = $log['xjgz'];
  998. $lists[$k]['fixed_performance'] = $log['jcjx'];
  999. $lists[$k]['psmonth'] = $log['yjx'];
  1000. }
  1001. if(!empty($log) && ($v['pwlevel'] != $log['pwlevel'])){ // 有薪级变化
  1002. $lists[$k]['pwstatus'] = 1;
  1003. }
  1004. $btjson = !empty($log)?$log['btjson']:null;
  1005. /*$btjson = Db::name('salary_record_log')
  1006. ->alias('srl')
  1007. ->join('salary_record sr','sr.id = srl.salary_record_id')
  1008. ->where('sr.month',$data['month'])
  1009. ->where('srl.user_id',$v['user_id'])
  1010. ->where('sr.status','in',[1,2])
  1011. ->where('srl.gwgz','>',0)
  1012. ->where('sr.del',0)
  1013. ->value('btjson');
  1014. if(!$btjson){
  1015. $btjson = Db::name('salary_record_log')
  1016. ->alias('srl')
  1017. ->join('salary_record sr','sr.id = srl.salary_record_id')
  1018. ->where('sr.month',$lastmonth)
  1019. ->where('srl.user_id',$v['user_id'])
  1020. ->where('sr.status','in',[1,2])
  1021. ->where('srl.gwgz','>',0)
  1022. ->where('sr.del',0)
  1023. ->value('btjson');
  1024. }*/
  1025. }else if($data['type'] == 5 && $data['continue'] == 1){
  1026. $btjson = Db::name('user_retired_salary')
  1027. ->alias('srl')
  1028. ->join('salary_record sr','sr.id = srl.salary_record_id')
  1029. ->where('sr.month',$data['month'])
  1030. ->where('srl.user_id',$v['user_id'])
  1031. ->where('sr.status','in',[1,2])
  1032. ->where('sr.del',0)
  1033. ->order('srl.id desc')
  1034. ->value('btjson');
  1035. if(!$btjson){
  1036. $btjson = Db::name('user_retired_salary')
  1037. ->alias('srl')
  1038. ->join('salary_record sr','sr.id = srl.salary_record_id')
  1039. ->where('sr.month',$lastmonth)
  1040. ->where('srl.user_id',$v['user_id'])
  1041. ->where('sr.status','in',[1,2])
  1042. ->where('sr.del',0)
  1043. ->order('srl.id desc')
  1044. ->value('btjson');
  1045. }
  1046. }
  1047. $btjson = !empty($btjson)?json_decode($btjson,true):[];
  1048. foreach ($types as $kk=>$vv){
  1049. if(empty($btjson)){
  1050. $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
  1051. }else{
  1052. $flag = 1;
  1053. foreach ($btjson as $key=>$val){
  1054. if($val['id'] == $vv['id']){
  1055. $flag = 0;
  1056. $lists[$k]['subsidies'.$vv['id']] = $val['money'];
  1057. }
  1058. }
  1059. if($flag == 1){ // 无相同的分类
  1060. $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
  1061. }
  1062. }
  1063. }
  1064. }
  1065. return ['types' => $types,'list' => $lists];
  1066. }
  1067. // 重新计算
  1068. public function reloadAdd($salaryRecordId,$uid,$orgId){
  1069. $data = Db::name('salary_record')->where('id',$salaryRecordId)->find();
  1070. $map[] = ['u.id','=',$uid];
  1071. $map[] = ['u.del','=',0];
  1072. $map[] = ['r.type','=',3];
  1073. $map[] = ['r.org_id','=',$orgId];
  1074. $map[] = ['ui.pay_type','=',$data['type']];
  1075. $lists = Db::name('user_info')
  1076. ->alias('ui')
  1077. ->join('user u','ui.user_id = u.id')
  1078. ->join('user_roles ur','ur.user_id = u.id')
  1079. ->join('roles r','ur.roles_id = r.id')
  1080. ->where($map)
  1081. ->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')
  1082. ->order('ui.sorts3 asc,u.id desc')
  1083. ->select();
  1084. if(!$lists){
  1085. HelpHander::error('该工资类型下无可发放工资的人员');
  1086. }
  1087. $types = [];
  1088. if($data['types']){
  1089. $types = Db::name('user_subsidies')->where('id','in',$data['types'])->where('del',0)->order('sorts asc')->select();
  1090. }
  1091. foreach ($lists as $k=>$v){
  1092. $lists[$k]['work_age'] = $v['work_date']?calculate_age($v['work_date']):0; // 工龄
  1093. if($data['type'] == 5){
  1094. $retired = Db::name('user_retired')->where('user_id',$v['user_id'])->find();
  1095. $lists[$k]['bz'] = $retired?$retired['bz']:0;
  1096. $lists[$k]['zysdbt'] = $retired?$retired['zysdbt']:0;
  1097. $lists[$k]['fb'] = $retired?$retired['fb']:0;
  1098. $lists[$k]['wybt'] = $retired?$retired['wybt']:0;
  1099. }else{
  1100. $lists[$k]['psname'] = '';
  1101. $lists[$k]['psmonth'] = 0;
  1102. $lists[$k]['psmoney'] = 0;
  1103. if($lists[$k]['psid'] > 0){
  1104. $psinfo = Db::name('post_salary')->where('id',$v['psid'])->find();
  1105. if($psinfo){
  1106. $lists[$k]['psname'] = $psinfo['job'];
  1107. $lists[$k]['psmoney'] = $psinfo['money'];
  1108. $lists[$k]['psmonth'] = $psinfo['month_performance'];
  1109. if($lists[$k]['work_age'] > 25 && $psinfo['month_performance2'] > 0){
  1110. $lists[$k]['psmonth'] = $psinfo['month_performance2'];
  1111. }
  1112. }
  1113. }
  1114. $lists[$k]['pwmoney'] = 0;
  1115. if($v['type'] > 0 && $v['company_id'] > 0){
  1116. $pay_wages = Db::name('pay_wages')
  1117. ->where('company_id',$v['company_id'])
  1118. ->where('type',$v['type'])
  1119. ->value('content');
  1120. $pay_wages = $pay_wages?json_decode($pay_wages,true):[];
  1121. foreach ($pay_wages as $kk=>$vv){
  1122. if($v['pwlevel'] == $vv['level']){
  1123. $lists[$k]['pwmoney'] = $vv['money'];
  1124. break;
  1125. }
  1126. }
  1127. }
  1128. if($data['type'] == 1||$data['type'] == 2){
  1129. $lists[$k]['gjj'] = true;
  1130. $lists[$k]['yla'] = true;
  1131. $lists[$k]['sye'] = true;
  1132. $lists[$k]['yli'] = true;
  1133. $lists[$k]['qynj'] = true;
  1134. }
  1135. }
  1136. $btjson = null;
  1137. $log = null;
  1138. if($data['type'] != 5){
  1139. $log = Db::name('salary_record_log')
  1140. ->where('salary_record_id',$salaryRecordId)
  1141. ->where('user_id',$uid)
  1142. ->find();
  1143. if(!empty($log)){
  1144. $lists[$k]['psmoney'] = $log['gwgz'];
  1145. $lists[$k]['pwmoney'] = $log['xjgz'];
  1146. $lists[$k]['fixed_performance'] = $log['jcjx'];
  1147. $lists[$k]['psmonth'] = $log['yjx'];
  1148. }
  1149. $btjson = !empty($log)?$log['btjson']:null;
  1150. }else{
  1151. $btjson = Db::name('user_retired_salary')
  1152. ->where('salary_record_id',$salaryRecordId)
  1153. ->where('user_id',$uid)
  1154. ->value('btjson');
  1155. }
  1156. $btjson = !empty($btjson)?json_decode($btjson,true):[];
  1157. foreach ($types as $kk=>$vv){
  1158. if(empty($btjson)){
  1159. $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
  1160. }else{
  1161. $flag = 1;
  1162. foreach ($btjson as $key=>$val){
  1163. if($val['id'] == $vv['id']){
  1164. $flag = 0;
  1165. $lists[$k]['subsidies'.$vv['id']] = $val['money'];
  1166. }
  1167. }
  1168. if($flag == 1){ // 无相同的分类
  1169. $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
  1170. }
  1171. }
  1172. }
  1173. // foreach ($types as $kk=>$vv){
  1174. // $lists[$k]['subsidies'.$vv['id']] = $this->getBtByName($vv,$lists[$k]['psid'],$lists[$k]['work_age'],$data['type'],$v['gender']);
  1175. // }
  1176. }
  1177. return ['types' => $types,'list' => $lists];
  1178. }
  1179. private function getBtByName($info,$psid,$age,$payType,$gender){
  1180. $val = 0;
  1181. if($payType == 1){ // 中心在编
  1182. if($info['type'] == 1){
  1183. if($info['gender'] == 0){
  1184. $val = $info['money'];
  1185. }else if($info['gender'] != 0&&$info['gender'] == $gender){
  1186. $val = $info['money'];
  1187. }
  1188. }else if($info['type'] == 3){ // 检测上个月的记录
  1189. $val = 0;
  1190. } else {
  1191. $dd = json_decode($info['values'],true);
  1192. $dd = $dd?$dd:[];
  1193. foreach ($dd as $k=>$v){
  1194. if($v['id'] == $psid){
  1195. if($v['money2']){
  1196. if($age < 15){
  1197. $val = $v['money1'];
  1198. } else if ($age >= 15 && $age < 25){
  1199. $val = $v['money2'];
  1200. }else{
  1201. $val = $v['money3'];
  1202. }
  1203. }else{
  1204. $val = $v['money1'];
  1205. }
  1206. break;
  1207. }
  1208. }
  1209. if($info['gender'] != 0&&$info['gender'] != $gender){
  1210. $val = 0;
  1211. }
  1212. }
  1213. }else{
  1214. if($info['type'] == 1){
  1215. if($info['gender'] == 0){
  1216. $val = $info['money'];
  1217. }else if($info['gender'] != 0&&$info['gender'] == $gender){
  1218. $val = $info['money'];
  1219. }
  1220. }else if($info['type'] == 3){ // 检测上个月的记录
  1221. $val = 0;
  1222. }
  1223. }
  1224. return $val;
  1225. }
  1226. public function info($id){
  1227. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  1228. if(!$info){
  1229. HelpHander::error('数据不存在');
  1230. }
  1231. $types = [];
  1232. if($info['types']){
  1233. $types = Db::name('user_subsidies')->where('id','in',$info['types'])->select();
  1234. }
  1235. $info['types'] = $types;
  1236. $info['sarlist'] = [];
  1237. if($info['status'] == 0){ // 审核中
  1238. $sarlist = Db::name('salary_apply_record')
  1239. ->alias('sar')
  1240. ->join('salary_apply sa','sa.id = sar.salary_apply_id')
  1241. ->where('sar.salary_apply_id',$info['cur_node'])
  1242. ->where('sar.salary_record_id',$info['id'])
  1243. ->where('sar.del',0)
  1244. ->field('sa.cate,sar.*')
  1245. ->order('sar.id asc')
  1246. ->select();
  1247. foreach ($sarlist as $k=>$v){
  1248. $sarlist[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  1249. }
  1250. $info['sarlist'] = $sarlist;
  1251. }
  1252. // 计算实发总金额
  1253. if($info['type'] == 5){
  1254. $totalSalary = Db::name('user_retired_salary')->where('salary_record_id',$id)->sum('salary');
  1255. }else{
  1256. $totalSalary = Db::name('salary_record_log')->where('salary_record_id',$id)->sum('salary');
  1257. }
  1258. $info['total_salary'] = $totalSalary;
  1259. //获取已审批人员
  1260. $alist = Db::name('salary_apply_record')
  1261. ->alias('sar')
  1262. ->join('salary_record sr','sr.id = sar.salary_record_id')
  1263. ->join('user_info ui','ui.user_id = sar.user_id')
  1264. ->where('sar.salary_record_id',$id)
  1265. ->where('sar.status',1)
  1266. ->where('sar.del',0)
  1267. ->field('sar.*,ui.name as user_name')
  1268. ->order('sar.id desc')
  1269. ->select();
  1270. $info['alist'] = $alist?$alist:[];
  1271. return $info;
  1272. }
  1273. public function lists($page,$size,$month,$type){
  1274. if($month != ''){
  1275. $map[] = ['month','=',$month];
  1276. }
  1277. if($type >= 0){
  1278. $map[] = ['type','=',$type];
  1279. }
  1280. $map[] = ['del','=',0];
  1281. $map = empty($map)?true:$map;
  1282. $lists = Db::name('salary_record')
  1283. ->where($map)
  1284. ->page($page,$size)
  1285. ->order('id desc')
  1286. ->select();
  1287. foreach ($lists as $k=>$v){
  1288. // 计算实发总金额
  1289. if($v['type'] == 5){
  1290. $totalSalary = Db::name('user_retired_salary')->where('salary_record_id',$v['id'])->sum('salary');
  1291. }else{
  1292. $totalSalary = Db::name('salary_record_log')->where('salary_record_id',$v['id'])->sum('salary');
  1293. }
  1294. $lists[$k]['total_salary'] = $totalSalary;
  1295. }
  1296. $total = Db::name('salary_record')
  1297. ->where($map)->count();
  1298. $data = [
  1299. 'total' => $total,
  1300. 'list' => $lists?$lists:[]
  1301. ];
  1302. return $data;
  1303. }
  1304. public function payinfo($id){
  1305. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  1306. if(!$info){
  1307. HelpHander::error('数据不存在');
  1308. }
  1309. $info['user_name'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  1310. if($info['status'] == 1||$info['status'] == 2){
  1311. $last_time = Db::name('salary_apply_record')
  1312. ->where('salary_record_id',$info['id'])
  1313. ->where('status',1)
  1314. ->where('del',0)
  1315. ->order('id desc')
  1316. ->value('update_time');
  1317. $info['last_time'] = date('Y年m月d日',strtotime($last_time));
  1318. $rsinfo = Db::name('salary_apply_record')
  1319. ->alias('sar')
  1320. ->join('salary_apply sa','sa.id = sar.salary_apply_id')
  1321. ->join('user_info ui','ui.user_id = sar.user_id')
  1322. ->where('sar.del',0)
  1323. ->where('sa.cate',2)
  1324. ->value('ui.name');
  1325. $info['rsl_user_name'] = $rsinfo?$rsinfo:'';
  1326. $cwinfo = Db::name('salary_apply_record')
  1327. ->alias('sar')
  1328. ->join('salary_apply sa','sa.id = sar.salary_apply_id')
  1329. ->join('user_info ui','ui.user_id = sar.user_id')
  1330. ->where('sar.del',0)
  1331. ->where('sa.cate',3)
  1332. ->value('ui.name');
  1333. $info['kj_user_name'] = $cwinfo?$cwinfo:'';
  1334. $cwlinfo = Db::name('salary_apply_record')
  1335. ->alias('sar')
  1336. ->join('salary_apply sa','sa.id = sar.salary_apply_id')
  1337. ->join('user_info ui','ui.user_id = sar.user_id')
  1338. ->where('sar.del',0)
  1339. ->where('sa.cate',4)
  1340. ->value('ui.name');
  1341. $info['cwl_user_name'] = $cwlinfo?$cwlinfo:'';
  1342. }else{
  1343. $info['last_time'] = '';
  1344. $info['kj_user_name'] = '';
  1345. $info['rsl_user_name'] = '';
  1346. $info['cwl_user_name'] = '';
  1347. }
  1348. if($info['type'] == 5){
  1349. $totalSalary = Db::name('user_retired_salary')->where('salary_record_id',$id)->sum('salary');
  1350. }else{
  1351. $totalSalary = Db::name('salary_record_log')->where('salary_record_id',$id)->sum('salary');
  1352. }
  1353. $info['total_salary'] = $totalSalary;
  1354. return $info;
  1355. }
  1356. public function del($id){
  1357. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  1358. if(!$info){
  1359. HelpHander::error('数据不存在');
  1360. }
  1361. // if($info['status'] == 2){
  1362. // HelpHander::error('该状态不能删除');
  1363. // }
  1364. $ret = Db::name('salary_record')->where('id',$id)->setField('del',1);
  1365. if(!$ret){
  1366. HelpHander::error('操作失败');
  1367. }
  1368. return true;
  1369. }
  1370. public function send($id){
  1371. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  1372. if(!$info){
  1373. HelpHander::error('记录不存在');
  1374. }
  1375. if($info['status'] != 3){
  1376. HelpHander::error('该状态不能重新发放');
  1377. }
  1378. $ret = Db::name('salary_record')->where('id',$id)->setField('status',0);
  1379. if(!$ret){
  1380. HelpHander::error('操作失败');
  1381. }
  1382. return true;
  1383. }
  1384. // 工资列表
  1385. public function listLog($page,$size,$name,$id){
  1386. $info = $this->info($id);
  1387. if($info['type'] == 5){ // 退休
  1388. $data = model('UserRetiredSalary')->lists($page,$size,$name,$info['id']);
  1389. } else {
  1390. if($name != ''){
  1391. $map[] = ['ui.name','like','%'.$name.'%'];
  1392. }
  1393. $map[] = ['ur.salary_record_id','=',$info['id']];
  1394. $lists = Db::name('salary_record_log')
  1395. ->alias('ur')
  1396. ->join('user_info ui','ui.user_id = ur.user_id')
  1397. ->where($map)
  1398. ->field('ur.*,ui.name')
  1399. ->page($page,$size)
  1400. ->order('ui.sorts3 asc,ur.id desc')
  1401. ->select();
  1402. $lists = $lists?$lists:[];
  1403. foreach ($lists as $k=>$v){
  1404. $btjson = $v['btjson']?json_decode($v['btjson'],true):[];
  1405. $kcmoney = 0;
  1406. foreach ($btjson as $kk=>$vv){
  1407. $lists[$k]['subsidies'.$vv['id']] = $vv['money'];
  1408. if($vv['cate'] == 2){ // 扣除项
  1409. $kcmoney += $vv['money'];
  1410. }
  1411. }
  1412. $lists[$k]['dktotal'] = round($v['sbgjj'] + $v['tax'] + $kcmoney,2);
  1413. $depjob = Db::name('user_job')
  1414. ->alias('uj')
  1415. ->join('job j','j.id = uj.job_id')
  1416. ->join('dep d','d.id = j.dep_id')
  1417. ->where('uj.user_id',$v['user_id'])
  1418. ->field('d.name as dep_name,j.name as job_name')
  1419. ->find();
  1420. $lists[$k]['dep_name'] = $depjob?$depjob['dep_name']:'';
  1421. //职级
  1422. $lists[$k]['job_name'] = Db::name('post_salary')->where('id',$v['psid'])->value('title');
  1423. }
  1424. $total = Db::name('salary_record_log')
  1425. ->alias('ur')
  1426. ->join('user_info ui','ui.user_id = ur.user_id')
  1427. ->where($map)->count();
  1428. $data = [
  1429. 'total' => $total,
  1430. 'list' => $lists?$lists:[]
  1431. ];
  1432. }
  1433. $data['info'] = $info;
  1434. return $data;
  1435. }
  1436. // 个税累计列表
  1437. public function listSpecial($page,$size,$name,$id){
  1438. $info = $this->info($id);
  1439. if($info){
  1440. if($name != ''){
  1441. $map[] = ['ui.name','like','%'.$name.'%'];
  1442. }
  1443. $map[] = ['ur.salary_record_id','=',$info['id']];
  1444. $lists = Db::name('salary_record_special')
  1445. ->alias('ur')
  1446. ->join('user_info ui','ui.user_id = ur.user_id')
  1447. ->where($map)
  1448. ->field('ur.*,ui.name,ui.pwlevel')
  1449. ->page($page,$size)
  1450. ->order('ui.sorts3 asc,ur.id desc')
  1451. ->select();
  1452. $lists = $lists?$lists:[];
  1453. foreach ($lists as $k=>$v){
  1454. $ljkc = $v['ljsr'] - $v['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
  1455. $lists[$k]['ljkc'] = round($ljkc,2);
  1456. $lists[$k]['ljyykyjsr'] = 0;
  1457. $lists[$k]['jmsr'] = 0;
  1458. $ljykjse = $v['ljykjse'] + $v['tax'];
  1459. $lists[$k]['ljykjse'] = round($ljykjse,2);
  1460. if($ljkc > 0){
  1461. $tax = $this->getYkyjTax($ljkc); // 累计应预扣预缴税额
  1462. $lists[$k]['ljyykyjsr'] = round($tax,2);
  1463. $jmse = round($tax*$v['jmsebl'],2); // 减免税额
  1464. $lists[$k]['jmsr'] = $jmse;
  1465. $tax = round($tax - $jmse - $ljykjse,2);
  1466. $tax = $tax>0?$tax:0;
  1467. }else{
  1468. $tax = 0;
  1469. }
  1470. $lists[$k]['nextTax'] = $tax;
  1471. }
  1472. $total = Db::name('salary_record_special')
  1473. ->alias('ur')
  1474. ->join('user_info ui','ui.user_id = ur.user_id')
  1475. ->where($map)->count();
  1476. $data = [
  1477. 'total' => $total,
  1478. 'list' => $lists?$lists:[]
  1479. ];
  1480. }
  1481. $data['info'] = $info;
  1482. return $data;
  1483. }
  1484. // 导出
  1485. public function exportListLog($name,$id){
  1486. $info = $this->info($id);
  1487. if($info['type'] == 5){ // 退休
  1488. $data = model('UserRetiredSalary')->lists(1,100000,$name,$info['id']);
  1489. } else {
  1490. if($name != ''){
  1491. $map[] = ['ui.name','like','%'.$name.'%'];
  1492. }
  1493. $map[] = ['ur.salary_record_id','=',$info['id']];
  1494. $lists = Db::name('salary_record_log')
  1495. ->alias('ur')
  1496. ->join('user_info ui','ui.user_id = ur.user_id')
  1497. ->where($map)
  1498. ->field('ur.*,ui.name,ui.pwlevel')
  1499. ->order('ui.sorts3 asc,ur.id desc')
  1500. ->select();
  1501. $lists = $lists?$lists:[];
  1502. foreach ($lists as $k=>$v){
  1503. $btjson = $v['btjson']?json_decode($v['btjson'],true):[];
  1504. $kcmoney = 0;
  1505. foreach ($btjson as $kk=>$vv){
  1506. $lists[$k]['subsidies'.$vv['id']] = $vv['money'];
  1507. if($vv['cate'] == 2){
  1508. $kcmoney += $vv['money'];
  1509. }
  1510. }
  1511. $lists[$k]['dktotal'] = $v['sbgjj'] + $v['tax'] + $kcmoney;
  1512. $depjob = Db::name('user_job')
  1513. ->alias('uj')
  1514. ->join('job j','j.id = uj.job_id')
  1515. ->join('dep d','d.id = j.dep_id')
  1516. ->where('uj.user_id',$v['user_id'])
  1517. ->field('d.name as dep_name,j.name as job_name')
  1518. ->find();
  1519. $lists[$k]['dep_name'] = $depjob?$depjob['dep_name']:'';
  1520. //职级
  1521. $lists[$k]['job_name'] = Db::name('post_salary')->where('id',$v['psid'])->value('title');
  1522. }
  1523. if($lists){
  1524. $darr = $lists[0];
  1525. foreach ($darr as $k=>$v){
  1526. $darr[$k] = '';
  1527. if($k == 'name'){
  1528. $darr[$k] = '合计';
  1529. }
  1530. if($info['type'] == 5){
  1531. $arrs = ['bz','zysdbt','fb','wybt'];
  1532. if(in_array($k,$arrs)){
  1533. $val = 0;
  1534. foreach ($lists as $kk=>$vv){
  1535. $val += $vv[$k];
  1536. }
  1537. $darr[$k] = round($val,2);
  1538. }
  1539. }else{
  1540. $arrs = ['gwgz','xjgz','jcjx','yjx','should_total','child','dktotal'];
  1541. if(in_array($k,$arrs)){
  1542. $val = 0;
  1543. foreach ($lists as $kk=>$vv){
  1544. $val += $vv[$k];
  1545. }
  1546. $darr[$k] = round($val,2);
  1547. }
  1548. }
  1549. $arrs = ['salary'];
  1550. foreach ($info['types'] as $kkk=>$vvv){
  1551. $arrs[] = "subsidies".$vvv['id'];
  1552. }
  1553. if(in_array($k,$arrs)){
  1554. $val = 0;
  1555. foreach ($lists as $kk=>$vv){
  1556. $val += $vv[$k];
  1557. }
  1558. $darr[$k] = round($val,2);
  1559. }
  1560. if($info['type'] == 1||$info['type'] == 2){
  1561. $arrs = ['sbgjj','tax'];
  1562. if(in_array($k,$arrs)){
  1563. $val = 0;
  1564. foreach ($lists as $kk=>$vv){
  1565. $val += $vv[$k];
  1566. }
  1567. $darr[$k] = round($val,2);
  1568. }
  1569. }
  1570. if($info['type'] == 3||$info['type'] == 4){
  1571. $arrs = ['tax'];
  1572. if(in_array($k,$arrs)){
  1573. $val = 0;
  1574. foreach ($lists as $kk=>$vv){
  1575. $val += $vv[$k];
  1576. }
  1577. $darr[$k] = round($val,2);
  1578. }
  1579. }
  1580. }
  1581. $lists[] = $darr;
  1582. }
  1583. //转成数字格式
  1584. $extra = ['dep_name','name','job_name'];
  1585. foreach ($lists as $k=>$v){
  1586. foreach ($v as $kk=>$vv){
  1587. if(!in_array($kk,$extra)){
  1588. $v[$kk] = (float)$vv;
  1589. }
  1590. }
  1591. $lists[$k] = $v;
  1592. }
  1593. $data = [
  1594. 'total' => count($lists),
  1595. 'list' => $lists?$lists:[]
  1596. ];
  1597. }
  1598. $columns = [
  1599. ["title" => "姓名","key" => "name"]
  1600. ];
  1601. if($info['type'] == 5){
  1602. $columns[] = ["title" => "补助","key" => "bz"];
  1603. $columns[] = ["title" => "中央适当补贴","key" => "zysdbt"];
  1604. $columns[] = ["title" => "房补","key" => "fb"];
  1605. $columns[] = ["title" => "物业补贴","key" => "wybt"];
  1606. }else{
  1607. $columns[] = ["title" => "部门","key" => "depName"];
  1608. $columns[] = ["title" => "职级","key" => "jobName"];
  1609. $columns[] = ["title" => "薪级","key" => "pwlevel"];
  1610. $columns[] = ["title" => "岗位工资","key" => "gwgz"];
  1611. $columns[] = ["title" => "薪级工资","key" => "xjgz"];
  1612. $columns[] = ["title" => "基础绩效","key" => "jcjx"];
  1613. $columns[] = ["title" => "月绩效","key" => "yjx"];
  1614. }
  1615. foreach ($info['types'] as $k=>$v){
  1616. if($v['cate'] == 1){
  1617. $columns[] = ["title" => $v['title'],"key" => "subsidies".$v['id']];
  1618. }
  1619. }
  1620. if($info['type'] != 5){
  1621. $columns[] = ["title" => "应发工资","key" => "shouldTotal"];
  1622. }
  1623. foreach ($info['types'] as $k=>$v){
  1624. if($v['cate'] != 1){
  1625. $columns[] = ["title" => $v['title'],"key" => "subsidies".$v['id']];
  1626. }
  1627. }
  1628. if($info['type'] == 1||$info['type'] == 2){
  1629. $columns[] = ["title" => "社保公积金代扣","key" => "sbgjj"];
  1630. }
  1631. if($info['type'] == 3||$info['type'] == 4){
  1632. $columns[] = ["title" => "个税","key" => "tax"];
  1633. }
  1634. if($info['type'] == 1||$info['type'] == 2){
  1635. $columns[] = ["title" => "个税","key" => "tax"];
  1636. }
  1637. if($info['type'] != 5){
  1638. $columns[] = ["title" => "代扣合计","key" => "dktotal"];
  1639. }
  1640. $columns[] = ["title" => "实发工资","key" => "salary"];
  1641. $data = [
  1642. 'columns' => $columns,
  1643. 'list' => $data['list']
  1644. ];
  1645. return $data;
  1646. }
  1647. public function mySalary($page,$size,$month,$userId){
  1648. if($month != ''){
  1649. $map[] = ['month','=',$month];
  1650. }
  1651. $map[] = ['user_id','=',$userId];
  1652. $lists = Db::name('common_salary')
  1653. ->where($map)
  1654. ->page($page,$size)
  1655. ->order('month desc,id desc')
  1656. ->select();
  1657. foreach ($lists as $k=>$v){
  1658. if($v['type'] == 1){
  1659. $lists[$k]['type_name'] = '中心编制';
  1660. }else if($v['type'] == 2){
  1661. $lists[$k]['type_name'] = '新华聘用';
  1662. }else if($v['type'] == 3){
  1663. $lists[$k]['type_name'] = '中心劳务';
  1664. }else if($v['type'] == 4){
  1665. $lists[$k]['type_name'] = '新华劳务';
  1666. }else{
  1667. $lists[$k]['type_name'] = '中心退休';
  1668. }
  1669. }
  1670. $total = Db::name('common_salary')
  1671. ->where($map)->count();
  1672. $data = [
  1673. 'total' => $total,
  1674. 'list' => $lists?$lists:[]
  1675. ];
  1676. return $data;
  1677. }
  1678. public function salaryDetail($id,$userId){
  1679. $info = Db::name('common_salary')->where('id',$id)->where('user_id',$userId)->find();
  1680. if(!$info){
  1681. HelpHander::error('记录不存在');
  1682. }
  1683. if($info['type'] == 5){ // 退休
  1684. $data = Db::name('user_retired_salary')->where('salary_record_id',$id)->where('user_id',$userId)->find();
  1685. }else{ // 其他
  1686. $data = Db::name('salary_record_log')->where('salary_record_id',$id)->where('user_id',$userId)->find();
  1687. }
  1688. if(!$data){
  1689. HelpHander::error('工资不存在');
  1690. }
  1691. $data['pay_type'] = $info['type'];
  1692. $data['btjson'] = $data['btjson']?json_decode($data['btjson'],true):[];
  1693. $data['gjj'] = '0';
  1694. $data['yla'] = '0';
  1695. $data['qynj'] = '0';
  1696. $data['yli'] = '0';
  1697. $data['sye'] = '0';
  1698. $sbdata = Db::name('salary_record_sb')->where('salary_record_id',$id)->where('user_id',$userId)->find();
  1699. if($sbdata){
  1700. $data['gjj'] = $sbdata['gjjp'];
  1701. $data['yla'] = $sbdata['ylap'];
  1702. $data['qynj'] = $sbdata['qynjp'];
  1703. $data['yli'] = $sbdata['ylip'];
  1704. $data['sye'] = $sbdata['syep'];
  1705. }
  1706. return $data;
  1707. }
  1708. public function sbinfo($id,$uid){
  1709. $info = Db::name('salary_record_sb')
  1710. ->alias('srs')
  1711. ->join('salary_record sr','sr.id = srs.salary_record_id')
  1712. ->where('srs.salary_record_id',$id)
  1713. ->where('srs.user_id',$uid)
  1714. ->field('sr.type,srs.*')
  1715. ->find();
  1716. if(!$info){
  1717. HelpHander::error('数据不存在');
  1718. }
  1719. return $info;
  1720. }
  1721. public function taxinfo($id,$uid){
  1722. $info = Db::name('salary_record_special')
  1723. ->alias('srs')
  1724. ->join('salary_record sr','sr.id = srs.salary_record_id')
  1725. ->where('srs.salary_record_id',$id)
  1726. ->where('srs.user_id',$uid)
  1727. ->field('sr.type,srs.*')
  1728. ->find();
  1729. if(!$info){
  1730. HelpHander::error('数据不存在');
  1731. }
  1732. return $info;
  1733. }
  1734. // 同意 或签
  1735. public function agree($id,$userId,$orgId){
  1736. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  1737. if(!$info){
  1738. HelpHander::error('记录不存在');
  1739. }
  1740. if($info['status'] != 0){
  1741. HelpHander::error('无权限操作');
  1742. }
  1743. $uinfo = Db::name('salary_apply_record')
  1744. ->where('salary_record_id',$id)
  1745. ->where('user_id',$userId)
  1746. ->where('salary_apply_id',$info['cur_node'])
  1747. ->where('status',0)
  1748. ->find();
  1749. if(!$uinfo){
  1750. HelpHander::error('无权限操作');
  1751. }
  1752. $apply = json_decode($info['apply'],true);
  1753. Db::startTrans();
  1754. try{
  1755. $res = Db::name('salary_apply_record')->where('id',$uinfo['id'])->update(['status'=>1,'update_time'=>date('Y-m-d H:i:s')]);
  1756. if(!$res){
  1757. \exception('操作失败');
  1758. }
  1759. if($info['cur_node'] == $apply[count($apply)-1]['id']){ // 最后一个节点
  1760. $res2 = Db::name('salary_record')->where('id',$id)->setField('status',1);
  1761. if(!$res2){
  1762. \exception('操作失败');
  1763. }
  1764. }else{
  1765. // 获取下一节点
  1766. $next = [];
  1767. foreach ($apply as $k=>$v){
  1768. if($v['id'] == $info['cur_node']){
  1769. $next = $apply[$k+1];
  1770. break;
  1771. }
  1772. }
  1773. $res2 = Db::name('salary_record')->where('id',$id)->setField('cur_node',$next['id']);
  1774. if(!$res2){
  1775. \exception('操作失败');
  1776. }
  1777. // 生成审核记录
  1778. $uids = explode(',',$next['userids']);
  1779. foreach ($uids as $k=>$v){
  1780. $sd = [
  1781. 'salary_record_id' => $id,
  1782. 'user_id' => $v,
  1783. 'status' => 0,
  1784. 'create_time' => date('Y-m-d H:i:s'),
  1785. 'salary_apply_id' => $next['id']
  1786. ];
  1787. $ret = Db::name('salary_apply_record')->insertGetId($sd);
  1788. if(!$ret){
  1789. \exception('操作失败');
  1790. }
  1791. model('Message')->add(3,$id,12,$v,$orgId,'工资流程待您的审核确认');
  1792. }
  1793. }
  1794. Db::commit();
  1795. }catch (Exception $e){
  1796. Db::rollback();
  1797. $this->error = '操作失败';
  1798. return false;
  1799. }
  1800. return true;
  1801. }
  1802. // 打回
  1803. public function disagree($id,$userId,$orgId){
  1804. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  1805. if(!$info){
  1806. HelpHander::error('记录不存在');
  1807. }
  1808. if($info['status'] != 0){
  1809. HelpHander::error('无权限操作');
  1810. }
  1811. $uinfo = Db::name('salary_apply_record')
  1812. ->where('salary_record_id',$id)
  1813. ->where('user_id',$userId)
  1814. ->where('salary_apply_id',$info['cur_node'])
  1815. ->where('status',0)
  1816. ->find();
  1817. if(!$uinfo){
  1818. HelpHander::error('无权限操作');
  1819. }
  1820. Db::startTrans();
  1821. try{
  1822. $res = Db::name('salary_apply_record')->where('id',$uinfo['id'])->update(['status'=>2,'update_time'=>date('Y-m-d H:i:s')]);
  1823. if(!$res){
  1824. \exception('操作失败');
  1825. }
  1826. $res2 = Db::name('salary_record')->where('id',$id)->setField('status',3);
  1827. if(!$res2){
  1828. \exception('操作失败');
  1829. }
  1830. Db::commit();
  1831. }catch (Exception $e){
  1832. Db::rollback();
  1833. $this->error = '操作失败';
  1834. return false;
  1835. }
  1836. return true;
  1837. }
  1838. // 会签版本
  1839. public function agreeold($id,$userId,$orgId){
  1840. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  1841. if(!$info){
  1842. HelpHander::error('记录不存在');
  1843. }
  1844. if($info['status'] != 0){
  1845. HelpHander::error('无权限操作');
  1846. }
  1847. $uinfo = Db::name('salary_apply_record')
  1848. ->where('salary_record_id',$id)
  1849. ->where('user_id',$userId)
  1850. ->where('salary_apply_id',$info['cur_node'])
  1851. ->where('status',0)
  1852. ->find();
  1853. if(!$uinfo){
  1854. HelpHander::error('无权限操作');
  1855. }
  1856. // 检查是否有除自己之外的未处理的人
  1857. $others = Db::name('salary_apply_record')
  1858. ->where('salary_record_id',$id)
  1859. ->where('user_id','<>',$userId)
  1860. ->where('salary_apply_id',$info['cur_node'])
  1861. ->where('status',0)
  1862. ->find();
  1863. $apply = json_decode($info['apply'],true);
  1864. Db::startTrans();
  1865. try{
  1866. $res = Db::name('salary_apply_record')->where('id',$uinfo['id'])->update(['status'=>1,'update_time'=>date('Y-m-d H:i:s')]);
  1867. if(!$res){
  1868. \exception('操作失败');
  1869. }
  1870. if(!$others){ // 没有未处理的人了,走下一节点
  1871. if($info['cur_node'] == $apply[count($apply)-1]['id']){ // 最后一个节点
  1872. $res2 = Db::name('salary_record')->where('id',$id)->setField('status',1);
  1873. if(!$res2){
  1874. \exception('操作失败');
  1875. }
  1876. }else{
  1877. // 获取下一节点
  1878. $next = [];
  1879. foreach ($apply as $k=>$v){
  1880. if($v['id'] == $info['cur_node']){
  1881. $next = $apply[$k+1];
  1882. break;
  1883. }
  1884. }
  1885. $res2 = Db::name('salary_record')->where('id',$id)->setField('cur_node',$next['id']);
  1886. if(!$res2){
  1887. \exception('操作失败');
  1888. }
  1889. // 生成审核记录
  1890. $uids = explode(',',$next['userids']);
  1891. foreach ($uids as $k=>$v){
  1892. $sd = [
  1893. 'salary_record_id' => $id,
  1894. 'user_id' => $v,
  1895. 'status' => 0,
  1896. 'create_time' => date('Y-m-d H:i:s'),
  1897. 'salary_apply_id' => $next['id']
  1898. ];
  1899. $ret = Db::name('salary_apply_record')->insertGetId($sd);
  1900. if(!$ret){
  1901. \exception('操作失败');
  1902. }
  1903. model('Message')->add(3,$id,12,$v,$orgId,'工资流程待您的审核确认');
  1904. }
  1905. }
  1906. }
  1907. Db::commit();
  1908. }catch (Exception $e){
  1909. Db::rollback();
  1910. return false;
  1911. }
  1912. return true;
  1913. }
  1914. public function confirm($id,$userId){
  1915. $info = Db::name('salary_record')->where('id',$id)->where('del',0)->find();
  1916. if(!$info){
  1917. HelpHander::error('记录不存在');
  1918. }
  1919. if($info['status'] != 1){
  1920. HelpHander::error('无权限操作');
  1921. }
  1922. $res = Db::name('salary_record')->where('id',$id)->update([
  1923. 'cw_time' => date('Y-m-d H:i:s'),
  1924. 'cw_user_id' => $userId,
  1925. 'status' => 2
  1926. ]);
  1927. if(!$res){
  1928. HelpHander::error('操作失败');
  1929. }
  1930. return true;
  1931. }
  1932. // 工资统计
  1933. public function financeSalaryStructure($type,$cate){
  1934. $map[] = ['b.type','=',$cate];
  1935. $map[] = ['b.status','=',2];
  1936. $map[] = ['b.del','=',0];
  1937. if($type == 1){
  1938. $map[] = ['b.month','=',date('Y-m')];
  1939. }else {// 当年
  1940. $year = date('Y');
  1941. $startMonth = $year . '-01';
  1942. $endMonth = ($year + 1) . '-01';
  1943. $map[] = ['b.month','>=',$startMonth];
  1944. $map[] = ['b.month','<',$endMonth];
  1945. }
  1946. if($cate == 5){ // 中心养老
  1947. $total = Db::name('user_retired_salary')
  1948. ->alias('a')
  1949. ->join('salary_record b','b.id = a.salary_record_id')
  1950. ->where($map)
  1951. ->sum('salary');
  1952. }else{// 其他
  1953. $total = Db::name('salary_record_log')
  1954. ->alias('a')
  1955. ->join('salary_record b','b.id = a.salary_record_id')
  1956. ->where($map)
  1957. ->sum('salary');
  1958. }
  1959. return $total;
  1960. }
  1961. public function taxlist($page,$size,$month,$name,$type){
  1962. if($month != ''){
  1963. $map[] = ['sr.month','=',$month];
  1964. }
  1965. if($name != ''){
  1966. $map[] = ['ui.name','=',$name];
  1967. }
  1968. if(in_array($type,[1,2])){
  1969. $map[] = ['sr.type','=',$type];
  1970. }
  1971. $map[] = ['sr.del','=',0];
  1972. $map = empty($map)?true:$map;
  1973. $lists = Db::name('salary_record_special')
  1974. ->alias('srs')
  1975. ->join('salary_record sr','sr.id = srs.salary_record_id')
  1976. ->join('user_info ui','ui.user_id = srs.user_id')
  1977. ->where($map)
  1978. ->page($page,$size)
  1979. ->order('sr.id desc,ui.sorts3 asc')
  1980. ->field('srs.*,ui.name,sr.month')
  1981. ->select();
  1982. foreach ($lists as $k=>$v){
  1983. $ljkc = $v['ljsr'] - $v['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
  1984. $lists[$k]['ljkc'] = round($ljkc,2);
  1985. $lists[$k]['ljyykyjsr'] = 0;
  1986. $lists[$k]['jmsr'] = 0;
  1987. $ljykjse = $v['ljykjse'] + $v['tax'];
  1988. $lists[$k]['ljykjse'] = round($ljykjse,2);
  1989. if($ljkc > 0){
  1990. $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
  1991. $lists[$k]['ljyykyjsr'] = round($tax,2);
  1992. $jmse = round($tax*$v['jmsebl'],2); // 减免税额
  1993. $lists[$k]['jmsr'] = $jmse;
  1994. $tax = round($tax - $jmse - $ljykjse,2);
  1995. $tax = $tax>0?$tax:0;
  1996. }else{
  1997. $tax = 0;
  1998. }
  1999. $lists[$k]['nextTax'] = $tax;
  2000. }
  2001. $total = Db::name('salary_record_special')
  2002. ->alias('srs')
  2003. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2004. ->join('user_info ui','ui.user_id = srs.user_id')
  2005. ->where($map)->count();
  2006. $data = [
  2007. 'total' => $total,
  2008. 'list' => $lists?$lists:[]
  2009. ];
  2010. return $data;
  2011. }
  2012. // 个税累计列表
  2013. public function ntaxlist($month,$name,$type){
  2014. if($month){
  2015. $map[] = ['sr.month','=',$month];
  2016. }else{
  2017. return [];
  2018. }
  2019. if($name != ''){
  2020. $map[] = ['ui.name','=',$name];
  2021. }
  2022. if(in_array($type,[1,2])){
  2023. $map[] = ['sr.type','=',$type];
  2024. }
  2025. $map[] = ['sr.del','=',0];
  2026. $map = empty($map)?true:$map;
  2027. $lists = Db::name('salary_record_special')
  2028. ->alias('srs')
  2029. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2030. ->join('user_info ui','ui.user_id = srs.user_id')
  2031. ->where($map)
  2032. ->group('sr.month,srs.user_id')
  2033. ->field('srs.*,ui.name,sr.month')
  2034. ->order('sr.id desc,ui.sorts3 asc')
  2035. ->select();
  2036. foreach ($lists as $k=>$v){
  2037. $children = Db::name('salary_record_special')
  2038. ->alias('srs')
  2039. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2040. ->join('user_info ui','ui.user_id = srs.user_id')
  2041. ->where('sr.month',$v['month'])
  2042. ->where('srs.user_id',$v['user_id'])
  2043. ->where('sr.del',0)
  2044. ->field('srs.*,ui.name,sr.month')
  2045. ->order('srs.id desc')
  2046. ->select();
  2047. $nsalary = 0;
  2048. $nmssr = 0;
  2049. $nsr = 0;
  2050. foreach ($children as $kk=>$vv){
  2051. $ljkc = $vv['ljsr'] - $vv['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
  2052. $children[$kk]['ljkc'] = round($ljkc,2);
  2053. $children[$kk]['ljyykyjsr'] = 0;
  2054. $children[$kk]['jmsr'] = 0;
  2055. $ljykjse = $vv['ljykjse'] + $vv['tax'];
  2056. $children[$kk]['ljykjse'] = round($ljykjse,2);
  2057. if($ljkc > 0){
  2058. $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
  2059. $children[$kk]['ljyykyjsr'] = round($tax,2);
  2060. $jmse = round($tax*$vv['jmsebl'],2); // 减免税额
  2061. $children[$kk]['jmsr'] = $jmse;
  2062. $tax = round($tax - $jmse - $ljykjse,2);
  2063. $tax = $tax>0?$tax:0;
  2064. }else{
  2065. $tax = 0;
  2066. }
  2067. $children[$kk]['nextTax'] = $tax;
  2068. $children[$kk]['child'] = 1;
  2069. $nsalary += $vv['salary'];
  2070. $nmssr += $vv['mssr'];
  2071. $nsr += $vv['sr'];
  2072. $children[$kk]['nsalary'] = $vv['salary'];
  2073. $children[$kk]['nmssr'] = $vv['mssr'];
  2074. $children[$kk]['nsr'] = $vv['sr'];
  2075. $children[$kk]['unique'] = 'C'.$vv['id'];
  2076. }
  2077. $lists[$k] = $children[0];
  2078. $lists[$k]['child'] = 0;
  2079. $lists[$k]['nsalary'] = round($nsalary,2);
  2080. $lists[$k]['nmssr'] = round($nmssr,2);
  2081. $lists[$k]['nsr'] = round($nsr,2);
  2082. $lists[$k]['unique'] = $children[0]['id'];
  2083. if(count($children) >= 2){
  2084. $lists[$k]['children'] = $children;
  2085. }
  2086. }
  2087. return $lists?$lists:[];
  2088. }
  2089. public function ntaxexport($month,$name,$type){
  2090. if($month != ''){
  2091. $map[] = ['sr.month','=',$month];
  2092. }
  2093. if($name != ''){
  2094. $map[] = ['ui.name','=',$name];
  2095. }
  2096. if(in_array($type,[1,2])){
  2097. $map[] = ['sr.type','=',$type];
  2098. }
  2099. $map[] = ['sr.del','=',0];
  2100. $map = empty($map)?true:$map;
  2101. $lists = Db::name('salary_record_special')
  2102. ->alias('srs')
  2103. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2104. ->join('user_info ui','ui.user_id = srs.user_id')
  2105. ->where($map)
  2106. ->group('sr.month,srs.user_id')
  2107. ->field('srs.*,ui.name,sr.month')
  2108. ->order('sr.id desc,ui.sorts3 asc')
  2109. ->select();
  2110. foreach ($lists as $k=>$v){
  2111. $children = Db::name('salary_record_special')
  2112. ->alias('srs')
  2113. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2114. ->join('user_info ui','ui.user_id = srs.user_id')
  2115. ->where('sr.month',$v['month'])
  2116. ->where('srs.user_id',$v['user_id'])
  2117. ->where('sr.del',0)
  2118. ->field('srs.*,ui.name,sr.month')
  2119. ->order('srs.id desc')
  2120. ->select();
  2121. $nsalary = 0;
  2122. $nmssr = 0;
  2123. $nsr = 0;
  2124. foreach ($children as $kk=>$vv){
  2125. $ljkc = $vv['ljsr'] - $vv['ljsqljxmhj']; // 累计预扣预缴应纳税所得额
  2126. $children[$kk]['ljkc'] = round($ljkc,2);
  2127. $children[$kk]['ljyykyjsr'] = 0;
  2128. $children[$kk]['jmsr'] = 0;
  2129. $ljykjse = $vv['ljykjse'] + $vv['tax'];
  2130. $children[$kk]['ljykjse'] = round($ljykjse,2);
  2131. if($ljkc > 0){
  2132. $tax = $this->getYkyjTax($ljkc);// 累计应预扣预缴税额
  2133. $children[$kk]['ljyykyjsr'] = round($tax,2);
  2134. $jmse = round($tax*$vv['jmsebl'],2); // 减免税额
  2135. $children[$kk]['jmsr'] = $jmse;
  2136. $tax = round($tax - $jmse - $ljykjse,2);
  2137. $tax = $tax>0?$tax:0;
  2138. }else{
  2139. $tax = 0;
  2140. }
  2141. $children[$kk]['nextTax'] = $tax;
  2142. $children[$kk]['child'] = 1;
  2143. $nsalary += $vv['salary'];
  2144. $nmssr += $vv['mssr'];
  2145. $nsr += $vv['sr'];
  2146. $children[$kk]['nsalary'] = $vv['salary'];
  2147. $children[$kk]['nmssr'] = $vv['mssr'];
  2148. $children[$kk]['nsr'] = $vv['sr'];
  2149. $children[$kk]['unique'] = 'C'.$vv['id'];
  2150. }
  2151. $lists[$k] = $children[0];
  2152. $lists[$k]['child'] = 0;
  2153. $lists[$k]['nsalary'] = round($nsalary,2);
  2154. $lists[$k]['nmssr'] = round($nmssr,2);
  2155. $lists[$k]['nsr'] = round($nsr,2);
  2156. $lists[$k]['unique'] = $children[0]['id'];
  2157. if(count($children) >= 2){
  2158. $lists[$k]['children'] = $children;
  2159. }
  2160. }
  2161. //转成数字格式
  2162. $extra = ['month','name'];
  2163. foreach ($lists as $k=>$v){
  2164. foreach ($v as $kk=>$vv){
  2165. if(!in_array($kk,$extra)){
  2166. $v[$kk] = (float)$vv;
  2167. }
  2168. }
  2169. $lists[$k] = $v;
  2170. }
  2171. $columns[] = ["title" => "月份","key" => "month"];
  2172. $columns[] = ["title" => "姓名","key" => "name"];
  2173. $columns[] = ["title" => "应发工资","key" => "nsalary"];
  2174. $columns[] = ["title" => "免税收入","key" => "nmssr"];
  2175. $columns[] = ["title" => "应税收入","key" => "nsr"];
  2176. $columns[] = ["title" => "累计应税收入","key" => "ljsr"];
  2177. $columns[] = ["title" => "累计减除费用","key" => "ljjc"];
  2178. $columns[] = ["title" => "累计公积金","key" => "ljgjj"];
  2179. $columns[] = ["title" => "累计养老保险","key" => "ljylobx"];
  2180. $columns[] = ["title" => "累计失业保险","key" => "ljsybx"];
  2181. $columns[] = ["title" => "累计医疗保险","key" => "ljylibx"];
  2182. $columns[] = ["title" => "累计职业年金","key" => "ljnj"];
  2183. $columns[] = ["title" => "累计子女教育","key" => "ljznjy"];
  2184. $columns[] = ["title" => "累计继续教育","key" => "ljjxjy"];
  2185. $columns[] = ["title" => "累计住房贷款利息","key" => "ljzfdklx"];
  2186. $columns[] = ["title" => "累计住房租金","key" => "ljzfzj"];
  2187. $columns[] = ["title" => "累计赡养老人","key" => "ljsylr"];
  2188. $columns[] = ["title" => "商业健康保险","key" => "syjkbx"];
  2189. $columns[] = ["title" => "延税养老保险","key" => "ysylbx"];
  2190. $columns[] = ["title" => "个人补扣/补交社保年金","key" => "grbk"];
  2191. $columns[] = ["title" => "年终奖补交社保年金","key" => "jzjbjsb"];
  2192. $columns[] = ["title" => "工资中补交社保年金","key" => "ljgzds"];
  2193. $columns[] = ["title" => "累计准予扣除的捐赠额","key" => "zykcjze"];
  2194. $columns[] = ["title" => "累计其他扣除","key" => "qtkc"];
  2195. $columns[] = ["title" => "税前扣除项目合计","key" => "ljsqljxmhj"];
  2196. $columns[] = ["title" => "累计预扣预缴应纳税所得额","key" => "ljkc"];
  2197. $columns[] = ["title" => "累计应预扣预缴税额","key" => "ljyykyjsr"];
  2198. $columns[] = ["title" => "减免税额","key" => "jmsr"];
  2199. $columns[] = ["title" => "累计已扣缴税额","key" => "ljykjse"];
  2200. $columns[] = ["title" => "下月应扣个人所得税税额","key" => "nextTax"];
  2201. $data = [
  2202. 'columns' => $columns,
  2203. 'list' => $lists
  2204. ];
  2205. return $data;
  2206. }
  2207. // 根据累计预扣预缴应纳税所得额获取累计应预扣预缴税额
  2208. public function getYkyjTax($ljkc){
  2209. $tax1 = $ljkc*0.03 - 0;
  2210. $tax2 = $ljkc*0.1 - 2520;
  2211. $tax3 = $ljkc*0.2 - 16920;
  2212. $tax4 = $ljkc*0.25 - 31920;
  2213. $tax5 = $ljkc*0.3 - 52920;
  2214. $tax6 = $ljkc*0.35 - 85920;
  2215. $tax7 = $ljkc*0.45 - 181920;
  2216. $tax = round(max([$tax1,$tax2,$tax3,$tax4,$tax5,$tax6,$tax7]),2); // 累计应预扣预缴税额
  2217. return $tax;
  2218. }
  2219. public function taxEdit(){
  2220. $id = input('id/d',0);
  2221. $info = Db::name('salary_record_special')->where('id',$id)->find();
  2222. if(!$info){
  2223. HelpHander::error('记录不存在');
  2224. }
  2225. $oldljsr = $info['ljsr'] - $info['sr'];
  2226. $data = [
  2227. 'id' => input('id/d',0),
  2228. 'salary' => input('salary/f',0),
  2229. 'mssr' => input('mssr/f',0),
  2230. // 'sr' => input('sr/f',0),
  2231. // 'ljsr' => input('ljsr/f',0),
  2232. 'ljjc' => input('ljjc/f',0),
  2233. 'ljgjj' => input('ljgjj/f',0),
  2234. 'ljylobx' => input('ljylobx/f',0),
  2235. 'ljylibx' => input('ljylibx/f',0),
  2236. 'ljsybx' => input('ljsybx/f',0),
  2237. 'ljnj' => input('ljnj/f',0),
  2238. 'ljznjy' => input('ljznjy/f',0),
  2239. 'ljjxjy' => input('ljjxjy/f',0),
  2240. 'ljzfdklx' => input('ljzfdklx/f',0),
  2241. 'ljzfzj' => input('ljzfzj/f',0),
  2242. 'ljsylr' => input('ljsylr/f',0),
  2243. 'syjkbx' => input('syjkbx/f',0),
  2244. 'ysylbx' => input('ysylbx/f',0),
  2245. 'grbk' => input('grbk/f',0),
  2246. 'jzjbjsb' => input('jzjbjsb/f',0),
  2247. 'ljgzds' => input('ljgzds/f',0),
  2248. 'qtkc' => input('qtkc/f',0),
  2249. // 'ljsqljxmhj' => input('ljsqljxmhj/f',0),
  2250. 'ljykjse' => input('ljykjse/f',0),
  2251. 'tax' => input('tax/f',0),
  2252. 'zykcjze' => input('zykcjze/f',0),
  2253. 'jmsebl' => input('jmsebl/f',0),
  2254. ];
  2255. $data['sr'] = round($data['salary'] - $data['mssr'],2);
  2256. $data['ljsr'] = round($oldljsr + $data['sr'],2);
  2257. $data['ljsqljxmhj'] = $data['ljjc'] + $data['ljgjj'] + $data['ljylobx'] + $data['ljsybx'] + $data['ljylibx'] + $data['ljnj'] + $data['ljznjy']
  2258. + $data['ljjxjy'] + $data['ljzfdklx'] + $data['ljzfzj'] + $data['ljsylr'] + $data['ljgzds'] + $data['syjkbx'] + $data['ysylbx']
  2259. + $data['grbk'] + $data['jzjbjsb'] + $data['zykcjze'] + $data['qtkc'];
  2260. $data['ljsqljxmhj'] = round($data['ljsqljxmhj'],2);
  2261. $ret = Db::name('salary_record_special')->where('id',$data['id'])->update($data);
  2262. if(!$ret){
  2263. HelpHander::error('操作失败');
  2264. }
  2265. return true;
  2266. }
  2267. // 社保公积金
  2268. public function sblist($month,$name,$type){
  2269. if($month){
  2270. $map[] = ['sr.month','=',$month];
  2271. }else{
  2272. return [];
  2273. }
  2274. if($name != ''){
  2275. $map[] = ['ui.name','=',$name];
  2276. }
  2277. if(in_array($type,[1,2])){
  2278. $map[] = ['sr.type','=',$type];
  2279. }
  2280. $map[] = ['sr.del','=',0];
  2281. $map = empty($map)?true:$map;
  2282. $lists = Db::name('salary_record_sb')
  2283. ->alias('srs')
  2284. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2285. ->join('user_info ui','ui.user_id = srs.user_id')
  2286. ->where($map)
  2287. // ->page($page,$size)
  2288. ->order('sr.id desc,ui.sorts3 asc')
  2289. ->field('ui.name,sr.month,srs.id,srs.user_id,sum(srs.qynj) as qynj,sum(srs.gjj) as gjj,
  2290. sum(srs.sb) as sb,sum(srs.syyl) as syyl,sum(srs.gssy) as gssy,sum(srs.ylnj) as ylnj,
  2291. sum(srs.gjjp) as gjjp,sum(srs.gjjc) as gjjc,sum(srs.ylap) as ylap,sum(srs.ylac) as ylac,
  2292. sum(srs.syep) as syep,sum(srs.syec) as syec,sum(srs.ylip) as ylip,sum(srs.ylic) as ylic,
  2293. sum(srs.gsc) as gsc,sum(srs.qynjp) as qynjp,sum(srs.qynjc) as qynjc')
  2294. ->group('sr.month,srs.user_id')
  2295. ->select();
  2296. foreach ($lists as $k=>$v){
  2297. $lists[$k]['sbtotalp'] = round($v['ylap'] + $v['syep'] + $v['ylip'],2);
  2298. $lists[$k]['sbtotalc'] = round($v['ylac'] + $v['syec'] + $v['ylic'] + $v['gsc'],2);
  2299. $lists[$k]['sbtotal'] = round($lists[$k]['sbtotalp'] + $lists[$k]['sbtotalc'],2);
  2300. $lists[$k]['gjjtotal'] = round($lists[$k]['gjjp'] + $lists[$k]['gjjc'],2);
  2301. $lists[$k]['qynjtotal'] = round($lists[$k]['qynjp'] + $lists[$k]['qynjc'],2);
  2302. }
  2303. // $total = Db::name('salary_record_sb')
  2304. // ->alias('srs')
  2305. // ->join('salary_record sr','sr.id = srs.salary_record_id')
  2306. // ->join('user_info ui','ui.user_id = srs.user_id')
  2307. // ->where($map)
  2308. // ->group('sr.month,srs.user_id')
  2309. // ->count();
  2310. // $data = [
  2311. // 'total' => $total,
  2312. // 'list' => $lists?$lists:[]
  2313. // ];
  2314. return $lists?$lists:[];
  2315. }
  2316. public function sbexport($month,$name,$type){
  2317. if($month != ''){
  2318. $map[] = ['sr.month','=',$month];
  2319. }
  2320. if($name != ''){
  2321. $map[] = ['ui.name','=',$name];
  2322. }
  2323. if(in_array($type,[1,2])){
  2324. $map[] = ['sr.type','=',$type];
  2325. }
  2326. $map[] = ['sr.del','=',0];
  2327. $map = empty($map)?true:$map;
  2328. $lists = Db::name('salary_record_sb')
  2329. ->alias('srs')
  2330. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2331. ->join('user_info ui','ui.user_id = srs.user_id')
  2332. ->where($map)
  2333. ->order('sr.id desc,ui.sorts3 asc')
  2334. ->field('ui.name,sr.month,srs.id,srs.user_id,sum(srs.qynj) as qynj,sum(srs.gjj) as gjj,
  2335. sum(srs.sb) as sb,sum(srs.syyl) as syyl,sum(srs.gssy) as gssy,sum(srs.ylnj) as ylnj,
  2336. sum(srs.gjjp) as gjjp,sum(srs.gjjc) as gjjc,sum(srs.ylap) as ylap,sum(srs.ylac) as ylac,
  2337. sum(srs.syep) as syep,sum(srs.syec) as syec,sum(srs.ylip) as ylip,sum(srs.ylic) as ylic,
  2338. sum(srs.gsc) as gsc,sum(srs.qynjp) as qynjp,sum(srs.qynjc) as qynjc')
  2339. ->group('sr.month,srs.user_id')
  2340. ->distinct(true)
  2341. ->select();
  2342. foreach ($lists as $k=>$v){
  2343. $lists[$k]['sbtotalp'] = round($v['ylap'] + $v['syep'] + $v['ylip'],2);
  2344. $lists[$k]['sbtotalc'] = round($v['ylac'] + $v['syec'] + $v['ylic'] + $v['gsc'],2);
  2345. $lists[$k]['sbtotal'] = round($lists[$k]['sbtotalp'] + $lists[$k]['sbtotalc'],2);
  2346. $lists[$k]['gjjtotal'] = round($lists[$k]['gjjp'] + $lists[$k]['gjjc'],2);
  2347. $lists[$k]['qynjtotal'] = round($lists[$k]['qynjp'] + $lists[$k]['qynjc'],2);
  2348. }
  2349. //转成数字格式
  2350. $extra = ['month','name'];
  2351. foreach ($lists as $k=>$v){
  2352. foreach ($v as $kk=>$vv){
  2353. if(!in_array($kk,$extra)){
  2354. $v[$kk] = (float)$vv;
  2355. }
  2356. }
  2357. $lists[$k] = $v;
  2358. }
  2359. $columns[] = ["title" => "月份","key" => "month"];
  2360. $columns[] = ["title" => "姓名","key" => "name"];
  2361. if($type == 2){
  2362. $columns[] = ["title" => "企业年金基数","key" => "qynj"];
  2363. }
  2364. $columns[] = ["title" => "公积金缴纳基数","key" => "gjj"];
  2365. if($type == 2){
  2366. $columns[] = ["title" => "社保缴纳基数","key" => "sn"];
  2367. }else{
  2368. $columns[] = ["title" => "生育/医疗基数","key" => "syyl"];
  2369. $columns[] = ["title" => "工伤/失业基数","key" => "gssy"];
  2370. $columns[] = ["title" => "养老/年金基数","key" => "ylnj"];
  2371. }
  2372. $columns[] = ["title" => "养老保险(个人)","key" => "ylap"];
  2373. $columns[] = ["title" => "失业保险(个人)","key" => "syep"];
  2374. $columns[] = ["title" => "医疗保险(个人)","key" => "ylip"];
  2375. $columns[] = ["title" => "个人小计(社保)","key" => "ylac"];
  2376. $columns[] = ["title" => "养老保险(单位)","key" => "ylac"];
  2377. $columns[] = ["title" => "失业保险(单位)","key" => "syec"];
  2378. $columns[] = ["title" => "医疗保险(单位)","key" => "ylic"];
  2379. $columns[] = ["title" => "工伤保险(单位)","key" => "gsc"];
  2380. $columns[] = ["title" => "单位小计(社保)","key" => "sbtotalc"];
  2381. $columns[] = ["title" => "社保合计","key" => "sbtotal"];
  2382. $columns[] = ["title" => "公积金(个人)","key" => "gjjp"];
  2383. $columns[] = ["title" => "公积金(单位)","key" => "gjjc"];
  2384. $columns[] = ["title" => "公积金合计","key" => "gjjtotal"];
  2385. if($type == 2){
  2386. $columns[] = ["title" => "企业年金(个人)","key" => "qynjp"];
  2387. $columns[] = ["title" => "企业年金(单位)","key" => "qynjc"];
  2388. $columns[] = ["title" => "企业年金合计","key" => "qynjtotal"];
  2389. }else{
  2390. $columns[] = ["title" => "职业年金(个人)","key" => "qynjp"];
  2391. $columns[] = ["title" => "职业年金(单位)","key" => "qynjc"];
  2392. $columns[] = ["title" => "职业年金合计","key" => "qynjtotal"];
  2393. }
  2394. $data = [
  2395. 'columns' => $columns,
  2396. 'list' => $lists
  2397. ];
  2398. return $data;
  2399. }
  2400. public function unapply($userId){
  2401. $lists = Db::name('salary_apply_record')
  2402. ->alias('sar')
  2403. ->join('salary_record sr','sr.id = sar.salary_record_id and sr.cur_node = sar.salary_apply_id')
  2404. ->where('sar.user_id',$userId)
  2405. ->where('sr.del',0)
  2406. ->where('sr.status',0)
  2407. ->field('sr.id,sr.month,sr.type,sar.create_time')
  2408. ->select();
  2409. $nlist = [];
  2410. foreach ($lists as $k=>$v){
  2411. $arr = [
  2412. 'id' => $v['id'],
  2413. 'create_time' => $v['create_time']
  2414. ];
  2415. if($v['type'] == 1){
  2416. $arr['title'] = $v['month'].'-中心编制';
  2417. }else if($v['type'] == 2){
  2418. $arr['title'] = $v['month'].'-新华聘用';
  2419. }else if($v['type'] == 3){
  2420. $arr['title'] = $v['month'].'-中心劳务';
  2421. }else if($v['type'] == 4){
  2422. $arr['title'] = $v['month'].'-新华劳务';
  2423. }else{
  2424. $arr['title'] = $v['month'].'-中心养老';
  2425. }
  2426. $nlist[] = $arr;
  2427. }
  2428. return $nlist;
  2429. }
  2430. // 个人明细
  2431. public function userdetail($page,$size,$type,$year){
  2432. // 先取出本年发放工资人数
  2433. if($type != 5){
  2434. $users = Db::name('salary_record_log')
  2435. ->alias('srl')
  2436. ->join('salary_record sr','sr.id = srl.salary_record_id')
  2437. ->join('user_info ui','ui.user_id = srl.user_id')
  2438. ->where('sr.type',$type)
  2439. ->where('sr.month','like',$year.'%')
  2440. ->where('sr.status',2)
  2441. ->where('sr.del',0)
  2442. ->group('srl.user_id')
  2443. ->page($page,$size)
  2444. ->field('ui.name,srl.user_id,sum(gwgz) as gwgz,sum(xjgz) as xjgz,sum(jcjx) as jcjx,sum(yjx) as yjx,
  2445. sum(tax) as tax,sum(salary) as salary,sum(should_total) as should_total,sum(sbgjj) as sbgjj')
  2446. ->distinct(true)
  2447. ->order('ui.sorts3 asc')
  2448. ->select();
  2449. return $users;
  2450. }else{
  2451. }
  2452. }
  2453. public function userYear($type,$year){
  2454. $lists = Db::name('salary_record_sb')
  2455. ->alias('srs')
  2456. // ->join('salary_record_log srl','srl.salary_record_id = srs.salary_record_id')
  2457. ->join('user_info ui','ui.user_id = srs.user_id')
  2458. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2459. ->where('sr.type',$type)
  2460. ->where('sr.month','like',$year.'%')
  2461. ->where('sr.status',2)
  2462. ->where('sr.del',0)
  2463. // ->field('ui.name,srl.user_id,sum(srl.should_total) as should_total,sum(srs.ylac) as ylac,sum(srs.ylic) as ylic,sum(srs.gsc) as gsc,
  2464. // sum(srs.syec) as syec,sum(srs.gjjc) as gjjc,sum(srs.qynjc) as qynjc')
  2465. ->field('ui.name,srs.user_id,srs.salary_record_id')
  2466. ->group('srs.user_id')
  2467. ->distinct(true)
  2468. ->order('ui.sorts3 asc')
  2469. ->select();
  2470. $lists = $lists?$lists:[];;
  2471. foreach ($lists as $k=>$v){
  2472. $lists[$k]['should_total'] = Db::name('salary_record_log')
  2473. ->alias('srl')
  2474. ->join('salary_record sr','sr.id = srl.salary_record_id')
  2475. ->where('sr.type',$type)
  2476. ->where('sr.month','like',$year.'%')
  2477. ->where('sr.status',2)
  2478. ->where('sr.del',0)
  2479. ->where('srl.user_id',$v['user_id'])
  2480. ->sum('srl.should_total');
  2481. $lists[$k]['ylac'] = Db::name('salary_record_sb')
  2482. ->alias('srl')
  2483. ->join('salary_record sr','sr.id = srl.salary_record_id')
  2484. ->where('sr.type',$type)
  2485. ->where('sr.month','like',$year.'%')
  2486. ->where('sr.status',2)
  2487. ->where('sr.del',0)
  2488. ->where('srl.user_id',$v['user_id'])
  2489. ->sum('srl.ylac');
  2490. $lists[$k]['ylic'] = Db::name('salary_record_sb')
  2491. ->alias('srl')
  2492. ->join('salary_record sr','sr.id = srl.salary_record_id')
  2493. ->where('sr.type',$type)
  2494. ->where('sr.month','like',$year.'%')
  2495. ->where('sr.status',2)
  2496. ->where('sr.del',0)
  2497. ->where('srl.user_id',$v['user_id'])
  2498. ->sum('srl.ylic');
  2499. // $lists[$k]['ylic'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('ylic');
  2500. $lists[$k]['gsc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('gsc');
  2501. $lists[$k]['syec'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('syec');
  2502. $lists[$k]['gjjc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('gjjc');
  2503. $lists[$k]['qynjc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('qynjc');
  2504. $lists[$k]['key'] = $k+1;
  2505. $lists[$k]['sb_total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['ylac'],2);
  2506. $lists[$k]['total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['ylac'] + $lists[$k]['gjjc'] + $lists[$k]['qynjc'],2);
  2507. }
  2508. return $lists;
  2509. }
  2510. public function yearExport($type,$year){
  2511. $lists = Db::name('salary_record_sb')
  2512. ->alias('srs')
  2513. // ->join('salary_record_log srl','srl.salary_record_id = srs.salary_record_id')
  2514. ->join('user_info ui','ui.user_id = srs.user_id')
  2515. ->join('salary_record sr','sr.id = srs.salary_record_id')
  2516. ->where('sr.type',$type)
  2517. ->where('sr.month','like',$year.'%')
  2518. ->where('sr.status',2)
  2519. ->where('sr.del',0)
  2520. // ->field('ui.name,srl.user_id,sum(srl.should_total) as should_total,sum(srs.ylac) as ylac,sum(srs.ylic) as ylic,sum(srs.gsc) as gsc,
  2521. // sum(srs.syec) as syec,sum(srs.gjjc) as gjjc,sum(srs.qynjc) as qynjc')
  2522. ->field('ui.name,srs.user_id,srs.salary_record_id')
  2523. ->group('srs.user_id')
  2524. ->distinct(true)
  2525. ->order('ui.sorts3 asc')
  2526. ->select();
  2527. $lists = $lists?$lists:[];;
  2528. foreach ($lists as $k=>$v){
  2529. $lists[$k]['should_total'] = Db::name('salary_record_log')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('should_total');
  2530. $lists[$k]['ylac'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('ylac');
  2531. $lists[$k]['ylic'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('ylic');
  2532. $lists[$k]['gsc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('gsc');
  2533. $lists[$k]['syec'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('syec');
  2534. $lists[$k]['gjjc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('gjjc');
  2535. $lists[$k]['qynjc'] = Db::name('salary_record_sb')->where('salary_record_id',$v['salary_record_id'])->where('user_id',$v['user_id'])->sum('qynjc');
  2536. $lists[$k]['key'] = $k+1;
  2537. $lists[$k]['sb_total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['ylac'],2);
  2538. $lists[$k]['total'] = round($lists[$k]['ylac'] + $lists[$k]['ylic'] + $lists[$k]['gsc'] + $lists[$k]['ylac'] + $lists[$k]['gjjc'] + $lists[$k]['qynjc'],2);
  2539. }
  2540. $columns[] = ["title" => "序号","key" => "key"];
  2541. $columns[] = ["title" => "姓名","key" => "name"];
  2542. $columns[] = ["title" => "应发工资","key" => "shouldTotal"];
  2543. $columns[] = ["title" => "养老保险(单位)","key" => "ylac"];
  2544. $columns[] = ["title" => "失业保险(单位)","key" => "syec"];
  2545. $columns[] = ["title" => "医疗保险(单位)","key" => "ylic"];
  2546. $columns[] = ["title" => "工伤保险(单位)","key" => "gsc"];
  2547. $columns[] = ["title" => "社保小计","key" => "sbTotal"];
  2548. $columns[] = ["title" => "住房公积金(单位)","key" => "gjjc"];
  2549. if($type == 2){
  2550. $columns[] = ["title" => "企业年金(单位)","key" => "qynjc"];
  2551. }else{
  2552. $columns[] = ["title" => "职业年金(单位)","key" => "qynjc"];
  2553. }
  2554. $columns[] = ["title" => "用工成本合计","key" => "total"];
  2555. $data = [
  2556. 'columns' => $columns,
  2557. 'list' => $lists
  2558. ];
  2559. return $data;
  2560. }
  2561. }