removed cost calculation (do dynamically)

This commit is contained in:
2024-12-10 15:54:55 +01:00
commit cff83d30e9
24 changed files with 678 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_agrikern
id: batch
label: Batch
description: 'Batch Asset'
name_pattern: 'Batch asset [asset:id]'
workflow: asset_default
new_revision: true

View File

@@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_agrikern
id: chemical
label: Chemical
description: 'Chemical Asset'
name_pattern: 'Chemical asset [asset:id]'
workflow: asset_default
new_revision: true

View File

@@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_agrikern
id: ingredient
label: Ingredient
description: 'Ingredient Asset'
name_pattern: 'Ingredient asset [asset:id]'
workflow: asset_default
new_revision: true

View File

@@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_agrikern
id: machine
label: Machine
description: 'Machine Asset'
name_pattern: 'Machine asset [asset:id]'
workflow: asset_default
new_revision: true

View File

@@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_agrikern
id: patch
label: Patch
description: 'Patch Asset'
name_pattern: 'Patch asset [asset:id]'
workflow: asset_default
new_revision: true

View File

@@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_agrikern
id: substance
label: Substance
description: 'Substance Asset'
name_pattern: 'Substance asset [asset:id]'
workflow: asset_default
new_revision: true

View File

@@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_agrikern
id: usage_machine
label: MachineUsage
description: 'Machine Usage Asset'
name_pattern: 'MachineUsage asset [asset:id]'
workflow: asset_default
new_revision: true

View File

@@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_agrikern
id: usage_substance
label: SubstanceUsage
description: 'Substance Usage Asset'
name_pattern: 'SubstanceUsage asset [asset:id]'
workflow: asset_default
new_revision: true

View File

@@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_agrikern
id: field_work
label: FieldWork
description: 'Field work log'
name_pattern: 'Field work log [log:id]'
workflow: farm_log_workflow
new_revision: true

12
farm_agrikern.info.yml Normal file
View File

@@ -0,0 +1,12 @@
name: AgriKern
description: Customize the farmOS datamodel to fit a central european agriculture farm.
type: module
package: farmOS Contrib
core_version_requirement: ^10
dependencies:
- farm:farm_entity
- farm:asset
- farm:log
- farm_land
- farm_season
- fraction

View File

