Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Sugar Logic function for retrieving YTD value
<?php
/* File: ./custom/include/Expressions/Expression/String/GetYTDValueExpression.php */
require_once('include/Expressions/Expression/String/StringExpression.php');
class GetYTDValueExpression extends StringExpression
{
function evaluate() {
$account_id = $this->context->id;
$q = "select round(sum(amount)) as ytdvalue from opportunities o ";
$q .= "join accounts_opportunities ao ";
$q .= "on o.id = ao.opportunity_id ";
$q .= "and ao.account_id = '{$account_id}' ";
$q .= "and ao.deleted = 0 ";
$q .= "and o.deleted = 0 ";
$q .= "and o.sales_stage = 'Closed Won' ";
$q .= "and year(o.date_entered) = year(curdate()) ";
$results = $this->context->db->query($q, true);
$row = $this->context->db->fetchByAssoc($results);
$ytdvalue = $row['ytdvalue'];
return $ytdvalue;
}
static function getJSEvaluate() {
return <<<EOQ
var s = '';
return s;
EOQ;
}
static function getOperationName() {
return "GetYTDValue";
}
static function getParamCount() {
return 0;
}
}
?>
@mmarum-sugarcrm
Copy link

mmarum-sugarcrm commented Feb 1, 2018

Any way to update this to use SugarQuery API?

@elchele
Copy link
Author

elchele commented Feb 2, 2018

My original thought was to use SugarQuery for it but then shied away from it as I still need the RAW on the SELECT to use SUM(). I'll revisit over the next couple of days and advise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment