Applied PHP-CS-Fixer rules

This commit is contained in:
Bernd Bestel
2020-09-01 21:29:47 +02:00
parent 3da8904cba
commit 836bcc82e5
56 changed files with 150 additions and 467 deletions

View File

@@ -5,21 +5,13 @@ namespace Grocy\Services;
class StockService extends BaseService
{
const TRANSACTION_TYPE_CONSUME = 'consume';
const TRANSACTION_TYPE_INVENTORY_CORRECTION = 'inventory-correction';
const TRANSACTION_TYPE_PRODUCT_OPENED = 'product-opened';
const TRANSACTION_TYPE_PURCHASE = 'purchase';
const TRANSACTION_TYPE_SELF_PRODUCTION = 'self-production';
const TRANSACTION_TYPE_STOCK_EDIT_NEW = 'stock-edit-new';
const TRANSACTION_TYPE_STOCK_EDIT_OLD = 'stock-edit-old';
const TRANSACTION_TYPE_TRANSFER_FROM = 'transfer_from';
const TRANSACTION_TYPE_TRANSFER_TO = 'transfer_to';
public function AddMissingProductsToShoppingList($listId = 1)
@@ -38,8 +30,8 @@ class StockService extends BaseService
$alreadyExistingEntry = $this->getDatabase()->shopping_list()->where('product_id', $missingProduct->id)->fetch();
if ($alreadyExistingEntry) // Update
{
if ($alreadyExistingEntry)
{ // Update
if ($alreadyExistingEntry->amount < $amountToAdd)
{
$alreadyExistingEntry->update([
@@ -47,10 +39,9 @@ class StockService extends BaseService
'shopping_list_id' => $listId
]);
}
}
else // Insert
{
else
{ // Insert
$shoppinglistRow = $this->getDatabase()->shopping_list()->createRow([
'product_id' => $missingProduct->id,
'amount' => $amountToAdd,
@@ -58,9 +49,7 @@ class StockService extends BaseService
]);
$shoppinglistRow->save();
}
}
}
public function AddProduct(int $productId, float $amount, $bestBeforeDate, $transactionType, $purchasedDate, $price, $quFactorPurchaseToStock, $locationId = null, $shoppingLocationId = null, &$transactionId = null)
@@ -70,9 +59,9 @@ class StockService extends BaseService
throw new \Exception('Product does not exist or is inactive');
}
// Tare weight handling
// Tare weight handling
// The given amount is the new total amount including the container weight (gross)
// The given amount is the new total amount including the container weight (gross)
// The amount to be posted needs to be the given amount - stock amount - tare weight
$productDetails = (object) $this->GetProductDetails($productId);
@@ -86,14 +75,14 @@ class StockService extends BaseService
$amount = $amount - floatval($productDetails->stock_amount) - floatval($productDetails->product->tare_weight);
}
//Sets the default best before date, if none is supplied
//Sets the default best before date, if none is supplied
if ($bestBeforeDate == null)
{
if (intval($productDetails->product->default_best_before_days) == -1)
{
$bestBeforeDate = date('2999-12-31');
}
else if (intval($productDetails->product->default_best_before_days) > 0)
elseif (intval($productDetails->product->default_best_before_days) > 0)
{
$bestBeforeDate = date('Y-m-d', strtotime(date('Y-m-d') . ' + ' . $productDetails->product->default_best_before_days . ' days'));
}
@@ -101,7 +90,6 @@ class StockService extends BaseService
{
$bestBeforeDate = date('Y-m-d');
}
}
if ($transactionType === self::TRANSACTION_TYPE_PURCHASE || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION || $transactionType == self::TRANSACTION_TYPE_SELF_PRODUCTION)
@@ -149,7 +137,6 @@ class StockService extends BaseService
{
throw new \Exception("Transaction type $transactionType is not valid (StockService.AddProduct)");
}
}
public function AddProductToShoppingList($productId, $amount = 1, $note = null, $listId = 1)
@@ -165,16 +152,16 @@ class StockService extends BaseService
}
$alreadyExistingEntry = $this->getDatabase()->shopping_list()->where('product_id = :1 AND shopping_list_id = :2', $productId, $listId)->fetch();
if ($alreadyExistingEntry) // Update
{
if ($alreadyExistingEntry)
{ // Update
$alreadyExistingEntry->update([
'amount' => ($alreadyExistingEntry->amount + $amount),
'shopping_list_id' => $listId,
'note' => $note
]);
}
else // Insert
{
else
{ // Insert
$shoppinglistRow = $this->getDatabase()->shopping_list()->createRow([
'product_id' => $productId,
'amount' => $amount,
@@ -183,7 +170,6 @@ class StockService extends BaseService
]);
$shoppinglistRow->save();
}
}
public function ClearShoppingList($listId = 1)
@@ -208,9 +194,9 @@ class StockService extends BaseService
throw new \Exception('Location does not exist');
}
// Tare weight handling
// Tare weight handling
// The given amount is the new total amount including the container weight (gross)
// The given amount is the new total amount including the container weight (gross)
// The amount to be posted needs to be the absolute value of the given amount - stock amount - tare weight
$productDetails = (object) $this->GetProductDetails($productId);
@@ -226,12 +212,12 @@ class StockService extends BaseService
if ($transactionType === self::TRANSACTION_TYPE_CONSUME || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION)
{
if ($locationId === null) // Consume from any location
{
if ($locationId === null)
{ // Consume from any location
$potentialStockEntries = $this->GetProductStockEntries($productId, false, $allowSubproductSubstitution);
}
else // Consume only from the supplied location
{
else
{ // Consume only from the supplied location
$potentialStockEntries = $this->GetProductStockEntriesForLocation($productId, $locationId, false, $allowSubproductSubstitution);
}
@@ -259,8 +245,8 @@ class StockService extends BaseService
break;
}
if ($amount >= $stockEntry->amount) // Take the whole stock entry
{
if ($amount >= $stockEntry->amount)
{ // Take the whole stock entry
$logRow = $this->getDatabase()->stock_log()->createRow([
'product_id' => $stockEntry->product_id,
'amount' => $stockEntry->amount * -1,
@@ -281,8 +267,8 @@ class StockService extends BaseService
$amount -= $stockEntry->amount;
}
else // Stock entry amount is > than needed amount -> split the stock entry resp. update the amount
{
else
{ // Stock entry amount is > than needed amount -> split the stock entry resp. update the amount
$restStockAmount = $stockEntry->amount - $amount;
$logRow = $this->getDatabase()->stock_log()->createRow([
@@ -307,7 +293,6 @@ class StockService extends BaseService
$amount = 0;
}
}
return $this->getDatabase()->lastInsertId();
@@ -316,7 +301,6 @@ class StockService extends BaseService
{
throw new Exception("Transaction type $transactionType is not valid (StockService.ConsumeProduct)");
}
}
public function EditStockEntry(int $stockRowId, float $amount, $bestBeforeDate, $locationId, $shoppingLocationId, $price, $open, $purchasedDate, $quFactorPurchaseToStock)
@@ -354,9 +338,7 @@ class StockService extends BaseService
{
$openedDate = date('Y-m-d');
}
else
if (!$open)
elseif (!$open)
{
$openedDate = null;
}
@@ -399,8 +381,8 @@ class StockService extends BaseService
$plugin = $this->LoadBarcodeLookupPlugin();
$pluginOutput = $plugin->Lookup($barcode);
if ($pluginOutput !== null) // Lookup was successful
{
if ($pluginOutput !== null)
{ // Lookup was successful
if ($addFoundProduct === true)
{
// Add product to database and include new product id in output
@@ -409,7 +391,6 @@ class StockService extends BaseService
$pluginOutput['id'] = $newRow->id;
}
}
return $pluginOutput;
@@ -466,7 +447,6 @@ class StockService extends BaseService
{
return $this->getDatabase()->uihelper_stock_current_overview_including_opened();
}
}
public function GetExpiringProducts(int $days = 5, bool $excludeExpired = false)
@@ -605,7 +585,7 @@ class StockService extends BaseService
public function GetProductStockEntries($productId, $excludeOpened = false, $allowSubproductSubstitution = false, $ordered = true)
{
// In order of next use:
// In order of next use:
// First expiring first, then first in first out
$sqlWhereProductId = 'product_id = :1';
@@ -623,7 +603,9 @@ class StockService extends BaseService
}
$result = $this->getDatabase()->stock()->where($sqlWhereProductId . ' ' . $sqlWhereAndOpen, $productId);
if ($ordered)
{
return $result->orderBy('best_before_date', 'ASC')->orderBy('purchased_date', 'ASC');
}
return $result;
}
@@ -662,9 +644,9 @@ class StockService extends BaseService
$shoppingLocationId = $productDetails->last_shopping_location_id;
}
// Tare weight handling
// Tare weight handling
// The given amount is the new total amount including the container weight (gross)
// The given amount is the new total amount including the container weight (gross)
// So assume that the amount in stock is the amount also including the container weight
$containerWeight = 0;
@@ -677,9 +659,7 @@ class StockService extends BaseService
{
throw new \Exception('The new amount cannot equal the current stock amount');
}
else
if ($newAmount > floatval($productDetails->stock_amount) + $containerWeight)
elseif ($newAmount > floatval($productDetails->stock_amount) + $containerWeight)
{
$bookingAmount = $newAmount - floatval($productDetails->stock_amount);
@@ -690,9 +670,7 @@ class StockService extends BaseService
return $this->AddProduct($productId, $bookingAmount, $bestBeforeDate, self::TRANSACTION_TYPE_INVENTORY_CORRECTION, date('Y-m-d'), $price, $locationId, $shoppingLocationId);
}
else
if ($newAmount < $productDetails->stock_amount + $containerWeight)
elseif ($newAmount < $productDetails->stock_amount + $containerWeight)
{
$bookingAmount = $productDetails->stock_amount - $newAmount;
@@ -747,8 +725,8 @@ class StockService extends BaseService
$newBestBeforeDate = date('Y-m-d', strtotime('+' . $product->default_best_before_days_after_open . ' days'));
}
if ($amount >= $stockEntry->amount) // Mark the whole stock entry as opened
{
if ($amount >= $stockEntry->amount)
{ // Mark the whole stock entry as opened
$logRow = $this->getDatabase()->stock_log()->createRow([
'product_id' => $stockEntry->product_id,
'amount' => $stockEntry->amount,
@@ -772,8 +750,8 @@ class StockService extends BaseService
$amount -= $stockEntry->amount;
}
else // Stock entry amount is > than needed amount -> split the stock entry
{
else
{ // Stock entry amount is > than needed amount -> split the stock entry
$restStockAmount = $stockEntry->amount - $amount;
$newStockRow = $this->getDatabase()->stock()->createRow([
@@ -812,7 +790,6 @@ class StockService extends BaseService
$amount = 0;
}
}
return $this->getDatabase()->lastInsertId();
@@ -827,7 +804,7 @@ class StockService extends BaseService
$productRow = $this->getDatabase()->shopping_list()->where('product_id = :1', $productId)->fetch();
//If no entry was found with for this product, we return gracefully
//If no entry was found with for this product, we return gracefully
if ($productRow != null && !empty($productRow))
{
$newAmount = $productRow->amount - $amount;
@@ -839,9 +816,7 @@ class StockService extends BaseService
{
$productRow->update(['amount' => $newAmount]);
}
}
}
public function TransferProduct(int $productId, float $amount, int $locationIdFrom, int $locationIdTo, $specificStockEntryId = 'default', &$transactionId = null)
@@ -861,9 +836,9 @@ class StockService extends BaseService
throw new \Exception('Destination location does not exist');
}
// Tare weight handling
// Tare weight handling
// The given amount is the new total amount including the container weight (gross)
// The given amount is the new total amount including the container weight (gross)
// The amount to be posted needs to be the absolute value of the given amount - stock amount - tare weight
$productDetails = (object) $this->GetProductDetails($productId);
@@ -871,7 +846,6 @@ class StockService extends BaseService
{
// Hard fail for now, as we not yet support transfering tare weight enabled products
throw new \Exception('Transfering tare weight enabled products is not yet possible');
if ($amount < floatval($productDetails->product->tare_weight))
{
throw new \Exception('The amount cannot be lower than the defined tare weight');
@@ -912,23 +886,22 @@ class StockService extends BaseService
$locationFrom = $this->getDatabase()->locations()->where('id', $locationIdFrom)->fetch();
$locationTo = $this->getDatabase()->locations()->where('id', $locationIdTo)->fetch();
// Product was moved from a non-freezer to freezer location -> freeze
// Product was moved from a non-freezer to freezer location -> freeze
if (intval($locationFrom->is_freezer) === 0 && intval($locationTo->is_freezer) === 1 && $productDetails->product->default_best_before_days_after_freezing > 0)
{
$newBestBeforeDate = date('Y-m-d', strtotime('+' . $productDetails->product->default_best_before_days_after_freezing . ' days'));
}
// Product was moved from a freezer to non-freezer location -> thaw
// Product was moved from a freezer to non-freezer location -> thaw
if (intval($locationFrom->is_freezer) === 1 && intval($locationTo->is_freezer) === 0 && $productDetails->product->default_best_before_days_after_thawing > 0)
{
$newBestBeforeDate = date('Y-m-d', strtotime('+' . $productDetails->product->default_best_before_days_after_thawing . ' days'));
}
}
$correlationId = uniqid();
if ($amount >= $stockEntry->amount) // Take the whole stock entry
{
if ($amount >= $stockEntry->amount)
{ // Take the whole stock entry
$logRowForLocationFrom = $this->getDatabase()->stock_log()->createRow([
'product_id' => $stockEntry->product_id,
'amount' => $stockEntry->amount * -1,
@@ -968,8 +941,8 @@ class StockService extends BaseService
$amount -= $stockEntry->amount;
}
else // Stock entry amount is > than needed amount -> split the stock entry resp. update the amount
{
else
{ // Stock entry amount is > than needed amount -> split the stock entry resp. update the amount
$restStockAmount = $stockEntry->amount - $amount;
$logRowForLocationFrom = $this->getDatabase()->stock_log()->createRow([
@@ -1026,7 +999,6 @@ class StockService extends BaseService
$amount = 0;
}
}
return $this->getDatabase()->lastInsertId();
@@ -1041,7 +1013,7 @@ class StockService extends BaseService
throw new \Exception('Booking does not exist or was already undone');
}
// Undo all correlated bookings first, in order from newest first to the oldest
// Undo all correlated bookings first, in order from newest first to the oldest
if (!$skipCorrelatedBookings && !empty($logRow->correlation_id))
{
$correlatedBookings = $this->getDatabase()->stock_log()->where('undone = 0 AND correlation_id = :1', $logRow->correlation_id)->orderBy('id', 'DESC')->fetchAll();
@@ -1122,7 +1094,7 @@ class StockService extends BaseService
}
elseif ($logRow->transaction_type === self::TRANSACTION_TYPE_TRANSFER_FROM)
{
// Add corresponding amount back to stock or
// Add corresponding amount back to stock or
// create a row if missing
$stockRow = $this->getDatabase()->stock()->where('stock_id = :1 AND location_id = :2', $logRow->stock_id, $logRow->location_id)->fetch();
@@ -1214,7 +1186,6 @@ class StockService extends BaseService
{
throw new \Exception('This booking cannot be undone');
}
}
public function UndoTransaction($transactionId)
@@ -1230,7 +1201,6 @@ class StockService extends BaseService
{
$this->UndoBooking($transactionBooking->id, true);
}
}
private function LoadBarcodeLookupPlugin()
@@ -1253,7 +1223,6 @@ class StockService extends BaseService
{
throw new \Exception("Plugin $pluginName was not found");
}
}
private function LocationExists($locationId)
@@ -1273,5 +1242,4 @@ class StockService extends BaseService
$shoppingListRow = $this->getDatabase()->shopping_lists()->where('id = :1', $listId)->fetch();
return $shoppingListRow !== null;
}
}