Announcement

Collapse
No announcement yet.

Calculate trough hook includes deleted record

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

  • Calculate trough hook includes deleted record

    Hi all,

    I made a hook to add some costs per year in a record in the entity "object" from underlying records in the entity "rows".
    It works as expected, but it also adds records that are marked as deleted in the database (colum deleted = 1).

    I tried if ( $row->get('deleted') = '1' ) where i now have if ( $row->get('status') != '3' ) but then it is still counted. Also i want to keep filtering out the status 3 rows.


    The hooks is as following:

    namespace Espo\Custom\Hooks\rows;

    use Espo\ORM\Entity;

    class MJOP extends \Espo\Core\Hooks\Base {

    public function afterSave( Entity $entity, array $options = array() ) {
    $object = $entity->get('object');
    $rows = $object->get('rows);

    $year0 = $year1 = $year2 = $year3 = $year4 = $year5 = $year6 = $year7 = $year8 = $year9 = 0;
    foreach ( $rows as $row) {

    $year0 = $row->get('year0');
    $year1 = $row->get('year1');
    $year2 = $row->get('year2');
    $year3 = $row->get('year3');
    $year4 = $row->get('year4');
    $year5 = $row->get('year5');
    $year6 = $row->get('year6');
    $year7 = $row->get('year7');
    $year8 = $row->get('year8');
    $year9 = $row->get('year9');

    if ( $row->get('status') != '3' ) {
    // count these numbers to object
    $amountyear0 += $year0;
    $amountyear1 += $year1;
    $amountyear2 += $year2;
    $amountyear3 += $year3;
    $amountyear4 += $year4;
    $amountyear5 += $year5;
    $amountyear6 += $year6;
    $amountyear7 += $year7;
    $amountyear8 += $year8;
    $amountyear9 += $year9;
    }
    }

    $object->set( array(
    'amountyear0' => $amountyear0,
    'amountyear1' => $amountyear1,
    'amountyear2' => $amountyear2,
    'amountyear3' => $amountyear3,
    'amountyear4' => $amountyear4,
    'amountyear5' => $amountyear5,
    'amountyear6' => $amountyear6,
    'amountyear7' => $amountyear7,
    'amountyear8' => $amountyear8,
    'amountyear9' => $amountyear9,
    ));

    $entityManager = $this->getEntityManager();
    $entityManager->saveEntity($object, array('skipModifiedBy' => true));
    }
    }
    ?>​
Working...
X