Introduction
When you’re developing an application that can display articles and/or create orders, you might be interested in knowing the exact stock of an article. Retrieving the available stock is done through a separate API method.
Contents
getStock Retrieving current stock for one or more articles
You can specify a stockId (similar in function to a syncMarker) to limit the results to changed stock since a specific moment.
Learn more about sync markers here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php require_once('Mplusqapiclient.php'); $mplusqapiclient = new Mplusqapiclient(); // Initialize the client with your details. // Then we initialize the variables $branchNumber = 1; // This means we want the stock information for this branch. $articleNumbers = array(4542, 4543, 4544); // This means we want stock information for the articles with these numbers. $stockId = null; // stockId works just like syncMarker, use this to retrieve all stock changes since the last time you checked. // Then we call the getStock() function wrapped in a try/catch block to intercept any exceptions. try { if (false !== ($stock_information = $mplusqapiclient->getStock($branchNumber, $articleNumbers, $stockId))) { // Success, we show how much stock information we got. exit(sprintf('Found stock information for %d articles.', count($stock_information))); } else { exit('Unable to retrieve stock information.'); } } catch (MplusQAPIException $e) { exit($e->getMessage()); } |
Stock information takes the form of an array containing arrays of the following form:
1 2 3 4 5 6 |
array( 'stockId' => 9842, // The stockId (syncMarker) of this stock item, register the highest value to use the next time you are synchronizing stock. 'amountFree' => 10, // The available stock that can be safely sold. 'amountReserved' => 3, // The stock that is currently reserved in undelivered orders. 'amountIncoming' => 20, // The stock that is incoming through procurement orders. ) |
getStockHistoryV2 Retrieving each individual change of the stock
You can specify a sinceStockId (similar in function to a syncMarker) to limit the results to changed stock since a specific moment.
Learn more about sync markers here.
You can also specify fromFinancialDateTime and throughFinancialDateTime to limit the results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php require_once('Mplusqapiclient.php'); $mplusqapiclient = new Mplusqapiclient(); // Initialize the client with your details. // Then we initialize the variables $branchNumber = 1; // This means we want the stock information for this branch. $articleNumbers = array(); $sinceStockId = null; // sinceStockId works just like syncMarker, use this to retrieve all stock changes since the last time you checked. // Then we call the getStock() function wrapped in a try/catch block to intercept any exceptions. try { if (false !== ($stock_histories = $mplusqapiclient->getStockHistoryV2($branchNumber, $articleNumbers, $sinceStockId))) { // Success, we show how much stock histories we got. exit(sprintf('Found %d stock changes.', count($stock_histories))); } else { exit('Unable to retrieve stock changes.'); } } catch (MplusQAPIException $e) { exit($e->getMessage()); } |
updateStock Relatively update stock for an article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php require_once('Mplusqapiclient.php'); $mplusqapiclient = new Mplusqapiclient(); // Initialize the client with your details. // Then we initialize the variables $branchNumber = 1; $articleNumber = 4542; $amountChanged = -3; // Then we call the updateStock() function wrapped in a try/catch block to intercept any exceptions. try { if (false !== $mplusqapiclient->updateStock($branchNumber, $articleNumber, $amountChanged)) { exit('Stock succesfully updated.'); } else { exit('Unable to update stock.'); } } catch (MplusQAPIException $e) { exit($e->getMessage()); } |
setStock Absolutely set stock for an article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php require_once('Mplusqapiclient.php'); $mplusqapiclient = new Mplusqapiclient(); // Initialize the client with your details. // Then we initialize the variables $branchNumber = 1; $articleNumber = 4542; $amountSet = 5; // Then we call the setStock() function wrapped in a try/catch block to intercept any exceptions. try { if (false !== $mplusqapiclient->setStock($branchNumber, $articleNumber, $amountSet)) { exit('Stock succesfully set.'); } else { exit('Unable to set stock.'); } } catch (MplusQAPIException $e) { exit($e->getMessage()); } |