26if (!defined(
'NOTOKENRENEWAL')) {
27 define(
'NOTOKENRENEWAL', 1);
29if (!defined(
'NOCSRFCHECK')) {
30 define(
'NOCSRFCHECK', 1);
33require
'../../main.inc.php';
34require_once DOL_DOCUMENT_ROOT .
'/core/lib/pdf.lib.php';
35require_once DOL_DOCUMENT_ROOT .
'/core/lib/date.lib.php';
36require_once DOL_DOCUMENT_ROOT .
'/core/lib/functions2.lib.php';
37require_once DOL_DOCUMENT_ROOT .
'/ai/lib/ai.lib.php';
38require_once DOL_DOCUMENT_ROOT .
'/includes/tecnickcom/tcpdf/tcpdf.php';
48if (!$user->hasRight(
'ai',
'assistant',
'use')) {
55$langs->loadLangs(array(
'products',
'stocks',
'suppliers',
'companies',
'margins',
'bills',
'main',
'reports@reports'));
68 'ref' =>
'Ref',
'label' =>
'Label',
'name' =>
'ThirdParty',
'socid' =>
'Customer',
'fk_soc' =>
'Customer',
69 'paye' =>
'Paid',
'status' =>
'Status',
'type' =>
'Type',
'email' =>
'Email',
'town' =>
'Town',
70 'date' =>
'DateInvoice',
'datef' =>
'DateInvoice',
'date_lim_reglement' =>
'DateMaxPayment',
71 'total_ht' =>
'TotalHT',
'total_ttc' =>
'TotalTTC',
'total_tva' =>
'AmountVAT',
72 'remaintopay' =>
'RemainderToPay',
'price' =>
'Price',
'price_ttc' =>
'PriceTTC',
'tva_tx' =>
'VATRate',
73 'code_client' =>
'CustomerCode',
'code_fournisseur' =>
'SupplierCode',
'fournisseur' =>
'Supplier'
75 if (isset($map[$k])) {
76 return $langs->trans($map[$k]);
94 if ($v ===
null || $v ===
'') {
98 return (
string) count($v);
100 $moneyfields = array(
'total_ht',
'total_ttc',
'total_tva',
'total_localtax1',
'total_localtax2',
'remaintopay',
'resteapayer',
'price',
'price_ttc',
'price_min',
'subprice',
'totalpaid');
101 if (in_array($k, $moneyfields,
true) && is_numeric($v)) {
102 return price($v, 0, $langs, 1, -1, -1,
'auto');
104 if (($k ==
'date' || $k ==
'datef' || $k ==
'tms' || preg_match(
'/(^|_)date($|_)|_date$|^date_/', $k)) && is_numeric($v) && (
int) $v > 100000000 && (
int) $v < 9999999999) {
108 return $langs->trans(((
string) $v ===
'1') ?
'Yes' :
'No');
114$json =
GETPOST(
'content',
'none');
116 $json = file_get_contents(
'php://input');
120$data = json_decode($json,
true);
121if (json_last_error() !== JSON_ERROR_NONE) {
122 dol_syslog(
"Invalid JSON data in PDF generation: " . json_last_error_msg(), LOG_ERR);
123 print
"Error: Invalid data format provided.";
128$title =
GETPOST(
'title',
'restricthtml');
130 $title =
"AI Report";
133$filename =
GETPOST(
'filename',
'restricthtml');
134if (empty($filename)) {
135 $filename =
'Report.pdf';
139$filename = preg_replace(
'/[\x00-\x1F\x7F\/\\:*?"<>|]/',
'', $filename);
141if (!preg_match(
'/\.pdf$/i', $filename)) {
146 print
"No data provided for PDF generation.";
151 $outputlangs = $langs;
156 $pdf_dimensions = [$format[
'width'], $format[
'height']];
159 $pdf->SetCreator(
"Dolibarr AI");
160 $pdf->SetAuthor($user->getFullName($langs));
162 $pdf->SetTitle($title);
163 $pdf->SetMargins(15, 15, 15);
164 $pdf->SetAutoPageBreak(
true, 15);
165 if (class_exists(
'TCPDF')) {
166 $pdf->setPrintHeader(
false);
167 $pdf->setPrintFooter(
false);
170 $pdf->SetFont(
'dejavusans',
'', 10);
173 $pdf->SetFont(
'',
'B', 16);
174 $pdf->Cell(0, 10, $title, 0, 1,
'C');
175 $pdf->SetFont(
'',
'', 9);
176 $pdf->SetTextColor(100, 100, 100);
179 $pdf->SetTextColor(0, 0, 0);
182 h3 { color: #333; border-bottom: 1px solid #ccc; font-size: 12pt; margin-top: 15px; }
183 table { width: 100%; border-collapse: collapse; }
184 th { background-color: #f0f0f0; font-weight: bold; padding: 5px; border: 1px solid #ccc; }
185 td { padding: 5px; border: 1px solid #ccc; }
186 .key { font-weight: bold; width: 30%; background-color: #f9f9f9; }
191 if (isset($data[
'customer']) && isset($data[
'details'])) {
193 $html .=
'<h3>' . $langs->trans(
'ThirdpartyDetails') .
'</h3>';
194 $html .=
'<table cellpadding="4">';
195 foreach ($data[
'customer'] as $k => $v) {
197 if ($k ===
'url' || $k ===
'id' || $k ===
'rowid') {
205 foreach ($data[
'details'] as $section => $rows) {
206 $sectionTitle =
dol_escape_htmltag(ucwords((
string) str_replace(
'_',
' ', $section)));
207 $html .=
'<h3>' . $sectionTitle .
'</h3>';
209 if (empty($rows) || !is_array($rows)) {
210 $html .=
'<p>' . $langs->trans(
'NoDataAvailable') .
'</p>';
213 $keys = array_keys($rows[0]);
214 $keys = array_filter(
219 static function (
string $k) {
220 return $k !==
'url' && $k !==
'rowid';
228 $keys = array_slice(array_values($keys), 0, 12);
230 $html .=
'<table cellpadding="4"><thead><tr>';
231 foreach ($keys as $k) {
234 $html .=
'</tr></thead><tbody>';
235 foreach ($rows as $row) {
237 foreach ($keys as $k) {
243 $html .=
'</tbody></table>';
246 } elseif (isset($data[0]) && is_array($data[0])) {
248 $keys = array_keys($data[0]);
249 $keys = array_filter(
254 static function (
string $k) {
255 return $k !==
'url' && $k !==
'rowid';
263 $keys = array_slice(array_values($keys), 0, 12);
265 $html .=
'<table cellpadding="4"><thead><tr nobr="true">';
266 foreach ($keys as $key) {
269 $html .=
'</tr></thead><tbody>';
270 foreach ($data as $row) {
271 $html .=
'<tr nobr="true">';
272 foreach ($keys as $key) {
278 $html .=
'</tbody></table>';
281 $html .=
'<table cellpadding="5">';
282 foreach ($data as $key => $val) {
284 if ($key ===
'url') {
287 $valStr = is_array($val) ? json_encode($val) : $val;
293 $pdf->writeHTML($html,
true,
false,
true,
false,
'');
294 $pdf->Output($filename,
'D');
296 dol_syslog(
"Error generating PDF: " . $e->getMessage(), LOG_ERR);
297 print
"PDF Error: Unable to generate PDF. Please contact administrator.";
aiCheckCsrfToken($context='')
Check the anti-CSRF token of a request sent to one of the AI Assistant endpoints.
aiPdfFormatValue($k, $v)
Localized display value for a raw API field (money, dates, flags); strips any HTML a report tool embe...
aiPdfFieldLabel($k)
Translated header for a raw API field name (mirrors FIELD_LABELS in ai_assistant.js).
dol_now($mode='gmt')
Return date for now.
dol_ucfirst($string, $encoding="UTF-8")
Convert first character of the first word of a string to upper.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
pdf_getFormat($outputlangs=null, $mode='setup')
Return array with format properties of default PDF format.
pdf_getPDFFontSize($outputlangs)
Return font size to use for PDF generation.
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.