protected function InventoryTest::adjustInventory in farmOS 2.x
Creates an inventory adjustment quantity + log for a given asset.
Parameters
\Drupal\asset\Entity\AssetInterface $asset: The asset to adjust inventory for.
string $adjustment: The type of adjustment ('reset', 'increment', or 'decrement').
string $value: The value of the adjustment.
string $measure: The quantity measure of the inventory. See quantity_measures().
int $units: The quantity units of the inventory (term ID).
Return value
\Drupal\log\Entity\LogInterface The log entity.
2 calls to InventoryTest::adjustInventory()
- InventoryTest::testMultipleAssetInventory in modules/
core/ inventory/ tests/ src/ Kernel/ InventoryTest.php - Test multiple asset inventories with measure+units pairs.
- InventoryTest::testSimpleAssetInventory in modules/
core/ inventory/ tests/ src/ Kernel/ InventoryTest.php - Test simple asset inventory.
File
- modules/
core/ inventory/ tests/ src/ Kernel/ InventoryTest.php, line 304
Class
- InventoryTest
- Tests for farmOS inventory logic.
Namespace
Drupal\Tests\farm_inventory\KernelCode
protected function adjustInventory(AssetInterface $asset, string $adjustment, string $value, string $measure = '', int $units = 0) {
$fraction = Fraction::createFromDecimal($value);
/** @var \Drupal\quantity\Entity\Quantity $quantity */
$quantity = Quantity::create([
'type' => 'standard',
'value' => [
'numerator' => $fraction
->getNumerator(),
'denominator' => $fraction
->getDenominator(),
],
'inventory_adjustment' => $adjustment,
'inventory_asset' => [
'target_id' => $asset
->id(),
],
]);
if (!empty($measure)) {
$quantity
->set('measure', $measure);
}
if (!empty($units)) {
$quantity
->set('units', [
'target_id' => $units,
]);
}
$quantity
->save();
/** @var \Drupal\log\Entity\LogInterface $log */
$log = Log::create([
'type' => 'adjustment',
'status' => 'done',
'quantity' => [
'target_id' => $quantity
->id(),
'target_revision_id' => $quantity
->getRevisionId(),
],
]);
$log
->save();
return $log;
}