1 | <?php |
||
2 | /* |
||
3 | * Copyright (C) 2022 Joe Nilson <[email protected]> |
||
4 | * |
||
5 | * This program is free software: you can redistribute it and/or modify |
||
6 | * it under the terms of the GNU Lesser General Public License as |
||
7 | * published by the Free Software Foundation, either version 3 of the |
||
8 | * License, or (at your option) any later version. |
||
9 | * |
||
10 | * This program is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | * GNU Lesser General Public License for more details. |
||
14 | * You should have received a copy of the GNU Lesser General Public License |
||
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
16 | */ |
||
17 | |||
18 | namespace FacturaScripts\Plugins\fsRepublicaDominicana\Lib; |
||
19 | |||
20 | use FacturaScripts\Core\Base\DataBase; |
||
21 | use FacturaScripts\Core\Base\DataBase\DataBaseWhere; |
||
22 | use FacturaScripts\Dinamic\Model\Cliente; |
||
23 | |||
24 | class WebserviceDgii |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | public $wsdlDGII = 'https://www.dgii.gov.do/wsMovilDGII/WSMovilDGII.asmx?WSDL'; |
||
30 | |||
31 | public $searchInitiator = 'https://dgii.gov.do/herramientas/consultas/Paginas/RNC.aspx'; |
||
32 | public $searchProcessor = 'https://dgii.gov.do/app/WebApps/ConsultasWeb2/ConsultasWeb/consultas/rnc.aspx'; |
||
33 | |||
34 | private $externalAPI = 'https://apiv1.artesanik.com/contribuyente'; |
||
35 | |||
36 | // private $viewState; |
||
37 | // private $viewStateGenerator; |
||
38 | // private $eventValidation; |
||
39 | |||
40 | public function getExternalAPI($queryParam) |
||
41 | { |
||
42 | //$content = file_get_contents($this->externalAPI."?query='$queryParam'"); |
||
43 | $ch = curl_init(); |
||
44 | $headers = array( |
||
45 | 'Accept: application/json', |
||
46 | 'Content-Type: application/json', |
||
47 | |||
48 | ); |
||
49 | curl_setopt($ch, CURLOPT_USERAGENT, 'curl/FacturaScripts/2022.4'); |
||
50 | curl_setopt($ch, CURLOPT_URL, $this->externalAPI."?query=$queryParam"); |
||
51 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
52 | curl_setopt($ch, CURLOPT_HEADER, 0); |
||
53 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
54 | $response = curl_exec($ch); |
||
55 | $result = \json_decode($response, false, 512, JSON_THROW_ON_ERROR); |
||
56 | curl_close($ch); |
||
57 | return $result; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return void |
||
62 | * @throws \SoapFault |
||
63 | */ |
||
64 | public function wdslConnection(): object |
||
65 | { |
||
66 | return new \SoapClient($this->wsdlDGII, array('encoding' => 'UTF-8')); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param object $wsdlConn |
||
71 | * @param integer $patronBusqueda |
||
72 | * @param string $paramValue |
||
73 | * @param integer $inicioFilas |
||
74 | * @param integer $filaFilas |
||
75 | * @return void |
||
76 | * @throws \JsonException |
||
77 | */ |
||
78 | public function wdslSearch( |
||
79 | int $patronBusqueda = 0, |
||
80 | string $paramValue = '', |
||
81 | int $inicioFilas = 1, |
||
82 | int $filaFilas = 1 |
||
83 | ): string { |
||
84 | $opciones = [ |
||
85 | 'patronBusqueda' => $patronBusqueda, |
||
86 | 'value' => $paramValue, |
||
87 | 'inicioFilas' => $inicioFilas, |
||
88 | 'filaFilas' => $filaFilas, |
||
89 | 'IMEI' => 0 |
||
90 | ]; |
||
91 | |||
92 | $wsdlConn = $this->wdslConnection(); |
||
93 | |||
94 | $result = $wsdlConn->__soapCall('GetContribuyentes', ['GetContribuyentes' => $opciones]); |
||
95 | $list = array(); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
96 | $getResult = explode("@@@", $result->GetContribuyentesResult); |
||
97 | |||
98 | return $getResult[0]; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param object $wsdlConn |
||
103 | * @param string $paramValue |
||
104 | * @return object |
||
105 | */ |
||
106 | public function wdslSearchCount(object $wsdlConn, string $paramValue = ''): object |
||
107 | { |
||
108 | $opciones = [ |
||
109 | 'value' => $paramValue, |
||
110 | 'IMEI' => 0 |
||
111 | ]; |
||
112 | |||
113 | $result = $wsdlConn->__soapCall('GetContribuyentesCount', ['GetContribuyentesCount' => $opciones]); |
||
114 | return $result->GetContribuyentesCountResult; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param object $item |
||
119 | * @return void |
||
120 | */ |
||
121 | public function buscarCliente(&$item): void |
||
122 | { |
||
123 | $item->existe = false; |
||
124 | $item->codcliente = ''; |
||
125 | $cli = new Cliente(); |
||
126 | $where = [ |
||
127 | new DataBaseWhere('cifnif', $item->RGE_RUC) |
||
128 | ]; |
||
129 | $cli->all($where); |
||
130 | if ($cli[0] !== null) { |
||
131 | $cliente = $cli[0]; |
||
132 | $item->existe = true; |
||
133 | $item->codcliente = $cliente->codcliente; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | private function curlSearch($page, $postData = '') |
||
0 ignored issues
–
show
|
|||
138 | { |
||
139 | $result = ""; |
||
0 ignored issues
–
show
|
|||
140 | $h = curl_init(); |
||
141 | curl_setopt($h, CURLOPT_URL, $page); |
||
142 | curl_setopt( |
||
143 | $h, |
||
144 | CURLOPT_USERAGENT, |
||
145 | 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0' |
||
146 | ); |
||
147 | curl_setopt($h, CURLOPT_REFERER, $this->searchInitiator); |
||
148 | if ($postData !== '') { |
||
149 | curl_setopt($h, CURLOPT_POST, true); |
||
150 | curl_setopt($h, CURLOPT_POSTFIELDS, $postData); |
||
151 | } |
||
152 | |||
153 | curl_setopt($h, CURLOPT_HEADER, false); |
||
154 | curl_setopt($h, CURLOPT_RETURNTRANSFER, 1); |
||
155 | $result = curl_exec($h); |
||
156 | curl_close($h); |
||
157 | return $result; |
||
158 | } |
||
159 | |||
160 | //Pedimos que nos den el VIEWSTATE y el EVENTVALIDATION a la página de busqueda |
||
161 | // public function autorizacionWeb(): void |
||
162 | // { |
||
163 | // $result = $this->curlSearch($this->searchInitiator); |
||
164 | // $html = new HtmlDocument(); |
||
165 | // $html->load($result); |
||
166 | // |
||
167 | // $this->viewState = $html->getElementById('#__VIEWSTATE', 0)->value; |
||
168 | // |
||
169 | // $this->viewStateGenerator = $html->getElementById('#__VIEWSTATEGENERATOR', 0)->value; |
||
170 | // |
||
171 | // $this->eventValidation = $html->getElementById('#__EVENTVALIDATION', 0)->value; |
||
172 | // } |
||
173 | |||
174 | //Si la busqueda no es por RNC y en su lugar es por nombre actualizamos viewstate y eventvalidation |
||
175 | // public function actualizarAutorizacion($tipoBusqueda) |
||
176 | // { |
||
177 | // $post = array( |
||
178 | // '__EVENTTARGET' => 'rbtnlTipoBusqueda$1', |
||
179 | // '__EVENTARGUMENT' => "", |
||
180 | // '__LASTFOCUS' => "", |
||
181 | // '__VIEWSTATE' => $this->viewState, |
||
182 | // '__VIEWSTATEGENERATOR' => $this->viewStateGenerator, |
||
183 | // '__EVENTVALIDATION' => $this->eventValidation, |
||
184 | // 'rbtnlTipoBusqueda' => $tipoBusqueda, |
||
185 | // 'txtRncCed' => '' |
||
186 | // ); |
||
187 | // |
||
188 | // $query = http_build_query($post); |
||
189 | // $result = $this->curlSearch($this->searchProcessor, $query); |
||
190 | // |
||
191 | // $doc = new HtmlDocument(); |
||
192 | // $html = $doc->load($result); |
||
193 | // $this->viewState = $html->getElementById('#__VIEWSTATE', 0)->value; |
||
194 | // $this->eventValidation = $html->getElementById('#__EVENTVALIDATION', 0)->value; |
||
195 | // } |
||
196 | |||
197 | // public function buscar($rnc = '', $nombre = '') |
||
198 | // { |
||
199 | // $resultados = ''; |
||
200 | // $this->autorizacionWeb(); |
||
201 | // $tipoBusqueda = (!empty($rnc)) ? 0 : 1; |
||
202 | // $valorBuscar = (!empty($rnc)) ? $rnc : strtoupper(trim($nombre)); |
||
203 | // $this->rnc = $rnc; |
||
204 | // $this->nombre = $nombre; |
||
205 | // $campo = (!empty($rnc)) ? 'ctl00$cphMain$txtRNCCedula' : 'ctl00$cphMain$txtRazonSocial'; |
||
206 | // $smMain = (!empty($rnc)) ? 'ctl00$cphMain$upBusqueda|ctl00$cphMain$btnBuscarPorRNC': ''; |
||
207 | // $boton = (!empty($rnc)) ? 'ctl00$cphMain$btnBuscarPorRNC' : 'ctl00$cphMain$btnBuscarPorRazonSocial'; |
||
208 | // |
||
209 | // if ($tipoBusqueda === 1) { |
||
210 | // $this->actualizarAutorizacion($tipoBusqueda); |
||
211 | // } |
||
212 | // |
||
213 | // $post = array( |
||
214 | // '__EVENTTARGET' => "", |
||
215 | // '__EVENTARGUMENT' => "", |
||
216 | // '__LASTFOCUS' => "", |
||
217 | // '__VIEWSTATE' => $this->viewState, |
||
218 | // '__VIEWSTATEGENERATOR' => $this->viewStateGenerator, |
||
219 | // '__EVENTVALIDATION' => $this->eventValidation, |
||
220 | // 'rbtnlTipoBusqueda' => $tipoBusqueda, |
||
221 | // 'ctl00$smMain' => $smMain, |
||
222 | // '__ASYNCPOST' => 'true', |
||
223 | // $campo => $valorBuscar, |
||
224 | // $boton => 'BUSCAR' |
||
225 | // ); |
||
226 | // |
||
227 | // $query = http_build_query($post); |
||
228 | // $result = $this->curlSearch($this->searchProcessor, $query); |
||
229 | // |
||
230 | // $doc = new HtmlDocument(); |
||
231 | // $html = $doc->load($result); |
||
232 | // |
||
233 | // $vacio = trim($html->getElementById('#cphMain_lblInformacion', 0)->value); |
||
234 | // |
||
235 | // if ($vacio !== '') { |
||
236 | // $resultados = $html->getElementById('#cphMain_lblInformacion', 0)->value; |
||
237 | // return $resultados; |
||
238 | // } else { |
||
239 | // $cabeceras = array(); |
||
240 | // $detalles = array(); |
||
241 | // $table = $html->getElementById('#cphMain_dvDatosContribuyentes'); |
||
242 | // $tbody = $table->find('tbody'); |
||
243 | // foreach ($html->getElementById('#cphMain_dvDatosContribuyentes') as $lista) { |
||
244 | // //$cabeceras = $this->loopLista($lista); |
||
245 | // } |
||
246 | // $this->cabecera = $cabeceras; |
||
247 | // $lista_interna = 0; |
||
248 | // foreach ($html->find('.GridItemStyle') as $lista) { |
||
249 | // $detalles[$lista_interna] = $this->loopLista($lista); |
||
250 | // $lista_interna++; |
||
251 | // } |
||
252 | // foreach ($html->find('.bg_celdas_alt') as $lista) { |
||
253 | // $detalles[$lista_interna] = $this->loopLista($lista); |
||
254 | // $lista_interna++; |
||
255 | // } |
||
256 | // $this->detalle = $detalles; |
||
257 | //// $this->total_cabecera = count($cabeceras); |
||
258 | //// $this->total_resultados = count($this->detalle); |
||
259 | // return $cabeceras; |
||
260 | // } |
||
261 | // } |
||
262 | |||
263 | // private function loopLista($lista) |
||
264 | // { |
||
265 | // $array = array(); |
||
266 | // foreach ($lista->find('td') as $item) { |
||
267 | // $array[] = $item->plaintext; |
||
268 | // } |
||
269 | // return $array; |
||
270 | // } |
||
271 | } |