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
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
|
<?php
namespace addons\antis\controller;
use addons\antis\model\Contract as ContractModel;
use addons\antis\model\ContractFile;
use addons\antis\model\ContractOther;
use addons\antis\model\ExamineRecord;
use addons\antis\model\FormField;
use addons\antis\model\Ratio;
use addons\antis\model\Receivables;
use addons\antis\model\Message;
use addons\antis\model\Staff;
use think\Db;
use think\Exception;
/**
* 合同接口
*/
class Contract extends StaffApi
{
protected $noNeedLogin = [];
protected $noNeedRight = [];
//创建合同
public function addContract()
{
$params = $this->request->post();
// 表单验证
if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
$this->error($result);
}
$result = FormField::checkFields(FormField::CONTRACT_TYPE, $params);
if ($result !== true) {
$this->error($result);
}
if (isset($params['num'])) {
if (empty($params['num'])) {
$params['num'] = ContractModel::getNum();
}
$contract = ContractModel::where(['num' => $params['num']])->find();
if ($contract) {
$this->error('合同编号已存在');
}
} else {
$params['num'] = ContractModel::getNum();
}
Db::startTrans();
try {
$result = ContractModel::createContract($params);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result) {
$this->success('添加合同成功');
}
}
//获取select合同列表
public function getSelectList()
{
$customer_id = input('customer_id');
$name = input('name','');
$where = [];
if ($customer_id) {
$where['customer_id'] = $customer_id;
}
if ($name) {
$where['name'] = ["like","%$name%"];
}
$records = ContractModel::where($where)->where(['check_status'=>2])->with(['ownerStaff'])->field('id,name,num,order_date,money')->order('id desc')->select();
$this->success('请求成功', $records);
}
//获取合同列表
public function getList()
{
$limit = input("limit/d", 10);
$customer_id = input('customer_id');
$contacts_id = input('contacts_id');
$status = input('status',0);
$params = $this->request->post();
$whereT =[];
$where= FormField::updateWhereField(FormField::CONTRACT_TYPE,$params);
if (isset($params['createtime']) && $params['createtime']) {//跟进状态
$createtime = $params['createtime'];
$createtime = explode(',', $createtime);
$where['order_date'] = ['between', [date('Y-m-d 00:00:00',strtotime($createtime[0])), date('Y-m-d 23:59:59',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()];
}
}
}
if ($status) {
if($status == 1){//待审核
$where['check_status']=['in',[0,1]];
}elseif($status == 2){//待回款
$where['check_status']=2;
$where['contract_status']=0;
}elseif($status == 3){//已回款
$where['check_status']=2;
$where['contract_status']=1;
}
}
if ($customer_id) {
$where['customer_id'] = $customer_id;
}
if ($contacts_id) {
$where['contacts_id'] = $contacts_id;
}
$records = ContractModel::where($where)->with([
'customer',
'contacts',
'ownerStaff',
'orderStaff',
'receivables'
])->order('id desc')->paginate($limit)->toArray();
$data = $records['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;
$ratio_m = 0;
if($v['money'] >0){
$ratio_m = round($v['receivables']['repayment_money'] / $v['money'] * 100, 2);
}
$v['receivables'] = [
'repayment_money' => $v['receivables']['repayment_money'],
'be_money' =>$be_money,
'ratio' => $ratio_m
];
}
$data[$k] = $v;
}
$whereT['owner_staff_id']=$where['owner_staff_id'];
$noMoney = ContractModel::where($whereT)->where(array('check_status'=>2))->sum('money');
$moneyinfo['repayment_money'] = Receivables::where($whereT)->where(array('check_status'=>2))->sum('money'); //已回款
$moneyinfo['be_money'] = sprintf("%.2f",$noMoney)-sprintf("%.2f",$moneyinfo['repayment_money']);//未回款
$moneyinfo['allmoney'] = $noMoney;//合同总金额
$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 getCustomerContacts()
{
$customer_id = input('customer_id');
$contacts_id = input('contacts_id');
$where = [];
if ($customer_id) {
$where['customer_id'] = $customer_id;
}
if ($contacts_id) {
$where['contacts_id'] = $contacts_id;
}
$records = ContractModel::where($where)->with(['receivables'])->field('id,name,money,num,check_status,order_date')->order('id desc')->select();
$records = collection($records)->toArray();
foreach ($records 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'];
$v['receivables'] = [
'repayment_money' => $v['receivables']['repayment_money'],
'be_money' => ($be_money > 0) ? $be_money : 0,
'ratio' => round($v['receivables']['repayment_money'] / $v['money'] * 100, 2)
];
}
$records[$k] = $v;
}
$this->success('请求成功', $records);
}
//获取合同详情
public function getDetail()
{
$id = input('id');
$contract = ContractModel::where(['id' => $id])->with([
'customer',
'contacts',
'ownerStaff',
'orderStaff',
'product',
'receivables',
'business'
])->find();
if (empty($contract)) {
//标记通知已读
Message::setRead(Message::CONTRACT_TYPE, $id);
$this->error('合同不存在');
}
$contract = $contract->toArray();
$contract = ContractOther::getOther($contract);
$receivablesMoney = Receivables::where(['contract_id' => $contract['id'], 'check_status' => 2])->sum('money');
//回款金额
$contract['receivables_money'] = $receivablesMoney;
$contract['ratios'] = json_decode($contract['ratios'],true);
if($contract['ratios']){
foreach ($contract['ratios'] as $k => $v) {
$v['staff'] = Staff::where(['id' => $v['staff_id']])->field('id,name,img,post')->find()->toArray();
$v['money'] = $contract['money'] * ($v['ratio'] / 100);
$contract['ratios'][$k] = $v;
}
}
if (empty($contract['receivables'])) {
$contract['receivables'] = [
'repayment_money' => 0,
'be_money' => $contract['money'],
'ratio' => 0
];
} else {
$be_money = $contract['money'] - $contract['receivables']['repayment_money'];
$be_ratio = 0;
if($contract['money'] > 0){
$be_ratio =round($contract['receivables']['repayment_money'] / $contract['money'] * 100, 2);
}
$contract['receivables'] = [
'repayment_money' => $contract['receivables']['repayment_money'],
'be_money' => ($be_money > 0) ? $be_money : 0,
'ratio' => $be_ratio
];
}
//产品删除不显示
if(isset($contract['product']) && $contract['product']){
foreach($contract['product'] as $k=>$v){
if(!$v['name'] && !$v['num']){
unset($contract['product'][$k]);
}
}
}
//标记通知已读
Message::setRead(Message::CONTRACT_TYPE, $id, $this->auth->id);
$this->success('请求成功', $contract);
}
//获取附件列表
public function getFilesList()
{
$id = input('contract_id');
$files = ContractFile::where(['contract_id' => $id])->field('file_id')->with(['file'])->select();
$this->success('请求成功', $files);
}
//撤回审核
public function cancel()
{
$id = input('id');
$customer = ContractModel::where(['id' => $id, 'check_status' => ['in', [0, 1]]])->find();
if (empty($customer)) {
$this->error('合同信息不存在');
}
$record = ExamineRecord::where([
'relation_type' => ExamineRecord::CONTRACT_TYPE,
'relation_id' => $id,
'status' => 0
])->find();
Db::startTrans();
try {
if ($message = Message::where(['relation_type' => 'examine', 'relation_id' => $record['id'], 'from_staff_id' => $this->auth->id])->find()) {
Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);
}
ContractModel::where(['id' => $id])->update(['check_status' => 4]);
ExamineRecord::where([
'relation_type' => ExamineRecord::CONTRACT_TYPE,
'relation_id' => $id,
'status' => 0
])->update(['status' => 3]);
Db::commit();
} catch (Exception $e) {
Db::rollback();;
$this->error($e->getMessage());
}
$this->success('撤回成功');
}
//修改合同
public function editContract()
{
$id = input('id');
$params = $this->request->post();
$row = ContractModel::where(['id' => $id, 'check_status' => ['in', [3, 4]]])->find();
if (empty($row)) {
$this->error('合同信息不存在');
}
// 表单验证
if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
$this->error($result);
}
$result = FormField::checkFields(FormField::CONTRACT_TYPE, $params,$id);
if ($result !== true) {
$this->error($result);
}
Db::startTrans();
try {
$params['owner_staff_id'] = $row['owner_staff_id'];
$result = ContractModel::updateContract($params);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('修改合同信息成功');
}
//获取合同编号
public function getContractNumber()
{
$this->success('请求成功', ['number' => ContractModel::getNum()]);
}
/**
* 获取业绩分割比例
*/
public function getRatio()
{
$ratios = Ratio::where(['status' => 1])->field('name,ratio')->select();
$ratios = collection($ratios)->toArray();
foreach ($ratios as $k => $v) {
$v['ratio'] = json_decode($v['ratio'], true);
$ratios[$k] = $v;
}
$this->success('请求成功', $ratios);
}
}
|