Commit 8f548a5d authored by jaapjansma's avatar jaapjansma
Browse files

update: added action for get expense by type; added total amount to expense get api

parent 07f9acf9
Version 1.0.5
-------------
* Added action to retrieve expense by type
* Added total amount to Expense Get api.
Version 1.0.4
-------------
......
......@@ -3,6 +3,37 @@ use CRM_Expenses_ExtensionUtil as E;
class CRM_Expenses_BAO_Expense extends CRM_Expenses_DAO_Expense {
/**
* Function to get values
*
* @return array $result found rows with data
* @access public
* @static
*/
public static function getValues($params) {
$result = array();
$options = _civicrm_api3_get_options_from_params($params);
$expense = new CRM_Expenses_BAO_Expense();
if (!empty($params)) {
$fields = self::fields();
foreach ($params as $key => $value) {
if (isset($fields[$key])) {
$expense->$key = $value;
}
}
}
$expense->orderBy($options['sort']);
$expense->limit($options['limit'] ? $options['limit'] : null, $options['offset']);
$expense->find();
while ($expense->fetch()) {
$row = array();
self::storeValues($expense, $row);
$row['total_amount'] = self::totalAmount($row['id']);
$result[$row['id']] = $row;
}
return $result;
}
/**
* Create a new Expense based on array-data
*
......@@ -61,5 +92,9 @@ class CRM_Expenses_BAO_Expense extends CRM_Expenses_DAO_Expense {
CRM_Expenses_Utils_File::deleteEntityFile(self::$_tableName, $id, 'Expense');
}
public static function totalAmount($expenseId) {
return \CRM_Core_DAO::singleValueQuery("SELECT SUM(`amount`) FROM `civicrm_expense_line` WHERE `expense_id` = %1", [1=>[$expenseId, 'Integer']]);
}
}
......@@ -6,7 +6,7 @@
*
* Generated from /buildkit/build/dataprocessor/web/sites/all/modules/civicrm/tools/extensions/expenses/xml/schema/CRM/Expenses/Expense.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:df354555170ceae707db39a30a429d7f)
* (GenCodeChecksum:ca4093f3c087424db187f73a4fd729f2)
*/
/**
......@@ -93,6 +93,13 @@ class CRM_Expenses_DAO_Expense extends CRM_Core_DAO {
parent::__construct();
}
/**
* Returns localized title of this entity.
*/
public static function getEntityTitle() {
return ts('Expenses');
}
/**
* Returns foreign keys and entity references.
*
......@@ -130,6 +137,7 @@ class CRM_Expenses_DAO_Expense extends CRM_Core_DAO {
'entity' => 'Expense',
'bao' => 'CRM_Expenses_DAO_Expense',
'localizable' => 0,
'add' => NULL,
],
'type_id' => [
'name' => 'type_id',
......@@ -148,6 +156,7 @@ class CRM_Expenses_DAO_Expense extends CRM_Core_DAO {
'optionGroupName' => 'expense_type',
'optionEditPath' => 'civicrm/admin/options/expense_type',
],
'add' => NULL,
],
'status_id' => [
'name' => 'status_id',
......@@ -166,6 +175,7 @@ class CRM_Expenses_DAO_Expense extends CRM_Core_DAO {
'optionGroupName' => 'expense_status',
'optionEditPath' => 'civicrm/admin/options/expense_status',
],
'add' => NULL,
],
'contact_id' => [
'name' => 'contact_id',
......@@ -182,6 +192,7 @@ class CRM_Expenses_DAO_Expense extends CRM_Core_DAO {
'html' => [
'type' => 'EntityRef',
],
'add' => NULL,
],
'source_contact_id' => [
'name' => 'source_contact_id',
......@@ -198,6 +209,7 @@ class CRM_Expenses_DAO_Expense extends CRM_Core_DAO {
'html' => [
'type' => 'EntityRef',
],
'add' => NULL,
],
'assignee_contact_id' => [
'name' => 'assignee_contact_id',
......@@ -214,6 +226,7 @@ class CRM_Expenses_DAO_Expense extends CRM_Core_DAO {
'html' => [
'type' => 'EntityRef',
],
'add' => NULL,
],
'case_id' => [
'name' => 'case_id',
......@@ -230,6 +243,7 @@ class CRM_Expenses_DAO_Expense extends CRM_Core_DAO {
'html' => [
'type' => 'EntityRef',
],
'add' => NULL,
],
'date' => [
'name' => 'date',
......@@ -244,6 +258,7 @@ class CRM_Expenses_DAO_Expense extends CRM_Core_DAO {
'html' => [
'type' => 'Select Date',
],
'add' => NULL,
],
'currency' => [
'name' => 'currency',
......@@ -268,6 +283,7 @@ class CRM_Expenses_DAO_Expense extends CRM_Core_DAO {
'nameColumn' => 'name',
'abbrColumn' => 'symbol',
],
'add' => NULL,
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
......
......@@ -6,7 +6,7 @@
*
* Generated from /buildkit/build/dataprocessor/web/sites/all/modules/civicrm/tools/extensions/expenses/xml/schema/CRM/Expenses/ExpenseLine.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:1136282588c53b565cbdffd3b53de658)
* (GenCodeChecksum:24e25b22cc8c29955ee8bf757312bb99)
*/
/**
......@@ -60,6 +60,13 @@ class CRM_Expenses_DAO_ExpenseLine extends CRM_Core_DAO {
parent::__construct();
}
/**
* Returns localized title of this entity.
*/
public static function getEntityTitle() {
return ts('Expense Lines');
}
/**
* Returns foreign keys and entity references.
*
......@@ -94,6 +101,7 @@ class CRM_Expenses_DAO_ExpenseLine extends CRM_Core_DAO {
'entity' => 'ExpenseLine',
'bao' => 'CRM_Expenses_DAO_ExpenseLine',
'localizable' => 0,
'add' => NULL,
],
'expense_id' => [
'name' => 'expense_id',
......@@ -110,6 +118,7 @@ class CRM_Expenses_DAO_ExpenseLine extends CRM_Core_DAO {
'html' => [
'type' => 'EntityRef',
],
'add' => NULL,
],
'description' => [
'name' => 'description',
......@@ -123,6 +132,7 @@ class CRM_Expenses_DAO_ExpenseLine extends CRM_Core_DAO {
'entity' => 'ExpenseLine',
'bao' => 'CRM_Expenses_DAO_ExpenseLine',
'localizable' => 0,
'add' => NULL,
],
'amount' => [
'name' => 'amount',
......@@ -141,6 +151,7 @@ class CRM_Expenses_DAO_ExpenseLine extends CRM_Core_DAO {
'html' => [
'type' => 'Text',
],
'add' => NULL,
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
......
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\ActionProvider\Action\Expenses;
use Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Parameter\OptionGroupByNameSpecification;
use Civi\ActionProvider\Parameter\ParameterBagInterface;
use Civi\ActionProvider\Parameter\Specification;
use Civi\ActionProvider\Parameter\SpecificationBag;
use Civi\ActionProvider\Utils\CustomField;
use Civi\ActionProvider\Utils\Fields;
use CRM_Expenses_ExtensionUtil as E;
class GetExpenseByType extends AbstractAction {
/**
* Returns the specification of the output parameters of this action.
*
* This function could be overriden by child classes.
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
$bag = new SpecificationBag();
Fields::getFieldsForEntity($bag,'Expense', 'get');
return $bag;
}
/**
* Run the action
*
* @param ParameterBagInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
*
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
try {
$apiParams = [];
if ($parameters->doesParameterExists('expense_type_id')) {
$apiParams['expense_type_id'] = $parameters->getParameter('expense_type_id');
} else {
$apiParams['expense_type_id'] = $this->configuration->getParameter('expense_type_id');
}
if ($parameters->doesParameterExists('contact_id')) {
$apiParams['contact_id'] = $parameters->getParameter('contact_id');
}
if ($parameters->doesParameterExists('case_id')) {
$apiParams['case_id'] = $parameters->getParameter('case_id');
}
$apiParams['options']['sort'] = 'date ASC';
if ($this->configuration->getParameter('sort') == 'newest') {
$apiParams['options']['sort'] = 'date DESC';
}
$apiParams['options']['limit'] = 1;
$entity = civicrm_api3('Expense', 'getsingle', $apiParams);
if ($entity) {
$this->setOutputFromEntity($entity, $output);
}
} catch (\Exception $e) {
// Do nothing
}
}
protected function setOutputFromEntity($entity, ParameterBagInterface $output) {
foreach($entity as $field => $value) {
if (stripos($field, 'custom_') !== 0) {
$output->setParameter($field, $value);
} else {
$custom_id = substr($field, 7);
if (is_numeric($custom_id)) {
$fieldName = CustomField::getCustomFieldName($custom_id);
if (is_array($value)) {
// The keys of the array contains the values of the selected options.
$value = array_keys($value);
}
$output->setParameter($fieldName, $value);
}
}
}
}
/**
* Returns the specification of the configuration options for the actual
* action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag([
new OptionGroupByNameSpecification('expense_type_id', 'expense_type', E::ts('Expense type ID'), false),
new Specification('sort', 'String', E::ts('Sort'), true, 'oldest', null, ['oldest' => E::ts('Oldest'), 'newest' => E::ts('Newest')])
]);
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
return new SpecificationBag([
new OptionGroupByNameSpecification('expense_type_id', 'expense_type', E::ts('Expense type ID'), false),
new Specification('contact_id', 'Integer', E::ts('Contact ID'), false, null, 'Contact'),
new Specification('case_id', 'Integer', E::ts('Case ID'), false, null, 'Case'),
]);
}
}
......@@ -60,6 +60,9 @@ class UploadAttachment extends AbstractAction {
'mime_type' => $file['mime_type'],
'uri' => $uri,
];
if ($parameters->doesParameterExists('description')) {
$fileParams['description'] = $parameters->getParameter('description');
}
$fileDao = \CRM_Core_BAO_File::create($fileParams);
$fileDao->find(TRUE);
......@@ -95,6 +98,7 @@ class UploadAttachment extends AbstractAction {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('expense_id', 'Integer', E::ts('Expense ID'), true));
$specs->addSpecification(new FileSpecification('file', E::ts('File'), false));
$specs->addSpecification(new Specification('description', 'String', E::ts('Description'), false));
return $specs;
}
......
......@@ -74,6 +74,12 @@ class Expenses implements CompilerPassInterface {
E::ts('Expense: Get expense by ID'),
[AbstractAction::DATA_RETRIEVAL_TAG, AbstractAction::SINGLE_CONTACT_ACTION_TAG],
]);
$actionTypeFactoryDefinition->addMethodCall('addAction', [
'expense_get_by_type',
'Civi\ActionProvider\Action\Expenses\GetExpenseByType',
E::ts('Expense: Get expense by Type'),
[AbstractAction::DATA_RETRIEVAL_TAG, AbstractAction::SINGLE_CONTACT_ACTION_TAG],
]);
$actionTypeFactoryDefinition->addMethodCall('addAction', [
'expense_line_get_by_id',
'Civi\ActionProvider\Action\Expenses\GetExpenseLineById',
......
......@@ -40,5 +40,28 @@ function civicrm_api3_expense_delete($params) {
* @throws API_Exception
*/
function civicrm_api3_expense_get($params) {
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Expense');
$returnValues = CRM_Expenses_BAO_Expense::getValues($params);
return civicrm_api3_create_success($returnValues, $params, 'Expense', 'Get');
}
/**
* FormProcessorInstance.Get API specification (optional)
* This is used for documentation and validation.
*
* @param array $spec description of fields supported by this API call
* @return void
* @see http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
*/
function _civicrm_api3_expense_get_spec(&$spec) {
$fields = CRM_Expenses_BAO_Expense::fields();
foreach($fields as $fieldname => $field) {
$spec[$fieldname] = $field;
}
$spec['total_amount'] = [
'name' => 'total_amount',
'api.filter' => FALSE,
'api.return' => TRUE,
'title' => E::ts('Total amount'),
'type' => CRM_Utils_Type::T_MONEY,
];
}
......@@ -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>2020-09-22</releaseDate>
<version>1.0.4</version>
<releaseDate>2020-10-20</releaseDate>
<version>1.0.5</version>
<develStage>stable</develStage>
<compatibility>
<ver>5.0</ver>
......
......@@ -51,12 +51,12 @@ CREATE TABLE `civicrm_expense` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'Unique Expense ID',
`type_id` int DEFAULT NULL ,
`status_id` int DEFAULT NULL ,
`contact_id` int unsigned NULL COMMENT 'FK to Contact',
`source_contact_id` int unsigned NULL COMMENT 'FK to Contact',
`assignee_contact_id` int unsigned NULL COMMENT 'FK to Contact',
`contact_id` int unsigned NULL COMMENT 'FK naar Contact',
`source_contact_id` int unsigned NULL COMMENT 'FK naar Contact',
`assignee_contact_id` int unsigned NULL COMMENT 'FK naar Contact',
`case_id` int unsigned NULL COMMENT 'FK to Case',
`date` date NULL ,
`currency` varchar(3) DEFAULT NULL COMMENT '3 character string, value from config setting or input via user.'
`currency` varchar(3) DEFAULT NULL COMMENT '3 tekens, gevuld door instelling configuratie of door invoer van gebruiker.'
,
PRIMARY KEY (`id`)
......
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