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.
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.
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
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
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.
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.
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 ********************************************************************************/
41 class OpportunityFormBase{
44 function checkForDuplicates($prefix){
45 require_once('include/formbase.php');
47 $focus = new Opportunity();
49 $baseQuery = 'select id, name, sales_stage,amount, date_closed from opportunities where deleted!=1 and (';
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']);
59 $result = $db->query($query.')');
61 while(($row=$db->fetchByAssoc($result)) != null) {
65 if ($i==-1) return null;
73 function buildTableForm($rows, $mod='Opportunities'){
75 global $current_language;
76 $mod_strings = return_module_language($current_language, $mod);
77 }else global $mod_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>';
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'> </td>";
86 require_once('include/formbase.php');
87 $form .= getPostToForm();
89 foreach ($rows[0] as $key=>$value){
91 $form .= "<td scope='col'>". $mod_strings[$mod_strings['db_'.$key]]. "</td>";
96 $rowColor = 'oddListRowS1';
97 foreach($rows as $row){
99 $form .= "<tr class='$rowColor'>";
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> </td>";
103 foreach ($row as $key=>$value){
106 $form .= "<td scope='row'><a target='_blank' href='index.php?module=Opportunities&action=DetailView&record=${row['id']}'>$value</a></td>";
109 $form .= "<td><a target='_blank' href='index.php?module=Opportunities&action=DetailView&record=${row['id']}'>$value</a></td>";
113 if($rowColor == 'evenListRowS1'){
114 $rowColor = 'oddListRowS1';
116 $rowColor = 'evenListRowS1';
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>";
129 function getForm($prefix, $mod='Opportunities'){
130 if(!ACLController::checkAccess('Opportunities', 'edit', true)){
134 global $current_language;
135 $mod_strings = return_module_language($current_language, $mod);
136 }else global $mod_strings;
138 global $sugar_version, $sugar_config;
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'];
146 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
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">
152 $the_form .= $this->getFormBody($prefix, $mod, "{$prefix}OppSave");
154 <input title="$lbl_save_button_title" accessKey="$lbl_save_button_key" class="button" type="submit" name="button" value=" $lbl_save_button_label " >
158 $the_form .= get_left_form_footer();
159 $the_form .= get_validate_record_js();
164 function getWideFormBody($prefix, $mod='Opportunities', $formname='', $lead='', $showaccount = true){
165 if(!ACLController::checkAccess('Opportunities', 'edit', true)){
171 global $mod_strings, $sugar_config;
172 $showaccount = $showaccount && $sugar_config['require_accounts'];
173 $temp_strings = $mod_strings;
175 global $current_language;
176 $mod_strings = return_module_language($current_language, $mod);
180 global $app_list_strings;
182 global $current_user;
184 // Unimplemented until jscalendar language files are fixed
185 // global $current_language;
186 // global $default_language;
187 // global $cal_codes;
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']);
198 //if(empty($this->bean->id))
199 $prePopProb = 'document.getElementsByName(\''.$prefix.'sales_stage\')[0].onchange();';
200 $probability_script=<<<EOQ
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];
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;
217 $user_id = $current_user->id;
221 // Unimplemented until jscalendar language files are fixed
222 // $cal_lang = (empty($cal_codes[$current_language])) ? $cal_codes[$default_language] : $cal_codes[$current_language];
228 if (isset($lead->opportunity_amount)) {
229 $opp_amount=$lead->opportunity_amount;
233 $jsCalendarImage = SugarThemeRegistry::current()->getImageURL('jscalendar.gif');
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}'>
240 <table cellspacing="0" cellpadding="0" border="0" width="100%">
242 <td width="20%" scope="row">$lbl_opportunity_name <span class="required">$lbl_required_symbol</span></td>
243 <td width="80%" scope="row">{$mod_strings['LBL_DESCRIPTION']}</td>
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>
250 <td scope="row">$lbl_date_closed <span class="required">$lbl_required_symbol</span></td>
253 <td ><input name='{$prefix}date_closed' onblur="parseDate(this, '$cal_dateformat');" size='12' maxlength='10' id='${prefix}jscal_field' type="text" value=""> <!--not_in_theme!--><img src="{$jsCalendarImage}" alt="{$app_strings['LBL_ENTER_DATE']}" id="${prefix}jscal_trigger" align="absmiddle"></td>
259 <td scope="row">${mod_strings['LBL_ACCOUNT_NAME']} <span class="required">${lbl_required_symbol}</span></td>
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=''> <input title="{$app_strings['LBL_SELECT_BUTTON_TITLE']}" 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>
268 <td scope="row">$lbl_sales_stage <span class="required">$lbl_required_symbol</span></td>
271 <td ><select name='{$prefix}sales_stage'>
273 $the_form .= get_select_options_with_id($app_list_strings['sales_stage_dom'], "");
278 <td scope="row">$lbl_amount <span class="required">$lbl_required_symbol</span></td>
281 <td ><input name='{$prefix}amount' type="text" value='{$opp_amount}'></td>
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);
293 <script type="text/javascript">
295 inputField : "{$prefix}jscal_field", ifFormat : "$cal_dateformat", showsTime : false, button : "${prefix}jscal_trigger", singleClick : true, step : 1, weekNumbers:false
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;
312 } // end getWideFormBody
314 function getFormBody($prefix, $mod='Opportunities', $formname=''){
315 if(!ACLController::checkAccess('Opportunities', 'edit', true)){
319 global $current_language;
320 $mod_strings = return_module_language($current_language, $mod);
321 }else global $mod_strings;
323 global $app_list_strings;
325 global $current_user;
326 global $sugar_config;
328 // Unimplemented until jscalendar language files are fixed
329 // global $current_language;
330 // global $default_language;
331 // global $cal_codes;
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'];
339 $ntc_date_format = $timedate->get_user_date_format();
340 $cal_dateformat = $timedate->get_cal_date_format();
342 $user_id = $current_user->id;
344 // Unimplemented until jscalendar language files are fixed
345 // $cal_lang = (empty($cal_codes[$current_language])) ? $cal_codes[$default_language] : $cal_codes[$current_language];
350 <input type="hidden" name="{$prefix}record" value="">
351 <input type="hidden" name="{$prefix}assigned_user_id" value='${user_id}'>
353 $lbl_opportunity_name <span class="required">$lbl_required_symbol</span><br>
354 <input name='{$prefix}name' type="text" value="">
356 if($sugar_config['require_accounts']){
358 ///////////////////////////////////////
360 /// SETUP ACCOUNT POPUP
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',
371 $json = getJSONobj();
372 $encoded_popup_request_data = $json->encode($popup_request_data);
375 ///////////////////////////////////////
378 ${mod_strings['LBL_ACCOUNT_NAME']} <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=''> <input title="{$app_strings['LBL_SELECT_BUTTON_TITLE']}" 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>
383 $jsCalendarImage = SugarThemeRegistry::current()->getImageURL('jscalendar.gif');
385 $lbl_date_closed <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 <span class="required">$lbl_required_symbol</span><br>
388 <select name='{$prefix}sales_stage'>
390 $the_form .= get_select_options_with_id($app_list_strings['sales_stage_dom'], "");
393 $lbl_amount <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">
398 inputField : "{$prefix}jscal_field", daFormat : "$cal_dateformat", ifFormat : "$cal_dateformat", showsTime : false, button : "jscal_trigger", singleClick : true, step : 1, weekNumbers:false
404 require_once('include/QuickSearchDefaults.php');
405 $qsd = QuickSearchDefaults::getQuickSearchDefaults();
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;
413 $javascript = new javascript();
414 $javascript->setFormName($formname);
415 $javascript->setSugarBean(new Opportunity());
416 $javascript->addRequiredFields($prefix);
417 $the_form .=$javascript->getScript();
425 function handleSave($prefix,$redirect=true, $useRequired=false){
426 global $current_user;
429 require_once('include/formbase.php');
431 $focus = new Opportunity();
432 if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))){
436 if(empty($_POST['currency_id'])){
437 $currency_id = $current_user->getPreference('currency');
438 if(isset($currency_id)){
439 $focus->currency_id = $currency_id;
442 $focus = populateFromPost($prefix, $focus);
443 if( !ACLController::checkAccess($focus->module_dir, 'edit', $focus->isOwner($current_user->id))){
444 ACLController::displayNoAccess(true);
446 $check_notify = FALSE;
447 if (isset($GLOBALS['check_notify'])) {
448 $check_notify = $GLOBALS['check_notify'];
451 $focus->save($check_notify);
453 if(!empty($_POST['duplicate_parent_id'])){
454 clone_relationship($focus->db, array('opportunities_contacts'),'opportunity_id', $_POST['duplicate_parent_id'], $focus->id);
456 $return_id = $focus->id;
458 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
460 handleRedirect($return_id,"Opportunities" );