Skip to content

Commit

Permalink
Gestion des quantité directement dans POSS3->transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
feuloren committed Aug 30, 2013
1 parent d3863bb commit 87d27e5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 122 deletions.
8 changes: 4 additions & 4 deletions src/Payutc/Bom/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ public static function transaction($usr_id_buyer, $items, $poi_id, $fun_id, $usr
$total_price = 0;
$purchases = array();
foreach ($items as $itm) {
$price = $itm[0]['price'] * $itm[1];
$price = $itm['price'] * $itm['qte'];
$total_price += $price;
$purchases[] = array(
'pur_date' => date('Y-m-d H:i:s'),
'pur_type' => 'product',
'obj_id' => $itm[0]['id'],
'pur_qte' => $itm[1],
'obj_id' => $itm['id'],
'pur_qte' => $itm['qte'],
'pur_price' => $price,
'pur_unit_price' => $itm[0]['price'],
'pur_unit_price' => $itm['price'],
'usr_id_buyer' => $usr_id_buyer,
'usr_id_seller' => $usr_id_seller,
'poi_id' => $poi_id,
Expand Down
142 changes: 28 additions & 114 deletions src/Payutc/Service/POSS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,115 +121,27 @@ public function transaction($fun_id, $badge_id, $obj_ids) {
throw new PossException($e->getMessage());
}

// récupérer les objets dans la db (note: pas de doublon)
$objects_ids = explode(" ", trim($obj_ids));
$r = Product::getAll(array('obj_ids'=>array_unique($objects_ids), 'fun_ids'=>array($fun_id)));
$items = array();
foreach($r as $itm) {
$items[$itm['id']] = $itm;
}

// y'a t il de l'alcool ?
$alcool = false;
foreach($items as $itm) {
if ($itm['alcool'] > 0) {
$alcool = true;
break;
}
}

// calcul le prix total
$total = 0;
foreach($objects_ids as $obj_id)
{
if(isset($items[$obj_id]))
{
$total += $items[$obj_id]['price'];
} else {
Log::warn("transaction($badge_id, ...) : $obj_id is unavailable");
throw new PossException("L'article $obj_id n'est pas disponible à la vente.");
// tranformer la chaine passee en un array exploitable
// il y a deux formats : ids séparés par des espaces (pas de quantités) ou json
\error_log($obj_ids);
$objects = json_decode($obj_ids);
if (is_null($objects)) { // espaces
$objects_ids = explode(" ", trim($obj_ids));

$objects = array();
foreach ($objects_ids as $id) {
$objects[] = array($id, 1);
}
}

// création de la liste des items à acheter (note: il peut y avoir des doublons)
$items_to_buy = array();
foreach($objects_ids as $id) {
$items_to_buy[] = array($items[$id], 1);
}

// si alcool, vérifier que le buyer est majeur
if($alcool)
{
if($buyer->isAdult() == 0) {
Log::warn("transaction($badge_id, $obj_ids) : Under-18 users can't buy alcohol");
throw new PossException($buyer->getNickname()." est mineur il ne peut pas acheter d'alcool !");
} else {
$objects_ids = array();
foreach($objects as $object) {
$objects_ids[] = $object[0];
}
}

// vérifier que le buyer a assez d'argent
if($buyer->getCredit() < $total) {
Log::warn("transaction($badge_id, $obj_ids) : Buyer have not enough money");
throw new PossException($buyer->getNickname()." n'a pas assez d'argent pour effectuer la transaction.");
}

// effectuer les achats
Purchase::transaction($buyer->getId(), $items_to_buy,
$this->application()->getId(), $fun_id,
$this->user()->getId(), $this->getRemoteIp());

// Retourner les infos sur l'utilisateur
$msg = $buyer->getMsgPerso($fun_id);

return array("firstname"=>$buyer->getFirstname(),
"lastname"=>$buyer->getLastname(),
"solde"=>$buyer->getCredit(),
"msg_perso"=>$msg);
}

/**
* $objects_list est de la forme [[obj1, qte1], [obj2, qt2], ...]
*/
public function transactionWithQte($fun_id, $badge_id, $objects_list) {
$this->checkRight(true, true, true, $fun_id);

// Verifier que le buyer existe
try {
$buyer = User::getUserFromBadge($badge_id);
}
catch(UserNotFound $ex) {
Log::warn("transaction($fun_id, $badge_id, $objects_list) : User not found");
throw new PossException("Ce badge n'a pas été reconnu");
}

// Vérifier que la carte n'est pas bloquée
try {
$buyer->checkNotBlockedMe();
}
catch(UserIsBlockedException $ex) {
Log::warn("transaction($fun_id, $badge_id, $objects_list) : Blocked card");
throw new PossException("Ce badge à été bloqué : son propriétaire doit le débloquer sur son interface de gestion");
}

// vérifier que l'utilisateur n'est pas bloqué sur cette fondation
try {
$buyer->checkNotBlockedFun($fun_id);
}
catch (UserIsBlockedException $e) {
Log::warn("transaction($fund_id, $badge_id, $objects_list) : Blocked user ({$e->getMessage()})");
throw new PossException($e->getMessage());
}

// récupérer les objets dans la db (note: pas de doublon)
$objects = json_decode($objects_list);
$objects_ids = array();
foreach($objects as $object) {
$objects_ids[] = $object[0];
}

$r = Product::getAll(array('obj_ids'=>array_unique($objects_ids), 'fun_ids'=>array($fun_id)));
$items = array();

$alcool = false;
foreach($r as $itm) {
$items[$itm['id']] = $itm;
}
Expand All @@ -243,39 +155,41 @@ public function transactionWithQte($fun_id, $badge_id, $objects_list) {
}
}

// calcul le prix total
// calcul le prix total et création de la liste des items à acheter (note: il peut y avoir des doublons)
$total = 0;
$items_to_buy = array();
foreach($objects as $object)
{
if(isset($items[$object[0]]))
{
$total += $items[$object[0]]['price'] * $object[1];
if ($object[1] > 0) {
$item = $items[$object[0]];
$item['qte'] = $object[1];
$items_to_buy[] = $item;
} else {
Log::warn("transaction($fun_id, $badge_id, $obj_ids) : Null quantity for article $object[0]");
throw new PossException("La quantité pour l'article est $object[0] nulle.");
}
$total += $item['price'] * $item['qte'];
} else {
Log::warn("transaction($badge_id, ...) : $object[0] is unavailable");
throw new PossException("L'article $object[0] n'est pas disponible à la vente.");
}
}

// création de la liste des items à acheter (note: il peut y avoir des doublons)
$items_to_buy = array();
foreach($objects as $object) {
if ($object[1] > 0)
$items_to_buy[] = array($items[$object[0]], $object[1]);
}

// si alcool, vérifier que le buyer est majeur
if($alcool)
{
if($buyer->isAdult() == 0) {
Log::warn("transaction($badge_id, $objects_list) : Under-18 users can't buy alcohol");
Log::warn("transaction($badge_id, $obj_ids) : Under-18 users can't buy alcohol");
throw new PossException($buyer->getNickname()." est mineur il ne peut pas acheter d'alcool !");
}
}

// vérifier que le buyer a assez d'argent
if($buyer->getCredit() < $total) {
Log::warn("transaction($badge_id, $objects_list) : Buyer have not enough money");
throw new PossException($buyer->getNickname()." n'a pas assez d'argent pour effectuer la transaction. $total");
Log::warn("transaction($badge_id, $obj_ids) : Buyer have not enough money");
throw new PossException($buyer->getNickname()." n'a pas assez d'argent pour effectuer la transaction.");
}

// effectuer les achats
Expand Down
10 changes: 6 additions & 4 deletions tests/PurchaseRwdbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ public function testTransaction()
$date = date('Y-m-d H:i:s');
$nbSells = Purchase::getNbSell(4, 1, $date);
$items = array(
array(array(
array(
'id' => 4,
'qte' => 1,
'price' => 150,
), 1),
array(array(
),
array(
'id' => 4,
'qte' => 3,
'price' => 150,
), 3),
),
);
Purchase::transaction(1, $items, 51, 1, 9447, "localhost");
$u = new User("trecouvr");
Expand Down

0 comments on commit 87d27e5

Please sign in to comment.