You are here

function farm_inventory_set in farmOS 7

Create a log for adjusting asset inventory.

Parameters

FarmAsset $asset: The asset to adjust.

string $inventory: The new asset inventory value.

int $timestamp: The timestamp of the inventory adjustment. Defaults to the current time.

string $log_type: The type of log to create. Defaults to "farm_observation".

bool $done: Boolean indicating whether or not the log should be marked "done". Defaults to TRUE.

Return value

\Log Returns the log that was created.

1 call to farm_inventory_set()
farm_inventory_asset_form_submit in modules/farm/farm_inventory/farm_inventory.module
Submit handler for processing the asset inventory field.

File

modules/farm/farm_inventory/farm_inventory.module, line 540

Code

function farm_inventory_set($asset, $inventory, $timestamp = REQUEST_TIME, $log_type = 'farm_observation', $done = TRUE) {

  // If the asset type does not have inventory enabled, bail with an error.
  if (!farm_inventory_enabled($asset)) {
    drupal_set_message(t('An inventory log was not created for @asset because inventory is not enabled for that asset type.', array(
      '@asset' => entity_label('farm_asset', $asset),
    )), 'error');
    return;
  }

  // Load the asset's current inventory level (as a fraction).
  $current_inventory = farm_inventory($asset);

  // Convert the current inventory to a fraction.
  $current_inventory_fraction = fraction_from_decimal($current_inventory);

  // Convert the new inventory to a fraction.
  $inventory_fraction = fraction_from_decimal($inventory);

  // Subtract the current inventory from the new inventory to figure out the
  // necessary adjustment value.
  $value_fraction = $inventory_fraction
    ->subtract($current_inventory_fraction);

  // Get the numerator and denominator.
  $numerator = $value_fraction
    ->getNumerator();
  $denominator = $value_fraction
    ->getDenominator();

  // If there is no value difference, bail.
  if (empty($numerator) || empty($denominator)) {
    return;
  }

  // If the log is an observation, set the name to:
  // "Inventory of [assets] is [inventory]".
  $log_name = '';
  if ($log_type == 'farm_observation') {
    $assets_summary = farm_log_entity_label_summary('farm_asset', $asset);
    $log_name = t('Inventory of !assets is !inventory', array(
      '!assets' => $assets_summary,
      '!inventory' => $inventory,
    ));
  }

  // Create a new farm log entity.
  $log = farm_log_create($log_type, $log_name, $timestamp, $done);

  // Create a new inventory field_collection entity attached to the log.
  $adjustment = entity_create('field_collection_item', array(
    'field_name' => 'field_farm_inventory',
  ));
  $adjustment
    ->setHostEntity('log', $log);

  // Create an entity wrapper for the adjustment.
  $adjustment_wrapper = entity_metadata_wrapper('field_collection_item', $adjustment);

  // Set the adjustment asset.
  $adjustment_wrapper->field_farm_inventory_asset = $asset;

  // Set the adjustment value (fraction numerator and denominator).
  $adjustment_wrapper->field_farm_inventory_value->numerator
    ->set($numerator);
  $adjustment_wrapper->field_farm_inventory_value->denominator
    ->set($denominator);

  // Save the adjustment.
  $adjustment_wrapper
    ->save();

  // Return the log.
  return $log;
}