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>
|
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-04-13 16:47:05 +02:00
|
|
|
|
class FactureLigne {
|
|
|
|
|
|
Function FactureLigne()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-09-25 20:36:27 +02:00
|
|
|
|
class Facture {
|
|
|
|
|
|
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;
|
2002-09-25 20:36:27 +02:00
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* Initialisation
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2003-02-01 22:13:19 +01:00
|
|
|
|
Function Facture($DB, $soc_idp="", $facid="") {
|
2002-09-25 20:36:27 +02:00
|
|
|
|
$this->db = $DB ;
|
|
|
|
|
|
$this->socidp = $soc_idp;
|
|
|
|
|
|
$this->products = array();
|
|
|
|
|
|
$this->db_table = "llx_facture";
|
|
|
|
|
|
$this->amount = 0;
|
|
|
|
|
|
$this->remise = 0;
|
|
|
|
|
|
$this->tva = 0;
|
|
|
|
|
|
$this->total = 0;
|
2002-12-14 17:25:46 +01:00
|
|
|
|
$this->propalid = 0;
|
2003-02-01 22:13:19 +01:00
|
|
|
|
$this->id = $facid;
|
2002-09-25 20:36:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2002-12-14 17:25:46 +01:00
|
|
|
|
Function create($userid) {
|
2002-09-25 20:36:27 +02:00
|
|
|
|
/*
|
|
|
|
|
|
* Insertion dans la base
|
|
|
|
|
|
*/
|
|
|
|
|
|
$socid = $this->socidp;
|
|
|
|
|
|
$number = $this->number;
|
|
|
|
|
|
$amount = $this->amount;
|
|
|
|
|
|
$remise = $this->remise;
|
|
|
|
|
|
|
2002-12-14 17:25:46 +01:00
|
|
|
|
if (! $remise) {
|
|
|
|
|
|
$remise = 0 ;
|
|
|
|
|
|
}
|
2002-09-25 20:36:27 +02:00
|
|
|
|
|
|
|
|
|
|
$totalht = ($amount - $remise);
|
|
|
|
|
|
$tva = tva($totalht);
|
|
|
|
|
|
$total = $totalht + $tva;
|
|
|
|
|
|
|
2002-12-14 17:25:46 +01:00
|
|
|
|
$sql = "INSERT INTO $this->db_table (facnumber, fk_soc, datec, amount, remise, tva, total, datef, note, fk_user_author) ";
|
|
|
|
|
|
$sql .= " VALUES ('$number', $socid, now(), $totalht, $remise, $tva, $total, $this->date,'$note',$userid);";
|
2002-09-25 20:36:27 +02:00
|
|
|
|
|
2002-12-14 17:25:46 +01:00
|
|
|
|
if ( $this->db->query($sql) )
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->id = $this->db->last_insert_id();
|
2002-09-25 20:36:27 +02:00
|
|
|
|
|
2003-04-13 21:29:42 +02:00
|
|
|
|
return $this->id;
|
2002-12-14 17:25:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
print $this->db->error() . '<b><br>'.$sql;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2003-04-13 21:29:42 +02:00
|
|
|
|
|
2002-09-25 20:36:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2003-02-01 20:45:45 +01:00
|
|
|
|
Function fetch($rowid)
|
|
|
|
|
|
{
|
2002-09-25 20:36:27 +02:00
|
|
|
|
|
2003-04-13 16:47:05 +02:00
|
|
|
|
$sql = "SELECT fk_soc,facnumber,amount,tva,total,remise,".$this->db->pdate(datef)."as df FROM llx_facture WHERE rowid=$rowid;";
|
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-04-13 16:47:05 +02:00
|
|
|
|
$this->id = $rowid;
|
|
|
|
|
|
$this->datep = $obj->dp;
|
|
|
|
|
|
$this->date = $obj->df;
|
|
|
|
|
|
$this->ref = $obj->facnumber;
|
|
|
|
|
|
$this->total_ht = $obj->amount;
|
|
|
|
|
|
$this->total_tva = $obj->tva;
|
|
|
|
|
|
$this->total_ttc = $obj->total;
|
|
|
|
|
|
$this->remise = $obj->remise;
|
|
|
|
|
|
$this->socidp = $obj->fk_soc;
|
|
|
|
|
|
$this->lignes = array();
|
2003-02-01 20:45:45 +01:00
|
|
|
|
$this->db->free();
|
2003-04-13 16:47:05 +02:00
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* Lignes
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
$sql = "SELECT l.description, l.price, l.qty, l.rowid, l.tva_taux";
|
|
|
|
|
|
$sql .= " FROM llx_facturedet as l WHERE l.fk_facture = ".$this->id;
|
|
|
|
|
|
|
|
|
|
|
|
$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();
|
|
|
|
|
|
$faclig->desc = stripslashes($objp->description);
|
|
|
|
|
|
$faclig->qty = $objp->qty;
|
|
|
|
|
|
$faclig->price = price($objp->price);
|
|
|
|
|
|
$faclig->tva_taux = $objp->tva_taux;
|
|
|
|
|
|
$this->lignes[$i] = $faclig;
|
|
|
|
|
|
$i++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$this->db->free();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
print $this->db->error();
|
|
|
|
|
|
}
|
2003-02-01 20:45:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
print $this->db->error();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2003-04-13 16:47:05 +02:00
|
|
|
|
/*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
Function fetch_client()
|
|
|
|
|
|
{
|
|
|
|
|
|
$client = new Societe($this->db);
|
|
|
|
|
|
$client->fetch($this->socidp);
|
|
|
|
|
|
$this->client = $client;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2002-09-25 20:36:27 +02:00
|
|
|
|
/*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2003-02-01 20:45:45 +01:00
|
|
|
|
Function valid($userid, $dir)
|
|
|
|
|
|
{
|
|
|
|
|
|
$sql = "UPDATE llx_facture SET fk_statut = 1, date_valid=now(), fk_user_valid=$userid";
|
|
|
|
|
|
$sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;";
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->db->query($sql) )
|
|
|
|
|
|
{
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
print $this->db->error() . ' in ' . $sql;
|
|
|
|
|
|
}
|
2002-09-25 20:36:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2002-12-14 17:25:46 +01:00
|
|
|
|
/*
|
|
|
|
|
|
* Suppression de la facture
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
Function delete($rowid)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
$sql = "DELETE FROM llx_facture WHERE rowid = $rowid AND fk_statut = 0;";
|
|
|
|
|
|
|
|
|
|
|
|
if ( $this->db->query( $sql) )
|
|
|
|
|
|
{
|
|
|
|
|
|
if ( $this->db->affected_rows() )
|
|
|
|
|
|
{
|
|
|
|
|
|
$sql = "DELETE FROM llx_fa_pr WHERE fk_facture = $rowid;";
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->db->query( $sql) )
|
|
|
|
|
|
{
|
2003-02-01 20:45:45 +01:00
|
|
|
|
|
|
|
|
|
|
$sql = "DELETE FROM llx_facturedet WHERE fk_facture = $rowid;";
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->db->query( $sql) )
|
|
|
|
|
|
{
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
print "Err : ".$this->db->error();
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2002-12-14 17:25:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
print "Err : ".$this->db->error();
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
print "Err : ".$this->db->error();
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2002-12-19 19:59:23 +01:00
|
|
|
|
/*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2002-12-14 17:25:46 +01:00
|
|
|
|
Function set_payed($rowid)
|
|
|
|
|
|
{
|
|
|
|
|
|
$sql = "UPDATE llx_facture set paye = 1 WHERE rowid = $rowid ;";
|
|
|
|
|
|
$return = $this->db->query( $sql);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Function set_valid($rowid, $userid)
|
|
|
|
|
|
{
|
2003-02-01 20:45:45 +01:00
|
|
|
|
global $conf;
|
|
|
|
|
|
|
2002-12-14 17:25:46 +01:00
|
|
|
|
$sql = "UPDATE llx_facture set fk_statut = 1, fk_user_valid = $userid WHERE rowid = $rowid ;";
|
|
|
|
|
|
$result = $this->db->query( $sql);
|
2003-02-01 20:45:45 +01:00
|
|
|
|
|
|
|
|
|
|
$dir = $conf->facture->outputdir . "/" . $rowid;
|
|
|
|
|
|
|
|
|
|
|
|
if (! is_dir ("$dir"))
|
|
|
|
|
|
{
|
2003-04-13 17:04:51 +02:00
|
|
|
|
if (! mkdir ("$dir", 755))
|
2003-02-01 20:45:45 +01:00
|
|
|
|
{
|
|
|
|
|
|
print $dir;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2003-04-13 16:47:05 +02:00
|
|
|
|
return $result;
|
2003-02-01 20:45:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
Function addline($facid, $desc, $pu, $qty)
|
|
|
|
|
|
{
|
|
|
|
|
|
$sql = "INSERT INTO llx_facturedet (fk_facture,description,price,qty) VALUES ($facid, '$desc', $pu, $qty) ;";
|
|
|
|
|
|
$result = $this->db->query( $sql);
|
|
|
|
|
|
|
|
|
|
|
|
$this->updateprice($facid);
|
|
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2003-02-01 22:13:19 +01:00
|
|
|
|
Function updateline($rowid, $desc, $pu, $qty)
|
|
|
|
|
|
{
|
|
|
|
|
|
$sql = "UPDATE llx_facturedet set description='$desc',price=$pu,qty=$qty WHERE rowid = $rowid ;";
|
|
|
|
|
|
$result = $this->db->query( $sql);
|
|
|
|
|
|
|
|
|
|
|
|
$this->updateprice($this->id);
|
|
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2003-02-01 20:45:45 +01:00
|
|
|
|
Function deleteline($rowid)
|
|
|
|
|
|
{
|
|
|
|
|
|
$sql = "DELETE FROM llx_facturedet WHERE rowid = $rowid;";
|
|
|
|
|
|
$result = $this->db->query( $sql);
|
|
|
|
|
|
|
|
|
|
|
|
$this->updateprice($this->id);
|
|
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
Function updateprice($facid)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
$sql = "SELECT sum(price*qty) FROM llx_facturedet WHERE fk_facture = $facid;";
|
|
|
|
|
|
|
|
|
|
|
|
$result = $this->db->query($sql);
|
|
|
|
|
|
|
|
|
|
|
|
if ($result)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($this->db->num_rows() )
|
|
|
|
|
|
{
|
|
|
|
|
|
$row = $this->db->fetch_row();
|
|
|
|
|
|
$totalht = $row[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$tva = tva($totalht);
|
|
|
|
|
|
$total = $totalht + $tva;
|
|
|
|
|
|
|
|
|
|
|
|
$sql = "UPDATE llx_facture SET amount = $totalht, tva=$tva, total=$total";
|
|
|
|
|
|
$sql .= " WHERE rowid = $facid ;";
|
|
|
|
|
|
|
|
|
|
|
|
$result = $this->db->query($sql);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2002-12-14 17:25:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
*
|
|
|
|
|
|
* G<EFBFBD>n<EFBFBD>ration du PDF
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
Function pdf()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print "<hr><b>G<>n<EFBFBD>ration du PDF</b><p>";
|
|
|
|
|
|
|
|
|
|
|
|
$command = "export DBI_DSN=\"".$GLOBALS["DBI"]."\" ";
|
|
|
|
|
|
$command .= " ; ../../scripts/facture-tex.pl --facture=$facid --pdf --ps" ;
|
|
|
|
|
|
|
|
|
|
|
|
$output = system($command);
|
|
|
|
|
|
print "<p>command : $command<br>";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-02-01 20:45:45 +01:00
|
|
|
|
}
|
2002-09-25 20:36:27 +02:00
|
|
|
|
?>
|