<?php 
// display title only in popup
if ($div == 'popup') {
    ?>
 
                <h1 class="legend"><?php 
    echo $a_tab['linked_operation_div']['label'];
    ?>
</h1>
          <?php 
}
if ($aRap != null) {
    $tableid = "tb" . $div;
    echo '<table id="' . $tableid . '">';
    for ($e = 0; $e < count($aRap); $e++) {
        $opRap = new Acc_Operation($cn);
        $opRap->jr_id = $aRap[$e];
        $internal = $opRap->get_internal();
        $array_jr = $cn->get_array('select jr_date,jr_montant,jr_comment from jrn where jr_id=$1', array($aRap[$e]));
        $amount = $array_jr[0]['jr_montant'];
        $str = "modifyOperation(" . $aRap[$e] . "," . $gDossier . ")";
        $rmReconciliation = new IButton('rmr');
        $rmReconciliation->label = SMALLX;
        $rmReconciliation->class = "tinybutton";
        $rmReconciliation->javascript = "return confirm_box(null,'" . _("vous confirmez?") . "',";
        $rmReconciliation->javascript .= sprintf('function () { dropLink(\'%s\',\'%s\',\'%s\',\'%s\');deleteRowRec(\'%s\',$(\'row%d\'));})', $gDossier, $div, $jr_id, $aRap[$e], $tableid, $e);
        if ($access == 'W') {
            $remove = $rmReconciliation->input();
        } else {
            $remove = '';
        }
示例#2
0
            echo '<table  style="width:100%;margin-left:0%">';
            foreach ($a_poste as $poste_id) {
                $Poste = new Acc_Account_Ledger($cn, $poste_id['pcm_val']);
                $Poste->load();
                $Poste->get_row_date($_GET['from_periode'], $_GET['to_periode'], $_GET['ople']);
                if (empty($Poste->row)) {
                    continue;
                }
                echo '<tr><td  class="mtitle" style="width:auto" colspan="6"><h2 class="title">' . $poste_id['pcm_val'] . ' ' . h($Poste->label) . '</h2></td></tr>';
                $detail = $Poste->row[0];
                $old = array();
                foreach ($Poste->row as $detail) {
                    /* avoid duplicates */
                    if (in_array($detail['jr_id'], $old) == TRUE) {
                        continue;
                    }
                    $old[] = $detail['jr_id'];
                    echo tr(td("Journal :" . $detail['jrn_def_name'], ''), 'style="width:auto" colspan="6"');
                    echo '<tr><td class="mtitle" style="width:auto" colspan="6">' . $detail['j_date'] . ' ' . $detail['jr_internal'] . ' ' . hb($detail['description']) . ' ' . hi($detail['jr_pj_number']) . '</td></tr>';
                    $op = new Acc_Operation($cn);
                    $op->poste = $poste_id['pcm_val'];
                    $op->jr_id = $detail['jr_id'];
                    echo $op->display_jrnx_detail(1);
                }
            }
            echo '</table>';
            echo Acc_Account_Ledger::HtmlTableHeader();
        }
        exit;
    }
}
示例#3
0
  <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>
<?php 
        if ($i < $linked_limit) {
            $amount_deb += $jnt_id == $this->content[$i]['letter'] && $this->content[$i]['j_debit'] == 't' ? $this->content[$i]['j_montant'] : 0;
            $amount_cred += $jnt_id == $this->content[$i]['letter'] && $this->content[$i]['j_debit'] == 'f' ? $this->content[$i]['j_montant'] : 0;
示例#4
0
  <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:void(0)\" onclick=\"viewOperation('" . $element . "'," . $gDossier . ")\" > " . $operation->get_internal() . " [ " . nb($l_amount) . " &euro; ]</A>";
        }
        //for
    }
    // if ( $a != null ) {
    // compute amount
    $amount_deb += $this->content[$i]['j_debit'] == 't' ? $this->content[$i]['j_montant'] : 0;
    $amount_cred += $this->content[$i]['j_debit'] == 'f' ? $this->content[$i]['j_montant'] : 0;
    ?>
