2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 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',
107 function Opportunity() {
109 global $sugar_config;
110 if(!$sugar_config['require_accounts']){
111 unset($this->required_fields['account_name']);
115 var $new_schema = true;
119 function get_summary_text()
121 return "$this->name";
124 function create_list_query($order_by, $where, $show_deleted = 0)
127 $custom_join = $this->custom_fields->getJOIN();
131 accounts.id as account_id,
132 accounts.name as account_name,
133 accounts.assigned_user_id account_id_owner,
134 users.user_name as assigned_user_name ";
136 $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 ";
149 $query .= $custom_join['join'];
152 if($show_deleted == 0){
154 ($this->rel_account_table.deleted is null OR $this->rel_account_table.deleted=0)
155 AND (accounts.deleted is null OR accounts.deleted=0)
156 AND opportunities.deleted=0";
157 }else if($show_deleted == 1){
158 $where_auto = " opportunities.deleted=1";
162 $query .= "where ($where) AND ".$where_auto;
164 $query .= "where ".$where_auto;
167 $query .= " ORDER BY $order_by";
169 $query .= " ORDER BY opportunities.name";
175 function create_export_query(&$order_by, &$where, $relate_link_join='')
177 $custom_join = $this->custom_fields->getJOIN(true, true,$where);
179 $custom_join['join'] .= $relate_link_join;
182 accounts.name as account_name,
183 users.user_name as assigned_user_name ";
185 $query .= $custom_join['select'];
187 $query .= " FROM opportunities ";
188 $query .= "LEFT JOIN users
189 ON opportunities.assigned_user_id=users.id";
190 $query .= " LEFT JOIN $this->rel_account_table
191 ON opportunities.id=$this->rel_account_table.opportunity_id
193 ON $this->rel_account_table.account_id=accounts.id ";
195 $query .= $custom_join['join'];
198 ($this->rel_account_table.deleted is null OR $this->rel_account_table.deleted=0)
199 AND (accounts.deleted is null OR accounts.deleted=0)
200 AND opportunities.deleted=0";
203 $query .= "where $where AND ".$where_auto;
205 $query .= "where ".$where_auto;
208 $query .= " ORDER BY opportunities.$order_by";
210 $query .= " ORDER BY opportunities.name";
214 function fill_in_additional_list_fields()
216 if ( $this->force_load_details == true)
218 $this->fill_in_additional_detail_fields();
222 function fill_in_additional_detail_fields()
224 parent::fill_in_additional_detail_fields();
226 $currency = new Currency();
227 $currency->retrieve($this->currency_id);
228 if($currency->id != $this->currency_id || $currency->deleted == 1){
229 $this->amount = $this->amount_usdollar;
230 $this->currency_id = $currency->id;
233 $camp = new Campaign();
234 $camp->retrieve($this->campaign_id);
235 $this->campaign_name = $camp->name;
236 $this->account_name = '';
237 $this->account_id = '';
238 $ret_values=Opportunity::get_account_detail($this->id);
239 if (!empty($ret_values)) {
240 $this->account_name=$ret_values['name'];
241 $this->account_id=$ret_values['id'];
242 $this->account_id_owner =$ret_values['assigned_user_id'];
247 /** Returns a list of the associated contacts
248 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
249 * All Rights Reserved..
250 * Contributor(s): ______________________________________..
252 function get_contacts()
254 $this->load_relationship('contacts');
255 $query_array=$this->contacts->getQuery(true);
257 //update the select clause in the retruned query.
258 $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 ";
261 foreach ($query_array as $qstring) {
262 $query.=' '.$qstring;
264 $temp = Array('id', 'first_name', 'last_name', 'title', 'email1', 'phone_work', 'opportunity_role', 'opportunity_rel_id');
265 return $this->build_related_list2($query, new Contact(), $temp);
268 function update_currency_id($fromid, $toid){
271 $currency = new Currency();
272 $currency->retrieve($toid);
273 foreach($fromid as $f){
274 if(!empty($idequals)){
277 $idequals .= "currency_id='$f'";
280 if(!empty($idequals)){
281 $query = "select amount, id from opportunities where (". $idequals. ") and deleted=0 and opportunities.sales_stage <> 'Closed Won' AND opportunities.sales_stage <> 'Closed Lost';";
282 $result = $this->db->query($query);
283 while($row = $this->db->fetchByAssoc($result)){
285 $query = "update opportunities set currency_id='".$currency->id."', amount_usdollar='".$currency->convertToDollar($row['amount'])."' where id='".$row['id']."';";
286 $this->db->query($query);
293 function get_list_view_data(){
294 global $locale, $current_language, $current_user, $mod_strings, $app_list_strings, $sugar_config;
295 $app_strings = return_application_language($current_language);
298 $temp_array = $this->get_list_view_array();
299 $temp_array['SALES_STAGE'] = empty($temp_array['SALES_STAGE']) ? '' : $temp_array['SALES_STAGE'];
300 $temp_array["ENCODED_NAME"]=$this->name;
304 function get_currency_symbol(){
305 if(isset($this->currency_id)){
306 $cur_qry = "select * from currencies where id ='".$this->currency_id."'";
307 $cur_res = $this->db->query($cur_qry);
308 if(!empty($cur_res)){
309 $cur_row = $this->db->fetchByAssoc($cur_res);
310 if(isset($cur_row['symbol'])){
311 return $cur_row['symbol'];
320 builds a generic search based on the query string using or
321 do not include any $this-> because this is called on without having the class instantiated
323 function build_generic_where_clause ($the_query_string) {
324 $where_clauses = Array();
325 $the_query_string = $GLOBALS['db']->quote($the_query_string);
326 array_push($where_clauses, "opportunities.name like '$the_query_string%'");
327 array_push($where_clauses, "accounts.name like '$the_query_string%'");
330 foreach($where_clauses as $clause)
332 if($the_where != "") $the_where .= " or ";
333 $the_where .= $clause;
340 function save($check_notify = FALSE)
342 // Bug 32581 - Make sure the currency_id is set to something
343 global $current_user, $app_list_strings;
345 if ( empty($this->currency_id) )
346 $this->currency_id = $current_user->getPreference('currency');
347 if ( empty($this->currency_id) )
348 $this->currency_id = -99;
350 //if probablity isn't set, set it based on the sales stage
351 if (!isset($this->probability) && !empty($this->sales_stage))
353 $prob_arr = $app_list_strings['sales_probability_dom'];
354 if (isset($prob_arr[$this->sales_stage]))
355 $this->probability = $prob_arr[$this->sales_stage];
358 require_once('modules/Opportunities/SaveOverload.php');
361 return parent::save($check_notify);
365 function save_relationship_changes($is_update)
367 //if account_id was replaced unlink the previous account_id.
368 //this rel_fields_before_value is populated by sugarbean during the retrieve call.
369 if (!empty($this->account_id) and !empty($this->rel_fields_before_value['account_id']) and
370 (trim($this->account_id) != trim($this->rel_fields_before_value['account_id']))) {
371 //unlink the old record.
372 $this->load_relationship('accounts');
373 $this->accounts->delete($this->id,$this->rel_fields_before_value['account_id']);
376 parent::save_relationship_changes($is_update);
378 if (!empty($this->contact_id)) {
379 $this->set_opportunity_contact_relationship($this->contact_id);
383 function set_opportunity_contact_relationship($contact_id)
385 global $app_list_strings;
386 $default = $app_list_strings['opportunity_relationship_type_default_key'];
387 $this->load_relationship('contacts');
388 $this->contacts->add($contact_id,array('contact_role'=>$default));
391 function set_notification_body($xtpl, $oppty)
393 global $app_list_strings;
395 $xtpl->assign("OPPORTUNITY_NAME", $oppty->name);
396 $xtpl->assign("OPPORTUNITY_AMOUNT", $oppty->amount);
397 $xtpl->assign("OPPORTUNITY_CLOSEDATE", $oppty->date_closed);
398 $xtpl->assign("OPPORTUNITY_STAGE", (isset($oppty->sales_stage)?$app_list_strings['sales_stage_dom'][$oppty->sales_stage]:""));
399 $xtpl->assign("OPPORTUNITY_DESCRIPTION", $oppty->description);
404 function bean_implements($interface){
406 case 'ACL':return true;
410 function listviewACLHelper(){
411 $array_assign = parent::listviewACLHelper();
413 if(!empty($this->account_id)){
415 if(!empty($this->account_id_owner)){
416 global $current_user;
417 $is_owner = $current_user->id == $this->account_id_owner;
420 if(!ACLController::moduleSupportsACL('Accounts') || ACLController::checkAccess('Accounts', 'view', $is_owner)){
421 $array_assign['ACCOUNT'] = 'a';
423 $array_assign['ACCOUNT'] = 'span';
426 return $array_assign;
430 * Static helper function for getting releated account info.
432 function get_account_detail($opp_id) {
433 $ret_array = array();
434 $db = DBManagerFactory::getInstance();
435 $query = "SELECT acc.id, acc.name, acc.assigned_user_id "
436 . "FROM accounts acc, accounts_opportunities a_o "
437 . "WHERE acc.id=a_o.account_id"
438 . " AND a_o.opportunity_id='$opp_id'"
439 . " AND a_o.deleted=0"
440 . " AND acc.deleted=0";
441 $result = $db->query($query, true,"Error filling in opportunity account details: ");
442 $row = $db->fetchByAssoc($result);
444 $ret_array['name'] = $row['name'];
445 $ret_array['id'] = $row['id'];
446 $ret_array['assigned_user_id'] = $row['assigned_user_id'];
451 function getCurrencyType(){