2006-02-09 13:25:34 +01:00
< ? php
/* Copyright ( C ) 2002 - 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2006-03-14 22:49:07 +01:00
* Copyright ( C ) 2004 - 2006 Laurent Destailleur < eldy @ users . sourceforge . net >
2006-02-09 13:25:34 +01:00
* Copyright ( C ) 2004 Sebastien Di Cintio < sdicintio @ ressource - toi . org >
* Copyright ( C ) 2004 Benoit Mortier < benoit . mortier @ opensides . be >
* Copyright ( C ) 2005 Marc Barilley / Ocebo < marc @ ocebo . com >
* Copyright ( C ) 2006 Andre Cianfarani < acianfa @ free . fr >
*
* 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 $
*/
/**
\file htdocs / facture . class . php
\ingroup facture
\brief Fichier de la classe des factures clients
\version $Revision $
*/
2006-06-18 16:18:41 +02:00
require_once ( DOL_DOCUMENT_ROOT . " /commonobject.class.php " );
2006-02-09 13:25:34 +01:00
require_once ( DOL_DOCUMENT_ROOT . " /product.class.php " );
2006-06-03 14:38:57 +02:00
require_once ( DOL_DOCUMENT_ROOT . " /client.class.php " );
2006-02-09 13:25:34 +01:00
/**
\class Facture
\brief Classe permettant la gestion des factures clients
*/
2006-06-18 16:18:41 +02:00
class Facture extends CommonObject
2006-02-09 13:25:34 +01:00
{
var $db ;
2006-06-18 16:18:41 +02:00
var $element = 'facture' ;
2006-06-03 14:38:57 +02:00
var $id ;
var $socidp ; // Id client
var $client ; // Objet societe client (<28> charger par fetch_client)
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
var $number ;
var $author ;
var $date ;
var $ref ;
2006-06-20 14:25:19 +02:00
var $ref_client ;
2006-02-09 13:25:34 +01:00
var $amount ;
var $remise ;
var $tva ;
var $total ;
var $note ;
2006-02-12 16:54:21 +01:00
var $note_public ;
2006-07-15 20:29:46 +02:00
var $statut ;
var $paye ; // 1 si facture pay<61> e COMPLETEMENT, 0 sinon
2006-02-09 13:25:34 +01:00
var $propalid ;
var $projetid ;
2006-05-21 22:23:10 +02:00
var $date_lim_reglement ;
2006-02-09 13:25:34 +01:00
var $cond_reglement_id ;
var $cond_reglement_code ;
var $mode_reglement_id ;
var $mode_reglement_code ;
2006-06-27 13:40:14 +02:00
var $modelpdf ;
2006-02-09 13:25:34 +01:00
2006-06-23 21:40:28 +02:00
// Pour board
var $nbtodo ;
var $nbtodolate ;
2006-06-13 00:20:24 +02:00
var $specimen ;
2006-06-23 21:40:28 +02:00
var $error ;
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
/**
* \brief Constructeur de la classe
2006-06-19 22:35:22 +02:00
* \param DB handler acc<EFBFBD> s base de donn<EFBFBD> es
* \param socidp id societe ( '' par defaut )
* \param facid id facture ( '' par defaut )
2006-02-09 13:25:34 +01:00
*/
2006-06-19 22:35:22 +02:00
function Facture ( $DB , $socidp = '' , $facid = '' )
2006-02-09 13:25:34 +01:00
{
$this -> db = $DB ;
$this -> id = $facid ;
2006-06-19 22:35:22 +02:00
$this -> socidp = $socidp ;
2006-02-09 13:25:34 +01:00
$this -> amount = 0 ;
$this -> remise = 0 ;
$this -> remise_percent = 0 ;
$this -> tva = 0 ;
$this -> total = 0 ;
$this -> propalid = 0 ;
$this -> projetid = 0 ;
$this -> remise_exceptionnelle = 0 ;
$this -> products = array (); // Tableau de lignes de factures
}
/**
* \brief Cr<EFBFBD> ation de la facture en base
* \param user object utilisateur qui cr<EFBFBD> e
*/
function create ( $user )
{
2006-06-16 02:33:04 +02:00
global $langs , $conf , $mysoc ;
2006-02-12 16:54:21 +01:00
// Nettoyage param<61> tres
$this -> note = trim ( $this -> note );
$this -> note_public = trim ( $this -> note_public );
2006-06-20 11:56:54 +02:00
$this -> ref_client = trim ( $this -> ref_client );
2006-02-12 16:54:21 +01:00
if ( ! $this -> remise ) $this -> remise = 0 ;
if ( ! $this -> mode_reglement_id ) $this -> mode_reglement_id = 0 ;
2006-02-09 13:25:34 +01:00
2006-02-12 16:54:21 +01:00
// On positionne en mode brouillon la facture
$this -> brouillon = 1 ;
2006-02-17 00:26:14 +01:00
2006-02-12 16:54:21 +01:00
dolibarr_syslog ( " Facture::create " );
2006-02-17 00:26:14 +01:00
2006-06-17 01:55:20 +02:00
$soc = new Societe ( $this -> db );
$soc -> fetch ( $this -> socidp );
$this -> db -> begin ();
2006-02-12 16:54:21 +01:00
// Facture r<> currente
2006-02-09 13:25:34 +01:00
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 -> cond_reglement_id = $_facrec -> cond_reglement_id ;
$this -> mode_reglement = $_facrec -> mode_reglement_id ;
$this -> mode_reglement_id = $_facrec -> mode_reglement_id ;
$this -> amount = $_facrec -> amount ;
2006-04-04 02:12:45 +02:00
$this -> remise_absolue = $_facrec -> remise_absolue ;
2006-02-09 13:25:34 +01:00
$this -> remise_percent = $_facrec -> remise_percent ;
2006-06-20 11:56:54 +02:00
$this -> remise = $_facrec -> remise ;
2006-02-09 13:25:34 +01:00
}
// Definition de la date limite
$datelim = $this -> calculate_date_lim_reglement ();
2006-02-12 16:54:21 +01:00
// Insertion dans la base
2006-02-17 00:26:14 +01:00
$socid = $this -> socidp ;
2006-02-09 13:25:34 +01:00
$number = $this -> number ;
$amount = $this -> amount ;
$remise = $this -> remise ;
$totalht = ( $amount - $remise );
2006-02-12 16:54:21 +01:00
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'facture (' ;
2006-04-04 02:12:45 +02:00
$sql .= ' facnumber, fk_soc, datec, amount, remise_absolue, remise_percent,' ;
2006-02-12 16:54:21 +01:00
$sql .= ' datef,' ;
$sql .= ' note,' ;
$sql .= ' note_public,' ;
2006-07-01 01:38:12 +02:00
$sql .= ' ref_client,' ;
2006-02-12 16:54:21 +01:00
$sql .= ' fk_user_author, fk_projet,' ;
2006-06-27 13:40:14 +02:00
$sql .= ' fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf) ' ;
2006-02-12 16:54:21 +01:00
$sql .= " VALUES ( " ;
2006-04-04 02:12:45 +02:00
$sql .= " ' $number ',' $socid ', now(), ' $totalht ', ' " . $this -> remise_absolue . " ' " ;
$sql .= " ,' " . $this -> remise_percent . " ', " . $this -> db -> idate ( $this -> date );
2006-02-12 16:54:21 +01:00
$sql .= " , " . ( $this -> note ? " ' " . addslashes ( $this -> note ) . " ' " : " null " );
$sql .= " , " . ( $this -> note_public ? " ' " . addslashes ( $this -> note_public ) . " ' " : " null " );
2006-06-20 11:56:54 +02:00
$sql .= " , " . ( $this -> ref_client ? " ' " . addslashes ( $this -> ref_client ) . " ' " : " null " );
2006-02-12 16:54:21 +01:00
$sql .= " , " . $user -> id ;
$sql .= " , " . ( $this -> projetid ? $this -> projetid : " null " );
$sql .= ',' . $this -> cond_reglement_id ;
$sql .= " , " . $this -> mode_reglement_id ;
2006-06-27 13:40:14 +02:00
$sql .= " , " . $this -> db -> idate ( $datelim ) . " , ' " . $this -> modelpdf . " ') " ;
2006-02-12 16:54:21 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2006-02-09 13:25:34 +01:00
{
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . 'facture' );
$sql = 'UPDATE ' . MAIN_DB_PREFIX . " facture SET facnumber='(PROV " . $this -> id . " )' WHERE rowid= " . $this -> id ;
2006-02-12 16:54:21 +01:00
$resql = $this -> db -> query ( $sql );
2006-02-09 13:25:34 +01:00
2006-02-12 16:54:21 +01:00
if ( $resql && $this -> id && $this -> propalid )
2006-02-09 13:25:34 +01:00
{
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'fa_pr (fk_facture, fk_propal) VALUES (' . $this -> id . ',' . $this -> propalid . ')' ;
2006-02-12 16:54:21 +01:00
$resql = $this -> db -> query ( $sql );
2006-02-09 13:25:34 +01:00
}
2006-02-12 16:54:21 +01:00
if ( $resql && $this -> id && $this -> commandeid )
2006-02-09 13:25:34 +01:00
{
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'co_fa (fk_facture, fk_commande) VALUES (' . $this -> id . ',' . $this -> commandeid . ')' ;
2006-02-12 16:54:21 +01:00
$resql = $this -> db -> query ( $sql );
2006-02-09 13:25:34 +01:00
}
/*
2006-02-12 16:54:21 +01:00
* Produits / services
*/
2006-02-09 13:25:34 +01:00
for ( $i = 0 ; $i < sizeof ( $this -> products ) ; $i ++ )
{
$prod = new Product ( $this -> db , $this -> products [ $i ]);
2006-02-12 16:54:21 +01:00
$res = $prod -> fetch ( $this -> products [ $i ]);
2006-06-17 01:55:20 +02:00
2006-06-16 02:33:04 +02:00
$tva_tx = get_default_tva ( $mysoc , $soc , $prod -> tva_tx );
2006-02-09 13:25:34 +01:00
// multiprix
if ( $conf -> global -> PRODUIT_MULTIPRICES == 1 )
$price = $prod -> multiprices [ $soc -> price_level ];
else
$price = $prod -> price ;
2006-02-12 16:54:21 +01:00
$resql = $this -> addline (
$this -> id ,
2006-02-12 15:11:55 +01:00
$prod -> description ,
2006-02-09 13:25:34 +01:00
$price ,
$this -> products_qty [ $i ],
2006-02-21 14:27:07 +01:00
$tva_tx ,
2006-02-09 13:25:34 +01:00
$this -> products [ $i ],
$this -> products_remise_percent [ $i ],
$this -> products_date_start [ $i ],
$this -> products_date_end [ $i ]
2006-02-12 16:54:21 +01:00
);
2006-02-09 13:25:34 +01:00
2006-02-12 16:54:21 +01:00
if ( $resql < 0 )
2006-02-09 13:25:34 +01:00
{
2006-02-12 16:54:21 +01:00
$this -> error = $this -> db -> error ;
2006-02-09 13:25:34 +01:00
dolibarr_print_error ( $this -> db );
2006-02-12 16:54:21 +01:00
break ;
2006-02-09 13:25:34 +01:00
}
}
/*
2006-02-12 16:54:21 +01:00
* Produits de la facture r<EFBFBD> currente
*/
if ( $resql && $this -> fac_rec > 0 )
2006-02-09 13:25:34 +01:00
{
for ( $i = 0 ; $i < sizeof ( $_facrec -> lignes ) ; $i ++ )
{
if ( $_facrec -> lignes [ $i ] -> produit_id )
{
$prod = new Product ( $this -> db , $_facrec -> lignes [ $i ] -> produit_id );
2006-06-16 02:33:04 +02:00
$res = $prod -> fetch ( $_facrec -> lignes [ $i ] -> produit_id );
2006-02-09 13:25:34 +01:00
}
2006-06-16 02:33:04 +02:00
$tva_tx = get_default_tva ( $mysoc , $soc , $prod -> tva_tx );
2006-02-09 13:25:34 +01:00
2006-02-12 16:54:21 +01:00
$result_insert = $this -> addline (
$this -> id ,
$_facrec -> lignes [ $i ] -> desc ,
$_facrec -> lignes [ $i ] -> subprice ,
$_facrec -> lignes [ $i ] -> qty ,
2006-02-21 14:27:07 +01:00
$tva_tx ,
2006-02-12 16:54:21 +01:00
$_facrec -> lignes [ $i ] -> produit_id ,
$_facrec -> lignes [ $i ] -> remise_percent );
2006-02-09 13:25:34 +01:00
if ( $result_insert < 0 )
{
dolibarr_print_error ( $this -> db );
}
}
}
if ( $resql )
{
2006-06-17 01:55:20 +02:00
$resql = $this -> update_price ( $this -> id );
2006-02-12 16:54:21 +01:00
if ( $resql )
{
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'BILL_CREATE' , $this , $user , $langs , $conf );
// Fin appel triggers
2006-06-13 00:20:24 +02:00
2006-02-12 16:54:21 +01:00
$this -> db -> commit ();
return $this -> id ;
}
else
{
$this -> db -> rollback ();
return - 3 ;
}
}
2006-02-09 13:25:34 +01:00
else
{
$this -> db -> rollback ();
return - 2 ;
}
}
else
{
2006-02-12 16:54:21 +01:00
$this -> error = $this -> db -> error ();
dolibarr_syslog ( " Facture::create error " . $this -> error . " sql= " . $sql );
2006-02-09 13:25:34 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
/**
* \brief Recup<EFBFBD> re l ' objet facture et ses lignes de factures
* \param rowid id de la facture a r<EFBFBD> cup<EFBFBD> rer
* \param societe_id id de societe
* \return int 1 si ok , < 0 si erreur
*/
function fetch ( $rowid , $societe_id = 0 )
{
//dolibarr_syslog("Facture::Fetch rowid : $rowid, societe_id : $societe_id");
2006-04-04 02:12:45 +02:00
$sql = 'SELECT f.fk_soc,f.facnumber,f.amount,f.tva,f.total,f.total_ttc,f.remise_percent,f.remise_absolue,f.remise' ;
2006-02-09 13:25:34 +01:00
$sql .= ',' . $this -> db -> pdate ( 'f.datef' ) . ' as df, f.fk_projet' ;
$sql .= ',' . $this -> db -> pdate ( 'f.date_lim_reglement' ) . ' as dlr' ;
2006-06-02 18:54:06 +02:00
$sql .= ', f.note, f.note_public, f.paye, f.fk_statut, f.fk_user_author, f.model_pdf' ;
2006-02-09 13:25:34 +01:00
$sql .= ', f.fk_mode_reglement, f.ref_client, p.code as mode_reglement_code, p.libelle as mode_reglement_libelle' ;
2006-07-13 17:16:42 +02:00
$sql .= ', f.fk_cond_reglement, c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_facture' ;
2006-06-07 18:25:23 +02:00
$sql .= ', cf.fk_commande' ;
2006-02-09 13:25:34 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'cond_reglement as c, ' . MAIN_DB_PREFIX . 'facture as f' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_paiement as p ON f.fk_mode_reglement = p.id' ;
2006-06-07 18:25:23 +02:00
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'co_fa as cf ON cf.fk_facture = f.rowid' ;
2006-02-09 13:25:34 +01:00
$sql .= ' WHERE f.rowid=' . $rowid . ' AND c.rowid = f.fk_cond_reglement' ;
if ( $societe_id > 0 )
{
$sql .= ' AND f.fk_soc = ' . $societe_id ;
}
$result = $this -> db -> query ( $sql );
if ( $result )
{
if ( $this -> db -> num_rows ( $result ))
{
$obj = $this -> db -> fetch_object ( $result );
//print strftime('%Y%m%d%H%M%S',$obj->df).' '.$obj->df.' '.dolibarr_print_date($obj->df);
$this -> id = $rowid ;
$this -> datep = $obj -> dp ;
$this -> date = $obj -> df ;
$this -> ref = $obj -> facnumber ;
$this -> ref_client = $obj -> ref_client ;
$this -> amount = $obj -> amount ;
2006-04-04 02:12:45 +02:00
$this -> remise_percent = $obj -> remise_percent ;
$this -> remise_absolue = $obj -> remise_absolue ;
2006-02-09 13:25:34 +01:00
$this -> remise = $obj -> remise ;
$this -> total_ht = $obj -> total ;
$this -> total_tva = $obj -> tva ;
$this -> total_ttc = $obj -> total_ttc ;
$this -> paye = $obj -> paye ;
$this -> socidp = $obj -> fk_soc ;
$this -> statut = $obj -> fk_statut ;
$this -> date_lim_reglement = $obj -> dlr ;
$this -> mode_reglement_id = $obj -> fk_mode_reglement ;
$this -> mode_reglement_code = $obj -> mode_reglement_code ;
$this -> mode_reglement = $obj -> mode_reglement_libelle ;
$this -> cond_reglement_id = $obj -> fk_cond_reglement ;
2006-07-13 17:16:42 +02:00
$this -> cond_reglement_code = $obj -> cond_reglement_code ;
2006-02-09 13:25:34 +01:00
$this -> cond_reglement = $obj -> cond_reglement_libelle ;
$this -> cond_reglement_facture = $obj -> cond_reglement_libelle_facture ;
$this -> projetid = $obj -> fk_projet ;
2006-02-12 16:54:21 +01:00
$this -> note = $obj -> note ;
$this -> note_public = $obj -> note_public ;
2006-02-09 13:25:34 +01:00
$this -> user_author = $obj -> fk_user_author ;
2006-06-02 18:54:06 +02:00
$this -> modelpdf = $obj -> model_pdf ;
2006-06-07 18:25:23 +02:00
$this -> commande_id = $obj -> fk_commande ;
2006-02-09 13:25:34 +01:00
$this -> lignes = array ();
2006-06-13 00:20:24 +02:00
2006-06-07 15:54:48 +02:00
if ( $this -> user_author )
{
$sql = " SELECT name, firstname " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user " ;
$sql .= " WHERE rowid = " . $this -> user_author ;
2006-06-13 00:20:24 +02:00
2006-06-07 15:54:48 +02:00
$resqluser = $this -> db -> query ( $sql );
2006-06-13 00:20:24 +02:00
2006-06-07 15:54:48 +02:00
if ( $resqluser )
{
$obju = $this -> db -> fetch_object ( $resqluser );
$this -> user_author_name = $obju -> name ;
$this -> user_author_firstname = $obju -> firstname ;
}
}
2006-06-13 00:20:24 +02:00
2006-06-07 18:25:23 +02:00
if ( $this -> commande_id )
{
$sql = " SELECT ref " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " commande " ;
$sql .= " WHERE rowid = " . $this -> commande_id ;
2006-06-13 00:20:24 +02:00
2006-06-07 18:25:23 +02:00
$resqlcomm = $this -> db -> query ( $sql );
2006-06-13 00:20:24 +02:00
2006-06-07 18:25:23 +02:00
if ( $resqlcomm )
{
$objc = $this -> db -> fetch_object ( $resqlcomm );
$this -> commande_ref = $objc -> ref ;
}
}
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
if ( $this -> statut == 0 )
{
$this -> brouillon = 1 ;
}
/*
* Lignes
*/
2006-06-15 02:50:42 +02:00
$sql = 'SELECT l.rowid, l.fk_product, l.description, l.price, l.qty, l.tva_taux, l.remise, l.remise_percent, l.subprice,' ;
$sql .= ' ' . $this -> db -> pdate ( 'l.date_start' ) . ' as date_start,' . $this -> db -> pdate ( 'l.date_end' ) . ' as date_end,' ;
2006-07-13 09:46:46 +02:00
$sql .= ' l.info_bits, l.total_ht, l.total_tva, l.total_ttc, l.fk_code_ventilation, l.fk_export_compta,' ;
2006-02-09 13:25:34 +01:00
$sql .= ' p.label as label, p.description as product_desc' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'facturedet as l' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON l.fk_product = p.rowid' ;
$sql .= ' WHERE l.fk_facture = ' . $this -> id ;
$sql .= ' ORDER BY l.rang' ;
$result2 = $this -> db -> query ( $sql );
if ( $result2 )
{
$num = $this -> db -> num_rows ( $result2 );
$i = 0 ; $total = 0 ;
while ( $i < $num )
{
$objp = $this -> db -> fetch_object ( $result2 );
$faclig = new FactureLigne ( $this -> db );
2006-07-13 09:46:46 +02:00
$faclig -> rowid = $objp -> rowid ;
$faclig -> desc = $objp -> description ; // Description ligne
$faclig -> libelle = $objp -> label ; // Label produit
$faclig -> product_desc = $objp -> product_desc ; // Description produit
$faclig -> qty = $objp -> qty ;
$faclig -> price = $objp -> price ;
$faclig -> subprice = $objp -> subprice ;
$faclig -> tva_taux = $objp -> tva_taux ;
$faclig -> remise = $objp -> remise ;
$faclig -> remise_percent = $objp -> remise_percent ;
$faclig -> produit_id = $objp -> fk_product ;
$faclig -> date_start = $objp -> date_start ;
$faclig -> date_end = $objp -> date_end ;
$faclig -> date_start = $objp -> date_start ;
$faclig -> date_end = $objp -> date_end ;
$faclig -> info_bits = $objp -> info_bits ;
$faclig -> total_ht = $objp -> total_ht ;
$faclig -> total_tva = $objp -> total_tva ;
$faclig -> total_ttc = $objp -> total_ttc ;
$faclig -> export_compta = $objp -> fk_export_compta ;
$faclig -> code_ventilation = $objp -> fk_code_ventilation ;
2006-02-09 13:25:34 +01:00
$this -> lignes [ $i ] = $faclig ;
$i ++ ;
}
$this -> db -> free ( $result2 );
$this -> db -> free ( $result );
return 1 ;
}
else
{
dolibarr_syslog ( 'Erreur Facture::Fetch rowid=' . $rowid . ', Erreur dans fetch des lignes' );
$this -> error = $this -> db -> error ();
return - 3 ;
}
}
else
{
dolibarr_syslog ( 'Erreur Facture::Fetch rowid=' . $rowid . ' numrows=0 sql=' . $sql );
$this -> error = 'Bill with id ' . $rowid . ' not found sql=' . $sql ;
return - 2 ;
}
$this -> db -> free ( $result );
}
else
{
dolibarr_syslog ( 'Erreur Facture::Fetch rowid=' . $rowid . ' Erreur dans fetch de la facture' );
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2006-04-04 02:12:45 +02:00
2006-07-01 01:38:12 +02:00
/**
* \brief Ajout d ' une ligne remise fixe dans la facture , en base
* \param idremise Id de la remise fixe
* \return int > 0 si ok , < 0 si ko
*/
function insert_discount ( $idremise )
{
global $langs ;
include_once ( DOL_DOCUMENT_ROOT . '/lib/price.lib.php' );
include_once ( DOL_DOCUMENT_ROOT . '/discount.class.php' );
$this -> db -> begin ();
$remise = new DiscountAbsolute ( $this -> db );
$result = $remise -> fetch ( $idremise );
if ( $result > 0 )
{
$facligne = new FactureLigne ( $this -> db );
$facligne -> fk_facture = $this -> id ;
$facligne -> fk_remise_except = $remise -> id ;
$facligne -> desc = $remise -> description ; // Description ligne
$facligne -> tva_tx = $remise -> tva_tx ;
$facligne -> subprice =- $remise -> amount_ht ;
$facligne -> price =- $remise -> amount_ht ;
$facligne -> fk_product = 0 ; // Id produit pr<70> d<EFBFBD> fini
$facligne -> qty = 1 ;
$facligne -> remise = 0 ;
$facligne -> remise_percent = 0 ;
$facligne -> rang =- 1 ;
$facligne -> info_bits = 2 ;
$tabprice = calcul_price_total ( $facligne -> qty , $facligne -> subprice , 0 , $facligne -> tva_tx );
$facligne -> total_ht = $tabprice [ 0 ];
$facligne -> total_tva = $tabprice [ 1 ];
$facligne -> total_ttc = $tabprice [ 2 ];
$result = $facligne -> insert ();
if ( $result > 0 )
{
$result = $this -> update_price ( $this -> id );
if ( $result > 0 )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
return - 1 ;
}
}
else
{
$this -> error = $facligne -> error ;
$this -> db -> rollback ();
return - 2 ;
}
}
else
{
$this -> db -> rollback ();
return - 2 ;
}
}
2006-02-09 13:25:34 +01:00
/**
* \brief Classe la facture dans un projet
* \param projid Id du projet dans lequel classer la facture
*/
function classin ( $projid )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture' ;
if ( $projid ) $sql .= ' SET fk_projet = ' . $projid ;
else $sql .= ' SET fk_projet = NULL' ;
$sql .= ' WHERE rowid = ' . $this -> id ;
if ( $this -> db -> query ( $sql ))
{
return 1 ;
}
else
{
dolibarr_print_error ( $this -> db );
return - 1 ;
}
}
function set_ref_client ( $ref_client )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture' ;
if ( empty ( $ref_client ))
$sql .= ' SET ref_client = NULL' ;
else
2006-06-20 11:56:54 +02:00
$sql .= ' SET ref_client = \'' . addslashes ( $ref_client ) . '\'' ;
2006-02-09 13:25:34 +01:00
$sql .= ' WHERE rowid = ' . $this -> id ;
if ( $this -> db -> query ( $sql ))
{
$this -> ref_client = $ref_client ;
return 1 ;
}
else
{
dolibarr_print_error ( $this -> db );
return - 1 ;
}
}
/**
2006-04-04 02:12:45 +02:00
* \brief Supprime la facture
* \param rowid id de la facture <EFBFBD> supprimer
*/
2006-02-09 13:25:34 +01:00
function delete ( $rowid )
{
global $user , $langs , $conf ;
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
$this -> db -> begin ();
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'facture_tva_sum WHERE fk_facture = ' . $rowid ;
if ( $this -> db -> query ( $sql ) )
{
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'fa_pr WHERE fk_facture = ' . $rowid ;
if ( $this -> db -> query ( $sql ) )
{
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'co_fa WHERE fk_facture = ' . $rowid ;
if ( $this -> db -> query ( $sql ) )
{
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'facturedet WHERE fk_facture = ' . $rowid ;
if ( $this -> db -> query ( $sql ) )
{
2006-04-04 02:12:45 +02:00
// On d<> saffecte de la facture les remises li<6C> es
2006-02-09 13:25:34 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'societe_remise_except' ;
2006-04-04 02:12:45 +02:00
$sql .= ' SET fk_facture = NULL WHERE fk_facture = ' . $rowid ;
2006-02-09 13:25:34 +01:00
if ( $this -> db -> query ( $sql ) )
{
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'facture WHERE rowid = ' . $rowid . ' AND fk_statut = 0' ;
$resql = $this -> db -> query ( $sql ) ;
if ( $resql )
{
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'BILL_DELETE' , $this , $user , $langs , $conf );
// Fin appel triggers
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
return - 6 ;
}
}
else
{
$this -> db -> rollback ();
return - 5 ;
}
}
else
{
$this -> db -> rollback ();
return - 4 ;
}
}
else
{
$this -> db -> rollback ();
return - 3 ;
}
}
else
{
$this -> db -> rollback ();
return - 2 ;
}
}
else
{
$this -> db -> rollback ();
return - 1 ;
}
}
/**
* \brief Renvoi une date limite de reglement de facture en fonction des
* conditions de reglements de la facture et date de facturation
* \param cond_reglement_id Condition de reglement <EFBFBD> utiliser , 0 = Condition actuelle de la facture
* \return date Date limite de r<EFBFBD> glement si ok , < 0 si ko
*/
function calculate_date_lim_reglement ( $cond_reglement_id = 0 )
{
if ( ! $cond_reglement_id )
$cond_reglement_id = $this -> cond_reglement_id ;
2006-04-20 20:57:38 +02:00
$sqltemp = 'SELECT c.fdm,c.nbjour,c.decalage' ;
2006-02-09 13:25:34 +01:00
$sqltemp .= ' FROM ' . MAIN_DB_PREFIX . 'cond_reglement as c' ;
$sqltemp .= ' WHERE c.rowid=' . $cond_reglement_id ;
$resqltemp = $this -> db -> query ( $sqltemp );
if ( $resqltemp )
{
if ( $this -> db -> num_rows ( $resqltemp ))
{
$obj = $this -> db -> fetch_object ( $resqltemp );
$cdr_nbjour = $obj -> nbjour ;
$cdr_fdm = $obj -> fdm ;
2006-04-20 20:57:38 +02:00
$cdr_decalage = $obj -> decalage ;
2006-02-09 13:25:34 +01:00
}
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
$this -> db -> free ( $resqltemp );
2006-06-13 00:20:24 +02:00
2006-04-20 20:57:38 +02:00
/* Definition de la date limite */
2006-06-13 00:20:24 +02:00
2006-04-20 20:57:38 +02:00
// 1 : ajout du nombre de jours
2006-02-09 13:25:34 +01:00
$datelim = $this -> date + ( $cdr_nbjour * 3600 * 24 );
2006-06-13 00:20:24 +02:00
2006-04-20 20:57:38 +02:00
// 2 : application de la r<> gle "fin de mois"
2006-02-09 13:25:34 +01:00
if ( $cdr_fdm )
{
$mois = date ( 'm' , $datelim );
$annee = date ( 'Y' , $datelim );
2006-04-20 20:57:38 +02:00
if ( $mois == 12 )
{
$mois = 1 ;
$annee += 1 ;
}
else
{
$mois += 1 ;
}
// On se d<> place au d<> but du mois suivant, et on retire un jour
$datelim = mktime ( 12 , 0 , 0 , $mois , 1 , $annee );
$datelim -= ( 3600 * 24 );
2006-02-09 13:25:34 +01:00
}
2006-06-13 00:20:24 +02:00
2006-04-20 20:57:38 +02:00
// 3 : application du d<> calage
$datelim += ( $cdr_decalage * 3600 * 24 );
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
return $datelim ;
}
/**
* \brief Tag la facture comme pay<EFBFBD> e compl<EFBFBD> tement + appel trigger BILL_PAYED
2006-06-13 00:20:24 +02:00
* \param user Objet utilisateur qui modifie
2006-02-09 13:25:34 +01:00
* \return int < 0 si ok , > 0 si ok
*/
function set_payed ( $user )
{
global $conf , $langs ;
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
dolibarr_syslog ( " Facture.class.php::set_payed rowid= " . $this -> id );
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture' ;
$sql .= ' SET paye=1 WHERE rowid = ' . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> use_webcal = ( $conf -> global -> PHPWEBCALENDAR_BILLSTATUS == 'always' ? 1 : 0 );
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'BILL_PAYED' , $this , $user , $langs , $conf );
// Fin appel triggers
}
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
return 1 ;
}
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
/**
* \brief Tag la facture comme non pay<EFBFBD> e compl<EFBFBD> tement + appel trigger BILL_UNPAYED
2006-06-13 00:20:24 +02:00
* \param user Objet utilisateur qui modifie
2006-02-09 13:25:34 +01:00
* \return int < 0 si ok , > 0 si ok
*/
function set_unpayed ( $user )
{
global $conf , $langs ;
dolibarr_syslog ( " Facture.class.php::set_unpayed rowid= " . $this -> id );
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture' ;
$sql .= ' SET paye=0 WHERE rowid = ' . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> use_webcal = ( $conf -> global -> PHPWEBCALENDAR_BILLSTATUS == 'always' ? 1 : 0 );
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'BILL_UNPAYED' , $this , $user , $langs , $conf );
// Fin appel triggers
}
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
return 1 ;
}
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
/**
* \brief Tag la facture comme payer partiellement
* \param rowid id de la facture <EFBFBD> modifier
*/
function set_paiement_started ( $rowid )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture set fk_statut=2 WHERE rowid = ' . $rowid ;
$return = $this -> db -> query ( $sql );
}
/**
* \brief Tag la facture comme abandonn<EFBFBD> e + appel trigger BILL_CANCEL
2006-06-13 00:20:24 +02:00
* \param user Objet utilisateur qui modifie
2006-02-09 13:25:34 +01:00
* \return int < 0 si ok , > 0 si ok
*/
function set_canceled ( $user )
{
global $conf , $langs ;
dolibarr_syslog ( " Facture.class.php::set_canceled rowid= " . $this -> id );
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture' ;
$sql .= ' SET fk_statut=3 WHERE rowid = ' . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> use_webcal = ( $conf -> global -> PHPWEBCALENDAR_BILLSTATUS == 'always' ? 1 : 0 );
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'BILL_CANCEL' , $this , $user , $langs , $conf );
// Fin appel triggers
}
return 1 ;
}
/**
2006-04-04 02:12:45 +02:00
* \brief Tag la facture comme valid<EFBFBD> e + appel trigger BILL_VALIDATE
* \param rowid Id de la facture <EFBFBD> valider
* \param user Utilisateur qui valide la facture
* \param soc Objet societe
* \param force_number R<EFBFBD> f<EFBFBD> rence <EFBFBD> forcer de la facture
* \return int < 0 si ko , > 0 si ok
2006-02-09 13:25:34 +01:00
*/
function set_valid ( $rowid , $user , $soc , $force_number = '' )
{
global $conf , $langs ;
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
$error = 0 ;
if ( $this -> brouillon )
{
2006-04-04 02:12:45 +02:00
$this -> db -> begin ();
2006-07-11 08:42:44 +02:00
// on v<> rifie si la facture est en num<75> rotation provisoire
$facref = substr ( $this -> ref , 1 , 4 );
2006-02-09 13:25:34 +01:00
if ( $force_number )
{
2006-02-24 20:43:06 +01:00
$numfa = $force_number ;
2006-02-09 13:25:34 +01:00
}
2006-08-05 18:44:43 +02:00
else if ( $facref == 'PROV' )
2006-02-09 13:25:34 +01:00
{
$numfa = $this -> getNextNumRef ( $soc );
}
2006-07-11 08:42:44 +02:00
else
{
$numfa = $this -> ref ;
}
2006-02-09 13:25:34 +01:00
2006-06-17 01:55:20 +02:00
$this -> update_price ( $this -> id );
2006-02-09 13:25:34 +01:00
2006-04-04 02:12:45 +02:00
// Validation de la facture
2006-02-09 13:25:34 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture ' ;
$sql .= " SET facnumber=' " . $numfa . " ', fk_statut = 1, fk_user_valid = " . $user -> id ;
if ( $conf -> global -> FAC_FORCE_DATE_VALIDATION )
{
2006-04-04 02:12:45 +02:00
// Si l'option est activ<69> e, on force la date de facture
2006-02-09 13:25:34 +01:00
$this -> date = time ();
$datelim = $this -> calculate_date_lim_reglement ();
2006-04-04 02:12:45 +02:00
$sql .= ', datef=' . $this -> db -> idate ( $this -> date );
$sql .= ', date_lim_reglement=' . $this -> db -> idate ( $datelim );
2006-02-09 13:25:34 +01:00
}
2006-04-04 02:12:45 +02:00
$sql .= ' WHERE rowid = ' . $rowid ;
$resql = $this -> db -> query ( $sql );
2006-02-09 13:25:34 +01:00
if ( $resql )
{
$this -> facnumber = $numfa ;
2006-06-08 00:18:30 +02:00
dolibarr_syslog ( " Facture::set_valid() sql= $sql " );
2006-02-09 13:25:34 +01:00
}
else
{
2006-06-08 00:18:30 +02:00
dolibarr_syslog ( " Facture::set_valid() Echec update - 10 - sql= $sql " );
2006-02-09 13:25:34 +01:00
dolibarr_print_error ( $this -> db );
$error ++ ;
}
2006-04-04 02:12:45 +02:00
2006-06-09 18:56:27 +02:00
2006-07-11 08:42:44 +02:00
// On v<> rifie si la facture <20> tait une provisoire
2006-08-05 18:44:43 +02:00
if ( $facref == 'PROV' )
2006-06-08 00:18:30 +02:00
{
2006-07-11 08:42:44 +02:00
// On renomme repertoire facture ($this->ref = ancienne ref, $numfa = nouvelle ref)
// afin de ne pas perdre les fichiers attach<63> s
$facref = sanitize_string ( $this -> ref );
$snumfa = sanitize_string ( $numfa );
$dirsource = $conf -> facture -> dir_output . '/' . $facref ;
$dirdest = $conf -> facture -> dir_output . '/' . $snumfa ;
if ( file_exists ( $dirsource ))
{
dolibarr_syslog ( " Facture::set_valid() renommage rep " . $dirsource . " en " . $dirdest );
if ( rename ( $dirsource , $dirdest ))
{
dolibarr_syslog ( " Renommage ok " );
// Suppression ancien fichier PDF dans nouveau rep
dol_delete_file ( $conf -> facture -> dir_output . '/' . $snumfa . '/' . $facref . '.*' );
}
}
2006-06-08 00:18:30 +02:00
}
2006-06-09 19:05:18 +02:00
2006-06-08 00:18:30 +02:00
2006-04-04 02:12:45 +02:00
/*
2006-06-08 00:18:30 +02:00
* Tope les lignes de remises fixes avec id des lignes de facture au montant n<EFBFBD> gatif
2006-04-04 02:12:45 +02:00
*/
2006-06-08 00:18:30 +02:00
/* TODO Toper les lignes de remises fixes avec id des lignes de facture au montant n<EFBFBD> gatif .
while ( $i < $nurmx && $remise_a_decrementee && ! $error )
2006-04-04 02:12:45 +02:00
{
2006-06-08 00:18:30 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$avoir = $obj -> amount ;
2006-06-13 00:20:24 +02:00
2006-06-08 00:18:30 +02:00
// On met <20> jour avoir comme affect<63> <20> facture
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'societe_remise_except' ;
$sql .= ' SET fk_facture = ' . $this -> id . ',' ;
$sql .= " amount_ht = ' " . price2num ( min ( $remise_a_decrementee , $avoir )) . " ' " ;
$sql .= ' WHERE rowid =' . $obj -> rowid ;
dolibarr_syslog ( " Societe::set_valid Mise a jour avoir sql= $sql " );
if ( ! $this -> db -> query ( $sql ))
2006-04-04 02:12:45 +02:00
{
2006-06-08 00:18:30 +02:00
$error ++ ;
}
if ( $remise_a_decrementee < $avoir )
{
// L'avoir n'a pas <20> t<EFBFBD> compl<70> tement consomm<6D> e, on ins<6E> re ligne du reste
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'societe_remise_except' ;
$sql .= ' (fk_soc, datec, amount_ht, fk_user, fk_facture, description) ' ;
$sql .= ' VALUES ' ;
$sql .= ' (' . $this -> socidp ;
$sql .= ' ,' . $obj -> datec ;
$sql .= " ,' " . price2num ( $avoir - $remise_a_decrementee ) . " ' " ;
$sql .= ' ,' . $user -> id ;
$sql .= ' ,null' ;
$sql .= " ,' " . addslashes ( $obj -> description ) . " ' " ;
$sql .= ')' ;
if ( ! $this -> db -> query ( $sql ))
2006-04-04 02:12:45 +02:00
{
2006-06-08 00:18:30 +02:00
$error ++ ;
2006-04-04 02:12:45 +02:00
}
}
2006-06-08 00:18:30 +02:00
$remise_a_decrementee -= min ( $remise_a_decrementee , $avoir );
$i ++ ;
2006-04-04 02:12:45 +02:00
}
2006-06-08 00:18:30 +02:00
*/
2006-06-13 00:20:24 +02:00
2006-08-05 18:44:43 +02:00
// On v<> rifie si la facture <20> tait une provisoire
if ( $facref == 'PROV' )
2006-07-11 08:42:44 +02:00
{
2006-08-05 18:44:43 +02:00
/*
* Pour chaque produit , on met a jour indicateur nbvente
* On cr<EFBFBD> e ici une d<EFBFBD> normalisation des donn<EFBFBD> es pas forc<EFBFBD> ment utilis<EFBFBD> e .
*/
$sql = 'SELECT fk_product FROM ' . MAIN_DB_PREFIX . 'facturedet' ;
$sql .= ' WHERE fk_facture = ' . $this -> id ;
$sql .= ' AND fk_product > 0' ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'product SET nbvente=nbvente+1 WHERE rowid = ' . $obj -> fk_product ;
$resql2 = $this -> db -> query ( $sql );
$i ++ ;
}
}
else
{
$error ++ ;
}
}
2006-02-09 13:25:34 +01:00
if ( $error == 0 )
{
2006-08-06 19:54:27 +02:00
// Classe la soci<63> t<EFBFBD> rattach<63> e comme client
$soc = new Societe ( $this -> db );
$soc -> id = $this -> socidp ;
$result = $soc -> set_as_client ();
2006-02-24 20:43:06 +01:00
$this -> ref = $numfa ;
2006-06-13 00:20:24 +02:00
2006-08-05 19:24:44 +02:00
$this -> use_webcal = ( $conf -> global -> PHPWEBCALENDAR_BILLSTATUS == 'always' ? 1 : 0 );
2006-02-09 13:25:34 +01:00
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'BILL_VALIDATE' , $this , $user , $langs , $conf );
// Fin appel triggers
2006-04-04 02:12:45 +02:00
$this -> db -> commit ();
2006-02-09 13:25:34 +01:00
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
2006-04-04 02:12:45 +02:00
$this -> db -> rollback ();
2006-02-09 13:25:34 +01:00
return - 1 ;
}
}
2006-07-11 08:42:44 +02:00
}
/**
*
*
*/
function reopen ( $userid )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture SET fk_statut = 0 " ;
$sql .= " WHERE rowid = $this->id ; " ;
if ( $this -> db -> query ( $sql ) )
{
return 1 ;
}
else
{
dolibarr_print_error ( $this -> db );
}
2006-02-09 13:25:34 +01:00
}
2006-06-13 00:20:24 +02:00
2006-07-13 16:31:41 +02:00
/**
* \brief Positionne modele derniere generation
* \param user Objet use qui modifie
* \param modelpdf Nom du modele
*/
function set_pdf_model ( $user , $modelpdf )
{
if ( $user -> rights -> facture -> creer )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture SET model_pdf = ' $modelpdf ' " ;
$sql .= " WHERE rowid = $this->id AND fk_statut < 2 ; " ;
if ( $this -> db -> query ( $sql ) )
{
$this -> modelpdf = $modelpdf ;
return 1 ;
}
else
{
dolibarr_print_error ( $this -> db );
return 0 ;
}
}
}
2006-02-09 13:25:34 +01:00
/**
2006-06-17 01:55:20 +02:00
* \brief Ajoute un produit dans les tableaux products , products_qty , products_date_start | end
* \param idproduct
* \param qty
* \param remise_percent
2006-06-17 20:12:46 +02:00
* \param date_start
* \param date_end
2006-06-17 01:55:20 +02:00
*/
2006-06-17 20:12:46 +02:00
function add_product ( $idproduct , $qty , $remise_percent , $date_start = '' , $date_end = '' )
2006-02-09 13:25:34 +01:00
{
if ( $idproduct > 0 )
{
$i = sizeof ( $this -> products ); // On recupere nb de produit deja dans tableau products
$this -> products [ $i ] = $idproduct ; // On ajoute a la suite
if ( ! $qty )
{
$qty = 1 ;
}
$this -> products_qty [ $i ] = $qty ;
$this -> products_remise_percent [ $i ] = $remise_percent ;
2006-06-17 20:12:46 +02:00
if ( $date_start ) { $this -> products_date_start [ $i ] = $date_start ; }
if ( $date_end ) { $this -> products_date_end [ $i ] = $date_end ; }
2006-02-09 13:25:34 +01:00
}
}
/**
2006-06-16 02:33:04 +02:00
* \brief Ajoute une ligne de facture ( associ<EFBFBD> <EFBFBD> un produit / service pr<EFBFBD> d<EFBFBD> fini ou non )
2006-06-17 20:12:46 +02:00
* \param facid Id de la facture
* \param desc Description de la ligne
* \param pu Prix unitaire
* \param qty Quantit<EFBFBD>
* \param txtva Taux de tva forc<EFBFBD> , sinon - 1
* \param fk_product Id du produit / service pred<EFBFBD> fini
* \param remise_percent Pourcentage de remise de la ligne
* \param date_start Date de debut de validit<EFBFBD> du service
* \param date_end Date de fin de validit<EFBFBD> du service
* \param ventil Code de ventilation comptable
2006-06-16 02:33:04 +02:00
* \remarks Les parametres sont deja cens<EFBFBD> etre juste et avec valeurs finales a l ' appel
* de cette methode . Aussi , pour le taux tva , il doit deja avoir ete d<EFBFBD> fini
* par l ' appelant par la methode get_default_tva ( societe_vendeuse , societe_acheteuse , taux_produit )
* et le desc doit deja avoir la bonne valeur ( a l ' appelant de gerer le multilangue )
*/
2006-06-17 20:12:46 +02:00
function addline ( $facid , $desc , $pu , $qty , $txtva , $fk_product = 0 , $remise_percent = 0 , $date_start = '' , $date_end = '' , $ventil = 0 )
2006-02-09 13:25:34 +01:00
{
global $conf ;
2006-06-17 20:12:46 +02:00
dolibarr_syslog ( " facture.class.php::addline( $facid , $desc , $pu , $qty , $txtva , $fk_product , $remise_percent , $date_start , $date_end , $ventil ) " );
2006-06-15 02:50:42 +02:00
include_once ( DOL_DOCUMENT_ROOT . '/lib/price.lib.php' );
2006-02-12 16:54:21 +01:00
2006-02-09 13:25:34 +01:00
if ( $this -> brouillon )
{
2006-06-15 02:50:42 +02:00
$this -> db -> begin ();
2006-02-09 13:25:34 +01:00
// Nettoyage param<61> tres
$remise_percent = price2num ( $remise_percent );
$qty = price2num ( $qty );
2006-02-12 16:54:21 +01:00
if ( ! $qty ) $qty = 1 ;
if ( ! $ventil ) $ventil = 0 ;
2006-06-15 02:50:42 +02:00
if ( ! $info_bits ) $info_bits = 0 ;
$pu = price2num ( $pu );
$txtva = price2num ( $txtva );
// Calcul du total TTC et de la TVA pour la ligne a partir de
// qty, pu, remise_percent et txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
$tabprice = calcul_price_total ( $qty , $pu , $remise_percent , $txtva );
$total_ht = $tabprice [ 0 ];
$total_tva = $tabprice [ 1 ];
$total_ttc = $tabprice [ 2 ];
// Anciens indicateurs: $price, $subprice, $remise (a ne plus utiliser)
2006-02-09 13:25:34 +01:00
$price = $pu ;
$subprice = $pu ;
$remise = 0 ;
if ( $remise_percent > 0 )
{
$remise = round (( $pu * $remise_percent / 100 ), 2 );
$price = ( $pu - $remise );
}
2006-06-17 18:52:30 +02:00
// Insertion ligne
$ligne = new FactureLigne ( $this -> db );
$ligne -> fk_facture = $facid ;
$ligne -> desc = $desc ;
$ligne -> price = $price ;
$ligne -> qty = $qty ;
$ligne -> txtva = $txtva ;
$ligne -> fk_product = $fk_product ;
$ligne -> remise_percent = $remise_percent ;
$ligne -> subprice = $subprice ;
$ligne -> remise = $remise ;
2006-06-17 20:12:46 +02:00
$ligne -> date_start = $date_start ;
$ligne -> date_end = $date_end ;
2006-06-17 18:52:30 +02:00
$ligne -> ventil = $ventil ;
2006-07-01 01:38:12 +02:00
$ligne -> rang =- 1 ;
2006-06-17 18:52:30 +02:00
$ligne -> info_bits = $info_bits ;
$ligne -> total_ht = $total_ht ;
$ligne -> total_tva = $total_tva ;
$ligne -> total_ttc = $total_ttc ;
$result = $ligne -> insert ();
if ( $result > 0 )
2006-02-09 13:25:34 +01:00
{
2006-06-15 02:50:42 +02:00
// Mise a jour informations denormalisees au niveau de la facture meme
2006-06-17 01:55:20 +02:00
$result = $this -> update_price ( $facid );
2006-06-15 02:50:42 +02:00
if ( $result > 0 )
{
$this -> db -> commit ();
return 1 ;
}
else
{
2006-06-17 01:55:20 +02:00
$this -> error = $this -> db -> error ();
dolibarr_syslog ( " Error sql= $sql , error= " . $this -> error );
2006-06-15 02:50:42 +02:00
$this -> db -> rollback ();
return - 1 ;
}
2006-02-09 13:25:34 +01:00
}
else
{
2006-06-17 18:52:30 +02:00
$this -> error = $ligne -> error ;
2006-06-17 01:55:20 +02:00
$this -> db -> rollback ();
return - 2 ;
2006-02-09 13:25:34 +01:00
}
}
}
/**
* \brief Mets <EFBFBD> jour une ligne de facture
* \param rowid Id de la ligne de facture
* \param desc Description de la ligne
* \param pu Prix unitaire
* \param qty Quantit<EFBFBD>
* \param remise_percent Pourcentage de remise de la ligne
2006-06-17 20:12:46 +02:00
* \param date_start Date de debut de validit<EFBFBD> du service
* \param date_end Date de fin de validit<EFBFBD> du service
2006-02-09 13:25:34 +01:00
* \param tva_tx Taux TVA
* \return int < 0 si erreur , > 0 si ok
*/
2006-06-17 20:12:46 +02:00
function updateline ( $rowid , $desc , $pu , $qty , $remise_percent = 0 , $date_start , $date_end , $txtva )
2006-02-09 13:25:34 +01:00
{
2006-06-17 20:12:46 +02:00
dolibarr_syslog ( " Facture::UpdateLine $rowid , $desc , $pu , $qty , $remise_percent , $date_start , $date_end , $txtva " );
2006-06-15 02:50:42 +02:00
include_once ( DOL_DOCUMENT_ROOT . '/lib/price.lib.php' );
2006-02-09 13:25:34 +01:00
if ( $this -> brouillon )
{
$this -> db -> begin ();
2006-06-15 02:50:42 +02:00
// Nettoyage param<61> tres
$remise_percent = price2num ( $remise_percent );
$qty = price2num ( $qty );
if ( ! $qty ) $qty = 1 ;
$pu = price2num ( $pu );
$txtva = price2num ( $txtva );
// Calcul du total TTC et de la TVA pour la ligne a partir de
// qty, pu, remise_percent et txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
$tabprice = calcul_price_total ( $qty , $pu , $remise_percent , $txtva );
$total_ht = $tabprice [ 0 ];
$total_tva = $tabprice [ 1 ];
$total_ttc = $tabprice [ 2 ];
// Anciens indicateurs: $price, $subprice, $remise (a ne plus utiliser)
$price = $pu ;
$subprice = $pu ;
2006-02-09 13:25:34 +01:00
$remise = 0 ;
2006-06-15 02:50:42 +02:00
if ( $remise_percent > 0 )
2006-02-09 13:25:34 +01:00
{
2006-06-15 02:50:42 +02:00
$remise = round (( $pu * $remise_percent / 100 ), 2 );
$price = ( $pu - $remise );
2006-02-09 13:25:34 +01:00
}
2006-06-15 02:50:42 +02:00
$price = price2num ( $price );
$subprice = price2num ( $subprice );
2006-02-09 13:25:34 +01:00
2006-06-15 02:50:42 +02:00
// Mise a jour ligne en base
2006-06-17 18:52:30 +02:00
$ligne = new FactureLigne ( $this -> db );
$ligne -> rowid = $rowid ;
$ligne -> fetch ( $rowid );
$ligne -> desc = $desc ;
$ligne -> price = $price ;
$ligne -> qty = $qty ;
2006-07-11 09:49:33 +02:00
$ligne -> tva_taux = $txtva ;
2006-06-17 18:52:30 +02:00
$ligne -> remise_percent = $remise_percent ;
$ligne -> subprice = $subprice ;
$ligne -> remise = $remise ;
2006-06-17 20:12:46 +02:00
$ligne -> date_start = $date_start ;
$ligne -> date_end = $date_end ;
2006-06-17 18:52:30 +02:00
$ligne -> total_ht = $total_ht ;
$ligne -> total_tva = $total_tva ;
$ligne -> total_ttc = $total_ttc ;
$result = $ligne -> update ();
2006-06-17 01:55:20 +02:00
if ( $result > 0 )
2006-02-09 13:25:34 +01:00
{
2006-06-15 02:50:42 +02:00
// Mise a jour info denormalisees au niveau facture
2006-06-17 01:55:20 +02:00
$this -> update_price ( $this -> id );
2006-02-09 13:25:34 +01:00
$this -> db -> commit ();
return $result ;
}
else
{
$this -> db -> rollback ();
return - 1 ;
}
}
else
{
2006-06-17 01:55:20 +02:00
$this -> error = " Facture::UpdateLine Invoice statut makes operation forbidden " ;
2006-02-09 13:25:34 +01:00
return - 2 ;
}
}
/**
* \brief Supprime une ligne facture de la base
* \param rowid id de la ligne de facture a supprimer
*/
function deleteline ( $rowid )
{
if ( $this -> brouillon )
{
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'facturedet WHERE rowid = ' . $rowid ;
$result = $this -> db -> query ( $sql );
2006-06-17 01:55:20 +02:00
$this -> update_price ( $this -> id );
2006-02-09 13:25:34 +01:00
}
}
/**
2006-06-15 02:50:42 +02:00
* \brief Mise <EFBFBD> jour des sommes de la facture et calculs denormalises
2006-04-04 02:12:45 +02:00
* \param facid id de la facture a modifier
* \return int < 0 si ko , > 0 si ok
*/
2006-06-17 01:55:20 +02:00
function update_price ( $facid )
2006-02-09 13:25:34 +01:00
{
2006-06-15 02:50:42 +02:00
$tvas = array ();
2006-02-09 13:25:34 +01:00
$err = 0 ;
2006-04-04 02:12:45 +02:00
2006-06-17 01:55:20 +02:00
// Liste des lignes factures a sommer
2006-06-17 03:53:32 +02:00
$sql = 'SELECT qty, tva_taux, subprice, remise_percent, price,' ;
$sql .= ' total_ht, total_tva, total_ttc' ;
2006-06-15 02:50:42 +02:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'facturedet' ;
$sql .= ' WHERE fk_facture = ' . $facid ;
2006-06-17 03:53:32 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2006-02-09 13:25:34 +01:00
{
2006-06-15 02:50:42 +02:00
$this -> total_ht = 0 ;
$this -> total_tva = 0 ;
$this -> total_ttc = 0 ;
2006-06-17 03:53:32 +02:00
$num = $this -> db -> num_rows ( $resql );
2006-02-09 13:25:34 +01:00
$i = 0 ;
while ( $i < $num )
{
2006-06-17 03:53:32 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2006-06-15 02:50:42 +02:00
$this -> total_ht += $obj -> total_ht ;
$this -> total_tva += ( $obj -> total_ttc - $obj -> total_ht );
$this -> total_ttc += $obj -> total_ttc ;
// Anciens indicateurs
2006-06-17 03:53:32 +02:00
$this -> amount_ht += ( $obj -> price * $obj -> qty );
2006-06-15 02:50:42 +02:00
$this -> total_remise += 0 ; // Plus de remise globale (toute remise est sur une ligne)
$tvas [ $obj -> tva_taux ] += ( $obj -> total_ttc - $obj -> total_ht );
/* \deprecated car simplifie par les 3 indicateurs total_ht , total_tva et total_ttc sur lignes
2006-02-09 13:25:34 +01:00
$products [ $i ][ 0 ] = $obj -> price ;
$products [ $i ][ 1 ] = $obj -> qty ;
$products [ $i ][ 2 ] = $obj -> tva_taux ;
2006-06-15 02:50:42 +02:00
*/
2006-02-09 13:25:34 +01:00
$i ++ ;
}
2006-06-17 03:53:32 +02:00
$this -> db -> free ( $resql );
2006-04-29 17:09:44 +02:00
2006-06-15 02:50:42 +02:00
/* \deprecated car simplifie par les 3 indicateurs total_ht , total_tva et total_ttc sur lignes
2006-04-04 02:12:45 +02:00
$calculs = calcul_price ( $products , $this -> remise_percent , $this -> remise_absolue );
2006-02-09 13:25:34 +01:00
$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 ];
2006-06-15 02:50:42 +02:00
*/
2006-02-09 13:25:34 +01:00
2006-06-15 02:50:42 +02:00
// Met a jour indicateurs sur facture
2006-02-09 13:25:34 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture ' ;
$sql .= " SET amount =' " . price2num ( $this -> amount_ht ) . " ' " ;
$sql .= " , remise=' " . price2num ( $this -> total_remise ) . " ' " ;
$sql .= " , total=' " . price2num ( $this -> total_ht ) . " ' " ;
$sql .= " , tva=' " . price2num ( $this -> total_tva ) . " ' " ;
$sql .= " , total_ttc=' " . price2num ( $this -> total_ttc ) . " ' " ;
$sql .= ' WHERE rowid = ' . $facid ;
2006-06-17 03:53:32 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2006-02-09 13:25:34 +01:00
{
2006-06-15 02:50:42 +02:00
// \TODO A supprimer car l'utilisation de facture_tva_sum non utilisable
// dans un context compta propre. On utilisera plutot les lignes.
2006-02-09 13:25:34 +01:00
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'facture_tva_sum WHERE fk_facture=' . $this -> id ;
if ( $this -> db -> query ( $sql ) )
{
foreach ( $tvas as $key => $value )
{
$sql_del = 'DELETE FROM ' . MAIN_DB_PREFIX . 'facture_tva_sum where fk_facture =' . $this -> id ;
$this -> db -> query ( $sql_del );
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . " facture_tva_sum (fk_facture,amount,tva_tx) values ( $this->id ,' " . price2num ( $tvas [ $key ]) . " ',' " . price2num ( $key ) . " '); " ;
if ( ! $this -> db -> query ( $sql ) )
{
dolibarr_print_error ( $this -> db );
$err ++ ;
}
}
}
else
{
$err ++ ;
}
if ( $err == 0 )
{
return 1 ;
}
else
{
return - 3 ;
}
}
else
{
dolibarr_print_error ( $this -> db );
}
}
else
{
dolibarr_print_error ( $this -> db );
}
}
/**
2006-04-29 17:09:44 +02:00
* \brief Applique une remise relative
2006-04-04 02:12:45 +02:00
* \param user User qui positionne la remise
* \param remise
* \return int < 0 si ko , > 0 si ok
*/
2006-02-09 13:25:34 +01:00
function set_remise ( $user , $remise )
{
2006-04-04 02:12:45 +02:00
$remise = trim ( $remise ) ? trim ( $remise ) : 0 ;
2006-02-09 13:25:34 +01:00
if ( $user -> rights -> facture -> creer )
{
2006-04-08 13:39:41 +02:00
$remise = price2num ( $remise );
2006-04-04 02:12:45 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture' ;
2006-04-08 13:39:41 +02:00
$sql .= ' SET remise_percent = ' . $remise ;
2006-04-04 02:12:45 +02:00
$sql .= ' WHERE rowid = ' . $this -> id . ' AND fk_statut = 0 ;' ;
2006-02-09 13:25:34 +01:00
2006-04-04 02:12:45 +02:00
if ( $this -> db -> query ( $sql ))
2006-02-09 13:25:34 +01:00
{
2006-04-04 02:12:45 +02:00
$this -> remise_percent = $remise ;
2006-06-17 01:55:20 +02:00
$this -> update_price ( $this -> id );
2006-02-09 13:25:34 +01:00
return 1 ;
}
else
{
2006-04-04 02:12:45 +02:00
$this -> error = $this -> db -> error ();
return - 1 ;
2006-02-09 13:25:34 +01:00
}
}
}
/**
2006-04-29 17:09:44 +02:00
* \brief Applique une remise absolue
2006-04-04 02:12:45 +02:00
* \param user User qui positionne la remise
* \param remise
* \return int < 0 si ko , > 0 si ok
*/
function set_remise_absolue ( $user , $remise )
{
$remise = trim ( $remise ) ? trim ( $remise ) : 0 ;
2006-06-13 00:20:24 +02:00
2006-04-04 02:12:45 +02:00
if ( $user -> rights -> facture -> creer )
{
2006-04-08 13:39:41 +02:00
$remise = price2num ( $remise );
2006-06-13 00:20:24 +02:00
2006-04-04 02:12:45 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture' ;
2006-04-08 13:39:41 +02:00
$sql .= ' SET remise_absolue = ' . $remise ;
2006-04-04 02:12:45 +02:00
$sql .= ' WHERE rowid = ' . $this -> id . ' AND fk_statut = 0 ;' ;
dolibarr_syslog ( " Facture::set_remise_absolue sql= $sql " );
if ( $this -> db -> query ( $sql ))
{
$this -> remise_absolue = $remise ;
2006-06-17 01:55:20 +02:00
$this -> update_price ( $this -> id );
2006-04-04 02:12:45 +02:00
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
}
/**
* \brief Renvoie la liste des sommes de tva
*/
2006-02-09 13:25:34 +01:00
function getSumTva ()
{
2006-06-11 02:36:17 +02:00
$tvs = array ();
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
$sql = 'SELECT amount, tva_tx FROM ' . MAIN_DB_PREFIX . 'facture_tva_sum WHERE fk_facture = ' . $this -> id ;
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
{
2006-06-11 02:36:17 +02:00
dolibarr_print_error ( $this -> db );
2006-02-09 13:25:34 +01:00
return - 1 ;
}
}
/**
* \brief Renvoie la sommes des paiements deja effectu<EFBFBD> s
* \remarks Utilis<EFBFBD> entre autre par certains mod<EFBFBD> les de factures
*/
function getSommePaiement ()
{
$sql = 'SELECT sum(amount) FROM ' . MAIN_DB_PREFIX . 'paiement_facture WHERE fk_facture = ' . $this -> id ;
if ( $this -> db -> query ( $sql ))
{
$row = $this -> db -> fetch_row ( 0 );
return $row [ 0 ];
}
else
{
return - 1 ;
}
}
/**
* \brief Retourne le libell<EFBFBD> du statut d ' une facture ( brouillon , valid<EFBFBD> e , abandonn<EFBFBD> e , pay<EFBFBD> e )
2006-03-18 23:46:29 +01:00
* \param mode 0 = libell<EFBFBD> long , 1 = libell<EFBFBD> court , 2 = Picto + Libell<EFBFBD> court , 3 = Picto , 4 = Picto + Libell<EFBFBD> long
2006-02-09 13:25:34 +01:00
* \return string Libelle
*/
2006-03-19 01:39:12 +01:00
function getLibStatut ( $mode = 0 , $alreadypayed =- 1 )
2006-02-09 13:25:34 +01:00
{
2006-03-19 01:39:12 +01:00
return $this -> LibStatut ( $this -> paye , $this -> statut , $mode , $alreadypayed );
2006-02-09 13:25:34 +01:00
}
/**
2006-03-19 01:39:12 +01:00
* \brief Renvoi le libell<EFBFBD> d ' un statut donn<EFBFBD>
* \param paye Etat paye
* \param statut Id statut
* \param mode 0 = libell<EFBFBD> long , 1 = libell<EFBFBD> court , 2 = Picto + Libell<EFBFBD> court , 3 = Picto , 4 = Picto + Libell<EFBFBD> long , 5 = Libell<EFBFBD> court + Picto
2006-04-08 14:52:30 +02:00
* \param alreadypayed Montant deja pay<EFBFBD>
2006-03-19 01:39:12 +01:00
* \return string Libell<EFBFBD> du statut
2006-02-09 13:25:34 +01:00
*/
2006-03-19 01:39:12 +01:00
function LibStatut ( $paye , $statut , $mode = 0 , $alreadypayed =- 1 )
2006-02-09 13:25:34 +01:00
{
global $langs ;
$langs -> load ( 'bills' );
2006-03-18 23:46:29 +01:00
if ( $mode == 0 )
{
$prefix = '' ;
if ( ! $paye )
{
if ( $statut == 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusDraft' );
2006-07-15 20:29:46 +02:00
if ( $statut == 3 && $alreadypayed <= 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusClosedUnpayed' );
if ( $statut == 3 && $alreadypayed > 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusClosedPayedPartially' );
2006-03-19 01:39:12 +01:00
if ( $alreadypayed <= 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusNotPayed' );
return $langs -> trans ( 'Bill' . $prefix . 'StatusStarted' );
2006-03-18 23:46:29 +01:00
}
else
{
return $langs -> trans ( 'Bill' . $prefix . 'StatusPayed' );
}
}
2006-02-09 13:25:34 +01:00
if ( $mode == 1 )
2006-03-18 23:46:29 +01:00
{
$prefix = 'Short' ;
if ( ! $paye )
{
if ( $statut == 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusDraft' );
2006-07-15 20:29:46 +02:00
if ( $statut == 3 && $alreadypayed <= 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusClosedUnpayed' );
if ( $statut == 3 && $alreadypayed > 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusClosedPayedPartially' );
2006-03-19 01:39:12 +01:00
if ( $alreadypayed <= 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusNotPayed' );
return $langs -> trans ( 'Bill' . $prefix . 'StatusStarted' );
2006-03-18 23:46:29 +01:00
}
else
{
return $langs -> trans ( 'Bill' . $prefix . 'StatusPayed' );
}
}
if ( $mode == 2 )
{
2006-02-09 13:25:34 +01:00
$prefix = 'Short' ;
2006-03-18 23:46:29 +01:00
if ( ! $paye )
{
2006-07-15 20:29:46 +02:00
if ( $statut == 0 ) return img_picto ( $langs -> trans ( 'BillStatusDraft' ), 'statut0' ) . ' ' . $langs -> trans ( 'Bill' . $prefix . 'StatusDraft' );
if ( $statut == 3 && $alreadypayed <= 0 ) return img_picto ( $langs -> trans ( 'BillStatusCanceled' ), 'statut5' ) . ' ' . $langs -> trans ( 'Bill' . $prefix . 'StatusClosedUnpayed' );
if ( $statut == 3 && $alreadypayed > 0 ) return img_picto ( $langs -> trans ( 'BillStatusClosedPayedPartially' ), 'statut7' ) . ' ' . $langs -> trans ( 'Bill' . $prefix . 'StatusClosedPayedPartially' );
if ( $alreadypayed <= 0 ) return img_picto ( $langs -> trans ( 'BillStatusNotPayed' ), 'statut1' ) . ' ' . $langs -> trans ( 'Bill' . $prefix . 'StatusNotPayed' );
return img_picto ( $langs -> trans ( 'BillStatusStarted' ), 'statut3' ) . ' ' . $langs -> trans ( 'Bill' . $prefix . 'StatusStarted' );
2006-03-18 23:46:29 +01:00
}
else
{
2006-07-15 20:29:46 +02:00
return img_picto ( $langs -> trans ( 'BillStatusPayed' ), 'statut6' ) . ' ' . $langs -> trans ( 'Bill' . $prefix . 'StatusPayed' );
2006-03-18 23:46:29 +01:00
}
}
if ( $mode == 3 )
2006-02-09 13:25:34 +01:00
{
2006-03-18 23:46:29 +01:00
$prefix = 'Short' ;
if ( ! $paye )
{
2006-07-15 20:29:46 +02:00
if ( $statut == 0 ) return img_picto ( $langs -> trans ( 'BillStatusDraft' ), 'statut0' );
if ( $statut == 3 && $alreadypayed <= 0 ) return img_picto ( $langs -> trans ( 'BillStatusCanceled' ), 'statut5' );
if ( $statut == 3 && $alreadypayed > 0 ) return img_picto ( $langs -> trans ( 'BillStatusClosedPayedPartially' ), 'statut7' );
if ( $alreadypayed <= 0 ) return img_picto ( $langs -> trans ( 'BillStatusNotPayed' ), 'statut1' );
return img_picto ( $langs -> trans ( 'BillStatusStarted' ), 'statut3' );
2006-03-18 23:46:29 +01:00
}
else
{
2006-07-15 20:29:46 +02:00
return img_picto ( $langs -> trans ( 'BillStatusPayed' ), 'statut6' );
2006-03-18 23:46:29 +01:00
}
2006-02-09 13:25:34 +01:00
}
2006-03-18 23:46:29 +01:00
if ( $mode == 4 )
2006-03-19 01:39:12 +01:00
{
if ( ! $paye )
{
if ( $statut == 0 ) return img_picto ( $langs -> trans ( 'BillStatusDraft' ), 'statut0' ) . ' ' . $langs -> trans ( 'BillStatusDraft' );
2006-07-15 20:29:46 +02:00
if ( $statut == 3 && $alreadypayed <= 0 ) return img_picto ( $langs -> trans ( 'BillStatusCanceled' ), 'statut5' ) . ' ' . $langs -> trans ( 'Bill' . $prefix . 'StatusCanceled' );
if ( $statut == 3 && $alreadypayed > 0 ) return img_picto ( $langs -> trans ( 'BillStatusClosedPayedPartially' ), 'statut7' ) . ' ' . $langs -> trans ( 'Bill' . $prefix . 'StatusClosedPayedPartially' );
2006-03-19 01:39:12 +01:00
if ( $alreadypayed <= 0 ) return img_picto ( $langs -> trans ( 'BillStatusNotPayed' ), 'statut1' ) . ' ' . $langs -> trans ( 'BillStatusNotPayed' );
return img_picto ( $langs -> trans ( 'BillStatusStarted' ), 'statut3' ) . ' ' . $langs -> trans ( 'BillStatusStarted' );
}
else
{
return img_picto ( $langs -> trans ( 'BillStatusPayed' ), 'statut6' ) . ' ' . $langs -> trans ( 'BillStatusPayed' );
}
}
if ( $mode == 5 )
2006-02-09 13:25:34 +01:00
{
2006-03-18 23:46:29 +01:00
$prefix = 'Short' ;
if ( ! $paye )
{
2006-07-15 20:29:46 +02:00
if ( $statut == 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusDraft' ) . ' ' . img_picto ( $langs -> trans ( 'BillStatusDraft' ), 'statut0' );
if ( $statut == 3 && $alreadypayed <= 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusCanceled' ) . ' ' . img_picto ( $langs -> trans ( 'BillStatusCanceled' ), 'statut5' );
if ( $statut == 3 && $alreadypayed > 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusClosedPayedPartially' ) . ' ' . img_picto ( $langs -> trans ( 'BillStatusClosedPayedPartially' ), 'statut7' );
if ( $alreadypayed <= 0 ) return $langs -> trans ( 'Bill' . $prefix . 'StatusNotPayed' ) . ' ' . img_picto ( $langs -> trans ( 'BillStatusNotPayed' ), 'statut1' );
return $langs -> trans ( 'Bill' . $prefix . 'StatusStarted' ) . ' ' . img_picto ( $langs -> trans ( 'BillStatusStarted' ), 'statut3' );
2006-03-18 23:46:29 +01:00
}
else
{
2006-07-15 20:29:46 +02:00
return $langs -> trans ( 'Bill' . $prefix . 'StatusPayed' ) . ' ' . img_picto ( $langs -> trans ( 'BillStatusPayed' ), 'statut6' );
2006-03-18 23:46:29 +01:00
}
2006-02-09 13:25:34 +01:00
}
}
/**
* \brief Renvoie la r<EFBFBD> f<EFBFBD> rence de facture suivante non utilis<EFBFBD> e en fonction du module
* de num<EFBFBD> rotation actif d<EFBFBD> fini dans FACTURE_ADDON
* \param soc objet societe
* \return string reference libre pour la facture
*/
function getNextNumRef ( $soc )
{
global $db , $langs ;
$langs -> load ( " bills " );
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
$dir = DOL_DOCUMENT_ROOT . " /includes/modules/facture/ " ;
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
if ( defined ( " FACTURE_ADDON " ) && FACTURE_ADDON )
{
$file = FACTURE_ADDON . " / " . FACTURE_ADDON . " .modules.php " ;
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
// Chargement de la classe de num<75> rotation
$classname = " mod_facture_ " . FACTURE_ADDON ;
require_once ( $dir . $file );
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
$obj = new $classname ();
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
$numref = " " ;
$numref = $obj -> getNumRef ( $soc , $this );
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
if ( $numref != " " )
{
return $numref ;
}
else
{
dolibarr_print_error ( $db , " Facture::getNextNumRef " . $obj -> error );
return " " ;
}
}
else
{
print $langs -> trans ( " Error " ) . " " . $langs -> trans ( " Error_FACTURE_ADDON_NotDefined " );
return " " ;
}
}
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
/**
2006-02-12 16:54:21 +01:00
* \brief Mets <EFBFBD> jour les commentaires priv<EFBFBD> s
* \param note Commentaire
* \return int < 0 si ko , > 0 si ok
2006-02-09 13:25:34 +01:00
*/
function update_note ( $note )
{
2006-02-12 17:23:43 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture' ;
$sql .= " SET note = ' " . addslashes ( $note ) . " ' " ;
$sql .= " WHERE rowid = " . $this -> id ;
2006-02-09 13:25:34 +01:00
2006-02-12 16:54:21 +01:00
if ( $this -> db -> query ( $sql ))
2006-02-09 13:25:34 +01:00
{
$this -> note = $note ;
return 1 ;
}
else
{
2006-02-12 17:23:43 +01:00
$this -> error = $this -> db -> error ();
2006-02-09 13:25:34 +01:00
return - 1 ;
}
}
2006-02-12 16:54:21 +01:00
/**
* \brief Mets <EFBFBD> jour les commentaires publiques
* \param note_public Commentaire
* \return int < 0 si ko , > 0 si ok
*/
function update_note_public ( $note_public )
{
2006-02-12 17:23:43 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture' ;
$sql .= " SET note_public = ' " . addslashes ( $note_public ) . " ' " ;
$sql .= " WHERE rowid = " . $this -> id ;
2006-02-12 16:54:21 +01:00
if ( $this -> db -> query ( $sql ))
{
$this -> note_public = $note_public ;
return 1 ;
}
else
{
2006-02-12 17:23:43 +01:00
$this -> error = $this -> db -> error ();
2006-02-12 16:54:21 +01:00
return - 1 ;
}
}
2006-06-13 00:20:24 +02:00
2006-02-09 13:25:34 +01:00
/**
2006-06-03 14:38:57 +02:00
* \brief Charge les informations de l 'onglet info dans l' objet facture
2006-02-12 16:54:21 +01:00
* \param id Id de la facture a charger
2006-02-09 13:25:34 +01:00
*/
function info ( $id )
{
2006-06-03 14:38:57 +02:00
$sql = 'SELECT c.rowid, ' . $this -> db -> pdate ( 'datec' ) . ' as datec,' ;
$sql .= ' ' . $this -> db -> pdate ( 'date_valid' ) . ' as datev,' ;
$sql .= ' fk_user_author, fk_user_valid' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'facture as c' ;
$sql .= ' WHERE c.rowid = ' . $id ;
2006-02-09 13:25:34 +01:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
if ( $this -> db -> num_rows ( $result ))
{
$obj = $this -> db -> fetch_object ( $result );
$this -> id = $obj -> rowid ;
if ( $obj -> fk_user_author )
{
$cuser = new User ( $this -> db , $obj -> fk_user_author );
$cuser -> fetch ();
$this -> user_creation = $cuser ;
}
if ( $obj -> fk_user_valid )
{
$vuser = new User ( $this -> db , $obj -> fk_user_valid );
$vuser -> fetch ();
$this -> user_validation = $vuser ;
}
$this -> date_creation = $obj -> datec ;
2006-06-03 14:38:57 +02:00
$this -> date_validation = $obj -> datev ;
2006-02-09 13:25:34 +01:00
}
$this -> db -> free ( $result );
}
else
{
dolibarr_print_error ( $this -> db );
}
}
/**
* \brief Change les conditions de r<EFBFBD> glement de la facture
* \param cond_reglement_id Id de la nouvelle condition de r<EFBFBD> glement
* \return int > 0 si ok , < 0 si ko
*/
function cond_reglement ( $cond_reglement_id )
{
dolibarr_syslog ( 'Facture::cond_reglement(' . $cond_reglement_id . ')' );
if ( $this -> statut >= 0 && $this -> paye == 0 )
{
$datelim = $this -> calculate_date_lim_reglement ( $cond_reglement_id );
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture' ;
$sql .= ' SET fk_cond_reglement = ' . $cond_reglement_id ;
$sql .= ', date_lim_reglement=' . $this -> db -> idate ( $datelim );
$sql .= ' WHERE rowid=' . $this -> id ;
if ( $this -> db -> query ( $sql ) )
{
$this -> cond_reglement_id = $cond_reglement_id ;
return 1 ;
}
else
{
dolibarr_syslog ( 'Facture::cond_reglement Erreur ' . $sql . ' - ' . $this -> db -> error ());
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
else
{
dolibarr_syslog ( 'Facture::cond_reglement, etat facture incompatible' );
$this -> error = 'Etat facture incompatible ' . $this -> statut . ' ' . $this -> paye ;
return - 2 ;
}
}
/**
* \brief Change le mode de r<EFBFBD> glement
* \param mode Id du nouveau mode
* \return int > 0 si ok , < 0 si ko
*/
function mode_reglement ( $mode_reglement_id )
{
dolibarr_syslog ( 'Facture::mode_reglement(' . $mode_reglement_id . ')' );
if ( $this -> statut >= 0 && $this -> paye == 0 )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture' ;
$sql .= ' SET fk_mode_reglement = ' . $mode_reglement_id ;
$sql .= ' WHERE rowid=' . $this -> id ;
if ( $this -> db -> query ( $sql ) )
{
$this -> mode_reglement_id = $mode_reglement_id ;
return 1 ;
}
else
{
dolibarr_syslog ( 'Facture::mode_reglement Erreur ' . $sql . ' - ' . $this -> db -> error ());
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
else
{
dolibarr_syslog ( 'Facture::mode_reglement, etat facture incompatible' );
$this -> error = 'Etat facture incompatible ' . $this -> statut . ' ' . $this -> paye ;
return - 2 ;
}
}
2006-07-15 20:29:46 +02:00
/**
2006-07-16 14:26:36 +02:00
* \brief Renvoi si les lignes de facture sont ventil<EFBFBD> es et / ou export<EFBFBD> es en compta
* \param user Utilisateur cr<EFBFBD> ant la demande
* \return int < 0 si ko , 0 = non , 1 = oui
*/
function getVentilExportCompta ()
{
// On v<> rifie si les lignes de factures ont <20> t<EFBFBD> export<72> es en compta et/ou ventil<69> es
$ventilExportCompta = 0 ;
for ( $i = 0 ; $i < sizeof ( $this -> lignes ) ; $i ++ )
{
if ( $this -> lignes [ $i ] -> export_compta <> 0 && $this -> lignes [ $i ] -> code_ventilation <> 0 )
{
$ventilExportCompta ++ ;
}
}
if ( $ventilExportCompta <> 0 )
{
return 1 ;
}
else
{
return 0 ;
}
}
2006-07-17 02:00:54 +02:00
/**
* \brief Renvoi si une facture peut etre supprim<EFBFBD> e compl<EFBFBD> tement .
* La r<EFBFBD> gle est la suivante :
* Si facture derni<EFBFBD> re , non provisoire , sans paiement et non export<EFBFBD> en compta -> oui fin de r<EFBFBD> gle
* Si facture brouillon et provisoire -> oui
* \return int < 0 si ko , 0 = non , 1 = oui
*/
2006-07-15 20:29:46 +02:00
function is_erasable ()
{
2006-07-17 02:00:54 +02:00
global $conf ;
2006-07-15 20:29:46 +02:00
2006-07-16 13:51:11 +02:00
// on v<> rifie si la facture est en num<75> rotation provisoire
2006-07-17 02:00:54 +02:00
$facref = substr ( $this -> ref , 1 , 4 );
2006-07-16 13:51:11 +02:00
// Si facture non brouillon et non provisoire
2006-07-17 02:00:54 +02:00
if ( $facref != 'PROV' && $conf -> compta -> enabled && $conf -> global -> FACTURE_ENABLE_EDITDELETE )
2006-07-15 20:29:46 +02:00
{
// On ne peut supprimer que la derni<6E> re facture valid<69> e
2006-07-16 13:51:11 +02:00
// pour ne pas avoir de trou dans la num<75> rotation
2006-07-15 20:29:46 +02:00
$sql = " SELECT MAX(facnumber) " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " facture " ;
2006-07-17 02:00:54 +02:00
$resql = $this -> db -> query ( $sql );
2006-07-15 20:29:46 +02:00
if ( $resql )
{
2006-07-17 02:00:54 +02:00
$maxfacnumber = $this -> db -> fetch_row ( $resql );
2006-07-15 20:29:46 +02:00
}
2006-07-16 14:26:36 +02:00
$ventilExportCompta = $this -> getVentilExportCompta ();
2006-07-15 20:29:46 +02:00
// Si derniere facture et si non ventil<69> e, on peut supprimer
if ( $maxfacnumber [ 0 ] == $this -> ref && $ventilExportCompta == 0 )
{
return 1 ;
}
}
2006-07-17 02:00:54 +02:00
else if ( $this -> statut == 0 && $facref == 'PROV' ) // Si facture brouillon et provisoire
2006-07-16 13:51:11 +02:00
{
return 1 ;
}
2006-07-15 20:29:46 +02:00
return 0 ;
}
2006-02-09 13:25:34 +01:00
/**
2006-07-16 14:26:36 +02:00
* \brief Cr<EFBFBD> <EFBFBD> une demande de pr<EFBFBD> l<EFBFBD> vement
2006-02-09 13:25:34 +01:00
* \param user Utilisateur cr<EFBFBD> ant la demande
2006-06-13 00:20:24 +02:00
* \return int < 0 si ko , > 0 si ok
2006-07-16 14:26:36 +02:00
*/
2006-02-09 13:25:34 +01:00
function demande_prelevement ( $user )
{
dolibarr_syslog ( " Facture::demande_prelevement $this->statut $this->paye $this->mode_reglement_id " );
$soc = new Societe ( $this -> db );
$soc -> id = $this -> socidp ;
$soc -> rib ();
if ( $this -> statut > 0 && $this -> paye == 0 && $this -> mode_reglement_id == 3 )
{
$sql = 'SELECT count(*) FROM ' . MAIN_DB_PREFIX . 'prelevement_facture_demande' ;
$sql .= ' WHERE fk_facture=' . $this -> id ;
$sql .= ' AND traite = 0' ;
if ( $this -> db -> query ( $sql ) )
{
$row = $this -> db -> fetch_row ();
if ( $row [ 0 ] == 0 )
{
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'prelevement_facture_demande' ;
$sql .= ' (fk_facture, amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib)' ;
$sql .= ' VALUES (' . $this -> id ;
$sql .= " ,' " . price2num ( $this -> total_ttc ) . " ' " ;
$sql .= ',now(),' . $user -> id ;
$sql .= " ,' " . $soc -> bank_account -> code_banque . " ' " ;
$sql .= " ,' " . $soc -> bank_account -> code_guichet . " ' " ;
$sql .= " ,' " . $soc -> bank_account -> number . " ' " ;
$sql .= " ,' " . $soc -> bank_account -> cle_rib . " ') " ;
if ( $this -> db -> query ( $sql ) )
{
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
dolibarr_syslog ( 'Facture::DemandePrelevement Erreur' );
return - 1 ;
}
}
else
{
$this -> error = " Une demande existe d<> j<EFBFBD> " ;
dolibarr_syslog ( 'Facture::DemandePrelevement Impossible de cr<63> er une demande, demande d<> ja en cours' );
}
}
else
{
$this -> error = $this -> db -> error ();
dolibarr_syslog ( 'Facture::DemandePrelevement Erreur -2' );
return - 2 ;
}
}
else
{
$this -> error = " Etat facture incompatible avec l'action " ;
dolibarr_syslog ( " Facture::DemandePrelevement Etat facture incompatible $this->statut , $this->paye , $this->mode_reglement_id " );
return - 3 ;
}
}
/**
* \brief Supprime une demande de pr<EFBFBD> l<EFBFBD> vement
* \param user utilisateur cr<EFBFBD> ant la demande
* \param did id de la demande a supprimer
*/
function demande_prelevement_delete ( $user , $did )
{
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'prelevement_facture_demande' ;
$sql .= ' WHERE rowid = ' . $did ;
$sql .= ' AND traite = 0' ;
if ( $this -> db -> query ( $sql ) )
{
return 0 ;
}
else
{
dolibarr_syslog ( 'Facture::DemandePrelevement Erreur' );
return - 1 ;
}
}
/**
2006-06-03 14:38:57 +02:00
* \brief Stocke un num<EFBFBD> ro de rang pour toutes les lignes de
2006-02-09 13:25:34 +01:00
* detail d 'une facture qui n' en ont pas .
*/
function line_order ()
{
$sql = 'SELECT count(rowid) FROM ' . MAIN_DB_PREFIX . 'facturedet' ;
2006-06-03 14:38:57 +02:00
$sql .= ' WHERE fk_facture=' . $this -> id ;
$sql .= ' AND rang = 0' ;
2006-02-09 13:25:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
$nl = $row [ 0 ];
}
if ( $nl > 0 )
{
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . 'facturedet' ;
2006-06-03 14:38:57 +02:00
$sql .= ' WHERE fk_facture=' . $this -> id ;
$sql .= ' ORDER BY rang ASC, rowid ASC' ;
2006-02-09 13:25:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$row = $this -> db -> fetch_row ( $resql );
$li [ $i ] = $row [ 0 ];
$i ++ ;
}
}
for ( $i = 0 ; $i < sizeof ( $li ) ; $i ++ )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facturedet SET rang = ' . ( $i + 1 );
2006-06-03 14:38:57 +02:00
$sql .= ' WHERE rowid = ' . $li [ $i ];
2006-02-09 13:25:34 +01:00
if ( ! $this -> db -> query ( $sql ) )
{
dolibarr_syslog ( $this -> db -> error ());
}
}
}
}
function line_up ( $rowid )
{
$this -> line_order ();
/* Lecture du rang de la ligne */
$sql = 'SELECT rang FROM ' . MAIN_DB_PREFIX . 'facturedet' ;
2006-06-03 14:38:57 +02:00
$sql .= ' WHERE rowid =' . $rowid ;
2006-02-09 13:25:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
$rang = $row [ 0 ];
}
if ( $rang > 1 )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facturedet SET rang = ' . $rang ;
2006-06-03 14:38:57 +02:00
$sql .= ' WHERE fk_facture = ' . $this -> id ;
$sql .= ' AND rang = ' . ( $rang - 1 );
2006-02-09 13:25:34 +01:00
if ( $this -> db -> query ( $sql ) )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facturedet SET rang = ' . ( $rang - 1 );
2006-06-03 14:38:57 +02:00
$sql .= ' WHERE rowid = ' . $rowid ;
2006-02-09 13:25:34 +01:00
if ( ! $this -> db -> query ( $sql ) )
{
dolibarr_print_error ( $this -> db );
}
}
else
{
dolibarr_print_error ( $this -> db );
}
}
}
function line_down ( $rowid )
{
$this -> line_order ();
/* Lecture du rang de la ligne */
$sql = 'SELECT rang FROM ' . MAIN_DB_PREFIX . 'facturedet' ;
2006-06-03 14:38:57 +02:00
$sql .= ' WHERE rowid =' . $rowid ;
2006-02-09 13:25:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
$rang = $row [ 0 ];
}
/* Lecture du rang max de la facture */
$sql = 'SELECT max(rang) FROM ' . MAIN_DB_PREFIX . 'facturedet' ;
2006-06-03 14:38:57 +02:00
$sql .= ' WHERE fk_facture =' . $this -> id ;
2006-02-09 13:25:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$row = $this -> db -> fetch_row ( $resql );
$max = $row [ 0 ];
}
if ( $rang < $max )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facturedet SET rang = ' . $rang ;
2006-06-03 14:38:57 +02:00
$sql .= ' WHERE fk_facture = ' . $this -> id ;
$sql .= ' AND rang = ' . ( $rang + 1 );
2006-02-09 13:25:34 +01:00
if ( $this -> db -> query ( $sql ) )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facturedet SET rang = ' . ( $rang + 1 );
2006-06-03 14:38:57 +02:00
$sql .= ' WHERE rowid = ' . $rowid ;
2006-02-09 13:25:34 +01:00
if ( ! $this -> db -> query ( $sql ) )
{
dolibarr_print_error ( $this -> db );
}
}
else
{
dolibarr_print_error ( $this -> db );
}
}
}
/**
* \brief Charge indicateurs this -> nbtodo et this -> nbtodolate de tableau de bord
* \param user Objet user
* \return int < 0 si ko , > 0 si ok
*/
function load_board ( $user )
{
2006-03-31 18:26:48 +02:00
global $conf , $user ;
2006-02-09 13:25:34 +01:00
$this -> nbtodo = $this -> nbtodolate = 0 ;
$sql = 'SELECT f.rowid,' . $this -> db -> pdate ( 'f.date_lim_reglement' ) . ' as datefin' ;
2006-03-31 18:26:48 +02:00
if ( ! $user -> rights -> commercial -> client -> voir && ! $user -> societe_id ) $sql .= " , sc.fk_soc, sc.fk_user " ;
2006-02-09 13:25:34 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'facture as f' ;
2006-03-31 18:26:48 +02:00
if ( ! $user -> rights -> commercial -> client -> voir && ! $user -> societe_id ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2006-02-09 13:25:34 +01:00
$sql .= ' WHERE f.paye=0 AND f.fk_statut = 1' ;
2006-03-31 18:26:48 +02:00
if ( $user -> societe_id ) $sql .= ' AND f.fk_soc = ' . $user -> societe_id ;
if ( ! $user -> rights -> commercial -> client -> voir && ! $user -> societe_id ) $sql .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
2006-02-09 13:25:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$this -> nbtodo ++ ;
if ( $obj -> datefin < ( time () - $conf -> facture -> client -> warning_delay )) $this -> nbtodolate ++ ;
}
return 1 ;
}
else
{
dolibarr_print_error ( $this -> db );
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2006-06-18 16:18:41 +02:00
/* gestion des contacts d'une facture */
2006-02-09 13:25:34 +01:00
/**
* \brief Retourne id des contacts clients de facturation
* \return array Liste des id contacts facturation
2006-06-13 00:20:24 +02:00
*/
2006-02-09 13:25:34 +01:00
function getIdBillingContact ()
{
return $this -> getIdContact ( 'external' , 'BILLING' );
}
/**
* \brief Retourne id des contacts clients de livraison
* \return array Liste des id contacts livraison
2006-06-13 00:20:24 +02:00
*/
2006-02-09 13:25:34 +01:00
function getIdShippingContact ()
{
return $this -> getIdContact ( 'external' , 'SHIPPING' );
}
2006-06-11 02:36:17 +02:00
/**
* \brief Initialise la facture avec valeurs fictives al<EFBFBD> atoire
* Sert <EFBFBD> g<EFBFBD> n<EFBFBD> rer une facture pour l ' aperu des mod<EFBFBD> les ou demo
*/
function initAsSpecimen ()
{
global $user , $langs ;
// Charge tableau des id de soci<63> t<EFBFBD> socids
$socids = array ();
$sql = " SELECT idp FROM " . MAIN_DB_PREFIX . " societe WHERE client=1 LIMIT 10 " ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num_socs = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num_socs )
{
$i ++ ;
2006-06-13 00:20:24 +02:00
2006-06-11 02:36:17 +02:00
$row = $this -> db -> fetch_row ( $resql );
$socids [ $i ] = $row [ 0 ];
}
}
2006-06-13 00:20:24 +02:00
2006-06-11 02:36:17 +02:00
// Charge tableau des produits prodids
$prodids = array ();
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " product WHERE envente=1 " ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num_prods = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num_prods )
{
$i ++ ;
$row = $this -> db -> fetch_row ( $resql );
$prodids [ $i ] = $row [ 0 ];
}
}
2006-06-13 00:20:24 +02:00
2006-06-11 02:36:17 +02:00
// Initialise param<61> tres
$this -> id = 0 ;
$this -> ref = 'SPECIMEN' ;
$this -> specimen = 1 ;
$socid = rand ( 1 , $num_socs );
$this -> socidp = $socids [ $socid ];
$this -> date = time ();
$this -> date_lim_reglement = $this -> date + 3600 * 24 * 30 ;
2006-06-12 22:25:47 +02:00
$this -> cond_reglement_code = 'RECEP' ;
$this -> mode_reglement_code = 'CHQ' ;
2006-06-11 02:36:17 +02:00
$this -> note_public = 'SPECIMEN' ;
$nbp = rand ( 1 , 9 );
$xnbp = 0 ;
while ( $xnbp < $nbp )
{
$ligne = new FactureLigne ( $this -> db );
$ligne -> desc = $langs -> trans ( " Description " ) . " " . $xnbp ;
$ligne -> qty = 1 ;
$ligne -> subprice = 100 ;
$ligne -> price = 100 ;
$ligne -> tva_taux = 19.6 ;
$prodid = rand ( 1 , $num_prods );
$ligne -> produit_id = $prodids [ $prodid ];
$this -> lignes [ $xnbp ] = $ligne ;
$xnbp ++ ;
}
2006-06-13 00:20:24 +02:00
2006-06-11 02:36:17 +02:00
$this -> amount_ht = $xnbp * 100 ;
$this -> total_ht = $xnbp * 100 ;
$this -> total_tva = $xnbp * 19.6 ;
$this -> total_ttc = $xnbp * 119.6 ;
}
2006-02-09 13:25:34 +01:00
}
/**
\class FactureLigne
\brief Classe permettant la gestion des lignes de factures
2006-06-15 02:50:42 +02:00
\remarks Gere des lignes de la table llx_facturedet
2006-02-09 13:25:34 +01:00
*/
class FactureLigne
{
2006-06-15 02:50:42 +02:00
var $db ;
2006-06-17 18:52:30 +02:00
var $error ;
2006-06-15 02:50:42 +02:00
2006-06-17 18:52:30 +02:00
// From llx_facturedet
2006-06-15 02:50:42 +02:00
var $rowid ;
2006-06-17 18:52:30 +02:00
var $desc ; // Description ligne
2006-06-23 21:05:33 +02:00
var $fk_facture ; // Id produit pr<70> d<EFBFBD> fini
2006-05-21 02:04:52 +02:00
var $qty ; // Quantit<69> (exemple 2)
var $subprice ; // P.U. HT (exemple 100)
2006-06-17 18:52:30 +02:00
var $remise ; // Montant calcul<75> de la remise % sur PU HT (exemple 20)
// subprice = price + remise
2006-05-21 02:04:52 +02:00
var $remise_percent ; // % de la remise ligne (exemple 20%)
var $price ; // P.U. HT apres remise % de ligne (exemple 80)
var $tva_taux ; // Taux tva produit/service (exemple 19.6)
2006-06-17 18:52:30 +02:00
var $fk_code_ventilation = 0 ;
var $fk_export_compta = 0 ;
var $rang = 0 ;
2006-05-21 02:04:52 +02:00
2006-02-09 13:25:34 +01:00
var $date_start ;
var $date_end ;
2006-08-09 01:22:56 +02:00
var $info_bits = 0 ; // Bit 0: 0 si TVA normal - 1 si TVA NPR
// Bit 1: 0 si ligne normal - 1 si bit discount
2006-06-15 02:50:42 +02:00
var $total_ht ; // Total HT de la ligne toute quantit<69> et incluant la remise ligne
var $total_tva ; // Total TVA de la ligne toute quantit<69> et incluant la remise ligne
var $total_ttc ; // Total TTC de la ligne toute quantit<69> et incluant la remise ligne
2006-02-09 13:25:34 +01:00
2006-06-17 18:52:30 +02:00
// From llx_product
var $ref ; // Reference produit
var $libelle ; // Label produit
var $product_desc ; // Description produit
2006-06-15 02:50:42 +02:00
2006-02-09 13:25:34 +01:00
/**
* \brief Constructeur d ' objets ligne de facture
* \param DB handler d ' acc<EFBFBD> s base de donn<EFBFBD> e
*/
function FactureLigne ( $DB )
{
$this -> db = $DB ;
}
/**
* \brief Recup<EFBFBD> re l ' objet ligne de facture
* \param rowid id de la ligne de facture
*/
2006-06-17 16:13:49 +02:00
function fetch ( $rowid )
2006-02-09 13:25:34 +01:00
{
2006-06-17 18:52:30 +02:00
$sql = 'SELECT fd.rowid, fd.fk_facture, fd.fk_product, fd.description, fd.price, fd.qty, fd.tva_taux,' ;
$sql .= ' fd.remise, fd.remise_percent, fd.fk_remise_except, fd.subprice,' ;
$sql .= ' ' . $this -> db -> pdate ( 'fd.date_start' ) . ' as date_start,' . $this -> db -> pdate ( 'fd.date_end' ) . ' as date_end,' ;
$sql .= ' fd.info_bits, fd.total_ht, fd.total_tva, fd.total_ttc, fd.rang,' ;
$sql .= ' fd.fk_code_ventilation, fd.fk_export_compta,' ;
$sql .= ' p.ref as product_ref, p.label as product_libelle, p.description as product_desc' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'facturedet as fd' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON fd.fk_product = p.rowid' ;
$sql .= ' WHERE fd.rowid = ' . $rowid ;
2006-02-09 13:25:34 +01:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$objp = $this -> db -> fetch_object ( $result );
2006-06-17 20:12:46 +02:00
$this -> rowid = $objp -> rowid ;
2006-06-17 16:13:49 +02:00
$this -> fk_facture = $objp -> fk_facture ;
$this -> desc = $objp -> description ;
2006-02-09 13:25:34 +01:00
$this -> qty = $objp -> qty ;
$this -> price = $objp -> price ;
$this -> subprice = $objp -> subprice ;
$this -> tva_taux = $objp -> tva_taux ;
$this -> remise = $objp -> remise ;
$this -> remise_percent = $objp -> remise_percent ;
2006-06-17 16:13:49 +02:00
$this -> fk_remise_except = $objp -> fk_remise_except ;
2006-02-09 13:25:34 +01:00
$this -> produit_id = $objp -> fk_product ;
$this -> date_start = $objp -> date_start ;
$this -> date_end = $objp -> date_end ;
2006-04-04 02:12:45 +02:00
$this -> info_bits = $objp -> info_bits ;
2006-06-15 02:50:42 +02:00
$this -> total_ht = $objp -> total_ht ;
$this -> total_tva = $objp -> total_tva ;
$this -> total_ttc = $objp -> total_ttc ;
2006-06-17 16:13:49 +02:00
$this -> fk_code_ventilation = $objp -> fk_code_ventilation ;
$this -> fk_export_compta = $objp -> fk_export_compta ;
$this -> rang = $objp -> rang ;
2006-06-17 18:52:30 +02:00
$this -> ref = $objp -> product_ref ;
$this -> libelle = $objp -> product_libelle ;
$this -> product_desc = $objp -> product_desc ;
2006-02-09 13:25:34 +01:00
$this -> db -> free ( $result );
}
else
{
dolibarr_print_error ( $this -> db );
}
}
2006-06-17 18:52:30 +02:00
/**
* \brief Ins<EFBFBD> re l ' objet ligne de facture en base
* \return int < 0 si ko , > 0 si ok
*/
function insert ()
{
2006-07-01 01:38:12 +02:00
dolibarr_syslog ( " FactureLigne.class::insert rang= " . $this -> rang );
2006-06-17 18:52:30 +02:00
$this -> db -> begin ();
2006-07-01 01:38:12 +02:00
$rangtouse = $this -> rang ;
if ( $rangtouse == - 1 )
{
// R<> cup<75> re rang max de la facture dans $rangmax
$sql = 'SELECT max(rang) as max FROM ' . MAIN_DB_PREFIX . 'facturedet' ;
$sql .= ' WHERE fk_facture =' . $this -> fk_facture ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$rangtouse = $obj -> max + 1 ;
}
else
{
dolibarr_print_error ( $this -> db );
$this -> db -> rollback ();
return - 1 ;
}
}
2006-06-17 18:52:30 +02:00
// Insertion dans base de la ligne
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'facturedet' ;
$sql .= ' (fk_facture, description, price, qty, tva_taux,' ;
$sql .= ' fk_product, remise_percent, subprice, remise, fk_remise_except,' ;
$sql .= ' date_start, date_end, fk_code_ventilation, fk_export_compta, ' ;
$sql .= ' rang,' ;
$sql .= ' info_bits, total_ht, total_tva, total_ttc)' ;
$sql .= " VALUES ( " . $this -> fk_facture . " , " ;
$sql .= " ' " . addslashes ( $this -> desc ) . " ', " ;
$sql .= " ' " . price2num ( $this -> price ) . " ', " ;
$sql .= " ' " . price2num ( $this -> qty ) . " ', " ;
$sql .= " ' " . price2num ( $this -> txtva ) . " ', " ;
if ( $this -> fk_product ) { $sql .= " ' " . $this -> fk_product . " ', " ; }
2006-06-30 01:48:44 +02:00
else { $sql .= 'null,' ; }
2006-06-17 18:52:30 +02:00
$sql .= " ' " . price2num ( $this -> remise_percent ) . " ', " ;
$sql .= " ' " . price2num ( $this -> subprice ) . " ', " ;
$sql .= " ' " . price2num ( $this -> remise ) . " ', " ;
if ( $this -> fk_remise_except ) $sql .= $this -> fk_remise_except . " , " ;
else $sql .= 'null,' ;
2006-06-17 20:12:46 +02:00
if ( $this -> date_start ) { $sql .= " ' " . $this -> date_start . " ', " ; }
2006-06-17 18:52:30 +02:00
else { $sql .= 'null,' ; }
2006-06-17 20:12:46 +02:00
if ( $this -> date_end ) { $sql .= " ' " . $this -> date_end . " ', " ; }
2006-06-17 18:52:30 +02:00
else { $sql .= 'null,' ; }
$sql .= ' ' . $this -> fk_code_ventilation . ',' ;
$sql .= ' ' . $this -> fk_export_compta . ',' ;
2006-07-01 01:38:12 +02:00
$sql .= ' ' . $rangtouse . ',' ;
2006-06-17 18:52:30 +02:00
$sql .= " ' " . $this -> info_bits . " ', " ;
$sql .= " ' " . price2num ( $this -> total_ht ) . " ', " ;
$sql .= " ' " . price2num ( $this -> total_tva ) . " ', " ;
$sql .= " ' " . price2num ( $this -> total_ttc ) . " ' " ;
$sql .= ')' ;
2006-07-01 01:38:12 +02:00
dolibarr_syslog ( " FactureLigne.class::insert sql= $sql " );
2006-06-17 18:52:30 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
dolibarr_syslog ( " FactureLigne::insert Error " . $this -> error );
$this -> db -> rollback ();
return - 2 ;
}
}
/**
* \brief Mise a jour de l ' objet ligne de facture en base
* \return int < 0 si ko , > 0 si ok
*/
function update ()
{
$this -> db -> begin ();
// Mise a jour ligne en base
$sql = " UPDATE " . MAIN_DB_PREFIX . " facturedet SET " ;
$sql .= " description=' " . addslashes ( $this -> desc ) . " ' " ;
$sql .= " ,price=' " . price2num ( $this -> price ) . " ' " ;
$sql .= " ,subprice=' " . price2num ( $this -> subprice ) . " ' " ;
$sql .= " ,remise=' " . price2num ( $this -> remise ) . " ' " ;
$sql .= " ,remise_percent=' " . price2num ( $this -> remise_percent ) . " ' " ;
2006-06-17 20:12:46 +02:00
if ( $this -> fk_remise_except ) $sql .= " ,fk_remise_except= " . $this -> fk_remise_except ;
2006-06-17 18:52:30 +02:00
else $sql .= " ,fk_remise_except=null " ;
2006-06-17 20:12:46 +02:00
$sql .= " ,tva_taux=' " . price2num ( $this -> tva_taux ) . " ' " ;
2006-06-17 18:52:30 +02:00
$sql .= " ,qty=' " . price2num ( $this -> qty ) . " ' " ;
2006-06-17 20:12:46 +02:00
if ( $this -> date_start ) { $sql .= " ,date_start=' " . $this -> date_start . " ' " ; }
2006-06-17 18:52:30 +02:00
else { $sql .= ',date_start=null' ; }
2006-06-17 20:12:46 +02:00
if ( $this -> date_end ) { $sql .= " ,date_end=' " . $this -> date_end . " ' " ; }
2006-06-17 18:52:30 +02:00
else { $sql .= ',date_end=null' ; }
$sql .= " ,rang=' " . $this -> rang . " ' " ;
$sql .= " ,info_bits=' " . $this -> info_bits . " ' " ;
$sql .= " ,total_ht=' " . price2num ( $this -> total_ht ) . " ' " ;
$sql .= " ,total_tva=' " . price2num ( $this -> total_tva ) . " ' " ;
$sql .= " ,total_ttc=' " . price2num ( $this -> total_ttc ) . " ' " ;
$sql .= " WHERE rowid = " . $this -> rowid ;
dolibarr_syslog ( " FactureLigne::update sql= $sql " );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
dolibarr_syslog ( " FactureLigne::update Error " . $this -> error );
$this -> db -> rollback ();
return - 2 ;
}
}
2006-06-23 21:05:33 +02:00
/**
* \brief Mise a jour en base des champs total_xxx de ligne de facture
* \return int < 0 si ko , > 0 si ok
*/
function update_total ()
{
$this -> db -> begin ();
// Mise a jour ligne en base
$sql = " UPDATE " . MAIN_DB_PREFIX . " facturedet SET " ;
$sql .= " total_ht=' " . price2num ( $this -> total_ht ) . " ' " ;
$sql .= " ,total_tva=' " . price2num ( $this -> total_tva ) . " ' " ;
$sql .= " ,total_ttc=' " . price2num ( $this -> total_ttc ) . " ' " ;
$sql .= " WHERE rowid = " . $this -> rowid ;
dolibarr_syslog ( " FactureLigne::update_total sql= $sql " );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
dolibarr_syslog ( " FactureLigne::update_total Error " . $this -> error );
$this -> db -> rollback ();
return - 2 ;
}
}
2006-02-09 13:25:34 +01:00
}
?>