noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_acc_ledger_sold.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  *   This file is part of NOALYSS.
00005  *
00006  *   NOALYSS is free software; you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation; either version 2 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   NOALYSS is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with NOALYSS; if not, write to the Free Software
00018  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00022 
00023 /* !\file
00024  * \brief class for the sold, herits from acc_ledger
00025  */
00026 require_once("class_iselect.php");
00027 require_once("class_icard.php");
00028 require_once("class_ispan.php");
00029 require_once("class_ihidden.php");
00030 require_once("class_idate.php");
00031 require_once("class_itext.php");
00032 require_once("class_ifile.php");
00033 require_once('class_acc_ledger.php');
00034 require_once('class_acc_compute.php');
00035 require_once('class_anc_operation.php');
00036 require_once('user_common.php');
00037 require_once('class_acc_payment.php');
00038 require_once('ac_common.php');
00039 require_once('class_own.php');
00040 require_once('class_itva_popup.php');
00041 require_once('class_acc_ledger_fin.php');
00042 require_once 'class_stock_goods.php';
00043 require_once 'class_acc_ledger.php';
00044 
00045 /* !\brief Handle the ledger of sold,
00046  *
00047  * @exception throw an exception is something is wrong
00048  */
00049 
00050 class Acc_Ledger_Sold extends Acc_Ledger {
00051 
00052     function __construct($p_cn, $p_init) {
00053         parent::__construct($p_cn, $p_init);
00054         $this->type = 'VEN';
00055     }
00056 
00057     /* !\brief verify that the data are correct before inserting or confirming
00058      * \param an array (usually $_POST)
00059      * \return String
00060      * \throw Exception if an error occurs
00061      */
00062 
00063     public function verify($p_array) {
00064         global $g_parameter, $g_user;
00065         
00066         if (is_array($p_array ) == false || empty($p_array))
00067                     throw new Exception ("Array empty");
00068         
00069         extract($p_array);
00070         
00071         /*
00072          * Check needed value
00073          */
00074         check_parameter($p_array,'p_jrn,e_date,e_client');
00075 
00076         /* check for a double reload */
00077         if (isset($mt) && $this->db->count_sql('select jr_mt from jrn where jr_mt=$1', array($mt)) != 0)
00078             throw new Exception(_('Double Encodage'), 5);
00079 
00080         /* check if we can write into this ledger */
00081         if ($g_user->check_jrn($p_jrn) != 'W')
00082             throw new Exception(_('Accès interdit'), 20);
00083 
00084         /* check if there is a customer */
00085         if (strlen(trim($e_client)) == 0)
00086             throw new Exception(_('Vous n\'avez pas donné de client'), 11);
00087 
00088         /*  check if the date is valid */
00089         if (isDate($e_date) == null) {
00090             throw new Exception(_('Date invalide'), 2);
00091         }
00092 
00093         $oPeriode = new Periode($this->db);
00094         if ($this->check_periode() == true) {
00095             $tperiode = $period;
00096             /* check that the datum is in the choosen periode */
00097             $oPeriode->p_id = $period;
00098             list ($min, $max) = $oPeriode->get_date_limit();
00099 
00100             if (cmpDate($e_date, $min) < 0 ||
00101                     cmpDate($e_date, $max) > 0)
00102                 throw new Exception(_('Date et periode ne correspondent pas'), 6);
00103         }
00104         else {
00105             $per = new Periode($this->db);
00106             $tperiode = $per->find_periode($e_date);
00107         }
00108 
00109         /* check if the periode is closed */
00110         if ($this->is_closed($tperiode) == 1) {
00111             throw new Exception(_('Periode fermee'), 6);
00112         }
00113         /* check if we are using the strict mode */
00114         if ($this->check_strict() == true) {
00115             /* if we use the strict mode, we get the date of the last
00116               operation */
00117             $last_date = $this->get_last_date();
00118             if ($last_date != null && cmpDate($e_date, $last_date) < 0)
00119                 throw new Exception(_('Vous utilisez le mode strict la dernière operation est date du ')
00120                 . $last_date . _(' vous ne pouvez pas encoder à une date antérieure'), 13);
00121         }
00122 
00123 
00124         $fiche = new Fiche($this->db);
00125         $fiche->get_by_qcode($e_client);
00126         if ($fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true)
00127             throw new Exception(_('La fiche ') . $e_client . _('n\'a pas de poste comptable'), 8);
00128 
00129         /* get the account and explode if necessary */
00130         $sposte = $fiche->strAttribut(ATTR_DEF_ACCOUNT);
00131         // if 2 accounts, take only the debit one for customer
00132         if (strpos($sposte, ',') != 0) {
00133             $array = explode(',', $sposte);
00134             $poste_val = $array[0];
00135         } else {
00136             $poste_val = $sposte;
00137         }
00138         /* The account exists */
00139 
00140         $poste = new Acc_Account_Ledger($this->db, $poste_val);
00141 
00142         if ($poste->load() == false) {
00143             throw new Exception(_('Pour la fiche ') . $e_client . _(' le poste comptable [') . $poste->id . _('] n\'existe pas'), 9);
00144         }
00145 
00146         /* Check if the card belong to the ledger */
00147         $fiche = new Fiche($this->db);
00148         $fiche->get_by_qcode($e_client, 'deb');
00149         if ($fiche->belong_ledger($p_jrn) != 1)
00150             throw new Exception(_('La fiche ') . $e_client . _('n\'est pas accessible à ce journal'), 10);
00151 
00152         $nb = 0;
00153 
00154         //----------------------------------------
00155         // foreach item
00156         //----------------------------------------
00157         for ($i = 0; $i < $nb_item; $i++) {
00158             if (strlen(trim(${'e_march' . $i})) == 0)
00159                 continue;
00160             /* check if amount are numeric and */
00161             if (isNumber(${'e_march' . $i . '_price'}) == 0)
00162                 throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('a un montant invalide [') . ${'e_march' . $i} . ']', 6);
00163             if (isNumber(${'e_quant' . $i}) == 0)
00164                 throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('a une quantité invalide [') . ${'e_quant' . $i} . ']', 7);
00165             /* check if all card has a ATTR_DEF_ACCOUNT */
00166             $fiche = new Fiche($this->db);
00167             $fiche->get_by_qcode(${'e_march' . $i});
00168             if ($fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true)
00169                 throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('n\'a pas de poste comptable'), 8);
00170 
00171             // Check if the given tva id is valid
00172             if ($g_parameter->MY_TVA_USE == 'Y') {
00173                 if (isNumber(${'e_march' . $i . '_tva_id'}) == 0)
00174                     throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('a un code tva invalide') . ' [' . ${'e_march' . $i . '_tva_id'} . ']', 13);
00175                 $tva_rate = new Acc_Tva($this->db);
00176                 $tva_rate->set_parameter('id', ${'e_march' . $i . '_tva_id'});
00177                 if ($tva_rate->load() != 0)
00178                     throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('a un code tva invalide') . ' [' . ${'e_march' . $i . '_tva_id'} . ']', 13);
00179 
00180                 /*
00181                  * check if the accounting for VAT are valid
00182                  */
00183                 $a_poste = explode(',', $tva_rate->tva_poste);
00184 
00185                 if (
00186                         $this->db->get_value('select count(*) from tmp_pcmn where pcm_val=$1', array($a_poste[0])) == 0 ||
00187                         $this->db->get_value('select count(*) from tmp_pcmn where pcm_val=$1', array($a_poste[1])) == 0)
00188                     throw new Exception(_(" La TVA " . $tva_rate->tva_label . " utilise des postes comptables inexistants"));
00189             }
00190             // if 2 accounts, take only the credit one
00191             /* The account exists */
00192             $sposte = $fiche->strAttribut(ATTR_DEF_ACCOUNT);
00193 
00194             if (strpos($sposte, ',') != 0) {
00195                 $array = explode(',', $sposte);
00196                 $poste_val = $array[1];
00197             } else {
00198                 $poste_val = $sposte;
00199             }
00200             $poste = new Acc_Account_Ledger($this->db, $poste_val);
00201             if ($poste->load() == false) {
00202                 throw new Exception(_('Pour la fiche ') . ${'e_march' . $i} . _(' le poste comptable [') . $poste->id . _('n\'existe pas'), 9);
00203             }
00204             /* Check if the card belong to the ledger */
00205             $fiche = new Fiche($this->db);
00206             $fiche->get_by_qcode(${'e_march' . $i});
00207             if ($fiche->belong_ledger($p_jrn, 'cred') != 1)
00208                 throw new Exception(_('La fiche ') . ${'e_march' . $i} . _('n\'est pas accessible à ce journal'), 10);
00209             $nb++;
00210         }
00211         if ($nb == 0)
00212             throw new Exception(_('Il n\'y a aucune marchandise'), 12);
00213         //------------------------------------------------------
00214         // The "Paid By"  check
00215         //------------------------------------------------------
00216 
00217         if ($e_mp != 0) {
00218             $this->check_payment($e_mp, ${"e_mp_qcode_" . $e_mp});
00219         }
00220     }
00221 
00222     /* !\brief insert into the database, it calls first the verify function,
00223      * change the value of this->jr_id and this->jr_internal
00224      * * It generates the document if gen_invoice is set and save the middle of payment if any ($e_mp)
00225      *
00226      * \param $p_array is usually $_POST or a predefined operation
00227      * \return string
00228      * \note throw an Exception
00229      */
00230 
00231     public function insert($p_array = null) {
00232         global $g_parameter;
00233         extract($p_array);
00234         $this->verify($p_array);
00235 
00236         $group = $this->db->get_next_seq("s_oa_group"); /* for analytic */
00237         $seq = $this->db->get_next_seq('s_grpt');
00238         $this->id = $p_jrn;
00239         $internal = $this->compute_internal_code($seq);
00240         $this->internal = $internal;
00241 
00242         $oPeriode = new Periode($this->db);
00243         $check_periode = $this->check_periode();
00244 
00245         if ($check_periode == true)
00246             $tperiode = $period;
00247         else
00248             $tperiode = $oPeriode->find_periode($e_date);
00249 
00250         $cust = new Fiche($this->db);
00251         $cust->get_by_qcode($e_client);
00252         $sposte = $cust->strAttribut(ATTR_DEF_ACCOUNT);
00253 
00254         // if 2 accounts, take only the debit one for the customer
00255         //
00256         if (strpos($sposte, ',') != 0) {
00257             $array = explode(',', $sposte);
00258             $poste = $array[0];
00259         } else {
00260             $poste = $sposte;
00261         }
00262 
00263         bcscale(4);
00264         try {
00265             $tot_amount = 0;
00266             $tot_tva = 0;
00267             $tot_debit = 0;
00268             $this->db->start();
00269             $tva = array();
00270             /* Save all the items without vat */
00271             for ($i = 0; $i < $nb_item; $i++) {
00272                 $n_both = 0;
00273                 if (strlen(trim(${'e_march' . $i})) == 0)
00274                     continue;
00275 
00276                 /* First we save all the items without vat */
00277                 $fiche = new Fiche($this->db);
00278                 $fiche->get_by_qcode(${"e_march" . $i});
00279                 $amount = bcmul(${'e_march' . $i . '_price'}, ${'e_quant' . $i});
00280                 $tot_amount = bcadd($tot_amount, $amount);
00281                 $acc_operation = new Acc_Operation($this->db);
00282                 $acc_operation->date = $e_date;
00283                 $sposte = $fiche->strAttribut(ATTR_DEF_ACCOUNT);
00284 
00285                 // if 2 accounts, take only the credit one
00286                 if (strpos($sposte, ',') != 0) {
00287                     $array = explode(',', $sposte);
00288                     $poste_val = $array[1];
00289                 } else {
00290                     $poste_val = $sposte;
00291                 }
00292 
00293                 $acc_operation->poste = $poste_val;
00294                 $acc_operation->amount = $amount;
00295                 $acc_operation->grpt = $seq;
00296                 $acc_operation->jrn = $p_jrn;
00297                 $acc_operation->type = 'c';
00298                 $acc_operation->periode = $tperiode;
00299                 if ($g_parameter->MY_UPDLAB == 'Y')
00300                     $acc_operation->desc = strip_tags(${"e_march" . $i . "_label"});
00301                 else
00302                     $acc_operation->desc = null;
00303 
00304                 $acc_operation->qcode = ${"e_march" . $i};
00305                 if ($amount < 0)
00306                     $tot_debit = bcadd($tot_debit, abs($amount));
00307 
00308                 $j_id = $acc_operation->insert_jrnx();
00309 
00310                 if ($g_parameter->MY_TVA_USE == 'Y') {
00311                     /* Compute sum vat */
00312                     $oTva = new Acc_Tva($this->db);
00313                     $idx_tva = ${'e_march' . $i . '_tva_id'};
00314                     $tva_item = ${'e_march' . $i . '_tva_amount'};
00315                     $oTva->set_parameter("id", $idx_tva);
00316                     $oTva->load();
00317                     /* if empty then we need to compute it */
00318                     if (trim($tva_item) == '' || ${'e_march'.$i.'_tva_amount'} == 0) {
00319                         /* retrieve tva */
00320                         $l = new Acc_Tva($this->db, $idx_tva);
00321                         $l->load();
00322                         $tva_item = bcmul($amount, $l->get_parameter('rate'));
00323                     }
00324                     if (isset($tva[$idx_tva]))
00325                         $tva[$idx_tva]+=$tva_item;
00326                     else
00327                         $tva[$idx_tva] = $tva_item;
00328                     if ($oTva->get_parameter("both_side") == 0) {
00329                         $tot_tva = round(bcadd($tva_item, $tot_tva), 2);
00330                     } else {
00331                         $n_both = $tva_item;
00332                         if ($n_both < 0)
00333                             $tot_debit = bcadd($tot_debit, abs($n_both));
00334                     }
00335                 }
00336 
00337                 /* Save the stock */
00338                 /* if the quantity is < 0 then the stock increase (return of
00339                  *  material)
00340                  */
00341                 $nNeg = (${"e_quant" . $i} < 0) ? -1 : 1;
00342 
00343                 // always save quantity but in withStock we can find
00344                 // what card need a stock management
00345                 if ($g_parameter->MY_STOCK = 'Y' && isset($repo))
00346                 {
00347                     $dir=(${'e_quant'.$i} < 0 ) ? 'd':'c';
00348                     Stock_Goods::insert_goods($this->db, array('j_id' => $j_id, 'goods' => ${'e_march' . $i}, 'quant' => $nNeg * ${'e_quant' . $i}, 'dir' => $dir, 'repo' => $repo));
00349                 }
00350 
00351 
00352                 if ($g_parameter->MY_ANALYTIC != "nu") {
00353                     // for each item, insert into operation_analytique */
00354                     $op = new Anc_Operation($this->db);
00355                     $op->oa_group = $group;
00356                     $op->j_id = $j_id;
00357                     $op->oa_date = $e_date;
00358                     $op->oa_debit = 'f';
00359                     $op->oa_description = sql_string($e_comm);
00360                     $op->save_form_plan($_POST, $i, $j_id);
00361                 }
00362                 if ($g_parameter->MY_TVA_USE == 'Y') {
00363                     /* save into quant_sold */
00364                     $r = $this->db->exec_sql("select insert_quant_sold ($1,$2,$3,$4,$5,$6,$7,$8,$9)", array(null, /* 1 */
00365                         $j_id, /* 2 */
00366                         ${'e_march' . $i}, /* 3 */
00367                         ${'e_quant' . $i}, /* 4 */
00368                         round($amount, 2), /* 5 */
00369                         $tva_item, /* 6 */
00370                         $idx_tva, /* 7 */
00371                         $e_client, /* 8 */
00372                         $n_both));
00373                 } else {
00374                     $r = $this->db->exec_sql("select insert_quant_sold ($1,$2,$3,$4,$5,$6,$7,$8,$9) ", array(null, /* 1 */
00375                         $j_id, /* 2 */
00376                         ${'e_march' . $i}, /* 3 */
00377                         ${'e_quant' . $i}, /* 4 */
00378                         $amount, // 5
00379                         0,
00380                         null,
00381                         $e_client,
00382                         0));
00383                 }  // if ( $g_parameter->MY_TVA_USE=='Y') {
00384             }// end loop : save all items
00385 
00386             /*  save total customer */
00387             $cust_amount = bcadd($tot_amount, $tot_tva);
00388             $acc_operation = new Acc_Operation($this->db);
00389             $acc_operation->date = $e_date;
00390             $acc_operation->poste = $poste;
00391             $acc_operation->amount = $cust_amount;
00392             $acc_operation->grpt = $seq;
00393             $acc_operation->jrn = $p_jrn;
00394             $acc_operation->type = 'd';
00395             $acc_operation->periode = $tperiode;
00396             $acc_operation->qcode = ${"e_client"};
00397             if ($cust_amount > 0)
00398                 $tot_debit = bcadd($tot_debit, $cust_amount);
00399             $let_tiers = $acc_operation->insert_jrnx();
00400 
00401 
00402             /** save all vat
00403              * $i contains the tva_id and value contains the vat amount
00404              * if if ($g_parameter->MY_TVA_USE == 'Y' )
00405              */
00406             if ($g_parameter->MY_TVA_USE == 'Y') {
00407                 foreach ($tva as $i => $value) {
00408                     $oTva = new Acc_Tva($this->db);
00409                     $oTva->set_parameter('id', $i);
00410                     $oTva->load();
00411 
00412                     $poste_vat = $oTva->get_side('c');
00413 
00414                     $cust_amount = bcadd($tot_amount, $tot_tva);
00415                     $acc_operation = new Acc_Operation($this->db);
00416                     $acc_operation->date = $e_date;
00417                     $acc_operation->poste = $poste_vat;
00418                     $acc_operation->amount = $value;
00419                     $acc_operation->grpt = $seq;
00420                     $acc_operation->jrn = $p_jrn;
00421                     $acc_operation->type = 'c';
00422                     $acc_operation->periode = $tperiode;
00423                     if ($value < 0)
00424                         $tot_debit = bcadd($tot_debit, abs($value));
00425                     $acc_operation->insert_jrnx();
00426 
00427                     // if TVA is on both side, we deduce it immediately
00428                     if ($oTva->get_parameter("both_side") == 1) {
00429                         $poste_vat = $oTva->get_side('d');
00430                         $cust_amount = bcadd($tot_amount, $tot_tva);
00431                         $acc_operation = new Acc_Operation($this->db);
00432                         $acc_operation->date = $e_date;
00433                         $acc_operation->poste = $poste_vat;
00434                         $acc_operation->amount = $value;
00435                         $acc_operation->grpt = $seq;
00436                         $acc_operation->jrn = $p_jrn;
00437                         $acc_operation->type = 'd';
00438                         $acc_operation->periode = $tperiode;
00439                         $acc_operation->insert_jrnx();
00440                         $tot_debit = bcadd($tot_debit, $value);
00441                         $n_both = $value;
00442                     }
00443                 }
00444             } // if ($g_parameter->MY_TVA_USE=='Y')
00445             /* insert into jrn */
00446             $acc_operation = new Acc_Operation($this->db);
00447             $acc_operation->date = $e_date;
00448             $acc_operation->echeance = $e_ech;
00449             $acc_operation->amount = abs(round($tot_debit, 2));
00450             $acc_operation->desc = $e_comm;
00451             $acc_operation->grpt = $seq;
00452             $acc_operation->jrn = $p_jrn;
00453             $acc_operation->periode = $tperiode;
00454             $acc_operation->pj = $e_pj;
00455             $acc_operation->mt = $mt;
00456 
00457             $this->jr_id = $acc_operation->insert_jrn();
00458 
00459             $this->pj = $acc_operation->set_pj();
00460 
00461             /*             * = e_pj then do not increment sequence */
00462             /* and e_pj is not null */
00463             if (strcmp($e_pj, $e_pj_suggest) == 0 && strlen(trim($e_pj)) != 0) {
00464                 $this->inc_seq_pj();
00465             }
00466 
00467             $this->db->exec_sql("update jrn set jr_internal='" . $internal . "' where " .
00468                     " jr_grpt_id = " . $seq);
00469 
00470             /* update quant_sold */
00471             $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));
00472 
00473             /* Save the attachment or generate doc */
00474             if (isset($_FILES['pj'])) {
00475                 if (strlen(trim($_FILES['pj']['name'])) != 0)
00476                     $this->db->save_upload_document($seq);
00477                 else
00478                 /* Generate an invoice and save it into the database */
00479                 if (isset($_POST['gen_invoice'])) {
00480                     $file = $this->create_document($internal, $p_array);
00481                     $this->doc='<A class="line" HREF="show_pj.php?' . dossier::get() . '&jr_grpt_id=' . $seq . '&jrn=' . $this->id . '">' . $file . '</A>';
00482                 }
00483             }
00484             //----------------------------------------
00485             // Save the payer
00486             //----------------------------------------
00487             if ($e_mp != 0) {
00488                 /* mp */
00489                 $mp = new Acc_Payment($this->db, $e_mp);
00490                 $mp->load();
00491 
00492                 /* fiche */
00493                 $fqcode = ${'e_mp_qcode_' . $e_mp};
00494                 $acfiche = new Fiche($this->db);
00495                 $acfiche->get_by_qcode($fqcode);
00496 
00497                 /* jrnx */
00498                 $acseq = $this->db->get_next_seq('s_grpt');
00499                 $acjrn = new Acc_Ledger($this->db, $mp->get_parameter('ledger_target'));
00500                 $acinternal = $acjrn->compute_internal_code($acseq);
00501 
00502                 /* Insert paid by  */
00503                 $acc_pay = new Acc_Operation($this->db);
00504                 $acc_pay->date = $e_date;
00505                 /* get the account and explode if necessary */
00506                 $sposte = $acfiche->strAttribut(ATTR_DEF_ACCOUNT);
00507                 // if 2 accounts, take only the debit one for customer
00508                 if (strpos($sposte, ',') != 0) {
00509                     $array = explode(',', $sposte);
00510                     $poste_val = $array[0];
00511                 } else {
00512                     $poste_val = $sposte;
00513                 }
00514                 $famount = bcsub($cust_amount, $acompte);
00515                 $acc_pay->poste = $poste_val;
00516                 $acc_pay->qcode = $fqcode;
00517                 $acc_pay->amount = abs(round($famount, 2));
00518                 $acc_pay->desc = null;
00519 
00520                 $acc_pay->grpt = $acseq;
00521                 $acc_pay->jrn = $mp->get_parameter('ledger_target');
00522                 $acc_pay->periode = $tperiode;
00523                 $acc_pay->type = ($famount >= 0) ? 'd' : 'c';
00524                 $acc_pay->insert_jrnx();
00525 
00526                 /* Insert supplier  */
00527                 $acc_pay = new Acc_Operation($this->db);
00528                 $acc_pay->date = $e_date;
00529                 $acc_pay->poste = $poste;
00530                 $acc_pay->qcode = $e_client;
00531                 $acc_pay->amount = abs(round($famount, 2));
00532                 $acc_pay->desc = null;
00533                 $acc_pay->grpt = $acseq;
00534                 $acc_pay->jrn = $mp->get_parameter('ledger_target');
00535                 $acc_pay->periode = $tperiode;
00536                 $acc_pay->type = ($famount >= 0) ? 'c' : 'd';
00537                 $let_other = $acc_pay->insert_jrnx();
00538 
00539                 /* insert into jrn */
00540                 $acc_pay->mt = $mt;
00541                 $acjrn->grpt_id = $acseq;
00542                 $acc_pay->desc = (!isset($e_comm_paiement) || strlen(trim($e_comm_paiement)) == 0) ? $e_comm : $e_comm_paiement;
00543                 $mp_jr_id = $acc_pay->insert_jrn();
00544                 $acjrn->update_internal_code($acinternal);
00545 
00546                 $r1 = $this->get_id($internal);
00547                 $r2 = $this->get_id($acinternal);
00548 
00549                 /*
00550                  * add lettering
00551                  */
00552                 $oletter = new Lettering($this->db);
00553                 $oletter->insert_couple($let_tiers, $let_other);
00554 
00555 
00556                 /* set the flag paid */
00557                 $Res = $this->db->exec_sql("update jrn set jr_rapt='paid' where jr_id=$1", array($r1));
00558 
00559                 /* Reconcialiation */
00560                 $rec = new Acc_Reconciliation($this->db);
00561                 $rec->set_jr_id($r1);
00562                 $rec->insert($r2);
00563 
00564 
00565                 /*
00566                  * save also into quant_fin
00567                  */
00568 
00569                 /* get ledger property */
00570                 $ledger = new Acc_Ledger_Fin($this->db, $acc_pay->jrn);
00571                 $prop = $ledger->get_propertie();
00572 
00573                 /* if ledger is FIN then insert into quant_fin */
00574                 if ($prop['jrn_def_type'] == 'FIN') {
00575                     $ledger->insert_quant_fin($acfiche->id, $mp_jr_id, $cust->id, bcmul($famount, 1));
00576                 }
00577             }
00578         } catch (Exception $e) {
00579             echo '<span class="error">' .
00580             'Erreur dans l\'enregistrement ' .
00581             __FILE__ . ':' . __LINE__ . ' ' .
00582             $e->getMessage();
00583             echo $e->getTrace();
00584 
00585             $this->db->rollback();
00586             throw new Exception ($e);
00587         }
00588         $this->db->commit();
00589 
00590         return $internal;
00591     }
00592 
00593     /* !
00594      * @brief show the summary of the operation and propose to save it
00595      * @param array contains normally $_POST. It proposes also to save
00596      * the Analytic accountancy
00597      * @param $p_summary false for the feedback, true to show the summary
00598      * @return string
00599      *
00600      */
00601 
00602     function confirm($p_array, $p_summary = false) {
00603         global $g_parameter;
00604         extract($p_array);
00605 
00606         // don't need to verify for a summary
00607         if (!$p_summary)
00608             $this->verify($p_array);
00609         $anc = null;
00610         // to show a select list for the analytic & VAT USE
00611         // if analytic is op (optionnel) there is a blank line
00612 
00613         bcscale(4);
00614         $client = new Fiche($this->db);
00615         $client->get_by_qcode($e_client, true);
00616 
00617         $client_name = $client->getName() .
00618                 ' ' . $client->strAttribut(ATTR_DEF_ADRESS) . ' ' .
00619                 $client->strAttribut(ATTR_DEF_CP) . ' ' .
00620                 $client->strAttribut(ATTR_DEF_CITY);
00621         $lPeriode = new Periode($this->db);
00622         if ($this->check_periode() == true) {
00623             $lPeriode->p_id = $period;
00624         } else {
00625             $lPeriode->find_periode($e_date);
00626         }
00627         $date_limit = $lPeriode->get_date_limit();
00628         $r = "";
00629         
00630         $r.='<TABLE>';
00631         if ( $p_summary ) {
00632             $jr_id=$this->db->get_value('select jr_id from jrn where jr_internal=$1',array($this->internal));
00633             $r.="<tr>";
00634             $r.='<td>';
00635             $r.=_('Détail opération ');
00636             $r.='</td>';
00637             $r.='<td>';
00638             $r.=sprintf ('<a class="line" style="display:inline" href="javascript:modifyOperation(%d,%d)">%s</a>',
00639                     $jr_id,dossier::id(),$this->internal);
00640             $r.='</td>';
00641             $r.="</tr>";
00642         }
00643         $r.='<tr>';
00644         if ( ! $p_summary) {
00645             $r.='<td>' . _('Numéro Pièce') .'</td><td>'. hb($e_pj) . '</td>';
00646         } else {
00647             
00648              if ( strcmp($this->pj,$e_pj) != 0 )
00649             {
00650                 $r.='<td>' . _('Numéro Pièce') .'</td><td>'. hb($this->pj) . 
00651                         '<span class="notice"> '._('Attention numéro pièce existante, elle a du être adaptée').'</span></td>';
00652             } else {
00653                 $r.='<td>' . _('Numéro Pièce') .'</td><td>'. hb($this->pj) . '</td>';
00654             }
00655         }
00656         $r.='</tr>';
00657         $r.='<tr>';
00658         $r.='<td> ' . _('Date') . '</td><td> ' . hb($e_date) . '</td>';
00659         $r.='</tr>';
00660         $r.='<tr>';
00661         $r.='<td>' . _('Echeance') . '</td><td> ' . hb($e_ech) . '</td>';
00662         $r.='</tr>';
00663         $r.='<tr>';
00664         $r.='<td> ' . _('Période Comptable') . '</td><td> ' .hb( $date_limit['p_start'] . '-' . $date_limit['p_end']) . '</td>';
00665         $r.='</tr>';
00666         $r.='<tr>';
00667         $r.='<td> ' . _('Journal') . '</td><td> ' . hb($this->get_name()) . '</td>';
00668         $r.='</tr>';
00669         $r.='<tr>';
00670         $r.='<td> ' . _('Libellé') . '</td><td> ' . hb($e_comm) . '</td>';
00671         $r.='</tr>';
00672         
00673         $r.='<tr>';
00674         $r.='<td> ' . _('Client') . '</td><td> ' . hb($e_client . ':' . $client_name) . '</td>';
00675         $r.='</tr>';
00676         $r.='</table>';
00677         $r.='<h2>' . _('Détail articles vendus') . '</h2>';
00678         $r.='<p class="decale">';
00679         $r.='<table class="result" >';
00680         $r.='<TR>';
00681         $r.="<th>" . _('Code') . "</th>";
00682         $r.="<th>" . _('Dénomination') . "</th>";
00683         $r.="<th style=\"text-align:right\">" . _('prix') . "</th>";
00684         $r.="<th style=\"text-align:right\">" . _('quantité') . "</th>";
00685 
00686 
00687         if ($g_parameter->MY_TVA_USE == 'Y') {
00688             $r.="<th style=\"text-align:right\">" . _('tva') . "</th>";
00689             $r.='<th style="text-align:right"> ' . _('Montant TVA') . '</th>';
00690             $r.='<th style="text-align:right">' . _('Montant HTVA') . '</th>';
00691             $r.='<th style="text-align:right">' . _('Montant TVAC') . '</th>';
00692         } else {
00693             $r.='<th style="text-align:right">' . _('Montant') . '</th>';
00694         }
00695         /* if we use the AC */
00696         if ($g_parameter->MY_ANALYTIC != 'nu') {
00697             $anc = new Anc_Plan($this->db);
00698             $a_anc = $anc->get_list();
00699             $x = count($a_anc);
00700             /* set the width of the col */
00701             $r.='<th colspan="' . $x . '">' . _('Compt. Analytique') . '</th>';
00702 
00703             /* add hidden variables pa[] to hold the value of pa_id */
00704             $r.=Anc_Plan::hidden($a_anc);
00705         }
00706         $r.='</tr>';
00707         $tot_amount = 0.0;
00708         $tot_tva = 0.0;
00709         for ($i = 0; $i < $nb_item; $i++) {
00710             if (strlen(trim(${"e_march" . $i})) == 0)
00711                 continue;
00712 
00713             /* retrieve information for card */
00714             $fiche = new Fiche($this->db);
00715             $fiche->get_by_qcode(${"e_march" . $i});
00716             if ($g_parameter->MY_UPDLAB == 'Y')
00717                 $fiche_name = h(${"e_march" . $i . "_label"});
00718             else
00719                 $fiche_name = $fiche->strAttribut(ATTR_DEF_NAME);
00720             if ($g_parameter->MY_TVA_USE == 'Y') {
00721                 $oTva = new Acc_Tva($this->db);
00722                 $idx_tva = ${"e_march" . $i . "_tva_id"};
00723 
00724                 $oTva->set_parameter('id', $idx_tva);
00725                 $oTva->load();
00726             }
00727             $op = new Acc_Compute();
00728             $amount = bcmul(${"e_march" . $i . "_price"}, ${'e_quant' . $i});
00729             $op->set_parameter("amount", $amount);
00730             if ($g_parameter->MY_TVA_USE == 'Y') {
00731                 $op->set_parameter('amount_vat_rate', $oTva->get_parameter('rate'));
00732                 $op->compute_vat();
00733                 $tva_computed = $op->get_parameter('amount_vat');
00734                 $tva_item = ${"e_march" . $i . "_tva_amount"};
00735                 if (isset($tva[$idx_tva]))
00736                     $tva[$idx_tva]+=$tva_item;
00737                 else
00738                     $tva[$idx_tva] = $tva_item;
00739                 $tot_tva = round(bcadd($tva_item, $tot_tva), 2);
00740             }
00741             $tot_amount = round(bcadd($tot_amount, $amount), 2);
00742 
00743             $r.='<tr>';
00744             $r.='<td>';
00745             $r.=${"e_march" . $i};
00746             $r.='</td>';
00747             $r.='<TD style="border-bottom:1px dotted grey;">';
00748             $r.=$fiche_name;
00749             $r.='</td>';
00750             $r.='<td class="num">';
00751             $r.=nbm(${"e_march" . $i . "_price"});
00752             $r.='</td>';
00753             $r.='<td class="num">';
00754             $r.=nbm(${"e_quant" . $i});
00755             $r.='</td>';
00756             $both_side=0;
00757             if ($g_parameter->MY_TVA_USE == 'Y') {
00758                 $r.='<td class="num">';
00759                 $r.=$oTva->get_parameter('label');
00760                 $r.='</td>';
00761                 $both_side=$oTva->get_parameter("both_side");
00762                 /* warning if tva_computed and given are not the
00763                   same */
00764                 if (bcsub($tva_item, $tva_computed) != 0 && ! ($tva_item == 0 && $both_side == 1)) {
00765                     $r.='<td style="background-color:red" class="num">';
00766                     $r.=HtmlInput::infobulle(28);
00767                     $r.='<a href="#" class="error" style="display:inline" title="' . _("Attention Différence entre TVA calculée et donnée") . '">'
00768                             . nbm($tva_item) . '<a>';
00769                 } else {
00770                     $r.='<td  class="num">';
00771                     $r.=nbm($tva_item);
00772                 }
00773                 $r.='</td>';
00774                 $r.='<td class="num">';
00775                 $r.=nbm($amount);
00776                 $r.='</td>';
00777                 $tot_row = bcadd($tva_item, $amount);
00778                 $r.=td(nbm($tot_row), 'class="num"');
00779             } else {
00780                 $r.='<td class="num">';
00781                 $r.=nbm($amount);
00782                 $r.='</td>';
00783             }
00784             // encode the pa
00785             if ($g_parameter->MY_ANALYTIC != 'nu') { // use of AA
00786                 // show form
00787                 $anc_op = new Anc_Operation($this->db);
00788                 $null = ($g_parameter->MY_ANALYTIC == 'op') ? 1 : 0;
00789                 $r.='<td>';
00790                 $p_mode = ($p_summary == false) ? 1 : 0;
00791                 $p_array['pa_id'] = $a_anc;
00792                 /* op is the operation it contains either a sequence or a jrnx.j_id */
00793                 $r.=HtmlInput::hidden('op[]=', $i);
00794                 $r.=$anc_op->display_form_plan($p_array, $null, $p_mode, $i, $amount);
00795                 $r.='</td>';
00796             }
00797 
00798 
00799             $r.='</tr>';
00800         } // end loop item
00801         //
00802         // Add the sum
00803         $decalage=($g_parameter->MY_TVA_USE == 'Y')?'<td></td><td></td><td></td><td></td>':'<td></td>';
00804          $tot = round(bcadd($tot_amount, $tot_tva), 2);
00805         $tot_tva=nbm($tot_tva);
00806         $tot=nbm($tot);
00807         $str_tot=_('Totaux');
00808         $tot_amount=nbm($tot_amount);
00809         $r.=<<<EOF
00810 <tr class="highlight">
00811     {$decalage}            
00812      <td>
00813                 {$str_tot}
00814      </td>
00815     <td class="num">
00816         {$tot_tva}
00817     </td>
00818     <td class="num">
00819         {$tot_amount}
00820     </td>
00821     <td class="num">
00822         {$tot}
00823     </td>
00824 EOF;
00825 
00826         $r.='</table>';
00827         $r.='</p>';
00828         if ($g_parameter->MY_ANALYTIC != 'nu' && ! $p_summary) // use of AA
00829             $r.='<input type="button" class="button" value="' . _('Vérifiez Imputation Analytique') . '" onClick="verify_ca(\'\');">';
00830         $r.=(! $p_summary )?'<div id="total_div_id" >':'<div>';
00831         $r.='<h2>Totaux</h2>';
00832        
00833         /* use VAT */
00834         if ($g_parameter->MY_TVA_USE == 'Y') {
00835             $r.='<table>';
00836             $r.='<tr><td>Total HTVA</td>';
00837             $r.=td(hb($tot_amount ),'class="num"');
00838             foreach ($tva as $i => $value) {
00839                 $oTva->set_parameter('id', $i);
00840                 $oTva->load();
00841 
00842                 $r.='<tr><td>  TVA ' . $oTva->get_parameter('label').'</td>';
00843                 $r.=td(hb(nbm($tva[$i])),'class="num"');
00844             }
00845             $r.='<tr>'.td(_('Total TVA')).td(hb($tot_tva),'class="num"');
00846             $r.='<tr>'.td(_('Total TVAC')).td(hb($tot),'class="num"');
00847             $r.='</table>';
00848         } else {
00849             $r.='<br>Total '.hb(nbm($tot));
00850         }
00851         $r.='</div>';
00852         /*  Add hidden */
00853         $r.=HtmlInput::hidden('e_client', $e_client);
00854         $r.=HtmlInput::hidden('nb_item', $nb_item);
00855         $r.=HtmlInput::hidden('p_jrn', $p_jrn);
00856         $mt = microtime(true);
00857         $r.=HtmlInput::hidden('mt', $mt);
00858 
00859         if (isset($period))
00860             $r.=HtmlInput::hidden('period', $period);
00861         /* \todo comment les types hidden gérent ils des contenus avec des quotes, double quote ou < > ??? */
00862         $r.=HtmlInput::hidden('e_comm', $e_comm);
00863         $r.=HtmlInput::hidden('e_date', $e_date);
00864         $r.=HtmlInput::hidden('e_ech', $e_ech);
00865         $r.=HtmlInput::hidden('e_pj', $e_pj);
00866         $r.=HtmlInput::hidden('e_pj_suggest', $e_pj_suggest);
00867 
00868         $e_mp = (isset($e_mp)) ? $e_mp : 0;
00869         $r.=HtmlInput::hidden('e_mp', $e_mp);
00870         
00871         if ( isset($repo) )  {
00872             // Show the available repository
00873             $r.= $this->select_depot($p_summary,$repo);
00874         }
00875 
00876         /* if the paymethod is not 0 and if a quick code is given */
00877         if ($e_mp != 0 && strlen(trim(${'e_mp_qcode_' . $e_mp})) != 0) {
00878             $r.=HtmlInput::hidden('e_mp_qcode_' . $e_mp, ${'e_mp_qcode_' . $e_mp});
00879             $r.=HtmlInput::hidden('acompte', $acompte);
00880             $r.=HtmlInput::hidden('e_comm_paiement', $e_comm_paiement);
00881             /* needed for generating a invoice */
00882             $r.=HtmlInput::hidden('qcode_benef', ${'e_mp_qcode_' . $e_mp});
00883 
00884             $fname = new Fiche($this->db);
00885             $fname->get_by_qcode(${'e_mp_qcode_' . $e_mp});
00886             $r.='<h2>' . "Payé par " . ${'e_mp_qcode_' . $e_mp} .
00887                     " " . $fname->getName() . '</h2> ' . '<p class="decale">' . _('Déduction acompte ') . h($acompte) . '</p>' .
00888                     _('Libellé :') . h($e_comm_paiement) ;
00889             $r.='<br>';
00890         }
00891 
00892         $r.=HtmlInput::hidden('jrn_type', $jrn_type);
00893         for ($i = 0; $i < $nb_item; $i++) {
00894             $r.=HtmlInput::hidden("e_march" . $i, ${"e_march" . $i});
00895             if (isset(${"e_march" . $i . "_label"}))
00896                 $r.=HtmlInput::hidden("e_march" . $i . "_label", ${"e_march" . $i . "_label"});
00897             $r.=HtmlInput::hidden("e_march" . $i . "_price", ${"e_march" . $i . "_price"});
00898             if ($g_parameter->MY_TVA_USE == 'Y') {
00899                 $r.=HtmlInput::hidden("e_march" . $i . "_tva_id", ${"e_march" . $i . "_tva_id"});
00900                 $r.=HtmlInput::hidden("e_march" . $i . "_tva_amount", ${"e_march" . $i . "_tva_amount"});
00901             }
00902             $r.=HtmlInput::hidden("e_quant" . $i, ${"e_quant" . $i});
00903         }
00904         return $r;
00905     }
00906 
00907     /* !\brief the function extra info allows to
00908      * - add a attachment
00909      * - generate an invoice
00910      * - insert extra info
00911      * \return string
00912      */
00913 
00914     public function extra_info() {
00915         $r = '<div id="facturation_div_id" style="height:185px;height:10rem">';
00916         // check for upload piece
00917         $file = new IFile();
00918         $file->table = 0;
00919         $r.='<p class="decale">';
00920         $r.=_("Ajoutez une pièce justificative ");
00921         $r.=$file->input("pj", "");
00922 
00923         if ($this->db->count_sql("select md_id,md_name from document_modele where md_affect='VEN'") > 0) {
00924 
00925 
00926             $r.=_('ou générer une facture') . ' <input type="checkbox" name="gen_invoice" CHECKED>';
00927             // We propose to generate  the invoice and some template
00928             $doc_gen = new ISelect();
00929             $doc_gen->name = "gen_doc";
00930             $doc_gen->value = $this->db->make_array(
00931                     "select md_id,md_name " .
00932                     " from document_modele where md_affect='VEN'");
00933             $r.=$doc_gen->input() . '<br>';
00934         }
00935         $r.='<br>';
00936         $obj = new IText();
00937         $r.=_('Numero de bon de commande : ') . $obj->input('bon_comm') . '<br>';
00938         $r.=_('Autre information : ') . $obj->input('other_info') . '<br>';
00939         $r.='</p>';
00940         $r.='</div>';
00941         return $r;
00942     }
00943 
00944     /**
00945      * @brief update the payment
00946      * @deprecated
00947      * 
00948      */
00949 
00950     function show_unpaid() {
00951         // Show list of unpaid sell
00952         // Date - date of payment - Customer - amount
00953         // Nav. bar
00954         $step = $_SESSION['g_pagesize'];
00955         $page = (isset($_GET['offset'])) ? $_GET['page'] : 1;
00956         $offset = (isset($_GET['offset'])) ? $_GET['offset'] : 0;
00957 
00958 
00959         $sql = SQL_LIST_UNPAID_INVOICE_DATE_LIMIT . " and jr_def_id=" . $this->id;
00960         list($max_line, $list) = ListJrn($this->db, $sql, null, $offset, 1);
00961         $sql = SQL_LIST_UNPAID_INVOICE . " and jr_def_id=" . $this->id;
00962         list($max_line2, $list2) = ListJrn($this->db, $sql, null, $offset, 1);
00963 
00964         // Get the max line
00965         $m = ($max_line2 > $max_line) ? $max_line2 : $max_line;
00966         $bar2 = navigation_bar($offset, $m, $step, $page);
00967 
00968         echo $bar2;
00969         echo '<h2 class="info"> ' . _('Echeance dépassée') . ' </h2>';
00970         echo $list;
00971         echo '<h2 class="info"> ' . _('Non Payée') . ' </h2>';
00972         echo $list2;
00973         echo $bar2;
00974         // Add hidden parameter
00975         $hid = new IHidden();
00976 
00977         echo '<hr>';
00978 
00979         if ($m != 0)
00980             echo HtmlInput::submit('paid', _('Mise à jour paiement'));
00981     }
00982 
00983     /* !\brief display the form for entering data for invoice,
00984      * \param $p_array is null or you can put the predef operation or the $_POST
00985      *
00986      * \return HTML string
00987      */
00988 
00989     function input($p_array = null, $p_readonly = 0) {
00990         global $g_parameter, $g_user;
00991         if ($p_array != null)
00992             extract($p_array);
00993 
00994         $flag_tva = $g_parameter->MY_TVA_USE;
00995         /* Add button */
00996         $f_add_button = new IButton('add_card');
00997         $f_add_button->tabindex = -1;
00998         $f_add_button->label = _('Créer une nouvelle fiche');
00999         $f_add_button->set_attribute('ipopup', 'ipop_newcard');
01000         $f_add_button->set_attribute('jrn', $this->id);
01001         $f_add_button->javascript = "this.jrn=\$('p_jrn').value; select_card_type(this);";
01002 
01003         $f_add_button2 = new IButton('add_card2');
01004         $f_add_button2->tabindex = -1;
01005         $f_add_button2->label = _('Créer une nouvelle fiche');
01006         $f_add_button2->set_attribute('ipopup', 'ipop_newcard');
01007         $f_add_button2->set_attribute('filter', $this->get_all_fiche_def());
01008         //    $f_add_button2->set_attribute('jrn',$this->id);
01009         $f_add_button2->javascript = " this.jrn=\$('p_jrn').value;select_card_type(this);";
01010 
01011         $str_add_button = "";
01012         $str_add_button2 = "";
01013         if ($g_user->check_action(FICADD) == 1) {
01014             $str_add_button = $f_add_button->input();
01015             $str_add_button2 = $f_add_button2->input();
01016         }
01017         // The first day of the periode
01018         $oPeriode = new Periode($this->db);
01019         list ($l_date_start, $l_date_end) = $oPeriode->get_date_limit($g_user->get_periode());
01020         if ($g_parameter->MY_DATE_SUGGEST == 'Y')
01021             $op_date = (!isset($e_date) ) ? $l_date_start : $e_date;
01022         else
01023             $op_date = (!isset($e_date) ) ? '' : $e_date;
01024 
01025 
01026         $e_ech = (isset($e_ech)) ? $e_ech : "";
01027         $e_comm = (isset($e_comm)) ? $e_comm : "";
01028 
01029         $r = '';
01030         $r.=dossier::hidden();
01031         $f_legend = _('Client');
01032 
01033         $Echeance = new IDate();
01034         $Echeance->setReadOnly(false);
01035 
01036         $Echeance->tabindex = 2;
01037         $label = HtmlInput::infobulle(4);
01038         $f_echeance = $Echeance->input('e_ech', $e_ech, _('Echéance') . $label);
01039         $Date = new IDate();
01040         $Date->setReadOnly(false);
01041 
01042         $f_date = $Date->input("e_date", $op_date);
01043 
01044         $f_periode = '';
01045         // Periode
01046         //--
01047         if ($this->check_periode() == true) {
01048             $l_user_per = $g_user->get_periode();
01049             $def = (isset($periode)) ? $periode : $l_user_per;
01050 
01051             $period = new IPeriod("period");
01052             $period->user = $g_user;
01053             $period->cn = $this->db;
01054             $period->value = $def;
01055             $period->type = OPEN;
01056             try {
01057                 $l_form_per = $period->input();
01058             } catch (Exception $e) {
01059                 if ($e->getCode() == 1) {
01060                     throw  Exception( _("Aucune période ouverte") );
01061                 }
01062             }
01063             $label = HtmlInput::infobulle(3);
01064             $f_periode = '<td>' . _("Période comptable") . "</td> <td> $label " . $l_form_per . '</td>';
01065         }
01066         /* if we suggest the next pj, then we need a javascript */
01067         $add_js = "";
01068         if ($g_parameter->MY_PJ_SUGGEST == 'Y') {
01069             $add_js = "update_pj();";
01070         }
01071         if ($g_parameter->MY_DATE_SUGGEST == 'Y') {
01072             $add_js.='get_last_date();';
01073         }
01074         $add_js.='update_name();';
01075         $add_js.='update_pay_method();';
01076         $add_js.='update_row("sold_item");';
01077 
01078         $wLedger = $this->select_ledger('VEN', 2);
01079         if ($wLedger == null)
01080             throw  Exception(_('Pas de journal disponible'));
01081         $wLedger->table = 1;
01082         $wLedger->javascript = "onChange='update_predef(\"ven\",\"f\",\"".$_REQUEST['ac']."\");$add_js'";
01083         $wLedger->label = " Journal " . HtmlInput::infobulle(2);
01084 
01085         $f_jrn = $wLedger->input();
01086 
01087         $Commentaire = new IText();
01088         $Commentaire->table = 0;
01089         $Commentaire->setReadOnly(false);
01090         $Commentaire->size = 60;
01091         $Commentaire->tabindex = 3;
01092 
01093         $label = HtmlInput::infobulle(1);
01094 
01095         $f_desc = $Commentaire->input("e_comm", h($e_comm)) . $label;
01096         // PJ
01097         //--
01098         /* suggest PJ ? */
01099         $default_pj = '';
01100         if ($g_parameter->MY_PJ_SUGGEST == 'Y') {
01101             $default_pj = $this->guess_pj();
01102         }
01103 
01104         $pj = new IText();
01105 
01106         $pj->table = 0;
01107         $pj->name = "e_pj";
01108         $pj->size = 10;
01109         $pj->value = (isset($e_pj)) ? $e_pj : $default_pj;
01110         $f_pj = $pj->input() . HtmlInput::hidden('e_pj_suggest', $default_pj);
01111         // Display the customer
01112         //--
01113         $fiche = 'deb';
01114 
01115         // Save old value and set a new one
01116         //--
01117         $e_client = ( isset($e_client) ) ? $e_client : "";
01118         $e_client_label = "&nbsp;"; //str_pad("",100,".");
01119         // retrieve e_client_label
01120         //--
01121 
01122         if (strlen(trim($e_client)) != 0) {
01123             $fClient = new Fiche($this->db);
01124             $fClient->get_by_qcode($e_client);
01125             $e_client_label = $fClient->strAttribut(ATTR_DEF_NAME) . ' ' .
01126                     ' Adresse : ' . $fClient->strAttribut(ATTR_DEF_ADRESS) . ' ' .
01127                     $fClient->strAttribut(ATTR_DEF_CP) . ' ' .
01128                     $fClient->strAttribut(ATTR_DEF_CITY) . ' ';
01129         }
01130 
01131         $W1 = new ICard();
01132         $W1->label = "Client " . HtmlInput::infobulle(0);
01133         $W1->name = "e_client";
01134         $W1->tabindex = 3;
01135         $W1->value = $e_client;
01136         $W1->table = 0;
01137         $W1->set_dblclick("fill_ipopcard(this);");
01138         $W1->set_attribute('ipopup', 'ipopcard');
01139 
01140         // name of the field to update with the name of the card
01141         $W1->set_attribute('label', 'e_client_label');
01142         // name of the field to update with the name of the card
01143         $W1->set_attribute('typecard', 'deb');
01144 
01145         // Add the callback function to filter the card on the jrn
01146         $W1->set_callback('filter_card');
01147         $W1->set_function('fill_data');
01148         $W1->javascript = sprintf(' onchange="fill_data_onchange(\'%s\');" ', $W1->name);
01149         $f_client_qcode = $W1->input();
01150         $client_label = new ISpan();
01151         $client_label->table = 0;
01152         $f_client = $client_label->input("e_client_label", $e_client_label);
01153         $f_client_bt = $W1->search();
01154 
01155 
01156         // Record the current number of article
01157         $Hid = new IHidden();
01158         $p_article = ( isset($nb_item)) ? $nb_item : $this->get_min_row();
01159         $r.=$Hid->input("nb_item", $p_article);
01160         $max = ($p_article < $this->get_min_row()) ? $this->get_min_row() : $p_article;
01161 
01162 
01163         $f_legend_detail = _("Détail articles vendus");
01164 
01165         // For each article
01166         //--
01167         for ($i = 0; $i < $max; $i++) {
01168             // Code id, price & vat code
01169             //--
01170             $march = (isset(${"e_march$i"})) ? ${"e_march$i"} : "";
01171             $march_price = (isset(${"e_march" . $i . "_price"})) ? ${"e_march" . $i . "_price"} : ""  ;
01172             if ($flag_tva == 'Y') {
01173                 $march_tva_id = (isset(${"e_march$i" . "_tva_id"})) ? ${"e_march$i" . "_tva_id"} : "";
01174                 $march_tva_amount = (isset(${"e_march$i" . "_tva_amount"})) ? ${"e_march$i" . "_tva_amount"} : "";
01175             }
01176             $march_label = (isset(${"e_march" . $i . "_label"})) ? ${"e_march" . $i . "_label"} : "";
01177 
01178             // retrieve the tva label and name
01179             //--
01180             if (strlen(trim($march)) != 0 && strlen(trim($march_label)) == 0) {
01181                 $fMarch = new Fiche($this->db);
01182                 $fMarch->get_by_qcode($march);
01183                 $march_label = $fMarch->strAttribut(ATTR_DEF_NAME);
01184                 if ($flag_tva == 'Y') {
01185                     if (!(isset(${"e_march$i" . "_tva_id"})))
01186                         $march_tva_id = $fMarch->strAttribut(ATTR_DEF_TVA);
01187                 }
01188             }
01189             // Show input
01190             //--
01191             $W1 = new ICard();
01192             $W1->label = "";
01193             $W1->name = "e_march" . $i;
01194             $W1->value = $march;
01195             $W1->table = 1;
01196             $W1->set_attribute('typecard', 'cred');
01197             $W1->set_dblclick("fill_ipopcard(this);");
01198             $W1->set_attribute('ipopup', 'ipopcard');
01199 
01200             // name of the field to update with the name of the card
01201             $W1->set_attribute('label', 'e_march' . $i . '_label');
01202             // name of the field with the price
01203             $W1->set_attribute('price', 'e_march' . $i . '_price');
01204             // name of the field with the TVA_ID
01205             $W1->set_attribute('tvaid', 'e_march' . $i . '_tva_id');
01206             // Add the callback function to filter the card on the jrn
01207             $W1->set_callback('filter_card');
01208             $W1->set_function('fill_data');
01209             $W1->javascript = sprintf(' onchange="fill_data_onchange(\'%s\');" ', $W1->name);
01210 
01211             $W1->readonly = false;
01212 
01213             $array[$i]['quick_code'] = $W1->input();
01214             $array[$i]['bt'] = $W1->search();
01215             // For computing we need some hidden field for holding the value
01216             $array[$i]['hidden'] = '';
01217             if ($flag_tva == 'Y')
01218                 $array[$i]['hidden'].=HtmlInput::hidden('tva_march' . $i, 0);
01219 
01220             $htva = new INum('htva_march' . $i);
01221             $htva->readOnly = 1;
01222             $htva->value = 0;
01223             $array[$i]['htva'] = $htva->input();
01224 
01225             if ($g_parameter->MY_TVA_USE == 'Y')
01226                 $tvac = new INum('tvac_march' . $i);
01227             else
01228                 $tvac = new IHidden('tvac_march' . $i);
01229 
01230             $tvac->readOnly = 1;
01231             $tvac->value = 0;
01232             $array[$i]['tvac'] = $tvac->input();
01233 
01234             if ($g_parameter->MY_UPDLAB == 'Y') {
01235                 $Span = new IText("e_march" . $i . "_label");
01236 
01237                 $Span->css_size = "100%";
01238             } else {
01239                 $Span = new ISpan("e_march" . $i . "_label");
01240             }
01241             $Span->value = $march_label;
01242             $Span->setReadOnly(false);
01243             // card's name, price
01244             //--
01245             $array[$i]['denom'] = $Span->input("e_march" . $i . "_label", $march_label);
01246             // price
01247             $Price = new INum();
01248             $Price->setReadOnly(false);
01249             $Price->size = 9;
01250             $Price->javascript = "onBlur='format_number(this);clean_tva($i);compute_ledger($i)'";
01251             $array[$i]['pu'] = $Price->input("e_march" . $i . "_price", $march_price);
01252             $array[$i]['tva'] = '';
01253             $array[$i]['amount_tva'] = '';
01254             // if tva is not needed then no tva field
01255             if ($flag_tva == 'Y') {
01256                 // vat label
01257                 //--
01258                 $Tva = new ITva_Popup($this->db);
01259                 $Tva->in_table = true;
01260                 $Tva->set_attribute('compute', $i);
01261 
01262                 $Tva->js = 'onblur="format_number(this);clean_tva(' . $i . ');compute_ledger(' . $i . ')"';
01263                 $Tva->value = $march_tva_id;
01264                 $array[$i]['tva'] = $Tva->input("e_march$i" . "_tva_id");
01265                 // vat amount
01266                 //--
01267                 $wTva_amount = new INum();
01268                 $wTva_amount->readOnly = false;
01269                 $wTva_amount->size = 6;
01270                 $wTva_amount->javascript = "onBlur='format_number(this);compute_ledger($i)'";
01271                 $array[$i]['amount_tva'] = $wTva_amount->input("e_march" . $i . "_tva_amount", $march_tva_amount);
01272             }
01273             // quantity
01274             //--
01275             $quant = (isset(${"e_quant$i"})) ? ${"e_quant$i"} : "1"
01276             ;
01277             $Quantity = new INum();
01278             $Quantity->setReadOnly(false);
01279             $Quantity->size = 8;
01280             $Quantity->javascript = "onChange='format_number(this);clean_tva($i);compute_ledger($i)'";
01281             $array[$i]['quantity'] = $Quantity->input("e_quant" . $i, $quant);
01282         }// foreach article
01283         $f_type = _('Client');
01284 
01285 
01286         ob_start();
01287         require_once('template/form_ledger_detail.php');
01288         $r.=ob_get_contents();
01289         ob_end_clean();
01290 
01291 
01292 
01293         // Set correctly the REQUEST param for jrn_type
01294         $r.=HtmlInput::hidden('jrn_type', 'VEN');
01295 
01296         $r.=HtmlInput::button('add_item', _('Ajout article'), ' onClick="ledger_add_row()"');
01297         $r.= create_script("$('" . $Date->id . "').focus()");
01298         return $r;
01299     }
01300     /**
01301      * Retrieve data from the view v_detail_sale
01302      * @global  $g_user connected user
01303      * @param $p_from jrn.jr_tech_per from 
01304      * @param type $p_end jrn.jr_tech_per to
01305      * @return type
01306      */
01307     function get_detail_sale($p_from,$p_end)
01308     {
01309         global $g_user;
01310         // Journal valide
01311         if ( $this->id == 0 ) die (__FILE__.":".__LINE__." Journal invalide");
01312         
01313         // Securite
01314         if ( $g_user->get_ledger_access($this->id) == 'X' ) return null;
01315         
01316         // get the data from the view
01317         $sql = "select * 
01318                 from v_detail_sale
01319                  where 
01320                 jr_def_id = $1 
01321                 and  jr_date >= (select p_start from parm_periode where p_id = $2) 
01322                 and  jr_date <= (select p_end from parm_periode where p_id  = $3) "
01323                 .' order by jr_date,substring(jr_pj_number,\'[0-9]+$\')::numeric asc ';
01324         $ret = $this->db->exec_sql($sql, array($this->id,$p_from, $p_end));
01325         return $ret;
01326     }
01327     /**
01328      * @brief compute an array with the heading cells for the
01329      * details, used for the export in CSV
01330      * @return array
01331      */
01332     static function heading_detail_sale()
01333     {
01334         $array['jr_id'] = _('Numéro opération');
01335         $array['jr_date'] = _('Date');
01336         $array['jr_date_paid'] = _('Date paiement');
01337         $array['jr_ech'] = _('Date échéance');
01338         $array['jr_tech_per'] = _('Période');
01339         $array['jr_comment'] = _('Libellé');
01340         $array['jr_pj_number'] = _('Pièce');
01341         $array['jr_internal'] = _('Interne');
01342         $array['jr_def_id'] = _('Code journal');
01343         $array['j_poste'] = _('Poste');
01344         $array['j_text'] = _('Commentaire');
01345         $array['j_qcode'] = _('Code Item');
01346         $array['item_card'] = _('N° fiche');
01347         $array['item_name'] = _('Nom fiche');
01348         $array['qs_client'] = _('N° fiche fournisseur');
01349         $array['tiers_name'] = _('Nom fournisseur');
01350         $array['quick_code'] = _('Code fournisseur');
01351         $array['tva_label'] = _('Nom TVA');
01352         $array['tva_comment'] = _('Commentaire TVA');
01353         $array['tva_both_side'] = _('TVA annulée');
01354         $array['vat_sided'] = _('TVA Non Payé');
01355         $array['vat_code'] = _('Code TVA');
01356         $array['vat'] = _('Montant TVA');
01357         $array['price'] = _('Total HTVA');
01358         $array['quantity'] = _('quantité');
01359         $array['price_per_unit'] = _('PU');
01360         $array['htva'] = _('HTVA Opération');
01361         $array['tot_vat'] = _('TVA Opération');
01362         return $array;
01363     }
01364     /* !\brief test function
01365      */
01366 
01367     static function test_me($p_string = '') {
01368         $cn = new Database(dossier::id());
01369         $a = new Acc_Ledger_Sold($cn, 2);
01370         echo $a->input();
01371     }
01372     
01373         }
01374 
 All Data Structures Namespaces Files Functions Variables Enumerations