Пример #1
0
 /**
  * Retrieve a user's quota using the rollup value, if available.  This method is useful for
  * fetching user quota data when you're unsure about whether or not the given user is a manager.
  * If you would like to force a direct quota, pass a false value to $should_rollup.
  *
  * @param $timeperiod_id String id of the TimePeriod to retrieve quota for
  * @param $user_id String value of the user id to retrieve.  If NULL, the $current_user is used
  * @param $should_rollup boolean value indicating whether or not the quota should be a rollup calculation; false by default
  *
  * @return array [currency_id => int, amount => number, formatted_amount => String]
  */
 public function getRollupQuota($timeperiod_id, $user_id = null, $should_rollup = false)
 {
     if (is_null($user_id)) {
         global $current_user;
         $user_id = $current_user->id;
     }
     // figure out the timeperiod
     // if we didn't find a time period, set the time period to be the current time period
     if (!is_guid($timeperiod_id) && is_numeric($timeperiod_id) && $timeperiod_id != 0) {
         // we have a timestamp, find timeperiod it belongs in
         $timeperiod_id = TimePeriod::getIdFromTimestamp($timeperiod_id);
     }
     if (!is_guid($timeperiod_id)) {
         $timeperiod_id = TimePeriod::getCurrentId();
     }
     $sq = new SugarQuery();
     $sq->select(array('quotas.currency_id', 'quotas.amount'));
     $sq->from(BeanFactory::getBean('Quotas'));
     $sq->where()->equals('user_id', $user_id)->equals('quota_type', $should_rollup ? 'Rollup' : 'Direct')->equals('timeperiod_id', $timeperiod_id);
     $sq->orderBy('date_modified', 'DESC');
     $sq->limit(1);
     // since there is only ever one row, just shift the value off the results
     $row = array_shift($sq->execute());
     if (empty($row)) {
         // This is to prevent return value of false when a given timeperiod has no quota.
         $row = array('currency_id' => -99, 'amount' => 0);
     }
     $row['formatted_amount'] = SugarCurrency::formatAmountUserLocale($row['amount'], $row['currency_id']);
     return $row;
 }
 /**
  * Utility Method to create the filter for the filer API to use
  *
  * @param ServiceBase $api                  Service Api Class
  * @param mixed $user_id                    Passed in User ID, if false, it will use the current use from $api->user
  * @param mixed $timeperiod_id              TimePeriod Id, if false, the current time period will be found an used
  * @return array                            The Filer array to be passed back into the filerList Api
  * @throws SugarApiExceptionNotAuthorized
  * @throws SugarApiExceptionInvalidParameter
  */
 protected function createFilter(ServiceBase $api, $user_id, $timeperiod_id)
 {
     // we need to check if the $api->user is a manager
     // if they are not a manager, throw back a 403 (Not Authorized) error
     if (!User::isManager($api->user->id)) {
         throw new SugarApiExceptionNotAuthorized();
     }
     $filter = array();
     // default draft to be 1
     $draft = 1;
     // if we did not find a user in the filters array, set it to the current user's id
     if ($user_id == false) {
         // use the current user, since on one was passed in
         $user_id = $api->user->id;
     } else {
         // make sure that the passed in user is a valid user
         /* @var $user User */
         // we use retrieveBean so it will return NULL and not an empty bean if the $args['user_id'] is invalid
         $user = BeanFactory::retrieveBean('Users', $user_id);
         if (is_null($user)) {
             throw new SugarApiExceptionInvalidParameter('Provided User is not valid');
         }
         // we found a user, so check to make sure that if it's not the current user, they only see committed data
         $draft = $user_id == $api->user->id ? 1 : 0;
     }
     // todo-sfa: Make sure that the passed in user can be viewed by the $api->user, need to check reportee tree
     // set the assigned_user_id
     array_push($filter, array('assigned_user_id' => $user_id));
     // set the draft flag depending on the assigned_user_id that is set from above
     array_push($filter, array('draft' => $draft));
     // if we didn't find a time period, set the time period to be the current time period
     if (!is_guid($timeperiod_id) && is_numeric($timeperiod_id) && $timeperiod_id != 0) {
         // we have a timestamp, find timeperiod it belongs in
         $timeperiod_id = TimePeriod::getIdFromTimestamp($timeperiod_id);
     }
     if (!is_guid($timeperiod_id)) {
         $timeperiod_id = TimePeriod::getCurrentId();
     }
     // fix up the timeperiod filter
     /* @var $tp TimePeriod */
     // we use retrieveBean so it will return NULL and not an empty bean if the $args['timeperiod_id'] is invalid
     $tp = BeanFactory::retrieveBean('TimePeriods', $timeperiod_id);
     if (is_null($tp)) {
         throw new SugarApiExceptionInvalidParameter('Provided TimePeriod is not valid');
     }
     array_push($filter, array('timeperiod_id' => $tp->id));
     return $filter;
 }
