Files
Nexora/app/Models/ProjectCodingConfig.php
Javi e42ce8b092
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
new functionality: Add project coding configuration feature for projects
2025-12-09 23:02:35 +01:00

107 lines
3.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProjectCodingConfig extends Model
{
use HasFactory;
protected $fillable = [
'project_id',
'format',
'elements',
'next_sequence',
'year_format',
'separator',
'sequence_length',
'auto_generate'
];
protected $casts = [
'elements' => 'array',
'auto_generate' => 'boolean',
];
public function project()
{
return $this->belongsTo(Project::class);
}
// Método para generar un código de documento
public function generateCode($type = 'DOC', $year = null)
{
$code = $this->format;
// Reemplazar variables
$replacements = [
'[PROJECT]' => $this->project->code ?? 'PROJ',
'[TYPE]' => $type,
'[YEAR]' => $year ?? date($this->year_format),
'[MONTH]' => date('m'),
'[DAY]' => date('d'),
'[SEQUENCE]' => str_pad($this->next_sequence, $this->sequence_length, '0', STR_PAD_LEFT),
'[RANDOM]' => strtoupper(substr(md5(uniqid()), 0, 6)),
];
// Si hay elementos personalizados, agregarlos
if (is_array($this->elements)) {
foreach ($this->elements as $key => $value) {
$replacements["[{$key}]"] = $value;
}
}
$code = str_replace(array_keys($replacements), array_values($replacements), $code);
// Incrementar secuencia
if (strpos($this->format, '[SEQUENCE]') !== false && $this->auto_generate) {
$this->increment('next_sequence');
}
return $code;
}
// Método para obtener elementos disponibles
public static function getAvailableElements()
{
return [
'project' => [
'label' => 'Código del Proyecto',
'description' => 'Código único del proyecto',
'variable' => '[PROJECT]',
],
'type' => [
'label' => 'Tipo de Documento',
'description' => 'Tipo de documento (DOC, IMG, PDF, etc.)',
'variable' => '[TYPE]',
],
'year' => [
'label' => 'Año',
'description' => 'Año actual en formato configurable',
'variable' => '[YEAR]',
],
'month' => [
'label' => 'Mes',
'description' => 'Mes actual (01-12)',
'variable' => '[MONTH]',
],
'day' => [
'label' => 'Día',
'description' => 'Día actual (01-31)',
'variable' => '[DAY]',
],
'sequence' => [
'label' => 'Secuencia',
'description' => 'Número secuencial autoincremental',
'variable' => '[SEQUENCE]',
],
'random' => [
'label' => 'Aleatorio',
'description' => 'Cadena aleatoria de 6 caracteres',
'variable' => '[RANDOM]',
],
];
}
}