28 |
*/ |
*/ |
29 |
|
|
30 |
|
|
31 |
/** \class Expedition |
/** |
32 |
|
\class Expedition |
33 |
\brief Classe de gestion des expeditions |
\brief Classe de gestion des expeditions |
34 |
*/ |
*/ |
35 |
class Expedition |
class Expedition |
38 |
var $id ; |
var $id ; |
39 |
var $brouillon; |
var $brouillon; |
40 |
var $entrepot_id; |
var $entrepot_id; |
41 |
|
|
42 |
/** |
/** |
43 |
* Initialisation |
* Initialisation |
44 |
* |
* |
224 |
} |
} |
225 |
|
|
226 |
/** |
/** |
227 |
* \brief Valide l'expedition |
* \brief Valide l'expedition, et met a jour le stock si stock géré |
228 |
* \param user Objet de l'utilisateur qui valide |
* \param user Objet de l'utilisateur qui valide |
229 |
* \return int |
* \return int |
230 |
*/ |
*/ |
231 |
function valid($user) |
function valid($user) |
232 |
{ |
{ |
233 |
|
global $conf; |
234 |
|
|
235 |
require_once DOL_DOCUMENT_ROOT ."/product/stock/mouvementstock.class.php"; |
require_once DOL_DOCUMENT_ROOT ."/product/stock/mouvementstock.class.php"; |
236 |
|
|
237 |
$result = 0; |
dolibarr_syslog("expedition.class.php::valid"); |
238 |
|
|
239 |
|
$this->db->begin(); |
240 |
|
|
241 |
|
$error = 0; |
242 |
|
|
243 |
if ($user->rights->expedition->valider) |
if ($user->rights->expedition->valider) |
244 |
{ |
{ |
245 |
$this->ref = "EXP".$this->id; |
$this->ref = "EXP".$this->id; |
246 |
|
|
247 |
|
// \todo Tester si non dejà au statut validé. Si oui, on arrete afin d'éviter |
248 |
|
// de décrémenter 2 fois le stock. |
249 |
|
|
250 |
$sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET ref='".$this->ref."', fk_statut = 1, date_valid=now(), fk_user_valid=$user->id"; |
$sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET ref='".$this->ref."', fk_statut = 1, date_valid=now(), fk_user_valid=$user->id"; |
251 |
$sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; |
$sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; |
252 |
|
|
253 |
if ($this->db->query($sql) ) |
if ($this->db->query($sql) ) |
254 |
{ |
{ |
|
$result = 1; |
|
|
|
|
255 |
// Si module stock géré et que expedition faite depuis un entrepot |
// Si module stock géré et que expedition faite depuis un entrepot |
256 |
if ($conf->stock->enabled && $this->entrepot_id) |
if ($conf->stock->enabled && $this->entrepot_id) |
257 |
{ |
{ |
258 |
/* |
/* |
259 |
* Enregistrement d'un mouvement de stock pour chaque produit de l'expedition |
* Enregistrement d'un mouvement de stock pour chaque produit de l'expedition |
260 |
*/ |
*/ |
261 |
|
|
262 |
|
dolibarr_syslog("expedition.class.php::valid enregistrement des mouvements"); |
263 |
|
|
264 |
|
$sql = "SELECT cd.fk_product, ed.qty "; |
265 |
|
$sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."expeditiondet as ed"; |
266 |
|
$sql.= " WHERE ed.fk_expedition = $this->id AND cd.rowid = ed.fk_commande_ligne"; |
267 |
|
|
268 |
$sql = "SELECT cd.fk_product, ed.qty "; |
$resql=$this->db->query($sql); |
269 |
$sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd , ".MAIN_DB_PREFIX."expeditiondet as ed"; |
if ($resql) |
|
$sql .= " WHERE ed.fk_expedition = $this->id AND cd.rowid = ed.fk_commande_ligne "; |
|
|
|
|
|
if ($this->db->query($sql)) |
|
270 |
{ |
{ |
271 |
$num = $this->db->num_rows(); |
$num = $this->db->num_rows($resql); |
272 |
$i=0; |
$i=0; |
273 |
while($i < $num) |
while($i < $num) |
274 |
{ |
{ |
275 |
|
dolibarr_syslog("expedition.class.php::valid movment $i"); |
276 |
|
|
277 |
|
$obj = $this->db->fetch_object($resql); |
278 |
|
|
279 |
$mouvS = new MouvementStock($this->db); |
$mouvS = new MouvementStock($this->db); |
280 |
$obj = $this->db->fetch_object(); |
$result=$mouvS->livraison($user, $obj->fk_product, $this->entrepot_id, $obj->qty); |
281 |
$mouvS->livraison($user, $obj->fk_product, $this->entrepot_id, $obj->qty, 0); |
if ($result < 0) |
282 |
|
{ |
283 |
|
$this->db->rollback(); |
284 |
|
$this->error=$this->db->error()." - sql=$sql"; |
285 |
|
dolibarr_syslog("expedition.class.php::valid ".$this->error); |
286 |
|
return -3; |
287 |
|
} |
288 |
$i++; |
$i++; |
289 |
} |
} |
290 |
|
|
291 |
|
} |
292 |
|
else |
293 |
|
{ |
294 |
|
$this->db->rollback(); |
295 |
|
$this->error=$this->db->error()." - sql=$sql"; |
296 |
|
dolibarr_syslog("expedition.class.php::valid ".$this->error); |
297 |
|
return -2; |
298 |
} |
} |
299 |
} |
} |
300 |
} |
} |
301 |
else |
else |
302 |
{ |
{ |
303 |
$result = -1; |
$this->db->rollback(); |
304 |
$this->error=$this->db->error()." - sql=".$sql; |
$this->error=$this->db->error()." - sql=$sql"; |
305 |
|
dolibarr_syslog("expedition.class.php::valid ".$this->error); |
306 |
|
return -1; |
307 |
} |
} |
308 |
} |
} |
309 |
return $result ; |
else |
310 |
|
{ |
311 |
|
$this->error="Non autorise"; |
312 |
|
dolibarr_syslog("expedition.class.php::valid ".$this->error); |
313 |
|
return -1; |
314 |
|
} |
315 |
|
|
316 |
|
$this->db->commit(); |
317 |
|
//dolibarr_syslog("expedition.class.php::valid commit"); |
318 |
|
return 1; |
319 |
} |
} |
320 |
|
|
321 |
|
|