function display_html()
 {
     $r = "";
     //---Html
     $array = $this->load();
     if (is_array($array) == false || empty($array)) {
         return 0;
     }
     $r .= '<table class="result" style="width:100%">';
     $ix = 0;
     $prev = 'xx';
     $idx = 0;
     $tot_deb = $tot_cred = 0;
     bcscale(2);
     foreach ($array as $row) {
         if ($prev != $row['po_name']) {
             if ($ix > 0) {
                 $r .= '<tr>';
                 $tot_solde = bcsub($tot_cred, $tot_deb);
                 $sign = " " . ($tot_solde > 0) ? 'C' : 'D';
                 $r .= td('') . td('') . td('');
                 $r .= td('') . td('') . td('') . td('') . td('') . td(nbm($tot_deb), ' class="num"') . td(nbm($tot_cred), ' class="num"') . td(nbm($tot_solde) . $sign, ' class="num notice"');
             }
             $r .= '<tr>' . '<td colspan="7" style="width:auto">' . '<h2>' . h($row['po_name'] . ' ' . $row['po_description']) . '</td></tr>';
             $r .= '<tr>' . '<th>' . '</th>' . '<th>' . _('Date') . '</th>' . '<th>' . _('Poste') . '</th>' . '<th>' . _('Quick_code') . '</th>' . '<th>' . _('Libellé') . '</th>' . '<th>' . '</th>' . '<th>' . _('Pièce') . '</th>' . '<th>' . _('Interne') . '</th>' . '<th style="text-align:right">' . _('Débit') . '</th>' . '<th style="text-align:right">' . _('Crédit') . '</th>' . '<th style="text-align:right">' . _('Prog.') . '</th>' . '</tr>';
             $tot_deb = $tot_cred = 0;
             $prev = $row['po_name'];
             $ix++;
         }
         $class = $idx % 2 == 0 ? 'even' : 'odd';
         $idx++;
         $r .= '<tr class="' . $class . '">';
         $detail = $row['jr_id'] != null ? HtmlInput::detail_op($row['jr_id'], $row['jr_internal']) : '';
         $post_detail = $row['j_poste'] != null ? HtmlInput::history_account($row['j_poste'], $row['j_poste']) : '';
         $card_detail = $row['f_id'] != null ? HtmlInput::history_card($row['f_id'], $row['qcode']) : '';
         $amount_deb = $row['oa_debit'] == 't' ? $row['oa_amount'] : 0;
         $amount_cred = $row['oa_debit'] == 'f' ? $row['oa_amount'] : 0;
         $tot_deb = bcadd($tot_deb, $amount_deb);
         $tot_cred = bcadd($tot_cred, $amount_cred);
         $tot_solde = bcsub($tot_cred, $tot_deb);
         /*
          * Checked button
          */
         $str_ck = "";
         $str_document = "";
         if ($row['jr_id'] != null) {
             /*
              * Get receipt info  
              */
             $str_document = HtmlInput::show_receipt_document($row['jr_id']);
             if ($str_document != "") {
                 $ck = new ICheckBox('ck[]', $row['jr_id']);
                 $str_ck = $ck->input();
             }
         }
         $r .= '<td>' . $str_ck . '</td>' . '<td>' . $row['oa_date'] . '</td>' . td($post_detail) . td($card_detail) . td($row['jr_comment']) . '<td>' . $str_document . '</td>' . td($row['jr_pj_number']) . '<td>' . $detail . '</td>' . '<td class="num">' . nbm($amount_deb) . '</td>' . '<td class="num">' . nbm($amount_cred) . '</td>' . '<td class="num">' . nbm($tot_solde) . '</td>';
         $r .= '</tr>';
     }
     $r .= '<tr>';
     $tot_solde = bcsub($tot_cred, $tot_deb);
     $sign = $tot_solde > 0 ? 'C' : 'D';
     $r .= td('') . td('') . td('');
     $r .= td('') . td('') . td('') . td('') . td('') . td(nbm($tot_deb), ' class="num"') . td(nbm($tot_cred), ' class="num"') . td(nbm($tot_solde) . $sign, '  class="num notice"');
     $r .= '</table>';
     return $r;
 }
 /**
 * @brief Show all the operation
 * @param$sql is the sql stmt, normally created by build_search_sql
 * @param$offset the offset
 * @param$p_paid if we want to see info about payment
  \code
  // Example
  // Build the sql
  list($sql,$where)=$Ledger->build_search_sql($_GET);
  // Count nb of line
  $max_line=$cn->count_sql($sql);
 
  $step=$_SESSION['g_pagesize'];
  $page=(isset($_GET['offset']))?$_GET['page']:1;
  $offset=(isset($_GET['offset']))?$_GET['offset']:0;
  // create the nav. bar
  $bar=navigation_bar($offset,$max_line,$step,$page);
  // show a part
  list($count,$html)= $Ledger->list_operation($sql,$offset,0);
  echo $html;
  // show nav bar
  echo $bar;
 
  \endcode
 * \see build_search_sql
 * \see display_search_form
 * \see search_form
 
 * \return HTML string
 */
 public function list_operation($sql, $offset, $p_paid = 0)
 {
     global $g_parameter, $g_user;
     bcscale(2);
     $table = new Sort_Table();
     $gDossier = dossier::id();
     $amount_paid = 0.0;
     $amount_unpaid = 0.0;
     $limit = $_SESSION['g_pagesize'] != -1 ? " LIMIT " . $_SESSION['g_pagesize'] : "";
     $offset = $_SESSION['g_pagesize'] != -1 ? " OFFSET " . Database::escape_string($offset) : "";
     $order = "  order by jr_date_order asc,jr_internal asc";
     // Sort
     $url = "?" . CleanUrl();
     $str_dossier = dossier::get();
     $table->add(_("Date"), $url, 'order by jr_date asc,substring(jr_pj_number,\'[0-9]+$\')::numeric asc', 'order by  jr_date desc,substring(jr_pj_number,\'[0-9]+$\')::numeric desc', "da", "dd");
     $table->add(_('Echeance'), $url, " order by  jr_ech asc", " order by  jr_ech desc", 'ea', 'ed');
     $table->add(_('Paiement'), $url, " order by  jr_date_paid asc", " order by  jr_date_paid desc", 'eap', 'edp');
     $table->add(_('Pièce'), $url, ' order by  substring(jr_pj_number,\'[0-9]+$\')::numeric asc ', ' order by  substring(jr_pj_number,\'[0-9]+$\')::numeric desc ', "pja", "pjd");
     $table->add(_('Tiers'), $url, " order by  name asc", " order by  name desc", 'na', 'nd');
     $table->add(_('Montant'), $url, " order by jr_montant asc", " order by jr_montant desc", "ma", "md");
     $table->add(_("Description"), $url, "order by jr_comment asc", "order by jr_comment desc", "ca", "cd");
     $ord = !isset($_GET['ord']) ? 'da' : $_GET['ord'];
     $order = $table->get_sql_order($ord);
     // Count
     $count = $this->db->count_sql($sql);
     // Add the limit
     $sql .= $order . $limit . $offset;
     // Execute SQL stmt
     $Res = $this->db->exec_sql($sql);
     //starting from here we can refactor, so that instead of returning the generated HTML,
     //this function returns a tree structure.
     $r = "";
     $Max = Database::num_row($Res);
     if ($Max == 0) {
         return array(0, _("Aucun enregistrement trouvé"));
     }
     $r .= '<table class="result">';
     $r .= "<tr >";
     $r .= "<th>" . _("n° interne") . "</th>";
     if ($this->type == 'ALL') {
         $r .= th('Journal');
     }
     $r .= '<th>' . $table->get_header(0) . '</th>';
     if ($p_paid != 0) {
         $r .= '<th>' . $table->get_header(1) . '</td>';
     }
     if ($p_paid != 0) {
         $r .= '<th>' . $table->get_header(2) . '</th>';
     }
     $r .= '<th>' . $table->get_header(3) . '</th>';
     $r .= '<th>' . $table->get_header(4) . '</th>';
     $r .= '<th>' . $table->get_header(6) . '</th>';
     $r .= th('Notes', ' style="width:15%"');
     $r .= '<th>' . $table->get_header(5) . '</th>';
     // if $p_paid is not equal to 0 then we have a paid column
     if ($p_paid != 0) {
         $r .= "<th> " . _('Payé') . "</th>";
     }
     $r .= "<th>" . _('Concerne') . "</th>";
     $r .= "<th>" . _('Document') . "</th>";
     $r .= "</tr>";
     // Total Amount
     $tot = 0.0;
     $gDossier = dossier::id();
     for ($i = 0; $i < $Max; $i++) {
         $row = Database::fetch_array($Res, $i);
         if ($i % 2 == 0) {
             $tr = '<TR class="odd">';
         } else {
             $tr = '<TR class="even">';
         }
         $r .= $tr;
         //internal code
         // button  modify
         $r .= "<TD>";
         // If url contains
         //
         $href = basename($_SERVER['PHP_SELF']);
         $r .= sprintf('<A class="detail" style="text-decoration:underline" HREF="javascript:modifyOperation(\'%s\',\'%s\')" >%s </A>', $row['jr_id'], $gDossier, $row['jr_internal']);
         $r .= "</TD>";
         if ($this->type == 'ALL') {
             $r .= td($row['jrn_def_name']);
         }
         // date
         $r .= "<TD>";
         $r .= $row['str_jr_date'];
         $r .= "</TD>";
         // echeance
         if ($p_paid != 0) {
             $r .= "<TD>";
             $r .= $row['str_jr_ech'];
             $r .= "</TD>";
             $r .= "<TD>";
             $r .= $row['str_jr_date_paid'];
             $r .= "</TD>";
         }
         // pj
         $r .= "<TD>";
         $r .= $row['jr_pj_number'];
         $r .= "</TD>";
         // Tiers
         $other = $row['quick_code'] != '' ? '[' . $row['quick_code'] . '] ' . $row['name'] . ' ' . $row['first_name'] : '';
         $r .= td($other);
         // comment
         $r .= "<TD>";
         $tmp_jr_comment = h($row['jr_comment']);
         $r .= $tmp_jr_comment;
         $r .= "</TD>";
         $r .= td(h($row['n_text']), ' style="font-size:0.87em%"');
         // Amount
         // If the ledger is financial :
         // the credit must be negative and written in red
         $positive = 0;
         // Check ledger type :
         if ($row['jrn_def_type'] == 'FIN') {
             $positive = $this->db->get_value("select qf_amount from quant_fin where jr_id=\$1", array($row['jr_id']));
             if ($this->db->count() != 0) {
                 $positive = $positive < 0 ? 1 : 0;
             }
         }
         $r .= "<TD align=\"right\">";
         $t_amount = $row['jr_montant'];
         if ($row['total_invoice'] != null && $row['total_invoice'] != $row['jr_montant']) {
             $t_amount = $row['total_invoice'];
         }
         $tot = $positive != 0 ? bcsub($tot, $t_amount) : bcadd($tot, $t_amount);
         //STAN $positive always == 0
         if ($row['jrn_def_type'] == 'FIN') {
             $r .= $positive != 0 ? "<font color=\"red\">  - " . nbm($t_amount) . "</font>" : nbm($t_amount);
         } else {
             $r .= $t_amount < 0 ? "<font color=\"red\">  " . nbm($t_amount) . "</font>" : nbm($t_amount);
         }
         $r .= "</TD>";
         // Show the paid column if p_paid is not null
         if ($p_paid != 0) {
             $w = new ICheckBox();
             $w->name = "rd_paid" . $row['jr_id'];
             $w->selected = $row['jr_rapt'] == 'paid' ? true : false;
             // if p_paid == 2 then readonly
             $w->readonly = $p_paid == 2 ? true : false;
             $h = new IHidden();
             $h->name = "set_jr_id" . $row['jr_id'];
             $r .= '<TD>' . $w->input() . $h->input() . '</TD>';
             if ($row['jr_rapt'] == 'paid') {
                 $amount_paid = bcadd($amount_paid, $t_amount);
             } else {
                 $amount_unpaid = bcadd($amount_unpaid, $t_amount);
             }
         }
         // Rapprochement
         $rec = new Acc_Reconciliation($this->db);
         $rec->set_jr_id($row['jr_id']);
         $a = $rec->get();
         $r .= "<TD>";
         if ($a != null) {
             foreach ($a as $key => $element) {
                 $operation = new Acc_Operation($this->db);
                 $operation->jr_id = $element;
                 $l_amount = $this->db->get_value("select jr_montant from jrn " . " where jr_id={$element}");
                 $r .= "<A class=\"detail\" HREF=\"javascript:modifyOperation('" . $element . "'," . $gDossier . ")\" > " . $operation->get_internal() . "[" . nbm($l_amount) . "]</A>";
             }
             //for
         }
         // if ( $a != null ) {
         $r .= "</TD>";
         if ($row['jr_valid'] == 'f') {
             $r .= "<TD>" . _("Opération annulée") . "</TD>";
         } else {
         }
         // else
         //document
         if ($row['jr_pj_name'] != "") {
             $r .= '<td>' . HtmlInput::show_receipt_document($row['jr_id']) . '</td>';
         } else {
             $r .= "<TD></TD>";
         }
         // end row
         $r .= "</tr>";
     }
     $amount_paid = round($amount_paid, 4);
     $amount_unpaid = round($amount_unpaid, 4);
     $tot = round($tot, 4);
     $r .= "<TR>";
     $r .= '<TD COLSPAN="5">Total</TD>';
     $r .= '<TD ALIGN="RIGHT">' . nbm($tot) . "</TD>";
     $r .= "</tr>";
     if ($p_paid != 0) {
         $r .= "<TR>";
         $r .= '<TD COLSPAN="5">' . _("Payé") . '</TD>';
         $r .= '<TD ALIGN="RIGHT">' . nbm($amount_paid) . "</TD>";
         $r .= "</tr>";
         $r .= "<TR>";
         $r .= '<TD COLSPAN="5">' . _("Non payé") . '</TD>';
         $r .= '<TD ALIGN="RIGHT">' . nbm($amount_unpaid) . "</TD>";
         $r .= "</tr>";
     }
     $r .= "</table>";
     return array($count, $r);
 }