00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 require_once("class_iselect.php");
00026 require_once("class_icard.php");
00027 require_once("class_ispan.php");
00028 require_once("class_ihidden.php");
00029 require_once("class_iperiod.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_parm_code.php');
00038 require_once('class_acc_payment.php');
00039 require_once('ac_common.php');
00040 require_once('class_itva_popup.php');
00041 require_once('class_acc_ledger_info.php');
00042 require_once('class_acc_ledger_fin.php');
00043 require_once 'class_stock_goods.php';
00044
00045
00046
00047
00048
00049 class Acc_Ledger_Purchase extends Acc_Ledger
00050 {
00051 function __construct ($p_cn,$p_init)
00052 {
00053 $this->type='ACH';
00054 parent::__construct($p_cn,$p_init);
00055 }
00056
00057
00058
00059
00060
00061 public function verify($p_array)
00062 {
00063 global $g_parameter,$g_user;
00064
00065 if (is_array($p_array ) == false || empty($p_array))
00066 throw new Exception ("Array empty");
00067
00068
00069
00070 check_parameter($p_array,'p_jrn,e_date,e_client');
00071
00072 extract ($p_array);
00073
00074 if ( $g_user->check_jrn($p_jrn) != 'W' )
00075 throw new Exception (_('Accès interdit'),20);
00076
00077
00078
00079 if ( isset($mt) && $this->db->count_sql('select jr_mt from jrn where jr_mt=$1',array($mt)) != 0 )
00080 throw new Exception (_('Double Encodage'),5);
00081
00082
00083 if ( strlen(trim($e_client)) == 0 )
00084 throw new Exception(_('Vous n\'avez pas donné de fournisseur'),11);
00085
00086
00087 if ( isDate($e_date) == null )
00088 {
00089 throw new Exception(_('Date invalide'), 2);
00090 }
00091 $oPeriode=new Periode($this->db);
00092 if ( $this->check_periode() == false)
00093 {
00094 $tperiode=$oPeriode->find_periode($e_date);
00095 }
00096 else
00097 {
00098 $tperiode=$period;
00099 $oPeriode->p_id=$tperiode;
00100
00101 list ($min,$max)=$oPeriode->get_date_limit($tperiode);
00102 if ( cmpDate($e_date,$min) < 0 ||
00103 cmpDate($e_date,$max) > 0)
00104 throw new Exception(_('Date et periode ne correspondent pas'),6);
00105 }
00106
00107 if ( $this->is_closed($tperiode)==1 )
00108 {
00109 throw new Exception(_('Periode fermee'),6);
00110 }
00111
00112
00113 if( $this->check_strict() == true)
00114 {
00115
00116
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 à la date du ')
00120 .$last_date._(' vous ne pouvez pas encoder à une '.
00121 ' date antérieure dans ce journal'),13);
00122
00123 }
00124
00125
00126 $fiche=new Fiche($this->db);
00127 $fiche->get_by_qcode($e_client);
00128
00129 if ( $fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true)
00130 throw new Exception(_('La fiche ').$e_client._('n\'a pas de poste comptable'),8);
00131
00132
00133 $sposte=$fiche->strAttribut(ATTR_DEF_ACCOUNT);
00134
00135 if ( strpos($sposte,',') != 0 )
00136 {
00137 $array=explode(',',$sposte);
00138 $poste_val=$array[1];
00139 }
00140 else
00141 {
00142 $poste_val=$sposte;
00143 }
00144
00145
00146 $poste=new Acc_Account_Ledger($this->db,$poste_val);
00147 if ( $poste->load() == false )
00148 {
00149 throw new Exception(_('Pour la fiche ').$e_client._(' le poste comptable [').$poste->id.'] '._('n\'existe pas'),9);
00150 }
00151
00152 $fiche=new Fiche ($this->db);
00153 $fiche->get_by_qcode($e_client,'cred');
00154 if ( $fiche->belong_ledger($p_jrn) !=1 )
00155 throw new Exception(_('La fiche ').$e_client._('n\'est pas accessible à ce journal'),10);
00156
00157 $nb=0;
00158
00159
00160
00161 if ($e_mp != 0 ) $this->check_payment($e_mp,${"e_mp_qcode_".$e_mp});
00162
00163
00164
00165
00166
00167 for ($i=0;$i< $nb_item;$i++)
00168 {
00169 if ( strlen(trim(${'e_march'.$i}))== 0) continue;
00170
00171 if ( isNumber(${'e_march'.$i.'_price'}) == 0 )
00172 throw new Exception(_('La fiche ').${'e_march'.$i}._('a un montant invalide').' ['.${'e_march'.$i}.']',6);
00173 if ( isNumber(${'e_quant'.$i}) == 0 )
00174 throw new Exception(_('La fiche ').${'e_march'.$i}._('a une quantité invalide').' ['.${'e_quant'.$i}.']',7);
00175
00176
00177 if ( $g_parameter->MY_TVA_USE=='Y')
00178 {
00179 if (${'e_march'.$i.'_tva_id'} == 0 )
00180 throw new Exception(_('La fiche ').${'e_march'.$i}._('a un code tva invalide').' ['.${'e_march'.$i.'_tva_id'}.']',13);
00181 $tva_rate=new Acc_Tva($this->db);
00182 $tva_rate->set_parameter('id',${'e_march'.$i.'_tva_id'});
00183
00184 if ( $tva_rate->load() != 0 )
00185 throw new Exception(_('La fiche ').${'e_march'.$i}._('a un code tva invalide').' ['.${'e_march'.$i.'_tva_id'}.']',13);
00186
00187
00188
00189 $a_poste=explode(',',$tva_rate->tva_poste);
00190
00191 if (
00192 $this->db->get_value('select count(*) from tmp_pcmn where pcm_val=$1',array($a_poste[0])) == 0 ||
00193 $this->db->get_value('select count(*) from tmp_pcmn where pcm_val=$1',array($a_poste[1])) == 0 )
00194 throw new Exception(_(" La TVA ".$tva_rate->tva_label." utilise des postes comptables inexistants"));
00195
00196 }
00197
00198 $fiche=new Fiche($this->db);
00199 $fiche->get_by_qcode(${'e_march'.$i});
00200 if ( $fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true)
00201 throw new Exception(_('La fiche ').${'e_march'.$i}._('n\'a pas de poste comptable'),8);
00202
00203
00204 $sposte=$fiche->strAttribut(ATTR_DEF_ACCOUNT);
00205
00206 if ( strpos($sposte,',') != 0 )
00207 {
00208 $array=explode(',',$sposte);
00209 $poste_val=$array[0];
00210 }
00211 else
00212 {
00213 $poste_val=$sposte;
00214 }
00215
00216
00217 $poste=new Acc_Account_Ledger($this->db,$poste_val);
00218 if ( $poste->load() == false )
00219 {
00220 throw new Exception(_('Pour la fiche ').${'e_march'.$i}._(' le poste comptable').' ['.$poste->id._('n\'existe pas'),9);
00221 }
00222
00223 $fiche=new Fiche ($this->db);
00224 $fiche->get_by_qcode(${'e_march'.$i});
00225 if ( $fiche->belong_ledger($p_jrn,'deb') !=1 )
00226 throw new Exception(_('La fiche ').${'e_march'.$i}._('n\'est pas accessible à ce journal'),10);
00227
00228
00229
00230
00231
00232
00233
00234 foreach (array(
00235 array(ATTR_DEF_DEPENSE_NON_DEDUCTIBLE,'DNA',ATTR_DEF_ACCOUNT_ND),
00236 array(ATTR_DEF_DEP_PRIV,'DEP_PRIV',ATTR_DEF_ACCOUNT_ND_PERSO),
00237 array(ATTR_DEF_TVA_NON_DEDUCTIBLE_RECUP,'TVA_DED_IMPOT',ATTR_DEF_ACCOUNT_ND_TVA),
00238 array(ATTR_DEF_TVA_NON_DEDUCTIBLE,'TVA_DNA',ATTR_DEF_ACCOUNT_ND_TVA_ND)) as $key)
00239 {
00240 if ( ! $fiche->empty_attribute($key[0]) && $fiche->empty_attribute($key[2]))
00241 {
00242 $a=new Acc_Parm_Code($this->db,$key[1]);
00243 if ( $this->db->count_sql('select pcm_val from tmp_pcmn where pcm_val=$1',array($a->p_value))==0)
00244 throw new Exception ($key[1]._("ce code n'a pas de poste comptable, créez ce poste : [".$a->p_value."]"));
00245 }
00246 if ( ! $fiche->empty_attribute($key[0]) && ! $fiche->empty_attribute($key[2]))
00247 {
00248 $nd_str=$fiche->strAttribut($key[2]);
00249 if ( $nd_str != '')
00250 {
00251 $poste_nd=new Acc_Account_Ledger($this->db,$nd_str);
00252 if ( $poste_nd->load() == false)
00253 {
00254 $nd_msg=sprintf(_("Pour la fiche %s, le compte contrepartie %s n'existe pas"),
00255 $fiche->getName(),$poste_nd->id);
00256 $nd_msg=h($nd_msg);
00257 throw new Exception ($nd_msg);
00258 }
00259 }
00260 }
00261 }
00262 $nb++;
00263 }
00264
00265 if ( $nb == 0 )
00266 throw new Exception(_('Il n\'y a aucune marchandise'),12);
00267
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277 private function compute_no_deductible(Acc_Compute $p_nd_amount, Fiche $p_fiche)
00278 {
00279 if (!$p_fiche->empty_attribute(ATTR_DEF_DEPENSE_NON_DEDUCTIBLE))
00280 {
00281 $p_nd_amount->amount_nd_rate = $p_fiche->strAttribut(ATTR_DEF_DEPENSE_NON_DEDUCTIBLE);
00282 $p_nd_amount->compute_nd();
00283 }
00284 if (!$p_fiche->empty_attribute(ATTR_DEF_TVA_NON_DEDUCTIBLE) )
00285 {
00286 $p_nd_amount->nd_vat_rate = $p_fiche->strAttribut(ATTR_DEF_TVA_NON_DEDUCTIBLE);
00287 $p_nd_amount->compute_nd_vat();
00288 }
00289 if (!$p_fiche->empty_attribute(ATTR_DEF_TVA_NON_DEDUCTIBLE_RECUP) )
00290 {
00291 $p_nd_amount->nd_ded_vat_rate = $p_fiche->strAttribut(ATTR_DEF_TVA_NON_DEDUCTIBLE_RECUP);
00292 $p_nd_amount->compute_ndded_vat();
00293 }
00294
00295 if (!$p_fiche->empty_attribute(ATTR_DEF_DEP_PRIV))
00296 {
00297 $p_nd_amount->amount_perso_rate = $p_fiche->strAttribut(ATTR_DEF_DEP_PRIV);
00298 $p_nd_amount->compute_perso();
00299 }
00300
00301 }
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 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)
00316 {
00317 global $g_parameter;
00318 if ($p_acc_operation->jrnx_id == 0) {
00319 throw new Exception(__FILE__.__LINE__.'invalid acc_operation.j_id');
00320 }
00321 $source_j_id=$p_acc_operation->jrnx_id ;
00322
00323
00324
00325
00326 if ($p_nd_amount->amount_nd_rate != 0)
00327 {
00328 $dna_default = new Acc_Parm_Code($this->db, 'DNA');
00329
00330
00331 if (!$p_fiche->empty_attribute(ATTR_DEF_ACCOUNT_ND))
00332 {
00333 $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND);
00334 } else
00335 {
00336 $dna = $dna_default->p_value;
00337 }
00338 $dna = ($dna == '') ? $dna_default->p_value : $dna;
00339
00340 $p_acc_operation->type = 'd';
00341 $p_acc_operation->amount = $p_nd_amount->amount_nd;
00342 $p_acc_operation->poste = $dna;
00343 $p_acc_operation->qcode = '';
00344 $p_acc_operation->desc=$this->find_label($dna)." ND ".$p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
00345 if ($p_nd_amount->amount_nd > 0)
00346 $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->amount_nd );
00347 $j_id = $p_acc_operation->insert_jrnx();
00348 }
00349
00350
00351
00352 if ($p_nd_amount->amount_perso != 0)
00353 {
00354 $dna_default = new Acc_Parm_Code($this->db, 'DEP_PRIV');
00355
00356
00357 $p_acc_operation->type = 'd';
00358 if (!$p_fiche->empty_attribute(ATTR_DEF_ACCOUNT_ND_PERSO))
00359 {
00360 $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND_PERSO);
00361 } else
00362 {
00363 $dna = $dna_default->p_value;
00364 }
00365 $dna = ($dna == '') ? $dna_default->p_value : $dna;
00366
00367 $p_acc_operation->amount = $p_nd_amount->amount_perso ;
00368 $p_acc_operation->poste = $dna;
00369 $p_acc_operation->qcode = '';
00370 $p_acc_operation->desc=$this->find_label($dna)." ND_PRIV ".$p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
00371 if ($p_nd_amount->amount_perso> 0)
00372 $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->amount_perso);
00373 $j_id = $p_acc_operation->insert_jrnx();
00374 }
00375 if ($p_nd_amount->nd_vat != 0)
00376 {
00377 $dna_default = new Acc_Parm_Code($this->db, 'TVA_DNA');
00378
00379
00380 $p_acc_operation->type = 'd';
00381 $p_acc_operation->qcode = '';
00382 if (!$p_fiche->empty_attribute(ATTR_DEF_ACCOUNT_ND_TVA_ND) && $p_tva_both == 0)
00383 {
00384 $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND_TVA_ND);
00385 } else
00386 {
00387 $dna = $dna_default->p_value;
00388 }
00389 $dna = ($dna == '') ? $dna_default->p_value : $dna;
00390
00391 $p_acc_operation->amount = $p_nd_amount->nd_vat;
00392 $p_acc_operation->poste = $dna;
00393 $p_acc_operation->desc=$this->find_label($dna)." ND_TVA ".$p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
00394 $j_id = $p_acc_operation->insert_jrnx();
00395 if ( $g_parameter->MY_ANALYTIC != "nu" )
00396 {
00397 $op=new Anc_Operation($this->db);
00398 $op->oa_group=$p_group;
00399 $op->j_id=$j_id;
00400 $op->oa_date=$p_acc_operation->date;
00401
00402 $op->oa_debit='t';
00403 $op->oa_description=sql_string('ND_TVA');
00404 $op->oa_jrnx_id_source=$source_j_id;
00405 $op->save_form_plan_vat_nd($_POST,$idx,$j_id,$p_nd_amount->nd_vat,$p_acc_operation->jrnx_id);
00406 }
00407 if ($p_nd_amount->nd_vat> 0)
00408 $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->nd_vat);
00409
00410 }
00411 if ($p_nd_amount->nd_ded_vat != 0)
00412 {
00413 $dna_default = new Acc_Parm_Code($this->db, 'TVA_DED_IMPOT');
00414
00415 if (!$p_fiche->empty_attribute(ATTR_DEF_ACCOUNT_ND_TVA) && $p_tva_both == 0)
00416 {
00417 $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND_TVA);
00418 } else
00419 {
00420 $dna = $dna_default->p_value;
00421 }
00422 $dna = ($dna == '') ? $dna_default->value : $dna;
00423
00424
00425
00426 $p_acc_operation->type = 'd';
00427 $p_acc_operation->qcode = '';
00428 $p_acc_operation->amount = $p_nd_amount->nd_ded_vat;
00429 $p_acc_operation->poste = $dna;
00430 $p_acc_operation->desc=$this->find_label($dna)." DED_TVA ".$p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
00431 if ($p_nd_amount->nd_ded_vat > 0)
00432 $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->nd_ded_vat);
00433 $j_id = $p_acc_operation->insert_jrnx();
00434 if ( $g_parameter->MY_ANALYTIC != "nu" )
00435 {
00436 $op=new Anc_Operation($this->db);
00437 $op->oa_group=$p_group;
00438 $op->j_id=$j_id;
00439 $op->oa_date=$p_acc_operation->date;
00440
00441 $op->oa_debit='t';
00442 $op->oa_description=sql_string('DED_TVA ');
00443 $op->oa_jrnx_id_source=$source_j_id;
00444 $op->save_form_plan_vat_nd($_POST,$idx,$j_id,$p_nd_amount->nd_ded_vat);
00445 }
00446 }
00447 }
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484 public function insert($p_array=null)
00485 {
00486 global $g_parameter;
00487 extract ($p_array);
00488 $this->verify($p_array) ;
00489
00490 $group=$this->db->get_next_seq("s_oa_group");
00491 $seq=$this->db->get_next_seq('s_grpt');
00492 $this->id=$p_jrn;
00493
00494 $internal=$this->compute_internal_code($seq);
00495 $this->internal=$internal;
00496
00497 $cust=new Fiche($this->db);
00498 $cust->get_by_qcode($e_client);
00499 $sposte=$cust->strAttribut(ATTR_DEF_ACCOUNT);
00500
00501 if ( strpos($sposte,',') != 0 )
00502 {
00503 $array=explode(',',$sposte);
00504 $poste=$array[1];
00505 }
00506 else
00507 {
00508 $poste=$sposte;
00509 }
00510
00511 $oPeriode=new Periode($this->db);
00512 $check_periode=$this->check_periode();
00513
00514 if ( $check_periode == true )
00515 $tperiode=$period;
00516 else
00517 $tperiode=$oPeriode->find_periode($e_date);
00518
00519 bcscale(4);
00520 try
00521 {
00522 $tot_amount=0;
00523 $tot_tva=0;
00524 $tot_debit=0;
00525 $this->db->start();
00526 $tot_nd=0;
00527 $tot_perso=0;
00528 $tot_tva_nd=0;
00529 $tot_tva_ndded=0;
00530 $tot_tva_reversed=0;
00531 $tva=array();
00532
00533 for ($i=0;$i< $nb_item;$i++)
00534 {
00535 $n_both=0;
00536 if ( strlen(trim(${'e_march'.$i})) == 0 ) continue;
00537
00538
00539 $fiche=new Fiche($this->db);
00540 $fiche->get_by_qcode(${"e_march".$i});
00541 $tva_both=0;
00542
00543 if ($g_parameter->MY_TVA_USE=='Y')
00544 {
00545 $idx_tva=${'e_march'.$i.'_tva_id'};
00546 $oTva=new Acc_Tva($this->db);
00547 $oTva->set_parameter('id',$idx_tva);
00548 $oTva->load();
00549 $tva_both=$oTva->get_parameter("both_side");
00550 }
00551
00552 $acc_operation=new Acc_Operation($this->db);
00553 $acc_operation->date=$e_date;
00554 $acc_operation->grpt=$seq;
00555 $acc_operation->jrn=$p_jrn;
00556 $acc_operation->type='d';
00557 $acc_operation->periode=$tperiode;
00558 $acc_operation->qcode="";
00559
00560
00561
00562 $amount=bcmul(${'e_march'.$i.'_price'},${'e_quant'.$i});
00563
00564 $acc_amount=new Acc_Compute();
00565 $acc_amount->check=false;
00566 $acc_amount->set_parameter('amount',$amount);
00567
00568
00569 if ( $g_parameter->MY_TVA_USE=='Y')
00570 {
00571 $acc_amount->set_parameter('amount_vat_rate',$oTva->get_parameter('rate'));
00572 if ( strlen(trim(${'e_march'.$i.'_tva_amount'})) ==0 || ${'e_march'.$i.'_tva_amount'} == 0)
00573 {
00574 $acc_amount->compute_vat();
00575
00576 }
00577 else
00578 {
00579 $acc_amount->amount_vat= ${'e_march'.$i.'_tva_amount'};
00580
00581 }
00582 $tot_tva=bcadd($tot_tva,$acc_amount->amount_vat);
00583 }
00584
00585
00586
00587 $save_amount_vat=$acc_amount->amount_vat;
00588 $this->compute_no_deductible($acc_amount, $fiche);
00589 $acc_amount->correct();
00590
00591 $acc_amount->amount_unpaid=($tva_both == 1 ) ? $save_amount_vat :0 ;
00592 $tot_tva_reversed=bcadd($tot_tva_reversed,$acc_amount->amount_unpaid);
00593
00594
00595
00596 $tot_amount=bcadd($tot_amount,$amount);
00597
00598
00599 $sposte=$fiche->strAttribut(ATTR_DEF_ACCOUNT);
00600
00601 if ( strpos($sposte,',') != 0 )
00602 {
00603 $array=explode(',',$sposte);
00604 $poste_val=$array[0];
00605 }
00606 else
00607 {
00608 $poste_val=$sposte;
00609 }
00610 if ( $g_parameter->MY_UPDLAB=='Y')
00611 $acc_operation->desc=strip_tags(${"e_march".$i."_label"});
00612 else
00613 $acc_operation->desc=null;
00614 $acc_operation->poste=$poste_val;
00615 $acc_operation->amount=$acc_amount->amount;
00616 $acc_operation->qcode=${"e_march".$i};
00617 if( $acc_amount->amount > 0 ) $tot_debit=bcadd($tot_debit,$acc_amount->amount);
00618 $j_id=$acc_operation->insert_jrnx();
00619
00620
00621 $this->insert_no_deductible($acc_amount, $fiche, $tva_both, $tot_debit,$acc_operation,$group,$i);
00622
00623
00624
00625 if ( $g_parameter->MY_TVA_USE=='Y')
00626 {
00627 $tva_item=$acc_amount->amount_vat;
00628
00629 if (isset($tva[$idx_tva] ) )
00630 $tva[$idx_tva]=bcadd($tva[$idx_tva],$tva_item);
00631 else
00632 $tva[$idx_tva]=$tva_item;
00633
00634 }
00635
00636
00637
00638
00639 $nNeg=(${"e_quant" . $i}< 0) ? -1 : 1;
00640
00641
00642
00643 if ( $g_parameter->MY_STOCK='Y'&& isset ($repo))
00644 {
00645 $dir=(${'e_quant'.$i} < 0 ) ? 'c':'d';
00646 Stock_Goods::insert_goods($this->db,array('j_id'=>$j_id,'goods'=>${'e_march'.$i},'quant'=>$nNeg*${'e_quant'.$i},'dir'=>$dir,'repo'=>$repo)) ;
00647 }
00648
00649 if ( $g_parameter->MY_ANALYTIC != "nu" )
00650 {
00651
00652 $op=new Anc_Operation($this->db);
00653 $op->oa_group=$group;
00654 $op->j_id=$j_id;
00655 $op->oa_date=$e_date;
00656 $op->oa_debit='t';
00657 $op->oa_description=sql_string($e_comm);
00658 $op->save_form_plan($_POST,$i,$j_id);
00659 }
00660
00661
00662 if ( $g_parameter->MY_TVA_USE=='Y')
00663 {
00664
00665 $r=$this->db->exec_sql("select insert_quant_purchase ".
00666 "(null".
00667 ",".$j_id.
00668 ",'".${"e_march".$i}."'".
00669 ",".${"e_quant".$i}.",".
00670 round($amount,2).
00671 ",".$acc_amount->amount_vat.
00672 ",".$oTva->get_parameter('id').
00673 ",".$acc_amount->amount_nd.
00674 ",".$acc_amount->nd_vat.
00675 ",".$acc_amount->nd_ded_vat.
00676 ",".$acc_amount->amount_perso.
00677 ",'".$e_client."',". $acc_amount->amount_unpaid.")");
00678
00679 }
00680 else
00681 {
00682 $r=$this->db->exec_sql("select insert_quant_purchase ".
00683 "(null".
00684 ",".$j_id.
00685 ",'".${"e_march".$i}."'".
00686 ",".${"e_quant".$i}.",".
00687 round($amount,2).
00688 ",0".
00689 ",null".
00690 ",".$acc_amount->amount_nd.
00691 ",0".
00692 ",".$acc_amount->nd_ded_vat.
00693 ",".$acc_amount->amount_perso.
00694 ",'".$e_client."',".$acc_amount->amount_unpaid.")");
00695
00696
00697 }
00698
00699 }
00700
00701 $cust_amount=round(bcadd($tot_amount,$tot_tva),2);
00702 $acc_operation=new Acc_Operation($this->db);
00703 $acc_operation->date=$e_date;
00704 $acc_operation->poste=$poste;
00705 $acc_operation->amount=$cust_amount-$tot_tva_reversed;
00706 $acc_operation->grpt=$seq;
00707 $acc_operation->jrn=$p_jrn;
00708 $acc_operation->type='c';
00709 $acc_operation->periode=$tperiode;
00710 $acc_operation->qcode=${"e_client"};
00711 if ( $cust_amount < 0 )
00712 $tot_debit=bcadd($tot_debit,abs($cust_amount));
00713 $let_client=$acc_operation->insert_jrnx();
00714
00715
00716 if ( $g_parameter->MY_TVA_USE=='Y')
00717 {
00718
00719
00720
00721 foreach ($tva as $i => $value)
00722 {
00723 $oTva=new Acc_Tva($this->db);
00724 $oTva->set_parameter('id',$i);
00725 $oTva->load();
00726
00727 $poste_vat=$oTva->get_side('d');
00728
00729 $cust_amount=bcadd($tot_amount,$tot_tva);
00730 $acc_operation=new Acc_Operation($this->db);
00731 $acc_operation->date=$e_date;
00732 $acc_operation->poste=$poste_vat;
00733 $acc_operation->amount=$value;
00734 $acc_operation->grpt=$seq;
00735 $acc_operation->jrn=$p_jrn;
00736 $acc_operation->type='d';
00737 $acc_operation->periode=$tperiode;
00738 if ( $value > 0 ) $tot_debit=bcadd($tot_debit,abs($value));
00739 $acc_operation->insert_jrnx();
00740
00741 if ( $oTva->get_parameter("both_side")==1)
00742 {
00743 $poste_vat=$oTva->get_side('c');
00744 $cust_amount=bcadd($tot_amount,$tot_tva);
00745 $cust_amount=bcsub($tot_amount,$tot_tva_reversed);
00746 $acc_operation=new Acc_Operation($this->db);
00747 $acc_operation->date=$e_date;
00748 $acc_operation->poste=$poste_vat;
00749 $acc_operation->amount=$tot_tva_reversed;
00750 $acc_operation->grpt=$seq;
00751 $acc_operation->jrn=$p_jrn;
00752 $acc_operation->type='c';
00753 $acc_operation->periode=$tperiode;
00754 $acc_operation->insert_jrnx();
00755 if ( $value < 0 ) $tot_debit=bcadd($tot_debit,abs($value));
00756 }
00757
00758 }
00759 }
00760
00761 $acc_operation=new Acc_Operation($this->db);
00762 $acc_operation->date=$e_date;
00763 $acc_operation->echeance=$e_ech;
00764 $acc_operation->amount=abs(round($tot_debit,2));
00765 $acc_operation->desc=$e_comm;
00766 $acc_operation->grpt=$seq;
00767 $acc_operation->jrn=$p_jrn;
00768 $acc_operation->periode=$tperiode;
00769 $acc_operation->pj=$e_pj;
00770 $acc_operation->mt=$mt;
00771 $this->jr_id=$acc_operation->insert_jrn();
00772 $this->pj=$acc_operation->set_pj();
00773
00774
00775 $this->grpt_id=$seq;
00776 $this->update_internal_code($internal);
00777
00778 $this->db->exec_sql('update quant_purchase set qp_internal = $1 where j_id in (select j_id from jrnx where j_grpt=$2)',
00779 array($internal,$seq));
00780
00781
00782 if ( strcmp($e_pj,$e_pj_suggest) == 0 && strlen(trim($e_pj)) != 0 )
00783 {
00784 $this->inc_seq_pj();
00785 }
00786
00787
00788 if ( isset ($_FILES))
00789 {
00790 if ( sizeof($_FILES) != 0 )
00791 $this->db->save_upload_document($seq);
00792 }
00793 $str_file="";
00794
00795
00796 if ( isset($_POST['gen_invoice']) )
00797 {
00798 $ref_doc= $this->create_document($internal,$p_array);
00799 $this->doc='<A class="line" HREF="show_pj.php?'.dossier::get().'&jr_grpt_id='.$seq.'&jrn='.$this->id.'">'.$ref_doc.'</A>';
00800 }
00801
00802
00803
00804
00805 if ( $e_mp != 0 )
00806 {
00807
00808 $mp=new Acc_Payment($this->db,$e_mp);
00809 $mp->load();
00810
00811
00812 if ($mp->get_parameter('qcode') == '')
00813 $fqcode=${'e_mp_qcode_'.$e_mp};
00814 else
00815 $fqcode=$mp->get_parameter('qcode');
00816
00817 $acfiche = new Fiche($this->db);
00818 $acfiche->get_by_qcode($fqcode);
00819
00820
00821 $acseq=$this->db->get_next_seq('s_grpt');
00822 $acjrn=new Acc_Ledger($this->db,$mp->get_parameter('ledger_target'));
00823 $acinternal=$acjrn->compute_internal_code($acseq);
00824
00825
00826 $acc_pay=new Acc_Operation($this->db);
00827 $acc_pay->date=$e_date;
00828
00829
00830 $sposte=$acfiche->strAttribut(ATTR_DEF_ACCOUNT);
00831
00832 if ( strpos($sposte,',') != 0 )
00833 {
00834 $array=explode(',',$sposte);
00835 $poste_val=$array[1];
00836 }
00837 else
00838 {
00839 $poste_val=$sposte;
00840 }
00841
00842 $famount=bcsub($cust_amount,$acompte);
00843 $acc_pay->poste=$poste_val;
00844 $acc_pay->qcode=$fqcode;
00845 $acc_pay->amount=abs(round($famount,2));
00846 $acc_pay->desc='';
00847 $acc_pay->grpt=$acseq;
00848 $acc_pay->jrn=$mp->get_parameter('ledger_target');
00849 $acc_pay->periode=$tperiode;
00850 $acc_pay->type=($famount>=0)?'c':'d';
00851 $acc_pay->insert_jrnx();
00852
00853
00854 $acc_pay=new Acc_Operation($this->db);
00855 $acc_pay->date=$e_date;
00856 $acc_pay->poste=$poste;
00857 $acc_pay->qcode=$e_client;
00858 $acc_pay->amount=abs(round($famount,2));
00859 $acc_pay->desc='';
00860 $acc_pay->grpt=$acseq;
00861 $acc_pay->jrn=$mp->get_parameter('ledger_target');
00862 $acc_pay->periode=$tperiode;
00863 $acc_pay->type=($famount>=0)?'d':'c';
00864 $let_other=$acc_pay->insert_jrnx();
00865
00866
00867 $acc_pay->mt=$mt;
00868 $acc_pay->desc=(!isset($e_comm_paiement) || strlen(trim($e_comm_paiement)) == 0) ?$e_comm:$e_comm_paiement;
00869 $mp_jr_id=$acc_pay->insert_jrn();
00870 $acjrn->grpt_id=$acseq;
00871 $acjrn->update_internal_code($acinternal);
00872
00873 $r1=$this->get_id($internal);
00874 $r2=$this->get_id($acinternal);
00875
00876
00877
00878
00879 $oletter=new Lettering($this->db);
00880 $oletter->insert_couple($let_client,$let_other);
00881
00882
00883 $Res=$this->db->exec_sql("update jrn set jr_rapt='paid' where jr_id=$1",array($r1));
00884
00885
00886 $rec=new Acc_Reconciliation($this->db);
00887 $rec->set_jr_id($r1);
00888 $rec->insert($r2);
00889
00890
00891
00892
00893
00894 $ledger=new Acc_Ledger_Fin($this->db,$acc_pay->jrn);
00895 $prop=$ledger->get_propertie();
00896
00897
00898 if ( $prop['jrn_def_type'] == 'FIN' )
00899 {
00900 $ledger->insert_quant_fin($acfiche->id,$mp_jr_id,$cust->id,bcmul($famount,-1));
00901 }
00902
00903
00904 }
00905 }
00906 catch (Exception $e)
00907 {
00908 echo '<span class="error">'.
00909 'Erreur dans l\'enregistrement '.
00910 __FILE__.':'.__LINE__.' '.
00911 $e->getMessage().$e->getTraceAsString();
00912
00913 $this->db->rollback();
00914 throw new Exception($e);
00915 }
00916 $this->db->commit();
00917 return $internal;
00918 }
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952 public function input($p_array=null,$p_readonly=0)
00953 {
00954 global $g_parameter,$g_user;
00955 if ( $p_array != null ) extract($p_array);
00956
00957 $flag_tva=$g_parameter->MY_TVA_USE;
00958
00959 $f_add_button=new IButton('add_card');
00960 $f_add_button->label=_('Créer une nouvelle fiche');
00961 $f_add_button->tabindex=-1;
00962 $f_add_button->set_attribute('jrn',$this->id);
00963 $f_add_button->javascript=" this.jrn=\$('p_jrn').value;select_card_type(this);";
00964
00965 $str_add_button="";
00966 if ($g_user->check_action(FICADD)==1)
00967 {
00968 $str_add_button=$f_add_button->input();
00969 }
00970
00971 $oPeriode=new Periode($this->db);
00972 list ($l_date_start,$l_date_end)=$oPeriode->get_date_limit($g_user->get_periode());
00973 if ( $g_parameter->MY_DATE_SUGGEST=='Y' )
00974 $op_date=( ! isset($e_date) ) ?$l_date_start:$e_date;
00975 else
00976 $op_date=( ! isset($e_date) ) ?'':$e_date;
00977
00978 $e_ech=(isset($e_ech))?$e_ech:"";
00979 $e_comm=(isset($e_comm))?$e_comm:"";
00980
00981 $r="";
00982 $r.=dossier::hidden();
00983 $f_legend_detail=_("Détail articles achetés");
00984
00985
00986
00987 $Date=new IDate();
00988 $Date->setReadOnly(false);
00989 $Date->table=1;
00990 $Date->tabindex=1;
00991 $f_date=$Date->input("e_date",$op_date);
00992
00993
00994 $Echeance=new IDate();
00995 $Echeance->setReadOnly(false);
00996 $Echeance->tabindex=2;
00997 $label=HtmlInput::infobulle(4);
00998 $f_echeance=$Echeance->input('e_ech',$e_ech,'Echéance'.$label);
00999 $f_periode="";
01000 if ($this->check_periode() == true)
01001 {
01002
01003
01004 $l_user_per=$g_user->get_periode();
01005 $def=(isset($periode))?$periode:$l_user_per;
01006
01007 $period=new IPeriod("period");
01008 $period->user=$g_user;
01009 $period->cn=$this->db;
01010 $period->value=$def;
01011 $period->type=OPEN;
01012 try
01013 {
01014 $l_form_per=$period->input();
01015 }
01016 catch (Exception $e)
01017 {
01018 if ($e->getCode() == 1 )
01019 {
01020 throw Exception( _("Aucune période ouverte"));
01021 }
01022 }
01023
01024 $r.="<td>";
01025 $label=HtmlInput::infobulle(3);
01026 $f_periode=_("Période comptable")." $label ".$l_form_per;
01027 }
01028
01029
01030
01031 $add_js="";
01032 if ( $g_parameter->MY_PJ_SUGGEST=='Y')
01033 {
01034 $add_js="update_pj();";
01035 }
01036 if ($g_parameter->MY_DATE_SUGGEST == 'Y')
01037 {
01038 $add_js.='get_last_date();';
01039 }
01040 $add_js.='update_name();';
01041 $add_js.='update_pay_method();';
01042 $add_js.='update_row("sold_item");';
01043
01044 $wLedger=$this->select_ledger('ACH',2);
01045 if ($wLedger == null) throw Exception(_('Pas de journal disponible'));
01046 $wLedger->javascript="onChange='update_predef(\"ach\",\"f\",\"".$_REQUEST['ac']."\");$add_js'";
01047 $wLedger->table=1;
01048 $f_jrn=$wLedger->input();
01049
01050
01051
01052 $Commentaire=new IText();
01053 $Commentaire->table=0;
01054 $Commentaire->setReadOnly(false);
01055 $Commentaire->size=60;
01056 $Commentaire->tabindex=3;
01057 $label=HtmlInput::infobulle(1) ;
01058 $f_desc=$Commentaire->input("e_comm",h($e_comm)).$label;
01059
01060
01061
01062
01063 $default_pj='';
01064 if ( $g_parameter->MY_PJ_SUGGEST=='Y')
01065 {
01066 $default_pj=$this->guess_pj();
01067 }
01068
01069 $pj=new IText();
01070 $pj->value=(isset($e_pj))?$e_pj:$default_pj;
01071
01072
01073 $pj->table=0;
01074 $pj->name="e_pj";
01075 $pj->size=10;
01076 $pj->readonly=false;
01077
01078 $f_pj=$pj->input().HtmlInput::hidden('e_pj_suggest',$default_pj);
01079
01080
01081
01082 $fiche='cred';
01083
01084
01085
01086 $e_client=( isset ($e_client) )?$e_client:"";
01087 $e_client_label=" ";
01088
01089
01090
01091
01092
01093 if ( strlen(trim($e_client)) != 0)
01094 {
01095 $fClient=new Fiche($this->db);
01096 $fClient->get_by_qcode($e_client);
01097 $e_client_label=$fClient->strAttribut(ATTR_DEF_NAME).' '.
01098 ' Adresse : '.$fClient->strAttribut(ATTR_DEF_ADRESS).' '.
01099 $fClient->strAttribut(ATTR_DEF_CP).' '.
01100 $fClient->strAttribut(ATTR_DEF_CITY).' ';
01101
01102
01103 }
01104
01105 $W1=new ICard();
01106 $W1->label=_("Fournisseur ").HtmlInput::infobulle(0) ;
01107 $W1->name="e_client";
01108 $W1->tabindex=3;
01109 $W1->value=$e_client;
01110 $W1->table=0;
01111 $W1->set_dblclick("fill_ipopcard(this);");
01112 $W1->set_attribute('ipopup','ipopcard');
01113
01114
01115 $W1->set_attribute('label','e_client_label');
01116
01117 $W1->set_attribute('typecard','cred');
01118
01119
01120 $W1->set_callback('filter_card');
01121 $W1->set_function('fill_data');
01122 $W1->javascript=sprintf(' onchange="fill_data_onchange(\'%s\');" ',
01123 $W1->name);
01124 $f_client_qcode=$W1->input();
01125 $client_label=new ISpan();
01126 $client_label->table=0;
01127 $f_client=$client_label->input("e_client_label",$e_client_label);
01128 $f_client_bt=$W1->search();
01129
01130
01131
01132
01133 $e_comment=(isset($e_comment))?$e_comment:"";
01134 $p_article= ( isset ($nb_item))?$nb_item:$this->get_min_row();
01135 $max=($p_article < $this->get_min_row())?$this->get_min_row():$p_article;
01136
01137 $Hid=new IHidden();
01138 $r.=$Hid->input("nb_item",$p_article);
01139
01140
01141
01142 for ($i=0;$i< $max ;$i++)
01143 {
01144
01145
01146 $march=(isset(${"e_march$i"}))?${"e_march$i"}:"" ;
01147 $march_price=(isset(${"e_march".$i."_price"}))?${"e_march".$i."_price"}:""
01148 ;
01149
01150 if ( $g_parameter->MY_TVA_USE=='Y')
01151 {
01152 $march_tva_id=(isset(${"e_march$i"."_tva_id"}))?${"e_march$i"."_tva_id"}:"";
01153 $march_tva_amount=(isset(${"e_march$i"."_tva_amount"}))?${"e_march$i"."_tva_amount"}:"";
01154 }
01155
01156
01157
01158 $march_label=(isset(${"e_march".$i."_label"}))?${"e_march".$i."_label"}:"";
01159
01160
01161 if ( strlen(trim($march))!=0 && strlen(trim($march_label))==0 )
01162 {
01163 $fMarch=new Fiche($this->db);
01164 $fMarch->get_by_qcode($march);
01165 $march_label=$fMarch->strAttribut(ATTR_DEF_NAME);
01166
01167 if ( ! isset($march_tva_id) && $g_parameter->MY_TVA_USE=='Y' )
01168 $march_tva_id=$fMarch->strAttribut(ATTR_DEF_TVA);
01169 }
01170
01171
01172 $W1=new ICard();
01173 $W1->label="";
01174 $W1->name="e_march".$i;
01175 $W1->value=$march;
01176 $W1->table=1;
01177 $W1->set_dblclick("fill_ipopcard(this);");
01178 $W1->set_attribute('ipopup','ipopcard');
01179
01180 $W1->set_attribute('typecard','deb');
01181
01182
01183 $W1->set_attribute('label','e_march'.$i.'_label');
01184
01185 $W1->set_attribute('purchase','e_march'.$i.'_price');
01186 $W1->set_attribute('price','e_march'.$i.'_price');
01187
01188
01189 $W1->set_attribute('tvaid','e_march'.$i.'_tva_id');
01190
01191 $W1->set_callback('filter_card');
01192 $W1->set_function('fill_data');
01193 $W1->javascript=sprintf(' onchange="fill_data_onchange(\'%s\');" ',
01194 $W1->name);
01195 $W1->readonly=false;
01196 $array[$i]['quick_code']=$W1->input();
01197 $array[$i]['bt']=$W1->search();
01198
01199 $array[$i]['hidden']='';
01200
01201 if ( $g_parameter->MY_TVA_USE=='Y')
01202 {
01203 $array[$i]['hidden'].=HtmlInput::hidden('tva_march'.$i,0);
01204 }
01205
01206 if ( $g_parameter->MY_TVA_USE=='Y')
01207 $tvac=new INum('tvac_march'.$i);
01208 else
01209 $tvac=new IHidden('tvac_march'.$i);
01210
01211 $tvac->readOnly=1;
01212 $tvac->value=0;
01213 $array[$i]['tvac']=$tvac->input();
01214
01215 $htva=new INum('htva_march'.$i);
01216 $htva->readOnly=1;
01217
01218 $htva->value=0;
01219 $array[$i]['htva']=$htva->input();
01220
01221 if ( $g_parameter->MY_UPDLAB == 'Y')
01222 {
01223 $Span=new IText("e_march".$i."_label");
01224 $Span->css_size="100%";
01225 } else
01226 {
01227 $Span=new ISpan("e_march".$i."_label");
01228 }
01229 $Span->value=$march_label;
01230 $Span->setReadOnly(false);
01231
01232
01233 $array[$i]['denom']=$Span->input("e_march".$i."_label",$march_label);
01234
01235 $Price=new INum();
01236 $Price->setReadOnly(false);
01237 $Price->size=9;
01238 $Price->javascript="onBlur='format_number(this);clean_tva($i);compute_ledger($i)'";
01239 $array[$i]['pu']=$Price->input("e_march".$i."_price",$march_price);
01240 if ( $g_parameter->MY_TVA_USE=='Y')
01241 {
01242
01243
01244
01245 $Tva=new ITva_Popup($this->db);
01246 $Tva->js="onblur=\"format_number(this);onChange=clean_tva($i);compute_ledger($i)\"";
01247 $Tva->in_table=true;
01248 $Tva->set_attribute('compute',$i);
01249 $Tva->value=$march_tva_id;
01250 $array[$i]['tva']=$Tva->input("e_march$i"."_tva_id");
01251
01252
01253
01254
01255 $Tva_amount=new INum();
01256 $Tva_amount->setReadOnly(false);
01257 $Tva_amount->size=9;
01258 $Tva_amount->javascript="onBlur='format_number(this);compute_ledger($i)'";
01259 $array[$i]['amount_tva']=$Tva_amount->input("e_march".$i."_tva_amount",$march_tva_amount);
01260 }
01261
01262
01263 $quant=(isset(${"e_quant$i"}))?${"e_quant$i"}:"1"
01264 ;
01265 $Quantity=new INum();
01266 $Quantity->setReadOnly(false);
01267 $Quantity->size=9;
01268 $Quantity->javascript="onChange=format_number(this);clean_tva($i);compute_ledger($i)";
01269 $array[$i]['quantity']=$Quantity->input("e_quant".$i,$quant);
01270
01271 }
01272 $f_type=_('Fournisseur');
01273
01274 ob_start();
01275 require_once('template/form_ledger_detail.php');
01276 $r.=ob_get_contents();
01277 ob_end_clean();
01278
01279
01280 $r.= HtmlInput::hidden('jrn_type','ACH');
01281 $r.= HtmlInput::button('add_item',_('Ajout article'), ' onClick="ledger_add_row()"');
01282
01283
01284
01285
01286 if ( $g_parameter->MY_PJ_SUGGEST=='Y')
01287 {
01288 $r.='<script> update_pj();</script>';
01289 }
01290
01291 $r.= create_script("$('".$Date->id."').focus()");
01292 return $r;
01293 }
01294
01295
01296
01297
01298
01299
01300
01301 function confirm($p_array,$p_summary=false)
01302 {
01303 global $g_parameter;
01304 extract ($p_array);
01305
01306
01307 if ( ! $p_summary )
01308 $this->verify($p_array) ;
01309
01310 $anc=null;
01311
01312
01313
01314 bcscale(4);
01315 $client=new Fiche($this->db);
01316 $client->get_by_qcode($e_client,true);
01317
01318 $client_name=h($client->getName().
01319 ' '.$client->strAttribut(ATTR_DEF_ADRESS).' '.
01320 $client->strAttribut(ATTR_DEF_CP).' '.
01321 $client->strAttribut(ATTR_DEF_CITY));
01322 $lPeriode=new Periode($this->db);
01323 if ($this->check_periode() == true)
01324 {
01325 $lPeriode->p_id=$period;
01326 }
01327 else
01328 {
01329 $lPeriode->find_periode($e_date);
01330 }
01331 $date_limit=$lPeriode->get_date_limit();
01332 $r="";
01333 $r.='<TABLE>';
01334 if ( $p_summary ) {
01335 $jr_id=$this->db->get_value('select jr_id from jrn where jr_internal=$1',array($this->internal));
01336 $r.="<tr>";
01337 $r.='<td>';
01338 $r.=_('Détail opération ');
01339 $r.='</td>';
01340 $r.='<td>';
01341 $r.=sprintf ('<a class="line" style="display:inline" href="javascript:modifyOperation(%d,%d)">%s</a>',
01342 $jr_id,dossier::id(),$this->internal);
01343 $r.='</td>';
01344 $r.="</tr>";
01345 }
01346 $r.='<tr>';
01347 if ( ! $p_summary) {
01348 $r.='<td>' . _('Numéro Pièce') .'</td><td>'. hb($e_pj) . '</td>';
01349 } else {
01350
01351 if ( strcmp($this->pj,$e_pj) != 0 )
01352 {
01353 $r.='<td>' . _('Numéro Pièce') .'</td><td>'. hb($this->pj) .
01354 '<span class="notice"> '._('Attention numéro pièce existante, elle a du être adaptée').'</span></td>';
01355 } else {
01356 $r.='<td>' . _('Numéro Pièce') .'</td><td>'. hb($this->pj) . '</td>';
01357 }
01358 }
01359 $r.='</tr>';
01360 $r.='<td> ' . _('Date') . '</td><td> ' . hb($e_date) . '</td>';
01361 $r.='</tr>';
01362 $r.='<tr>';
01363 $r.='<td>' . _('Echeance') . '</td><td> ' . hb($e_ech) . '</td>';
01364 $r.='</tr>';
01365
01366
01367 $r.='<tr>';
01368 $r.='<td> ' . _('Période Comptable') . '</td><td> ' .hb( $date_limit['p_start'] . '-' . $date_limit['p_end']) . '</td>';
01369 $r.='</tr>';
01370 $r.='<tr>';
01371 $r.='<td> ' . _('Journal') . '</td><td> ' . hb($this->get_name()) . '</td>';
01372 $r.='</tr>';
01373 $r.='<tr>';
01374 $r.='<td> ' . _('Libellé') . '</td><td> ' . hb($e_comm) . '</td>';
01375 $r.='</tr>';
01376 $r.='<tr>';
01377
01378 $r.='<tr>';
01379 $r.='<td> ' . _('Fournisseur') . '</td><td> ' . hb($e_client . ':' . $client_name) . '</td>';
01380 $r.='</tr>';
01381 $r.='</table>';
01382 $r.='<h2>' . _('Détail articles achetés') . '</h2>';
01383 $r.='<p class="decale">';
01384 $r.='<table class="result" >';
01385 $r.='<TR>';
01386 $r.="<th>" . _('Code') . "</th>";
01387 $r.="<th>" . _('Dénomination') . "</th>";
01388 $r.="<th style=\"text-align:right\">" . _('prix') . "</th>";
01389 $r.="<th style=\"text-align:right\">" . _('quantité') . "</th>";
01390
01391
01392 if ($g_parameter->MY_TVA_USE == 'Y') {
01393 $r.="<th style=\"text-align:right\">" . _('tva') . "</th>";
01394 $r.='<th style="text-align:right"> ' . _('Montant TVA') . '</th>';
01395 $r.='<th style="text-align:right">' . _('Montant HTVA') . '</th>';
01396 $r.='<th style="text-align:right">' . _('Montant TVAC') . '</th>';
01397 } else {
01398 $r.='<th style="text-align:right">' . _('Montant') . '</th>';
01399 }
01400
01401
01402 if ($g_parameter->MY_ANALYTIC!='nu')
01403 {
01404 $anc=new Anc_Plan($this->db);
01405 $a_anc=$anc->get_list();
01406 $x=count($a_anc);
01407
01408 $r.='<th colspan="'.$x.'">'._('Compt. Analytique').'</th>';
01409
01410
01411 $r.=Anc_Plan::hidden($a_anc);
01412 }
01413
01414 $r.='</tr>';
01415 $tot_amount=0.0;
01416 $tot_tva=0.0;
01417
01418
01419
01420 for ($i = 0; $i < $nb_item;$i++)
01421 {
01422 $tot_row=0;
01423 if ( strlen(trim(${"e_march".$i})) == 0 ) continue;
01424
01425
01426 $fiche=new Fiche($this->db);
01427 $fiche->get_by_qcode(${"e_march".$i});
01428 if ( $g_parameter->MY_UPDLAB=='Y')
01429 $fiche_name=h(${"e_march".$i."_label"});
01430 else
01431 $fiche_name=$fiche->strAttribut (ATTR_DEF_NAME);
01432
01433 $amount=bcmul(${"e_march".$i."_price"},${'e_quant'.$i});
01434 if ( $g_parameter->MY_TVA_USE=='Y')
01435 {
01436 $idx_tva=${"e_march".$i."_tva_id"};
01437 $oTva=new Acc_Tva($this->db);
01438 $oTva->set_parameter('id',$idx_tva);
01439 $oTva->load();
01440 $op=new Acc_Compute();
01441
01442 $op->set_parameter("amount",$amount);
01443 $op->set_parameter('amount_vat_rate',$oTva->get_parameter('rate'));
01444 $op->compute_vat();
01445 $tva_computed=$op->get_parameter('amount_vat');
01446
01447 if ( strlen (trim (${'e_march'.$i.'_tva_amount'})) == 0)
01448 {
01449 $tva_item=$op->get_parameter('amount_vat');
01450 }
01451 else
01452 $tva_item=round(${'e_march'.$i.'_tva_amount'},2);
01453
01454 if (isset($tva[$idx_tva] ) )
01455 $tva[$idx_tva]+=$tva_item;
01456 else
01457 $tva[$idx_tva]=$tva_item;
01458
01459
01460
01461 }
01462 $tot_amount=round(bcadd($tot_amount,$amount),2);
01463 $tot_row=bcadd($tot_row,$amount);
01464 $r.='<tr>';
01465 $r.='<td>';
01466 $r.=${"e_march".$i};
01467 $r.='</td>';
01468 $r.='<TD style="border-bottom:1px dotted grey;">';
01469 $r.=$fiche_name;
01470 $r.='</td>';
01471 $r.='<td class="num">';
01472 $r.=nbm(${"e_march".$i."_price"});
01473 $r.='</td>';
01474 $r.='<td class="num">';
01475 $r.=nbm(${"e_quant".$i});
01476 $r.='</td>';
01477 $both_side=0;
01478 if ($g_parameter->MY_TVA_USE == 'Y')
01479 {
01480 $r.='<td class="num">';
01481 $r.=$oTva->get_parameter('label');
01482 $both_side=$oTva->get_parameter("both_side");
01483 if ( $both_side == 0) {
01484 $tot_row=bcadd($tot_row,$tva_item);
01485 $tot_tva=round(bcadd($tva_item,$tot_tva),2);
01486 }
01487 $r.='</td>';
01488
01489
01490 bcscale(2);
01491 $css_void_tva=($both_side == 1)?'style="text-decoration:line-through"':'';
01492 if ( bcsub($tva_item,$tva_computed) != 0 && ! ($tva_item == 0 && $both_side == 1))
01493 {
01494
01495 $r.='<td style="background-color:red" class="num" '.$css_void_tva.'>';
01496 $r.=HtmlInput::infobulle(28);
01497 $r.='<a href="#" class="error" style="display:inline" title="'. _("Attention Différence entre TVA calculée et donnée").'">'
01498 .nbm($tva_item).'<a>';
01499 }
01500 else{
01501 $r.='<td class="num" '.$css_void_tva.'>';
01502 $r.=nbm($tva_item);
01503 }
01504 $r.='</td>';
01505 $r.='<td class="num">';
01506 $r.=nbm(round($amount,2));
01507 $r.='</td>';
01508 }
01509 $r.='<td class="num">';
01510 $r.=nbm(round($tot_row,2));
01511 $r.='</td>';
01512
01513 if ( $g_parameter->MY_ANALYTIC!='nu')
01514 {
01515
01516 $anc_op=new Anc_Operation($this->db);
01517 $null=($g_parameter->MY_ANALYTIC=='op')?1:0;
01518 $r.='<td>';
01519 $p_mode=($p_summary==false)?1:0;
01520 $p_array['pa_id']=$a_anc;
01521
01522 $r.=HtmlInput::hidden('op[]=',$i);
01523 $r.=$anc_op->display_form_plan($p_array,$null,$p_mode,$i,$amount);
01524 $r.='</td>';
01525 }
01526
01527
01528 $r.='</tr>';
01529
01530 }
01531
01532 $decalage=($g_parameter->MY_TVA_USE == 'Y')?'<td></td><td></td><td></td><td></td>':'<td></td>';
01533 $tot = round(bcadd($tot_amount, $tot_tva), 2);
01534 $tot_tva=nbm($tot_tva);
01535 $tot=nbm($tot);
01536 $str_tot=_('Totaux');
01537 $tot_amount=nbm($tot_amount);
01538 $r.=<<<EOF
01539 <tr class="highlight">
01540 {$decalage}
01541 <td>
01542 {$str_tot}
01543 </td>
01544 <td class="num">
01545 {$tot_tva}
01546 </td>
01547 <td class="num">
01548 {$tot_amount}
01549 </td>
01550 <td class="num">
01551 {$tot}
01552 </td>
01553 EOF;
01554
01555 $r.='</table>';
01556 $r.='</p>';
01557 if ( $g_parameter->MY_ANALYTIC!='nu' && !$p_summary)
01558 $r.='<input type="button" class="button" value="'._('Vérifiez imputation analytique').'" onClick="verify_ca(\'\');">';
01559
01560 $r.=(! $p_summary )?'<div id="total_div_id" >':'<div>';
01561 $r.='<h2>Totaux</h2>';
01562
01563 if ($g_parameter->MY_TVA_USE == 'Y') {
01564 $r.='<table>';
01565 $r.='<tr><td>Total HTVA</td>';
01566 $r.=td(hb($tot_amount ),'class="num"');
01567 foreach ($tva as $i => $value) {
01568 $oTva->set_parameter('id', $i);
01569 $oTva->load();
01570
01571 $r.='<tr><td> TVA ' . $oTva->get_parameter('label').'</td>';
01572 $r.=td(hb(nbm($tva[$i])),'class="num"');
01573 }
01574 $r.='<tr>'.td(_('Total TVA')).td(hb($tot_tva),'class="num"');
01575 $r.='<tr>'.td(_('Total TVAC')).td(hb($tot),'class="num"');
01576 $r.='</table>';
01577 } else {
01578 $r.='<br>Total '.hb($tot);
01579 }
01580 $r.='</div>';
01581
01582 $r.=HtmlInput::hidden('e_client',$e_client);
01583 $r.=HtmlInput::hidden('nb_item',$nb_item);
01584 $r.=HtmlInput::hidden('p_jrn',$p_jrn);
01585 if ( isset($period))
01586 $r.=HtmlInput::hidden('period',$period);
01587 $r.=HtmlInput::hidden('e_comm',$e_comm);
01588 $r.=HtmlInput::hidden('e_date',$e_date);
01589 $r.=HtmlInput::hidden('e_ech',$e_ech);
01590 $r.=HtmlInput::hidden('jrn_type',$jrn_type);
01591 $r.=HtmlInput::hidden('e_pj',$e_pj);
01592 $r.=HtmlInput::hidden('e_pj_suggest',$e_pj_suggest);
01593 $mt=microtime(true);
01594 $r.=HtmlInput::hidden('mt',$mt);
01595
01596 $e_mp=(isset($e_mp))?$e_mp:0;
01597 $r.=HtmlInput::hidden('e_mp',$e_mp);
01598
01599
01600
01601
01602 for ($i=0;$i < $nb_item;$i++)
01603 {
01604 $r.=HtmlInput::hidden("e_march".$i,${"e_march".$i});
01605 if (isset (${"e_march".$i."_label"})) $r.=HtmlInput::hidden("e_march".$i."_label",${"e_march".$i."_label"});
01606 $r.=HtmlInput::hidden("e_march".$i."_price",${"e_march".$i."_price"});
01607 if ( $g_parameter->MY_TVA_USE=='Y' )
01608 {
01609 $r.=HtmlInput::hidden("e_march".$i."_tva_id",${"e_march".$i."_tva_id"});
01610 $r.=HtmlInput::hidden('e_march'.$i.'_tva_amount', ${'e_march'.$i.'_tva_amount'});
01611 }
01612 $r.=HtmlInput::hidden("e_quant".$i,${"e_quant".$i});
01613
01614 }
01615
01616
01617
01618
01619 if ( $e_mp!=0 && strlen (trim (${'e_mp_qcode_'.$e_mp})) != 0 )
01620 {
01621 $r.=HtmlInput::hidden('e_mp_qcode_'.$e_mp,${'e_mp_qcode_'.$e_mp});
01622 $r.=HtmlInput::hidden('acompte',$acompte);
01623 $r.=HtmlInput::hidden('e_comm_paiement',$e_comm_paiement);
01624
01625 $r.=HtmlInput::hidden('qcode_benef', ${'e_mp_qcode_' . $e_mp});
01626 $fname = new Fiche($this->db);
01627 $fname->get_by_qcode(${'e_mp_qcode_' . $e_mp});
01628 $r.='<h2>' . _("Payé par")." " . ${'e_mp_qcode_' . $e_mp} .
01629 " " . $fname->getName() . '</h2> ' . '<p class="decale">' . _('Déduction acompte ') . h($acompte) . '</p>' .
01630 _('Libellé :') . h($e_comm_paiement) ;
01631 $r.='<br>';
01632 $r.='<br>';
01633 }
01634
01635
01636 return $r;
01637 }
01638
01639
01640
01641
01642
01643
01644
01645 public function extra_info()
01646 {
01647 $r="";
01648 $r = '<div id="facturation_div_id" style="height:185px;height:10rem">';
01649 $r.='<p class="decale">';
01650
01651 $file=new IFile();
01652 $file->table=0;
01653 $r.=_("Ajoutez une pièce justificative ");
01654 $r.=$file->input("pj","");
01655
01656 if ( $this->db->count_sql("select md_id,md_name from document_modele where md_affect='ACH'") > 0 )
01657 {
01658
01659 $r.=_('ou générer un document').' <input type="checkbox" name="gen_invoice" >';
01660
01661 $doc_gen=new ISelect();
01662 $doc_gen->name="gen_doc";
01663 $doc_gen->value=$this->db->make_array(
01664 "select md_id,md_name ".
01665 " from document_modele where md_affect='ACH'");
01666 $r.=$doc_gen->input().'<br>';
01667 }
01668 $r.='<br>';
01669 $obj=new IText();
01670 $r.=_('Numero de bon de commande : ').$obj->input('bon_comm').'<br>';
01671 $r.=_('Autre information : ').$obj->input('other_info').'<br>';
01672 $r.='</p>';
01673 $r.='</div>';
01674 return $r;
01675 }
01676
01677
01678
01679
01680
01681
01682
01683 function show_unpaid_deprecated()
01684 {
01685
01686
01687
01688 $step=$_SESSION['g_pagesize'];
01689 $page=(isset($_GET['offset']))?$_GET['page']:1;
01690 $offset=(isset($_GET['offset']))?$_GET['offset']:0;
01691
01692
01693 $sql=SQL_LIST_UNPAID_INVOICE_DATE_LIMIT." and jr_def_id=".$this->id ;
01694 list($max_line,$list)=$this->list_operation($sql,null,$offset,1);
01695 $sql=SQL_LIST_UNPAID_INVOICE." and jr_def_id=".$this->id ;
01696 list($max_line2,$list2)=$this->list_operation($sql,null,$offset,1);
01697
01698
01699 $m=($max_line2>$max_line)?$max_line2:$max_line;
01700 $bar2=navigation_bar($offset,$m,$step,$page);
01701
01702 echo $bar2;
01703 echo '<h2 class="info"> '._('Echeance dépassée').' </h2>';
01704 echo $list;
01705 echo '<h2 class="info"> '._('Non Payée').' </h2>';
01706 echo $list2;
01707 echo $bar2;
01708
01709 $hid=new IHidden();
01710
01711 echo '<hr>';
01712
01713 if ( $m != 0 )
01714 echo HtmlInput::submit('paid',_('Mise à jour paiement'));
01715
01716
01717 }
01718
01719
01720
01721
01722
01723
01724
01725 function get_detail_purchase($p_from,$p_end)
01726 {
01727 global $g_user;
01728
01729 if ( $this->id == 0 ) die (__FILE__.":".__LINE__." Journal invalide");
01730
01731
01732 if ( $g_user->get_ledger_access($this->id) == 'X' ) return null;
01733
01734
01735 $sql = "select *
01736 from v_detail_purchase
01737 where
01738 jr_def_id = $1
01739 and jr_date >= (select p_start from parm_periode where p_id = $2)
01740 and jr_date <= (select p_end from parm_periode where p_id = $3) "
01741 .' order by jr_date,substring(jr_pj_number,\'[0-9]+$\')::numeric asc ';
01742 $ret = $this->db->exec_sql($sql, array($this->id,$p_from, $p_end));
01743 return $ret;
01744 }
01745
01746
01747
01748
01749
01750 static function heading_detail_purchase()
01751 {
01752 $array['jr_id'] = _('Numéro opération');
01753 $array['jr_date'] = _('Date');
01754 $array['jr_date_paid'] = _('Date paiement');
01755 $array['jr_ech'] = _('Date échéance');
01756 $array['jr_tech_per'] = _('Période');
01757 $array['jr_comment'] = _('Libellé');
01758 $array['jr_pj_number'] = _('Pièce');
01759 $array['jr_internal'] = _('Interne');
01760 $array['jr_def_id'] = _('Code journal');
01761 $array['j_poste'] = _('Poste');
01762 $array['j_text'] = _('Commentaire');
01763 $array['j_qcode'] = _('Code Item');
01764 $array['item_card'] = _('N° fiche');
01765 $array['item_name'] = _('Nom fiche');
01766 $array['qp_supplier'] = _('N° fiche fournisseur');
01767 $array['tiers_name'] = _('Nom fournisseur');
01768 $array['quick_code'] = _('Code fournisseur');
01769 $array['tva_label'] = _('Nom TVA');
01770 $array['tva_comment'] = _('Commentaire TVA');
01771 $array['tva_both_side'] = _('TVA annulée');
01772 $array['vat_sided'] = _('TVA Non Payé');
01773 $array['vat_code'] = _('Code TVA');
01774 $array['vat'] = _('Montant TVA');
01775 $array['price'] = _('Total HTVA');
01776 $array['quantity'] = _('quantité');
01777 $array['price_per_unit'] = _('PU');
01778 $array['non_ded_amount'] = _('Montant ND');
01779 $array['non_ded_tva'] = _('Montant TVA ND');
01780 $array['non_ded_tva_recup'] = _('TVA récup.');
01781 $array['htva'] = _('HTVA Opération');
01782 $array['tot_vat'] = _('TVA Opération');
01783 $array['tot_tva_np'] = _('TVA NP opération');
01784 return $array;
01785 }
01786
01787 }
01788
01789
01790
01791
01792