]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Opportunities/OpportunityFormBase.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Opportunities / OpportunityFormBase.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
41 class OpportunityFormBase{
42
43
44 function checkForDuplicates($prefix){
45         require_once('include/formbase.php');
46         
47         $focus = new Opportunity();
48         $query = '';
49         $baseQuery = 'select id, name, sales_stage,amount, date_closed  from opportunities where deleted!=1 and (';
50
51         if(isset($_POST[$prefix.'name']) && !empty($_POST[$prefix.'name'])){
52                 $query = $baseQuery ."  name like '%".$_POST[$prefix.'name']."%'";
53                 $query .= getLikeForEachWord('name', $_POST[$prefix.'name']);
54         }
55
56         if(!empty($query)){
57                 $rows = array();
58                 global $db;
59                 $result = $db->query($query.')');
60                 $i=-1;
61                 while(($row=$db->fetchByAssoc($result)) != null) {
62                         $i++;
63                         $rows[$i] = $row;
64                 }
65                 if ($i==-1) return null;
66                 
67                 return $rows;           
68         }
69         return null;
70 }
71
72
73 function buildTableForm($rows, $mod='Opportunities'){
74         if(!empty($mod)){
75         global $current_language;
76         $mod_strings = return_module_language($current_language, $mod);
77         }else global $mod_strings;
78         global $app_strings;
79         $cols = sizeof($rows[0]) * 2 + 1;
80         $form = '<table width="100%"><tr><td>'.$mod_strings['MSG_DUPLICATE']. '</td></tr><tr><td height="20"></td></tr></table>';
81
82         $form .= "<form action='index.php' method='post' name='dupOpps'><input type='hidden' name='selectedOpportunity' value=''>";
83         $form .= "<table width='100%' cellpadding='0' cellspacing='0' class='list view'>";
84         $form .= "<tr class='pagination'><td colspan='$cols'><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td><input type='submit' class='button' name='ContinueOpportunity' value='${mod_strings['LNK_NEW_OPPORTUNITY']}'></td></tr></table></td></tr><tr>";
85         $form .= "<tr><td scope='col'>&nbsp;</td>";
86     require_once('include/formbase.php');
87         $form .= getPostToForm();
88         if(isset($rows[0])){
89                 foreach ($rows[0] as $key=>$value){
90                         if($key != 'id'){
91                                         $form .= "<td scope='col'>". $mod_strings[$mod_strings['db_'.$key]]. "</td>";
92                 }}
93                 $form .= "</tr>";
94         }
95
96         $rowColor = 'oddListRowS1';
97         foreach($rows as $row){
98
99                 $form .= "<tr class='$rowColor'>";
100
101                 $form .= "<td width='1%' nowrap='nowrap'><a href='#' onclick='document.dupOpps.selectedOpportunity.value=\"${row['id']}\";document.dupOpps.submit();'>[${app_strings['LBL_SELECT_BUTTON_LABEL']}]</a>&nbsp;&nbsp;</td>";
102                 $wasSet = false;
103                 foreach ($row as $key=>$value){
104                                 if($key != 'id'){
105                                         if(!$wasSet){
106                                         $form .= "<td scope='row'><a target='_blank' href='index.php?module=Opportunities&action=DetailView&record=${row['id']}'>$value</a></td>";
107                                         $wasSet = true;
108                                         }else{
109                                         $form .= "<td><a target='_blank' href='index.php?module=Opportunities&action=DetailView&record=${row['id']}'>$value</a></td>";
110                                         }
111                                 }}
112
113                 if($rowColor == 'evenListRowS1'){
114                         $rowColor = 'oddListRowS1';
115                 }else{
116                          $rowColor = 'evenListRowS1';
117                 }
118                 $form .= "</tr>";
119         }
120     $form .= "<tr class='pagination'><td colspan='$cols'><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td><input type='submit' class='button' name='ContinueOpportunity' value='${mod_strings['LNK_NEW_OPPORTUNITY']}'></td></tr></table></td></tr><tr>";
121         $form .= "</table><BR></form>";
122
123         return $form;
124
125
126
127 }
128
129 function getForm($prefix, $mod='Opportunities'){
130         if(!ACLController::checkAccess('Opportunities', 'edit', true)){
131                 return '';
132         }
133 if(!empty($mod)){
134         global $current_language;
135         $mod_strings = return_module_language($current_language, $mod);
136 }else global $mod_strings;
137 global $app_strings;
138 global $sugar_version, $sugar_config;
139
140
141 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
142 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
143 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
144
145
146 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
147 $the_form .= <<<EOQ
148                 <form name="{$prefix}OppSave" onSubmit="return check_form('{$prefix}OppSave')" method="POST" action="index.php">
149                         <input type="hidden" name="{$prefix}module" value="Opportunities">
150                         <input type="hidden" name="${prefix}action" value="Save">
151 EOQ;
152 $the_form .= $this->getFormBody($prefix, $mod, "{$prefix}OppSave");
153 $the_form .= <<<EOQ
154                 <input title="$lbl_save_button_title" accessKey="$lbl_save_button_key" class="button" type="submit" name="button" value="  $lbl_save_button_label  " >
155                 </form>
156
157 EOQ;
158 $the_form .= get_left_form_footer();
159 $the_form .= get_validate_record_js();
160
161 return $the_form;
162 }
163
164 function getWideFormBody($prefix, $mod='Opportunities', $formname='', $lead='', $showaccount = true){
165         if(!ACLController::checkAccess('Opportunities', 'edit', true)){
166                 return '';
167         }
168         if(empty($lead)){
169                 $lead = new Lead();
170         }
171 global $mod_strings, $sugar_config;
172 $showaccount = $showaccount && $sugar_config['require_accounts'];
173 $temp_strings = $mod_strings;
174 if(!empty($mod)){
175         global $current_language;
176         $mod_strings = return_module_language($current_language, $mod);
177 }
178
179 global $app_strings;
180 global $app_list_strings;
181 global $theme;
182 global $current_user;
183 global $timedate;
184 // Unimplemented until jscalendar language files are fixed
185 // global $current_language;
186 // global $default_language;
187 // global $cal_codes;
188
189 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
190 $lbl_opportunity_name = $mod_strings['LBL_OPPORTUNITY_NAME'];
191 $lbl_sales_stage = $mod_strings['LBL_SALES_STAGE'];
192 $lbl_date_closed = $mod_strings['LBL_DATE_CLOSED'];
193 $lbl_amount = $mod_strings['LBL_AMOUNT'];
194 $lbl_probability = $mod_strings['LBL_PROBABILITY'];
195 $json = getJSONobj();
196 $prob_array = $json->encode($app_list_strings['sales_probability_dom']);
197 //$prePopProb = '';
198 //if(empty($this->bean->id)) 
199 $prePopProb = 'document.getElementsByName(\''.$prefix.'sales_stage\')[0].onchange();';
200 $probability_script=<<<EOQ
201         <script>
202         prob_array = $prob_array;
203         document.getElementsByName('{$prefix}sales_stage')[0].onchange = function() {
204                         if(typeof(document.getElementsByName('{$prefix}sales_stage')[0].value) != "undefined" && prob_array[document.getElementsByName('{$prefix}sales_stage')[0].value]) {
205                                 document.getElementsByName('{$prefix}probability')[0].value = prob_array[document.getElementsByName('{$prefix}sales_stage')[0].value];
206                         } 
207                 };
208         $prePopProb
209         </script>
210 EOQ;
211
212 $ntc_date_format = $timedate->get_user_date_format();
213 $cal_dateformat = $timedate->get_cal_date_format();
214 if (isset($lead->assigned_user_id)) {
215         $user_id=$lead->assigned_user_id;
216 } else {
217         $user_id = $current_user->id;
218 }
219
220
221 // Unimplemented until jscalendar language files are fixed
222 // $cal_lang = (empty($cal_codes[$current_language])) ? $cal_codes[$default_language] : $cal_codes[$current_language];
223 $cal_lang = "en";
224
225 $the_form="";
226
227
228 if (isset($lead->opportunity_amount)) {
229         $opp_amount=$lead->opportunity_amount;
230 } else {
231         $opp_amount='';
232 }
233 $jsCalendarImage = SugarThemeRegistry::current()->getImageURL('jscalendar.gif');
234 $the_form .= <<<EOQ
235
236                         <input type="hidden" name="{$prefix}record" value="">
237                         <input type="hidden" name="{$prefix}account_name">
238                         <input type="hidden" name="{$prefix}assigned_user_id" value='${user_id}'>
239
240 <table cellspacing="0" cellpadding="0" border="0" width="100%">
241 <tr>
242     <td width="20%" scope="row">$lbl_opportunity_name&nbsp;<span class="required">$lbl_required_symbol</span></td>
243     <td width="80%" scope="row">{$mod_strings['LBL_DESCRIPTION']}</td>
244 </tr>
245 <tr>
246     <td ><input name='{$prefix}name' type="text" value="{$lead->opportunity_name}"></td>
247         <td  rowspan="7"><textarea name='{$prefix}description' rows='5' cols='50'></textarea></td>
248 </tr>
249 <tr>
250     <td scope="row">$lbl_date_closed&nbsp;<span class="required">$lbl_required_symbol</span></td>
251 </tr>
252 <tr>
253 <td ><input name='{$prefix}date_closed' onblur="parseDate(this, '$cal_dateformat');" size='12' maxlength='10' id='${prefix}jscal_field' type="text" value="">&nbsp;<!--not_in_theme!--><img src="{$jsCalendarImage}" alt="{$app_strings['LBL_ENTER_DATE']}"  id="${prefix}jscal_trigger" align="absmiddle"></td>
254 </tr>
255 EOQ;
256 if($showaccount){
257         $the_form .= <<<EOQ
258 <tr>
259     <td scope="row">${mod_strings['LBL_ACCOUNT_NAME']}&nbsp;<span class="required">${lbl_required_symbol}</span></td>
260 </tr>
261 <tr>
262     <td ><input readonly id='qc_account_name' name='account_name' type='text' value="" size="16"><input id='qc_account_id' name='account_id' type="hidden" value=''>&nbsp;<input  title="{$app_strings['LBL_SELECT_BUTTON_TITLE']}" accessKey="{$app_strings['LBL_SELECT_BUTTON_KEY']}" type="button" class="button" value='{$app_strings['LBL_SELECT_BUTTON_LABEL']}' name=btn1 LANGUAGE=javascript onclick='return window.open("index.php?module=Accounts&action=Popup&html=Popup_picker&form={$formname}&form_submit=false","","width=600,height=400,resizable=1,scrollbars=1");'></td>
263 </tr>
264 EOQ;
265 }
266 $the_form .= <<<EOQ
267 <tr>
268     <td scope="row">$lbl_sales_stage&nbsp;<span class="required">$lbl_required_symbol</span></td>
269 </tr>
270 <tr>
271     <td ><select name='{$prefix}sales_stage'>
272 EOQ;
273 $the_form .= get_select_options_with_id($app_list_strings['sales_stage_dom'], "");
274 $the_form .= <<<EOQ
275                 </select></td>
276 </tr>
277 <tr>
278     <td scope="row">$lbl_amount&nbsp;<span class="required">$lbl_required_symbol</span></td>
279 </tr>
280 <tr>
281     <td ><input name='{$prefix}amount' type="text" value='{$opp_amount}'></td>
282 </tr>
283 EOQ;
284 //carry forward custom lead fields to opportunities during Lead Conversion
285         $tempOpp = new Opportunity();
286         if (method_exists($lead, 'convertCustomFieldsForm')) $lead->convertCustomFieldsForm($the_form, $tempOpp, $prefix);
287         unset($tempOpp);
288
289 $the_form .= <<<EOQ
290
291 </table>
292
293                 <script type="text/javascript">
294                 Calendar.setup ({
295                         inputField : "{$prefix}jscal_field", ifFormat : "$cal_dateformat", showsTime : false, button : "${prefix}jscal_trigger", singleClick : true, step : 1, weekNumbers:false
296                 });
297                 </script>
298
299
300 EOQ;
301
302
303
304 $javascript = new javascript();
305 $javascript->setFormName($formname);
306 $javascript->setSugarBean(new Opportunity());
307 $javascript->addRequiredFields($prefix);
308 $the_form .=$javascript->getScript();
309 $mod_strings = $temp_strings;
310 return $the_form;
311
312 } // end getWideFormBody
313
314 function getFormBody($prefix, $mod='Opportunities', $formname=''){
315         if(!ACLController::checkAccess('Opportunities', 'edit', true)){
316                 return '';
317         }
318 if(!empty($mod)){
319         global $current_language;
320         $mod_strings = return_module_language($current_language, $mod);
321 }else global $mod_strings;
322 global $app_strings;
323 global $app_list_strings;
324 global $theme;
325 global $current_user;
326 global $sugar_config;
327 global $timedate;
328 // Unimplemented until jscalendar language files are fixed
329 // global $current_language;
330 // global $default_language;
331 // global $cal_codes;
332
333 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
334 $lbl_opportunity_name = $mod_strings['LBL_OPPORTUNITY_NAME'];
335 $lbl_sales_stage = $mod_strings['LBL_SALES_STAGE'];
336 $lbl_date_closed = $mod_strings['LBL_DATE_CLOSED'];
337 $lbl_amount = $mod_strings['LBL_AMOUNT'];
338
339 $ntc_date_format = $timedate->get_user_date_format();
340 $cal_dateformat = $timedate->get_cal_date_format();
341
342 $user_id = $current_user->id;
343
344 // Unimplemented until jscalendar language files are fixed
345 // $cal_lang = (empty($cal_codes[$current_language])) ? $cal_codes[$default_language] : $cal_codes[$current_language];
346 $cal_lang = "en";
347
348 $the_form = <<<EOQ
349 <p>
350                         <input type="hidden" name="{$prefix}record" value="">
351                         <input type="hidden" name="{$prefix}assigned_user_id" value='${user_id}'>
352
353                 $lbl_opportunity_name&nbsp;<span class="required">$lbl_required_symbol</span><br>
354                 <input name='{$prefix}name' type="text" value="">
355 EOQ;
356 if($sugar_config['require_accounts']){
357
358 ///////////////////////////////////////
359 ///
360 /// SETUP ACCOUNT POPUP
361
362 $popup_request_data = array(
363         'call_back_function' => 'set_return',
364         'form_name' => "{$prefix}OppSave",
365         'field_to_name_array' => array(
366                 'id' => 'account_id',
367                 'name' => 'account_name',
368                 ),
369         );
370
371 $json = getJSONobj();
372 $encoded_popup_request_data = $json->encode($popup_request_data);
373
374 //
375 ///////////////////////////////////////
376
377 $the_form .= <<<EOQ
378                 ${mod_strings['LBL_ACCOUNT_NAME']}&nbsp;<span class="required">${lbl_required_symbol}</span><br>
379                 <input class='sqsEnabled' autocomplete='off' id='qc_account_name' name='account_name' type='text' value="" size="16"><input id='qc_account_id' name='account_id' type="hidden" value=''>&nbsp;<input title="{$app_strings['LBL_SELECT_BUTTON_TITLE']}" accessKey="{$app_strings['LBL_SELECT_BUTTON_KEY']}" type="button" class="button" value='{$app_strings['LBL_SELECT_BUTTON_LABEL']}' name=btn1
380                         onclick='open_popup("Accounts", 600, 400, "", true, false, {$encoded_popup_request_data});' /><br>
381 EOQ;
382 }
383 $jsCalendarImage = SugarThemeRegistry::current()->getImageURL('jscalendar.gif');
384 $the_form .= <<<EOQ
385                 $lbl_date_closed&nbsp;<span class="required">$lbl_required_symbol</span> <br><span class="dateFormat">$ntc_date_format</span><br>
386                 <input name='{$prefix}date_closed' size='12' maxlength='10' id='{$prefix}jscal_field' type="text" value=""> <!--not_in_theme!--><img src="{$jsCalendarImage}" alt="{$app_strings['LBL_ENTER_DATE']}"  id="jscal_trigger" align="absmiddle"><br>
387                 $lbl_sales_stage&nbsp;<span class="required">$lbl_required_symbol</span><br>
388                 <select name='{$prefix}sales_stage'>
389 EOQ;
390 $the_form .= get_select_options_with_id($app_list_strings['sales_stage_dom'], "");
391 $the_form .= <<<EOQ
392                 </select><br>
393                 $lbl_amount&nbsp;<span class="required">$lbl_required_symbol</span><br>
394                 <input name='{$prefix}amount' type="text"></p>
395                 <input type='hidden' name='lead_source' value=''>
396                 <script type="text/javascript">
397                 Calendar.setup ({
398                         inputField : "{$prefix}jscal_field", daFormat : "$cal_dateformat", ifFormat : "$cal_dateformat", showsTime : false, button : "jscal_trigger", singleClick : true, step : 1, weekNumbers:false
399                 });
400                 </script>
401 EOQ;
402
403
404 require_once('include/QuickSearchDefaults.php');
405 $qsd = new QuickSearchDefaults();
406 $sqs_objects = array('qc_account_name' => $qsd->getQSParent());
407 $sqs_objects['qc_account_name']['populate_list'] = array('qc_account_name', 'qc_account_id');
408 $quicksearch_js = '<script type="text/javascript" language="javascript">sqs_objects = ' . $json->encode($sqs_objects) . '</script>';
409 $the_form .= $quicksearch_js;
410
411
412
413 $javascript = new javascript();
414 $javascript->setFormName($formname);
415 $javascript->setSugarBean(new Opportunity());
416 $javascript->addRequiredFields($prefix);
417 $the_form .=$javascript->getScript();
418
419
420 return $the_form;
421
422 }
423
424
425 function handleSave($prefix,$redirect=true, $useRequired=false){
426     global $current_user;
427         
428         
429         require_once('include/formbase.php');
430         
431         $focus = new Opportunity();
432         if($useRequired &&  !checkRequired($prefix, array_keys($focus->required_fields))){
433                 return null;
434         }
435
436     if(empty($_POST['currency_id'])){
437         $currency_id = $current_user->getPreference('currency');
438         if(isset($currency_id)){
439             $focus->currency_id =   $currency_id;
440         }
441     }
442         $focus = populateFromPost($prefix, $focus);
443         if( !ACLController::checkAccess($focus->module_dir, 'edit', $focus->isOwner($current_user->id))){
444                 ACLController::displayNoAccess(true);
445         }
446         $check_notify = FALSE;
447         if (isset($GLOBALS['check_notify'])) {
448                 $check_notify = $GLOBALS['check_notify'];
449         }
450
451         $focus->save($check_notify);
452
453         if(!empty($_POST['duplicate_parent_id'])){
454                 clone_relationship($focus->db, array('opportunities_contacts'),'opportunity_id',  $_POST['duplicate_parent_id'], $focus->id);
455         }
456         $return_id = $focus->id;
457         
458         $GLOBALS['log']->debug("Saved record with id of ".$return_id);
459         if($redirect){
460                 handleRedirect($return_id,"Opportunities" );
461         }else{
462                 return $focus;
463         }
464 }
465
466 }
467 ?>