コード例 #1
0
ファイル: commande.class.php プロジェクト: ripasch/dolibarr
 /**
  *  \brief   Validate order
  *  \param   user     	User making status change
  *  \return  int		<=0 if OK, >0 if KO
  */
 function valid($user)
 {
     global $conf, $langs;
     require_once DOL_DOCUMENT_ROOT . "/lib/files.lib.php";
     $error = 0;
     // Protection
     if ($this->statut == 1) {
         dol_syslog("Commande::valid no draft status", LOG_WARNING);
         return 0;
     }
     if (!$user->rights->commande->valider) {
         $this->error = 'Permission denied';
         dol_syslog("Commande::valid " . $this->error, LOG_ERR);
         return -1;
     }
     $now = dol_now();
     $this->db->begin();
     // Definition du nom de module de numerotation de commande
     $soc = new Societe($this->db);
     $soc->fetch($this->socid);
     // Class of company linked to order
     $result = $soc->set_as_client();
     // Define new ref
     if (!$error && preg_match('/^[\\(]?PROV/i', $this->ref)) {
         $num = $this->getNextNumRef($soc);
     } else {
         $num = $this->ref;
     }
     // Validate
     $sql = "UPDATE " . MAIN_DB_PREFIX . "commande";
     $sql .= " SET ref = '" . $num . "'";
     $sql .= ", fk_statut = 1";
     $sql .= ", date_valid=" . $this->db->idate($now);
     $sql .= ", fk_user_valid = " . $user->id;
     $sql .= " WHERE rowid = " . $this->id;
     dol_syslog("Commande::valid() sql=" . $sql);
     $resql = $this->db->query($sql);
     if (!$resql) {
         dol_syslog("Commande::valid() Echec update - 10 - sql=" . $sql, LOG_ERR);
         dol_print_error($this->db);
         $error++;
     }
     if (!$error) {
         // If stock is incremented on validate order, we must increment it
         if ($result >= 0 && $conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
             require_once DOL_DOCUMENT_ROOT . "/product/stock/class/mouvementstock.class.php";
             $langs->load("agenda");
             // Loop on each line
             for ($i = 0; $i < sizeof($this->lines); $i++) {
                 if ($this->lines[$i]->fk_product > 0) {
                     $mouvP = new MouvementStock($this->db);
                     // We decrement stock of product (and sub-products)
                     $entrepot_id = "1";
                     // TODO ajouter possibilite de choisir l'entrepot
                     $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $entrepot_id, $this->lines[$i]->qty, $this->lines[$i]->subprice);
                     if ($result < 0) {
                         $error++;
                     }
                 }
             }
         }
     }
     if (!$error) {
         $this->oldref = '';
         // Rename directory if dir was a temporary ref
         if (preg_match('/^[\\(]?PROV/i', $this->ref)) {
             // On renomme repertoire ($this->ref = ancienne ref, $numfa = nouvelle ref)
             // afin de ne pas perdre les fichiers attaches
             $comref = dol_sanitizeFileName($this->ref);
             $snum = dol_sanitizeFileName($num);
             $dirsource = $conf->commande->dir_output . '/' . $comref;
             $dirdest = $conf->commande->dir_output . '/' . $snum;
             if (file_exists($dirsource)) {
                 dol_syslog("Commande::valid() rename dir " . $dirsource . " into " . $dirdest);
                 if (@rename($dirsource, $dirdest)) {
                     $this->oldref = $comref;
                     dol_syslog("Rename ok");
                     // Suppression ancien fichier PDF dans nouveau rep
                     dol_delete_file($conf->commande->dir_output . '/' . $snum . '/' . $comref . '.*');
                 }
             }
         }
     }
     // Set new ref and current status
     if (!$error) {
         $this->ref = $num;
         $this->statut = 1;
     }
     if (!$error) {
         // Appel des triggers
         include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php";
         $interface = new Interfaces($this->db);
         $result = $interface->run_triggers('ORDER_VALIDATE', $this, $user, $langs, $conf);
         if ($result < 0) {
             $error++;
             $this->errors = $interface->errors;
         }
         // Fin appel triggers
     }
     if (!$error) {
         $this->db->commit();
         return 1;
     } else {
         $this->db->rollback();
         $this->error = $this->db->lasterror();
         return -1;
     }
 }
