00001 <?
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 require_once("constant.php");
00022 require_once("postgres.php");
00023 require_once("class_parm_code.php");
00024 require_once("class_widget.php");
00025
00026 require_once('class_fiche.php');
00027 require_once('class_poste.php');
00028 require_once('user_common.php');
00036
00037
00038 class Customer extends fiche{
00039
00040 var $poste;
00041 var $name;
00042 var $street;
00043 var $country;
00044 var $cp;
00045 var $vat_number;
00049 function Customer($p_cn,$p_id=0) {
00050 $this->fiche_def_ref=FICHE_TYPE_CLIENT;
00051 fiche::fiche($p_cn,$p_id) ;
00052
00053 }
00057 function GetFromPoste($p_poste=0) {
00058 $this->poste=($p_poste==0)?$this->poste:$p_poste;
00059 $sql="select * from vw_client where poste_comptable=".$this->poste;
00060 $Res=ExecSql($this->cn,$sql);
00061 if ( pg_NumRows($Res) == 0) return null;
00062
00063 $row=pg_fetch_array($Res,0);
00064 $this->name=$row['name'];
00065 $this->id=$row['f_id'];
00066 $this->street=$row['rue'];
00067 $this->cp=$row['code_postal'];
00068 $this->country=$row['pays'];
00069 $this->vat_number=$row['tva_num'];
00070
00071 }
00084 function VatListing($p_year) {
00085 $cond_sql=" and A.j_date = B.j_date
00086 and extract(year from A.j_date) ='$p_year'";
00087
00088
00089
00090 $s=new parm_code($this->cn,'VENTE');
00091 $s->Get();
00092 $SOLD=$s->p_value;
00093
00094 $c=new parm_code($this->cn,'CUSTOMER');
00095 $c->Get();
00096 $CUSTOMER=$c->p_value;
00097
00098 $t=new parm_code($this->cn,'COMPTE_TVA');
00099 $t->Get();
00100 $TVA=$t->p_value;
00101
00102
00103 $sql="select j_grpt
00104 from
00105 jrnx as A
00106 join jrnx as B using (j_grpt)
00107 where
00108 A.j_poste like '".$CUSTOMER."%' and
00109 B.j_poste like '".$SOLD."%'
00110 $cond_sql
00111 ";
00112
00113 $Res=ExecSql($this->cn,$sql);
00114
00115
00116
00117
00118 $a_Res=array();
00119 for ($i=0; $i < pg_NumRows($Res);$i++) {
00120
00121
00122 $row1=pg_fetch_array($Res,$i);
00123
00124
00125
00126 $Res2=ExecSql($this->cn,"select j_poste,j_montant,j_debit from jrnx where j_grpt=".$row1['j_grpt']);
00127 $a_row=array();
00128
00129
00130 for ($e=0;$e < pg_NumRows($Res2);$e++) {
00131 $a_row[]=pg_fetch_array($Res2,$e);
00132 }
00133
00134
00135
00136 foreach ($a_row as $e) {
00137 if ( substr($e['j_poste'],0, strlen($CUSTOMER))==$CUSTOMER) {
00138 $customer=$e['j_poste'];
00139
00140 $this->GetFromPoste($customer);
00141 $a_Res[$customer]['name']=$this->name;
00142 $a_Res[$customer]['vat_number']=$this->vat_number;
00143 break;
00144
00145 }
00146 }
00147
00148
00149 foreach ($a_row as $e) {
00150 $amount=0;
00151 $tva=0;
00152 if ( substr($e['j_poste'],0, strlen($SOLD))==$SOLD) {
00153 $amount=($e['j_debit']=='f')?$e['j_montant']:$e['j_montant']*-1;
00154 }
00155 if ( substr($e['j_poste'],0, strlen($TVA))==$TVA) {
00156 $tva=($e['j_debit']=='f')?$e['j_montant']:$e['j_montant']*-1;
00157 }
00158
00159
00160 $a_Res[$customer]['amount']=(isset($a_Res[$customer]['amount']))?$a_Res[$customer]['amount']:0;
00161 $a_Res[$customer]['amount']+=$amount;
00162
00163
00164
00165 $a_Res[$customer]['tva']=(isset($a_Res[$customer]['tva']))?$a_Res[$customer]['tva']:0;
00166 $a_Res[$customer]['tva']+=$tva;
00167
00168
00169
00170 $a_Res[$customer]['customer']=$customer;
00171
00172
00173
00174
00175 if (!isset($a_Res[$customer]['vat_number']) || strcmp($a_Res[$customer]['vat_number'], "") == 0)
00176 {
00177 unset($a_Res[$customer]);
00178 }
00179
00180 }
00181
00182 }
00183 return $a_Res;
00184 }
00195 function Summary($p_search)
00196 {
00197 $p_search=FormatString($p_search);
00198 $url=urlencode($_SERVER['REQUEST_URI']);
00199 $script=$_SERVER['SCRIPT_NAME'];
00200
00201
00202 $all_client=$this->CountByDef($this->fiche_def_ref,$p_search);
00203
00204 $offset=( isset ($_REQUEST['offset'] )) ?$_REQUEST['offset']:0;
00205 $page=(isset($_REQUEST['page']))?$_REQUEST['page']:1;
00206 $bar=jrn_navigation_bar($offset,$all_client,$_SESSION['g_pagesize'],$page);
00207
00208 $search="";
00209 if ( trim($p_search) != "" )
00210 {
00211 $search=" and f_id in
00212 (select f_id from jnt_fic_att_value
00213 join fiche using (f_id)
00214 join attr_value using (jft_id)
00215 where
00216 ad_id=1 and av_text ~* '$p_search')";
00217 }
00218
00219 $step_client=$this->GetAll($offset,$search);
00220 if ( $all_client == 0 ) return "";
00221 $r=$bar;
00222 $r.='<table>
00223 <TR style="background-color:lightgrey;">
00224 <TH>Quick Code</TH>
00225 <th>Nom</th>
00226 <th>Adresse</th>
00227 <th>Solde</th>
00228 <th colspan="3">Action </th>
00229 </TR>';
00230 if ( sizeof ($step_client ) == 0 )
00231 return $r;
00232 foreach ($step_client as $client ) {
00233 $r.="<TR>";
00234 $e=sprintf('<A HREF="%s?p_action=client&sa=detail&f_id=%d&url=%s" title="Détail"> ',
00235 $script,$client->id,$url);
00236
00237 $r.="<TD> $e".$client->strAttribut(ATTR_DEF_QUICKCODE)."</A></TD>";
00238 $r.="<TD>".$client->strAttribut(ATTR_DEF_NAME)."</TD>";
00239 $r.="<TD>".$client->strAttribut(ATTR_DEF_ADRESS).
00240 " ".$client->strAttribut(ATTR_DEF_CP).
00241 " ".$client->strAttribut(ATTR_DEF_PAYS).
00242 "</TD>";
00243
00244 $post=new poste($this->cn,$client->strAttribut(ATTR_DEF_ACCOUNT));
00245
00246 $User=new cl_user($this->cn);
00247 $filter_year=" j_tech_per in (select p_id from parm_periode ".
00248 "where p_exercice='".$User->getExercice()."')";
00249 $a=$post->GetSoldeDetail($filter_year);
00250 $r.=sprintf('<TD align="right"> %15.2f€</TD>',$a['solde']);
00251 $r.="<TD>";
00252
00253 $r.=sprintf('<A HREF="%s?p_action=contact&qcode=%s&url=%s" title="Contact">Contact</A></td>',
00254 $script,$client->strAttribut(ATTR_DEF_QUICKCODE),$url);
00255 $r.=sprintf('<td><A HREF="%s?p_action=suivi_courrier&sa=list&qcode=%s&url=%s" title="Action">Courrier</A></td> ',
00256 $script,$client->strAttribut(ATTR_DEF_QUICKCODE) ,$url);
00257
00258
00259
00260 $r.='<td><A HREF="commercial.php?p_action=facture&sa=list&p_periode=-1&qcode='.$client->strAttribut(ATTR_DEF_QUICKCODE).'&url='.$url.'" title="Historique Facture">Facture</A></td>';
00261
00262 $r.='</TD>';
00263
00264 $r.="</TR>";
00265
00266 }
00267 $r.="</TABLE>";
00268 $r.=$bar;
00269 return $r;
00270 }
00271
00272 }
00273
00274 ?>