Index.php
12.9 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
namespace app\admin\controller\facrm\business;
use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
/**
*商机管理
*/
class Index extends Backend
{
protected $model = null;
/**
* 快速搜索时执行查找的字段
*/
protected $searchFields = 'name';
protected $childrenAdminIds = [];
protected $noNeedRight = ['product'];
public function _initialize()
{
parent::_initialize();
$this->model = model('\app\admin\model\facrm\Business');
$this->request->filter(['strip_tags']);
}
/**
* 列表
*/
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
$scene = model('\app\admin\model\facrm\Scene');
$scene_list = $scene->where('types', 'business')
->where(function ($query) {
$query->where('admin_id', 0)->whereor('admin_id', $this->auth->id);
})
->column('id,name');
if ($this->request->isAjax()) {
$filter = $this->request->get("filter", '');
$filter = (array)json_decode($filter, true);
$filter_w = [];
$filter_w['owner_user_id'] = $this->auth->id;
if (isset($filter['scene_id'])) {
if (!isset($scene_list[$filter['scene_id']])) {
$this->error(__("您没有权限"));
}
switch ($filter['scene_id']) {
case 7:
//全部客户 超级管理员不做限制
if (!$this->auth->isSuperAdmin()){
$this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
if ($this->childrenAdminIds)
$filter_w['owner_user_id'] = ['in', $this->childrenAdminIds];
}else{
$filter_w['owner_user_id']=['neq',''];
}
break;
case 8:
//我的客户
$filter_w['owner_user_id'] = $this->auth->id;
break;
case 9:
//下属客户
$this->childrenAdminIds = $this->auth->getChildrenAdminIds(false);
if ($this->childrenAdminIds){
$filter_w['owner_user_id'] = ['in', $this->childrenAdminIds];
}else{
$filter_w['owner_user_id']=-100;
}
break;
default://其它的还做TODO
$filter_w['owner_user_id'] = $this->auth->id;
}
unset($filter['scene_id']);
$this->request->get(['filter' => json_encode($filter)]);
}
$customer_id = $this->request->param("customer_id");
if ($customer_id) {
$filter_w['customer_id'] = $customer_id;
}
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
$this->request->request(['custom' => $filter_w]);
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where($where)
->where($filter_w)
->with([
])
->order($sort, $order)->fetchSql(false)
->count();
$list = $this->model
->where($where)
->where($filter_w)
->with([
'createUser' => function ($user) {
$user->field('id,username,nickname');
},
'ownerUser' => function ($user) {
$user->field('id,username,nickname');
},
'customer' => function ($customer) {
$customer->field('id,name');
},
])
->order($sort, $order)
->limit($offset, $limit)->fetchSql(false)
->select();
$result = array("total" => $total, "rows" => $list);
return json($result);
} else {
$this->assignconfig("is_end_list", $this->model->getIsEndList());
$this->view->assign("scene_list", $scene_list);
}
/**
* 自定义字段
*/
$fields = \app\admin\model\facrm\Fields::getCustomFieldsTable('business', [], 'isfilter=1 or islist=1');
if ($fields) {
$this->assignconfig('fields', json_encode($fields));
}
return $this->view->fetch();
}
/**
* 添加
* @return mixed
*/
public function add()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if (!isset($params['customer_id']))
$this->error(__('请选择客户'));
$customer = model('\app\admin\model\facrm\Customer');
$row_c = $customer->get($params['customer_id']);
if (!$row_c)
$this->error(__('没有找到客户'));
$this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
if (!in_array($row_c['owner_user_id'], $this->childrenAdminIds)) {
$this->error(__('You have no permission'));
}
$params['next_time'] = $params['next_time'] ? strtotime($params['next_time']) : 0;
$params['deal_time'] = $params['deal_time'] ? strtotime($params['deal_time']) : 0;
$params = array_merge($params, [
'create_user_id' => $this->auth->id,
'owner_user_id' => $row_c['owner_user_id'],
'type_id' => '1',
'status' => 1,
]);
$params = $this->preExcludeFields($params);
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->model->validateFailException(true)->validate($validate);
}
$result = $this->model->allowField(true)->save($params);
//添加商机产品
$product_data = ($this->request->post("product/a"));
$business_id = $this->model->id;
foreach ($product_data as &$row) {
$row['business_id'] = $business_id;
}
$this->model->product()->saveAll($product_data);
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were inserted'));
}
}
return $this->view->fetch();
}
/**
* 修改
* @param null $ids
*/
public function edit($ids = NULL)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$auth = new \addons\facrm\library\Auth();
if (!$auth->checkBusinessAuth($row, $this->auth)) {
$this->error(__('您没有权限'));
}
if (!$auth->checkCustomerAuth($row->customer, $this->auth)) {
$this->error(__('您没有权限:客户权限'));
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
$params['next_time'] = $params['next_time'] ? strtotime($params['next_time']) : 0;
$params['deal_time'] = $params['deal_time'] ? strtotime($params['deal_time']) : 0;
$result = false;
Db::startTrans();
try {
if ($row->is_end != 0) exception(__("不能修改的状态"));
if ($params['is_end'] == 1) exception(__("不能手动改成成交状态,请添加合同!"));
$business_id = $row->id;
$result = $row->allowField(true)->save($params);
if ($result) {
//更新产品,先全部删除再添加
$productModel = model('\app\admin\model\facrm\business\Product');
$this->model->product()->where('business_id', $business_id)->delete();
$product_data = ($this->request->post("product/a"));
if ($product_data){
foreach ($product_data as &$r) {
$r['business_id'] = $business_id;
}
$productModel->saveAll($product_data);
}
}
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were updated'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$this->view->assign("row", $row);
$this->view->assign("product_list", $row->product);
return $this->view->fetch();
}
/**
* 删除
* @param string $ids
*/
public function del($ids = "")
{
if (!$this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $ids ? $ids : $this->request->post("ids");
if ($ids) {
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
$this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->where('owner_user_id', 'in', $this->childrenAdminIds)->select();
$count = 0;
Db::startTrans();
try {
foreach ($list as $k => $v) {
$count += $v->delete();
}
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success();
} else {
$this->error(__('No rows were deleted'));
}
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
/**
* 意向产品
*/
public function product($business_id = null)
{
//设置过滤方法
$this->request->filter(['strip_tags']);
if (!$business_id) $this->error(__("商机ID有误"));
$this->model = model("app\admin\model\\facrm\business\Product");
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where($where)
->where('business_id', $business_id)
->order($sort, $order)
->count();
$list = $this->model
->where($where)
->where('business_id', $business_id)
->with(['info'])
->order($sort, $order)
->limit($offset, $limit)
->select();
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
}