コード例 #2
0
ファイル: expedition.class.php プロジェクト: Albertopf/prueba
 /**
  *  Validate object and update stock if option enabled
  *
  *  @param      User		$user       Object user that validate
  *  @param		int			$notrigger	1=Does not execute triggers, 0= execuete triggers
  *  @return     int						<0 if OK, >0 if KO
  */
 function valid($user, $notrigger = 0)
 {
     global $conf, $langs;
     require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
     dol_syslog(get_class($this) . "::valid");
     // Protection
     if ($this->statut) {
         dol_syslog(get_class($this) . "::valid no draft status", LOG_WARNING);
         return 0;
     }
     if (!(empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->expedition->creer) || !empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->expedition->shipping_advance->validate))) {
         $this->error = 'Permission denied';
         dol_syslog(get_class($this) . "::valid " . $this->error, LOG_ERR);
         return -1;
     }
     $this->db->begin();
     $error = 0;
     // Define new ref
     $soc = new Societe($this->db);
     $soc->fetch($this->socid);
     // Class of company linked to order
     $result = $soc->set_as_client();
     // Define new ref
     if (!$error && (preg_match('/^[\\(]?PROV/i', $this->ref) || empty($this->ref))) {
         $numref = $this->getNextNumRef($soc);
     } else {
         $numref = "EXP" . $this->id;
     }
     $this->newref = $numref;
     $now = dol_now();
     // Validate
     $sql = "UPDATE " . MAIN_DB_PREFIX . "expedition SET";
     $sql .= " ref='" . $numref . "'";
     $sql .= ", fk_statut = 1";
     $sql .= ", date_valid = '" . $this->db->idate($now) . "'";
     $sql .= ", fk_user_valid = " . $user->id;
     $sql .= " WHERE rowid = " . $this->id;
     dol_syslog(get_class($this) . "::valid update expedition", LOG_DEBUG);
     $resql = $this->db->query($sql);
     if (!$resql) {
         $this->error = $this->db->lasterror();
         $error++;
     }
     // If stock increment is done on sending (recommanded choice)
     if (!$error && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT)) {
         require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php';
         $langs->load("agenda");
         // Loop on each product line to add a stock movement
         // TODO possibilite d'expedier a partir d'une propale ou autre origine
         $sql = "SELECT cd.fk_product, cd.subprice,";
         $sql .= " ed.rowid, ed.qty, ed.fk_entrepot,";
         $sql .= " edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock";
         $sql .= " FROM " . MAIN_DB_PREFIX . "commandedet as cd,";
         $sql .= " " . MAIN_DB_PREFIX . "expeditiondet as ed";
         $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
         $sql .= " WHERE ed.fk_expedition = " . $this->id;
         $sql .= " AND cd.rowid = ed.fk_origin_line";
         dol_syslog(get_class($this) . "::valid select details", LOG_DEBUG);
         $resql = $this->db->query($sql);
         if ($resql) {
             $cpt = $this->db->num_rows($resql);
             for ($i = 0; $i < $cpt; $i++) {
                 $obj = $this->db->fetch_object($resql);
                 if (empty($obj->edbrowid)) {
                     $qty = $obj->qty;
                 } else {
                     $qty = $obj->edbqty;
                 }
                 if ($qty <= 0) {
                     continue;
                 }
                 dol_syslog(get_class($this) . "::valid movement index " . $i . " ed.rowid=" . $obj->rowid . " edb.rowid=" . $obj->edbrowid);
                 //var_dump($this->lines[$i]);
                 $mouvS = new MouvementStock($this->db);
                 $mouvS->origin =& $this;
                 if (empty($obj->edbrowid)) {
                     // line without batch detail
                     // We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
                     $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans("ShipmentValidatedInDolibarr", $numref));
                     if ($result < 0) {
                         $error++;
                         break;
                     }
                 } else {
                     // line with batch detail
                     // We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
                     $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans("ShipmentValidatedInDolibarr", $numref), '', $obj->eatby, $obj->sellby, $obj->batch);
                     if ($result < 0) {
                         $error++;
                         break;
                     }
                     // We update content of table llx_product_batch (will be rename into llx_product_stock_batch inantoher version)
                     // We can set livraison_batch to deprecated and adapt livraison to handle batch too (mouvS->_create also calls mouvS->_create_batch)
                     if (!empty($conf->productbatch->enabled)) {
                         $result = $mouvS->livraison_batch($obj->fk_origin_stock, $qty);
                         // ->fk_origin_stock = id into table llx_product_batch (will be rename into llx_product_stock_batch in another version)
                         if ($result < 0) {
                             $error++;
                             $this->errors[] = $mouvS->error;
                             break;
                         }
                     }
                 }
             }
         } else {
             $this->db->rollback();
             $this->error = $this->db->error();
             return -2;
         }
     }
     if (!$error && !$notrigger) {
         // Call trigger
         $result = $this->call_trigger('SHIPPING_VALIDATE', $user);
         if ($result < 0) {
             $error++;
         }
         // End call triggers
     }
     if (!$error) {
         $this->oldref = $this->ref;
         // Rename directory if dir was a temporary ref
         if (preg_match('/^[\\(]?PROV/i', $this->ref)) {
             // On renomme repertoire ($this->ref = ancienne ref, $numfa = nouvelle ref)
             // in order not to lose the attached files
             $oldref = dol_sanitizeFileName($this->ref);
             $newref = dol_sanitizeFileName($numref);
             $dirsource = $conf->expedition->dir_output . '/sending/' . $oldref;
             $dirdest = $conf->expedition->dir_output . '/sending/' . $newref;
             if (file_exists($dirsource)) {
                 dol_syslog(get_class($this) . "::valid rename dir " . $dirsource . " into " . $dirdest);
                 if (@rename($dirsource, $dirdest)) {
                     dol_syslog("Rename ok");
                     // Rename docs starting with $oldref with $newref
                     $listoffiles = dol_dir_list($conf->expedition->dir_output . '/sending/' . $newref, 'files', 1, '^' . preg_quote($oldref, '/'));
                     foreach ($listoffiles as $fileentry) {
                         $dirsource = $fileentry['name'];
                         $dirdest = preg_replace('/^' . preg_quote($oldref, '/') . '/', $newref, $dirsource);
                         $dirsource = $fileentry['path'] . '/' . $dirsource;
                         $dirdest = $fileentry['path'] . '/' . $dirdest;
                         @rename($dirsource, $dirdest);
                     }
                 }
             }
         }
     }
     // Set new ref and current status
     if (!$error) {
         $this->ref = $numref;
         $this->statut = 1;
     }
     if (!$error) {
         $this->db->commit();
         return 1;
     } else {
         foreach ($this->errors as $errmsg) {
             dol_syslog(get_class($this) . "::valid " . $errmsg, LOG_ERR);
             $this->error .= $this->error ? ', ' . $errmsg : $errmsg;
         }
         $this->db->rollback();
         return -1 * $error;
     }
 }
