add vO.1 service credit

This commit is contained in:
benoitpotier 2017-02-23 16:18:08 +01:00
parent 1acb527a0f
commit 2d0b900e92
3 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: potier
* Date: 23/02/17
* Time: 14:02
*/

View File

@ -0,0 +1,128 @@
<?php
/**
* Created by PhpStorm.
* User: potier
* Date: 23/02/17
* Time: 14:03
*/
require_once __DIR__ . '/Types.php';
class Credit extends Scores_Ws_Server
{
/**
* Retourne le nombre de crédit disponible à un utilisateur
* @return int
*/
public function getCredit()
{
$this->authenticate();
$this->permission('credit');
$idClient = $this->User->idClient;
try {
$sql = "SELECT * FROM sdv1.credit__balance c
WHERE c.idClient=:idClient";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue('idClient', $idClient);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$rnvpResult = $stmt->fetch(\PDO::FETCH_OBJ);
$credit = $rnvpResult->balance;
}
} catch(\Doctrine\DBAL\DBALException $e) {
if ($this->logger !== null) {
$this->logger->error($e->getMessage());
}
}
$this->wsLog('credit', $idClient);
return $credit;
}
/**
* Ajoute des crédits supplémentaires à un utilisateur
* @param $nbCredit
*/
public function addCredit($nbCredit)
{
$this->authenticate();
$this->permission('addcredit');
$this->updateCredit($this->User->idClient, $this->User->login, $nbCredit);
$this->wsLog('addcredit', $this->User->idClient, $nbCredit);
}
/**
* Retire des crédits à un utilisateur
* @param $nbCredit
* @return bool|multitype
*/
public function subCredit($nbCredit)
{
$this->authenticate();
$this->permission('subcredit');
try {
$sql = 'INSERT INTO sdv1.credit__consumption(idClient, login, balance, created)
VALUES(:idClient, :login, :consumption, :created)';
$stmt = $this->conn->prepare($sql);
$stmt->bindValue('idClient', $this->User->idClient);
$stmt->bindValue('login', $this->User->login);
$stmt->bindValue('balance', $nbCredit);
$stmt->bindValue('created', date('YmdHis'));
$stmt->execute();
} catch(\Doctrine\DBAL\DBALException $e) {
if ($this->logger !== null) {
$this->logger->error($e->getMessage());
}
}
$this->updateCredit($this->User->idClient, $this->User->login, $nbCredit);
$this->wsLog('subcredit', $this->User->idClient, $nbCredit);
}
/**
* Modifie les crédits à un utilisateur
* @param $idClient
* @param $login
* @param $nbCredit
*/
public function updateCredit($idClient, $login, $nbCredit)
{
try {
$sql = "SELECT * FROM sdv1.credit__balance c
WHERE c.idClient=:idClient";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue('idClient', $idClient);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$rnvpResult = $stmt->fetch(\PDO::FETCH_OBJ);
$balance = $rnvpResult->balance;
$balance += $nbCredit;
$sql = "UPDATE sdv1.credit__balance SET balance=:balance AND updated=:updated;";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue('balance', $balance);
$stmt->bindValue('updated', date('YmdHis'));
$stmt->execute();
} else {
$sql = 'INSERT INTO sdv1.credit__balance(idClient, login, balance, created, updated)
VALUES(:idClient, :login, :balance, :created, :updated)';
$stmt = $this->conn->prepare($sql);
$stmt->bindValue('idClient', $idClient);
$stmt->bindValue('login', $login);
$stmt->bindValue('balance', $nbCredit);
$stmt->bindValue('created', date('YmdHis'));
$stmt->bindValue('updated', date('YmdHis'));
$stmt->execute();
}
} catch(\Doctrine\DBAL\DBALException $e) {
if ($this->logger !== null) {
$this->logger->error($e->getMessage());
}
}
}
}

View File

@ -0,0 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: potier
* Date: 23/02/17
* Time: 14:03
*/