noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_customer.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS 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  *   NOALYSS 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 NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00020 require_once("constant.php");
00021 require_once('class_database.php');
00022 require_once("class_acc_parm_code.php");
00023 require_once('class_periode.php');
00024 require_once('class_fiche.php');
00025 require_once('class_acc_account_ledger.php');
00026 require_once('user_common.php');
00027 /*! \file
00028  * \brief Derived from class fiche Customer are a specific kind of card
00029  */
00030 /*!
00031  * \brief  class  Customer are a specific kind of card
00032  */
00033 
00034 // Use the view vw_customer
00035 //
00036 class Customer extends Fiche
00037 {
00038 
00039     var $poste;      /*!<  $poste poste comptable */
00040     var $name;        /*!<  $name name of the company */
00041     var $street;      /*!<  $street Street */
00042     var $country;     /*!<  $country Country */
00043     var $cp;          /*!<  $cp Zip code */
00044     var $vat_number;  /*!<  $vat_number vat number */
00045 
00046     /*! \brief Constructor
00047     /* only a db connection is needed */
00048     function __construct($p_cn,$p_id=0)
00049     {
00050         $this->fiche_def_ref=FICHE_TYPE_CLIENT;
00051         parent::__construct($p_cn,$p_id) ;
00052 
00053     }
00054     /*! \brief  Get all info contains in the view
00055      *  thanks to the poste elt (account)
00056     */
00057     function get_by_account($p_poste=0)
00058     {
00059         $this->poste=($p_poste==0)?$this->poste:$p_poste;
00060         $sql="select * from vw_client where poste_comptable=$1";
00061         $Res=$this->cn->exec_sql($sql,array($this->poste));
00062         if ( Database::num_row($Res) == 0) return null;
00063         if ( Database::num_row($Res) > 1 ) throw new Exception ('Plusieurs fiches avec le même poste',1);
00064         // There is only _one_ row by customer
00065         $row=Database::fetch_array($Res,0);
00066         $this->name=$row['name'];
00067         $this->id=$row['f_id'];
00068         $this->street=$row['rue'];
00069         $this->cp=$row['code_postal'];
00070         $this->country=$row['pays'];
00071         $this->vat_number=$row['tva_num'];
00072 
00073     }
00074     /*!
00075      * \brief  Get all the info for making a vat listing
00076      *           for the vat administration
00077      *
00078      * \param    $p_year
00079      * 
00080      * \return  double array structure is 
00081      *            ( j_poste,name,vat_number,amount,tva,customer(object)
00082      *
00083      */
00084     function VatListing($p_year)
00085     {
00086         $cond_sql=" and   A.j_date = B.j_date and extract(year from A.j_date) ='$p_year'";
00087         /* List of customer  */
00088         $aCustomer=$this->cn->get_array('select f_id,name,quick_code,tva_num,poste_comptable from vw_client '.
00089                                         " where tva_num !='' ");
00090 
00091         /* Use the code */
00092 
00093         // BASE ACCOUNT
00094         // for belgium
00095         $s=new Acc_Parm_Code($this->cn,'VENTE');
00096         $s->load();
00097         $SOLD=$s->p_value;
00098 
00099         $c=new Acc_Parm_Code($this->cn,'CUSTOMER');
00100         $c->load();
00101         $CUSTOMER=$c->p_value;
00102 
00103         $t=new Acc_Parm_Code($this->cn,'COMPTE_TVA');
00104         $t->load();
00105         $TVA=$t->p_value;
00106 
00107         $a_Res=array();
00108         /* for each customer compute VAT, Amount...*/
00109         foreach ($aCustomer as $l )
00110         {
00111             // Seek the customer
00112             //---
00113             $customer=$l['quick_code'];
00114             $a_Res[$customer]['name']=$l['name'];
00115             $a_Res[$customer]['vat_number']=$l['tva_num'];
00116             $a_Res[$customer]['amount']=0;
00117             $a_Res[$customer]['tva']=0;
00118             $a_Res[$customer]['poste_comptable']=$l['poste_comptable'];
00119             /* retrieve only operation of sold and vat */
00120             // Get all the sell operation
00121             //----
00122             $sql="select distinct j_grpt
00123                  from
00124                  jrnx as A
00125                  join jrnx as B using (j_grpt)
00126                  where
00127                  A.j_qcode = '".$l['quick_code']."' and
00128                  B.j_poste::text like '".$SOLD."%'
00129                  $cond_sql
00130                  ";
00131 
00132             $Res=$this->cn->exec_sql($sql);
00133             // Foreach operation
00134             // where 7% or tva account are involved
00135             // and store the result in an array (a_Res)
00136             //---
00137 
00138             for ($i=0; $i < Database::num_row($Res);$i++)
00139             {
00140                 // Get each row
00141                 //---
00142                 $row1=Database::fetch_array($Res,$i);
00143 
00144 
00145                 // select the operation
00146                 //----
00147                 $Res2=$this->cn->exec_sql("select j_poste,j_montant,j_debit from jrnx where j_grpt=".$row1['j_grpt']);
00148                 $a_row=Database::fetch_all($Res2);
00149 
00150                 // Store the amount in the array
00151                 //---
00152                 foreach ($a_row as $e)
00153                 {
00154                     $amount=0;
00155                     $tva=0;
00156                     if ( substr($e['j_poste'],0, strlen($SOLD))===$SOLD)
00157                     {
00158                         $amount=($e['j_debit']=='f')?$e['j_montant']:$e['j_montant']*-1;
00159                     }
00160                     if ( substr($e['j_poste'],0, strlen($TVA))===$TVA)
00161                     {
00162                         $tva=($e['j_debit']=='f')?$e['j_montant']:$e['j_montant']*-1;
00163                     }
00164                     // store sold
00165                     //---
00166                     $a_Res[$customer]['amount']=(isset($a_Res[$customer]['amount']))?$a_Res[$customer]['amount']:0;
00167                     $a_Res[$customer]['amount']+=$amount;
00168 
00169                     // store vat
00170                     //---
00171                     $a_Res[$customer]['tva']=(isset($a_Res[$customer]['tva']))?$a_Res[$customer]['tva']:0;
00172                     $a_Res[$customer]['tva']+=$tva;
00173 
00174                     // store customef info
00175                     //---
00176                     $a_Res[$customer]['customer']=$customer;
00177                 }
00178             }// foreach $a
00179         } // foreach ( customer)
00180 
00181         return $a_Res;
00182     }
00183 
00184 
00185 }
00186 
00187 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations