From 60aa363a91c09f25065daa2bb0d17f3b3d4e1746 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Wed, 9 Feb 2011 14:45:42 +0000
Subject: [PATCH] Ergo: Add option "MAIN_DISABLEDRAFTSTATUS" to reduce nb of
 clicks

---
 .../cheque/class/remisecheque.class.php       | 108 ++++++++++--------
 htdocs/compta/paiement/cheque/fiche.php       |  58 +++++++---
 htdocs/projet/class/project.class.php         |  62 +++++++---
 htdocs/projet/fiche.php                       |  31 ++++-
 4 files changed, 171 insertions(+), 88 deletions(-)

diff --git a/htdocs/compta/paiement/cheque/class/remisecheque.class.php b/htdocs/compta/paiement/cheque/class/remisecheque.class.php
index 2f22a119999..5d52f2d1e06 100644
--- a/htdocs/compta/paiement/cheque/class/remisecheque.class.php
+++ b/htdocs/compta/paiement/cheque/class/remisecheque.class.php
@@ -113,10 +113,10 @@ class RemiseCheque extends CommonObject
 	}
 
 	/**
-	 *	\brief  	Create a receipt to send cheques
-	 *	\param  	user 			Utilisateur qui effectue l'operation
-	 *	\param  	account_id 		Compte bancaire concerne
-	 *	\return		int				<0 if KO, >0 if OK
+	 *	Create a receipt to send cheques
+	 *	@param  	user 			Utilisateur qui effectue l'operation
+	 *	@param  	account_id 		Compte bancaire concerne
+	 *	@return		int				<0 if KO, >0 if OK
 	 */
 	function create($user, $account_id)
 	{
@@ -125,6 +125,8 @@ class RemiseCheque extends CommonObject
 		$this->errno = 0;
 		$this->id = 0;
 
+		$now=dol_now();
+
 		$this->db->begin();
 
 		$sql = "INSERT INTO ".MAIN_DB_PREFIX."bordereau_cheque (";
@@ -132,17 +134,19 @@ class RemiseCheque extends CommonObject
 		$sql.= ", date_bordereau";
 		$sql.= ", fk_user_author";
 		$sql.= ", fk_bank_account";
+		$sql.= ", statut";
 		$sql.= ", amount";
 		$sql.= ", number";
 		$sql.= ", entity";
 		$sql.= ", nbcheque";
 		$sql.= ") VALUES (";
-		$sql.= $this->db->idate(mktime());
-		$sql.= ", ".$this->db->idate(mktime());
+		$sql.= $this->db->idate($now);
+		$sql.= ", ".$this->db->idate($now);
 		$sql.= ", ".$user->id;
 		$sql.= ", ".$account_id;
 		$sql.= ", 0";
 		$sql.= ", 0";
+		$sql.= ", 0";
 		$sql.= ", ".$conf->entity;
 		$sql.= ", 0";
 		$sql.= ")";
@@ -155,7 +159,7 @@ class RemiseCheque extends CommonObject
 			if ($this->id == 0)
 			{
 				$this->errno = -1024;
-				dol_syslog("Remisecheque::Create Erreur Lecture ID ($this->errno)", LOG_ERR);
+				dol_syslog("Remisecheque::Create Error read id ".$this->errno, LOG_ERR);
 			}
 
 			if ($this->id > 0 && $this->errno == 0)
@@ -169,7 +173,7 @@ class RemiseCheque extends CommonObject
 				if (! $resql)
 				{
 					$this->errno = -1025;
-					dol_syslog("RemiseCheque::Create ERREUR UPDATE ($this->errno)", LOG_ERR);
+					dol_syslog("RemiseCheque::Create Error update ".$this->errno, LOG_ERR);
 				}
 			}
 
@@ -197,7 +201,7 @@ class RemiseCheque extends CommonObject
 				else
 				{
 					$this->errno = -1026;
-					dol_syslog("RemiseCheque::Create Error ($this->errno)", LOG_ERR);
+					dol_syslog("RemiseCheque::Create Error ".$this->errno, LOG_ERR);
 				}
 			}
 
@@ -214,7 +218,7 @@ class RemiseCheque extends CommonObject
 					if (!$resql)
 					{
 						$this->errno = -18;
-						dol_syslog("RemiseCheque::Create Error update bank ($this->errno)", LOG_ERR);
+						dol_syslog("RemiseCheque::Create Error update bank ".$this->errno, LOG_ERR);
 					}
 				}
 			}
