class_lettering.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of PhpCompta.
00004  *
00005  *   PhpCompta is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   PhpCompta is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with PhpCompta; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 /* $Revision: 4796 $ */
00020 
00021 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00022 
00023 /*!\file
00024  * \brief letter the accounting entry (row level)
00025  */
00026 require_once ('class_user.php');
00027 
00028 /**
00029  *@brief mother class for the lettering by account and by card
00030  * use the tables jnt_letter, letter_deb and letter_cred
00031  * - "account"=>"account",       => the accounting of the j_id (use by Lettering_Account)
00032  * - "quick_code"=>"quick_code", => the quick_code of the j_id (used by Lettering_Card)
00033  * - "start"=>"start",     => date of the first day
00034  * - "end"=>"end",                 => date of the last day
00035  * - "sql_ledger"=>"sql_ledger"  => the sql clause to filter on the available ledgers
00036 */
00037 class Lettering
00038 {
00039 
00040     protected $variable=array("account"=>"account", /* the accounting of the j_id (use by Lettering_Account) */
00041                               "quick_code"=>"quick_code", /* the quick_code of the j_id (used by Lettering_Card) */
00042                               "start"=>"start",         /* date of the first day */
00043                               "end"=>"end",             /* date of the last day */
00044                               "sql_ledger"=>"sql_ledger"        /*   the sql clause to filter on the available ledgers */
00045                              )
00046                         ;
00047     /**
00048      * constructor
00049      *@param $p_init resource to database
00050      *@note by default start and end are the 1.1.exercice to 31.12.exercice
00051      */
00052     function __construct ($p_init)
00053     {
00054         $this->db=$p_init;
00055         $a=new User($p_init);
00056         $exercice=$a->get_exercice();
00057         $this->start='01.01.'.$exercice;
00058         $this->end='31.12.'.$exercice;
00059         // available ledgers
00060         $this->sql_ledger=str_replace('jrn_def_id','jr_def_id',$a->get_ledger_sql('ALL',3));
00061 
00062     }
00063     public function get_parameter($p_string)
00064     {
00065         if ( array_key_exists($p_string,$this->variable) )
00066         {
00067             $idx=$this->variable[$p_string];
00068             return $this->$idx;
00069         }
00070         else
00071             throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
00072     }
00073     public function set_parameter($p_string,$p_value)
00074     {
00075         if ( array_key_exists($p_string,$this->variable) )
00076         {
00077             $idx=$this->variable[$p_string];
00078             $this->$idx=$p_value;
00079         }
00080         else
00081             throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
00082     }
00083     /**
00084      *Use to just insert a couple of lettered operation
00085      */
00086     function insert_couple($j_id1,$j_id2)
00087     {
00088 
00089         /*  take needed data */
00090         $first=$this->db->get_value('select j_debit from jrnx where j_id=$1',array($j_id1));
00091         if ( $this->db->count() == 0 ) throw new Exception ('Opération non existante');
00092 
00093         $second=$this->db->get_value('select j_debit from jrnx where j_id=$1',array($j_id2));
00094         if ( $this->db->count() == 0 ) throw new Exception ('Opération non existante');
00095                 $sql_already="select distinct(jl_id)
00096                         from jnt_letter
00097                         left outer join letter_deb using (jl_id)
00098                         left outer join letter_cred using (jl_id)
00099                         where
00100                         letter_deb.j_id = $1 or letter_cred.j_id=$1";
00101                 $let1=0;$let2=0;
00102                 $already=$this->db->get_array($sql_already,array($j_id1));
00103                 if ( count ($already ) > 0) {
00104                         if ( count($already)==1) {
00105                                 // retrieve the letter
00106                                 $let1=$this->db->get_value("select distinct(jl_id)
00107                                                                                 from jnt_letter
00108                                                                                 left outer join letter_deb using (jl_id)
00109                                                                                 left outer join letter_cred using (jl_id)
00110                                                                                 where
00111                                                                                 letter_deb.j_id = $1 or letter_cred.j_id=$1",array($j_id1));
00112                         }else
00113                         {
00114                                 return;
00115                         }
00116                 }
00117 
00118                 $already=$this->db->get_array($sql_already,array($j_id2));
00119                 if ( count ($already ) > 0) {
00120                         if ( count($already)==1) {
00121                                 // retrieve the letter
00122                                 $let2=$this->db->get_value("select distinct(jl_id)
00123                                                                                 from jnt_letter
00124                                                                                 left outer join letter_deb using (jl_id)
00125                                                                                 left outer join letter_cred using (jl_id)
00126                                                                                 where
00127                                                                                 letter_deb.j_id = $1 or letter_cred.j_id=$1",array($j_id2));
00128                         }else  {
00129                                 return;
00130                         }
00131                 }
00132                 $jl_id=0;
00133                 // already linked together
00134                 if ( $let1 != 0 && $let1 == $let2 )return;
00135 
00136                 // already linked
00137                 if ( $let1 != 0 && $let2!=0 && $let1 != $let2 )return;
00138 
00139                 // none is linked
00140                 if ( $let1 == 0 && $let2==0)
00141                 {
00142                         $jl_id=$this->db->get_next_seq("jnt_letter_jl_id_seq");
00143                         $this->db->exec_sql('insert into jnt_letter(jl_id) values($1)',
00144                                                                 array($jl_id));
00145                 }
00146                 // one is linked but not the other
00147                 if ( $let1 == 0 && $let2 != 0 ) $jl_id=$let2;
00148                 if ( $let1 != 0 && $let2 == 0 ) $jl_id=$let1;
00149 
00150                 /* insert */
00151         if ( $first == 't')
00152         {
00153             // save into letter_deb
00154             if ($let1 == 0) $ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id',array($j_id1,$jl_id));
00155         }
00156         else
00157         {
00158             if ($let1 == 0)$lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id)  values($1,$2) returning lc_id',array($j_id1,$jl_id));
00159         }
00160         if ( $second == 't')
00161         {
00162             // save into letter_deb
00163             if ($let2 == 0)$ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id',array($j_id2,$jl_id));
00164         }
00165         else
00166         {
00167             if ($let2 == 0)$lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id)  values($1,$2) returning lc_id',array($j_id2,$jl_id));
00168         }
00169 
00170     }
00171     public function get_info()
00172     {
00173         return var_export(self::$variable,true);
00174     }
00175     public function verify()
00176     {
00177         // Verify that the elt we want to add is correct
00178     }
00179     /**
00180      *@brief save from array
00181      *@param $p_array
00182     @code
00183     'gDossier' => string '13' (length=2)
00184     'letter_j_id' =>
00185         ck => array
00186     @endcode
00187     */
00188     public function save($p_array)
00189     {
00190         if ( ! isset ($p_array['letter_j_id'])) return;
00191         $this->db->exec_sql('delete from jnt_letter where jl_id=$1',array($p_array['jnt_id']));
00192 
00193         $this->db->start();
00194         $jl_id=$this->db->get_next_seq("jnt_letter_jl_id_seq");
00195         $this->db->exec_sql('insert into jnt_letter(jl_id) values($1)',
00196                             array($jl_id));
00197 
00198         // save the source
00199         $deb=$this->db->get_value('select j_debit,j_montant from jrnx where j_id=$1',array($p_array['j_id']));
00200         if ( $deb == 't')
00201         {
00202             // save into letter_deb
00203             $ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id',array($p_array['j_id'],$jl_id));
00204         }
00205         else
00206         {
00207             $lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id)  values($1,$2) returning lc_id',array($p_array['j_id'],$jl_id));
00208         }
00209         $count=0;
00210         // save dest
00211         for($i=0;$i<count($p_array['letter_j_id']);$i++)
00212         {
00213             if (isset ($p_array['ck'][$i]) && $p_array['ck'][$i] !="-2")
00214             { //if 1
00215                 // save the dest
00216                 $deb=$this->db->get_value('select j_debit,j_montant from jrnx where j_id=$1',array($p_array['ck'][$i]));
00217                 if ( $deb == 't')
00218                 {
00219                     $count++;
00220                     // save into letter_deb
00221                     $ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id',array($p_array['ck'][$i],$jl_id));
00222                 }
00223                 else
00224                 {
00225                     $count++;
00226                     $lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id)  values($1,$2) returning lc_id',array($p_array['ck'][$i],$jl_id));
00227                 }
00228             } //end if 1
00229         } //end for
00230         // save into jnt_letter
00231         /* if only one row we delete the joint */
00232         if ( $count==0)
00233         {
00234             $this->db->rollback();
00235         }
00236         $this->db->commit();
00237     }
00238     /**
00239      *@brief retrieve * row thanks a condition
00240      */
00241     public function seek($cond,$p_array=null)
00242     {
00243         /*
00244           $sql="select * from * where $cond";
00245           return $this->cn->get_array($cond,$p_array)
00246         */
00247     }
00248     public function insert()
00249     {
00250         if ( $this->verify() != 0 ) return;
00251 
00252     }
00253     /**
00254      *show all the record from jrnx and their status (linked or not)
00255      *it fills the array $this->content
00256      */
00257     protected function show_all()
00258     {
00259         $this->get_all();
00260         $r="";
00261         ob_start();
00262         include('template/letter_all.php');
00263         $r=ob_get_contents();
00264         ob_clean();
00265         return $r;
00266     }
00267         function get_linked($p_jlid)
00268         {
00269                 $sql="select j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,
00270              j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,
00271              coalesce(comptaproc.get_letter_jnt(j_id),-1) as letter
00272              from jrnx join jrn on (j_grpt = jr_grpt_id)
00273                          where
00274                          j_id in (select j_id from letter_cred where jl_id=$1
00275                                         union all
00276                                         select j_id from letter_deb where jl_id=$1)
00277                                         order by j_date";
00278 
00279                 $this->linked=$this->db->get_array($sql,array($p_jlid));
00280         }
00281     /**
00282      *show only the lettered records from jrnx
00283      *it fills the array $this->content
00284      */
00285     protected function show_lettered()
00286     {
00287         $this->get_letter();
00288         $r="";
00289         ob_start();
00290         include('template/letter_all.php');
00291         $r=ob_get_contents();
00292         ob_clean();
00293         return $r;
00294     }
00295         /**
00296      *show only the lettered records from jrnx
00297      *it fills the array $this->content
00298      */
00299     protected function show_lettered_diff()
00300     {
00301         $this->get_letter_diff();
00302         $r="";
00303         ob_start();
00304         include('template/letter_all.php');
00305         $r=ob_get_contents();
00306         ob_clean();
00307         return $r;
00308     }
00309 
00310     /**
00311      *show only the not lettered records from jrnx
00312      *it fills the array $this->content
00313      */
00314 
00315     protected function show_not_lettered()
00316     {
00317         $this->get_unletter();
00318         $r="";
00319         ob_start();
00320         include('template/letter_all.php');
00321         $r=ob_get_contents();
00322         ob_clean();
00323         return $r;
00324     }
00325     /**
00326      *wrapper : it call show_all, show_lettered or show_not_lettered depending
00327      * of the parameter
00328      *@param $p_type poss. values are all, unletter, letter
00329      */
00330     public function show_list($p_type)
00331     {
00332         switch($p_type)
00333         {
00334         case 'all':
00335                 return $this->show_all();
00336             break;
00337         case 'unletter':
00338             return $this->show_not_lettered();
00339             break;
00340         case 'letter':
00341             return $this->show_lettered();
00342             break;
00343                 case 'letter_diff':
00344                         return $this->show_lettered_diff();
00345                         break;
00346         }
00347         throw new Exception ("[$p_type] is no unknown");
00348     }
00349 
00350     public function show_letter($p_jid)
00351     {
00352         $j_debit=$this->db->get_value('select j_Debit from jrnx where j_id=$1',array($p_jid));
00353         $amount_init=$this->db->get_value('select j_montant from jrnx where j_id=$1',array($p_jid));
00354 
00355         $this->get_filter($p_jid);
00356         // retrieve jnt_letter.id
00357         $sql="select distinct(jl_id) from jnt_letter  left outer join letter_deb using (jl_id) left outer join letter_cred using (jl_id)
00358              where letter_deb.j_id = $1 or letter_cred.j_id=$2";
00359         $a_jnt_id=$this->db->get_array($sql,array($p_jid,$p_jid));
00360 
00361         if (count($a_jnt_id)==0 )
00362                 {
00363                         $jnt_id=-2;
00364                 } else
00365                 {
00366                         $jnt_id=$a_jnt_id[0]['jl_id'];
00367                 }
00368                 $this->get_linked($jnt_id);
00369         ob_start();
00370         require_once('template/letter_prop.php');
00371         $r=ob_get_contents();
00372         ob_clean();
00373         $r.=HtmlInput::hidden('j_id',$p_jid);
00374         $r.=HtmlInput::hidden('jnt_id',$jnt_id);
00375 
00376         return $r;
00377     }
00378 
00379     public function update()
00380     {
00381         if ( $this->verify() != 0 ) return;
00382     }
00383 
00384     public function load()
00385 {}
00386 
00387     public function delete()
00388     {
00389         throw new Exception ('delete not implemented');
00390     }
00391     /**
00392      * Unit test for the class
00393      */
00394     static function test_me()
00395     {}
00396 
00397 }
00398 /**
00399  * only for operation retrieved thanks a account (jrnx.j_poste)
00400  * manage the accounting entries for a given account
00401  */
00402 
00403 class Lettering_Account extends Lettering
00404 {
00405     function __construct($p_init,$p_account=null)
00406     {
00407         parent::__construct($p_init);
00408         $this->account=$p_account;
00409         $this->object_type='account';
00410     }
00411 
00412     /**
00413      * fills the this->content, datas are filtered thanks
00414      * - fil_deb poss values t (debit), f(credit), ' ' (everything)
00415      * - fil_amount_max max amount
00416      * - fil_amount_min min amount
00417      * - $this->start min date
00418      * - $this->end max date
00419      * - this->account: accounting
00420      */
00421     public function get_filter($p_jid=0)
00422     {
00423         $filter_deb='';
00424         if (isset($this->fil_deb))
00425         {
00426             switch ($this->fil_deb)
00427             {
00428             case 0:
00429                 $filter_deb=" and j_debit='t' ";
00430                 break;
00431             case 1:
00432                 $filter_deb=" and j_debit='f' ";
00433                 break;
00434             case 2:
00435                 $filter_deb=" ";
00436                 break;
00437             }
00438 
00439         }
00440         $filter_amount="";
00441         if ( isset ($this->fil_amount_max ) &&
00442                 isset ($this->fil_amount_min ) &&
00443                 isNumber($this->fil_amount_max)==1 &&
00444                 isNumber($this->fil_amount_min)==1 &&
00445                 ($this->fil_amount_max != 0 || $this->fil_amount_min != 0) )
00446             $filter_amount=" and (j_montant >= $this->fil_amount_min and j_montant<=$this->fil_amount_max  or (coalesce(comptaproc.get_letter_jnt($p_jid),-1)= coalesce(comptaproc.get_letter_jnt(j_id),-1) and coalesce(comptaproc.get_letter_jnt($p_jid),-1) <> -1 )) ";
00447         $sql="
00448              select j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,
00449              j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,
00450              coalesce(comptaproc.get_letter_jnt(j_id),-1) as letter
00451              from jrnx join jrn on (j_grpt = jr_grpt_id)
00452              where j_poste = $1 and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY')
00453              and $this->sql_ledger
00454              $filter_deb
00455              $filter_amount
00456              order by j_date,j_id";
00457 
00458         $this->content=$this->db->get_array($sql,array($this->account,$this->start,$this->end));
00459     }
00460 
00461     /**
00462      * fills this->content with all the operation for the this->account(jrnx.j_poste)
00463      */
00464     public function get_all()
00465     {
00466         $sql=" with let_diff as (select jl_id,deb_amount-cred_amount as diff_letter1
00467                         from
00468                         ( select jl_id,coalesce(sum(j_montant),0) as cred_amount from letter_cred join jrnx using (j_id) group by jl_id) as CRED
00469                         left join (select jl_id,coalesce(sum(j_montant),0) as deb_amount from letter_deb join jrnx using (j_id) group by jl_id) as DEB using (jl_id)) ,
00470                         letter_jl as (select jl_id,j_id from letter_cred union all select jl_id,j_id from letter_deb)
00471                         select j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,jr_pj_number,
00472                                                 j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,
00473                                                         coalesce(let_diff.jl_id,-1) as letter,
00474                                         diff_letter1 as letter_diff
00475                                                 from jrnx join jrn on (j_grpt = jr_grpt_id)
00476                                                 left join letter_jl using (j_id)
00477                                                 left join let_diff using (jl_id)
00478              where j_poste = $1 and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY')
00479              and $this->sql_ledger
00480 
00481              order by j_date,j_id";
00482         $this->content=$this->db->get_array($sql,array($this->account,$this->start,$this->end));
00483     }
00484     /**
00485      * same as get_all but only for lettered operation
00486      */
00487     public function get_letter()
00488     {
00489         $sql="
00490                         with let_diff as (select jl_id,deb_amount-cred_amount as diff_letter1
00491                         from
00492                         ( select jl_id,coalesce(sum(j_montant),0) as cred_amount from letter_cred join jrnx using (j_id) group by jl_id) as CRED
00493                         left join (select jl_id,coalesce(sum(j_montant),0) as deb_amount from letter_deb join jrnx using (j_id) group by jl_id) as DEB using (jl_id)) ,
00494                         letter_jl as (select jl_id,j_id from letter_cred union all select jl_id,j_id from letter_deb)
00495                         select j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,jr_pj_number,
00496                                                 j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,
00497                                                 let_diff.jl_id as letter,
00498                                         diff_letter1 as letter_diff
00499                                                 from jrnx join jrn on (j_grpt = jr_grpt_id)
00500                                                  join letter_jl using (j_id)
00501                                                 left join let_diff using (jl_id)
00502                                         where j_poste = $1 and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY')
00503              and $this->sql_ledger
00504              order by j_date,j_id";
00505         $this->content=$this->db->get_array($sql,array($this->account,$this->start,$this->end));
00506     }
00507          /**
00508      * same as get_all but only for lettered operation
00509      */
00510     public function get_letter_diff()
00511     {
00512         $sql="
00513             with let_diff as (select jl_id,deb_amount-cred_amount as diff_letter1
00514                         from
00515                         ( select jl_id,coalesce(sum(j_montant),0) as cred_amount from letter_cred join jrnx using (j_id) group by jl_id) as CRED
00516                         left join (select jl_id,coalesce(sum(j_montant),0) as deb_amount from letter_deb join jrnx using (j_id) group by jl_id) as DEB using (jl_id)) ,
00517                         letter_jl as (select jl_id,j_id from letter_cred union all select jl_id,j_id from letter_deb)
00518                         select  distinct j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,jr_pj_number,
00519                                                 j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,
00520                                                 let_diff.jl_id as letter,
00521                                         diff_letter1 as letter_diff
00522                                                 from
00523                                                 jrnx join jrn on (j_grpt = jr_grpt_id)
00524                                                  join letter_jl using (j_id)
00525                                                 join let_diff using (jl_id)
00526              where j_poste = $1 and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY')
00527              and $this->sql_ledger
00528                          and diff_letter1 <> 0
00529              order by j_date,j_id";
00530         $this->content=$this->db->get_array($sql,array($this->account,$this->start,$this->end));
00531     }
00532     /**
00533      * same as get_all but only for unlettered operation
00534      */
00535 
00536     public function get_unletter()
00537     {
00538         $sql="
00539                         with letter_jl as (select jl_id,j_id from letter_cred union all select jl_id,j_id from letter_deb)
00540                         select j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,jr_pj_number,
00541                                                 j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,
00542                                                 -1 as letter,
00543                                         0 as letter_diff
00544                                                 from jrnx join jrn on (j_grpt = jr_grpt_id)
00545              where j_poste = $1 and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY')
00546              and $this->sql_ledger
00547              and j_id not in (select j_id from letter_jl)
00548              order by j_date,j_id";
00549         $this->content=$this->db->get_array($sql,array($this->account,$this->start,$this->end));
00550     }
00551 
00552 }
00553 /**
00554  * only for operation retrieved thanks a quick_code
00555  * manage the accounting entries for a given card
00556  */
00557 class Lettering_Card extends Lettering
00558 {
00559     /**
00560      *constructor
00561      *@param $p_init db resource
00562      *@param $p_qcode quick_code of the jrnx.j_id
00563      */
00564     function __construct($p_init,$p_qcode=null)
00565     {
00566         parent::__construct($p_init);
00567         $this->quick_code=$p_qcode;
00568         $this->object_type='card';
00569     }
00570     /**
00571      * fills the this->content, datas are filtered thanks
00572      * - fil_deb poss values t (debit), f(credit), ' ' (everything)
00573      * - fil_amount_max max amount
00574      * - fil_amount_min min amount
00575      * - $this->start min date
00576      * - $this->end max date
00577      * - this->quick_code: quick_code
00578      */
00579     public function get_filter($p_jid=0)
00580     {
00581         $filter_deb='';
00582         if (isset($this->fil_deb))
00583         {
00584             switch ($this->fil_deb)
00585             {
00586             case 0:
00587                 $filter_deb=" and j_debit='t' ";
00588                 break;
00589             case 1:
00590                 $filter_deb=" and j_debit='f' ";
00591                 break;
00592             case 2:
00593                 $filter_deb=" ";
00594                 break;
00595             }
00596 
00597         }
00598         $filter_amount="";
00599         if ( isset ($this->fil_amount_max ) &&
00600                 isset ($this->fil_amount_min ) &&
00601                 isNumber($this->fil_amount_max)==1 &&
00602                 isNumber($this->fil_amount_min)==1 &&
00603                 ($this->fil_amount_max != 0 || $this->fil_amount_min != 0) )
00604           $filter_amount=" and (j_montant between $this->fil_amount_min and $this->fil_amount_max or (coalesce(comptaproc.get_letter_jnt($p_jid),-1)= coalesce(comptaproc.get_letter_jnt(j_id),-1) and coalesce(comptaproc.get_letter_jnt($p_jid),-1) <> -1 )) ";
00605         $sql="
00606             with let_diff as (select jl_id,deb_amount-cred_amount as diff_letter1
00607                         from
00608                         ( select jl_id,coalesce(sum(j_montant),0) as cred_amount from letter_cred join jrnx using (j_id) group by jl_id) as CRED
00609                         left join (select jl_id,coalesce(sum(j_montant),0) as deb_amount from letter_deb join jrnx using (j_id) group by jl_id) as DEB using (jl_id)) ,
00610                         letter_jl as (select jl_id,j_id from letter_cred union all select jl_id,j_id from letter_deb)
00611                         select distinct j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,jr_pj_number,
00612                                                 j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,
00613                                                 coalesce(let_diff.jl_id,-1) as letter,
00614                                         diff_letter1 as letter_diff
00615                                                 from jrnx join jrn on (j_grpt = jr_grpt_id)
00616                                                 left join letter_jl using (j_id)
00617                                                 left join let_diff using (jl_id)
00618              where j_qcode = upper($1) and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY')
00619              and $this->sql_ledger
00620              $filter_deb
00621              $filter_amount
00622              order by j_date,j_id";
00623 
00624         $this->content=$this->db->get_array($sql,array($this->quick_code,$this->start,$this->end));
00625     }
00626     /**
00627      * fills this->content with all the operation for the this->quick_code(j_qcode)
00628      */
00629     public function get_all()
00630     {
00631         $sql="
00632        with let_diff as (select jl_id,deb_amount-cred_amount as diff_letter1
00633                         from
00634                         ( select jl_id,coalesce(sum(j_montant),0) as cred_amount from letter_cred join jrnx using (j_id) group by jl_id) as CRED
00635                         left join (select jl_id,coalesce(sum(j_montant),0) as deb_amount from letter_deb join jrnx using (j_id) group by jl_id) as DEB using (jl_id)) ,
00636                         letter_jl as (select jl_id,j_id from letter_cred union all select jl_id,j_id from letter_deb)
00637                         select DISTINCT j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,jr_pj_number,
00638                                                 j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,
00639                                                 coalesce(let_diff.jl_id,-1) as letter,
00640                                         diff_letter1 as letter_diff
00641                                                 from jrnx join jrn on (j_grpt = jr_grpt_id)
00642                                                 left join letter_jl using (j_id)
00643                                                 left join let_diff using (jl_id)
00644              where j_qcode = upper($1) and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY')
00645              and $this->sql_ledger
00646 
00647              order by j_date,j_id";
00648         $this->content=$this->db->get_array($sql,array($this->quick_code,$this->start,$this->end));
00649     }
00650     /**
00651      * same as get_all but only for lettered operation
00652      */
00653 
00654     public function get_letter()
00655     {
00656         $sql="
00657     with let_diff as (select jl_id,deb_amount-cred_amount as diff_letter1
00658                         from
00659                         ( select jl_id,coalesce(sum(j_montant),0) as cred_amount from letter_cred join jrnx using (j_id) group by jl_id) as CRED
00660                         left join (select jl_id,coalesce(sum(j_montant),0) as deb_amount from letter_deb join jrnx using (j_id) group by jl_id) as DEB using (jl_id)) ,
00661                         letter_jl as (select jl_id,j_id from letter_cred union all select jl_id,j_id from letter_deb)
00662                         select j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,jr_pj_number,
00663                                                 j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,
00664                                                 let_diff.jl_id as letter,
00665                                         diff_letter1 as letter_diff
00666                                                 from jrnx join jrn on (j_grpt = jr_grpt_id)
00667                                                 join letter_jl using (j_id)
00668                                                 left join let_diff using (jl_id)
00669              where j_qcode = upper($1) and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY')
00670              and $this->sql_ledger
00671              order by j_date,j_id";
00672         $this->content=$this->db->get_array($sql,array($this->quick_code,$this->start,$this->end));
00673     }
00674             public function get_letter_diff()
00675     {
00676         $sql="
00677    with let_diff as (select jl_id,deb_amount-cred_amount as diff_letter1
00678                         from
00679                         ( select jl_id,coalesce(sum(j_montant),0) as cred_amount from letter_cred join jrnx using (j_id) group by jl_id) as CRED
00680                         left join (select jl_id,coalesce(sum(j_montant),0) as deb_amount from letter_deb join jrnx using (j_id) group by jl_id) as DEB using (jl_id)) ,
00681                         letter_jl as (select jl_id,j_id from letter_cred union all select jl_id,j_id from letter_deb)
00682                         select distinct j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,jr_pj_number,
00683                                                 j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,
00684                                                 let_diff.jl_id as letter,
00685                                         diff_letter1 as letter_diff
00686                                                 from jrnx join jrn on (j_grpt = jr_grpt_id)
00687                                                 left join letter_jl using (j_id)
00688                                                 left join let_diff using (jl_id)
00689              where j_qcode = upper($1) and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY')
00690              and $this->sql_ledger
00691                          and diff_letter1 <>0
00692              order by j_date,j_id";
00693         $this->content=$this->db->get_array($sql,array($this->quick_code,$this->start,$this->end));
00694     }
00695     /**
00696      * same as get_all but only for unlettered operation
00697      */
00698     public function get_unletter()
00699     {
00700         $sql="
00701              select j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,jr_pj_number,
00702              j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,
00703              -1 as letter,
00704                          0 as letter_diff
00705              from jrnx join jrn on (j_grpt = jr_grpt_id)
00706              where j_qcode = upper($1) and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY')
00707              and $this->sql_ledger
00708              and j_id not in (select j_id from letter_deb join jnt_letter using (jl_id) union select j_id from letter_cred join jnt_letter using (jl_id) )
00709              order by j_date,j_id";
00710         $this->content=$this->db->get_array($sql,array($this->quick_code,$this->start,$this->end));
00711     }
00712 
00713 }