You need another Hook for the item entity so that when an item is deleted the invoice entity needs to be updated, so you can use the Hook below for the item entity, create a new file under Hooks > PositionenAtelierrechnung > RechnungAmount.php and use the code below, this code should work, however let me know if you face any issues:
PHP Code:
<?php
namespace Espo\Custom\Hooks\PositionenAtelierrechnung;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Repository\Option\RemoveOptions;
use Espo\Core\Hook\Hook\BeforeRemove;
use Espo\Core\Hook\Hook\AfterRemove;
class RechnungAmount implements
BeforeRemove,
AfterRemove
{
public function __construct(private EntityManager $entityManager)
{}
public function beforeRemove(Entity $entity, RemoveOptions $options): void
{
if (!$entity->get('atelierrechnungPositionenId')){
return;
}
$atelierrechnungPositionen = $entity->get('atelierrechnungPositionenId');
$atelierrechnungPositionen->removeLinkMultipleId('positionenAtelierrechnung', $entity->getId());
$this->entityManager->saveEntity($atelierrechnungPositionen);
}
public function afterRemove(Entity $entity, RemoveOptions $options): void
{
$atelierrechnungPositionen = $entity->get('atelierrechnungPositionenId');
if (!$atelierrechnungPositionen->has('positionenAtelierrechnung')) {
return;
}
$itemList = $atelierrechnungPositionen->get('positionenAtelierrechnung');
if (!is_array($itemList)) {
return;
}
if (count($itemList)) {
$amount = 0.0;
foreach ($itemList as $item) {
$amount += $item->verkaufspreisNetto;
}
$amount = round($amount, 2);
$atelierrechnungPositionen->set('summeNetto', $amount);
$this->entityManager->saveEntity($atelierrechnungPositionen);
}
}
}
Hope this help mate
Leave a comment: