Blame view

application/admin/controller/qingdong/examine/ExamineRecord.php 8.49 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  <?php

  

  namespace app\admin\controller\qingdong\examine;

  

  use addons\qingdong\model\Flow;

  use addons\qingdong\model\Message;

  use addons\qingdong\model\Staff;

  use addons\qingdong\model\ExamineRecord as ExamineRecordModel;

  use addons\qingdong\model\BusinessStatus;

  use addons\qingdong\model\Business;

  use app\admin\controller\qingdong\Base;

  use think\Db;

  use think\Exception;

  use think\Log;

  

  /**

   * 审批记录

   */

  class ExamineRecord extends Base {

  	protected $noNeedLogin = [];

  	protected $noNeedRight = [];

  

      /**

       * 审核

       */

  	public function examine() {

  		$relation_type = input('relation_type');

  		$relation_id   = input('relation_id');

  		$content       = input('content','');

  		$status        = input('status');

          $record = ExamineRecordModel::where([

              'relation_type' => $relation_type,

              'relation_id' => $relation_id,

              'status' => 0,

              'check_staff_id' => $this->_staff->id

          ])->find();

          if (empty($record)) {

              $this->error('没有待审核数据');

          }

          if ($this->request->isAjax()) {

              $staff = Staff::info();

              Db::startTrans();

              try {

                  $model = new ExamineRecordModel();

                  if ($model->isUpdate(true)->save([

                          'status' => $status,

                          'content' => $content,

                          'check_time' => time()

                      ], ['id' => $record['id']]) == false) {

                      throw new Exception('修改失败');

                  }

                  switch ($relation_type) {

                      case $model::CONSUME_TYPE://费用

                          $rowModel = new \addons\qingdong\model\Consume();

                          $row = $rowModel->where(['id' => $relation_id])->find();

                          break;

                      case $model::CONTRACT_TYPE://合同

                          $rowModel = new \addons\qingdong\model\Contract();

                          $row = $rowModel->where(['id' => $relation_id])->find();

                          break;

                      case $model::RECEIVABLES_TYPE://回款

                          $rowModel = new \addons\qingdong\model\Receivables();

                          $row = $rowModel->where(['id' => $relation_id])->find();

                          break;

                      case $model::ACHIEVEMENT_TYPE://业绩目标

                          $rowModel = new \addons\qingdong\model\AchievementRecords();

                          $row = $rowModel->where(['id' => $relation_id])->find()->toArray();

                          break;

                      case $model::APPROVAL_TYPE://审批

                          $rowModel = new \addons\qingdong\model\Approval();

                          $row = $rowModel->where(['id' => $relation_id])->find()->toArray();

                          $row['staff_id'] = $row['create_staff_id'];

                          break;

                      default:

                          throw new Exception('参数错误');

                  }

                  if ($message = Message::where(['relation_type' => 'examine', 'relation_id' => $record['id'], 'to_staff_id' => $this->auth->id])->find()) {

                      Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);

                  }

                  $check_staff_ids = explode(',', trim($row['check_staff_ids'], ','));

                  $check_staff_ids[] = $staff->id;

                  if ($status == 1) {//审核通过

                      $flow = Flow::getstepdetail($relation_type, $relation_id);

                      //给下一审核人发送通知

                      $result = Flow::sendStepRecord($flow, $relation_type, $relation_id, $check_staff_ids, $staff->id);

  

                      //已完成审核

                      if ($result['status'] == true) {

                          $rowModel->save([

                              'check_status' => 2,

                              'check_staff_ids' => implode(',', $check_staff_ids),

                              'order_id' => $result['order_id']

                          ], ['id' => $relation_id]);

                          switch ($relation_type) {

                              case $model::CONTRACT_TYPE://合同

                                  \addons\qingdong\model\Customer::where(['id' => $row['customer_id']])->update(['contract_status' => 1]);

                                 //合同签署成功后商机变为赢单

                                 if($row['business_id']){

                                      BusinessStatus::create(array('business_id'=>$row['business_id'],'type'=>4,'remark'=>'合同签署'));

                                     Business::where(array('id'=>$row['business_id']))->update(array('contract_status'=>1,'updatetime'=>time()));

                                 }

                                  break;

                              case $model::ACHIEVEMENT_TYPE://业绩目标

                                  $m = new \addons\qingdong\model\Achievement();

                                  $m->where(['type' => $row['type'],'status'=>$row['status'], 'obj_id' => $row['obj_id']])->delete();

                                  unset($row['id']);

                                  unset($row['createtime']);

                                  unset($row['updatetime']);

                                  unset($row['deletetime']);

                                  $m->allowField(true)->save($row);

                                  $row['staff_id'] = $row['obj_id'];

                                  break;

                              case $model::RECEIVABLES_TYPE://回款

                                  $m = new \addons\qingdong\model\Contract();

                                  $contract = $m->where(['id' => $row['contract_id']])->with(['receivables'])->find();

                                  if ($contract['receivables']['repayment_money'] >= $contract['money']) {

                                      $m->save(['contract_status' => 1], ['id' => $row['contract_id']]);//已完成

                                  }

                                  break;

                          }

  

                          Message::addMessage(Message::EXAMINE_ADOPT_TYPE, $record['id'], $row['owner_staff_id'] ?? $row['staff_id'], $staff->id);

  

                          //删除 或签的待审批通知

                          Message::setRead(Message::EXAMINE_TYPE, $record['id']);

                          ExamineRecordModel::where([

                              'relation_type' => $relation_type,

                              'relation_id' => $relation_id,

                              'status' => 0,

                          ])->update(['status' => 3, 'check_time' => time()]);

                      } else {

                          $rowModel->save([

                              'check_staff_ids' => implode(',', $check_staff_ids),

                              'order_id' => $result['order_id']

                          ], ['id' => $relation_id]);

                      }

                  } else {

                      //审核未通过

                      $rowModel->save(['check_status' => 3, 'check_staff_ids' => ''], ['id' => $relation_id]);

                      Message::addMessage(Message::EXAMINE_REFUSE_TYPE, $record['id'], $row['owner_staff_id'] ?? $row['staff_id'], $staff->id, '');

  

                      //删除待审批通知

                      $ids = ExamineRecordModel::where([

                          'relation_type' => $relation_type,

                          'relation_id' => $relation_id,

                          'status' => 0,

                      ])->column('id');

                      Message::where(['relation_type' => Message::EXAMINE_TYPE, 'relation_id' => ['in', $ids], 'status' => 0])->update(['read_time' => time(), 'status' => 1]);

  

                      ExamineRecordModel::where([

                          'relation_type' => $relation_type,

                          'relation_id' => $relation_id,

                          'status' => 0,

                      ])->update(['status' => 3, 'check_time' => time()]);

                  }

  

  

                  Db::commit();

              } catch (Exception $e) {

                  Db::rollback();

                  Log::record($e->getMessage());

                  $this->error($e->getMessage());

              }

  

              $this->success('审核成功');

          }

  

  

          $this->assign('relation_type',$relation_type);

          $this->assign('relation_id',$relation_id);

          $this->assign('status',$status);

          return $this->view->fetch();

  	}

  

  }