Gestion de l'odonnancement des lignes

This commit is contained in:
Rodolphe Quiedeville 2005-04-22 15:14:42 +00:00
parent 4f8d294437
commit da2149cbe5

View File

@ -124,28 +124,6 @@ class Facture
$datelim=mktime(0,0,0,$mois,$fins[$mois-1],$annee); $datelim=mktime(0,0,0,$mois,$fins[$mois-1],$annee);
} }
/*
* Lecture de la remise exceptionnelle
*
*/
$sql = "SELECT rowid, rc.amount_ht, fk_soc, fk_user";
$sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
$sql .= " WHERE rc.fk_soc =". $this->socidp;
$sql .= " AND fk_facture IS NULL";
$resql = $this->db->query($sql) ;
if ( $resql)
{
$nurmx = $this->db->num_rows($resql);
if ($nurmx > 0)
{
$row = $this->db->fetch_row($resql);
$this->remise_exceptionnelle = $row;
}
$this->db->free($resql);
}
/* /*
* Insertion dans la base * Insertion dans la base
*/ */
@ -261,13 +239,6 @@ class Facture
$this->updateprice($this->id); $this->updateprice($this->id);
/*
* Affectation de la remise exceptionnelle
*/
$this->_affect_remise_exceptionnelle();
$this->updateprice($this->id);
return $this->id; return $this->id;
} }
else else
@ -283,6 +254,8 @@ class Facture
function _affect_remise_exceptionnelle() function _affect_remise_exceptionnelle()
{ {
$error = 0;
if ($this->remise_exceptionnelle[1] > 0) if ($this->remise_exceptionnelle[1] > 0)
{ {
if ($this->remise_exceptionnelle[1] > ($this->total_ht * 0.9)) if ($this->remise_exceptionnelle[1] > ($this->total_ht * 0.9))
@ -303,7 +276,11 @@ class Facture
$sql .= " ,amount_ht = '".ereg_replace(",",".",$remise)."'"; $sql .= " ,amount_ht = '".ereg_replace(",",".",$remise)."'";
$sql .= " WHERE rowid =".$this->remise_exceptionnelle[0]; $sql .= " WHERE rowid =".$this->remise_exceptionnelle[0];
$sql .= " AND fk_soc =". $this->socidp; $sql .= " AND fk_soc =". $this->socidp;
$this->db->query( $sql) ;
if (! $this->db->query( $sql))
{
$error++;
}
$sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise_except"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise_except";
@ -315,7 +292,10 @@ class Facture
$sql .= " ,".$this->remise_exceptionnelle[3]; $sql .= " ,".$this->remise_exceptionnelle[3];
$sql .= ")"; $sql .= ")";
$this->db->query( $sql) ; if (! $this->db->query( $sql) )
{
$error++;
}
} }
else else
@ -332,13 +312,18 @@ class Facture
$sql .= " SET fk_facture = ".$this->id; $sql .= " SET fk_facture = ".$this->id;
$sql .= " WHERE rowid =".$this->remise_exceptionnelle[0]; $sql .= " WHERE rowid =".$this->remise_exceptionnelle[0];
$sql .= " AND fk_soc =". $this->socidp; $sql .= " AND fk_soc =". $this->socidp;
$this->db->query( $sql) ;
if (! $this->db->query( $sql) )
{
$error++;
} }
} }
} }
return $error;
}
/** /**
* \brief Recupére l'objet facture et ses lignes de factures * \brief Recupére l'objet facture et ses lignes de factures
* \param rowid id de la facture a récupérer * \param rowid id de la facture a récupérer
@ -472,17 +457,81 @@ class Facture
*/ */
function valid($userid) function valid($userid)
{ {
$sql = "UPDATE ".MAIN_DB_PREFIX."facture SET fk_statut = 1, date_valid=now(), fk_user_valid=$userid"; $error = 0;
$sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; if ($this->db->begin())
if ($this->db->query($sql) )
{ {
return 1; /*
* Lecture de la remise exceptionnelle
*
*/
$sql = "SELECT rowid, rc.amount_ht, fk_soc, fk_user";
$sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
$sql .= " WHERE rc.fk_soc =". $this->socidp;
$sql .= " AND fk_facture IS NULL";
$resql = $this->db->query($sql) ;
if ( $resql)
{
$nurmx = $this->db->num_rows($resql);
if ($nurmx > 0)
{
$row = $this->db->fetch_row($resql);
$this->remise_exceptionnelle = $row;
}
$this->db->free($resql);
} }
else else
{ {
dolibarr_print_error($this->db); dolibarr_syslog("Facture::Valide Erreur lecture Remise");
$error++;
}
/*
* Affectation de la remise exceptionnelle
*/
if ( $this->_affect_remise_exceptionnelle() <> 0)
{
$error++;
}
else
{
$this->updateprice($this->id);
$sql = "UPDATE ".MAIN_DB_PREFIX."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) )
{
$error++;
dolibarr_syslog("Facture::Valide Erreur ");
}
}
if ($error == 0)
{
$this->db->commit();
}
else
{
$this->db->rollback();
}
}
else
{
$error++;
}
if ($error > 0)
{
return 0;
}
else
{
return 1;
} }
} }
@ -620,6 +669,7 @@ class Facture
function set_valid($rowid, $user, $soc, $force_number='') function set_valid($rowid, $user, $soc, $force_number='')
{ {
global $conf; global $conf;
$error = 0;
if ($this->brouillon) if ($this->brouillon)
{ {
@ -634,9 +684,50 @@ class Facture
$numfa = facture_get_num($soc, $this->prefixe_facture); // définit dans includes/modules/facture $numfa = facture_get_num($soc, $this->prefixe_facture); // définit dans includes/modules/facture
} }
if ($this->db->begin())
{
/*
* Lecture de la remise exceptionnelle
*
*/
$sql = "SELECT rowid, rc.amount_ht, fk_soc, fk_user";
$sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
$sql .= " WHERE rc.fk_soc =". $this->socidp;
$sql .= " AND fk_facture IS NULL";
$resql = $this->db->query($sql) ;
$sql = "UPDATE ".MAIN_DB_PREFIX."facture set facnumber='$numfa', fk_statut = 1, fk_user_valid = $user->id"; if ( $resql)
{
$nurmx = $this->db->num_rows($resql);
if ($nurmx > 0)
{
$row = $this->db->fetch_row($resql);
$this->remise_exceptionnelle = $row;
}
$this->db->free($resql);
}
else
{
dolibarr_syslog("Facture::Valide Erreur lecture Remise");
$error++;
}
/* Affectation de la remise exceptionnelle */
if ( $this->_affect_remise_exceptionnelle() <> 0)
{
$error++;
}
else
{
$this->updateprice($this->id);
}
/* Validation de la facture */
$sql = "UPDATE ".MAIN_DB_PREFIX."facture ";
$sql .= " SET facnumber='$numfa', fk_statut = 1, fk_user_valid = $user->id";
/* Si l'option est activée on force la date de facture */ /* Si l'option est activée on force la date de facture */
@ -652,6 +743,7 @@ class Facture
{ {
dolibarr_syslog("Facture::set_valid() - 10"); dolibarr_syslog("Facture::set_valid() - 10");
dolibarr_print_error($this->db); dolibarr_print_error($this->db);
$error++;
} }
/* /*
@ -704,8 +796,29 @@ class Facture
$i++; $i++;
} }
} }
else
{
$error++;
}
return $result; if ($error == 0)
{
$this->db->commit();
}
else
{
$this->db->rollback();
}
}
if ($error == 0)
{
return 1;
}
else
{
return 0;
}
} }
} }
@ -1354,6 +1467,151 @@ class Facture
/* /*
* *
*/ */
/**
* \brief Classe la facture
* \param cat_id id de la catégorie dans laquelle classer la facture
*
*/
function line_order()
{
$sql = "SELECT count(rowid) FROM ".MAIN_DB_PREFIX."facturedet";
$sql .= " WHERE fk_facture=".$this->id;
$sql .= " AND rang = 0";
$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";
$sql .= " WHERE fk_facture=".$this->id;
$sql .= " ORDER BY rang ASC, rowid ASC";
$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);
$sql .= " WHERE rowid = ".$li[$i];
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";
$sql .= " WHERE rowid =".$rowid;
$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 ;
$sql .= " WHERE fk_facture = ".$this->id;
$sql .= " AND rang = ".($rang - 1);
if ($this->db->query($sql) )
{
$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet SET rang = ".($rang - 1);
$sql .= " WHERE rowid = ".$rowid;
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";
$sql .= " WHERE rowid =".$rowid;
$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";
$sql .= " WHERE fk_facture =".$this->id;
$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;
$sql .= " WHERE fk_facture = ".$this->id;
$sql .= " AND rang = ".($rang+1);
if ($this->db->query($sql) )
{
$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet SET rang = ".($rang+1);
$sql .= " WHERE rowid = ".$rowid;
if (! $this->db->query($sql) )
{
dolibarr_print_error($this->db);
}
}
else
{
dolibarr_print_error($this->db);
}
}
}
} }