Consume.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\ExamineRecord;
  4. use addons\qingdongams\model\Consume as ConsumeModel;
  5. use addons\qingdongams\model\Message;
  6. use think\Db;
  7. use think\Exception;
  8. /**
  9. * 费用接口
  10. */
  11. class Consume extends StaffApi
  12. {
  13. protected $noNeedLogin = [];
  14. protected $noNeedRight = [];
  15. //添加费用
  16. public function addConsume()
  17. {
  18. $params = $this->request->post();
  19. // 表单验证
  20. if (($result = $this->qingdongamsValidate($params,get_class(), 'create')) !== true) {
  21. $this->error($result);
  22. }
  23. Db::startTrans();
  24. try {
  25. $lastId = ConsumeModel::createConsume($params);
  26. Db::commit();
  27. } catch (Exception $e) {
  28. Db::rollback();
  29. $this->error($e->getMessage());
  30. }
  31. if ($result) {
  32. $this->success('添加费用成功',['id'=>$lastId]);
  33. }
  34. }
  35. //编辑费用
  36. public function editConsume()
  37. {
  38. $params = $this->request->post();
  39. // 表单验证
  40. if (($result = $this->qingdongamsValidate($params, get_class(),'create')) !== true) {
  41. $this->error($result);
  42. }
  43. Db::startTrans();
  44. try {
  45. $result = ConsumeModel::updateConsume($params);
  46. Db::commit();
  47. } catch (Exception $e) {
  48. Db::rollback();
  49. $this->error($e->getMessage());
  50. }
  51. if ($result) {
  52. $this->success('编辑费用成功');
  53. }
  54. }
  55. //获取待审批费用数量
  56. public function getConsumeNumber()
  57. {
  58. $faqi = ConsumeModel::where([
  59. 'staff_id' => $this->auth->id,
  60. 'check_status' => ['in', [0, 1]]
  61. ])->count();
  62. $shenpi = ConsumeModel::where([
  63. 'check_status' => ['in', [0, 1]],
  64. ])->where([])->count();
  65. $this->success('请求成功', ['faqi' => $faqi, 'shenpi' => $shenpi]);
  66. }
  67. //获取费用列表
  68. public function getList()
  69. {
  70. $limit = input("limit/d", 10);
  71. $customer_id = input('customer_id', '', 'intval');
  72. //发起人 查询
  73. $staff_id = input('staff_id', '', 'intval');
  74. $name = input('name', '', 'intval');
  75. $check_status = input('check_status');//1审核中、2审核通过、3审核未通过
  76. $relation_type = input('relation_type');//
  77. $relation_id = input('relation_id');//
  78. $ids=input('ids');
  79. $type = input('type', 1);
  80. $whereFlow = $followWhere = [];
  81. $where = [];
  82. $times = input('times', '');
  83. if ($check_status) {
  84. //0待审核、1审核中、2审核通过、3审核未通过、4撤销
  85. if ($check_status == 1) {
  86. $where['check_status'] = ['in', [0, 1]];
  87. $followWhere[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',check_staff_ids)')];
  88. } elseif ($check_status == 2) {
  89. $where['check_status'] = 2;
  90. $followWhere[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',check_staff_ids)')];
  91. } elseif ($check_status == 3) {
  92. $where['check_status'] = 3;
  93. $followWhere[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',check_staff_ids)')];
  94. }elseif ($check_status == 4) {
  95. $where['check_status'] = 4;
  96. $followWhere[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',check_staff_ids)')];
  97. }elseif ($check_status == 9) {
  98. $where['check_status'] = 9;
  99. $followWhere[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',check_staff_ids)')];
  100. }
  101. }
  102. if ($type == 1) {//我发起的
  103. $where['staff_id'] = $this->auth->id;
  104. } elseif ($type == 2) {//我审批的
  105. $whereFlow = $followWhere;
  106. }elseif ($type == 3 && empty($ids)){
  107. $this->success('请求成功',[]);
  108. }
  109. if ($ids) {
  110. $where['id'] = ['in',explode(',',$ids)];
  111. }
  112. if ($customer_id) {
  113. $where['customer_id'] = $customer_id;
  114. if(isset($where['staff_id']))unset($where['staff_id']);
  115. }
  116. if ($name) {
  117. $where['number'] = ["like","%{$name}%"];
  118. }
  119. if ($staff_id) {
  120. $where['staff_id'] = $staff_id;
  121. }
  122. if ($relation_type) {
  123. $where['relation_type'] = $relation_type;
  124. unset($where['staff_id']);
  125. }
  126. if ($relation_id) {
  127. $where['relation_id'] = $relation_id;
  128. }
  129. if ($times) {
  130. $times = setTimes($times, 'time');
  131. $where['createtime'] = ['between', $times];
  132. }
  133. $records = ConsumeModel::where($where)->where($whereFlow)->with(['staff', 'customer','consume_detail'])->order('id desc')->paginate($limit);
  134. $this->success('请求成功', $records);
  135. }
  136. //获取费用详情
  137. public function getDetail()
  138. {
  139. $id = input('id', '', 'intval');
  140. $consume = ConsumeModel::where(['id' => $id])
  141. ->with(['staff', 'customer','consume_detail'])->find();
  142. if (empty($consume)) {
  143. $this->error('费用不存在');
  144. }
  145. if($consume['staff_id'] == $this->auth->id && $consume['check_status'] != 2){
  146. $consume['is_edit'] = 1;
  147. }else{
  148. $consume['is_edit'] = 0;
  149. }
  150. $consume['is_operation']=0;
  151. if($consume['staff_id'] == $this->auth->id){
  152. //是否可以操作
  153. $consume['is_operation']=1;
  154. }
  155. if ($consume['check_status'] == 0 || $consume['check_status'] == 1) {
  156. $consume['is_examine'] = ExamineRecord::isExaminse(ExamineRecord::CONSUME_TYPE, $id);
  157. } else {
  158. $consume['is_examine'] = 0;
  159. }
  160. //标记通知已读
  161. Message::setRead(Message::CONSUME_TYPE, $id, $this->auth->id);
  162. $this->success('请求成功', $consume);
  163. }
  164. //撤销费用
  165. public function cancelConsume()
  166. {
  167. $id = input('id');
  168. if(empty($id)){
  169. $this->error('id不能为空');
  170. }
  171. $consume=ConsumeModel::where(['id'=>$id])->find();
  172. if(empty($consume)){
  173. $this->error('信息不存在');
  174. }
  175. if($consume['staff_id'] != $this->auth->id){
  176. $this->error('只有创建人可以撤销费用');
  177. }
  178. Db::startTrans();
  179. try {
  180. ConsumeModel::where(['id'=>$id])->update(['check_status'=>4]);
  181. ExamineRecord::cancelExaminse( ExamineRecord::CONSUME_TYPE,$id);
  182. Db::commit();
  183. } catch (Exception $e) {
  184. Db::rollback();
  185. $this->error($e->getMessage());
  186. }
  187. $this->success('撤销成功');
  188. }
  189. //终止费用
  190. public function revokeConsume()
  191. {
  192. $id = input('id');
  193. if(empty($id)){
  194. $this->error('id不能为空');
  195. }
  196. $consume=ConsumeModel::where(['id'=>$id])->find();
  197. if(empty($consume)){
  198. $this->error('信息不存在');
  199. }
  200. if($consume['staff_id'] != $this->auth->id){
  201. $this->error('只有创建人可以终止费用');
  202. }
  203. Db::startTrans();
  204. try {
  205. ConsumeModel::where(['id'=>$id])->update(['check_status'=>9]);
  206. ExamineRecord::cancelExaminse( ExamineRecord::CONSUME_TYPE,$id);
  207. Db::commit();
  208. } catch (Exception $e) {
  209. Db::rollback();
  210. $this->error($e->getMessage());
  211. }
  212. $this->success('终止费用');
  213. }
  214. //费用计算
  215. public function consume_count(){
  216. $number=input('number');
  217. $this->success('请求成功',['money'=>ConsumeModel::getMileageMoney($number)]);
  218. }
  219. //获取费用编号
  220. public function getNumber()
  221. {
  222. $this->success('请求成功', ['number' => ConsumeModel::getNumber()]);
  223. }
  224. //获取出纳列表
  225. public function getCashierList()
  226. {
  227. $limit = input("limit/d", 10);
  228. $customer_id = input('customer_id', '', 'intval');
  229. //发起人 查询
  230. $staff_id = input('staff_id', '', 'intval');
  231. $ids=input('ids');
  232. $name=input('name');
  233. $is_cashier = input('is_cashier', NULL);
  234. $where = ['check_status'=>2];
  235. if ($ids) {
  236. $where['id'] = ['in',explode(',',$ids)];
  237. }
  238. if ($name) {
  239. $where['number'] = ["like","%{$name}%"];
  240. }
  241. if ($customer_id) {
  242. $where['customer_id'] = $customer_id;
  243. }
  244. if ($staff_id) {
  245. $where['staff_id'] = $staff_id;
  246. }
  247. if($is_cashier!==NULL){
  248. $where['is_cashier'] = $is_cashier;
  249. }
  250. $records = ConsumeModel::where($where)->with(['staff', 'customer','consume_detail'])->order('id desc')->paginate($limit);
  251. $this->success('请求成功', $records);
  252. }
  253. //出纳
  254. public function cashier(){
  255. $ids=input('ids');
  256. if(empty($ids)){
  257. $this->error('参数错误');
  258. }
  259. $ids=explode(',',$ids);
  260. ConsumeModel::where(['id'=>['in',$ids]])->update(['is_cashier'=>1]);
  261. $data=[];
  262. foreach ($ids as $id){
  263. $data[] = [
  264. 'relation_type' => ExamineRecord::CONSUME_TYPE,
  265. 'relation_id' => $id,
  266. 'check_staff_id' => $this->auth->id,
  267. 'status' => 1,
  268. 'content' => '出纳审核',
  269. 'createtime' => time(),
  270. 'updatetime' => time()
  271. ];
  272. }
  273. $examineModel=new ExamineRecord();
  274. $examineModel->insertAll($data);
  275. $this->success('操作成功');
  276. }
  277. }