function mouvement(&$PDOdb, &$object, $fk_product, $qty, $fk_warehouse_from, $fk_warehouse_to)
 {
     global $db, $user, $langs;
     dol_include_once('/product/stock/class/mouvementstock.class.php');
     dol_include_once('/product/class/product.class.php');
     /*var_dump($fk_product, $qty,$fk_warehouse_from, $fk_warehouse_to);
     		exit;
     			*/
     $stock = new MouvementStock($db);
     $label = '';
     if (method_exists($object, 'getNomUrl')) {
         $label .= $object->getNomUrl(1);
     }
     if (!empty($conf->global->ROUTING_INFO_ALERT)) {
         $product = new Product($db);
         $product->fetch($fk_product);
         $msg = $product->getNomUrl(0) . ' ' . $product->label . ' ' . $langs->trans('MoveFrom') . ' ' . $wh_from_label . ' ' . $langs->trans('MoveTo') . ' ' . $wh_to_label;
         setEventMessage($msg);
     }
     $stock->origin = $object;
     $stock->reception($user, $fk_product, $fk_warehouse_to, $qty, 0, $label);
     $stock->livraison($user, $fk_product, $fk_warehouse_from, $qty, 0, $label);
 }
Exemple #2
0
 /**
  *  \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;
     }
 }
Exemple #3
0
 /**
  *	Set draft status
  *
  *	@param	User	$user			Object user that modify
  *	@param	int		$idwarehouse	Id warehouse to use for stock change.
  *	@return	int						<0 if KO, >0 if OK
  */
 function set_draft($user, $idwarehouse = -1)
 {
     global $conf, $langs;
     $error = 0;
     if ($this->statut == self::STATUS_DRAFT) {
         dol_syslog(get_class($this) . "::set_draft already draft status", LOG_WARNING);
         return 0;
     }
     $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) {
         // 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)) {
             require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php';
             $langs->load("agenda");
             $num = count($this->lines);
             for ($i = 0; $i < $num; $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->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("InvoiceBackToDraftInDolibarr", $this->ref));
                     } else {
                         $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans("InvoiceBackToDraftInDolibarr", $this->ref));
                     }
                     // we use 0 for price, to not change the weighted average value
                 }
             }
         }
         if ($error == 0) {
             $old_statut = $this->statut;
             $this->brouillon = 1;
             $this->statut = self::STATUS_DRAFT;
             // Call trigger
             $result = $this->call_trigger('BILL_UNVALIDATE', $user);
             if ($result < 0) {
                 $error++;
                 $this->statut = $old_statut;
                 $this->brouillon = 0;
             }
             // End call triggers
         } else {
             $this->db->rollback();
             return -1;
         }
         if ($error == 0) {
             $this->db->commit();
             return 1;
         } else {
             $this->db->rollback();
             return -1;
         }
     } else {
         $this->error = $this->db->error();
         $this->db->rollback();
         return -1;
     }
 }
 /**
  *  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;
     }
 }
Exemple #5
0
 /**
  *	Set draft status
  *
  *	@param	User	$user			Object user that modify
  *	@param	int		$idwarehouse	Id warehouse to use for stock change.
  *	@return	int						<0 if KO, >0 if OK
  */
 function set_draft($user, $idwarehouse = -1)
 {
     global $conf, $langs;
     $error = 0;
     if ($this->statut == 0) {
         dol_syslog(get_class($this) . "::set_draft already draft status", LOG_WARNING);
         return 0;
     }
     $this->db->begin();
     $sql = "UPDATE " . MAIN_DB_PREFIX . "facture";
     $sql .= " SET fk_statut = 0";
     $sql .= " WHERE rowid = " . $this->id;
     dol_syslog(get_class($this) . "::set_draft sql=" . $sql, LOG_DEBUG);
     $result = $this->db->query($sql);
     if ($result) {
         // Si on decremente le produit principal et ses composants a la validation de facture, on réincrement
         if ($this->type != 3 && $result >= 0 && $conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_BILL) {
             require_once DOL_DOCUMENT_ROOT . "/product/stock/class/mouvementstock.class.php";
             $langs->load("agenda");
             $num = count($this->lines);
             for ($i = 0; $i < $num; $i++) {
                 if ($this->lines[$i]->fk_product > 0) {
                     $mouvP = new MouvementStock($this->db);
                     // We decrease stock for product
                     if ($this->type == 2) {
                         $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("InvoiceBackToDraftInDolibarr", $this->ref));
                     } else {
                         $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("InvoiceBackToDraftInDolibarr", $this->ref));
                     }
                 }
             }
         }
         if ($error == 0) {
             $this->db->commit();
             return 1;
         } else {
             $this->db->rollback();
             return -1;
         }
     } else {
         $this->error = $this->db->error();
         $this->db->rollback();
         return -1;
     }
 }
