Added amount field to case fee, total fee to case, and code to calculate the total

This commit is contained in:
naomi 2021-11-12 09:11:00 +00:00
parent 3eba291d5a
commit d9453c1323
3 changed files with 68 additions and 0 deletions

View File

@ -72,3 +72,15 @@ function opencase_cases_update_8029() {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('notes', 'oc_case', 'oc_case', $storage_definition);
}
function opencase_cases_update_8030() {
$storage_definition = \Drupal\Core\Field\BaseFieldDefinition::create('decimal')
->setLabel(t('Total Fee'))
->setRevisionable(TRUE)
->setSettings([
'prefix' => '£',
])
->SetDisplayConfigurable("view", true);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('total_fee', 'oc_case', 'oc_case', $storage_definition);
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types = 1);
/**
* @file
@ -64,6 +65,51 @@ function opencase_cases_theme() {
];
}
/**
* @return Drupal\opencase_cases\entity\OCCaseFee[]
*/
function opencase_cases_get_case_fees(Drupal\opencase_cases\entity\OCCase $case): array {
$query = \Drupal::entityQuery('oc_case_fee')
->condition('oc_case.target_id', $case->id());
$case_fee_ids = $query->execute();
$case_fees = [];
foreach($case_fee_ids as $id) {
$case_fees[] = Drupal\opencase_cases\entity\OCCaseFee::load($id);
}
return $case_fees;
}
function opencase_cases_get_amount(Drupal\opencase_cases\entity\OCCaseFee $case_fee): string {
return $case_fee->amount->first()->value;
}
function opencase_cases_get_case(Drupal\opencase_cases\entity\OCCaseFee $case_fee): Drupal\opencase_cases\entity\OCCase {
return $case_fee->oc_case->referencedEntities()[0];
}
function opencase_cases_update_total_fee_for_case_belonging_to_case_fee(Drupal\opencase_cases\entity\OCCaseFee $case_fee): void {
$total = 0;
$case = opencase_cases_get_case($case_fee);
$case_fees = opencase_cases_get_case_fees($case);
foreach($case_fees as $case_fee) {
$total += opencase_cases_get_amount($case_fee);
}
$case->set('total_fee', $total);
$case->save();
}
function opencase_cases_oc_case_fee_update(Drupal\opencase_cases\entity\OCCaseFee $case_fee): void {
opencase_cases_update_total_fee_for_case_belonging_to_case_fee($case_fee);
}
function opencase_cases_oc_case_fee_insert(Drupal\opencase_cases\entity\OCCaseFee $case_fee): void {
opencase_cases_update_total_fee_for_case_belonging_to_case_fee($case_fee);
}
function opencase_cases_oc_case_fee_delete(Drupal\opencase_cases\entity\OCCaseFee $case_fee): void {
opencase_cases_update_total_fee_for_case_belonging_to_case_fee($case_fee);
}
function opencase_cases_entity_base_field_info($entity_type) {
$fields = array();

View File

@ -305,6 +305,16 @@ class OCCase extends RevisionableContentEntityBase implements OCCaseInterface {
))
->setDisplayConfigurable('form', true)
->setDisplayConfigurable('view', true);
$fields['total_fee'] = BaseFieldDefinition::create('decimal')
->setLabel(t('Total Fee'))
->setRevisionable(TRUE)
->setSettings([
'prefix' => '£',
])
->SetDisplayConfigurable("view", true);
return $fields;
}
}