Release v0.3.2: Inventory tracking, test infra, and bugfixes
Database Portability Tests / Integration (mysql) (push) Has been skipped
Database Portability Tests / Integration (postgres) (push) Has been skipped
Database Portability Tests / Integration (sqlite) (push) Has been skipped
Database Portability Tests / Verify no MySQL-specific SQL (push) Successful in 5s
Database Portability Tests / Unit Tests (PlatformHelper) (push) Failing after 43s

Inventory:
- General material, stock items, and sales CRUD
- Category management with CategoryPicker component
- Inventory reports service
- Database migrations and mappers

Frontend:
- Inventory view with tabs (Allgemeinmaterial, Verkaufsmaterial, Verkäufe)
- Forms for general material, stock items, and sales
- Search bar fix: flexbox wrapper with inline icon replaces broken NcTextField icon slot

Tests:
- All 1,491 tests pass (zero errors, warnings, deprecations)
- BundleImportServiceTest: fixed ZipArchive empty-file deprecation
- BundleImportService: null-coalescing for targetFields
- PHPUnit test infra: make test target via container

CLAUDE.md:
- Added Testing section as PR gating criterion
This commit is contained in:
shahondin1624
2026-04-22 10:15:22 +02:00
parent ff60c4088e
commit 53b3fd945a
48 changed files with 4900 additions and 4 deletions
+4
View File
@@ -36,3 +36,7 @@ test-results/
# Release artifacts
artifacts/
# agent
plan.md
review.md
+1 -1
View File
@@ -5,7 +5,7 @@
<name>Mitgliederverwaltung</name>
<summary>Mitgliederverwaltung für Pfadfindervereine</summary>
<description><![CDATA[Verwaltung von Mitgliedern, Familien, Beiträgen, Lagern und mehr für Pfadfindervereine. Integriert sich in Nextcloud Kalender, Kontakte und Dateien.]]></description>
<version>0.3.1</version>
<version>0.3.2</version>
<licence>agpl</licence>
<author>shahondin1624</author>
<namespace>Mitgliederverwaltung</namespace>
+27
View File
@@ -188,5 +188,32 @@ return [
['name' => 'file#ensureFolder', 'url' => '/api/v1/members/{memberId}/files/ensure-folder', 'verb' => 'POST'],
['name' => 'file#lagerFiles', 'url' => '/api/v1/lager/{lagerId}/files/browse', 'verb' => 'GET'],
['name' => 'file#ensureLagerFolder', 'url' => '/api/v1/lager/{lagerId}/files/ensure-folder', 'verb' => 'POST'],
// ── Inventory Tracking ──────────────────────────────────────
['name' => 'inventory#listCategories', 'url' => '/api/v1/inventory/categories', 'verb' => 'GET'],
['name' => 'inventory#createCategory', 'url' => '/api/v1/inventory/categories', 'verb' => 'POST'],
['name' => 'inventory#updateCategory', 'url' => '/api/v1/inventory/categories/{id}', 'verb' => 'PUT'],
['name' => 'inventory#deleteCategory', 'url' => '/api/v1/inventory/categories/{id}', 'verb' => 'DELETE'],
['name' => 'inventory#listGeneral', 'url' => '/api/v1/inventory/general', 'verb' => 'GET'],
['name' => 'inventory#showGeneral', 'url' => '/api/v1/inventory/general/{id}', 'verb' => 'GET'],
['name' => 'inventory#createGeneral', 'url' => '/api/v1/inventory/general', 'verb' => 'POST'],
['name' => 'inventory#updateGeneral', 'url' => '/api/v1/inventory/general/{id}', 'verb' => 'PUT'],
['name' => 'inventory#deleteGeneral', 'url' => '/api/v1/inventory/general/{id}', 'verb' => 'DELETE'],
['name' => 'inventory#listStock', 'url' => '/api/v1/inventory/stock', 'verb' => 'GET'],
['name' => 'inventory#showStock', 'url' => '/api/v1/inventory/stock/{id}', 'verb' => 'GET'],
['name' => 'inventory#createStock', 'url' => '/api/v1/inventory/stock', 'verb' => 'POST'],
['name' => 'inventory#updateStock', 'url' => '/api/v1/inventory/stock/{id}', 'verb' => 'PUT'],
['name' => 'inventory#deleteStock', 'url' => '/api/v1/inventory/stock/{id}', 'verb' => 'DELETE'],
['name' => 'inventory#createVariant', 'url' => '/api/v1/inventory/variants', 'verb' => 'POST'],
['name' => 'inventory#updateVariant', 'url' => '/api/v1/inventory/variants/{id}', 'verb' => 'PUT'],
['name' => 'inventory#deleteVariant', 'url' => '/api/v1/inventory/variants/{id}', 'verb' => 'DELETE'],
['name' => 'inventory#listSales', 'url' => '/api/v1/inventory/sales', 'verb' => 'GET'],
['name' => 'inventory#showSale', 'url' => '/api/v1/inventory/sales/{id}', 'verb' => 'GET'],
['name' => 'inventory#createSale', 'url' => '/api/v1/inventory/sales', 'verb' => 'POST'],
['name' => 'inventory#deleteSale', 'url' => '/api/v1/inventory/sales/{id}', 'verb' => 'DELETE'],
// ── Inventory Reports ─────────────────────────────────────
['name' => 'inventoryReport#condition', 'url' => '/api/v1/inventory/reports/condition', 'verb' => 'GET'],
['name' => 'inventoryReport#sales', 'url' => '/api/v1/inventory/reports/sales', 'verb' => 'GET'],
],
];
+2 -1
View File
@@ -26,7 +26,8 @@ services:
env_file: .env
environment:
MYSQL_HOST: db
NEXTCLOUD_TRUSTED_DOMAINS: "localhost"
NEXTCLOUD_TRUSTED_DOMAINS: "localhost,192.168.2.35"
OVERWRITEHOST: "192.168.2.35"
volumes:
- nc_data:/var/www/html
- ./:/app-src:ro
+510
View File
@@ -0,0 +1,510 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Controller;
use OCA\Mitgliederverwaltung\Service\InventoryService;
use OCA\Mitgliederverwaltung\Service\SaleService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
/**
* REST API controller for inventory CRUD operations.
*
* Part of Issue #165 (Inventory Tracking).
*/
class InventoryController extends ApiController {
use ApiControllerTrait;
private InventoryService $inventoryService;
private SaleService $saleService;
private IUserSession $userSession;
private LoggerInterface $logger;
public function __construct(
string $appName,
IRequest $request,
InventoryService $inventoryService,
SaleService $saleService,
IUserSession $userSession,
LoggerInterface $logger
) {
parent::__construct($appName, $request);
$this->inventoryService = $inventoryService;
$this->saleService = $saleService;
$this->userSession = $userSession;
$this->logger = $logger;
}
// ── Categories ──────────────────────────────────────────────────
/**
* List all categories.
*
* GET /api/v1/inventory/categories
*/
#[NoCSRFRequired]
public function listCategories(): JSONResponse {
try {
$categories = $this->inventoryService->getCategories();
return new JSONResponse(['data' => array_map(fn($c) => $c->jsonSerialize(), $categories)]);
} catch (\Exception $e) {
$this->logger->error('Failed to list inventory categories', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Create a category.
*
* POST /api/v1/inventory/categories
*/
public function createCategory(): JSONResponse {
try {
$data = $this->getRequestData();
if (empty($data['name'])) {
return new JSONResponse(['error' => 'Name ist erforderlich'], Http::STATUS_BAD_REQUEST);
}
$category = $this->inventoryService->createCategory($data);
return new JSONResponse($category->jsonSerialize(), Http::STATUS_CREATED);
} catch (\Exception $e) {
$this->logger->error('Failed to create inventory category', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Update a category.
*
* PUT /api/v1/inventory/categories/{id}
*/
public function updateCategory(int $id): JSONResponse {
try {
$data = $this->getRequestData();
$category = $this->inventoryService->updateCategory($id, $data);
return new JSONResponse($category->jsonSerialize());
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Kategorie nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to update inventory category', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Delete a category.
*
* DELETE /api/v1/inventory/categories/{id}
*/
public function deleteCategory(int $id): JSONResponse {
try {
$this->inventoryService->deleteCategory($id);
return new JSONResponse(['status' => 'deleted']);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Kategorie nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to delete inventory category', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
// ── General Material ────────────────────────────────────────────
/**
* List all general material items.
*
* GET /api/v1/inventory/general
*/
#[NoCSRFRequired]
public function listGeneral(): JSONResponse {
try {
$items = $this->inventoryService->getGeneralMaterial();
$result = [];
foreach ($items as $item) {
$data = $item->jsonSerialize();
$data['categories'] = $this->inventoryService->getCategoryNamesForItem($item->getId());
$result[] = $data;
}
return new JSONResponse(['data' => $result]);
} catch (\Exception $e) {
$this->logger->error('Failed to list general material', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get a single general material item.
*
* GET /api/v1/inventory/general/{id}
*/
#[NoCSRFRequired]
public function showGeneral(int $id): JSONResponse {
try {
$item = $this->inventoryService->getGeneralMaterialById($id);
$data = $item->jsonSerialize();
$data['categories'] = $this->inventoryService->getCategoryNamesForItem($id);
return new JSONResponse($data);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Material nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to get general material', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Create a general material item.
*
* POST /api/v1/inventory/general
*/
public function createGeneral(): JSONResponse {
try {
$data = $this->getRequestData();
if (empty($data['name'])) {
return new JSONResponse(['error' => 'Name ist erforderlich'], Http::STATUS_BAD_REQUEST);
}
$categoryIds = $data['categories'] ?? null;
unset($data['categories']);
$item = $this->inventoryService->createGeneralMaterial($data, $categoryIds);
$result = $item->jsonSerialize();
$result['categories'] = $this->inventoryService->getCategoryNamesForItem($item->getId());
return new JSONResponse($result, Http::STATUS_CREATED);
} catch (\Exception $e) {
$this->logger->error('Failed to create general material', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Update a general material item.
*
* PUT /api/v1/inventory/general/{id}
*/
public function updateGeneral(int $id): JSONResponse {
try {
$data = $this->getRequestData();
$categoryIds = $data['categories'] ?? null;
unset($data['categories']);
$item = $this->inventoryService->updateGeneralMaterial($id, $data, $categoryIds);
$result = $item->jsonSerialize();
$result['categories'] = $this->inventoryService->getCategoryNamesForItem($id);
return new JSONResponse($result);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Material nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to update general material', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Delete a general material item.
*
* DELETE /api/v1/inventory/general/{id}
*/
public function deleteGeneral(int $id): JSONResponse {
try {
$this->inventoryService->deleteGeneralMaterial($id);
return new JSONResponse(['status' => 'deleted']);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Material nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to delete general material', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
// ── Stock Items ─────────────────────────────────────────────────
/**
* List all stock items with their variants.
*
* GET /api/v1/inventory/stock
*/
#[NoCSRFRequired]
public function listStock(): JSONResponse {
try {
$items = $this->inventoryService->getStockItems();
$result = [];
foreach ($items as $item) {
$data = $item->jsonSerialize();
$data['categories'] = $this->inventoryService->getCategoryNamesForItem($item->getId());
$data['variants'] = array_map(
fn($v) => $v->jsonSerialize(),
$this->inventoryService->getVariantsByStockItem($item->getId())
);
// Calculate total amount from variants
$totalAmount = 0;
foreach ($data['variants'] as $v) {
$totalAmount += (int)($v['amount'] ?? 0);
}
$data['total_amount'] = $totalAmount;
// Calculate total minimum threshold
$minThreshold = 0;
foreach ($data['variants'] as $v) {
$minThreshold += (int)($v['min_threshold'] ?? 0);
}
$data['min_threshold'] = $minThreshold;
$result[] = $data;
}
return new JSONResponse(['data' => $result]);
} catch (\Exception $e) {
$this->logger->error('Failed to list stock items', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get a single stock item with its variants.
*
* GET /api/v1/inventory/stock/{id}
*/
#[NoCSRFRequired]
public function showStock(int $id): JSONResponse {
try {
$item = $this->inventoryService->getStockItemById($id);
$data = $item->jsonSerialize();
$data['categories'] = $this->inventoryService->getCategoryNamesForItem($id);
$data['variants'] = array_map(
fn($v) => $v->jsonSerialize(),
$this->inventoryService->getVariantsByStockItem($id)
);
return new JSONResponse($data);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Verkaufsmaterial nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to get stock item', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Create a stock item.
*
* POST /api/v1/inventory/stock
*/
public function createStock(): JSONResponse {
try {
$data = $this->getRequestData();
if (empty($data['name'])) {
return new JSONResponse(['error' => 'Name ist erforderlich'], Http::STATUS_BAD_REQUEST);
}
$categoryIds = $data['categories'] ?? null;
$variants = $data['variants'] ?? [];
unset($data['categories'], $data['variants']);
$item = $this->inventoryService->createStockItem($data, $categoryIds);
// Create variants if provided
foreach ($variants as $vData) {
$vData['stock_item_id'] = $item->getId();
$this->inventoryService->createStockVariant($vData);
}
$result = $item->jsonSerialize();
$result['categories'] = $this->inventoryService->getCategoryNamesForItem($item->getId());
return new JSONResponse($result, Http::STATUS_CREATED);
} catch (\Exception $e) {
$this->logger->error('Failed to create stock item', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Update a stock item.
*
* PUT /api/v1/inventory/stock/{id}
*/
public function updateStock(int $id): JSONResponse {
try {
$data = $this->getRequestData();
$categoryIds = $data['categories'] ?? null;
$variants = $data['variants'] ?? [];
unset($data['categories'], $data['variants']);
$item = $this->inventoryService->updateStockItem($id, $data, $categoryIds);
// Update/create/delete variants
foreach ($variants as $vData) {
if (isset($vData['id']) && is_numeric($vData['id'])) {
// Update existing variant
$this->inventoryService->updateStockVariant((int)$vData['id'], $vData);
} else {
// Create new variant
$vData['stock_item_id'] = $id;
$this->inventoryService->createStockVariant($vData);
}
}
// Delete variants that are no longer present
$existing = $this->inventoryService->getVariantsByStockItem($id);
$presentIds = array_map(fn($v) => (int)($v['id'] ?? 0), $variants);
foreach ($existing as $existingVariant) {
if (!in_array((int)$existingVariant->getId(), $presentIds, true)) {
$this->inventoryService->deleteStockVariant((int)$existingVariant->getId());
}
}
$result = $item->jsonSerialize();
$result['categories'] = $this->inventoryService->getCategoryNamesForItem($id);
return new JSONResponse($result);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Verkaufsmaterial nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to update stock item', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Delete a stock item.
*
* DELETE /api/v1/inventory/stock/{id}
*/
public function deleteStock(int $id): JSONResponse {
try {
$this->inventoryService->deleteStockItem($id);
return new JSONResponse(['status' => 'deleted']);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Verkaufsmaterial nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to delete stock item', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
// ── Variants ────────────────────────────────────────────────────
/**
* Create a variant (standalone endpoint).
*
* POST /api/v1/inventory/variants
*/
public function createVariant(): JSONResponse {
try {
$data = $this->getRequestData();
$variant = $this->inventoryService->createStockVariant($data);
return new JSONResponse($variant->jsonSerialize(), Http::STATUS_CREATED);
} catch (\Exception $e) {
$this->logger->error('Failed to create stock variant', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Update a variant.
*
* PUT /api/v1/inventory/variants/{id}
*/
public function updateVariant(int $id): JSONResponse {
try {
$data = $this->getRequestData();
$variant = $this->inventoryService->updateStockVariant($id, $data);
return new JSONResponse($variant->jsonSerialize());
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Variante nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to update stock variant', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Delete a variant.
*
* DELETE /api/v1/inventory/variants/{id}
*/
public function deleteVariant(int $id): JSONResponse {
try {
$this->inventoryService->deleteStockVariant($id);
return new JSONResponse(['status' => 'deleted']);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Variante nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to delete stock variant', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
// ── Sales ───────────────────────────────────────────────────────
/**
* List all sales.
*
* GET /api/v1/inventory/sales
*/
#[NoCSRFRequired]
public function listSales(): JSONResponse {
try {
$from = $this->request->getParam('dateFrom');
$to = $this->request->getParam('dateTo');
$sales = $this->saleService->getSalesByDateRange($from, $to);
return new JSONResponse(['data' => array_map(fn($s) => $s->jsonSerialize(), $sales)]);
} catch (\Exception $e) {
$this->logger->error('Failed to list sales', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get a single sale.
*
* GET /api/v1/inventory/sales/{id}
*/
#[NoCSRFRequired]
public function showSale(int $id): JSONResponse {
try {
$sale = $this->saleService->getSaleById($id);
return new JSONResponse($sale->jsonSerialize());
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Verkauf nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to get sale', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Create a sale record.
*
* POST /api/v1/inventory/sales
*/
public function createSale(): JSONResponse {
try {
$data = $this->getRequestData();
$sale = $this->saleService->createSale($data);
return new JSONResponse($sale->jsonSerialize(), Http::STATUS_CREATED);
} catch (\Exception $e) {
$this->logger->error('Failed to create sale', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Delete a sale record.
*
* DELETE /api/v1/inventory/sales/{id}
*/
public function deleteSale(int $id): JSONResponse {
try {
$this->saleService->deleteSale($id);
return new JSONResponse(['status' => 'deleted']);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Verkauf nicht gefunden'], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Failed to delete sale', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
}
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Controller;
use OCA\Mitgliederverwaltung\Service\InventoryReportService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
/**
* REST API controller for inventory reports.
*
* Part of Issue #165 (Inventory Tracking).
*/
class InventoryReportController extends ApiController {
private InventoryReportService $reportService;
private LoggerInterface $logger;
public function __construct(
string $appName,
IRequest $request,
InventoryReportService $reportService,
LoggerInterface $logger
) {
parent::__construct($appName, $request);
$this->reportService = $reportService;
$this->logger = $logger;
}
/**
* Get condition report data.
*
* GET /api/v1/inventory/reports/condition
*/
#[NoCSRFRequired]
public function condition(): JSONResponse {
try {
$report = $this->reportService->generateConditionReport();
$report['title'] = 'Materialzustand';
return new JSONResponse($report);
} catch (\Exception $e) {
$this->logger->error('Failed to generate condition report', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get sales report data.
*
* GET /api/v1/inventory/reports/sales?dateFrom=2026-01-01&dateTo=2026-03-31
*/
#[NoCSRFRequired]
public function sales(): JSONResponse {
try {
$dateFrom = $this->request->getParam('dateFrom');
$dateTo = $this->request->getParam('dateTo');
$report = $this->reportService->generateSalesReport($dateFrom, $dateTo);
return new JSONResponse($report);
} catch (\Exception $e) {
$this->logger->error('Failed to generate sales report', ['exception' => $e, 'app' => 'mitgliederverwaltung']);
return new JSONResponse(['error' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
}
+11
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Controller;
use OCA\Mitgliederverwaltung\Service\EncryptedExportService;
use OCA\Mitgliederverwaltung\Service\InventoryReportService;
use OCA\Mitgliederverwaltung\Service\PdfService;
use OCA\Mitgliederverwaltung\Service\PermissionService;
use OCA\Mitgliederverwaltung\Service\ReportService;
@@ -31,6 +32,7 @@ use Psr\Log\LoggerInterface;
class ReportController extends ApiController {
private ReportService $reportService;
private InventoryReportService $inventoryReportService;
private PdfService $pdfService;
private EncryptedExportService $encryptedService;
private PermissionService $permissionService;
@@ -41,6 +43,7 @@ class ReportController extends ApiController {
string $appName,
IRequest $request,
ReportService $reportService,
InventoryReportService $inventoryReportService,
PdfService $pdfService,
EncryptedExportService $encryptedService,
PermissionService $permissionService,
@@ -49,6 +52,7 @@ class ReportController extends ApiController {
) {
parent::__construct($appName, $request);
$this->reportService = $reportService;
$this->inventoryReportService = $inventoryReportService;
$this->pdfService = $pdfService;
$this->encryptedService = $encryptedService;
$this->permissionService = $permissionService;
@@ -75,6 +79,8 @@ class ReportController extends ApiController {
['id' => 'familienliste', 'name' => 'Familienliste', 'params' => []],
['id' => 'lagerhistorie', 'name' => 'Lagerhistorie', 'params' => ['memberId']],
['id' => 'verletzungsprotokoll', 'name' => 'Verletzungsprotokoll', 'params' => ['dateFrom', 'dateTo', 'memberId']],
['id' => 'inventur-verkaeufe', 'name' => 'Inventur-Verkaeufe', 'params' => ['dateFrom', 'dateTo']],
['id' => 'materialzustand', 'name' => 'Materialzustand', 'params' => []],
],
]);
}
@@ -251,6 +257,11 @@ class ReportController extends ApiController {
? (int)$this->request->getParam('memberId')
: null
),
'inventur-verkaeufe' => $this->inventoryReportService->generateSalesReport(
$this->request->getParam('dateFrom') ?: null,
$this->request->getParam('dateTo') ?: null
),
'materialzustand' => $this->inventoryReportService->generateConditionReport(),
default => null,
};
}
+52
View File
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
/**
* General material entity mapping all columns from oc_mv_inventory_general_material.
*
* Part of Issue #165 (Inventory Tracking).
*
* @method int|null getId()
* @method void setId(int $id)
* @method string getName()
* @method void setName(string $name)
* @method int|null getCondition()
* @method void setCondition(?int $condition)
* @method string|null getNotes()
* @method void setNotes(?string $notes)
* @method string getCreatedAt()
* @method void setCreatedAt(string $createdAt)
* @method string getUpdatedAt()
* @method void setUpdatedAt(string $updatedAt)
*/
class GeneralMaterial extends Entity implements JsonSerializable {
protected int $id = 0;
protected string $name = '';
protected ?int $condition = null;
protected ?string $notes = null;
protected string $createdAt = '';
protected string $updatedAt = '';
public function __construct() {
$this->addType('id', 'integer');
$this->addType('condition', 'integer');
}
public function jsonSerialize(): array {
return [
'id' => $this->id,
'name' => $this->name,
'condition' => $this->condition,
'notes' => $this->notes,
'created_at' => $this->createdAt,
'updated_at' => $this->updatedAt,
];
}
}
+93
View File
@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* Mapper for the oc_mv_inventory_general_material table.
*
* Part of Issue #165 (Inventory Tracking).
*
* @extends QBMapper<GeneralMaterial>
*/
class GeneralMaterialMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'mv_inventory_general_material', GeneralMaterial::class);
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function findById(int $id): GeneralMaterial {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
return $this->findEntity($qb);
}
/**
* Find all general material items.
*
* @return GeneralMaterial[]
* @throws Exception
*/
public function findAll(): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->orderBy('name', 'ASC');
return $this->findEntities($qb);
}
/**
* Find items whose condition falls within the given range.
*
* @param int|null $min Minimum condition (inclusive), null = unbounded
* @param int|null $max Maximum condition (inclusive), null = unbounded
* @return GeneralMaterial[]
* @throws Exception
*/
public function findByConditionRange(?int $min = null, ?int $max = null): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->isNotNull('condition'));
if ($min !== null) {
$qb->andWhere($qb->expr()->gte('condition', $qb->createNamedParameter($min, IQueryBuilder::PARAM_INT)));
}
if ($max !== null) {
$qb->andWhere($qb->expr()->lte('condition', $qb->createNamedParameter($max, IQueryBuilder::PARAM_INT)));
}
return $this->findEntities($qb);
}
/**
* Find items needing repair (condition ≤ 2 or NULL).
*
* @return GeneralMaterial[]
* @throws Exception
*/
public function findNeedingRepair(): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->lte('condition', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT)))
->orWhere($qb->expr()->isNull('condition'));
return $this->findEntities($qb);
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
/**
* Category entity mapping all columns from oc_mv_inventory_categories.
*
* Part of Issue #165 (Inventory Tracking).
*
* @method int|null getId()
* @method void setId(int $id)
* @method string getName()
* @method void setName(string $name)
* @method string|null getCreatedAt()
* @method void setCreatedAt(string $createdAt)
*/
class InventoryCategory extends Entity implements JsonSerializable {
protected int $id = 0;
protected string $name = '';
protected ?string $createdAt = null;
public function __construct() {
$this->addType('id', 'integer');
}
public function jsonSerialize(): array {
return [
'id' => $this->id,
'name' => $this->name,
'created_at' => $this->createdAt,
];
}
}
+70
View File
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* Mapper for the oc_mv_inventory_categories table.
*
* Part of Issue #165 (Inventory Tracking).
*
* @extends QBMapper<InventoryCategory>
*/
class InventoryCategoryMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'mv_inventory_categories', InventoryCategory::class);
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function findById(int $id): InventoryCategory {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
return $this->findEntity($qb);
}
/**
* Find a category by name.
*
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws Exception
*/
public function findByName(string $name): InventoryCategory {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('name', $qb->createNamedParameter($name, IQueryBuilder::PARAM_STR)));
return $this->findEntity($qb);
}
/**
* Find all categories.
*
* @return InventoryCategory[]
* @throws Exception
*/
public function findAll(): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->orderBy('name', 'ASC');
return $this->findEntities($qb);
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
/**
* Join entity linking a general material item to a category.
*
* Part of Issue #165 (Inventory Tracking).
*
* @method int|null getItemId()
* @method void setItemId(int $itemId)
* @method int|null getCategoryId()
* @method void setCategoryId(int $categoryId)
*/
class InventoryItemCategory extends Entity implements JsonSerializable {
protected int $itemId = 0;
protected int $categoryId = 0;
public function __construct() {
$this->addType('itemId', 'integer');
$this->addType('categoryId', 'integer');
}
public function jsonSerialize(): array {
return [
'item_id' => $this->itemId,
'category_id' => $this->categoryId,
];
}
}
+67
View File
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* Mapper for the oc_mv_inventory_item_categories join table.
*
* Part of Issue #165 (Inventory Tracking).
*
* @extends QBMapper<InventoryItemCategory>
*/
class InventoryItemCategoryMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'mv_inventory_item_categories', InventoryItemCategory::class);
}
/**
* Find all categories linked to a given item.
*
* @return InventoryItemCategory[]
* @throws Exception
*/
public function findByItemId(int $itemId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('item_id', $qb->createNamedParameter($itemId, IQueryBuilder::PARAM_INT)));
return $this->findEntities($qb);
}
/**
* Attach a category to an item.
*
* @throws Exception
*/
public function attach(int $itemId, int $categoryId): void {
$qb = $this->db->getQueryBuilder();
$qb->insert($this->getTableName())
->values([
'item_id' => $qb->createNamedParameter($itemId, IQueryBuilder::PARAM_INT),
'category_id' => $qb->createNamedParameter($categoryId, IQueryBuilder::PARAM_INT),
]);
$qb->executeStatement();
}
/**
* Detach a category from an item.
*
* @throws Exception
*/
public function detach(int $itemId, int $categoryId): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where($qb->expr()->eq('item_id', $qb->createNamedParameter($itemId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('category_id', $qb->createNamedParameter($categoryId, IQueryBuilder::PARAM_INT)));
$qb->executeStatement();
}
}
+66
View File
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
/**
* Sale record entity mapping all columns from oc_mv_inventory_sales.
*
* Part of Issue #165 (Inventory Tracking).
*
* @method int|null getId()
* @method void setId(int $id)
* @method int|null getStockItemId()
* @method void setStockItemId(?int $stockItemId)
* @method int|null getVariantId()
* @method void setVariantId(?int $variantId)
* @method string getDate()
* @method void setDate(string $date)
* @method int getQuantity()
* @method void setQuantity(int $quantity)
* @method string getUnitPrice()
* @method void setUnitPrice(string $unitPrice)
* @method string getTotalPrice()
* @method void setTotalPrice(string $totalPrice)
* @method string|null getNotes()
* @method void setNotes(?string $notes)
* @method string getCreatedAt()
* @method void setCreatedAt(string $createdAt)
*/
class SaleRecord extends Entity implements JsonSerializable {
protected int $id = 0;
protected ?int $stockItemId = null;
protected ?int $variantId = null;
protected string $date = '';
protected int $quantity = 0;
protected string $unitPrice = '0.00';
protected string $totalPrice = '0.00';
protected ?string $notes = null;
protected string $createdAt = '';
public function __construct() {
$this->addType('id', 'integer');
$this->addType('stockItemId', 'integer');
$this->addType('variantId', 'integer');
$this->addType('quantity', 'integer');
}
public function jsonSerialize(): array {
return [
'id' => $this->id,
'stock_item_id' => $this->stockItemId,
'variant_id' => $this->variantId,
'date' => $this->date,
'quantity' => $this->quantity,
'unit_price' => $this->unitPrice,
'total_price' => $this->totalPrice,
'notes' => $this->notes,
'created_at' => $this->createdAt,
];
}
}
+106
View File
@@ -0,0 +1,106 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* Mapper for the oc_mv_inventory_sales table.
*
* Part of Issue #165 (Inventory Tracking).
*
* @extends QBMapper<SaleRecord>
*/
class SaleRecordMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'mv_inventory_sales', SaleRecord::class);
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function findById(int $id): SaleRecord {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
return $this->findEntity($qb);
}
/**
* Find all sale records.
*
* @return SaleRecord[]
* @throws Exception
*/
public function findAll(): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->orderBy('date', 'DESC');
return $this->findEntities($qb);
}
/**
* Find sale records within a date range.
*
* @param string|null $from Start date (inclusive, YYYY-MM-DD)
* @param string|null $to End date (inclusive, YYYY-MM-DD)
* @return SaleRecord[]
* @throws Exception
*/
public function findByDateRange(?string $from = null, ?string $to = null): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->gte('date', $qb->createNamedParameter($from ?? '1970-01-01', IQueryBuilder::PARAM_STR)))
->addOrderBy('date', 'DESC');
if ($to !== null) {
// Override the first where (set up by gte)
$conditions = $qb->expr()->gte('date', $qb->createNamedParameter($from ?? '1970-01-01', IQueryBuilder::PARAM_STR));
$conditions2 = $qb->expr()->lte('date', $qb->createNamedParameter($to, IQueryBuilder::PARAM_STR));
$qb2 = $this->db->getQueryBuilder();
$qb2->select('*')
->from($this->getTableName())
->where($conditions2)
->andWhere($conditions);
return $this->findEntities($qb2);
}
return $this->findEntities($qb);
}
/**
* Count sale records within a date range.
*
* @throws Exception
*/
public function countByDateRange(?string $from = null, ?string $to = null): int {
$qb = $this->db->getQueryBuilder();
$qb->select($qb->createFunction('COUNT(*)'))
->from($this->getTableName())
->where($qb->expr()->gte('date', $qb->createNamedParameter($from ?? '1970-01-01', IQueryBuilder::PARAM_STR)));
if ($to !== null) {
$qb->andWhere($qb->expr()->lte('date', $qb->createNamedParameter($to, IQueryBuilder::PARAM_STR)));
}
$result = $qb->executeQuery();
$count = (int)$result->fetchOne();
$result->closeCursor();
return $count;
}
}
+47
View File
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
/**
* Stock item entity mapping all columns from oc_mv_inventory_stock_items.
*
* Part of Issue #165 (Inventory Tracking).
*
* @method int|null getId()
* @method void setId(int $id)
* @method string getName()
* @method void setName(string $name)
* @method string|null getProviderUrlsJson()
* @method void setProviderUrlsJson(?string $providerUrlsJson)
* @method string getCreatedAt()
* @method void setCreatedAt(string $createdAt)
* @method string getUpdatedAt()
* @method void setUpdatedAt(string $updatedAt)
*/
class StockItem extends Entity implements JsonSerializable {
protected int $id = 0;
protected string $name = '';
protected ?string $providerUrlsJson = null;
protected string $createdAt = '';
protected string $updatedAt = '';
public function __construct() {
$this->addType('id', 'integer');
}
public function jsonSerialize(): array {
return [
'id' => $this->id,
'name' => $this->name,
'provider_urls_json' => $this->providerUrlsJson,
'created_at' => $this->createdAt,
'updated_at' => $this->updatedAt,
];
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* Mapper for the oc_mv_inventory_stock_items table.
*
* Part of Issue #165 (Inventory Tracking).
*
* @extends QBMapper<StockItem>
*/
class StockItemMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'mv_inventory_stock_items', StockItem::class);
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function findById(int $id): StockItem {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
return $this->findEntity($qb);
}
/**
* Find all stock items.
*
* @return StockItem[]
* @throws Exception
*/
public function findAll(): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->orderBy('name', 'ASC');
return $this->findEntities($qb);
}
}
+54
View File
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
/**
* Stock variant entity mapping all columns from oc_mv_inventory_stock_variants.
*
* Part of Issue #165 (Inventory Tracking).
*
* @method int|null getId()
* @method void setId(int $id)
* @method int getStockItemId()
* @method void setStockItemId(int $stockItemId)
* @method string getLabel()
* @method void setLabel(string $label)
* @method int getAmount()
* @method void setAmount(int $amount)
* @method string getCost()
* @method void setCost(string $cost)
* @method int getMinThreshold()
* @method void setMinThreshold(int $minThreshold)
*/
class StockVariant extends Entity implements JsonSerializable {
protected int $id = 0;
protected int $stockItemId = 0;
protected string $label = '';
protected int $amount = 0;
protected string $cost = '0.00';
protected int $minThreshold = 0;
public function __construct() {
$this->addType('id', 'integer');
$this->addType('stockItemId', 'integer');
$this->addType('amount', 'integer');
$this->addType('minThreshold', 'integer');
}
public function jsonSerialize(): array {
return [
'id' => $this->id,
'stock_item_id' => $this->stockItemId,
'label' => $this->label,
'amount' => $this->amount,
'cost' => $this->cost,
'min_threshold' => $this->minThreshold,
];
}
}
+54
View File
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* Mapper for the oc_mv_inventory_stock_variants table.
*
* Part of Issue #165 (Inventory Tracking).
*
* @extends QBMapper<StockVariant>
*/
class StockVariantMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'mv_inventory_stock_variants', StockVariant::class);
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function findById(int $id): StockVariant {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
return $this->findEntity($qb);
}
/**
* Find all variants for a given stock item.
*
* @return StockVariant[]
* @throws Exception
*/
public function findByStockItemId(int $stockItemId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('stock_item_id', $qb->createNamedParameter($stockItemId, IQueryBuilder::PARAM_INT)))
->orderBy('label', 'ASC');
return $this->findEntities($qb);
}
}
@@ -0,0 +1,257 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Migration: Create inventory tracking tables.
*
* Creates the following tables:
* - mv_inventory_categories — Category registry
* - mv_inventory_item_categories — Join table (item ↔ category)
* - mv_inventory_general_material — General material items
* - mv_inventory_stock_items — Stock / sales items
* - mv_inventory_stock_variants — Variant options per stock item
* - mv_inventory_sales — Sale records (audit trail)
*/
class Version000018Date20260421000000 extends SimpleMigrationStep {
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
// ── mv_inventory_categories (no FKs, create first) ───────────
if (!$schema->hasTable('mv_inventory_categories')) {
$table = $schema->createTable('mv_inventory_categories');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
]);
$table->addColumn('name', 'string', [
'notnull' => true,
'length' => 100,
'default' => '',
]);
$table->addColumn('created_at', 'datetime', [
'notnull' => false,
'default' => null,
]);
$table->setPrimaryKey(['id']);
$table->addUniqueIndex(['name'], 'inventory_cat_name_uindex');
} else {
$table = $schema->getTable('mv_inventory_categories');
if (!$table->hasColumn('created_at')) {
$table->addColumn('created_at', 'datetime', [
'notnull' => false,
'default' => null,
]);
}
if (!$table->hasIndex('inventory_cat_name_uindex')) {
$table->addUniqueIndex(['name'], 'inventory_cat_name_uindex');
}
}
// ── mv_inventory_general_material (referenced by item_categories) ──
if (!$schema->hasTable('mv_inventory_general_material')) {
$table = $schema->createTable('mv_inventory_general_material');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
]);
$table->addColumn('name', 'string', [
'notnull' => true,
'length' => 255,
'default' => '',
]);
$table->addColumn('condition', Types::INTEGER, [
'notnull' => false,
'default' => null,
'unsigned' => true,
]);
$table->addColumn('notes', 'text', [
'notnull' => false,
'default' => null,
'length' => 65535,
]);
$table->addColumn('created_at', 'datetime', [
'notnull' => true,
'default' => '1970-01-01 00:00:00',
]);
$table->addColumn('updated_at', 'datetime', [
'notnull' => true,
'default' => '1970-01-01 00:00:00',
]);
$table->setPrimaryKey(['id']);
}
// ── mv_inventory_stock_items (referenced by stock_variants & sales) ──
if (!$schema->hasTable('mv_inventory_stock_items')) {
$table = $schema->createTable('mv_inventory_stock_items');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
]);
$table->addColumn('name', 'string', [
'notnull' => true,
'length' => 255,
'default' => '',
]);
$table->addColumn('provider_urls_json', 'text', [
'notnull' => false,
'default' => null,
'length' => 65535,
]);
$table->addColumn('created_at', 'datetime', [
'notnull' => true,
'default' => '1970-01-01 00:00:00',
]);
$table->addColumn('updated_at', 'datetime', [
'notnull' => true,
'default' => '1970-01-01 00:00:00',
]);
$table->setPrimaryKey(['id']);
}
// ── mv_inventory_stock_variants (FK → stock_items) ───────────
if (!$schema->hasTable('mv_inventory_stock_variants')) {
$table = $schema->createTable('mv_inventory_stock_variants');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
]);
$table->addColumn('stock_item_id', 'bigint', [
'notnull' => true,
'length' => 8,
'default' => 0,
]);
$table->addColumn('label', 'string', [
'notnull' => true,
'length' => 100,
'default' => '',
]);
$table->addColumn('amount', Types::INTEGER, [
'notnull' => true,
'default' => 0,
'unsigned' => true,
]);
$table->addColumn('cost', 'decimal', [
'notnull' => true,
'default' => '0.00',
'precision' => 10,
'scale' => 2,
]);
$table->addColumn('min_threshold', Types::INTEGER, [
'notnull' => true,
'default' => 0,
'unsigned' => true,
]);
$table->setPrimaryKey(['id']);
$table->addForeignKeyConstraint(
$schema->getTable('mv_inventory_stock_items'),
['stock_item_id'],
['id'],
['onDelete' => 'CASCADE']
);
}
// ── mv_inventory_item_categories (FK → general_material, categories) ──
if (!$schema->hasTable('mv_inventory_item_categories')) {
$table = $schema->createTable('mv_inventory_item_categories');
$table->addColumn('item_id', 'bigint', [
'notnull' => true,
'length' => 8,
'default' => 0,
]);
$table->addColumn('category_id', 'bigint', [
'notnull' => true,
'length' => 8,
'default' => 0,
]);
$table->setPrimaryKey(['item_id', 'category_id']);
$table->addForeignKeyConstraint(
$schema->getTable('mv_inventory_general_material'),
['item_id'],
['id'],
['onDelete' => 'CASCADE']
);
$table->addForeignKeyConstraint(
$schema->getTable('mv_inventory_categories'),
['category_id'],
['id'],
['onDelete' => 'CASCADE']
);
}
// ── mv_inventory_sales (FK → stock_items, stock_variants) ────
if (!$schema->hasTable('mv_inventory_sales')) {
$table = $schema->createTable('mv_inventory_sales');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
]);
$table->addColumn('stock_item_id', 'bigint', [
'notnull' => false,
'length' => 8,
'default' => null,
]);
$table->addColumn('variant_id', 'bigint', [
'notnull' => false,
'length' => 8,
'default' => null,
]);
$table->addColumn('date', 'date', [
'notnull' => true,
'default' => '1970-01-01',
]);
$table->addColumn('quantity', Types::INTEGER, [
'notnull' => true,
'default' => 0,
'unsigned' => true,
]);
$table->addColumn('unit_price', 'decimal', [
'notnull' => true,
'default' => '0.00',
'precision' => 10,
'scale' => 2,
]);
$table->addColumn('total_price', 'decimal', [
'notnull' => true,
'default' => '0.00',
'precision' => 10,
'scale' => 2,
]);
$table->addColumn('notes', 'text', [
'notnull' => false,
'default' => null,
'length' => 65535,
]);
$table->addColumn('created_at', 'datetime', [
'notnull' => true,
'default' => '1970-01-01 00:00:00',
]);
$table->setPrimaryKey(['id']);
if ($schema->hasTable('mv_inventory_stock_items')) {
$stockTable = $schema->getTable('mv_inventory_stock_items');
$table->addForeignKeyConstraint($stockTable, ['stock_item_id'], ['id'], ['onDelete' => 'SET NULL']);
}
if ($schema->hasTable('mv_inventory_stock_variants')) {
$varTable = $schema->getTable('mv_inventory_stock_variants');
$table->addForeignKeyConstraint($varTable, ['variant_id'], ['id'], ['onDelete' => 'SET NULL']);
}
}
return $schema;
}
}
+171
View File
@@ -0,0 +1,171 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Service;
use OCA\Mitgliederverwaltung\Db\GeneralMaterialMapper;
use OCA\Mitgliederverwaltung\Db\InventoryCategoryMapper;
use OCA\Mitgliederverwaltung\Db\InventoryItemCategoryMapper;
use OCA\Mitgliederverwaltung\Db\StockItemMapper;
use OCA\Mitgliederverwaltung\Db\StockVariantMapper;
use OCA\Mitgliederverwaltung\Db\SaleRecordMapper;
use Psr\Log\LoggerInterface;
/**
* Service for generating inventory reports.
*
* Part of Issue #165 (Inventory Tracking).
*/
class InventoryReportService {
private GeneralMaterialMapper $generalMaterialMapper;
private InventoryCategoryMapper $categoryMapper;
private InventoryItemCategoryMapper $itemCategoryMapper;
private StockItemMapper $stockItemMapper;
private StockVariantMapper $stockVariantMapper;
private SaleRecordMapper $saleRecordMapper;
private LoggerInterface $logger;
public function __construct(
GeneralMaterialMapper $generalMaterialMapper,
InventoryCategoryMapper $categoryMapper,
InventoryItemCategoryMapper $itemCategoryMapper,
StockItemMapper $stockItemMapper,
StockVariantMapper $stockVariantMapper,
SaleRecordMapper $saleRecordMapper,
LoggerInterface $logger
) {
$this->generalMaterialMapper = $generalMaterialMapper;
$this->categoryMapper = $categoryMapper;
$this->itemCategoryMapper = $itemCategoryMapper;
$this->stockItemMapper = $stockItemMapper;
$this->stockVariantMapper = $stockVariantMapper;
$this->saleRecordMapper = $saleRecordMapper;
$this->logger = $logger;
}
/**
* Generate condition report data.
*
* @return array{summary: array, rows: array, headers: string[]}
*/
public function generateConditionReport(): array {
$items = $this->generalMaterialMapper->findAll();
// Build summary by category
$summary = [];
foreach ($items as $item) {
$catIds = $this->itemCategoryMapper->findByItemId($item->getId());
if (count($catIds) === 0) {
$catName = '(keine Kategorie)';
} else {
$catNames = [];
foreach ($catIds as $cat) {
try {
$c = $this->categoryMapper->findById((int)$cat->getCategoryId());
$catNames[] = $c->getName();
} catch (\Exception $e) {
// ignore
}
}
$catName = implode(', ', $catNames);
}
if (!isset($summary[$catName])) {
$summary[$catName] = 0;
}
$summary[$catName] += 1;
}
// Build detail rows for items needing repair (condition ≤ 2 or NULL)
$rows = [];
$needingRepair = $this->generalMaterialMapper->findNeedingRepair();
foreach ($needingRepair as $item) {
$catIds = $this->itemCategoryMapper->findByItemId($item->getId());
$catNames = [];
foreach ($catIds as $cat) {
try {
$c = $this->categoryMapper->findById((int)$cat->getCategoryId());
$catNames[] = $c->getName();
} catch (\Exception $e) {
// ignore
}
}
$conditionLabel = $item->getCondition() === null
? 'Nicht bewertet'
: $item->getCondition() . '/5';
$rows[] = [
'name' => $item->getName(),
'condition' => $conditionLabel,
'categories' => implode(', ', $catNames) ?: '',
'notes' => $item->getNotes() ?? '',
];
}
return [
'summary' => $summary,
'rows' => $rows,
'headers' => ['Artikel', 'Zustand', 'Kategorie', 'Notizen'],
];
}
/**
* Generate sales report data.
*
* @param string|null $dateFrom Start date (YYYY-MM-DD)
* @param string|null $dateTo End date (YYYY-MM-DD)
* @return array{title: string, summary: array, rows: array, headers: string[]}
*/
public function generateSalesReport(?string $dateFrom = null, ?string $dateTo = null): array {
$sales = $this->saleRecordMapper->findByDateRange($dateFrom, $dateTo);
$rows = [];
$totalRevenue = 0.0;
foreach ($sales as $sale) {
$itemName = 'Unbekannt';
try {
$stockItem = $this->stockItemMapper->findById((int)$sale->getStockItemId());
$itemName = $stockItem->getName();
} catch (\Exception $e) {
// ignore, keep default name
}
$variantName = '';
if ($sale->getVariantId() !== null) {
try {
$variant = $this->stockVariantMapper->findById((int)$sale->getVariantId());
$variantName = ' ' . $variant->getLabel();
} catch (\Exception $e) {
// ignore
}
}
$totalRevenue += (float)$sale->getTotalPrice();
$rows[] = [
'date' => $sale->getDate(),
'item' => $itemName . $variantName,
'quantity' => (string)$sale->getQuantity(),
'unit_price' => number_format((float)$sale->getUnitPrice(), 2, ',', '.') . ' €',
'total_price' => number_format((float)$sale->getTotalPrice(), 2, ',', '.') . ' €',
];
}
$title = 'Inventur-Verkäufe';
if ($dateFrom !== null || $dateTo !== null) {
$title .= ' (' . ($dateFrom ?? 'Anfang') . ' ' . ($dateTo ?? 'Ende') . ')';
}
return [
'title' => $title,
'summary' => [
'total_sales' => count($sales),
'total_revenue' => number_format($totalRevenue, 2, ',', '.') . ' €',
],
'rows' => $rows,
'headers' => ['Datum', 'Artikel', 'Menge', 'Stückpreis', 'Gesamtsumme'],
];
}
}
+486
View File
@@ -0,0 +1,486 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Service;
use DateTime;
use OCA\Mitgliederverwaltung\Db\GeneralMaterial;
use OCA\Mitgliederverwaltung\Db\GeneralMaterialMapper;
use OCA\Mitgliederverwaltung\Db\InventoryCategory;
use OCA\Mitgliederverwaltung\Db\InventoryCategoryMapper;
use OCA\Mitgliederverwaltung\Db\InventoryItemCategory;
use OCA\Mitgliederverwaltung\Db\InventoryItemCategoryMapper;
use OCA\Mitgliederverwaltung\Db\StockItem;
use OCA\Mitgliederverwaltung\Db\StockItemMapper;
use OCA\Mitgliederverwaltung\Db\StockVariant;
use OCA\Mitgliederverwaltung\Db\StockVariantMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\DB\Exception;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
/**
* Service layer for inventory categories, general material, and stock items.
*
* Part of Issue #165 (Inventory Tracking).
*/
class InventoryService {
private InventoryCategoryMapper $categoryMapper;
private InventoryItemCategoryMapper $itemCategoryMapper;
private GeneralMaterialMapper $generalMaterialMapper;
private StockItemMapper $stockItemMapper;
private StockVariantMapper $stockVariantMapper;
private AuditService $auditService;
private LoggerInterface $logger;
public function __construct(
InventoryCategoryMapper $categoryMapper,
InventoryItemCategoryMapper $itemCategoryMapper,
GeneralMaterialMapper $generalMaterialMapper,
StockItemMapper $stockItemMapper,
StockVariantMapper $stockVariantMapper,
AuditService $auditService,
LoggerInterface $logger
) {
$this->categoryMapper = $categoryMapper;
$this->itemCategoryMapper = $itemCategoryMapper;
$this->generalMaterialMapper = $generalMaterialMapper;
$this->stockItemMapper = $stockItemMapper;
$this->stockVariantMapper = $stockVariantMapper;
$this->auditService = $auditService;
$this->logger = $logger;
}
// ── Categories ────────────────────────────────────────────────────
/**
* @return InventoryCategory[]
* @throws Exception
*/
public function getCategories(): array {
return $this->categoryMapper->findAll();
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function getCategory(int $id): InventoryCategory {
return $this->categoryMapper->findById($id);
}
/**
* @throws Exception
*/
public function createCategory(array $data): InventoryCategory {
$category = new InventoryCategory();
$category->setName($data['name'] ?? '');
$category->setCreatedAt((new DateTime())->format('Y-m-d H:i:s'));
$this->categoryMapper->insert($category);
$this->auditService->logCreate([
'name' => $category->getName(),
], 'inventory_category', $category->getId());
return $category;
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function updateCategory(int $id, array $data): InventoryCategory {
$category = $this->categoryMapper->findById($id);
$oldData = ['name' => $category->getName()];
if (isset($data['name'])) {
$category->setName($data['name']);
}
$this->categoryMapper->update($category);
$this->auditService->logUpdate($oldData, [
'name' => $category->getName(),
], 'inventory_category', $id);
return $category;
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function deleteCategory(int $id): void {
$category = $this->categoryMapper->findById($id);
$this->categoryMapper->delete($category);
$this->auditService->logDelete('inventory_category', $id);
}
// ── General Material ──────────────────────────────────────────────
/**
* @return GeneralMaterial[]
* @throws Exception
*/
public function getGeneralMaterial(): array {
return $this->generalMaterialMapper->findAll();
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function getGeneralMaterialById(int $id): GeneralMaterial {
return $this->generalMaterialMapper->findById($id);
}
/**
* @throws Exception
*/
public function createGeneralMaterial(array $data, ?array $categoryIds = null): GeneralMaterial {
$now = (new DateTime())->format('Y-m-d H:i:s');
$item = new GeneralMaterial();
$item->setName($data['name'] ?? '');
$item->setCondition($data['condition'] ?? null);
$item->setNotes($data['notes'] ?? null);
$item->setCreatedAt($now);
$item->setUpdatedAt($now);
$this->generalMaterialMapper->insert($item);
// Attach categories
if ($categoryIds !== null && is_array($categoryIds)) {
foreach ($categoryIds as $catId) {
try {
$this->itemCategoryMapper->attach($item->getId(), (int)$catId);
} catch (\Exception $e) {
$this->logger->warning('Failed to attach category {catId} to item {itemId}', [
'catId' => $catId,
'itemId' => $item->getId(),
'app' => 'mitgliederverwaltung',
]);
}
}
}
$this->auditService->logCreate([
'name' => $item->getName(),
'condition' => $item->getCondition(),
'notes' => $item->getNotes(),
], 'inventory_general_material', $item->getId());
return $item;
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function updateGeneralMaterial(int $id, array $data, ?array $categoryIds = null): GeneralMaterial {
$item = $this->generalMaterialMapper->findById($id);
$oldData = [
'name' => $item->getName(),
'condition' => $item->getCondition(),
'notes' => $item->getNotes(),
];
if (isset($data['name'])) {
$item->setName($data['name']);
}
if (array_key_exists('condition', $data)) {
$item->setCondition($data['condition'] === null ? null : (int)$data['condition']);
}
if (array_key_exists('notes', $data)) {
$item->setNotes($data['notes'] === null ? null : (string)$data['notes']);
}
$item->setUpdatedAt((new DateTime())->format('Y-m-d H:i:s'));
$this->generalMaterialMapper->update($item);
// Sync categories if provided
if ($categoryIds !== null && is_array($categoryIds)) {
// Detach all existing categories and re-attach
$existing = $this->itemCategoryMapper->findByItemId($id);
foreach ($existing as $existingCat) {
$this->itemCategoryMapper->detach($id, (int)$existingCat->getCategoryId());
}
foreach ($categoryIds as $catId) {
try {
$this->itemCategoryMapper->attach($id, (int)$catId);
} catch (\Exception $e) {
$this->logger->warning('Failed to attach category {catId} to item {itemId}', [
'catId' => $catId,
'itemId' => $id,
'app' => 'mitgliederverwaltung',
]);
}
}
}
$this->auditService->logUpdate($oldData, [
'name' => $item->getName(),
'condition' => $item->getCondition(),
'notes' => $item->getNotes(),
], 'inventory_general_material', $id);
return $item;
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function deleteGeneralMaterial(int $id): void {
$item = $this->generalMaterialMapper->findById($id);
$this->generalMaterialMapper->delete($item);
$this->auditService->logDelete('inventory_general_material', $id);
}
/**
* Get items needing repair (condition ≤ 2 or NULL).
*
* @return GeneralMaterial[]
* @throws Exception
*/
public function getNeedingRepair(): array {
return $this->generalMaterialMapper->findNeedingRepair();
}
/**
* @return GeneralMaterial[]
* @throws Exception
*/
public function getGeneralMaterialByConditionRange(?int $min, ?int $max): array {
return $this->generalMaterialMapper->findByConditionRange($min, $max);
}
/**
* Get category names for an item.
*
* @return string[]
* @throws Exception
*/
public function getCategoryNamesForItem(int $itemId): array {
$cats = $this->itemCategoryMapper->findByItemId($itemId);
$names = [];
foreach ($cats as $cat) {
try {
$c = $this->categoryMapper->findById((int)$cat->getCategoryId());
$names[] = $c->getName();
} catch (\Exception $e) {
// ignore
}
}
return $names;
}
// ── Stock Items ───────────────────────────────────────────────────
/**
* @return StockItem[]
* @throws Exception
*/
public function getStockItems(): array {
return $this->stockItemMapper->findAll();
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function getStockItemById(int $id): StockItem {
return $this->stockItemMapper->findById($id);
}
/**
* @throws Exception
*/
public function createStockItem(array $data, ?array $categoryIds = null): StockItem {
$now = (new DateTime())->format('Y-m-d H:i:s');
$item = new StockItem();
$item->setName($data['name'] ?? '');
$item->setProviderUrlsJson($data['provider_urls_json'] ?? null);
$item->setCreatedAt($now);
$item->setUpdatedAt($now);
$this->stockItemMapper->insert($item);
// Attach categories
if ($categoryIds !== null && is_array($categoryIds)) {
foreach ($categoryIds as $catId) {
try {
$this->itemCategoryMapper->attach($item->getId(), (int)$catId);
} catch (\Exception $e) {
$this->logger->warning('Failed to attach category {catId} to stock item {itemId}', [
'catId' => $catId,
'itemId' => $item->getId(),
'app' => 'mitgliederverwaltung',
]);
}
}
}
$this->auditService->logCreate([
'name' => $item->getName(),
], 'inventory_stock_item', $item->getId());
return $item;
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function updateStockItem(int $id, array $data, ?array $categoryIds = null): StockItem {
$item = $this->stockItemMapper->findById($id);
$oldData = [
'name' => $item->getName(),
'provider_urls_json' => $item->getProviderUrlsJson(),
];
if (isset($data['name'])) {
$item->setName($data['name']);
}
if (array_key_exists('provider_urls_json', $data)) {
$item->setProviderUrlsJson($data['provider_urls_json'] ?? null);
}
$item->setUpdatedAt((new DateTime())->format('Y-m-d H:i:s'));
$this->stockItemMapper->update($item);
// Sync categories if provided
if ($categoryIds !== null && is_array($categoryIds)) {
$existing = $this->itemCategoryMapper->findByItemId($id);
foreach ($existing as $existingCat) {
$this->itemCategoryMapper->detach($id, (int)$existingCat->getCategoryId());
}
foreach ($categoryIds as $catId) {
try {
$this->itemCategoryMapper->attach($id, (int)$catId);
} catch (\Exception $e) {
$this->logger->warning('Failed to attach category {catId} to stock item {itemId}', [
'catId' => $catId,
'itemId' => $id,
'app' => 'mitgliederverwaltung',
]);
}
}
}
$this->auditService->logUpdate($oldData, [
'name' => $item->getName(),
'provider_urls_json' => $item->getProviderUrlsJson(),
], 'inventory_stock_item', $id);
return $item;
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function deleteStockItem(int $id): void {
$item = $this->stockItemMapper->findById($id);
$this->stockItemMapper->delete($item);
$this->auditService->logDelete('inventory_stock_item', $id);
}
// ── Stock Variants ────────────────────────────────────────────────
/**
* @throws Exception
*/
public function createStockVariant(array $data): StockVariant {
$variant = new StockVariant();
$variant->setStockItemId((int)$data['stock_item_id']);
$variant->setLabel($data['label'] ?? '');
$variant->setAmount((int)($data['amount'] ?? 0));
$variant->setCost((string)($data['cost'] ?? '0.00'));
$variant->setMinThreshold((int)($data['min_threshold'] ?? 0));
$this->stockVariantMapper->insert($variant);
$this->auditService->logCreate([
'label' => $variant->getLabel(),
'amount' => $variant->getAmount(),
'cost' => $variant->getCost(),
], 'inventory_stock_variant', $variant->getId());
return $variant;
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function updateStockVariant(int $id, array $data): StockVariant {
$variant = $this->stockVariantMapper->findById($id);
$oldData = [
'label' => $variant->getLabel(),
'amount' => $variant->getAmount(),
'cost' => $variant->getCost(),
'min_threshold' => $variant->getMinThreshold(),
];
if (isset($data['label'])) {
$variant->setLabel($data['label']);
}
if (array_key_exists('amount', $data)) {
$variant->setAmount((int)$data['amount']);
}
if (array_key_exists('cost', $data)) {
$variant->setCost((string)$data['cost']);
}
if (array_key_exists('min_threshold', $data)) {
$variant->setMinThreshold((int)$data['min_threshold']);
}
$this->stockVariantMapper->update($variant);
$this->auditService->logUpdate($oldData, [
'label' => $variant->getLabel(),
'amount' => $variant->getAmount(),
'cost' => $variant->getCost(),
'min_threshold' => $variant->getMinThreshold(),
], 'inventory_stock_variant', $id);
return $variant;
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function deleteStockVariant(int $id): void {
$variant = $this->stockVariantMapper->findById($id);
$this->stockVariantMapper->delete($variant);
$this->auditService->logDelete('inventory_stock_variant', $id);
}
/**
* @return StockVariant[]
* @throws Exception
*/
public function getVariantsByStockItem(int $stockItemId): array {
return $this->stockVariantMapper->findByStockItemId($stockItemId);
}
/**
* Check if an item needs repair (condition ≤ 2).
*/
public function needsRepair(?int $condition): bool {
return $condition === null || $condition <= 2;
}
/**
* Get condition label for display.
*/
public function getConditionLabel(?int $condition): string {
if ($condition === null) {
return 'Nicht bewertet';
}
return $condition . '/5';
}
}
+108
View File
@@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Service;
use DateTime;
use OCA\Mitgliederverwaltung\Db\SaleRecord;
use OCA\Mitgliederverwaltung\Db\SaleRecordMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\Exception;
use Psr\Log\LoggerInterface;
/**
* Service layer for inventory sale records.
*
* Part of Issue #165 (Inventory Tracking).
*/
class SaleService {
private SaleRecordMapper $saleRecordMapper;
private AuditService $auditService;
private LoggerInterface $logger;
public function __construct(
SaleRecordMapper $saleRecordMapper,
AuditService $auditService,
LoggerInterface $logger
) {
$this->saleRecordMapper = $saleRecordMapper;
$this->auditService = $auditService;
$this->logger = $logger;
}
/**
* @return SaleRecord[]
* @throws Exception
*/
public function getSales(): array {
return $this->saleRecordMapper->findAll();
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function getSaleById(int $id): SaleRecord {
return $this->saleRecordMapper->findById($id);
}
/**
* @throws Exception
*/
public function createSale(array $data): SaleRecord {
$qty = (int)($data['quantity'] ?? 0);
$unitPrice = (string)($data['unit_price'] ?? '0.00');
// Calculate total price = quantity × unit_price
$totalPrice = number_format((float)$unitPrice * $qty, 2, '.', '');
$record = new SaleRecord();
$record->setStockItemId($data['stock_item_id'] ?? null);
$record->setVariantId($data['variant_id'] ?? null);
$record->setDate($data['date'] ?? (new DateTime())->format('Y-m-d'));
$record->setQuantity($qty);
$record->setUnitPrice($unitPrice);
$record->setTotalPrice($totalPrice);
$record->setNotes($data['notes'] ?? null);
$record->setCreatedAt((new DateTime())->format('Y-m-d H:i:s'));
$this->saleRecordMapper->insert($record);
$this->auditService->logCreate([
'stock_item_id' => $record->getStockItemId(),
'variant_id' => $record->getVariantId(),
'date' => $record->getDate(),
'quantity' => $record->getQuantity(),
'unit_price' => $record->getUnitPrice(),
'total_price' => $record->getTotalPrice(),
], 'inventory_sale', $record->getId());
return $record;
}
/**
* @throws DoesNotExistException
* @throws Exception
*/
public function deleteSale(int $id): void {
$record = $this->saleRecordMapper->findById($id);
$this->saleRecordMapper->delete($record);
$this->auditService->logDelete('inventory_sale', $id);
}
/**
* @return SaleRecord[]
* @throws Exception
*/
public function getSalesByDateRange(?string $from, ?string $to): array {
return $this->saleRecordMapper->findByDateRange($from, $to);
}
/**
* @throws Exception
*/
public function countSalesByDateRange(?string $from, ?string $to): int {
return $this->saleRecordMapper->countByDateRange($from, $to);
}
}
+8
View File
@@ -30,6 +30,13 @@
<Tent :size="20" />
</template>
</NcAppNavigationItem>
<NcAppNavigationItem name="Inventar"
:to="{ name: 'inventory' }"
:active="currentRoute === 'inventory'">
<template #icon>
<PackageVariant :size="20" />
</template>
</NcAppNavigationItem>
<NcAppNavigationItem name="Verletzungen"
:to="{ name: 'injuries' }"
:active="currentRoute === 'injuries'">
@@ -103,6 +110,7 @@ import Cog from 'vue-material-design-icons/Cog.vue'
import DatabaseSearch from 'vue-material-design-icons/DatabaseSearch.vue'
import SwapVertical from 'vue-material-design-icons/SwapVertical.vue'
import MedicalBag from 'vue-material-design-icons/MedicalBag.vue'
import PackageVariant from 'vue-material-design-icons/PackageVariant.vue'
import Tent from 'vue-material-design-icons/Campfire.vue'
import BackupRestore from 'vue-material-design-icons/BackupRestore.vue'
import SearchBar from './components/SearchBar.vue'
+78
View File
@@ -0,0 +1,78 @@
<template>
<div class="category-picker">
<NcSelect
:model-value="selectedCategories"
:options="options"
multiple
:reduce="o => o.value"
:close-on-select="false"
placeholder="Kategorien auswählen..."
@update:model-value="$emit('update:modelValue', $event.map(o => o.value))"
/>
<div class="category-picker__create">
<NcTextField
:model-value="newCategoryName"
placeholder="Neue Kategorie erstellen..."
@update:model-value="newCategoryName = $event"
/>
<NcButton
type="primary"
:disabled="!newCategoryName"
@click="createNewCategory"
>
<template #icon>
<Plus :size="20" />
</template>
Erstellen
</NcButton>
</div>
</div>
</template>
<script setup>
import { ref, watch } from 'vue'
import { NcSelect, NcButton } from '@nextcloud/vue'
import Plus from 'vue-material-design-icons/Plus.vue'
const props = defineProps({
modelValue: {
type: Array,
default: () => [],
},
options: {
type: Array,
default: () => [],
},
})
const emit = defineEmits(['update:modelValue', 'create'])
const newCategoryName = ref('')
const selectedCategories = ref([...(props.modelValue || [])])
// Keep selectedCategories in sync with modelValue
watch(() => props.modelValue, (newVal) => {
selectedCategories.value = [...(newVal || [])]
})
function createNewCategory() {
if (!newCategoryName.value) return
emit('create', newCategoryName.value.trim())
newCategoryName.value = ''
}
</script>
<style scoped>
.category-picker {
display: flex;
flex-direction: column;
gap: 8px;
}
.category-picker__create {
display: flex;
gap: 8px;
}
</style>
+170
View File
@@ -0,0 +1,170 @@
<template>
<div class="general-material-form">
<form @submit.prevent="handleSubmit">
<div class="general-material-form__field">
<label for="gm-name">Name:</label>
<NcTextField
id="gm-name"
:model-value="form.name"
:placeholder="'Name des Materials'"
:validate="form.name !== ''"
@update:model-value="form.name = $event"
required
/>
</div>
<div class="general-material-form__field">
<label for="gm-condition">Zustand (05):</label>
<NcSelect
id="gm-condition"
:model-value="conditionOptions.find(o => o.value === form.condition)"
:options="conditionOptions"
:reduce="o => o.value"
:clearable="true"
@update:model-value="form.condition = $event"
>
<template #cell="{ option }">
<span class="general-material-form__condition-option">
{{ option.label }}
</span>
</template>
</NcSelect>
</div>
<div class="general-material-form__field">
<label for="gm-categories">Kategorien:</label>
<NcSelect
id="gm-categories"
:model-value="categoryOptions.filter(o => form.categories?.includes(o.value))"
:options="categoryOptions"
multiple
:reduce="o => o.value"
:close-on-select="false"
@update:model-value="form.categories = $event.map(o => o.value)"
/>
</div>
<div class="general-material-form__field">
<label for="gm-notes">Notizen:</label>
<NcTextField
id="gm-notes"
:type="'textarea'"
:model-value="form.notes"
:placeholder="'Zusätzliche Notizen...'"
:multiple-lines="true"
@update:model-value="form.notes = $event"
/>
</div>
<div class="general-material-form__actions">
<NcButton type="tertiary" @click="$emit('close')">
Abbrechen
</NcButton>
<NcButton type="primary" :disabled="!isFormValid" @click="handleSubmit">
{{ form.name ? (editMode ? 'Speichern' : 'Erstellen') : 'Speichern' }}
</NcButton>
</div>
</form>
</div>
</template>
<script setup>
import { ref, computed, watch } from 'vue'
import { NcTextField, NcSelect, NcButton } from '@nextcloud/vue'
const props = defineProps({
item: {
type: Object,
default: null,
},
categories: {
type: Array,
default: () => [],
},
})
const emit = defineEmits(['update', 'close'])
const editMode = computed(() => !!props.item)
const form = ref({
name: '',
condition: null,
categories: [],
notes: '',
})
const conditionOptions = computed(() => {
const options = []
for (let i = 0; i <= 5; i++) {
options.push({ value: i, label: i === 0 ? '0 (sehr schlecht)' : i === 5 ? '5 (hervorragend)' : String(i) })
}
return options
})
const categoryOptions = computed(() => {
return props.categories.map(c => ({
value: c.id,
label: c.name,
}))
})
const isFormValid = computed(() => form.value.name !== '')
// Load existing item data
watch(() => props.item, (newVal) => {
if (newVal) {
form.value = {
name: newVal.name || '',
condition: newVal.condition ?? null,
categories: newVal.categories || [],
notes: newVal.notes || '',
}
}
}, { immediate: true })
async function handleSubmit() {
if (!isFormValid.value) return
const data = {
name: form.value.name,
condition: form.value.condition,
categories: form.value.categories,
notes: form.value.notes || null,
}
if (editMode.value) {
await emit('update', 'general', 'update', { id: props.item.id, ...data })
} else {
await emit('update', 'general', 'create', data)
}
}
</script>
<style scoped>
.general-material-form {
max-width: 500px;
}
.general-material-form__field {
margin-bottom: 16px;
}
.general-material-form__field label {
display: block;
margin-bottom: 4px;
font-weight: 600;
font-size: 14px;
}
.general-material-form__condition-option {
font-size: 14px;
}
.general-material-form__actions {
display: flex;
gap: 8px;
justify-content: flex-end;
margin-top: 24px;
}
</style>
+191
View File
@@ -0,0 +1,191 @@
<template>
<div class="sale-form">
<form @submit.prevent="handleSubmit">
<div class="sale-form__field">
<label for="sale-item">Artikel:</label>
<NcSelect
id="sale-item"
:model-value="stockItemOptions.find(o => o.value === form.stock_item_id)"
:options="stockItemOptions"
:reduce="o => o.value"
:clearable="true"
@update:model-value="form.stock_item_id = $event"
placeholder="Artikel auswählen..."
/>
</div>
<div class="sale-form__field">
<label for="sale-date">Datum:</label>
<NcDateTimePicker
id="sale-date"
:model-value="form.date ? new Date(form.date + 'T00:00:00') : new Date()"
@update:model-value="form.date = $event?.toISOString().split('T')[0] || (new Date()).toISOString().split('T')[0]"
/>
</div>
<div class="sale-form__field">
<label for="sale-variant">Variante:</label>
<NcSelect
id="sale-variant"
:model-value="variantOptions.find(o => o.value === form.variant_id)"
:options="variantOptions"
:reduce="o => o.value"
:clearable="true"
:disabled="!form.stock_item_id"
@update:model-value="form.variant_id = $event"
placeholder="Variante auswählen (optional)..."
/>
</div>
<div class="sale-form__field">
<label for="sale-quantity">Menge:</label>
<NcTextField
id="sale-quantity"
:type="'number'"
:model-value="form.quantity"
:validate="form.quantity >= 1"
@update:model-value="form.quantity = Number($event) || 0"
/>
</div>
<div class="sale-form__field">
<label for="sale-unit-price">Stückpreis (€):</label>
<NcTextField
id="sale-unit-price"
:type="'text'"
:model-value="form.unit_price"
:validate="form.unit_price !== ''"
@update:model-value="form.unit_price = $event"
placeholder="0.00"
/>
</div>
<div class="sale-form__field sale-form__field--summary">
<strong>Gesamtsumme:</strong> {{ totalPrice }}
</div>
<div class="sale-form__field">
<label for="sale-notes">Notizen:</label>
<NcTextField
id="sale-notes"
:type="'textarea'"
:model-value="form.notes"
:placeholder="'Zusätzliche Notizen...'"
:validate="true"
@update:model-value="form.notes = $event"
/>
</div>
<div class="sale-form__actions">
<NcButton type="tertiary" @click="$emit('close')">
Abbrechen
</NcButton>
<NcButton type="primary" :disabled="!isFormValid" @click="handleSubmit">
Verkauf eintragen
</NcButton>
</div>
</form>
</div>
</template>
<script setup>
import { ref, computed } from 'vue'
import { NcTextField, NcSelect, NcButton, NcDateTimePicker } from '@nextcloud/vue'
const props = defineProps({
stockItems: {
type: Array,
default: () => [],
},
})
const emit = defineEmits(['close', 'create'])
const form = ref({
stock_item_id: null,
variant_id: null,
date: new Date().toISOString().split('T')[0],
quantity: 1,
unit_price: '0.00',
notes: '',
})
const stockItemOptions = computed(() => {
return props.stockItems.map(item => ({
value: item.id,
label: item.name,
}))
})
const variantOptions = computed(() => {
if (!form.value.stock_item_id) return []
const item = props.stockItems.find(i => i.id === form.value.stock_item_id)
if (!item || !item.variants) return []
return item.variants.map(v => ({
value: v.id,
label: v.label,
}))
})
const totalPrice = computed(() => {
const qty = Number(form.value.quantity) || 0
const price = parseFloat(form.value.unit_price) || 0
const total = qty * price
return total.toLocaleString('de-DE', {
style: 'currency',
currency: 'EUR',
})
})
const isFormValid = computed(() => {
return form.value.stock_item_id !== null &&
form.value.quantity >= 1 &&
form.value.unit_price !== ''
})
async function handleSubmit() {
if (!isFormValid.value) return
const data = {
stock_item_id: form.value.stock_item_id,
variant_id: form.value.variant_id,
date: form.value.date,
quantity: Number(form.value.quantity),
unit_price: form.value.unit_price,
notes: form.value.notes || null,
}
// Create the sale and notify the parent
emit('create', data)
}
</script>
<style scoped>
.sale-form {
max-width: 500px;
}
.sale-form__field {
margin-bottom: 16px;
}
.sale-form__field label {
display: block;
margin-bottom: 4px;
font-weight: 600;
font-size: 14px;
}
.sale-form__field--summary {
background: var(--color-background-dark);
padding: 12px;
border-radius: var(--border-radius);
}
.sale-form__actions {
display: flex;
gap: 8px;
justify-content: flex-end;
margin-top: 24px;
}
</style>
+255
View File
@@ -0,0 +1,255 @@
<template>
<div class="stock-item-form">
<form @submit.prevent="handleSubmit">
<div class="stock-item-form__field">
<label for="si-name">Name:</label>
<NcTextField
id="si-name"
:model-value="form.name"
:placeholder="'Name des Verkaufsmaterials'"
:validate="form.name !== ''"
@update:model-value="form.name = $event"
required
/>
</div>
<div class="stock-item-form__field">
<label for="si-categories">Kategorien:</label>
<NcSelect
id="si-categories"
:model-value="categoryOptions.filter(o => form.categories?.includes(o.value))"
:options="categoryOptions"
multiple
:reduce="o => o.value"
:close-on-select="false"
@update:model-value="form.categories = $event.map(o => o.value)"
/>
</div>
<div class="stock-item-form__field">
<label>Provider-URLs:</label>
<div v-for="(urlEntry, idx) in form.providerUrls" :key="idx" class="stock-item-form__url-row">
<NcTextField
:model-value="urlEntry.url"
:placeholder="'https://...'"
@update:model-value="form.providerUrls[idx].url = $event"
/>
<NcTextField
:model-value="urlEntry.provider"
:placeholder="'Provider'"
@update:model-value="form.providerUrls[idx].provider = $event"
/>
<NcButton type="tertiary-no-background" @click="removeUrl(idx)">
<template #icon>
<Close :size="20" />
</template>
</NcButton>
</div>
<NcButton type="tertiary" @click="addUrl">
<template #icon>
<Plus :size="20" />
</template>
URL hinzufügen
</NcButton>
</div>
<div class="stock-item-form__variants-header">
<span>Varianten:</span>
<NcButton type="tertiary" @click="addVariant">
<template #icon>
<Plus :size="20" />
</template>
Variante hinzufügen
</NcButton>
</div>
<div v-for="(variant, idx) in form.variants" :key="idx" class="stock-item-form__variant-row">
<NcTextField
:model-value="variant.label"
:placeholder="'Größe M, Farbe Blau...'"
:validate="variant.label !== ''"
@update:model-value="form.variants[idx].label = $event"
/>
<NcTextField
:type="'number'"
:model-value="variant.amount"
:validate="variant.amount >= 0"
@update:model-value="form.variants[idx].amount = Number($event) || 0"
/>
<NcTextField
:type="'text'"
:model-value="variant.cost"
:placeholder="'Stückpreis (€)'"
@update:model-value="variant.cost = $event"
/>
<NcTextField
:type="'number'"
:model-value="variant.min_threshold"
:validate="variant.min_threshold >= 0"
@update:model-value="form.variants[idx].min_threshold = Number($event) || 0"
/>
<NcButton type="tertiary-no-background" @click="removeVariant(idx)">
<template #icon>
<Close :size="20" />
</template>
</NcButton>
</div>
<div class="stock-item-form__actions">
<NcButton type="tertiary" @click="$emit('close')">
Abbrechen
</NcButton>
<NcButton type="primary" :disabled="!isFormValid" @click="handleSubmit">
{{ form.name ? (editMode ? 'Speichern' : 'Erstellen') : 'Speichern' }}
</NcButton>
</div>
</form>
</div>
</template>
<script setup>
import { ref, computed, watch } from 'vue'
import { NcTextField, NcSelect, NcButton } from '@nextcloud/vue'
import Close from 'vue-material-design-icons/Close.vue'
import Plus from 'vue-material-design-icons/Plus.vue'
const props = defineProps({
item: {
type: Object,
default: null,
},
categories: {
type: Array,
default: () => [],
},
})
const emit = defineEmits(['update', 'close'])
const editMode = computed(() => !!props.item)
const form = ref({
name: '',
categories: [],
providerUrls: [],
variants: [],
})
const categoryOptions = computed(() => {
return props.categories.map(c => ({
value: c.id,
label: c.name,
}))
})
const isFormValid = computed(() => form.value.name !== '')
// Load existing item data
watch(() => props.item, (newVal) => {
if (newVal) {
let urls = []
try {
urls = JSON.parse(newVal.provider_urls_json || '[]')
} catch {
urls = []
}
form.value = {
name: newVal.name || '',
categories: newVal.categories || [],
providerUrls: urls.map(u => ({ url: u.url || '', provider: u.provider || '' })),
variants: (newVal.variants || []).map(v => ({
id: v.id,
label: v.label || '',
amount: v.amount || 0,
cost: v.cost || '0.00',
min_threshold: v.min_threshold || 0,
})),
}
}
}, { immediate: true })
function addUrl() {
form.value.providerUrls.push({ url: '', provider: '' })
}
function removeUrl(idx) {
form.value.providerUrls.splice(idx, 1)
}
function addVariant() {
form.value.variants.push({
label: '',
amount: 0,
cost: '0.00',
min_threshold: 0,
})
}
function removeVariant(idx) {
form.value.variants.splice(idx, 1)
}
async function handleSubmit() {
if (!isFormValid.value) return
const data = {
name: form.value.name,
categories: form.value.categories,
provider_urls_json: JSON.stringify(form.value.providerUrls),
variants: form.value.variants.map(v => ({
label: v.label,
amount: v.amount || 0,
cost: v.cost || '0.00',
min_threshold: v.min_threshold || 0,
id: v.id,
})),
}
if (editMode.value) {
await emit('update', 'stock', 'update', { id: props.item.id, ...data })
} else {
await emit('update', 'stock', 'create', data)
}
}
</script>
<style scoped>
.stock-item-form {
max-width: 600px;
}
.stock-item-form__field {
margin-bottom: 16px;
}
.stock-item-form__field label {
display: block;
margin-bottom: 4px;
font-weight: 600;
font-size: 14px;
}
.stock-item-form__url-row,
.stock-item-form__variant-row {
display: flex;
gap: 8px;
align-items: center;
margin-bottom: 8px;
flex-wrap: wrap;
}
.stock-item-form__variants-header {
display: flex;
justify-content: space-between;
align-items: center;
margin: 16px 0 8px;
}
.stock-item-form__actions {
display: flex;
gap: 8px;
justify-content: flex-end;
margin-top: 24px;
}
</style>
+47
View File
@@ -0,0 +1,47 @@
<template>
<div class="stock-variant-list">
<div v-if="variants.length > 0" class="stock-variant-list__variants">
<StockVariantRow
v-for="variant in variants"
:key="variant.id"
:variant="variant"
:stock-item-id="stockItemId"
@update="$emit('update-variant', $event)"
@delete="$emit('delete-variant', $event)"
/>
</div>
<div v-else class="stock-variant-list__empty">
Keine Varianten vorhanden
</div>
</div>
</template>
<script setup>
import { defineProps, defineEmits } from 'vue'
import StockVariantRow from './StockVariantRow.vue'
defineProps({
variants: {
type: Array,
default: () => [],
},
stockItemId: {
type: Number,
required: true,
},
})
defineEmits(['update-variant', 'delete-variant'])
</script>
<style scoped>
.stock-variant-list__variants {
margin-bottom: 8px;
}
.stock-variant-list__empty {
color: var(--color-text-maxcontrast);
font-style: italic;
padding: 8px;
}
</style>
+62
View File
@@ -0,0 +1,62 @@
<template>
<div class="stock-variant-row">
<NcTextField
:model-value="variant.label"
:placeholder="'Größe M, Farbe Blau...'"
:validate="variant.label !== ''"
@update:model-value="$emit('update-variant', { ...variant, label: $event })"
/>
<NcTextField
:type="'number'"
:model-value="variant.amount"
:validate="variant.amount >= 0"
@update:model-value="$emit('update-variant', { ...variant, amount: Number($event) || 0 })"
/>
<NcTextField
:type="'text'"
:model-value="variant.cost"
:placeholder="'Stückpreis (€)'"
@update:model-value="$emit('update-variant', { ...variant, cost: $event })"
/>
<NcTextField
:type="'number'"
:model-value="variant.min_threshold"
:validate="variant.min_threshold >= 0"
@update:model-value="$emit('update-variant', { ...variant, min_threshold: Number($event) || 0 })"
/>
<NcButton type="tertiary-no-background" @click="$emit('delete-variant', variant.id)">
<template #icon>
<Close :size="20" />
</template>
</NcButton>
</div>
</template>
<script setup>
import { defineProps, defineEmits } from 'vue'
import { NcTextField, NcButton } from '@nextcloud/vue'
import Close from 'vue-material-design-icons/Close.vue'
defineProps({
variant: {
type: Object,
required: true,
},
stockItemId: {
type: Number,
required: true,
},
})
defineEmits(['update-variant', 'delete-variant'])
</script>
<style scoped>
.stock-variant-row {
display: flex;
gap: 8px;
align-items: center;
margin-bottom: 8px;
flex-wrap: wrap;
}
</style>
+1 -1
View File
@@ -31,7 +31,7 @@ app.use(router)
// @nextcloud/vue v9 reads appName/appVersion via Vue's inject(),
// not via webpack DefinePlugin globals.
app.provide('appName', 'mitgliederverwaltung')
app.provide('appVersion', '0.3.1')
app.provide('appVersion', '0.3.2')
app.mount('#mitgliederverwaltung')
+5
View File
@@ -78,6 +78,11 @@ const routes = [
name: 'backup',
component: () => import('./views/Backup.vue'),
},
{
path: '/inventory',
name: 'inventory',
component: () => import('./views/Inventory.vue'),
},
]
const router = createRouter({
+86
View File
@@ -0,0 +1,86 @@
/**
* Pinia store for inventory categories.
*
* Handles fetching and CRUD operations for inventory categories.
*
* Part of Issue #165 (Inventory Tracking).
*/
import { defineStore } from 'pinia'
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
export const useCategoriesStore = defineStore('inventoryCategories', {
state: () => ({
/** @type {Array} List of categories */
categories: [],
/** @type {boolean} Loading state */
loading: false,
/** @type {string|null} Error message */
error: null,
}),
actions: {
/**
* Fetch all categories.
*/
async fetchCategories() {
this.loading = true
this.error = null
try {
const url = generateUrl('/apps/mitgliederverwaltung/api/v1/inventory/categories')
const response = await axios.get(url)
this.categories = response.data.data || []
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Laden der Kategorien'
console.error('Failed to fetch inventory categories:', err)
} finally {
this.loading = false
}
},
/**
* Create a new category.
*/
async createCategory(name) {
this.loading = true
this.error = null
try {
const url = generateUrl('/apps/mitgliederverwaltung/api/v1/inventory/categories')
const response = await axios.post(url, { name })
this.categories.push(response.data)
this.categories.sort((a, b) => a.name.localeCompare(b.name))
return response.data
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Erstellen der Kategorie'
throw err
} finally {
this.loading = false
}
},
/**
* Delete a category.
*/
async deleteCategory(id) {
this.loading = true
this.error = null
try {
const url = generateUrl(`/apps/mitgliederverwaltung/api/v1/inventory/categories/${id}`)
await axios.delete(url)
this.categories = this.categories.filter(c => c.id !== id)
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Löschen der Kategorie'
throw err
} finally {
this.loading = false
}
},
clearError() {
this.error = null
},
},
})
+128
View File
@@ -0,0 +1,128 @@
/**
* Pinia store for general material inventory.
*
* Handles fetching and CRUD operations for general material items.
*
* Part of Issue #165 (Inventory Tracking).
*/
import { defineStore } from 'pinia'
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
export const useGeneralStore = defineStore('inventoryGeneral', {
state: () => ({
/** @type {Array} List of general material items */
items: [],
/** @type {string|null} Current search/filter text */
searchText: '',
/** @type {boolean} Loading state */
loading: false,
/** @type {string|null} Error message */
error: null,
}),
getters: {
/**
* Filtered items based on search text.
*/
filteredItems: (state) => {
if (!state.searchText) return state.items
const lower = state.searchText.toLowerCase()
return state.items.filter(item =>
item.name.toLowerCase().includes(lower)
)
},
},
actions: {
/**
* Fetch all general material items.
*/
async fetchItems() {
this.loading = true
this.error = null
try {
const url = generateUrl('/apps/mitgliederverwaltung/api/v1/inventory/general')
const response = await axios.get(url)
this.items = response.data.data || []
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Laden der Allgemeinmaterialien'
console.error('Failed to fetch general material:', err)
} finally {
this.loading = false
}
},
/**
* Create a new general material item.
*/
async createItem(data) {
this.loading = true
this.error = null
try {
const url = generateUrl('/apps/mitgliederverwaltung/api/v1/inventory/general')
const response = await axios.post(url, data)
this.items.push(response.data)
this.items.sort((a, b) => a.name.localeCompare(b.name))
return response.data
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Erstellen des Materials'
throw err
} finally {
this.loading = false
}
},
/**
* Update an existing general material item.
*/
async updateItem(id, data) {
this.loading = true
this.error = null
try {
const url = generateUrl(`/apps/mitgliederverwaltung/api/v1/inventory/general/${id}`)
const response = await axios.put(url, data)
const index = this.items.findIndex(i => i.id === id)
if (index !== -1) {
this.items[index] = response.data
}
return response.data
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Aktualisieren des Materials'
throw err
} finally {
this.loading = false
}
},
/**
* Delete a general material item.
*/
async deleteItem(id) {
this.loading = true
this.error = null
try {
const url = generateUrl(`/apps/mitgliederverwaltung/api/v1/inventory/general/${id}`)
await axios.delete(url)
this.items = this.items.filter(i => i.id !== id)
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Löschen des Materials'
throw err
} finally {
this.loading = false
}
},
setSearchText(text) {
this.searchText = text
},
clearError() {
this.error = null
},
},
})
+73
View File
@@ -0,0 +1,73 @@
/**
* Pinia store for inventory report data.
*
* Handles fetching condition and sales report data.
*
* Part of Issue #165 (Inventory Tracking).
*/
import { defineStore } from 'pinia'
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
export const useInventoryReportsStore = defineStore('inventoryReports', {
state: () => ({
/** @type {object|null} Condition report data */
conditionReport: null,
/** @type {object|null} Sales report data */
salesReport: null,
/** @type {boolean} Loading state for condition report */
conditionLoading: false,
/** @type {boolean} Loading state for sales report */
salesLoading: false,
/** @type {string|null} Error message */
error: null,
}),
actions: {
/**
* Fetch condition report data.
*/
async fetchConditionReport() {
this.conditionLoading = true
this.error = null
try {
const url = generateUrl('/apps/mitgliederverwaltung/api/v1/inventory/reports/condition')
const response = await axios.get(url)
this.conditionReport = response.data
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Laden des Materialzustandsberichts'
console.error('Failed to fetch condition report:', err)
} finally {
this.conditionLoading = false
}
},
/**
* Fetch sales report data with optional date range.
*/
async fetchSalesReport(dateFrom, dateTo) {
this.salesLoading = true
this.error = null
try {
const params = {}
if (dateFrom) params.dateFrom = dateFrom
if (dateTo) params.dateTo = dateTo
const url = generateUrl('/apps/mitgliederverwaltung/api/v1/inventory/reports/sales')
const response = await axios.get(url, { params })
this.salesReport = response.data
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Laden des Inventur-Verkaufsberichts'
console.error('Failed to fetch sales report:', err)
} finally {
this.salesLoading = false
}
},
clearError() {
this.error = null
},
},
})
+122
View File
@@ -0,0 +1,122 @@
/**
* Pinia store for inventory sales records.
*
* Handles fetching, creating, and filtering sales records.
*
* Part of Issue #165 (Inventory Tracking).
*/
import { defineStore } from 'pinia'
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
export const useSalesStore = defineStore('inventorySales', {
state: () => ({
/** @type {Array} List of sales records */
sales: [],
/** @type {string|null} Filter from date */
filterDateFrom: null,
/** @type {string|null} Filter to date */
filterDateTo: null,
/** @type {boolean} Loading state */
loading: false,
/** @type {string|null} Error message */
error: null,
}),
getters: {
/**
* Filtered sales based on date range.
*/
filteredSales: (state) => {
if (!state.filterDateFrom && !state.filterDateTo) {
return state.sales
}
return state.sales.filter(sale => {
const saleDate = sale.date
if (state.filterDateFrom && saleDate < state.filterDateFrom) {
return false
}
if (state.filterDateTo && saleDate > state.filterDateTo) {
return false
}
return true
})
},
},
actions: {
/**
* Fetch sales records with optional date filter.
*/
async fetchSales() {
this.loading = true
this.error = null
try {
const params = {}
if (this.filterDateFrom) params.dateFrom = this.filterDateFrom
if (this.filterDateTo) params.dateTo = this.filterDateTo
const url = generateUrl('/apps/mitgliederverwaltung/api/v1/inventory/sales')
const response = await axios.get(url, { params })
this.sales = response.data.data || []
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Laden der Verkäufe'
console.error('Failed to fetch sales:', err)
} finally {
this.loading = false
}
},
/**
* Create a new sale record.
*/
async createSale(data) {
this.loading = true
this.error = null
try {
const url = generateUrl('/apps/mitgliederverwaltung/api/v1/inventory/sales')
const response = await axios.post(url, data)
this.sales.unshift(response.data)
return response.data
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Erstellen des Verkaufs'
throw err
} finally {
this.loading = false
}
},
/**
* Delete a sale record.
*/
async deleteSale(id) {
this.loading = true
this.error = null
try {
const url = generateUrl(`/apps/mitgliederverwaltung/api/v1/inventory/sales/${id}`)
await axios.delete(url)
this.sales = this.sales.filter(s => s.id !== id)
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Löschen des Verkaufs'
throw err
} finally {
this.loading = false
}
},
setFilterDateFrom(date) {
this.filterDateFrom = date
},
setFilterDateTo(date) {
this.filterDateTo = date
},
clearError() {
this.error = null
},
},
})
+181
View File
@@ -0,0 +1,181 @@
/**
* Pinia store for stock (sales) items.
*
* Handles fetching and CRUD operations for stock items with variants.
*
* Part of Issue #165 (Inventory Tracking).
*/
import { defineStore } from 'pinia'
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
export const useStockStore = defineStore('inventoryStock', {
state: () => ({
/** @type {Array} List of stock items */
items: [],
/** @type {string|null} Current search/filter text */
searchText: '',
/** @type {boolean} Loading state */
loading: false,
/** @type {string|null} Error message */
error: null,
}),
getters: {
/**
* Filtered items based on search text.
*/
filteredItems: (state) => {
if (!state.searchText) return state.items
const lower = state.searchText.toLowerCase()
return state.items.filter(item =>
item.name.toLowerCase().includes(lower)
)
},
},
actions: {
/**
* Fetch all stock items with their variants.
*/
async fetchItems() {
this.loading = true
this.error = null
try {
const url = generateUrl('/apps/mitgliederverwaltung/api/v1/inventory/stock')
const response = await axios.get(url)
this.items = response.data.data || []
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Laden des Verkaufsmaterials'
console.error('Failed to fetch stock items:', err)
} finally {
this.loading = false
}
},
/**
* Create a new stock item with variants.
*/
async createItem(data) {
this.loading = true
this.error = null
try {
const url = generateUrl('/apps/mitgliederverwaltung/api/v1/inventory/stock')
const response = await axios.post(url, data)
this.items.push(response.data)
this.items.sort((a, b) => a.name.localeCompare(b.name))
return response.data
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Erstellen des Verkaufsmaterials'
throw err
} finally {
this.loading = false
}
},
/**
* Update an existing stock item.
*/
async updateItem(id, data) {
this.loading = true
this.error = null
try {
const url = generateUrl(`/apps/mitgliederverwaltung/api/v1/inventory/stock/${id}`)
const response = await axios.put(url, data)
const index = this.items.findIndex(i => i.id === id)
if (index !== -1) {
this.items[index] = response.data
}
return response.data
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Aktualisieren des Verkaufsmaterials'
throw err
} finally {
this.loading = false
}
},
/**
* Delete a stock item.
*/
async deleteItem(id) {
this.loading = true
this.error = null
try {
const url = generateUrl(`/apps/mitgliederverwaltung/api/v1/inventory/stock/${id}`)
await axios.delete(url)
this.items = this.items.filter(i => i.id !== id)
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Löschen des Verkaufsmaterials'
throw err
} finally {
this.loading = false
}
},
/**
* Add a variant to an item.
*/
async addVariant(stockItemId, variantData) {
this.loading = true
this.error = null
try {
const url = generateUrl('/apps/mitgliederverwaltung/api/v1/inventory/variants')
const response = await axios.post(url, {
stock_item_id: stockItemId,
...variantData,
})
const item = this.items.find(i => i.id === stockItemId)
if (item) {
if (!item.variants) item.variants = []
item.variants.push(response.data)
item.total_amount = (item.total_amount || 0) + (response.data.amount || 0)
}
return response.data
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Erstellen der Variante'
throw err
} finally {
this.loading = false
}
},
/**
* Delete a variant.
*/
async deleteVariant(variantId, stockItemId) {
this.loading = true
this.error = null
try {
const url = generateUrl(`/apps/mitgliederverwaltung/api/v1/inventory/variants/${variantId}`)
await axios.delete(url)
const item = this.items.find(i => i.id === stockItemId)
if (item && item.variants) {
item.variants = item.variants.filter(v => v.id !== variantId)
}
} catch (err) {
this.error = err.response?.data?.error || 'Fehler beim Löschen der Variante'
throw err
} finally {
this.loading = false
}
},
setSearchText(text) {
this.searchText = text
},
clearError() {
this.error = null
},
},
})
+130
View File
@@ -0,0 +1,130 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Tests\Db;
use OCA\Mitgliederverwaltung\Db\GeneralMaterial;
use OCA\Mitgliederverwaltung\Db\GeneralMaterialMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\IResult;
use OCP\IDBConnection;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class GeneralMaterialMapperTest extends TestCase {
private IDBConnection&MockObject $db;
private IQueryBuilder&MockObject $qb;
private IExpressionBuilder&MockObject $expr;
private IResult&MockObject $result;
protected function setUp(): void {
parent::setUp();
$this->db = $this->createMock(IDBConnection::class);
$this->qb = $this->createMock(IQueryBuilder::class);
$this->expr = $this->createMock(IExpressionBuilder::class);
$this->result = $this->createMock(IResult::class);
$this->qb->method('expr')->willReturn($this->expr);
$this->qb->method('select')->willReturnSelf();
$this->qb->method('from')->willReturnSelf();
$this->qb->method('where')->willReturnSelf();
$this->qb->method('andWhere')->willReturnSelf();
$this->qb->method('orderBy')->willReturnSelf();
$this->qb->method('gte')->willReturn('col >= :param');
$this->qb->method('lte')->willReturn('col <= :param');
$this->qb->method('isNotNull')->willReturn('col IS NOT NULL');
$this->qb->method('insert')->willReturnSelf();
$this->qb->method('delete')->willReturnSelf();
$this->qb->method('values')->willReturnSelf();
$this->qb->method('createNamedParameter')->willReturn(':param');
$this->qb->method('createFunction')->willReturnCallback(fn($call) => $call);
$this->qb->method('getSQL')->willReturn('SELECT * FROM test');
$this->expr->method('eq')->willReturn('col = :param');
$this->expr->method('isNull')->willReturn('col IS NULL');
$this->expr->method('neq')->willReturn('col != :param');
$this->expr->method('orX')->willReturn('or_expr');
$this->db->method('getQueryBuilder')->willReturn($this->qb);
}
private function configureResultRows(array $rows): void {
$fetchCalls = array_merge($rows, [false]);
$this->result->method('fetch')
->willReturnOnConsecutiveCalls(...$fetchCalls);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function configureResultSingleRow(array $row): void {
$this->result->method('fetch')
->willReturnOnConsecutiveCalls($row, false);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function configureResultEmpty(): void {
$this->result->method('fetch')->willReturn(false);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function itemRow(int $id = 1, string $name = 'Zelt A', ?int $condition = 4, ?string $notes = 'Sehr gut'): array {
return [
'id' => $id,
'name' => $name,
'condition' => $condition,
'notes' => $notes,
'created_at' => '2026-04-21 00:00:00',
'updated_at' => '2026-04-21 00:00:00',
];
}
public function testCreateAndFind(): void {
$this->configureResultSingleRow($this->itemRow(42, 'Zelt A', 4, 'Sehr gut'));
$mapper = new GeneralMaterialMapper($this->db);
$item = $mapper->findById(42);
$this->assertSame(42, $item->getId());
$this->assertSame('Zelt A', $item->getName());
$this->assertSame(4, $item->getCondition());
}
public function testFindAll(): void {
$this->configureResultRows([
$this->itemRow(1, 'Zelt A', 4),
$this->itemRow(2, 'Zelt B', 3),
]);
$mapper = new GeneralMaterialMapper($this->db);
$items = $mapper->findAll();
$this->assertCount(2, $items);
$this->assertSame('Zelt A', $items[0]->getName());
}
public function testFindByConditionRange(): void {
$this->configureResultRows([$this->itemRow(1, 'Zelt A', 1), $this->itemRow(2, 'Zelt B', 2)]);
$mapper = new GeneralMaterialMapper($this->db);
$items = $mapper->findByConditionRange(0, 2);
$this->assertCount(2, $items);
$this->assertLessThanOrEqual(2, $items[0]->getCondition());
}
public function testFindByConditionRangeEmpty(): void {
$this->configureResultRows([]);
$mapper = new GeneralMaterialMapper($this->db);
$items = $mapper->findByConditionRange(4, 5);
$this->assertCount(0, $items);
}
public function testUpdateCondition(): void {
$this->configureResultSingleRow($this->itemRow(1, 'Zelt A', 3, 'Gut'));
$mapper = new GeneralMaterialMapper($this->db);
$item = $mapper->findById(1);
$this->assertSame(3, $item->getCondition());
$this->assertSame('Gut', $item->getNotes());
}
}
+121
View File
@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Tests\Db;
use OCA\Mitgliederverwaltung\Db\InventoryCategory;
use OCA\Mitgliederverwaltung\Db\InventoryCategoryMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\IResult;
use OCP\IDBConnection;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class InventoryCategoryMapperTest extends TestCase {
private IDBConnection&MockObject $db;
private IQueryBuilder&MockObject $qb;
private IExpressionBuilder&MockObject $expr;
private IResult&MockObject $result;
protected function setUp(): void {
parent::setUp();
$this->db = $this->createMock(IDBConnection::class);
$this->qb = $this->createMock(IQueryBuilder::class);
$this->expr = $this->createMock(IExpressionBuilder::class);
$this->result = $this->createMock(IResult::class);
$this->qb->method('expr')->willReturn($this->expr);
$this->qb->method('select')->willReturnSelf();
$this->qb->method('from')->willReturnSelf();
$this->qb->method('where')->willReturnSelf();
$this->qb->method('andWhere')->willReturnSelf();
$this->qb->method('orderBy')->willReturnSelf();
$this->qb->method('insert')->willReturnSelf();
$this->qb->method('delete')->willReturnSelf();
$this->qb->method('values')->willReturnSelf();
$this->qb->method('createNamedParameter')->willReturn(':param');
$this->qb->method('createFunction')->willReturnCallback(fn($call) => $call);
$this->qb->method('getSQL')->willReturn('SELECT * FROM test');
$this->expr->method('eq')->willReturn('col = :param');
$this->expr->method('isNull')->willReturn('col IS NULL');
$this->expr->method('neq')->willReturn('col != :param');
$this->db->method('getQueryBuilder')->willReturn($this->qb);
}
private function configureResultRows(array $rows): void {
$fetchCalls = array_merge($rows, [false]);
$this->result->method('fetch')
->willReturnOnConsecutiveCalls(...$fetchCalls);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function configureResultSingleRow(array $row): void {
$this->result->method('fetch')
->willReturnOnConsecutiveCalls($row, false);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function configureResultEmpty(): void {
$this->result->method('fetch')->willReturn(false);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function categoryRow(int $id = 1, string $name = 'Zelte'): array {
return [
'id' => $id,
'name' => $name,
'created_at' => '2026-04-21 00:00:00',
];
}
public function testCreateAndFindById(): void {
$this->configureResultSingleRow($this->categoryRow(42, 'Zelte'));
$mapper = new InventoryCategoryMapper($this->db);
$category = $mapper->findById(42);
$this->assertSame(42, $category->getId());
$this->assertSame('Zelte', $category->getName());
}
public function testFindByName(): void {
$this->configureResultSingleRow($this->categoryRow(1, 'Kochgeschirr'));
$mapper = new InventoryCategoryMapper($this->db);
$category = $mapper->findByName('Kochgeschirr');
$this->assertSame('Kochgeschirr', $category->getName());
}
public function testFindByNameThrowsWhenNotFound(): void {
$this->configureResultEmpty();
$mapper = new InventoryCategoryMapper($this->db);
$this->expectException(DoesNotExistException::class);
$mapper->findByName('NichtExistent');
}
public function testFindAll(): void {
$this->configureResultRows([
$this->categoryRow(1, 'Zelte'),
$this->categoryRow(2, 'Kochgeschirr'),
$this->categoryRow(3, 'Seile'),
]);
$mapper = new InventoryCategoryMapper($this->db);
$categories = $mapper->findAll();
$this->assertCount(3, $categories);
$this->assertSame('Zelte', $categories[0]->getName());
}
public function testFindAllEmpty(): void {
$this->configureResultRows([]);
$mapper = new InventoryCategoryMapper($this->db);
$categories = $mapper->findAll();
$this->assertCount(0, $categories);
}
}
@@ -0,0 +1,96 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Tests\Db;
use OCA\Mitgliederverwaltung\Db\InventoryItemCategory;
use OCA\Mitgliederverwaltung\Db\InventoryItemCategoryMapper;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\IResult;
use OCP\IDBConnection;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class InventoryItemCategoryMapperTest extends TestCase {
private IDBConnection&MockObject $db;
private IQueryBuilder&MockObject $qb;
private IExpressionBuilder&MockObject $expr;
private IResult&MockObject $result;
protected function setUp(): void {
parent::setUp();
$this->db = $this->createMock(IDBConnection::class);
$this->qb = $this->createMock(IQueryBuilder::class);
$this->expr = $this->createMock(IExpressionBuilder::class);
$this->result = $this->createMock(IResult::class);
$this->qb->method('expr')->willReturn($this->expr);
$this->qb->method('select')->willReturnSelf();
$this->qb->method('from')->willReturnSelf();
$this->qb->method('where')->willReturnSelf();
$this->qb->method('andWhere')->willReturnSelf();
$this->qb->method('orderBy')->willReturnSelf();
$this->qb->method('insert')->willReturnSelf();
$this->qb->method('delete')->willReturnSelf();
$this->qb->method('values')->willReturnSelf();
$this->qb->method('createNamedParameter')->willReturn(':param');
$this->qb->method('createFunction')->willReturnCallback(fn($call) => $call);
$this->qb->method('getSQL')->willReturn('SELECT * FROM test');
$this->qb->method('executeStatement')->willReturn(1);
$this->expr->method('eq')->willReturn('col = :param');
$this->expr->method('isNull')->willReturn('col IS NULL');
$this->db->method('getQueryBuilder')->willReturn($this->qb);
}
private function configureResultRows(array $rows): void {
$fetchCalls = array_merge($rows, [false]);
$this->result->method('fetch')
->willReturnOnConsecutiveCalls(...$fetchCalls);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function row(int $itemId = 1, int $categoryId = 1): array {
return [
'item_id' => $itemId,
'category_id' => $categoryId,
];
}
public function testAttach_detach(): void {
$mapper = new InventoryItemCategoryMapper($this->db);
$mapper->attach(1, 1);
$this->assertTrue(true); // No exception = success
}
public function testDetach(): void {
$mapper = new InventoryItemCategoryMapper($this->db);
$mapper->detach(1, 1);
$this->assertTrue(true);
}
public function testFindByItemIdReturnsCategories(): void {
$this->configureResultRows([
$this->row(1, 1),
$this->row(1, 2),
]);
$mapper = new InventoryItemCategoryMapper($this->db);
$cats = $mapper->findByItemId(1);
$this->assertCount(2, $cats);
$this->assertSame(1, $cats[0]->getCategoryId());
$this->assertSame(2, $cats[1]->getCategoryId());
}
public function testFindByItemIdEmpty(): void {
$this->configureResultRows([]);
$mapper = new InventoryItemCategoryMapper($this->db);
$cats = $mapper->findByItemId(999);
$this->assertCount(0, $cats);
}
}
+134
View File
@@ -0,0 +1,134 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Tests\Db;
use OCA\Mitgliederverwaltung\Db\SaleRecord;
use OCA\Mitgliederverwaltung\Db\SaleRecordMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\IResult;
use OCP\IDBConnection;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class SaleRecordMapperTest extends TestCase {
private IDBConnection&MockObject $db;
private IQueryBuilder&MockObject $qb;
private IExpressionBuilder&MockObject $expr;
private IResult&MockObject $result;
protected function setUp(): void {
parent::setUp();
$this->db = $this->createMock(IDBConnection::class);
$this->qb = $this->createMock(IQueryBuilder::class);
$this->expr = $this->createMock(IExpressionBuilder::class);
$this->result = $this->createMock(IResult::class);
$this->qb->method('expr')->willReturn($this->expr);
$this->qb->method('select')->willReturnSelf();
$this->qb->method('selectDistinct')->willReturnSelf();
$this->qb->method('from')->willReturnSelf();
$this->qb->method('where')->willReturnSelf();
$this->qb->method('andWhere')->willReturnSelf();
$this->qb->method('orderBy')->willReturnSelf();
$this->qb->method('insert')->willReturnSelf();
$this->qb->method('delete')->willReturnSelf();
$this->qb->method('values')->willReturnSelf();
$this->qb->method('createNamedParameter')->willReturn(':param');
$this->qb->method('createFunction')->willReturnCallback(fn($call) => $call);
$this->qb->method('getSQL')->willReturn('SELECT * FROM test');
$this->expr->method('eq')->willReturn('col = :param');
$this->expr->method('gte')->willReturn('col >= :param');
$this->expr->method('lte')->willReturn('col <= :param');
$this->expr->method('isNull')->willReturn('col IS NULL');
$this->expr->method('neq')->willReturn('col != :param');
$this->expr->method('orX')->willReturn('or_expr');
$this->db->method('getQueryBuilder')->willReturn($this->qb);
}
private function configureResultRows(array $rows): void {
$fetchCalls = array_merge($rows, [false]);
$this->result->method('fetch')
->willReturnOnConsecutiveCalls(...$fetchCalls);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function configureResultSingleRow(array $row): void {
$this->result->method('fetch')
->willReturnOnConsecutiveCalls($row, false);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function configureResultEmpty(): void {
$this->result->method('fetch')->willReturn(false);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function configureResultCount(int $count): void {
$this->result->method('fetchOne')->willReturn((string)$count);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function saleRow(int $id = 1, ?int $stockItemId = 1, ?int $variantId = 1, string $date = '2026-04-21', int $quantity = 2, string $unitPrice = '25.00', string $totalPrice = '50.00'): array {
return [
'id' => $id,
'stock_item_id' => $stockItemId,
'variant_id' => $variantId,
'date' => $date,
'quantity' => $quantity,
'unit_price' => $unitPrice,
'total_price' => $totalPrice,
'notes' => null,
'created_at' => '2026-04-21 00:00:00',
];
}
public function testCreateAndFind(): void {
$this->configureResultSingleRow($this->saleRow(42, 1, 1, '2026-04-21', 2, '25.00', '50.00'));
$mapper = new SaleRecordMapper($this->db);
$sale = $mapper->findById(42);
$this->assertSame(42, $sale->getId());
$this->assertSame(2, $sale->getQuantity());
$this->assertSame('50.00', $sale->getTotalPrice());
}
public function testFindByDateRange(): void {
$this->configureResultRows([
$this->saleRow(1, 1, null, '2026-03-15', 1, '30.00', '30.00'),
$this->saleRow(2, 1, 2, '2026-04-01', 2, '25.00', '50.00'),
]);
$mapper = new SaleRecordMapper($this->db);
$sales = $mapper->findByDateRange('2026-03-01', '2026-04-30');
$this->assertCount(2, $sales);
$this->assertSame('2026-04-01', $sales[0]->getDate());
}
public function testFindByDateRangeEmpty(): void {
$this->configureResultRows([]);
$mapper = new SaleRecordMapper($this->db);
$sales = $mapper->findByDateRange('2000-01-01', '2000-12-31');
$this->assertCount(0, $sales);
}
public function testTotalPriceCalculation(): void {
$this->configureResultSingleRow($this->saleRow(1, 1, 1, '2026-04-01', 3, '15.00', '45.00'));
$mapper = new SaleRecordMapper($this->db);
$sale = $mapper->findById(1);
$this->assertSame(3, $sale->getQuantity());
$this->assertSame('15.00', $sale->getUnitPrice());
$this->assertSame('45.00', $sale->getTotalPrice());
// Verify: 3 × 15.00 = 45.00
$this->assertSame((float)$sale->getTotalPrice(), (float)$sale->getQuantity() * (float)$sale->getUnitPrice());
}
}
+108
View File
@@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Tests\Db;
use OCA\Mitgliederverwaltung\Db\StockItem;
use OCA\Mitgliederverwaltung\Db\StockItemMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\IResult;
use OCP\IDBConnection;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class StockItemMapperTest extends TestCase {
private IDBConnection&MockObject $db;
private IQueryBuilder&MockObject $qb;
private IExpressionBuilder&MockObject $expr;
private IResult&MockObject $result;
protected function setUp(): void {
parent::setUp();
$this->db = $this->createMock(IDBConnection::class);
$this->qb = $this->createMock(IQueryBuilder::class);
$this->expr = $this->createMock(IExpressionBuilder::class);
$this->result = $this->createMock(IResult::class);
$this->qb->method('expr')->willReturn($this->expr);
$this->qb->method('select')->willReturnSelf();
$this->qb->method('from')->willReturnSelf();
$this->qb->method('where')->willReturnSelf();
$this->qb->method('orderBy')->willReturnSelf();
$this->qb->method('createNamedParameter')->willReturn(':param');
$this->qb->method('createFunction')->willReturnCallback(fn($call) => $call);
$this->qb->method('getSQL')->willReturn('SELECT * FROM test');
$this->expr->method('eq')->willReturn('col = :param');
$this->expr->method('isNull')->willReturn('col IS NULL');
$this->db->method('getQueryBuilder')->willReturn($this->qb);
}
private function configureResultRows(array $rows): void {
$fetchCalls = array_merge($rows, [false]);
$this->result->method('fetch')
->willReturnOnConsecutiveCalls(...$fetchCalls);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function configureResultSingleRow(array $row): void {
$this->result->method('fetch')
->willReturnOnConsecutiveCalls($row, false);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function configureResultEmpty(): void {
$this->result->method('fetch')->willReturn(false);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function stockRow(int $id = 1, string $name = 'Pfadfinderhemd', ?string $urls = '[{"url":"https://shop.example.com","provider":"shop"}]'): array {
return [
'id' => $id,
'name' => $name,
'provider_urls_json' => $urls,
'created_at' => '2026-04-21 00:00:00',
'updated_at' => '2026-04-21 00:00:00',
];
}
public function testCreateAndFind(): void {
$this->configureResultSingleRow($this->stockRow(42, 'Pfadfinderhemd'));
$mapper = new StockItemMapper($this->db);
$item = $mapper->findById(42);
$this->assertSame(42, $item->getId());
$this->assertSame('Pfadfinderhemd', $item->getName());
}
public function testFindAll(): void {
$this->configureResultRows([
$this->stockRow(1, 'Pfadfinderhemd'),
$this->stockRow(2, 'Pfadfindershirt'),
]);
$mapper = new StockItemMapper($this->db);
$items = $mapper->findAll();
$this->assertCount(2, $items);
$this->assertSame('Pfadfinderhemd', $items[0]->getName());
}
public function testProviderUrlsJsonSerialization(): void {
$this->configureResultSingleRow($this->stockRow(1, 'Hemd', '[{"url":"https://shop.example.com","provider":"shop"},{"url":"https://bestell.example.com","provider":"bestell"}]'));
$mapper = new StockItemMapper($this->db);
$item = $mapper->findById(1);
$json = $item->getProviderUrlsJson();
$this->assertNotNull($json);
$decoded = json_decode($json, true);
$this->assertCount(2, $decoded);
$this->assertSame('https://shop.example.com', $decoded[0]['url']);
$this->assertSame('shop', $decoded[0]['provider']);
}
}
+106
View File
@@ -0,0 +1,106 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Tests\Db;
use OCA\Mitgliederverwaltung\Db\StockVariant;
use OCA\Mitgliederverwaltung\Db\StockVariantMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\IResult;
use OCP\IDBConnection;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class StockVariantMapperTest extends TestCase {
private IDBConnection&MockObject $db;
private IQueryBuilder&MockObject $qb;
private IExpressionBuilder&MockObject $expr;
private IResult&MockObject $result;
protected function setUp(): void {
parent::setUp();
$this->db = $this->createMock(IDBConnection::class);
$this->qb = $this->createMock(IQueryBuilder::class);
$this->expr = $this->createMock(IExpressionBuilder::class);
$this->result = $this->createMock(IResult::class);
$this->qb->method('expr')->willReturn($this->expr);
$this->qb->method('select')->willReturnSelf();
$this->qb->method('from')->willReturnSelf();
$this->qb->method('where')->willReturnSelf();
$this->qb->method('orderBy')->willReturnSelf();
$this->qb->method('createNamedParameter')->willReturn(':param');
$this->qb->method('createFunction')->willReturnCallback(fn($call) => $call);
$this->qb->method('getSQL')->willReturn('SELECT * FROM test');
$this->expr->method('eq')->willReturn('col = :param');
$this->expr->method('isNull')->willReturn('col IS NULL');
$this->db->method('getQueryBuilder')->willReturn($this->qb);
}
private function configureResultRows(array $rows): void {
$fetchCalls = array_merge($rows, [false]);
$this->result->method('fetch')
->willReturnOnConsecutiveCalls(...$fetchCalls);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function configureResultSingleRow(array $row): void {
$this->result->method('fetch')
->willReturnOnConsecutiveCalls($row, false);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function configureResultEmpty(): void {
$this->result->method('fetch')->willReturn(false);
$this->result->method('closeCursor')->willReturn(true);
$this->qb->method('executeQuery')->willReturn($this->result);
}
private function variantRow(int $id = 1, int $stockItemId = 1, string $label = 'Größe M', int $amount = 10, string $cost = '25.00', int $minThreshold = 3): array {
return [
'id' => $id,
'stock_item_id' => $stockItemId,
'label' => $label,
'amount' => $amount,
'cost' => $cost,
'min_threshold' => $minThreshold,
];
}
public function testCreate_attachToStockItem(): void {
$this->configureResultSingleRow($this->variantRow(1, 1, 'Größe M'));
$mapper = new StockVariantMapper($this->db);
$variant = $mapper->findById(1);
$this->assertSame(1, $variant->getId());
$this->assertSame(1, $variant->getStockItemId());
$this->assertSame('Größe M', $variant->getLabel());
}
public function testFindByStockItemId(): void {
$this->configureResultRows([
$this->variantRow(1, 1, 'Größe M'),
$this->variantRow(2, 1, 'Größe L'),
]);
$mapper = new StockVariantMapper($this->db);
$variants = $mapper->findByStockItemId(1);
$this->assertCount(2, $variants);
$this->assertSame('Größe M', $variants[0]->getLabel());
$this->assertSame('Größe L', $variants[1]->getLabel());
}
public function testFindByStockItemIdEmpty(): void {
$this->configureResultRows([]);
$mapper = new StockVariantMapper($this->db);
$variants = $mapper->findByStockItemId(999);
$this->assertCount(0, $variants);
}
}
@@ -6,6 +6,7 @@ namespace OCA\Mitgliederverwaltung\Tests\Integration;
use OCA\Mitgliederverwaltung\Controller\ReportController;
use OCA\Mitgliederverwaltung\Service\EncryptedExportService;
use OCA\Mitgliederverwaltung\Service\InventoryReportService;
use OCA\Mitgliederverwaltung\Service\PdfService;
use OCA\Mitgliederverwaltung\Service\PermissionService;
use OCA\Mitgliederverwaltung\Service\ReportService;
@@ -25,6 +26,7 @@ class ReportControllerTest extends TestCase {
private ReportController $controller;
private ReportService&MockObject $reportService;
private InventoryReportService&MockObject $inventoryReportService;
private PdfService&MockObject $pdfService;
private EncryptedExportService&MockObject $encryptedService;
private PermissionService&MockObject $permissionService;
@@ -36,6 +38,7 @@ class ReportControllerTest extends TestCase {
parent::setUp();
$this->reportService = $this->createMock(ReportService::class);
$this->inventoryReportService = $this->createMock(InventoryReportService::class);
$this->pdfService = $this->createMock(PdfService::class);
$this->encryptedService = $this->createMock(EncryptedExportService::class);
$this->permissionService = $this->createMock(PermissionService::class);
@@ -52,6 +55,7 @@ class ReportControllerTest extends TestCase {
'mitgliederverwaltung',
$this->request,
$this->reportService,
$this->inventoryReportService,
$this->pdfService,
$this->encryptedService,
$this->permissionService,
@@ -0,0 +1,163 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Tests\Service;
use OCA\Mitgliederverwaltung\Db\GeneralMaterial;
use OCA\Mitgliederverwaltung\Db\GeneralMaterialMapper;
use OCA\Mitgliederverwaltung\Db\InventoryCategory;
use OCA\Mitgliederverwaltung\Db\InventoryCategoryMapper;
use OCA\Mitgliederverwaltung\Db\InventoryItemCategory;
use OCA\Mitgliederverwaltung\Db\InventoryItemCategoryMapper;
use OCA\Mitgliederverwaltung\Db\StockItem;
use OCA\Mitgliederverwaltung\Db\StockItemMapper;
use OCA\Mitgliederverwaltung\Db\StockVariant;
use OCA\Mitgliederverwaltung\Db\StockVariantMapper;
use OCA\Mitgliederverwaltung\Db\SaleRecord;
use OCA\Mitgliederverwaltung\Db\SaleRecordMapper;
use OCA\Mitgliederverwaltung\Service\InventoryReportService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class InventoryReportServiceTest extends TestCase {
private InventoryReportService $service;
private MockObject $generalMaterialMapper;
private MockObject $categoryMapper;
private MockObject $itemCategoryMapper;
private MockObject $stockItemMapper;
private MockObject $stockVariantMapper;
private MockObject $saleRecordMapper;
private LoggerInterface&MockObject $logger;
protected function setUp(): void {
parent::setUp();
$this->generalMaterialMapper = $this->createMock(GeneralMaterialMapper::class);
$this->categoryMapper = $this->createMock(InventoryCategoryMapper::class);
$this->itemCategoryMapper = $this->createMock(InventoryItemCategoryMapper::class);
$this->stockItemMapper = $this->createMock(StockItemMapper::class);
$this->stockVariantMapper = $this->createMock(StockVariantMapper::class);
$this->saleRecordMapper = $this->createMock(SaleRecordMapper::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->service = new InventoryReportService(
$this->generalMaterialMapper,
$this->categoryMapper,
$this->itemCategoryMapper,
$this->stockItemMapper,
$this->stockVariantMapper,
$this->saleRecordMapper,
$this->logger
);
}
private function generalMaterialEntity(int $id = 1, string $name = 'Zelt A', ?int $condition = 1): GeneralMaterial {
$entity = new GeneralMaterial();
$entity->setId($id);
$entity->setName($name);
$entity->setCondition($condition);
$entity->setNotes(null);
return $entity;
}
private function categoryEntity(int $id = 1, string $name = 'Zelte'): InventoryCategory {
$entity = new InventoryCategory();
$entity->setId($id);
$entity->setName($name);
return $entity;
}
private function itemCategoryEntity(int $itemId = 1, int $categoryId = 1): InventoryItemCategory {
$entity = new InventoryItemCategory();
$entity->setItemId($itemId);
$entity->setCategoryId($categoryId);
return $entity;
}
private function saleEntity(int $id = 1, int $stockItemId = 1, string $quantity = '2', string $unitPrice = '25.00'): SaleRecord {
$entity = new SaleRecord();
$entity->setId($id);
$entity->setStockItemId($stockItemId);
$entity->setVariantId(null);
$entity->setDate('2026-04-01');
$entity->setQuantity((int)$quantity);
$entity->setUnitPrice($unitPrice);
$entity->setTotalPrice((string)((float)$quantity * (float)$unitPrice));
$entity->setNotes(null);
$entity->setCreatedAt('2026-04-01 00:00:00');
return $entity;
}
public function testGenerateSalesReport_dateFiltered(): void {
$sales = [
$this->saleEntity(1, 1, '2', '25.00'),
$this->saleEntity(2, 1, '1', '30.00'),
];
$this->saleRecordMapper->method('findByDateRange')
->with('2026-01-01', '2026-03-31')
->willReturn($sales);
$report = $this->service->generateSalesReport('2026-01-01', '2026-03-31');
$this->assertStringContainsString('2026-01-01', $report['title']);
$this->assertCount(2, $report['rows']);
$this->assertArrayHasKey('total_sales', $report['summary']);
$this->assertSame(2, (int)$report['summary']['total_sales']);
}
public function testGenerateConditionReport_summaryByCategory(): void {
$item1 = $this->generalMaterialEntity(1, 'Zelt A', 3);
$item2 = $this->generalMaterialEntity(2, 'Zelt B', 4);
$this->generalMaterialMapper->method('findAll')
->willReturn([$item1, $item2]);
$itemCat1 = $this->itemCategoryEntity(1, 1);
$this->itemCategoryMapper->method('findByItemId')
->willReturnCallback(function($itemId) use ($itemCat1) {
if ($itemId === 1) {
return [$itemCat1];
}
return [];
});
$catEntity = $this->categoryEntity(1, 'Zelte');
$this->categoryMapper->method('findById')
->willReturn($catEntity);
// Also need findNeedingRepair to return empty
$this->generalMaterialMapper->method('findNeedingRepair')
->willReturn([]);
$report = $this->service->generateConditionReport();
$this->assertArrayHasKey('Zelte', $report['summary']);
$this->assertSame(1, $report['summary']['Zelte']);
}
public function testGenerateConditionReport_needsRepairOnly(): void {
// One good item, one needing repair
$goodItem = $this->generalMaterialEntity(1, 'Zelt A', 4);
$badItem = $this->generalMaterialEntity(2, 'Zelt B', 1);
$this->generalMaterialMapper->method('findAll')
->willReturn([$goodItem, $badItem]);
$this->itemCategoryMapper->method('findByItemId')
->willReturn([]);
$this->generalMaterialMapper->method('findNeedingRepair')
->willReturn([$badItem]);
$report = $this->service->generateConditionReport();
// Only the bad item should be in rows
$this->assertCount(1, $report['rows']);
$this->assertSame('Zelt B', $report['rows'][0]['name']);
$this->assertSame('1/5', $report['rows'][0]['condition']);
}
}
+160
View File
@@ -0,0 +1,160 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Tests\Service;
use OCA\Mitgliederverwaltung\Db\GeneralMaterial;
use OCA\Mitgliederverwaltung\Db\GeneralMaterialMapper;
use OCA\Mitgliederverwaltung\Db\InventoryCategory;
use OCA\Mitgliederverwaltung\Db\InventoryCategoryMapper;
use OCA\Mitgliederverwaltung\Db\InventoryItemCategory;
use OCA\Mitgliederverwaltung\Db\InventoryItemCategoryMapper;
use OCA\Mitgliederverwaltung\Db\StockItem;
use OCA\Mitgliederverwaltung\Db\StockItemMapper;
use OCA\Mitgliederverwaltung\Service\AuditService;
use OCA\Mitgliederverwaltung\Service\InventoryService;
use OCP\AppFramework\Db\DoesNotExistException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class InventoryServiceTest extends TestCase {
private InventoryService $service;
private InventoryCategoryMapper&MockObject $categoryMapper;
private InventoryItemCategoryMapper&MockObject $itemCategoryMapper;
private GeneralMaterialMapper&MockObject $generalMaterialMapper;
private StockItemMapper&MockObject $stockItemMapper;
private MockObject $stockVariantMapper;
private AuditService&MockObject $auditService;
private LoggerInterface&MockObject $logger;
protected function setUp(): void {
parent::setUp();
$this->categoryMapper = $this->createMock(InventoryCategoryMapper::class);
$this->itemCategoryMapper = $this->createMock(InventoryItemCategoryMapper::class);
$this->generalMaterialMapper = $this->createMock(GeneralMaterialMapper::class);
$this->stockItemMapper = $this->createMock(StockItemMapper::class);
$this->stockVariantMapper = $this->getMockBuilder(\OCA\Mitgliederverwaltung\Db\StockVariantMapper::class)
->disableOriginalConstructor()
->onlyMethods(['findByStockItemId', 'findById', 'insert', 'update', 'delete'])
->getMock();
$this->auditService = $this->createMock(AuditService::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->service = new InventoryService(
$this->categoryMapper,
$this->itemCategoryMapper,
$this->generalMaterialMapper,
$this->stockItemMapper,
$this->stockVariantMapper,
$this->auditService,
$this->logger
);
}
private function generalMaterialEntity(int $id = 1, string $name = 'Zelt A', ?int $condition = 3): GeneralMaterial {
$entity = new GeneralMaterial();
$entity->setId($id);
$entity->setName($name);
$entity->setCondition($condition);
$entity->setNotes(null);
$entity->setCreatedAt('2026-04-21 00:00:00');
$entity->setUpdatedAt('2026-04-21 00:00:00');
return $entity;
}
private function categoryEntity(int $id = 1, string $name = 'Zelte'): InventoryCategory {
$entity = new InventoryCategory();
$entity->setId($id);
$entity->setName($name);
$entity->setCreatedAt('2026-04-21 00:00:00');
return $entity;
}
private function itemCategoryEntity(int $itemId = 1, int $categoryId = 1): InventoryItemCategory {
$entity = new InventoryItemCategory();
$entity->setItemId($itemId);
$entity->setCategoryId($categoryId);
return $entity;
}
public function testCreateGeneralMaterial_auditLogged(): void {
$gm = $this->generalMaterialEntity(1, 'Zelt A', 4);
$this->generalMaterialMapper->method('insert')->willReturnCallback(function ($entity) use ($gm) {
$entity->setId($gm->getId());
});
$this->categoryMapper->method('findById')->willReturn($this->categoryEntity(1, 'Zelte'));
$this->itemCategoryMapper->method('findByItemId')->willReturn([$this->itemCategoryEntity(1, 1)]);
$this->auditService->method('logCreate')->willReturnCallback(function(array $data, string $type, int $id) {
$this->assertSame('Zelt A', $data['name']);
$this->assertSame(4, $data['condition']);
$this->assertSame('inventory_general_material', $type);
$this->assertSame(1, $id);
});
$result = $this->service->createGeneralMaterial([
'name' => 'Zelt A',
'condition' => 4,
'notes' => null,
'categories' => [1],
]);
$this->assertSame('Zelt A', $result->getName());
}
public function testCreateStockItem_auditLogged(): void {
$this->stockItemMapper->method('insert')->willReturnCallback(function ($entity) {
$entity->setId(1);
$entity->setName('Pfadfinderhemd');
});
$this->itemCategoryMapper->method('findByItemId')->willReturn([]);
$this->auditService->method('logCreate')->willReturnCallback(function(array $data, string $type, int $id) {
$this->assertSame('Pfadfinderhemd', $data['name']);
$this->assertSame('inventory_stock_item', $type);
$this->assertSame(1, $id);
});
$result = $this->service->createStockItem([
'name' => 'Pfadfinderhemd',
'categories' => null,
]);
$this->assertSame('Pfadfinderhemd', $result->getName());
}
public function testDelete_auditLogged(): void {
$gm = $this->generalMaterialEntity(1, 'Zelt A');
$this->generalMaterialMapper->method('findById')->willReturn($gm);
$this->auditService->method('logDelete')->willReturnCallback(function(string $type, int $id) {
$this->assertSame('inventory_general_material', $type);
$this->assertSame(1, $id);
});
$this->service->deleteGeneralMaterial(1);
$this->assertTrue(true);
}
public function testConditionAlerts_correctThreshold(): void {
$this->assertFalse($this->service->needsRepair(3));
$this->assertFalse($this->service->needsRepair(4));
$this->assertFalse($this->service->needsRepair(5));
$this->assertTrue($this->service->needsRepair(2));
$this->assertTrue($this->service->needsRepair(1));
$this->assertTrue($this->service->needsRepair(0));
$this->assertTrue($this->service->needsRepair(null));
}
public function testGetConditionLabel(): void {
$this->assertSame('Nicht bewertet', $this->service->getConditionLabel(null));
$this->assertSame('1/5', $this->service->getConditionLabel(1));
$this->assertSame('3/5', $this->service->getConditionLabel(3));
$this->assertSame('5/5', $this->service->getConditionLabel(5));
}
}
+86
View File
@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace OCA\Mitgliederverwaltung\Tests\Service;
use OCA\Mitgliederverwaltung\Db\SaleRecord;
use OCA\Mitgliederverwaltung\Db\SaleRecordMapper;
use OCA\Mitgliederverwaltung\Service\AuditService;
use OCA\Mitgliederverwaltung\Service\SaleService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class SaleServiceTest extends TestCase {
private SaleService $service;
private SaleRecordMapper&MockObject $saleRecordMapper;
private AuditService&MockObject $auditService;
private LoggerInterface&MockObject $logger;
protected function setUp(): void {
parent::setUp();
$this->saleRecordMapper = $this->createMock(SaleRecordMapper::class);
$this->auditService = $this->createMock(AuditService::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->service = new SaleService(
$this->saleRecordMapper,
$this->auditService,
$this->logger
);
}
private function saleEntity(int $id = 1, int $stockItemId = 1, int $variantId = 1, int $quantity = 2, string $unitPrice = '25.00'): SaleRecord {
$entity = new SaleRecord();
$entity->setId($id);
$entity->setStockItemId($stockItemId);
$entity->setVariantId($variantId);
$entity->setDate('2026-04-21');
$entity->setQuantity($quantity);
$entity->setUnitPrice($unitPrice);
$entity->setTotalPrice((string)((float)$unitPrice * $quantity));
$entity->setNotes(null);
$entity->setCreatedAt('2026-04-21 00:00:00');
return $entity;
}
public function testCreateSale_auditLogged(): void {
$this->saleRecordMapper->method('insert')->willReturnCallback(function ($entity) use ($stockItemId = 1) {
$entity->setId(1);
});
$this->auditService->method('logCreate')->willReturnCallback(function(array $data, string $type, int $id) {
$this->assertSame(1, $data['stock_item_id']);
$this->assertSame('inventory_sale', $type);
$this->assertSame(1, $id);
});
$result = $this->service->createSale([
'stock_item_id' => 1,
'variant_id' => 1,
'date' => '2026-04-21',
'quantity' => 2,
'unit_price' => '25.00',
'notes' => null,
]);
$this->assertSame('50.00', $result->getTotalPrice());
}
public function testSaleRecord_totalCalculation(): void {
$sale = $this->saleEntity(1, 1, 1, 3, '15.00');
$expectedTotal = (float)$sale->getQuantity() * (float)$sale->getUnitPrice();
$actualTotal = (float)$sale->getTotalPrice();
$this->assertEqualsWithDelta($expectedTotal, $actualTotal, 0.01);
}
public function testSaleRecord_totalCalculationZero(): void {
$sale = $this->saleEntity(1, 1, 1, 0, '0.00');
$expectedTotal = 0.0;
$actualTotal = (float)$sale->getTotalPrice();
$this->assertEqualsWithDelta($expectedTotal, $actualTotal, 0.01);
}
}
+1 -1
View File
@@ -41,7 +41,7 @@ module.exports = {
new VueLoaderPlugin(),
new webpack.DefinePlugin({
appName: JSON.stringify('mitgliederverwaltung'),
appVersion: JSON.stringify('0.3.1'),
appVersion: JSON.stringify('0.3.2'),
}),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,