Beispiel #1
0
  <?php 
        } else {
            ?>
  <td></td>
  <td style="text-align:right"> <?php 
            echo nb($this->content[$i]['j_montant']);
            ?>
 </td>
  <?php 
        }
        ?>
<td style="text-align:center">
<?php 
        // Rapprochement
        $rec = new Acc_Reconciliation($this->db);
        $rec->set_jr_id($this->content[$i]['jr_id']);
        $a = $rec->get();
        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}");
                echo "<A class=\"detail\" HREF=\"javascript:viewOperation('" . $element . "'," . $gDossier . ")\" > " . $operation->get_internal() . " [ {$l_amount} &euro; ]</A>";
            }
            //for
        }
        // if ( $a != null ) {
        ?>
</td>

</tr>
 /**
 * @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);
 }
 public function insert($p_array = null)
 {
     global $g_parameter;
     extract($p_array);
     $this->verify($p_array);
     $group = $this->db->get_next_seq("s_oa_group");
     /* for analytic */
     $seq = $this->db->get_next_seq('s_grpt');
     $this->id = $p_jrn;
     $internal = $this->compute_internal_code($seq);
     $this->internal = $internal;
     $oPeriode = new Periode($this->db);
     $check_periode = $this->check_periode();
     if ($check_periode == true) {
         $tperiode = $period;
     } else {
         $tperiode = $oPeriode->find_periode($e_date);
     }
     $cust = new Fiche($this->db);
     $cust->get_by_qcode($e_client);
     $sposte = $cust->strAttribut(ATTR_DEF_ACCOUNT);
     // if 2 accounts, take only the debit one for the customer
     //
     if (strpos($sposte, ',') != 0) {
         $array = explode(',', $sposte);
         $poste = $array[0];
     } else {
         $poste = $sposte;
     }
     bcscale(4);
     try {
         $tot_amount = 0;
         $tot_tva = 0;
         $tot_debit = 0;
         $this->db->start();
         $tva = array();
         /* Save all the items without vat */
         for ($i = 0; $i < $nb_item; $i++) {
             $n_both = 0;
             if (strlen(trim(${'e_march' . $i})) == 0) {
                 continue;
             }
             /* First we save all the items without vat */
             $fiche = new Fiche($this->db);
             $fiche->get_by_qcode(${"e_march" . $i});
             $amount = bcmul(${'e_march' . $i . '_price'}, ${'e_quant' . $i});
             $tot_amount = round(bcadd($tot_amount, $amount), 2);
             $acc_operation = new Acc_Operation($this->db);
             $acc_operation->date = $e_date;
             $sposte = $fiche->strAttribut(ATTR_DEF_ACCOUNT);
             // if 2 accounts, take only the credit one
             if (strpos($sposte, ',') != 0) {
                 $array = explode(',', $sposte);
                 $poste_val = $array[1];
             } else {
                 $poste_val = $sposte;
             }
             $acc_operation->poste = $poste_val;
             $acc_operation->amount = $amount;
             $acc_operation->grpt = $seq;
             $acc_operation->jrn = $p_jrn;
             $acc_operation->type = 'c';
             $acc_operation->periode = $tperiode;
             if ($g_parameter->MY_UPDLAB == 'Y') {
                 $acc_operation->desc = strip_tags(${"e_march" . $i . "_label"});
             } else {
                 $acc_operation->desc = null;
             }
             $acc_operation->qcode = ${"e_march" . $i};
             if ($amount < 0) {
                 $tot_debit = bcadd($tot_debit, abs($amount));
             }
             $j_id = $acc_operation->insert_jrnx();
             if ($g_parameter->MY_TVA_USE == 'Y') {
                 /* Compute sum vat */
                 $oTva = new Acc_Tva($this->db);
                 $idx_tva = ${'e_march' . $i . '_tva_id'};
                 $tva_item = ${'e_march' . $i . '_tva_amount'};
                 $oTva->set_parameter("id", $idx_tva);
                 $oTva->load();
                 /* if empty then we need to compute it */
                 if (trim($tva_item) == '' || ${'e_march' . $i . '_tva_amount'} == 0) {
                     /* retrieve tva */
                     $l = new Acc_Tva($this->db, $idx_tva);
                     $l->load();
                     $tva_item = bcmul($amount, $l->get_parameter('rate'));
                 }
                 if (isset($tva[$idx_tva])) {
                     $tva[$idx_tva] += $tva_item;
                 } else {
                     $tva[$idx_tva] = $tva_item;
                 }
                 if ($oTva->get_parameter("both_side") == 0) {
                     $tot_tva = round(bcadd($tva_item, $tot_tva), 2);
                 } else {
                     $n_both = $tva_item;
                     if ($n_both < 0) {
                         $tot_debit = bcadd($tot_debit, abs($n_both));
                     }
                 }
             }
             /* Save the stock */
             /* if the quantity is < 0 then the stock increase (return of
              *  material)
              */
             $nNeg = ${"e_quant" . $i} < 0 ? -1 : 1;
             // always save quantity but in withStock we can find
             // what card need a stock management
             if ($g_parameter->MY_STOCK = 'Y' && isset($repo)) {
                 $dir = ${'e_quant' . $i} < 0 ? 'd' : 'c';
                 Stock_Goods::insert_goods($this->db, array('j_id' => $j_id, 'goods' => ${'e_march' . $i}, 'quant' => $nNeg * ${'e_quant' . $i}, 'dir' => $dir, 'repo' => $repo));
             }
             if ($g_parameter->MY_ANALYTIC != "nu") {
                 // for each item, insert into operation_analytique */
                 $op = new Anc_Operation($this->db);
                 $op->oa_group = $group;
                 $op->j_id = $j_id;
                 $op->oa_date = $e_date;
                 $op->oa_debit = 'f';
                 $op->oa_description = sql_string($e_comm);
                 $op->save_form_plan($_POST, $i, $j_id);
             }
             if ($g_parameter->MY_TVA_USE == 'Y') {
                 /* save into quant_sold */
                 $r = $this->db->exec_sql("select insert_quant_sold (\$1,\$2,\$3,\$4,\$5,\$6,\$7,\$8,\$9)", array(null, $j_id, ${'e_march' . $i}, ${'e_quant' . $i}, round($amount, 2), $tva_item, $idx_tva, $e_client, $n_both));
             } else {
                 $r = $this->db->exec_sql("select insert_quant_sold (\$1,\$2,\$3,\$4,\$5,\$6,\$7,\$8,\$9) ", array(null, $j_id, ${'e_march' . $i}, ${'e_quant' . $i}, $amount, 0, null, $e_client, 0));
             }
             // if ( $g_parameter->MY_TVA_USE=='Y') {
         }
         // end loop : save all items
         /*  save total customer */
         $cust_amount = bcadd($tot_amount, $tot_tva);
         $acc_operation = new Acc_Operation($this->db);
         $acc_operation->date = $e_date;
         $acc_operation->poste = $poste;
         $acc_operation->amount = $cust_amount;
         $acc_operation->grpt = $seq;
         $acc_operation->jrn = $p_jrn;
         $acc_operation->type = 'd';
         $acc_operation->periode = $tperiode;
         $acc_operation->qcode = ${"e_client"};
         if ($cust_amount > 0) {
             $tot_debit = bcadd($tot_debit, $cust_amount);
         }
         $let_tiers = $acc_operation->insert_jrnx();
         /** save all vat
          * $i contains the tva_id and value contains the vat amount
          * if if ($g_parameter->MY_TVA_USE == 'Y' )
          */
         if ($g_parameter->MY_TVA_USE == 'Y') {
             foreach ($tva as $i => $value) {
                 $oTva = new Acc_Tva($this->db);
                 $oTva->set_parameter('id', $i);
                 $oTva->load();
                 $poste_vat = $oTva->get_side('c');
                 $cust_amount = bcadd($tot_amount, $tot_tva);
                 $acc_operation = new Acc_Operation($this->db);
                 $acc_operation->date = $e_date;
                 $acc_operation->poste = $poste_vat;
                 $acc_operation->amount = $value;
                 $acc_operation->grpt = $seq;
                 $acc_operation->jrn = $p_jrn;
                 $acc_operation->type = 'c';
                 $acc_operation->periode = $tperiode;
                 if ($value < 0) {
                     $tot_debit = bcadd($tot_debit, abs($value));
                 }
                 $acc_operation->insert_jrnx();
                 // if TVA is on both side, we deduce it immediately
                 if ($oTva->get_parameter("both_side") == 1) {
                     $poste_vat = $oTva->get_side('d');
                     $cust_amount = bcadd($tot_amount, $tot_tva);
                     $acc_operation = new Acc_Operation($this->db);
                     $acc_operation->date = $e_date;
                     $acc_operation->poste = $poste_vat;
                     $acc_operation->amount = $value;
                     $acc_operation->grpt = $seq;
                     $acc_operation->jrn = $p_jrn;
                     $acc_operation->type = 'd';
                     $acc_operation->periode = $tperiode;
                     $acc_operation->insert_jrnx();
                     $tot_debit = bcadd($tot_debit, $value);
                     $n_both = $value;
                 }
             }
         }
         // if ($g_parameter->MY_TVA_USE=='Y')
         /* insert into jrn */
         $acc_operation = new Acc_Operation($this->db);
         $acc_operation->date = $e_date;
         $acc_operation->echeance = $e_ech;
         $acc_operation->amount = abs(round($tot_debit, 2));
         $acc_operation->desc = $e_comm;
         $acc_operation->grpt = $seq;
         $acc_operation->jrn = $p_jrn;
         $acc_operation->periode = $tperiode;
         $acc_operation->pj = $e_pj;
         $acc_operation->mt = $mt;
         $this->jr_id = $acc_operation->insert_jrn();
         $this->pj = $acc_operation->set_pj();
         /*             * = e_pj then do not increment sequence */
         /* and e_pj is not null */
         if (strcmp($e_pj, $e_pj_suggest) == 0 && strlen(trim($e_pj)) != 0) {
             $this->inc_seq_pj();
         }
         $this->db->exec_sql("update jrn set jr_internal='" . $internal . "' where " . " jr_grpt_id = " . $seq);
         /* update quant_sold */
         $this->db->exec_sql('update quant_sold set qs_internal = $1 where j_id in (select j_id from jrnx where j_grpt=$2)', array($internal, $seq));
         /* Save the attachment or generate doc */
         if (isset($_FILES['pj'])) {
             if (strlen(trim($_FILES['pj']['name'])) != 0) {
                 $this->db->save_upload_document($seq);
             } else {
                 /* Generate an invoice and save it into the database */
                 if (isset($_POST['gen_invoice'])) {
                     $file = $this->create_document($internal, $p_array);
                     $this->doc = '<A class="line" HREF="show_pj.php?' . dossier::get() . '&jr_grpt_id=' . $seq . '&jrn=' . $this->id . '">' . $file . '</A>';
                 }
             }
         }
         //----------------------------------------
         // Save the payer
         //----------------------------------------
         if ($e_mp != 0) {
             /* mp */
             $mp = new Acc_Payment($this->db, $e_mp);
             $mp->load();
             /* fiche */
             $fqcode = ${'e_mp_qcode_' . $e_mp};
             $acfiche = new Fiche($this->db);
             $acfiche->get_by_qcode($fqcode);
             /* jrnx */
             $acseq = $this->db->get_next_seq('s_grpt');
             $acjrn = new Acc_Ledger($this->db, $mp->get_parameter('ledger_target'));
             $acinternal = $acjrn->compute_internal_code($acseq);
             /* Insert paid by  */
             $acc_pay = new Acc_Operation($this->db);
             $acc_pay->date = $e_date;
             /* get the account and explode if necessary */
             $sposte = $acfiche->strAttribut(ATTR_DEF_ACCOUNT);
             // if 2 accounts, take only the debit one for customer
             if (strpos($sposte, ',') != 0) {
                 $array = explode(',', $sposte);
                 $poste_val = $array[0];
             } else {
                 $poste_val = $sposte;
             }
             $famount = bcsub($cust_amount, $acompte);
             $acc_pay->poste = $poste_val;
             $acc_pay->qcode = $fqcode;
             $acc_pay->amount = abs(round($famount, 2));
             $acc_pay->desc = null;
             $acc_pay->grpt = $acseq;
             $acc_pay->jrn = $mp->get_parameter('ledger_target');
             $acc_pay->periode = $tperiode;
             $acc_pay->type = $famount >= 0 ? 'd' : 'c';
             $acc_pay->insert_jrnx();
             /* Insert supplier  */
             $acc_pay = new Acc_Operation($this->db);
             $acc_pay->date = $e_date;
             $acc_pay->poste = $poste;
             $acc_pay->qcode = $e_client;
             $acc_pay->amount = abs(round($famount, 2));
             $acc_pay->desc = null;
             $acc_pay->grpt = $acseq;
             $acc_pay->jrn = $mp->get_parameter('ledger_target');
             $acc_pay->periode = $tperiode;
             $acc_pay->type = $famount >= 0 ? 'c' : 'd';
             $let_other = $acc_pay->insert_jrnx();
             /* insert into jrn */
             $acc_pay->mt = $mt;
             $acjrn->grpt_id = $acseq;
             $acc_pay->desc = !isset($e_comm_paiement) || strlen(trim($e_comm_paiement)) == 0 ? $e_comm : $e_comm_paiement;
             $mp_jr_id = $acc_pay->insert_jrn();
             $acjrn->update_internal_code($acinternal);
             $r1 = $this->get_id($internal);
             $r2 = $this->get_id($acinternal);
             /*
              * add lettering
              */
             $oletter = new Lettering($this->db);
             $oletter->insert_couple($let_tiers, $let_other);
             /* set the flag paid */
             $Res = $this->db->exec_sql("update jrn set jr_rapt='paid' where jr_id=\$1", array($r1));
             /* Reconcialiation */
             $rec = new Acc_Reconciliation($this->db);
             $rec->set_jr_id($r1);
             $rec->insert($r2);
             /*
              * save also into quant_fin
              */
             /* get ledger property */
             $ledger = new Acc_Ledger_Fin($this->db, $acc_pay->jrn);
             $prop = $ledger->get_propertie();
             /* if ledger is FIN then insert into quant_fin */
             if ($prop['jrn_def_type'] == 'FIN') {
                 $ledger->insert_quant_fin($acfiche->id, $mp_jr_id, $cust->id, bcmul($famount, 1));
             }
         }
     } catch (Exception $e) {
         echo '<span class="error">' . 'Erreur dans l\'enregistrement ' . __FILE__ . ':' . __LINE__ . ' ' . $e->getMessage();
         echo $e->getTrace();
         $this->db->rollback();
         throw new Exception($e);
     }
     $this->db->commit();
     return $internal;
 }
