/**
  * @brief check if the payment method is valid
  * @param$e_mp is the value and $e_mp_qcode is the quickcode
  * \return nothing throw an Exception
  */
 public function check_payment($e_mp, $e_mp_qcode)
 {
     /*   Check if the "paid by" is empty, */
     if ($e_mp != 0) {
         /* the paid by is not empty then check if valid */
         $empl = new Fiche($this->db);
         $empl->get_by_qcode($e_mp_qcode);
         if ($empl->empty_attribute(ATTR_DEF_ACCOUNT) == true) {
             throw new Exception(_("Celui qui paie n' a pas de poste comptable"), 20);
         }
         /* get the account and explode if necessary */
         $sposte = $empl->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;
         }
         $poste = new Acc_Account_Ledger($this->db, $poste_val);
         if ($poste->load() == false) {
             throw new Exception(sprintf(_("Pour la fiche %s le poste comptable [%s] n'existe pas"), $empl->quick_code, $poste->id), 9);
         }
     }
 }
 /**
  * Verify that the data are correct before inserting or confirming
  * @brief verify the data 
  * @param an array (usually $_POST)
  * @return String
  * @throw Exception on error occurs
  */
 public function verify($p_array)
 {
     global $g_user;
     if (is_array($p_array) == false || empty($p_array)) {
         throw new Exception("Array empty");
     }
     /*
      * Check needed value
      */
     check_parameter($p_array, 'p_jrn');
     extract($p_array);
     /* check for a double reload */
     if (isset($mt) && $this->db->count_sql('select jr_mt from jrn where jr_mt=$1', array($mt)) != 0) {
         throw new Exception(_('Double Encodage'), 5);
     }
     /* check if we can write into this ledger */
     if ($g_user->check_jrn($p_jrn) != 'W') {
         throw new Exception(_('Accès interdit'), 20);
     }
     /* check if there is a bank account linked to the ledger */
     $bank_id = $this->get_bank();
     if ($this->db->count() == 0) {
         throw new Exception("Ce journal n'a pas de compte en banque, allez dans paramètre->journal pour régler cela");
     }
     /* check if the accounting of the bank is correct */
     $fBank = new Fiche($this->db, $bank_id);
     $bank_accounting = $fBank->strAttribut(ATTR_DEF_ACCOUNT);
     if (trim($bank_accounting) == '') {
         throw new Exception('Le poste comptable du compte en banque de ce journal est invalide');
     }
     /* check if the account exists */
     $poste = new Acc_Account_Ledger($this->db, $bank_accounting);
     if ($poste->load() == false) {
         throw new Exception('Le poste comptable du compte en banque de ce journal est invalide');
     }
     if ($chdate != 1 && $chdate != 2) {
         throw new Exception('Le choix de date est invalide');
     }
     if ($chdate == 1) {
         /*  check if the date is valid */
         if (isDate($e_date) == null) {
             throw new Exception('Date invalide', 2);
         }
         $oPeriode = new Periode($this->db);
         if ($this->check_periode() == false) {
             $periode = $oPeriode->find_periode($e_date);
         } else {
             $oPeriode->p_id = $periode;
             list($min, $max) = $oPeriode->get_date_limit();
             if (cmpDate($e_date, $min) < 0 || cmpDate($e_date, $max) > 0) {
                 throw new Exception(_('Date et periode ne correspondent pas'), 6);
             }
         }
         /* check if the periode is closed */
         if ($this->is_closed($periode) == 1) {
             throw new Exception(_('Periode fermee'), 6);
         }
         /* check if we are using the strict mode */
         if ($this->check_strict() == true) {
             /* if we use the strict mode, we get the date of the last
             			operation */
             $last_date = $this->get_last_date();
             if ($last_date != null && cmpDate($e_date, $last_date) < 0) {
                 throw new Exception(_('Vous utilisez le mode strict la dernière operation est à la date du ') . $last_date . _(' vous ne pouvez pas encoder à une date antérieure'), 15);
             }
         }
     }
     $acc_pay = new Acc_Operation($this->db);
     $nb = 0;
     $tot_amount = 0;
     //----------------------------------------
     // foreach item
     //----------------------------------------
     for ($i = 0; $i < $nb_item; $i++) {
         if (strlen(trim(${'e_other' . $i})) == 0) {
             continue;
         }
         /* check if amount are numeric and */
         if (isNumber(${'e_other' . $i . '_amount'}) == 0) {
             throw new Exception('La fiche ' . ${'e_other' . $i} . 'a un montant invalide [' . ${'e_other' . $i . '_amount'} . ']', 6);
         }
         /* compute the total */
         $tot_amount += round(${'e_other' . $i . '_amount'}, 2);
         /* check if all card has a ATTR_DEF_ACCOUNT */
         $fiche = new Fiche($this->db);
         $fiche->get_by_qcode(${'e_other' . $i});
         if ($fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true) {
             throw new Exception('La fiche ' . ${'e_other' . $i} . 'n\'a pas de poste comptable', 8);
         }
         $sposte = $fiche->strAttribut(ATTR_DEF_ACCOUNT);
         // if 2 accounts, take only the debit one for customer
         if (strpos($sposte, ',') != 0) {
             $array = explode(',', $sposte);
             $poste_val = $array[1];
         } else {
             $poste_val = $sposte;
         }
         /* The account exists */
         $poste = new Acc_Account_Ledger($this->db, $poste_val);
         if ($poste->load() == false) {
             throw new Exception('Pour la fiche ' . ${'e_other' . $i} . ' le poste comptable [' . $poste->id . 'n\'existe pas', 9);
         }
         /* Check if the card belong to the ledger */
         $fiche = new Fiche($this->db);
         $fiche->get_by_qcode(${'e_other' . $i});
         if ($fiche->belong_ledger($p_jrn, 'deb') != 1) {
             throw new Exception('La fiche ' . ${'e_other' . $i} . 'n\'est pas accessible à ce journal', 10);
         }
         if ($chdate == 2) {
             /*  check if the date is valid */
             if (isDate(${'dateop' . $i}) == null) {
                 throw new Exception('Date invalide', 2);
             }
             $oPeriode = new Periode($this->db);
             if ($this->check_periode() == false) {
                 $periode = $oPeriode->find_periode(${'dateop' . $i});
             } else {
                 $oPeriode->p_id = $periode;
                 list($min, $max) = $oPeriode->get_date_limit();
                 if (cmpDate(${'dateop' . $i}, $min) < 0 || cmpDate(${'dateop' . $i}, $max) > 0) {
                     throw new Exception(_('Date et periode ne correspondent pas'), 6);
                 }
             }
             /* check if the periode is closed */
             if ($this->is_closed($periode) == 1) {
                 throw new Exception(_('Periode fermee'), 6);
             }
             /* check if we are using the strict mode */
             if ($this->check_strict() == true) {
                 /* if we use the strict mode, we get the date of the last
                 	  operation */
                 $last_date = $this->get_last_date();
                 if ($last_date != null && cmpDate(${'dateop' . $i}, $last_date) < 0) {
                     throw new Exception(_('Vous utilisez le mode strict la dernière operation est à la date du ') . $last_date . _(' vous ne pouvez pas encoder à une date antérieure'), 15);
                 }
             }
         }
         $nb++;
     }
     if ($nb == 0) {
         throw new Exception('Il n\'y a aucune opération', 12);
     }
     /* Check if the last_saldo and first_saldo are correct */
     if (strlen(trim($last_sold)) != 0 && isNumber($last_sold) && strlen(trim($first_sold)) != 0 && isNumber($first_sold)) {
         $diff = $last_sold - $first_sold;
         $diff = round($diff, 2) - round($tot_amount, 2);
         if ($first_sold != 0 && $last_sold != 0) {
             if ($diff != 0) {
                 throw new Exception('Le montant de l\'extrait est incorrect' . $tot_amount . ' extrait ' . $diff, 13);
             }
         }
     }
 }
 public function verify($p_array)
 {
     global $g_parameter, $g_user;
     if (is_array($p_array) == false || empty($p_array)) {
         throw new Exception("Array empty");
     }
     extract($p_array);
     /*
      * Check needed value
      */
     check_parameter($p_array, 'p_jrn,e_date,e_client');
     /* check for a double reload */
     if (isset($mt) && $this->db->count_sql('select jr_mt from jrn where jr_mt=$1', array($mt)) != 0) {
         throw new Exception(_('Double Encodage'), 5);
     }
     /* check if we can write into this ledger */
     if ($g_user->check_jrn($p_jrn) != 'W') {
         throw new Exception(_('Accès interdit'), 20);
     }
     /* check if there is a customer */
     if (strlen(trim($e_client)) == 0) {
         throw new Exception(_('Vous n\'avez pas donné de client'), 11);
     }
     /*  check if the date is valid */
     if (isDate($e_date) == null) {
         throw new Exception(_('Date invalide'), 2);
     }
     $oPeriode = new Periode($this->db);
     if ($this->check_periode() == true) {
         $tperiode = $period;
         /* check that the datum is in the choosen periode */
         $oPeriode->p_id = $period;
         list($min, $max) = $oPeriode->get_date_limit();
         if (cmpDate($e_date, $min) < 0 || cmpDate($e_date, $max) > 0) {
             throw new Exception(_('Date et periode ne correspondent pas'), 6);
         }
     } else {
         $per = new Periode($this->db);
         $tperiode = $per->find_periode($e_date);
     }
     /* check if the periode is closed */
     if ($this->is_closed($tperiode) == 1) {
         throw new Exception(_('Periode fermee'), 6);
     }
     /* check if we are using the strict mode */
     if ($this->check_strict() == true) {
         /* if we use the strict mode, we get the date of the last
            operation */
         $last_date = $this->get_last_date();
         if ($last_date != null && cmpDate($e_date, $last_date) < 0) {
             throw new Exception(_('Vous utilisez le mode strict la dernière operation est date du ') . $last_date . _(' vous ne pouvez pas encoder à une date antérieure'), 13);
         }
     }
     $fiche = new Fiche($this->db);
     $fiche->get_by_qcode($e_client);
     if ($fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true) {
         throw new Exception(_('La fiche ') . $e_client . _('n\'a pas de poste comptable'), 8);
     }
     /* get the account and explode if necessary */
     $sposte = $fiche->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;
     }
     /* The account exists */
     $poste = new Acc_Account_Ledger($this->db, $poste_val);
     if ($poste->load() == false) {
         throw new Exception(_('Pour la fiche ') . $e_client . _(' le poste comptable [') . $poste->id . _('] n\'existe pas'), 9);
     }
     /* Check if the card belong to the ledger */
     $fiche = new Fiche($this->db);
     $fiche->get_by_qcode($e_client, 'deb');
     if ($fiche->belong_ledger($p_jrn) != 1) {
         throw new Exception(_('La fiche ') . $e_client . _('n\'est pas accessible à ce journal'), 10);
     }
     $nb = 0;
     //----------------------------------------
     // foreach item
     //----------------------------------------
     for ($i = 0; $i < $nb_item; $i++) {
         if (strlen(trim(${'e_march' . $i})) == 0) {
             continue;
         }
         /* check if amount are numeric and */
         if (isNumber(${'e_march' . $i . '_price'}) == 0) {
             throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('a un montant invalide [') . ${'e_march' . $i} . ']', 6);
         }
         if (isNumber(${'e_quant' . $i}) == 0) {
             throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('a une quantité invalide [') . ${'e_quant' . $i} . ']', 7);
         }
         /* check if all card has a ATTR_DEF_ACCOUNT */
         $fiche = new Fiche($this->db);
         $fiche->get_by_qcode(${'e_march' . $i});
         if ($fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true) {
             throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('n\'a pas de poste comptable'), 8);
         }
         // Check if the given tva id is valid
         if ($g_parameter->MY_TVA_USE == 'Y') {
             if (isNumber(${'e_march' . $i . '_tva_id'}) == 0) {
                 throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('a un code tva invalide') . ' [' . ${'e_march' . $i . '_tva_id'} . ']', 13);
             }
             $tva_rate = new Acc_Tva($this->db);
             $tva_rate->set_parameter('id', ${'e_march' . $i . '_tva_id'});
             if ($tva_rate->load() != 0) {
                 throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('a un code tva invalide') . ' [' . ${'e_march' . $i . '_tva_id'} . ']', 13);
             }
             /*
              * check if the accounting for VAT are valid
              */
             $a_poste = explode(',', $tva_rate->tva_poste);
             if ($this->db->get_value('select count(*) from tmp_pcmn where pcm_val=$1', array($a_poste[0])) == 0 || $this->db->get_value('select count(*) from tmp_pcmn where pcm_val=$1', array($a_poste[1])) == 0) {
                 throw new Exception(_(" La TVA " . $tva_rate->tva_label . " utilise des postes comptables inexistants"));
             }
         }
         // if 2 accounts, take only the credit one
         /* The account exists */
         $sposte = $fiche->strAttribut(ATTR_DEF_ACCOUNT);
         if (strpos($sposte, ',') != 0) {
             $array = explode(',', $sposte);
             $poste_val = $array[1];
         } else {
             $poste_val = $sposte;
         }
         $poste = new Acc_Account_Ledger($this->db, $poste_val);
         if ($poste->load() == false) {
             throw new Exception(_('Pour la fiche ') . ${'e_march' . $i} . _(' le poste comptable [') . $poste->id . _('n\'existe pas'), 9);
         }
         /* Check if the card belong to the ledger */
         $fiche = new Fiche($this->db);
         $fiche->get_by_qcode(${'e_march' . $i});
         if ($fiche->belong_ledger($p_jrn, 'cred') != 1) {
             throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('n\'est pas accessible à ce journal'), 10);
         }
         $nb++;
     }
     if ($nb == 0) {
         throw new Exception(_('Il n\'y a aucune marchandise'), 12);
     }
     //------------------------------------------------------
     // The "Paid By"  check
     //------------------------------------------------------
     if ($e_mp != 0) {
         $this->check_payment($e_mp, ${"e_mp_qcode_" . $e_mp});
     }
 }
 /**
  * Insert into JRNX the No Deductible amount and into Analytic Accountancy for the ND VAT
  * @param Acc_Compute $p_nd_amount content ND amount
  * @param Fiche $p_fiche Card of the Service
  * @param type $p_tva_both  0 if TVA is normal or 1 if on both side
  * @param type $p_tot_debit total debit
  * @param $p_acc_operation Acc_Operation for inserting into jrnx
  * @param $p_group group for AC
  * @param $idx row number
  * 
  * @see Acc_Ledger_Purchase::insert
  */
 private function insert_no_deductible(Acc_Compute $p_nd_amount, Fiche $p_fiche, $p_tva_both, &$p_tot_debit, Acc_Operation $p_acc_operation, $p_group, $idx)
 {
     global $g_parameter;
     if ($p_acc_operation->jrnx_id == 0) {
         throw new Exception(__FILE__ . __LINE__ . 'invalid acc_operation.j_id');
     }
     $source_j_id = $p_acc_operation->jrnx_id;
     /*
      * Save all the no deductible
      *     ATTR_DEF_ACCOUNT_ND_TVA,ATTR_DEF_ACCOUNT_ND_TVA_ND,ATTR_DEF_ACCOUNT_ND_PERSO,ATTR_DEF_ACCOUNT_ND
      */
     if ($p_nd_amount->amount_nd_rate != 0) {
         $dna_default = new Acc_Parm_Code($this->db, 'DNA');
         /* save op. */
         if (!$p_fiche->empty_attribute(ATTR_DEF_ACCOUNT_ND)) {
             $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND);
         } else {
             $dna = $dna_default->p_value;
         }
         $dna = $dna == '' ? $dna_default->p_value : $dna;
         $p_acc_operation->type = 'd';
         $p_acc_operation->amount = $p_nd_amount->amount_nd;
         $p_acc_operation->poste = $dna;
         $p_acc_operation->qcode = '';
         $p_acc_operation->desc = $this->find_label($dna) . " ND " . $p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
         if ($p_nd_amount->amount_nd > 0) {
             $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->amount_nd);
         }
         $j_id = $p_acc_operation->insert_jrnx();
     }
     /*
      * ATTR_DEF_ACCOUNT_ND_PERSO
      */
     if ($p_nd_amount->amount_perso != 0) {
         $dna_default = new Acc_Parm_Code($this->db, 'DEP_PRIV');
         /* save op. */
         $p_acc_operation->type = 'd';
         if (!$p_fiche->empty_attribute(ATTR_DEF_ACCOUNT_ND_PERSO)) {
             $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND_PERSO);
         } else {
             $dna = $dna_default->p_value;
         }
         $dna = $dna == '' ? $dna_default->p_value : $dna;
         $p_acc_operation->amount = $p_nd_amount->amount_perso;
         $p_acc_operation->poste = $dna;
         $p_acc_operation->qcode = '';
         $p_acc_operation->desc = $this->find_label($dna) . " ND_PRIV " . $p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
         if ($p_nd_amount->amount_perso > 0) {
             $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->amount_perso);
         }
         $j_id = $p_acc_operation->insert_jrnx();
     }
     if ($p_nd_amount->nd_vat != 0) {
         $dna_default = new Acc_Parm_Code($this->db, 'TVA_DNA');
         /* save op. */
         $p_acc_operation->type = 'd';
         $p_acc_operation->qcode = '';
         if (!$p_fiche->empty_attribute(ATTR_DEF_ACCOUNT_ND_TVA_ND)) {
             $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND_TVA_ND);
         } else {
             $dna = $dna_default->p_value;
         }
         $dna = $dna == '' ? $dna_default->p_value : $dna;
         $p_acc_operation->amount = $p_nd_amount->nd_vat;
         $p_acc_operation->poste = $dna;
         $p_acc_operation->desc = $this->find_label($dna) . " ND_TVA " . $p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
         $j_id = $p_acc_operation->insert_jrnx();
         if ($g_parameter->MY_ANALYTIC != "nu") {
             $op = new Anc_Operation($this->db);
             $op->oa_group = $p_group;
             $op->j_id = $j_id;
             $op->oa_date = $p_acc_operation->date;
             $op->oa_debit = 't';
             $op->oa_description = sql_string('ND_TVA');
             $op->oa_jrnx_id_source = $source_j_id;
             $op->save_form_plan_vat_nd($_POST, $idx, $j_id, $p_nd_amount->nd_vat, $p_acc_operation->jrnx_id);
         }
         if ($p_nd_amount->nd_vat > 0) {
             $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->nd_vat);
         }
     }
     if ($p_nd_amount->nd_ded_vat != 0) {
         $dna_default = new Acc_Parm_Code($this->db, 'TVA_DED_IMPOT');
         /* save op. */
         if (!$p_fiche->empty_attribute(ATTR_DEF_ACCOUNT_ND_TVA)) {
             $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND_TVA);
         } else {
             $dna = $dna_default->p_value;
         }
         $dna = $dna == '' ? $dna_default->value : $dna;
         $p_acc_operation->type = 'd';
         $p_acc_operation->qcode = '';
         $p_acc_operation->amount = $p_nd_amount->nd_ded_vat;
         $p_acc_operation->poste = $dna;
         $p_acc_operation->desc = $this->find_label($dna) . " DED_TVA " . $p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
         if ($p_nd_amount->nd_ded_vat > 0) {
             $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->nd_ded_vat);
         }
         $j_id = $p_acc_operation->insert_jrnx();
         if ($g_parameter->MY_ANALYTIC != "nu") {
             $op = new Anc_Operation($this->db);
             $op->oa_group = $p_group;
             $op->j_id = $j_id;
             $op->oa_date = $p_acc_operation->date;
             $op->oa_debit = 't';
             $op->oa_description = sql_string('DED_TVA ');
             $op->oa_jrnx_id_source = $source_j_id;
             $op->save_form_plan_vat_nd($_POST, $idx, $j_id, $p_nd_amount->nd_ded_vat);
         }
     }
 }