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