From f2d42cce825abc05612ef2b924166ad2772af953 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 10 Oct 2005 19:48:22 +0000 Subject: [PATCH] Debut debuggage fonction commandes fournisseurs --- htdocs/fourn/commande/fiche.php | 232 ++++++++++--------- htdocs/fourn/commande/history.php | 36 +-- htdocs/fourn/fournisseur.class.php | 45 ++-- htdocs/fourn/fournisseur.commande.class.php | 234 +++++++++++--------- 4 files changed, 298 insertions(+), 249 deletions(-) diff --git a/htdocs/fourn/commande/fiche.php b/htdocs/fourn/commande/fiche.php index de787373cdf..da01dd75bcb 100644 --- a/htdocs/fourn/commande/fiche.php +++ b/htdocs/fourn/commande/fiche.php @@ -29,19 +29,21 @@ */ require("./pre.inc.php"); - -if (!$user->rights->fournisseur->commande->lire) accessforbidden(); - require_once(DOL_DOCUMENT_ROOT."/project.class.php"); require_once(DOL_DOCUMENT_ROOT."/propal.class.php"); +if (!$user->rights->fournisseur->commande->lire) accessforbidden(); + + // Sécurité accés client +$socidp=0; if ($user->societe_id > 0) { $action = ''; $socidp = $user->societe_id; } + /* * */ @@ -74,28 +76,29 @@ if ($_POST["action"] == 'setremise' && $user->rights->commande->creer) */ if ($_POST["action"] == 'addligne' && $user->rights->fournisseur->commande->creer) { - $comf = new CommandeFournisseur($db); - $comf->fetch($_GET["id"]); + $comf = new CommandeFournisseur($db); + $comf->fetch($_GET["id"]); - if ($_POST["p_idprod"] > 0) + if ($_POST["p_idprod"] > 0) { - $result = $comf->addline("DESC", - $_POST["pu"], - $_POST["pqty"], - $_POST["tva_tx"], - $_POST["p_idprod"], - $_POST["premise"]); + $result = $comf->addline($_POST["desc"], + $_POST["pu"], + $_POST["pqty"], + $_POST["tva_tx"], + $_POST["p_idprod"], + $_POST["premise"]); } - else + else { - $result = $comf->addline($_POST["desc"], - $_POST["pu"], - $_POST["qty"], - $_POST["tva_tx"], - 0, - $_POST["remise_percent"]); + $result = $comf->addline($_POST["desc"], + $_POST["pu"], + $_POST["qty"], + $_POST["tva_tx"], + 0, + $_POST["remise_percent"]); } Header("Location: fiche.php?id=".$_GET["id"]); + exit; } if ($_POST["action"] == 'updateligne' && $user->rights->commande->creer) @@ -122,25 +125,27 @@ if ($_GET["action"] == 'deleteline' && $user->rights->fournisseur->commande->cre $result = $comf->delete_line($_GET["lineid"]); } -if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == yes && $user->rights->fournisseur->commande->valider) +if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == 'yes' && $user->rights->fournisseur->commande->valider) { - $commande = new CommandeFournisseur($db); - $commande->fetch($_GET["id"]); - $soc = new Societe($db); - $soc->fetch($commande->soc_id); - $result = $commande->valid($user); - Header("Location: fiche.php?id=".$_GET["id"]); + $commande = new CommandeFournisseur($db); + $commande->fetch($_GET["id"]); + $soc = new Societe($db); + $soc->fetch($commande->soc_id); + $result = $commande->valid($user); + Header("Location: fiche.php?id=".$_GET["id"]); + exit; } -if ($_POST["action"] == 'confirm_approve' && $_POST["confirm"] == yes && $user->rights->fournisseur->commande->approuver) +if ($_POST["action"] == 'confirm_approve' && $_POST["confirm"] == 'yes' && $user->rights->fournisseur->commande->approuver) { - $commande = new CommandeFournisseur($db); - $commande->fetch($_GET["id"]); - $result = $commande->approve($user); - Header("Location: fiche.php?id=".$_GET["id"]); + $commande = new CommandeFournisseur($db); + $commande->fetch($_GET["id"]); + $result = $commande->approve($user); + Header("Location: fiche.php?id=".$_GET["id"]); + exit; } -if ($_POST["action"] == 'confirm_refuse' && $_POST["confirm"] == yes && $user->rights->fournisseur->commande->approuver) +if ($_POST["action"] == 'confirm_refuse' && $_POST["confirm"] == 'yes' && $user->rights->fournisseur->commande->approuver) { $commande = new CommandeFournisseur($db); $commande->fetch($_GET["id"]); @@ -148,56 +153,66 @@ if ($_POST["action"] == 'confirm_refuse' && $_POST["confirm"] == yes && $user->r Header("Location: fiche.php?id=".$_GET["id"]); } -if ($_POST["action"] == 'confirm_commande' && $_POST["confirm"] == yes && $user->rights->fournisseur->commande->commander) +if ($_POST["action"] == 'confirm_commande' && $_POST["confirm"] == 'yes' && $user->rights->fournisseur->commande->commander) { - $commande = new CommandeFournisseur($db); - $commande->fetch($_GET["id"]); - $result = $commande->commande($user, $_GET["datecommande"], $_GET["methode"]); - Header("Location: fiche.php?id=".$_GET["id"]); + $commande = new CommandeFournisseur($db); + $commande->fetch($_GET["id"]); + $result = $commande->commande($user, $_GET["datecommande"], $_GET["methode"]); + Header("Location: fiche.php?id=".$_GET["id"]); + exit; } -if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == yes && $user->rights->fournisseur->commande->creer ) +if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == 'yes' && $user->rights->fournisseur->commande->creer ) { - $commande = new CommandeFournisseur($db); - $commande->id = $_GET["id"]; - $commande->delete(); - Header("Location: index.php"); + $commande = new CommandeFournisseur($db); + $commande->id = $_GET["id"]; + $commande->delete(); + Header("Location: index.php"); + exit; } if ($_POST["action"] == 'livraison' && $user->rights->fournisseur->commande->receptionner) { - $commande = new CommandeFournisseur($db); - $commande->fetch($_GET["id"]); - - $date_liv = mktime(0,0,0,$_POST["remonth"],$_POST["reday"],$_POST["reyear"]); - - $result = $commande->Livraison($user, $date_liv, $_POST["type"]); - Header("Location: fiche.php?id=".$_GET["id"]); + $commande = new CommandeFournisseur($db); + $commande->fetch($_GET["id"]); + + $date_liv = mktime(0,0,0,$_POST["remonth"],$_POST["reday"],$_POST["reyear"]); + + $result = $commande->Livraison($user, $date_liv, $_POST["type"]); + Header("Location: fiche.php?id=".$_GET["id"]); + exit; } if ($_POST["action"] == 'confirm_cancel' && $_POST["confirm"] == yes && $user->rights->fournisseur->commande->annuler) { - $commande = new CommandeFournisseur($db); - $commande->fetch($_GET["id"]); - $result = $commande->cancel($user); - Header("Location: fiche.php?id=".$_GET["id"]); + $commande = new CommandeFournisseur($db); + $commande->fetch($_GET["id"]); + $result = $commande->cancel($user); + Header("Location: fiche.php?id=".$_GET["id"]); + exit; } /* * Créé une commande */ -if ($_GET["action"] == 'create') +if ($_GET["action"] == 'create') { - $fourn = new Fournisseur($db); - $fourn->fetch($_GET["socid"]); + $fourn = new Fournisseur($db); + $fourn->fetch($_GET["socid"]); - if ($fourn->create_commande($user) == 0) + if ($fourn->create_commande($user) > 0) { - $idc = $fourn->single_open_commande; - Header("Location:fiche.php?id=".$idc); + $idc = $fourn->single_open_commande; + Header("Location:fiche.php?id=".$idc); + exit; + } + else + { + $mesg=$fourn->error; + print "x $mesg x"; } } @@ -305,7 +320,8 @@ if ($_GET["id"] > 0) print ''; print '"; print ''; + print ''; + print img_object($langs->trans("ShowSupplier"),'company').' '.$soc->nom.''; print ''; print ''; @@ -387,7 +403,7 @@ if ($_GET["id"] > 0) { $objp = $db->fetch_object($resql); print ""; - print "\n"; + print ''; if ($objp->fk_product > 0) { print ''; print ''; - print ''; + print ''; print ''; print ''; print "\n"; @@ -476,54 +492,54 @@ if ($_GET["id"] > 0) print "
'.$langs->trans("Supplier")."'; - print ''.$soc->nom.'
".$objp->ref."'.img_object($langs->trans("ShowProduct"),'product').' '.$objp->ref.''; @@ -465,7 +481,7 @@ if ($_GET["id"] > 0) $html->select_produits_fournisseurs($commande->fourn_id,'','p_idprod',$filtre); print ' %% 
"; print ''; - /* + /** * Boutons actions */ - if ($user->societe_id == 0 && $commande->statut < 3 && $_GET["action"] <> 'valid') - { - print '
'; - - if ($commande->statut == 0 && $num_lignes > 0) - { - if ($user->rights->fournisseur->commande->valider) - { - print ''.$langs->trans("Valid").''; - } - } - - if ($commande->statut == 1) - { - if ($user->rights->fournisseur->commande->annuler) - { - print ''.$langs->trans("CancelOrder").''; - } - - if ($user->rights->fournisseur->commande->approuver) - { - print ''.$langs->trans("ApproveOrder").''; - - print ''.$langs->trans("RefuseOrder").''; - } - } - - if ($commande->statut == 2) - { - if ($user->rights->fournisseur->commande->approuver) - { - print ''.$langs->trans("RefuseOrder").''; - } - } - - if ($commande->statut == 0) - { - if ($user->rights->fournisseur->commande->creer) - { - print ''.$langs->trans("Delete").''; - } - } - - print "
"; - } + if ($user->societe_id == 0 && $commande->statut < 3 && $_GET["action"] <> 'valid') + { + print '
'; + + if ($commande->statut == 0 && $num_lignes > 0) + { + if ($user->rights->fournisseur->commande->valider) + { + print ''.$langs->trans("Valid").''; + } + } + + if ($commande->statut == 1) + { + if ($user->rights->fournisseur->commande->annuler) + { + print ''.$langs->trans("CancelOrder").''; + } + + if ($user->rights->fournisseur->commande->approuver) + { + print ''.$langs->trans("ApproveOrder").''; + + print ''.$langs->trans("RefuseOrder").''; + } + } + + if ($commande->statut == 2) + { + if ($user->rights->fournisseur->commande->approuver) + { + print ''.$langs->trans("RefuseOrder").''; + } + } + + if ($commande->statut == 0) + { + if ($user->rights->fournisseur->commande->creer) + { + print ''.$langs->trans("Delete").''; + } + } + + print "
"; + } /* * Documents générés diff --git a/htdocs/fourn/commande/history.php b/htdocs/fourn/commande/history.php index a39711b69d9..ba53ed57c50 100644 --- a/htdocs/fourn/commande/history.php +++ b/htdocs/fourn/commande/history.php @@ -104,26 +104,26 @@ if ($_GET["id"] > 0) print ""; } - // Auteur - print ''.$langs->trans("Author").''.$author->fullname.''; - print ' '; print "\n"; - + print "
"; - /* - * Historique - * - */ - echo '
'; + /* + * Suivi historique + * Date - Statut - Auteur + */ + print '
'; - $sql = "SELECT l.fk_statut, ".$db->pdate("l.datelog") ."as dl, u.firstname, u.name"; + print ''; + print ''; + print ''; + + $sql = "SELECT l.fk_statut, ".$db->pdate("l.datelog") ."as dl, u.rowid, u.code, u.firstname, u.name"; $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur_log as l "; $sql .= " , ".MAIN_DB_PREFIX."user as u "; $sql .= " WHERE l.fk_commande = ".$commande->id." AND u.rowid = l.fk_user"; $sql .= " ORDER BY l.rowid DESC"; $resql = $db->query($sql); - if ($resql) { $num = $db->num_rows($resql); @@ -132,22 +132,22 @@ if ($_GET["id"] > 0) $var=True; while ($i < $num) { + $var=!$var; + $obj = $db->fetch_object($resql); print ""; print '\n"; - print '\n"; - print ''; - - print ""; - + print ''; + print ''; $i++; - $var=!$var; } $db->free($resql); } @@ -155,8 +155,8 @@ if ($_GET["id"] > 0) { dolibarr_print_error($db); } - print "
'.$langs->trans("Date").''.$langs->trans("Status").''.$langs->trans("Author").'
'.dolibarr_print_date($obj->dl,"%a %d %b %Y %H:%M:%S")." '; + print ' '; print $commande->statuts[$obj->fk_statut]."'.$obj->firstname. " " . $obj->name.'
'; + print img_object($langs->trans("ShowUser"),'user').' '.$obj->code.'
"; + print ''; } else diff --git a/htdocs/fourn/fournisseur.class.php b/htdocs/fourn/fournisseur.class.php index 6b85462b1be..b2d83f546b1 100644 --- a/htdocs/fourn/fournisseur.class.php +++ b/htdocs/fourn/fournisseur.class.php @@ -103,29 +103,32 @@ class Fournisseur extends Societe { } - function create_commande($user) - { - $result = -1; - dolibarr_syslog("Fournisseur::Create_Commande"); - $comm = new CommandeFournisseur($this->db); - $comm->soc_id = $this->id; - if ( $comm->create($user) == 0 ) - { - dolibarr_syslog("Fournisseur::Create_Commande : Success"); + /** + * \brief Créé la commande au statut brouillon + * \param user Utilisateur qui crée + * \return int <0 si ko, id de la commande créée si ok + */ + function create_commande($user) + { + dolibarr_syslog("Fournisseur::Create_Commande"); + $comm = new CommandeFournisseur($this->db); + $comm->soc_id = $this->id; + + if ($comm->create($user) > 0) + { + dolibarr_syslog("Fournisseur::Create_Commande : Success"); + $this->single_open_commande = $comm->id; + + return $comm->id; + } + else + { + dolibarr_syslog("Fournisseur::Create_Commande : Failed"); + return -1; + } + } - $this->single_open_commande = $comm->id; - - $result = 0; - } - else - { - dolibarr_syslog("Fournisseur::Create_Commande : Failed"); - $result = -2; - } - - return $result; - } function ProductCommande($user, $product_id) { diff --git a/htdocs/fourn/fournisseur.commande.class.php b/htdocs/fourn/fournisseur.commande.class.php index 0f60b1b884c..ee894c73f15 100644 --- a/htdocs/fourn/fournisseur.commande.class.php +++ b/htdocs/fourn/fournisseur.commande.class.php @@ -123,20 +123,29 @@ $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_methode_commande_fournisseur as cm ON cm return $result ; } - /** - * Log - * - */ - function log($user, $statut, $datelog) - { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur_log (datelog, fk_commande, fk_statut, fk_user)"; - $sql .= " VALUES (".$this->db->idate($datelog).",".$this->id.", $statut, ".$user->id.")"; - if ( $this->db->query($sql) ) - { - return 0; - } + /** + * \brief Insère ligne de log + * \param user Utilisateur qui modifie la commande + * \param statut Statut de la commande + * \param datelog Date de modification + * \return int <0 si ko, >0 si ok + */ + function log($user, $statut, $datelog) + { + $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur_log (datelog, fk_commande, fk_statut, fk_user)"; + $sql.= " VALUES (".$this->db->idate($datelog).",".$this->id.", $statut, ".$user->id.")"; + + if ( $this->db->query($sql) ) + { + return 1; + } + else + { + return -1; + } } + /** * Valide la commande * @@ -395,102 +404,123 @@ $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_methode_commande_fournisseur as cm ON cm } return $result ; } - /** - * Créé la commande - * - */ - function create($user) - { - dolibarr_syslog("CommandeFournisseur::Create"); - dolibarr_syslog("CommandeFournisseur::Create soc id=".$this->soc_id); - /* On positionne en mode brouillon la commande */ - $this->brouillon = 1; - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur (fk_soc, date_creation, fk_user_author, fk_statut) "; - $sql .= " VALUES (".$this->soc_id.", now(), ".$user->id.",0)"; - - if ( $this->db->query($sql) ) - { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."commande_fournisseur"); - $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id; - if ($this->db->query($sql)) - { - - /* - * - * - */ - dolibarr_syslog("CommandeFournisseur::Create : Success"); - return 0; - } - else - { - dolibarr_syslog("CommandeFournisseur::Create : Failed 2"); - return -2; - } - } - else - { - dolibarr_syslog("CommandeFournisseur::Create : Failed 1"); - dolibarr_syslog("CommandeFournisseur::Create : ".$this->db->error()); - dolibarr_syslog("CommandeFournisseur::Create : ".$sql); - return -1; - } + /** + * \brief Créé la commande au statut brouillon + * \param user Utilisateur qui crée + * \return int <0 si ko, >0 si ok + */ + function create($user) + { + dolibarr_syslog("CommandeFournisseur::Create soc id=".$this->soc_id); + + $this->db->begin(); + + /* On positionne en mode brouillon la commande */ + $this->brouillon = 1; + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur (fk_soc, date_creation, fk_user_author, fk_statut) "; + $sql .= " VALUES (".$this->soc_id.", now(), ".$user->id.",0)"; + + if ( $this->db->query($sql) ) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."commande_fournisseur"); + + $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur"; + $sql.= " SET ref='(PROV".$this->id.")'"; + $sql.= " WHERE rowid=".$this->id; + if ($this->db->query($sql)) + { + // On logue creation pour historique + $this->log($user, 0, time()); + + dolibarr_syslog("CommandeFournisseur::Create : Success"); + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error()." - ".$sql; + dolibarr_syslog("CommandeFournisseur::Create: Failed -2 - ".$this->error); + $this->db->rollback(); + return -2; + } + } + else + { + $this->error=$this->db->error()." - ".$sql; + dolibarr_syslog("CommandeFournisseur::Create: Failed -1 - ".$this->error); + $this->db->rollback(); + return -1; + } } - /** - * Ajoute une ligne de commande - * - */ - function addline($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0) + + /** + * \brief Ajoute une ligne de commande + * \param desc Description + * \param pu Prix unitaire + * \param qty Quantité + * \param txtva Taux tva + * \param fk_product Id produit + * \param remise_percent Remise + * \param int <0 si ko, >0 si ok + */ + function addline($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0) { - $qty = ereg_replace(",",".",$qty); - $pu = ereg_replace(",",".",$pu); + $qty = ereg_replace(",",".",$qty); + $pu = ereg_replace(",",".",$pu); + $desc=trim($desc); - if ($this->brouillon && strlen(trim($desc))) - { - if (strlen(trim($qty))==0) - { - $qty=1; - } + dolibarr_syslog("Fournisseur_Commande.class.php::addline $desc, $pu, $qty, $txtva, $fk_product, $remise_percent"); + + if ($this->brouillon) + { + $this->db->begin(); + + if (strlen(trim($qty))==0) + { + $qty=1; + } + + if ($fk_product > 0) + { + $prod = new Product($this->db, $fk_product); + if ($prod->fetch($fk_product) > 0) + { + $prod->get_buyprice($this->fourn_id,$qty); - if ($fk_product > 0) - { - $prod = new Product($this->db, $fk_product); - if ($prod->fetch($fk_product) > 0) - { - $desc = $prod->libelle; - $txtva = $prod->tva_tx; + $desc = $prod->libelle; + $txtva = $prod->tva_tx; + $pu = $prod->buyprice/$qty; + $ref = $prod->ref; + } + } + + $remise = 0; + $price = round(ereg_replace(",",".",$pu), 2); + $subprice = $price; + if (trim(strlen($remise_percent)) > 0) + { + $remise = round(($pu * $remise_percent / 100), 2); + $price = $pu - $remise; + } + + $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($desc) . "','" . addslashes($desc) . "',$fk_product,".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."','".$ref."') ;"; + + if ( $this->db->query( $sql) ) + { + $this->update_price(); - $prod->get_buyprice($this->fourn_id,$qty); - - $pu = $prod->buyprice/$qty; - } - } - - $remise = 0; - $price = round(ereg_replace(",",".",$pu), 2); - $subprice = $price; - if (trim(strlen($remise_percent)) > 0) - { - $remise = round(($pu * $remise_percent / 100), 2); - $price = $pu - $remise; - } - - $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($desc) . "','" . addslashes($desc) . "',$fk_product,".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."','".$ref."') ;"; - - if ( $this->db->query( $sql) ) - { - $this->update_price(); - return 0; - } - else - { - - return -1; - } - } + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); + return -1; + } + } } /**