| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\Validator;
- use App\Http\Controllers\ResponseController;
- use App\Http\Controllers\ResourcesController;
- use App\Http\Controllers\EncryptionController;
- class InvoiceControlController 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 compareInvoice(Request $request) {
- $validator = Validator::make($request->all(), [
- 'IDARCHIVOPDF' => 'required|string',
- 'IDARCHIVOXML' => 'required|string',
- 'USER' => 'required|string',
- 'LINE_NUMBER' => 'required|string',
- 'REQUEST_LINE' => 'required|integer',
- ]);
- if ($validator->fails()) {
- return $this->responseController->makeResponse(
- true,
- "ERR_INVOICE_COMPARE000: Se encontraron uno o más errores.",
- $this->responseController->makeErrors($validator->errors()->messages()),
- 401
- );
- }
- $responseData = $request->all();
- $idArchivoXml = $this->encController->decrypt($responseData['IDARCHIVOXML']);
- $idArchivoPdf = $this->encController->decrypt($responseData['IDARCHIVOPDF']);
- try {
- $arrLineRequestData = DB::table('S002V01TLINE')
- ->where('LINE_NULI', '=', $responseData['LINE_NUMBER'])
- ->where('LINE_IDLI', '=', $responseData['REQUEST_LINE'])
- ->where('LINE_ESTA', '=', 'En espera')
- ->where('ARSE_ESTA', '=', 'Activo')
- ->join('S002V01TARSE', 'LINE_IDLI','=', 'ARSE_IDLI')
- ->get([
- 'LINE_IDLI',
- 'LINE_NUPR',
- 'LINE_NUOR',
- 'LINE_OTPR',
- 'LINE_OTCO',
- 'ARSE_IDAS',
- 'ARSE_IDLI',
- 'ARSE_IDAR',
- 'ARSE_NUPR',
- 'ARSE_IDIN',
- 'ARSE_CANT',
- 'ARSE_PRTO',
- ]);
- } catch (\Throwable $th) {
- return $this->responseController->makeResponse(
- true,
- "ERR_INVOICE_COMPARE001: Ocurrió un error al obtener la información de la base de datos del documento.",
- $th->getMessage(),
- 500
- );
- }
- if (empty($arrLineRequestData)) {
- return $this->responseController->makeResponse(true, "ERR_INVOICE_COMPARE002: No se pudo obtener la información de la línea de solicitud de compra.", [], 401);
- }
- try {
- $fileTempData = (array) DB::table('S002V01TARTE')
- ->where('ARTE_NULI', '=', $responseData['LINE_NUMBER'])
- ->where('ARTE_IDAR', '=', $idArchivoXml)
- ->where('ARTE_ESTA', '=', 'Activo')
- ->first([
- 'ARTE_IDAR',
- 'ARTE_NULI',
- 'ARTE_NOAR',
- 'ARTE_EXTE',
- 'ARTE_TAMA',
- 'ARTE_UBTE',
- 'ARTE_ESTA',
- 'ARTE_USRE',
- 'ARTE_FERE',
- 'ARTE_USMO',
- 'ARTE_FEMO',
- 'ARTE_FEAR',
- ]);
- } catch (\Throwable $th) {
- return $this->responseController->makeResponse(
- true,
- "ERR_INVOICE_COMPARE003: Ocurrió un error al obtener la información de la base de datos del documento.",
- $th->getMessage(),
- 500
- );
- }
- if (empty($fileTempData)) {
- return $this->responseController->makeResponse(true, "ERR_INVOICE_COMPARE004: No se pudo obtener la información del documento.", [], 401);
- }
- if ( !file_exists($fileTempData['ARTE_UBTE']) ) {
- return $this->responseController->makeResponse(true, "ERR_INVOICE_COMPARE005: No se pudo obtener el documento.", [], 401);
- }
- try {
- $xml = simplexml_load_file($fileTempData['ARTE_UBTE'], 'SimpleXMLElement', LIBXML_NOCDATA);
- $ns = $xml->getNamespaces(true);
- $xml->registerXPathNamespace('c', $ns['cfdi']);
- $xml->registerXPathNamespace('t', $ns['tfd']);
- } catch (\Throwable $th) {
- return $this->responseController->makeResponse(
- true,
- "ERR_INVOICE_COMPARE006: Ocurrió un error al obtener la información del documento.",
- $th->getMessage(),
- 500
- );
- }
- $responseValidateEmisor = $this->validateEmisorCfdi($xml);
-
- $responseValidateReceptor = $this->validateReceptorCfdi($xml);
- return $responseValidateEmisor;
- return $arrEmisor;
- }
- private function validateEmisorCfdi($xml) {
- $responseValidate = array('error' => false, 'msg' => '', 'response' => []);
- $arrEmisor = $xml->xpath('//cfdi:Comprobante//cfdi:Emisor');
- $arrEmisor = json_decode(json_encode($arrEmisor), true);
- $arrEmisor = $arrEmisor[0]['@attributes'];
- try {
- $objTaxInformation = (array) DB::table('S002V01TINFI')
- ->where('INFI_ESTA','=','Activo')
- ->first([
- 'INFI_NOFI AS NOMBRE_FISCAL',
- 'INFI_XRFC AS RFC',
- 'INFI_CORF AS CODIGO_REGIMEN',
- ]);
- } catch (\Throwable $th) {
- $responseValidate['error'] = true;
- $responseValidate['msg'] = "ERR_INVOICE_VALIDATEEMISOR000: Ocurrió un error al obtener la información físcal.";
- $responseValidate['response'] = $th->getMessage();
- }
- if (empty($objTaxInformation)) {
- $responseValidate['error'] = true;
- $responseValidate['msg'] = "ERR_INVOICE_VALIDATEEMISOR001: No se encontró la información físcal";
- }
-
- if ($objTaxInformation['NOMBRE_FISCAL'] != $arrEmisor['Nombre']) {
- $responseValidate['error'] = true;
- $responseValidate['response'][] = 'Ocurrió un error al válidar el Nombre Fiscal del emisor de la factura.';
- }
- if ($objTaxInformation['RFC'] != $arrEmisor['Rfc']) {
- $responseValidate['error'] = true;
- $responseValidate['response'][] = 'Ocurrió un error al válidar el R.F.C del emisor de la factura.';
- }
- if ($objTaxInformation['CODIGO_REGIMEN'] != $arrEmisor['RegimenFiscal']) {
- $responseValidate['error'] = true;
- $responseValidate['response'][] = 'Ocurrió un error al válidar el Regimen Fiscal del emisor de la factura.';
- }
- return $responseValidate;
- }
- private function validateReceptorCfdi($xml) {
- $responseValidate = array('error' => false, 'msg' => '', 'response' => []);
- $arrReceptor = $xml->xpath('//cfdi:Comprobante//cfdi:Receptor');
- $arrReceptor = json_decode(json_encode($arrReceptor), true);
- $arrReceptor = $arrReceptor[0]['@attributes'];
- }
- }
|