fix(settings): fee only from fee_service_charge_percentage; deploy:tenant

Remove service_charge_percentage alias and config/fee.php fallback (empty → 0).
Move code-managed Montzar/price runtime keys to can_edit=1 in deploy SQL.
Add settings:deploy and deploy:tenant artisan commands for server deploy.
Advanced admin settings merge runtime keys from local DB; block manual edits.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-19 19:55:29 +03:30
co-authored by Cursor
parent 5b2ed2e5fc
commit 027170a33f
12 changed files with 353 additions and 142 deletions
@@ -0,0 +1,151 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
class DeploySettingsConstCommand extends Command
{
protected $signature = 'settings:deploy
{database?* : نام دیتابیس (اختیاری؛ پیش‌فرض DB_DATABASE)}
{--file= : مسیر SQL (پیش‌فرض database/sql/deploy_settings_const.sql)}
{--preserve : deploy_settings_const_preserve_value_sync_can_edit.sql}';
protected $description = 'اجرای deploy_settings_const.sql روی DB tenant با اتصال .env';
public function handle(): int
{
$path = $this->resolveSqlPath();
if ($path === null) {
return self::FAILURE;
}
$cfg = config('database.connections.'.config('database.default'));
if (($cfg['driver'] ?? '') !== 'mysql') {
$this->error('فقط mysql پشتیبانی می‌شود.');
return self::FAILURE;
}
$host = (string) ($cfg['host'] ?? '127.0.0.1');
$port = (string) ($cfg['port'] ?? '3306');
$user = (string) ($cfg['username'] ?? '');
$pass = (string) ($cfg['password'] ?? '');
$defaultDb = (string) ($cfg['database'] ?? '');
if ($user === '') {
$this->error('DB_USERNAME در .env خالی است.');
return self::FAILURE;
}
$databases = $this->argument('database');
if ($databases === [] || $databases === null) {
if ($defaultDb === '') {
$this->error('نام DB ندهید → DB_DATABASE را در .env بگذارید یا آرگومان بدهید.');
return self::FAILURE;
}
$databases = [$defaultDb];
}
$mysql = $this->findMysqlBinary();
if ($mysql === null) {
$this->error('mysql client پیدا نشد. نصب کنید یا اجرا کنید: bash database/sql/run_deploy_settings.sh');
return self::FAILURE;
}
$sql = file_get_contents($path);
if ($sql === false || trim($sql) === '') {
$this->error("فایل SQL خالی یا غیرقابل خواندن: {$path}");
return self::FAILURE;
}
foreach ($databases as $db) {
$db = trim((string) $db);
if ($db === '') {
continue;
}
$this->info("==> {$path} → {$db} @ {$host}:{$port}");
$process = new Process([
$mysql,
'-h', $host,
'-P', $port,
'-u', $user,
'--default-character-set=utf8mb4',
$db,
], null, ['MYSQL_PWD' => $pass]);
$process->setInput($sql);
$process->setTimeout(null);
$process->run(function ($type, $buffer) {
$line = trim($buffer);
if ($line === '') {
return;
}
if ($type === Process::ERR) {
$this->error($line);
} else {
$this->line($line);
}
});
if (! $process->isSuccessful()) {
$this->error("خطا روی دیتابیس: {$db}");
return self::FAILURE;
}
$this->info(" done: {$db}");
}
$this->info('==> all done');
return self::SUCCESS;
}
private function resolveSqlPath(): ?string
{
if ($this->option('preserve')) {
$relative = 'database/sql/deploy_settings_const_preserve_value_sync_can_edit.sql';
} else {
$relative = $this->option('file') ?: 'database/sql/deploy_settings_const.sql';
}
$path = str_starts_with($relative, '/')
? $relative
: base_path($relative);
if (! is_file($path)) {
$this->error("فایل SQL پیدا نشد: {$path}");
return null;
}
return $path;
}
private function findMysqlBinary(): ?string
{
$process = new Process(['command', '-v', 'mysql']);
$process->run();
if ($process->isSuccessful()) {
$bin = trim($process->getOutput());
if ($bin !== '') {
return $bin;
}
}
foreach (['/usr/bin/mysql', '/usr/local/bin/mysql'] as $candidate) {
if (is_executable($candidate)) {
return $candidate;
}
}
return null;
}
}
@@ -0,0 +1,55 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class DeployTenantCommand extends Command
{
protected $signature = 'deploy:tenant
{database?* : نام دیتابیس برای settings:deploy (پیش‌فرض DB_DATABASE)}
{--preserve : deploy_settings_const_preserve_value_sync_can_edit.sql}
{--file= : مسیر SQL سفارشی برای settings:deploy}
{--skip-migrate : فقط settings:deploy}
{--skip-settings : فقط migrate}';
protected $description = 'migrate + deploy_settings_const.sql — دیپلوی DB tenant با یک دستور';
public function handle(): int
{
if (! $this->option('skip-migrate')) {
$this->info('==> 1/2 php artisan migrate --force');
if ($this->call('migrate', ['--force' => true]) !== self::SUCCESS) {
$this->error('migrate ناموفق بود.');
return self::FAILURE;
}
}
if (! $this->option('skip-settings')) {
$this->info('==> 2/2 php artisan settings:deploy');
$settingsArgs = [];
$databases = $this->argument('database');
if ($databases !== [] && $databases !== null) {
$settingsArgs['database'] = $databases;
}
if ($this->option('preserve')) {
$settingsArgs['--preserve'] = true;
}
if ($this->option('file')) {
$settingsArgs['--file'] = $this->option('file');
}
if ($this->call('settings:deploy', $settingsArgs) !== self::SUCCESS) {
$this->error('settings:deploy ناموفق بود.');
return self::FAILURE;
}
}
$this->info('==> deploy:tenant done');
return self::SUCCESS;
}
}
@@ -29,25 +29,11 @@ class SettingsController extends Controller
->orderBy('key');
if ($request->boolean('system_only')) {
if (! $request->boolean('skip_overlay')) {
$overlay = app(SuperAdminProgramSettingsOverlay::class)->items();
if ($overlay) {
return response()->json($this->authorizeSettingsPayload($this->overlayToRows($overlay)));
}
}
$local = $query->get();
$rows = $query
->where(function ($q) {
$q->where('can_edit', 0)->orWhere('can_edit', false)->orWhere('can_edit', '0');
})
->where(function ($q) {
$q->where('is_active', 1)
->orWhere('is_active', true)
->orWhere('is_active', '1');
})
->get();
return response()->json($this->authorizeSettingsPayload($rows));
return response()->json($this->authorizeSettingsPayload(
$this->composeSystemSettings($local)
));
}
if ($request->boolean('editable_only')) {
@@ -92,6 +78,13 @@ class SettingsController extends Controller
$setting = new Setting();
$setting->key = $request->key;
$setting->can_edit = 1;
} elseif (
SuperAdminProgramSettingsOverlay::isRuntime((string) $request->key)
&& ! $this->isPanelManagedKey((string) $request->key)
) {
return response()->json([
'message' => 'این مقدار توسط سیستم به‌روز می‌شود و از پنل قابل ویرایش نیست',
], 403);
} elseif (!$setting->can_edit && !$this->isPanelManagedKey((string) $request->key)) {
return response()->json([
'message' => 'این تنظیم از سوپرادمین کنترل می‌شود و در پنل خانگی قابل ویرایش نیست',
@@ -163,6 +156,44 @@ class SettingsController extends Controller
return in_array($key, self::PANEL_MANAGED_KEYS, true);
}
/**
* تنظیمات پیشرفته: can_edit=0 (سوپرادمین) + کلیدهای can_edit=1 که کد مستقیم می‌نویسد.
*/
private function composeSystemSettings($localRows)
{
$overlay = app(SuperAdminProgramSettingsOverlay::class);
$saMap = $overlay->map();
$byKey = [];
if ($saMap !== null) {
foreach ($saMap as $key => $item) {
if (! is_array($item)) {
continue;
}
$byKey[(string) $key] = $this->overlayRowToSetting($item);
}
} else {
foreach ($localRows as $row) {
if (! $this->isEditableCanEdit($row->can_edit ?? null)) {
$byKey[(string) $row->key] = $row;
}
}
}
foreach ($localRows as $row) {
$key = (string) $row->key;
if (
SuperAdminProgramSettingsOverlay::isLocalOwned($key)
|| SuperAdminProgramSettingsOverlay::isRuntime($key)
) {
$byKey[$key] = $row;
}
}
return collect(array_values($byKey))->sortBy(fn ($row) => (string) ($row->key ?? ''))->values();
}
/**
* get_settings: کلیدهای سوپرادمین (can_edit=0) + ردیف‌های جدول لوکال با can_edit=1.
* اگر سوپرادمین در دسترس نباشد کل جدول لوکال برمی‌گردد.
+5 -7
View File
@@ -197,17 +197,15 @@ class ConfigService
return max(0.0, (float) $this->get('montzar_diff', 0));
}
/** درصد کارمزد سفارش — fee_service_charge_percentage، service_charge_percentage، config/fee.php */
/** درصد کارمزد سفارش — فقط fee_service_charge_percentage؛ خالی = ۰ */
public function orderServiceChargePercentage(): float
{
foreach (['fee_service_charge_percentage', 'service_charge_percentage'] as $key) {
$raw = $this->getString($key, '');
if ($raw !== '') {
return $this->normalizeServiceChargeRatio((float) $raw);
}
$raw = trim($this->getString('fee_service_charge_percentage', ''));
if ($raw === '') {
return 0.0;
}
return $this->normalizeServiceChargeRatio((float) config('fee.service_charge_percentage', 0));
return $this->normalizeServiceChargeRatio((float) $raw);
}
/**
@@ -13,7 +13,7 @@ class SuperAdminProgramSettingsOverlay
'tracking_systems',
];
/** لاگ/قیمت لحظه‌ای همکار — مال دیتابیس نسخه است نه سوپرادمین */
/** لاگ/قیمت لحظه‌ای همکار — can_edit=1 در DB؛ کد مستقیم UpdateConfig می‌زند، نه سوپرادمین */
public const RUNTIME_KEYS = [
'montzar_last_product_log',
'montzar_order_response',