コード例 #3
0
ファイル: propal.class.php プロジェクト: ADDAdev/Dolibarr
 /**
  *	Close the commercial proposal
  *
  *	@param      User	$user		Object user that close
  *	@param      int		$statut		Statut
  *	@param      string	$note		Comment
  *	@return     int         		<0 if KO, >0 if OK
  */
 function cloture($user, $statut, $note)
 {
     global $langs, $conf;
     $this->statut = $statut;
     $error = 0;
     $now = dol_now();
     $this->db->begin();
     $sql = "UPDATE " . MAIN_DB_PREFIX . "propal";
     $sql .= " SET fk_statut = " . $statut . ", note_private = '" . $this->db->escape($note) . "', date_cloture='" . $this->db->idate($now) . "', fk_user_cloture=" . $user->id;
     $sql .= " WHERE rowid = " . $this->id;
     $resql = $this->db->query($sql);
     if ($resql) {
         $modelpdf = $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED ? $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED : $this->modelpdf;
         $trigger_name = 'PROPAL_CLOSE_REFUSED';
         if ($statut == 2) {
             $trigger_name = 'PROPAL_CLOSE_SIGNED';
             $modelpdf = $conf->global->PROPALE_ADDON_PDF_ODT_TOBILL ? $conf->global->PROPALE_ADDON_PDF_ODT_TOBILL : $this->modelpdf;
             // The connected company is classified as a client
             $soc = new Societe($this->db);
             $soc->id = $this->socid;
             $result = $soc->set_as_client();
             if ($result < 0) {
                 $this->error = $this->db->error();
                 $this->db->rollback();
                 return -2;
             }
         }
         if ($statut == 4) {
             $trigger_name = 'PROPAL_CLASSIFY_BILLED';
         }
         if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
             // Define output language
             $outputlangs = $langs;
             if (!empty($conf->global->MAIN_MULTILANGS)) {
                 $outputlangs = new Translate("", $conf);
                 $newlang = GETPOST('lang_id') ? GETPOST('lang_id') : $this->client->default_lang;
                 $outputlangs->setDefaultLang($newlang);
             }
             //$ret=$object->fetch($id);    // Reload to get new records
             $this->generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
         }
         // Call trigger
         $result = $this->call_trigger($trigger_name, $user);
         if ($result < 0) {
             $error++;
         }
         // End call triggers
         if (!$error) {
             $this->db->commit();
             return 1;
         } else {
             $this->db->rollback();
             return -1;
         }
     } else {
         $this->error = $this->db->error();
         $this->db->rollback();
         return -1;
     }
 }
