You are here

function farm_livestock_asset_inventory in farmOS 7

Calculate the inventory for a given animal or group asset.

This will recursively calculate the inventory of animal assets that are members of a group at the given timestamp.

Parameters

\FarmAsset $asset: The animal or group asset.

int $time: Unix timestamp limiter. Only inventory and group membership logs before this time will be included. Defaults to the current time. Set to 0 to load the absolute last.

bool $done: Whether or not to only show logs that are marked as "done". TRUE will limit to logs that are done, and FALSE will limit to logs that are not done. If this is set to NULL, no filtering will be applied. Defaults to TRUE.

false $archived: Whether or not to include archived member assets. Defaults to FALSE.

Return value

int Returns the total inventory of animal assets, including group members.

File

modules/farm/farm_livestock/farm_livestock.module, line 514

Code

function farm_livestock_asset_inventory(FarmAsset $asset, $time = REQUEST_TIME, $done = TRUE, $archived = FALSE) {

  // Start count at 0.
  $count = 0;

  // Calculate inventory of a single animal asset.
  if ($asset->type == 'animal') {

    // Get inventory at the specified time.
    $inventory = farm_inventory($asset, $time, $done);

    // Use the inventory if valid.
    if (!empty($inventory) && is_numeric($inventory)) {
      $count += $inventory;
    }
    else {
      $count++;
    }
  }

  // Recursively calculate inventory of group assets.
  if ($asset->type == 'group') {

    // Get group members.
    $members = farm_group_members($asset, $time, $done, $archived);

    // Get inventory of each member.
    foreach ($members as $member) {
      $count += farm_livestock_asset_inventory($member, $time, $done, $archived);
    }
  }

  // Return total inventory count.
  return $count;
}