You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wkcrm/application/crm/traits/AutoNumberTrait.php

83 lines
2.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* 自动编号(合同、回款、回访、发票)
*
* @author qifan
* @date 2020-12-09
*/
namespace app\crm\traits;
use app\crm\model\NumberSequence;
use think\Db;
trait AutoNumberTrait
{
private $stringToDate = ['yyyyMMdd' => 'Ymd', 'yyyy' => 'Y', 'yyyyMM' => 'Ym'];
/**
* 获取自动编号
*
* @param $type 1合同2回款3回访4发票
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getAutoNumbers($type)
{
$number = '';
$data = [];
# 根据设置重置编号(不想改下面的代码,在这里在写一个,多公海版本出来后,用定时来做)
$list = Db::name('crm_number_sequence')->where('number_type', $type)->where('status', 0)->select();
foreach ($list AS $key => $value) {
if ($value['type'] == 3 && $value['reset'] != 0) {
# 1每天2每月3每年
$currentDate = [
1 => date('Y-m-d'),
2 => date('Y-m'),
3 => date('Y')
];
$lastDate = [
1 => date('Y-m-d', $value['last_date']),
2 => date('Y-m', $value['last_date']),
3 => date('Y', $value['last_date'])
];
if ($currentDate[$value['reset']] != $lastDate[$value['reset']]) {
Db::name('crm_number_sequence')->where('number_sequence_id', $value['number_sequence_id'])->update([
'last_number' => !empty($value['value']) ? $value['value'] : 1
]);
}
}
}
$info = Db::name('crm_number_sequence')->where('number_type', $type)->order('sort', 'asc')->where('status', 0)->select();
foreach ($info AS $key => $value) {
# 文本
if ($value['type'] == 1) {
$number .= $value['value'] . '-';
}
# 日期
if ($value['type'] == 2) {
$number .= date($this->stringToDate[$value['value']]) . '-';
}
# 数字
if ($value['type'] == 3) {
$number .= $value['last_number'] . '-';
# 需要更新的数据
$data[] = [
'number_sequence_id' => $value['number_sequence_id'],
'last_number' => $value['last_number'] + $value['increase_number'],
'last_date' => time()
];
}
}
return ['number' => rtrim($number, '-'), 'data' => $data];
}
}