Api.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2022 勾股工作室
  4. * @license https://opensource.org/licenses/GPL-3.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\contract\controller;
  9. use app\api\BaseController;
  10. use app\contract\model\ContractLog;
  11. use think\facade\Db;
  12. use think\facade\View;
  13. class Api extends BaseController
  14. {
  15. //获取合同协议
  16. public function get_contract()
  17. {
  18. $param = get_params();
  19. $where = array();
  20. $whereOr = array();
  21. if (!empty($param['keywords'])) {
  22. $where[] = ['id|name', 'like', '%' . $param['keywords'] . '%'];
  23. }
  24. $where[] = ['delete_time', '=', 0];
  25. $where[] = ['check_status', '=', 2];
  26. $uid = $this->uid;
  27. $auth = isAuth($uid,'contract_admin');
  28. if($auth==0){
  29. $whereOr[] =['admin_id|prepared_uid|sign_uid|keeper_uid', '=', $uid];
  30. $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',share_ids)")];
  31. $dids = get_department_role($this->uid);
  32. if(!empty($dids)){
  33. $whereOr[] =['sign_did', 'in', $dids];
  34. }
  35. }
  36. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  37. $list = Db::name('Contract')
  38. ->field('id,name,code,customer_id,sign_uid,sign_time')
  39. ->order('id desc')
  40. ->where($where)
  41. ->where(function ($query) use($whereOr) {
  42. $query->whereOr($whereOr);
  43. })
  44. ->paginate($rows, false)->each(function($item, $key){
  45. $item['sign_name'] = Db::name('Admin')->where('id',$item['sign_uid'])->value('name');
  46. $item['sign_time'] = date('Y-m-d', $item['sign_time']);
  47. $item['customer'] = Db::name('Customer')->where('id',$item['customer_id'])->value('name');
  48. return $item;
  49. });
  50. table_assign(0, '', $list);
  51. }
  52. //添加附件
  53. public function add_file()
  54. {
  55. $param = get_params();
  56. $param['create_time'] = time();
  57. $param['admin_id'] = $this->uid;
  58. $fid = Db::name('ContractFile')->strict(false)->field(true)->insertGetId($param);
  59. if ($fid) {
  60. $log_data = array(
  61. 'field' => 'file',
  62. 'action' => 'upload',
  63. 'contract_id' => $param['contract_id'],
  64. 'admin_id' => $param['admin_id'],
  65. 'old_content' => '',
  66. 'new_content' => $param['file_name'],
  67. 'create_time' => time(),
  68. );
  69. Db::name('ContractLog')->strict(false)->field(true)->insert($log_data);
  70. return to_assign(0, '上传成功', $fid);
  71. }
  72. }
  73. //删除
  74. public function delete_file()
  75. {
  76. if (request()->isDelete()) {
  77. $id = get_params("id");
  78. $data['id'] = $id;
  79. $data['delete_time'] = time();
  80. if (Db::name('ContractFile')->update($data) !== false) {
  81. $detail = Db::name('ContractFile')->where('id', $id)->find();
  82. $file_name = Db::name('File')->where('id', $detail['file_id'])->value('name');
  83. $log_data = array(
  84. 'field' => 'file',
  85. 'action' => 'delete',
  86. 'contract_id' => $detail['contract_id'],
  87. 'admin_id' => $this->uid,
  88. 'new_content' => $file_name,
  89. 'create_time' => time(),
  90. );
  91. Db::name('ContractLog')->strict(false)->field(true)->insert($log_data);
  92. return to_assign(0, "删除成功");
  93. } else {
  94. return to_assign(1, "删除失败");
  95. }
  96. } else {
  97. return to_assign(1, "错误的请求");
  98. }
  99. }
  100. //状态改变等操作
  101. public function check()
  102. {
  103. if (request()->isPost()) {
  104. $param = get_params();
  105. if($param['check_status'] == 0){
  106. $param['check_step_sort'] = 0;
  107. }
  108. if($param['check_status'] == 1){
  109. $check_admin_ids = isset($param['check_admin_ids'])?$param['check_admin_ids']:'';
  110. $flow_data = set_flow($param['flow_id'],$check_admin_ids,$this->uid);
  111. $param['check_admin_ids'] = $flow_data['check_admin_ids'];
  112. $flow = $flow_data['flow'];
  113. $check_type = $flow_data['check_type'];
  114. //删除原来的审核流程和审核记录
  115. Db::name('FlowStep')->where(['action_id'=>$param['id'],'type'=>4,'delete_time'=>0])->update(['delete_time'=>time()]);
  116. Db::name('FlowRecord')->where(['action_id'=>$param['id'],'type'=>4,'delete_time'=>0])->update(['delete_time'=>time()]);
  117. if($check_type == 2){
  118. $flow_step = array(
  119. 'action_id' => $param['id'],
  120. 'type' => 4,
  121. 'flow_uids' => $param['check_admin_ids'],
  122. 'create_time' => time()
  123. );
  124. //增加审核流程
  125. Db::name('FlowStep')->strict(false)->field(true)->insertGetId($flow_step);
  126. }
  127. else{
  128. foreach ($flow as $key => &$value){
  129. $value['action_id'] = $param['id'];
  130. $value['sort'] = $key;
  131. $value['type'] = 4;
  132. $value['create_time'] = time();
  133. }
  134. //增加审核流程
  135. Db::name('FlowStep')->strict(false)->field(true)->insertAll($flow);
  136. }
  137. $checkData=array(
  138. 'action_id' => $param['id'],
  139. 'step_id' => 0,
  140. 'check_user_id' => $this->uid,
  141. 'type' => 4,
  142. 'check_time' => time(),
  143. 'status' => 0,
  144. 'content' => '提交申请',
  145. 'create_time' => time()
  146. );
  147. $aid = Db::name('FlowRecord')->strict(false)->field(true)->insertGetId($checkData);
  148. //发送消息通知
  149. $msg=[
  150. 'from_uid'=>$this->uid,
  151. 'title'=>'合同',
  152. 'action_id'=>$param['id']
  153. ];
  154. $users = $param['check_admin_ids'];
  155. sendMessage($users,51,$msg);
  156. }
  157. if($param['check_status'] == 3){
  158. $param['check_uid'] = $this->uid;
  159. $param['check_time'] = time();
  160. $param['check_remark'] = $param['mark'];
  161. }
  162. if($param['check_status'] == 4){
  163. $param['stop_uid'] = $this->uid;
  164. $param['stop_time'] = time();
  165. $param['stop_remark'] = $param['mark'];
  166. }
  167. if($param['check_status'] == 5){
  168. $param['void_uid'] = $this->uid;
  169. $param['void_time'] = time();
  170. $param['void_remark'] = $param['mark'];
  171. }
  172. $old = Db::name('Contract')->where('id', $param['id'])->find();
  173. if (Db::name('Contract')->strict(false)->update($param) !== false) {
  174. $log_data = array(
  175. 'field' => 'check_status',
  176. 'contract_id' => $param['id'],
  177. 'admin_id' => $this->uid,
  178. 'new_content' => $param['check_status'],
  179. 'old_content' => $old['check_status'],
  180. 'create_time' => time(),
  181. );
  182. Db::name('ContractLog')->strict(false)->field(true)->insert($log_data);
  183. return to_assign(0, "操作成功");
  184. } else {
  185. return to_assign(1, "操作失败");
  186. }
  187. } else {
  188. return to_assign(1, "错误的请求");
  189. }
  190. }
  191. //归档等操作
  192. public function archive()
  193. {
  194. if (request()->isPost()) {
  195. $param = get_params();
  196. $old = 1;
  197. if($param['archive_status'] == 1){
  198. $param['archive_uid'] = $this->uid;
  199. $param['archive_time'] = time();
  200. $old = 0;
  201. }
  202. $old = Db::name('Contract')->where('id', $param['id'])->find();
  203. if (Db::name('Contract')->strict(false)->update($param) !== false) {
  204. $log_data = array(
  205. 'field' => 'archive_status',
  206. 'contract_id' => $param['id'],
  207. 'admin_id' => $this->uid,
  208. 'new_content' => $param['archive_status'],
  209. 'old_content' => $old['archive_status'],
  210. 'create_time' => time(),
  211. );
  212. Db::name('ContractLog')->strict(false)->field(true)->insert($log_data);
  213. return to_assign(0, "操作成功");
  214. } else {
  215. return to_assign(1, "操作失败");
  216. }
  217. } else {
  218. return to_assign(1, "错误的请求");
  219. }
  220. }
  221. //合同操作日志列表
  222. public function contract_log()
  223. {
  224. $param = get_params();
  225. $list = new ContractLog();
  226. $content = $list->contract_log($param);
  227. return to_assign(0, '', $content);
  228. }
  229. //获取客户列表
  230. public function get_customer()
  231. {
  232. $param = get_params();
  233. $where = array();
  234. if (!empty($param['keywords'])) {
  235. $where[] = ['id|name', 'like', '%' . $param['keywords'] . '%'];
  236. }
  237. $where[] = ['delete_time', '=', 0];
  238. $uid = $this->uid;
  239. $auth = isAuth($uid,'customer_admin');
  240. $dids = get_department_role($this->uid);
  241. if($auth==0){
  242. $whereOr[] =['belong_uid', '=', $uid];
  243. if(!empty($dids)){
  244. $whereOr[] =['belong_did', 'in', $dids];
  245. }
  246. $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',share_ids)")];
  247. }
  248. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  249. $list = Db::name('Customer')->field('id,name,address')->order('id asc')->where($where)->paginate($rows, false)->each(function($item, $key){
  250. $contact = Db::name('CustomerContact')->where(['cid'=>$item['id'],'is_default'=>1])->find();
  251. if(!empty($contact)){
  252. $item['contact_name'] = $contact['name'];
  253. $item['contact_mobile'] = $contact['mobile'];
  254. }
  255. else{
  256. $item['contact_name'] = '';
  257. $item['contact_mobile'] = '';
  258. }
  259. return $item;
  260. });
  261. table_assign(0, '', $list);
  262. }
  263. }