Пример #3
0
 /**
  * @return TimePeriod
  */
 public function getTimeperiod()
 {
     $config = $this->getForecastConfig();
     $type = $config['timeperiod_leaf_interval'];
     $id = $this->getArg('timeperiod_id');
     if (!is_guid($id) && is_numeric($id)) {
         $id = TimePeriod::getIdFromTimestamp($id, $type);
     }
     return TimePeriod::getByType($type, $id);
 }
 /**
  * Utility Method to create the filter for the filer API to use
  *
  * @param ServiceBase $api                  Service Api Class
  * @param mixed $user_id                    Passed in User ID, if false, it will use the current use from $api->user
  * @param mixed $timeperiod_id              TimePeriod Id, if false, the current time period will be found an used
  * @param string $parent_type               Type of worksheet to return, defaults to 'opportunities', but can be 'products'
  * @return array                            The Filer array to be passed back into the filerList Api
  * @throws SugarApiExceptionNotAuthorized
  * @throws SugarApiExceptionInvalidParameter
  */
 protected function createFilter(ServiceBase $api, $user_id, $timeperiod_id, $parent_type = 'Opportunities')
 {
     $filter = array();
     // default draft to be 1
     $draft = 1;
     // if we did not find a user in the filters array, set it to the current user's id
     if ($user_id == false) {
         // use the current user, since on one was passed in
         $user_id = $api->user->id;
     } else {
         // make sure that the passed in user is a valid user
         /* @var $user User */
         // we use retrieveBean so it will return NULL and not an empty bean if the $args['user_id'] is invalid
         $user = BeanFactory::retrieveBean('Users', $user_id);
         if (is_null($user)) {
             throw new SugarApiExceptionInvalidParameter('Provided User is not valid');
         }
         // we found a user, so check to make sure that if it's not the current user, they only see committed data
         $draft = $user_id == $api->user->id ? 1 : 0;
     }
     // so we have a valid user, and it's not the $api->user, we need to check if the $api->user is a manager
     // if they are not a manager, throw back a 403 (Not Authorized) error
     if ($draft == 0 && !User::isManager($api->user->id)) {
         throw new SugarApiExceptionNotAuthorized();
     }
     // todo-sfa: Make sure that the passed in user can be viewed by the $api->user, need to check reportee tree
     // set the assigned_user_id
     array_push($filter, array('assigned_user_id' => $user_id));
     // set the draft flag depending on the assigned_user_id that is set from above
     array_push($filter, array('draft' => $draft));
     // if we didn't find a time period, set the time period to be the current time period
     if (!is_guid($timeperiod_id) && is_numeric($timeperiod_id) && $timeperiod_id != 0) {
         // we have a timestamp, find timeperiod it belongs in
         $timeperiod_id = TimePeriod::getIdFromTimestamp($timeperiod_id);
     }
     if (!is_guid($timeperiod_id)) {
         $timeperiod_id = TimePeriod::getCurrentId();
     }
     // fix up the timeperiod filter
     /* @var $tp TimePeriod */
     // we use retrieveBean so it will return NULL and not an empty bean if the $args['timeperiod_id'] is invalid
     $tp = BeanFactory::retrieveBean('TimePeriods', $timeperiod_id);
     if (is_null($tp)) {
         throw new SugarApiExceptionInvalidParameter('Provided TimePeriod is not valid');
     }
     array_push($filter, array('$and' => array(array('date_closed_timestamp' => array('$gte' => $tp->start_date_timestamp)), array('date_closed_timestamp' => array('$lte' => $tp->end_date_timestamp)))));
     if (empty($parent_type)) {
         // get the forecast_by setting
         /* @var $admin Administration */
         $admin = BeanFactory::getBean('Administration');
         $settings = $admin->getConfigForModule('Forecasts', $api->platform);
         $parent_type = $settings['forecast_by'];
     }
     // we only want to view parent_types of 'Opportunities' here
     array_push($filter, array('parent_type' => $parent_type));
     return $filter;
 }