Conditional Formatting for Overdue Dates in List View

Hi all,

Have been struggling for a few hours on a basic LogicHook to highlight overdue dates in List View. :cry:

I followed the instructions here and that worked very nicely.

Then I hoped it would be a simple case of;
if($bean->due_date < $now) {
//highlight red
}

However, $bean->due_date is in the format d/m/Y - as per my user settings - and I know that this will change for another user so I can’t rely on this being consistent.

I think the closest I got was using:
$due_date_time = new DateTime();
$due_date_time = $timedate->fromUser($bean->due_date, $current_user);

But then I get the error:
[ERROR] fromUser: Conversion of 20/09/2021 from user format d/m/Y H:i failed

Any ideas on how I can proceed?

Maybe I can go and get the date from the database instead of relying on $bean->due_date, but this is where my skills ran out.

Many thanks in advance.
Will share final solution here for others to use once I’ve managed it.

Mike

OK, tried my idea, but using
$db_bean = BeanFactory::getBean('AOS_Invoices', $bean->id);
Returned $db_bean->due_date in the user defined format again… :upside_down_face:

How can I convert this formatted date to a consistent datetime?

I think you are right about taking the time from DB. Check this sample for some ideas:

Thanks for the reply, I really appreciate it.

The eureka moment came from here:

After that, it was easy. Will post the finished code in a moment.

<?php
//prevents directly accessing this file from a web browser
if (!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');

class HighlightOverdueLogicHook{
    public function highlightOverdue(SugarBean $bean, $event, $arguments){
        //If due date in past then highlight it red, otherwise highlight blue
                
        $due_date = $GLOBALS['timedate']->fromString($bean->due_date);
        //LoggerManager::getLogger()->debug($due_date);
        
        global $timedate;
        $yesterday = $timedate->getNow();
        $yesterday->sub(new DateInterval('PT24H'));
        //LoggerManager::getLogger()->debug($yesterday);

        if($due_date < $yesterday) {
            $bean->due_date = "<div style='border: solid 3px red'>".$bean->due_date."</div>";
        } else {
            $bean->due_date = "<div style='border: solid 3px blue'>".$bean->due_date."</div>";
        }
    }
}

Anybody wishing to implement this code will need to read this first:

The code above replaces the second example block, with the first example block modified appropriately for the class, function and module names.

1 Like