Added transaction id to shifts for bulk deletion

This commit is contained in:
Igor Scheller 2022-06-03 21:46:16 +02:00
parent aa249868ba
commit 96277dcfc4
6 changed files with 165 additions and 4 deletions

View File

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
use Illuminate\Database\Schema\Blueprint;
class ShiftsAddTransactionId extends Migration
{
/**
* Run the migration
*/
public function up(): void
{
if (!$this->schema->hasTable('Shifts')) {
return;
}
$this->schema->table('Shifts', function (Blueprint $table) {
$table->unsignedInteger('transaction_id')->nullable()->default(null);
});
}
/**
* Reverse the migration
*/
public function down(): void
{
if (!$this->schema->hasTable('Shifts')) {
return;
}
$this->schema->table('Shifts', function (Blueprint $table) {
$table->dropColumn('transaction_id');
});
}
}

View File

@ -556,13 +556,22 @@ function Shift_update($shift)
); );
} }
/**
* Get the next free shifts transaction id
*/
function Shift_get_next_transaction_id(): int
{
return Db::selectOne('SELECT MAX(transaction_id) + 1 AS transaction_id FROM Shifts')['transaction_id'] ?? 1;
}
/** /**
* Create a new shift. * Create a new shift.
* *
* @param array $shift * @param array $shift
* @param int $transactionId
* @return int ID of the new created shift * @return int ID of the new created shift
*/ */
function Shift_create($shift) function Shift_create($shift, $transactionId = null)
{ {
DB::insert(' DB::insert('
INSERT INTO `Shifts` ( INSERT INTO `Shifts` (
@ -573,11 +582,12 @@ function Shift_create($shift)
`title`, `title`,
`description`, `description`,
`URL`, `URL`,
`transaction_id`,
`created_by_user_id`, `created_by_user_id`,
`edited_at_timestamp`, `edited_at_timestamp`,
`created_at_timestamp` `created_at_timestamp`
) )
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
', ',
[ [
$shift['shifttype_id'], $shift['shifttype_id'],
@ -587,6 +597,7 @@ function Shift_create($shift)
$shift['title'], $shift['title'],
$shift['description'], $shift['description'],
$shift['URL'], $shift['URL'],
$transactionId,
auth()->user()->id, auth()->user()->id,
time(), time(),
time(), time(),

View File

@ -2,7 +2,9 @@
use Engelsystem\Database\Db; use Engelsystem\Database\Db;
use Engelsystem\Helpers\Carbon; use Engelsystem\Helpers\Carbon;
use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\User\User;
/** /**
* @return string * @return string
@ -363,9 +365,10 @@ function admin_shifts()
throw_redirect(page_link_to('admin_shifts')); throw_redirect(page_link_to('admin_shifts'));
} }
$transactionId = Shift_get_next_transaction_id();
foreach ($session->get('admin_shifts_shifts', []) as $shift) { foreach ($session->get('admin_shifts_shifts', []) as $shift) {
$shift['URL'] = null; $shift['URL'] = null;
$shift_id = Shift_create($shift); $shift_id = Shift_create($shift, $transactionId);
engelsystem_log( engelsystem_log(
'Shift created: ' . $shifttypes[$shift['shifttype_id']] 'Shift created: ' . $shifttypes[$shift['shifttype_id']]
@ -424,7 +427,12 @@ function admin_shifts()
. '</div>'; . '</div>';
} }
return page_with_title(admin_shifts_title(), [ return page_with_title(
admin_shifts_title() . ' ' . sprintf(
'<a href="%s">%s</a>',
page_link_to('admin_shifts_history'),
icon('clock-history')
), [
msg(), msg(),
form([ form([
div('row',[ div('row',[
@ -494,3 +502,90 @@ function admin_shifts()
]) ])
]); ]);
} }
/**
* @return string
*/
function admin_shifts_history_title(): string
{
return __('Shifts history');
}
/**
* Display shifts transaction history
*
* @return string
*/
function admin_shifts_history(): string
{
if (!auth()->can('admin_shifts')) {
throw new HttpForbidden();
}
$request = request();
$transactionId = $request->postData('transaction_id');
if ($request->hasPostData('delete') && $transactionId) {
$shifts = Db::select('
SELECT SID
FROM Shifts
WHERE transaction_id = ?
', [$transactionId]);
engelsystem_log('Deleting ' . count($shifts) . ' shifts (transaction id ' . $transactionId . ')');
foreach ($shifts as $shift) {
$shift = Shift($shift['SID']);
UserWorkLog_from_shift($shift);
shift_delete($shift['SID']);
engelsystem_log(
'Deleted shift ' . $shift['name']
. ' from ' . date('Y-m-d H:i', $shift['start'])
. ' to ' . date('Y-m-d H:i', $shift['end'])
);
}
success(sprintf(__('%s shifts deleted.'), count($shifts)));
throw_redirect(page_link_to('admin_shifts_history'));
}
$shifts = Db::select('
SELECT
transaction_id,
title,
COUNT(SID) AS count,
MIN(start) AS start,
MAX(end) AS end,
created_by_user_id AS user_id,
created_at_timestamp AS created_at
FROM Shifts
WHERE transaction_id IS NOT NULL
GROUP BY transaction_id
ORDER BY transaction_id DESC
');
foreach ($shifts as &$shift) {
$shift['user'] = User_Nick_render(User::find($shift['user_id']));
$shift['start'] = Carbon::createFromTimestamp($shift['start'])->format(__('Y-m-d H:i'));
$shift['end'] = Carbon::createFromTimestamp($shift['end'])->format(__('Y-m-d H:i'));
$shift['created_at'] = Carbon::createFromTimestamp($shift['created_at'])->format(__('Y-m-d H:i'));
$shift['actions'] = form([
form_hidden('transaction_id', $shift['transaction_id']),
form_submit('delete', icon('trash') . __('delete all'), 'btn-sm', true, 'danger'),
]);
}
return page_with_title(admin_shifts_history_title(), [
msg(),
table([
'transaction_id' => __('ID'),
'title' => __('Title'),
'count' => __('Count'),
'start' => __('Start'),
'end' => __('End'),
'user' => __('User'),
'created_at' => __('Created'),
'actions' => ''
], $shifts)
], true);
}

View File

@ -531,6 +531,18 @@ msgstr "Benötigte Engel"
msgid "Shift deleted." msgid "Shift deleted."
msgstr "Schicht gelöscht." msgstr "Schicht gelöscht."
msgid "Shifts history"
msgstr "Schichten historie"
msgid "%s shifts deleted."
msgstr "%s Schichten gelöscht."
msgid "Created"
msgstr "Erstellt"
msgid "delete all"
msgstr "alle löschen"
#: includes/controller/shifts_controller.php:238 #: includes/controller/shifts_controller.php:238
#, php-format #, php-format
msgid "Do you want to delete the shift %s from %s to %s?" msgid "Do you want to delete the shift %s from %s to %s?"

View File

@ -27,6 +27,7 @@ class LegacyMiddleware implements MiddlewareInterface
'users', 'users',
'user_driver_licenses', 'user_driver_licenses',
'user_worklog', 'user_worklog',
'admin_shifts_history',
]; ];
/** @var ContainerInterface */ /** @var ContainerInterface */
@ -176,6 +177,8 @@ class LegacyMiddleware implements MiddlewareInterface
$title = admin_shifts_title(); $title = admin_shifts_title();
$content = admin_shifts(); $content = admin_shifts();
return [$title, $content]; return [$title, $content];
case 'admin_shifts_history':
return [admin_shifts_history_title(), admin_shifts_history()];
} }
throw_redirect(page_link_to('login')); throw_redirect(page_link_to('login'));

View File

@ -57,6 +57,7 @@ trait HasDatabase
['migration' => '2021_10_12_000000_add_shifts_description'], ['migration' => '2021_10_12_000000_add_shifts_description'],
['migration' => '2021_12_30_000000_remove_admin_news_html_privilege'], ['migration' => '2021_12_30_000000_remove_admin_news_html_privilege'],
['migration' => '2022_06_02_000000_create_voucher_edit_permission'], ['migration' => '2022_06_02_000000_create_voucher_edit_permission'],
['migration' => '2022_06_03_000000_shifts_add_transaction_id'],
] ]
); );