2002-09-25 20:36:27 +02:00
< ? PHP
2003-02-01 20:45:45 +01:00
/* Copyright ( C ) 2002 - 2003 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2004-01-30 23:12:26 +01:00
* Copyright ( C ) 2004 Laurent Destailleur < eldy @ users . sourceforge . net >
2002-09-25 20:36:27 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*
* $Id $
* $Source $
*
*/
2003-06-27 18:12:36 +02:00
class FactureLigne
{
2003-04-13 16:47:05 +02:00
Function FactureLigne ()
{
}
}
2003-06-27 18:12:36 +02:00
class Facture
{
2002-09-25 20:36:27 +02:00
var $id ;
var $db ;
var $socidp ;
var $number ;
var $author ;
var $date ;
var $ref ;
var $amount ;
var $remise ;
var $tva ;
var $total ;
var $note ;
var $db_table ;
2002-12-14 17:25:46 +01:00
var $propalid ;
2003-05-16 22:35:47 +02:00
var $projetid ;
2003-09-16 00:12:07 +02:00
/**
* Initialisation de la class
2002-09-25 20:36:27 +02:00
*
*/
2003-06-21 17:22:27 +02:00
Function Facture ( $DB , $soc_idp = " " , $facid = " " )
{
$this -> db = $DB ;
$this -> socidp = $soc_idp ;
$this -> products = array ();
2004-02-01 02:50:21 +01:00
$this -> db_table = MAIN_DB_PREFIX . " facture " ;
2003-06-21 17:22:27 +02:00
$this -> amount = 0 ;
$this -> remise = 0 ;
2003-09-06 02:31:41 +02:00
$this -> remise_percent = 0 ;
2003-06-21 17:22:27 +02:00
$this -> tva = 0 ;
$this -> total = 0 ;
$this -> propalid = 0 ;
$this -> projetid = 0 ;
$this -> id = $facid ;
2002-09-25 20:36:27 +02:00
}
2003-09-16 00:12:07 +02:00
/**
* Cr<EFBFBD> <EFBFBD> la facture
2002-09-25 20:36:27 +02:00
*
*
*/
2003-08-13 01:04:33 +02:00
Function create ( $user )
2003-06-22 11:57:42 +02:00
{
2003-10-12 20:16:50 +02:00
/* On positionne en mode brouillon la facture */
2003-08-11 21:58:31 +02:00
$this -> brouillon = 1 ;
2003-10-12 20:16:50 +02:00
/* Facture r<> currente */
if ( $this -> fac_rec > 0 )
{
require_once DOL_DOCUMENT_ROOT . '/compta/facture/facture-rec.class.php' ;
$_facrec = new FactureRec ( $this -> db , $this -> fac_rec );
$_facrec -> fetch ( $this -> fac_rec );
$this -> projetid = $_facrec -> projetid ;
$this -> cond_reglement = $_facrec -> cond_reglement_id ;
$this -> amount = $_facrec -> amount ;
$this -> remise = $_facrec -> remise ;
$this -> remise_percent = $_facrec -> remise_percent ;
}
2004-02-01 02:50:21 +01:00
$sql = " SELECT fdm,nbjour FROM " . MAIN_DB_PREFIX . " cond_reglement WHERE rowid = $this->cond_reglement " ;
2003-07-21 15:05:24 +02:00
if ( $this -> db -> query ( $sql ) )
{
if ( $this -> db -> num_rows ())
{
$obj = $this -> db -> fetch_object ( 0 );
$cdr_nbjour = $obj -> nbjour ;
$cdr_fdm = $obj -> fdm ;
}
$this -> db -> free ();
}
$datelim = $this -> date + ( $cdr_nbjour * 3600 * 24 );
2003-06-22 11:57:42 +02:00
/*
* Insertion dans la base
*/
$socid = $this -> socidp ;
$number = $this -> number ;
$amount = $this -> amount ;
$remise = $this -> remise ;
2002-09-25 20:36:27 +02:00
2003-06-22 11:57:42 +02:00
if ( ! $remise )
{
$remise = 0 ;
}
2003-04-13 21:29:42 +02:00
2003-06-22 11:57:42 +02:00
if ( ! $this -> projetid )
{
$this -> projetid = " NULL " ;
}
$totalht = ( $amount - $remise );
$tva = tva ( $totalht );
$total = $totalht + $tva ;
2003-09-14 10:19:12 +02:00
$sql = " INSERT INTO $this->db_table (facnumber, fk_soc, datec, amount, remise, remise_percent, datef, note, fk_user_author,fk_projet, fk_cond_reglement, date_lim_reglement) " ;
$sql .= " VALUES (' $number ', $socid , now(), $totalht , $remise , $this->remise_percent , " . $this -> db -> idate ( $this -> date ) . " ,' $this->note ', $user->id , $this->projetid , $this->cond_reglement , " . $this -> db -> idate ( $datelim ) . " ) " ;
2003-06-22 11:57:42 +02:00
if ( $this -> db -> query ( $sql ) )
{
$this -> id = $this -> db -> last_insert_id ();
2003-06-24 22:55:27 +02:00
2004-02-01 02:50:21 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture SET facnumber='(PROV " . $this -> id . " )' WHERE rowid= " . $this -> id ;
2003-09-23 19:50:20 +02:00
$this -> db -> query ( $sql );
2003-06-24 22:55:27 +02:00
if ( $this -> id && $this -> propalid )
{
2004-02-01 02:50:21 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " fa_pr (fk_facture, fk_propal) VALUES ( " . $this -> id . " , " . $this -> propalid . " ) " ;
2003-06-24 22:55:27 +02:00
$this -> db -> query ( $sql );
}
2003-11-25 13:31:13 +01:00
if ( $this -> id && $this -> commandeid )
{
2004-02-01 02:50:21 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " co_fa (fk_facture, fk_commande) VALUES ( " . $this -> id . " , " . $this -> commandeid . " ) " ;
2003-11-25 13:31:13 +01:00
$this -> db -> query ( $sql );
}
2003-08-13 01:04:33 +02:00
/*
2003-09-17 19:31:32 +02:00
* Produits
2003-08-13 01:04:33 +02:00
*
*/
for ( $i = 0 ; $i < sizeof ( $this -> products ) ; $i ++ )
{
$prod = new Product ( $this -> db , $this -> products [ $i ]);
$prod -> fetch ( $this -> products [ $i ]);
2003-10-10 20:35:42 +02:00
$result_insert = $this -> addline ( $this -> id ,
$prod -> libelle ,
$prod -> price ,
$this -> products_qty [ $i ],
$prod -> tva_tx ,
$this -> products [ $i ],
$this -> products_remise_percent [ $i ]);
if ( $result_insert < 0 )
2003-08-13 01:04:33 +02:00
{
print $sql . '<br>' . $this -> db -> error () . '<br>' ;
}
}
2003-10-12 20:16:50 +02:00
/*
* Produits de la facture r<EFBFBD> currente
*
*/
if ( $this -> fac_rec > 0 )
{
for ( $i = 0 ; $i < sizeof ( $_facrec -> lignes ) ; $i ++ )
{
if ( $_facrec -> lignes [ $i ] -> produit_id )
{
$prod = new Product ( $this -> db , $_facrec -> lignes [ $i ] -> produit_id );
$prod -> fetch ( $_facrec -> lignes [ $i ] -> produit_id );
}
$result_insert = $this -> addline ( $this -> id ,
2003-11-05 11:00:08 +01:00
addslashes ( $_facrec -> lignes [ $i ] -> desc ),
2003-10-12 20:41:05 +02:00
$_facrec -> lignes [ $i ] -> subprice ,
2003-10-12 20:16:50 +02:00
$_facrec -> lignes [ $i ] -> qty ,
$_facrec -> lignes [ $i ] -> tva_taux ,
$_facrec -> lignes [ $i ] -> produit_id ,
$_facrec -> lignes [ $i ] -> remise_percent );
if ( $result_insert < 0 )
{
print $sql . '<br>' . $this -> db -> error () . '<br>' ;
}
}
}
/*
*
*
*/
2003-08-13 01:04:33 +02:00
$this -> updateprice ( $this -> id );
2003-06-22 11:57:42 +02:00
return $this -> id ;
}
else
{
print $this -> db -> error () . '<b><br>' . $sql ;
return 0 ;
}
}
2002-09-25 20:36:27 +02:00
2003-09-16 00:12:07 +02:00
/**
* Recup<EFBFBD> re l ' objet facture
2002-09-25 20:36:27 +02:00
*
*
*/
2003-09-09 18:06:22 +02:00
Function fetch ( $rowid , $societe_id = 0 )
2003-02-01 20:45:45 +01:00
{
2002-09-25 20:36:27 +02:00
2003-10-12 20:16:50 +02:00
$sql = " SELECT f.fk_soc,f.facnumber,f.amount,f.tva,f.total,f.total_ttc,f.remise,f.remise_percent, " . $this -> db -> pdate ( " f.datef " ) . " as df,f.fk_projet, " . $this -> db -> pdate ( " f.date_lim_reglement " ) . " as dlr, c.rowid as cond_regl_id, c.libelle, c.libelle_facture, f.note, f.paye, f.fk_statut, f.fk_user_author " ;
2004-02-01 02:50:21 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facture as f, " . MAIN_DB_PREFIX . " cond_reglement as c " ;
2003-07-21 15:05:24 +02:00
$sql .= " WHERE f.rowid= $rowid AND c.rowid = f.fk_cond_reglement " ;
2003-02-01 20:45:45 +01:00
2003-09-09 18:06:22 +02:00
if ( $societe_id > 0 )
{
$sql .= " AND f.fk_soc = " . $societe_id ;
}
2003-02-01 20:45:45 +01:00
if ( $this -> db -> query ( $sql ) )
{
if ( $this -> db -> num_rows ())
{
$obj = $this -> db -> fetch_object ( 0 );
2003-08-04 20:08:24 +02:00
$this -> id = $rowid ;
$this -> datep = $obj -> dp ;
$this -> date = $obj -> df ;
$this -> ref = $obj -> facnumber ;
2003-09-09 17:38:50 +02:00
$this -> amount = $obj -> amount ;
$this -> remise = $obj -> remise ;
$this -> total_ht = $obj -> total ;
2003-08-04 20:08:24 +02:00
$this -> total_tva = $obj -> tva ;
2003-09-09 17:38:50 +02:00
$this -> total_ttc = $obj -> total_ttc ;
2003-08-04 20:08:24 +02:00
$this -> paye = $obj -> paye ;
2003-09-06 02:31:41 +02:00
$this -> remise_percent = $obj -> remise_percent ;
2003-08-04 20:08:24 +02:00
$this -> socidp = $obj -> fk_soc ;
$this -> statut = $obj -> fk_statut ;
2003-07-21 15:05:24 +02:00
$this -> date_lim_reglement = $obj -> dlr ;
2003-10-12 20:16:50 +02:00
$this -> cond_reglement_id = $obj -> cond_regl_id ;
2003-08-04 20:08:24 +02:00
$this -> cond_reglement = $obj -> libelle ;
2003-09-19 15:39:05 +02:00
$this -> cond_reglement_facture = $obj -> libelle_facture ;
2003-08-04 20:08:24 +02:00
$this -> projetid = $obj -> fk_projet ;
$this -> note = stripslashes ( $obj -> note );
2003-09-09 18:06:22 +02:00
$this -> user_author = $obj -> fk_user_author ;
2003-08-04 20:08:24 +02:00
$this -> lignes = array ();
if ( $this -> statut == 0 )
{
$this -> brouillon = 1 ;
}
2003-02-01 20:45:45 +01:00
$this -> db -> free ();
2003-04-13 16:47:05 +02:00
/*
* Lignes
*/
2003-10-12 20:41:05 +02:00
$sql = " SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_taux, l.remise_percent, l.subprice " ;
2004-02-01 02:50:21 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facturedet as l WHERE l.fk_facture = " . $this -> id ;
2003-04-13 16:47:05 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$num = $this -> db -> num_rows ();
$i = 0 ; $total = 0 ;
while ( $i < $num )
{
$objp = $this -> db -> fetch_object ( $i );
$faclig = new FactureLigne ();
2003-10-12 20:16:50 +02:00
$faclig -> desc = stripslashes ( $objp -> description );
$faclig -> qty = $objp -> qty ;
$faclig -> price = $objp -> price ;
2003-10-12 20:41:05 +02:00
$faclig -> subprice = $objp -> subprice ;
2003-10-12 20:16:50 +02:00
$faclig -> tva_taux = $objp -> tva_taux ;
$faclig -> remise_percent = $objp -> remise_percent ;
$faclig -> produit_id = $objp -> fk_product ;
2003-04-13 16:47:05 +02:00
$this -> lignes [ $i ] = $faclig ;
$i ++ ;
}
$this -> db -> free ();
2003-09-09 17:38:50 +02:00
return 1 ;
2003-04-13 16:47:05 +02:00
}
else
{
print $this -> db -> error ();
2003-09-09 17:38:50 +02:00
return - 1 ;
2003-04-13 16:47:05 +02:00
}
2003-02-01 20:45:45 +01:00
}
2003-07-21 15:05:24 +02:00
else
{
print " Error " ;
2003-09-09 17:38:50 +02:00
return - 2 ;
2003-07-21 15:05:24 +02:00
}
2003-02-01 20:45:45 +01:00
}
else
{
print $this -> db -> error ();
2003-09-09 17:38:50 +02:00
return - 3 ;
2003-02-01 20:45:45 +01:00
}
}
2003-09-16 00:12:07 +02:00
/**
* Recup<EFBFBD> re l ' objet client li<EFBFBD> <EFBFBD> la facture
2003-04-13 16:47:05 +02:00
*
*/
Function fetch_client ()
{
$client = new Societe ( $this -> db );
$client -> fetch ( $this -> socidp );
$this -> client = $client ;
}
2003-09-16 00:12:07 +02:00
/**
* Valide la facture
2002-09-25 20:36:27 +02:00
*
*
*/
2003-02-01 20:45:45 +01:00
Function valid ( $userid , $dir )
{
2004-02-01 02:50:21 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture SET fk_statut = 1, date_valid=now(), fk_user_valid= $userid " ;
2003-02-01 20:45:45 +01:00
$sql .= " WHERE rowid = $this->id AND fk_statut = 0 ; " ;
2003-10-22 17:39:28 +02:00
if ( $this -> db -> query ( $sql ) )
{
return 1 ;
}
else
{
print $this -> db -> error () . ' in ' . $sql ;
}
}
/**
* Class la facture
*
*
*/
Function classin ( $cat_id )
{
2004-02-01 02:50:21 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture SET fk_projet = $cat_id " ;
2003-10-22 17:39:28 +02:00
$sql .= " WHERE rowid = $this->id ; " ;
2003-02-01 20:45:45 +01:00
if ( $this -> db -> query ( $sql ) )
{
return 1 ;
}
else
{
print $this -> db -> error () . ' in ' . $sql ;
}
2002-09-25 20:36:27 +02:00
}
2003-09-16 00:12:07 +02:00
/**
* Supprime la facture
2002-12-14 17:25:46 +01:00
*
*/
Function delete ( $rowid )
{
2004-02-01 02:50:21 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " facture_tva_sum WHERE fk_facture = $rowid ; " ;
2002-12-14 17:25:46 +01:00
if ( $this -> db -> query ( $sql ) )
{
2004-02-01 02:50:21 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " fa_pr WHERE fk_facture = $rowid ; " ;
2002-12-14 17:25:46 +01:00
2003-09-17 18:14:32 +02:00
if ( $this -> db -> query ( $sql ) )
{
2003-11-25 13:31:13 +01:00
2004-02-01 02:50:21 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " co_fa WHERE fk_facture = $rowid ; " ;
2003-09-17 18:14:32 +02:00
2002-12-14 17:25:46 +01:00
if ( $this -> db -> query ( $sql ) )
{
2004-02-01 02:50:21 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " facturedet WHERE fk_facture = $rowid ; " ;
2003-11-25 13:31:13 +01:00
2003-02-01 20:45:45 +01:00
if ( $this -> db -> query ( $sql ) )
{
2004-02-01 02:50:21 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " facture WHERE rowid = $rowid AND fk_statut = 0; " ;
2003-11-25 13:31:13 +01:00
if ( $this -> db -> query ( $sql ) )
{
return 1 ;
}
else
{
print " Err : " . $this -> db -> error ();
return - 1 ;
}
2003-02-01 20:45:45 +01:00
}
else
{
print " Err : " . $this -> db -> error ();
2003-11-25 13:31:13 +01:00
return - 2 ;
2003-02-01 20:45:45 +01:00
}
2002-12-14 17:25:46 +01:00
}
else
{
print " Err : " . $this -> db -> error ();
2003-11-25 13:31:13 +01:00
return - 3 ;
2002-12-14 17:25:46 +01:00
}
}
2003-09-17 18:14:32 +02:00
else
{
print " Err : " . $this -> db -> error ();
2003-11-25 13:31:13 +01:00
return - 4 ;
2003-09-17 18:14:32 +02:00
}
2002-12-14 17:25:46 +01:00
}
else
{
print " Err : " . $this -> db -> error ();
2003-11-25 13:31:13 +01:00
return - 5 ;
2002-12-14 17:25:46 +01:00
}
}
2003-09-16 00:12:07 +02:00
/**
* Tag la facture comme pay<EFBFBD> e
2002-12-19 19:59:23 +01:00
*
*
*/
2002-12-14 17:25:46 +01:00
Function set_payed ( $rowid )
{
2004-02-01 02:50:21 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture set paye = 1 WHERE rowid = $rowid ; " ;
2002-12-14 17:25:46 +01:00
$return = $this -> db -> query ( $sql );
}
2003-09-16 00:12:07 +02:00
/**
* Valide la facture
2003-07-07 12:03:53 +02:00
*
*/
2003-09-23 19:50:20 +02:00
Function set_valid ( $rowid , $user , $soc )
2002-12-14 17:25:46 +01:00
{
2003-08-04 20:08:24 +02:00
if ( $this -> brouillon )
{
$action_notify = 2 ; // ne pas modifier cette valeur
2003-07-10 20:10:14 +02:00
2003-09-23 19:50:20 +02:00
$numfa = facture_get_num ( $soc ); // d<> finit dans includes/modules/facture
2004-02-01 02:50:21 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture set facnumber=' $numfa ', fk_statut = 1, fk_user_valid = $user->id WHERE rowid = $rowid ; " ;
2003-08-04 20:08:24 +02:00
$result = $this -> db -> query ( $sql );
2003-07-10 20:10:14 +02:00
2004-01-30 23:12:26 +01:00
if ( ! $result ) { print " Err : " . $this -> db -> error (); return - 1 ; }
2003-08-04 20:08:24 +02:00
/*
* Notify
*
*/
$filepdf = FAC_OUTPUTDIR . " / " . $this -> ref . " / " . $this -> ref . " .pdf " ;
$mesg = " La facture " . $this -> ref . " a <20> t<EFBFBD> valid<69> e. \n " ;
$notify = New Notify ( $this -> db );
$notify -> send ( $action_notify , $this -> socidp , $mesg , " facture " , $rowid , $filepdf );
/*
* Update Stats
*
*/
2004-02-01 02:50:21 +01:00
$sql = " SELECT fk_product FROM " . MAIN_DB_PREFIX . " facturedet WHERE fk_facture = " . $this -> id ;
2003-10-12 20:16:50 +02:00
$sql .= " AND fk_product > 0 " ;
2003-08-04 20:08:24 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
2003-07-10 20:10:14 +02:00
{
2003-08-04 20:08:24 +02:00
$num = $this -> db -> num_rows ();
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $i );
2004-02-01 02:50:21 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " product SET nbvente=nbvente+1 WHERE rowid = " . $obj -> fk_product ;
2003-08-04 20:08:24 +02:00
$db2 = $this -> db -> clone ();
$result = $db2 -> query ( $sql );
$i ++ ;
}
2003-07-10 20:10:14 +02:00
}
2003-08-04 20:08:24 +02:00
/*
* Contrats
*/
$contrat = new Contrat ( $this -> db );
$contrat -> create_from_facture ( $this -> id , $user , $this -> socidp );
2003-08-03 20:04:50 +02:00
2003-08-04 20:08:24 +02:00
return $result ;
}
2003-02-01 20:45:45 +01:00
}
2003-09-16 00:12:07 +02:00
/**
* Ajoute un produit dans la facture
2003-02-01 20:45:45 +01:00
*
*/
2003-10-10 20:35:42 +02:00
Function add_product ( $idproduct , $qty , $remise_percent )
2003-08-13 01:04:33 +02:00
{
if ( $idproduct > 0 )
{
$i = sizeof ( $this -> products );
$this -> products [ $i ] = $idproduct ;
if ( ! $qty )
{
$qty = 1 ;
}
$this -> products_qty [ $i ] = $qty ;
2003-10-10 20:35:42 +02:00
$this -> products_remise_percent [ $i ] = $remise_percent ;
2003-08-13 01:04:33 +02:00
}
}
2003-09-16 00:12:07 +02:00
/**
2003-09-23 19:50:20 +02:00
* Ajoute une ligne de facture
2003-08-13 01:04:33 +02:00
*
*/
2003-10-12 20:16:50 +02:00
Function addline ( $facid , $desc , $pu , $qty , $txtva , $fk_product = 0 , $remise_percent = 0 )
2003-02-01 20:45:45 +01:00
{
2003-08-04 20:08:24 +02:00
if ( $this -> brouillon )
2003-06-27 18:12:36 +02:00
{
2003-09-16 00:12:07 +02:00
if ( strlen ( trim ( $qty )) == 0 )
{
$qty = 1 ;
}
2003-09-23 19:50:20 +02:00
$remise = 0 ;
$price = round ( ereg_replace ( " , " , " . " , $pu ), 2 );
$subprice = $price ;
if ( trim ( strlen ( $remise_percent )) > 0 )
{
$remise = round (( $pu * $remise_percent / 100 ), 2 );
$price = $pu - $remise ;
}
2003-09-16 00:12:07 +02:00
2004-02-01 02:50:21 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " facturedet (fk_facture,description,price,qty,tva_taux, fk_product, remise_percent, subprice, remise) " ;
2003-09-23 19:50:20 +02:00
$sql .= " VALUES ( $facid , ' $desc ', $price , $qty , $txtva , $fk_product , $remise_percent , $subprice , $remise ) ; " ;
2003-08-04 20:08:24 +02:00
if ( $this -> db -> query ( $sql ) )
{
$this -> updateprice ( $facid );
2003-09-16 00:12:07 +02:00
return 1 ;
}
else
{
2003-10-12 20:16:50 +02:00
print " <br> $sql <br> " ;
2003-09-16 00:12:07 +02:00
return - 1 ;
2003-08-04 20:08:24 +02:00
}
2003-06-27 18:12:36 +02:00
}
2003-02-01 20:45:45 +01:00
}
2003-09-16 00:12:07 +02:00
/**
* Mets <EFBFBD> jour une ligne de facture
2003-02-01 20:45:45 +01:00
*
*/
2003-09-23 19:50:20 +02:00
Function updateline ( $rowid , $desc , $pu , $qty , $remise_percent = 0 )
2003-02-01 22:13:19 +01:00
{
2003-08-04 20:08:24 +02:00
if ( $this -> brouillon )
{
2003-09-16 00:12:07 +02:00
if ( strlen ( trim ( $qty )) == 0 )
{
$qty = 1 ;
}
2003-09-23 19:50:20 +02:00
$remise = 0 ;
$price = round ( ereg_replace ( " , " , " . " , $pu ), 2 );
$subprice = $price ;
if ( trim ( strlen ( $remise_percent )) > 0 )
{
$remise = round (( $pu * $remise_percent / 100 ), 2 );
$price = $pu - $remise ;
}
else
{
$remise_percent = 0 ;
}
2004-02-01 02:50:21 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " facturedet set description=' $desc ',price= $price ,subprice= $subprice ,remise= $remise ,remise_percent= $remise_percent ,qty= $qty WHERE rowid = $rowid ; " ;
2003-08-04 20:08:24 +02:00
$result = $this -> db -> query ( $sql );
2003-02-01 22:13:19 +01:00
2003-08-04 20:08:24 +02:00
$this -> updateprice ( $this -> id );
}
2003-02-01 22:13:19 +01:00
}
2003-09-16 00:12:07 +02:00
/**
* Supprime une ligne
2003-02-01 22:13:19 +01:00
*
*/
2003-02-01 20:45:45 +01:00
Function deleteline ( $rowid )
{
2003-08-04 20:08:24 +02:00
if ( $this -> brouillon )
{
2004-02-01 02:50:21 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " facturedet WHERE rowid = $rowid ; " ;
2003-08-04 20:08:24 +02:00
$result = $this -> db -> query ( $sql );
2003-02-01 20:45:45 +01:00
2003-08-04 20:08:24 +02:00
$this -> updateprice ( $this -> id );
}
2003-02-01 20:45:45 +01:00
}
2003-09-16 00:12:07 +02:00
/**
2003-10-09 01:18:41 +02:00
* Mise <EFBFBD> jour des sommes de la facture
2003-02-01 20:45:45 +01:00
*
*/
Function updateprice ( $facid )
{
2003-09-17 18:14:32 +02:00
include_once DOL_DOCUMENT_ROOT . " /lib/price.lib.php " ;
2003-09-16 00:12:07 +02:00
$err = 0 ;
2004-02-01 02:50:21 +01:00
$sql = " SELECT price, qty, tva_taux FROM " . MAIN_DB_PREFIX . " facturedet WHERE fk_facture = $facid ; " ;
2003-02-01 20:45:45 +01:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
2003-07-10 19:06:26 +02:00
$num = $this -> db -> num_rows ();
$i = 0 ;
while ( $i < $num )
2003-02-01 20:45:45 +01:00
{
2003-07-10 19:06:26 +02:00
$obj = $this -> db -> fetch_object ( $i );
2003-09-17 18:14:32 +02:00
$products [ $i ][ 0 ] = $obj -> price ;
$products [ $i ][ 1 ] = $obj -> qty ;
$products [ $i ][ 2 ] = $obj -> tva_taux ;
2003-09-15 18:06:16 +02:00
2003-07-10 19:06:26 +02:00
$i ++ ;
2003-02-01 20:45:45 +01:00
}
2003-09-17 18:14:32 +02:00
$this -> db -> free ();
2003-09-15 18:06:16 +02:00
/*
2003-09-17 18:14:32 +02:00
*
2003-09-15 18:06:16 +02:00
*/
2003-09-17 18:14:32 +02:00
$calculs = calcul_price ( $products , $this -> remise_percent );
$this -> total_remise = $calculs [ 3 ];
$this -> amount_ht = $calculs [ 4 ];
$this -> total_ht = $calculs [ 0 ];
$this -> total_tva = $calculs [ 1 ];
$this -> total_ttc = $calculs [ 2 ];
$tvas = $calculs [ 5 ];
2003-09-16 00:12:07 +02:00
/*
*
*/
2004-02-01 02:50:21 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture SET amount = $this->amount_ht , remise= $this->total_remise , total= $this->total_ht , tva= $this->total_tva , total_ttc= $this->total_ttc " ;
2003-02-01 20:45:45 +01:00
$sql .= " WHERE rowid = $facid ; " ;
2003-09-09 17:38:50 +02:00
if ( $this -> db -> query ( $sql ) )
{
2003-09-16 00:12:07 +02:00
2004-02-01 02:50:21 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " facture_tva_sum WHERE fk_facture= " . $this -> id ;
2003-09-16 00:12:07 +02:00
if ( $this -> db -> query ( $sql ) )
{
2003-09-17 18:14:32 +02:00
foreach ( $tvas as $key => $value )
2003-09-16 00:12:07 +02:00
{
2004-02-01 02:50:21 +01:00
$sql = " REPLACE INTO " . MAIN_DB_PREFIX . " facture_tva_sum SET fk_facture= " . $this -> id ;
2003-09-17 18:14:32 +02:00
$sql .= " , amount = " . $tvas [ $key ];
2003-09-16 00:12:07 +02:00
$sql .= " , tva_tx= " . $key ;
if ( ! $this -> db -> query ( $sql ) )
{
print " $sql <br> " ;
$err ++ ;
}
}
}
else
{
$err ++ ;
}
if ( $err == 0 )
{
return 1 ;
}
else
{
return - 3 ;
}
2003-09-09 17:38:50 +02:00
}
else
{
print " $sql <br> " ;
return - 2 ;
}
2003-02-01 20:45:45 +01:00
}
2003-07-10 19:06:26 +02:00
else
{
print " Error " ;
2003-09-09 17:38:50 +02:00
return - 1 ;
2003-07-10 19:06:26 +02:00
}
2002-12-14 17:25:46 +01:00
}
2003-09-17 19:31:32 +02:00
/**
* Applique une remise
2003-07-24 13:05:38 +02:00
*
*/
2003-09-06 02:31:41 +02:00
Function set_remise ( $user , $remise )
{
if ( $user -> rights -> facture -> creer )
{
$this -> remise_percent = $remise ;
2004-02-01 02:50:21 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture SET remise_percent = " . ereg_replace ( " , " , " . " , $remise );
2003-09-06 02:31:41 +02:00
$sql .= " WHERE rowid = $this->id AND fk_statut = 0 ; " ;
if ( $this -> db -> query ( $sql ) )
{
$this -> updateprice ( $this -> id );
return 1 ;
}
else
{
print $this -> db -> error () . ' in ' . $sql ;
return 0 ;
}
}
}
2003-09-17 19:31:32 +02:00
/**
* Envoie une relance
2003-09-06 02:31:41 +02:00
*
*
*/
2003-07-24 13:05:38 +02:00
Function send_relance ( $destinataire , $replytoname , $replytomail , $user )
{
$soc = new Societe ( $this -> db , $this -> socidp );
$file = FAC_OUTPUTDIR . " / " . $this -> ref . " / " . $this -> ref . " .pdf " ;
if ( file_exists ( $file ))
{
2002-12-14 17:25:46 +01:00
2003-07-24 13:05:38 +02:00
$sendto = $soc -> contact_get_email ( $destinataire );
$sendtoid = $destinataire ;
if ( strlen ( $sendto ))
{
$subject = " Relance facture $this->ref " ;
$message = " Nous apportons <20> votre connaissance que la facture $this->ref n'a toujours pas <20> t<EFBFBD> r<> gl<67> e. \n \n Cordialement \n \n " ;
$filename = " $this->ref .pdf " ;
$replyto = $replytoname . " < " . $replytomail . " > " ;
$mailfile = new CMailFile ( $subject ,
$sendto ,
$replyto ,
$message ,
$file ,
" application/pdf " ,
$filename );
if ( $mailfile -> sendfile () )
{
$sendto = htmlentities ( $sendto );
2004-02-01 02:50:21 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " actioncomm (datea,fk_action,fk_soc,note,fk_facture, fk_contact,fk_user_author, label, percent) VALUES (now(), 10 , $this->socidp ,'Relance envoy<6F> e <20> $sendto ', $this->id , $sendtoid , $user->id , 'Relance Facture par mail',100); " ;
2003-07-24 13:05:38 +02:00
if ( ! $this -> db -> query ( $sql ) )
{
print $this -> db -> error ();
print " <p> $sql </p> " ;
}
}
else
{
print " <b>!! erreur d'envoi<br> $sendto <br> $replyto <br> $filename " ;
}
}
else
{
print " Can't get email $sendto " ;
}
}
}
2003-09-16 00:12:07 +02:00
/**
* Renvoie la liste des sommes de tva
*
*/
Function getSumTva ()
{
2004-02-01 02:50:21 +01:00
$sql = " SELECT amount, tva_tx FROM " . MAIN_DB_PREFIX . " facture_tva_sum WHERE fk_facture = " . $this -> id ;
2003-09-16 00:12:07 +02:00
if ( $this -> db -> query ( $sql ))
{
$num = $this -> db -> num_rows ();
$i = 0 ;
while ( $i < $num )
{
$row = $this -> db -> fetch_row ( $i );
$tvs [ $row [ 1 ]] = $row [ 0 ];
$i ++ ;
}
return $tvs ;
}
else
{
return - 1 ;
}
}
2003-10-27 13:39:21 +01:00
/**
* Renvoie la sommes des paiements
*
*/
Function getSommePaiement ()
{
2004-02-01 02:50:21 +01:00
$sql = " SELECT sum(amount) FROM " . MAIN_DB_PREFIX . " paiement WHERE fk_facture = " . $this -> id ;
2003-10-27 13:39:21 +01:00
if ( $this -> db -> query ( $sql ))
{
$row = $this -> db -> fetch_row ( 0 );
return $row [ 0 ];
}
else
{
return - 1 ;
}
}
2003-10-09 01:18:41 +02:00
/**
* RODO TODO
2002-12-14 17:25:46 +01:00
*
*/
Function pdf ()
2003-07-10 19:06:26 +02:00
{
2002-12-14 17:25:46 +01:00
}
2003-02-01 20:45:45 +01:00
}
2002-09-25 20:36:27 +02:00
?>