Integrate dynamic booking data and add Economic sync task

Enhanced PDF generation with dynamic booking, department, and customer data, improving template variability. Introduced a new cron job and script to sync Economic invoice statuses, ensuring data consistency and error handling in automated tasks.
This commit is contained in:
Jepp9350
2025-04-09 15:10:30 +02:00
parent 110ac5c28f
commit ce2e233e5f
4 changed files with 131 additions and 20 deletions
+25
View File
@@ -2,6 +2,7 @@
// prevent direct access
use classes\backup_store;
use classes\economic;
use objects\bookings_o;
use objects\logs_o;
use objects\users_o;
@@ -57,6 +58,12 @@ $cron_tasks = [
'next_run' => 0,
'function' => 'backup',
],
'SyncEconomicInvoiceStatus' => [
'interval' => 60, // 1 minute
'last_run' => 0,
'next_run' => 0,
'function' => 'SyncEconomicInvoiceStatus',
],
];
function checkUnfulfilledBookings(): void
@@ -103,6 +110,24 @@ function SyncUserEconomicCustomerDetails(): void
$users_o->syncAllUsersEconomicCustomerDetails();
}
/**
* @throws Exception
*/
function SyncEconomicInvoiceStatus(): void
{
$economic = new economic();
try {
$economic->getTasks()->runCheckErrors();
} catch (Exception $e) {
// Do nothing, this is automatically running
}
try {
$economic->getTasks()->runCheckDrafts();
} catch (Exception $e) {
// Do nothing, this is automatically running
}
}
foreach ( $cron_tasks as $task => $data ) {
$lastRun = redis->get_last_crond_run($task) === null ? 0 : redis->get_last_crond_run($task);
$nextRun = $lastRun + $data['interval'];
@@ -0,0 +1,31 @@
<?php
/**
* This script is used to sync the economic invoice status with the database.
* It is run as a cron job.
*
* index.php runs this script.
*/
// prevent direct access
use classes\economic;
if (!defined('WD')) {
exit;
}
$start = microtime(true);
// Sync the economic invoice status
$economic = new economic();
try {
$economic->getTasks()->runCheckErrors();
} catch (Exception $e) {
// This is automatically running, so we don't need to log the error
}
try {
$economic->getTasks()->runCheckDrafts();
} catch (Exception $e) {
// This is automatically running, so we don't need to log the error
}
$end = microtime(true);
//$slack = new \classes\slack();
//$slack->send_message("The booking sync script has finished. It took " . round($end - $start, 2) . " seconds to run.");
@@ -39,17 +39,23 @@ class wash_certificate extends html2pdf_template
//$html .= self::getSignature();
$html .= self::setDocumentFields(
'123',
'123',
'123',
'123',
self::getKey('time') . ' ' . self::getKey('date'),
self::getKey('carried_out_by'),
self::getKey('seal_number'),
);
$html .= self::setDocumentRegistrationNumberFields(
'123',
'123',
self::getKey('reg_1'),
self::getKey('reg_2'),
);
$html .= self::setDocumentCustomerSupplierFields(
'123',
'123',
[
self::getKey('customer_name'),
self::getKey('customer_address'),
],
[
self::getKey('department_name'),
self::getKey('department_address'),
],
);
$html .= self::setSignatureField(
'123',
@@ -106,23 +112,43 @@ class wash_certificate extends html2pdf_template
';
}
private function setDocumentCustomerSupplierFields(string $customer_details_column, string $supplier_details_column): string
private function setDocumentCustomerSupplierFields(array $customer_details_column, array $supplier_details_column): string
{
$column_width_px = 257;
$spacer_width_px = 32;
$column_2_additional_width_px = 12;
// Parse the customer details column
$customer_details_html = '';
foreach ( $customer_details_column as $key => $value ) {
// If the item is the first item, add a strong tag
if ($key === 0) {
$customer_details_html .= '<strong>' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '</strong><br>';
} else {
$customer_details_html .= htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '<br>';
}
}
// Parse the supplier details column
$supplier_details_html = '';
foreach ( $supplier_details_column as $key => $value ) {
// If the item is the first item, add a strong tag
if ($key === 0) {
$supplier_details_html .= '<strong>' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '</strong><br>';
} else {
$supplier_details_html .= htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '<br>';
}
}
return '
<table style="position: absolute; top: 720px; left: 0; width: 100%; border-collapse: collapse; margin-left: 72px; margin-right: 72px; align-content: space-between;">
<table style="position: absolute; top: 712px; left: 0; width: 100%; border-collapse: collapse; margin-left: 72px; margin-right: 72px; align-content: space-between;">
<tr>
<td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
<strong>' . htmlspecialchars($customer_details_column, ENT_QUOTES, 'UTF-8') . '</strong>
' . $customer_details_html . '
</td>
<!-- Spacer -->
<td style="max-width: ' . $spacer_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent; min-width: ' . $spacer_width_px . 'px; padding: 0; margin: 0;">
<div style="width: ' . $spacer_width_px . 'px; height: 1px; background-color: transparent;"></div>
</td>
<td style="width: ' . ($column_width_px + $column_2_additional_width_px) . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
<strong>' . htmlspecialchars($supplier_details_column, ENT_QUOTES, 'UTF-8') . '</strong>
' . $supplier_details_html . '
</td>
</tr>
</table>
@@ -132,12 +158,13 @@ class wash_certificate extends html2pdf_template
private function setSignatureField(string $column_1): string
{
$column_width_px = 219;
$column_height_px = 30;
$margin_left_px = 263;
$margin_right_px = $margin_left_px;
return '
<table style="position: absolute; top: 878px; left: 0; width: 100%; border-collapse: collapse; margin-left: ' . $margin_left_px . 'px; ' . $margin_right_px . 'px; align-content: space-between;">
<table style="position: absolute; top: 878px; left: 0; width: 100%; border-collapse: collapse; margin-left: ' . $margin_left_px . 'px; ' . $margin_right_px . 'px; align-content: space-between; max-height: ' . $column_height_px . 'px; min-height: ' . $column_height_px . 'px;">
<tr>
<td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid black;">
<td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid black; min-height: ' . $column_height_px . 'px; max-height: ' . $column_height_px . 'px;">
<strong>' . htmlspecialchars($column_1, ENT_QUOTES, 'UTF-8') . '</strong>
</td>
</tr>
@@ -25,6 +25,31 @@ class pdfGeneratorRoute
//self::requirePermission('modules_pdf_generator_test');
if (true) {
//(new logs_o())->add('orderInvoices', 'global', 1, $user->id, 'LIST_COLLECTED_INVOICES', 'User accessed the list of collected order invoices');
// Get the booking from its ID
$id = 434;
$booking = (new \objects\bookings_o())->select($id);
if (!$booking->exists()) {
$response->error('Booking not found', 404);
}
// Get the department from the booking
$department = (new \objects\departments_o())->select((int)$booking->department->value());
if (!$department->exists()) {
$response->error('Department not found', 404);
}
// Get the branding from the department
$branding = (new \objects\branding_o())->select((int)$department->branding->value());
if (!$branding->exists()) {
$response->error('Branding not found', 404);
}
// Get the customer from the booking
$customer = (new \objects\users_o())->getUserByCustomerNumber((int)$booking->customer_number->value());
$department_array = $department->asArray();
$customer_array = $customer->asArray();
$booking_array = $booking->asArray();
$department_array['branding'] = $branding->asArray();
//print_r($department_array);
//print_r($customer_array);
//print_r($booking_array);
$pdf_generator = new pdf_generator();
$pdf_generator->add_html($pdf_generator->templates->getTemplate('wash_certificate')
->setCompany([
@@ -44,14 +69,17 @@ class pdfGeneratorRoute
])
->addData([
'seal_number' => 123456,
'reg_1' => 'EC21233',
'reg_2' => 'FB1703',
'date' => '2023-10-01',
'time' => '12:00',
'reg_1' => $booking_array['regNrTraekker'],
'reg_2' => $booking_array['regNrTrailer'],
'date' => $booking_array['date'],
'time' => date('H:i'),
'carried_out_by' => 'John Doe',
'department_id' => 1,
'customer_name' => 'Customer Name',
'type' => 'Interior Cleaning',
'department_id' => $department->id,
'department_name' => $department_array['branding']['name'] ?: $department_array['name'],
'department_address' => $department_array['branding']['address'] ?: $department_array['description'],
'customer_name' => $customer->getCustomerName($customer_array['customer_number']),
'customer_address' => $customer->getCustomerEcocomicData($customer_array['customer_number'])->economic_customer->address ?: '-',
'type' => $booking_array['wash_type'],
])
->getHtml()
);