ExamineRecord.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Business;
  4. use addons\qingdongams\model\BusinessStatus;
  5. use addons\qingdongams\model\Flow;
  6. use addons\qingdongams\model\FormApproval;
  7. use addons\qingdongams\model\Invoice;
  8. use addons\qingdongams\model\Message;
  9. use addons\qingdongams\model\Staff;
  10. use addons\qingdongams\model\ExamineRecord as ExamineRecordModel;
  11. use addons\qingdongams\model\Order;
  12. use addons\qingdongams\model\PartsStock;
  13. use addons\qingdongams\model\Workorder;
  14. use addons\qingdongams\model\Quote;
  15. use addons\qingdongams\model\Consume;
  16. use addons\qingdongams\model\AchievementRecords;
  17. use addons\qingdongams\model\PartsStockReload;
  18. use addons\qingdongams\model\Contract;
  19. use addons\qingdongams\model\Receivables;
  20. use addons\qingdongams\model\Approval;
  21. use addons\qingdongams\model\CustomerProduct;
  22. use addons\qingdongams\model\Customer;
  23. use addons\qingdongams\model\Achievement;
  24. use think\Db;
  25. use think\Exception;
  26. use think\Log;
  27. /**
  28. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  29. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  30. * @desc 售后微信:qingdong_crm
  31. */
  32. /**
  33. * 审批记录
  34. */
  35. class ExamineRecord extends StaffApi
  36. {
  37. protected $noNeedLogin = [];
  38. protected $noNeedRight = [];
  39. //审批记录
  40. public function getList()
  41. {
  42. $relation_type = input('relation_type');
  43. $relation_id = input('relation_id');
  44. $list = ExamineRecordModel::where([
  45. 'relation_type' => $relation_type,
  46. 'relation_id' => $relation_id,
  47. ])->with('checkStaff')->select();
  48. $list = collection($list)->toArray();
  49. $this->success('请求成功', $list);
  50. }
  51. //审核
  52. public function examine()
  53. {
  54. $relation_type = input('relation_type');
  55. $relation_id = input('relation_id');
  56. $content = input('content', '');
  57. $status = input('status');
  58. $record = ExamineRecordModel::where([
  59. 'relation_type' => $relation_type,
  60. 'relation_id' => $relation_id,
  61. 'status' => 0,
  62. ])->find();
  63. if (empty($record)) {
  64. $this->error('没有待审核数据');
  65. }
  66. $staff = Staff::info();
  67. Db::startTrans();
  68. try {
  69. $model = new ExamineRecordModel();
  70. if ($model->isUpdate(true)->save([
  71. 'status' => $status,
  72. 'content' => $content,
  73. 'check_time' => time(),
  74. 'remark' => ''
  75. ], ['id' => $record['id']]) == false) {
  76. throw new Exception('修改失败');
  77. }
  78. switch ($relation_type) {
  79. case $model::CONSUME_TYPE://费用
  80. $rowModel = new Consume();
  81. $row = $rowModel->where(['id' => $relation_id])->find();
  82. break;
  83. case $model::CONTRACT_TYPE://合同
  84. $rowModel = new Contract();
  85. $row = $rowModel->where(['id' => $relation_id])->find();
  86. break;
  87. case $model::RECEIVABLES_TYPE://回款
  88. $rowModel = new Receivables();
  89. $row = $rowModel->where(['id' => $relation_id])->find();
  90. break;
  91. case $model::ACHIEVEMENT_TYPE://业绩目标
  92. $rowModel = new AchievementRecords();
  93. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  94. break;
  95. case $model::APPROVAL_TYPE://工作审批
  96. $rowModel = new Approval();
  97. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  98. $row['staff_id'] = $row['create_staff_id'];
  99. break;
  100. case $model::PARTS_TYPE://零件
  101. $rowModel = new PartsStockReload();
  102. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  103. $row['staff_id'] = $row['create_staff_id'];
  104. break;
  105. case $model::QUOTE_TYPE://报价单
  106. $rowModel = new Quote();
  107. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  108. $row['staff_id'] = $row['create_staff_id'];
  109. break;
  110. case $model::WORKORDER_TYPE://工单
  111. $rowModel = new Workorder();
  112. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  113. $row['staff_id'] = $row['create_staff_id'];
  114. break;
  115. case $model::INVOICE_TYPE://发票
  116. $rowModel = new Invoice();
  117. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  118. $row['staff_id'] = $row['create_staff_id'];
  119. break;
  120. default:
  121. throw new Exception('参数错误');
  122. }
  123. if ($message = Message::where(['relation_type' => 'examine', 'relation_id' => $record['id'], 'to_staff_id' => $this->auth->id])->find()) {
  124. Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);
  125. }
  126. $check_staff_ids = explode(',', trim($row['check_staff_ids'], ','));
  127. $check_staff_ids[] = $staff->id;
  128. if ($status == 1) {//审核通过
  129. $flow = Flow::getstepdetail($relation_type, $relation_id);
  130. //给下一审核人发送通知
  131. $result = Flow::sendStepRecord($flow, $relation_type, $relation_id, $check_staff_ids, $staff->id);
  132. //已完成审核
  133. if ($result['status'] == true) {
  134. $rowModel->save([
  135. 'check_status' => 2,
  136. 'check_staff_ids' => implode(',', $check_staff_ids),
  137. 'order_id' => $result['order_id']
  138. ], ['id' => $relation_id]);
  139. switch ($relation_type) {
  140. case $model::CONTRACT_TYPE://合同
  141. Customer::where(['id' => $row['customer_id']])->update(['contract_status' => 1]);
  142. //合同签署成功后商机变为赢单
  143. if ($row['business_id']) {
  144. BusinessStatus::create(['business_id' => $row['business_id'], 'type' => 4, 'remark' => '合同签署']);
  145. Business::where(['id' => $row['business_id']])->update(['contract_status' => 1, 'updatetime' => time()]);
  146. }
  147. $customerUpdate = ['contract_status' => 1, 'trade_date' => date('Y-m-d')];
  148. $customerInfo = Customer::where(['id' => $row['customer_id']])->find();
  149. if (!in_array($customerInfo['follow'], ['已经购买'])) {
  150. $customerUpdate['follow'] = '准备付款';
  151. }
  152. Customer::where(['id' => $row['customer_id']])
  153. ->update($customerUpdate);
  154. $orderModel = new Order();
  155. $order = [
  156. 'customer_id' => $row['customer_id'],
  157. 'contacts_id' => $row['contacts_id'],
  158. 'contract_id' => $row['id'],
  159. 'create_staff_id' => $row['owner_staff_id'],
  160. 'owner_staff_id' => $row['owner_staff_id'],
  161. ];
  162. $orderModel->isUpdate(false)->save($order);
  163. $order_id = $orderModel->getLastInsID();
  164. break;
  165. case $model::ACHIEVEMENT_TYPE://业绩目标
  166. $m = new Achievement();
  167. $m->where(['type' => $row['type'], 'status' => $row['status'], 'obj_id' => $row['obj_id']])->delete();
  168. unset($row['id']);
  169. unset($row['createtime']);
  170. unset($row['updatetime']);
  171. unset($row['deletetime']);
  172. $m->allowField(true)->save($row);
  173. $row['staff_id'] = $row['obj_id'];
  174. break;
  175. /*case $model::ACHIEVEMENT_TYPE://业绩目标
  176. $m = new Achievement();
  177. $m->where([ 'type' => $row['type'], 'obj_id' => $row['obj_id']])->delete();
  178. unset($row['id']);
  179. $monthAchievement = round($row['yeartarget'] / 12, 2);
  180. $row['january'] = $row['february'] = $row['march'] = $row['april'] = $row['may'] = $row['june']
  181. = $row['july'] = $row['august'] = $row['september'] = $row['october']
  182. = $row['november'] = $row['december'] = $monthAchievement;
  183. $row['createtime']=strtotime($row['createtime']);
  184. $m->allowField(true)->save($row);
  185. $row['staff_id'] = $row['obj_id'];
  186. break;*/
  187. case $model::RECEIVABLES_TYPE://回款
  188. $m = new Contract();
  189. $contract = $m->where(['id' => $row['contract_id']])->with(['receivables'])->find();
  190. if (!empty($contract['receivables']) && $contract['receivables']['repayment_money'] >= $contract['money']) {
  191. Customer::where(['id' => $row['customer_id']])->update(['follow' => '已经购买']);
  192. $m->save(['contract_status' => 1], ['id' => $row['contract_id']]);//已完成
  193. }
  194. break;
  195. case $model::PARTS_TYPE://零件出入库
  196. $parts = $row['parts'];
  197. $add = [
  198. 'type' => $row['type'],
  199. 'odd_numbers' => $row['odd_numbers'],
  200. 'storage_time' => $row['storage_time'],
  201. 'desc' => $row['desc'],
  202. 'create_staff_id' => $row['create_staff_id'],
  203. ];
  204. foreach ($parts as $v) {
  205. $add['parts_id'] = $v['id'];
  206. $add['number'] = $v['number'];
  207. PartsStock::addStock($add);
  208. }
  209. break;
  210. case $model::WORKORDER_TYPE://工单
  211. $update = [];
  212. if ($row['is_charge'] == 2 || $row['money'] == 0) {
  213. if ($row['workorder_type'] == '返厂维修') {
  214. $update['process'] = 5;
  215. } else if (in_array($row['workorder_type'], ['上门维修', '配件安装', '电话售后'])) {
  216. $update['status'] = 3;
  217. Workorder::setComplete($row['id']);
  218. }
  219. } else {
  220. $update['process'] = 4;
  221. }
  222. Workorder::where(['id' => $row['id']])->update($update);
  223. Message::addMessage($relation_type, $relation_id, $row['owner_staff_id'], $staff->id,
  224. "工单《{$row['workorder_number']}》费用审核成功,请及时查看!");
  225. break;
  226. case $model::QUOTE_TYPE:
  227. Quote::where(['id' => $row['id']])->update([
  228. 'status' => 1]);
  229. break;
  230. }
  231. Message::addMessage(Message::EXAMINE_ADOPT_TYPE, $record['id'], $row['owner_staff_id'] ?? $row['staff_id'], $staff->id);
  232. //删除 或签的待审批通知
  233. Message::setRead(Message::EXAMINE_TYPE, $record['id']);
  234. ExamineRecordModel::where([
  235. 'relation_type' => $relation_type,
  236. 'relation_id' => $relation_id,
  237. 'status' => 0,
  238. ])->update(['status' => 3, 'check_time' => time()]);
  239. } else {
  240. $rowModel->save([
  241. 'check_staff_ids' => implode(',', $check_staff_ids),
  242. 'order_id' => $result['order_id']
  243. ], ['id' => $relation_id]);
  244. }
  245. } else {
  246. //审核未通过
  247. $rowModel->save(['check_status' => 3, 'check_staff_ids' => ''], ['id' => $relation_id]);
  248. Message::addMessage(Message::EXAMINE_REFUSE_TYPE, $record['id'], $row['owner_staff_id'] ?? $row['staff_id'], $staff->id, '');
  249. //删除待审批通知
  250. $ids = ExamineRecordModel::where([
  251. 'relation_type' => $relation_type,
  252. 'relation_id' => $relation_id,
  253. 'status' => 0,
  254. ])->column('id');
  255. Message::where(['relation_type' => Message::EXAMINE_TYPE, 'relation_id' => ['in', $ids], 'status' => 0])->update(['read_time' => time(), 'status' => 1]);
  256. ExamineRecordModel::where([
  257. 'relation_type' => $relation_type,
  258. 'relation_id' => $relation_id,
  259. 'status' => 0,
  260. ])->update(['status' => 3, 'check_time' => time()]);
  261. }
  262. Db::commit();
  263. } catch (Exception $e) {
  264. Db::rollback();
  265. $this->error($e->getMessage());
  266. }
  267. $this->success('审核成功');
  268. }
  269. //批量审核费用
  270. public function examine_consume()
  271. {
  272. $relation_type = input('relation_type');
  273. $relation_id = input('relation_id');
  274. $content = input('content');
  275. $status = input('status');
  276. $relation_id = explode(',', $relation_id);
  277. if (empty($relation_id)) {
  278. $this->error('参数错误');
  279. }
  280. $record = ExamineRecordModel::where([
  281. 'relation_type' => $relation_type,
  282. 'relation_id' => ['in', $relation_id],
  283. 'status' => ['neq', 0],
  284. ])->select();
  285. if (!empty($record)) {
  286. $this->error('有已审核数据,请刷新页面');
  287. }
  288. $saveIds = ExamineRecordModel::where([
  289. 'relation_type' => $relation_type,
  290. 'relation_id' => ['in', $relation_id],
  291. ])->column('id');
  292. $staff = Staff::info();
  293. Db::startTrans();
  294. try {
  295. $model = new ExamineRecordModel();
  296. if ($model->isUpdate(true)->save([
  297. 'status' => $status,
  298. 'content' => $content,
  299. 'check_time' => time()
  300. ], ['id' => ['in', $saveIds]]) == false) {
  301. throw new Exception('修改失败');
  302. }
  303. switch ($relation_type) {
  304. case $model::CONSUME_TYPE://费用
  305. $rowModel = new Consume();
  306. $row = $rowModel->where(['id' => ['in', $relation_id]])->select();
  307. $row = collection($row)->toArray();
  308. break;
  309. default:
  310. throw new Exception('参数错误');
  311. }
  312. if ($message_ids = Message::where(['relation_type' => 'examine', 'relation_id' => ['in', $saveIds], 'to_staff_id' => $this->auth->id])->column('id')) {
  313. Message::where(['id' => ['in', $message_ids]])->update(['status' => 1, 'read_time' => time()]);
  314. }
  315. foreach ($row as $r) {
  316. $rowModel = new Consume();
  317. $flow_staff_ids = explode(',', trim($r['flow_staff_ids'], ','));
  318. $check_staff_ids = explode(',', trim($r['check_staff_ids'], ','));
  319. $check_staff_ids[] = $staff->id;
  320. $diff = array_diff($flow_staff_ids, $check_staff_ids);
  321. if ($status == 1) {//审核通过
  322. //记录
  323. if (empty($diff)) {
  324. $rowModel->save([
  325. 'check_status' => 2,
  326. 'check_staff_ids' => implode(',', $check_staff_ids)
  327. ], ['id' => $r['id']]);
  328. Message::addMessage($relation_type, $r['id'], $r['owner_staff_id'] ?? $r['staff_id'], $staff->id, '您提交的审批已审核通过!');
  329. } else {
  330. $diff = array_values($diff);
  331. if ($relation_type == $model::CONSUME_TYPE) {
  332. $rowModel->save(['check_staff_ids' => implode(',', $check_staff_ids), 'next_staff_id' => $diff[0], 'check_status' => 1], ['id' => $r['id']]);
  333. } else {
  334. $rowModel->save(['check_staff_ids' => implode(',', $check_staff_ids)], ['id' => $r['id']]);
  335. }
  336. $model::addExaminse($relation_type, $r['id'], $diff[0]);
  337. }
  338. } else {
  339. $rowModel->save(['check_status' => 3, 'check_staff_ids' => implode(',', $check_staff_ids)], ['id' => $r['id']]);
  340. Message::addMessage($relation_type, $r['id'], $row['owner_staff_id'] ?? $row['staff_id'], $staff->id, '您提交的审批被拒绝!');
  341. }
  342. }
  343. Db::commit();
  344. } catch (Exception $e) {
  345. Db::rollback();
  346. $this->error($e->getMessage());
  347. }
  348. $this->success('审核成功');
  349. }
  350. //加签
  351. public function addExamineStaff()
  352. {
  353. $relation_type = input('relation_type');
  354. $relation_id = input('relation_id');
  355. $staff_id = input('staff_id');
  356. $remark = input('remark/a');
  357. $model = new ExamineRecordModel();
  358. switch ($relation_type) {
  359. case $model::CONSUME_TYPE://费用
  360. $rowModel = new Consume();
  361. break;
  362. case $model::CONTRACT_TYPE://合同
  363. $rowModel = new Contract();
  364. break;
  365. case $model::RECEIVABLES_TYPE://回款
  366. $rowModel = new Receivables();
  367. break;
  368. case $model::ACHIEVEMENT_TYPE://业绩目标
  369. $rowModel = new AchievementRecords();
  370. break;
  371. case $model::PARTS_TYPE://零件
  372. $rowModel = new PartsStockReload();
  373. break;
  374. case $model::APPROVAL_TYPE://工作审批
  375. $rowModel = new Approval();
  376. break;
  377. case $model::QUOTE_TYPE://报价单
  378. $rowModel = new Quote();
  379. break;
  380. case $model::WORKORDER_TYPE://工单
  381. $rowModel = new Workorder();
  382. break;
  383. default:
  384. throw new Exception('参数错误');
  385. }
  386. $examine = ExamineRecordModel::where(['relation_type' => $relation_type, 'relation_id' => $relation_id, 'status' => 0])->find();
  387. ExamineRecordModel::where(['relation_type' => $relation_type, 'relation_id' => $relation_id, 'status' => 0])->update(['deletetime' => time()]);
  388. Message::setRead(Message::EXAMINE_TYPE, $examine['id']);
  389. $row = $rowModel->get($relation_id);
  390. if (empty($row)) {
  391. $this->error('信息不存在');
  392. }
  393. $row->save(['flow_staff_ids' => $staff_id]);
  394. $flow_staff_ids = explode(',', trim($staff_id, ','));
  395. $check_staff_ids = explode(',', trim(isset($row['check_staff_id']) ? $row['check_staff_id'] : $row['check_staff_ids'], ','));
  396. $diff = array_diff($flow_staff_ids, $check_staff_ids);
  397. $diff = array_values($diff);
  398. Message::setRead($relation_type, $relation_id);
  399. $model::addExaminse($relation_type, $relation_id, $diff[0]);
  400. $this->success('请求成功');
  401. }
  402. //审核人列表
  403. public function flowStaffList()
  404. {
  405. $relation_type = input('relation_type');
  406. $relation_id = input('relation_id');
  407. $model = new ExamineRecordModel();
  408. switch ($relation_type) {
  409. case $model::CONSUME_TYPE://费用
  410. $rowModel = new Consume();
  411. break;
  412. case $model::CONTRACT_TYPE://合同
  413. $rowModel = new Contract();
  414. break;
  415. case $model::RECEIVABLES_TYPE://回款
  416. $rowModel = new Receivables();
  417. break;
  418. case $model::ACHIEVEMENT_TYPE://业绩目标
  419. $rowModel = new AchievementRecords();
  420. break;
  421. case $model::PARTS_TYPE://零件
  422. $rowModel = new PartsStockReload();
  423. break;
  424. case $model::APPROVAL_TYPE://工作审批
  425. $rowModel = new Approval();
  426. break;
  427. case $model::QUOTE_TYPE://报价单
  428. $rowModel = new Quote();
  429. break;
  430. case $model::WORKORDER_TYPE://工单
  431. $rowModel = new Workorder();
  432. break;
  433. case $model::INVOICE_TYPE://发票
  434. $rowModel = new Invoice();
  435. break;
  436. default:
  437. throw new Exception('参数错误');
  438. }
  439. $row = $rowModel->get($relation_id);
  440. if (empty($row)) {
  441. $this->error('信息不存在');
  442. }
  443. $flow_staff_id = explode(',', $row->flow_staff_ids);
  444. $staff = Staff::where(['id' => ['in', $flow_staff_id]])->column('id,name,img,post', 'id');
  445. $data = [];
  446. foreach ($flow_staff_id as $id) {
  447. $data[] = $staff[$id] ?? [];
  448. }
  449. $this->success('请求成功', $data);
  450. }
  451. /**
  452. * 获取审批列表
  453. */
  454. public function get_examine_list()
  455. {
  456. $limit = input("limit/d", 10);
  457. //合同审批,回款审批、业绩目标审批、办公审批 费用
  458. $relation_type = input('type');
  459. $status = input('status', 0);
  460. if ($status == 1) {
  461. $status = array('in', '1,2');
  462. }
  463. $data = ExamineRecordModel::where(['relation_type' => $relation_type,
  464. 'status' => $status,
  465. 'check_staff_id' => $this->auth->id])->order('id desc')->paginate($limit)->toArray();
  466. if ($data['data']) {
  467. foreach ($data['data'] as $k => $v) {
  468. $data['data'][$k]['content_info'] = Message::where(array('relation_id' => $v['id'],
  469. 'relation_type' => 'examine'))->value('content')?:$v['content'];
  470. if ($v['relation_type'] == ExamineRecordModel::CONSUME_TYPE) {
  471. $data['data'][$k]['relation_name'] = Consume::where(['id' => $v['relation_id']])->value('consume_type');
  472. } elseif ($v['relation_type'] == ExamineRecordModel::CONTRACT_TYPE) {
  473. $data['data'][$k]['relation_name'] = Contract::where(['id' => $v['relation_id']])->value('name');
  474. } elseif ($v['relation_type'] == ExamineRecordModel::RECEIVABLES_TYPE) {
  475. $data['data'][$k]['relation_name'] = Receivables::where(['id' => $v['relation_id']])->value('number');
  476. } elseif ($v['relation_type'] == ExamineRecordModel::ACHIEVEMENT_TYPE) {
  477. $data['data'][$k]['relation_name'] = AchievementRecords::where(['id' => $v['relation_id']])->value('year');;
  478. } elseif ($v['relation_type'] == ExamineRecordModel::APPROVAL_TYPE) {
  479. $approval = Approval::where(['id' => $v['relation_id']])->value('formapproval_id');
  480. $data['data'][$k]['relation_name'] = '';
  481. if ($approval) {
  482. $data['data'][$k]['relation_name'] = FormApproval::where(['id' => $approval])->value('name');
  483. }
  484. } elseif ($v['relation_type'] == ExamineRecordModel::CARD_TYPE) {
  485. $data['data'][$k]['relation_name'] = '补卡';
  486. } elseif ($v['relation_type'] == ExamineRecordModel::LEAVE_TYPE) {
  487. $data['data'][$k]['relation_name'] = '请假';
  488. }elseif ($v['relation_type'] == ExamineRecordModel::INVOICE_TYPE) {
  489. $data['data'][$k]['relation_name'] = Invoice::where(['id' => $v['relation_id']])->value('number');;
  490. } else {
  491. $data['data'][$k]['relation_name'] = '';
  492. }
  493. }
  494. }
  495. $this->success('请求成功', $data);
  496. }
  497. }