Timer.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. namespace app\api\controller;
  3. use PhpOffice\PhpWord\TemplateProcessor;
  4. use think\Controller;
  5. use think\Db;
  6. use think\Exception;
  7. use tools\Qxsms;
  8. class Timer extends Controller
  9. {
  10. public function apply(){ // 流程超时提醒
  11. $apps = Db::name('approval')->where('max_day','>',0)->field('id,max_day')->select();
  12. $curDate = date('Y-m-d');
  13. $cts = strtotime($curDate.' 00:00:00');
  14. $cte = strtotime($curDate.' 00:00:00')+24*60*60;
  15. if($apps){
  16. foreach ($apps as $v){
  17. $st = date('Y-m-d H:i:s',$cts - $v['max_day']*24*60*60);
  18. $et = date('Y-m-d H:i:s',$cte - $v['max_day']*24*60*60);
  19. $applys = Db::name('apply')
  20. ->where('status',1)
  21. ->where('approval_id',$v['id'])
  22. ->where('create_time','>=',$st)
  23. ->where('create_time','<',$et)
  24. ->field('id,user_id,nodeid,org_id')
  25. ->select();
  26. foreach ($applys as $key=>$val){
  27. model('Apply')->remindAuditCommon($val);
  28. }
  29. }
  30. }
  31. }
  32. // 获取今年节假日
  33. public function holiday(){
  34. $url = "http://timor.tech/api/holiday/year/".date('Y');
  35. $content = file_get_contents($url);
  36. if($content){
  37. $data = json_decode($content,true);
  38. if(!empty($data['holiday'])){
  39. $holiday = $data['holiday'];
  40. foreach ($holiday as $k=>$v){
  41. $d = [
  42. 'day' => $v['date'],
  43. 'holiday' => $v['holiday']?1:0,
  44. 'name' => $v['name'],
  45. 'target' => isset($v['target'])?$v['target']:''
  46. ];
  47. $day = Db::name('holiday')->where('day',$d['day'])->find();
  48. if(!$day){
  49. Db::name('holiday')->insert($d);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. // 房产合同到期提醒
  56. public function houseContract(){
  57. $curDay = date('Y-m-d');
  58. // 先获取能接受提醒的人
  59. $users = Db::name('house_auth')
  60. ->where('type',1)
  61. ->where('remind',1)
  62. ->field('user_id,ids')
  63. ->select();
  64. if(!$users){
  65. exit();
  66. }
  67. foreach ($users as $k=>$v){
  68. $users[$k]['ids'] = $v['ids']?explode(',',$v['ids']):[];
  69. }
  70. $lists = Db::name('house_contract')
  71. ->where('del',0)
  72. ->where('status',1)
  73. ->where('remind',1)
  74. ->where('remind_date',$curDay)
  75. ->field('id,sn,org_id')
  76. ->select();
  77. if(!$lists){
  78. exit();
  79. }
  80. foreach ($lists as $k=>$v){
  81. $levels = Db::name('house_contract_relation')
  82. ->alias('hcr')
  83. ->join('house h','h.id = hcr.house_id')
  84. ->where('hcr.contract_id',$v['id'])
  85. ->column('level_id');
  86. $levels = $levels?$levels:[];
  87. foreach ($users as $kk=>$vv){
  88. $arr = array_intersect($levels,$vv['ids']);
  89. if($arr){
  90. model('Message')->add(3,$v['id'],9,$vv['user_id'],$v['org_id'],'合同['.$v['sn'].']即将到期,请及时续约');
  91. }
  92. }
  93. }
  94. }
  95. // 房产标签到期提醒
  96. public function houseMark(){
  97. $curDay = date('Y-m-d');
  98. // 先获取能接受提醒的人
  99. $users = Db::name('house_auth')
  100. ->where('type',1)
  101. ->where('remind',1)
  102. ->field('user_id,ids')
  103. ->select();
  104. if(!$users){
  105. exit();
  106. }
  107. foreach ($users as $k=>$v){
  108. $users[$k]['ids'] = $v['ids']?explode(',',$v['ids']):[];
  109. }
  110. $lists = Db::name('house_mark')
  111. ->alias('hm')
  112. ->join('house h','h.id = hm.house_id')
  113. ->where('h.del',0)
  114. ->where('h.enable',1)
  115. ->where('hm.type',1)
  116. ->where('hm.end_time',$curDay)
  117. ->field('hm.id,h.level_id,hm.content,hm.org_id')
  118. ->select();
  119. if(!$lists){
  120. exit();
  121. }
  122. foreach ($lists as $k=>$v){
  123. foreach ($users as $kk=>$vv){
  124. if(in_array($v['level_id'],$vv['ids'])){
  125. model('Message')->add(3,$v['id'],10,$vv['user_id'],$v['org_id'],$v['content']);
  126. }
  127. }
  128. }
  129. }
  130. // 支付单提醒
  131. public function housePay(){
  132. $curDay = date('Y-m-d');
  133. // 先获取能接受提醒的人
  134. $users = Db::name('house_auth')
  135. ->where('type',1)
  136. ->where('remind',1)
  137. ->field('user_id,ids')
  138. ->select();
  139. if(!$users){
  140. exit();
  141. }
  142. foreach ($users as $k=>$v){
  143. $users[$k]['ids'] = $v['ids']?explode(',',$v['ids']):[];
  144. }
  145. $lists = Db::name('house_contract_pay')
  146. ->alias('hcp')
  147. ->join('house_contract hc','hc.id = hcp.contract_id')
  148. ->where('hcp.del',0)
  149. ->where('hcp.status',0)
  150. ->where('hc.del',0)
  151. ->where('hc.status',0)
  152. ->where('hcp.remind',1)
  153. ->where('hcp.remind_date',$curDay)
  154. ->field('hcp.id,hcp.contract_id,hc.org_id')
  155. ->select();
  156. if(!$lists){
  157. exit();
  158. }
  159. foreach ($lists as $k=>$v){
  160. $levels = Db::name('house_contract_relation')
  161. ->alias('hcr')
  162. ->join('house h','h.id = hcr.house_id')
  163. ->where('hcr.contract_id',$v['id'])
  164. ->column('level_id');
  165. $levels = $levels?$levels:[];
  166. foreach ($users as $kk=>$vv){
  167. $arr = array_intersect($levels,$vv['ids']);
  168. if($arr){
  169. model('Message')->add(3,$v['contract_id'],9,$vv['user_id'],$v['org_id'],'合同['.$v['sn'].']缴费即将到期,请及时处理');
  170. }
  171. }
  172. }
  173. }
  174. // 房产合同到期释放房产
  175. public function houseRelease(){
  176. $curDay = date('Y-m-d');
  177. $lists = Db::name('house_contract')
  178. ->where('end_time','<',$curDay)
  179. ->where('del',0)
  180. ->where('release',1)
  181. ->where('is_released',0)
  182. ->where('status',1)
  183. ->field('id')
  184. ->select();
  185. if($lists){
  186. foreach ($lists as $k=>$v){
  187. try{
  188. Db::name('house_contract')->where('id',$v['id'])->update(['is_released'=>1]);
  189. $houses = Db::name('house_contract_relation')->where('contract_id',$v['id'])->column('house_id');
  190. if($houses){
  191. $dd = [
  192. 'cur_lessee_id' => 0,
  193. 'cur_contract_id' => 0
  194. ];
  195. Db::name('house')->where('id','in',$houses)->update($dd);
  196. }
  197. Db::commit();
  198. }catch (Exception $e){
  199. trace($e->getMessage());
  200. Db::rollback();
  201. halt($e->getMessage());
  202. }
  203. }
  204. }
  205. }
  206. // 发放员工年假
  207. public function annual(){
  208. //年假统一为:工作满1年不满10年:5天;
  209. // 满10年不满20年:10天;
  210. // 满20年:15天。
  211. $str = date('m-d');
  212. if($str != '01-01'){ // 每年的1月1日执行
  213. exit();
  214. }
  215. $year = date('Y');
  216. // 每年的1月1日执行
  217. $users = Db::name('user')
  218. ->alias('u')
  219. ->join('user_info ui','u.id = ui.user_id')
  220. ->where('u.del',0)
  221. ->where('u.enable',1)
  222. ->where('ui.is_working','in',[1,2])
  223. ->where('ui.work_date','exp','is not null')
  224. ->where('ui.work_date','<>','')
  225. // ->where('ui.annual_year',2023)
  226. ->where('ui.annual_year','<>',$year)
  227. ->field('u.id,ui.name,ui.work_date')
  228. ->select();
  229. foreach ($users as $k=>$v){
  230. $age = calculate_age($v['work_date']);
  231. $day = 0;
  232. if($age >= 1 && $age < 10){
  233. $day = 5;
  234. }else if($age >= 10 && $age < 20){
  235. $day = 10;
  236. }else if($age >= 20){
  237. $day = 15;
  238. }
  239. $users[$k]['age'] = $age;
  240. $users[$k]['day'] = $day;
  241. $ret = Db::name('user_info')->where('user_id',$v['id'])->update([
  242. 'annual_year' => $year,
  243. 'annual_leave' => $day,
  244. 'update_time' => date('Y-m-d H:i:s')
  245. ]);
  246. if(!$ret){
  247. trace('年假更新失败:'.$v['id']);
  248. }
  249. }
  250. halt($users);
  251. }
  252. // 每月1号保存用户档案
  253. public function historyUser(){
  254. $company = Db::name('company')->select();
  255. $month = date('Y年m月');
  256. foreach ($company as $k=>$v){
  257. $data = [
  258. 'title' => $month.'-'.$v['title'].'-档案',
  259. 'org_id' => $v['org_id'],
  260. 'company_id' => $v['id'],
  261. 'remark' => ''
  262. ];
  263. $ret = model('UserHistory')->add($data);
  264. var_dump($ret);
  265. }
  266. $data = [
  267. 'title' => $month.'-其他-档案',
  268. 'org_id' => 4,
  269. 'company_id' => 0,
  270. 'remark' => ''
  271. ];
  272. $ret = model('UserHistory')->add($data);
  273. var_dump($ret);
  274. }
  275. // 下载印花税word文档
  276. public function stampword(){
  277. $ids = input('ids','','trim');
  278. if(!$ids){
  279. exit();
  280. }
  281. $templateProcessor = new TemplateProcessor(__DIR__.'/../../../public/word/stamp_tmp.docx');
  282. $arr = [];
  283. $ids = explode(',',$ids);
  284. foreach ($ids as $k=>$v){
  285. $ret = model('Contract')->info($v);
  286. if($ret){
  287. $arr[] = [
  288. 'programSn' => $ret['program_sn'],
  289. 'company' => $ret['company'],
  290. 'fhUserName' => $ret['fh_user_name'],
  291. 'agent' => $ret['agent'],
  292. 'name1' => '应税凭证名称',
  293. 'title' => $ret['title'],
  294. 'name2' => '对方单位名称',
  295. 'company2' => $ret['company2'],
  296. 'name3' => '签订日期',
  297. 'handleDate' => $ret['handle_date'],
  298. 'name4' => '适用印花税税目',
  299. 'stampTitle' => $ret['stamp']['title'],
  300. 'name5' => '计税金额',
  301. 'money' => $ret['money'],
  302. 'name6' => '适用税率',
  303. 'stampBl' => $ret['stamp']['bl'].'‰',
  304. 'name7' => '本期应纳税额',
  305. 'sdMoney' => $ret['sd_money'],
  306. ];
  307. }
  308. }
  309. // $arr = [
  310. // [
  311. // 'programSn' => 'N0001',
  312. // 'company' => '中心',
  313. // 'fhUserName' => '张三',
  314. // 'agent' => '李四',
  315. // 'name1' => '应税凭证名称',
  316. // 'title' => '111',
  317. // 'name2' => '对方单位名称',
  318. // 'company2' => '111',
  319. // 'name3' => '签订日期',
  320. // 'handleDate' => '111',
  321. // 'name4' => '适用印花税税目',
  322. // 'stampTitle' => '111',
  323. // 'name5' => '计税金额',
  324. // 'money' => '111',
  325. // 'name6' => '适用税率',
  326. // 'stampBl' => '111',
  327. // 'name7' => '本期应纳税额',
  328. // 'sdMoney' => '111',
  329. // ],
  330. // [
  331. // 'programSn' => 'N0002',
  332. // 'company' => '中心',
  333. // 'fhUserName' => '张三',
  334. // 'agent' => '李四',
  335. // 'name1' => '应税凭证名称',
  336. // 'title' => '111',
  337. // 'name2' => '对方单位名称',
  338. // 'company2' => '111',
  339. // 'name3' => '签订日期',
  340. // 'handleDate' => '111',
  341. // 'name4' => '适用印花税税目',
  342. // 'stampTitle' => '111',
  343. // 'name5' => '计税金额',
  344. // 'money' => '111',
  345. // 'name6' => '适用税率',
  346. // 'stampBl' => '111',
  347. // 'name7' => '本期应纳税额',
  348. // 'sdMoney' => '111',
  349. // ],
  350. // ];
  351. $templateProcessor->cloneBlock('block_name', 0, true, false, $arr);
  352. // 将 word 文档保存至 你设置的路径
  353. $file_dir = './word/zs' . time() . ".docx";
  354. $templateProcessor->saveAs($file_dir);
  355. $file = fopen ( $file_dir, "rb" );//以只读和二进制模式打开文件
  356. Header ( "Content-type: application/octet-stream" ); //告诉浏览器这是一个文件流格式的文件
  357. Header ( "Accept-Ranges: bytes" ); //请求范围的度量单位
  358. Header ( "Accept-Length: " . filesize ( $file_dir ) ); //Content-Length是指定包含于请求或响应中数据的字节长度
  359. //用来告诉浏览器,文件是可以当做附件被下载,下载后的文件名称为$file_name该变量的值。
  360. Header ( "Content-Disposition: attachment; filename=印花税计算表.docx" );
  361. echo fread ( $file, filesize ( $file_dir ) ); //读取文件内容并直接输出到浏览器
  362. fclose ( $file );
  363. @unlink($file_dir);
  364. exit ();
  365. }
  366. // 提醒即将超时的记录,每天早上8点执行一次
  367. public function importantRecord(){
  368. $curTime = date('Y-m-d H:i:s',time() + 24*60*60);
  369. $lists = Db::name('important_record')
  370. ->where('status','in',[0,1])
  371. ->where('end_time','<=',$curTime)
  372. ->where('end_time','>',date('Y-m-d H:i:s'))
  373. ->field('id,user_id,org_id')
  374. ->select();
  375. $lists = $lists?$lists:[];
  376. foreach ($lists as $k=>$v){
  377. model('Message')->add(3,$v['id'],14,$v['user_id'],$v['org_id'],'您有一项重要事项即将超时,请及时处理.');
  378. }
  379. }
  380. public function cpay(){
  381. $lists = Db::name('contract_pay')
  382. ->alias('cp')
  383. ->join('apply a','a.id = cp.apply_id')
  384. ->where('cp.dep_user_id',0)
  385. ->where('cp.status',1)
  386. ->where('a.approval_id',88)
  387. ->field('cp.id,cp.apply_id')
  388. ->limit(5)
  389. ->select();
  390. dump($lists);
  391. $lists = $lists?$lists:[];
  392. foreach ($lists as $k=>$v){
  393. model('ContractPay')->formatContractPay($v['apply_id']);
  394. }
  395. }
  396. public function editIpmcEmail(){
  397. $config = config('app.email');
  398. $str = 'ABCDEFHHIGKLMNOPGRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
  399. $str1 = msubstr(str_shuffle($str), 0, mt_rand(2,6));
  400. $str2 = msubstr(str_shuffle($str), 0, mt_rand(2,6));
  401. $str3 = msubstr(str_shuffle($str), 0, mt_rand(2,6));
  402. $password = $str1.'&'.$str2.'.'.$str3;
  403. $data = [
  404. 'id' => 176,
  405. 'password' => $password,
  406. 'is_password' => 1,
  407. 'uname' => '',
  408. 'position' => '',
  409. 'tel' => '',
  410. 'branch_id' => 0,
  411. 'active' => 1, // 启用
  412. 'is_imap' => 0,
  413. 'limits' => 0,
  414. 'limitg' => 0,
  415. 'is_convert' => 0,
  416. 'login_password' => 0,
  417. 'disabled_password' => 1,
  418. 'my_spam_mode' => 1,
  419. '_key' => $config['key'],
  420. '_lang' => 'zh-cn',
  421. ];
  422. $url = $config['host'].'User/update';
  423. $ret = $this->http_post($url,$data);
  424. }
  425. private function http_post($url,$param){
  426. try{
  427. $ret = curl_post($url,$param);
  428. $data = json_decode($ret,true);
  429. if($data['status'] == 1){
  430. return $data;
  431. }else{
  432. \exception($data['msg']);
  433. }
  434. }catch (\think\Exception $e){
  435. trace($data['msg']);
  436. return false;
  437. }
  438. }
  439. // 自动处理待确定订餐变为已确定
  440. public function dinner(){
  441. $vals = Db::name('config')->where('name','dinner_apply_limit')->value('value');
  442. if($vals){
  443. $limitTime = date('Y-m-d').' '.$vals;
  444. $curTime = date('Y-m-d H:i');
  445. if($curTime > $limitTime){
  446. Db::name('dinner_orders')->where('del',0)->where('status',1)->update([
  447. 'status'=>2,
  448. 'update_time'=>date('Y-m-d H:i:s')
  449. ]);
  450. }
  451. }
  452. }
  453. // 提醒审核人员确认订单,每分钟运行一次
  454. public function dinnerapply(){
  455. $curTime = date('H:i:s');
  456. $cates = Db::name('dinner_cate')
  457. ->where('del',0)
  458. ->where('enable',1)
  459. ->where('end',$curTime)
  460. ->select();
  461. if(!$cates){
  462. exit();
  463. }
  464. $company = Db::name('dinner_company')
  465. ->where('del',0)
  466. ->where('enable',1)
  467. ->where('user_id','>',0)
  468. ->select();
  469. if(!$company){
  470. exit();
  471. }
  472. foreach ($cates as $k=>$v){
  473. foreach ($company as $kk=>$vv){
  474. $orders = Db::name('dinner_order_goods')
  475. ->alias('a')
  476. ->join('dinner_orders b','b.id = a.order_id')
  477. ->where('a.company_id',$vv['id'])
  478. ->where('b.cate_id',$v['id'])
  479. ->where('b.status',1)
  480. ->where('b.del',0)
  481. ->field('a.id')
  482. ->find();
  483. if($orders){
  484. $context = $v['title'].'售卖已结束,请及时确认订单。';
  485. model('Message')->add(3,0,0,$vv['user_id'],4,$context,1);
  486. }
  487. }
  488. }
  489. }
  490. // 更新打卡状态
  491. public function attendance(){
  492. $yday = date('Y-m-d',time() - 86400);
  493. // 昨天不跨天进行中的状态改为已完成
  494. $map1[] = ['day','=',$yday];
  495. $map1[] = ['next','=',0];
  496. $map1[] = ['status','=',0];
  497. $yyday = date('Y-m-d',time() - 2*86400);
  498. // 昨天跨天进行中的状态改为已完成
  499. $map2[] = ['day','=',$yyday];
  500. $map2[] = ['next','=',1];
  501. $map2[] = ['status','=',0];
  502. $lists = Db::name('attendance_user_class')
  503. ->whereOr([ $map1, $map2 ])
  504. ->field('id,content')
  505. ->select();
  506. $lists = $lists?$lists:[];
  507. foreach ($lists as $k=>$v){
  508. $dates = json_decode($v['content'],true);
  509. foreach ($dates as $kk=>$vv){
  510. $dates[$kk]['sstatus'] = 1;
  511. $dates[$kk]['estatus'] = 1;
  512. }
  513. Db::name('attendance_user_class')->where('id',$v['id'])->update([
  514. 'content' => json_encode($dates),
  515. 'status' => 1,
  516. 'update_time' => date('Y-m-d H:i:s')
  517. ]);
  518. }
  519. }
  520. // 昨日未打卡的日期
  521. public function attendanceReissue(){
  522. $yday = date('Y-m-d',time() - 86400);
  523. model('AttendanceUserClass')->attendanceReissueFixed($yday); // 固定班制
  524. model('AttendanceUserClass')->attendanceReissueClass($yday); // 排班制
  525. }
  526. }