<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://sugarclub.sugarcrm.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Why am I getting HTTP: 500 Internal Server Error when I am trying to use after_retrieve?</title><link>https://sugarclub.sugarcrm.com/dev-club/f/questions-answers/4385/why-am-i-getting-http-500-internal-server-error-when-i-am-trying-to-use-after_retrieve</link><description>I am trying to use logic hook &amp;#39;after_retrieve&amp;#39;, but I keep getting a 500 Internal Server Error when it is trying to load records into the subpanel. 
 
 It happens whether I have something called by after_retrieve or even just set it as an empty array</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Why am I getting HTTP: 500 Internal Server Error when I am trying to use after_retrieve?</title><link>https://sugarclub.sugarcrm.com/thread/21973?ContentTypeID=1</link><pubDate>Thu, 14 Jan 2021 13:43:51 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:b351d2e6-6e1a-4f88-9e9d-2bc4b97cdeab</guid><dc:creator>Rodrigo Manara</dc:creator><description>&lt;p&gt;good eye !!!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why am I getting HTTP: 500 Internal Server Error when I am trying to use after_retrieve?</title><link>https://sugarclub.sugarcrm.com/thread/21963?ContentTypeID=1</link><pubDate>Wed, 13 Jan 2021 21:21:06 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:e5c4f6f0-9535-4b89-88dd-a2499953f513</guid><dc:creator>Francesca Shiekh</dc:creator><description>&lt;p&gt;500 errors are&amp;nbsp;almost always syntax errors, you have an error on line 1, you are opening a curly bracket instead of a square bracket :)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;$hook_array&lt;strong&gt;{&lt;/strong&gt;&amp;#39;after_retrieve&amp;#39;] = Array();&lt;/p&gt;
&lt;p&gt;should be&amp;nbsp;&lt;/p&gt;
&lt;p&gt;$hook_array['after_retrieve'] = Array();&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;FrancescaS&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why am I getting HTTP: 500 Internal Server Error when I am trying to use after_retrieve?</title><link>https://sugarclub.sugarcrm.com/thread/21961?ContentTypeID=1</link><pubDate>Wed, 13 Jan 2021 20:32:58 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:00d12e4b-3c14-4d24-8f55-ec78f8010340</guid><dc:creator>Amy Cox</dc:creator><description>&lt;p&gt;I don&amp;#39;t see anything in the error log, sugarcrm or apache.&amp;nbsp; I now it has nothing to do with the code, because just putting the following in by itself makes it error.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;$hook_array{&amp;#39;after_retrieve&amp;#39;] = Array();&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Course Logic Hook&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="php"&gt;&amp;lt;?php

global $db, $sugar_config;

class Courses_Logic_Hooks {

  //Set field: name
  public function Set_Course_Name(&amp;amp;$focus, $action, $args = array())
  {
    $focus-&amp;gt;name = $focus-&amp;gt;course_name_c;
  }