</td>
</tr>

<?php 
 function __construct($p_cn, $p_jrid = 0)
 {
     parent::__construct($p_cn);
     $this->jr_id = $p_jrid;
     $this->det = new stdClass();
 }
             */
            $progress = 0;
            $current_exercice = $op['p_exercice'];
            $tot_deb = 0;
            $tot_cred = 0;
        }
        $diff = bcsub($op['deb_montant'], $op['cred_montant']);
        $progress = bcadd($progress, $diff);
        $tot_deb = bcadd($tot_deb, $op['deb_montant']);
        $tot_cred = bcadd($tot_cred, $op['cred_montant']);
        echo '"' . $op['j_qcode'] . '";' . '"' . $op['j_date_fmt'] . '"' . ";" . '"' . $op['jr_pj_number'] . '"' . ";" . '"' . $op['jr_internal'] . '"' . ";" . '"' . $op['jrn_def_code'] . '"' . ";" . '"' . $op['jrn_def_name'] . '"' . ";" . '"' . $op['description'] . '"' . ";" . nb($op['deb_montant']) . ";" . nb($op['cred_montant']) . ";" . nb(abs($progress)) . ';' . '"' . ($op['letter'] == -1 ? '' : strtoupper(base_convert($op['letter'], 10, 36))) . '"';
        printf("\n");
    }
} else {
    echo '"Poste";"Qcode";"date";"ref";"internal";';
    echo "\"Description\";" . "\"Montant\";" . "\"D/C\"";
    printf("\r\n");
    foreach ($Fiche->row as $op) {
        $acc = new Acc_Operation($cn);
        $acc->jr_id = $op['jr_id'];
        $result = $acc->get_jrnx_detail();
        foreach ($result as $r) {
            printf('"%s";"%s";"%s";"%s";"%s";%s;%s;"%s"', $r['j_poste'], $r['j_qcode'], $r['jr_date'], $op['jr_pj_number'], $r['jr_internal'], $r['description'], nb($r['j_montant']), $r['debit']);
            printf("\r\n");
        }
    }
}
$solde_type = $tot_deb > $tot_cred ? "solde débiteur" : "solde créditeur";
$diff = abs($tot_deb - $tot_cred);
printf('"' . "{$solde_type}" . '"' . ";" . nb($diff) . ";" . nb($tot_deb) . ";" . nb($tot_cred) . "\n");
exit;
        }
        $solde_type = $tot_deb > $tot_cred ? "solde débiteur" : "solde créditeur";
        $diff = abs($tot_deb - $tot_cred);
        printf(";;;" . '"' . _('total') . '";' . '"' . $current_exercice . '";' . '"' . "{$solde_type}" . '"' . ";" . nb($tot_deb) . ";" . nb($tot_cred) . ";" . nb($diff) . ";" . "\n");
    }
} else {
    /* detail of all operation */
    if (count($a_poste) == 0) {
        exit;
    }
    foreach ($a_poste as $pos) {
        $Poste = new Acc_Account_Ledger($cn, $pos['pcm_val']);
        $Poste->get_name();
        list($array, $tot_deb, $tot_cred) = $Poste->get_row_date($_REQUEST['from_periode'], $_REQUEST['to_periode'], $_GET['ople']);
        if (count($Poste->row) == 0) {
            continue;
        }
        echo '"Poste";' . '"Lib.";' . '"QuickCode";' . "\"Code interne\";" . "\"Date\";" . "\"Description\";" . "\"Montant\";" . "\"D/C\"";
        printf("\n");
        foreach ($Poste->row as $a) {
            $op = new Acc_Operation($cn);
            $op->jr_id = $a['jr_id'];
            $result = $op->get_jrnx_detail();
            foreach ($result as $r) {
                printf('"%s";"%s";"%s";"%s";"%s";"%s";"%s";%12.2f;"%s"', $r['j_poste'], $r['pcm_lib'], $r['j_qcode'], $r['jr_internal'], $r['jr_date'], $a['description'], $a['jr_pj_number'], nb($r['j_montant']), $r['debit']);
                printf("\r\n");
            }
        }
    }
    exit;
}
 $pdf->Cell($size[$l], 6, $row['letter'] != -1 ? $row['letter'] : '', 0, 0, $align[$l]);
 $l++;
 $pdf->Cell($size[$l], 6, sprintf('% 12.2f', $row['deb_montant']), 0, 0, $align[$l]);
 $l++;
 $pdf->Cell($size[$l], 6, sprintf('% 12.2f', $row['cred_montant']), 0, 0, $align[$l]);
 $l++;
 $pdf->Cell($size[$l], 6, sprintf('% 12.2f', abs($prog)), 0, 0, $align[$l]);
 $l++;
 $pdf->ln();
 $tot_deb = bcadd($tot_deb, $row['deb_montant']);
 $tot_cred = bcadd($tot_deb, $row['cred_montant']);
 /* -------------------------------------- */
 /* if details are asked we show them here */
 /* -------------------------------------- */
 if (isset($_GET['oper_detail'])) {
     $detail = new Acc_Operation($cn);
     $detail->jr_id = $row['jr_id'];
     $a_detail = $detail->get_jrnx_detail();
     for ($f = 0; $f < count($a_detail); $f++) {
         $l = 0;
         $pdf->Cell($size[$l], 6, '', 0, 0, $align[$l]);
         $l++;
         $pdf->Cell($size[$l], 6, $a_detail[$f]['j_qcode'], 0, 0, $align[$l]);
         $l++;
         $pdf->Cell($size[$l], 6, $a_detail[$f]['j_poste'], 0, 0, $align[$l]);
         $l++;
         if ($a_detail[$f]['j_qcode'] == '') {
             $lib = $a_detail[$f]['pcm_lib'];
         } else {
             $f_id = $cn->get_value('select f_id from vw_poste_qcode where j_qcode=$1', array($a_detail[$f]['j_qcode']));
             $lib = $cn->get_value('select ad_value from fiche_detail where ad_id=$1 and f_id=$2', array(ATTR_DEF_NAME, $f_id));
示例#9
0
 /**
  * @brief save the operation into the jrnx,jrn, ,
  *  CA and pre_def
  * @param$p_array
  *
  * \return array with [0] = false if failed otherwise true, [1] error
  * code
  */
 function save($p_array = null)
 {
     if ($p_array == null) {
         throw new Exception('save cannot use a empty array');
     }
     global $g_parameter;
     extract($p_array);
     try {
         $msg = $this->verify($p_array);
         if (!empty($msg)) {
             echo $this->display_warning($msg, _("Attention : il vaut mieux utiliser les fiches que les postes comptables "));
         }
         $this->db->start();
         $seq = $this->db->get_next_seq('s_grpt');
         $internal = $this->compute_internal_code($seq);
         $group = $this->db->get_next_seq("s_oa_group");
         $tot_amount = 0;
         $tot_deb = 0;
         $tot_cred = 0;
         $oPeriode = new Periode($this->db);
         $check_periode = $this->check_periode();
         if ($check_periode == false) {
             $oPeriode->find_periode($e_date);
         } else {
             $oPeriode->id = $period;
         }
         $count = 0;
         for ($i = 0; $i < $nb_item; $i++) {
             if (!isset(${'qc_' . $i}) && !isset(${'poste' . $i})) {
                 continue;
             }
             $acc_op = new Acc_Operation($this->db);
             $quick_code = "";
             // First we save the jrnx
             if (isset(${'qc_' . $i})) {
                 $qc = new Fiche($this->db);
                 $qc->get_by_qcode(${'qc_' . $i}, false);
                 $sposte = $qc->strAttribut(ATTR_DEF_ACCOUNT);
                 /*  if there are 2 accounts take following the deb or cred */
                 if (strpos($sposte, ',') != 0) {
                     $array = explode(",", $sposte);
                     $poste = isset(${'ck' . $i}) ? $array[0] : $array[1];
                 } else {
                     $poste = $sposte;
                     if ($poste == '') {
                         throw new Exception(sprintf(_("La fiche %s n'a pas de poste comptable"), ${"qc_" . $i}));
                     }
                 }
                 $quick_code = ${'qc_' . $i};
             } else {
                 $poste = ${'poste' . $i};
             }
             $acc_op->date = $e_date;
             // compute the periode is do not check it
             if ($check_periode == false) {
                 $acc_op->periode = $oPeriode->p_id;
             }
             $acc_op->desc = null;
             if (strlen(trim(${'ld' . $i})) != 0) {
                 $acc_op->desc = ${'ld' . $i};
             }
             $acc_op->amount = round(${'amount' . $i}, 2);
             $acc_op->grpt = $seq;
             $acc_op->poste = $poste;
             $acc_op->jrn = $this->id;
             $acc_op->type = isset(${'ck' . $i}) ? 'd' : 'c';
             $acc_op->qcode = $quick_code;
             $j_id = $acc_op->insert_jrnx();
             $tot_amount += round($acc_op->amount, 2);
             $tot_deb += $acc_op->type == 'd' ? $acc_op->amount : 0;
             $tot_cred += $acc_op->type == 'c' ? $acc_op->amount : 0;
             if ($g_parameter->MY_ANALYTIC != "nu") {
                 if (preg_match("/^[6,7]+/", $poste) == 1) {
                     // 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 = $acc_op->type == 'd' ? 't' : 'f';
                     $op->oa_description = $desc;
                     $op->save_form_plan($p_array, $count, $j_id);
                     $count++;
                 }
             }
         }
         // loop for each item
         $acc_end = new Acc_Operation($this->db);
         $acc_end->amount = $tot_deb;
         if ($check_periode == false) {
             $acc_end->periode = $oPeriode->p_id;
         }
         $acc_end->date = $e_date;
         $acc_end->desc = $desc;
         $acc_end->grpt = $seq;
         $acc_end->jrn = $this->id;
         $acc_end->mt = $mt;
         $jr_id = $acc_end->insert_jrn();
         $this->jr_id = $jr_id;
         if ($jr_id == false) {
             throw new Exception(_('Balance incorrecte'));
         }
         $acc_end->pj = $e_pj;
         /* if e_suggest != e_pj then do not increment sequence */
         if (strcmp($e_pj, $e_pj_suggest) == 0 && strlen(trim($e_pj)) != 0) {
             $this->inc_seq_pj();
         }
         $this->pj = $acc_end->set_pj();
         $this->db->exec_sql("update jrn set jr_internal='" . $internal . "' where " . " jr_grpt_id = " . $seq);
         $this->internal = $internal;
         // Save now the predef op
         //------------------------
         if (isset($opd_name) && trim($opd_name) != "") {
             $opd = new Pre_Op_Advanced($this->db);
             $opd->get_post();
             $opd->save();
         }
         if (isset($this->with_concerned) && $this->with_concerned == true) {
             $orap = new acc_reconciliation($this->db);
             $orap->jr_id = $jr_id;
             $orap->insert($jrn_concerned);
         }
         /**
          * Save the file is any
          */
         if (isset($_FILES["pj"])) {
             $this->db->save_upload_document($seq);
         }
     } catch (Exception $a) {
         throw $a;
     } catch (Exception $e) {
         $this->db->rollback();
         echo _('OPERATION ANNULEE ');
         echo '<hr>';
         echo __FILE__ . __LINE__ . $e->getMessage();
         exit;
     }
     $this->db->commit();
     return true;
 }
示例#10
0
 function HtmlTableDetail($p_array = null, $op_let = 0)
 {
     if ($p_array == null) {
         $p_array = $_REQUEST;
     }
     $name = $this->getName();
     list($array, $tot_deb, $tot_cred) = $this->get_row_date($p_array['from_periode'], $p_array['to_periode'], $op_let);
     if (count($this->row) == 0) {
         return;
     }
     $qcode = $this->strAttribut(ATTR_DEF_QUICKCODE);
     $rep = "";
     $already_seen = array();
     echo '<h2 class="info">' . $this->id . " " . $name . '</h2>';
     echo "<TABLE class=\"result\" style=\"width:100%;border-collapse:separate;border-spacing:5px\">";
     echo "<TR>" . "<TH>" . _("n° de pièce / Code interne") . " </TH>" . "<TH>" . _("Date") . "</TH>" . "<TH>" . _("Description") . " </TH>" . "<TH>" . _('Montant') . "  </TH>" . "<TH> " . _('Débit/Crédit') . " </TH>" . "</TR>";
     foreach ($this->row as $op) {
         if (in_array($op['jr_internal'], $already_seen)) {
             continue;
         } else {
             $already_seen[] = $op['jr_internal'];
         }
         echo "<TR  style=\"text-align:center;background-color:lightgrey\">" . "<td>" . $op['jr_pj_number'] . " / " . $op['jr_internal'] . "</td>" . "<td>" . $op['j_date'] . "</td>" . "<td>" . h($op['description']) . "</td>" . "<td>" . "</td>" . "<td>" . "</td>" . "</TR>";
         $ac = new Acc_Operation($this->cn);
         $ac->jr_id = $op['jr_id'];
         $ac->qcode = $qcode;
         echo $ac->display_jrnx_detail(1);
     }
     $solde_type = $tot_deb > $tot_cred ? _("solde débiteur") : _("solde créditeur");
     $diff = round(abs($tot_deb - $tot_cred), 2);
     echo "<TR>" . "<TD>{$solde_type}" . "<TD>{$diff}</TD>" . "<TD></TD>" . "<TD>{$tot_deb}</TD>" . "<TD>{$tot_cred}</TD>" . "</TR>";
     echo "</table>";
     return;
 }
示例#11
0
    $html = ob_get_contents();
    ob_end_clean();
    $html = escape_xml($html);
    header('Content-type: text/xml; charset=UTF-8');
    echo <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<data>
<ctl>{$div}</ctl>
<code>{$html}</code>
</data>
EOF;
    exit;
}
// check if the user can access the ledger where the operation is (view) and
// if he can modify it
$op = new Acc_Operation($cn);
$op->jr_id = $_REQUEST['jr_id'];
$ledger = $op->get_ledger();
if ($ledger == "") {
    ob_start();
    echo HtmlInput::title_box(_("Information"), $div);
    require_once NOALYSS_INCLUDE . '/template/ledger_detail_forbidden.php';
    echo HtmlInput::button_close($div);
    $html = ob_get_contents();
    ob_end_clean();
    $html = escape_xml($html);
    if (!headers_sent()) {
        header('Content-type: text/xml; charset=UTF-8');
    } else {
        echo "HTML" . unescape_xml($html);
    }
示例#12
0
     }
     echo $positive == 0 ? "<font color=\"red\">  - " . nbm($line['montant']) . "</font>" : nbm($line['montant']);
 } else {
     if (isset($line['TVAC'])) {
         echo nbm($line['TVAC']) < 0 ? "<font color=\"red\">  - " . nbm($line['TVAC']) . "</font>" : nbm($line['TVAC']);
     } else {
         echo nbm($line['montant']);
     }
 }
 echo "</TD>";
 echo "</tr>";
 echo '</table>';
 //////////////////////////////////////////////////////////////////////////////////////////////////////
 // Add detail for each operation
 //////////////////////////////////////////////////////////////////////////////////////////////////////
 $op = new Acc_Operation($cn);
 $op->jr_id = $line['jr_id'];
 $op->get();
 $obj = $op->get_quant();
 switch ($obj->signature) {
     case 'FIN':
         require 'template/operation_detail_fin.php';
         break;
     case 'ACH':
         require 'template/operation_detail_ach.php';
         break;
     case 'VEN':
         require 'template/operation_detail_ven.php';
         break;
     case 'ODS':
         require 'template/operation_detail_misc.php';
 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;
 }
 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;
 }