123 |
return $result ; |
return $result ; |
124 |
|
|
125 |
} |
} |
|
/** |
|
|
* Log |
|
|
* |
|
|
*/ |
|
|
function log($user, $statut, $datelog) |
|
|
{ |
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur_log (datelog, fk_commande, fk_statut, fk_user)"; |
|
|
$sql .= " VALUES (".$this->db->idate($datelog).",".$this->id.", $statut, ".$user->id.")"; |
|
126 |
|
|
127 |
if ( $this->db->query($sql) ) |
/** |
128 |
{ |
* \brief Insère ligne de log |
129 |
return 0; |
* \param user Utilisateur qui modifie la commande |
130 |
} |
* \param statut Statut de la commande |
131 |
|
* \param datelog Date de modification |
132 |
|
* \return int <0 si ko, >0 si ok |
133 |
|
*/ |
134 |
|
function log($user, $statut, $datelog) |
135 |
|
{ |
136 |
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur_log (datelog, fk_commande, fk_statut, fk_user)"; |
137 |
|
$sql.= " VALUES (".$this->db->idate($datelog).",".$this->id.", $statut, ".$user->id.")"; |
138 |
|
|
139 |
|
if ( $this->db->query($sql) ) |
140 |
|
{ |
141 |
|
return 1; |
142 |
|
} |
143 |
|
else |
144 |
|
{ |
145 |
|
return -1; |
146 |
|
} |
147 |
} |
} |
148 |
|
|
149 |
/** |
/** |
150 |
* Valide la commande |
* Valide la commande |
151 |
* |
* |
404 |
} |
} |
405 |
return $result ; |
return $result ; |
406 |
} |
} |
|
/** |
|
|
* Créé la commande |
|
|
* |
|
|
*/ |
|
|
function create($user) |
|
|
{ |
|
|
dolibarr_syslog("CommandeFournisseur::Create"); |
|
|
dolibarr_syslog("CommandeFournisseur::Create soc id=".$this->soc_id); |
|
|
/* On positionne en mode brouillon la commande */ |
|
|
$this->brouillon = 1; |
|
|
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur (fk_soc, date_creation, fk_user_author, fk_statut) "; |
|
|
$sql .= " VALUES (".$this->soc_id.", now(), ".$user->id.",0)"; |
|
|
|
|
|
if ( $this->db->query($sql) ) |
|
|
{ |
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."commande_fournisseur"); |
|
407 |
|
|
408 |
$sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id; |
/** |
409 |
if ($this->db->query($sql)) |
* \brief Créé la commande au statut brouillon |
410 |
{ |
* \param user Utilisateur qui crée |
411 |
|
* \return int <0 si ko, >0 si ok |
412 |
/* |
*/ |
413 |
* |
function create($user) |
|
* |
|
|
*/ |
|
|
dolibarr_syslog("CommandeFournisseur::Create : Success"); |
|
|
return 0; |
|
|
} |
|
|
else |
|
|
{ |
|
|
dolibarr_syslog("CommandeFournisseur::Create : Failed 2"); |
|
|
return -2; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
dolibarr_syslog("CommandeFournisseur::Create : Failed 1"); |
|
|
dolibarr_syslog("CommandeFournisseur::Create : ".$this->db->error()); |
|
|
dolibarr_syslog("CommandeFournisseur::Create : ".$sql); |
|
|
return -1; |
|
|
} |
|
|
} |
|
|
/** |
|
|
* Ajoute une ligne de commande |
|
|
* |
|
|
*/ |
|
|
function addline($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0) |
|
414 |
{ |
{ |
415 |
$qty = ereg_replace(",",".",$qty); |
dolibarr_syslog("CommandeFournisseur::Create soc id=".$this->soc_id); |
|
$pu = ereg_replace(",",".",$pu); |
|
|
|
|
|
if ($this->brouillon && strlen(trim($desc))) |
|
|
{ |
|
|
if (strlen(trim($qty))==0) |
|
|
{ |
|
|
$qty=1; |
|
|
} |
|
|
|
|
|
if ($fk_product > 0) |
|
|
{ |
|
|
$prod = new Product($this->db, $fk_product); |
|
|
if ($prod->fetch($fk_product) > 0) |
|
|
{ |
|
|
$desc = $prod->libelle; |
|
|
$txtva = $prod->tva_tx; |
|
|
|
|
|
$prod->get_buyprice($this->fourn_id,$qty); |
|
|
|
|
|
$pu = $prod->buyprice/$qty; |
|
|
} |
|
|
} |
|
|
|
|
|
$remise = 0; |
|
|
$price = round(ereg_replace(",",".",$pu), 2); |
|
|
$subprice = $price; |
|
|
if (trim(strlen($remise_percent)) > 0) |
|
|
{ |
|
|
$remise = round(($pu * $remise_percent / 100), 2); |
|
|
$price = $pu - $remise; |
|
|
} |
|
416 |
|
|
417 |
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet (fk_commande,label,description,fk_product, price,qty,tva_tx, remise_percent, subprice, remise, ref)"; |
$this->db->begin(); |
418 |
$sql .= " VALUES ($this->id, '" . addslashes($desc) . "','" . addslashes($desc) . "',$fk_product,".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."','".$ref."') ;"; |
|
419 |
|
/* On positionne en mode brouillon la commande */ |
420 |
|
$this->brouillon = 1; |
421 |
|
|
422 |
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur (fk_soc, date_creation, fk_user_author, fk_statut) "; |
423 |
|
$sql .= " VALUES (".$this->soc_id.", now(), ".$user->id.",0)"; |
424 |
|
|
425 |
|
if ( $this->db->query($sql) ) |
426 |
|
{ |
427 |
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."commande_fournisseur"); |
428 |
|
|
429 |
|
$sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur"; |
430 |
|
$sql.= " SET ref='(PROV".$this->id.")'"; |
431 |
|
$sql.= " WHERE rowid=".$this->id; |
432 |
|
if ($this->db->query($sql)) |
433 |
|
{ |
434 |
|
// On logue creation pour historique |
435 |
|
$this->log($user, 0, time()); |
436 |
|
|
437 |
|
dolibarr_syslog("CommandeFournisseur::Create : Success"); |
438 |
|
$this->db->commit(); |
439 |
|
return 1; |
440 |
|
} |
441 |
|
else |
442 |
|
{ |
443 |
|
$this->error=$this->db->error()." - ".$sql; |
444 |
|
dolibarr_syslog("CommandeFournisseur::Create: Failed -2 - ".$this->error); |
445 |
|
$this->db->rollback(); |
446 |
|
return -2; |
447 |
|
} |
448 |
|
} |
449 |
|
else |
450 |
|
{ |
451 |
|
$this->error=$this->db->error()." - ".$sql; |
452 |
|
dolibarr_syslog("CommandeFournisseur::Create: Failed -1 - ".$this->error); |
453 |
|
$this->db->rollback(); |
454 |
|
return -1; |
455 |
|
} |
456 |
|
} |
457 |
|
|
458 |
if ( $this->db->query( $sql) ) |
/** |
459 |
{ |
* \brief Ajoute une ligne de commande |
460 |
$this->update_price(); |
* \param desc Description |
461 |
return 0; |
* \param pu Prix unitaire |
462 |
} |
* \param qty Quantité |
463 |
else |
* \param txtva Taux tva |
464 |
{ |
* \param fk_product Id produit |
465 |
|
* \param remise_percent Remise |
466 |
|
* \param int <0 si ko, >0 si ok |
467 |
|
*/ |
468 |
|
function addline($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0) |
469 |
|
{ |
470 |
|
$qty = ereg_replace(",",".",$qty); |
471 |
|
$pu = ereg_replace(",",".",$pu); |
472 |
|
$desc=trim($desc); |
473 |
|
|
474 |
return -1; |
dolibarr_syslog("Fournisseur_Commande.class.php::addline $desc, $pu, $qty, $txtva, $fk_product, $remise_percent"); |
475 |
} |
|
476 |
} |
if ($this->brouillon) |
477 |
|
{ |
478 |
|
$this->db->begin(); |
479 |
|
|
480 |
|
if (strlen(trim($qty))==0) |
481 |
|
{ |
482 |
|
$qty=1; |
483 |
|
} |
484 |
|
|
485 |
|
if ($fk_product > 0) |
486 |
|
{ |
487 |
|
$prod = new Product($this->db, $fk_product); |
488 |
|
if ($prod->fetch($fk_product) > 0) |
489 |
|
{ |
490 |
|
$prod->get_buyprice($this->fourn_id,$qty); |
491 |
|
|
492 |
|
$desc = $prod->libelle; |
493 |
|
$txtva = $prod->tva_tx; |
494 |
|
$pu = $prod->buyprice/$qty; |
495 |
|
$ref = $prod->ref; |
496 |
|
} |
497 |
|
} |
498 |
|
|
499 |
|
$remise = 0; |
500 |
|
$price = round(ereg_replace(",",".",$pu), 2); |
501 |
|
$subprice = $price; |
502 |
|
if (trim(strlen($remise_percent)) > 0) |
503 |
|
{ |
504 |
|
$remise = round(($pu * $remise_percent / 100), 2); |
505 |
|
$price = $pu - $remise; |
506 |
|
} |
507 |
|
|
508 |
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet (fk_commande,label,description,fk_product, price, qty, tva_tx, remise_percent, subprice, remise, ref)"; |
509 |
|
$sql .= " VALUES ($this->id, '" . addslashes($desc) . "','" . addslashes($desc) . "',$fk_product,".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."','".$ref."') ;"; |
510 |
|
|
511 |
|
if ( $this->db->query( $sql) ) |
512 |
|
{ |
513 |
|
$this->update_price(); |
514 |
|
|
515 |
|
$this->db->commit(); |
516 |
|
return 1; |
517 |
|
} |
518 |
|
else |
519 |
|
{ |
520 |
|
$this->db->rollback(); |
521 |
|
return -1; |
522 |
|
} |
523 |
|
} |
524 |
} |
} |
525 |
|
|
526 |
/** |
/** |