Commit f5ea40c7 by Damien Moulard

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

parent 430157d2
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Caissier;
use App\Entity\Groupe; use App\Entity\Groupe;
use App\Entity\Prestataire; use App\Entity\Prestataire;
use App\Entity\Rubrique; use App\Entity\Rubrique;
...@@ -16,18 +17,21 @@ use Symfony\Component\Routing\Annotation\Route; ...@@ -16,18 +17,21 @@ use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
class PrestatairesController extends FrontController class PrestatairesController extends FrontController
{ {
protected $em; protected $em;
private $router; private $router;
private $session; 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->em = $em;
$this->router = $router; $this->router = $router;
$this->session = $session; $this->session = $session;
$this->security = $security;
} }
/** /**
...@@ -165,45 +169,53 @@ class PrestatairesController extends FrontController ...@@ -165,45 +169,53 @@ class PrestatairesController extends FrontController
} }
/** /**
* Get the total transactions amount towards the Prestataire since the last time it was fetched. * Get the total transactions amount towards the Prestataire operated by the connected Caissier,
* Exclude Reconversions from calculation. * since the last time it was fetched.
* *
* @Route("/prestataires/get_last_transactions", name="get_presta_last_transactions") * @Route("/prestataires/get_caissier_last_transactions", name="get_caissier_last_transactions")
* @IsGranted({"ROLE_CAISSIER", "ROLE_PRESTATAIRE"}) * @IsGranted({"ROLE_CAISSIER"})
*/ */
public function getLastTransactionsAmount() public function getLastCaissierTransactionsAmount()
{ {
if (!$this->session->has('_prestagere')) { if (!$this->session->has('_prestagere')) {
return null; 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()); $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) { 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 // Get total amount of transactions operated by connecteed Caissier for this Prestataire
$flux = $this->em->getRepository(Flux::class)->getQueryByPrestataire( $flux = $this->em->getRepository(Flux::class)->getQueryByCaissier(
$this->session->get('_prestagere'), $user,
null, $presta,
null,
$datetime_last_export->format(("Y-m-d H:i:s")) $datetime_last_export->format(("Y-m-d H:i:s"))
)->getResult(); )->getResult();
$total_amount = 0; $total_amount = 0;
foreach ($flux as $flux_item) { 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") { if ($flux_item->getType() != "reconversion_prestataire") {
$total_amount += $flux_item->getMontant(); $total_amount += $flux_item->getMontant();
} }
} }
// Set now as this presta last export date // Set now as this caissier last export datetime
$presta->setLastTransactionsExportDatetime(new \Datetime('now')); $caissier->setLastTransactionsExportDatetime(new \Datetime('now'));
$this->em->persist($presta); $this->em->persist($caissier);
$this->em->flush(); $this->em->flush();
$str_datetime = $datetime_last_export->format('d/m/Y H\hi'); $str_datetime = $datetime_last_export->format('d/m/Y H\hi');
......
...@@ -30,6 +30,8 @@ class Caissier ...@@ -30,6 +30,8 @@ class Caissier
private $prestataire; private $prestataire;
/** /**
* Caissiers can export all the transactions since the last export.
*
* @ORM\Column(type="datetime", nullable=true) * @ORM\Column(type="datetime", nullable=true)
*/ */
private $lastTransactionsExportDatetime; private $lastTransactionsExportDatetime;
......
...@@ -352,7 +352,9 @@ class Prestataire extends AccountableObject implements AccountableInterface ...@@ -352,7 +352,9 @@ class Prestataire extends AccountableObject implements AccountableInterface
private $comments; 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) * @ORM\Column(type="datetime", nullable=true)
*/ */
......
...@@ -177,24 +177,27 @@ class FluxRepository extends ServiceEntityRepository ...@@ -177,24 +177,27 @@ class FluxRepository extends ServiceEntityRepository
} }
/** /**
* @param User $user * @param User $user operator of the flux
* @param Prestataire $presta * @param Prestataire $presta
* @param bool $onlySameDay * @param string $from Date from which to fetch the flux
* *
* @return Query Returns a query fo finding an array of 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 = "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 .= " 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 = :c_id"; // set operateur $sqlQuery .= " AND f.user_id = :cid"; // set operateur
if ($onlySameDay) { if ($from != null) {
$sqlQuery .= ' AND DATE(f.created_at) = CURDATE()'; $sqlQuery .= " AND f.created_at >= :from";
} }
$statement = $this->connection->prepare($sqlQuery); $statement = $this->connection->prepare($sqlQuery);
$statement->bindValue(':p_id', $presta->getId()); $statement->bindValue(':pid', $presta->getId());
$statement->bindValue(':c_id', $user->getId()); $statement->bindValue(':cid', $user->getId());
if ($from != null) {
$statement->bindValue(':from', $from);
}
$statement->execute(); $statement->execute();
$results = $statement->fetchAll(); $results = $statement->fetchAll();
$qb = $this->createQueryBuilder('f'); $qb = $this->createQueryBuilder('f');
......
...@@ -7,6 +7,7 @@ use App\Entity\AchatMonnaieAConfirmerPrestataire; ...@@ -7,6 +7,7 @@ use App\Entity\AchatMonnaieAConfirmerPrestataire;
use App\Entity\AchatMonnaieAdherent; use App\Entity\AchatMonnaieAdherent;
use App\Entity\AchatMonnaiePrestataire; use App\Entity\AchatMonnaiePrestataire;
use App\Entity\Adherent; use App\Entity\Adherent;
use App\Entity\Caissier;
use App\Entity\Comptoir; use App\Entity\Comptoir;
use App\Entity\CotisationAdherent; use App\Entity\CotisationAdherent;
use App\Entity\CotisationPrestataire; use App\Entity\CotisationPrestataire;
...@@ -106,7 +107,7 @@ class AppExtension extends AbstractExtension ...@@ -106,7 +107,7 @@ class AppExtension extends AbstractExtension
new \Twig_SimpleFunction('getPaymentReceiptUrlFromFlux', [$this, 'getPaymentReceiptUrlFromFlux']), new \Twig_SimpleFunction('getPaymentReceiptUrlFromFlux', [$this, 'getPaymentReceiptUrlFromFlux']),
new \Twig_SimpleFunction('getDonType', [$this, 'getDonType']), new \Twig_SimpleFunction('getDonType', [$this, 'getDonType']),
new \Twig_SimpleFunction('getLastTavCotisationForAdherent', [$this, 'getLastTavCotisationForAdherent']), 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('checkExistingRecurringPayment', [$this, 'checkExistingRecurringPayment']),
new \Twig_SimpleFunction('parameter', function ($name) { new \Twig_SimpleFunction('parameter', function ($name) {
return $this->container->getParameter($name); return $this->container->getParameter($name);
...@@ -549,15 +550,17 @@ class AppExtension extends AbstractExtension ...@@ -549,15 +550,17 @@ class AppExtension extends AbstractExtension
return str_replace('@', '@', $email); return str_replace('@', '@', $email);
} }
public function getPrestaLastTransactionsExportDate(?Adherent $adherent = null) public function getCaissierLastTransactionsExportDate(?Adherent $adherent = null)
{ {
if (!$this->session->has('_prestagere')) { if (!$this->session->has('_prestagere')) {
return null; return null;
} }
$presta = $this->em->getRepository(Prestataire::class)->findOneById($this->session->get('_prestagere')->getId()); $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 = ""; $res = "";
if (null == $datetime_last_export) { if (null == $datetime_last_export) {
......
...@@ -4,10 +4,10 @@ ...@@ -4,10 +4,10 @@
<i class="fa fa-list-alt mr-4"></i> {{'Transactions'|trans }} <i class="fa fa-list-alt mr-4"></i> {{'Transactions'|trans }}
{% endblock blocktitle %} {% endblock blocktitle %}
{% block blockcontent %} {% 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> <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_presta_last_transactions') }}'> <a class='btn btn-xs btn-primary mt-2' href='{{ path('get_caissier_last_transactions') }}'>
{{ 'Exporter'|trans }} {{ 'Exporter'|trans }}
</a> </a>
{% endblock blockcontent %} {% 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