Skip to content

Commit

Permalink
Gestion des quantités
Browse files Browse the repository at this point in the history
  • Loading branch information
feuloren committed Aug 29, 2013
1 parent 592bfd5 commit 97059a9
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/Payutc/Bom/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Purchase
public static function getNbSell($obj_id, $fun_id, $start=null, $end=null, $tick=null)
{
$qb = Dbal::createQueryBuilder();
$qb->select('count(*) as nb', 'pur.pur_date')
$qb->select('sum(pur_qte) as nb', 'pur.pur_date')
->from('t_purchase_pur', 'pur')
->where('pur.obj_id = :obj_id')->setParameter('obj_id', $obj_id)
->andWhere('pur.fun_id = :fun_id')->setParameter('fun_id', $fun_id)
Expand Down Expand Up @@ -82,7 +82,7 @@ public static function cancelById($pur_id)
// update purchase + buyer + stock, then commit
$qb->execute();
User::incCreditById($pur['usr_id_buyer'], $pur['pur_price']);
Product::incStockById($pur['obj_id'], 1);
Product::incStockById($pur['obj_id'], $pur['pur_qte']);
Dbal::commit();
}
catch (Exception $e) {
Expand All @@ -102,12 +102,15 @@ public static function transaction($usr_id_buyer, $items, $poi_id, $fun_id, $usr
$total_price = 0;
$purchases = array();
foreach ($items as $itm) {
$total_price += $itm['price'];
$price = $itm[0]['price'] * $itm[1];
$total_price += $price;
$purchases[] = array(
'pur_date' => date('Y-m-d H:i:s'),
'pur_type' => 'product',
'obj_id' => $itm['id'],
'pur_price' => $itm['price'],
'obj_id' => $itm[0]['id'],
'pur_qte' => $itm[1],
'pur_price' => $price,
'pur_unit_price' => $itm[0]['price'],
'usr_id_buyer' => $usr_id_buyer,
'usr_id_seller' => $usr_id_seller,
'poi_id' => $poi_id,
Expand All @@ -125,7 +128,7 @@ public static function transaction($usr_id_buyer, $items, $poi_id, $fun_id, $usr

foreach ($purchases as $pur) {
$a = $conn->insert('t_purchase_pur', $pur);
Product::decStockById($pur['obj_id'], 1);
Product::decStockById($pur['obj_id'], $pur['pur_qte']);
}
$conn->commit();
}
Expand All @@ -144,7 +147,7 @@ public static function transaction($usr_id_buyer, $items, $poi_id, $fun_id, $usr
*/
public static function getRank($fun_id, $obj_id, $start, $end, $top, $sort_by) {
$qb = Dbal::createQueryBuilder();
$qb->select('sum(pur.pur_price) as totalPrice', 'count(*) as nbBuy', 'usr.usr_firstname', 'usr.usr_lastname', 'usr.usr_nickname')
$qb->select('sum(pur.pur_price) as totalPrice', 'sum(pur_qte) as nbBuy', 'usr.usr_firstname', 'usr.usr_lastname', 'usr.usr_nickname')
->from('t_purchase_pur', 'pur')
->from('ts_user_usr', 'usr')
->where('usr.usr_id = pur.usr_id_buyer')
Expand Down
23 changes: 23 additions & 0 deletions src/Payutc/Migrations/Version20130829233731.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Payutc\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration,
Doctrine\DBAL\Schema\Schema;

class Version20130829233731 extends AbstractMigration
{
public function up(Schema $schema)
{
$this->addSql("ALTER TABLE `t_purchase_pur`
ADD `pur_qte` INT UNSIGNED NOT NULL DEFAULT '1' COMMENT 'Nombre d''object acheté' AFTER `obj_id` ,
ADD `pur_unit_price` INT UNSIGNED NOT NULL COMMENT 'Prix unitaire de l''object acheté' AFTER `pur_qte`");
}

public function down(Schema $schema)
{
$this->addSql("ALTER TABLE `t_purchase_pur2`
DROP `pur_qte`,
DROP `pur_unit_price`;");
}
}
108 changes: 107 additions & 1 deletion src/Payutc/Service/POSS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function transaction($fun_id, $badge_id, $obj_ids) {
// 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[] = $items[$id];
$items_to_buy[] = array($items[$id], 1);
}

// si alcool, vérifier que le buyer est majeur
Expand Down Expand Up @@ -186,6 +186,112 @@ public function transaction($fun_id, $badge_id, $obj_ids) {
"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;
}

// 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 as $object)
{
if(isset($items[$object[0]]))
{
$total += $items[$object[0]]['price'] * $object[1];
} 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");
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");
}

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

public function getImage64($img_id, $outw = 0, $outh = 0)
{
$r = parent::getImage64($img_id, $outw, $outh);
Expand Down

0 comments on commit 97059a9

Please sign in to comment.