Added a plugin system for looking up products against external services by barcode (references #6)

This commit is contained in:
Bernd Bestel
2018-04-22 19:47:46 +02:00
parent 4853174d03
commit a9a1358b08
12 changed files with 318 additions and 1 deletions

View File

@@ -208,4 +208,44 @@ class StockService extends BaseService
$productRow = $this->Database->products()->where('id = :1', $productId)->fetch();
return $productRow !== null;
}
private function LoadBarcodeLookupPlugin()
{
$pluginName = defined('STOCK_BARCODE_LOOKUP_PLUGIN') ? STOCK_BARCODE_LOOKUP_PLUGIN : '';
if (empty($pluginName))
{
throw new \Exception('No barcode lookup plugin defined');
}
$path = __DIR__ . "/../data/plugins/$pluginName.php";
if (file_exists($path))
{
require_once $path;
return new $pluginName($this->Database->locations()->fetchAll(), $this->Database->quantity_units()->fetchAll());
}
else
{
throw new \Exception("Plugin $pluginName was not found");
}
}
public function ExternalBarcodeLookup($barcode, $addFoundProduct)
{
$plugin = $this->LoadBarcodeLookupPlugin();
$pluginOutput = $plugin->Lookup($barcode);
if ($pluginOutput !== null) // Lookup was successful
{
if ($addFoundProduct === true)
{
// Add product to database and include new product id in output
$newRow = $this->Database->products()->createRow($pluginOutput);
$newRow->save();
$pluginOutput['id'] = $newRow->id;
}
}
return $pluginOutput;
}
}