Merge branch 'release'

This commit is contained in:
mzl
2025-11-03 18:16:51 +08:00
parent a0935c4332
commit 359755fdf1
38 changed files with 2339 additions and 36 deletions
@@ -0,0 +1,267 @@
<?php
namespace app\common\service;
use app\common\model\FinPaymentStock;
use app\common\model\FinReceipt;
use app\common\validate\FinPaymentApplyValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\FinPaymentApply;
use support\Redis;
use support\Request;
class FinPaymentApplyService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = FinPaymentApply::class;
/**
* 验证器
*/
protected $validator = FinPaymentApplyValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
/**
* 构建查询条件
*/
protected function buildQueryConfig(Request $request, string $scene = 'index'): array
{
$p = $request->all();
$config = [];
$config['model'] = $this->modelClass;
switch ($scene) {
case 'index':
// ====== ====== 排序 ====== ======
$config['order']['id'] = 'desc';
// ====== ====== 筛选 ====== ======
// 汇款国家
if (isset($p['country_id']) && !empty($p['country_id'])) {
$config['where']['country_id'] = $p['country_id'];
}
// 收款账户筛选
if (isset($p['account_id']) && !empty($p['account_id'])) {
$config['where']['account_id'] = $p['account_id'];
}
// 入账资料超时
if (isset($p['scene']) && $p['scene'] == 'file_timeout') {
$config['where']['claimed_at'] = ['between', [1, time()-24*60*60]];
}
// 关键字
if (isset($p['keyword']) && $p['keyword']) {
$k = '%' . $p['keyword'] . '%';
$config['orWhere'][] = ['remitter', 'like', $k];
}
break;
case 'show':
$config['where']['id'] = $p['id'];
break;
case 'update':
case 'destroy':
$config['where']['id'] = $p['id'];
break;
case 'store':
break;
}
return $config;
}
public function beforeServiceStore($data, Request $request)
{
$data['apply_no'] = $this->generateUniqueApplyNo();
$data['doc_user_id'] = $request->user->uid;
$data['voucher_file'] = json_encode($data['voucher_file']);
$data['submit_files'] = json_encode($data['submit_files']);
redis::set('stock_' . $data['doc_user_id'], json_encode($request->post('stock')), 'EX', 30);
unset($data['stock']);
if($data['voucher_file'] == '[]' || $data['voucher_file'] == '{}' || $data['voucher_file'] == '""')
$data['voucher_file'] = '';
if($data['submit_files'] == '[]' || $data['submit_files'] == '{}' || $data['submit_files'] == '""')
$data['submit_files'] = '';
return $data;
}
public function afterServiceStore($res)
{
$data = [
'payment_apply_id' => $res['id'],
];
$piStock = json_decode(redis::get('stock_' . $res['doc_user_id']), true);
redis::del('stock' . $res['doc_user_id']);
foreach ($piStock as $stock) {
foreach ($stock['stock_id'] as $v) {
$data['stock_id'] = $v;
$data['pi_no'] = $stock['pi_on'];
FinPaymentStock::create($data);
}
}
}
public function beforeServiceUpdate($data, Request $request)
{
unset($data['stock']);
$data['submit_files'] = json_encode($data['submit_files']);
if($data['submit_files'] == '[]' || $data['submit_files'] == '{}' || $data['submit_files'] == '""')
$data['submit_files'] = '';
$data['voucher_file'] = json_encode($data['voucher_file']);
if($data['voucher_file'] == '[]' || $data['voucher_file'] == '{}' || $data['voucher_file'] == '""')
$data['voucher_file'] = '';
return $data;
}
/**
* 生成唯一的付款申请编码
* 格式:PA + 日期 + 6位字母数字组合
*
* @return string 唯一编码
*/
public function generateUniqueApplyNo()
{
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$maxAttempts = 10; // 防止无限循环
$secondChar = 'PA' . date('Ymd');
for ($attempt = 0; $attempt < $maxAttempts; $attempt++) {
// 生成后6位随机字符
$suffix = '';
for ($i = 0; $i < 6; $i++) {
$suffix .= $chars[rand(0, strlen($chars) - 1)];
}
$applyNo = $secondChar . $suffix;
// 检查是否已存在
$existing = FinPaymentApply::where('apply_no', $applyNo)->first();
if (!$existing) {
return $applyNo;
}
}
// 如果1000次尝试后仍然重复,使用时间戳确保唯一性
$timestamp = substr(time(), -6);
return $secondChar . $timestamp;
}
/**
* 根据相似度排序付款记录
* @param Request $request
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Pagination\LengthAwarePaginator
*/
public function complexQueryIndex(Request $request)
{
$limit = (int)$request->get('size', 15);
$page = (int)$request->get('current', 1);
$param = $request->all();
$where = [];
$orWhere = [];
// 汇款国家
if (isset($param['country_id']) && !empty($param['country_id'])) {
$where[] = ['country_id', $param['country_id']];
}
// 收款账户筛选
if (isset($param['account_id']) && !empty($param['account_id'])) {
$where[] = ['account_id', $param['account_id']];
}
// 入账资料超时
if (isset($param['scene']) && $param['scene'] == 'file_timeout') {
$where[] = ['submit_files', ''];
$where[] = ['claimed_at', '<', time()-24*60*60];
$where[] = ['claimed_at', '>', 1];
}
// 关键字
if (isset($param['keyword']) && $param['keyword']) {
$k = '%' . $param['keyword'] . '%';
$where[] = ['remitter', 'like', $k];
}
// 构建查询
$query = $this->modelClass::where($where);
// 是否已关联收款记录的筛选
$correlation = $param['correlation']??'';
if ($correlation === 'receipt') { //已关联
$query->whereHas('receipt');
} elseif ($correlation === 'unbind') { //未关联
$query->whereDoesntHave('receipt');
}
if ((isset($param['scene']) && $param['scene'] == 'list') || empty($param['receipt_id'])) {
return $query->orderBy('id','desc')->paginate($limit, ['*'], 'page', $page);
}
$receipt = FinReceipt::where('id', $param['receipt_id'])->first();
$account_id = $receipt->account_id;
$currency_id = $receipt->currency_id;
$remitter = $receipt->remitter;
$amount = $receipt->amount;
// 计算十天前的时间戳
$sevenDaysAgo = time() - (10 * 24 * 60 * 60);
// 构建相似度排序逻辑
$similarityOrder = [];
// 如果有传入参数,计算相似度
if (!empty($account_id)) {
$similarityOrder[] = "CASE WHEN account_id = {$account_id} THEN 0 ELSE 1 END";
}
if (!empty($currency_id)) {
$similarityOrder[] = "CASE WHEN currency_id = {$currency_id} THEN 0 ELSE 1 END";
}
if (!empty($remitter)) {
$similarityOrder[] = "CASE WHEN remitter = '{$remitter}' THEN 0 ELSE 1 END";
}
if (!empty($amount)) {
$amount = (float)$amount;
$similarityOrder[] = "ABS(amount - {$amount})";
$similarityOrder[] = "CASE WHEN (ABS(amount - {$amount}) / {$amount} * 100) <= 5 THEN 0 ELSE 1 END";
}
// 构建排序SQL
$orderByClause = "";
if (!empty($similarityOrder)) {
$orderByClause = "CASE
WHEN created_at >= {$sevenDaysAgo} THEN
(" . implode(' + ', $similarityOrder) . ")
ELSE 0
END ASC, ";
}
$orderByClause .= "CASE
WHEN created_at > {$sevenDaysAgo} THEN id
ELSE 999999
END DESC, ";
$orderByClause .= "id DESC";
$query->orderByRaw($orderByClause);
return $query->paginate($limit, ['*'], 'page', $page);
}
}
@@ -0,0 +1,62 @@
<?php
namespace app\common\service;
use app\common\validate\FinReceiptHisValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\FinReceiptHis;
use support\exception\BusinessException;
use support\Request;
class FinReceiptHisService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = FinReceiptHis::class;
/**
* 验证器
*/
protected $validator = FinReceiptHisValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
/**
* 构建查询条件
*/
protected function buildQueryConfig(Request $request, string $scene = 'index'): array
{
$p = $request->all();
$config = [];
$config['model'] = $this->modelClass;
switch ($scene) {
case 'index':
if(empty($p['receipt_id'])){
throw new BusinessException('receipt_id不能为空');
}
// ====== ====== 排序 ====== ======
$config['order']['id'] = 'desc';
// ====== ====== 筛选 ====== ======
$config['where']['receipt_id'] = $p['receipt_id'];
break;
case 'show':
$config['where']['id'] = $p['id'];
break;
}
return $config;
}
}
+315
View File
@@ -0,0 +1,315 @@
<?php
namespace app\common\service;
use app\common\logic\FinReceiptLogic;
use app\common\model\FinPaymentApply;
use app\common\model\FinPaymentStock;
use app\common\model\FinReceiptHis;
use app\common\validate\FinReceiptValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\FinReceipt;
use support\exception\BusinessException;
use support\Request;
class FinReceiptService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = FinReceipt::class;
/**
* 验证器
*/
protected $validator = FinReceiptValidate::class;
protected $logic;
public function __construct()
{
$this->logic = (new FinReceiptLogic());
}
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
/**
* 构建查询条件
*/
protected function buildQueryConfig(Request $request, string $scene = 'index'): array
{
$p = $request->all();
$config = [];
$config['model'] = $this->modelClass;
switch ($scene) {
case 'index':
// ====== ====== 排序 ====== ======
$config['order']['id'] = 'desc';
// ====== ====== 筛选 ====== ======
// 汇款国家
if (isset($p['country_id']) && !empty($p['country_id'])) {
$config['where']['country_id'] = $p['country_id'];
}
// 没有付款记录的收款记录
if (isset($p['payment_apply']) && $p['payment_apply'] == 'not') { //未认领
$config['where']['payment_apply_id'] = 0;
} elseif (isset($p['payment_apply']) && $p['payment_apply'] == 'already') { //已认领
$config['where']['payment_apply_id'] = ['>', 0];
} elseif (isset($p['payment_apply']) && $p['payment_apply'] == 'timeout') { //超时未认领
$config['where']['payment_apply_id'] = 0;
$config['where']['created_at'] = ['<' ,time() - 24*60*60];
}
// 收款申请筛选
if (isset($p['payment_apply_id']) && !empty($p['payment_apply_id'])) {
$config['where']['payment_apply_id'] = $p['payment_apply_id'];
}
// 收款账户筛选
if (isset($p['account_id']) && !empty($p['account_id'])) {
$config['where']['account_id'] = $p['account_id'];
}
// 关键字
if (isset($p['keyword']) && $p['keyword']) {
$k = '%' . $p['keyword'] . '%';
$config['orWhere'][] = ['remitter', 'like', $k];
}
break;
case 'show':
$config['where']['id'] = $p['id'];
break;
case 'update':
case 'payment':
case 'destroy':
$config['where']['id'] = $p['id'];
break;
case 'store':
break;
}
return $config;
}
public function beforeServiceStore($data, Request $request)
{
if(isset($data['payment_apply_id']) && empty($data['payment_apply_id'])){
unset($data['payment_apply_id']);
}
$data['receipt_no'] = $this->generateUniqueReceipt();
$data['arrived_at'] = strtotime( $data['arrived_at']);
$data['created_by'] = $request->user->uid;
$data['updated_by'] = $request->user->uid;
$data['fx_receipt_file'] = json_encode($data['fx_receipt_file']);
if($data['fx_receipt_file'] == '[]' || $data['fx_receipt_file'] == '{}' || $data['fx_receipt_file'] == '""')
$data['fx_receipt_file'] = '';
return $data;
}
public function afterServiceStore($res)
{
$receipt = FinReceipt::where('id',$res->id)->first()->toArray();
$receipt = array_merge($receipt,['receipt_id' => $res->id]);
unset($receipt['id']);
FinReceiptHis::create($receipt);
}
public function beforeServiceUpdate($data, Request $request)
{
$data['updated_by'] = $request->user->uid;
if(isset($data['arrived_at']))
$data['arrived_at'] = strtotime($data['arrived_at']);
if(isset($data['received_at']))
$data['received_at'] = strtotime($data['received_at']);
$data['fx_receipt_file'] = json_encode($data['fx_receipt_file']);
if($data['fx_receipt_file'] == '[]' || $data['fx_receipt_file'] == '{}' || $data['fx_receipt_file'] == '""')
$data['fx_receipt_file'] = '';
return $data;
}
public function afterServiceUpdate($res)
{
$receipt = FinReceipt::where('id',$res['id'])->first()->toArray();
$receipt = array_merge($receipt,['receipt_id' => $res['id']]);
unset($receipt['id']);
FinReceiptHis::create($receipt);
}
/**
* 生成唯一的付款申请编码
* 格式:PA + 日期 + 6位字母数字组合
*
* @return string 唯一编码
*/
public function generateUniqueReceipt()
{
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$maxAttempts = 10; // 防止无限循环
$secondChar = 'RE' . date('Ymd');
for ($attempt = 0; $attempt < $maxAttempts; $attempt++) {
// 生成后6位随机字符
$suffix = '';
for ($i = 0; $i < 6; $i++) {
$suffix .= $chars[rand(0, strlen($chars) - 1)];
}
$ReceiptNo = $secondChar . $suffix;
// 检查是否已存在
$existing = FinReceipt::where('receipt_no', $ReceiptNo)->first();
if (!$existing) {
return $ReceiptNo;
}
}
// 如果1000次尝试后仍然重复,使用时间戳确保唯一性
$timestamp = substr(time(), -6);
return $secondChar . $timestamp;
}
/**
* 根据相似度排序付款记录
* @param Request $request
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Pagination\LengthAwarePaginator
*/
public function complexQueryIndex(Request $request)
{
$limit = (int)$request->get('size', 15);
$page = (int)$request->get('current', 1);
$param = $request->all();
$where = [];
// 汇款国家
if (isset($param['country_id']) && !empty($param['country_id'])) {
$where[] = ['country_id', $param['country_id']];
}
// 收款账户筛选
if (isset($param['account_id']) && !empty($param['account_id'])) {
$where[] = ['account_id', $param['account_id']];
}
// 收款申请筛选
if (isset($param['payment_apply_id']) && !empty($param['payment_apply_id'])) {
$where[]['payment_apply_id'] = $param['payment_apply_id'];
}
// 关键字
if (isset($param['keyword']) && $param['keyword']) {
$k = '%' . $param['keyword'] . '%';
$where[] = ['remitter', 'like', $k];
}
// 构建查询
$query = $this->modelClass::where($where);
// 没有付款记录的收款记录
if (isset($param['payment_apply']) && $param['payment_apply'] == 'not') {
$query->where('payment_apply_id', 0);
} elseif (isset($param['payment_apply']) && $param['payment_apply'] == 'already') {
$query->where('payment_apply_id', '>', 0);
} elseif (isset($param['payment_apply']) && $param['payment_apply'] == 'timeout') { //超时未认领
$query->where('payment_apply_id', 0);
$query->where('created_at', '<' ,time() - 24*60*60);
}
if (isset($param['scene']) && $param['scene'] == 'list') {
return $query->paginate($limit, ['*'], 'page', $page);
}
// 计算七天前的时间戳
$sevenDaysAgo = time() - (10 * 24 * 60 * 60);
// 构建相似度排序逻辑
$similarityOrder = [];
// 如果有传入参数,计算相似度
if (isset($param['account_id']) && !empty($param['account_id'])) {
$similarityOrder[] = "CASE WHEN account_id = {$param['account_id']} THEN 0 ELSE 1 END";
}
if (isset($param['country_id']) && !empty($param['country_id'])) {
$similarityOrder[] = "CASE WHEN country_id = {$param['country_id']} THEN 0 ELSE 1 END";
}
if (isset($param['keyword']) && $param['keyword']) {
$keyword = addslashes($param['keyword']);
$similarityOrder[] = "CASE WHEN remitter LIKE '%{$keyword}%' THEN 0 ELSE 1 END";
}
if (isset($param['amount']) && !empty($param['amount'])) {
$amount = (float)$param['amount'];
$similarityOrder[] = "ABS(amount - {$amount})";
}
// 构建排序SQL
$orderByClause = "";
if (!empty($similarityOrder)) {
$orderByClause = "CASE
WHEN created_at >= {$sevenDaysAgo} THEN
(" . implode(' + ', $similarityOrder) . ")
ELSE 999999
END ASC, ";
}
$orderByClause .= "CASE
WHEN created_at < {$sevenDaysAgo} THEN id
ELSE 0
END DESC";
$query->orderByRaw($orderByClause);
return $query->paginate($limit, ['*'], 'page', $page);
}
public function credit($id)
{
$finReceipt = FinReceipt::find($id);
if(!empty($finReceipt->received_at)){
throw new BusinessException('该记录已将确认入账');
}
$res = $this->logic->credit($id);
$this->afterServiceUpdate(['id'=>$id]);
return $res;
}
/**
* 更新:绑定收款记录,计算折让金额
* @param Request $request
* @param array $data
* @param string $scene
* @return mixed
*/
public function serviceUpdatePayment(Request $request, array $data = [], string $scene = 'payment')
{
$payment_apply_id = $request->input('payment_apply_id');
$id = $request->input('id');
$data = $this->preProcess(ucfirst($scene), $data, $request);
$config = $this->buildQueryConfig($request, $scene);
FinPaymentApply::where('id', $payment_apply_id)->update(['receipt_id' => $id, 'claimed_at' => time(), 'claimed_by' => $request->user->uid]);
FinPaymentStock::where('payment_apply_id', $payment_apply_id)->update(['receipt_id' => $id]);
//折让金额
$payment = FinPaymentApply::where('id', $payment_apply_id)->first();
$receipt = FinReceipt::find($id);
$data['discount_amount'] = $payment->amount - $receipt->amount ?? 0;
$updated = $this->logicUpdateBy($config, $data);
return $this->postProcess(ucfirst($scene), $data);
}
}
+51 -5
View File
@@ -161,7 +161,8 @@ class StockOrdersService
{
// 启动审批
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
$appro->start('stock_orders', 'stock_orders', $item->id, [], $item->salesperson_id);
$formData = $this->checkNewOrUsedVehicles($item->id);
$appro->start('stock_orders', 'stock_orders', $item->id, $formData, $item->salesperson_id);
// 异步生成pdf文件
\Webman\RedisQueue\Redis::send('stock-order-pdf', ['id'=>$item->id]);
}
@@ -295,7 +296,8 @@ class StockOrdersService
public function approvalNullify($item)
{
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
$appro->startNullify('stock_orders_nullify', 'stock_orders', $item->id, [], $item->salesperson_id);
$formData = $this->checkNewOrUsedVehicles($item->id);
$appro->startNullify('stock_orders_nullify', 'stock_orders', $item->id, $formData, $item->salesperson_id);
}
/**
@@ -479,14 +481,19 @@ class StockOrdersService
// 检查数据中是否存在该字段且为数组
if (!empty($data[$field]) && is_array($data[$field])) {
// 处理该字段下的每个文件
foreach ($data[$field] as $fileUrl) {
foreach ($data[$field] as $fileInfo) {
$fileData = [
'order_id' => $order->id,
'file_code' => $fileCode,
'file_url' => $fileUrl,
'file_url' => $fileInfo['url'] ?? '',
'file_name' => StockOrdersFiles::FILE_CODE[$fileCode],
'original_name' => $fileInfo['original_name'] ?? StockOrdersFiles::FILE_CODE[$fileCode],
];
StockOrdersFiles::create($fileData);
// 只有当file_url不为空时才创建记录
if (!empty($fileData['file_url'])) {
StockOrdersFiles::create($fileData);
}
}
}
}
@@ -905,6 +912,7 @@ class StockOrdersService
'id' => $file->id,
'file_url' => $file->file_url,
'file_full_url' => $this->getFileUrl($file->file_url),
'original_name' => $file->original_name,
];
}
@@ -978,4 +986,42 @@ class StockOrdersService
}
return (new \app\common\service\OssService)->generatePrivateUrl($fileUrl);
}
/**
* 检查备货单中的车辆是新车还是二手车
* @param int $stockOrderId 备货单ID
* @return array 返回包含has_new和has_used的数组
*/
public function checkNewOrUsedVehicles(int $stockOrderId): array
{
// 获取备货单关联的所有车辆项
$vehicles = StockOrderItems::where('order_id', $stockOrderId)->get();
$hasNew = false;
$hasUsed = false;
$submitNote = '';
foreach ($vehicles as $vehicle) {
// 判断是否为二手车(mileage > 0)
if ($vehicle->mileage > 0) {
$hasUsed = true;
$submitNote = '本单只有二手车';
} else {
$hasNew = true;
$submitNote = '本单只有新车';
}
// 如果两种类型都已经存在,可以提前退出循环
if ($hasNew && $hasUsed) {
$submitNote = '本单包含新车和二手车';
break;
}
}
return [
'has_new' => $hasNew ? 1 : 0,
'has_used' => $hasUsed ? 1 : 0,
'submit_note' => $submitNote
];
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace app\common\service;
use app\common\logic\SysConfigLogic;
use app\common\logic\SysCurrencyLogic;
use app\common\logic\SystemsLogic;
use support\Redis;
class SysConfigService
{
protected $logic;
public function __construct()
{
$this->logic = (new SysConfigLogic());
}
/**
* 获取系统list
*/
public static function getConfig(string $config_key)
{
$value = Redis::get($config_key);
if (empty($value)) {
$config = (new SysConfigLogic())->findByKey($config_key);
if (empty($config)) {
return null;
}
$value = $config->config_value;
Redis::set($config->config_key, $value, 'EX', 7200);
}
return $value;
}
}