Travel reimburse feature added

This commit is contained in:
2026-07-07 22:48:57 +02:00
parent e1ef2456ea
commit 30269316b2
29 changed files with 21392 additions and 26 deletions

View File

@@ -0,0 +1,24 @@
<?php
use api\classes\API_companies;
session_start();
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_companies.php";
$API_companies = new API_companies();
if (!$GLOBALS['modules_enabled']['customers']) {
echo '405 Not Allowed';
exit;
}
if ($API_companies->request_method === 'GET') {
$API_companies->checkPermissions('customer-companies', 'RO');
$companies = $API_companies->getCompanies();
$API_companies->apiOutput($code = 200, $data = $companies);
}

View File

@@ -0,0 +1,51 @@
<?php
use api\classes\API_portalsettings;
use api\classes\API_office_travel_reimburse;
session_start();
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_portalsettings.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_office_travel_reimburse.php";
$API_portalsettings = new API_portalsettings();
if (!$GLOBALS['modules_enabled']['office']) {
echo '405 Not Allowed';
exit;
}
if ($API_portalsettings->request_method === 'GET') {
$API_portalsettings->checkPermissions('office-travel-reimburse', 'RO');
$travel_reimbursements = $API_portalsettings->getTravelReimburseSettings();
$API_portalsettings->apiOutput($code = 200, $travel_reimbursements);
} elseif ($API_portalsettings->request_method === 'PUT') {
$API_portalsettings->checkPermissions('admin-portalsettings', 'RW');
$API_office_travel_reimburse = new API_office_travel_reimburse();
# Edit the portal settings of the platform
$requiredFields = [
'office_travelreimburse_cents_homework_incl' => ['type' => 'int'],
'office_travelreimburse_cents_homework_excl' => ['type' => 'int'],
'office_travelreimburse_cents_business_incl' => ['type' => 'int'],
'office_travelreimburse_cents_business_excl' => ['type' => 'int'],
'portal_uuid' => ['type' => 'uuid'],
];
if (count($API_portalsettings->getPortalSettings()) !== 1) {
$API_portalsettings->apiOutput(400, ['error' => 'Invalid portal_uuid']);
}
$API_portalsettings->validateData($requiredFields);
# Update the permission
$API_portalsettings->updatePortalSettingsOfficeTravelReimburse();
}

View File

