<?php

namespace App\Http\Controllers;

use App\Http\Controllers\ResponseController;
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 OrderReceptionController extends Controller
{
    private $responseController;
    private $encController;
    private $resourcesController;

    public function __construct(){
        $this->responseController = new ResponseController();
        $this->encController = new EncryptionController();
        $this->resourcesController = new ResourcesController();
    }

    public function getOrderReception($line) {
        DB::beginTransaction();
        try {
            $getOrderReception = DB::table('S002V01TORCO')
                ->where('ORCO_NULI', '=', $line)
                ->where('ORCO_ESTA', '=', 'Recibido')
                ->orWhere('ORCO_ESTA', '=', 'Facturado')
                ->orWhere('ORCO_ESTA', '=', 'Devuelto')
                ->join('S002V01TDESP', 'DESP_IDDE', '=', 'ORCO_NUDE')
                ->join('S002V01TPROV', 'PROV_NUPR', '=', 'ORCO_NUPR')
                ->join('S002V01TREFI', 'REFI_CRFI', '=', 'PROV_CRFI')
                ->get([
                    'ORCO_NUOR',
                    'ORCO_NUPR',
                    'ORCO_NUDE',
                    'ORCO_MONE',
                    'ORCO_COST',
                    'ORCO_PROY',
                    'ORCO_ESTA',
                    'ORCO_NULI',
                    'ORCO_USRE',
                    'ORCO_FERE',
                    'ORCO_USMO',
                    'ORCO_FEMO',
                    'DESP_IDDE',
                    'DESP_NOMB',
                    'DESP_CALL',
                    'DESP_NUEX',
                    'DESP_NUIN',
                    'DESP_COPO',
                    'DESP_COLO',
                    'DESP_MUNI',
                    'DESP_ENTI',
                    'PROV_NUPR',
                    'PROV_NOCO',
                    'PROV_NOMB',
                    'PROV_APPA',
                    'PROV_APMA',
                    'PROV_CORR',
                    'PROV_LAD1',
                    'PROV_TEL1',
                    'PROV_LAD2',
                    'PROV_TEL2',
                    'PROV_XRFC',
                    'PROV_XTAX',
                    'REFI_CRFI',
                    'REFI_DRFI',
                    'PROV_TIPO',
                    'PROV_SIWE',
                    'PROV_MEPA',
                ]);

        } catch (\Throwable $th) {
            DB::rollBack();
            return $this->responseController->makeResponse(true, "ERR_ORDER_RECEPTION_GET000: Ocurrió un error al obtener los datos.", $th->getMessage(), 500);
        }
        DB::commit();
        return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $getOrderReception);
    }
}