コード例 #4
0
ファイル: commande.class.php プロジェクト: Albertopf/prueba
 /**
  *	Validate order
  *
  *	@param		User	$user     		User making status change
  *	@param		int		$idwarehouse	Id of warehouse to use for stock decrease
  *  @param		int		$notrigger		1=Does not execute triggers, 0= execuete triggers
  *	@return  	int						<=0 if OK, >0 if KO
  */
 function valid($user, $idwarehouse = 0, $notrigger = 0)
 {
     global $conf, $langs;
     require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
     $error = 0;
     // Protection
     if ($this->statut == self::STATUS_VALIDATED) {
         dol_syslog(get_class($this) . "::valid action abandonned: no draft status", LOG_WARNING);
         return 0;
     }
     if (!(empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer) || !empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate))) {
         $this->error = 'ErrorPermissionDenied';
         dol_syslog(get_class($this) . "::valid " . $this->error, LOG_ERR);
         return -1;
     }
     $now = dol_now();
     $this->db->begin();
     // Definition du nom de module de numerotation de commande
     $soc = new Societe($this->db);
     $soc->fetch($this->socid);
     // Class of company linked to order
     $result = $soc->set_as_client();
     // Define new ref
     if (!$error && (preg_match('/^[\\(]?PROV/i', $this->ref) || empty($this->ref))) {
         $num = $this->getNextNumRef($soc);
     } else {
         $num = $this->ref;
     }
     $this->newref = $num;
     // Validate
     $sql = "UPDATE " . MAIN_DB_PREFIX . "commande";
     $sql .= " SET ref = '" . $num . "',";
     $sql .= " fk_statut = " . self::STATUS_VALIDATED . ",";
     $sql .= " date_valid='" . $this->db->idate($now) . "',";
     $sql .= " fk_user_valid = " . $user->id;
     $sql .= " WHERE rowid = " . $this->id;
     dol_syslog(get_class($this) . "::valid()", LOG_DEBUG);
     $resql = $this->db->query($sql);
     if (!$resql) {
         dol_print_error($this->db);
         $this->error = $this->db->lasterror();
         $error++;
     }
     if (!$error) {
         // If stock is incremented on validate order, we must increment it
         if ($result >= 0 && !empty($conf->stock->enabled) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) {
             require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php';
             $langs->load("agenda");
             // Loop on each line
             $cpt = count($this->lines);
             for ($i = 0; $i < $cpt; $i++) {
                 if ($this->lines[$i]->fk_product > 0) {
                     $mouvP = new MouvementStock($this->db);
                     $mouvP->origin =& $this;
                     // We decrement stock of product (and sub-products)
                     $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("OrderValidatedInDolibarr", $num));
                     if ($result < 0) {
                         $error++;
                         $this->error = $mouvP->error;
                     }
                 }
                 if ($error) {
                     break;
                 }
             }
         }
     }
     if (!$error && !$notrigger) {
         // Call trigger
         $result = $this->call_trigger('ORDER_VALIDATE', $user);
         if ($result < 0) {
             $error++;
         }
         // End call triggers
     }
     if (!$error) {
         $this->oldref = $this->ref;
         // Rename directory if dir was a temporary ref
         if (preg_match('/^[\\(]?PROV/i', $this->ref)) {
             // On renomme repertoire ($this->ref = ancienne ref, $num = nouvelle ref)
             // in order not to lose the attachments
             $oldref = dol_sanitizeFileName($this->ref);
             $newref = dol_sanitizeFileName($num);
             $dirsource = $conf->commande->dir_output . '/' . $oldref;
             $dirdest = $conf->commande->dir_output . '/' . $newref;
             if (file_exists($dirsource)) {
                 dol_syslog(get_class($this) . "::valid() rename dir " . $dirsource . " into " . $dirdest);
                 if (@rename($dirsource, $dirdest)) {
                     dol_syslog("Rename ok");
                     // Rename docs starting with $oldref with $newref
                     $listoffiles = dol_dir_list($conf->commande->dir_output . '/' . $newref, 'files', 1, '^' . preg_quote($oldref, '/'));
                     foreach ($listoffiles as $fileentry) {
                         $dirsource = $fileentry['name'];
                         $dirdest = preg_replace('/^' . preg_quote($oldref, '/') . '/', $newref, $dirsource);
                         $dirsource = $fileentry['path'] . '/' . $dirsource;
                         $dirdest = $fileentry['path'] . '/' . $dirdest;
                         @rename($dirsource, $dirdest);
                     }
                 }
             }
         }
     }
     // Set new ref and current status
     if (!$error) {
         $this->ref = $num;
         $this->statut = self::STATUS_VALIDATED;
     }
     if (!$error) {
         $this->db->commit();
         return 1;
     } else {
         $this->db->rollback();
         return -1;
     }
 }
コード例 #5
0
ファイル: propal.class.php プロジェクト: nrjacker4/crm-php
 /**
  *	Close the commercial proposal
  *
  *	@param      User	$user		Object user that close
  *	@param      int		$statut		Statut
  *	@param      text	$note		Comment
  *	@return     int         		<0 if KO, >0 if OK
  */
 function cloture($user, $statut, $note)
 {
     global $langs, $conf;
     $this->statut = $statut;
     $error = 0;
     $now = dol_now();
     $this->db->begin();
     $sql = "UPDATE " . MAIN_DB_PREFIX . "propal";
     $sql .= " SET fk_statut = " . $statut . ", note = '" . $this->db->escape($note) . "', date_cloture=" . $this->db->idate($now) . ", fk_user_cloture=" . $user->id;
     $sql .= " WHERE rowid = " . $this->id;
     $resql = $this->db->query($sql);
     if ($resql) {
         if ($statut == 2) {
             // Classe la societe rattachee comme client
             $soc = new Societe($this->db);
             $soc->id = $this->socid;
             $result = $soc->set_as_client();
             if ($result < 0) {
                 $this->error = $this->db->error();
                 $this->db->rollback();
                 return -2;
             }
             // Appel des triggers
             include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php";
             $interface = new Interfaces($this->db);
             $result = $interface->run_triggers('PROPAL_CLOSE_SIGNED', $this, $user, $langs, $conf);
             if ($result < 0) {
                 $error++;
                 $this->errors = $interface->errors;
             }
             // Fin appel triggers
         } else {
             // Appel des triggers
             include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php";
             $interface = new Interfaces($this->db);
             $result = $interface->run_triggers('PROPAL_CLOSE_REFUSED', $this, $user, $langs, $conf);
             if ($result < 0) {
                 $error++;
                 $this->errors = $interface->errors;
             }
             // Fin appel triggers
         }
         $this->db->commit();
         return 1;
     } else {
         $this->error = $this->db->error();
         $this->db->rollback();
         return -1;
     }
 }