@@ -0,0 +1,89 @@
<?php
use api\classes\API_office_travel_reimburse;
session_start();
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_office_travel_reimburse.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_companies.php";
$API_office_travel_reimburse = new API_office_travel_reimburse();
if (!$GLOBALS['modules_enabled']['office']) {
echo '405 Not Allowed';
exit;
}
if ($API_office_travel_reimburse->request_method === 'GET') {
$API_office_travel_reimburse->checkPermissions('office-travel-reimburse', 'RO');
$_GET['builder'] = [1 => ['where' => [0 => 'user_uuid', 1 => $_SESSION['user']['user_uuid']]]];
$travel_reimbursements = $API_office_travel_reimburse->getTravelReimburse();
$API_office_travel_reimburse->apiOutput($code = 200, $travel_reimbursements);
} elseif ($API_office_travel_reimburse->request_method === 'POST') {
$API_office_travel_reimburse->checkPermissions('office-travel-reimburse', 'RW');
$API_office_travel_reimburse->return_url = false;
$API_office_travel_reimburse->setTravelReimburseCompany();
$API_office_travel_reimburse->postedData['user_uuid'] = $_SESSION['user']['user_uuid'];
$requiredFields = [
'user_uuid' => ['type' => 'uuid'],
'departure_postcode' => ['type' => 'string'],
'destination_postcode' => ['type' => 'string'],
'travel_date' => ['type' => 'date'],
'travel_distance' => ['type' => 'int'],
'office_travelreimburse_company_uuid' => ['type' => 'uuid'],
'office_travelreimburse_company_name' => ['type' => 'string'],
'homework' => ['type' => 'int']
];
$optionalFields = [
'travel_description' => ['type' => 'string']
];
$API_office_travel_reimburse->validateData($requiredFields, $optionalFields);
$API_office_travel_reimburse->createTravelReimburse();
} elseif ($API_office_travel_reimburse->request_method === 'PUT') {
# Only superuser can delete permission due to fact that the backend needs programming when setting a permission
$API_office_travel_reimburse->checkPermissions('office-travel-reimburse', 'RW');
$API_office_travel_reimburse->return_url = false;
$API_office_travel_reimburse->setTravelReimburseCompany();
$API_office_travel_reimburse->postedData['user_uuid'] = $_SESSION['user']['user_uuid'];
$requiredFields = [
'reimburse_uuid' => ['type' => 'uuid'],
'user_uuid' => ['type' => 'uuid'],
'departure_postcode' => ['type' => 'string'],
'destination_postcode' => ['type' => 'string'],
'travel_date' => ['type' => 'date'],
'travel_distance' => ['type' => 'int'],
'office_travelreimburse_company_uuid' => ['type' => 'uuid'],
'office_travelreimburse_company_name' => ['type' => 'string'],
'homework' => ['type' => 'boolean']
];
$optionalFields = [
'travel_description' => ['type' => 'string'],
];
$API_office_travel_reimburse->validateData($requiredFields, $optionalFields);
$API_office_travel_reimburse->updateTravelReimburse();
} elseif ($API_office_travel_reimburse->request_method === 'DELETE') {
# Only superuser can delete permission due to fact that the backend needs programming when setting a permission
$API_office_travel_reimburse->checkPermissions('office-travel-reimburse', 'RW');
$API_office_travel_reimburse->return_url = false;
$requiredFields = [
'reimburse_uuid' => ['type' => 'uuid']
];
$API_office_travel_reimburse->validateData($requiredFields);
$API_office_travel_reimburse->deleteTravelReimburse();
}

View File