Exemple #6
0
 /**
  *	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;
     }
 }
 private function quitSotck($lines, $isreturn = false)
 {
     global $db, $langs;
     require_once DOL_DOCUMENT_ROOT . "/product/stock/class/mouvementstock.class.php";
     $userstatic = new User($db);
     $userstatic->fetch($_SESSION['uid']);
     $error = 0;
     $cash = new Cash($db);
     $terminal = $_SESSION['TERMINAL_ID'];
     $cash->fetch($terminal);
     $warehouse = $cash->fk_warehouse;
     foreach ($lines as $line) {
         if (sizeof($line) > 0) {
             if ($line['idProduct']) {
                 $mouvP = new MouvementStock($db);
                 // We decrease stock for product
                 if (!$isreturn) {
                     $result = $mouvP->livraison($userstatic, $line['idProduct'], $warehouse, $line['cant'], $line['price'], $langs->trans("TicketCreatedInDolibarr"));
                 } else {
                     $result = $mouvP->reception($userstatic, $line['idProduct'], $warehouse, $line['cant'], $line['price'], $langs->trans("TicketCreatedInDolibarr"));
                 }
                 if ($result < 0) {
                     $error++;
                 }
             }
         }
     }
     return $error;
 }
	/**
	 *        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;
		}
	}
Exemple #9
0
 /**
  *      Tag invoice as validated + call trigger BILL_VALIDATE
  *      @param     	user            Object user that validate
  *      @param     	force_number	Reference to force on invoice
  *	    @return		int				<0 if KO, >0 if OK
  */
 function validate($user, $force_number = '')
 {
     global $conf, $langs;
     require_once DOL_DOCUMENT_ROOT . "/lib/files.lib.php";
     $error = 0;
     // Protection
     if (!$this->brouillon) {
         dol_syslog("Facture::validate no draft status", LOG_WARNING);
         return 0;
     }
     if (!$user->rights->facture->valider) {
         $this->error = 'Permission denied';
         dol_syslog("Facture::validate " . $this->error, LOG_ERR);
         return -1;
     }
     $this->db->begin();
     $this->fetch_thirdparty();
     $this->fetch_lines();
     // Check parameters
     if ($this->type == 1) {
         // 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 . " sql=" . $sql;
             $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;
         }
     }
     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;
         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("Facture::validate sql=" . $sql);
         $resql = $this->db->query($sql);
         if (!$resql) {
             dol_syslog("Facture::validate Echec update - 10 - sql=" . $sql, LOG_ERR);
             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 ($result >= 0 && $conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_BILL) {
                 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 decrease stock for product
                         $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, $langs->trans("InvoiceValidatedInDolibarr", $num));
                         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 facture ($this->ref = ancienne ref, $num = nouvelle ref)
                 // afin de ne pas perdre les fichiers attaches
                 $facref = dol_sanitizeFileName($this->ref);
                 $snumfa = dol_sanitizeFileName($num);
                 $dirsource = $conf->facture->dir_output . '/' . $facref;
                 $dirdest = $conf->facture->dir_output . '/' . $snumfa;
                 if (file_exists($dirsource)) {
                     dol_syslog("Facture::validate rename dir " . $dirsource . " into " . $dirdest);
                     if (@rename($dirsource, $dirdest)) {
                         $this->oldref = $facref;
                         dol_syslog("Rename ok");
                         // Suppression ancien fichier PDF dans nouveau rep
                         dol_delete_file($conf->facture->dir_output . '/' . $snumfa . '/' . $facref . '.*');
                     }
                 }
             }
         }
         // Set new ref and define current statut
         if (!$error) {
             $this->ref = $num;
             $this->facnumber = $num;
             $this->statut = 1;
         }
         $this->use_webcal = $conf->global->PHPWEBCALENDAR_BILLSTATUS == 'always' ? 1 : 0;
         // Trigger calls
         if (!$error) {
             // Appel des triggers
             include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php";
             $interface = new Interfaces($this->db);
             $result = $interface->run_triggers('BILL_VALIDATE', $this, $user, $langs, $conf);
             if ($result < 0) {
                 $error++;
                 $this->errors = $interface->errors;
             }
             // Fin appel triggers
         }
     } else {
         $error++;
     }
     if (!$error) {
         $this->db->commit();
         return 1;
     } else {
         $this->db->rollback();
         $this->error = $this->db->lasterror();
         return -1;
     }
 }
 /**
  *	Set draft status
  *
  *	@param	User	$user			Object user that modify
  *	@param	int		$idwarehouse	Id warehouse to use for stock change.
  *	@return	int						<0 if KO, >0 if OK
  */
 function set_draft($user, $idwarehouse = -1)
 {
     global $conf, $langs;
     $error = 0;
     if ($this->statut == self::STATUS_DRAFT) {
         dol_syslog(get_class($this) . "::set_draft already draft status", LOG_WARNING);
         return 0;
     }
     $this->db->begin();
     $sql = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn";
     $sql .= " SET fk_statut = 0";
     $sql .= " WHERE rowid = " . $this->id;
     dol_syslog(get_class($this) . "::set_draft", LOG_DEBUG);
     $result = $this->db->query($sql);
     if ($result) {
         // 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)) {
             require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php';
             $langs->load("agenda");
             $cpt = count($this->lines);
             for ($i = 0; $i < $cpt; $i++) {
                 if ($this->lines[$i]->fk_product > 0) {
                     $mouvP = new MouvementStock($this->db);
                     // We increase stock for product
                     $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("InvoiceBackToDraftInDolibarr", $this->ref));
                 }
             }
         }
         // Triggers call
         if (!$error && empty($notrigger)) {
             // Call trigger
             $result = $this->call_trigger('BILL_SUPPLIER_VALIDATE', $user);
             if ($result < 0) {
                 $error++;
             }
             // End call triggers
         }
         if ($error == 0) {
             $this->db->commit();
             return 1;
         } else {
             $this->db->rollback();
             return -1;
         }
     } else {
         $this->error = $this->db->error();
         $this->db->rollback();
         return -1;
     }
 }
    /**
     *		Set draft status
     *		@param		user		Object user that modify
     *		@param		int			<0 if KO, >0 if OK
     */
    function set_draft($user)
    {
        global $conf,$langs;

        $error=0;

        if ($this->statut == 0)
        {
            dol_syslog("FactureFournisseur::set_draft already draft status", LOG_WARNING);
            return 0;
        }

        $this->db->begin();

        $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn";
        $sql.= " SET fk_statut = 0";
        $sql.= " WHERE rowid = ".$this->id;

        dol_syslog("FactureFournisseur::set_draft sql=".$sql, LOG_DEBUG);
        if ($this->db->query($sql))
        {
            // Si on incremente le produit principal et ses composants a la validation de facture fournisseur, on decremente
            if ($result >= 0 && $conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)
            {
                require_once(DOL_DOCUMENT_ROOT."/product/stock/class/mouvementstock.class.php");
                $langs->load("agenda");

                for ($i = 0 ; $i < sizeof($this->lines) ; $i++)
                {
                    if ($this->lines[$i]->fk_product > 0)
                    {
                        $mouvP = new MouvementStock($this->db);
                        // We increase stock for product
                        $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, $langs->trans("InvoiceBackToDraftInDolibarr",$this->ref));
                    }
                }
            }

            if ($error == 0)
            {
                $this->db->commit();
                return 1;
            }
            else
            {
                $this->db->rollback();
                return -1;
            }
        }
        else
        {
            $this->error=$this->db->error();
            $this->db->rollback();
            return -1;
        }
    }
 $resultvalid = $invoice->validate($user, $obj_facturation->numInvoice(), 0);
 if ($warehouseidtodecrease > 0) {
     // Decrease
     require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php';
     $langs->load("agenda");
     // Loop on each line
     $cpt = count($invoice->lines);
     for ($i = 0; $i < $cpt; $i++) {
         if ($invoice->lines[$i]->fk_product > 0) {
             $mouvP = new MouvementStock($db);
             $mouvP->origin =& $invoice;
             // We decrease stock for product
             if ($invoice->type == $invoice::TYPE_CREDIT_NOTE) {
                 $result = $mouvP->reception($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarrFromPos", $invoice->newref));
             } else {
                 $result = $mouvP->livraison($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarrFromPos", $invoice->newref));
             }
             if ($result < 0) {
                 $error++;
             }
         }
     }
 }
 $id = $invoice->id;
 // Add the payment
 $payment = new Paiement($db);
 $payment->datepaye = $now;
 $payment->bank_account = $conf_fkaccount;
 $payment->amounts[$invoice->id] = $obj_facturation->prixTotalTtc();
 $payment->note = $langs->trans("Payment") . ' ' . $langs->trans("Invoice") . ' ' . $obj_facturation->numInvoice();
 $payment->paiementid = $invoice->mode_reglement_id;
 /**
  *  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;
     }
 }
 static function addStockMouvementDolibarr($fk_product, $qty, $description, $fk_entrepot, $price = 0)
 {
     global $db, $user, $conf;
     require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
     require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php';
     $mouvS = new MouvementStock($db);
     $conf->global->PRODUIT_SOUSPRODUITS = false;
     // Dans le cas asset il ne faut pas de destocke recurssif
     if ($fk_entrepot > 0) {
         if ($qty > 0) {
             $result = $mouvS->reception($user, $fk_product, $fk_entrepot, $qty, $price, $description);
         } else {
             $result = $mouvS->livraison($user, $fk_product, $fk_entrepot, -$qty, $price, $description);
         }
     }
 }