/** * 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; } }
/** * 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; } }