Contract.php
12.4 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
<?php
namespace app\admin\model\facrm;
use think\Model;
use traits\model\SoftDelete;
/**
* 合同模型
* Class Contract
* @package app\admin\model\facrm
*/
class Contract extends Model
{
use SoftDelete;
// 表名
protected $name = 'facrm_contract';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
protected $deleteTime = 'delete_time';
protected static function init()
{
$forfun = function (&$row) {
foreach ($row->data as $k => $v) {
//数组的字段处理成字符串,主要处理是自定义字段多选的值
if (!empty($v) && is_array($v)) $row->data[$k] = implode(',', $v);
}
};
self::afterUpdate(
function ($row) {
$changed = $row->getChangedData();
if ($changed && isset($changed['follow_time'])) return;//跟进操作不加日志
$msg = Operatelog::getTitle() ? "" : "更变合同";
if ($msg && isset($changed['check_status']) && $changed['check_status']) {
//标题存在说明在之前已经赋值过,不再处理
$check_status = ['-1' => "未提交", '0' => "待审核", 1 => '审核合同', 2 => '审核通过', 3 => '审核未通过'];
$msg = $check_status[$changed['check_status']];
}
Operatelog::record(0, $row->id, 0, $msg, json_encode($changed));
}
);
self::afterInsert(function ($row) use ($forfun) {
//添加操作日志
Operatelog::record(0, $row->id, 0, Operatelog::getTitle() ? "" : "新增合同", json_encode($row));
});
/**
* 插入前
*/
self::beforeInsert(function ($row) use ($forfun) {
$forfun($row);
});
/**
* 更新前
*/
self::beforeUpdate(function ($row) use ($forfun) {
$forfun($row);
});
}
/**
* 创建者
*/
public function createUser()
{
return $this->hasOne('\app\admin\model\Admin', 'id', 'create_user_id');
}
/**
* 拥有者
*/
public function ownerUser()
{
return $this->hasOne('\app\admin\model\Admin', 'id', 'owner_user_id');
}
/**
* 签单人
* @return \think\model\relation\HasOne
*/
public function orderAdmin()
{
return $this->hasOne('\app\admin\model\Admin', 'id', 'order_admin_id');
}
/**
* 客户对象
* @return \think\model\relation\BelongsTo
*/
public function customer()
{
return $this->belongsTo('\app\admin\model\facrm\Customer', 'customer_id', 'id');
}
/**
* 联系人
* @return \think\model\relation\BelongsTo
*/
public function contacts()
{
return $this->belongsTo('\app\admin\model\facrm\customer\Contacts', 'contacts_id');
}
/**
* 商机对象
* @return \think\model\relation\BelongsTo
*/
public function business()
{
return $this->belongsTo('\app\admin\model\facrm\Business', 'business_id', 'id');
}
/**
* 关联商品
* @return \think\model\relation\HasMany
*/
public function product()
{
return $this->hasMany('\app\admin\model\facrm\contract\Product', 'contract_id', 'id');
}
/**
* 收款
* @return \think\model\relation\HasMany
*/
public function receivables()
{
return $this->hasMany('\app\admin\model\facrm\contract\Receivables', 'contract_id', 'id');
}
/**
* 审批日志
* @return \think\model\relation\HasMany
*/
public function flowLog()
{
return $this->hasMany('\app\admin\model\facrm\flow\Log', 'types_id', 'id')->where('types', 'contract');
}
/**
* 合同、回款统计
* @param $startDate 开始时间
* @param $endDate 结束时间
* @param $owner_user_ids
* @return array|bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getAchievementEchart($startDate, $endDate, $owner_user_ids)
{
$contractModel = new \app\admin\model\facrm\Contract();
$receivablesModel = new \app\admin\model\facrm\contract\Receivables();
// 生成查询的开始和结束时间,默认取30日
!is_numeric($startDate) && $starttime = strtotime($startDate);
!is_numeric($endDate) && $endtime = strtotime($endDate);
$isnotrangeDate = empty($starttime) && empty($endtime);
$nearly = '30';
if ($isnotrangeDate) {
$endtime = time();
$nearly -= 1;
$starttime = strtotime("-{$nearly} day"); // 最近30天日期
} elseif ($starttime > $endtime) {
$this->error = '起始时间要小于终止时间';
return false;
}
$totalseconds = $endtime - $starttime;;
if ($totalseconds > 86400 * 30 * 2) {
$format = '%Y-%m';
} else {
if ($totalseconds > 86400) {
$format = '%Y-%m-%d';
} else {
$format = '%H:00';
}
}
//合同数量和合同金额
if ($owner_user_ids && is_numeric($owner_user_ids)) {
$contractModel->where('order_admin_id', $owner_user_ids);
$receivablesModel->where('order_admin_id', $owner_user_ids);
} elseif ($owner_user_ids && is_array($owner_user_ids)) {
$contractModel->where('order_admin_id', 'in', $owner_user_ids);
$receivablesModel->where('order_admin_id', 'in', $owner_user_ids);
}
//合同金额
$lists = $contractModel->where('order_time', 'between time', [$starttime, $endtime])
->field('COUNT(*) AS nums, sum(money) as money, DATE_FORMAT(FROM_UNIXTIME(order_time), "' . $format . '") AS add_date')
->where('check_status', 2)->group('add_date')
->select();
//回款金额
$listsd = $receivablesModel->where('return_time', 'between time', [$starttime, $endtime])
->field('COUNT(*) AS nums, sum(money) as money, DATE_FORMAT(FROM_UNIXTIME(return_time), "' . $format . '") AS add_date')
->where('check_status', 2)->group('add_date')
->select();
if ($totalseconds > 84600 * 30 * 2) {
$starttime = strtotime('last month', $starttime);
while (($starttime = strtotime('next month', $starttime)) <= $endtime) {
$column[] = date('Y-m', $starttime);
}
} else {
if ($totalseconds > 86400) {
for ($time = $starttime; $time <= $endtime;) {
$column[] = date("Y-m-d", $time);
$time += 86400;
}
} else {
for ($time = $starttime; $time <= $endtime;) {
$column[] = date("H:00", $time);
$time += 3600;
}
}
}
$r_count = $c_count = $c_money = $r_money = array_fill_keys($column, 0);
foreach ($lists as $k => $v) {
$c_count[$v['add_date']] = $v['nums'];//合同数量
$c_money[$v['add_date']] = $v['money'];//合同金额
}
foreach ($listsd as $k => $v) {
$r_money[$v['add_date']] = $v['money'];//回款金额
$r_count[$v['add_date']] = $v['nums'];//回款数量
}
$result = [
'date' => array_keys($c_count),
'c_money' => array_values($c_money),
'c_count' => array_values($c_count),
'r_money' => array_values($r_money),
'r_count' => array_values($r_count),
];
$data = [
'date' => $result['date'],
'data' => [
"合同数量" => $result['c_count'],
"合同金额" => $result['c_money'],
"回款金额" => $result['r_money'],
"回款数量" => $result['r_count'],
],
];
return $data;
}
/**
* 根据规则自动生成编码
* @param $prefix
* @return int|mixed|string
*/
public static function autoNo($prefix)
{
$replace_data = ['Y' => date('Y'), 'm' => date('m'), 'd' => date('d'), 'h' => date("H"), 'i' => date("i"), 's' => date("s"), 'rand' => rand(100000, 999999)];
return $prefix ? __($prefix, $replace_data) : '';
}
/**
* 修改
* @param $row 当前修改的实体
* @param $params 参数
* @param $product_data 商品集
*/
public function edit($row, $params, $product_data)
{
//是否采用模型验证
$name = str_replace("\\model\\", "\\validate\\", get_class($this));
$row->validateFailException(true)->validate($name . '.edit');
$contract_id = $row->id;
$result = $row->allowField(true)->save($params);
if ($result) {
//更新产品,先全部删除再添加
$productModel = new \app\admin\model\facrm\product\Product();
foreach ($row->product as $p) {
$p->info()->setInc('inventory', $p['nums']);//恢复库存
$p->delete(true);
}
$i = 1;
foreach ($product_data as $key => &$r) {
if (!$r['nums']) \exception("数量不对 {$i}行");
$r['contract_id'] = $contract_id;
$pinfo = $productModel->where('id', $r['product_id'])->where('status', 1)->find();
if (!$pinfo) {
\exception("产品不存在, {$i}行");
}
//判断产品库存
if ($pinfo['inventory'] < $r['nums']) {
\exception($pinfo['name'] . "库存不足, {$i}行");
}
$r['sales_price'] = $r['price'];// bcsub($pinfo['price'], $row['discount'] * $pinfo['price'] / 100, 2);//销售价格
$r['price'] = $pinfo['price'];//产品原价
$r['price'] = $pinfo['price'];
$r['discount'] = $params['discount_rate'] ? $params['discount_rate'] : 0; //优惠
$r['subtotal'] = bcmul($r['sales_price'], $r['nums'], 2);//销售价格小计
$r['unit'] = $pinfo['unit'];//单位
$i++;
//减少库存
$pinfo->setDec('inventory', $r['nums']);
}
$row->product()->saveAll($product_data);
}
return $result;
}
/**
* 添加
* @param $params 参数
* @param $product_data 商品集
*/
public function add($params, $product_data)
{
if (!is_array($product_data)||count($product_data)<=0){
\exception("请填写产品");
}
//是否采用模型验证
$name = str_replace("\\model\\", "\\validate\\", get_class($this));
$this->validateFailException(true)->validate($name . '.add');
$result = $this->allowField(true)->save($params);
//添加商机产品
$contract_id = $this->id;
$productModel = new \app\admin\model\facrm\product\Product();
$i = 1;
foreach ($product_data as $key => &$row) {
if (!$row['nums']) \exception("数量不对 {$i}行");
$row['contract_id'] = $contract_id;
$pinfo = $productModel->where('id', $row['product_id'])->where('status', 1)->find();
if (!$pinfo) {
\exception("产品不存在, {$i}行");
}
//判断产品库存
if (($pinfo['inventory']) < $row['nums']) {
\exception($pinfo['name'] . "库存不足, {$i}行");
}
$row['sales_price'] = $row['price'];// bcsub($pinfo['price'], $row['discount'] * $pinfo['price'] / 100, 2);//销售价格
$row['price'] = $pinfo['price'];//产品原价
$row['discount'] = $params['discount_rate'] ? $params['discount_rate'] : 0; //优惠
$row['subtotal'] = bcmul($row['sales_price'], $row['nums'], 2);//销售价格小计
$row['unit'] = $pinfo['unit'];//单位
$i++;
//减少库存
$pinfo->setDec('inventory', $row['nums']);
}
$this->product()->saveAll($product_data);
return $result;
}
}