@@ -0,0 +1,211 @@
<?php
use api\classes\API_office_travel_reimburse;
use bin\php\Classes\mailBuilder;
use Dompdf\Dompdf;
use Dompdf\Options;
session_start();
require_once "{$_SERVER['DOCUMENT_ROOT']}/../vendor/autoload.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_office_travel_reimburse.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_companies.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/bin/php/Classes/mailBuilder.php";
$API_office_travel_reimburse = new API_office_travel_reimburse();
if (!$GLOBALS['modules_enabled']['office']) {
echo '405 Not Allowed';
exit;
}
if ($API_office_travel_reimburse->request_method === 'POST') {
ini_set('display_errors', 1);
$API_office_travel_reimburse->checkPermissions('office-travel-reimburse', 'RO');
$API_office_travel_reimburse->return_url = false;
$portal_settings = $GLOBALS['conn']->query("SELECT * FROM system_settings")->fetch_assoc();
$month = $_POST['calender_month'];
$firstDay = $month . '-01';
$lastDay = date('Y-m-t', strtotime($firstDay));
$_GET['builder'] = [
1 => [
'where' => [
0 => 'user_uuid',
1 => $_SESSION['user']['user_uuid']
],
'between' => [
0 => 'travel_date',
1 => $firstDay,
2 => $lastDay
]
]
];
$travel_reimbursements = $API_office_travel_reimburse->getTravelReimburse();
// format is this: 2026-07
$month = $_POST['calender_month'];
$lastDay = date('Y-m-t', strtotime($month . '-01'));
$invoiceNumber = 'D-' . date('nY', strtotime($month . '-01'));
$totalBusinessKm = 0;
$totalHomeworkKm = 0;
foreach ($travel_reimbursements as $trip) {
if ($trip['homework']) {
$totalHomeworkKm += $trip['travel_distance'];
} else {
$totalBusinessKm += $trip['travel_distance'];
}
}
$totalKm = $totalBusinessKm + $totalHomeworkKm;
$amountExcl =
($totalBusinessKm * $portal_settings['office_travelreimburse_cents_business_excl']) +
($totalHomeworkKm * $portal_settings['office_travelreimburse_cents_homework_excl']);
$amountIncl =
($totalBusinessKm * $portal_settings['office_travelreimburse_cents_business_incl']) +
($totalHomeworkKm * $portal_settings['office_travelreimburse_cents_homework_incl']);
// cents -> euros
$amountExcl /= 100;
$amountIncl /= 100;
ob_start();
?>
<h2><?= __('travelReimburse') ?></h2>
<table border="0" cellpadding="4">
<tr>
<td>
<strong><?= __('date') ?>:</strong>
</td>
<td><?= date('d/m/Y', strtotime($lastDay)); ?></td>
</tr>
<tr>
<td>
<strong><?= __('full_name') ?>:</strong>
</td>
<td><?= htmlspecialchars($_SESSION['user']['user_full_name']); ?></td>
</tr>
<tr>
<td>
<strong><?= __('invoice_number') ?>:</strong>
</td>
<td><?= $invoiceNumber; ?></td>
</tr>
<tr>
<td>
<strong><?= __('total_travel_excl') ?>:</strong>
</td>
<td>€ <?= number_format($amountExcl, 2, ',', '.'); ?></td>
</tr>
<tr>
<td>
<strong><?= __('total_travel_incl') ?>:</strong>
</td>
<td>€ <?= number_format($amountIncl, 2, ',', '.'); ?></td>
</tr>
</table>
<br>
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th><?= __('date') ?></th>
<th><?= __('from') ?></th>
<th><?= __('to') ?></th>
<th><?= __('company') ?></th>
<th>KM <?= __('homework') ?></th>
<th>KM <?= __('business') ?></th>
<th><?= __('description') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($travel_reimbursements as $trip) { ?>
<tr>
<td><?= date('d-m-Y', strtotime($trip['travel_date'])); ?></td>
<td><?= htmlspecialchars($trip['departure_postcode']); ?></td>
<td><?= htmlspecialchars($trip['destination_postcode']); ?></td>
<td><?= htmlspecialchars($trip['office_travelreimburse_company_name']); ?></td>
<td><?= ($trip['homework'] ? $trip['travel_distance'] : '') ?></td>
<td><?= ($trip['homework'] ? '' : $trip['travel_distance']) ?></td>
<td><?= $trip['travel_description']; ?></td>
</tr>
<?php } ?>
<tr>
<td colspan="5" align="right">
<strong><?= __('total') . ' ' . __('business') ?> km</strong>
</td>
<td>
<strong><?= $totalBusinessKm; ?></strong>
</td>
<td></td>
</tr>
<tr>
<td colspan="5" align="right">
<strong><?= __('total') . ' ' . __('homework') ?> km</strong>
</td>
<td>
<strong><?= $totalHomeworkKm; ?></strong>
</td>
<td></td>
</tr>
<tr>
<td colspan="5" align="right">
<strong><?= __('total') ?></strong>
</td>
<td>
<strong><?= $totalKm; ?></strong>
</td>
<td></td>
</tr>
</tbody>
</table>
<?php
$html = ob_get_clean();
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$pdfFile = sys_get_temp_dir() . '/travel_reimburse.pdf';
file_put_contents(
$pdfFile,
$dompdf->output()
);
$mail = new mailBuilder();
$mail->addAddress($_SESSION['user']['user_email'], $_SESSION['user']['user_full_name']);
$mail->subject = 'Travel reimbursement ' . $_SESSION['user']['user_full_name'] . ' - ' . $invoiceNumber;
$mail->mailText = 'Your travel reimbursement is attached.';
try {
$mail->addAttachment($pdfFile, 'travel_reimbursement.pdf');
} catch (\PHPMailer\PHPMailer\Exception $e) {
}
$mail->sendMail();
unlink($pdfFile);
}