Announcement

Collapse
No announcement yet.

Get Data from calculated field in an Relation

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get Data from calculated field in an Relation

    Hi,

    In my Entity Opportunities is an 1:n Relation to a custom entity called "Kredite". The Entity "Kredite" has an not storable field "Restschuld" that is calculated in the loadAdditionalFields Method. Now I want to show the sum of all "Kredite" belong to an Opportunity in an not storable field but when I try to get the field "Restschuld" it is "0" (maybe because it is not storable) is the a way to get the data from the field?

    public function loadAdditionalFields(Entity $entity)
    {
    parent::loadAdditionalFields($entity);
    $entity->loadLinkMultipleField('kredites');
    $kredite = $entity->get('kredites');
    $restschuldsum = 0;
    foreach ($kredite as $kredit){
    $restschuldsum = $restschuldsum + $kredit->get('restschuld');
    }

    $entity->set('testing', $restschuldsum." ");
    }

    Or is there a way to save the Data in the loadAdditionalFields method

    public function loadAdditionalFields(Entity $entity)
    {
    parent::loadAdditionalFields($entity);
    $rate = $entity->get('rate');
    $zinssatz = $entity->get('zinssatz');
    $startdatum = date_create($entity->get('startdatum'));
    $laufzeit = $entity->get('laufzeit');
    $now = new \DateTime("now");
    $startschuld = $entity->get('startschuld');

    $interval = date_diff($startdatum, $now);
    $month = $interval->y * 12 + $interval->m;

    $schuld = $startschuld;
    for($i=0; $i < $month; $i++) {
    $zinsanteil = $schuld * $zinssatz / 12 / 100;
    $tilgung = $rate - $zinsanteil;
    $schuld = $schuld - $tilgung;
    }
    $entity->set('restschuld', $schuld);

    //save?

    }

  • #2
    Hello
    loadLinkMultipleField loads list of related entities, but for them didn't call the method loadAdditionalFields, where you set your field 'restschuld'
    Propose you to set the method _getRestschuld for entity Kredite (i think the path is custom/Espo/Custom/Entities/Kredite.php), where you need to return calculated value (example in Entity CampaignTrackingUrl)

    Comment


    • #3
      Hi,

      I have created a method like this:

      protected function _getCalcRestschuld(Entity $entity)
      {
      $rate = $entity->get('rate');
      $zinssatz = $entity->get('zinssatz');
      $startdatum = date_create($entity->get('startdatum'));
      $laufzeit = $entity->get('laufzeit');
      $now = new \DateTime("now");
      $startschuld = $entity->get('startschuld');

      $interval = date_diff($startdatum, $now);
      //$month = $interval->format('%m') + $interval->format('%y') * 12;
      $month = $interval->y * 12 + $interval->m;

      $schuld = $startschuld;
      for($i=0; $i < $month; $i++) {
      $zinsanteil = $schuld * $zinssatz / 12 / 100;
      $tilgung = $rate - $zinsanteil;
      $schuld = $schuld - $tilgung;

      }
      return $schuld;
      }

      and want to access it like I cecked it for "UrltoUse"

      $kredit->get('CalcRestschuld');

      but won't work.


      Comment


      • #4
        if you create it in folder Entities, you need to use $this instead of $entity, and this method has no argument. Read application/Espo/ORM/Entity.php and example, I wrote before

        Comment


        • #5
          My fault...

          Had the function under custom/Espo/Custom/Services/Kredite.php and not in /custom/Espo/Custom/Entities/Kredite.php now it works well.

          Thx for support!!!

          I plan to code an Android and iOS app for my project, now my big question when I access the calculated field via API is there the function "loadAdditionalFields" called?

          Comment


          • #6
            API access is going thru controllers, and in controllers use data from services at most. so I think Yes. But you can check the code

            Comment

            Working...
            X