]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Opportunities/Opportunity.php
Release 6.5.10
[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-2013 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->getCustomJoin();
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         $query .= $custom_join['select'];
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         $query .= $custom_join['join'];
149                 $where_auto = '1=1';
150                 if($show_deleted == 0){
151                         $where_auto = "
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";
157                 }
158
159                 if($where != "")
160                         $query .= "where ($where) AND ".$where_auto;
161                 else
162                         $query .= "where ".$where_auto;
163
164                 if($order_by != "")
165                         $query .= " ORDER BY $order_by";
166                 else
167                         $query .= " ORDER BY opportunities.name";
168
169                 return $query;
170         }
171
172
173     function create_export_query(&$order_by, &$where, $relate_link_join='')
174     {
175         $custom_join = $this->getCustomJoin(true, true, $where);
176         $custom_join['join'] .= $relate_link_join;
177                                 $query = "SELECT
178                                 opportunities.*,
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
187                                 LEFT JOIN accounts
188                                 ON $this->rel_account_table.account_id=accounts.id ";
189         $query .= $custom_join['join'];
190                 $where_auto = "
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";
194
195         if($where != "")
196                 $query .= "where $where AND ".$where_auto;
197         else
198                 $query .= "where ".$where_auto;
199
200         if($order_by != "")
201                 $query .= " ORDER BY opportunities.$order_by";
202         else
203                 $query .= " ORDER BY opportunities.name";
204         return $query;
205     }
206
207         function fill_in_additional_list_fields()
208         {
209                 if ( $this->force_load_details == true)
210                 {
211                         $this->fill_in_additional_detail_fields();
212                 }
213         }
214
215         function fill_in_additional_detail_fields()
216         {
217                 parent::fill_in_additional_detail_fields();
218
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;
225                 }
226                 }
227        //get campaign name
228         if(!empty($this->campaign_id)) {
229                 $camp = new Campaign();
230                 $camp->retrieve($this->campaign_id);
231             $this->campaign_name = $camp->name;
232         }
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'];
241                 }
242                 }
243         }
244
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): ______________________________________..
249         */
250         function get_contacts()
251         {
252                 $this->load_relationship('contacts');
253                 $query_array=$this->contacts->getQuery(true);
254
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 ";
257
258                 $query='';
259                 foreach ($query_array as $qstring) {
260                         $query.=' '.$qstring;
261                 }
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);
264         }
265
266         function update_currency_id($fromid, $toid){
267                 $idequals = '';
268
269                 $currency = new Currency();
270                 $currency->retrieve($toid);
271                 foreach($fromid as $f){
272                         if(!empty($idequals)){
273                                 $idequals .=' or ';
274                         }
275                         $idequals .= "currency_id='$f'";
276                 }
277
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)){
282
283                                 $query = "update opportunities set currency_id='".$currency->id."', amount_usdollar='".$currency->convertToDollar($row['amount'])."' where id='".$row['id']."';";
284                                 $this->db->query($query);
285
286                         }
287
288         }
289         }
290
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);
294         $params = array();
295
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;
299                 return $temp_array;
300         }
301
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'];
310                         }
311                }
312            }
313            return '';
314     }
315
316
317         /**
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
320         */
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%'");
326
327         $the_where = "";
328         foreach($where_clauses as $clause)
329         {
330                 if($the_where != "") $the_where .= " or ";
331                 $the_where .= $clause;
332         }
333
334
335         return $the_where;
336 }
337
338         function save($check_notify = FALSE)
339     {
340         // Bug 32581 - Make sure the currency_id is set to something
341         global $current_user, $app_list_strings;
342
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;
347
348         //if probablity isn't set, set it based on the sales stage
349         if (!isset($this->probability) && !empty($this->sales_stage))
350         {
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];
354         }
355
356                 require_once('modules/Opportunities/SaveOverload.php');
357
358                 perform_save($this);
359                 return parent::save($check_notify);
360
361         }
362
363         function save_relationship_changes($is_update)
364         {
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']);
372                 }
373                 // Bug 38529 & 40938 - exclude currency_id
374                 parent::save_relationship_changes($is_update, array('currency_id'));
375                 
376                 if (!empty($this->contact_id)) {
377                         $this->set_opportunity_contact_relationship($this->contact_id);
378                 }
379         }
380
381         function set_opportunity_contact_relationship($contact_id)
382         {
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));
387         }
388
389         function set_notification_body($xtpl, $oppty)
390         {
391                 global $app_list_strings;
392
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);
398
399                 return $xtpl;
400         }
401
402         function bean_implements($interface){
403                 switch($interface){
404                         case 'ACL':return true;
405                 }
406                 return false;
407         }
408         function listviewACLHelper(){
409                 $array_assign = parent::listviewACLHelper();
410                 $is_owner = false;
411                 if(!empty($this->account_id)){
412
413                         if(!empty($this->account_id_owner)){
414                                 global $current_user;
415                                 $is_owner = $current_user->id == $this->account_id_owner;
416                         }
417                 }
418                         if(!ACLController::moduleSupportsACL('Accounts') || ACLController::checkAccess('Accounts', 'view', $is_owner)){
419                                 $array_assign['ACCOUNT'] = 'a';
420                         }else{
421                                 $array_assign['ACCOUNT'] = 'span';
422                         }
423
424                 return $array_assign;
425         }
426
427         /**
428          * Static helper function for getting releated account info.
429          */
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);
441                 if($row != null) {
442                         $ret_array['name'] = $row['name'];
443                         $ret_array['id'] = $row['id'];
444                         $ret_array['assigned_user_id'] = $row['assigned_user_id'];
445                 }
446                 return $ret_array;
447         }
448 }
449 function getCurrencyType(){
450
451 }
452
453 ?>