@@ -0,0 +1,43 @@
<?php
namespace Drupal\farm_agrikern\Plugin\Asset\AssetType;
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
/**
* Provides the batch asset type.
*
* @AssetType(
* id = "batch",
* label = @Translation("Batch"),
* )
*/
class Batch extends FarmAssetType {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
$field_info = [
'substance_field' => [
'type' => 'entity_reference',
'label' => $this->t('Substance'),
'description' => $this->t('What substance was added?'),
'target_type' => 'asset',
'target_bundle' => 'substance',
'multiple' => FALSE,
'required' => TRUE,
],
'unit_price_field' => [
'type' => 'fraction',
'label' => $this->t('Unit price'),
'description' => $this->t('Price in Euro per unit of the substance. Price is positive if the batch was pruchased and is negative if the batch was produced.'),
'required' => TRUE,
],
];
foreach ($field_info as $name => $info) {
$fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info);
}
return $fields;
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Drupal\farm_agrikern\Plugin\Asset\AssetType;
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
/**
* Provides the chemical asset type.
*
* @AssetType(
* id = "chemical",
* label = @Translation("Chemical"),
* )
*/
class Chemical extends FarmAssetType {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
return $fields;
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Drupal\farm_agrikern\Plugin\Asset\AssetType;
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
/**
* Provides the ingredient asset type.
*
* @AssetType(
* id = "ingredient",
* label = @Translation("Ingredient"),
* )
*/
class Ingredient extends FarmAssetType {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
$field_info = [
'chemical_field' => [
'type' => 'entity_reference',
'label' => $this->t('Chemical'),
'description' => $this->t('What chemical does the ingredient track?'),
'target_type' => 'asset',
'target_bundle' => 'chemical',
'multiple' => FALSE,
'required' => TRUE,
],
'amount_field' => [
'type' => 'fraction',
'label' => $this->t('Amount'),
'description' => $this->t('How much of the chemical is in the ingredient in g/l'),
'required' => TRUE,
],
];
foreach ($field_info as $name => $info) {
$fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info);
}
return $fields;
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Drupal\farm_agrikern\Plugin\Asset\AssetType;
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
/**
* Provides the machine asset type.
*
* @AssetType(
* id = "machine",
* label = @Translation("Machine"),
* )
*/
class Machine extends FarmAssetType {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
$field_info = [
'unit_price_field' => [
'type' => 'fraction',
'label' => $this->t('Unit price'),
'description' => $this->t('Price in Euro per unit of usage on the machine.'),
'required' => TRUE,
],
'unit_field' => [
'type' => 'entity_reference',
'label' => $this->t('Unit'),
'description' => $this->t('What unit is used to track machine usage?'),
'target_type' => 'taxonomy_term',
'target_bundle' => 'unit',
'auto_create' => TRUE,
'multiple' => FALSE,
'required' => TRUE,
],
];
foreach ($field_info as $name => $info) {
$fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info);
}
return $fields;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Drupal\farm_agrikern\Plugin\Asset\AssetType;
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
/**
* Provides the machineusage asset type.
*
* @AssetType(
* id = "usage_machine",
* label = @Translation("Machine Usage"),
* )
*/
class MachineUsage extends Usage {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
$field_info = [
'machine_field' => [
'type' => 'entity_reference',
'label' => $this->t('Machine'),
'description' => $this->t('What machine was used?'),
'target_type' => 'asset',
'target_bundle' => 'machine',
'multiple' => FALSE,
'required' => TRUE,
],
];
foreach ($field_info as $name => $info) {
$fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info);
}
return $fields;
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Drupal\farm_agrikern\Plugin\Asset\AssetType;
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
use phpDocumentor\Reflection\PseudoTypes\False_;
/**
* Provides the patch asset type.
*
* @AssetType(
* id = "patch",
* label = @Translation("Patch"),
* )
*/
class Patch extends FarmAssetType {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
$field_info = [
'land_field' => [
'type' => 'entity_reference',
'label' => $this->t('Land'),
'description' => $this->t("At what Land asset is the patch located?"),
'target_type' => 'asset',
'target_bundle' => 'land',
'required' => TRUE,
'multiple' => FALSE,
],
'crop_field' => [
'type' => 'entity_reference',
'label' => $this->t('Crop/variety'),
'description' => $this->t("Enter this patch asset's crop/variety."),
'target_type' => 'taxonomy_term',
'target_bundle' => 'crop',
'auto_create' => TRUE,
'required' => TRUE,
'multiple' => FALSE,
],
'season_field' => [
'type' => 'entity_reference',
'label' => $this->t('Season'),
'description' => $this->t('Assign this to a season for easier searching later.'),
'target_type' => 'taxonomy_term',
'target_bundle' => 'season',
'auto_create' => TRUE,
'multiple' => FALSE,
'required' => TRUE,
],
'area_field' => [
'type' => 'fraction',
'label' => $this->t('Area'),
'description' => $this->t('What is the area of the patch in hectares (ha)?'),
'required' => TRUE,
],
];
foreach ($field_info as $name => $info) {
$fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info);
}
return $fields;
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Drupal\farm_agrikern\Plugin\Asset\AssetType;
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
/**
* Provides the substance asset type.
*
* @AssetType(
* id = "substance",
* label = @Translation("Substance"),
* )
*/
class Substance extends FarmAssetType {
/**
* {@inheritdoc}
*/
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,
];
$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);
return $fields;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Drupal\farm_agrikern\Plugin\Asset\AssetType;
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
/**
* Provides the usage_substance asset type.
*
* @AssetType(
* id = "usage_substance",
* label = @Translation("Substance Usage"),
* )
*/
class SubstanceUsage extends Usage {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
$field_info = [
'batch_field' => [
'type' => 'entity_reference',
'label' => $this->t('Batch'),
'description' => $this->t('What batch of the substance was used?'),
'target_type' => 'asset',
'target_bundle' => 'batch',
'multiple' => FALSE,
'required' => TRUE,
],
];
foreach ($field_info as $name => $info) {
$fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info);
}
return $fields;
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Drupal\farm_agrikern\Plugin\Asset\AssetType;
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
/**
* Provides the usage asset type.
*/
class Usage extends FarmAssetType {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
$field_info = [
'amount_field' => [
'type' => 'fraction',
'label' => $this->t('Amount'),
'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);
}
return $fields;
}
}

