ExamineRecord.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\controller\qingdong\examine;
  3. use addons\qingdong\model\Flow;
  4. use addons\qingdong\model\Message;
  5. use addons\qingdong\model\Staff;
  6. use addons\qingdong\model\ExamineRecord as ExamineRecordModel;
  7. use addons\qingdong\model\BusinessStatus;
  8. use addons\qingdong\model\Business;
  9. use app\admin\controller\qingdong\Base;
  10. use think\Db;
  11. use think\Exception;
  12. use think\Log;
  13. /**
  14. * 审批记录
  15. */
  16. class ExamineRecord extends Base {
  17. protected $noNeedLogin = [];
  18. protected $noNeedRight = [];
  19. /**
  20. * 审核
  21. */
  22. public function examine() {
  23. $relation_type = input('relation_type');
  24. $relation_id = input('relation_id');
  25. $content = input('content','');
  26. $status = input('status');
  27. $record = ExamineRecordModel::where([
  28. 'relation_type' => $relation_type,
  29. 'relation_id' => $relation_id,
  30. 'status' => 0,
  31. 'check_staff_id' => $this->_staff->id
  32. ])->find();
  33. if (empty($record)) {
  34. $this->error('没有待审核数据');
  35. }
  36. if ($this->request->isAjax()) {
  37. $staff = Staff::info();
  38. Db::startTrans();
  39. try {
  40. $model = new ExamineRecordModel();
  41. if ($model->isUpdate(true)->save([
  42. 'status' => $status,
  43. 'content' => $content,
  44. 'check_time' => time()
  45. ], ['id' => $record['id']]) == false) {
  46. throw new Exception('修改失败');
  47. }
  48. switch ($relation_type) {
  49. case $model::CONSUME_TYPE://费用
  50. $rowModel = new \addons\qingdong\model\Consume();
  51. $row = $rowModel->where(['id' => $relation_id])->find();
  52. break;
  53. case $model::CONTRACT_TYPE://合同
  54. $rowModel = new \addons\qingdong\model\Contract();
  55. $row = $rowModel->where(['id' => $relation_id])->find();
  56. break;
  57. case $model::RECEIVABLES_TYPE://回款
  58. $rowModel = new \addons\qingdong\model\Receivables();
  59. $row = $rowModel->where(['id' => $relation_id])->find();
  60. break;
  61. case $model::ACHIEVEMENT_TYPE://业绩目标
  62. $rowModel = new \addons\qingdong\model\AchievementRecords();
  63. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  64. break;
  65. case $model::APPROVAL_TYPE://审批
  66. $rowModel = new \addons\qingdong\model\Approval();
  67. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  68. $row['staff_id'] = $row['create_staff_id'];
  69. break;
  70. default:
  71. throw new Exception('参数错误');
  72. }
  73. if ($message = Message::where(['relation_type' => 'examine', 'relation_id' => $record['id'], 'to_staff_id' => $this->auth->id])->find()) {
  74. Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);
  75. }
  76. $check_staff_ids = explode(',', trim($row['check_staff_ids'], ','));
  77. $check_staff_ids[] = $staff->id;
  78. if ($status == 1) {//审核通过
  79. $flow = Flow::getstepdetail($relation_type, $relation_id);
  80. //给下一审核人发送通知
  81. $result = Flow::sendStepRecord($flow, $relation_type, $relation_id, $check_staff_ids, $staff->id);
  82. //已完成审核
  83. if ($result['status'] == true) {
  84. $rowModel->save([
  85. 'check_status' => 2,
  86. 'check_staff_ids' => implode(',', $check_staff_ids),
  87. 'order_id' => $result['order_id']
  88. ], ['id' => $relation_id]);
  89. switch ($relation_type) {
  90. case $model::CONTRACT_TYPE://合同
  91. \addons\qingdong\model\Customer::where(['id' => $row['customer_id']])->update(['contract_status' => 1]);
  92. //合同签署成功后商机变为赢单
  93. if($row['business_id']){
  94. BusinessStatus::create(array('business_id'=>$row['business_id'],'type'=>4,'remark'=>'合同签署'));
  95. Business::where(array('id'=>$row['business_id']))->update(array('contract_status'=>1,'updatetime'=>time()));
  96. }
  97. break;
  98. case $model::ACHIEVEMENT_TYPE://业绩目标
  99. $m = new \addons\qingdong\model\Achievement();
  100. $m->where(['type' => $row['type'],'status'=>$row['status'], 'obj_id' => $row['obj_id']])->delete();
  101. unset($row['id']);
  102. unset($row['createtime']);
  103. unset($row['updatetime']);
  104. unset($row['deletetime']);
  105. $m->allowField(true)->save($row);
  106. $row['staff_id'] = $row['obj_id'];
  107. break;
  108. case $model::RECEIVABLES_TYPE://回款
  109. $m = new \addons\qingdong\model\Contract();
  110. $contract = $m->where(['id' => $row['contract_id']])->with(['receivables'])->find();
  111. if ($contract['receivables']['repayment_money'] >= $contract['money']) {
  112. $m->save(['contract_status' => 1], ['id' => $row['contract_id']]);//已完成
  113. }
  114. break;
  115. }
  116. Message::addMessage(Message::EXAMINE_ADOPT_TYPE, $record['id'], $row['owner_staff_id'] ?? $row['staff_id'], $staff->id);
  117. //删除 或签的待审批通知
  118. Message::setRead(Message::EXAMINE_TYPE, $record['id']);
  119. ExamineRecordModel::where([
  120. 'relation_type' => $relation_type,
  121. 'relation_id' => $relation_id,
  122. 'status' => 0,
  123. ])->update(['status' => 3, 'check_time' => time()]);
  124. } else {
  125. $rowModel->save([
  126. 'check_staff_ids' => implode(',', $check_staff_ids),
  127. 'order_id' => $result['order_id']
  128. ], ['id' => $relation_id]);
  129. }
  130. } else {
  131. //审核未通过
  132. $rowModel->save(['check_status' => 3, 'check_staff_ids' => ''], ['id' => $relation_id]);
  133. Message::addMessage(Message::EXAMINE_REFUSE_TYPE, $record['id'], $row['owner_staff_id'] ?? $row['staff_id'], $staff->id, '');
  134. //删除待审批通知
  135. $ids = ExamineRecordModel::where([
  136. 'relation_type' => $relation_type,
  137. 'relation_id' => $relation_id,
  138. 'status' => 0,
  139. ])->column('id');
  140. Message::where(['relation_type' => Message::EXAMINE_TYPE, 'relation_id' => ['in', $ids], 'status' => 0])->update(['read_time' => time(), 'status' => 1]);
  141. ExamineRecordModel::where([
  142. 'relation_type' => $relation_type,
  143. 'relation_id' => $relation_id,
  144. 'status' => 0,
  145. ])->update(['status' => 3, 'check_time' => time()]);
  146. }
  147. Db::commit();
  148. } catch (Exception $e) {
  149. Db::rollback();
  150. Log::record($e->getMessage());
  151. $this->error($e->getMessage());
  152. }
  153. $this->success('审核成功');
  154. }
  155. $this->assign('relation_type',$relation_type);
  156. $this->assign('relation_id',$relation_id);
  157. $this->assign('status',$status);
  158. return $this->view->fetch();
  159. }
  160. }