Contract.php 22 KB

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