Help in Formula Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sapyantuk
    Senior Member
    • Oct 2020
    • 265

    Help in Formula Loop

    Hello Team,
    I am creating Leave Management System Using Espo
    i have two date field Leave from and Leave Till staff will request leave for multiple Date Like 2022-05-27 ( Leave From ) 2022-5-30 ( leave till ) so leave Date will be 2022-05-27 , 2022-05-28 , 2022-05-29 and 2022-05-30 . now how to write a code so that result will be


    2022-05-27
    2022-05-28
    2022-05-29
    2022-05-30

    so that i can create a record in another entity on return dates array.

    yuri
    Last edited by sapyantuk; 05-27-2022, 01:41 PM.
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    I think best way is to use Hook on your entity and then you could benefit from Carbon something like below:

    Code:
    <?php
    namespace Espo\Custom\Hooks\YOUR_ENTITY;
    
    use Espo\ORM\Entity;
    
    use Carbon\Carbon;
    
    use Carbon\CarbonPeriod;
    
    class MyHook
    {
    
    // optional parameter, defines in which order hooks will be processed less value means sooner
    public static $order = 5;
    
    public function beforeSave(Entity $entity, array $options): void
    {
    
       if ($entity->isNew()) {
    
    
       $fromDate = Carbon::createFromFormat($entity->get('from_date'));
    
      $tillDate = Carbon::createFromFormat($entity->get('till_date'));
    
      $dateRange = CarbonPeriod::create($fromDate, $tillDate);
    
       $dateRange->toArray();
    
       }
     }
    }
    You can add code which loop through the dateRange and create new record for the other entity.

    Hope this helps.
    Rabii
    Web Dev

    Comment

    Working...