Skip to content

Instantly share code, notes, and snippets.

@elchele
Last active February 2, 2018 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elchele/91af5f9e6be26a1234fd2079ddfe62b5 to your computer and use it in GitHub Desktop.
Save elchele/91af5f9e6be26a1234fd2079ddfe62b5 to your computer and use it in GitHub Desktop.
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;
}
}
?>
@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