new functionality: Add project coding configuration feature for projects
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled

This commit is contained in:
2025-12-09 23:02:35 +01:00
parent 7b00887372
commit e42ce8b092
13 changed files with 1169 additions and 28 deletions

View File

@@ -22,17 +22,33 @@ class CodeEdit extends Component
'maxLength' => 'required|integer|min:2|max:12',
];
public function mount($componentId, $initialName = '')
public function mount($componentId, $initialName = '', $initialMaxLength = 3, $initialDocumentTypes = [])
{
$this->componentId = $componentId;
$this->name = $initialName;
$this->maxLength = 3;
$this->maxLength = $initialMaxLength;
$this->documentTypes = $initialDocumentTypes;
// Disparar evento inicial para establecer el nombre
// Guardar datos iniciales
$this->initialData = [
'name' => $initialName,
'maxLength' => $initialMaxLength,
'documentTypes' => $initialDocumentTypes
];
// Disparar eventos iniciales
$this->dispatch('nameUpdated',
componentId: $this->componentId,
data: [
'name' => $this->name
'name' => $this->name,
]
);
$this->dispatch('componentUpdated',
componentId: $this->componentId,
data: [
'documentTypes' => $this->documentTypes,
'maxLength' => $this->maxLength
]
);
}
@@ -74,7 +90,7 @@ class CodeEdit extends Component
$this->documentTypes[] = [
'code' => $this->codeInput,
'label' => $this->labelInput,
'max_length' => $this->maxLength,
//'max_length' => $this->maxLength,
];
$this->sortList();

View File

@@ -3,11 +3,14 @@
namespace App\Livewire;
use Livewire\Component;
use App\Models\Project;
use App\Models\ProjectCodingConfig;
class ProjectNameCoder extends Component
{
public $components = [];
public $nextId = 1;
public $project;
protected $listeners = [
'nameUpdated' => 'headerLabelUpdate',
@@ -15,10 +18,40 @@ class ProjectNameCoder extends Component
'removeComponent' => 'removeComponent'
];
public function mount()
public function mount(Project $project)
{
// Inicializar con un componente vacío
$this->addComponent();
$this->project = $project;
// Si hay configuración inicial, cargarla
if ($project->codingConfig) {
$this->loadDatabaseConfiguration();
} else {
// Inicializar con un componente vacío
$this->addComponent();
}
}
private function loadDatabaseConfiguration()
{
// Buscar la configuración de codificación del proyecto
$config = $this->project->codingConfig;
if ($config && isset($config->elements['components'])) {
$this->components = $config->elements['components'];
$this->nextId = count($this->components) + 1;
// Asegurar que cada componente tenga los campos necesarios
foreach ($this->components as &$component) {
$component['data'] = $component['data'] ?? [];
$component['order'] = $component['order'] ?? 0;
$component['headerLabel'] = $component['headerLabel'] ?? '';
$component['documentTypes'] = $component['documentTypes'] ?? [];
}
} else {
// Si no hay configuración, inicializar con un componente vacío
$this->addComponent();
}
}
public function addComponent()
@@ -72,7 +105,6 @@ class ProjectNameCoder extends Component
}
}
}
// Ordenar el array por el campo 'order'
usort($this->components, function($a, $b) {
return $a['order'] - $b['order'];
@@ -142,6 +174,101 @@ class ProjectNameCoder extends Component
return $total;
}
public function saveConfiguration()
{
try {
// Preparar la configuración completa
$configData = [
'components' => $this->components,
//'total_components' => $this->componentsCount,
//'total_document_types' => $this->totalDocumentTypes,
//'generated_format' => $this->generateFormatString(),
//'last_updated' => now()->toDateTimeString(),
];
// Buscar o crear la configuración de codificación
$codingConfig = ProjectCodingConfig::firstOrNew(['project_id' => $this->project->id]);
// Actualizar los campos
$codingConfig->fill([
'elements' => $configData,
'format' => $this->generateFormatString(),
'auto_generate' => true,
]);
$codingConfig->save();
// Emitir evento de éxito
$this->dispatch('configurationSaved', [
'message' => 'Configuración guardada exitosamente',
'format' => $this->generateFormatString()
]);
} catch (\Exception $e) {
$this->dispatch('configurationError', [
'message' => 'Error al guardar la configuración: ' . $e->getMessage()
]);
}
}
private function autoSave()
{
// Auto-guardar cada 30 segundos de inactividad o cuando haya cambios importantes
// Esto es opcional pero mejora la experiencia de usuario
$this->dispatch('configurationAutoSaved');
}
private function generateFormatString()
{
$formatParts = [];
// Agregar el código del proyecto
$formatParts[] = $this->project->code ?? $this->project->reference ?? 'PROJ';
// Agregar cada componente
foreach ($this->components as $component) {
if (!empty($component['headerLabel'])) {
$formatParts[] = '[' . strtoupper($component['headerLabel']) . ']';
}
}
// Agregar el nombre del documento
$formatParts[] = '[DOCUMENT_NAME]';
return implode('-', $formatParts);
}
public function getExampleCodeAttribute()
{
$exampleParts = [];
// Agregar el código del proyecto
$exampleParts[] = $this->project->code ?? $this->project->reference ?? 'PROJ';
// Agregar cada componente con un valor de ejemplo
foreach ($this->components as $component) {
if (!empty($component['headerLabel'])) {
$exampleParts[] = $this->getExampleForComponent($component);
}
}
// Agregar nombre de documento de ejemplo
$exampleParts[] = 'Documento-Ejemplo';
return implode('-', $exampleParts);
}
private function getExampleForComponent($component)
{
if (isset($component['data']['documentTypes']) && count($component['data']['documentTypes']) > 0) {
// Tomar el primer tipo de documento como ejemplo
$firstType = $component['data']['documentTypes'][0];
return $firstType['code'] ?? $firstType['name'] ?? 'TIPO';
}
return 'VALOR';
}
public function render()
{
return view('livewire.project-name-coder');