]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Opportunities/Opportunity.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Opportunities / Opportunity.php
1 <?php
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.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description:
41  ********************************************************************************/
42
43
44
45
46
47
48
49
50
51
52
53
54
55 // Opportunity is used to store customer information.
56 class Opportunity extends SugarBean {
57         var $field_name_map;
58         // Stored fields
59         var $id;
60         var $lead_source;
61         var $date_entered;
62         var $date_modified;
63         var $modified_user_id;
64         var $assigned_user_id;
65         var $created_by;
66         var $created_by_name;
67         var $modified_by_name;
68         var $description;
69         var $name;
70         var $opportunity_type;
71         var $amount;
72         var $amount_usdollar;
73         var $currency_id;
74         var $date_closed;
75         var $next_step;
76         var $sales_stage;
77         var $probability;
78         var $campaign_id;
79
80         // These are related
81         var $account_name;
82         var $account_id;
83         var $contact_id;
84         var $task_id;
85         var $note_id;
86         var $meeting_id;
87         var $call_id;
88         var $email_id;
89         var $assigned_user_name;
90
91         var $table_name = "opportunities";
92         var $rel_account_table = "accounts_opportunities";
93         var $rel_contact_table = "opportunities_contacts";
94         var $module_dir = "Opportunities";
95
96         var $importable = true;
97         var $object_name = "Opportunity";
98
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'
101         );
102
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',
105                                                                         // Bug 38529 & 40938
106                                                                         'currency_id' => 'currencies',
107                                                                         );
108
109         function Opportunity() {
110                 parent::SugarBean();
111                 global $sugar_config;
112                 if(!$sugar_config['require_accounts']){
113                         unset($this->required_fields['account_name']);
114                 }
115         }
116
117         var $new_schema = true;
118
119
120
121         function get_summary_text()
122         {
123                 return "$this->name";
124         }
125
126         function create_list_query($order_by, $where, $show_deleted = 0)
127         {
128
129 $custom_join = $this->custom_fields->getJOIN();
130                 $query = "SELECT ";
131
132                 $query .= "
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                             if($custom_join){
138                                                                 $query .= $custom_join['select'];
139                                                         }
140                             $query .= " ,opportunities.*
141                             FROM opportunities ";
142
143
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
148                             LEFT JOIN accounts
149                             ON $this->rel_account_table.account_id=accounts.id ";
150                             if($custom_join){
151                                         $query .= $custom_join['join'];
152                                 }
153                 $where_auto = '1=1';
154                 if($show_deleted == 0){
155                         $where_auto = "
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";
161                 }
162
163                 if($where != "")
164                         $query .= "where ($where) AND ".$where_auto;
165                 else
166                         $query .= "where ".$where_auto;
167
168                 if($order_by != "")
169                         $query .= " ORDER BY $order_by";
170                 else
171                         $query .= " ORDER BY opportunities.name";
172
173                 return $query;
174         }
175
176
177     function create_export_query(&$order_by, &$where, $relate_link_join='')
178     {
179         $custom_join = $this->custom_fields->getJOIN(true, true,$where);
180                 if($custom_join)
181                                 $custom_join['join'] .= $relate_link_join;
182                                 $query = "SELECT
183                                 opportunities.*,
184                                 accounts.name as account_name,
185                                 users.user_name as assigned_user_name ";
186                                                                 if($custom_join){
187                                                                         $query .= $custom_join['select'];
188                                                                 }
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
194                                 LEFT JOIN accounts
195                                 ON $this->rel_account_table.account_id=accounts.id ";
196                                                                 if($custom_join){
197                                                                         $query .= $custom_join['join'];
198                                                                 }
199                 $where_auto = "
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";
203
204         if($where != "")
205                 $query .= "where $where AND ".$where_auto;
206         else
207                 $query .= "where ".$where_auto;
208
209         if($order_by != "")
210                 $query .= " ORDER BY opportunities.$order_by";
211         else
212                 $query .= " ORDER BY opportunities.name";
213         return $query;
214     }
215
216         function fill_in_additional_list_fields()
217         {
218                 if ( $this->force_load_details == true)
219                 {
220                         $this->fill_in_additional_detail_fields();
221                 }
222         }
223
224         function fill_in_additional_detail_fields()
225         {
226                 parent::fill_in_additional_detail_fields();
227
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;
234                 }
235                 }
236        //get campaign name
237         if(!empty($this->campaign_id)) {
238                 $camp = new Campaign();
239                 $camp->retrieve($this->campaign_id);
240             $this->campaign_name = $camp->name;
241         }
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'];
250                 }
251                 }
252         }
253
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): ______________________________________..
258         */
259         function get_contacts()
260         {
261                 $this->load_relationship('contacts');
262                 $query_array=$this->contacts->getQuery(true);
263
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 ";
266
267                 $query='';
268                 foreach ($query_array as $qstring) {
269                         $query.=' '.$qstring;
270                 }
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);
273         }
274
275         function update_currency_id($fromid, $toid){
276                 $idequals = '';
277
278                 $currency = new Currency();
279                 $currency->retrieve($toid);
280                 foreach($fromid as $f){
281                         if(!empty($idequals)){
282                                 $idequals .=' or ';
283                         }
284                         $idequals .= "currency_id='$f'";
285                 }
286
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)){
291
292                                 $query = "update opportunities set currency_id='".$currency->id."', amount_usdollar='".$currency->convertToDollar($row['amount'])."' where id='".$row['id']."';";
293                                 $this->db->query($query);
294
295                         }
296
297         }
298         }
299
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);
303         $params = array();
304
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;
308                 return $temp_array;
309         }
310
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'];
319                         }
320                }
321            }
322            return '';
323     }
324
325
326         /**
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
329         */
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%'");
335
336         $the_where = "";
337         foreach($where_clauses as $clause)
338         {
339                 if($the_where != "") $the_where .= " or ";
340                 $the_where .= $clause;
341         }
342
343
344         return $the_where;
345 }
346
347         function save($check_notify = FALSE)
348     {
349         // Bug 32581 - Make sure the currency_id is set to something
350         global $current_user, $app_list_strings;
351
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;
356
357         //if probablity isn't set, set it based on the sales stage
358         if (!isset($this->probability) && !empty($this->sales_stage))
359         {
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];
363         }
364
365                 require_once('modules/Opportunities/SaveOverload.php');
366
367                 perform_save($this);
368                 return parent::save($check_notify);
369
370         }
371
372         function save_relationship_changes($is_update)
373         {
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']);
381                 }
382                 // Bug 38529 & 40938 - exclude currency_id
383                 parent::save_relationship_changes($is_update, array('currency_id'));
384                 
385                 if (!empty($this->contact_id)) {
386                         $this->set_opportunity_contact_relationship($this->contact_id);
387                 }
388         }
389
390         function set_opportunity_contact_relationship($contact_id)
391         {
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));
396         }
397
398         function set_notification_body($xtpl, $oppty)
399         {
400                 global $app_list_strings;
401
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);
407
408                 return $xtpl;
409         }
410
411         function bean_implements($interface){
412                 switch($interface){
413                         case 'ACL':return true;
414                 }
415                 return false;
416         }
417         function listviewACLHelper(){
418                 $array_assign = parent::listviewACLHelper();
419                 $is_owner = false;
420                 if(!empty($this->account_id)){
421
422                         if(!empty($this->account_id_owner)){
423                                 global $current_user;
424                                 $is_owner = $current_user->id == $this->account_id_owner;
425                         }
426                 }
427                         if(!ACLController::moduleSupportsACL('Accounts') || ACLController::checkAccess('Accounts', 'view', $is_owner)){
428                                 $array_assign['ACCOUNT'] = 'a';
429                         }else{
430                                 $array_assign['ACCOUNT'] = 'span';
431                         }
432
433                 return $array_assign;
434         }
435
436         /**
437          * Static helper function for getting releated account info.
438          */
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);
450                 if($row != null) {
451                         $ret_array['name'] = $row['name'];
452                         $ret_array['id'] = $row['id'];
453                         $ret_array['assigned_user_id'] = $row['assigned_user_id'];
454                 }
455                 return $ret_array;
456         }
457 }
458 function getCurrencyType(){
459
460 }
461
462 ?>