@@ -224,31 +228,34 @@ class RemiseCheque extends CommonObject
 				if ($this->updateAmount() <> 0)
 				{
 					$this->errno = -1027;
-					dol_syslog("RemiseCheque::Create ERREUR ($this->errno)");
+					dol_syslog("RemiseCheque::Create Error update amount ".$this->errno, LOG_ERR);
 				}
 			}
 		}
 		else
 		{
-			$result = -1;
+			$this->errno = -1;
 			$this->error=$this->db->lasterror();
 			$this->errno=$this->db->lasterrno();
-			dol_syslog("RemiseCheque::Create Erreur $result INSERT Mysql");
+			dol_syslog("RemiseCheque::Create Error ".$this->error, LOG_ERR);
 		}
 
+	    if (! $this->errno && ! empty($conf->global->MAIN_DISABLEDRAFTSTATUS))
+        {
+            $res=$this->validate($user);
+            //if ($res < 0) $error++;
+        }
 
-		if ($this->errno == 0)
-		{
-			$this->db->commit();
-			return $this->id;
-		}
-		else
-		{
-			$this->db->rollback();
-			dol_syslog("RemiseCheque::Create ROLLBACK ($this->errno)");
-			return $this->errno;
-		}
-
+        if (! $this->errno)
+        {
+            $this->db->commit();
+            return $this->id;
+        }
+        else
+        {
+            $this->db->rollback();
+            return $this->errno;
+        }
 	}
 
 	/**
@@ -306,8 +313,9 @@ class RemiseCheque extends CommonObject
 	}
 
 	/**
-	 *  Validate receipt
-	 *  @param  user 	User
+	 *  Validate a receipt
+	 *  @param     user 	User
+	 *  @return    int      <0 if KO, >0 if OK
 	 */
 	function validate($user)
 	{
@@ -317,13 +325,12 @@ class RemiseCheque extends CommonObject
 
 		$this->db->begin();
 
-		$num=$this->getNextNumber();
+		$numref=$this->getNextNumber();
 
-		if ($this->errno == 0 && $num)
+		if ($this->errno == 0 && $numref)
 		{
 			$sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
-			$sql.= " SET statut = 1";
-			$sql.= ", number = '".$num."'";
+			$sql.= " SET statut = 1, number = '".$numref."'";
 			$sql.= " WHERE rowid = ".$this->id;
 			$sql.= " AND entity = ".$conf->entity;
 			$sql.= " AND statut = 0";
@@ -336,6 +343,7 @@ class RemiseCheque extends CommonObject
 
 				if ($num == 1)
 				{
+				    $this->number = $numref;
 					$this->statut = 1;
 				}
 				else
@@ -355,14 +363,14 @@ class RemiseCheque extends CommonObject
 		if ($this->errno == 0)
 		{
 			$this->db->commit();
+			return 1;
 		}
 		else
 		{
 			$this->db->rollback();
 			dol_syslog("RemiseCheque::Validate ".$this->errno, LOG_ERR);
+            return $this->errno;
 		}
-
-		return $this->errno;
 	}
 
 
@@ -446,10 +454,10 @@ class RemiseCheque extends CommonObject
 
 
 	/**
-	 *	\brief  	Build document
-	 *	\param 		model 			Model name
-	 *	\param 		outputlangs		Object langs
-	 * 	\return  	int        		<0 if KO, >0 if OK
+	 *	Build document
+	 *	@param 		model 			Model name
+	 *	@param 		outputlangs		Object langs
+	 * 	@return  	int        		<0 if KO, >0 if OK
 	 */
 	function generatePdf($model='blochet', $outputlangs)
 	{
@@ -457,7 +465,7 @@ class RemiseCheque extends CommonObject
 
 		if (empty($model)) $model='blochet';
 
-		dol_syslog("RemiseCheque::generatePdf model=".$model, LOG_DEBUG);
+		dol_syslog("RemiseCheque::generatePdf model=".$model." id=".$this->id, LOG_DEBUG);
 
 		$dir=DOL_DOCUMENT_ROOT ."/includes/modules/cheque/pdf/";
 
@@ -469,7 +477,7 @@ class RemiseCheque extends CommonObject
 			require_once($dir.$file);
 
 			$classname='BordereauCheque'.ucfirst($model);
-			$pdf = new $classname($db);
+			$docmodel = new $classname($db);
 
 			$sql = "SELECT b.banque, b.emetteur, b.amount, b.num_chq";
 			$sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
@@ -488,27 +496,27 @@ class RemiseCheque extends CommonObject
 				$i = 0;
 				while ( $objp = $this->db->fetch_object($result) )
 				{
-					$pdf->lines[$i]->bank_chq = $objp->banque;
-					$pdf->lines[$i]->emetteur_chq = $objp->emetteur;
-					$pdf->lines[$i]->amount_chq = $objp->amount;
-					$pdf->lines[$i]->num_chq = $objp->num_chq;
+					$docmodel->lines[$i]->bank_chq = $objp->banque;
+					$docmodel->lines[$i]->emetteur_chq = $objp->emetteur;
+					$docmodel->lines[$i]->amount_chq = $objp->amount;
+					$docmodel->lines[$i]->num_chq = $objp->num_chq;
 					$i++;
 				}
 			}
-			$pdf->nbcheque = $this->nbcheque;
-			$pdf->number = $this->number;
-			$pdf->amount = $this->amount;
-			$pdf->date   = $this->date_bordereau;
+			$docmodel->nbcheque = $this->nbcheque;
+			$docmodel->number = $this->number;
+			$docmodel->amount = $this->amount;
+			$docmodel->date   = $this->date_bordereau;
 
 			$account = new Account($this->db);
 			$account->fetch($this->account_id);
 
-			$pdf->account = &$account;
+			$docmodel->account = &$account;
 
 			// We save charset_output to restore it because write_file can change it if needed for
 			// output format that does not support UTF8.
 			$sav_charset_output=$outputlangs->charset_output;
-			$result=$pdf->write_file($conf->banque->dir_output.'/bordereau', $this->number, $outputlangs);
+			$result=$docmodel->write_file($conf->banque->dir_output.'/bordereau', $this->number, $outputlangs);
 			if ($result > 0)
 			{
 				$outputlangs->charset_output=$sav_charset_output;
@@ -518,7 +526,7 @@ class RemiseCheque extends CommonObject
 			{
 				$outputlangs->charset_output=$sav_charset_output;
 				dol_syslog("Error");
-				dol_print_error($db,$pdf->error);
+				dol_print_error($db,$docmodel->error);
 				return 0;
 			}
 		}
diff --git a/htdocs/compta/paiement/cheque/fiche.php b/htdocs/compta/paiement/cheque/fiche.php
index a38b58160af..2c4462c114b 100644
--- a/htdocs/compta/paiement/cheque/fiche.php
+++ b/htdocs/compta/paiement/cheque/fiche.php
@@ -20,8 +20,8 @@
 
 /**
  *	\file       htdocs/compta/paiement/cheque/fiche.php
- *	\ingroup    facture
- *	\brief      Tab cheque deposit
+ *	\ingroup    bank, invoice
+ *	\brief      Page for cheque deposits
  *	\version    $Id$
  */
 
@@ -35,8 +35,8 @@ $langs->load('bills');
 $langs->load('banks');
 $langs->load('companies');
 
-$id = isset($_REQUEST["id"])?$_REQUEST["id"]:'';
-$ref= isset($_REQUEST["ref"])?$_REQUEST["ref"]:'';
+$id = GETPOST("id");
+$ref= GETPOST("ref");
 
 // Security check
 $fieldid = isset($_GET["ref"])?'number':'rowid';
@@ -88,8 +88,24 @@ if ($_GET['action'] == 'create' && $_GET["accountid"] > 0 && $user->rights->banq
 	$result = $remisecheque->create($user, $_GET["accountid"]);
 	if ($result > 0)
 	{
-		Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$remisecheque->id);
-		exit;
+        if ($remisecheque->statut == 1)     // If statut is validated, we build doc
+        {
+            $remisecheque->fetch($remisecheque->id);    // To force to reload all properties in correct property name
+    	    // Define output language
+    	    $outputlangs = $langs;
+            $newlang='';
+            if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id'])) $newlang=$_REQUEST['lang_id'];
+            //if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
+            if (! empty($newlang))
+            {
+                $outputlangs = new Translate("",$conf);
+                $outputlangs->setDefaultLang($newlang);
+            }
+            $result = $remisecheque->generatePdf($_POST["model"], $outputlangs);
+        }
+
+        Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$remisecheque->id);
+        exit;
 	}
 	else
 	{
@@ -136,7 +152,19 @@ if ($_REQUEST['action'] == 'confirm_valide' && $_REQUEST['confirm'] == 'yes' &&
 	$result = $remisecheque->validate($user);
 	if ($result >= 0)
 	{
-		Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$remisecheque->id);
+        // Define output language
+        $outputlangs = $langs;
+        $newlang='';
+        if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id'])) $newlang=$_REQUEST['lang_id'];
+        //if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
+        if (! empty($newlang))
+        {
+            $outputlangs = new Translate("",$conf);
+            $outputlangs->setDefaultLang($newlang);
+        }
+        $result = $remisecheque->generatePdf($_POST["model"], $outputlangs);
+
+        Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$remisecheque->id);
 		exit;
 	}
 	else
