Contract.php 22 KB

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