Beispiel #4
0
         }
         alert(_("Changement impossible: on ne peut pas changer la date dans une période fermée"));
     }
     $html = ob_get_contents();
     ob_end_clean();
     break;
     ////////////////////////////////////////////////////////////////////////////
     // remove a reconciliation
     ////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////
 // remove a reconciliation
 ////////////////////////////////////////////////////////////////////////////
 case 'rmr':
     if ($access == 'W') {
         $rec = new Acc_Reconciliation($cn);
         $rec->set_jr_id($jr_id);
         $rec->remove($_GET['jr_id2']);
     }
     break;
     ////////////////////////////////////////////////////////////////////////////
     // ask for a date for reversing the operation
     ////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////
 // ask for a date for reversing the operation
 ////////////////////////////////////////////////////////////////////////////
 case 'ask_extdate':
     $date = new IDate('p_date');
     $html .= "<form id=\"form_" . $div . "\" onsubmit=\"return reverseOperation(this);\">";
     $html .= HtmlInput::hidden('jr_id', $_REQUEST['jr_id']) . HtmlInput::hidden('div', $div) . dossier::hidden() . HtmlInput::hidden('act', 'reverseop');
     $html .= '<h2 class="info">' . _('entrez une date') . ' </H2>' . $date->input();
     $html .= HtmlInput::submit('x', 'accepter');
 public function insert($p_array = null)
 {
     global $g_parameter;
     bcscale(2);
     $internal_code = "";
     $oid = 0;
     extract($p_array);
     $ret = '';
     // Debit = banque
     $bank_id = $this->get_bank();
     $fBank = new Fiche($this->db, $bank_id);
     $e_bank_account = $fBank->strAttribut(ATTR_DEF_QUICKCODE);
     // Get the saldo
     $pPeriode = new Periode($this->db);
     $sposte = $fBank->strAttribut(ATTR_DEF_ACCOUNT);
     // if 2 accounts, take only the debit one for customer
     if (strpos($sposte, ',') != 0) {
         $array = explode(',', $sposte);
         $poste_val = $array[0];
     } else {
         $poste_val = $sposte;
     }
     $acc_account = new Acc_Account_Ledger($this->db, $poste_val);
     // If date = deposit date
     if ($chdate == 1) {
         if ($this->check_periode() == true) {
             $pPeriode->p_id = $periode;
         } else {
             $pPeriode->find_periode($e_date);
         }
         $exercice = $pPeriode->get_exercice();
         $filter_year = "  j_tech_per in (select p_id from parm_periode where  p_exercice='" . $exercice . "')";
         $asolde = $acc_account->get_solde_detail($filter_year);
         $deb = $asolde['debit'];
         $cred = $asolde['credit'];
         $solde = bcsub($deb, $cred);
         $new_solde = $solde;
     }
     try {
         $this->db->start();
         $amount = 0.0;
         $idx_operation = 0;
         $ret = '<table class="result" >';
         $ret .= tr(th('Date') . th('n° interne') . th('Quick Code') . th('Nom') . th('Libellé') . th('Montant', ' style="text-align:right"'));
         // Credit = goods
         $get_solde = true;
         for ($i = 0; $i < $nb_item; $i++) {
             // insert it into the database
             // and quit the loop ?
             if (strlen(trim(${"e_other{$i}"})) == 0) {
                 continue;
             }
             if ($chdate == 2) {
                 $e_date = ${'dateop' . $i};
             }
             // if date is date of operation
             if ($chdate == 2 && $get_solde) {
                 $get_solde = false;
                 if ($this->check_periode() == true) {
                     $pPeriode->p_id = $periode;
                 } else {
                     $pPeriode->find_periode($e_date);
                 }
                 $exercice = $pPeriode->get_exercice();
                 $filter_year = "  j_tech_per in (select p_id from parm_periode where  p_exercice='" . $exercice . "')";
                 $solde = $acc_account->get_solde($filter_year);
                 $new_solde = $solde;
             }
             $fPoste = new Fiche($this->db);
             $fPoste->get_by_qcode(${"e_other{$i}"});
             // round it
             ${"e_other{$i}" . "_amount"} = round(${"e_other{$i}" . "_amount"}, 2);
             $amount += ${"e_other{$i}" . "_amount"};
             // Record a line for the bank
             // Compute the j_grpt
             $seq = $this->db->get_next_seq('s_grpt');
             $acc_operation = new Acc_Operation($this->db);
             $acc_operation->date = $e_date;
             $sposte = $fPoste->strAttribut(ATTR_DEF_ACCOUNT);
             // if 2 accounts
             if (strpos($sposte, ',') != 0) {
                 $array = explode(',', $sposte);
                 if (${"e_other{$i}" . "_amount"} < 0) {
                     $poste_val = $array[1];
                 } else {
                     $poste_val = $array[0];
                 }
             } else {
                 $poste_val = $sposte;
             }
             $acc_operation->poste = $poste_val;
             $acc_operation->amount = ${"e_other{$i}" . "_amount"} * -1;
             $acc_operation->grpt = $seq;
             $acc_operation->jrn = $p_jrn;
             $acc_operation->type = 'd';
             if (isset($periode)) {
                 $tperiode = $periode;
             } else {
                 $per = new Periode($this->db);
                 $tperiode = $per->find_periode($e_date);
             }
             $acc_operation->periode = $tperiode;
             $acc_operation->qcode = ${"e_other" . $i};
             $j_id = $acc_operation->insert_jrnx();
             $acc_operation = new Acc_Operation($this->db);
             $acc_operation->date = $e_date;
             $sposte = $fBank->strAttribut(ATTR_DEF_ACCOUNT);
             // if 2 accounts
             if (strpos($sposte, ',') != 0) {
                 $array = explode(',', $sposte);
                 if (${"e_other{$i}" . "_amount"} < 0) {
                     $poste_val = $array[1];
                 } else {
                     $poste_val = $array[0];
                 }
             } else {
                 $poste_val = $sposte;
             }
             $acc_operation->poste = $poste_val;
             $acc_operation->amount = ${"e_other{$i}" . "_amount"};
             $acc_operation->grpt = $seq;
             $acc_operation->jrn = $p_jrn;
             $acc_operation->type = 'd';
             $acc_operation->periode = $tperiode;
             $acc_operation->qcode = $e_bank_account;
             $acc_operation->insert_jrnx();
             if (sql_string(${"e_other{$i}" . "_comment"}) == null) {
                 // if comment is blank set a default one
                 $comment = "  compte : " . $fBank->strAttribut(ATTR_DEF_NAME) . ' a ' . $fPoste->strAttribut(ATTR_DEF_NAME);
             } else {
                 $comment = ${'e_other' . $i . '_comment'};
             }
             $acc_operation = new Acc_Operation($this->db);
             $acc_operation->jrn = $p_jrn;
             $acc_operation->amount = abs(${"e_other{$i}" . "_amount"});
             $acc_operation->date = $e_date;
             $acc_operation->desc = $comment;
             $acc_operation->grpt = $seq;
             $acc_operation->periode = $tperiode;
             $acc_operation->mt = $mt;
             $idx_operation++;
             $acc_operation->pj = '';
             if (trim($e_pj) != '' && $this->numb_operation() == true) {
                 $acc_operation->pj = $e_pj . str_pad($idx_operation, 3, 0, STR_PAD_LEFT);
             }
             if (trim($e_pj) != '' && $this->numb_operation() == false) {
                 $acc_operation->pj = $e_pj;
             }
             $jr_id = $acc_operation->insert_jrn();
             // 	  $acc_operation->set_pj();
             $this->db->exec_sql('update jrn set jr_pj_number=$1 where jr_id=$2', array($acc_operation->pj, $jr_id));
             $internal = $this->compute_internal_code($seq);
             if (trim(${"e_concerned" . $i}) != "") {
                 if (strpos(${"e_concerned" . $i}, ',') != 0) {
                     $aRapt = explode(',', ${"e_concerned" . $i});
                     foreach ($aRapt as $rRapt) {
                         // Add a "concerned operation to bound these op.together
                         //
                         $rec = new Acc_Reconciliation($this->db);
                         $rec->set_jr_id($jr_id);
                         if (isNumber($rRapt) == 1) {
                             $rec->insert($rRapt);
                         }
                     }
                 } else {
                     if (isNumber(${"e_concerned" . $i}) == 1) {
                         $rec = new Acc_Reconciliation($this->db);
                         $rec->set_jr_id($jr_id);
                         $rec->insert(${"e_concerned{$i}"});
                     }
                 }
             }
             // Set Internal code
             $this->grpt_id = $seq;
             /**
              * save also into quant_fin
              */
             $this->insert_quant_fin($fBank->id, $jr_id, $fPoste->id, ${"e_other{$i}" . "_amount"});
             if ($g_parameter->MY_ANALYTIC != "nu") {
                 // for each item, insert into operation_analytique */
                 $op = new Anc_Operation($this->db);
                 $op->oa_group = $this->db->get_next_seq("s_oa_group");
                 /* for analytic */
                 $op->j_id = $j_id;
                 $op->oa_date = $e_date;
                 $op->oa_debit = 'f';
                 $op->oa_description = sql_string($comment);
                 $op->save_form_plan($_POST, $i, $j_id);
             }
             $this->update_internal_code($internal);
             $js_detail = HtmlInput::detail_op($jr_id, $internal);
             // Compute display
             $row = td($e_date) . td($js_detail) . td(${"e_other{$i}"}) . td($fPoste->strAttribut(ATTR_DEF_NAME)) . td(${"e_other" . $i . "_comment"}) . td(nbm(${"e_other{$i}" . "_amount"}), 'class="num"');
             $class = $i % 2 == 0 ? ' class="even" ' : ' class="odd" ';
             $ret .= tr($row, $class);
             if ($i == 0) {
                 // first record we upload the files and
                 // keep variable to update other row of jrn
                 if (isset($_FILES)) {
                     $oid = $this->db->save_upload_document($seq);
                 }
             } else {
                 if ($oid != 0) {
                     $this->db->exec_sql("update jrn set jr_pj=\$1 , jr_pj_name=\$2,\n                                            jr_pj_type=\$3  where jr_grpt_id=\$4", array($oid, $_FILES['pj']['name'], $_FILES['pj']['type'], $seq));
                 }
             }
         }
         // for nbitem
         // increment pj
         if (strlen(trim($e_pj)) != 0) {
             $this->inc_seq_pj();
         }
         $ret .= '</table>';
     } catch (Exception $e) {
         $r = '<span class="error">' . 'Erreur dans l\'enregistrement ' . __FILE__ . ':' . __LINE__ . ' ' . $e->getMessage();
         $this->db->rollback();
         throw new Exception($r);
     }
     $this->db->commit();
     $r = "";
     $r .= "<br>Ancien solde " . nbm($solde);
     $new_solde = bcadd($new_solde, $amount);
     $r .= "<br>Nouveau solde " . nbm($new_solde);
     $ret .= $r;
     return $ret;
 }