コード例 #6
0
	/**
	 *        Validate object and update stock if option enabled
	 *        @param      user        Object user that validate
	 *        @return     int
	 */
	function valid($user)
	{
		global $conf, $langs;
		
        require_once(DOL_DOCUMENT_ROOT."/lib/files.lib.php");

		dol_syslog("Expedition::valid");

		// Protection
		if ($this->statut)
		{
			dol_syslog("Expedition::valid no draft status", LOG_WARNING);
			return 0;
		}

		if (! $user->rights->expedition->valider)
		{
			$this->error='Permission denied';
			dol_syslog("Expedition::valid ".$this->error, LOG_ERR);
			return -1;
		}

		$this->db->begin();

		$error = 0;

		// Define new ref
		$soc = new Societe($this->db);
		$soc->fetch($this->socid);

		// Class of company linked to order
		$result=$soc->set_as_client();

		// Define new ref
		if (! $error && (preg_match('/^[\(]?PROV/i', $this->ref)))
		{
			$numref = $this->getNextNumRef($soc);
		}
		else
		{
			$numref = "EXP".$this->id;
		}

		$now=dol_now();

		// Validate
		$sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
		$sql.= " ref='".$numref."'";
		$sql.= ", fk_statut = 1";
		$sql.= ", date_valid = '".$this->db->idate($now)."'";
		$sql.= ", fk_user_valid = ".$user->id;
		$sql.= " WHERE rowid = ".$this->id;

		dol_syslog("Expedition::valid update expedition sql=".$sql);
		$resql=$this->db->query($sql);
		if (! $resql)
		{
			dol_syslog("Expedition::valid Echec update - 10 - sql=".$sql, LOG_ERR);
			$this->error=$this->db->lasterror();
			$error++;
		}

		// If stock increment is done on sending (recommanded choice)
		if (! $error && $conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_SHIPMENT)
		{
			require_once DOL_DOCUMENT_ROOT."/product/stock/class/mouvementstock.class.php";
            
			$langs->load("agenda");

			// Loop on each product line to add a stock movement
			// TODO possibilite d'expedier a partir d'une propale ou autre origine
			$sql = "SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot";
			$sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd";
			$sql.= ", ".MAIN_DB_PREFIX."expeditiondet as ed";
			$sql.= " WHERE ed.fk_expedition = ".$this->id;
			$sql.= " AND cd.rowid = ed.fk_origin_line";

			dol_syslog("Expedition::valid select details sql=".$sql);
			$resql=$this->db->query($sql);
			if ($resql)
			{
				$num = $this->db->num_rows($resql);
				$i=0;
				while($i < $num)
				{
					dol_syslog("Expedition::valid movement index ".$i);
					$obj = $this->db->fetch_object($resql);

					//var_dump($this->lines[$i]);
					$mouvS = new MouvementStock($this->db);
					// We decrement stock of product (and sub-products)
					// We use warehouse selected for each line
					$result=$mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, $obj->subprice);
					if ($result < 0) { $error++; break; }

					$i++;
				}
			}
			else
			{
				$this->db->rollback();
				$this->error=$this->db->error();
				dol_syslog("Expedition::valid ".$this->error, LOG_ERR);
				return -2;
			}
		}

		if (! $error)
		{
			// On efface le repertoire de pdf provisoire
			$expeditionref = dol_sanitizeFileName($this->ref);
			if ($conf->expedition->dir_output)
			{
				$dir = $conf->expedition->dir_output . "/" . $expeditionref;
				$file = $dir . "/" . $expeditionref . ".pdf";
				if (file_exists($file))
				{
					if (!dol_delete_file($file))
					{
						$this->error=$langs->trans("ErrorCanNotDeleteFile",$file);
					}
				}
				if (file_exists($dir))
				{
					if (!dol_delete_dir($dir))
					{
						$this->error=$langs->trans("ErrorCanNotDeleteDir",$dir);
					}
				}
			}
		}

		// Set new ref and current status
		if (! $error)
		{
			$this->ref = $numref;
			$this->statut = 1;
		}

		if (! $error)
		{
			// Appel des triggers
			include_once(DOL_DOCUMENT_ROOT."/core/class/interfaces.class.php");
			$interface=new Interfaces($this->db);
			$result=$interface->run_triggers('SHIPPING_VALIDATE',$this,$user,$langs,$conf);
			if ($result < 0) { $error++; $this->errors=$interface->errors; }
			// Fin appel triggers
		}

		if (! $error)
		{
			$this->db->commit();
			return 1;
		}
		else
		{
			foreach($this->errors as $errmsg)
			{
	            dol_syslog(get_class($this)."::valid ".$errmsg, LOG_ERR);
	            $this->error.=($this->error?', '.$errmsg:$errmsg);
			}
			$this->db->rollback();
			return -1*$error;
		}
	}
