Skip to content

Commit

Permalink
Gestion des réductions
Browse files Browse the repository at this point in the history
  • Loading branch information
feuloren committed Nov 27, 2013
1 parent e0b20cd commit a901f83
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 8 deletions.
20 changes: 18 additions & 2 deletions src/Payutc/Bom/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use \Payutc\Exception\TransactionAborted;
use \Payutc\Exception\TransactionNotFound;
use \Payutc\Exception\TransactionAlreadyValidated;
use \Payutc\Exception\InvalidReduction;

class Transaction {
protected $id;
Expand Down Expand Up @@ -361,18 +362,33 @@ static public function create($buyer, $seller, $appId, $funId, $objects, $callba
}

// If there is no quantity for this product, fail
if(count($object) != 2 || empty($object[1])){
if(count($object) < 2 || empty($object[1])){
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.");
}

$price = $product['price'] * $object[1];
// Apply the reduction
if(isset($object[2]) && !is_null($object[2])) {
if ($object[2] <= 0 || $object[2] >= 1) {
Log::warn("transaction($funId, $transactionId) : Invalid reduction $object[2] for article $object[0]");
throw new InvalidReduction("La réduction pour l'article $object[0] est invalide.");
} else {
$reduction = $object[2];
$price = $price * (1 - $reduction);
}
} else {
$reduction = null;
}

// Add the product to the transaction
$conn->insert('t_purchase_pur', array(
'tra_id' => $transactionId,
'obj_id' => $product['id'],
'pur_qte' => $object[1],
'pur_price' => $product['price'] * $object[1],
'pur_price' => $price,
'pur_unit_price' => $product['price'],
'pur_reduction' => $reduction,
), array("integer", "integer", "integer", "integer", "integer"));
}

Expand Down
3 changes: 3 additions & 0 deletions src/Payutc/Exception/InvalidReduction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php namespace Payutc\Exception;

class InvalidReduction extends PayutcException {}
21 changes: 21 additions & 0 deletions src/Payutc/Migrations/Version20131126235734.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Payutc\Migrations;

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

class Version20131126235734 extends AbstractMigration
{
public function up(Schema $schema)
{
$this->addSql("ALTER TABLE `t_purchase_pur`
ADD `pur_reduction` FLOAT NULL DEFAULT NULL AFTER `pur_unit_price`");
}

public function down(Schema $schema)
{
$this->addSql("ALTER TABLE `t_purchase_pur`
DROP `pur_reduction`;");
}
}
6 changes: 3 additions & 3 deletions src/Payutc/Service/POSS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ public function transaction($fun_id, $badge_id, $obj_ids) {
}

// 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
// $objects est un array de array($idProduct, $qte)
// il y a deux formats : ids séparés par des espaces (pas de quantités) ou json array d'arrays (id_product, qte, %reduction)
// $objects est un array de array($idProduct, $qte, $reduc)
$objects = json_decode($obj_ids);
Log::debug('decoded objects', array('objects' => $objects));

if (!is_array($objects)) {
$objects_ids = explode(" ", trim($obj_ids));
$objects = array();
foreach ($objects_ids as $id) {
$objects[] = array($id, 1);
$objects[] = array($id, 1, null);
}
}

Expand Down
28 changes: 25 additions & 3 deletions tests/Payutc/Bom/TransactionRwdbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public function testValidate(){
*/
public function testCreateAndValidate(){
$items = array(
array(5, 1),
array(5, 1)
array(5, 1, null),
array(5, 1, 0.1)
);

$matthieu = new User("mguffroy");
Expand All @@ -91,14 +91,36 @@ public function testCreateAndValidate(){
$this->assertEquals('V', $transaction->getStatus());

$u = new User("mguffroy");
$this->assertEquals(4660, $u->getCredit());
$this->assertEquals(4677, $u->getCredit());

$p = Product::getOne(5,1);
$this->assertEquals(40, $p['stock']);

$r = Purchase::getNbSell(5, 1);
$this->assertEquals(4, $r);
}

/**
* @requires PHP 5.4
* @expectedException \Payutc\Exception\InvalidReduction
*/
public function testIllegalReduction(){
$items = array(array(5, 1, 0));

$matthieu = new User("mguffroy");
$transaction = Transaction::createAndValidate($matthieu, $matthieu, 51, 1, $items, null, null);
}

/**
* @requires PHP 5.4
* @expectedException \Payutc\Exception\InvalidReduction
*/
public function testIllegalReduction2(){
$items = array(array(5, 1, 1));

$matthieu = new User("mguffroy");
$transaction = Transaction::createAndValidate($matthieu, $matthieu, 51, 1, $items, null, null);
}

/**
* @requires PHP 5.4
Expand Down
63 changes: 63 additions & 0 deletions tests/Payutc/Service/Poss3RwdbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,69 @@ public function testTransaction()
$this->assertEquals(80, $purchases[2]['pur_price']);
}

/**
* @requires PHP 5.4
*/
public function testTransactionWithQuanityAndReductions()
{
$u = new User("trecouvr");
$solde = $u->getCredit();
$nb_purchase = count($u->getLastPurchases());
$cookie = '';
$r = httpSend('POSS3', 'loginCas', $cookie, array(
'ticket' => 'trecouvr@POSS3',
'service' => 'POSS3'
));
$this->assertEquals(200, $r->code);
$r = httpSend('POSS3', 'loginApp', $cookie, array(
'key' => 'my_app'
));
$this->assertEquals(200, $r->code);

$objects = array(array(1, 1, null), // pas de réduction
array(2, 2), // réduction non précisée
array(1, 2, 0.1), // 10% de reduction
array(2, 2, 0.03), // 3%, pour tester l'arrondi inf
array(3, 1, 0.03), // 3%, pour tester l'arrondi sup
);
/* soit un prix de 1€ + 1,6€ + 2€ * (1-0,1) + 1,6€ * (1-0,03) *
* 1,7€ * (1-0,03) = 7,601€
* Mais le système doit arrondir à 7,60€ parcequ'on ne peut pas enlever de montant inférieur au centime
*/

$r = httpSend('POSS3', 'transaction', $cookie, array(
'fun_id' => 1,
'badge_id' => 'ABCDABCD',
'obj_ids' => json_encode($objects)
));
$o = array(
'firstname' => 'Thomas',
'lastname' => 'Recouvreux',
'solde' => $solde-760,
'msg_perso' => 'http://payutc.github.io'
);
$this->assertEquals($o, $r->body);
$this->assertEquals(200, $r->code);
$u = new User("trecouvr");
$this->assertEquals($solde-760, $u->getCredit());
$purchases = $u->getLastPurchases();
sort_by_key($purchases, 'pur_id');
$this->assertEquals($nb_purchase+count($objects), count($purchases));
$purchases = array_slice($purchases, count($purchases)-count($objects));
// obj ids
$this->assertEquals(1, $purchases[0]['obj_id']);
$this->assertEquals(2, $purchases[1]['obj_id']);
$this->assertEquals(1, $purchases[2]['obj_id']);
$this->assertEquals(2, $purchases[3]['obj_id']);
$this->assertEquals(3, $purchases[4]['obj_id']);
// total price
$this->assertEquals(100, $purchases[0]['pur_price']);
$this->assertEquals(160, $purchases[1]['pur_price']);
$this->assertEquals(180, $purchases[2]['pur_price']);
$this->assertEquals(155, $purchases[3]['pur_price']);
$this->assertEquals(165, $purchases[4]['pur_price']);
}

/**
* @requires PHP 5.4
*/
Expand Down

0 comments on commit a901f83

Please sign in to comment.