diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 8f6762e8..a255a6cb 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -307,15 +307,23 @@ class StockApiController extends BaseApiController } $default_location_id = $this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'product_presets_location_id'); - $default_qu_id = $this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'product_presets_qu_id'); - if (!$default_location_id) + { $default_location_id = $this->getDatabase()->locations()->limit(1)->fetch()['id']; - + } + + $default_qu_id = $this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'product_presets_qu_id'); if (!$default_qu_id) + { $default_qu_id = $this->getDatabase()->quantity_units()->limit(1)->fetch()['id']; + } - $shopping_location_id = array_key_exists('shopping_location_id', $requestBody) ? $requestBody['shopping_location_id'] : null; + $shopping_location_id = null; + if (array_key_exists('shopping_location_id', $requestBody)) + { + $shopping_location_id = $requestBody['shopping_location_id']; + } + $parsedData = json_decode($requestBody['json-data'], true); $lastInsertId = $this->getStockService()->AddMultipleProducts($parsedData, $default_qu_id, diff --git a/controllers/StockController.php b/controllers/StockController.php index ede428e7..e88e9d4c 100644 --- a/controllers/StockController.php +++ b/controllers/StockController.php @@ -73,23 +73,31 @@ class StockController extends BaseController 'locations' => $this->getDatabase()->locations()->orderBy('name') ]); } - + public function UploadJson(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { $location_id = $this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'product_presets_location_id'); $location = null; if ($location_id > 0) + { $location = $this->getDatabase()->locations()->where('id', $location_id).fetch(); + } else + { $location = $this->getDatabase()->locations()->limit(1)->fetch(); - + } + $qu_id = $this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'product_presets_location_id'); $quantity_unit = null; if ($qu_id > 0) + { $quantity_unit = $this->getDatabase()->quantity_units()->where('id', $qui_id).fetch(); + } else + { $quantity_unit = $this->getDatabase()->quantity_units()->limit(1)->fetch(); - + } + return $this->renderPage($response, 'uploadjson', [ 'location' => $location, 'quantityunit' => $quantity_unit, diff --git a/helpers/KrogerToGrocyConverter.php b/helpers/KrogerToGrocyConverter.php index 0e2501d7..9835ed95 100644 --- a/helpers/KrogerToGrocyConverter.php +++ b/helpers/KrogerToGrocyConverter.php @@ -6,10 +6,10 @@ class KrogerToGrocyConverter { // Fields returned: 'name', 'location_id', 'qu_id_purchase', 'qu_id_stock', // 'qu_factor_purchase_to_stock', 'barcode', 'default_best_before_days' - // 'quantity', 'transaction_date', 'price_paid', 'picture_url' + // 'quantity', 'transaction_date', 'price_paid', 'picture_url', 'min_stock_amount' public static function ConvertJson($data, $default_quantity_units, $default_location_id) { - if (array_key_exists("data", $data)) + if ($data != null && array_key_exists("data", $data)) { $data = $data['data']; } diff --git a/tests/KrogerToGrocyConverterTest.php b/tests/KrogerToGrocyConverterTest.php index b86b4149..8c419a18 100644 --- a/tests/KrogerToGrocyConverterTest.php +++ b/tests/KrogerToGrocyConverterTest.php @@ -17,35 +17,35 @@ final class KrogerToGrocyConverterTest extends TestCase $this->assertEquals( "0072220005165", KrogerToGrocyConverter::ConvertUpcToBarcode("0000007222000516") - ); - - $this->assertEquals( - "0000000041300", - KrogerToGrocyConverter::ConvertUpcToBarcode("0000000004130") - ); - } - - public function testConvertJson(): void - { - $testjson = file_get_contents(dirname(__FILE__) . "/testdata/receipts.json"); - $default_quantity_units = 3; - $default_location_id = 2; + ); + + $this->assertEquals( + "0000000041300", + KrogerToGrocyConverter::ConvertUpcToBarcode("0000000004130") + ); + } + + public function testConvertJson(): void + { + $testjson = file_get_contents(dirname(__FILE__) . "/testdata/receipts.json"); + $default_quantity_units = 3; + $default_location_id = 2; - $products = KrogerToGrocyConverter::ConvertJson(json_decode($testjson, true), $default_quantity_units, $default_location_id); + $products = KrogerToGrocyConverter::ConvertJson(json_decode($testjson, true), $default_quantity_units, $default_location_id); - $expectedjson = file_get_contents(dirname(__FILE__) . "/testdata/receipts_expected.json"); - $expected = json_decode($expectedjson, true); + $expectedjson = file_get_contents(dirname(__FILE__) . "/testdata/receipts_expected.json"); + $expected = json_decode($expectedjson, true); - $this->assertEquals(count($expected), count($products)); + $this->assertEquals(count($expected), count($products)); - foreach ($expected as $index => $product) - { - foreach ($product as $key => $value) - { - $this->assertEquals($value, $products[$index][$key], "Failed matching key " . $key); - } - } - } + foreach ($expected as $index => $product) + { + foreach ($product as $key => $value) + { + $this->assertEquals($value, $products[$index][$key], "Failed matching key " . $key); + } + } + } } ?> \ No newline at end of file diff --git a/version.json b/version.json index 1f57e4d1..1286133c 100644 --- a/version.json +++ b/version.json @@ -1,4 +1,4 @@ { - "Version": "2.6.3", + "Version": "2.6.2", "ReleaseDate": "2020-03-29" } diff --git a/views/layout/default.blade.php b/views/layout/default.blade.php index 89d0860b..2b0dc6a6 100644 --- a/views/layout/default.blade.php +++ b/views/layout/default.blade.php @@ -209,7 +209,7 @@ @if(GROCY_FEATURE_FLAG_UPLOAD_JSON)