Consume.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\ExamineRecord;
  4. use addons\qingdong\model\Consume as ConsumeModel;
  5. use addons\qingdong\model\Message;
  6. use addons\qingdong\model\File;
  7. use addons\qingdong\model\Staff;
  8. use think\Db;
  9. use think\Exception;
  10. /**
  11. * 费用接口
  12. */
  13. class Consume extends StaffApi
  14. {
  15. protected $noNeedLogin = [];
  16. protected $noNeedRight = [];
  17. //添加费用
  18. public function addConsume()
  19. {
  20. $params = $this->request->post();
  21. // 表单验证
  22. if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
  23. $this->error($result);
  24. }
  25. Db::startTrans();
  26. try {
  27. $result = ConsumeModel::createConsume($params);
  28. Db::commit();
  29. } catch (Exception $e) {
  30. Db::rollback();
  31. $this->error($e->getMessage());
  32. }
  33. if ($result) {
  34. $this->success('添加费用成功');
  35. }
  36. }
  37. //编辑费用
  38. public function editConsume()
  39. {
  40. $params = $this->request->post();
  41. // 表单验证
  42. if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
  43. $this->error($result);
  44. }
  45. Db::startTrans();
  46. try {
  47. $result = ConsumeModel::updateConsume($params);
  48. Db::commit();
  49. } catch (Exception $e) {
  50. Db::rollback();
  51. $this->error($e->getMessage());
  52. }
  53. if ($result) {
  54. $this->success('添加费用成功');
  55. }
  56. }
  57. //获取费用列表
  58. public function getList()
  59. {
  60. $limit = input("limit/d", 10);
  61. $customer_id = input('customer_id', '', 'intval');
  62. $times = input('times', '');
  63. $status = input('check_status',0);
  64. $where = [];
  65. $params = $this->request->post();
  66. if ($times) {//
  67. $times = explode(',', $times);
  68. $where['createtime'] = ['between', [strtotime($times[0]), strtotime($times[1]) + 86400 - 1]];
  69. }
  70. if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选
  71. $where['staff_id'] = $params['staff_id'];
  72. } else {
  73. $where['staff_id'] = ['in', Staff::getMyStaffIds()];
  74. if (isset($params['type']) && $params['type']) {//客户分类
  75. if ($params['type'] == 1) {//我的客户
  76. $where['staff_id'] = $this->auth->id;
  77. } elseif ($params['type'] == 2) {//下属负责的客户
  78. $where['staff_id'] = ['in', Staff::getLowerStaffId()];
  79. }
  80. }
  81. }
  82. if ($status) {
  83. if($status == 1){//待审核
  84. $where['check_status']=['in',[0,1]];
  85. }elseif($status == 2){//通过
  86. $where['check_status']=2;
  87. }elseif($status == 3){//审核拒绝
  88. $where['contract_status']=['in',[3,4]];
  89. }
  90. }
  91. if($customer_id){
  92. $where['customer_id'] = $customer_id;
  93. }
  94. $records = ConsumeModel::where($where)->with(['staff','customer'])->order('id desc')->paginate($limit)->toArray();
  95. $remoney = ConsumeModel::where(['staff_id'=>$where['staff_id']])
  96. ->where(array('check_status'=>2))->sum('money');
  97. $inmoney = ConsumeModel::where(['staff_id'=>$where['staff_id']])
  98. ->where(array('check_status'=>['in',[0,1]]))->sum('money');
  99. $nomoney = ConsumeModel::where(['staff_id'=>$where['staff_id']])
  100. ->where(array('check_status'=>['in',[3,4]]))->sum('money');
  101. $moneyinfo['remoney'] = $remoney;//已回款
  102. $moneyinfo['inmoney'] = $inmoney;//回款中
  103. $moneyinfo['nomoney'] = $nomoney;//未回款
  104. $moneyinfo['allmoney'] = $remoney+$inmoney+$nomoney;//总金额
  105. $records['moneyinfo']=$moneyinfo;
  106. $this->success('请求成功', $records);
  107. }
  108. //获取费用详情
  109. public function getDetail()
  110. {
  111. $id = input('id', '', 'intval');
  112. $consume = ConsumeModel::where(['id' => $id])->with(['staff','customer'])->find();
  113. if(empty($consume)){
  114. $this->error('数据不存在');
  115. }
  116. $consume['files'] = File::where(['id' => ['in', explode(',', $consume['file_ids'])]])->field('id,types,name,file_path')->select();
  117. if ($consume['check_status'] == 0 || $consume['check_status'] == 1) {
  118. $consume['is_examine'] = ExamineRecord::isExaminse(ExamineRecord::CONSUME_TYPE, $id);
  119. } else {
  120. $consume['is_examine'] = 0;
  121. }
  122. //标记通知已读
  123. Message::setRead(Message::CONSUME_TYPE, $id, $this->auth->id);
  124. $this->success('请求成功', $consume);
  125. }
  126. }