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-2012 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->custom_fields->getJOIN();
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 ";
138 $query .= $custom_join['select'];
140 $query .= " ,opportunities.*
141 FROM opportunities ";
144 $query .= "LEFT JOIN users
145 ON opportunities.assigned_user_id=users.id ";
146 $query .= "LEFT JOIN $this->rel_account_table
147 ON opportunities.id=$this->rel_account_table.opportunity_id
149 ON $this->rel_account_table.account_id=accounts.id ";
151 $query .= $custom_join['join'];
154 if($show_deleted == 0){
156 ($this->rel_account_table.deleted is null OR $this->rel_account_table.deleted=0)
157 AND (accounts.deleted is null OR accounts.deleted=0)
158 AND opportunities.deleted=0";
159 }else if($show_deleted == 1){
160 $where_auto = " opportunities.deleted=1";
164 $query .= "where ($where) AND ".$where_auto;
166 $query .= "where ".$where_auto;
169 $query .= " ORDER BY $order_by";
171 $query .= " ORDER BY opportunities.name";
177 function create_export_query(&$order_by, &$where, $relate_link_join='')
179 $custom_join = $this->custom_fields->getJOIN(true, true,$where);
181 $custom_join['join'] .= $relate_link_join;
184 accounts.name as account_name,
185 users.user_name as assigned_user_name ";
187 $query .= $custom_join['select'];
189 $query .= " FROM opportunities ";
190 $query .= "LEFT JOIN users
191 ON opportunities.assigned_user_id=users.id";
192 $query .= " LEFT JOIN $this->rel_account_table
193 ON opportunities.id=$this->rel_account_table.opportunity_id
195 ON $this->rel_account_table.account_id=accounts.id ";
197 $query .= $custom_join['join'];
200 ($this->rel_account_table.deleted is null OR $this->rel_account_table.deleted=0)
201 AND (accounts.deleted is null OR accounts.deleted=0)
202 AND opportunities.deleted=0";
205 $query .= "where $where AND ".$where_auto;
207 $query .= "where ".$where_auto;
210 $query .= " ORDER BY opportunities.$order_by";
212 $query .= " ORDER BY opportunities.name";
216 function fill_in_additional_list_fields()
218 if ( $this->force_load_details == true)
220 $this->fill_in_additional_detail_fields();
224 function fill_in_additional_detail_fields()
226 parent::fill_in_additional_detail_fields();
228 if(!empty($this->currency_id)) {
229 $currency = new Currency();
230 $currency->retrieve($this->currency_id);
231 if($currency->id != $this->currency_id || $currency->deleted == 1){
232 $this->amount = $this->amount_usdollar;
233 $this->currency_id = $currency->id;
237 if(!empty($this->campaign_id)) {
238 $camp = new Campaign();
239 $camp->retrieve($this->campaign_id);
240 $this->campaign_name = $camp->name;
242 $this->account_name = '';
243 $this->account_id = '';
244 if(!empty($this->id)) {
245 $ret_values=Opportunity::get_account_detail($this->id);
246 if (!empty($ret_values)) {
247 $this->account_name=$ret_values['name'];
248 $this->account_id=$ret_values['id'];
249 $this->account_id_owner =$ret_values['assigned_user_id'];
254 /** Returns a list of the associated contacts
255 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
256 * All Rights Reserved..
257 * Contributor(s): ______________________________________..
259 function get_contacts()
261 $this->load_relationship('contacts');
262 $query_array=$this->contacts->getQuery(true);
264 //update the select clause in the retruned query.
265 $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 ";
268 foreach ($query_array as $qstring) {
269 $query.=' '.$qstring;
271 $temp = Array('id', 'first_name', 'last_name', 'title', 'email1', 'phone_work', 'opportunity_role', 'opportunity_rel_id');
272 return $this->build_related_list2($query, new Contact(), $temp);
275 function update_currency_id($fromid, $toid){
278 $currency = new Currency();
279 $currency->retrieve($toid);
280 foreach($fromid as $f){
281 if(!empty($idequals)){
284 $idequals .= "currency_id='$f'";
287 if(!empty($idequals)){
288 $query = "select amount, id from opportunities where (". $idequals. ") and deleted=0 and opportunities.sales_stage <> 'Closed Won' AND opportunities.sales_stage <> 'Closed Lost';";
289 $result = $this->db->query($query);
290 while($row = $this->db->fetchByAssoc($result)){
292 $query = "update opportunities set currency_id='".$currency->id."', amount_usdollar='".$currency->convertToDollar($row['amount'])."' where id='".$row['id']."';";
293 $this->db->query($query);
300 function get_list_view_data(){
301 global $locale, $current_language, $current_user, $mod_strings, $app_list_strings, $sugar_config;
302 $app_strings = return_application_language($current_language);
305 $temp_array = $this->get_list_view_array();
306 $temp_array['SALES_STAGE'] = empty($temp_array['SALES_STAGE']) ? '' : $temp_array['SALES_STAGE'];
307 $temp_array["ENCODED_NAME"]=$this->name;
311 function get_currency_symbol(){
312 if(isset($this->currency_id)){
313 $cur_qry = "select * from currencies where id ='".$this->currency_id."'";
314 $cur_res = $this->db->query($cur_qry);
315 if(!empty($cur_res)){
316 $cur_row = $this->db->fetchByAssoc($cur_res);
317 if(isset($cur_row['symbol'])){
318 return $cur_row['symbol'];
327 builds a generic search based on the query string using or
328 do not include any $this-> because this is called on without having the class instantiated
330 function build_generic_where_clause ($the_query_string) {
331 $where_clauses = Array();
332 $the_query_string = $GLOBALS['db']->quote($the_query_string);
333 array_push($where_clauses, "opportunities.name like '$the_query_string%'");
334 array_push($where_clauses, "accounts.name like '$the_query_string%'");
337 foreach($where_clauses as $clause)
339 if($the_where != "") $the_where .= " or ";
340 $the_where .= $clause;
347 function save($check_notify = FALSE)
349 // Bug 32581 - Make sure the currency_id is set to something
350 global $current_user, $app_list_strings;
352 if ( empty($this->currency_id) )
353 $this->currency_id = $current_user->getPreference('currency');
354 if ( empty($this->currency_id) )
355 $this->currency_id = -99;
357 //if probablity isn't set, set it based on the sales stage
358 if (!isset($this->probability) && !empty($this->sales_stage))
360 $prob_arr = $app_list_strings['sales_probability_dom'];
361 if (isset($prob_arr[$this->sales_stage]))
362 $this->probability = $prob_arr[$this->sales_stage];
365 require_once('modules/Opportunities/SaveOverload.php');
368 return parent::save($check_notify);
372 function save_relationship_changes($is_update)
374 //if account_id was replaced unlink the previous account_id.
375 //this rel_fields_before_value is populated by sugarbean during the retrieve call.
376 if (!empty($this->account_id) and !empty($this->rel_fields_before_value['account_id']) and
377 (trim($this->account_id) != trim($this->rel_fields_before_value['account_id']))) {
378 //unlink the old record.
379 $this->load_relationship('accounts');
380 $this->accounts->delete($this->id,$this->rel_fields_before_value['account_id']);
382 // Bug 38529 & 40938 - exclude currency_id
383 parent::save_relationship_changes($is_update, array('currency_id'));
385 if (!empty($this->contact_id)) {
386 $this->set_opportunity_contact_relationship($this->contact_id);
390 function set_opportunity_contact_relationship($contact_id)
392 global $app_list_strings;
393 $default = $app_list_strings['opportunity_relationship_type_default_key'];
394 $this->load_relationship('contacts');
395 $this->contacts->add($contact_id,array('contact_role'=>$default));
398 function set_notification_body($xtpl, $oppty)
400 global $app_list_strings;
402 $xtpl->assign("OPPORTUNITY_NAME", $oppty->name);
403 $xtpl->assign("OPPORTUNITY_AMOUNT", $oppty->amount);
404 $xtpl->assign("OPPORTUNITY_CLOSEDATE", $oppty->date_closed);
405 $xtpl->assign("OPPORTUNITY_STAGE", (isset($oppty->sales_stage)?$app_list_strings['sales_stage_dom'][$oppty->sales_stage]:""));
406 $xtpl->assign("OPPORTUNITY_DESCRIPTION", $oppty->description);
411 function bean_implements($interface){
413 case 'ACL':return true;
417 function listviewACLHelper(){
418 $array_assign = parent::listviewACLHelper();
420 if(!empty($this->account_id)){
422 if(!empty($this->account_id_owner)){
423 global $current_user;
424 $is_owner = $current_user->id == $this->account_id_owner;
427 if(!ACLController::moduleSupportsACL('Accounts') || ACLController::checkAccess('Accounts', 'view', $is_owner)){
428 $array_assign['ACCOUNT'] = 'a';
430 $array_assign['ACCOUNT'] = 'span';
433 return $array_assign;
437 * Static helper function for getting releated account info.
439 function get_account_detail($opp_id) {
440 $ret_array = array();
441 $db = DBManagerFactory::getInstance();
442 $query = "SELECT acc.id, acc.name, acc.assigned_user_id "
443 . "FROM accounts acc, accounts_opportunities a_o "
444 . "WHERE acc.id=a_o.account_id"
445 . " AND a_o.opportunity_id='$opp_id'"
446 . " AND a_o.deleted=0"
447 . " AND acc.deleted=0";
448 $result = $db->query($query, true,"Error filling in opportunity account details: ");
449 $row = $db->fetchByAssoc($result);
451 $ret_array['name'] = $row['name'];
452 $ret_array['id'] = $row['id'];
453 $ret_array['assigned_user_id'] = $row['assigned_user_id'];
458 function getCurrencyType(){