  //Set field: curriculum_type
  //No longer used, but kept for reference. Now replaced via /custom/extension/lev_courses/vardefs/sugarfield_curriculum_type.php
  public function Set_Curriculum_Type(&amp;amp;$focus, $action, $args = array())
  {
      require_once(&amp;#39;modules/Lev_Course_Catalog/Lev_Course_Catalog.php&amp;#39;);
      $course_catalog = new Lev_Course_Catalog();
      $ctype =$course_catalog-&amp;gt;retrieve($focus-&amp;gt;lev_course_catalog_id_c);
      $focus-&amp;gt;curriculum_type =  preg_replace(&amp;#39;/[_]/&amp;#39;, &amp;#39; &amp;#39;, $ctype-&amp;gt;curriculum_type);

  }

  //Set field: start_date and start_day_of_week
  public function Set_Start_Date(&amp;amp;$focus, $action, $args = array())
  {
      $sql = &amp;lt;&amp;lt;&amp;lt;SQL
SELECT coursedate.class_date AS first_class_date FROM lev_course_class_dates coursedate
      LEFT JOIN lev_courses_lev_course_class_dates_c connect ON coursedate.id=connect.lev_courses_lev_course_class_dateslev_course_class_dates_idb AND coursedate.deleted=0
      LEFT JOIN lev_courses course on connect.lev_courses_lev_course_class_dateslev_courses_ida=course.id and connect.deleted=0
      WHERE course.deleted=0 AND course.id=&amp;#39;{$focus-&amp;gt;id}&amp;#39; ORDER BY coursedate.class_date ASC LIMIT 1
SQL;

      $result = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;query($sql);
      while ($row = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;fetchByAssoc($result))
      {
        $focus-&amp;gt;start_date = $row[&amp;#39;first_class_date&amp;#39;];
        $focus-&amp;gt;start_day_of_week = date(&amp;#39;l&amp;#39;, strtotime($row[&amp;#39;first_class_date&amp;#39;]));
      }
  }

  //Set field: end_date and end_day_of_week
  public function Set_End_Date(&amp;amp;$focus, $action, $args = array())
  {
      $sql = &amp;lt;&amp;lt;&amp;lt;SQL
SELECT coursedate.class_date AS last_class_date FROM lev_course_class_dates coursedate
      LEFT JOIN lev_courses_lev_course_class_dates_c connect ON coursedate.id=connect.lev_courses_lev_course_class_dateslev_course_class_dates_idb AND coursedate.deleted=0
      LEFT JOIN lev_courses course on connect.lev_courses_lev_course_class_dateslev_courses_ida=course.id and connect.deleted=0
      WHERE course.deleted=0 AND course.id=&amp;#39;{$focus-&amp;gt;id}&amp;#39; ORDER BY coursedate.class_date DESC LIMIT 1
SQL;

      $result = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;query($sql);
      while ($row = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;fetchByAssoc($result))
      {
        $focus-&amp;gt;end_date = $row[&amp;#39;last_class_date&amp;#39;];
        $focus-&amp;gt;end_day_of_week = date(&amp;#39;l&amp;#39;, strtotime($row[&amp;#39;last_class_date&amp;#39;]));
      }
  }

  //Set field: academic_period
  public function Set_Academic_Period(&amp;amp;$focus, $action, $args = array())
  {
      if (!empty($focus-&amp;gt;start_date))
      {
        if (date(&amp;#39;Y&amp;#39;, strtotime($focus-&amp;gt;start_date)) &amp;lt; &amp;#39;2013&amp;#39;)
        {
          if (date(&amp;#39;n&amp;#39;, strtotime($focus-&amp;gt;start_date)) &amp;lt; 5)
          {
            $affix = &amp;#39;21&amp;#39;;
          }
          else if (date(&amp;#39;n&amp;#39;, strtotime($focus-&amp;gt;start_date)) &amp;lt; 9)
          {
            $affix = &amp;#39;25&amp;#39;;
          }
          else
          {
            $affix = &amp;#39;29&amp;#39;;
          }
        }
        else
        {
          $affix = &amp;#39;00&amp;#39;;
        }
        $focus-&amp;gt;academic_period = date(&amp;#39;Y&amp;#39;, strtotime($focus-&amp;gt;start_date)).$affix;
      }
  }

  //Set field: total_client_fee
  public function Set_Total_Fee(&amp;amp;$focus, $action, $args = array())
  {
      if ($focus-&amp;gt;all_inclusive_fee == 1)
      {
        $focus-&amp;gt;total_client_fee = $focus-&amp;gt;targeted_fee;
      }
      else
      {
        //Get sum of &amp;quot;Total to Client&amp;quot; field from all linked Course Costs
        $sql = &amp;lt;&amp;lt;&amp;lt;SQL
SELECT sum(cost_c.total_to_client_c) AS total_to_client 
FROM lev_course_costs cost
LEFT JOIN lev_course_costs_cstm cost_c
    ON cost.id = cost_c.id_c and cost.deleted =0
LEFT JOIN lev_courses_lev_course_costs_c connect 
    ON cost.id=connect.lev_courses_lev_course_costslev_course_costs_idb 
    AND cost.deleted=0
 LEFT JOIN lev_courses course 
    ON connect.lev_courses_lev_course_costslev_courses_ida=course.id 
    AND connect.deleted=0
WHERE course.deleted=0 AND course.id=&amp;#39;{$focus-&amp;gt;id}&amp;#39;
SQL;

        $result = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;query($sql);
        while ($row = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;fetchByAssoc($result))
        {
          $total_to_client = $row[&amp;#39;total_to_client&amp;#39;];
        }

        $focus-&amp;gt;total_client_fee = $focus-&amp;gt;targeted_fee + $total_to_client;
      }
  }

  //Set field: total_expense
  public function Set_Total_Course_Expense(&amp;amp;$focus, $action, $args = array())
  {
      //Get sum of &amp;quot;Total to CBT&amp;quot; field from all linked Course Costs
      $sql = &amp;lt;&amp;lt;&amp;lt;SQL
SELECT sum(cost_c.total_expense_c) AS total_cbt 
FROM lev_course_costs cost
LEFT JOIN lev_course_costs_cstm cost_c
    ON cost.id = cost_c.id_c and cost.deleted = 0
LEFT JOIN lev_courses_lev_course_costs_c connect 
    ON cost.id=connect.lev_courses_lev_course_costslev_course_costs_idb 
    AND cost.deleted=0
LEFT JOIN lev_courses course 
    ON connect.lev_courses_lev_course_costslev_courses_ida=course.id 
    AND connect.deleted=0
WHERE course.deleted=0 AND course.id=&amp;#39;{$focus-&amp;gt;id}&amp;#39;
SQL;

      $result = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;query($sql);
      while ($row = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;fetchByAssoc($result))
      {
        $total_cbt = $row[&amp;#39;total_cbt&amp;#39;];
      }

      $focus-&amp;gt;total_expense = ((
      (!empty($focus-&amp;gt;contact_hours) ? $focus-&amp;gt;contact_hours : 0) 
      + 
      (!empty($focus-&amp;gt;prep_hours) ? $focus-&amp;gt;prep_hours : 0)
      ) * 
      (!empty($focus-&amp;gt;hourly_fee_to_instructor) ? $focus-&amp;gt;hourly_fee_to_instructor : 0) 
      ) + (
      (!empty($focus-&amp;gt;lunch_vouchers) ? $focus-&amp;gt;lunch_vouchers : 0) 
      * 
      (!empty($focus-&amp;gt;lunch_voucher_amt) ? $focus-&amp;gt;lunch_voucher_amt : 0)
      ) + 
      (!empty($total_cbt) ? $total_cbt : 0);
  }

  //Set field: course_net
  public function Set_Course_Net(&amp;amp;$focus, $action, $args = array())
  {
      $focus-&amp;gt;course_net = $focus-&amp;gt;total_client_fee - $focus-&amp;gt;total_expense;
  }


  //Set field: gross_margin
  public function Set_Gross_Margin_Percent(&amp;amp;$focus, $action, $args = array())
  {
      if($focus-&amp;gt;total_client_fee == &amp;#39;&amp;#39; || $focus-&amp;gt;total_client_fee == 0){
          $focus-&amp;gt;gross_margin = 0;
      } else {
          $focus-&amp;gt;gross_margin = $focus-&amp;gt;course_net / $focus-&amp;gt;total_client_fee * 100;
      }
  }

  //Set a request variable for contract id (lev_contracts_lev_courseslev_contracts_ida)
  public function Set_Request_ContractID(&amp;amp;$focus, $action, $args = array())
  {
      //Get sum of &amp;quot;Total to CBT&amp;quot; field from all linked Course Costs
      $sql = &amp;lt;&amp;lt;&amp;lt;SQL
SELECT  account_id_c, accounts.name as company_name FROM lev_contracts
INNER JOIN accounts on accounts.id = lev_contracts.account_id_c
      WHERE lev_contracts.deleted=0 AND lev_contracts.id=&amp;#39;{$focus-&amp;gt;lev_contracts_lev_courseslev_contracts_ida}&amp;#39;
SQL;
      $result = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;query($sql);
      while ($row = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;fetchByAssoc($result))
      {
        $_POST[&amp;#39;lev_contract_name&amp;#39;] = $row[&amp;#39;company_name&amp;#39;];
      }
  }



          function setAlertField($bean, $event, $arguments)
        {
            $sql = &amp;lt;&amp;lt;&amp;lt;SQL
            SELECT contract.contract_status AS status FROM lev_contracts contract
                  LEFT JOIN lev_contracts_lev_courses_c connect ON contract.id=connect.lev_contracts_lev_courseslev_contracts_ida AND contract.deleted=0 AND (contract.contract_status = &amp;#39;Proposal_Active&amp;#39; OR contract.contract_status = &amp;#39;Proposal_Rejected&amp;#39;)
                  LEFT JOIN lev_courses course on connect.lev_contracts_lev_courseslev_courses_idb=course.id and connect.deleted=0
                  WHERE course.deleted=0 AND course.id=&amp;#39;{$bean-&amp;gt;id}&amp;#39;
SQL;
                  $result = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;query($sql);
                  $bean-&amp;gt;alertfield_c = &amp;#39;&amp;#39;;
                  while ($row = $GLOBALS[&amp;#39;db&amp;#39;]-&amp;gt;fetchByAssoc($result))
                  {
                    $bean-&amp;gt;alertfield_c = &amp;#39;&amp;lt;pre style=&amp;quot;border:2px solid #c00; padding: 5px;margin: 5px; color:#0000CC; text-align: center; width: 100%; text-decoration: bold;&amp;quot;&amp;gt; COURSE HAS BEEN BILLED - PLEASE BE CAUTIOUS WHEN MAKING CHANGES&amp;lt;/pre&amp;gt;&amp;#39;;
                  }
        }


  //check for completed courses and change CSS
  public function Completed_Course($bean, $action, $args = array()){
    //fire on detail view and edit view
    if (($_REQUEST[&amp;#39;action&amp;#39;] == &amp;#39;DetailView&amp;#39; || $_REQUEST[&amp;#39;action&amp;#39;] == &amp;#39;EditView&amp;#39; &amp;amp;&amp;amp; $_REQUEST[&amp;#39;module&amp;#39;] == &amp;#39;Lev_Courses&amp;#39;) &amp;amp;&amp;amp; $_POST[&amp;#39;lev_course_completed&amp;#39;] == &amp;#39;yes&amp;#39;)
    {
        echo &amp;#39;&amp;lt;script src=&amp;quot;custom/Resources/javascript/Lev_Courses/completedCourse.js&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&amp;#39;;
    }


   }

  //save parent contract to finish the calculations and auto populate fields
    public function saveParentCourse($bean, $event, $args){
        //check if any total field is updated
        if(($bean-&amp;gt;total_client_fee !=$bean-&amp;gt;fetched_row[&amp;#39;total_client_fee&amp;#39;] ) || ($bean-&amp;gt;total_expense !=$bean-&amp;gt;fetched_row[&amp;#39;total_expense&amp;#39;] ) ){
            $parent_contract = $bean-&amp;gt;get_linked_beans(&amp;#39;lev_contracts_lev_courses&amp;#39;,&amp;#39;Lev_Contracts&amp;#39;);

            if($parent_contract[0]-&amp;gt;id !=&amp;#39;&amp;#39;){

                    require_once(&amp;#39;modules/Lev_Contracts/Lev_Contracts.php&amp;#39;);
                    $lev_contract = new Lev_Contracts();
                    $lev_contract-&amp;gt;retrieve($parent_contract[0]-&amp;gt;id);
                    $lev_contract-&amp;gt;save();
                    //$parent_contract = $lev_course-&amp;gt;get_linked_beans(&amp;#39;lev_courses_lev_course_costs&amp;#39;,&amp;#39;Lev_Course_Costs&amp;#39;);

            }

        }

    }

  //calculate all totals and date after related bean is deleted
  public function update_Course_Data($bean, $event, $args){
      //save bean to calauclate all auto populated fields
      $bean-&amp;gt;save();
  }



    function Check_Hourly_Fee(&amp;amp;$bean, $event, $arguments)
    {

        $result = true;
        if (!empty($bean-&amp;gt;contact_id_c))
        {
            $getInstructor = new Contact();
            $getInstructor-&amp;gt;retrieve($bean-&amp;gt;contact_id_c);

	    if (!empty($getInstructor-&amp;gt;instructor_pay_rate_max_c))  // Added this check because it was returning false when there was no max pay rate set - 9/5/2012 Adam Canova
	    {
                if ($bean-&amp;gt;hourly_fee_to_instructor &amp;gt; $getInstructor-&amp;gt;instructor_pay_rate_max_c)
                {
                    $result = false;
                }
	    }
        }


        if($result) {
            return true; //let save proceed as normal
        }

        $_SESSION[&amp;#39;myError&amp;#39;] = &amp;#39;Hourly Fee to Instructor must not exceed the Instructor\&amp;#39;s Maximum Pay Rate&amp;#39;;
        //build the URL to return to
        $module = $_REQUEST[&amp;#39;module&amp;#39;];
        $action = &amp;quot;&amp;amp;action=EditView&amp;quot;; //we need to have EditView b/c that is the action we are taking
        $returnModule = &amp;quot;&amp;amp;return_module=&amp;quot; . $_REQUEST[&amp;#39;return_module&amp;#39;];

        $offset = $_REQUEST[&amp;#39;offset&amp;#39;];
        if ($offset == &amp;quot;&amp;quot;) {
        } else {
            $offset = &amp;quot;&amp;amp;offset=$offset&amp;quot;;
        }

        $stamp = $_REQUEST[&amp;#39;stamp&amp;#39;];
        if ($stamp == &amp;quot;&amp;quot;) {
        } else {
            $stamp = &amp;quot;&amp;amp;stamp=$stamp&amp;quot;;
        }

        if ($recordId == &amp;quot;&amp;quot;) {
            $returnAction = &amp;quot;&amp;amp;return_action=DetailView&amp;quot;;
        } else {
            $recordId = &amp;quot;&amp;amp;record=&amp;quot; . $recordId;
        }

        $url = &amp;quot;index.php?module=&amp;quot; . $module . $offset . $stamp . $action . $returnModule . $returnAction . $recordId;
        header(&amp;quot;Location: $url&amp;quot;);
        $_SESSION[&amp;#39;Lev_CoursesBean&amp;#39;] = $bean; //store the bean in session so we can retrieve the values later
        exit; //goto the location contained in header
    }

    function Check_Course_Status(&amp;amp;$bean, $event, $arguments)
    {

    }
}

?&amp;gt;
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why am I getting HTTP: 500 Internal Server Error when I am trying to use after_retrieve?</title><link>https://sugarclub.sugarcrm.com/thread/21959?ContentTypeID=1</link><pubDate>Wed, 13 Jan 2021 18:52:55 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:5488b2b1-8e92-48d2-93ef-b1af6bd8fbc4</guid><dc:creator>Andr&amp;#233; Lopes</dc:creator><description>&lt;p&gt;Do you see any error into either apache or php error_log?&lt;/p&gt;
&lt;p&gt;Can you share content of your after_retrieve&amp;nbsp;logic hook?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>