Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.77% |
1 / 130 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| FreshDeskTicketSync | |
0.77% |
1 / 130 |
|
33.33% |
1 / 3 |
1231.95 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
0.00% |
0 / 118 |
|
0.00% |
0 / 1 |
756 | |||
| htmlToTextWithLineBreaks | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Models\TblLeave; |
| 6 | use Carbon\Carbon; |
| 7 | use Illuminate\Console\Command; |
| 8 | use Illuminate\Support\Facades\Log; |
| 9 | use Illuminate\Support\Facades\Http; |
| 10 | use Illuminate\Http\Request; |
| 11 | use GuzzleHttp\Client; |
| 12 | |
| 13 | class FreshDeskTicketSync extends Command |
| 14 | { |
| 15 | /** |
| 16 | * The name and signature of the console command. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $signature = 'sync:freshdesk'; |
| 21 | |
| 22 | /** |
| 23 | * The console command description. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $description = 'Command description'; |
| 28 | |
| 29 | /** |
| 30 | * Create a new command instance. |
| 31 | * |
| 32 | * @return void |
| 33 | */ |
| 34 | public function __construct() |
| 35 | { |
| 36 | parent::__construct(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Execute the console command. |
| 41 | * |
| 42 | * @return int |
| 43 | */ |
| 44 | public function handle() |
| 45 | { |
| 46 | |
| 47 | $regions = [ |
| 48 | "CAT" => "firebusinesssl-help.freshdesk.com", |
| 49 | "MAD" => "firebusinessmadrid.freshdesk.com", |
| 50 | "ALM" => "firebusinessalmeria.freshdesk.com" |
| 51 | ]; |
| 52 | |
| 53 | foreach ($regions as $reg => $url) { |
| 54 | |
| 55 | $companyId = 0; |
| 56 | |
| 57 | switch ($reg) { |
| 58 | case 'CAT': |
| 59 | $companyId = 19; |
| 60 | break; |
| 61 | case 'MAD': |
| 62 | $companyId = 18; |
| 63 | break; |
| 64 | case 'ALM': |
| 65 | $companyId = 21; |
| 66 | break; |
| 67 | default: |
| 68 | $region = 19; |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | $apiKey = env('FRESHDESK_TOKEN_' . $reg) . ":X"; |
| 73 | |
| 74 | for ($i = 1; $i <= 10; $i++) { |
| 75 | |
| 76 | $domain = 'https://' . $url . '/api/v2/tickets?page=' . $i. '&include=requester,description'; |
| 77 | |
| 78 | $response = Http::withBasicAuth($apiKey, 'X')->get($domain); |
| 79 | |
| 80 | $data = json_decode($response); |
| 81 | |
| 82 | if($data){ |
| 83 | foreach ($data as $item) { |
| 84 | |
| 85 | if(in_array("RIESGO DE BAJA", $item->tags) || in_array("BAJA", $item->tags)){ |
| 86 | |
| 87 | $cleanHtml = null; |
| 88 | |
| 89 | if(isset($item->description)){ |
| 90 | $cleanHtml = str_replace(["\n", "\r"], '', $item->description); |
| 91 | $cleanHtml = preg_replace('/\s+/', ' ', $cleanHtml); |
| 92 | |
| 93 | $doc = new \DOMDocument(); |
| 94 | libxml_use_internal_errors(true); |
| 95 | |
| 96 | $doc->loadHTML('<?xml encoding="UTF-8">' . $cleanHtml, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); |
| 97 | libxml_clear_errors(); |
| 98 | |
| 99 | $fields = ['Client ID','Service Code','Nombre','CIF','Teléfono','Email','Dirección', 'Nombre (2nd try)', 'Teléfono llamada', 'Fecha y hora', 'Teléfono1', 'Resumen']; |
| 100 | $result = array_fill_keys($fields, null); |
| 101 | |
| 102 | $tables = $doc->getElementsByTagName('table'); |
| 103 | |
| 104 | foreach ($tables as $table) { |
| 105 | $rows = $table->getElementsByTagName('tr'); |
| 106 | |
| 107 | foreach ($rows as $row) { |
| 108 | $tds = $row->getElementsByTagName('td'); |
| 109 | if ($tds->length >= 2) { |
| 110 | $key = trim($tds->item(0)->textContent); |
| 111 | $value = trim($tds->item(1)->textContent); |
| 112 | |
| 113 | $key = mb_convert_encoding($key, 'UTF-8', 'UTF-8'); |
| 114 | $value = mb_convert_encoding($value, 'UTF-8', 'UTF-8'); |
| 115 | |
| 116 | if (in_array($key, $fields)) { |
| 117 | if ($key === "Resumen") { |
| 118 | $resumenText = trim($this->htmlToTextWithLineBreaks($tds->item(1))); |
| 119 | $result[$key] = $resumenText; |
| 120 | } else { |
| 121 | $result[$key] = $value; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | $spans = $doc->getElementsByTagName('span'); |
| 130 | foreach ($spans as $span) { |
| 131 | $text = trim($span->nodeValue); |
| 132 | $parts = explode(':', $text, 2); |
| 133 | if (count($parts) === 2) { |
| 134 | $result[trim($parts[0]) . 1] = trim($parts[1]); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | $container = $doc->getElementsByTagName('div')->item(1); |
| 139 | |
| 140 | $lines = []; |
| 141 | if(!empty($container)){ |
| 142 | foreach (@$container->childNodes as $node) { |
| 143 | if ($node->nodeName === 'br') { |
| 144 | $lines[] = "\n"; |
| 145 | } elseif ($node->nodeType === XML_ELEMENT_NODE && $node->nodeName === 'span') { |
| 146 | $lines[] = $node->textContent; |
| 147 | } elseif ($node->nodeType === XML_TEXT_NODE) { |
| 148 | $lines[] = $node->textContent; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | $text = implode('', $lines); |
| 154 | $text_lines = array_filter(array_map('trim', preg_split('/\n+/', $text))); |
| 155 | |
| 156 | foreach ($text_lines as $line) { |
| 157 | if (strpos($line, ':') !== false) { |
| 158 | list($key, $value) = explode(':', $line, 2); |
| 159 | $result[trim($key) . 1] = trim($value); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | $aiwf = Http::withHeaders([ |
| 164 | 'Accept' => 'application/json', |
| 165 | 'Content-Type' => 'application/json', |
| 166 | ])->post('https://aiwf.ibvgroup.com/webhook/client-finder', [ |
| 167 | 'ticket_id' => $item->id, |
| 168 | 'region' => strtolower($reg) |
| 169 | ]); |
| 170 | |
| 171 | $aiwfData = json_decode($aiwf->body()); |
| 172 | |
| 173 | $aiwfData = @$aiwfData->data->crm_record; |
| 174 | |
| 175 | TblLeave::upsert( |
| 176 | [ |
| 177 | [ |
| 178 | 'company_id' => $companyId, |
| 179 | 'freshdesk_ticket_id' => $item->id, |
| 180 | 'freshdesk_ticket_link' => $url . '/a/tickets/' . $item->id, |
| 181 | 'customer_name' => $aiwfData->service_name ?? $result['Nombre'] ?? $result['Nombre (2nd try)'] ?? $result['Nombre del cliente1'] ?? null, |
| 182 | 'customer_email' => $aiwfData->email ?? $result['Email'] ?? $result['Correo1'] ?? null, |
| 183 | 'customer_telephone_number' => $aiwfData->phone ?? $result['Teléfono'] ?? $result['Teléfono llamada'] ?? $result['Teléfono1'] ?? null, |
| 184 | 'gestiona_customer_id' => $aiwfData->g3w_client_id ?? $result['Client ID'] ?? null, |
| 185 | 'description' => $cleanHtml, |
| 186 | 'date_of_receipt' => $item->created_at, |
| 187 | 'private_comment' => $result['Motivo/Consulta1'] ?? null, |
| 188 | 'comment_for_administration' => $result['Resumen'], |
| 189 | 'created_by' => 'System', |
| 190 | 'updated_by' => 'System', |
| 191 | 'updated_at' => now() |
| 192 | ], |
| 193 | ], |
| 194 | ['freshdesk_ticket_id'], |
| 195 | [ |
| 196 | 'company_id', |
| 197 | 'freshdesk_ticket_link', |
| 198 | 'customer_name', |
| 199 | 'customer_email', |
| 200 | 'customer_telephone_number', |
| 201 | 'gestiona_customer_id', |
| 202 | 'description', |
| 203 | 'date_of_receipt', |
| 204 | 'private_comment', |
| 205 | 'comment_for_administration', |
| 206 | 'created_by', |
| 207 | 'updated_by', |
| 208 | 'updated_at', |
| 209 | ] |
| 210 | ); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | } |
| 217 | |
| 218 | |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | function htmlToTextWithLineBreaks(\DOMNode $node) { |
| 223 | $text = ''; |
| 224 | |
| 225 | if(!empty($node)){ |
| 226 | foreach (@$node->childNodes as $child) { |
| 227 | if ($child->nodeType === XML_TEXT_NODE) { |
| 228 | $text .= $child->nodeValue; |
| 229 | } elseif ($child->nodeName === 'br') { |
| 230 | $text .= "\n"; |
| 231 | } elseif ($child->nodeName === 'div' || $child->nodeName === 'p') { |
| 232 | $text .= $this->htmlToTextWithLineBreaks($child) . "\n"; |
| 233 | } else { |
| 234 | $text .= $this->htmlToTextWithLineBreaks($child); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return $text; |
| 240 | } |
| 241 | } |