add autocounter

This commit is contained in:
2024-10-29 19:41:04 +05:00
parent 3eadd3c69b
commit efd49ae392
7 changed files with 38 additions and 2 deletions

View File

@@ -23,7 +23,9 @@ class IncomingLetterResource extends Resource
return $form
->schema([
TextInput::make('number')
->required(),
->integer()
->required()
->default(fn () => getLatestNumber() + 1),
TextInput::make('name')
->required(),

View File

@@ -11,6 +11,6 @@ class CreateIncomingLetter extends CreateRecord
protected function getRedirectUrl(): string
{
return static::getResource()::getUrl('index');
return $this->getResource()::getUrl('index');
}
}

View File

@@ -3,7 +3,9 @@
use App\Modules\EmptyModule;
use App\Modules\ModuleContract;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
/**
@@ -94,3 +96,29 @@ function temp_cache(string $key = ''): mixed
? $tempCache->get($key)
: cache()->driver('array');
}
/**
* Log
*/
function logDB(): void
{
if (! app()->isLocal()) {
return;
}
DB::listen(function ($query) {
Log::info($query->sql, $query->bindings, $query->time);
});
}
/**
* Get latest number
*
* @param string $tableName
*/
function getLatestNumber(string $tableName = 'incoming_letters', string $column = 'number'): int
{
$data = DB::table($tableName)->select(DB::raw("MAX(CAST(REGEXP_REPLACE({$column}, '[^0-9]', '') AS UNSIGNED)) AS max_number"))->first();
return intval($data->max_number) ?? 0;
}

View File

@@ -16,6 +16,7 @@ return new class extends Migration
$table->string('number')->nullable();
$table->string('name')->nullable();
$table->string('main_file')->nullable();
$table->softDeletes();
$table->timestamps();
});
}

View File

@@ -4,10 +4,12 @@ namespace App\Modules\IncomingLetter;
use App\Modules\Makeable;
use App\Modules\ModuleContract;
use Illuminate\Database\Eloquent\SoftDeletes;
class IncomingLetterModule implements ModuleContract
{
use Makeable;
use SoftDeletes;
/**
* Module is enabled

View File

@@ -16,6 +16,7 @@ return new class extends Migration
$table->string('number')->nullable();
$table->string('name')->nullable();
$table->string('main_file')->nullable();
$table->softDeletes();
$table->timestamps();
});
}

View File

@@ -24,6 +24,8 @@ class AppServiceProvider extends ServiceProvider
Model::shouldBeStrict(! app()->isProduction());
$this->registerModules();
// logDB();
}
/**