2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 /*********************************************************************************
41 ********************************************************************************/
55 // Opportunity is used to store customer information.
56 class Opportunity extends SugarBean {
63 var $modified_user_id;
64 var $assigned_user_id;
67 var $modified_by_name;
70 var $opportunity_type;
89 var $assigned_user_name;
91 var $table_name = "opportunities";
92 var $rel_account_table = "accounts_opportunities";
93 var $rel_contact_table = "opportunities_contacts";
94 var $module_dir = "Opportunities";
96 var $importable = true;
97 var $object_name = "Opportunity";
99 // This is used to retrieve related fields from form posts.
100 var $additional_column_fields = Array('assigned_user_name', 'assigned_user_id', 'account_name', 'account_id', 'contact_id', 'task_id', 'note_id', 'meeting_id', 'call_id', 'email_id'
103 var $relationship_fields = Array('task_id'=>'tasks', 'note_id'=>'notes', 'account_id'=>'accounts',
104 'meeting_id'=>'meetings', 'call_id'=>'calls', 'email_id'=>'emails', 'project_id'=>'project',
106 'currency_id' => 'currencies',
109 function Opportunity() {
111 global $sugar_config;
112 if(!$sugar_config['require_accounts']){
113 unset($this->required_fields['account_name']);
117 var $new_schema = true;
121 function get_summary_text()
123 return "$this->name";
126 function create_list_query($order_by, $where, $show_deleted = 0)
129 $custom_join = $this->getCustomJoin();
133 accounts.id as account_id,
134 accounts.name as account_name,
135 accounts.assigned_user_id account_id_owner,
136 users.user_name as assigned_user_name ";
137 $query .= $custom_join['select'];
138 $query .= " ,opportunities.*
139 FROM opportunities ";
142 $query .= "LEFT JOIN users
143 ON opportunities.assigned_user_id=users.id ";
144 $query .= "LEFT JOIN $this->rel_account_table
145 ON opportunities.id=$this->rel_account_table.opportunity_id
147 ON $this->rel_account_table.account_id=accounts.id ";
148 $query .= $custom_join['join'];
150 if($show_deleted == 0){
152 ($this->rel_account_table.deleted is null OR $this->rel_account_table.deleted=0)
153 AND (accounts.deleted is null OR accounts.deleted=0)
154 AND opportunities.deleted=0";
155 }else if($show_deleted == 1){
156 $where_auto = " opportunities.deleted=1";
160 $query .= "where ($where) AND ".$where_auto;
162 $query .= "where ".$where_auto;
165 $query .= " ORDER BY $order_by";
167 $query .= " ORDER BY opportunities.name";
173 function create_export_query(&$order_by, &$where, $relate_link_join='')
175 $custom_join = $this->getCustomJoin(true, true, $where);
176 $custom_join['join'] .= $relate_link_join;
179 accounts.name as account_name,
180 users.user_name as assigned_user_name ";
181 $query .= $custom_join['select'];
182 $query .= " FROM opportunities ";
183 $query .= "LEFT JOIN users
184 ON opportunities.assigned_user_id=users.id";
185 $query .= " LEFT JOIN $this->rel_account_table
186 ON opportunities.id=$this->rel_account_table.opportunity_id
188 ON $this->rel_account_table.account_id=accounts.id ";
189 $query .= $custom_join['join'];
191 ($this->rel_account_table.deleted is null OR $this->rel_account_table.deleted=0)
192 AND (accounts.deleted is null OR accounts.deleted=0)
193 AND opportunities.deleted=0";
196 $query .= "where $where AND ".$where_auto;
198 $query .= "where ".$where_auto;
201 $query .= " ORDER BY opportunities.$order_by";
203 $query .= " ORDER BY opportunities.name";
207 function fill_in_additional_list_fields()
209 if ( $this->force_load_details == true)
211 $this->fill_in_additional_detail_fields();
215 function fill_in_additional_detail_fields()
217 parent::fill_in_additional_detail_fields();
219 if(!empty($this->currency_id)) {
220 $currency = new Currency();
221 $currency->retrieve($this->currency_id);
222 if($currency->id != $this->currency_id || $currency->deleted == 1){
223 $this->amount = $this->amount_usdollar;
224 $this->currency_id = $currency->id;
228 if(!empty($this->campaign_id)) {
229 $camp = new Campaign();
230 $camp->retrieve($this->campaign_id);
231 $this->campaign_name = $camp->name;
233 $this->account_name = '';
234 $this->account_id = '';
235 if(!empty($this->id)) {
236 $ret_values=Opportunity::get_account_detail($this->id);
237 if (!empty($ret_values)) {
238 $this->account_name=$ret_values['name'];
239 $this->account_id=$ret_values['id'];
240 $this->account_id_owner =$ret_values['assigned_user_id'];
245 /** Returns a list of the associated contacts
246 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
247 * All Rights Reserved..
248 * Contributor(s): ______________________________________..
250 function get_contacts()
252 $this->load_relationship('contacts');
253 $query_array=$this->contacts->getQuery(true);
255 //update the select clause in the retruned query.
256 $query_array['select']="SELECT contacts.id, contacts.first_name, contacts.last_name, contacts.title, contacts.email1, contacts.phone_work, opportunities_contacts.contact_role as opportunity_role, opportunities_contacts.id as opportunity_rel_id ";
259 foreach ($query_array as $qstring) {
260 $query.=' '.$qstring;
262 $temp = Array('id', 'first_name', 'last_name', 'title', 'email1', 'phone_work', 'opportunity_role', 'opportunity_rel_id');
263 return $this->build_related_list2($query, new Contact(), $temp);
266 function update_currency_id($fromid, $toid){
269 $currency = new Currency();
270 $currency->retrieve($toid);
271 foreach($fromid as $f){
272 if(!empty($idequals)){
275 $idequals .= "currency_id='$f'";
278 if(!empty($idequals)){
279 $query = "select amount, id from opportunities where (". $idequals. ") and deleted=0 and opportunities.sales_stage <> 'Closed Won' AND opportunities.sales_stage <> 'Closed Lost';";
280 $result = $this->db->query($query);
281 while($row = $this->db->fetchByAssoc($result)){
283 $query = "update opportunities set currency_id='".$currency->id."', amount_usdollar='".$currency->convertToDollar($row['amount'])."' where id='".$row['id']."';";
284 $this->db->query($query);
291 function get_list_view_data(){
292 global $locale, $current_language, $current_user, $mod_strings, $app_list_strings, $sugar_config;
293 $app_strings = return_application_language($current_language);
296 $temp_array = $this->get_list_view_array();
297 $temp_array['SALES_STAGE'] = empty($temp_array['SALES_STAGE']) ? '' : $temp_array['SALES_STAGE'];
298 $temp_array["ENCODED_NAME"]=$this->name;
302 function get_currency_symbol(){
303 if(isset($this->currency_id)){
304 $cur_qry = "select * from currencies where id ='".$this->currency_id."'";
305 $cur_res = $this->db->query($cur_qry);
306 if(!empty($cur_res)){
307 $cur_row = $this->db->fetchByAssoc($cur_res);
308 if(isset($cur_row['symbol'])){
309 return $cur_row['symbol'];
318 builds a generic search based on the query string using or
319 do not include any $this-> because this is called on without having the class instantiated
321 function build_generic_where_clause ($the_query_string) {
322 $where_clauses = Array();
323 $the_query_string = $GLOBALS['db']->quote($the_query_string);
324 array_push($where_clauses, "opportunities.name like '$the_query_string%'");
325 array_push($where_clauses, "accounts.name like '$the_query_string%'");
328 foreach($where_clauses as $clause)
330 if($the_where != "") $the_where .= " or ";
331 $the_where .= $clause;
338 function save($check_notify = FALSE)
340 // Bug 32581 - Make sure the currency_id is set to something
341 global $current_user, $app_list_strings;
343 if ( empty($this->currency_id) )
344 $this->currency_id = $current_user->getPreference('currency');
345 if ( empty($this->currency_id) )
346 $this->currency_id = -99;
348 //if probablity isn't set, set it based on the sales stage
349 if (!isset($this->probability) && !empty($this->sales_stage))
351 $prob_arr = $app_list_strings['sales_probability_dom'];
352 if (isset($prob_arr[$this->sales_stage]))
353 $this->probability = $prob_arr[$this->sales_stage];
356 require_once('modules/Opportunities/SaveOverload.php');
359 return parent::save($check_notify);
363 function save_relationship_changes($is_update)
365 //if account_id was replaced unlink the previous account_id.
366 //this rel_fields_before_value is populated by sugarbean during the retrieve call.
367 if (!empty($this->account_id) and !empty($this->rel_fields_before_value['account_id']) and
368 (trim($this->account_id) != trim($this->rel_fields_before_value['account_id']))) {
369 //unlink the old record.
370 $this->load_relationship('accounts');
371 $this->accounts->delete($this->id,$this->rel_fields_before_value['account_id']);
373 // Bug 38529 & 40938 - exclude currency_id
374 parent::save_relationship_changes($is_update, array('currency_id'));
376 if (!empty($this->contact_id)) {
377 $this->set_opportunity_contact_relationship($this->contact_id);
381 function set_opportunity_contact_relationship($contact_id)
383 global $app_list_strings;
384 $default = $app_list_strings['opportunity_relationship_type_default_key'];
385 $this->load_relationship('contacts');
386 $this->contacts->add($contact_id,array('contact_role'=>$default));
389 function set_notification_body($xtpl, $oppty)
391 global $app_list_strings;
393 $xtpl->assign("OPPORTUNITY_NAME", $oppty->name);
394 $xtpl->assign("OPPORTUNITY_AMOUNT", $oppty->amount);
395 $xtpl->assign("OPPORTUNITY_CLOSEDATE", $oppty->date_closed);
396 $xtpl->assign("OPPORTUNITY_STAGE", (isset($oppty->sales_stage)?$app_list_strings['sales_stage_dom'][$oppty->sales_stage]:""));
397 $xtpl->assign("OPPORTUNITY_DESCRIPTION", $oppty->description);
402 function bean_implements($interface){
404 case 'ACL':return true;
408 function listviewACLHelper(){
409 $array_assign = parent::listviewACLHelper();
411 if(!empty($this->account_id)){
413 if(!empty($this->account_id_owner)){
414 global $current_user;
415 $is_owner = $current_user->id == $this->account_id_owner;
418 if(!ACLController::moduleSupportsACL('Accounts') || ACLController::checkAccess('Accounts', 'view', $is_owner)){
419 $array_assign['ACCOUNT'] = 'a';
421 $array_assign['ACCOUNT'] = 'span';
424 return $array_assign;
428 * Static helper function for getting releated account info.
430 function get_account_detail($opp_id) {
431 $ret_array = array();
432 $db = DBManagerFactory::getInstance();
433 $query = "SELECT acc.id, acc.name, acc.assigned_user_id "
434 . "FROM accounts acc, accounts_opportunities a_o "
435 . "WHERE acc.id=a_o.account_id"
436 . " AND a_o.opportunity_id='$opp_id'"
437 . " AND a_o.deleted=0"
438 . " AND acc.deleted=0";
439 $result = $db->query($query, true,"Error filling in opportunity account details: ");
440 $row = $db->fetchByAssoc($result);
442 $ret_array['name'] = $row['name'];
443 $ret_array['id'] = $row['id'];
444 $ret_array['assigned_user_id'] = $row['assigned_user_id'];
449 function getCurrencyType(){