Add additional filters to livestock weight report #323

This commit is contained in:
Michael Stenta 2020-07-30 14:57:09 -04:00
commit 65190926a0
2 changed files with 110 additions and 11 deletions

View File

@ -2,8 +2,12 @@ name = Farm Livestock Weight
description = Features for tracking weight of animals with observation logs.
core = 7.x
package = farmOS
dependencies[] = farm_asset
dependencies[] = farm_livestock
dependencies[] = farm_flags
dependencies[] = farm_group
dependencies[] = farm_inventory
dependencies[] = farm_log
dependencies[] = farm_quantity
dependencies[] = farm_quantity_log
dependencies[] = farm_report

View File

@ -167,7 +167,8 @@ function farm_livestock_weight_group_report_form($form, &$form_state) {
if (!empty($group->id)) {
$label = entity_label('farm_asset', $group);
if ($group->archived) {
$label = $label . ' (' . t('archived') . ')';
$date = strftime('%Y-%m-%d', $group->archived);
$label = $label . ' (' . t('archived') . ' ' . $date . ')';
}
$options[$group->id] = $label;
}
@ -194,14 +195,48 @@ function farm_livestock_weight_group_report_form($form, &$form_state) {
);
$form['input']['archived'] = array(
'#type' => 'checkbox',
'#title' => t('Include archived animals'),
'#type' => 'select',
'#title' => t('Archived'),
'#options' => array(0 => t('No'), 1 => t('Yes'), 2 => '-' . t('Any') . '-'),
'#default_value' => 0,
);
$form['input']['flags'] = array(
'#type' => 'select',
'#title' => t('Flags'),
'#options' => farm_flags_field_allowed_values(),
'#multiple' => TRUE,
);
// Provide a default date in the format YYYY-MM-DD.
$format = 'Y-m-d';
$current_date = date($format, REQUEST_TIME);
$form['input']['birth'] = array(
'#type' => 'fieldset',
'#title' => t('Birth Date'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['input']['birth']['earliest_birth'] = array(
'#type' => 'date_select',
'#title' => t('Earliest Birth'),
'#default_value' => '',
'#date_year_range' => '-10:+1',
'#date_format' => $format,
'#date_label_position' => 'within',
);
$form['input']['birth']['latest_birth'] = array(
'#type' => 'date_select',
'#title' => t('Latest Birth'),
'#default_value' => '',
'#date_year_range' => '-10:+1',
'#date_format' => $format,
'#date_label_position' => 'within',
);
$form['input']['start_date'] = array(
'#type' => 'date_select',
'#title' => t('Start date'),
@ -287,7 +322,15 @@ function farm_livestock_weight_group_report(&$form_state) {
$group_ids = $form_state['values']['group'];
// Check if we should include archived assets.
$archived = $form_state['values']['archived'];
$include_archived = (bool) $form_state['values']['archived'];
$only_archived = $form_state['values']['archived'] == 1;
// Get list of flags to filter by.
$flags = $form_state['values']['flags'];
// Get date of birth filters.
$earliest_birth = strtotime($form_state['values']['earliest_birth']);
$latest_birth = strtotime($form_state['values']['latest_birth']);
// Get the start and end dates.
$start_date = strtotime($form_state['values']['start_date']);
@ -305,10 +348,9 @@ function farm_livestock_weight_group_report(&$form_state) {
// Load the farm group asset.
$group = farm_asset_load($id);
$group_name = $group->name;
// Load the farm group members.
$members = farm_group_members($group, REQUEST_TIME, TRUE, $archived);
$members = farm_group_members($group, REQUEST_TIME, TRUE, $include_archived);
// Loop through members.
foreach ($members as $asset) {
@ -318,6 +360,54 @@ function farm_livestock_weight_group_report(&$form_state) {
continue;
}
// Skip non-archived assets if only displaying archived assets.
if ($only_archived && !$asset->archived) {
continue;
}
// Check if asset has required flags.
if (!empty($flags)) {
// Compare asset flags to required flags.
$asset_flags = farm_flags_load($asset);
$diff = array_diff($flags, $asset_flags);
// Skip the asset if it doesn't have required flags.
if (!empty($diff)) {
continue;
}
}
// Check if a birth date filter is specified.
if ($earliest_birth || $latest_birth) {
// Get the assets birth date.
$birth_date = NULL;
if (!empty($asset->field_farm_date[LANGUAGE_NONE][0]['value'])) {
$birth_date = $asset->field_farm_date[LANGUAGE_NONE][0]['value'];
}
// Skip assets without a birth date.
if (empty($birth_date)) {
continue;
}
// Skip assets born before earliest birth date.
if ($earliest_birth) {
if ($birth_date < $earliest_birth) {
continue;
}
}
// Skip assets born after latest birth date.
if ($latest_birth) {
if ($birth_date > $latest_birth) {
continue;
}
}
}
// Save the animal info
$asset->group = $group;
@ -411,13 +501,14 @@ function farm_livestock_weight_group_report(&$form_state) {
foreach ($animals as $animal) {
// Add a row of data.
$row = array();
$name_label = $animal->name;
$name_label = htmlspecialchars(entity_label('farm_asset', $animal));
if ($animal->archived) {
$name_label = $name_label . ' (' . t('archived') . ')';
$date = strftime('%Y-%m-%d', $animal->archived);
$name_label = $name_label . ' (' . t('archived') . ' ' . $date . ')';
}
$row[] = $animal->id;
$row[] = $name_label;
$row[] = $animal->group->name;
$row[] = htmlspecialchars(entity_label('farm_asset', $animal->group));
// Save the logs.
$logs = $animal->all_log_data;
@ -501,8 +592,12 @@ function farm_livestock_weight_group_report(&$form_state) {
$csvdata .= implode(',', $row) . PHP_EOL;
}
// Create HTML Table
$table = theme('table', array('header' => $header, 'rows' => $table_data));
$table = '<div class="alert alert-danger">' . t('No animal assets match the above criteria.') . '</div>';
if (!empty($table_data)) {
// Create HTML Table
$table = theme('table', array('header' => $header, 'rows' => $table_data));
}
// Create the div to hold report graphs.
$graph_markup = array();