@@ -155,13 +183,15 @@ if ($_POST['action'] == 'builddoc' && $user->rights->banque->cheque)
 		$remisecheque->setDocModel($user, $_REQUEST['model']);
 	}*/
 
-	$outputlangs = $langs;
-	if (! empty($_REQUEST['lang_id']))
-	{
-		$outputlangs = new Translate("",$conf);
-		$outputlangs->setDefaultLang($_REQUEST['lang_id']);
-	}
-
+    $outputlangs = $langs;
+    $newlang='';
+    if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id'])) $newlang=$_REQUEST['lang_id'];
+    //if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
+    if (! empty($newlang))
+    {
+        $outputlangs = new Translate("",$conf);
+        $outputlangs->setDefaultLang($newlang);
+    }
 	$result = $remisecheque->generatePdf($_POST["model"], $outputlangs);
 	if ($result <= 0)
 	{
diff --git a/htdocs/projet/class/project.class.php b/htdocs/projet/class/project.class.php
index 51bb6d7e384..545e9fdbafb 100644
--- a/htdocs/projet/class/project.class.php
+++ b/htdocs/projet/class/project.class.php
@@ -57,8 +57,8 @@ class Project extends CommonObject
 	var $statuts;
 
 	/**
-	 *    \brief  Constructeur de la classe
-	 *    \param  DB          handler acces base de donnees
+	 *    Constructor of class
+	 *    @param  DB          Database access handler
 	 */
 	function Project($DB)
 	{
@@ -70,12 +70,15 @@ class Project extends CommonObject
 	}
 
 	/**
-	 *    \brief      Create a project into database
-	 *    \param      user        Id utilisateur qui cree
-	 *    \return     int         <0 si ko, id du projet cree si ok
+	 *    Create a project into database
+	 *    @param      user        User making creation
+	 *    @return     int         <0 if KO, id of created project if OK
 	 */
 	function create($user, $notrigger=0)
 	{
+	    global $conf;
+
+		$error=0;
 		$ret=0;
 
 		// Check parameters
@@ -86,12 +89,15 @@ class Project extends CommonObject
 			return -1;
 		}
 
+		$this->db->begin();
+
 		$sql = "INSERT INTO ".MAIN_DB_PREFIX."projet (";
 		$sql.= "ref";
 		$sql.= ", title";
 		$sql.= ", description";
 		$sql.= ", fk_soc";
 		$sql.= ", fk_user_creat";
+		$sql.= ", fk_statut";
 		$sql.= ", public";
 		$sql.= ", datec";
 		$sql.= ", dateo";
@@ -102,6 +108,7 @@ class Project extends CommonObject
 		$sql.= ", '".addslashes($this->description)."'";
 		$sql.= ", ".($this->socid > 0?$this->socid:"null");
 		$sql.= ", ".$user->id;
+        $sql.= ", 0";
 		$sql.= ", ".($this->public?1:0);
 		$sql.= ", ".($this->datec!=''?$this->db->idate($this->datec):'null');
 		$sql.= ", ".($this->dateo!=''?$this->db->idate($this->dateo):'null');
@@ -127,12 +134,28 @@ class Project extends CommonObject
 		}
 		else
 		{
-			$this->error=$this->db->lasterror();
-			dol_syslog("Project::Create error -2 ".$this->error, LOG_ERR);
-			$ret = -2;
+            $this->error=$this->db->lasterror();
+            $this->errno=$this->db->lasterrno();
+            dol_syslog("Project::Create error -2 ".$this->error, LOG_ERR);
+			$error++;
+		}
+
+        if (! $error && ! empty($conf->global->MAIN_DISABLEDRAFTSTATUS))
+		{
+            $res=$this->setValid($user);
+            if ($res < 0) $error++;
 		}
 
