Contract.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-31 20:43
  10. // +----------------------------------------------------------------------
  11. namespace app\system\controller;
  12. use app\BaseController;
  13. use app\lib\OrderLib;
  14. use app\model\api\ArticleModel;
  15. use app\model\api\InfoAudit;
  16. use app\model\api\PayTrade;
  17. use library\utils\WxpayV2 as wxpayApi;
  18. use app\model\api\User as UserModel;
  19. use app\model\api\ContractRecord as UserContractRecordModel;
  20. use app\model\api\UserClock as UserClockModel;
  21. use app\model\api\ContractComment as ContractCommentModel;
  22. use app\model\api\UserDetail as UserDetailModel;
  23. use app\model\api\UserScoreDetail as UserScoreDetailModel;
  24. use app\model\api\UserShowTemplate;
  25. use library\services\UtilService;
  26. use app\Request;
  27. use think\facade\Db;
  28. class Contract extends BaseController
  29. {
  30. /**
  31. * 合同列表
  32. * @param Request $request
  33. * @return
  34. */
  35. public function UserClockList(Request $request)
  36. {
  37. $pageSize = 50;
  38. $post = UtilService::getMore(
  39. [
  40. ['page', 1],
  41. ['phone',0], //甲方uid或者电话
  42. ['to_phone',''], //乙方uid或者电话
  43. ['contract_no', ''],
  44. ['status',''],
  45. ['time',[]],
  46. ], $request
  47. );
  48. $where = [];
  49. if(!empty($post['phone'])){
  50. $where[]=["phone","=",$post['phone']];
  51. }
  52. // else
  53. // if(!empty($post['mobile'])){
  54. // $m = Db::name("user")->where("mobile",$post['mobile'])->find();
  55. // if(!empty($m)) {
  56. // $where[]=["o.uid","=",$m['uid']];
  57. // }
  58. // }
  59. if(!empty($post['to_phone'])){
  60. $where[]=["to_phone","=",$post['to_phone']];
  61. }
  62. if(!empty($post['contract_no'])){
  63. $where[]=["contract_no","=",$post['contract_no']];
  64. }
  65. if(!empty($post['status'])){
  66. $where[]=["status","=",$post['status']];
  67. }
  68. // if(in_array((string)$post['status'],["0","1","-1"])){
  69. // $where[]=["status","=",(int)$post['status']];
  70. // }
  71. //创建时间
  72. $startTime="";
  73. $endTime="";
  74. if(!empty($post['time'][0]) && !empty($post['time'][1])) {
  75. $startTime = strtotime($post['time'][0]);
  76. $endTime = strtotime($post['time'][1]);
  77. $where[]=["check_time","between","{$startTime},{$endTime}"];
  78. }
  79. $order = new UserContractRecordModel();
  80. $data = $order
  81. // ->alias("o")
  82. // ->field("*,u1.mobile,u1.nickname,a.title as show_template_title,a.imgs as show_template_imgs")
  83. // ->leftJoin("user u1","u1.uid = uid")
  84. // ->leftJoin("show_template a","a.id = show_template_id")
  85. ->field('*')
  86. ->where($where)
  87. ->page((int)$post["page"], $pageSize)
  88. ->order("id","desc")
  89. ->select()
  90. ->toArray();
  91. $pageCount = $order->where($where)->count();
  92. $result = UtilService::getParam([
  93. "id",
  94. "contract_no",
  95. "uid",
  96. "phone",
  97. "address",
  98. "card",
  99. "to_uid",
  100. "to_phone",
  101. "to_address",
  102. "to_card",
  103. "status",
  104. "from_check",
  105. "to_check",
  106. "uid_img",
  107. "to_uid_img",
  108. "price",
  109. "deposit",
  110. "balance",
  111. "content",
  112. "period",
  113. // "template_id",
  114. ['start_time', 'start_time', function ($item) {
  115. return empty($item) ? "-" : date('Y-m-d H:i:s', $item);
  116. }],
  117. ['end_time', 'end_time', function ($item) {
  118. return empty($item) ? "-" : date('Y-m-d H:i:s', $item);
  119. }],
  120. ['create_time', 'create_time', function ($item) {
  121. return empty($item) ? "-" : date('Y-m-d H:i:s', $item);
  122. }],
  123. ['delete_time', 'delete_time', function ($item) {
  124. return empty($item) ? "-" : date('Y-m-d H:i:s', $item);
  125. }],
  126. ['check_time', 'check_time', function ($item) {
  127. return empty($item) ? "-" : date('Y-m-d H:i:s', $item);
  128. }],
  129. ], $data);
  130. return app('json')->success([
  131. 'list' => $result,
  132. 'pageCount' => $pageCount,
  133. 'pageSize' => $pageSize,
  134. 'page' => $post['page'],
  135. ]);
  136. }
  137. /**
  138. * 合约详细
  139. * @param Request $request
  140. * @return mixed
  141. */
  142. public function getContractInfo(Request $request) {
  143. [$id] = UtilService::getMore([
  144. ['id','','empty','参数错误']
  145. ],$request,true);
  146. $adminInfo = UserContractRecordModel::get(compact('id'));
  147. return app('json')->success([
  148. 'id' => $id,
  149. 'contract_no' =>$adminInfo['contract_no'],
  150. 'uid' => $adminInfo['uid'],
  151. 'phone' => $adminInfo['phone'],
  152. 'address' => $adminInfo['address'],
  153. 'card' =>$adminInfo['card'],
  154. 'to_uid' => $adminInfo['to_uid'],
  155. 'to_phone' => $adminInfo['to_phone'],
  156. 'to_address' => $adminInfo['to_address'],
  157. 'to_card' => $adminInfo['to_card'],
  158. 'status' => $adminInfo['status'],
  159. 'from_check' => $adminInfo['from_check'],
  160. 'to_check' => $adminInfo['to_check'],
  161. 'uid_img' => $adminInfo['uid_img'],
  162. 'to_uid_img' => $adminInfo['to_uid_img'],
  163. 'price' => $adminInfo['price'],
  164. 'deposit' => $adminInfo['deposit'],
  165. 'balance' => $adminInfo['balance'],
  166. 'content' => $adminInfo['content'],
  167. 'period' => $adminInfo['period'],
  168. 'start_time' => date('Y-m-d H:i:s', $adminInfo['start_time']),
  169. 'end_time' => date('Y-m-d H:i:s', $adminInfo['end_time']),
  170. 'create_time' => date('Y-m-d H:i:s', $adminInfo['create_time']),
  171. 'delete_time' => date('Y-m-d H:i:s', $adminInfo['delete_time']),
  172. 'check_time' => date('Y-m-d H:i:s', $adminInfo['check_time']),
  173. // 'template_id' => $adminInfo['template_id'],
  174. ]);
  175. }
  176. /**
  177. * 签约列表
  178. * @param Request $request
  179. */
  180. public function ContractRecordList(Request $request)
  181. {
  182. $pageSize = 50;
  183. $post = UtilService::getMore(
  184. [
  185. ['page', 1],
  186. ['uid',0], //员工uid
  187. ['nickname',''], //名称
  188. ['contract_id', ''], //签约id
  189. ['time',[]],
  190. ], $request
  191. );
  192. $where = [];
  193. if(!empty($post['uid'])){
  194. $where[]=["uid","=",$post['uid']];
  195. }
  196. // else
  197. // if(!empty($post['mobile'])){
  198. // $m = Db::name("user")->where("mobile",$post['mobile'])->find();
  199. // if(!empty($m)) {
  200. // $where[]=["o.uid","=",$m['uid']];
  201. // }
  202. // }
  203. if(!empty($post['nickname'])){
  204. $where[]=["nickname","like",$post['nickname']];
  205. }
  206. if(!empty($post['contract_id'])){
  207. $where[]=["contract_id","=",$post['contract_id']];
  208. }
  209. //创建时间
  210. $startTime="";
  211. $endTime="";
  212. if(!empty($post['time'][0]) && !empty($post['time'][1])) {
  213. $startTime = strtotime($post['time'][0]);
  214. $endTime = strtotime($post['time'][1]);
  215. $where[]=["create_time","between","{$startTime},{$endTime}"];
  216. }
  217. $order = new UserClockModel;
  218. $data = $order
  219. // ->alias("o")
  220. // ->field("*,u1.mobile,u1.nickname,a.title as show_template_title,a.imgs as show_template_imgs")
  221. // ->leftJoin("user u1","u1.uid = uid")
  222. // ->leftJoin("show_template a","a.id = show_template_id")
  223. ->field('*')
  224. ->where($where)
  225. ->page((int)$post["page"], $pageSize)
  226. ->order("id","desc")
  227. ->select()
  228. ->toArray();
  229. $pageCount = $order->where($where)->count();
  230. $result = UtilService::getParam([
  231. "id",
  232. "uid",
  233. "nickname",
  234. "contract_id",
  235. "longitude",
  236. "latitude",
  237. ['create_time', 'create_time', function ($item) {
  238. return empty($item) ? "-" : date('Y-m-d H:i:s', $item);
  239. }],
  240. ], $data);
  241. return app('json')->success([
  242. 'list' => $result,
  243. 'pageCount' => $pageCount,
  244. 'pageSize' => $pageSize,
  245. 'page' => $post['page'],
  246. ]);
  247. }
  248. /**
  249. * 评论列表
  250. * @param Request $request
  251. */
  252. public function contractCommentList(Request $request)
  253. {
  254. $pageSize = 50;
  255. $post = UtilService::getMore(
  256. [
  257. ['page', 1],
  258. ['uid',0], //员工uid
  259. ['to_uid',''], //名称
  260. ['contract_no', ''], //签约id
  261. ['time',[]],
  262. ], $request
  263. );
  264. $where = [];
  265. if(!empty($post['uid'])){
  266. $where[]=["uid","=",$post['uid']];
  267. }
  268. // else
  269. // if(!empty($post['mobile'])){
  270. // $m = Db::name("user")->where("mobile",$post['mobile'])->find();
  271. // if(!empty($m)) {
  272. // $where[]=["o.uid","=",$m['uid']];
  273. // }
  274. // }
  275. if(!empty($post['nickname'])){
  276. $where[]=["nickname","like",$post['nickname']];
  277. }
  278. if(!empty($post['contract_id'])){
  279. $where[]=["contract_id","=",$post['contract_id']];
  280. }
  281. //创建时间
  282. $startTime="";
  283. $endTime="";
  284. if(!empty($post['time'][0]) && !empty($post['time'][1])) {
  285. $startTime = strtotime($post['time'][0]);
  286. $endTime = strtotime($post['time'][1]);
  287. $where[]=["create_time","between","{$startTime},{$endTime}"];
  288. }
  289. $order = new ContractCommentModel();
  290. $data = $order
  291. // ->alias("o")
  292. // ->field("*,u1.mobile,u1.nickname,a.title as show_template_title,a.imgs as show_template_imgs")
  293. // ->leftJoin("user u1","u1.uid = uid")
  294. // ->leftJoin("show_template a","a.id = show_template_id")
  295. ->field('*')
  296. ->where($where)
  297. ->page((int)$post["page"], $pageSize)
  298. ->order("id","desc")
  299. ->select()
  300. ->toArray();
  301. $pageCount = $order->where($where)->count();
  302. $result = UtilService::getParam([
  303. "id",
  304. "contract_no",
  305. "uid",
  306. "to_uid",
  307. ['create_time', 'create_time', function ($item) {
  308. return empty($item) ? "-" : date('Y-m-d H:i:s', $item);
  309. }],
  310. ], $data);
  311. return app('json')->success([
  312. 'list' => $result,
  313. 'pageCount' => $pageCount,
  314. 'pageSize' => $pageSize,
  315. 'page' => $post['page'],
  316. ]);
  317. }
  318. // 删除评论
  319. public function deleteComment(\think\Request $request)
  320. {
  321. $id = $request->param('id');
  322. $article = ContractCommentModel::where('id', $id)->find();
  323. if (!$article) {
  324. return app('json')->fail('评论不存在');
  325. }
  326. $article->delete();
  327. return app('json')->success();
  328. }
  329. /**
  330. * 合约列表
  331. * @param \app\Request $request
  332. * @return mixed
  333. */
  334. public function getContractList(\think\Request $request)
  335. {
  336. // $pageSize = 50;
  337. // $post = UtilService::getMore([
  338. // ['page',1],
  339. // ['pageSize',50],
  340. // ['nickname',''],
  341. //// ['uid',''],
  342. // ['parent_uid',''],
  343. // ['mobile',''],
  344. // ['status',''],
  345. // ['time',[]],
  346. // ],$request);
  347. //// $post['uid']=$request->user["uid"];
  348. // $data = (new UserModel)->getDataList($post,"*",1);
  349. // return app('json')->success([
  350. // 'list' => $data["list"],
  351. // 'pageCount' => $data["totalCount"],
  352. // 'pageSize' => $data["pageSize"],
  353. // 'page' => $data["page"],
  354. // ]);
  355. $post = UtilService::getMore([
  356. ['page', 1],
  357. ['pageSize', 50],
  358. ['status',-2] //1未签约 2已签约 3已解约
  359. ], $request);
  360. $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
  361. $post["page"] = $post["page"]<=0 ? 1 : (int)$post["page"];
  362. $where=[];
  363. $where[]=["uid","=",$request->user["uid"]];
  364. $totalCount = (new UserContractRecordModel)->where($where)->count();
  365. if ($post["status"]!=-2){
  366. $where[]=['status','=',$post["status"]];
  367. }
  368. $data=null;
  369. if($totalCount>0){
  370. $data = (new UserContractRecordModel)
  371. // ->field("")
  372. // ->alias("ut")
  373. // ->join("show_template t", "t.id = show_template_id","left")
  374. ->where($where)
  375. ->order("is_default", "desc")
  376. ->order("id", "desc")
  377. ->page($post["page"], $post["pageSize"])
  378. ->select();
  379. foreach($data as $k=>$v){
  380. $data[$k]["is_use"] = 1;//是否已经购买或者可以使用
  381. switch ($data[$k]["status"]){
  382. case 0:
  383. $data[$k]["status_name"] = "未签约";
  384. break;
  385. case 1:
  386. $data[$k]["status_name"] = "已签约";
  387. break;
  388. case -1:
  389. $data[$k]["status_name"] = "已解约";
  390. break;
  391. }
  392. }
  393. }
  394. $data = empty($data)?[]:$data;
  395. return app('json')->success(["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount]);
  396. }
  397. /**
  398. * 甲方创建合约
  399. */
  400. public function form_save(Request $request){
  401. $post = UtilService::getMore([
  402. ['uid','0'], //甲方uid
  403. ['phone',''], //甲方手机号
  404. ['address',''], //甲方地址
  405. ['card',''], //甲方身份证号
  406. ['to_uid','0'], //员工uid
  407. ['price',0], //服务费
  408. ['deposit',0], // 定金
  409. ['balance',0], //余款
  410. ['period',0], // 周期天数
  411. ['start_time',''], // 开始时间
  412. ['end_time',''], // 结束时间
  413. // ['uid_img',''] //甲方签名图片路径
  414. // ['to_phone',''].
  415. // ['to_address',''],
  416. // ['to_card',''],
  417. // ['data',[]],
  418. // ['year',''],
  419. // ['month',''],
  420. // ['day',''],
  421. ],$request);
  422. $id = (int)$post["id"];
  423. $uid = (int)$post["uid"];
  424. if(empty($uid)){
  425. return app('json')->fail("参数错误");
  426. }
  427. // $year = $post['year'];
  428. // $month = $post['month'];
  429. // $day = $post['day'];
  430. //
  431. //// 检查年月日是否为空
  432. // if (empty($year) || empty($month) || empty($day)) {
  433. // return app('json')->fail("日期参数错误");
  434. // }
  435. //
  436. //// 创建 DateTime 对象
  437. // $date = \DateTime::createFromFormat('Y-m-d', "$year-$month-$day");
  438. //
  439. //// 检查日期是否有效
  440. // if (!$date || $date->format('Y-m-d') !== "$year-$month-$day") {
  441. // return app('json')->fail("无效的日期");
  442. // }
  443. // unset($post['year']);
  444. // unset($post['month']);
  445. // unset($post['day']);
  446. //// 转换为时间戳
  447. // $timestamp = $date->getTimestamp();
  448. //
  449. // $post['create_time'] = $timestamp;
  450. //用户信息
  451. // if(!empty($post["uid_img"])){
  452. // $post['status'] = 2;
  453. // }else{
  454. $post['status'] = 0;
  455. // }
  456. if (empty($id)){
  457. $post['contract_no'] = makeOrderId($post['uid'],"CR");
  458. $r = (new UserContractRecordModel)->save($post);
  459. }else{
  460. $r = (new UserContractRecordModel)->where("id",$id)->update($post);
  461. }
  462. return app('json')->success("数据保存成功");
  463. }
  464. /**
  465. * 甲方签约合约
  466. */
  467. public function check(Request $request){
  468. $post = UtilService::getMore([
  469. ['id',0], //合约id
  470. ['uid_img',''], //甲方签名图片路径
  471. ['check_time','']//签约时间
  472. // ['to_phone',''].
  473. // ['to_address',''],
  474. // ['to_card',''],
  475. // ['data',[]],
  476. // ['year',''],
  477. // ['month',''],
  478. // ['day',''],
  479. ],$request);
  480. $info = (new UserContractRecordModel)->where("id",$post["id"])->find();
  481. if(empty($info)){
  482. return app('json')->fail("合约不存在");
  483. }
  484. // 检查签约时间是否为空
  485. if (empty($post['uid_img'])) {
  486. return app('json')->fail("签字错误");
  487. }
  488. // 检查签约时间是否为空
  489. if (empty($post['check_time'])) {
  490. return app('json')->fail("日期参数错误");
  491. }
  492. // $post['status'] = 1;
  493. $post['from_check'] = 1;
  494. if ($info['to_check']==1){
  495. $post['status']==1;
  496. }
  497. // $post['contract_no'] = makeOrderId($post['uid'],"CR");
  498. $r = (new UserContractRecordModel)->where("id",$post["id"])->update($post);
  499. return app('json')->success("数据保存成功");
  500. }
  501. // 乙方确认
  502. public function to_check(Request $request){
  503. $post = UtilService::getMore([
  504. ['id','0'], //
  505. ['to_uid_img',''], //乙方签名图片路径
  506. ['to_phone',''], //乙方手机号
  507. ['to_address',''], //乙方地址
  508. ['to_card',''], //乙方身份证号
  509. ['to_check_time','']//签约时间
  510. ],$request);
  511. $info = (new UserContractRecordModel)->where("id",$post["id"])->find();
  512. if(empty($info)){
  513. return app('json')->fail("合约不存在");
  514. }
  515. // 检查签约时间是否为空
  516. if (empty($post['to_uid_img'])) {
  517. return app('json')->fail("签字错误");
  518. }
  519. // 检查签约时间是否为空
  520. if (empty($post['to_check_time'])) {
  521. return app('json')->fail("日期参数错误");
  522. }
  523. $post['to_check'] = 1;
  524. if ($info['from_check']==1){
  525. $post['status']==1;
  526. }
  527. $r = (new UserContractRecordModel)->where("id",$post["id"])->update($post);
  528. return app('json')->success("数据保存成功");
  529. }
  530. // 员工签到
  531. public function clock_in(Request $request){
  532. $post = UtilService::getMore([
  533. ['id','0'], //合约id
  534. ['longitude',''], //经度
  535. ['latitude',''], //纬度
  536. ],$request);
  537. $uid = (int)$post["uid"];
  538. // 检查签约时间是否为空
  539. if (empty($post['id'])) {
  540. return app('json')->fail("签字错误");
  541. }
  542. $nickname=\app\model\api\User::where('uid',$uid)->value('nickname');
  543. $r = (new UserContractRecordModel)->where("id",$post["id"])->save([
  544. 'uid'=>$uid,
  545. 'nickname' => $nickname,
  546. 'longitude'=>$post['longitude'],
  547. 'latitude'=>$post['latitude'],
  548. 'contract_id'=>$post['id'],
  549. 'create_time'=>time()
  550. ]);
  551. return app('json')->success("数据保存成功");
  552. }
  553. // 添加客户评论
  554. public function comment(Request $request){
  555. $post = UtilService::getMore([
  556. ['id','0'], //合约id
  557. ['content',''], //评论内容
  558. ],$request);
  559. $uid = (int)$post["uid"];
  560. $info = (new UserContractRecordModel)->where("id",$post["id"])->find();
  561. if(empty($info)){
  562. return app('json')->fail("合约不存在");
  563. }
  564. if ($info['status']!=1){
  565. return app('json')->fail("只能在签约期间才能发表评论");
  566. }
  567. // 检查签约时间是否为空
  568. if (empty($post['id'])) {
  569. return app('json')->fail("签字错误");
  570. }
  571. $r = (new ContractCommentModel)->save([
  572. 'uid'=>$uid,
  573. 'contract_id'=>$post['id'],
  574. 'content'=>$post['content'],
  575. 'create_time'=>time()
  576. ]);
  577. return app('json')->success("数据保存成功");
  578. }
  579. }