/**
  * @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;
 }
    die('Appel direct ne sont pas permis');
}
if ($g_user->check_module('PREDOP') == 0) {
    exit;
}
$name = HtmlInput::default_value_post("opd_name", "");
if (trim($name) != '') {
    $od_id = HtmlInput::default_value_post("od_id", -1);
    if ($od_id == -1 || isNumber($od_id) == 0) {
        return;
    }
    $cn->exec_sql('delete from op_predef where od_id=$1', array($od_id));
    $cn->exec_sql("delete from op_predef_detail where od_id=\$1", array($od_id));
    $jrn_type = HtmlInput::default_value_post("jrn_type", null);
    switch ($jrn_type) {
        case 'ACH':
            $operation = new Pre_op_ach($cn);
            break;
        case 'VEN':
            $operation = new Pre_op_ven($cn);
            break;
        case 'ODS':
            $operation = new Pre_Op_Advanced($cn);
            break;
        default:
            throw new Exception(_('Type de journal invalide'));
    }
    $operation->get_post();
    $operation->save();
    $cn->commit();
}