Contract.php 21 KB

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