mirror of
https://github.com/grocy/grocy.git
synced 2025-08-20 04:12:59 +00:00
Finalized frontend external barcode lookup implementation (references #158)
This commit is contained in:
1
data/plugins/.gitignore
vendored
1
data/plugins/.gitignore
vendored
@@ -1,3 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
!DemoBarcodeLookupPlugin.php
|
||||
|
@@ -1,89 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Grocy\Helpers\BaseBarcodeLookupPlugin;
|
||||
|
||||
/*
|
||||
This class must extend BaseBarcodeLookupPlugin (in namespace Grocy\Helpers)
|
||||
*/
|
||||
class DemoBarcodeLookupPlugin extends BaseBarcodeLookupPlugin
|
||||
{
|
||||
/*
|
||||
To use this plugin, configure it in data/config.php like this:
|
||||
Setting('STOCK_BARCODE_LOOKUP_PLUGIN', 'DemoBarcodeLookupPlugin');
|
||||
*/
|
||||
|
||||
/*
|
||||
To try it:
|
||||
|
||||
Call the API function at /api/stock/barcodes/external-lookup/{barcode}
|
||||
|
||||
Or use the product picker workflow "External barcode lookup (via plugin)"
|
||||
|
||||
When you also add ?add=true as a query parameter to the API call,
|
||||
on a successful lookup the product is added to the database and in the output
|
||||
the new product id is included (automatically, nothing to do here in the plugin)
|
||||
*/
|
||||
|
||||
/*
|
||||
Provided references:
|
||||
|
||||
$this->Locations contains all locations
|
||||
$this->QuantityUnits contains all quantity units
|
||||
*/
|
||||
|
||||
/*
|
||||
Useful hints:
|
||||
|
||||
Get a quantity unit by name:
|
||||
$quantityUnit = FindObjectInArrayByPropertyValue($this->QuantityUnits, 'name', 'Piece');
|
||||
|
||||
Get a location by name:
|
||||
$location = FindObjectInArrayByPropertyValue($this->Locations, 'name', 'Fridge');
|
||||
*/
|
||||
|
||||
/*
|
||||
This class must implement the protected abstract function ExecuteLookup($barcode),
|
||||
which is called with the barcode that needs to be looked up and must return an
|
||||
associative array of the product model or null, when nothing was found for the barcode
|
||||
|
||||
The returned array must be a valid product object (see the "products" database table for all available properties/columns):
|
||||
[
|
||||
// Required properties:
|
||||
'name' => '',
|
||||
'location_id' => 1, // A valid id of a location object, check against $this->Locations
|
||||
'qu_id_purchase' => 1, // A valid id of a quantity unit object, check against $this->QuantityUnits
|
||||
'qu_id_stock' => 1, // A valid id of a quantity unit object, check against $this->QuantityUnits
|
||||
|
||||
// These are virtual properties (not part of the product object, will be automatically handled as needed)
|
||||
'qu_factor_purchase_to_stock' => 1, // Normally 1 when quantity unit stock and purchase is the same
|
||||
'barcode' => $barcode // The barcode of the product, maybe just pass through $barcode or manipulate it if necessary
|
||||
|
||||
// Optional virtual properties
|
||||
'image_url' => '' // When provided, the corresponding image will be downloaded and set as the product picture
|
||||
]
|
||||
*/
|
||||
protected function ExecuteLookup($barcode)
|
||||
{
|
||||
if ($barcode === 'nothing')
|
||||
{
|
||||
// Demonstration when nothing is found
|
||||
return null;
|
||||
}
|
||||
elseif ($barcode === 'error')
|
||||
{
|
||||
// Demonstration when an error occurred
|
||||
throw new \Exception('This is the error message from the plugin...');
|
||||
}
|
||||
else
|
||||
{
|
||||
return [
|
||||
'name' => 'LookedUpProduct_' . RandomString(5),
|
||||
'location_id' => $this->Locations[0]->id, // Take the first location as a default
|
||||
'qu_id_purchase' => $this->QuantityUnits[0]->id, // Take the first QU as a default
|
||||
'qu_id_stock' => $this->QuantityUnits[0]->id, // Take the first QU as a default
|
||||
'qu_factor_purchase_to_stock' => 1,
|
||||
'barcode' => $barcode
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Grocy\Helpers\BaseBarcodeLookupPlugin;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
/*
|
||||
To use this plugin, configure it in data/config.php like this:
|
||||
Setting('STOCK_BARCODE_LOOKUP_PLUGIN', 'OpenFoodFactsBarcodeLookupPlugin');
|
||||
*/
|
||||
|
||||
class OpenFoodFactsBarcodeLookupPlugin extends BaseBarcodeLookupPlugin
|
||||
{
|
||||
protected function ExecuteLookup($barcode)
|
||||
{
|
||||
$webClient = new Client(['http_errors' => false]);
|
||||
$response = $webClient->request('GET', "https://world.openfoodfacts.net/api/v2/product/$barcode?fields=product_name,image_url");
|
||||
$statusCode = $response->getStatusCode();
|
||||
|
||||
// Guzzle throws exceptions for connection errors, so nothing to do on that here
|
||||
|
||||
$data = json_decode($response->getBody());
|
||||
if ($statusCode == 404 || $data->status != 1)
|
||||
{
|
||||
// Nothing found for the given barcode
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
$imageUrl = '';
|
||||
if (isset($data->product->image_url) && !empty($data->product->image_url))
|
||||
{
|
||||
$imageUrl = $data->product->image_url;
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => $data->product->product_name,
|
||||
'location_id' => $this->Locations[0]->id,
|
||||
'qu_id_purchase' => $this->QuantityUnits[0]->id,
|
||||
'qu_id_stock' => $this->QuantityUnits[0]->id,
|
||||
'qu_factor_purchase_to_stock' => 1,
|
||||
'barcode' => $barcode,
|
||||
'image_url' => $imageUrl
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user