PartyFee.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 PartyFee extends Model
  8. {
  9. public function add(){
  10. $year = input('year','','trim');
  11. if(!$year){
  12. HelpHander::error('未选择年份');
  13. }
  14. // 检查该年份是否已生成过党费
  15. $ret = Db::name('party_fee')->where('year',$year)->find();
  16. if($ret){
  17. HelpHander::error('该年份已生成过党费');
  18. }
  19. $recordIds = Db::name('salary_record')->where('month','like',$year.'-%')->where('type','in',[1,2])->where('status',2)->where('del',0)->column('id');
  20. if(!$recordIds){
  21. HelpHander::error('该年份工资还没有已发放的工资');
  22. }
  23. $duids = Db::name('user_info')->where('political','in',['中共党员','中共预备党员'])->column('user_id'); // 党员
  24. $map[] = ['salary_record_id','in',$recordIds];
  25. if($duids){
  26. $map[] = ['user_id','in',$duids];
  27. }else{
  28. $map[] = ['user_id','=',0];
  29. }
  30. $userIds = Db::name('salary_record_log')->where($map)->column('user_id');
  31. if(!$userIds){
  32. HelpHander::error('没有可生成党费的人员或未发放工资');
  33. }
  34. $userIds = array_unique($userIds);
  35. $curTime = date('Y-m-d H:i:s');
  36. Db::startTrans();
  37. try{
  38. $btids = Db::name('user_subsidies')->where('prop',2)->column('id');
  39. $btids = $btids?$btids:[];
  40. $arr = [];
  41. foreach ($userIds as $uid){
  42. $d = [
  43. 'year' => $year,
  44. 'user_id' => $uid,
  45. 'should_total' => 0,
  46. 'yjx' => 0,
  47. 'tax' => 0,
  48. 'sbgjj' => 0,
  49. 'bt' => 0,
  50. 'cardinal' => 0,
  51. 'bl' => 0,
  52. 'fee' => 0,
  53. 'create_time' => $curTime
  54. ];
  55. $logs = Db::name('salary_record_log')->where('salary_record_id','in',$recordIds)->where('gwgz','>',0)->where('user_id',$uid)->select();
  56. $logs = $logs?$logs:[];
  57. foreach ($logs as $kk=>$vv){
  58. $d['should_total'] += $vv['should_total'];
  59. $d['yjx'] += $vv['yjx'];
  60. $d['tax'] += $vv['tax'];
  61. $d['sbgjj'] += $vv['sbgjj'];
  62. $btjson = $vv['btjson']?json_decode($vv['btjson'],true):[];
  63. $bt = 0;
  64. foreach ($btjson as $bk=>$bv){
  65. if($bv['cate'] == 1 && in_array($bv['id'],$btids)){
  66. $bt += floatval($bv['money']);
  67. }
  68. }
  69. $d['bt'] += $bt;
  70. }
  71. $d['should_total'] = round($d['should_total'],2);
  72. $d['yjx'] = round($d['yjx'],2);
  73. $d['tax'] = round($d['tax'],2);
  74. $d['sbgjj'] = round($d['sbgjj'],2);
  75. $d['bt'] = round($d['bt'],2);
  76. $d['cardinal'] = round($d['should_total'] - $d['yjx'] - $d['tax'] - $d['sbgjj'] - $d['bt'],2);
  77. $d['cardinal'] = $d['cardinal'] > 0?$d['cardinal']:0;
  78. $bl = 0;
  79. if($d['cardinal'] > 0){
  80. $cardinal = $d['cardinal']/count($logs);
  81. if($cardinal <= 3000){
  82. $bl = 0.005;
  83. }else if($cardinal > 3000 && $cardinal <= 5000){
  84. $bl = 0.01;
  85. }else if($cardinal > 5000 && $cardinal <= 10000){
  86. $bl = 0.015;
  87. }else{
  88. $bl = 0.02;
  89. }
  90. }
  91. $d['bl'] = $bl;
  92. $d['fee'] = round($d['cardinal']*$bl,2);
  93. $arr[] = $d;
  94. if(count($arr) == 30){
  95. $ret = Db::name('party_fee')->insertAll($arr);
  96. if(!$ret || count($arr) != $ret){
  97. \exception('保存失败');
  98. }
  99. $arr = [];
  100. }
  101. }
  102. if(count($arr) > 0){
  103. $ret = Db::name('party_fee')->insertAll($arr);
  104. if(!$ret || count($arr) != $ret){
  105. \exception('保存失败');
  106. }
  107. }
  108. Db::commit();
  109. }catch (Exception $e){
  110. Db::rollback();
  111. trace($e->getMessage());
  112. HelpHander::error('操作失败'.$e->getMessage());
  113. }
  114. return true;
  115. }
  116. public function oldadd(){
  117. $month = input('month','','trim');
  118. $year = input('year','','trim');
  119. $type = input('type/d',0);
  120. if(!in_array($type,[1,2])){
  121. HelpHander::error('参数错误');
  122. }
  123. if($type == 1 && !$month){
  124. HelpHander::error('未选择月份');
  125. }
  126. if($type == 2 && !$year){
  127. HelpHander::error('未选择年份');
  128. }
  129. if($type == 1){
  130. $slists = Db::name('salary_record')->where('month',$month)->where('type','in',[1,2])->where('status',2)->where('del',0)->select();
  131. if(!$slists){
  132. HelpHander::error('该月份没有已发放的工资');
  133. }
  134. }else{
  135. $slists = Db::name('salary_record')->where('month','like',$year.'-%')->where('type','in',[1,2])->where('status',2)->where('del',0)->select();
  136. if(!$slists){
  137. HelpHander::error('该年份工资还没有已发放的工资');
  138. }
  139. }
  140. $duids = Db::name('user_info')->where('political','in',['中共党员','中共预备党员'])->column('user_id'); // 党员
  141. Db::startTrans();
  142. try{
  143. foreach ($slists as $kk=>$vv){
  144. $map = [];
  145. $uids = Db::name('party_fee')->where('month',$vv['month'])->column('user_id');
  146. $map[] = ['gwgz','>',0];
  147. $map[] = ['salary_record_id','=',$vv['id']];
  148. if($uids){
  149. $map[] = ['user_id','not in',$uids];
  150. }
  151. if($duids){
  152. $map[] = ['user_id','in',$duids];
  153. }
  154. $lists = Db::name('salary_record_log')->where($map)->select();
  155. if($lists){
  156. $arr = [];
  157. $curTime = date('Y-m-d H:i:s');
  158. foreach ($lists as $k=>$v){
  159. $btjson = $v['btjson']?json_decode($v['btjson'],true):[];
  160. $kcmoney = 0;
  161. foreach ($btjson as $kkk=>$vvv){
  162. $lists[$k]['subsidies'.$vvv['id']] = $vvv['money'];
  163. if($vvv['cate'] == 2){ // 扣除项
  164. $kcmoney += $vvv['money'];
  165. }
  166. }
  167. $dktotal = round($v['sbgjj'] + $v['tax'] + $kcmoney,2);
  168. $dktotal = $dktotal>0?$dktotal:0;
  169. $cardinal = round($v['gwgz'] + $v['xjgz'] + $v['jcjx'] - $dktotal,2);
  170. $cardinal = $cardinal>0?$cardinal:0;
  171. $bl = 0;
  172. if($cardinal <= 3000){
  173. $bl = 0.005;
  174. }else if($cardinal > 3000 && $cardinal <= 5000){
  175. $bl = 0.01;
  176. }else if($cardinal > 5000 && $cardinal <= 10000){
  177. $bl = 0.015;
  178. }else{
  179. $bl = 0.02;
  180. }
  181. $dd = [
  182. 'user_id' => $v['user_id'],
  183. 'month' => $vv['month'],
  184. 'gwgz' => $v['gwgz'],
  185. 'xjgz' => $v['xjgz'],
  186. 'jcjx' => $v['jcjx'],
  187. 'dktotal' => $dktotal,
  188. 'cardinal' => $cardinal,
  189. 'bl' => $bl,
  190. 'fee' => round($cardinal*$bl,2),
  191. 'create_time' => $curTime
  192. ];
  193. $arr[] = $dd;
  194. if(count($arr) == 30){
  195. $ret = Db::name('party_fee')->insertAll($arr);
  196. if(!$ret || count($arr) != $ret){
  197. \exception('保存失败');
  198. }
  199. $arr = [];
  200. }
  201. }
  202. if(count($arr) > 0){
  203. $ret = Db::name('party_fee')->insertAll($arr);
  204. if(!$ret || count($arr) != $ret){
  205. \exception('保存失败');
  206. }
  207. }
  208. }
  209. }
  210. Db::commit();
  211. }catch (Exception $e){
  212. Db::rollback();
  213. trace($e->getMessage());
  214. HelpHander::error('操作失败'.$e->getMessage());
  215. }
  216. return true;
  217. }
  218. public function info($id){
  219. $info = $this->where('id',$id)->where('del',0)->find();
  220. if(!$info){
  221. HelpHander::error('数据不存在');
  222. }
  223. $data = $info->toArray();
  224. return $data;
  225. }
  226. public function del($id){
  227. $ret = $this->delete($id);
  228. if(!$ret){
  229. HelpHander::error('删除失败');
  230. }
  231. return true;
  232. }
  233. public function lists($title,$year){
  234. $map[] = ['a.year','=',$year];
  235. if($title){
  236. $map[] = ['b.name','like','%'.$title.'%'];
  237. }
  238. $lists = Db::name('party_fee')
  239. ->alias('a')
  240. ->join('user_info b','a.user_id = b.user_id')
  241. ->where($map)
  242. ->order('b.sorts3 asc,b.user_id asc')
  243. ->field('a.*,b.name')
  244. ->select();
  245. $lists = $lists?$lists:[];
  246. $all = [
  247. 'name' => '合计',
  248. 'year' => '',
  249. 'should_total' => 0,
  250. 'yjx' => 0,
  251. 'tax' => 0,
  252. 'sbgjj' => 0,
  253. 'bt' => 0,
  254. 'cardinal' => 0,
  255. 'bl' => '',
  256. 'fee' => 0
  257. ];
  258. foreach ($lists as $k=>$v){
  259. $all['should_total'] += $v['should_total'];
  260. $all['yjx'] += $v['yjx'];
  261. $all['tax'] += $v['tax'];
  262. $all['sbgjj'] += $v['sbgjj'];
  263. $all['bt'] += $v['bt'];
  264. $all['cardinal'] += $v['cardinal'];
  265. $all['fee'] += $v['fee'];
  266. }
  267. $all['should_total'] = round($all['should_total'],2);
  268. $all['yjx'] = round($all['yjx'],2);
  269. $all['tax'] = round($all['tax'],2);
  270. $all['sbgjj'] = round($all['sbgjj'],2);
  271. $all['bt'] = round($all['bt'],2);
  272. $all['cardinal'] = round($all['cardinal'],2);
  273. $all['fee'] = round($all['fee'],2);
  274. $lists[] = $all;
  275. $columns = [
  276. ["title" => "姓名","key" => "name"],
  277. ["title" => "应发工资","key" => "shouldTotal"],
  278. ["title" => "奖励性绩效","key" => "yjx"],
  279. ["title" => "个人所得税","key" => "tax"],
  280. ["title" => "五险二金","key" => "sbgjj"],
  281. ["title" => "津贴补贴和改革性补贴","key" => "bt"],
  282. ["title" => "党费基数","key" => "cardinal"],
  283. ["title" => "党费","key" => "fee"],
  284. ];
  285. $data = [
  286. 'columns' => $columns,
  287. 'list' => $lists
  288. ];
  289. return $data;
  290. }
  291. public function ulists($userId,$year){
  292. $map[] = ['a.year','=',$year];
  293. $map[] = ['a.user_id','=',$userId];
  294. $lists = Db::name('party_fee')
  295. ->alias('a')
  296. ->join('user_info b','a.user_id = b.user_id')
  297. ->where($map)
  298. ->order('b.sorts3 asc,b.user_id asc')
  299. ->field('a.*,b.name')
  300. ->select();
  301. $lists = $lists?$lists:[];
  302. $columns = [
  303. // ["title" => "姓名","key" => "name"],
  304. ["title" => "应发工资","key" => "shouldTotal"],
  305. ["title" => "奖励性绩效","key" => "yjx"],
  306. ["title" => "个人所得税","key" => "tax"],
  307. ["title" => "五险二金","key" => "sbgjj"],
  308. ["title" => "津贴补贴和改革性补贴","key" => "bt"],
  309. ["title" => "党费基数","key" => "cardinal"],
  310. ["title" => "党费","key" => "fee"],
  311. ];
  312. $data = [
  313. 'columns' => $columns,
  314. 'list' => $lists
  315. ];
  316. return $data;
  317. }
  318. }