自媒体渠道绑定部门
This commit is contained in:
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace app\adminapi\logic\finance;
|
||||
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\model\dept\Dept;
|
||||
use app\common\model\finance\AccountCost;
|
||||
use app\common\service\qywx\MediaChannelService;
|
||||
|
||||
@@ -16,14 +17,31 @@ class AccountCostLogic extends BaseLogic
|
||||
$costDate = (string) $params['cost_date'];
|
||||
$mediaChannelCode = trim((string) ($params['media_channel_code'] ?? ''));
|
||||
$mediaChannelName = MediaChannelService::getNameByCode($mediaChannelCode);
|
||||
$supportsDeptBinding = AccountCost::supportsDeptBinding();
|
||||
$deptId = $supportsDeptBinding ? (int) ($params['dept_id'] ?? 0) : 0;
|
||||
$deptName = $supportsDeptBinding ? self::resolveDeptName($deptId) : '';
|
||||
|
||||
if (AccountCost::where('cost_date', $costDate)->where('media_channel_code', $mediaChannelCode)->count() > 0) {
|
||||
self::setError('该日期下所选渠道的账户消耗已存在,请直接编辑');
|
||||
if ($supportsDeptBinding && $deptName === '') {
|
||||
self::setError('请选择绑定部门');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
AccountCost::create([
|
||||
$existsQuery = AccountCost::where('cost_date', $costDate)
|
||||
->where('media_channel_code', $mediaChannelCode);
|
||||
if ($supportsDeptBinding) {
|
||||
$existsQuery->where('dept_id', $deptId);
|
||||
}
|
||||
|
||||
if ($existsQuery->count() > 0) {
|
||||
self::setError($supportsDeptBinding
|
||||
? '该日期下所选渠道和部门的账户消耗已存在,请直接编辑'
|
||||
: '该日期下所选渠道的账户消耗已存在,请直接编辑');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'cost_date' => $costDate,
|
||||
'media_channel_code' => $mediaChannelCode,
|
||||
'media_channel_name' => $mediaChannelName,
|
||||
@@ -33,7 +51,13 @@ class AccountCostLogic extends BaseLogic
|
||||
'creator_name' => $adminName,
|
||||
'updater_id' => $adminId,
|
||||
'updater_name' => $adminName,
|
||||
]);
|
||||
];
|
||||
if ($supportsDeptBinding) {
|
||||
$payload['dept_id'] = $deptId;
|
||||
$payload['dept_name'] = $deptName;
|
||||
}
|
||||
|
||||
AccountCost::create($payload);
|
||||
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
@@ -53,6 +77,31 @@ class AccountCostLogic extends BaseLogic
|
||||
return false;
|
||||
}
|
||||
|
||||
$supportsDeptBinding = AccountCost::supportsDeptBinding();
|
||||
$deptId = $supportsDeptBinding ? (int) ($params['dept_id'] ?? 0) : 0;
|
||||
$deptName = $supportsDeptBinding ? self::resolveDeptName($deptId) : '';
|
||||
if ($supportsDeptBinding && $deptName === '') {
|
||||
self::setError('请选择绑定部门');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($supportsDeptBinding) {
|
||||
if (AccountCost::where('id', '<>', (int) $params['id'])
|
||||
->where('cost_date', (string) $model->cost_date)
|
||||
->where('media_channel_code', (string) $model->media_channel_code)
|
||||
->where('dept_id', $deptId)
|
||||
->count() > 0) {
|
||||
self::setError('该日期下所选渠道和部门的账户消耗已存在,请直接编辑');
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($supportsDeptBinding) {
|
||||
$model->dept_id = $deptId;
|
||||
$model->dept_name = $deptName;
|
||||
}
|
||||
$model->amount = round((float) $params['amount'], 2);
|
||||
$model->remark = (string) ($params['remark'] ?? '');
|
||||
$model->updater_id = $adminId;
|
||||
@@ -91,4 +140,18 @@ class AccountCostLogic extends BaseLogic
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static function resolveDeptName(int $deptId): string
|
||||
{
|
||||
if ($deptId <= 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$dept = Dept::find($deptId);
|
||||
if (!$dept) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return (string) ($dept->name ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user