-		return $ret;
+		if (! $error)
+		{
+            $this->db->commit();
+            return $ret;
+		}
+		else
+		{
+            $this->db->rollback();
+            return -1;
+		}
 	}
 
 
@@ -350,8 +373,9 @@ class Project extends CommonObject
 	}
 
 	/**
-	 *    \brief    Supprime le projet dans la base
-	 *    \param    Utilisateur
+	 *    Delete a project from database
+	 *    @param       user            User
+	 *    @param       notrigger       Disable triggers
 	 */
 	function delete($user, $notrigger=0)
 	{
@@ -433,11 +457,11 @@ class Project extends CommonObject
 	}
 
 	/**
-	 *		\brief		Validate a project
-	 *		\param		user		User that validate
-	 *		\return		int			<0 if KO, >0 if OK
+	 *		Validate a project
+	 *		@param		user		User that validate
+	 *		@return		int			<0 if KO, >0 if OK
 	 */
-	function setValid($user, $outputdir)
+	function setValid($user)
 	{
 		global $langs, $conf;
 
@@ -485,11 +509,11 @@ class Project extends CommonObject
 	}
 
 	/**
-	 *		\brief		Close a project
-	 *		\param		user		User that validate
-	 *		\return		int			<0 if KO, >0 if OK
+	 *		Close a project
+	 *		@param		user		User that validate
+	 *		@return		int			<0 if KO, >0 if OK
 	 */
