Contract.php 21 KB

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