Blame view

addons/antis/controller/Business.php 10.5 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  <?php

  namespace addons\antis\controller;

  use addons\antis\model\Business as BusinessModel;

  use addons\antis\model\BusinessOther;

  use addons\antis\model\FormField;

  use addons\antis\model\Staff;

  use addons\antis\model\Message;

  use addons\antis\model\Contract;

  use addons\antis\model\BusinessStatus;

  use addons\antis\model\File;

  use think\Db;

  use think\Exception;

  /**

   * 商机接口

   */

  class Business extends StaffApi

  {

      protected $noNeedLogin = [];

      protected $noNeedRight = [];

  

  

      //创建商机

      public function addBusiness()

      {

          $params = $this->request->post();

  

          $result = FormField::checkFields(FormField::BUSINESS_TYPE, $params);

          if ($result !== true) {

              $this->error($result);

          }

          Db::startTrans();

          try {

  

              $result = BusinessModel::createBusiness($params);

              Db::commit();

          } catch (Exception $e) {

              Db::rollback();

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

          }

          if ($result) {

              $this->success('添加商机成功');

          }

      }

  

  

      //获取商机列表

      public function getList()

      {

          $limit = input("limit/d", 10);

          $customer_id = input('customer_id');

          $params = $this->request->post();

  

          $where= FormField::updateWhereField(FormField::BUSINESS_TYPE,$params);

  

          if (isset($params['createtime']) && $params['createtime']) {//

              $createtime = $params['createtime'];

              $createtime = explode(',', $createtime);

              $where['createtime'] = ['between', [strtotime($createtime[0]), strtotime($createtime[1]) + 86400 - 1]];

          }

          if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选

              $where['owner_staff_id'] = $params['staff_id'];

          } else {

              $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];

              if (isset($params['type']) && $params['type']) {//客户分类

                  if ($params['type'] == 1) {//我的创建

                      $where['owner_staff_id'] = $this->auth->id;

                  } elseif ($params['type'] == 2) {//下属创建

                      $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];

                  }

              }

          }

          $wheres =[];

          if (isset($params['contract_status']) && $params['contract_status']) {

              if($params['contract_status'] == 1){

                  $wheres['contract_status'] = 0;

              }

              if($params['contract_status'] == 2){

                  $wheres['contract_status'] = 1;

              }

          }

  

          if ($customer_id) {

              $where['customer_id'] = $customer_id;

          }

          $records = BusinessModel::where($where)->where($wheres)->with([

              'customer',

              'ownerStaff',

          ])->order('id desc')->paginate($limit)->toArray();

          $data=[];

          foreach($records['data'] as $k=>$v){

              $types = BusinessStatus::where(array('business_id'=>$v['id']))->order('id desc')->value('type');

              if($types){

                  $v['type'] = (int)$types;

              }else{

                  $v['type'] = 0;

              }

              $data[$k] = $v;

          }

          $allMoney = BusinessModel::where($where)->where($wheres)->sum('money');

          $moneyinfo['repayment_money'] = BusinessModel::where($where)->where(array('contract_status'=>1))->sum('money'); //成交金额

          $moneyinfo['no_money'] = sprintf("%.2f",$allMoney)-sprintf("%.2f",$moneyinfo['repayment_money']);//未成交金额

          $moneyinfo['allmoney'] = $allMoney;//合同总金额

          $this->success('请求成功', ['moneyinfo' => $moneyinfo, 'total' => $records['total'], 'per_page' => $records['per_page'], 'current_page' => $records['current_page'], 'last_page' => $records['last_page'], 'data' => $data]);

  

      }

  

      //获取商机详情

      public function getDetail()

      {

          $id = input('id');

          $contract = BusinessModel::where(['id' => $id])->with([

              'customer',

              'ownerStaff',

              'product',

          ])->find();

          if (empty($contract)) {

              $this->error('商机不存在');

          }

          $contract = $contract->toArray();

          $contract = BusinessOther::getOther($contract);

          $type = BusinessStatus::where(array('business_id'=>$contract['id']))->order('id desc')->value('type');

          $contract['type'] = $type ? (int)$type : 0;

          //产品删除不显示

          if(isset($contract['product']) && $contract['product']){

              foreach($contract['product'] as $k=>$v){

                  if(!$v['name'] && !$v['num']){

                      unset($contract['product'][$k]);

                  }

              }

          }

          //标记通知已读

          Message::setRead(Message::BUSINESS_TYPE, $id, $this->auth->id);

  

          $this->success('请求成功', $contract);

      }

  

      //修改商机

      public function editBusiness()

      {

          $id = input('id');

          $params = $this->request->post();

          $row = BusinessModel::where(['id' => $id])->find();

          if (empty($row)) {

              $this->error('商机信息不存在');

          }

  

          $result = FormField::checkFields(FormField::BUSINESS_TYPE, $params,$id);

          if ($result !== true) {

              $this->error($result);

          }

          Db::startTrans();

          try {

              $result = BusinessModel::updateBusiness($params);

              Db::commit();

          } catch (Exception $e) {

              Db::rollback();

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

          }

          $this->success('修改商机成功');

      }

  

      /**

       * 转移商机

       */

      public function batch_change()

      {

          $ids = input('id');

          $staff_id = input('owner_staff_id');

          if(!$ids || !$staff_id){

              $this->error('参数不正确');

          }

          $ids = BusinessModel::where([

              'id' => $ids

          ])->column('id');

          if (empty($ids)) {

              $this->error('商机不存在');

          }

          $result = BusinessModel::batchTransfer($ids, $staff_id);

          if(!$result){

              $this->error('操作失败');

          }

          $this->success('操作成功');

      }

      //删除商机

      public function delete()

      {

          $ids = input('id');

          if(!$ids){

              $this->error('参数不正确');

          }

          $data = BusinessModel::where([

              'id' => $ids

          ])->column('id');

          if (empty($data)) {

              $this->error('商机不存在');

          }

          $result = BusinessModel::where(array('id'=>$ids))->update(array('updatetime'=>time(),'deletetime'=>time()));

          if(!$result){

              $this->error('删除失败');

          }

          $this->success('删除成功');

      }

      //关联商机列表

      public function business_list(){

          $ids = input('customer_id');

          if(!$ids){

              $this->error('参数不正确');

          }

          $data= BusinessModel::where(['customer_id'=>$ids,'owner_staff_id'=>['in',Staff::getMyStaffIds()]])->field('id,name')->select();

  

          $this->success('请求成功',$data);

      }

      //商机合同

      public function contract(){

          $limit = input("limit/d", 10);

          $ids = input('id');

          if(!$ids){

              $this->error('参数不正确');

          }

          $contract = Contract::where(array('business_id'=>$ids))->with([

              'customer',

              'contacts',

              'ownerStaff',

              'orderStaff',

              'receivables'

          ])->order('id desc')->paginate($limit)->toArray();

          $data = isset($contract['data'])?$contract['data']:[];

          if($data){

              foreach ($data as $k => $v) {

                  if (empty($v['receivables'])) {

                      $v['receivables'] = [

                          'repayment_money' => 0,

                          'be_money' => $v['money'],

                          'ratio' => 0

                      ];

                  } else {

                      $be_money = $v['money'] - $v['receivables']['repayment_money'];

                      $be_money =   ($be_money > 0) ? $be_money : 0;

                      $v['receivables'] = [

                          'repayment_money' => $v['receivables']['repayment_money'],

                          'be_money' =>$be_money,

                          'ratio' => round($v['receivables']['repayment_money'] / $v['money'] * 100, 2)

                      ];

                  }

                  $data[$k] = $v;

              }

          }

          $this->success('请求成功', ['total' => $contract['total'], 'per_page' => $contract['per_page'], 'current_page' => $contract['current_page'], 'last_page' => $contract['last_page'], 'data' => $data]);

  

      }

      //商机状态

      public function business_status(){

          $id = input('id');

          $type = input('type',0);

          $remark = input('remark');

          $file = input('file');

          if(!$id){

              $this->error('参数不正确');

          }

          $data = array(

              'id'=>$id,

              'type'=>$type,

              'remark'=>$remark,

              'file'=>$file,

          );

          $business =BusinessModel::batchStatus($data);

          if(!$business){

              $this->error('操作失败');

          }

          $this->success('操作成功');

      }

      //商机历史

      public function business_history(){

          $id = input('id');

          if(!$id){

              $this->error('参数不正确');

          }

          $business = BusinessStatus::where(array('business_id'=>$id))->order('id desc')->select();

          foreach($business as $k=>$v){

              $business[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);

              if($v['file']){

                  $business[$k]['file'] = File::where(array('id'=>['in',$v['file']]))->field('id,types,name,save_name,size,file_path')->select();

              }

          }

          $this->success('请求成功',$business);

      }

      //查询商机列表

      public function get_select_list(){

          $limit = input("limit/d", 10);

          $params = $this->request->post();

          $where = [];

          if(isset($params['name']) && $params['name']){

              $where['name'] = array('like','%'.$params['name'].'%');

          }

          $data= BusinessModel::where(['owner_staff_id'=>['in',Staff::getMyStaffIds()]])->where($where)->field('id,name')->order('id desc')->paginate($limit)->toArray();;

          $this->success('请求成功',$data);

      }

  }