-	function setClose($user, $outputdir)
+	function setClose($user)
 	{
 		global $langs, $conf;
 
diff --git a/htdocs/projet/fiche.php b/htdocs/projet/fiche.php
index cce01b5f8c3..d7d7a7a9bd9 100644
--- a/htdocs/projet/fiche.php
+++ b/htdocs/projet/fiche.php
@@ -70,6 +70,10 @@ if ($_POST["action"] == 'add' && $user->rights->projet->creer)
 
     if (! $error)
     {
+        $error=0;
+
+        $db->begin();
+
         $project = new Project($db);
 
         $project->ref             = $_POST["ref"];
@@ -86,14 +90,31 @@ if ($_POST["action"] == 'add' && $user->rights->projet->creer)
         {
             // Add myself as project leader
             $result = $project->add_contact($user->id, 'PROJECTLEADER', 'internal');
+            if ($result < 0)
+            {
+                $langs->load("errors");
+                $mesg='<div class="error">'.$langs->trans($project->error).'</div>';
+                $error++;
+            }
+        }
+        else
+        {
+            $langs->load("errors");
+            $mesg='<div class="error">'.$langs->trans($project->error).'</div>';
+            $error++;
+        }
+
+        if (! $error)
+        {
+            $db->commit();
 
             Header("Location:fiche.php?id=".$project->id);
             exit;
         }
         else
         {
-            $langs->load("errors");
-            $mesg='<div class="error">'.$langs->trans($project->error).'</div>';
+            $db->rollback();
+
             $_GET["action"] = 'create';
         }
     }
@@ -177,7 +198,7 @@ if (GETPOST('action') == 'confirm_validate' && GETPOST('confirm') == 'yes')
     $project = new Project($db);
     $project->fetch(GETPOST("id"));
 
-    $result = $project->setValid($user, $conf->projet->outputdir);
+    $result = $project->setValid($user);
     if ($result <= 0)
     {
         $mesg='<div class="error">'.$project->error.'</div>';
@@ -188,7 +209,7 @@ if (GETPOST('action') == 'confirm_close' && GETPOST('confirm') == 'yes')
 {
     $project = new Project($db);
     $project->fetch(GETPOST("id"));
-    $result = $project->setClose($user, $conf->projet->outputdir);
+    $result = $project->setClose($user);
     if ($result <= 0)
     {
         $mesg='<div class="error">'.$project->error.'</div>';
@@ -199,7 +220,7 @@ if (GETPOST('action') == 'confirm_reopen' && GETPOST('confirm') == 'yes')
 {
     $project = new Project($db);
     $project->fetch(GETPOST("id"));
-    $result = $project->setValid($user, $conf->projet->outputdir);
+    $result = $project->setValid($user);
     if ($result <= 0)
     {
         $mesg='<div class="error">'.$project->error.'</div>';
-- 
GitLab