About entity\sumRelated issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #16
    Here is how you can do it, follow steps below:

    Create a file custom/Espo/Custom/Resources/metadata/app/scheduledJobs.json:

    PHP Code:
    {
        "CalculateToplamBorc": {
            "jobClassName": "Espo\\Custom\\Jobs\\CalculateToplamBorc"
        }
    }


    Create a file custom/Espo/Custom/Jobs/CalculateToplamBorc.php with the content.

    PHP Code:
    <?php
    
    namespace Espo\Custom\Jobs;
    
    use Espo\Core\Job\JobDataLess;
    use Espo\Core\ORM\EntityManager;
    use Espo\Custom\Entities\Muhasebe;
    
    class CalculateToplamBorc implements JobDataLess
    {
        
        public function __construct(
            private EntityManager $entityManager
        ){}
    
        public function run(): void
        {    
            $muhasebeList = $this->entityManager
                            ->getRDBRepository(Muhasebe::ENTITY_TYPE)
                            ->where([
                                'accountId!=' => null,
                            ])
                            ->find();
    
            foreach ($muhasebeList as $muhasebe) {
    
                $opportunitiesList = $entityManager
                    ->getRDBRepository(Muhasebe::ENTITY_TYPE)
                    ->getRelation($muhasebe, 'opportunities')
                    ->where([
                        'accountId' => $muhasebe->get('accountId')
                    ])
                    ->order('createdAt', 'DESC')
                    ->findOne();
    
                $total = 0;
    
                foreach ($opportunitiesList as $opportunity) {
                    $total = $total + $opportunity->get('amount');
                }
    
                $muhasebe->set('toplamBorc', $total);
    
                $this->entityManager->saveEntity($muhasebe);
    
            }
        }    
    }

    Once you complete the steps above, clean cache and rebuild the system and go to administration > Scheduled Jobs and then create schedule job you should be to find your custom job there and just add (Scheduling) and make sure it is active and that is it the job will be executed based on your Scheduling.

    Hope this helps
    Rabii
    Web Dev

    Comment

    • alpkrgl
      Junior Member
      • Oct 2023
      • 21

      #17
      Thanks for reply ?

      But there is 3-4 entites. So how can i use this formula for 3-4 entities? Is it posible to use multi with these codes?

      Comment


      • rabii
        rabii commented
        Editing a comment
        what do you mean by 3 entities ?
    • alpkrgl
      Junior Member
      • Oct 2023
      • 21

      #18
      There are 3 entities i want it to do auto recalculate. Is it posible? Thanks

      Comment


      • rabii
        rabii commented
        Editing a comment
        yes you can do it, you can just add the other entities on the same custom job or create a new job for each entity.
    Working...