/**
  * doCustomUpdateRate
  *
  * Return true to skip updates for this module.
  * Return false to do default update of base_rate column.
  * To custom processing, do here and return true.
  *
  * @access public
  * @param  string $table
  * @param  string $column
  * @param  string $currencyId
  * @return boolean true if custom processing was done
  */
 public function doCustomUpdateRate($table, $column, $currencyId)
 {
     // get the conversion rate
     $sq = new SugarQuery();
     $sq->select(array('conversion_rate'));
     $sq->from(BeanFactory::getBean('Currencies'));
     $sq->where()->equals('id', $currencyId);
     $rate = $sq->getOne();
     $ids = $this->getProductsWithNonClosedQuote();
     // setup SQL statement
     $query = sprintf("UPDATE %s SET %s = %s\n        WHERE id IN (%s)\n        AND currency_id = %s", $table, $column, $this->db->quote($rate), implode(",", $ids), $this->db->quoted($currencyId));
     // execute
     $result = $this->db->query($query, true, string_format($GLOBALS['app_strings']['ERR_DB_QUERY'], array(__CLASS__, $query)));
     return !empty($result);
 }
Esempio n. 2
0
 /**
  * Gets timeperiod based on date and type
  *
  * @param string Database formatted date
  * @param string Timeperiod type
  * @return Timeperiod|bool Timeperiod if found, false if not
  */
 protected static function getByDateAndType($date, $type = "")
 {
     global $app_strings;
     $db = DBManagerFactory::getInstance();
     if ($date instanceof SugarDateTime) {
         $date = $date->asDbDate(false);
     }
     $timestamp = strtotime($date);
     $retVal = false;
     if (empty($type)) {
         $admin = BeanFactory::getBean("Administration");
         $config = $admin->getConfigForModule("Forecasts", "base");
         $type = $config["timeperiod_leaf_interval"];
     }
     $sq = new SugarQuery();
     $sq->select(array('id'));
     $sq->from(BeanFactory::getBean('TimePeriods'))->where()->equals('type', $type)->lte('start_date_timestamp', $timestamp)->gte('end_date_timestamp', $timestamp);
     $timeperiod_id = $sq->getOne();
     if (!empty($timeperiod_id)) {
         $tp = BeanFactory::getBean('TimePeriods');
         $tp->retrieve($timeperiod_id);
         $retVal = $tp;
     }
     return $retVal;
 }