View File

@@ -0,0 +1,53 @@
<?php
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"),
* )
*/
class FieldWork extends FarmLogType {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
$field_info = [
'patch_field' => [
'type' => 'entity_reference',
'label' => $this->t('Patch'),
'description' => $this->t("What patch was the work done on?"),
'target_type' => 'asset',
'target_bundle' => 'patch',
'required' => TRUE,
'multiple' => FALSE,
],
];
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');
return $fields;
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Drupal\farm_agrikern\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Provides a FieldWorkMachines constraint.
*
* @Constraint(
* id = "AgriKernFieldWorkMachines",
* label = @Translation("FieldWorkMachines", context = "Validation"),
* )
*
* @DCG
* To apply this constraint on third party entity types implement either
* hook_entity_base_field_info_alter() or hook_entity_bundle_field_info_alter().
*
* @see https://www.drupal.org/node/2015723
*/
final class FieldWorkMachinesConstraint extends Constraint {
public string $notUnique = 'Some machines were entered twice.';
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Drupal\farm_agrikern\Plugin\Validation\Constraint;
use Drupal\Core\Field\FieldItemListInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the FieldWorkMachines constraint.
*/
final class FieldWorkMachinesConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate(mixed $items, Constraint $constraint): void {
if (!$items instanceof FieldItemListInterface) {
throw new \InvalidArgumentException(
sprintf('The validated value must be instance of \Drupal\Core\Field\FieldItemListInterface, %s was given.', get_debug_type($items))
);
}
$seen_machines = [];
foreach ($items as $delta => $item) {
$machine_usage = \Drupal::entityTypeManager()->getStorage('asset')->load($item->target_id);
$machine_id = $machine_usage->machine_field->target_id;
if (isset($seen_machines[$machine_id])) {
$this->context->buildViolation($constraint->notUnique)
->atPath($delta)
->addViolation();
}
$seen_machines[$machine_id] = true;
}
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Drupal\farm_agrikern\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Provides a SubstanceIngredients constraint.
*
* @Constraint(
* id = "AgriKernSubstanceIngredients",
* label = @Translation("SubstanceIngredients", context = "Validation"),
* )
*
* @DCG
* To apply this constraint on third party entity types implement either
* hook_entity_base_field_info_alter() or hook_entity_bundle_field_info_alter().
*
* @see https://www.drupal.org/node/2015723
*/
final class SubstanceIngredientsConstraint extends Constraint {
public string $notUnique = 'Some chemicals were entered twice.';
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Drupal\farm_agrikern\Plugin\Validation\Constraint;
use Drupal\Core\Field\FieldItemListInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the SubstanceIngredients constraint.
* Ensures there are no duplicate Chemicals in the Ingredients of a Substance.
*/
final class SubstanceIngredientsConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate(mixed $items, Constraint $constraint): void {
if (!$items instanceof FieldItemListInterface) {
throw new \InvalidArgumentException(
sprintf('The validated value must be instance of \Drupal\Core\Field\FieldItemListInterface, %s was given.', get_debug_type($items))
);
}
$seen_quantities = [];
foreach ($items as $delta => $item) {
$ingredient = \Drupal::entityTypeManager()->getStorage('asset')->load($item->target_id);
$chemical_id = $ingredient->chemical_field->target_id;
if (isset($seen_quantities[$chemical_id])) {
$this->context->buildViolation($constraint->notUnique)
->atPath($delta)
->addViolation();
}
$seen_quantities[$chemical_id] = true;
}
}
}