fixed warehouse article

This commit is contained in:
2025-12-17 17:00:53 +01:00
parent 37b0a22b33
commit 9680ff004e
3 changed files with 230 additions and 49 deletions

View File

@@ -56,12 +56,16 @@ class WarehouseArticleController extends TTCrud {
protected function beforeCreate() {
if (!in_array($this->user->id, [2, 5, 6, 145, 14]))
self::sendError("Sie haben keine Berechtigung, Artikel zu erstellen.");
$this->validateArticleNumber($_POST);
return true;
}
protected function beforeUpdate($postData): bool {
if (!in_array($this->user->id, [2, 5, 6, 145, 14]))
self::sendError("Sie haben keine Berechtigung, Artikel zu bearbeiten.");
$this->validateArticleNumber($postData, $postData['id'] ?? null);
(new WarehouseHistoryController)->create($postData, $this->mod);
return true;
}
@@ -84,6 +88,38 @@ class WarehouseArticleController extends TTCrud {
self::updateSellPrices($postData['id']);
}
/**
* Validate article number for duplicates and correct category prefix
*/
private function validateArticleNumber(array $postData, ?int $excludeId = null): void {
$articleNumber = $postData['articleNumber'] ?? '';
$categoryId = $postData['category_id'] ?? null;
if (empty($articleNumber)) {
self::sendError("Artikelnummer ist erforderlich.");
}
// Check for duplicate article number
$existingArticles = WarehouseArticleModel::getAll(['articleNumber' => $articleNumber]);
foreach ($existingArticles as $existing) {
if ($excludeId === null || $existing->id != $excludeId) {
self::sendError("Artikelnummer '{$articleNumber}' existiert bereits (Artikel ID: {$existing->id}).");
}
}
// Validate category prefix
if ($categoryId) {
$category = WarehouseCategory::get($categoryId);
if ($category && $category->articleNumberPrefix) {
$expectedPrefix = $category->articleNumberPrefix;
$articlePrefix = substr($articleNumber, 0, strlen($expectedPrefix));
if ($articlePrefix !== $expectedPrefix) {
self::sendError("Artikelnummer muss mit dem Kategorie-Prefix '{$expectedPrefix}' beginnen.");
}
}
}
}
public static function updateSellPrices(int $id): void { // Added return type hint
$a = WarehouseArticleModel::get($id);
if (!$a instanceof WarehouseArticleModel) throw new Exception("Invalid article type");