UserSalaryRecord.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 UserSalaryRecord extends Model
  8. {
  9. // 工资发放
  10. public function sendSalary($data){
  11. $month = $data['month'];
  12. $types = $data['types'];
  13. $types = $types?explode(',',$types):[];
  14. $users = Db::name('user_salary')
  15. ->where('status',1)
  16. ->where('company_id',$data['company_id'])
  17. ->field('create_time,update_time,remark,status',true)
  18. ->select();
  19. $curTime = date('Y-m-d H:i:s');
  20. $bls = Db::name('user_bl')->select();
  21. foreach ($users as $k=>$v){
  22. $udata = $this->getUserBase($v['user_id']);
  23. $age = $udata['age'];
  24. $gender = $udata['gender'];
  25. $rdata = [
  26. 'user_id' => $v['user_id'],
  27. 'company_id' => $data['company_id'],
  28. 'type' => $udata['type'],
  29. 'psid' => $udata['psid'],
  30. 'pwlevel' => $udata['pwlevel'],
  31. 'month_performance' => $udata['month_performance'],
  32. 'wages' => $udata['wages'],
  33. 'post_salary' => $udata['post_salary'],
  34. 'salary_record_id' => $data['salary_record_id'],
  35. 'create_time' => $curTime,
  36. 'month' => $month,
  37. 'fixed_performance' => $v['fixed_performance'],
  38. 'child' => $v['child'],
  39. 'mssr' => $v['mssr'],
  40. 'qtbt' => $v['qtbt'],
  41. ];
  42. $sbgjj = $this->getSbGjj($v['user_id'],$data['company_id'],$bls);
  43. $rdata['sbgjj'] = json_encode($sbgjj);
  44. $butie = $this->getButie($v['user_id'],$data['company_id'],$udata['psid'],$age,$gender,$types,$v,$month);
  45. $rdata['btjson'] = $butie['btjson'];
  46. $rdata['should_total'] = $rdata['wages'] + $rdata['post_salary'] + $rdata['month_performance'] + $rdata['fixed_performance'] + $butie['total'] + $v['qtbt'];
  47. $taxdata = $this->getTax($v['user_id'],$data['company_id'],$month,$rdata,$sbgjj,$v);
  48. $rdata['tax'] = $taxdata['tax'];
  49. $rdata['salary'] = $rdata['should_total'] - $taxdata['tax'] - $rdata['child'] - $sbgjj['gjjp'] - $sbgjj['qynjp'] - $sbgjj['ptotal'];
  50. $rdata['salary'] = $rdata['salary'] > 0?$rdata['salary']:0;
  51. $rid = Db::name('user_salary_record')->insertGetId($rdata);
  52. if(!$rid){
  53. return false;
  54. }
  55. $rtax = $taxdata['data'];
  56. $rtax['record_id'] = $rid;
  57. $ret = Db::name('user_salary_record_tax')->insert($rtax);
  58. if(!$ret){
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. private function getTax($userId,$companyId,$month,$rdata,$sbgjj,$user){
  65. $special = Db::name('user_special')->where('user_id',$userId)->find();
  66. $ysmoney = $rdata['should_total'] - $rdata['mssr']; // 应发-减免税额 = 应税收入
  67. $jc = 5000;
  68. $tax = 0;
  69. //获取上月累计
  70. $mtime = strtotime($month.'-01');
  71. $my = date('Y',$mtime);
  72. $sy = date('Y',$mtime - 2*24*60*60);
  73. $data = [
  74. 'sr' => 0,
  75. 'jc' => 0,
  76. 'nj' => 0,
  77. 'gjj' => 0,
  78. 'ylobx' => 0,
  79. 'sybx' => 0,
  80. 'ylibx' => 0,
  81. 'znjy' => 0,
  82. 'jxjy' => 0,
  83. 'zfdklx' => 0,
  84. 'zfzj' => 0,
  85. 'sylr' => 0,
  86. 'donations' => 0,
  87. 'ykjse' => 0,
  88. 'syjkbx' => 0,
  89. 'ysylbx' => 0,
  90. 'other' => 0,
  91. 'min_tax' => 0
  92. ]; // 累计收入
  93. $smonth = date('Y-m',$mtime - 2*24*60*60);
  94. // 先查tax记录
  95. $info = Db::name('user_salary_record_tax')
  96. ->alias('t')
  97. ->join('user_salary_record r','r.id = t.record_id')
  98. ->where('r.user_id',$userId)
  99. ->where('r.month',$smonth)
  100. ->field('t.*')
  101. ->find();
  102. if(!$info){
  103. $info = Db::name('user_total')
  104. ->where('year',$sy)
  105. ->where('user_id',$userId)
  106. ->find();
  107. }
  108. if($info){
  109. $data = [
  110. 'sr' => $info['sr'],
  111. 'jc' => $info['jc'],
  112. 'nj' => $info['nj'],
  113. 'gjj' => $info['gjj'],
  114. 'ylobx' => $info['ylobx'],
  115. 'sybx' => $info['sybx'],
  116. 'ylibx' => $info['ylibx'],
  117. 'znjy' => $info['znjy'],
  118. 'jxjy' => $info['jxjy'],
  119. 'zfdklx' => $info['zfdklx'],
  120. 'zfzj' => $info['zfzj'],
  121. 'sylr' => $info['sylr'],
  122. 'donations' => $info['donations'],
  123. 'ykjse' => $info['ykjse'],
  124. 'syjkbx' => $info['syjkbx'],
  125. 'ysylbx' => $info['ysylbx'],
  126. 'other' => $info['other'],
  127. 'min_tax' => $info['min_tax']
  128. ];
  129. }
  130. if($companyId == 1){ // 算上个月的税
  131. // 累计预扣预缴应纳税所得额
  132. $ljkc = $data['jc'] + $data['nj'] + $data['gjj'] + $data['ylobx'] + $data['sybx'] + $data['ylibx'] + $data['znjy'] + $data['jxjy']
  133. + $data['zfdklx'] + $data['zfzj'] + $data['sylr'] + $data['donations'] + $data['syjkbx'] + $data['ysylbx'] + $data['other'];
  134. $tax1 = $ljkc*0.03 - 0;
  135. $tax2 = $ljkc*0.1 - 2520;
  136. $tax3 = $ljkc*0.2 - 16920;
  137. $tax4 = $ljkc*0.25 - 31920;
  138. $tax5 = $ljkc*0.3 - 52920;
  139. $tax6 = $ljkc*0.35 - 85920;
  140. $tax7 = $ljkc*0.45 - 181920;
  141. $tax = round(max([$tax1,$tax2,$tax3,$tax4,$tax5,$tax6,$tax7]),2);
  142. $tax = round($tax - $tax*$data['min_tax'],2);
  143. }
  144. $newdata = [
  145. 'sr' => $data['sr'] + $ysmoney,
  146. 'jc' => $data['jc'] + $jc,
  147. 'nj' => $data['nj'] + $sbgjj['qynjp'],
  148. 'gjj' => $data['gjj'] + $sbgjj['gjjp'],
  149. 'ylobx' => $data['ylobx'] + $sbgjj['ylap'],
  150. 'sybx' => $data['sybx'] + $sbgjj['syep'],
  151. 'ylibx' => $data['ylibx'] + $sbgjj['ylip'],
  152. 'znjy' => $data['znjy'],
  153. 'jxjy' => $data['jxjy'],
  154. 'zfdklx' => $data['zfdklx'],
  155. 'zfzj' => $data['zfzj'],
  156. 'sylr' => $data['sylr'],
  157. 'donations' => $data['donations'] + $user['donations'],
  158. 'ykjse' => $data['ykjse'],
  159. 'syjkbx' => $user['syjkbx'],
  160. 'ysylbx' => $user['ysylbx'],
  161. 'other' => $user['other'],
  162. 'min_tax' => $user['min_tax']
  163. ];
  164. if($special){
  165. $newdata['znjy'] += $special['znjy'];
  166. $newdata['jxjy'] += $special['jxjy'];
  167. $newdata['zfdklx'] += $special['zfdklx'];
  168. $newdata['zfzj'] += $special['zfzj'];
  169. $newdata['sylr'] += $special['sylr'];
  170. }
  171. if($companyId == 2){ // 新华算本月税
  172. // 累计预扣预缴应纳税所得额
  173. $ljkc = $newdata['jc'] + $newdata['nj'] + $newdata['gjj'] + $newdata['ylobx'] + $newdata['sybx'] + $newdata['ylibx'] + $newdata['znjy'] + $newdata['jxjy']
  174. + $newdata['zfdklx'] + $newdata['zfzj'] + $newdata['sylr'] + $newdata['donations'] + $newdata['syjkbx'] + $newdata['ysylbx'] + $newdata['other'];
  175. $tax1 = $ljkc*0.03 - 0;
  176. $tax2 = $ljkc*0.1 - 2520;
  177. $tax3 = $ljkc*0.2 - 16920;
  178. $tax4 = $ljkc*0.25 - 31920;
  179. $tax5 = $ljkc*0.3 - 52920;
  180. $tax6 = $ljkc*0.35 - 85920;
  181. $tax7 = $ljkc*0.45 - 181920;
  182. $tax = round(max([$tax1,$tax2,$tax3,$tax4,$tax5,$tax6,$tax7]),2);
  183. $tax = round($tax - $tax*$newdata['min_tax'],2);
  184. }
  185. $tax = $tax>0?$tax:0;
  186. $newdata['ykjse'] += $tax;
  187. return ['tax'=>$tax,'data' => $newdata];
  188. }
  189. private function getButie($userId,$companyId,$post,$age,$gender,$types,$user,$month){
  190. $data = [];
  191. $data[] = [
  192. 'name' => '其他补贴',
  193. 'money' => $user['qtbt']
  194. ];
  195. foreach ($types as $val){
  196. switch ($val){
  197. case '1': // 防暑降温费
  198. $var = $this->getBtByName('fsjwf',$userId,$post,$age,$gender,$user,$month);
  199. $data[] = [
  200. 'name' => '防暑降温费',
  201. 'money' => $var
  202. ];
  203. break;
  204. case '2': // 禁食猪肉补贴
  205. $var = $this->getBtByName('jszrbt',$userId,$post,$age,$gender,$user,$month);
  206. $data[] = [
  207. 'name' => '禁食猪肉补贴',
  208. 'money' => $var
  209. ];
  210. break;
  211. }
  212. }
  213. $total = 0;
  214. foreach ($data as $k=>$v){
  215. $total += $v['money'];
  216. }
  217. return ['total'=>$total,'btjson' => json_encode($data)];
  218. }
  219. private function getBtByName($name,$userId,$post,$age,$gender,$user,$month){
  220. $info = Db::name('user_subsidies')->where('name',$name)->find();
  221. if(!$info){
  222. return 0;
  223. }
  224. $val = 0;
  225. if($info['type'] == 1){
  226. $val = $info['money'];
  227. } else {
  228. $dd = json_encode($info['values'],true);
  229. foreach ($dd as $k=>$v){
  230. if($v['id'] == $post){
  231. if($v['money2']){
  232. if($age < 15){
  233. $val = $info['money1'];
  234. } else if ($age >= 15 && $age < 25){
  235. $val = $info['money2'];
  236. }else{
  237. $val = $info['money3'];
  238. }
  239. }else{
  240. $val = $info['money1'];
  241. }
  242. break;
  243. }
  244. }
  245. }
  246. // 检查用户是否有补贴
  247. $btjson = $user['btjson'];
  248. if($btjson){
  249. $btjson = json_decode($btjson,true);
  250. foreach ($btjson as $k=>$v){
  251. if($v['name'] == $name){
  252. switch ($v['type']){
  253. case "1":
  254. if($v['end']&&$v['end'] >= $month){
  255. $val = (int) $v['money'] > 0 ?$v['money']:0;
  256. }
  257. break;
  258. case "2":
  259. if($v['end']&&$v['end'] < $month){
  260. $val = 0;
  261. }
  262. break;
  263. case "3":
  264. if($v['end']&&$v['end'] == 0){
  265. $val = 0;
  266. }
  267. break;
  268. }
  269. }
  270. }
  271. }
  272. return $val;
  273. }
  274. // 社保公积金
  275. private function getSbGjj($userId,$companyId,$bls){
  276. $base = Db::name('user_base')->where('user_id',$userId)->where('company_id',$companyId)->find();
  277. $data = [
  278. 'qynj' => 0,
  279. 'gjj' => 0,
  280. 'sb' => 0,
  281. 'syyl' => 0,
  282. 'gssy' => 0,
  283. 'ylnj' => 0,
  284. 'ylap' => 0,
  285. 'ylac' => 0,
  286. 'syep' => 0,
  287. 'syec' => 0,
  288. 'ylip' => 0,
  289. 'ylic' => 0,
  290. 'gsc' => 0,
  291. 'syuc' => 0,
  292. 'ptotal' => 0,
  293. 'ctotal' => 0,
  294. 'sbtotal' => 0,
  295. 'qynjp' => 0,
  296. 'qynjc' => 0,
  297. 'qynjtotal' => 0,
  298. 'gjjp' => 0,
  299. 'gjjc' => 0,
  300. 'gjjtotal' => 0,
  301. ];
  302. if(!$base){
  303. return $data;
  304. }
  305. $data['qynj'] = $base['qynj'];
  306. $data['gjj'] = $base['gjj'];
  307. $data['sb'] = $base['sb'];
  308. $data['syyl'] = $base['syyl'];
  309. $data['gssy'] = $base['gssy'];
  310. $data['ylnj'] = $base['ylnj'];
  311. // 计算各项扣除
  312. foreach ($bls as $key=>$val){
  313. if($val['company_id'] == $base['company_id']){
  314. switch ($val['name']){
  315. case 'gjj':
  316. $data['gjjp'] = calculate_money($base['gjj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  317. $data['gjjc'] = calculate_money($base['gjj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  318. $data['gjjtotal'] = round($data['gjjp'] + $data['gjjc'],2);
  319. break;
  320. case 'qynj':
  321. if($val['company_id'] == 1){
  322. $data['qynjp'] = calculate_money($base['ylnj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  323. $data['qynjc'] = calculate_money($base['ylnj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  324. } else {
  325. $data['qynjp'] = calculate_money($base['qynj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  326. $data['qynjc'] = calculate_money($base['qynj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  327. }
  328. $data['qynjtotal'] = round($data['qynjp'] + $data['qynjc'],2);
  329. break;
  330. case 'yla':
  331. if($val['company_id'] == 1){
  332. $data['sylap'] = calculate_money($base['ylnj'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  333. $data['sylac'] = calculate_money($base['ylnj'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  334. } else {
  335. $data['sylap'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  336. $data['sylac'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  337. }
  338. $data['ptotal'] += $data['sylap'];
  339. $data['ctotal'] += $data['sylac'];
  340. break;
  341. case 'sye':
  342. if($val['company_id'] == 1){
  343. $data['syep'] = calculate_money($base['gssy'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  344. $data['syec'] = calculate_money($base['gssy'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  345. } else {
  346. $data['syep'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  347. $data['syec'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  348. }
  349. $data['ptotal'] += $data['syep'];
  350. $data['ctotal'] += $data['syec'];
  351. break;
  352. case 'yli':
  353. if($val['company_id'] == 1){
  354. $data['ylip'] = calculate_money($base['syyl'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  355. $data['ylic'] = calculate_money($base['syyl'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  356. } else {
  357. $data['ylip'] = calculate_money($base['sb'],$val['p_bl'],$val['p_bl_type'],$val['p_bl_extra'],$val['p_free_bl']);
  358. $data['ylic'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  359. }
  360. $data['ptotal'] += $data['ylip'];
  361. $data['ctotal'] += $data['ylic'];
  362. break;
  363. case 'gs':
  364. if($val['company_id'] == 1){
  365. $data['gsc'] = calculate_money($base['gssy'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  366. } else {
  367. $data['gsc'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  368. }
  369. $data['ctotal'] += $data['gsc'];
  370. break;
  371. case 'syu':
  372. if($val['company_id'] == 1){
  373. $data['syuc'] = calculate_money($base['gssy'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  374. } else {
  375. $data['syuc'] = calculate_money($base['sb'],$val['c_bl'],$val['c_bl_type'],$val['c_bl_extra'],$val['c_free_bl']);
  376. }
  377. $data['ctotal'] += $data['syuc'];
  378. break;
  379. }
  380. }
  381. }
  382. $data['ctotal'] = round($data['ctotal'],2);
  383. $data['ptotal'] = round($data['ptotal'],2);
  384. $data['sbtotal'] = $data['ptotal'] + $data['ctotal'];
  385. return $data;
  386. }
  387. // 获取用户基本工资
  388. private function getUserBase($userId){
  389. $uinfo = Db::name('user_info')->where('user_id',$userId)->field('pwlevel,psid,join_time,gender,company_id,type')->find();
  390. $post = Db::name('post_salary')->where('id',$uinfo['psid'])->find();
  391. $data['post_salary'] = $post?$post['money']:0;
  392. $data['month_performance'] = $post?$post['money']:0;
  393. $wages = Db::name('pay_wages')->where('company_id',$uinfo['company_id'])->where('type',$uinfo['type'])->value('content');
  394. $data['wages'] = 0;
  395. if($wages){
  396. $wages = json_decode($wages,true);
  397. foreach ($wages as $kk=>$vv){
  398. if($vv['level'] == $uinfo['pwlevel']){
  399. $data['wages'] = $vv['money'];
  400. break;
  401. }
  402. }
  403. }
  404. $data['age'] = 0;
  405. $curYear = date('Y');
  406. if($uinfo['join_time']){
  407. $joinYear = date('Y',strtotime($uinfo['join_time']));
  408. }
  409. $data['age'] = $curYear - $joinYear;
  410. $data['gender'] = $uinfo['gender'];
  411. $data['type'] = $uinfo['type'];
  412. $data['pwlevel'] = $uinfo['pwlevel'];
  413. $data['psid'] = $uinfo['psid'];
  414. return $data;
  415. }
  416. public function info($id){
  417. $info = $this->where('id',$id)->find();
  418. if(!$info){
  419. HelpHander::error('数据不存在');
  420. }
  421. $data = $info->toArray();
  422. $data['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  423. return $data;
  424. }
  425. public function lists($page,$size,$name,$srid){
  426. if($name != ''){
  427. $map[] = ['ui.name','like','%'.$name.'%'];
  428. }
  429. $map[] = ['ur.salary_record_id','=',$srid];
  430. $lists = Db::name('user_salary_record')
  431. ->alias('ur')
  432. ->join('user_info ui','ui.user_id = ur.user_id')
  433. ->where($map)
  434. ->field('ur.*,ui.name')
  435. ->page($page,$size)
  436. ->order('ui.sorts2 asc,ur.id desc')
  437. ->select();
  438. foreach ($lists as $k=>$v){
  439. $lists[$k]['sbgjj'] = json_decode($v['sbgjj'],true);
  440. $btjson = json_decode($v['btjson'],true);
  441. $lists[$k]['btjson'] = $btjson?$btjson:[];
  442. $bttotal = 0;
  443. if($btjson){
  444. foreach ($btjson as $kk=>$vv){
  445. $bttotal += $vv['money'];
  446. }
  447. }
  448. $lists[$k]['bttotal'] = round($bttotal,2);
  449. $lists[$k]['ptotal'] = round($lists[$k]['sbgjj']['ptotal'],2);
  450. }
  451. $total = Db::name('user_salary_record')
  452. ->alias('ur')
  453. ->join('user_info ui','ui.user_id = ur.user_id')
  454. ->where($map)->count();
  455. $data = [
  456. 'total' => $total,
  457. 'list' => $lists?$lists:[]
  458. ];
  459. return $data;
  460. }
  461. }