InvoiceControlController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Carbon;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Http\Controllers\Controller;
  7. use Illuminate\Support\Facades\Validator;
  8. use App\Http\Controllers\ResponseController;
  9. use App\Http\Controllers\ResourcesController;
  10. use App\Http\Controllers\EncryptionController;
  11. class InvoiceControlController extends Controller {
  12. private $responseController;
  13. private $encController;
  14. private $resourcesController;
  15. public function __construct() {
  16. $this->responseController = new ResponseController();
  17. $this->encController = new EncryptionController();
  18. $this->resourcesController = new ResourcesController();
  19. }
  20. public function compareInvoice(Request $request) {
  21. $validator = Validator::make($request->all(), [
  22. 'IDARCHIVOPDF' => 'required|string',
  23. 'IDARCHIVOXML' => 'required|string',
  24. 'USER' => 'required|string',
  25. 'LINE_NUMBER' => 'required|string',
  26. 'REQUEST_LINE' => 'required|integer',
  27. ]);
  28. if ($validator->fails()) {
  29. return $this->responseController->makeResponse(
  30. true,
  31. "ERR_INVOICE_COMPARE000: Se encontraron uno o más errores.",
  32. $this->responseController->makeErrors($validator->errors()->messages()),
  33. 401
  34. );
  35. }
  36. $responseData = $request->all();
  37. $idArchivoXml = $this->encController->decrypt($responseData['IDARCHIVOXML']);
  38. $idArchivoPdf = $this->encController->decrypt($responseData['IDARCHIVOPDF']);
  39. try {
  40. $arrLineRequestData = DB::table('S002V01TLINE')
  41. ->where('LINE_NULI', '=', $responseData['LINE_NUMBER'])
  42. ->where('LINE_IDLI', '=', $responseData['REQUEST_LINE'])
  43. ->where('LINE_ESTA', '=', 'En espera')
  44. ->where('ARSE_ESTA', '=', 'Activo')
  45. ->join('S002V01TARSE', 'LINE_IDLI','=', 'ARSE_IDLI')
  46. ->get([
  47. 'LINE_IDLI',
  48. 'LINE_NUPR',
  49. 'LINE_NUOR',
  50. 'LINE_OTPR',
  51. 'LINE_OTCO',
  52. 'ARSE_IDAS',
  53. 'ARSE_IDLI',
  54. 'ARSE_IDAR',
  55. 'ARSE_NUPR',
  56. 'ARSE_IDIN',
  57. 'ARSE_CANT',
  58. 'ARSE_PRTO',
  59. ]);
  60. } catch (\Throwable $th) {
  61. return $this->responseController->makeResponse(
  62. true,
  63. "ERR_INVOICE_COMPARE001: Ocurrió un error al obtener la información de la base de datos del documento.",
  64. $th->getMessage(),
  65. 500
  66. );
  67. }
  68. if (empty($arrLineRequestData)) {
  69. return $this->responseController->makeResponse(true, "ERR_INVOICE_COMPARE002: No se pudo obtener la información de la línea de solicitud de compra.", [], 401);
  70. }
  71. try {
  72. $fileTempData = (array) DB::table('S002V01TARTE')
  73. ->where('ARTE_NULI', '=', $responseData['LINE_NUMBER'])
  74. ->where('ARTE_IDAR', '=', $idArchivoXml)
  75. ->where('ARTE_ESTA', '=', 'Activo')
  76. ->first([
  77. 'ARTE_IDAR',
  78. 'ARTE_NULI',
  79. 'ARTE_NOAR',
  80. 'ARTE_EXTE',
  81. 'ARTE_TAMA',
  82. 'ARTE_UBTE',
  83. 'ARTE_ESTA',
  84. 'ARTE_USRE',
  85. 'ARTE_FERE',
  86. 'ARTE_USMO',
  87. 'ARTE_FEMO',
  88. 'ARTE_FEAR',
  89. ]);
  90. } catch (\Throwable $th) {
  91. return $this->responseController->makeResponse(
  92. true,
  93. "ERR_INVOICE_COMPARE003: Ocurrió un error al obtener la información de la base de datos del documento.",
  94. $th->getMessage(),
  95. 500
  96. );
  97. }
  98. if (empty($fileTempData)) {
  99. return $this->responseController->makeResponse(true, "ERR_INVOICE_COMPARE004: No se pudo obtener la información del documento.", [], 401);
  100. }
  101. if ( !file_exists($fileTempData['ARTE_UBTE']) ) {
  102. return $this->responseController->makeResponse(true, "ERR_INVOICE_COMPARE005: No se pudo obtener el documento.", [], 401);
  103. }
  104. try {
  105. $xml = simplexml_load_file($fileTempData['ARTE_UBTE'], 'SimpleXMLElement', LIBXML_NOCDATA);
  106. $ns = $xml->getNamespaces(true);
  107. $xml->registerXPathNamespace('c', $ns['cfdi']);
  108. $xml->registerXPathNamespace('t', $ns['tfd']);
  109. } catch (\Throwable $th) {
  110. return $this->responseController->makeResponse(
  111. true,
  112. "ERR_INVOICE_COMPARE006: Ocurrió un error al obtener la información del documento.",
  113. $th->getMessage(),
  114. 500
  115. );
  116. }
  117. $responseValidateEmisor = $this->validateEmisorCfdi($xml);
  118. $responseValidateReceptor = $this->validateReceptorCfdi($xml);
  119. return $responseValidateEmisor;
  120. return $arrEmisor;
  121. }
  122. private function validateEmisorCfdi($xml) {
  123. $responseValidate = array('error' => false, 'msg' => '', 'response' => []);
  124. $arrEmisor = $xml->xpath('//cfdi:Comprobante//cfdi:Emisor');
  125. $arrEmisor = json_decode(json_encode($arrEmisor), true);
  126. $arrEmisor = $arrEmisor[0]['@attributes'];
  127. try {
  128. $objTaxInformation = (array) DB::table('S002V01TINFI')
  129. ->where('INFI_ESTA','=','Activo')
  130. ->first([
  131. 'INFI_NOFI AS NOMBRE_FISCAL',
  132. 'INFI_XRFC AS RFC',
  133. 'INFI_CORF AS CODIGO_REGIMEN',
  134. ]);
  135. } catch (\Throwable $th) {
  136. $responseValidate['error'] = true;
  137. $responseValidate['msg'] = "ERR_INVOICE_VALIDATEEMISOR000: Ocurrió un error al obtener la información físcal.";
  138. $responseValidate['response'] = $th->getMessage();
  139. }
  140. if (empty($objTaxInformation)) {
  141. $responseValidate['error'] = true;
  142. $responseValidate['msg'] = "ERR_INVOICE_VALIDATEEMISOR001: No se encontró la información físcal";
  143. }
  144. if ($objTaxInformation['NOMBRE_FISCAL'] != $arrEmisor['Nombre']) {
  145. $responseValidate['error'] = true;
  146. $responseValidate['response'][] = 'Ocurrió un error al válidar el Nombre Fiscal del emisor de la factura.';
  147. }
  148. if ($objTaxInformation['RFC'] != $arrEmisor['Rfc']) {
  149. $responseValidate['error'] = true;
  150. $responseValidate['response'][] = 'Ocurrió un error al válidar el R.F.C del emisor de la factura.';
  151. }
  152. if ($objTaxInformation['CODIGO_REGIMEN'] != $arrEmisor['RegimenFiscal']) {
  153. $responseValidate['error'] = true;
  154. $responseValidate['response'][] = 'Ocurrió un error al válidar el Regimen Fiscal del emisor de la factura.';
  155. }
  156. return $responseValidate;
  157. }
  158. private function validateReceptorCfdi($xml) {
  159. $responseValidate = array('error' => false, 'msg' => '', 'response' => []);
  160. $arrReceptor = $xml->xpath('//cfdi:Comprobante//cfdi:Receptor');
  161. $arrReceptor = json_decode(json_encode($arrReceptor), true);
  162. $arrReceptor = $arrReceptor[0]['@attributes'];
  163. }
  164. }