diff --git a/modules/farm/farm_inventory/farm_inventory.info b/modules/farm/farm_inventory/farm_inventory.info index 2b0d1dd5b..1b738e093 100644 --- a/modules/farm/farm_inventory/farm_inventory.info +++ b/modules/farm/farm_inventory/farm_inventory.info @@ -21,4 +21,5 @@ features[field_instance][] = field_collection_item-field_farm_inventory-field_fa features[field_instance][] = field_collection_item-field_farm_inventory-field_farm_inventory_value features[views_view][] = farm_inventory_entityreference_view features[views_view][] = farm_inventory_log +files[] = views/handlers/farm_inventory_handler_field_asset_inventory_value.inc files[] = views/handlers/farm_inventory_handler_filter_asset_type.inc diff --git a/modules/farm/farm_inventory/farm_inventory.module b/modules/farm/farm_inventory/farm_inventory.module index ee47ef777..e02966f12 100644 --- a/modules/farm/farm_inventory/farm_inventory.module +++ b/modules/farm/farm_inventory/farm_inventory.module @@ -423,8 +423,11 @@ function farm_inventory_format($inventory) { /** * Build a query to calculate an asset's inventory level. * - * @param int $asset_id - * The asset id to search for. + * @param int|string $asset_id + * The asset id to search for. This can either be a specific id, or a field + * alias string from another query (ie: 'mytable.assetid'). For an example + * of field alias string usage, see the Views field handler code in + * farm_inventory_handler_field_asset_inventory_value::query(). * @param int $time * Unix timestamp limiter. Only logs before this time will be included. * Defaults to the current time. Set to 0 to load the absolute last. diff --git a/modules/farm/farm_inventory/farm_inventory.views.inc b/modules/farm/farm_inventory/farm_inventory.views.inc index d1aff4c99..dd4d05b57 100644 --- a/modules/farm/farm_inventory/farm_inventory.views.inc +++ b/modules/farm/farm_inventory/farm_inventory.views.inc @@ -68,6 +68,16 @@ function farm_inventory_views_data() { */ function farm_inventory_views_data_alter(&$data) { + // Asset inventory. + $data['farm_asset']['inventory'] = array( + 'title' => t('Asset inventory value'), + 'help' => t('The current asset inventory value.'), + 'field' => array( + 'handler' => 'farm_inventory_handler_field_asset_inventory_value', + 'click sortable' => TRUE, + ), + ); + // Add custom asset type filter that only includes types with inventory // enabled. $data['farm_asset']['inventory_asset_type'] = array( diff --git a/modules/farm/farm_inventory/views/handlers/farm_inventory_handler_field_asset_inventory_value.inc b/modules/farm/farm_inventory/views/handlers/farm_inventory_handler_field_asset_inventory_value.inc new file mode 100644 index 000000000..51a221d34 --- /dev/null +++ b/modules/farm/farm_inventory/views/handlers/farm_inventory_handler_field_asset_inventory_value.inc @@ -0,0 +1,110 @@ + TRUE, 'bool' => TRUE); + + // Don't include future logs by default. + $options['future'] = array('default' => FALSE, 'bool' => TRUE); + + return $options; + } + + /** + * {@inheritdoc} + */ + public function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + + // Only include log that are "done". + $form['done'] = array( + '#type' => 'checkbox', + '#title' => t('Only include done logs'), + '#description' => t('Filters out logs that have not been marked as done.'), + '#default_value' => !empty($this->options['done']), + ); + + // Allow future logs to be included. + $form['future'] = array( + '#type' => 'checkbox', + '#title' => t('Include future logs'), + '#description' => t('Finds the last log, even if it is in the future.'), + '#default_value' => !empty($this->options['future']), + ); + } + + /** + * {@inheritdoc} + */ + public function query() { + $this->ensure_my_table(); + + // Build a sub-query that will be used in the join to calculate the asset's + // current inventory level. + $asset_id_field = $this->table_alias . '.id'; + $done = !empty($this->options['done']) ? TRUE : FALSE; + $future = !empty($this->options['future']) ? TRUE : FALSE; + if ($future) { + $time = 0; + } + else { + $time = REQUEST_TIME; + } + $query = farm_inventory_query($asset_id_field, $time, $done); + + // Add the inventory value field. + $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array(); + $this->field_alias = $this->query->add_field(NULL, '(' . $query . ')', 'asset_inventory', $params); + + $this->add_additional_fields(); + + // Join in the {farm_inventory_asset_type} table and add the "individual" + // field so we can see if we need to treat assets as individuals. + $join = new views_join(); + $join->construct('farm_inventory_asset_type', $this->table_alias, 'type', 'type'); + $this->farm_inventory_asset_type_table = $this->query->ensure_table('farm_inventory_asset_type', $this->relationship, $join); + $this->individual_field = $this->query->add_field($this->farm_inventory_asset_type_table, 'individual'); + + } + + /** + * {@inheritdoc} + */ + function render($values) { + + // Get the inventory value. + $value = $this->get_value($values); + + // Get the "individual" setting. + $individual = $values->{$this->individual_field}; + + // If the value is null, and this asset should be treated as an individual, + // set the inventory to 1. + if (is_null($value) && !empty($individual)) { + return '1'; + } + + // Otherwise, return the formatted inventory string. + else { + return farm_inventory_format($value); + } + } +}