Consume.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace app\admin\controller\qingdongams\customer;
  3. use addons\qingdongams\model\ConsumeDetail;
  4. use addons\qingdongams\model\ExamineRecord;
  5. use addons\qingdongams\model\File;
  6. use addons\qingdongams\model\Flow;
  7. use addons\qingdongams\model\Message;
  8. use addons\qingdongams\model\Staff;
  9. use app\admin\controller\qingdongams\Base;
  10. use addons\qingdongams\model\ContractOther;
  11. use addons\qingdongams\model\Form;
  12. use addons\qingdongams\model\Contacts;
  13. use addons\qingdongams\model\Customer;
  14. use addons\qingdongams\model\OperationLog;
  15. use addons\qingdongams\model\Record;
  16. use addons\qingdongams\model\ContractFile;
  17. use think\Db;
  18. use think\Exception;
  19. /**
  20. * 费用管理
  21. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  22. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  23. * @desc 售后微信:qingdong_crm
  24. */
  25. class Consume extends Base
  26. {
  27. protected $relationSearch = true;
  28. /**
  29. * @var \addons\qingdongams\model\Consume
  30. */
  31. protected $model = null;
  32. public function _initialize()
  33. {
  34. parent::_initialize();
  35. $this->model = new \addons\qingdongams\model\Consume;
  36. }
  37. /**
  38. * 查看
  39. */
  40. public function index()
  41. {
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. //0:全部 1:我负责的 2:下属负责的
  49. $type = input('type', 0);
  50. switch ($type) {
  51. case 1:
  52. $staff = Staff::info();
  53. $wheres['staff_id'] = $staff->id;
  54. break;
  55. case 2:
  56. $wheres['staff_id'] = array('in', Staff::getLowerStaffId());
  57. break;
  58. default:
  59. $wheres['staff_id'] = array('in', Staff::getMyStaffIds());
  60. break;
  61. }
  62. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  63. $list = $this->model->where($where)->where($wheres)->with(['customer', 'staff'])->order($sort, $order)->paginate($limit);
  64. $result = array("total" => $list->total(), "rows" => $list->items());
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 添加
  71. */
  72. public function add()
  73. {
  74. if ($this->request->isPost()) {
  75. $params = $this->request->post("row/a");
  76. if ($params) {
  77. $params = $this->preExcludeFields($params);
  78. // 表单验证
  79. if (($result = $this->qingdongamsValidate($params, 'Consume', 'create')) !== true) {
  80. $this->error($result);
  81. }
  82. $result = false;
  83. $data = $this->request->post('row/a');
  84. // if(isset($data['flow_staff_ids'])){
  85. // if(!$data['flow_staff_ids']){
  86. // $this->error('请选择审批人');
  87. // }
  88. // }
  89. $params['staff_id'] = $this->_staff->id;
  90. $params['customer_id'] = $data['customer_id'];
  91. $params['money'] = $data['money'];
  92. $params['number'] = $data['number'];
  93. $params['submit_date'] = $data['submit_date'];
  94. $params['detail'] = $data['detail']??[];
  95. Db::startTrans();
  96. try {
  97. $result = $this->model::createConsume($params);
  98. Db::commit();
  99. } catch (Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. if ($result !== false) {
  104. $this->success('添加成功');
  105. } else {
  106. $this->error(__('No rows were inserted'));
  107. }
  108. }
  109. $this->error(__('Parameter %s can not be empty', ''));
  110. }
  111. $flow = Flow::getsteplist(Flow::CONSUME_STATUS);
  112. if (empty($flow)) {
  113. $this->error('无可用审批流,请联系管理员');
  114. }
  115. $customer_id = input('customer_id', null);
  116. $this->assign('customer_id', $customer_id);
  117. $this->assign('relation_type', input('relation_type'));
  118. $this->assign('relation_id', input('relation_id'));
  119. $this->assign('customer', Customer::get($customer_id));
  120. $this->assign('flow', $flow);
  121. $this->assign('staff', Staff::getStaff());
  122. $this->assign('number',$this->model::getNumber());
  123. $this->assign('consumetype',$this->model::getconsumetype());
  124. return $this->view->fetch();
  125. }
  126. private function operateParasm($datas)
  127. {
  128. $detail_field = ['consume_type', 'consume_date', 'car_number', 'consume_money', 'start_mileage', 'end_mileage', 'mileage', 'file_ids', 'remark', 'detail_id'];
  129. $list = [];
  130. foreach ($datas as $k => $v) {
  131. if (in_array($k, $detail_field) && is_array($v)) {
  132. foreach ($v as $key => $val) {
  133. if ($k == 'detail_id') {
  134. $k = 'id';
  135. }
  136. $list[$key][$k] = $val;
  137. }
  138. }
  139. }
  140. return array_values($list);
  141. }
  142. /**
  143. * 修改费用
  144. */
  145. public function edit($ids = null)
  146. {
  147. $map['id'] = $ids;
  148. if ($this->request->isAjax()) {
  149. $params = $this->request->post('row/a');
  150. $params = $this->preExcludeFields($params);
  151. $row = $this->model::where(['id' => $ids, 'check_status' => ['in', [0, 1, 3, 4, 5]]])->find();
  152. if (empty($row)) {
  153. $this->error('审核通过的费用不可编辑');
  154. }
  155. $data = $this->request->post('row/a');
  156. $params['staff_id'] = $this->_staff->id;
  157. $params['customer_id'] = $data['customer_id'];
  158. $params['money'] = $data['money'];
  159. $params['number'] = $data['number'];
  160. $params['submit_date'] = $data['submit_date'];
  161. $params['detail'] = $data['detail']??[];
  162. Db::startTrans();
  163. try {
  164. $params['id'] = $ids;
  165. $result = $this->model::updateConsume($params);
  166. Db::commit();
  167. } catch (Exception $e) {
  168. Db::rollback();
  169. $this->error($e->getMessage());
  170. }
  171. if ($result !== false) {
  172. $this->success('修改成功');
  173. } else {
  174. $this->error('修改失败');
  175. }
  176. }
  177. $row = $this->model->where($map)->with(['customer'])->find();
  178. if ($row['file_ids']) {
  179. $wheres['id'] = array('in', $row['file_ids']);
  180. $file = File::where($wheres)->column('file_path');
  181. $row['file_ids'] = implode(',', $file);
  182. }
  183. $flow = Flow::getsteplist(Flow::CONSUME_STATUS);
  184. if (empty($flow)) {
  185. $this->error('无可用审批流,请联系管理员');
  186. }
  187. $this->assign('flow', $flow);
  188. $row=$row->toArray();
  189. $detail=ConsumeDetail::where(['consume_id'=>$ids])->select();
  190. if($detail){
  191. $detail=collection($detail)->toArray();
  192. }
  193. $row['detail']= $detail;
  194. foreach ($row['detail'] as &$d){
  195. $file_path=File::where(['id'=>['in',explode(',',$d['file_ids'])]])->column('file_path');
  196. $d['file_ids']=implode(',',$file_path);
  197. }
  198. $this->view->assign("row", $row);
  199. $this->assign('staff', Staff::getStaff());
  200. $this->assign('consumetype',$this->model::getconsumetype());
  201. return $this->view->fetch();
  202. }
  203. /**
  204. * 费用详情
  205. */
  206. public function detail($ids = null)
  207. {
  208. $row = $this->model->with(['staff', 'customer', 'followStaff'])->where([
  209. 'id' => $ids,
  210. ])->find();
  211. if (empty($row)) {
  212. $this->error(__('No Results were found'));
  213. }
  214. $row['detail']= ConsumeDetail::where(['consume_id'=>$ids])->select();
  215. $this->assign('flow', Flow::getstepdetail(Flow::CONSUME_STATUS, $ids));
  216. //标记通知已读
  217. Message::setRead(Message::CONSUME_TYPE, $ids, $this->_staff->id);
  218. //审批记录
  219. $this->assign('examine_record', ExamineRecord::getList(ExamineRecord::CONSUME_TYPE, $ids));
  220. $this->assign('row', $row);
  221. $this->assign('ids', $ids);
  222. return $this->view->fetch();
  223. }
  224. /**
  225. * 删除
  226. */
  227. public function del($ids = "")
  228. {
  229. if (!$this->request->isPost()) {
  230. $this->error(__("Invalid parameters"));
  231. }
  232. $ids = $ids ? $ids : $this->request->post("ids");
  233. $row = $this->model::where(['id' => $ids, 'check_status' => ['in', [0, 1, 3, 4, 5]]])->find();
  234. if (empty($row)) {
  235. $this->error('审核通过的费用不可删除');
  236. }
  237. $row = $this->model->get($ids);
  238. $this->modelValidate = true;
  239. if (!$row) {
  240. $this->error(__('No Results were found'));
  241. }
  242. $row->delete();
  243. $this->success();
  244. }
  245. /**
  246. * 交通费
  247. */
  248. public function clf() {
  249. return $this->view->fetch('clf');
  250. }
  251. /**
  252. * 其他
  253. */
  254. public function other() {
  255. return $this->view->fetch('other');
  256. }
  257. /**
  258. * 费用统计
  259. */
  260. public function consume_count() {
  261. $number=input('number');
  262. $this->success('请求成功','',['money'=>$this->model::getMileageMoney($number)]);
  263. }
  264. }