|
|
@@ -952,35 +952,154 @@ class RequestLineController extends Controller
|
|
|
return $this->responseController->makeResponse(false, "ÉXITO", $arrArtitleSelected);
|
|
|
}
|
|
|
|
|
|
- public function checkItemsWorkOrder ($resources, $line) {
|
|
|
+ public function checkItemsWorkOrder ($resources, $idOrder, $user, $line) {
|
|
|
foreach ($resources as $resource) {
|
|
|
|
|
|
if ($resource['REQ'] !== '-') {
|
|
|
- $cant = intval($resource['REQ']);
|
|
|
- $id = $resource['ID'];
|
|
|
+ goto next;
|
|
|
+ }
|
|
|
+
|
|
|
+ $id = $this->encController->decrypt($resource['REQ']);
|
|
|
+ if (is_null($id)) {
|
|
|
+ return $this->responseController->makeResponse(true, 'El ID de la herramienta o refacción no está encriptado correctamente.', [], 500);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $amountStock = DB::table('S002V01TSTAR')
|
|
|
+ ->where([
|
|
|
+ ['STAR_IDIS', '=', $id],
|
|
|
+ ['STAR_NULI', '=', $line],
|
|
|
+ ['STAR_ESTA', '=', 'Activo'],
|
|
|
+ ])
|
|
|
+ ->count();
|
|
|
+ } catch (\Throwable $th) {
|
|
|
+ return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
|
|
|
+ }
|
|
|
|
|
|
+ $amountRequest = intval($resource['REQ']);
|
|
|
+ if ($amountRequest > $amountStock) {
|
|
|
+ goto next;
|
|
|
+ }
|
|
|
+
|
|
|
+ $adquisitionAmount = $amountRequest - $amountStock;
|
|
|
+
|
|
|
+ try {
|
|
|
+ $adquisition = (array) DB::table('S002V01TINST')
|
|
|
+ ->where([
|
|
|
+ ['INST_IDIS', '=', $id],
|
|
|
+ ['INST_NULI', '=', $line],
|
|
|
+ ['INST_ESTA', '=', 'Activo'],
|
|
|
+ ])
|
|
|
+ ->join('S002V01TINAR', 'INAR_CODI', '=', 'INST_COMO')
|
|
|
+ ->join('S002V01TARTI', 'ARTI_IDAR', '=', 'INST_ARTI')
|
|
|
+ ->join('S002V01TDEAR', 'DEAR_IDDE', '=', 'INAR_IDDE')
|
|
|
+ ->first([
|
|
|
+ 'ARTI_IDAR',
|
|
|
+ 'DEAR_NUPR',
|
|
|
+ 'INAR_IDIN',
|
|
|
+ ]);
|
|
|
+ } catch (\Throwable $th) {
|
|
|
+ return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de la adquisición.', $th->getMessage(), 500);
|
|
|
+ }
|
|
|
+ if (is_null($adquisition) || empty($adquisition)) {
|
|
|
+ return $this->responseController->makeResponse(true, 'No se pudo obtener la información de la adquisición.', [], 500);
|
|
|
+ }
|
|
|
+
|
|
|
+ $now = $this->functionsController->now();
|
|
|
+ $currentDate = $now->toDateTimeString();
|
|
|
+
|
|
|
+ try {
|
|
|
+ $idLine = DB::table('S002V01TLINE')->insertGetId([
|
|
|
+ 'LINE_NUPR' => $adquisition['DEAR_NUPR'],
|
|
|
+ 'LINE_OTPR' => null,
|
|
|
+ 'LINE_OTCO' => $idOrder,
|
|
|
+ 'LINE_NULI' => $line,
|
|
|
+ 'LINE_ESTA' => 'En OC',
|
|
|
+ 'LINE_USRE' => $user,
|
|
|
+ 'LINE_FERE' => $currentDate,
|
|
|
+ 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
|
|
|
+ ]);
|
|
|
+ } catch (\Throwable $th) {
|
|
|
+ return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar la línea de solicitud.', $th->getMessage(), 500);
|
|
|
+ }
|
|
|
+ if (is_null($idLine) || $idLine < 1) {
|
|
|
+ return $this->responseController->makeResponse(true, 'No se pudo registrar la línea de solicitud.', [], 500);
|
|
|
+ }
|
|
|
+
|
|
|
+ for ($i=1; $i <= $adquisitionAmount; $i++) {
|
|
|
try {
|
|
|
- $items = DB::table('S002V01TSTAR')
|
|
|
- ->where([
|
|
|
- ['STAR_NULI', '=', $line],
|
|
|
- ['STAR_IDIS', '=', $id],
|
|
|
- ])
|
|
|
- ->join('S002V01TUNID', 'UNID_IDUN', '=', 'STAR_IDUN')
|
|
|
- ->get([
|
|
|
- 'UNID_NOMB AS UNIT',
|
|
|
- 'STAR_CONS AS TYPE',
|
|
|
- ]);
|
|
|
- $items = json_decode(json_encode($items), true);
|
|
|
+ $validateInsert = DB::table('S002V01TARSE')->insert([
|
|
|
+ 'ARSE_IDAS' => $i,
|
|
|
+ 'ARSE_NULI' => $line,
|
|
|
+ 'ARSE_IDLI' => $idLine,
|
|
|
+ 'ARSE_IDAR' => $adquisition['ARTI_IDAR'],
|
|
|
+ 'ARSE_NUPR' => $adquisition['DEAR_NUPR'],
|
|
|
+ 'ARSE_IDIN' => $adquisition['INAR_IDIN'],
|
|
|
+ 'ARSE_USRE' => $user,
|
|
|
+ 'ARSE_FERE' => $currentDate,
|
|
|
+ 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
|
|
|
+ ]);
|
|
|
} catch (\Throwable $th) {
|
|
|
- return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los datos de las herramientas y refacciones.', $th->getMessage(), 500);
|
|
|
+ return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar los artículos en la línea de solicitud.', $th->getMessage(), 500);
|
|
|
}
|
|
|
|
|
|
- if ($cant > count($items)) {
|
|
|
-
|
|
|
- }
|
|
|
+ if (is_null($validateInsert) || !$validateInsert) {
|
|
|
+ return $this->responseController->makeResponse(true, 'No se pudo registrar los artículos en la línea de solicitud.', [], 500);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $typeOrder = rand(0, 1) === 0 ? 'ASC' : 'DESC';
|
|
|
+
|
|
|
+ try {
|
|
|
+ $dispatch = (array) DB::table('S002V01TDESP')
|
|
|
+ ->where([
|
|
|
+ ['DESP_NULI', '=', $line],
|
|
|
+ ['DESP_ESTA', '=', 'Activo'],
|
|
|
+ ])
|
|
|
+ ->orderBy('DESP_IDDE', $typeOrder)
|
|
|
+ ->first([
|
|
|
+ 'DESP_IDDE'
|
|
|
+ ]);
|
|
|
+ } catch (\Throwable $th) {
|
|
|
+ return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del despacho.', [], 500);
|
|
|
+ }
|
|
|
+ if (is_null($dispatch) || empty($dispatch)) {
|
|
|
+ return $this->responseController->makeResponse(true, 'No se pudo obtener la información del despacho.', [], 500);
|
|
|
+ }
|
|
|
|
|
|
+ try {
|
|
|
+ $idOrder = DB::table('S002V01TORCO')->insertGetId([
|
|
|
+ 'ORCO_NULI' => $line,
|
|
|
+ 'ORCO_IDLI' => $idLine,
|
|
|
+ 'ORCO_IDDE' => $dispatch['DESP_IDDE'],
|
|
|
+ 'ORCO_USRE' => $user,
|
|
|
+ 'ORCO_FERE' => $currentDate,
|
|
|
+ 'ORCO_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
|
|
|
+ ]);
|
|
|
+ } catch (\Throwable $th) {
|
|
|
+ return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar la orden de compra.', $th->getMessage(), 500);
|
|
|
+ }
|
|
|
+ if (is_null($idOrder) || $idOrder < 1) {
|
|
|
+ return $this->responseController->makeResponse(true, 'No se pudo registrar la línea de solicitud.', [], 500);
|
|
|
}
|
|
|
|
|
|
+ try {
|
|
|
+ $validateInsert = DB::table('S002V01THIOR')->insert([
|
|
|
+ 'HIOR_NULI' => $idLine,
|
|
|
+ 'HIOR_NUOR' => $idOrder,
|
|
|
+ 'HIOR_POEN' => 10,
|
|
|
+ 'HIOR_USRE' => $user,
|
|
|
+ 'HIOR_FERE' => $currentDate,
|
|
|
+ 'HIOR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
|
|
|
+ ]);
|
|
|
+ } catch (\Throwable $th) {
|
|
|
+ return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar el historial de la orden de compra.', $th->getMessage(), 500);
|
|
|
+ }
|
|
|
+ if (is_null($validateInsert) || !$validateInsert) {
|
|
|
+ return $this->responseController->makeResponse(true, 'No se pudo registrar el historial de la orden de compra.', [], 500);
|
|
|
+ }
|
|
|
+
|
|
|
+ next:
|
|
|
}
|
|
|
}
|
|
|
}
|