Esempio n. 3
0
 function handleCreateCase($email, $userId)
 {
     global $current_user, $mod_strings, $current_language;
     $mod_strings = return_module_language($current_language, "Emails");
     $GLOBALS['log']->debug('In handleCreateCase');
     $c = BeanFactory::getBean('Cases');
     $this->getCaseIdFromCaseNumber($email->name, $c);
     if (!$this->handleCaseAssignment($email) && $this->isMailBoxTypeCreateCase()) {
         // create a case
         $GLOBALS['log']->debug('retrieveing email');
         $email->retrieve($email->id);
         $c = BeanFactory::getBean('Cases');
         $c->description = $email->description;
         if (empty($c->description) && !empty($email->description_html)) {
             $c->description = $email->description_html;
         }
         $c->assigned_user_id = $userId;
         $c->name = $email->name;
         $c->status = 'New';
         $c->priority = 'P1';
         $c->team_id = $_REQUEST['team_id'];
         $c->team_set_id = $_REQUEST['team_set_id'];
         if (!empty($email->reply_to_email)) {
             $contactAddr = $email->reply_to_email;
         } else {
             $contactAddr = $email->from_addr;
         }
         $GLOBALS['log']->debug('finding related accounts with address ' . $contactAddr);
         if ($accountIds = $this->getRelatedId($contactAddr, 'accounts')) {
             if (sizeof($accountIds) == 1) {
                 $c->account_id = $accountIds[0];
                 $acct = BeanFactory::getBean('Accounts', $c->account_id);
                 $c->account_name = $acct->name;
             }
             // if
         }
         // if
         $c->save(true);
         $caseId = $c->id;
         $c = BeanFactory::getBean('Cases', $caseId);
         if (empty($c->case_number)) {
             $sq = new SugarQuery();
             $sq->select(array('case_number'));
             $sq->from(BeanFactory::getBean('Cases'))->where()->equals('id', $c->id);
             $c->case_number = $sq->getOne();
         }
         if ($c->load_relationship('emails')) {
             $c->emails->add($email->id);
         }
         // if
         if ($contactIds = $this->getRelatedId($contactAddr, 'contacts')) {
             if (!empty($contactIds) && $c->load_relationship('contacts')) {
                 if (!$accountIds && count($contactIds) == 1) {
                     $contact = BeanFactory::getBean('Contacts', $contactIds[0]);
                     if ($contact->load_relationship('accounts')) {
                         $acct = $contact->accounts->get();
                         if ($c->load_relationship('accounts') && !empty($acct[0])) {
                             $c->accounts->add($acct[0]);
                         }
                     }
                 }
                 $c->contacts->add($contactIds);
             }
             // if
         }
         // if
         $c->email_id = $email->id;
         $email->parent_type = "Cases";
         $email->parent_id = $caseId;
         // assign the email to the case owner
         $email->assigned_user_id = $c->assigned_user_id;
         $email->name = str_replace('%1', $c->case_number, $c->getEmailSubjectMacro()) . " " . $email->name;
         $email->save();
         $GLOBALS['log']->debug('InboundEmail created one case with number: ' . $c->case_number);
         $createCaseTemplateId = $this->get_stored_options('create_case_email_template', "");
         if (!empty($this->stored_options)) {
             $storedOptions = unserialize(base64_decode($this->stored_options));
         }
         if (!empty($createCaseTemplateId)) {
             $fromName = "";
             $fromAddress = "";
             if (!empty($this->stored_options)) {
                 $fromAddress = $storedOptions['from_addr'];
                 $fromName = from_html($storedOptions['from_name']);
                 $replyToName = !empty($storedOptions['reply_to_name']) ? from_html($storedOptions['reply_to_name']) : $fromName;
                 $replyToAddr = !empty($storedOptions['reply_to_addr']) ? $storedOptions['reply_to_addr'] : $fromAddress;
             }
             // if
             $defaults = $current_user->getPreferredEmail();
             $fromAddress = !empty($fromAddress) ? $fromAddress : $defaults['email'];
             $fromName = !empty($fromName) ? $fromName : $defaults['name'];
             $to[0]['email'] = $contactAddr;
             // handle to name: address, prefer reply-to
             if (!empty($email->reply_to_name)) {
                 $to[0]['display'] = $email->reply_to_name;
             } elseif (!empty($email->from_name)) {
                 $to[0]['display'] = $email->from_name;
             }
             $et = BeanFactory::getBean('EmailTemplates', $createCaseTemplateId);
             if (empty($et->subject)) {
                 $et->subject = '';
             }
             if (empty($et->body)) {
                 $et->body = '';
             }
             if (empty($et->body_html)) {
                 $et->body_html = '';
             }
             $et->subject = "Re:" . " " . str_replace('%1', $c->case_number, $c->getEmailSubjectMacro() . " " . $c->name);
             $html = trim($email->description_html);
             $plain = trim($email->description);
             $email->email2init();
             $email->from_addr = $email->from_addr_name;
             $email->to_addrs = $email->to_addrs_names;
             $email->cc_addrs = $email->cc_addrs_names;
             $email->bcc_addrs = $email->bcc_addrs_names;
             $email->from_name = $email->from_addr;
             $email = $email->et->handleReplyType($email, "reply");
             $ret = $email->et->displayComposeEmail($email);
             $ret['description'] = empty($email->description_html) ? str_replace("\n", "\n<BR/>", $email->description) : $email->description_html;
             $reply = BeanFactory::getBean('Emails');
             $reply->type = 'out';
             $reply->to_addrs = $to[0]['email'];
             $reply->to_addrs_arr = $to;
             $reply->cc_addrs_arr = array();
             $reply->bcc_addrs_arr = array();
             $reply->from_name = $fromName;
             $reply->from_addr = $fromAddress;
             $reply->reply_to_name = $replyToName;
             $reply->reply_to_addr = $replyToAddr;
             $reply->name = $et->subject;
             $reply->description = $et->body . "<div><hr /></div>" . $email->description;
             if (!$et->text_only) {
                 $reply->description_html = $et->body_html . "<div><hr /></div>" . $email->description;
             }
             $GLOBALS['log']->debug('saving and sending auto-reply email');
             //$reply->save(); // don't save the actual email.
             $reply->send();
         }
         // if
     } else {
         if (!empty($email->reply_to_email)) {
             $contactAddr = $email->reply_to_email;
         } else {
             $contactAddr = $email->from_addr;
         }
         $this->handleAutoresponse($email, $contactAddr);
     }
 }
 /**
  * If the user is the top level manager, set their rollup quota value correctly
  *
  * @param string $user_id
  * @param string $timeperiod_id
  * @throws SugarQueryException
  */
 protected function fixTopLevelManagerQuotaRollup($user_id, $timeperiod_id)
 {
     if (User::isTopLevelManager($user_id)) {
         $sq = new SugarQuery();
         $sq->select(array())->fieldRaw('sum(quota * base_rate)', 'quota');
         $sq->from($this);
         $sq->where()->equals('assigned_user_id', $user_id)->equals('timeperiod_id', $timeperiod_id)->equals('draft', 1);
         $quota = $sq->getOne();
         $this->commitQuota($quota, $user_id, $timeperiod_id, 'Rollup');
     }
 }