コード例 #7
0
ファイル: facture.class.php プロジェクト: ADDAdev/Dolibarr
 /**
  * Tag invoice as validated + call trigger BILL_VALIDATE
  * Object must have lines loaded with fetch_lines
  *
  * @param	User	$user           Object user that validate
  * @param   string	$force_number	Reference to force on invoice
  * @param	int		$idwarehouse	Id of warehouse to use for stock decrease if option to decreasenon stock is on (0=no decrease)
  * @param	int		$notrigger		1=Does not execute triggers, 0= execuete triggers
  * @return	int						<0 if KO, >0 if OK
  */
 function validate($user, $force_number = '', $idwarehouse = 0, $notrigger = 0)
 {
     global $conf, $langs;
     require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
     $now = dol_now();
     $error = 0;
     dol_syslog(get_class($this) . '::validate user='******', force_number=' . $force_number . ', idwarehouse=' . $idwarehouse);
     // Check parameters
     if (!$this->brouillon) {
         dol_syslog(get_class($this) . "::validate no draft status", LOG_WARNING);
         return 0;
     }
     if (!$user->rights->facture->valider) {
         $this->error = 'Permission denied';
         dol_syslog(get_class($this) . "::validate " . $this->error, LOG_ERR);
         return -1;
     }
     $this->db->begin();
     $this->fetch_thirdparty();
     $this->fetch_lines();
     // Check parameters
     if ($this->type == self::TYPE_REPLACEMENT) {
         // Controle que facture source connue
         if ($this->fk_facture_source <= 0) {
             $this->error = $langs->trans("ErrorFieldRequired", $langs->trans("InvoiceReplacement"));
             $this->db->rollback();
             return -10;
         }
         // Charge la facture source a remplacer
         $facreplaced = new Facture($this->db);
         $result = $facreplaced->fetch($this->fk_facture_source);
         if ($result <= 0) {
             $this->error = $langs->trans("ErrorBadInvoice");
             $this->db->rollback();
             return -11;
         }
         // Controle que facture source non deja remplacee par une autre
         $idreplacement = $facreplaced->getIdReplacingInvoice('validated');
         if ($idreplacement && $idreplacement != $this->id) {
             $facreplacement = new Facture($this->db);
             $facreplacement->fetch($idreplacement);
             $this->error = $langs->trans("ErrorInvoiceAlreadyReplaced", $facreplaced->ref, $facreplacement->ref);
             $this->db->rollback();
             return -12;
         }
         $result = $facreplaced->set_canceled($user, 'replaced', '');
         if ($result < 0) {
             $this->error = $facreplaced->error;
             $this->db->rollback();
             return -13;
         }
     }
     // Define new ref
     if ($force_number) {
         $num = $force_number;
     } else {
         if (preg_match('/^[\\(]?PROV/i', $this->ref)) {
             if (!empty($conf->global->FAC_FORCE_DATE_VALIDATION)) {
                 $this->date = dol_now();
                 $this->date_lim_reglement = $this->calculate_date_lim_reglement();
             }
             $num = $this->getNextNumRef($this->client);
         } else {
             $num = $this->ref;
         }
     }
     $this->newref = $num;
     if ($num) {
         $this->update_price(1);
         // Validate
         $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture';
         $sql .= " SET facnumber='" . $num . "', fk_statut = 1, fk_user_valid = " . $user->id . ", date_valid = '" . $this->db->idate($now) . "'";
         if (!empty($conf->global->FAC_FORCE_DATE_VALIDATION)) {
             $sql .= ", datef='" . $this->db->idate($this->date) . "'";
             $sql .= ", date_lim_reglement='" . $this->db->idate($this->date_lim_reglement) . "'";
         }
         $sql .= ' WHERE rowid = ' . $this->id;
         dol_syslog(get_class($this) . "::validate", LOG_DEBUG);
         $resql = $this->db->query($sql);
         if (!$resql) {
             dol_print_error($this->db);
             $error++;
         }
         // On verifie si la facture etait une provisoire
         if (!$error && preg_match('/^[\\(]?PROV/i', $this->ref)) {
             // La verif qu'une remise n'est pas utilisee 2 fois est faite au moment de l'insertion de ligne
         }
         if (!$error) {
             // Define third party as a customer
             $result = $this->client->set_as_client();
             // Si active on decremente le produit principal et ses composants a la validation de facture
             if ($this->type != self::TYPE_DEPOSIT && $result >= 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_BILL) && $idwarehouse > 0) {
                 require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php';
                 $langs->load("agenda");
                 // Loop on each line
                 $cpt = count($this->lines);
                 for ($i = 0; $i < $cpt; $i++) {
                     if ($this->lines[$i]->fk_product > 0) {
                         $mouvP = new MouvementStock($this->db);
                         $mouvP->origin =& $this;
                         // We decrease stock for product
                         if ($this->type == self::TYPE_CREDIT_NOTE) {
                             $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarr", $num));
                         } else {
                             $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarr", $num));
                         }
                         if ($result < 0) {
                             $error++;
                         }
                     }
                 }
             }
         }
         // Trigger calls
         if (!$error && !$notrigger) {
             // Call trigger
             $result = $this->call_trigger('BILL_VALIDATE', $user);
             if ($result < 0) {
                 $error++;
             }
             // End call triggers
         }
         if (!$error) {
             $this->oldref = $this->ref;
             // Rename directory if dir was a temporary ref
             if (preg_match('/^[\\(]?PROV/i', $this->ref)) {
                 // Rename of object directory ($this->ref = old ref, $num = new ref)
                 // to  not lose the linked files
                 $oldref = dol_sanitizeFileName($this->ref);
                 $newref = dol_sanitizeFileName($num);
                 $dirsource = $conf->facture->dir_output . '/' . $oldref;
                 $dirdest = $conf->facture->dir_output . '/' . $newref;
                 if (file_exists($dirsource)) {
                     dol_syslog(get_class($this) . "::validate rename dir " . $dirsource . " into " . $dirdest);
                     if (@rename($dirsource, $dirdest)) {
                         dol_syslog("Rename ok");
                         // Rename docs starting with $oldref with $newref
                         $listoffiles = dol_dir_list($conf->facture->dir_output . '/' . $newref, 'files', 1, '^' . preg_quote($oldref, '/'));
                         foreach ($listoffiles as $fileentry) {
                             $dirsource = $fileentry['name'];
                             $dirdest = preg_replace('/^' . preg_quote($oldref, '/') . '/', $newref, $dirsource);
                             $dirsource = $fileentry['path'] . '/' . $dirsource;
                             $dirdest = $fileentry['path'] . '/' . $dirdest;
                             @rename($dirsource, $dirdest);
                         }
                     }
                 }
             }
         }
         // Set new ref and define current statut
         if (!$error) {
             $this->ref = $num;
             $this->facnumber = $num;
             $this->statut = 1;
             $this->brouillon = 0;
             $this->date_validation = $now;
         }
     } else {
         $error++;
     }
     if (!$error) {
         $this->db->commit();
         return 1;
     } else {
         $this->db->rollback();
         return -1;
     }
 }
