Announcement

Collapse
No announcement yet.

Calculate Task field name?

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

  • Calculate Task field name?

    In Task entity, I have a field name called, "tot_hours". Once field name, "status", has changed to "Completed", calculated the difference of date_end and date_start. In the applications\espo\modules\crm\services\task.php, code below:
    public function handleHours(Entity $entity, array $options) { $entityManager = $this->getEntityManager(); $task_status = $entityManager->getRepository('Task'); $pdo = $this->getEntityManager()->getPDO(); if($task_status->get('status') == 'Completed' && empty($task_status->get('tot_hours'))){ $sql = "UPDATE task SET tot_hours = TIME_TO_SEC(TIMEDIFF(date_end,date_start))/3600 WHERE status = 'Completed'"; $stmt = $pdo->prepare($sql); $stmt->execute(); $entityManager->saveEntity($entityManager); } else{ if($task_status->get('status' != 'Completed' && !empty($task_status->get('tot_hours')))) { $othesql = "UPDATE task SET tot_hours = NULL WHERE status <> 'Completed'"; $othe_stmt = $pdo->prepare($othesql); $othe_stmt->execute(); $entityManager->saveEntity($entityManager); } } } The field name, "tot_hours", doesn't change in the db when status has been changed to 'Completed'. Not sure why. Assistance please.
    Last edited by worldmiros; 01-07-2016, 04:46 PM.

  • #2
    Any help here?

    Comment


    • #3
      yuri Is the code above correct to calculate fields when status has been changed?

      Comment


      • #4
        afterSave()

        Comment


        • #5
          use beforeSave
          because it can loop.

          Comment


          • worldmiros
            worldmiros commented
            Editing a comment
            yuri Is applications\espo\modules\crm\services\task.php the correct file to implement beforeSave() function? And as an example, should I look at the beforeSave() function in QuoteItem.php as a guideline?
            Last edited by worldmiros; 01-08-2016, 02:28 PM. Reason: wanting to ask another question for a work project that is time sensitive

        • #6
          yuri If I want to run a query in beforeSave() function, is that possible? If so, how can I retrieve the db configuration to run a pdo query. I try using $this->getConfig(), but when I, "$stmt = $pdo->prepare($sql), "method 'prepare' not found in class", occurs.

          Comment


          • #7

            public function beforeSave(Entity $entity) { $entityManager = $this->getEntityManager(); $task_status = $entity->get('status'); $pdo = $this->getConfig(); if($task_status == 'Completed' && empty($task_status)) { $sql = "UPDATED task SET tot_hours = TIME_TO_SEC(TIMEDIFF(date_end,date_start))/3600 WHERE status = 'Completed'"; $stmt = $pdo->prepare($sql); $entity->set('tot_hours',$stmt->execute()); } else{ if($task_status != 'Completed' && !empty($entity->get('tot_hours'))){ $sec_sql = "UPDATE task SET tot_hours = NULL WHERE status <> 'Completed'"; $othe_stmt = $pdo->prepare($sec_sql); $entity->set('tot_hours',$othe_stmt->execute()); } } }
            Last edited by worldmiros; 01-08-2016, 07:45 PM.

            Comment


            • worldmiros
              worldmiros commented
              Editing a comment
              get error, "you have not modified the record".

          • #8
            yuri
            PHP Code:
              protected function beforeSave(Entity $entity,array $options) {          if($entity->isFieldChanged('status')){         if($entity->get('status') == 'Completed'){             $dateStart $entity->get('dateStart');             $dateEnd $entity->get('dateEnd');              $diff abs(strtotime($dateStart)) - abs(strtotime($dateEnd));             $totalHours floor($diff) / 3600;              $entity->set('tot_hours',$totalHours);         }         else{             $entity->set('tot_hours',null);         }     } } 
            The code above was implemented in, application/espo/modules/crm/services/task.php file, but data in the column_name, "tot_hours", didn't change value. I used beforeSave() function in, application/espo/modules/crm/repositories/task.php, as a reference. You said to use the beforeSave() function to reach my goal. The only difference between the code above and the beforeSave in the other directory is,
            PHP Code:
            parent::beforeSave($entity,$options

            Comment


            • #9
              Issue has been resolved!

              Comment

              Working...
              X