| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- <?php
- /*
- Desarrollador: Ing. Jean Jairo Benitez Meza
- Ultima Modificación: 15/09/2023
- Módulo: Analisis de Fallas
- */
- namespace App\Http\Controllers;
- use App\Http\Controllers\Controller;
- use App\Http\Controllers\ResponseController;
- use App\Http\Controllers\FunctionsController;
- use App\Http\Controllers\EncryptionController;
- use App\Http\Controllers\ResourcesController;
- use Illuminate\Http\Request;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Validator;
- class FailureListController extends Controller
- {
- private $responseController;
- private $encController;
- private $functionsController;
- private $resourcesController;
- public function __construct() {
- $this->responseController = new ResponseController();
- $this->encController = new EncryptionController();
- $this->functionsController = new FunctionsController();
- $this->resourcesController = new ResourcesController();
- }
- public function getFailures($user, $line) {
- $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
- if ($arrResponseCheckUser['error']) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
- }
- try {
- $arrFailures = DB::table('S002V01TLIFA')
- ->where('LIFA_NULI', '=', $line)
- ->get([
- 'LIFA_IDFA AS ID_FALLA',
- 'LIFA_NOFA AS NOMBRE_FALLA',
- 'LIFA_NIVE AS NIVEL_CRITICIDAD',
- 'LIFA_CAUS AS CAUSA_FALLA',
- 'LIFA_SOLU AS SOLICION',
- 'LIFA_DESC AS DESCRIPCION',
- 'LIFA_ESTA AS ESTADO',
- 'LIFA_USRE AS USUARIO_REGISTRA',
- 'LIFA_FERE AS FECHA_REGISTRA',
- 'LIFA_USMO AS USUARIO_MODIFICA',
- 'LIFA_FEMO AS FECHA_MODIFICA',
- ]);
- $arrFailures = json_decode(json_encode($arrFailures), true);
- } catch (\Throwable $th) {
- return $this->responseController->makeResponse(true, "ERR_FAILURES_GET000: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
- }
- $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrFailures, $line);
- if ($responseCheckLatestUpdate['error']) {
- return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
- }
- $arrFailures = $responseCheckLatestUpdate['response'];
- return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrFailures);
- }
- public function getFailuresActives($user, $line) {
- try {
- $getFailures = DB::table('S002V01TLIFA')
- ->where('LIFA_ESTA', '=', 'Activo')
- ->where('LIFA_NULI', '=', 1)
- ->get([
- 'LIFA_IDFA AS ID_FALLA',
- 'LIFA_NOFA AS NOMBRE_FALLA',
- 'LIFA_NIVE AS NIVEL_CRITICIDAD',
- 'LIFA_CAUS AS CAUSA_FALLA',
- 'LIFA_SOLU AS SOLICION',
- 'LIFA_DESC AS DESCRIPCION',
- 'LIFA_ESTA AS ESTADO',
- 'LIFA_USRE AS USUARIO_REGISTRA',
- 'LIFA_FERE AS FECHA_REGISTRA',
- 'LIFA_USMO AS USUARIO_MODIFICA',
- 'LIFA_FEMO AS FECHA_MODIFICA',
- ]);
- } catch (\Throwable $th) {
- return $this->responseController->makeResponse(true, "ERR_FAILURES_GET000: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
- }
- return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $getFailures);
- }
- public function getFailureById($idFailure, $user, $line) {
- $idFailure = $this->encController->decrypt($idFailure);
- try {
- $arrFailures = (array) DB::table('S002V01TLIFA')
- ->where('LIFA_IDFA', '=', $idFailure)
- ->where('LIFA_NULI', '=', $line)
- ->where('LIFA_ESTA', '=', 'Activo')
- ->first([
- 'LIFA_IDFA AS ID_FALLA',
- 'LIFA_NOFA AS NOMBRE_FALLA',
- 'LIFA_NIVE AS NIVEL_CRITICIDAD',
- 'LIFA_CAUS AS CAUSA_FALLA',
- 'LIFA_SOLU AS SOLICION',
- 'LIFA_DESC AS DESCRIPCION',
- 'LIFA_ESTA AS ESTADO',
- 'LIFA_USRE AS USUARIO_REGISTRA',
- 'LIFA_FERE AS FECHA_REGISTRA',
- 'LIFA_USMO AS USUARIO_MODIFICA',
- 'LIFA_FEMO AS FECHA_MODIFICA',
- ]);
- } catch (\Throwable $th) {
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_GETBYID000: No se pudo realizar la consulta a la base.",
- $th->getMessage(),
- 500
- );
- }
- if ( !empty($arrFailures) ) {
- try {
- $arrEquipment = DB::table('S002V01TLFEQ')
- ->where('LFEQ_IDFA', '=', $arrFailures['ID_FALLA'])
- ->where('LFEQ_NULI', '=', $line)
- ->where('LFEQ_ESTA', '=', 'Activo')
- ->where('EQUI_NULI', '=', $line)
- ->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'LFEQ_COEQ')
- ->get([
- 'EQUI_COEQ AS CODIGO',
- 'EQUI_TIPO AS TIPO',
- 'EQUI_MODE AS MODELO',
- 'EQUI_IDEQ AS ID_EQUIPO',
- 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
- // 'EQUI_GAIM AS GALERIA_IMAGENES',
- 'EQUI_ELOR AS ELEMENTO_ORIGEN',
- 'EQUI_TICO AS TIPO_CODIGO'
- ]);
- } catch (\Throwable $th) {
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_GETBYID001: No se pudo realizar la consulta a la base.",
- $th->getMessage(),
- 500
- );
- }
-
- $arrEquipment = json_decode( json_encode($arrEquipment), true );
- foreach ($arrEquipment as $keyEquipment => $equipment) {
- $equipment['ID_EQUIPO'] = strval($equipment['ID_EQUIPO']);
- $arrEquipment[$keyEquipment] = $equipment;
- }
- $arrFailures['EQUIPAMIENTOS'] = $arrEquipment;
- }
- return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrFailures);
- }
- public function getFailureListByClassification($clasificate, $user, $line) {
- try {
- $clasificate = $this->encController->decrypt($clasificate);
- } catch (\Throwable $th) {
- return $this->responseController->makeResponse(true, "ERR_FAILURES_GETCLASS000: No se pudo obtener las clasificación.", $th->getMessage(), 500);
- }
- try {
- $getFailures = DB::table('S002V01TLIFA')
- ->where('LIFA_NIVE', '=', $clasificate)
- ->where('LIFA_ESTA', '=', 'Activo')
- ->where('LIFA_NULI', '=', 1)
- ->get([
- 'LIFA_IDFA AS ID_FALLA',
- 'LIFA_NOFA AS NOMBRE_FALLA',
- 'LIFA_NIVE AS NIVEL_CRITICIDAD',
- 'LIFA_CAUS AS CAUSA_FALLA',
- 'LIFA_SOLU AS SOLICION',
- 'LIFA_DESC AS DESCRIPCION',
- 'LIFA_ESTA AS ESTADO',
- 'LIFA_USRE AS USUARIO_REGISTRA',
- 'LIFA_FERE AS FECHA_REGISTRA',
- 'LIFA_USMO AS USUARIO_MODIFICA',
- 'LIFA_FEMO AS FECHA_MODIFICA',
- ]);
- } catch (\Throwable $th) {
- return $this->responseController->makeResponse(true, "ERR_FAILURES_GETCLASS001: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
- }
- return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $getFailures);
- }
-
- public function registerFailures(Request $request) {
- $validator = Validator::make($request->all(), [
- 'NOMBRE_FALLA' => 'required|string',
- 'NIVEL' => 'required|string',
- 'CAUSA' => 'required|string',
- 'SOLUCION' => 'required|string',
- 'DESCRIPCION' => 'required|string',
- 'EQUIPAMIENTOS' => 'required',
- 'NUMERO_LINEA' => 'required|integer',
- 'USUARIO' => 'required|string',
- ]);
- if ($validator->fails()) {
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_REG000: Se encontraron uno o más errores.",
- $this->responseController->makeErrors($validator->errors()->messages()),
- 401
- );
- }
- DB::beginTransaction();
- $requestData = $request->all();
- try {
- $user = $this->encController->decrypt($requestData['USUARIO']);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", $th->getMessage(), 500);
- }
- $now = $this->functionsController->now();
- $currentDate = $now->toDateTimeString();
- try {
- $idFailure = DB::table('S002V01TLIFA')->insertGetId([
- 'LIFA_NULI' => $requestData['NUMERO_LINEA'],
- 'LIFA_NOFA' => $requestData['NOMBRE_FALLA'],
- 'LIFA_NIVE' => $requestData['NIVEL'],
- 'LIFA_CAUS' => $requestData['CAUSA'],
- 'LIFA_SOLU' => $requestData['SOLUCION'],
- 'LIFA_DESC' => $requestData['DESCRIPCION'],
- 'LIFA_USRE' => $user,
- 'LIFA_FERE' => $currentDate,
- 'LIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
- ]);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_REG002: Ocurrió un error al registrar el formulario en la lista de falla.",
- $th->getMessage(),
- 500
- );
- }
- if ( !$idFailure ) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_REG003: No se pudo registrar el formulario en la lista de falla.",
- [],
- 500
- );
- }
- foreach ($requestData['EQUIPAMIENTOS'] as $keyEquipment => $equipment) {
- try {
- $validateRegisterEquipment = DB::table('S002V01TLFEQ')->insert([
- 'LFEQ_NULI' => $requestData['NUMERO_LINEA'],
- 'LFEQ_IDFA' => $idFailure,
- 'LFEQ_COEQ' => $equipment,
- 'LFEQ_USRE' => $user,
- 'LFEQ_FERE' => $currentDate,
- 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
- ]);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_REG004: Ocurrió un error al registrar el formulario en la lista de falla.",
- $th->getMessage(),
- 500
- );
- }
- if ( !$validateRegisterEquipment ) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_REG005: No se pudo registrar el equipamiento $equipment en la lista de fallas.",
- [],
- 500
- );
- }
- }
-
- DB::commit();
- return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
- }
- public function updateFailures(Request $request) {
- $validator = Validator::make($request->all(), [
- 'ID_FALLA' => 'required|integer',
- 'NOMBRE_FALLA' => 'required|string',
- 'NIVEL' => 'required|string',
- 'CAUSA' => 'required|string',
- 'SOLUCION' => 'required|string',
- 'DESCRIPCION' => 'required|string',
- 'EQUIPAMIENTOS' => 'required',
- 'NUMERO_LINEA' => 'required|integer',
- 'USUARIO' => 'required|string',
- ]);
- if ($validator->fails()) {
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_UPD000: Se encontraron uno o más errores.",
- $this->responseController->makeErrors($validator->errors()->messages()),
- 401
- );
- }
- DB::beginTransaction();
- $requestData = $request->all();
- try {
- $user = $this->encController->decrypt($request['USUARIO']);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_FAILURES_UPD001: No se pudo obtener el usuario.", $th->getMessage(), 500);
- }
- $now = $this->functionsController->now();
- $currentDate = $now->toDateTimeString();
- try {
- $idFailure = DB::table('S002V01TLIFA')
- ->where('LIFA_NULI', '=', $requestData['NUMERO_LINEA'])
- ->where('LIFA_IDFA', '=', $requestData['ID_FALLA'])
- ->update([
- 'LIFA_NOFA' => $requestData['NOMBRE_FALLA'],
- 'LIFA_NIVE' => $requestData['NIVEL'],
- 'LIFA_CAUS' => $requestData['CAUSA'],
- 'LIFA_SOLU' => $requestData['SOLUCION'],
- 'LIFA_DESC' => $requestData['DESCRIPCION'],
- 'LIFA_USMO' => $user,
- 'LIFA_FEMO' => $currentDate,
- 'LIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
- ]);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_UPD002: Ocurrió un error al modificar el formulario en la lista de falla.",
- $th->getMessage(),
- 500
- );
- }
- if ( !$idFailure ) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_UPD003: No se pudo modificar el formulario en la lista de falla.",
- [],
- 500
- );
- }
- foreach ($requestData['EQUIPAMIENTOS'] as $keyEquipment => $equipment) {
- try {
- $validateExists = DB::table('S002V01TLFEQ')
- ->where('LFEQ_IDFA', '=', $requestData['ID_FALLA'])
- ->where('LFEQ_NULI', '=', $requestData['NUMERO_LINEA'])
- ->where('LFEQ_COEQ', '=', $equipment)
- ->exists();
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_UPD004: Ocurrió un error al consultar el equipamiento.",
- $th->getMessage(),
- 500
- );
- }
-
- if ( $validateExists ) {
- try {
- $validate = DB::table('S002V01TLFEQ')
- ->where('LFEQ_NULI', '=', $requestData['NUMERO_LINEA'])
- ->where('LFEQ_IDFA', '=', $requestData['ID_FALLA'])
- ->where('LFEQ_COEQ', '=', $equipment)
- ->update([
- 'LFEQ_ESTA' => 'Activo',
- 'LFEQ_USMO' => $user,
- 'LFEQ_FEMO' => $currentDate,
- 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
- ]);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_UPD005: Ocurrió un error al modificar el formulario en la lista de falla el equipamiento $equipment.",
- $th->getMessage(),
- 500
- );
- }
- } else {
- try {
- $validate = DB::table('S002V01TLFEQ')->insert([
- 'LFEQ_NULI' => $requestData['NUMERO_LINEA'],
- 'LFEQ_IDFA' => $requestData['ID_FALLA'],
- 'LFEQ_COEQ' => $equipment,
- 'LFEQ_USRE' => $user,
- 'LFEQ_FERE' => $currentDate,
- 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
- ]);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_UPD006: Ocurrió un error al registrar el formulario en la lista de falla el equipamiento $equipment.",
- $th->getMessage(),
- 500
- );
- }
- }
- if ( !$validate ) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_UPD007: No se pudo modificar el equipamiento $equipment en la lista de fallas.",
- [],
- 500
- );
- }
- }
- try {
- $getListEquipment = DB::table('S002V01TLFEQ')
- ->where('LFEQ_IDFA', '=', $requestData['ID_FALLA'])
- ->where('LFEQ_NULI', '=', $requestData['NUMERO_LINEA'])
- ->get([
- 'LFEQ_IDLF',
- 'LFEQ_IDFA',
- 'LFEQ_COEQ',
- 'LFEQ_ESTA',
- ]);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_UPD008: Ocurrió un error al consultar la lista de equipamientos.",
- $th->getMessage(),
- 500
- );
- }
- $arrListEquipment = json_decode( json_encode($getListEquipment), true );
-
- foreach ($arrListEquipment as $keyListEquipment => $listEquipment) {
- if( !in_array($listEquipment['LFEQ_COEQ'], $requestData['EQUIPAMIENTOS']) ) {
- try {
- $validateDelete = DB::table('S002V01TLFEQ')
- ->where('LFEQ_NULI', '=', $requestData['NUMERO_LINEA'])
- ->where('LFEQ_IDFA', '=', $requestData['ID_FALLA'])
- ->where('LFEQ_COEQ', '=', $listEquipment['LFEQ_COEQ'])
- ->update([
- 'LFEQ_ESTA' => 'Eliminado',
- 'LFEQ_USMO' => $user,
- 'LFEQ_FEMO' => $currentDate,
- 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
- ]);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_UPD009: Ocurrió un error al eliminar de la lista de falla el equipamiento ". $listEquipment['LFEQ_COEQ'].".",
- $th->getMessage(),
- 500
- );
- }
- if ( !$validateDelete ) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_UPD010: No se pudo eliminar de la lista de falla el equipamiento ". $listEquipment['LFEQ_COEQ'].".",
- [],
- 500
- );
- }
- }
- }
-
- DB::commit();
- return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
- }
- public function deleteFailures(Request $request) {
- $validator = Validator::make($request->all(), [
- 'ID_FALLA' => 'required|integer',
- 'NUMERO_LINEA' => 'required|integer',
- 'USUARIO' => 'required|string',
- ]);
- if ($validator->fails()) {
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_DEL000: Se encontraron uno o más errores.",
- $this->responseController->makeErrors($validator->errors()->messages()),
- 401
- );
- }
- DB::beginTransaction();
- $requestData = $request->all();
-
- try {
- $user = $this->encController->decrypt($request['USUARIO']);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL001: No se pudo obtener el usuario.", $th->getMessage(), 500);
- }
-
- $now = $this->functionsController->now();
- $currentDate = $now->toDateTimeString();
- try {
- $exist = DB::table('S002V01TLIFA')
- ->where('LIFA_IDFA', '=', $requestData['ID_FALLA'])
- ->where('LIFA_NULI', '=', $requestData['NUMERO_LINEA'])
- ->exists();
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL002: Ocurrió un error al consultar en la base de datos.", $th->getMessage(), 500);
- }
-
- if (!$exist) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL003: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
- }
- try {
- $validateUpdate = DB::table('S002V01TLIFA')
- ->where('LIFA_IDFA', '=', $requestData['ID_FALLA'])
- ->where('LIFA_NULI', '=', $requestData['NUMERO_LINEA'])
- ->update([
- 'LIFA_ESTA' => 'Eliminado',
- 'LIFA_USMO' => $user,
- 'LIFA_FEMO' => $currentDate,
- 'LIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
- ]);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL004: Ocurrió un error al modificar el formulario en la base de datos.", $th->getMessage(), 500);
- }
- if (!$validateUpdate) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL005: No se pudo modificar el formulario en la base de datos.", [], 500);
- }
- try {
- $validateDelete = DB::table('S002V01TLFEQ')
- ->where('LFEQ_IDFA', '=', $requestData['ID_FALLA'])
- ->where('LFEQ_NULI', '=', $requestData['NUMERO_LINEA'])
- ->update([
- 'LFEQ_ESTA' => 'Eliminado',
- 'LFEQ_USMO' => $user,
- 'LFEQ_FEMO' => $currentDate,
- 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
- ]);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(
- true,
- "ERR_FAILURES_DEL006: Ocurrió un error al eliminar la lista de equipamientos.",
- $th->getMessage(),
- 500
- );
- }
- if ( !$validateDelete ) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL007: No se pudo eliminar la lista de equipamientos.", [], 500);
- }
- DB::commit();
- return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
- }
- }
|