From bd0e9a927618717c9011c40f69690514e9dc6e15 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 19 Dec 2021 20:20:39 +0100 Subject: [PATCH] bug fixes / dbmapper fixes --- Application/Timerecording/login.tpl.php | 15 +++++++- Controller/ApiController.php | 14 ++++--- Controller/BackendController.php | 22 +++++++++-- Controller/TimerecordingController.php | 3 +- Models/SessionElementMapper.php | 4 +- Models/SessionMapper.php | 49 +++---------------------- tests/Models/SessionMapperTest.php | 1 - 7 files changed, 52 insertions(+), 56 deletions(-) diff --git a/Application/Timerecording/login.tpl.php b/Application/Timerecording/login.tpl.php index eebdd6e..ec6a275 100755 --- a/Application/Timerecording/login.tpl.php +++ b/Application/Timerecording/login.tpl.php @@ -1,4 +1,17 @@ -getData('account') ?? $request->header->account); - $employee = EmployeeMapper::getFromAccount($account)->limit(1)->execute(); - $type = (int) ($request->getData('type') ?? ClockingType::OFFICE); - $status = (int) ($request->getData('status') ?? ClockingStatus::START); + $employee = EmployeeMapper::get() + ->with('profile') + ->with('profile/account') + ->where('profile/account', $account) + ->limit(1) + ->execute(); + + $type = (int) ($request->getData('type') ?? ClockingType::OFFICE); + $status = (int) ($request->getData('status') ?? ClockingStatus::START); if ($employee instanceof NullEmployee) { return null; diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 3f53d3e..f7f3daf 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -19,6 +19,7 @@ use Modules\HumanResourceManagement\Models\EmployeeMapper; use Modules\HumanResourceTimeRecording\Models\NullSession; use Modules\HumanResourceTimeRecording\Models\SessionMapper; use phpOMS\Contract\RenderableInterface; +use phpOMS\DataStorage\Database\Query\OrderType; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; use phpOMS\Stdlib\Base\SmartDateTime; @@ -68,7 +69,13 @@ final class BackendController extends Controller implements DashboardElementInte $view->setTemplate('/Modules/HumanResourceTimeRecording/Theme/Backend/private-dashboard'); $view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1006303001, $request, $response)); - $employee = EmployeeMapper::getFromAccount($request->header->account)->limit(1)->execute()->getId(); + $employee = EmployeeMapper::get() + ->with('profile') + ->with('profile/account') + ->where('profile/account', $request->header->account) + ->execute() + ->getId(); + $lastOpenSession = SessionMapper::getMostPlausibleOpenSessionForEmployee($employee); $start = new SmartDateTime('now'); @@ -76,7 +83,11 @@ final class BackendController extends Controller implements DashboardElementInte $limit = $start->getEndOfMonth(); $limit->smartModify(0, -2, 0); - $list = SessionMapper::getSessionListForEmployee($employee, $start, 0); + $list = SessionMapper::getAll() + ->where('employee', $employee) + ->where('createdAt', $start->format('Y-m-d H:i:s'), '<=') + ->sort('id', OrderType::DESC) + ->execute(); $view->addData('sessions', $list); $view->addData('lastSession', $lastOpenSession); @@ -104,7 +115,12 @@ final class BackendController extends Controller implements DashboardElementInte $view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1006303001, $request, $response)); $session = SessionMapper::get()->where('id', (int) $request->getData('id'))->execute(); - $employee = EmployeeMapper::getFromAccount($request->header->account)->getId(); + $employee = EmployeeMapper::get() + ->with('profile') + ->with('profile/account') + ->where('profile/account', $request->header->account) + ->execute() + ->getId(); if ($session->getEmployee()->getId() !== $employee) { $view->addData('session', new NullSession()); diff --git a/Controller/TimerecordingController.php b/Controller/TimerecordingController.php index 853a18b..1334719 100755 --- a/Controller/TimerecordingController.php +++ b/Controller/TimerecordingController.php @@ -16,6 +16,7 @@ namespace Modules\HumanResourceTimeRecording\Controller; use Modules\HumanResourceTimeRecording\Models\SessionMapper; use phpOMS\Contract\RenderableInterface; +use phpOMS\DataStorage\Database\Query\OrderType; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; use phpOMS\Views\View; @@ -48,7 +49,7 @@ final class TimerecordingController extends Controller $view->setTemplate('/Modules/HumanResourceTimeRecording/Theme/Timeterminal/overview'); $view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1006301001, $request, $response)); - $list = SessionMapper::getNewest(50); + $list = SessionMapper::getAll()->sort('id', OrderType::DESC)->limit(50)->execute(); $view->addData('sessions', $list); return $view; diff --git a/Models/SessionElementMapper.php b/Models/SessionElementMapper.php index da816cc..25cc148 100755 --- a/Models/SessionElementMapper.php +++ b/Models/SessionElementMapper.php @@ -47,8 +47,8 @@ final class SessionElementMapper extends DataMapperFactory */ public const BELONGS_TO = [ 'session' => [ - 'mapper' => SessionMapper::class, - 'external' => 'hr_timerecording_session_element_session', + 'mapper' => SessionMapper::class, + 'external' => 'hr_timerecording_session_element_session', ], ]; diff --git a/Models/SessionMapper.php b/Models/SessionMapper.php index 2c46caf..0fd5237 100755 --- a/Models/SessionMapper.php +++ b/Models/SessionMapper.php @@ -52,10 +52,10 @@ final class SessionMapper extends DataMapperFactory */ public const HAS_MANY = [ 'sessionElements' => [ - 'mapper' => SessionElementMapper::class, - 'table' => 'hr_timerecording_session_element', - 'self' => 'hr_timerecording_session_element_session', - 'external' => null, + 'mapper' => SessionElementMapper::class, + 'table' => 'hr_timerecording_session_element', + 'self' => 'hr_timerecording_session_element_session', + 'external' => null, ], ]; @@ -67,8 +67,8 @@ final class SessionMapper extends DataMapperFactory */ public const BELONGS_TO = [ 'employee' => [ - 'mapper' => EmployeeMapper::class, - 'external' => 'hr_timerecording_session_employee', + 'mapper' => EmployeeMapper::class, + 'external' => 'hr_timerecording_session_employee', ], ]; @@ -160,41 +160,4 @@ final class SessionMapper extends DataMapperFactory return null; } - - /** - * Get sessions for employee - * - * @param int $employee Employee id - * @param \DateTime $start Start - * @param int $offset Offset - * @param int $limit Result limit - * - * @return Session[] - * - * @since 1.0.0 - */ - public static function getSessionListForEmployee(int $employee, \DateTime $start, int $offset = 0, int $limit = 50) : array - { - $query = new Builder(self::$db); - $query = self::getQuery($query) - ->where(self::TABLE . '_d1.hr_timerecording_session_employee', '=', $employee) - ->andWhere(self::TABLE . '_d1.' . self::CREATED_AT, '<=', $start->format('Y-m-d H:i:s')) - ->orderBy(self::TABLE . '_d1.' . self::CREATED_AT, 'DESC') - ->offset($offset) - ->limit($limit); - - $sth = self::$db->con->prepare($query->toSql()); - $sth->execute(); - - $results = $sth->fetchAll(\PDO::FETCH_ASSOC); - $objs = self::populateIterable($results === false ? [] : $results); - - foreach ($objs as $obj) { - self::fillRelations($obj, RelationType::ALL, 6); - } - - self::clear(); - - return $objs; - } } diff --git a/tests/Models/SessionMapperTest.php b/tests/Models/SessionMapperTest.php index 11d83f7..99224a7 100755 --- a/tests/Models/SessionMapperTest.php +++ b/tests/Models/SessionMapperTest.php @@ -49,7 +49,6 @@ final class SessionMapperTest extends \PHPUnit\Framework\TestCase self::assertNull(SessionMapper::getMostPlausibleOpenSessionForEmployee(9999)); // @todo implement - //self::assertGreaterThan(0, \count(SessionMapper::getSessionListForEmployee(1, (new \DateTime('now'))->modify('+1 month')))); // self::assertGreaterThan(0, SessionMapper::getMostPlausibleOpenSessionForEmployee(1)->getId()); } }