Little changes for pull request #338

This commit is contained in:
Bernd Bestel 2019-08-10 08:20:52 +02:00
parent c757ee3874
commit cc1c6a6442
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
4 changed files with 27 additions and 16 deletions

View File

@ -165,7 +165,7 @@ class StockApiController extends BaseApiController
throw new \Exception('An new amount is required'); throw new \Exception('An new amount is required');
} }
$bestBeforeDate = date('Y-m-d'); $bestBeforeDate = null;
if (array_key_exists('best_before_date', $requestBody) && IsIsoDate($requestBody['best_before_date'])) if (array_key_exists('best_before_date', $requestBody) && IsIsoDate($requestBody['best_before_date']))
{ {
$bestBeforeDate = $requestBody['best_before_date']; $bestBeforeDate = $requestBody['best_before_date'];
@ -290,7 +290,7 @@ class StockApiController extends BaseApiController
public function RemoveFromShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function RemoveItemFromShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
try try
{ {

View File

@ -1534,9 +1534,9 @@
} }
} }
}, },
"/stock/shoppinglist/remove-from-list": { "/stock/shoppinglist/remove-product": {
"post": { "post": {
"summary": "Removes a product from the shoppinglist, if it is on it", "summary": "Removes the given product from the given shopping list, if it is on it",
"tags": [ "tags": [
"Stock" "Stock"
], ],
@ -1549,21 +1549,21 @@
"properties": { "properties": {
"product_id": { "product_id": {
"type": "integer", "type": "integer",
"description": "The product id from the item on the shopping list" "description": "A valid product id of the item on the shopping list"
}, },
"list_id": { "list_id": {
"type": "integer", "type": "integer",
"description": "The shopping list id to use, when omitted, the default shopping list (with id 1) is used" "description": "A valid shopping list id, when omitted, the default shopping list (with id 1) is used"
}, },
"product_amount": { "product_amount": {
"type": "integer", "type": "integer",
"description": "The amount of prodcut units to remove, when omitted, the default amount of 1 is used" "description": "The amount of product units to remove, when omitted, the default amount of 1 is used"
} }
}, },
"example": { "example": {
"product_id": 3, "product_id": 3,
"list_id": 2, "list_id": 2,
"product_amount": 5, "product_amount": 5
} }
} }
} }
@ -1574,7 +1574,7 @@
"description": "The operation was successful" "description": "The operation was successful"
}, },
"400": { "400": {
"description": "The operation was not successful (possible errors are: Not existing shopping list, No product id supplied)", "description": "The operation was not successful (possible errors are: Not existing shopping list, Invalid product id supplied)",
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {

View File

@ -167,7 +167,7 @@ $app->group('/api', function()
{ {
$this->post('/stock/shoppinglist/add-missing-products', '\Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList'); $this->post('/stock/shoppinglist/add-missing-products', '\Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList');
$this->post('/stock/shoppinglist/clear', '\Grocy\Controllers\StockApiController:ClearShoppingList'); $this->post('/stock/shoppinglist/clear', '\Grocy\Controllers\StockApiController:ClearShoppingList');
$this->post('/stock/shoppinglist/remove-from-list', '\Grocy\Controllers\StockApiController:RemoveFromShoppingList'); $this->post('/stock/shoppinglist/remove-product', '\Grocy\Controllers\StockApiController:RemoveItemFromShoppingList');
} }
// Recipes // Recipes

View File

@ -146,7 +146,7 @@ class StockService extends BaseService
} }
} }
public function AddProduct(int $productId, float $amount, string $bestBeforeDate, $transactionType, $purchasedDate, $price, $locationId = null) public function AddProduct(int $productId, float $amount, $bestBeforeDate, $transactionType, $purchasedDate, $price, $locationId = null)
{ {
if (!$this->ProductExists($productId)) if (!$this->ProductExists($productId))
{ {
@ -170,10 +170,17 @@ class StockService extends BaseService
//Sets the default best before date, if none is supplied //Sets the default best before date, if none is supplied
if ($bestBeforeDate == null) if ($bestBeforeDate == null)
{ {
if ($productDetails->product->default_best_before_days == -1) { if (intval($productDetails->product->default_best_before_days) == -1)
{
$bestBeforeDate = date('2999-12-31'); $bestBeforeDate = date('2999-12-31');
} else { }
$bestBeforeDate = date('Y-m-d', strtotime(date('Y-m-d') . ' + '.$productDetails->product->default_best_before_days.' days')); else if (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'));
}
else
{
$bestBeforeDate = date('Y-m-d');
} }
} }
@ -313,7 +320,7 @@ class StockService extends BaseService
} }
} }
public function InventoryProduct(int $productId, int $newAmount, string $bestBeforeDate, $locationId = null, $price = null) public function InventoryProduct(int $productId, int $newAmount, $bestBeforeDate, $locationId = null, $price = null)
{ {
if (!$this->ProductExists($productId)) if (!$this->ProductExists($productId))
{ {
@ -513,7 +520,9 @@ class StockService extends BaseService
{ {
throw new \Exception('Shopping list does not exist'); throw new \Exception('Shopping list does not exist');
} }
$productRow = $this->Database->shopping_list()->where('product_id = :1', $productId)->fetch(); $productRow = $this->Database->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)) if ($productRow != null && !empty($productRow))
{ {
@ -521,7 +530,9 @@ class StockService extends BaseService
if ($newAmount < 1) if ($newAmount < 1)
{ {
$productRow->delete(); $productRow->delete();
} else { }
else
{
$productRow->update(array('amount' => $newAmount)); $productRow->update(array('amount' => $newAmount));
} }