create POD dates pedido

This commit is contained in:
amazuecos
2025-04-28 02:03:40 +02:00
parent 029757bb40
commit d1e5731d1d
5 changed files with 139 additions and 14 deletions

View File

@ -18,6 +18,7 @@ use App\Entities\Produccion\OrdenTrabajoFileEntity;
use App\Entities\Produccion\OrdenTrabajoTareaEntity;
use App\Entities\Tarifas\Acabados\TarifaAcabadoEntity;
use App\Models\Configuracion\ConfigVariableModel;
use App\Models\Configuracion\FestivoModel;
use App\Models\Configuracion\MaquinaModel;
use App\Models\OrdenTrabajo\OrdenTrabajoFileModel;
use App\Models\OrdenTrabajo\OrdenTrabajoTareaProgressDate;
@ -56,6 +57,7 @@ class ProductionService extends BaseService
protected OrdenTrabajoUser $otUser;
protected OrdenTrabajoEntity $ot;
protected OrdenTrabajoFileModel $otFileModel;
protected FestivoModel $festivoModel;
protected OrdenTrabajoTareaProgressDate $otTareaProgressDate;
protected PedidoModel $pedidoModel;
protected UserModel $userModel;
@ -90,7 +92,7 @@ class ProductionService extends BaseService
* `tirada <= podValue`
* @var boolean
*/
protected bool $isPOD = false;
public bool $isPOD = false;
/**
* Indica si la orden de trabajo contiene gofrado
@ -182,6 +184,7 @@ class ProductionService extends BaseService
$this->otFileModel = model(OrdenTrabajoFileModel::class);
$this->pedidoModel = model(PedidoModel::class);
$this->otTareaProgressDate = model(OrdenTrabajoTareaProgressDate::class);
$this->festivoModel = model(FestivoModel::class);
$this->ordenTrabajoConfig = config('OrdenTrabajo');
$this->statusColor = $this->ordenTrabajoConfig->OT_COLORS["sin_imprimir"];
$this->configVariableModel = model(ConfigVariableModel::class);
@ -260,25 +263,19 @@ class ProductionService extends BaseService
/**
* Inserta las fechas del pedido asociado a la orden de trabajo en `orden_trabajo_tareas`
* Si el pedido es POD las fechas se insertan automaticamente usando `createDatesForPOD` y
* en base a las variables :
* - pod_impresion_dias_tras_confirmacion [fecha_impresion_at]
* - pod_encuadernacion_dias_tras_confirmacion [fecha_encuadernacion]
* - pod_entrega_real_dias_tras_confirmacion [fecha_impresion_at]
* - pod_embalaje_dias_tras_confirmacion [fecha_impresion_at]
*
* @param OrdenTrabajoEntity $ot
* @return integer|boolean|string ID
*/
protected function storeOrdenTrabajoDates(): int|bool|string
{
$fecha_encuadernado = Time::now()->addDays(2)->format("Y-m-d");
$fecha_entrega_real = Time::now()->addDays(5)->format("Y-m-d");
//$fecha_embalaje_at = Time::now()->addDays(4)->format("Y-m-d");
$data = [
"orden_trabajo_id" => $this->ot->id,
"fecha_encuadernado_at" => $fecha_encuadernado,
"fecha_entrega_real_at" => $fecha_entrega_real,
"fecha_impresion_at" => Time::now()->format("Y-m-d"),
//"embalaje_at" => $fecha_embalaje_at,
"fecha_entrega_externo" => $this->pedido->fecha_entrega_externo,
];
$otDateId = $this->otDate->insert($data);
$this->otDate->updateUserDateMap($this->ot->id, $data);
$otDateId = $this->otDate->insert(["orden_trabajo_id" => $this->ot->id]);
return $otDateId;
}
/**
@ -1787,4 +1784,71 @@ class ProductionService extends BaseService
}
return $tareaCosido;
}
public function createDatesForPOD(): array
{
$podDates = [
'fecha_encuadernado' => null,
'fecha_entrega_real' => null,
'fecha_embalaje' => null,
'fecha_impresion' => null,
];
try {
$impresionDays = $this->configVariableModel->getVariable('pod_impresion_dias_tras_confirmacion')->value;
$encuadernadoDays = $this->configVariableModel->getVariable('pod_encuadernacion_dias_tras_confirmacion')->value;
$entregaRealDays = $this->configVariableModel->getVariable('pod_entrega_real_dias_tras_confirmacion')->value;
$embalajeDays = $this->configVariableModel->getVariable('pod_embalaje_dias_tras_confirmacion')->value;
$totalDays = array_sum([$impresionDays, $encuadernadoDays, $entregaRealDays, $embalajeDays]);
$dates = $this->generateWorkdaysFromToday($totalDays);
for ($i = 0; $i < count($dates); $i++) {
if ($i == $impresionDays) {
$podDates['fecha_impresion'] = $dates[$i];
}
if ($i == $encuadernadoDays) {
$podDates['fecha_encuadernado'] = $dates[$i];
}
if ($i == $entregaRealDays) {
$podDates['fecha_entrega_real'] = $dates[$i];
}
if ($i == $embalajeDays) {
$podDates['fecha_embalaje'] = $dates[$i];
}
}
return $podDates;
} catch (\Throwable $th) {
return[ "error" => $th->getMessage()];
}
}
/**
* Devuelve un array con la longitud `$count` con las fechas que corresponde con días laborales.
* Menos fines de semana y festivos.
*
* @param integer $count
* @return array <str,YYYY-MM-DD>
*/
public function generateWorkdaysFromToday(int $count): array
{
$current = Time::now();
$workdays = [];
while (count($workdays) < $count) {
$dayOfWeek = $current->format('N'); // 1 (Lunes) to 7 (Domingo)
if ($dayOfWeek < 6) { // Lunes a Viernes
$date = $current->format('Y-m-d 00:00:00');
if ($this->festivoModel->isFestivo($date) == false) {
$workdays[] = $date;
}
}
$current = $current->addDays(1);
}
return $workdays;
}
public function updatePodDates() : bool
{
$dates = $this->createDatesForPOD();
return $this->pedidoModel->update($this->pedido->id,$dates);
}
}