コード例 #8
0
ファイル: SocieteTest.php プロジェクト: nrjacker4/crm-php
    /**
     * testSocieteOther
     *
     * @param	Societe	$localobject	Company
     * @return	int		$id				Id of company
     *
     * @depends	testIdProfCheck
     * The depends says test is run only if previous is ok
     */
    public function testSocieteOther($localobject)
    {
    	global $conf,$user,$langs,$db;
		$conf=$this->savconf;
		$user=$this->savuser;
		$langs=$this->savlangs;
		$db=$this->savdb;

        $result=$localobject->set_as_client();
        print __METHOD__." id=".$localobject->id." result=".$result."\n";
        $this->assertLessThan($result, 0);

        $result=$localobject->set_price_level(1,$user);
        print __METHOD__." id=".$localobject->id." result=".$result."\n";
        $this->assertLessThan($result, 0);

        $result=$localobject->set_remise_client(10,'Gift',$user);
        print __METHOD__." id=".$localobject->id." result=".$result."\n";
        $this->assertLessThan($result, 0);

        $result=$localobject->getNomUrl(1);
        print __METHOD__." id=".$localobject->id." result=".$result."\n";
        $this->assertNotEquals($result, '');

        $result=$localobject->isInEEC();
        print __METHOD__." id=".$localobject->id." country_code=".$this->country_code." result=".$result."\n";
        $this->assertTrue(true, $result);

        $localobject->info($localobject->id);
        print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
        $this->assertNotEquals($localobject->date_creation, '');

        return $localobject->id;
    }
コード例 #9
0
 /**
  *  Validate object and update stock if option enabled
  *
  *  @param      User		$user       Object user that validate
  *  @return     int						<0 if OK, >0 if KO
  */
 function valid($user)
 {
     global $conf, $langs;
     require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
     dol_syslog(get_class($this) . "::valid");
     // Protection
     if ($this->statut) {
         dol_syslog(get_class($this) . "::valid no draft status", LOG_WARNING);
         return 0;
     }
     if (!$user->rights->expedition->valider) {
         $this->error = 'Permission denied';
         dol_syslog(get_class($this) . "::valid " . $this->error, LOG_ERR);
         return -1;
     }
     $this->db->begin();
     $error = 0;
     // Define new ref
     $soc = new Societe($this->db);
     $soc->fetch($this->socid);
     // Class of company linked to order
     $result = $soc->set_as_client();
     // Define new ref
     if (!$error && preg_match('/^[\\(]?PROV/i', $this->ref)) {
         $numref = $this->getNextNumRef($soc);
     } else {
         $numref = "EXP" . $this->id;
     }
     $now = dol_now();
     // Validate
     $sql = "UPDATE " . MAIN_DB_PREFIX . "expedition SET";
     $sql .= " ref='" . $numref . "'";
     $sql .= ", fk_statut = 1";
     $sql .= ", date_valid = '" . $this->db->idate($now) . "'";
     $sql .= ", fk_user_valid = " . $user->id;
     $sql .= " WHERE rowid = " . $this->id;
     dol_syslog(get_class($this) . "::valid update expedition sql=" . $sql);
     $resql = $this->db->query($sql);
     if (!$resql) {
         dol_syslog(get_class($this) . "::valid Echec update - 10 - sql=" . $sql, LOG_ERR);
         $this->error = $this->db->lasterror();
         $error++;
     }
     // If stock increment is done on sending (recommanded choice)
     if (!$error && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT)) {
         require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php';
         $langs->load("agenda");
         // Loop on each product line to add a stock movement
         // TODO possibilite d'expedier a partir d'une propale ou autre origine
         $sql = "SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot, ed.rowid";
         $sql .= " FROM " . MAIN_DB_PREFIX . "commandedet as cd,";
         $sql .= " " . MAIN_DB_PREFIX . "expeditiondet as ed";
         $sql .= " WHERE ed.fk_expedition = " . $this->id;
         $sql .= " AND cd.rowid = ed.fk_origin_line";
         dol_syslog(get_class($this) . "::valid select details sql=" . $sql);
         $resql = $this->db->query($sql);
         if ($resql) {
             $cpt = $this->db->num_rows($resql);
             for ($i = 0; $i < $cpt; $i++) {
                 dol_syslog(get_class($this) . "::valid movement index " . $i);
                 $obj = $this->db->fetch_object($resql);
                 //var_dump($this->lines[$i]);
                 $mouvS = new MouvementStock($this->db);
                 $mouvS->origin =& $this;
                 // We decrement stock of product (and sub-products)
                 // We use warehouse selected for each line
                 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, $obj->subprice, $langs->trans("ShipmentValidatedInDolibarr", $numref));
                 if ($result < 0) {
                     $error++;
                     break;
                 }
                 if (!empty($conf->productbatch->enabled)) {
                     $details = ExpeditionLigneBatch::FetchAll($this->db, $obj->rowid);
                     foreach ($details as $dbatch) {
                         $result = $mouvS->livraison_batch($dbatch->fk_origin_stock, $dbatch->dluo_qty);
                         if ($result < 0) {
                             $error++;
                             $this->errors[] = $mouvS->{$error};
                             break 2;
                         }
                     }
                 }
             }
         } else {
             $this->db->rollback();
             $this->error = $this->db->error();
             dol_syslog(get_class($this) . "::valid " . $this->error, LOG_ERR);
             return -2;
         }
     }
     if (!$error) {
         $this->oldref = '';
         // Rename directory if dir was a temporary ref
         if (preg_match('/^[\\(]?PROV/i', $this->ref)) {
             // On renomme repertoire ($this->ref = ancienne ref, $numfa = nouvelle ref)
             // in order not to lose the attached files
             $oldref = dol_sanitizeFileName($this->ref);
             $newref = dol_sanitizeFileName($numref);
             $dirsource = $conf->expedition->dir_output . '/sending/' . $oldref;
             $dirdest = $conf->expedition->dir_output . '/sending/' . $newref;
             if (file_exists($dirsource)) {
                 dol_syslog(get_class($this) . "::valid rename dir " . $dirsource . " into " . $dirdest);
                 if (@rename($dirsource, $dirdest)) {
                     $this->oldref = $oldref;
                     dol_syslog("Rename ok");
                     // Suppression ancien fichier PDF dans nouveau rep
                     dol_delete_file($dirdest . '/' . $oldref . '*.*');
                 }
             }
         }
     }
     // Set new ref and current status
     if (!$error) {
         $this->ref = $numref;
         $this->statut = 1;
     }
     if (!$error) {
         // Appel des triggers
         include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
         $interface = new Interfaces($this->db);
         $result = $interface->run_triggers('SHIPPING_VALIDATE', $this, $user, $langs, $conf);
         if ($result < 0) {
             $error++;
             $this->errors = $interface->errors;
         }
         // Fin appel triggers
     }
     if (!$error) {
         $this->db->commit();
         return 1;
     } else {
         foreach ($this->errors as $errmsg) {
             dol_syslog(get_class($this) . "::valid " . $errmsg, LOG_ERR);
             $this->error .= $this->error ? ', ' . $errmsg : $errmsg;
         }
         $this->db->rollback();
         return -1 * $error;
     }
 }
