new functionality: Add project coding configuration feature for projects
This commit is contained in:
@@ -87,4 +87,109 @@ class Project extends Model
|
||||
{
|
||||
return $this->belongsTo(Company::class);
|
||||
}
|
||||
|
||||
// Agregar estas relaciones al modelo Project
|
||||
public function codingConfig()
|
||||
{
|
||||
return $this->hasOne(ProjectCodingConfig::class);
|
||||
}
|
||||
|
||||
public function documentStatuses()
|
||||
{
|
||||
//return $this->hasMany(ProjectDocumentStatus::class);
|
||||
}
|
||||
|
||||
public function defaultStatus()
|
||||
{
|
||||
//return $this->hasOne(ProjectDocumentStatus::class)->where('is_default', true);
|
||||
}
|
||||
|
||||
// Método para inicializar la configuración
|
||||
public function initializeSettings()
|
||||
{
|
||||
// Crear configuración de codificación si no existe
|
||||
if (!$this->codingConfig) {
|
||||
$this->codingConfig()->create([
|
||||
'format' => '[PROJECT]-[TYPE]-[YEAR]-[SEQUENCE]',
|
||||
'next_sequence' => 1,
|
||||
'year_format' => 'Y',
|
||||
'separator' => '-',
|
||||
'sequence_length' => 4,
|
||||
'auto_generate' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
// Crear estados predeterminados si no existen
|
||||
/*
|
||||
if ($this->documentStatuses()->count() === 0) {
|
||||
$defaultStatuses = [
|
||||
[
|
||||
'name' => 'Borrador',
|
||||
'slug' => 'draft',
|
||||
'color' => '#6b7280', // Gris
|
||||
'order' => 1,
|
||||
'is_default' => true,
|
||||
'allow_upload' => true,
|
||||
'allow_edit' => true,
|
||||
'allow_delete' => true,
|
||||
'requires_approval' => false,
|
||||
'description' => 'Documento en proceso de creación',
|
||||
],
|
||||
[
|
||||
'name' => 'En Revisión',
|
||||
'slug' => 'in_review',
|
||||
'color' => '#f59e0b', // Ámbar
|
||||
'order' => 2,
|
||||
'is_default' => false,
|
||||
'allow_upload' => false,
|
||||
'allow_edit' => false,
|
||||
'allow_delete' => false,
|
||||
'requires_approval' => true,
|
||||
'description' => 'Documento en proceso de revisión',
|
||||
],
|
||||
[
|
||||
'name' => 'Aprobado',
|
||||
'slug' => 'approved',
|
||||
'color' => '#10b981', // Verde
|
||||
'order' => 3,
|
||||
'is_default' => false,
|
||||
'allow_upload' => false,
|
||||
'allow_edit' => false,
|
||||
'allow_delete' => false,
|
||||
'requires_approval' => false,
|
||||
'description' => 'Documento aprobado',
|
||||
],
|
||||
[
|
||||
'name' => 'Rechazado',
|
||||
'slug' => 'rejected',
|
||||
'color' => '#ef4444', // Rojo
|
||||
'order' => 4,
|
||||
'is_default' => false,
|
||||
'allow_upload' => true,
|
||||
'allow_edit' => true,
|
||||
'allow_delete' => false,
|
||||
'requires_approval' => false,
|
||||
'description' => 'Documento rechazado',
|
||||
],
|
||||
[
|
||||
'name' => 'Archivado',
|
||||
'slug' => 'archived',
|
||||
'color' => '#8b5cf6', // Violeta
|
||||
'order' => 5,
|
||||
'is_default' => false,
|
||||
'allow_upload' => false,
|
||||
'allow_edit' => false,
|
||||
'allow_delete' => false,
|
||||
'requires_approval' => false,
|
||||
'description' => 'Documento archivado',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($defaultStatuses as $status) {
|
||||
//$this->documentStatuses()->create($status);
|
||||
}
|
||||
}*/
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
107
app/Models/ProjectCodingConfig.php
Normal file
107
app/Models/ProjectCodingConfig.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?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]',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user