getModuleName(); $isTrackingEnabled = ModTracker::isTrackingEnabledForModule($moduleName); if (!$isTrackingEnabled) { return; } if ($eventName == 'vtiger.entity.aftersave.final') { $recordId = $data->getId(); $columnFields = $data->getData(); $vtEntityDelta = new VTEntityDelta(); $delta = $vtEntityDelta->getEntityDelta($moduleName, $recordId, true); $newerEntity = $vtEntityDelta->getNewEntity($moduleName, $recordId); $newerColumnFields = $newerEntity->getData(); if ($moduleName === 'SalesOrder') { $recordId = $data->getId(); $focus = CRMEntity::getInstance($moduleName); $focus->retrieve_entity_info($recordId, $moduleName); $accountId = $focus->column_fields['account_id']; if (empty($accountId)) return; $toEmail = "souldibachir3150@gmail.com"; // $toEmail = getSingleFieldValue('vtiger_account', 'email1', 'accountid', $accountId); if (empty($toEmail)) return; /** ----------------------------------------------------------- * 🔥 Secure ExportPDF with login cookie (your working version) * ----------------------------------------------------------- */ ob_clean(); $loginUrl = "https://sophal.net/sophalcrm/index.php?module=Users&action=Login"; $exportUrl = "https://sophal.net/sophalcrm/index.php?module=SalesOrder&action=ExportPDF&record=$recordId"; // 1) Login $post = http_build_query([ 'username' => 'admin', // ⚠️ Replace later with "pdfbot" 'password' => 'Sophal@Crm@Sophal', // ⚠️ Replace later with another password ]); $contextLogin = stream_context_create([ 'http' => [ 'method' => 'POST', 'header' => "Content-Type: application/x-www-form-urlencoded\r\n", 'content' => $post, ] ]); file_get_contents($loginUrl, false, $contextLogin); // Extract session cookie $cookies = []; foreach ($http_response_header as $hdr) { if (stripos($hdr, 'Set-Cookie:') === 0) { $cookies[] = trim(substr($hdr, 11), ';'); } } $cookieHeader = 'Cookie: ' . implode('; ', $cookies); // 2) Download PDF $contextPDF = stream_context_create([ 'http' => [ 'method' => 'GET', 'header' => $cookieHeader ] ]); $pdfContent = file_get_contents($exportUrl, false, $contextPDF); // 3) Validate if (strpos($pdfContent, '%PDF') !== 0) { error_log("❌ ExportPDF returned invalid PDF for SalesOrder #$recordId"); return; } // 4) Save to storage $pdfPath = "storage/SalesOrder_{$recordId}.pdf"; $filePath = $_SERVER['DOCUMENT_ROOT']."/sophalcrm/storage/SalesOrder_$recordId.pdf"; file_put_contents($filePath, $pdfContent); ob_end_clean(); /** ----------------------------------------------------------- * 📧 Email body + send * ----------------------------------------------------------- */ $subject = "Sales Order #" . $focus->column_fields['salesorder_no']; $body = '
|
|
| SOPHAL SPA |
|
Bonjour '.$accountId.', Votre bon de commande n° '.$focus->column_fields['salesorder_no'].' est maintenant disponible. Vous pouvez le télécharger en cliquant sur le bouton ci-dessous : 📄 Télécharger le Bon de Commande ⚠️ Ceci est un email automatique. Merci de ne pas répondre à ce message.
Pour toute assistance, veuillez contacter notre service client : Cordialement, |
|
© 2025 SOPHAL SPA — Tous droits réservés. HASSI BEN OKBA ORAN |