Files
api/services/nginx/app/routes/pdfGeneratorRoute.php
T
Jepp9350 ce2e233e5f 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.
2025-04-09 15:10:30 +02:00

136 lines
6.0 KiB
PHP

<?php
namespace routes;
use classes\pdf_generator;
use classes\pdf_store;
use classes\response;
use classes\router;
use objects\logs_o;
use traits\route_t;
class pdfGeneratorRoute
{
use route_t;
public function run(): void
{
global /** @var response $response */
/** @var router $router */
$router, $response;
/** PDF Generator > GET */
$this->get('/modules/pdf-generator/test', function () {
global $response;
//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([
'name' => 'Truck Wash',
'address' => 'Letland Allé 2',
'zip' => 2630,
'city' => 'Taastrup',
'phone_prefix' => 45,
'phone' => 43717886,
'email' => 'cph@truckwash.dk',
'website' => 'www.truckwash.dk',
'images' => [
'logo' => '/truckwash-banner-png.png',
'banner' => '/truckwash-banner-png.png',
'signature' => '/truckwash-underskrift.png',
],
])
->addData([
'seal_number' => 123456,
'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' => $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()
);
$pdf_path = $pdf_generator->generate_pdf();
$response->success([
'pdf_path' => $pdf_path,
'message' => 'PDF generated successfully',
'link' => (new pdf_store())->getPresignedUrl(
$pdf_path
),
]);
} else {
(new logs_o())->add('orderInvoices', 'global', 0, 0, 'LIST_COLLECTED_INVOICES', 'User tried to access the list of collected order invoices without a valid session');
$response->error('Invalid session', 400);
}
},
[
'list_collected_invoices' => 'List ALL collected order invoices. This is a superuser-only route.'
]
);
$this->get('/modules/pdf-generator/material/order', function () {
self::requireParameters([
'id'
]);
global $response;
// Generate the PDF
$pdf_generator = new pdf_generator();
$html = $pdf_generator->add_html($pdf_generator->templates->getTemplate('material_transaction')
->addData([
'reg_1' => "AB12345",
'reg_2' => "CD67890",
'seal_number' => '123456',
'date' => '2023-10-01',
'time' => '12:00',
'carried_out_by' => 'John Doe',
'department_id' => 1,
'customer_name' => 'Customer Name',
'reference' => 'Reference Number',
'notes' => 'Some notes here',
])->getHtml());
$pdf_path = $pdf_generator->generate_pdf();
$pdf_storage = new pdf_store();
$response->success([
'pdf_path' => $pdf_path,
'link' => $pdf_storage->getPresignedUrl(
$pdf_path
),
'message' => 'PDF generated successfully'
]);
});
}
}