Contract.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 ContractRecordList(Request $request)
  36. {
  37. $pageSize = 50;
  38. $post = UtilService::getMore(
  39. [
  40. ['page', 1],
  41. ['phone',''], //甲方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 UserClockList(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. var_dump($data);
  231. $result = UtilService::getParam([
  232. "id",
  233. "uid",
  234. "nickname",
  235. "contract_id",
  236. "longitude",
  237. "latitude",
  238. ['create_time', 'create_time', function ($item) {
  239. return empty($item) ? "-" : date('Y-m-d H:i:s', $item);
  240. }],
  241. ], $data);
  242. var_dump($pageSize);
  243. var_dump($pageCount);
  244. var_dump($result);die();
  245. return app('json')->success([
  246. 'list' => $result,
  247. 'pageCount' => $pageCount,
  248. 'pageSize' => $pageSize,
  249. 'page' => $post['page'],
  250. ]);
  251. }
  252. /**
  253. * 评论列表
  254. * @param Request $request
  255. */
  256. public function contractCommentList(Request $request)
  257. {
  258. $pageSize = 50;
  259. $post = UtilService::getMore(
  260. [
  261. ['page', 1],
  262. ['uid',0], //员工uid
  263. ['to_uid',''], //名称
  264. ['contract_no', ''], //签约id
  265. ['time',[]],
  266. ], $request
  267. );
  268. $where = [];
  269. if(!empty($post['uid'])){
  270. $where[]=["uid","=",$post['uid']];
  271. }
  272. // else
  273. // if(!empty($post['mobile'])){
  274. // $m = Db::name("user")->where("mobile",$post['mobile'])->find();
  275. // if(!empty($m)) {
  276. // $where[]=["o.uid","=",$m['uid']];
  277. // }
  278. // }
  279. if(!empty($post['nickname'])){
  280. $where[]=["nickname","like",$post['nickname']];
  281. }
  282. if(!empty($post['contract_id'])){
  283. $where[]=["contract_id","=",$post['contract_id']];
  284. }
  285. //创建时间
  286. $startTime="";
  287. $endTime="";
  288. if(!empty($post['time'][0]) && !empty($post['time'][1])) {
  289. $startTime = strtotime($post['time'][0]);
  290. $endTime = strtotime($post['time'][1]);
  291. $where[]=["create_time","between","{$startTime},{$endTime}"];
  292. }
  293. $order = new ContractCommentModel();
  294. $data = $order
  295. // ->alias("o")
  296. // ->field("*,u1.mobile,u1.nickname,a.title as show_template_title,a.imgs as show_template_imgs")
  297. // ->leftJoin("user u1","u1.uid = uid")
  298. // ->leftJoin("show_template a","a.id = show_template_id")
  299. ->field('*')
  300. ->where($where)
  301. ->page((int)$post["page"], $pageSize)
  302. ->order("id","desc")
  303. ->select()
  304. ->toArray();
  305. $pageCount = $order->where($where)->count();
  306. $result = UtilService::getParam([
  307. "id",
  308. "contract_no",
  309. "uid",
  310. "to_uid",
  311. ['create_time', 'create_time', function ($item) {
  312. return empty($item) ? "-" : date('Y-m-d H:i:s', $item);
  313. }],
  314. ], $data);
  315. return app('json')->success([
  316. 'list' => $result,
  317. 'pageCount' => $pageCount,
  318. 'pageSize' => $pageSize,
  319. 'page' => $post['page'],
  320. ]);
  321. }
  322. // 删除评论
  323. public function deleteComment(\think\Request $request)
  324. {
  325. $id = $request->param('id');
  326. $article = ContractCommentModel::where('id', $id)->find();
  327. if (!$article) {
  328. return app('json')->fail('评论不存在');
  329. }
  330. $article->delete();
  331. return app('json')->success();
  332. }
  333. /**
  334. * 合约列表
  335. * @param \app\Request $request
  336. * @return mixed
  337. */
  338. public function getContractList(\think\Request $request)
  339. {
  340. // $pageSize = 50;
  341. // $post = UtilService::getMore([
  342. // ['page',1],
  343. // ['pageSize',50],
  344. // ['nickname',''],
  345. //// ['uid',''],
  346. // ['parent_uid',''],
  347. // ['mobile',''],
  348. // ['status',''],
  349. // ['time',[]],
  350. // ],$request);
  351. //// $post['uid']=$request->user["uid"];
  352. // $data = (new UserModel)->getDataList($post,"*",1);
  353. // return app('json')->success([
  354. // 'list' => $data["list"],
  355. // 'pageCount' => $data["totalCount"],
  356. // 'pageSize' => $data["pageSize"],
  357. // 'page' => $data["page"],
  358. // ]);
  359. $post = UtilService::getMore([
  360. ['page', 1],
  361. ['pageSize', 50],
  362. ['status',-2] //1未签约 2已签约 3已解约
  363. ], $request);
  364. $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
  365. $post["page"] = $post["page"]<=0 ? 1 : (int)$post["page"];
  366. $where=[];
  367. $where[]=["uid","=",$request->user["uid"]];
  368. $totalCount = (new UserContractRecordModel)->where($where)->count();
  369. if ($post["status"]!=-2){
  370. $where[]=['status','=',$post["status"]];
  371. }
  372. $data=null;
  373. if($totalCount>0){
  374. $data = (new UserContractRecordModel)
  375. // ->field("")
  376. // ->alias("ut")
  377. // ->join("show_template t", "t.id = show_template_id","left")
  378. ->where($where)
  379. ->order("is_default", "desc")
  380. ->order("id", "desc")
  381. ->page($post["page"], $post["pageSize"])
  382. ->select();
  383. foreach($data as $k=>$v){
  384. $data[$k]["is_use"] = 1;//是否已经购买或者可以使用
  385. switch ($data[$k]["status"]){
  386. case 0:
  387. $data[$k]["status_name"] = "未签约";
  388. break;
  389. case 1:
  390. $data[$k]["status_name"] = "已签约";
  391. break;
  392. case -1:
  393. $data[$k]["status_name"] = "已解约";
  394. break;
  395. }
  396. }
  397. }
  398. $data = empty($data)?[]:$data;
  399. return app('json')->success(["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount]);
  400. }
  401. /**
  402. * 甲方创建合约
  403. */
  404. public function form_save(Request $request){
  405. $post = UtilService::getMore([
  406. ['uid','0'], //甲方uid
  407. ['phone',''], //甲方手机号
  408. ['address',''], //甲方地址
  409. ['card',''], //甲方身份证号
  410. ['to_uid','0'], //员工uid
  411. ['price',0], //服务费
  412. ['deposit',0], // 定金
  413. ['balance',0], //余款
  414. ['period',0], // 周期天数
  415. ['start_time',''], // 开始时间
  416. ['end_time',''], // 结束时间
  417. // ['uid_img',''] //甲方签名图片路径
  418. // ['to_phone',''].
  419. // ['to_address',''],
  420. // ['to_card',''],
  421. // ['data',[]],
  422. // ['year',''],
  423. // ['month',''],
  424. // ['day',''],
  425. ],$request);
  426. $id = (int)$post["id"];
  427. $uid = (int)$post["uid"];
  428. if(empty($uid)){
  429. return app('json')->fail("参数错误");
  430. }
  431. // $year = $post['year'];
  432. // $month = $post['month'];
  433. // $day = $post['day'];
  434. //
  435. //// 检查年月日是否为空
  436. // if (empty($year) || empty($month) || empty($day)) {
  437. // return app('json')->fail("日期参数错误");
  438. // }
  439. //
  440. //// 创建 DateTime 对象
  441. // $date = \DateTime::createFromFormat('Y-m-d', "$year-$month-$day");
  442. //
  443. //// 检查日期是否有效
  444. // if (!$date || $date->format('Y-m-d') !== "$year-$month-$day") {
  445. // return app('json')->fail("无效的日期");
  446. // }
  447. // unset($post['year']);
  448. // unset($post['month']);
  449. // unset($post['day']);
  450. //// 转换为时间戳
  451. // $timestamp = $date->getTimestamp();
  452. //
  453. // $post['create_time'] = $timestamp;
  454. //用户信息
  455. // if(!empty($post["uid_img"])){
  456. // $post['status'] = 2;
  457. // }else{
  458. $post['status'] = 0;
  459. // }
  460. if (empty($id)){
  461. $post['contract_no'] = makeOrderId($post['uid'],"CR");
  462. $r = (new UserContractRecordModel)->save($post);
  463. }else{
  464. $r = (new UserContractRecordModel)->where("id",$id)->update($post);
  465. }
  466. return app('json')->success("数据保存成功");
  467. }
  468. /**
  469. * 甲方签约合约
  470. */
  471. public function check(Request $request){
  472. $post = UtilService::getMore([
  473. ['id',0], //合约id
  474. ['uid_img',''], //甲方签名图片路径
  475. ['check_time','']//签约时间
  476. // ['to_phone',''].
  477. // ['to_address',''],
  478. // ['to_card',''],
  479. // ['data',[]],
  480. // ['year',''],
  481. // ['month',''],
  482. // ['day',''],
  483. ],$request);
  484. $info = (new UserContractRecordModel)->where("id",$post["id"])->find();
  485. if(empty($info)){
  486. return app('json')->fail("合约不存在");
  487. }
  488. // 检查签约时间是否为空
  489. if (empty($post['uid_img'])) {
  490. return app('json')->fail("签字错误");
  491. }
  492. // 检查签约时间是否为空
  493. if (empty($post['check_time'])) {
  494. return app('json')->fail("日期参数错误");
  495. }
  496. // $post['status'] = 1;
  497. $post['from_check'] = 1;
  498. if ($info['to_check']==1){
  499. $post['status']==1;
  500. }
  501. // $post['contract_no'] = makeOrderId($post['uid'],"CR");
  502. $r = (new UserContractRecordModel)->where("id",$post["id"])->update($post);
  503. return app('json')->success("数据保存成功");
  504. }
  505. // 乙方确认
  506. public function to_check(Request $request){
  507. $post = UtilService::getMore([
  508. ['id','0'], //
  509. ['to_uid_img',''], //乙方签名图片路径
  510. ['to_phone',''], //乙方手机号
  511. ['to_address',''], //乙方地址
  512. ['to_card',''], //乙方身份证号
  513. ['to_check_time','']//签约时间
  514. ],$request);
  515. $info = (new UserContractRecordModel)->where("id",$post["id"])->find();
  516. if(empty($info)){
  517. return app('json')->fail("合约不存在");
  518. }
  519. // 检查签约时间是否为空
  520. if (empty($post['to_uid_img'])) {
  521. return app('json')->fail("签字错误");
  522. }
  523. // 检查签约时间是否为空
  524. if (empty($post['to_check_time'])) {
  525. return app('json')->fail("日期参数错误");
  526. }
  527. $post['to_check'] = 1;
  528. if ($info['from_check']==1){
  529. $post['status']==1;
  530. }
  531. $r = (new UserContractRecordModel)->where("id",$post["id"])->update($post);
  532. return app('json')->success("数据保存成功");
  533. }
  534. // 员工签到
  535. public function clock_in(Request $request){
  536. $post = UtilService::getMore([
  537. ['id','0'], //合约id
  538. ['longitude',''], //经度
  539. ['latitude',''], //纬度
  540. ],$request);
  541. $uid = (int)$post["uid"];
  542. // 检查签约时间是否为空
  543. if (empty($post['id'])) {
  544. return app('json')->fail("签字错误");
  545. }
  546. $nickname=\app\model\api\User::where('uid',$uid)->value('nickname');
  547. $r = (new UserContractRecordModel)->where("id",$post["id"])->save([
  548. 'uid'=>$uid,
  549. 'nickname' => $nickname,
  550. 'longitude'=>$post['longitude'],
  551. 'latitude'=>$post['latitude'],
  552. 'contract_id'=>$post['id'],
  553. 'create_time'=>time()
  554. ]);
  555. return app('json')->success("数据保存成功");
  556. }
  557. // 添加客户评论
  558. public function comment(Request $request){
  559. $post = UtilService::getMore([
  560. ['id','0'], //合约id
  561. ['content',''], //评论内容
  562. ],$request);
  563. $uid = (int)$post["uid"];
  564. $info = (new UserContractRecordModel)->where("id",$post["id"])->find();
  565. if(empty($info)){
  566. return app('json')->fail("合约不存在");
  567. }
  568. if ($info['status']!=1){
  569. return app('json')->fail("只能在签约期间才能发表评论");
  570. }
  571. // 检查签约时间是否为空
  572. if (empty($post['id'])) {
  573. return app('json')->fail("签字错误");
  574. }
  575. $r = (new ContractCommentModel)->save([
  576. 'uid'=>$uid,
  577. 'contract_id'=>$post['id'],
  578. 'content'=>$post['content'],
  579. 'create_time'=>time()
  580. ]);
  581. return app('json')->success("数据保存成功");
  582. }
  583. }