Qual: Début uniformisation code commandes fournisseurs avec commandes clients.

This commit is contained in:
Laurent Destailleur 2006-12-05 00:47:40 +00:00
parent 5ed51c4dfc
commit 6b0465c239
6 changed files with 2965 additions and 2477 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -49,7 +49,7 @@ if ($_GET["id"] > 0)
$soc = new Societe($db); $soc = new Societe($db);
$commande = new CommandeFournisseur($db); $commande = new CommandeFournisseur($db);
if ( $commande->fetch($_GET["id"]) == 0) if ( $commande->fetch($_GET["id"]) >= 0)
{ {
$soc->fetch($commande->socid); $soc->fetch($commande->socid);

View File

@ -70,7 +70,7 @@ $html = new Form($db);
if ($_GET["id"] > 0) if ($_GET["id"] > 0)
{ {
$commande = new CommandeFournisseur($db); $commande = new CommandeFournisseur($db);
if ( $commande->fetch($_GET["id"]) == 0) if ( $commande->fetch($_GET["id"]) >= 0)
{ {
$soc = new Societe($db); $soc = new Societe($db);
$soc->fetch($commande->socid); $soc->fetch($commande->socid);

View File

@ -65,47 +65,47 @@ class CommandeFournisseur extends Commande
/** /**
* Lit une commande * Lit une commande
*/ */
function fetch ($id) function fetch($id)
{ {
$sql = "SELECT c.rowid, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.tva,"; $sql = "SELECT c.rowid, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.tva,";
$sql .= " ".$this->db->pdate("c.date_commande")." as date_commande, c.fk_projet, c.remise_percent, c.source, c.fk_methode_commande,"; $sql .= " ".$this->db->pdate("c.date_commande")." as date_commande, c.fk_projet, c.remise_percent, c.source, c.fk_methode_commande,";
$sql .= " c.note, c.note_public,"; $sql .= " c.note, c.note_public,";
$sql .= " cm.libelle as methode_commande"; $sql .= " cm.libelle as methode_commande";
$sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c"; $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_methode_commande_fournisseur as cm ON cm.rowid = c.fk_methode_commande"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_methode_commande_fournisseur as cm ON cm.rowid = c.fk_methode_commande";
$sql .= " WHERE c.rowid = ".$id; $sql .= " WHERE c.rowid = ".$id;
$result = $this->db->query($sql) ; $resql = $this->db->query($sql) ;
if ($result) if ($resql)
{ {
$obj = $this->db->fetch_object(); $obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid; $this->id = $obj->rowid;
$this->ref = $obj->ref; $this->ref = $obj->ref;
$this->socid = $obj->fk_soc; $this->socid = $obj->fk_soc;
$this->fourn_id = $obj->fk_soc; $this->fourn_id = $obj->fk_soc;
$this->statut = $obj->fk_statut; $this->statut = $obj->fk_statut;
$this->user_author_id = $obj->fk_user_author; $this->user_author_id = $obj->fk_user_author;
$this->total_ht = $obj->total_ht; $this->total_ht = $obj->total_ht;
$this->total_tva = $obj->tva; $this->total_tva = $obj->tva;
$this->total_ttc = $obj->total_ttc; $this->total_ttc = $obj->total_ttc;
$this->date_commande = $obj->date_commande; // date à laquelle la commande a été transmise $this->date_commande = $obj->date_commande; // date à laquelle la commande a été transmise
$this->date = $obj->date_creation; $this->date = $obj->date_creation;
$this->remise_percent = $obj->remise_percent; $this->remise_percent = $obj->remise_percent;
$this->methode_commande_id = $obj->fk_methode_commande; $this->methode_commande_id = $obj->fk_methode_commande;
$this->methode_commande = $obj->methode_commande; $this->methode_commande = $obj->methode_commande;
$this->source = $obj->source; $this->source = $obj->source;
$this->facturee = $obj->facture; $this->facturee = $obj->facture;
$this->projet_id = $obj->fk_projet; $this->projet_id = $obj->fk_projet;
$this->note = $obj->note; $this->note = $obj->note;
$this->note_public = $obj->note_public; $this->note_public = $obj->note_public;
$this->db->free(); $this->db->free();
if ($this->statut == 0) $this->brouillon = 1; if ($this->statut == 0) $this->brouillon = 1;
// export pdf ----------- // export pdf -----------
$this->lignes = array(); $this->lignes = array();
$sql = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_tx, l.remise_percent, l.subprice,'; $sql = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_tx, l.remise_percent, l.subprice,';
@ -115,45 +115,52 @@ class CommandeFournisseur extends Commande
$sql.= ' WHERE l.fk_commande = '.$this->id; $sql.= ' WHERE l.fk_commande = '.$this->id;
$sql.= ' ORDER BY l.rowid'; $sql.= ' ORDER BY l.rowid';
$result = $this->db->query($sql); $result = $this->db->query($sql);
if ($result) if ($result)
{ {
$num = $this->db->num_rows($result); $num = $this->db->num_rows($result);
$i = 0; $i = 0;
while ($i < $num) while ($i < $num)
{ {
$objp = $this->db->fetch_object($result); $objp = $this->db->fetch_object($result);
$ligne = new CommandeFournisseurLigne(); $ligne = new CommandeFournisseurLigne();
$ligne->desc = $objp->description; // Description ligne $ligne->desc = $objp->description; // Description ligne
$ligne->qty = $objp->qty; $ligne->qty = $objp->qty;
$ligne->tva_tx = $objp->tva_tx; $ligne->tva_tx = $objp->tva_tx;
$ligne->subprice = $objp->subprice; $ligne->subprice = $objp->subprice;
$ligne->remise_percent = $objp->remise_percent; $ligne->remise_percent = $objp->remise_percent;
$ligne->price = $objp->price; $ligne->price = $objp->price;
$ligne->fk_product = $objp->fk_product; $ligne->fk_product = $objp->fk_product;
$ligne->libelle = $objp->label; // Label produit $ligne->libelle = $objp->label; // Label produit
$ligne->product_desc = $objp->product_desc; // Description produit $ligne->product_desc = $objp->product_desc; // Description produit
$ligne->ref = $objp->ref; $ligne->ref = $objp->ref;
$this->lignes[$i] = $ligne; $this->lignes[$i] = $ligne;
//dolibarr_syslog("1 ".$ligne->desc); //dolibarr_syslog("1 ".$ligne->desc);
//dolibarr_syslog("2 ".$ligne->product_desc); //dolibarr_syslog("2 ".$ligne->product_desc);
$i++; $i++;
} }
$this->db->free($result); $this->db->free($result);
} return 0;
else }
{ else
dolibarr_syslog("CommandeFournisseur::Fetch Error $sql"); {
dolibarr_syslog("CommandeFournisseur::Fetch Error ".$this->db->error()); $this->error=$this->db->error()." sql=".$sql;
return -1; dolibarr_syslog("CommandeFournisseur::Fetch ".$this->error);
} return -1;
} }
} }
else
{
$this->error=$this->db->error()." sql=".$sql;
dolibarr_syslog("CommandeFournisseur::Fetch ".$this->error);
return -1;
}
}
/** /**
* \brief Insère ligne de log * \brief Insère ligne de log
@ -621,7 +628,7 @@ class CommandeFournisseur extends Commande
$desc = trim($desc); $desc = trim($desc);
$remise_percent = price2num($remise_percent); $remise_percent = price2num($remise_percent);
dolibarr_syslog("Fournisseur_Commande.class.php::addline $desc, $pu, $qty, $txtva, $fk_product, $remise_percent"); dolibarr_syslog("Fournisseur.Commande.class::addline $desc, $pu, $qty, $txtva, $fk_product, $remise_percent");
if ($qty < 1 && ! $fk_product) if ($qty < 1 && ! $fk_product)
{ {
@ -639,7 +646,7 @@ class CommandeFournisseur extends Commande
if ($prod->fetch($fk_product) > 0) if ($prod->fetch($fk_product) > 0)
{ {
$result=$prod->get_buyprice($this->fourn_id,$qty); $result=$prod->get_buyprice($this->fourn_id,$qty);
if ($result) if ($result > 0)
{ {
$label = $prod->libelle; $label = $prod->libelle;
$desc = $prod->description; $desc = $prod->description;
@ -647,13 +654,25 @@ class CommandeFournisseur extends Commande
$pu = $prod->fourn_pu; $pu = $prod->fourn_pu;
$ref = $prod->ref; $ref = $prod->ref;
} }
else if ($result == 0 || $result == -1)
{ {
$this->error="Aucun tarif trouvé pour cette quantité. Quantité saisie insuffisante ?"; $this->error="Aucun tarif trouvé pour cette quantité. Quantité saisie insuffisante ?";
$this->db->rollback(); $this->db->rollback();
dolibarr_syslog($this->error); dolibarr_syslog("Fournisseur.commande.class::addline result=".$result." - ".$this->error);
return -1; return -1;
} }
if ($result < -1)
{
$this->error=$prod->error;
$this->db->rollback();
dolibarr_syslog("Fournisseur.commande.class::addline result=".$result." - ".$this->error);
return -1;
}
}
else
{
$this->error=$this->db->error();
return -1;
} }
} }
@ -668,7 +687,9 @@ class CommandeFournisseur extends Commande
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet (fk_commande,label,description,fk_product, price, qty, tva_tx, remise_percent, subprice, remise, ref)"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet (fk_commande,label,description,fk_product, price, qty, tva_tx, remise_percent, subprice, remise, ref)";
$sql .= " VALUES ($this->id, '" . addslashes($label) . "','" . addslashes($desc) . "',".$fk_product.",".price2num($price).", '$qty', $txtva, $remise_percent,'".price2num($subprice)."','".price2num($remise)."','".$ref."') ;"; $sql .= " VALUES ($this->id, '" . addslashes($label) . "','" . addslashes($desc) . "',".$fk_product.",".price2num($price).", '$qty', $txtva, $remise_percent,'".price2num($subprice)."','".price2num($remise)."','".$ref."') ;";
if ( $this->db->query( $sql) ) dolibarr_syslog('Fournisseur.commande.class::addline sql='.$sql);
$resql=$this->db->query($sql);
if ($resql)
{ {
$this->update_price(); $this->update_price();
@ -683,28 +704,35 @@ class CommandeFournisseur extends Commande
} }
} }
/** /**
* Supprime une ligne de la commande * Supprime une ligne de la commande
* *
*/ */
function delete_line($idligne) function delete_line($idligne)
{
if ($this->statut == 0)
{ {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet WHERE rowid = ".$idligne; if ($this->statut == 0)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet WHERE rowid = ".$idligne;
$resql=$this->db->query($sql);
if ($this->db->query($sql) ) dolibarr_syslog("Fournisseur.commande.class::delete_line sql=".$sql);
{ if ($resql)
$this->update_price(); {
$result=$this->update_price();
return 0; return 0;
} }
else else
{ {
return -1; $this->error=$this->db->error();
} return -1;
}
}
else
{
return -1;
}
} }
}
/** /**
* Mettre à jour le prix * Mettre à jour le prix
* *
@ -1150,6 +1178,7 @@ class CommandeFournisseur extends Commande
else else
{ {
$this->error=$this->db->error(); $this->error=$this->db->error();
dolibarr_syslog("Commande.fournisseur.class::updateline ".$this->error);
$this->db->rollback(); $this->db->rollback();
return -1; return -1;
} }
@ -1157,6 +1186,7 @@ class CommandeFournisseur extends Commande
else else
{ {
$this->error="Commande::updateline Order status makes operation forbidden"; $this->error="Commande::updateline Order status makes operation forbidden";
dolibarr_syslog("Commande.fournisseur.class::updateline ".$this->error);
return -2; return -2;
} }
} }

View File

@ -559,71 +559,73 @@ class Product
} }
/** /**
* \brief Lit le prix pratiqué par un fournisseur * \brief Lit le prix pratiqué par un fournisseur
* \param fourn_id Id du fournisseur * \param fourn_id Id du fournisseur
* \param qty Quantite recherchée * \param qty Quantite recherchée
* \return int <0 si ko, 0 si ok mais rien trouvé, 1 si ok et trouvé * \return int <0 si ko, 0 si ok mais rien trouvé, 1 si ok et trouvé
*/ */
function get_buyprice($fourn_id, $qty) function get_buyprice($fourn_id, $qty)
{ {
$result = 0; $result = 0;
$sql = "SELECT pf.price as price, pf.quantity as quantity"; $sql = "SELECT pf.price as price, pf.quantity as quantity";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pf"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pf";
$sql.= " WHERE pf.fk_soc = ".$fourn_id; $sql.= " WHERE pf.fk_soc = ".$fourn_id;
$sql.= " AND pf.fk_product =" .$this->id; $sql.= " AND pf.fk_product =" .$this->id;
$sql.= " AND quantity <= ".$qty; $sql.= " AND quantity <= ".$qty;
$sql.= " ORDER BY quantity DESC"; $sql.= " ORDER BY quantity DESC";
$sql.= " LIMIT 1"; $sql.= " LIMIT 1";
dolibarr_syslog("Product::get_buyprice $fourn_id,$qty sql=$sql"); dolibarr_syslog("Product::get_buyprice $fourn_id,$qty sql=$sql");
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
if ($obj && $obj->quantity > 0) if ($obj && $obj->quantity > 0)
{ {
$this->buyprice = $obj->price; // \deprecated $this->buyprice = $obj->price; // \deprecated
$this->fourn_pu = $obj->price / $obj->quantity; // Prix unitaire du produit pour le fournisseur $fourn_id $this->fourn_pu = $obj->price / $obj->quantity; // Prix unitaire du produit pour le fournisseur $fourn_id
return 1; return 1;
} }
else else
{ {
// On refait le meme select mais sans critere de quantite // On refait le meme select mais sans critere de quantite
$sql = "SELECT pf.price as price, pf.quantity as quantity"; $sql = "SELECT pf.price as price, pf.quantity as quantity";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pf"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pf";
$sql.= " WHERE pf.fk_soc = ".$fourn_id; $sql.= " WHERE pf.fk_soc = ".$fourn_id;
$sql.= " AND pf.fk_product =" .$this->id; $sql.= " AND pf.fk_product =" .$this->id;
//$sql.= " AND quantity <= ".$qty; //$sql.= " AND quantity <= ".$qty;
$sql.= " ORDER BY quantity DESC"; $sql.= " ORDER BY quantity DESC";
$sql.= " LIMIT 1"; $sql.= " LIMIT 1";
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
$num=$this->db->num_rows($result); $num=$this->db->num_rows($result);
if ($num) if ($num)
{ {
return -1; // Ce produit existe chez ce fournisseur mais qté insuffisante return -1; // Ce produit existe chez ce fournisseur mais qté insuffisante
} }
else
{
return 0; // Ce produit n'existe pas chez ce fournisseur
}
}
else
{
$this->error=$this->db->error();
return -3;
}
}
}
else else
{ {
return 0; // Ce produit n'existe pas chez ce fournisseur $this->error=$this->db->error();
} return -2;
} }
else return $result;
{ }
return -3;
}
}
}
else
{
return -2;
}
return $result;
}
/** /**