top.window.location = '../../index.php'"; exit; } require_once __DIR__ . "/" . "../../includes/include_basics_only.php"; require_once __DIR__ . "/" . "../../includes/classes/ConnectPDO.php"; use OcomonApi\Support\Email; use includes\classes\ConnectPDO; $conn = ConnectPDO::getInstance(); $auth = new AuthNew($_SESSION['s_logado'], $_SESSION['s_nivel'], 2); $return = []; $erro = false; $exception = ""; $mailNotification = ""; $config = getConfig($conn); $rowconfmail = getMailConfig($conn); /* Informações sobre a área destino */ $rowAreaTo = getAreaInfo($conn, $config['conf_wrty_area']); $days = $config['conf_days_bf']; /* Quantidade de dias de antecedência para o envio do e-mail */ if ($days != 0) { $qryWarranty = "SELECT e.comp_cod, u.inst_nome AS unit, e.comp_inv AS tag, l.local AS department, e.comp_sn AS serial_number, e.comp_nf AS invoice_number, t.tipo_nome AS equipment_type, f.fab_nome as manufacturer, m.marc_nome as model, fo.forn_nome AS supplier, date_add(date_format(e.comp_data_compra, '%Y-%m-%d'), INTERVAL warranty_time.tempo_meses MONTH) AS expiring_date FROM equipamentos e LEFT JOIN fabricantes f on f.fab_cod = e.comp_fab LEFT JOIN fornecedores fo on fo.forn_cod = e.comp_fornecedor LEFT JOIN email_warranty_equipment email_sent on email_sent.equipment_id = e.comp_cod, tipo_equip t, marcas_comp m, instituicao u, localizacao l, tempo_garantia warranty_time WHERE date_sub(date_add(date_format(e.comp_data_compra, '%Y-%m-%d'), INTERVAL warranty_time.tempo_meses MONTH), INTERVAL {$days} DAY) >= date_sub(date_format(curdate(), '%Y-%m-%d'), INTERVAL {$days} DAY) AND date_sub(date_add(date_format(e.comp_data_compra, '%Y-%m-%d'), INTERVAL warranty_time.tempo_meses MONTH), INTERVAL {$days} DAY) <= date_add(date_format(curdate(), '%Y-%m-%d'), INTERVAL 0 DAY) AND e.comp_inst = u.inst_cod AND e.comp_local = l.loc_id AND e.comp_garant_meses = warranty_time.tempo_cod AND e.comp_tipo_equip = t.tipo_cod AND e.comp_marca = m.marc_cod AND email_sent.sent_date IS NULL ORDER BY expiring_date, model LIMIT 10 "; /* Limitado em 10 registros por consulta para evitar gargalo no envio de e-mails */ try { $execWarranty = $conn->query($qryWarranty); } catch (Exception $e) { return; } $mailSendMethod = 'send'; if ($rowconfmail['mail_queue']) { $mailSendMethod = 'queue'; } $event = "mail-about-warranty"; $eventTemplate = getEventMailConfig($conn, $event); foreach ($execWarranty->fetchAll() as $rowWrt) { $VARS = array(); $VARS['%serial%'] = $rowWrt['serial_number']; $VARS['%tipo%'] = $rowWrt['equipment_type']; $VARS['%modelo%'] = $rowWrt['model']; $VARS['%vencimento%'] = dateScreen($rowWrt['expiring_date'],1); $VARS['%notafiscal%'] = $rowWrt['invoice_number']; $VARS['%fornecedor%'] = $rowWrt['supplier']; $VARS['%local%'] = $rowWrt['department']; $VARS['%departamento%'] = $rowWrt['department']; $VARS['%etiqueta%'] = $rowWrt['tag']; $VARS['%unidade%'] = $rowWrt['unit']; // $mailSent = send_mail($event, $rowAreaTo['email'], $rowconfmail, $eventTemplate, $VARS); /* Disparo do e-mail (ou fila no banco) para a área de atendimento */ $mail = (new Email())->bootstrap( transvars($eventTemplate['msg_subject'], $VARS), transvars($eventTemplate['msg_body'], $VARS), $rowAreaTo['email'], $eventTemplate['msg_fromname'] ); if (!$mail->{$mailSendMethod}()) { $mailNotification .= "
" . TRANS('EMAIL_NOT_SENT') . "
" . $mail->message()->getText(); } else { $sql = "INSERT INTO email_warranty_equipment ( equipment_id ) VALUES ( '" . $rowWrt['comp_cod'] . "' ) "; try { $conn->exec($sql); } catch (Exception $e) { $exception .= "
" . $e->getMessage(); } } // if ($mailSent) { // $sql = "INSERT INTO email_warranty_equipment // ( // equipment_id // ) // VALUES // ( // '" . $rowWrt['comp_cod'] . "' // ) // "; // try { // $conn->exec($sql); // } // catch (Exception $e) { // $exception .= "
" . $e->getMessage(); // } // } } } $return['msg'] = "Success!"; // dump($return); return true;