]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Accounts/Account.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Accounts / Account.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-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:  Defines the Account SugarBean Account entity with the necessary
41  * methods and variables.
42  ********************************************************************************/
43
44 require_once("include/SugarObjects/templates/company/Company.php");
45
46 // Account is used to store account information.
47 class Account extends Company {
48         var $field_name_map = array();
49         // Stored fields
50         var $date_entered;
51         var $date_modified;
52         var $modified_user_id;
53         var $assigned_user_id;
54         var $annual_revenue;
55         var $billing_address_street;
56         var $billing_address_city;
57         var $billing_address_state;
58         var $billing_address_country;
59         var $billing_address_postalcode;
60
61     var $billing_address_street_2;
62     var $billing_address_street_3;
63     var $billing_address_street_4;
64
65         var $description;
66         var $email1;
67         var $email2;
68         var $email_opt_out;
69         var $invalid_email;
70         var $employees;
71         var $id;
72         var $industry;
73         var $name;
74         var $ownership;
75         var $parent_id;
76         var $phone_alternate;
77         var $phone_fax;
78         var $phone_office;
79         var $rating;
80         var $shipping_address_street;
81         var $shipping_address_city;
82         var $shipping_address_state;
83         var $shipping_address_country;
84         var $shipping_address_postalcode;
85     
86     var $shipping_address_street_2;
87     var $shipping_address_street_3;
88     var $shipping_address_street_4;
89     
90     var $campaign_id;
91     
92         var $sic_code;
93         var $ticker_symbol;
94         var $account_type;
95         var $website;
96         var $custom_fields;
97
98         var $created_by;
99         var $created_by_name;
100         var $modified_by_name;
101
102         // These are for related fields
103         var $opportunity_id;
104         var $case_id;
105         var $contact_id;
106         var $task_id;
107         var $note_id;
108         var $meeting_id;
109         var $call_id;
110         var $email_id;
111         var $member_id;
112         var $parent_name;
113         var $assigned_user_name;
114         var $account_id = '';
115         var $account_name = '';
116         var $bug_id ='';
117         var $module_dir = 'Accounts';
118         var $emailAddress;
119
120
121         var $table_name = "accounts";
122         var $object_name = "Account";
123         var $importable = true;
124         var $new_schema = true;
125         // This is used to retrieve related fields from form posts.
126         var $additional_column_fields = Array('assigned_user_name', 'assigned_user_id', 'opportunity_id', 'bug_id', 'case_id', 'contact_id', 'task_id', 'note_id', 'meeting_id', 'call_id', 'email_id', 'parent_name', 'member_id'
127         );
128         var $relationship_fields = Array('opportunity_id'=>'opportunities', 'bug_id' => 'bugs', 'case_id'=>'cases',
129                                                                         'contact_id'=>'contacts', 'task_id'=>'tasks', 'note_id'=>'notes',
130                                                                         'meeting_id'=>'meetings', 'call_id'=>'calls', 'email_id'=>'emails','member_id'=>'members',
131                                                                         'project_id'=>'project',
132                                                                         );
133
134     //Meta-Data Framework fields
135     var $push_billing;
136     var $push_shipping;
137
138         function Account() {
139         parent::Company();
140
141         $this->setupCustomFields('Accounts');
142
143                 foreach ($this->field_defs as $field) 
144                 {
145                         if(isset($field['name']))
146                         {
147                                 $this->field_name_map[$field['name']] = $field;
148                         }
149                 }
150
151
152         //Combine the email logic original here with bug #26450.
153                 if( (!empty($_REQUEST['parent_id']) && !empty($_REQUEST['parent_type']) && $_REQUEST['parent_type'] == 'Emails'
154                 && !empty($_REQUEST['return_module']) && $_REQUEST['return_module'] == 'Emails' )
155                 ||
156                 (!empty($_REQUEST['parent_type']) && $_REQUEST['parent_type'] != 'Accounts' &&
157                 !empty($_REQUEST['return_module']) && $_REQUEST['return_module'] != 'Accounts') ){
158                         $_REQUEST['parent_name'] = '';
159                         $_REQUEST['parent_id'] = '';
160                 }
161         }
162
163         function get_summary_text()
164         {
165                 return $this->name;
166         }
167
168         function get_contacts() {
169                 return $this->get_linked_beans('contacts','Contact');
170         }
171
172
173
174         function clear_account_case_relationship($account_id='', $case_id='')
175         {
176                 if (empty($case_id)) $where = '';
177                 else $where = " and id = '$case_id'";
178                 $query = "UPDATE cases SET account_name = '', account_id = '' WHERE account_id = '$account_id' AND deleted = 0 " . $where;
179                 $this->db->query($query,true,"Error clearing account to case relationship: ");
180         }
181
182         /**
183         * This method is used to provide backward compatibility with old data that was prefixed with http://
184         * We now automatically prefix http://
185         * @deprecated.
186         */
187         function remove_redundant_http()
188         {       /*
189                 if(preg_match("@http://@", $this->website))
190                 {
191                         $this->website = substr($this->website, 7);
192                 }
193                 */
194         }
195
196         function fill_in_additional_list_fields()
197         {
198                 parent::fill_in_additional_list_fields();
199         // Fill in the assigned_user_name
200         //      $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
201
202         }
203
204         function fill_in_additional_detail_fields()
205         {
206         parent::fill_in_additional_detail_fields();
207
208         //rrs bug: 28184 - instead of removing this code altogether just adding this check to ensure that if the parent_name
209         //is empty then go ahead and fill it.
210         if(empty($this->parent_name) && !empty($this->id)){
211                         $query = "SELECT a1.name from accounts a1, accounts a2 where a1.id = a2.parent_id and a2.id = '$this->id' and a1.deleted=0";
212                         $result = $this->db->query($query,true," Error filling in additional detail fields: ");
213
214                         // Get the id and the name.
215                         $row = $this->db->fetchByAssoc($result);
216
217                         if($row != null)
218                         {
219                                 $this->parent_name = $row['name'];
220                         }
221                         else
222                         {
223                                 $this->parent_name = '';
224                         }
225         }               
226         
227         // Set campaign name if there is a campaign id
228                 if( !empty($this->campaign_id)){
229                         
230                         $camp = new Campaign();
231                     $where = "campaigns.id='{$this->campaign_id}'";
232                     $campaign_list = $camp->get_full_list("campaigns.name", $where, true);
233                     $this->campaign_name = $campaign_list[0]->name;
234                 }
235         }
236
237         function get_list_view_data(){
238                 global $system_config,$current_user;
239                 $temp_array = $this->get_list_view_array();
240                 $temp_array["ENCODED_NAME"]=$this->name;
241 //              $temp_array["ENCODED_NAME"]=htmlspecialchars($this->name, ENT_QUOTES);
242                 if(!empty($this->billing_address_state))
243                 {
244                         $temp_array["CITY"] = $this->billing_address_city . ', '. $this->billing_address_state;
245                 }
246                 else
247                 {
248                         $temp_array["CITY"] = $this->billing_address_city;
249                 }
250                 $temp_array["BILLING_ADDRESS_STREET"]  = $this->billing_address_street;
251                 $temp_array["SHIPPING_ADDRESS_STREET"] = $this->shipping_address_street;
252         if(isset($system_config->settings['system_skypeout_on']) && $system_config->settings['system_skypeout_on'] == 1){
253         if(!empty($temp_array['PHONE_OFFICE']) && skype_formatted($temp_array['PHONE_OFFICE'])){
254                 $temp_array['PHONE_OFFICE'] = '<a href="callto://' . $temp_array['PHONE_OFFICE']. '">'.$temp_array['PHONE_OFFICE']. '</a>' ;
255         }}
256         $temp_array["EMAIL1"] = $this->emailAddress->getPrimaryAddress($this);
257                 $this->email1 = $temp_array['EMAIL1'];
258                 $temp_array["EMAIL1_LINK"] = $current_user->getEmailLink('email1', $this, '', '', 'ListView');
259                 return $temp_array;
260         }
261         /**
262                 builds a generic search based on the query string using or
263                 do not include any $this-> because this is called on without having the class instantiated
264         */
265         function build_generic_where_clause ($the_query_string) {
266         $where_clauses = Array();
267         $the_query_string = $this->db->quote($the_query_string);
268         array_push($where_clauses, "accounts.name like '$the_query_string%'");
269         if (is_numeric($the_query_string)) {
270                 array_push($where_clauses, "accounts.phone_alternate like '%$the_query_string%'");
271                 array_push($where_clauses, "accounts.phone_fax like '%$the_query_string%'");
272                 array_push($where_clauses, "accounts.phone_office like '%$the_query_string%'");
273         }
274
275         $the_where = "";
276         foreach($where_clauses as $clause)
277         {
278                 if(!empty($the_where)) $the_where .= " or ";
279                 $the_where .= $clause;
280         }
281
282         return $the_where;
283 }
284
285
286         function create_export_query(&$order_by, &$where, $relate_link_join='')
287         {
288                 $custom_join = $this->custom_fields->getJOIN(true, true,$where);
289                         if($custom_join)
290                                 $custom_join['join'] .= $relate_link_join;
291                          $query = "SELECT
292                                 accounts.*,email_addresses.email_address email_address,
293                                 accounts.name as account_name,
294                                 users.user_name as assigned_user_name ";
295                                                 if($custom_join){
296                                                         $query .= $custom_join['select'];
297                                                 }
298                                                  $query .= " FROM accounts ";
299                          $query .= "LEFT JOIN users
300                                         ON accounts.assigned_user_id=users.id ";
301
302                                                 //join email address table too.
303                                                 $query .=  ' LEFT JOIN  email_addr_bean_rel on accounts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'Accounts\' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 ';
304                                                 $query .=  ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ;
305
306                                                 if($custom_join){
307                                                         $query .= $custom_join['join'];
308                                                 }
309
310                         $where_auto = "( accounts.deleted IS NULL OR accounts.deleted=0 )";
311
312                 if($where != "")
313                         $query .= "where ($where) AND ".$where_auto;
314                 else
315                         $query .= "where ".$where_auto;
316
317                 if(!empty($order_by))
318                         $query .=  " ORDER BY ". $this->process_order_by($order_by, null);
319
320                 return $query;
321         }
322
323         function set_notification_body($xtpl, $account)
324         {
325                 $xtpl->assign("ACCOUNT_NAME", $account->name);
326                 $xtpl->assign("ACCOUNT_TYPE", $account->account_type);
327                 $xtpl->assign("ACCOUNT_DESCRIPTION", $account->description);
328
329                 return $xtpl;
330         }
331
332         function bean_implements($interface){
333                 switch($interface){
334                         case 'ACL':return true;
335                 }
336                 return false;
337         }
338         function get_unlinked_email_query($type=array()) {
339
340                 return get_unlinked_email_query($type, $this);
341         }
342
343 }