InvoiceControlController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. 'IDARCHIVO' => 'required|string',
  23. 'USER' => 'required|string',
  24. 'LINE_NUMBER' => 'required|string',
  25. 'REQUEST_LINE' => 'required|integer',
  26. ]);
  27. if ($validator->fails()) {
  28. return $this->responseController->makeResponse(
  29. true,
  30. "ERR_INVOICE_COMPARE000: Se encontraron uno o más errores.",
  31. $this->responseController->makeErrors($validator->errors()->messages()),
  32. 401
  33. );
  34. }
  35. $responseData = $request->all();
  36. $idArchivo = $this->encController->decrypt($responseData['IDARCHIVO']);
  37. try {
  38. $arrLineRequestData = DB::table('S002V01TLINE')
  39. ->where('LINE_NULI', '=', $responseData['LINE_NUMBER'])
  40. ->where('LINE_IDLI', '=', $responseData['REQUEST_LINE'])
  41. ->where('LINE_ESTA', '=', 'En espera')
  42. ->where('ARSE_ESTA', '=', 'Activo')
  43. ->join('S002V01TARSE', 'LINE_IDLI','=', 'ARSE_IDLI')
  44. ->get([
  45. 'LINE_IDLI',
  46. 'LINE_NUPR',
  47. 'LINE_NUOR',
  48. 'LINE_OTPR',
  49. 'LINE_OTCO',
  50. 'ARSE_IDAS',
  51. 'ARSE_IDLI',
  52. 'ARSE_IDAR',
  53. 'ARSE_NUPR',
  54. 'ARSE_IDIN',
  55. 'ARSE_CANT',
  56. 'ARSE_PRTO',
  57. ]);
  58. } catch (\Throwable $th) {
  59. return $this->responseController->makeResponse(
  60. true,
  61. "ERR_INVOICE_COMPARE001: Ocurrió un error al obtener la información de la base de datos del documento.",
  62. $th->getMessage(),
  63. 500
  64. );
  65. }
  66. if (empty($arrLineRequestData)) {
  67. return $this->responseController->makeResponse(true, "ERR_INVOICE_COMPARE002: No se pudo obtener la información de la línea de solicitud de compra.", [], 401);
  68. }
  69. try {
  70. $fileTempData = (array) DB::table('S002V01TARTE')
  71. ->where('ARTE_NULI', '=', $responseData['LINE_NUMBER'])
  72. ->where('ARTE_IDAR', '=', $idArchivo)
  73. ->where('ARTE_ESTA', '=', 'Activo')
  74. ->first([
  75. 'ARTE_IDAR',
  76. 'ARTE_NULI',
  77. 'ARTE_NOAR',
  78. 'ARTE_EXTE',
  79. 'ARTE_TAMA',
  80. 'ARTE_UBTE',
  81. 'ARTE_ESTA',
  82. 'ARTE_USRE',
  83. 'ARTE_FERE',
  84. 'ARTE_USMO',
  85. 'ARTE_FEMO',
  86. 'ARTE_FEAR',
  87. ]);
  88. } catch (\Throwable $th) {
  89. return $this->responseController->makeResponse(
  90. true,
  91. "ERR_INVOICE_COMPARE003: Ocurrió un error al obtener la información de la base de datos del documento.",
  92. $th->getMessage(),
  93. 500
  94. );
  95. }
  96. if (empty($fileTempData)) {
  97. return $this->responseController->makeResponse(true, "ERR_INVOICE_COMPARE004: No se pudo obtener la información del documento.", [], 401);
  98. }
  99. if ( !file_exists($fileTempData['ARTE_UBTE']) ) {
  100. return $this->responseController->makeResponse(true, "ERR_INVOICE_COMPARE005: No se pudo obtener el documento.", [], 401);
  101. }
  102. try {
  103. $xml = simplexml_load_file($fileTempData['ARTE_UBTE'], 'SimpleXMLElement', LIBXML_NOCDATA);
  104. $ns = $xml->getNamespaces(true);
  105. $xml->registerXPathNamespace('c', $ns['cfdi']);
  106. $xml->registerXPathNamespace('t', $ns['tfd']);
  107. } catch (\Throwable $th) {
  108. return $this->responseController->makeResponse(
  109. true,
  110. "ERR_INVOICE_COMPARE006: Ocurrió un error al obtener la información del documento.",
  111. $th->getMessage(),
  112. 500
  113. );
  114. }
  115. // echo "<pre>";
  116. $arrConceptos = $xml->xpath('//cfdi:Comprobante//cfdi:Conceptos//cfdi:Concepto');
  117. $arrConceptos = json_decode(json_encode($arrConceptos), true);
  118. foreach ($arrConceptos as $keyConceptos => $conceptos) {
  119. $conceptos = $conceptos['@attributes'];
  120. // print_r($conceptos);
  121. // return $conceptos;
  122. }
  123. return $arrLineRequestData;
  124. }
  125. }