Fix: uniformize code

This commit is contained in:
Regis Houssin 2012-07-06 13:43:59 +02:00
parent d5060adf9d
commit 3bfd9182e0
3 changed files with 831 additions and 902 deletions

View File

@ -508,79 +508,79 @@ class Commande extends CommonObject
* Cancel an order * Cancel an order
* If stock is decremented on order validation, we must reincrement it * If stock is decremented on order validation, we must reincrement it
* *
* @param User $user Object user
* @param int $idwarehouse Id warehouse to use for stock change. * @param int $idwarehouse Id warehouse to use for stock change.
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function cancel($user, $idwarehouse=-1) function cancel($idwarehouse=-1)
{ {
global $conf,$langs; global $conf,$user,$langs;
$error=0; $error=0;
if ($user->rights->commande->valider) $this->db->begin();
{
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."commande"; $sql = "UPDATE ".MAIN_DB_PREFIX."commande";
$sql.= " SET fk_statut = -1"; $sql.= " SET fk_statut = -1";
$sql.= " WHERE rowid = ".$this->id; $sql.= " WHERE rowid = ".$this->id;
$sql.= " AND fk_statut = 1"; $sql.= " AND fk_statut = 1";
dol_syslog("Commande::cancel sql=".$sql, LOG_DEBUG); dol_syslog("Commande::cancel sql=".$sql, LOG_DEBUG);
if ($this->db->query($sql)) if ($this->db->query($sql))
{ {
// If stock is decremented on validate order, we must reincrement it // If stock is decremented on validate order, we must reincrement it
if ($conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) if ($conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1)
{ {
require_once(DOL_DOCUMENT_ROOT."/product/stock/class/mouvementstock.class.php"); require_once(DOL_DOCUMENT_ROOT."/product/stock/class/mouvementstock.class.php");
$langs->load("agenda"); $langs->load("agenda");
$num=count($this->lines); $num=count($this->lines);
for ($i = 0; $i < $num; $i++) for ($i = 0; $i < $num; $i++)
{ {
if ($this->lines[$i]->fk_product > 0) if ($this->lines[$i]->fk_product > 0)
{ {
$mouvP = new MouvementStock($this->db); $mouvP = new MouvementStock($this->db);
// We increment stock of product (and sub-products) // We increment stock of product (and sub-products)
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("OrderCanceledInDolibarr",$this->ref)); $result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("OrderCanceledInDolibarr",$this->ref));
if ($result < 0) { $error++; } if ($result < 0) {
} $error++;
} }
} }
}
}
if (! $error) if (! $error)
{ {
// Appel des triggers // Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"); include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($this->db); $interface=new Interfaces($this->db);
$result=$interface->run_triggers('ORDER_CANCEL',$this,$user,$langs,$conf); $result=$interface->run_triggers('ORDER_CANCEL',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; } if ($result < 0) {
// Fin appel triggers $error++; $this->errors=$interface->errors;
} }
// Fin appel triggers
}
if (! $error) if (! $error)
{ {
$this->statut=-1; $this->statut=-1;
$this->db->commit(); $this->db->commit();
return 1; return 1;
} }
else else
{ {
$this->error=$mouvP->error; $this->error=$mouvP->error;
$this->db->rollback(); $this->db->rollback();
return -1; return -1;
} }
} }
else else
{ {
$this->error=$this->db->error(); $this->error=$this->db->error();
$this->db->rollback(); $this->db->rollback();
dol_syslog($this->error, LOG_ERR); dol_syslog($this->error, LOG_ERR);
return -1; return -1;
} }
} }
}
/** /**
* Create order * Create order

File diff suppressed because it is too large Load Diff

View File

@ -202,7 +202,7 @@ class CommandeTest extends PHPUnit_Framework_TestCase
$langs=$this->savlangs; $langs=$this->savlangs;
$db=$this->savdb; $db=$this->savdb;
$result=$localobject->cancel($user); $result=$localobject->cancel();
print __METHOD__." id=".$localobject->id." result=".$result."\n"; print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);