163 lines
4.9 KiB
PHP
163 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\FinReceiptHis;
|
|
use app\common\validate\FinReceiptValidate;
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
use app\common\model\FinReceipt;
|
|
use support\Request;
|
|
|
|
class FinReceiptService
|
|
{
|
|
|
|
use CrudServiceTrait;
|
|
|
|
/**
|
|
* 指定当前要操作的模型
|
|
*/
|
|
protected string $modelClass = FinReceipt::class;
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected $validator = FinReceiptValidate::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['payment_apply']) && $p['payment_apply'] == 'not') {
|
|
$config['where']['payment_apply_id'] = ['null'];
|
|
} elseif (isset($p['payment_apply']) && $p['payment_apply'] == 'already') {
|
|
$config['where']['payment_apply_id'] = ['not null'];
|
|
}
|
|
|
|
// 收款账户筛选
|
|
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 '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;
|
|
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']);
|
|
|
|
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;
|
|
}
|
|
} |