diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 8aa63598..4ac5ac81 100755 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -762,6 +762,7 @@ $routes->group('produccion', ['namespace' => 'App\Controllers\Produccion'], func $routes->post("reset/date", 'Ordentrabajo::reset_orden_trabajo_date'); $routes->post("update/pedido/date", 'Ordentrabajo::update_orden_trabajo_pedido_date'); $routes->post("reset/pedido/date", 'Ordentrabajo::reset_orden_trabajo_pedido_date'); + $routes->post("update/pod/pedido/date/(:num)",'Ordentrabajo::update_pod_pedido_dates/$1'); $routes->post("update/pedido", 'Ordentrabajo::update_orden_trabajo_pedido'); $routes->post("update/user", 'Ordentrabajo::update_orden_trabajo_user'); $routes->post("update", 'Ordentrabajo::update_orden_trabajo'); diff --git a/ci4/app/Controllers/Produccion/Ordentrabajo.php b/ci4/app/Controllers/Produccion/Ordentrabajo.php index bc1286cf..61741e33 100755 --- a/ci4/app/Controllers/Produccion/Ordentrabajo.php +++ b/ci4/app/Controllers/Produccion/Ordentrabajo.php @@ -585,4 +585,17 @@ class Ordentrabajo extends BaseController ]; return $this->response->setJSON($data); } + public function update_pod_pedido_dates($orden_trabajo_id) + { + + $this->produccionService->init($orden_trabajo_id); + if($this->produccionService->isPOD){ + $status = $this->produccionService->updatePodDates(); + return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => $status, "data" => $this->produccionService->getPedido()]); + }else{ + return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => false, "data" => $this->produccionService->getPedido()]); + + } + + } } diff --git a/ci4/app/Database/Seeds/PedidoPodSeeder.php b/ci4/app/Database/Seeds/PedidoPodSeeder.php new file mode 100644 index 00000000..e15ed26a --- /dev/null +++ b/ci4/app/Database/Seeds/PedidoPodSeeder.php @@ -0,0 +1,43 @@ + "pod_impresion_dias_tras_confirmacion", + "value" => 1, + "description" => "Días a los que se establece la fecha de impresion en pedido tras confirmación" + ], + [ + "name" => "pod_encuadernacion_dias_tras_confirmacion", + "value" => 2, + "description" => "Días a los que se establece la fecha de encuadernación en pedido tras confirmación" + ], + [ + "name" => "pod_entrega_real_dias_tras_confirmacion", + "value" => 5, + "description" => "Días a los que se establece la fecha entrega real en pedido tras confirmación" + ], + [ + "name" => "pod_embalaje_dias_tras_confirmacion", + "value" => 4, + "description" => "Días a los que se establece la fecha de encuadernación en pedido tras confirmación" + ], + ]; + foreach ($data as $key => $row) { + if($m->getVariable($row['name']) == null) + { + $m->insert($row); + } + } + } +} diff --git a/ci4/app/Models/Configuracion/FestivoModel.php b/ci4/app/Models/Configuracion/FestivoModel.php index 003e4e01..5d0b7b49 100755 --- a/ci4/app/Models/Configuracion/FestivoModel.php +++ b/ci4/app/Models/Configuracion/FestivoModel.php @@ -64,5 +64,9 @@ class FestivoModel extends BaseModel ], ]; + public function isFestivo($date) + { + return $this->where('date',$date)->countAllResults() > 0 ? true : false; + } } diff --git a/ci4/app/Services/ProductionService.php b/ci4/app/Services/ProductionService.php index f39bf921..95090b4a 100755 --- a/ci4/app/Services/ProductionService.php +++ b/ci4/app/Services/ProductionService.php @@ -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 + */ + 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); + + } }