Contract.php 21 KB

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