Commit f5ea40c7 by Damien Moulard

transactions export by caissier: switch from prestataire level export to caissier level export

parent 430157d2
......@@ -2,6 +2,7 @@
namespace App\Controller;
use App\Entity\Caissier;
use App\Entity\Groupe;
use App\Entity\Prestataire;
use App\Entity\Rubrique;
......@@ -16,18 +17,21 @@ use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
class PrestatairesController extends FrontController
{
protected $em;
private $router;
private $session;
private $security;
public function __construct(EntityManagerInterface $em, RouterInterface $router, SessionInterface $session)
public function __construct(EntityManagerInterface $em, RouterInterface $router, SessionInterface $session, Security $security)
{
$this->em = $em;
$this->router = $router;
$this->session = $session;
$this->security = $security;
}
/**
......@@ -165,45 +169,53 @@ class PrestatairesController extends FrontController
}
/**
* Get the total transactions amount towards the Prestataire since the last time it was fetched.
* Exclude Reconversions from calculation.
* Get the total transactions amount towards the Prestataire operated by the connected Caissier,
* since the last time it was fetched.
*
* @Route("/prestataires/get_last_transactions", name="get_presta_last_transactions")
* @IsGranted({"ROLE_CAISSIER", "ROLE_PRESTATAIRE"})
* @Route("/prestataires/get_caissier_last_transactions", name="get_caissier_last_transactions")
* @IsGranted({"ROLE_CAISSIER"})
*/
public function getLastTransactionsAmount()
public function getLastCaissierTransactionsAmount()
{
if (!$this->session->has('_prestagere')) {
return null;
}
// Get last export datetime (presta creation if first export)
$user = $this->security->getUser();
$presta = $this->em->getRepository(Prestataire::class)->findOneById($this->session->get('_prestagere')->getId());
$caissier = $this->em->getRepository(Caissier::class)->findOneBy(['prestataire' => $presta, 'user' => $user]);
$datetime_last_export = $presta->getLastTransactionsExportDatetime();
// Get last caissier export datetime
$datetime_last_export = $caissier->getLastTransactionsExportDatetime();
if (null == $datetime_last_export) {
$datetime_last_export = $presta->getCreatedAt();
// for transition purposes from Prestataire level export towards Caissier level export,
// get prestataire last export if caissier export date is not yet set
$datetime_last_export = $caissier->getLastTransactionsExportDatetime();
if (null == $datetime_last_export) {
// prestaire creation if first export
$datetime_last_export = $presta->getCreatedAt();
}
}
// Get total amount
$flux = $this->em->getRepository(Flux::class)->getQueryByPrestataire(
$this->session->get('_prestagere'),
null,
null,
// Get total amount of transactions operated by connecteed Caissier for this Prestataire
$flux = $this->em->getRepository(Flux::class)->getQueryByCaissier(
$user,
$presta,
$datetime_last_export->format(("Y-m-d H:i:s"))
)->getResult();
$total_amount = 0;
foreach ($flux as $flux_item) {
// Exclude reconversions from calculation
// Exclude reconversions from calculation (extra security but probably unecessary, as reconversions don't have an operator)
if ($flux_item->getType() != "reconversion_prestataire") {
$total_amount += $flux_item->getMontant();
}
}
// Set now as this presta last export date
$presta->setLastTransactionsExportDatetime(new \Datetime('now'));
$this->em->persist($presta);
// Set now as this caissier last export datetime
$caissier->setLastTransactionsExportDatetime(new \Datetime('now'));
$this->em->persist($caissier);
$this->em->flush();
$str_datetime = $datetime_last_export->format('d/m/Y H\hi');
......
......@@ -30,6 +30,8 @@ class Caissier
private $prestataire;
/**
* Caissiers can export all the transactions since the last export.
*
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastTransactionsExportDatetime;
......
......@@ -352,7 +352,9 @@ class Prestataire extends AccountableObject implements AccountableInterface
private $comments;
/**
* Caissiers can export all the transactions since the last export.
* [UNUSED]
* This field has been moved to the Caissier entity.
* This field is kept for transitions purposes only and isn't updated anymore.
*
* @ORM\Column(type="datetime", nullable=true)
*/
......
......@@ -177,24 +177,27 @@ class FluxRepository extends ServiceEntityRepository
}
/**
* @param User $user
* @param Prestataire $presta
* @param bool $onlySameDay
* @param User $user operator of the flux
* @param Prestataire $presta
* @param string $from Date from which to fetch the flux
*
* @return Query Returns a query fo finding an array of Flux
*/
public function getQueryByCaissier(User $user, Prestataire $presta, $onlySameDay = true)
public function getQueryByCaissier(User $user, Prestataire $presta, $from = true)
{
// TODO change here
$sqlQuery = "SELECT f.id FROM {$this->tableName} f";
$sqlQuery .= " WHERE ((f.type = 'adherent_prestataire' AND f.prestataire_id = :p_id) OR (f.type = 'prestataire_prestataire' AND f.prestataire_dest_id = :id))";
$sqlQuery .= " AND f.user_id = :c_id"; // set operateur
if ($onlySameDay) {
$sqlQuery .= ' AND DATE(f.created_at) = CURDATE()';
$sqlQuery .= " WHERE ((f.type = 'adherent_prestataire' AND f.prestataire_id = :pid) OR (f.type = 'prestataire_prestataire' AND f.prestataire_dest_id = :pid))";
$sqlQuery .= " AND f.user_id = :cid"; // set operateur
if ($from != null) {
$sqlQuery .= " AND f.created_at >= :from";
}
$statement = $this->connection->prepare($sqlQuery);
$statement->bindValue(':p_id', $presta->getId());
$statement->bindValue(':c_id', $user->getId());
$statement->bindValue(':pid', $presta->getId());
$statement->bindValue(':cid', $user->getId());
if ($from != null) {
$statement->bindValue(':from', $from);
}
$statement->execute();
$results = $statement->fetchAll();
$qb = $this->createQueryBuilder('f');
......
......@@ -7,6 +7,7 @@ use App\Entity\AchatMonnaieAConfirmerPrestataire;
use App\Entity\AchatMonnaieAdherent;
use App\Entity\AchatMonnaiePrestataire;
use App\Entity\Adherent;
use App\Entity\Caissier;
use App\Entity\Comptoir;
use App\Entity\CotisationAdherent;
use App\Entity\CotisationPrestataire;
......@@ -106,7 +107,7 @@ class AppExtension extends AbstractExtension
new \Twig_SimpleFunction('getPaymentReceiptUrlFromFlux', [$this, 'getPaymentReceiptUrlFromFlux']),
new \Twig_SimpleFunction('getDonType', [$this, 'getDonType']),
new \Twig_SimpleFunction('getLastTavCotisationForAdherent', [$this, 'getLastTavCotisationForAdherent']),
new \Twig_SimpleFunction('getPrestaLastTransactionsExportDate', [$this, 'getPrestaLastTransactionsExportDate']),
new \Twig_SimpleFunction('getCaissierLastTransactionsExportDate', [$this, 'getCaissierLastTransactionsExportDate']),
new \Twig_SimpleFunction('checkExistingRecurringPayment', [$this, 'checkExistingRecurringPayment']),
new \Twig_SimpleFunction('parameter', function ($name) {
return $this->container->getParameter($name);
......@@ -549,15 +550,17 @@ class AppExtension extends AbstractExtension
return str_replace('@', '@', $email);
}
public function getPrestaLastTransactionsExportDate(?Adherent $adherent = null)
public function getCaissierLastTransactionsExportDate(?Adherent $adherent = null)
{
if (!$this->session->has('_prestagere')) {
return null;
}
$presta = $this->em->getRepository(Prestataire::class)->findOneById($this->session->get('_prestagere')->getId());
$user = $this->security->getUser();
$caissier = $this->em->getRepository(Caissier::class)->findOneBy(['prestataire' => $presta, 'user' => $user]);
$datetime_last_export = $presta->getLastTransactionsExportDatetime();
$datetime_last_export = $caissier->getLastTransactionsExportDatetime();
$res = "";
if (null == $datetime_last_export) {
......
......@@ -4,10 +4,10 @@
<i class="fa fa-list-alt mr-4"></i> {{'Transactions'|trans }}
{% endblock blocktitle %}
{% block blockcontent %}
{% set datetime_last_export = getPrestaLastTransactionsExportDate() %}
{% set datetime_last_export = getCaissierLastTransactionsExportDate() %}
<p>Récupérer le montant total des transactions (hors reconversions) depuis le <b>{{ datetime_last_export }}</b></p>
<a class='btn btn-xs btn-primary mt-2' href='{{ path('get_presta_last_transactions') }}'>
<p>Récupérer le montant total des transactions opérées par ce compte Caissier (hors reconversions) depuis le <b>{{ datetime_last_export }}</b></p>
<a class='btn btn-xs btn-primary mt-2' href='{{ path('get_caissier_last_transactions') }}'>
{{ 'Exporter'|trans }}
</a>
{% endblock blockcontent %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment