171 lines
8.4 KiB
PHP
171 lines
8.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Class WarehouseOrderRecommendationModel
|
|
*
|
|
* This class represents a recommendation model for warehouse orders based on the critical and warning amounts of articles in the warehouse.
|
|
*
|
|
* @property int $articleId The ID of the article.
|
|
* @property string $articleTitle The title of the article.
|
|
* @property int $count The current count of items for the article.
|
|
* @property string $status The status of the article's inventory ("Kritisch" or "Warnung").
|
|
* @property string $recommendation The recommendation message for ordering.
|
|
*/
|
|
class WarehouseOrderRecommendationModel {
|
|
public int $articleId;
|
|
public ?int $locationId;
|
|
public string $locationTitle;
|
|
public string $articleTitle;
|
|
public int $count;
|
|
public string $status;
|
|
public string $recommendation;
|
|
|
|
/**
|
|
* WarehouseOrderRecommendationModel constructor.
|
|
*
|
|
* Initializes a new instance of the WarehouseOrderRecommendationModel class.
|
|
*
|
|
* @param array $data An associative array of property values to initialize the model.
|
|
*/
|
|
public function __construct(array $data = []) {
|
|
foreach ($data as $field => $value) {
|
|
if (property_exists(get_called_class(), $field)) {
|
|
$this->$field = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves recommendations based on the provided filter.
|
|
*
|
|
* @param array $filter An associative array of filter options.
|
|
* @return array An array of WarehouseOrderRecommendationModel instances.
|
|
*/
|
|
private static function customGet(array $filter = []): array {
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
|
|
$filterOptions = [];
|
|
|
|
// Convert filter to filterOptions because the filter is not directly usable
|
|
if (isset($filter['articleTitle'])) {
|
|
$filterOptions['title'] = $filter['articleTitle'];
|
|
}
|
|
|
|
$articles = [];
|
|
foreach (WarehouseArticleModel::getAll($filterOptions) as $article) {
|
|
$articles[$article->id] = $article;
|
|
}
|
|
|
|
$items = WarehouseItemModel::getAll();
|
|
|
|
$recommendations = [];
|
|
// Check if the amount of items is below the critical or warning amount
|
|
foreach ($articles as $article) {
|
|
$items = array_filter($items, function ($item) use ($article) {
|
|
return $item->articleId === $article->id;
|
|
});
|
|
|
|
$count = array_reduce($items, function ($carry, $item) {
|
|
return $carry + $item->quantity;
|
|
}, 0);
|
|
|
|
if ($count <= $article->criticalAmount) {
|
|
$minOrderAmount = $article->criticalAmount - $count;
|
|
$recommendedOrderAmount = $article->warningAmount - $count;
|
|
$recommendations[] = new WarehouseOrderRecommendationModel(["articleId" => $article->id,
|
|
"articleTitle" => $article->title,
|
|
"count" => $count,
|
|
"status" => "Kritisch",
|
|
"recommendation" => "Bestellen, min. $minOrderAmount, empfohlen: $recommendedOrderAmount"]);
|
|
} else if ($count <= $article->warningAmount) {
|
|
$minOrderAmount = $article->warningAmount - $count;
|
|
$recommendations[] = new WarehouseOrderRecommendationModel(["articleId" => $article->id,
|
|
"articleTitle" => $article->title,
|
|
"count" => $count,
|
|
"status" => "Warnung",
|
|
"recommendation" => "Bestellen, min. $minOrderAmount"]);
|
|
}
|
|
}
|
|
|
|
$locations = [];
|
|
foreach (WarehouseLocationModel::getAll() as $location) {
|
|
$locations[$location->id] = $location;
|
|
}
|
|
|
|
$locationThresholdOverrides = WarehouseLocationThresholdOverrideModel::getAll();
|
|
|
|
foreach ($locationThresholdOverrides as $locationThresholdOverride) {
|
|
if (!isset($articles[$locationThresholdOverride->articleId])) {
|
|
continue;
|
|
}
|
|
|
|
$items = array_filter($items, function ($item) use ($locationThresholdOverride) {
|
|
return $item->articleId === $locationThresholdOverride->articleId;
|
|
});
|
|
|
|
$count = array_reduce($items, function ($carry, $item) {
|
|
return $carry + $item->quantity;
|
|
}, 0);
|
|
|
|
if ($count <= $locationThresholdOverride->criticalAmount) {
|
|
$minOrderAmount = $locationThresholdOverride->criticalAmount - $count;
|
|
$recommendedOrderAmount = $locationThresholdOverride->warningAmount - $count;
|
|
$recommendations[] = new WarehouseOrderRecommendationModel(["articleId" => $locationThresholdOverride->articleId,
|
|
"articleTitle" => $articles[$locationThresholdOverride->articleId]->title,
|
|
"locationId" => $locationThresholdOverride->locationId,
|
|
"locationTitle" => $locations[$locationThresholdOverride->locationId]->title,
|
|
"count" => $count,
|
|
"status" => "Kritisch",
|
|
"recommendation" => "Bestellen, min. $minOrderAmount, empfohlen: $recommendedOrderAmount"]);
|
|
} else if ($count <= $locationThresholdOverride->warningAmount) {
|
|
$minOrderAmount = $locationThresholdOverride->warningAmount - $count;
|
|
$recommendations[] = new WarehouseOrderRecommendationModel(["articleId" => $locationThresholdOverride->articleId,
|
|
"articleTitle" => $articles[$locationThresholdOverride->articleId]->title,
|
|
"locationId" => $locationThresholdOverride->locationId,
|
|
"locationTitle" => $locations[$locationThresholdOverride->locationId]->title,
|
|
"count" => $count,
|
|
"status" => "Warnung",
|
|
"recommendation" => "Bestellen, min. $minOrderAmount"]);
|
|
}
|
|
}
|
|
|
|
return $recommendations;
|
|
}
|
|
|
|
/**
|
|
* Counts the number of recommendations based on the provided filter.
|
|
*
|
|
* @param array $filter An associative array of filter options.
|
|
* @return int The number of recommendations.
|
|
*/
|
|
public static function count(array $filter = []): int {
|
|
return count(self::customGet($filter));
|
|
}
|
|
|
|
/**
|
|
* Retrieves all recommendations based on the provided filter, limit, offset, and order.
|
|
*
|
|
* @param array $filter An associative array of filter options.
|
|
* @param int|null $limit The maximum number of recommendations to return.
|
|
* @param int $offset The number of recommendations to skip.
|
|
* @param array $order An associative array containing the key to sort by and the order direction ('ASC' or 'DESC').
|
|
* @return array An array of WarehouseOrderRecommendationModel instances.
|
|
*/
|
|
public static function getAll(array $filter = [], int $limit = null, int $offset = 0, array $order = ["key" => null]): array {
|
|
$recommendations = self::customGet($filter);
|
|
|
|
// apply limit,offset and order
|
|
if ($order['key'] !== null) {
|
|
usort($recommendations, function ($a, $b) use ($order) {
|
|
return $order['order'] === 'ASC' ? $a->{$order['key']} <=> $b->{$order['key']} : $b->{$order['key']} <=> $a->{$order['key']};
|
|
});
|
|
}
|
|
|
|
if ($limit !== null) {
|
|
return array_slice($recommendations, $offset, $limit);
|
|
}
|
|
return $recommendations;
|
|
}
|
|
|
|
} |