Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 53 additions & 170 deletions sources/Afup/Association/Cotisations.php

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions sources/Afup/Association/CotisationsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Afup\Site\Utils\Utils;
use AppBundle\Association\Model\Repository\CompanyMemberRepository;
use AppBundle\MembershipFee\Model\Repository\MembershipFeeRepository;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

Expand All @@ -15,6 +16,7 @@ public function __construct(
private TokenStorageInterface $tokenStorage,
private CompanyMemberRepository $companyMemberRepository,
private AuthorizationCheckerInterface $authorizationChecker,
private MembershipFeeRepository $membershipFeeRepository,
) {}

public function create(): Cotisations
Expand All @@ -25,6 +27,7 @@ public function create(): Cotisations
);

$cotisations->setCompanyMemberRepository($this->companyMemberRepository);
$cotisations->setMembershipFeeRepository($this->membershipFeeRepository);

return $cotisations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
use Afup\Site\Utils\Utils;
use AppBundle\Association\MemberType;
use AppBundle\Association\Model\CompanyMember;
use AppBundle\MembershipFee\Model\MembershipFee;

final readonly class SubscriptionManagement
{
public function __construct(private Cotisations $cotisations) {}

public function createInvoiceForInscription(CompanyMember $company, $numberOfMembers): array
{
$endSubscription = $this->cotisations->finProchaineCotisation(false);
$endSubscription = $this->cotisations->getNextSubscriptionExpiration(null);

// Create the invoice
$this->cotisations->ajouter(
Expand All @@ -28,12 +29,12 @@ public function createInvoiceForInscription(CompanyMember $company, $numberOfMem
$endSubscription->format('U'),
'',
);
$subscriptionArray = $this->cotisations->obtenirDerniere(MemberType::MemberCompany, $company->getId());
$subscription = $this->cotisations->getLastestByUserTypeAndId(MemberType::MemberCompany, $company->getId());

if ($subscriptionArray === false) {
if (!$subscription instanceof MembershipFee) {
throw new \RuntimeException('An error occured');
}

return ['invoice' => $subscriptionArray['numero_facture'], 'token' => $subscriptionArray['token']];
return ['invoice' => $subscription->getInvoiceNumber(), 'token' => $subscription->getToken()];
}
}
21 changes: 13 additions & 8 deletions sources/AppBundle/Association/UserMembership/SeniorityComputer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

namespace AppBundle\Association\UserMembership;

use Afup\Site\Association\Cotisations;
use AppBundle\Association\MemberType;
use AppBundle\Association\Model\CompanyMember;
use AppBundle\Association\Model\User;
use AppBundle\MembershipFee\Model\MembershipFee;
use AppBundle\MembershipFee\Model\Repository\MembershipFeeRepository;
use CCMBenchmark\Ting\Repository\Collection;

class SeniorityComputer
{
public function __construct(private readonly Cotisations $cotisations) {}
public function __construct(private readonly MembershipFeeRepository $membershipFeeRepository) {}

public function computeCompany(CompanyMember $companyMember)
{
$cotis = $this->cotisations->obtenirListe(MemberType::MemberCompany, $companyMember->getId());
$cotis = $this->membershipFeeRepository->getListByUserTypeAndId(MemberType::MemberCompany, $companyMember->getId());

$infos = $this->computeFromCotisationsAndReturnInfos($cotis);

Expand All @@ -24,7 +26,7 @@ public function computeCompany(CompanyMember $companyMember)

public function computeCompanyAndReturnInfos(CompanyMember $companyMember): array
{
$cotis = $this->cotisations->obtenirListe(MemberType::MemberCompany, $companyMember->getId());
$cotis = $this->membershipFeeRepository->getListByUserTypeAndId(MemberType::MemberCompany, $companyMember->getId());

return $this->computeFromCotisationsAndReturnInfos($cotis);
}
Expand All @@ -38,20 +40,23 @@ public function compute(User $user)

public function computeAndReturnInfos(User $user): array
{
$cotis = $this->cotisations->obtenirListe(MemberType::MemberPhysical, $user->getId());
$cotis = $this->membershipFeeRepository->getListByUserTypeAndId(MemberType::MemberPhysical, $user->getId());

return $this->computeFromCotisationsAndReturnInfos($cotis);
}

private function computeFromCotisationsAndReturnInfos(array $cotisations): array
/**
* @param Collection<MembershipFee> $cotisations
*/
private function computeFromCotisationsAndReturnInfos(Collection $cotisations): array
{
$now = new \DateTime();
$diffs = [];

$years = [];
foreach ($cotisations as $coti) {
$from = new \DateTimeImmutable('@' . $coti['date_debut']);
$to = new \DateTimeImmutable('@' . $coti['date_fin']);
$from = $coti->getStartDate();
$to = $coti->getEndDate();
$to = min($now, $to);
$diffs[] = $from->diff($to);
$years[] = $from->format('Y');
Expand Down
8 changes: 3 additions & 5 deletions sources/AppBundle/Association/UserMembership/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use AppBundle\Email\Mailer\MailUser;
use AppBundle\Email\Mailer\MailUserFactory;
use AppBundle\Email\Mailer\Message;
use AppBundle\MembershipFee\Model\MembershipFee;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

Expand Down Expand Up @@ -85,10 +86,7 @@ public function sendWelcomeEmail(User $user): bool
return $this->mailer->send($message);
}

/**
* @return array
*/
public function getLastSubscription(User $user)
public function getLastSubscription(User $user): ?MembershipFee
{
if ($user->getCompanyId()) {
$id = $user->getCompanyId();
Expand All @@ -98,6 +96,6 @@ public function getLastSubscription(User $user)
$personType = MemberType::MemberPhysical;
}

return $this->cotisations->obtenirDerniere($personType, $id);
return $this->cotisations->getLastestByUserTypeAndId($personType, $id);
}
}
5 changes: 3 additions & 2 deletions sources/AppBundle/Controller/Website/Member/IndexAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use AppBundle\Association\UserMembership\BadgesComputer;
use AppBundle\Association\UserMembership\UserService;
use AppBundle\GeneralMeeting\GeneralMeetingRepository;
use AppBundle\MembershipFee\Model\MembershipFee;
use AppBundle\Security\Authentication;
use AppBundle\Twig\ViewRenderer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand Down Expand Up @@ -36,8 +37,8 @@ public function __invoke(): Response
$cotisation = $userService->getLastSubscription($user);

$dateFinCotisation = null;
if ($cotisation) {
$dateFinCotisation = new \DateTimeImmutable('@' . $cotisation['date_fin']);
if ($cotisation instanceof MembershipFee) {
$dateFinCotisation = $cotisation->getEndDate();
}

$daysBeforeMembershipExpiration = $user->getDaysBeforeMembershipExpiration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use AppBundle\Association\Model\Repository\UserRepository;
use AppBundle\Association\Model\User;
use AppBundle\AuditLog\Audit;
use AppBundle\MembershipFee\Model\Repository\MembershipFeeRepository;
use Assert\Assertion;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
Expand All @@ -23,6 +24,7 @@ final class DownloadAction extends AbstractController
public function __construct(
private readonly UserRepository $userRepository,
private readonly CompanyMemberRepository $companyMemberRepository,
private readonly MembershipFeeRepository $membershipFeeRepository,
private readonly Cotisations $cotisations,
private readonly Droits $droits,
private readonly Audit $audit,
Expand All @@ -40,19 +42,19 @@ public function __invoke(Request $request): BinaryFileResponse

$tempfile = tempnam(sys_get_temp_dir(), 'membership_fee_download');
$numeroFacture = $this->cotisations->genererFacture($id, $tempfile);
$cotisation = $this->cotisations->obtenir($id);
$membershipFee = $this->membershipFeeRepository->get($id);

if ($cotisation['type_personne'] == MemberType::MemberCompany->value) {
$company = $this->companyMemberRepository->get($cotisation['id_personne']);
if ($membershipFee->getUserType() == MemberType::MemberCompany) {
$company = $this->companyMemberRepository->get($membershipFee->getUserId());
Assertion::isInstanceOf($company, CompanyMember::class);
$patternPrefix = $company->getCompanyName();
} else {
$user = $this->userRepository->get($cotisation['id_personne']);
$user = $this->userRepository->get($membershipFee->getUserId());
Assertion::isInstanceOf($user, User::class);
$patternPrefix = $user->getLastName();
}

$pattern = str_replace(' ', '', $patternPrefix) . '_' . $numeroFacture . '_' . date('dmY', (int) $cotisation['date_debut']) . '.pdf';
$pattern = str_replace(' ', '', $patternPrefix) . '_' . $numeroFacture . '_' . $membershipFee->getStartDate()->format('dmY') . '.pdf';

$response = new BinaryFileResponse($tempfile, Response::HTTP_OK, [], false);
$response->deleteFileAfterSend(true);
Expand Down
30 changes: 13 additions & 17 deletions sources/AppBundle/Controller/Website/Membership/Fee/IndexAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use AppBundle\Association\Model\Repository\CompanyMemberRepository;
use AppBundle\Association\Model\Repository\UserRepository;
use AppBundle\Association\UserMembership\UserService;
use AppBundle\MembershipFee\Model\MembershipFee;
use AppBundle\MembershipFee\Model\Repository\MembershipFeeRepository;
use AppBundle\Payment\PayboxBilling;
use AppBundle\Payment\PayboxFactory;
use AppBundle\Twig\ViewRenderer;
Expand All @@ -29,6 +31,7 @@ public function __construct(
private readonly UserService $userService,
private readonly PayboxFactory $payboxFactory,
private readonly Cotisations $cotisations,
private readonly MembershipFeeRepository $membershipFeeRepository,
private readonly Droits $droits,
) {}

Expand All @@ -43,34 +46,27 @@ public function __invoke(): Response
$now = new \DateTime('now');
$isSubjectedToVat = Vat::isSubjectedToVat($now);

if (!$cotisation) {
if (!$cotisation instanceof MembershipFee) {
$message = '';
} else {
$endSubscription = $this->cotisations->finProchaineCotisation($cotisation);
$endSubscription = $this->cotisations->getNextSubscriptionExpiration($cotisation);
$message = sprintf(
'Votre dernière cotisation -- %s € -- est valable jusqu\'au %s. <br />
Si vous renouvelez votre cotisation maintenant, celle-ci sera valable jusqu\'au %s.',
number_format((float) $cotisation['montant'], 2, ',', ' '),
date("d/m/Y", (int) $cotisation['date_fin']),
number_format((float) $cotisation->getAmount(), 2, ',', ' '),
$cotisation->getEndDate()->format('d/m/Y'),
$endSubscription->format('d/m/Y'),
);
}

$cotisations_physique = $this->cotisations->obtenirListe(MemberType::MemberPhysical, $user->getId());
$cotisations_morale = $this->cotisations->obtenirListe(MemberType::MemberCompany, $user->getCompanyId());
$cotisations_physique = $this->membershipFeeRepository->getListByUserTypeAndId(MemberType::MemberPhysical, $user->getId());
$cotisations_morale = $this->membershipFeeRepository->getListByUserTypeAndId(MemberType::MemberCompany, $user->getCompanyId());

if (is_array($cotisations_morale) && is_array($cotisations_physique)) {
$liste_cotisations = array_merge($cotisations_physique, $cotisations_morale);
} elseif (is_array($cotisations_morale)) {
$liste_cotisations = $cotisations_morale;
} elseif (is_array($cotisations_physique)) {
$liste_cotisations = $cotisations_physique;
} else {
$liste_cotisations = [];
}
/** @var array<int, MembershipFee> $liste_cotisations */
$liste_cotisations = array_merge(iterator_to_array($cotisations_physique), iterator_to_array($cotisations_morale));

foreach ($liste_cotisations as $k => $cotisation) {
$liste_cotisations[$k]['telecharger_facture'] = $this->cotisations->isCurrentUserAllowedToReadInvoice($cotisation['id']);
$cotisation->setDownloadInvoice($this->cotisations->isCurrentUserAllowedToReadInvoice((string) $cotisation->getId()));
}

if ($user->getCompanyId() > 0) {
Expand Down Expand Up @@ -112,7 +108,7 @@ public function __invoke(): Response
'isSubjectedToVat' => $isSubjectedToVat,
'title' => 'Ma cotisation',
'cotisations' => $liste_cotisations,
'time' => time(),
'time' => new \DateTime(),
'montant' => $montant,
'libelle' => $libelle,
'paybox' => $paybox,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __invoke(Request $request): Response
$generalMeetingPlanned = $generalMeetingRepository->hasGeneralMeetingPlanned();

$cotisation = $userService->getLastSubscription($user);
$needsMembersheepFeePayment = $latestDate->getTimestamp() > strtotime("+14 day", (int) $cotisation['date_fin']);
$needsMembersheepFeePayment = $latestDate->getTimestamp() > strtotime("+14 day", $cotisation->getEndDate()->getTimestamp());

if ($needsMembersheepFeePayment) {
return $this->view->render('admin/association/membership/generalmeeting_membersheepfee.html.twig', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace AppBundle\Controller\Website\Membership;

use Afup\Site\Association\Cotisations;
use AppBundle\MembershipFee\Model\MembershipFee;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -18,12 +19,12 @@ public function __invoke(string $invoiceNumber, ?string $token): Response
{
$invoice = $this->cotisations->getByInvoice($invoiceNumber, $token);

if (!$invoice) {
if (!$invoice instanceof MembershipFee) {
throw $this->createNotFoundException(sprintf('Could not find the invoice "%s" with token "%s"', $invoiceNumber, $token));
}

ob_start();
$this->cotisations->genererFacture($invoice['id']);
$this->cotisations->genererFacture($invoice->getId());
$pdf = ob_get_clean();

$response = new Response($pdf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use AppBundle\Association\Model\Repository\CompanyMemberRepository;
use AppBundle\Association\Model\Repository\UserRepository;
use AppBundle\AuditLog\Audit;
use AppBundle\MembershipFee\Model\MembershipFee;
use AppBundle\Payment\PayboxResponseFactory;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -46,9 +47,9 @@ public function __invoke(Request $request): Response

if ($etat == AFUP_COTISATIONS_PAIEMENT_REGLE) {
$account = $this->cotisations->getAccountFromCmd($payboxResponse->getCmd());
$lastCotisation = $this->cotisations->obtenirDerniere(MemberType::from($account['type']), $account['id']);
$lastCotisation = $this->cotisations->getLastestByUserTypeAndId(MemberType::from($account['type']), $account['id']);

if ($lastCotisation === false && $account['type'] == MemberType::MemberPhysical->value) {
if (!$lastCotisation instanceof MembershipFee && $account['type'] == MemberType::MemberPhysical->value) {
$user = $this->userRepository->get($account['id']);
$this->eventDispatcher->dispatch(new NewMemberEvent($user));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Afup\Site\Association\Cotisations;
use AppBundle\Association\Model\Repository\CompanyMemberRepository;
use AppBundle\Compta\BankAccount\BankAccountFactory;
use AppBundle\MembershipFee\Model\MembershipFee;
use AppBundle\Payment\PayboxBilling;
use AppBundle\Payment\PayboxFactory;
use AppBundle\Twig\ViewRenderer;
Expand All @@ -25,7 +26,10 @@ public function __construct(
public function __invoke(string $invoiceNumber, ?string $token): Response
{
$invoice = $this->cotisations->getByInvoice($invoiceNumber, $token);
$company = $this->companyMemberRepository->get($invoice['id_personne']);
$company = null;
if ($invoice instanceof MembershipFee) {
$company = $this->companyMemberRepository->get($invoice->getUserId());
}

if (!$invoice || $company === null) {
throw $this->createNotFoundException(sprintf('Could not find the invoice "%s" with token "%s"', $invoiceNumber, $token));
Expand All @@ -35,7 +39,7 @@ public function __invoke(string $invoiceNumber, ?string $token): Response

$paybox = $this->payboxFactory->createPayboxForSubscription(
'F' . $invoiceNumber,
(float) $invoice['montant'],
(float) $invoice->getAmount(),
$company->getEmail(),
$payboxBilling,
);
Expand All @@ -45,7 +49,7 @@ public function __invoke(string $invoiceNumber, ?string $token): Response
return $this->view->render('site/company_membership/payment.html.twig', [
'paybox' => $paybox,
'invoice' => $invoice,
'bankAccount' => $bankAccountFactory->createApplyableAt(new \DateTimeImmutable('@' . $invoice['date_debut'])),
'bankAccount' => $bankAccountFactory->createApplyableAt(new \DateTimeImmutable('@' . $invoice->getStartDate()->getTimestamp())),
'afup' => [
'raison_sociale' => AFUP_RAISON_SOCIALE,
'adresse' => AFUP_ADRESSE,
Expand Down
12 changes: 12 additions & 0 deletions sources/AppBundle/MembershipFee/Model/MembershipFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class MembershipFee implements NotifyPropertyInterface
private ?int $nbReminders = null;
private ?DateTime $lastReminderDate = null;

private bool $downloadInvoice = false;

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -196,4 +198,14 @@ public function setLastReminderDate(?DateTime $lastReminderDate): self
$this->lastReminderDate = $lastReminderDate;
return $this;
}

public function canDownloadInvoice(): bool
{
return $this->downloadInvoice;
}

public function setDownloadInvoice(bool $hasPermission): bool
{
return $this->downloadInvoice = $hasPermission;
}
}
Loading
Loading