Ejemplo n.º 1
0
 /**
  * @brief get_detail gives the detail of row
  * this array must contains at least the field
  *       <ul>
  *       <li> montant</li>
  *       <li> grpt_id
  *       </ul>
  * the following field will be added
  *       <ul>
  *       <li> HTVA
  *       <li> TVAC
  *       <li> TVA array with
  *          <ul>
  *          <li> field 0 idx
  *          <li> array containing tva_id,tva_label and tva_amount
  *          </ul>
  *       </ul>
  *
  * @paramp_array the structure is set in get_rowSimple, this array is
  *        modified,
  * @param $trunc if the data must be truncated, usefull for pdf export
  * @paramp_jrn_type is the type of the ledger (ACH or VEN)
  * @param $a_TVA TVA Array (default null)
  * @param $a_ParmCode Array (default null)
  * \return p_array
  */
 function get_detail(&$p_array, $p_jrn_type, $trunc = 0, $a_TVA = null, $a_ParmCode = null)
 {
     bcscale(2);
     if ($a_TVA == null) {
         //Load TVA array
         $a_TVA = $this->db->get_array('select tva_id,tva_label,tva_poste
                                     from tva_rate where tva_rate != 0 order by tva_rate,tva_label,tva_id');
     }
     if ($a_ParmCode == null) {
         //Load Parm_code
         $a_ParmCode = $this->db->get_array('select p_code,p_value from parm_code');
     }
     // init
     $p_array['client'] = "";
     $p_array['TVAC'] = 0;
     $p_array['TVA'] = array();
     $p_array['AMOUNT_TVA'] = 0.0;
     $p_array['dep_priv'] = 0;
     $p_array['dna'] = 0;
     $p_array['tva_dna'] = 0;
     $p_array['tva_np'] = 0;
     $dep_priv = 0.0;
     //
     // Retrieve data from jrnx
     $sql = "select j_id,j_poste,j_montant, j_debit,j_qcode from jrnx where " . " j_grpt=" . $p_array['grpt_id'];
     $Res2 = $this->db->exec_sql($sql);
     $data_jrnx = Database::fetch_all($Res2);
     $c = 0;
     // Parse data from jrnx and fill diff. field
     foreach ($data_jrnx as $code) {
         $idx_tva = 0;
         $poste = new Acc_Account_Ledger($this->db, $code['j_poste']);
         // if card retrieve name if the account is not a VAT account
         if (strlen(trim($code['j_qcode'])) != 0 && $poste->isTva() == 0) {
             $fiche = new Fiche($this->db);
             $fiche->get_by_qcode(trim($code['j_qcode']), false);
             $fiche_def_id = $fiche->get_fiche_def_ref_id();
             // Customer or supplier
             if ($fiche_def_id == FICHE_TYPE_CLIENT || $fiche_def_id == FICHE_TYPE_FOURNISSEUR || $fiche_def_id == FICHE_TYPE_ADM_TAX) {
                 $p_array['TVAC'] = $code['j_montant'];
                 $p_array['client'] = $trunc == 0 ? $fiche->getName() : mb_substr($fiche->getName(), 0, 20);
                 $p_array['reversed'] = false;
                 if ($fiche_def_id == FICHE_TYPE_CLIENT && $code['j_debit'] == 'f') {
                     $p_array['reversed'] = true;
                     $p_array['TVAC'] *= -1;
                 }
                 if ($fiche_def_id == FICHE_TYPE_ADM_TAX && $code['j_debit'] == 'f') {
                     $p_array['reversed'] = true;
                     $p_array['TVAC'] *= -1;
                 }
                 if ($fiche_def_id == FICHE_TYPE_FOURNISSEUR && $code['j_debit'] == 't') {
                     $p_array['reversed'] = true;
                     $p_array['TVAC'] *= -1;
                 }
             } else {
                 // if we use the ledger ven / ach for others card than supplier and customer
                 if ($fiche_def_id != FICHE_TYPE_VENTE && $fiche_def_id != FICHE_TYPE_ACH_MAR && $fiche_def_id != FICHE_TYPE_ACH_SER && $fiche_def_id != FICHE_TYPE_ACH_MAT) {
                     $p_array['TVAC'] = $code['j_montant'];
                     $p_array['client'] = $trunc == 0 ? $fiche->getName() : mb_substr($fiche->getName(), 0, 20);
                     $p_array['reversed'] = false;
                     if ($p_jrn_type == 'ACH' && $code['j_debit'] == 't') {
                         $p_array['reversed'] = true;
                         $p_array['TVAC'] *= -1;
                     }
                     if ($p_jrn_type == 'VEN' && $code['j_debit'] == 'f') {
                         $p_array['reversed'] = true;
                         $p_array['TVAC'] *= -1;
                     }
                 }
             }
         }
         // if TVA, load amount, tva id and rate in array
         foreach ($a_TVA as $line_tva) {
             list($tva_deb, $tva_cred) = explode(',', $line_tva['tva_poste']);
             if ($code['j_poste'] == $tva_deb || $code['j_poste'] == $tva_cred) {
                 // For the reversed operation
                 if ($p_jrn_type == 'ACH' && $code['j_debit'] == 'f') {
                     $code['j_montant'] = -1 * $code['j_montant'];
                 }
                 if ($p_jrn_type == 'VEN' && $code['j_debit'] == 't') {
                     $code['j_montant'] = -1 * $code['j_montant'];
                 }
                 $p_array['AMOUNT_TVA'] += $code['j_montant'];
                 $p_array['TVA'][$c] = array($idx_tva, array($line_tva['tva_id'], $line_tva['tva_label'], $code['j_montant']));
                 $c++;
                 $idx_tva++;
             }
         }
         // isDNA
         // If operation is reversed then  amount are negatif
         /* if ND */
         if ($p_array['jrn_def_type'] == 'ACH') {
             $purchase = new Gestion_Purchase($this->db);
             $purchase->search_by_jid($code['j_id']);
             $purchase->load();
             $dep_priv += $purchase->qp_dep_priv;
             $p_array['dep_priv'] = $dep_priv;
             $p_array['dna'] = bcadd($p_array['dna'], $purchase->qp_nd_amount);
             $p_array['tva_dna'] = bcadd($p_array['tva_dna'], bcadd($purchase->qp_nd_tva, $purchase->qp_nd_tva_recup));
             $p_array['tva_np'] = bcadd($purchase->qp_vat_sided, $p_array['tva_np']);
         }
         if ($p_array['jrn_def_type'] == 'VEN') {
             $sold = new gestion_sold($this->db);
             $sold->search_by_jid($code['j_id']);
             $sold->load();
             $p_array['tva_np'] = bcadd($sold->qs_vat_sided, $p_array['tva_np']);
         }
     }
     $p_array['TVAC'] = sprintf('% 10.2f', $p_array['TVAC']);
     $p_array['HTVA'] = sprintf('% 10.2f', $p_array['TVAC'] - $p_array['AMOUNT_TVA'] - $p_array['tva_dna']);
     $r = "";
     $a_tva_amount = array();
     // inline TVA (used for the PDF)
     foreach ($p_array['TVA'] as $linetva) {
         foreach ($a_TVA as $tva) {
             if ($tva['tva_id'] == $linetva[1][0]) {
                 $a = $tva['tva_id'];
                 $a_tva_amount[$a] = $linetva[1][2];
             }
         }
     }
     foreach ($a_TVA as $line_tva) {
         $a = $line_tva['tva_id'];
         if (isset($a_tva_amount[$a])) {
             $tmp = sprintf("% 10.2f", $a_tva_amount[$a]);
             $r .= "{$tmp}";
         } else {
             $r .= sprintf("% 10.2f", 0);
         }
     }
     $p_array['TVA_INLINE'] = $r;
     return $p_array;
 }