From 90df30e41213873074bffc2b7c796d48b3b8ddc3 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 21 Mar 2019 17:37:56 +0100 Subject: [PATCH] NEW Add trigget FICHINTER_UNVALIDATE Standardize code of setDraft. --- htdocs/bom/class/bom.class.php | 9 ++- htdocs/comm/propal/class/propal.class.php | 19 ++++-- htdocs/commande/class/commande.class.php | 10 +++- htdocs/compta/facture/class/facture.class.php | 9 ++- htdocs/fichinter/class/fichinter.class.php | 59 +++++++++++++------ .../fourn/class/fournisseur.facture.class.php | 10 +++- .../class/supplier_proposal.class.php | 33 +++++++++-- 7 files changed, 114 insertions(+), 35 deletions(-) diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index a4b70660c7f..9646b5d0d2e 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -604,7 +604,7 @@ class BOM extends CommonObject */ public function setDraft($user) { - global $conf,$langs; + global $conf, $langs; $error=0; @@ -630,6 +630,11 @@ class BOM extends CommonObject dol_syslog(get_class($this)."::setDraft", LOG_DEBUG); if ($this->db->query($sql)) { + if (! $error) + { + $this->oldcopy= clone $this; + } + if (!$error) { // Call trigger $result=$this->call_trigger('BOM_UNVALIDATE', $user); @@ -640,7 +645,7 @@ class BOM extends CommonObject $this->status=self::STATUS_DRAFT; $this->db->commit(); return 1; - }else { + } else { $this->db->rollback(); return -1; } diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index dc9bc9ea87e..7015cc51977 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -2610,12 +2610,20 @@ class Propal extends CommonObject // phpcs:enable $error=0; + // Protection + if ($this->statut <= self::STATUS_DRAFT) + { + return 0; + } + + dol_syslog(get_class($this)."::setDraft", LOG_DEBUG); + $this->db->begin(); - $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_statut = ".self::STATUS_DRAFT; + $sql = "UPDATE ".MAIN_DB_PREFIX."propal"; + $sql.= " SET fk_statut = ".self::STATUS_DRAFT; $sql.= " WHERE rowid = ".$this->id; - dol_syslog(__METHOD__, LOG_DEBUG); $resql=$this->db->query($sql); if (!$resql) { @@ -2626,8 +2634,6 @@ class Propal extends CommonObject if (! $error) { $this->oldcopy= clone $this; - $this->statut = self::STATUS_DRAFT; - $this->brouillon = 1; } if (! $notrigger && empty($error)) @@ -2640,7 +2646,10 @@ class Propal extends CommonObject if (! $error) { - $this->db->commit(); + $this->statut = self::STATUS_DRAFT; + $this->brouillon = 1; + + $this->db->commit(); return 1; } else diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index cc985378732..464d7ba84a4 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -499,16 +499,22 @@ class Commande extends CommonOrder return -1; } + dol_syslog(get_class($this)."::set_draft", LOG_DEBUG); + $this->db->begin(); $sql = "UPDATE ".MAIN_DB_PREFIX."commande"; $sql.= " SET fk_statut = ".self::STATUS_DRAFT; $sql.= " WHERE rowid = ".$this->id; - dol_syslog(get_class($this)."::set_draft", LOG_DEBUG); if ($this->db->query($sql)) { - // If stock is decremented on validate order, we must reincrement it + if (! $error) + { + $this->oldcopy= clone $this; + } + + // If stock is decremented on validate order, we must reincrement it if (! empty($conf->stock->enabled) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) { $result = 0; diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 4e9b21ef2d5..5a67f46ade1 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -2554,16 +2554,22 @@ class Facture extends CommonInvoice return 0; } + dol_syslog(get_class($this)."::set_draft", LOG_DEBUG); + $this->db->begin(); $sql = "UPDATE ".MAIN_DB_PREFIX."facture"; $sql.= " SET fk_statut = ".self::STATUS_DRAFT; $sql.= " WHERE rowid = ".$this->id; - dol_syslog(get_class($this)."::set_draft", LOG_DEBUG); $result=$this->db->query($sql); if ($result) { + if (! $error) + { + $this->oldcopy= clone $this; + } + // Si on decremente le produit principal et ses composants a la validation de facture, on réincrement if ($this->type != self::TYPE_DEPOSIT && $result >= 0 && ! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_BILL)) { @@ -2589,6 +2595,7 @@ class Facture extends CommonInvoice $old_statut=$this->statut; $this->brouillon = 1; $this->statut = self::STATUS_DRAFT; + // Call trigger $result=$this->call_trigger('BILL_UNVALIDATE', $user); if ($result < 0) diff --git a/htdocs/fichinter/class/fichinter.class.php b/htdocs/fichinter/class/fichinter.class.php index 43cd79bfe70..d306a2f2b7d 100644 --- a/htdocs/fichinter/class/fichinter.class.php +++ b/htdocs/fichinter/class/fichinter.class.php @@ -445,35 +445,56 @@ class Fichinter extends CommonObject * Set status to draft * * @param User $user User that set draft - * @return int <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK */ public function setDraft($user) { global $langs, $conf; - if ($this->statut != 0) + $error=0; + + // Protection + if ($this->statut <= self::STATUS_DRAFT) { - $this->db->begin(); + return 0; + } - $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter"; - $sql.= " SET fk_statut = 0"; - $sql.= " WHERE rowid = ".$this->id; - $sql.= " AND entity = ".$conf->entity; + dol_syslog(get_class($this)."::setDraft", LOG_DEBUG); - dol_syslog("Fichinter::setDraft", LOG_DEBUG); - $resql=$this->db->query($sql); - if ($resql) - { - $this->db->commit(); - return 1; - } - else - { - $this->db->rollback(); - $this->error=$this->db->lasterror(); - return -1; + $this->db->begin(); + + $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter"; + $sql.= " SET fk_statut = ".self::STATUS_DRAFT; + $sql.= " WHERE rowid = ".$this->id; + + $resql=$this->db->query($sql); + if ($resql) + { + if (!$error) { + $this->oldcopy = clone $this; + } + + if (!$error) { + // Call trigger + $result=$this->call_trigger('FICHINTER_UNVALIDATE', $user); + if ($result < 0) $error++; + } + + if (!$error) { + $this->statut=self::STATUS_DRAFT; + $this->db->commit(); + return 1; + } else { + $this->db->rollback(); + return -1; } } + else + { + $this->db->rollback(); + $this->error=$this->db->lasterror(); + return -1; + } } /** diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php index 4450ee611f8..425ef686b1c 100644 --- a/htdocs/fourn/class/fournisseur.facture.class.php +++ b/htdocs/fourn/class/fournisseur.facture.class.php @@ -1471,16 +1471,22 @@ class FactureFournisseur extends CommonInvoice return 0; } + dol_syslog(get_class($this)."::set_draft", LOG_DEBUG); + $this->db->begin(); $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn"; - $sql.= " SET fk_statut = 0"; + $sql.= " SET fk_statut = ".self::STATUS_DRAFT; $sql.= " WHERE rowid = ".$this->id; - dol_syslog(get_class($this)."::set_draft", LOG_DEBUG); $result=$this->db->query($sql); if ($result) { + if (! $error) + { + $this->oldcopy= clone $this; + } + // Si on incremente le produit principal et ses composants a la validation de facture fournisseur, on decremente if ($result >= 0 && ! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) { diff --git a/htdocs/supplier_proposal/class/supplier_proposal.class.php b/htdocs/supplier_proposal/class/supplier_proposal.class.php index 90e6a10c0a0..a767dceb450 100644 --- a/htdocs/supplier_proposal/class/supplier_proposal.class.php +++ b/htdocs/supplier_proposal/class/supplier_proposal.class.php @@ -1883,14 +1883,39 @@ class SupplierProposal extends CommonObject // phpcs:enable global $conf,$langs; - $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal SET fk_statut = 0"; + $error = 0; + + if ($this->statut == self::STATUS_DRAFT) + { + dol_syslog(get_class($this)."::setDraft already draft status", LOG_WARNING); + return 0; + } + + $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal"; + $sql.= " SET fk_statut = ".self::STATUS_DRAFT; $sql.= " WHERE rowid = ".$this->id; if ($this->db->query($sql)) { - $this->statut = 0; - $this->brouillon = 1; - return 1; + if (!$error) { + $this->oldcopy = clone $this; + } + + if (!$error) { + // Call trigger + $result=$this->call_trigger('SUPPLIER_PROPOSAL_UNVALIDATE', $user); + if ($result < 0) $error++; + } + + if (!$error) { + $this->statut=self::STATUS_DRAFT; + $this->brouillon = 1; + $this->db->commit(); + return 1; + } else { + $this->db->rollback(); + return -1; + } } else {