コード例 #10
0
 /**
  *	Close the commercial proposal
  *
  *	@param      User	$user		Object user that close
  *	@param      int		$statut		Statut
  *	@param      string	$note		Comment
  *	@return     int         		<0 if KO, >0 if OK
  */
 function cloture($user, $statut, $note)
 {
     global $langs, $conf;
     $this->statut = $statut;
     $error = 0;
     $now = dol_now();
     $this->db->begin();
     $sql = "UPDATE " . MAIN_DB_PREFIX . "propal";
     $sql .= " SET fk_statut = " . $statut . ", note_private = '" . $this->db->escape($note) . "', date_cloture='" . $this->db->idate($now) . "', fk_user_cloture=" . $user->id;
     $sql .= " WHERE rowid = " . $this->id;
     $resql = $this->db->query($sql);
     if ($resql) {
         if ($statut == 2) {
             // The connected company is classified as a client
             $soc = new Societe($this->db);
             $soc->id = $this->socid;
             $result = $soc->set_as_client();
             if ($result < 0) {
                 $this->error = $this->db->error();
                 $this->db->rollback();
                 return -2;
             }
             if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
                 // Define output language
                 $outputlangs = $langs;
                 if (!empty($conf->global->MAIN_MULTILANGS)) {
                     $outputlangs = new Translate("", $conf);
                     $newlang = GETPOST('lang_id') ? GETPOST('lang_id') : $this->client->default_lang;
                     $outputlangs->setDefaultLang($newlang);
                 }
                 //$ret=$object->fetch($id);    // Reload to get new records
                 propale_pdf_create($this->db, $this, $conf->global->PROPALE_ADDON_PDF_ODT_TOBILL ? $conf->global->PROPALE_ADDON_PDF_ODT_TOBILL : $this->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
             }
             // Appel des triggers
             include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
             $interface = new Interfaces($this->db);
             $result = $interface->run_triggers('PROPAL_CLOSE_SIGNED', $this, $user, $langs, $conf);
             if ($result < 0) {
                 $error++;
                 $this->errors = $interface->errors;
             }
             // Fin appel triggers
         } else {
             if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
                 // Define output language
                 $outputlangs = $langs;
                 if (!empty($conf->global->MAIN_MULTILANGS)) {
                     $outputlangs = new Translate("", $conf);
                     $newlang = GETPOST('lang_id') ? GETPOST('lang_id') : $this->client->default_lang;
                     $outputlangs->setDefaultLang($newlang);
                 }
                 //$ret=$object->fetch($id);    // Reload to get new records
                 propale_pdf_create($this->db, $this, $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED ? $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED : $this->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
             }
             // Appel des triggers
             include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
             $interface = new Interfaces($this->db);
             $result = $interface->run_triggers('PROPAL_CLOSE_REFUSED', $this, $user, $langs, $conf);
             if ($result < 0) {
                 $error++;
                 $this->errors = $interface->errors;
             }
             // Fin appel triggers
         }
         $this->db->commit();
         return 1;
     } else {
         $this->error = $this->db->error();
         $this->db->rollback();
         return -1;
     }
 }