From 5dc329d4166f930f4e84c467e60d4c47a3956870 Mon Sep 17 00:00:00 2001 From: matze Date: Tue, 10 Dec 2024 16:50:53 +0100 Subject: [PATCH] added batch work log --- ...bstance.yml => asset.type.batch_usage.yml} | 8 +-- ...chine.yml => asset.type.machine_usage.yml} | 2 +- config/install/log.type.batch_work.yml | 12 +++++ src/Plugin/Asset/AssetType/Batch.php | 2 +- .../{SubstanceUsage.php => BatchUsage.php} | 10 ++-- src/Plugin/Asset/AssetType/MachineUsage.php | 2 +- src/Plugin/Asset/AssetType/Substance.php | 52 +++++++++++-------- src/Plugin/Asset/AssetType/Usage.php | 7 --- src/Plugin/Log/LogType/BatchWork.php | 42 +++++++++++++++ src/Plugin/Log/LogType/FieldWork.php | 41 +++++++-------- .../Constraint/BatchWorkBatchesConstraint.php | 27 ++++++++++ .../BatchWorkBatchesConstraintValidator.php | 38 ++++++++++++++ 12 files changed, 179 insertions(+), 64 deletions(-) rename config/install/{asset.type.usage_substance.yml => asset.type.batch_usage.yml} (50%) rename config/install/{asset.type.usage_machine.yml => asset.type.machine_usage.yml} (92%) create mode 100644 config/install/log.type.batch_work.yml rename src/Plugin/Asset/AssetType/{SubstanceUsage.php => BatchUsage.php} (77%) create mode 100644 src/Plugin/Log/LogType/BatchWork.php create mode 100644 src/Plugin/Validation/Constraint/BatchWorkBatchesConstraint.php create mode 100644 src/Plugin/Validation/Constraint/BatchWorkBatchesConstraintValidator.php diff --git a/config/install/asset.type.usage_substance.yml b/config/install/asset.type.batch_usage.yml similarity index 50% rename from config/install/asset.type.usage_substance.yml rename to config/install/asset.type.batch_usage.yml index dbdc969..a845017 100644 --- a/config/install/asset.type.usage_substance.yml +++ b/config/install/asset.type.batch_usage.yml @@ -4,9 +4,9 @@ dependencies: enforced: module: - farm_agrikern -id: usage_substance -label: SubstanceUsage -description: 'Substance Usage Asset' -name_pattern: 'SubstanceUsage asset [asset:id]' +id: batch_usage +label: BatchUsage +description: 'Batch Usage Asset' +name_pattern: 'BatchUsage asset [asset:id]' workflow: asset_default new_revision: true \ No newline at end of file diff --git a/config/install/asset.type.usage_machine.yml b/config/install/asset.type.machine_usage.yml similarity index 92% rename from config/install/asset.type.usage_machine.yml rename to config/install/asset.type.machine_usage.yml index c97cf7c..05ca8ea 100644 --- a/config/install/asset.type.usage_machine.yml +++ b/config/install/asset.type.machine_usage.yml @@ -4,7 +4,7 @@ dependencies: enforced: module: - farm_agrikern -id: usage_machine +id: machine_usage label: MachineUsage description: 'Machine Usage Asset' name_pattern: 'MachineUsage asset [asset:id]' diff --git a/config/install/log.type.batch_work.yml b/config/install/log.type.batch_work.yml new file mode 100644 index 0000000..007f3b5 --- /dev/null +++ b/config/install/log.type.batch_work.yml @@ -0,0 +1,12 @@ +langcode: en +status: true +dependencies: + enforced: + module: + - farm_agrikern +id: batch_work +label: BatchWork +description: 'Batch work log' +name_pattern: 'Batch work log [log:id]' +workflow: farm_log_workflow +new_revision: true diff --git a/src/Plugin/Asset/AssetType/Batch.php b/src/Plugin/Asset/AssetType/Batch.php index 1a23084..34527de 100644 --- a/src/Plugin/Asset/AssetType/Batch.php +++ b/src/Plugin/Asset/AssetType/Batch.php @@ -22,7 +22,7 @@ class Batch extends FarmAssetType { 'substance_field' => [ 'type' => 'entity_reference', 'label' => $this->t('Substance'), - 'description' => $this->t('What substance was added?'), + 'description' => $this->t('What substance doas this batch track?'), 'target_type' => 'asset', 'target_bundle' => 'substance', 'multiple' => FALSE, diff --git a/src/Plugin/Asset/AssetType/SubstanceUsage.php b/src/Plugin/Asset/AssetType/BatchUsage.php similarity index 77% rename from src/Plugin/Asset/AssetType/SubstanceUsage.php rename to src/Plugin/Asset/AssetType/BatchUsage.php index 8899bf0..90bce53 100644 --- a/src/Plugin/Asset/AssetType/SubstanceUsage.php +++ b/src/Plugin/Asset/AssetType/BatchUsage.php @@ -5,14 +5,14 @@ namespace Drupal\farm_agrikern\Plugin\Asset\AssetType; use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType; /** -* Provides the usage_substance asset type. +* Provides the batch usage asset type. * * @AssetType( -* id = "usage_substance", -* label = @Translation("Substance Usage"), +* id = "batch_usage", +* label = @Translation("Batch Usage"), * ) */ -class SubstanceUsage extends Usage { +class BatchUsage extends Usage { /** * {@inheritdoc} */ @@ -22,7 +22,7 @@ class SubstanceUsage extends Usage { 'batch_field' => [ 'type' => 'entity_reference', 'label' => $this->t('Batch'), - 'description' => $this->t('What batch of the substance was used?'), + 'description' => $this->t('What batch was used?'), 'target_type' => 'asset', 'target_bundle' => 'batch', 'multiple' => FALSE, diff --git a/src/Plugin/Asset/AssetType/MachineUsage.php b/src/Plugin/Asset/AssetType/MachineUsage.php index cd04c27..5802ef9 100644 --- a/src/Plugin/Asset/AssetType/MachineUsage.php +++ b/src/Plugin/Asset/AssetType/MachineUsage.php @@ -8,7 +8,7 @@ use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType; * Provides the machineusage asset type. * * @AssetType( -* id = "usage_machine", +* id = "machine_usage", * label = @Translation("Machine Usage"), * ) */ diff --git a/src/Plugin/Asset/AssetType/Substance.php b/src/Plugin/Asset/AssetType/Substance.php index ae5b235..de2fca9 100644 --- a/src/Plugin/Asset/AssetType/Substance.php +++ b/src/Plugin/Asset/AssetType/Substance.php @@ -18,30 +18,36 @@ class Substance extends FarmAssetType { */ public function buildFieldDefinitions() { $fields = parent::buildFieldDefinitions(); - $options = [ - 'type' => 'entity_reference', - 'label' => $this->t('Ingredients'), - 'description' => $this->t('What ingredients are in the substance?'), - 'target_type' => 'asset', - 'target_bundle' => 'ingredient', - 'multiple' => TRUE, - 'required' => FALSE, + + $field_info = [ + 'ingredients_field' => [ + 'type' => 'entity_reference', + 'label' => $this->t('Ingredients'), + 'description' => $this->t('What ingredients are in the substance?'), + 'target_type' => 'asset', + 'target_bundle' => 'ingredient', + 'multiple' => TRUE, + 'required' => FALSE, + ], + 'unit_field' => [ + 'type' => 'entity_reference', + 'label' => $this->t('Unit'), + 'description' => $this->t('What unit is the substance measured in?'), + 'target_type' => 'taxonomy_term', + 'target_bundle' => 'unit', + 'auto_create' => TRUE, + 'multiple' => FALSE, + 'required' => TRUE, + ], ]; - $fields['ingredients_field'] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($options) - ->addConstraint('AgriKernSubstanceIngredients'); - - $options = [ - 'type' => 'entity_reference', - 'label' => $this->t('Unit'), - 'description' => $this->t('What unit is the substance measured in?'), - 'target_type' => 'taxonomy_term', - 'target_bundle' => 'unit', - 'auto_create' => TRUE, - 'multiple' => FALSE, - 'required' => TRUE, - ]; - $fields['unit_field'] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($options); - + + foreach ($field_info as $name => $info) { + $fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info); + } + + $fields['ingredients_field']->addConstraint('AgriKernSubstanceIngredients'); + + return $fields; } } \ No newline at end of file diff --git a/src/Plugin/Asset/AssetType/Usage.php b/src/Plugin/Asset/AssetType/Usage.php index e05f2f5..b99b2c7 100644 --- a/src/Plugin/Asset/AssetType/Usage.php +++ b/src/Plugin/Asset/AssetType/Usage.php @@ -20,13 +20,6 @@ class Usage extends FarmAssetType { 'description' => $this->t('How much was the quantiy used in the quantities unit of measurement?'), 'required' => TRUE, ], - // This field is automatically filled using LogEventSubscriber - 'cost_field' => [ - 'type' => 'fraction', - 'label' => $this->t('Cost'), - 'description' => $this->t('THIS FIELD IS GENERATED AUTOMATICALLY AND SHOULD NOT BE ADJUSTED!'), - 'required' => FALSE, - ], ]; foreach ($field_info as $name => $info) { $fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info); diff --git a/src/Plugin/Log/LogType/BatchWork.php b/src/Plugin/Log/LogType/BatchWork.php new file mode 100644 index 0000000..c2920b9 --- /dev/null +++ b/src/Plugin/Log/LogType/BatchWork.php @@ -0,0 +1,42 @@ + [ + 'type' => 'entity_reference', + 'label' => $this->t('Batch usages'), + 'description' => $this->t('What batches were used and how much?'), + 'target_type' => 'asset', + 'target_bundle' => 'batch_usage', + 'multiple' => TRUE, + 'required' => TRUE, + ], + ]; + + foreach ($field_info as $name => $info) { + $fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info); + } + + $fields['batch_usages_field']->addConstraint('AgriKernBatchWorkBatches'); + + return $fields; + } +} diff --git a/src/Plugin/Log/LogType/FieldWork.php b/src/Plugin/Log/LogType/FieldWork.php index 4a6bcd0..4ec72bb 100644 --- a/src/Plugin/Log/LogType/FieldWork.php +++ b/src/Plugin/Log/LogType/FieldWork.php @@ -5,20 +5,20 @@ namespace Drupal\farm_agrikern\Plugin\Log\LogType; use Drupal\farm_entity\Plugin\Log\LogType\FarmLogType; /** - * Provides the field work log type. - * - * @LogType( - * id = "field_work", - * label = @Translation("Field work"), - * ) - */ +* Provides the field work log type. +* +* @LogType( +* id = "field_work", +* label = @Translation("Field work"), +* ) +*/ class FieldWork extends WorkLog { /** * {@inheritdoc} */ public function buildFieldDefinitions() { $fields = parent::buildFieldDefinitions(); - + $field_info = [ 'patch_field' => [ 'type' => 'entity_reference', @@ -29,24 +29,21 @@ class FieldWork extends WorkLog { 'required' => TRUE, 'multiple' => FALSE, ], + 'machine_usages_field' => [ + 'type' => 'entity_reference', + 'label' => $this->t('Machine usages'), + 'description' => $this->t('What machines were used and how much?'), + 'target_type' => 'asset', + 'target_bundle' => 'machine_usage', + 'multiple' => TRUE, + 'required' => TRUE, + ], ]; - + foreach ($field_info as $name => $info) { $fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info); } - - $options = [ - 'type' => 'entity_reference', - 'label' => $this->t('Machine usages'), - 'description' => $this->t('What machines were used and how much?'), - 'target_type' => 'asset', - 'target_bundle' => 'usage_machine', - 'multiple' => TRUE, - 'required' => TRUE, - - ]; - $fields['machine_usages_field'] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($options) - ->addConstraint('AgriKernFieldWorkMachines'); + $fields['machine_usages_field']->addConstraint('AgriKernFieldWorkMachines'); return $fields; } diff --git a/src/Plugin/Validation/Constraint/BatchWorkBatchesConstraint.php b/src/Plugin/Validation/Constraint/BatchWorkBatchesConstraint.php new file mode 100644 index 0000000..7f569e0 --- /dev/null +++ b/src/Plugin/Validation/Constraint/BatchWorkBatchesConstraint.php @@ -0,0 +1,27 @@ + $item) { + $batch_usage = \Drupal::entityTypeManager()->getStorage('asset')->load($item->target_id); + $batch_id = $batch_usage->batch_field->target_id; + if (isset($seen_batches[$batch_id])) { + $this->context->buildViolation($constraint->notUnique) + ->atPath($delta) + ->addViolation(); + } + $seen_batches[$batch_id] = true; + } + } + +}