0
\$\begingroup\$

enter image description here I pass a function into the variable ($monthly_receivable) to get the value of each months to pass and get the result of a function.. but the process is dumb slow. This was the only January data but it can add a month range example January 2016 to December 2019.

When I try to remove that variable monthly_receivable it was smooth.

This is my code from server-side to view (client-side):

Controller

public function notes_receivable_summary($start_date, $end_date) {
$loans_list = $this->db->query("SELECT 
                                borr_name,
                                    co_borrower, 
                                    released_date, 
                                    due_from, 
                                    due_to, 
                                    pn, 
                                    no_months, 
                                    loan_ref, 
                                    loan_id, 
                                    pn 
                                FROM v_borrowers_nr dd
                                WHERE (df between '$start_date' AND '$end_date')
                                ORDER BY loan_ref")->result();
$months     = $this->db->query("SELECT dd FROM v_months_nr WHERE dd between '$start_date' and '$end_date'")->result();

$data['monthly_receivable'] = function($date, $loan_ref, $loan_id){
        $enc_url = explode('|', $this->Main_model->encdec($this->uri->segment(2), 'd'));
        $s_date  = $enc_url[1];
        $e_date  = $enc_url[2];
        $sd      = date('Y-m-d', strtotime('-1 months', strtotime($s_date)));
        $ed      = $e_date;

  $q             = $this->db->query("SELECT * FROM f_monthly_rcvble('$loan_ref', $loan_id, '$start_date', '$end_date', '$date')")->row();
    return $q;
};
$this->load->view('pages/ajax/reports/sample_nr', $data);
}

View

<table id="displayTableNR" class="table displayTableNR js-sort-table">
  <?php $month = array(1=>'Jan', 2=>'Feb', 3=>'Mar', 4=>'Apr', 5=>'May', 6=>'Jun', 7=>'Jul', 8=>'Aug', 9=>'Sep', 10=>'Oct', 11=>'Nov', 12=>'Dec'); ?>
  <?php $total_as_of_h = 0; ?>
  <?php $range_date    = !empty($months) ? date('M Y', strtotime($months[0]->dd)) . ' - ' . date('M Y', strtotime($months[count($months) - 1]->dd)) : null; ?>
  <thead>
      <tr class="menu1">
          <th class="table-head b-right" scope="col">Ref. No.</th>
          <th class="table-head b-right js-sort-string fixed-side" scope="col">Name</th>
          <th class="table-head b-right fixed-side" scope="col">Co-Borrower</th>
          <th class="table-head b-right fixed-side" scope="col"><div class="wd-132px">Release Date</div></th>
          <th class="table-head b-right js-sort-date fixed-side" scope="col">From</th>
          <th class="table-head b-right js-sort-date fixed-side" scope="col">To</th>
          <th class="table-head b-right fixed-side text-right" scope="col">PN</th>
          <th class="table-head fixed-side" scope="col">Terms</th>

          <?php $count = 0; ?>
          <?php foreach ($months as $row): ?>
              <?php $d = explode('-', $row->dd); ?>
              <th class="table-head text-center font-strong <?php echo $count % 2 === 0 ? 'hexa-color-1' : 'hexa-color-2'; ?>" colspan="10"><?php echo strtoupper($month[(int) $d[1]]) . ' ' . $d[0]; ?></th>
          <?php $count++; ?>
          <?php $total_as_of_h++; ?>
          <?php endforeach; ?>

          <?php if ($total_as_of_h === count($months)): ?>
              <th class="td-head text-center" colspan="3">COLLECTION ACTUAL/CLOSED OB/EFP</th>
          <?php endif; ?>
      </tr>
      <tr class="menu2">
          <th class="table-head b-right fixed-side"></th>
          <th class="table-head b-right fixed-side"></th>
          <th class="table-head b-right fixed-side"></th>
          <th class="table-head b-right fixed-side"></th>
          <th class="table-head b-right fixed-side"></th>
          <th class="table-head b-right fixed-side"></th>
          <th class="table-head b-right fixed-side"></th>
          <th class="table-head fixed-side"></th>

          <?php $count = 0; ?>
          <?php foreach ($months as $row): ?>
              <th class="table-head font-strong <?php echo $count % 2 === 0 ? 'hexa-color-1' : 'hexa-color-2'; ?>"><div class="wd-132px text-center">Current Target</div></th>
              <th class="table-head font-strong amt_pd <?php echo $count % 2 === 0 ? 'hexa-color-1' : 'hexa-color-2'; ?>"><div class="wd-132px text-center">Actual Collection</div></th>
              <th class="table-head font-strong <?php echo $count % 2 == 0 ? 'hexa-color-1' : 'hexa-color-2'; ?>"><div class="wd-132px text-center">UA/SP</div></th>
              <th class="table-head font-strong <?php echo $count % 2 == 0 ? 'hexa-color-1' : 'hexa-color-2'; ?>"><div class="wd-132px text-center">Past Due Target UA/SP</div></th>
              <th class="table-head font-strong <?php echo $count % 2 == 0 ? 'hexa-color-1' : 'hexa-color-2'; ?>"><div class="wd-132px text-center">Actual Collection UA/SP</div></th>
              <th class="table-head font-strong <?php echo $count % 2 == 0 ? 'hexa-color-1' : 'hexa-color-2'; ?>"><div class="wd-132px text-center">Past Due Balance UA/SP</div></th>
              <th class="table-head font-strong <?php echo $count % 2 == 0 ? 'hexa-color-1' : 'hexa-color-2'; ?>"><div class="wd-132px text-center">Advanced Payment</div></th>
              <th class="table-head font-strong <?php echo $count % 2 == 0 ? 'hexa-color-1' : 'hexa-color-2'; ?>"><div class="wd-132px text-center">OB Closed</div></th>
              <th class="table-head font-strong <?php echo $count % 2 == 0 ? 'hexa-color-1' : 'hexa-color-2'; ?>"><div class="wd-132px text-center">Early Full Payments</div></th>
              <th class="table-head font-strong <?php echo $count % 2 == 0 ? 'hexa-color-1' : 'hexa-color-2'; ?>"><div class="wd-132px text-center">Adjustments</div></th>
          <?php $count++; ?>
          <?php endforeach; ?>

          <?php if ($total_as_of_h === count($months)): ?>
              <th class="table-head">TOTAL</th>
              <th class="table-head"><div class="wd-118px text-center">(<?php echo $range_date; ?>)</div></th>
              <th class="table-head">NR BAL</th>
          <?php endif; ?>
      </tr>
  </thead>
  <tbody>

  <?php $totAmntToPay             = 0; ?>            
  <?php $totCollection            = 0; ?>            
  <?php $totBalance               = 0; ?>            

  <?php foreach($loans_list as $r): ?>
  <?php $name   = explode('|', $r->borr_name); ?>
  <tr class="">
    <td class="td-border fixed-side"><?php echo $r->loan_ref; ?></td>
    <?php if (count($name) < 3): ?>
      <td class="td-border br-name-nr fixed-side"><div class="wd-210px"><?php echo strtoupper($name[0]) . ', ' . strtoupper($name[1]); ?></div></td>
    <?php else: ?>
      <td class="td-border br-name-nr fixed-side"><div class="wd-210px"><?php echo strtoupper($name[0]) . ', ' . strtoupper($name[1]) . ' ' . strtoupper($name[2]); ?></div></td>
    <?php endif; ?>
    <td class="td-border fixed-side"><div class="wd-145px"><?php echo strtoupper($r->co_borrower); ?></div></td>
    <td class="td-border fixed-side"><?php echo date('Y-m-d', strtotime($r->released_date)); ?></td>
    <td class="td-border fixed-side"><?php echo date('Y-m-d', strtotime($r->due_from)); ?></td>
    <td class="td-border fixed-side"><?php echo date('Y-m-d', strtotime($r->due_to)); ?></td>
    <td class="td-border text-right fixed-side"><?php echo number_format($r->pn, 2); ?></td>
    <td class="td-border fixed-side"><?php echo number_format($r->no_months, 0); ?></td>

    <!-- TOTALS VARIABLE -->
    <?php $ct1                   = 0; ?>
    <?php $curr_or_prev_uasp     = 0; ?>
    <?php $tot_prev_uasp_balance = 0; ?>

    <?php foreach($months as $row): ?>
    <?php $lref = $r->loan_ref; ?>
    <?php $lid  = $r->loan_id; ?>
    <?php $nr                     = $monthly_receivable($row->dd, $lref, $lid); ?>
      <?php if (is_object($nr)): ?>
        <td class="td-border text-right"><?php echo number_format($nr->amount_due, 2); ?></td>
        <td class="td-border text-right"><?php echo number_format($nr->actual_collection, 2); ?></td>
          <?php if ($ct1 == 0): ?>
            <td class="td-border text-right"></td>
          <?php else: ?>  
            <td class="td-border text-right"><?php echo number_format($nr->col_ua_sp, 2); ?></td>
          <?php endif; ?>
        <td class="td-border text-right"><?php echo number_format($nr->past_due_target_ua_sp, 2); //Past Due Target UA/SP ?></td>
        <td class="td-border text-right"><a href="#" class="clrd-tooltip" data-toggle="tooltip" data-placement="right" title=""><?php echo number_format($nr->past_due_collection_tot_ua_sp, 2); ?></a></td>
        <td class="td-border text-right"><?php echo number_format($nr->past_due_balance, 2); //Past Due Target Balance ?></td>
        <td class="td-border text-right"><a href="#" class="clrd-tooltip" data-toggle="tooltip" data-placement="right" title=""><?php echo number_format($nr->advanced_payment, 2); ?></a></td>
        <td class="td-border text-right"><?php echo number_format($nr->ob_closed, 2); ?></td>
        <td class="td-border text-right"><?php echo number_format($nr->early_full_payments, 2); ?></td>
        <td class="td-border text-right"><?php echo number_format($nr->adjustments, 2); ?></td>
      <?php endif; ?>
    <!-- TOTALS -->
    <?php $ct1++; ?> 
    <?php endforeach; ?>
    <td class="td-border text-right total-to-pay"><?php echo number_format($col_tot_pn, 2); ?></td>
    <td class="td-border text-right total-coll"><?php echo number_format($col_tot_col, 2); ?></td>
    <td class="td-border text-right total-bal"><?php echo number_format($col_tot_pn - $col_tot_col, 2); ?></td>
  </tr>
</tbody>
<tfoot>
    <tr>
        <th class="table-head b-right fixed-side">Ref. No.</th>
        <th class="table-head b-right fixed-side">Name</th>
        <th class="table-head b-right fixed-side">Co-Borrower</th>
        <th class="table-head b-right fixed-side">Release Date</th>
        <th class="table-head b-right fixed-side">From</th>
        <th class="table-head b-right fixed-side">To</th>
        <th class="table-head b-right text-right fixed-side"><?php echo number_format($totPN, 2); ?></th>
        <th class="table-head fixed-side">Terms</th>

        <?php if ($total_as_of_h === count($months)): ?>
            <td class="table-head text-right"><?php echo number_format($totAmntToPay, 2); ?></td>
            <td class="table-head text-right"><?php echo number_format($totCollection, 2); ?></td>
            <td class="table-head text-right"><?php echo number_format($totBalance, 2); ?></td>
        <?php endif; ?>
    </tr>
  </tfoot>
</table>


enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review! If you look at the guidelines at How to ask a good question?, you will see that it's recommended to state what your code does not your main concerns about it in the title. \$\endgroup\$ – AlexV Jun 24 at 9:26
  • \$\begingroup\$ How are the $loans_list and $months result sets magically arriving in the view? \$\endgroup\$ – mickmackusa Jun 24 at 11:41
  • \$\begingroup\$ Sorry i forgot to input $data['months'] and $data['loans_list'] \$\endgroup\$ – don pentavia Jun 25 at 8:09
  • \$\begingroup\$ $month is for the month displaying on the top of each column data. Like Jan, Feb etc. \$\endgroup\$ – don pentavia Jun 25 at 15:33
  • \$\begingroup\$ Your view load is incorrect and makes your script "broken". Are there any other anomalies? \$\endgroup\$ – mickmackusa Jun 26 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.