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
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 :
📞 +213541229487
✉️ COMMERCIAL@SOPHAL.DZ

Cordialement,
SOPHAL SPA

© 2025 SOPHAL SPA — Tous droits réservés.
HASSI BEN OKBA ORAN
'; // Send email $outgoingModel = Settings_Vtiger_Systems_Model::getInstanceFromServerType('email', 'OutgoingServer'); $outgoingModel->pdf_path = $pdfPath; $outgoingModel->to_email = $toEmail; $ajaxAction = new Settings_Vtiger_OutgoingServerAjax_Action(); $ajaxAction->sendTestMail($outgoingModel, $subject, $body, $pdfPath); } if (is_array($delta)) { $inserted = false; foreach ($delta as $fieldName => $values) { if ($fieldName != 'modifiedtime') { if (!$inserted) { $checkRecordPresentResult = $adb->pquery('SELECT * FROM vtiger_modtracker_basic WHERE crmid = ? AND status = ?', array($recordId, ModTracker::$CREATED)); if (!$adb->num_rows($checkRecordPresentResult) && $data->isNew()) { $status = ModTracker::$CREATED; } else { $status = ModTracker::$UPDATED; } $this->id = $adb->getUniqueId('vtiger_modtracker_basic'); $changedOn = $newerColumnFields['modifiedtime']; if ($moduleName == 'Users') { $date_var = date("Y-m-d H:i:s"); $changedOn = $adb->formatDate($date_var, true); } $adb->pquery('INSERT INTO vtiger_modtracker_basic(id, crmid, module, whodid, changedon, status) VALUES(?,?,?,?,?,?)', array( $this->id, $recordId, $moduleName, $current_user->id, $changedOn, $status )); $inserted = true; } $adb->pquery( 'INSERT INTO vtiger_modtracker_detail(id,fieldname,prevalue,postvalue) VALUES(?,?,?,?)', array($this->id, $fieldName, $values['oldValue'], $values['currentValue']) ); } } } header("Location: index.php"); exit; } if ($eventName == 'vtiger.entity.beforedelete') { $recordId = $data->getId(); $columnFields = $data->getData(); $id = $adb->getUniqueId('vtiger_modtracker_basic'); $adb->pquery('INSERT INTO vtiger_modtracker_basic(id, crmid, module, whodid, changedon, status) VALUES(?,?,?,?,?,?)', array($id, $recordId, $moduleName, $current_user->id, date('Y-m-d H:i:s', time()), ModTracker::$DELETED)); } if ($eventName == 'vtiger.entity.afterrestore') { $recordId = $data->getId(); $columnFields = $data->getData(); $id = $adb->getUniqueId('vtiger_modtracker_basic'); $adb->pquery('INSERT INTO vtiger_modtracker_basic(id, crmid, module, whodid, changedon, status) VALUES(?,?,?,?,?,?)', array($id, $recordId, $moduleName, $current_user->id, date('Y-m-d H:i:s', time()), ModTracker::$RESTORED)); } } }