]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Opportunities/Opportunity.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Opportunities / Opportunity.php
1 <?php
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.
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                                                                         );
106         
107         function Opportunity() {
108                 parent::SugarBean();
109                 global $sugar_config;
110                 if(!$sugar_config['require_accounts']){
111                         unset($this->required_fields['account_name']);
112                 }
113         }
114
115         var $new_schema = true;
116
117         
118
119         function get_summary_text()
120         {
121                 return "$this->name";
122         }
123
124         function create_list_query($order_by, $where, $show_deleted = 0)
125         {
126                 
127 $custom_join = $this->custom_fields->getJOIN();
128                 $query = "SELECT ";
129             
130                 $query .= "
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 ";
135                             if($custom_join){
136                                                                 $query .= $custom_join['select'];
137                                                         }
138                             $query .= " ,opportunities.*
139                             FROM opportunities ";
140
141
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
146                             LEFT JOIN accounts
147                             ON $this->rel_account_table.account_id=accounts.id ";
148                             if($custom_join){
149                                         $query .= $custom_join['join'];
150                                 }
151                 $where_auto = '1=1';
152                 if($show_deleted == 0){     
153                         $where_auto = "
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";
159                 }
160
161                 if($where != "")
162                         $query .= "where ($where) AND ".$where_auto;
163                 else
164                         $query .= "where ".$where_auto;
165
166                 if($order_by != "")
167                         $query .= " ORDER BY $order_by";
168                 else
169                         $query .= " ORDER BY opportunities.name";
170
171                 return $query;
172         }
173
174
175     function create_export_query(&$order_by, &$where, $relate_link_join='')
176     {
177         $custom_join = $this->custom_fields->getJOIN(true, true,$where);
178                 if($custom_join)
179                                 $custom_join['join'] .= $relate_link_join;
180                                 $query = "SELECT
181                                 opportunities.*,
182                                 accounts.name as account_name,
183                                 users.user_name as assigned_user_name ";
184                                                                 if($custom_join){
185                                                                         $query .= $custom_join['select'];
186                                                                 }
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
192                                 LEFT JOIN accounts
193                                 ON $this->rel_account_table.account_id=accounts.id ";
194                                                                 if($custom_join){
195                                                                         $query .= $custom_join['join'];
196                                                                 }
197                 $where_auto = "
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";
201
202         if($where != "")
203                 $query .= "where $where AND ".$where_auto;
204         else
205                 $query .= "where ".$where_auto;
206
207         if($order_by != "")
208                 $query .= " ORDER BY opportunities.$order_by";
209         else
210                 $query .= " ORDER BY opportunities.name";
211         return $query;
212     }
213
214         function fill_in_additional_list_fields()
215         {
216                 if ( $this->force_load_details == true)
217                 {
218                         $this->fill_in_additional_detail_fields();
219                 }
220         }
221
222         function fill_in_additional_detail_fields()
223         {               
224                 parent::fill_in_additional_detail_fields();
225                 
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;
231                 }
232        //get campaign name
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'];
243                 }
244
245                 }
246
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): ______________________________________..
251         */
252         function get_contacts()
253         {
254                 $this->load_relationship('contacts');
255                 $query_array=$this->contacts->getQuery(true);
256                 
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 ";
259         
260                 $query='';
261                 foreach ($query_array as $qstring) {
262                         $query.=' '.$qstring;
263                 }       
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);
266         }
267
268         function update_currency_id($fromid, $toid){
269                 $idequals = '';
270                 
271                 $currency = new Currency();
272                 $currency->retrieve($toid);
273                 foreach($fromid as $f){
274                         if(!empty($idequals)){
275                                 $idequals .=' or ';
276                         }
277                         $idequals .= "currency_id='$f'";
278                 }
279
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)){
284
285                                 $query = "update opportunities set currency_id='".$currency->id."', amount_usdollar='".$currency->convertToDollar($row['amount'])."' where id='".$row['id']."';";
286                                 $this->db->query($query);
287
288                         }
289
290         }
291         }
292
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);
296         $params = array();
297                 
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;
301                 return $temp_array;
302         }
303
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'];   
312                         }
313                }
314            }
315            return '';
316     } 
317
318         
319         /**
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
322         */
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%'");
328
329         $the_where = "";
330         foreach($where_clauses as $clause)
331         {
332                 if($the_where != "") $the_where .= " or ";
333                 $the_where .= $clause;
334         }
335
336
337         return $the_where;
338 }
339
340         function save($check_notify = FALSE) 
341     {
342         // Bug 32581 - Make sure the currency_id is set to something
343         global $current_user, $app_list_strings;
344         
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;
349             
350         //if probablity isn't set, set it based on the sales stage
351         if (!isset($this->probability) && !empty($this->sales_stage))
352         {
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];
356         }
357         
358                 require_once('modules/Opportunities/SaveOverload.php');
359                 
360                 perform_save($this);
361                 return parent::save($check_notify);
362
363         }
364         
365         function save_relationship_changes($is_update)
366         {
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']);                                                                                                        
374                 }
375
376                 parent::save_relationship_changes($is_update);
377                 
378                 if (!empty($this->contact_id)) {
379                         $this->set_opportunity_contact_relationship($this->contact_id);
380                 }
381         }
382
383         function set_opportunity_contact_relationship($contact_id)
384         {
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));
389         }
390
391         function set_notification_body($xtpl, $oppty)
392         {
393                 global $app_list_strings;
394                 
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);
400
401                 return $xtpl;
402         }
403
404         function bean_implements($interface){
405                 switch($interface){
406                         case 'ACL':return true;
407                 }
408                 return false;
409         }
410         function listviewACLHelper(){
411                 $array_assign = parent::listviewACLHelper();
412                 $is_owner = false;
413                 if(!empty($this->account_id)){
414                         
415                         if(!empty($this->account_id_owner)){
416                                 global $current_user;
417                                 $is_owner = $current_user->id == $this->account_id_owner;
418                         }
419                 }
420                         if(!ACLController::moduleSupportsACL('Accounts') || ACLController::checkAccess('Accounts', 'view', $is_owner)){
421                                 $array_assign['ACCOUNT'] = 'a';
422                         }else{
423                                 $array_assign['ACCOUNT'] = 'span';
424                         }
425                 
426                 return $array_assign;
427         }
428         
429         /**
430          * Static helper function for getting releated account info.
431          */
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);
443                 if($row != null) {
444                         $ret_array['name'] = $row['name'];
445                         $ret_array['id'] = $row['id'];
446                         $ret_array['assigned_user_id'] = $row['assigned_user_id'];
447                 }
448                 return $ret_array;
449         }
450 }
451 function getCurrencyType(){
452         
453 }
454
455 ?>