Commit e07563be authored by jaapjansma's avatar jaapjansma
Browse files

Added data processor field output handler for getting the total amount of an expense per case.

parent aec26029
Version 1.2.0
--------------
* Added data processor field output handler for getting the total amount of an expense per case.
Version 1.1.0
--------------
......
......@@ -34,6 +34,11 @@ class Expenses implements CompilerPassInterface {
'Civi\DataProcessor\FieldOutputHandler\Expenses\EditExpenseLineFieldOutputHandler',
E::ts('Edit Expense Link'),
]);
$factoryDefinition->addMethodCall('addOutputHandler', [
'expense_total_amount_per_case',
'Civi\DataProcessor\FieldOutputHandler\Expenses\TotalAmountPerCase',
E::ts('Expense total amount per case'),
]);
}
if ($container->hasDefinition('action_provider')) {
......
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\DataProcessor\FieldOutputHandler\Expenses;
use Civi\DataProcessor\FieldOutputHandler\AbstractSimpleFieldOutputHandler;
use Civi\DataProcessor\FieldOutputHandler\FieldOutput;
use Civi\DataProcessor\FieldOutputHandler\HTMLFieldOutput;
use CRM_Expenses_ExtensionUtil as E;
class TotalAmountPerCase extends AbstractSimpleFieldOutputHandler {
protected $number_of_decimals;
protected $decimal_sep;
protected $thousand_sep;
protected $prefix = '';
protected $suffix = '';
protected $status_ids = array();
protected $type_ids = array();
/**
* Returns the data type of this field
*
* @return String
*/
protected function getType() {
return $this->inputFieldSpec->type;
}
/**
* Returns the formatted value
*
* @param $rawRecord
* @param $formattedRecord
*
* @return \Civi\DataProcessor\FieldOutputHandler\FieldOutput
*/
public function formatField($rawRecord, $formattedRecord) {
$case_id = $rawRecord[$this->inputFieldSpec->alias];
$sql = "
SELECT SUM(`line`.`amount`) AS `total_amount`
FROM `civicrm_expense` `expense`
INNER JOIN `civicrm_expense_line` `line` ON `line`.`expense_id` = `expense`.`id`
WHERE `expense`.`case_id` = %1
";
if (count($this->type_ids)) {
$sql .= " AND `expense`.`type_id` IN (".implode(", ", $this->type_ids).")";
}
if (count($this->status_ids)) {
$sql .= " AND `expense`.`status_id` IN (".implode(", ", $this->status_ids).")";
}
$sqlParams[1] = [$case_id, 'Integer'];
$value = \CRM_Core_DAO::singleValueQuery($sql, $sqlParams);
$formattedValue = $value;
if (is_numeric($this->number_of_decimals) && $value !== null) {
$formattedValue = number_format((float) $value, $this->number_of_decimals, $this->decimal_sep, $this->thousand_sep);
}
if ($formattedValue != null) {
$formattedValue = $this->prefix . $formattedValue . $this->suffix;
}
$output = new FieldOutput((float) $rawRecord[$this->inputFieldSpec->alias]);
$output->formattedValue = $formattedValue;
return $output;
}
/**
* Returns the label of the field for selecting a field.
*
* This could be override in a child class.
*
* @return string
*/
protected function getFieldTitle() {
return E::ts('Case ID Field');
}
/**
* Initialize the processor
*
* @param String $alias
* @param String $title
* @param array $configuration
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
*/
public function initialize($alias, $title, $configuration) {
parent::initialize($alias, $title, $configuration);
$this->outputFieldSpec->type = 'String';
if (isset($configuration['number_of_decimals'])) {
$this->number_of_decimals = $configuration['number_of_decimals'];
}
if (isset($configuration['decimal_separator'])) {
$this->decimal_sep = $configuration['decimal_separator'];
}
if (isset($configuration['thousand_separator'])) {
$this->thousand_sep = $configuration['thousand_separator'];
}
if (isset($configuration['prefix'])) {
$this->prefix = $configuration['prefix'];
}
if (isset($configuration['suffix'])) {
$this->suffix = $configuration['suffix'];
}
if (isset($configuration['type_ids']) && is_array($configuration['type_ids'])) {
$this->type_ids = $configuration['type_ids'];
}
if (isset($configuration['status_ids']) && is_array($configuration['status_ids'])) {
$this->status_ids = $configuration['status_ids'];
}
}
/**
* When this handler has additional configuration you can add
* the fields on the form with this function.
*
* @param \CRM_Core_Form $form
* @param array $field
*/
public function buildConfigurationForm(\CRM_Core_Form $form, $field=array()) {
parent::buildConfigurationForm($form, $field);
$statuses = \CRM_Core_OptionGroup::values('expense_type');
$form->add('select', 'type_ids', E::ts('Type'), $statuses, FALSE, ['class' => 'huge crm-select2', 'placeholder' => E::ts(' - Any type -'), 'multiple' => true]);
$statuses = \CRM_Core_OptionGroup::values('expense_status');
$form->add('select', 'status_ids', E::ts('Status'), $statuses, FALSE, ['class' => 'huge crm-select2', 'placeholder' => E::ts(' - Any status -'), 'multiple' => true]);
$form->add('text', 'number_of_decimals', E::ts('Number of decimals'), false);
$form->add('text', 'decimal_separator', E::ts('Decimal separator'), false);
$form->add('text', 'thousand_separator', E::ts('Thousand separator'), false);
$form->add('text', 'prefix', E::ts('Prefix (e.g. &euro;)'), false);
$form->add('text', 'suffix', E::ts('Suffix (e.g. $)'), false);
if (isset($field['configuration'])) {
$configuration = $field['configuration'];
$defaults = array();
if (isset($configuration['number_of_decimals'])) {
$defaults['number_of_decimals'] = $configuration['number_of_decimals'];
}
if (isset($configuration['decimal_separator'])) {
$defaults['decimal_separator'] = $configuration['decimal_separator'];
}
if (isset($configuration['thousand_separator'])) {
$defaults['thousand_separator'] = $configuration['thousand_separator'];
}
if (isset($configuration['prefix'])) {
$defaults['prefix'] = $configuration['prefix'];
}
if (isset($configuration['suffix'])) {
$defaults['suffix'] = $configuration['suffix'];
}
if (isset($configuration['type_ids'])) {
$defaults['type_ids'] = $configuration['type_ids'];
}
if (isset($configuration['status_ids'])) {
$defaults['status_ids'] = $configuration['status_ids'];
}
$form->setDefaults($defaults);
}
}
/**
* When this handler has configuration specify the template file name
* for the configuration form.
*
* @return false|string
*/
public function getConfigurationTemplateFileName() {
return "CRM/Dataprocessor/Form/Field/Configuration/Expenses/TotalAmountPerCase.tpl";
}
/**
* Process the submitted values and create a configuration array
*
* @param $submittedValues
* @return array
*/
public function processConfiguration($submittedValues) {
$configuration = parent::processConfiguration($submittedValues);
$configuration['number_of_decimals'] = $submittedValues['number_of_decimals'];
$configuration['decimal_separator'] = $submittedValues['decimal_separator'];
$configuration['thousand_separator'] = $submittedValues['thousand_separator'];
$configuration['prefix'] = $submittedValues['prefix'];
$configuration['suffix'] = $submittedValues['suffix'];
$configuration['type_ids'] = isset($submittedValues['type_ids']) ? $submittedValues['type_ids'] : false;
$configuration['status_ids'] = isset($submittedValues['status_ids']) ? $submittedValues['status_ids'] : false;
return $configuration;
}
}
......@@ -13,8 +13,8 @@
<url desc="Documentation">https://lab.civicrm.org/extensions/expenses/blob/master/README.md</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2021-02-09</releaseDate>
<version>1.1.0</version>
<releaseDate>2021-02-15</releaseDate>
<version>1.2.0</version>
<develStage>stable</develStage>
<compatibility>
<ver>5.0</ver>
......
{crmScope extensionKey='expenses'}
<div class="crm-section">
<div class="label">{$form.field.label}</div>
<div class="content">{$form.field.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.type_ids.label}</div>
<div class="content">{$form.type_ids.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.status_ids.label}</div>
<div class="content">{$form.status_ids.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.number_of_decimals.label}</div>
<div class="content">{$form.number_of_decimals.html}
<p class="description">{ts}Leave empty for no formatting{/ts}</p>
</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.decimal_separator.label}</div>
<div class="content">{$form.decimal_separator.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.thousand_separator.label}</div>
<div class="content">{$form.thousand_separator.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.prefix.label}</div>
<div class="content">{$form.prefix.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.suffix.label}</div>
<div class="content">{$form.suffix.html}</div>
<div class="clear"></div>
</div>
{/crmScope}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment