generated from liutk/owl-admin-base
135 lines
4.7 KiB
PHP
135 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services\Finance;
|
|
|
|
use App\Admin\Filters\StoreMasterCommissionFilter;
|
|
use App\Admin\Services\BaseService;
|
|
use App\Enums\CheckStatus;
|
|
use App\Models\Store;
|
|
use App\Models\StoreMasterCommission;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class StoreMasterCommissionService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['store', 'master', 'workflow'];
|
|
|
|
protected string $modelName = StoreMasterCommission::class;
|
|
|
|
protected string $modelFilterName = StoreMasterCommissionFilter::class;
|
|
|
|
public function store($data): bool
|
|
{
|
|
Validator::validate(
|
|
data: $data,
|
|
rules: [
|
|
'month' => ['bail', 'required', 'date_format:Y-m'],
|
|
'store_id' => ['bail', 'required', 'int'],
|
|
'commission' => ['bail', 'required', 'numeric'],
|
|
'daily_expenses' => ['bail', 'required', 'numeric'],
|
|
'employee_expenses' => ['bail', 'required', 'numeric'],
|
|
'other_expenses' => ['bail', 'required', 'numeric'],
|
|
],
|
|
attributes: [
|
|
'month' => __('finance.store_master_commission.month'),
|
|
'store_id' => __('finance.store_master_commission.store'),
|
|
'commission' => __('finance.store_master_commission.commission'),
|
|
'daily_expenses' => __('finance.store_master_commission.daily_expenses'),
|
|
'employee_expenses' => __('finance.store_master_commission.employee_expenses'),
|
|
'other_expenses' => __('finance.store_master_commission.other_expenses'),
|
|
],
|
|
);
|
|
|
|
$store = Store::findOrFail($data['store_id']);
|
|
|
|
$attributes = array_merge($data, [
|
|
'store_master_id' => $store->master_id,
|
|
]);
|
|
|
|
$this->modelName::create($attributes);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
Validator::validate(
|
|
data: $data,
|
|
rules: [
|
|
'month' => ['filled', 'required', 'date_format:Y-m'],
|
|
'store_id' => ['filled', 'required', 'int'],
|
|
'commission' => ['filled', 'required', 'numeric'],
|
|
'daily_expenses' => ['filled', 'required', 'numeric'],
|
|
'employee_expenses' => ['filled', 'required', 'numeric'],
|
|
'other_expenses' => ['filled', 'required', 'numeric'],
|
|
],
|
|
attributes: [
|
|
'month' => __('finance.store_master_commission.month'),
|
|
'store_id' => __('finance.store_master_commission.store'),
|
|
'commission' => __('finance.store_master_commission.commission'),
|
|
'daily_expenses' => __('finance.store_master_commission.daily_expenses'),
|
|
'employee_expenses' => __('finance.store_master_commission.employee_expenses'),
|
|
'other_expenses' => __('finance.store_master_commission.other_expenses'),
|
|
],
|
|
);
|
|
|
|
$model = $this->query()->whereKey($primaryKey)->firstOrFail();
|
|
|
|
switch ($model->workflow->check_status) {
|
|
case CheckStatus::Processing:
|
|
admin_abort('不能修改审核中的店长提成记录');
|
|
break;
|
|
case CheckStatus::Success:
|
|
admin_abort('不能修改审核通过的店长提成记录');
|
|
break;
|
|
}
|
|
|
|
foreach ([
|
|
'month',
|
|
'store_id',
|
|
'commission',
|
|
'daily_expenses',
|
|
'employee_expenses',
|
|
'other_expenses',
|
|
] as $attribute) {
|
|
if (! array_key_exists($attribute, $data)) {
|
|
continue;
|
|
}
|
|
|
|
switch ($attribute) {
|
|
case 'store_id':
|
|
if ($model->store_id != $data['store_id']) {
|
|
$store = Store::findOrFail($data['store_id']);
|
|
$model->store_id = $store->id;
|
|
$model->store_master_id = $store->master_id;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
$model->{$attribute} = $data[$attribute];
|
|
break;
|
|
}
|
|
}
|
|
|
|
$model->save();
|
|
|
|
return true;
|
|
}
|
|
|
|
public function preDelete(array $ids): void
|
|
{
|
|
if (
|
|
$this->query()
|
|
->whereIn($this->primaryKey(), $ids)
|
|
->whereHas('workflow', fn ($query) => $query->whereIn('check_status', [CheckStatus::Processing, CheckStatus::Success]))
|
|
->exists()
|
|
) {
|
|
admin_abort('选中记录中包含审核中或审核通过的记录');
|
|
}
|
|
}
|
|
|
|
public function sortColumn()
|
|
{
|
|
return 'id';
|
|
}
|
|
}
|