55namespace Afup \Site \Association ;
66
77use Afup \Site \Droits ;
8- use Afup \Site \Utils \Base_De_Donnees ;
98use Afup \Site \Utils \Mailing ;
109use Afup \Site \Utils \PDF_Facture ;
1110use Afup \Site \Utils \Utils ;
1211use Afup \Site \Utils \Vat ;
12+ use AppBundle \Accounting \Invoices \InvoiceGenerator ;
1313use AppBundle \Association \MemberType ;
14+ use AppBundle \Controller \Admin \Membership \MembershipFeePayment ;
15+ use AppBundle \Association \Model \CompanyMember ;
1416use AppBundle \Association \Model \Repository \CompanyMemberRepository ;
1517use AppBundle \Association \Model \Repository \UserRepository ;
18+ use AppBundle \Association \Model \User ;
1619use AppBundle \Compta \BankAccount \BankAccountFactory ;
1720use AppBundle \Email \Mailer \Attachment ;
1821use AppBundle \Email \Mailer \Mailer ;
@@ -45,45 +48,11 @@ class Cotisations
4548 private ?MembershipFeeRepository $ membershipFeeRepository = null ;
4649
4750 public function __construct (
48- private readonly Base_De_Donnees $ _bdd ,
51+ private readonly UserRepository $ userRepository ,
52+ private readonly InvoiceGenerator $ invoiceGenerator ,
4953 private readonly ?Droits $ _droits = null ,
5054 ) {}
5155
52- /**
53- * Renvoit la cotisation demandée
54- *
55- * @param int $id Identifiant de la cotisation
56- * @param string $champs Champs à renvoyer
57- * @return array
58- */
59- public function obtenir ($ id , string $ champs = '* ' )
60- {
61- $ requete = 'SELECT ' ;
62- $ requete .= ' ' . $ champs . ' ' ;
63- $ requete .= 'FROM ' ;
64- $ requete .= ' afup_cotisations ' ;
65- $ requete .= 'WHERE ' ;
66- $ requete .= ' id= ' . $ id ;
67- return $ this ->_bdd ->obtenirEnregistrement ($ requete );
68- }
69-
70- /**
71- * Renvoit le numéro de la prochaine facture au format : {année}-{index depuis le début de l'année}
72- *
73- */
74- public function _genererNumeroFacture (): string
75- {
76- $ requete = 'SELECT ' ;
77- $ requete .= " MAX(CAST(SUBSTRING_INDEX(numero_facture, '-', -1) AS UNSIGNED)) + 1 " ;
78- $ requete .= 'FROM ' ;
79- $ requete .= ' afup_cotisations ' ;
80- $ requete .= 'WHERE ' ;
81- $ requete .= ' LEFT(numero_facture, 4)= ' . $ this ->_bdd ->echapper (date ('Y ' ));
82- $ requete .= ' OR LEFT(numero_facture, 10)= ' . $ this ->_bdd ->echapper ("COTIS- " . date ('Y ' ));
83- $ index = $ this ->_bdd ->obtenirUn ($ requete );
84- return 'COTIS- ' . date ('Y ' ) . '- ' . (is_null ($ index ) ? 1 : $ index );
85- }
86-
8756 /**
8857 * Ajoute une cotisation
8958 *
@@ -101,23 +70,27 @@ public function _genererNumeroFacture(): string
10170 public function ajouter (MemberType $ type_personne , $ id_personne , $ montant , $ type_reglement ,
10271 $ informations_reglement , $ date_debut , $ date_fin , $ commentaires , $ referenceClient = null ): bool
10372 {
104- $ requete = 'INSERT INTO ' ;
105- $ requete .= ' afup_cotisations (type_personne, id_personne, montant, type_reglement , informations_reglement, ' ;
106- $ requete .= ' date_debut, date_fin, numero_facture, token, commentaires, reference_client, date_facture) ' ;
107- $ requete .= 'VALUES ( ' ;
108- $ requete .= $ type_personne ->value . ', ' ;
109- $ requete .= $ id_personne . ', ' ;
110- $ requete .= $ montant . ', ' ;
111- $ requete .= $ this ->_bdd ->echapper ($ type_reglement ) . ', ' ;
112- $ requete .= $ this ->_bdd ->echapper ($ informations_reglement ) . ', ' ;
113- $ requete .= $ date_debut . ', ' ;
114- $ requete .= $ date_fin . ', ' ;
115- $ requete .= $ this ->_bdd ->echapper ($ this ->_genererNumeroFacture ()) . ', ' ;
116- $ requete .= $ this ->_bdd ->echapper (base64_encode (random_bytes (30 ))) . ', ' ;
117- $ requete .= $ this ->_bdd ->echapper ($ commentaires ) . ', ' ;
118- $ requete .= $ this ->_bdd ->echapper ($ referenceClient ) . ', ' ;
119- $ requete .= $ this ->_bdd ->echapper (date ('Y-m-d\TH:i:s ' )) . ') ' ;
120- return $ this ->_bdd ->executer ($ requete ) !== false ;
73+ try {
74+ $ membershipFee = new MembershipFee ();
75+ $ membershipFee
76+ ->setUserType ($ type_personne )
77+ ->setUserId ($ id_personne )
78+ ->setAmount ($ montant )
79+ ->setPaymentType (MembershipFeePayment::from ($ type_reglement ))
80+ ->setPaymentDetails ($ informations_reglement )
81+ ->setStartDate (new DateTime ('@ ' . $ date_debut ))
82+ ->setEndDate (new DateTime ('@ ' . $ date_fin ))
83+ ->setInvoiceNumber ($ this ->membershipFeeRepository ->generateInvoiceNumber ())
84+ ->setToken (base64_encode (random_bytes (30 )))
85+ ->setComments ($ commentaires )
86+ ->setClientReference ($ referenceClient )
87+ ->setInvoiceDate (new \DateTimeImmutable ())
88+ ;
89+ $ this ->membershipFeeRepository ->save ($ membershipFee );
90+ return true ;
91+ } catch (\Exception $ e ) {
92+ return false ;
93+ }
12194 }
12295
12396 public function isAlreadyPaid ($ cmd ): bool
@@ -267,12 +240,17 @@ public function supprimer(int $id): bool
267240 */
268241 public function genererFacture ($ id_cotisation , $ chemin = null )
269242 {
270- $ requete = 'SELECT * FROM afup_cotisations WHERE id= ' . $ id_cotisation ;
271- $ cotisation = $ this ->_bdd ->obtenirEnregistrement ($ requete );
243+ $ cotisation = $ this ->membershipFeeRepository ->get ($ id_cotisation );
244+
245+
246+ $ userRepository = match ($ cotisation ->getUserType ()) {
247+ MemberType::MemberCompany => $ this ->companyMemberRepository ,
248+ default => $ this ->userRepository ,
249+ };
272250
273- $ table = $ cotisation [ ' type_personne ' ] == MemberType::MemberCompany-> value ? ' afup_personnes_morales ' : ' afup_personnes_physiques ' ;
274- $ requete = ' SELECT * FROM ' . $ table . ' WHERE id= ' . $ cotisation[ ' id_personne ' ] ;
275- $ personne = $ this ->_bdd -> obtenirEnregistrement ( $ requete );
251+ /** @var User|CompanyMember $user */
252+ $ user = $ userRepository -> get ( $ cotisation-> getUserId ()) ;
253+ $ invoiceData = $ this ->invoiceGenerator -> getInvoiceData ( $ user );
276254
277255 if ($ cotisation ['date_facture ' ] !== null ) {
278256 $ dateFacture = \DateTimeImmutable::createFromFormat ('Y-m-d H:i:s ' , $ cotisation ['date_facture ' ]);
@@ -298,15 +276,8 @@ public function genererFacture($id_cotisation, $chemin = null)
298276 $ pdf ->Cell (130 , 5 , 'Objet : Facture n° ' . $ cotisation ['numero_facture ' ]);
299277 $ pdf ->SetFont ('Arial ' , '' , 10 );
300278
301- if ($ cotisation ['type_personne ' ] == MemberType::MemberCompany->value ) {
302- $ nom = $ personne ['raison_sociale ' ];
303- $ patternPrefix = $ personne ['raison_sociale ' ];
304- } else {
305- $ nom = $ personne ['prenom ' ] . ' ' . $ personne ['nom ' ];
306- $ patternPrefix = $ personne ['nom ' ];
307- }
308279 $ pdf ->Ln (10 );
309- $ pdf ->MultiCell (130 , 5 , $ nom . "\n" . $ personne [ ' adresse ' ] . "\n" . $ personne [ ' code_postal ' ] . "\n" . $ personne [ ' ville ' ] );
280+ $ pdf ->MultiCell (130 , 5 , $ invoiceData -> recipient . "\n" . $ invoiceData -> address . "\n" . $ invoiceData -> zipcode . "\n" . $ invoiceData -> city );
310281
311282 if (isset ($ cotisation ['reference_client ' ])) {
312283 $ pdf ->Ln (10 );
@@ -379,7 +350,7 @@ public function genererFacture($id_cotisation, $chemin = null)
379350 $ pdf ->Cell (10 , 5 , 'Lors de votre règlement, merci de préciser la mention : "Facture n° ' . $ cotisation ['numero_facture ' ] . '" ' );
380351
381352 if (is_null ($ chemin )) {
382- $ pattern = str_replace (' ' , '' , $ patternPrefix ) . '_ ' . $ cotisation ['numero_facture ' ] . '_ ' . date ('dmY ' , (int ) $ cotisation ['date_debut ' ]) . '.pdf ' ;
353+ $ pattern = str_replace (' ' , '' , $ invoiceData -> patternPrefix ) . '_ ' . $ cotisation ['numero_facture ' ] . '_ ' . date ('dmY ' , (int ) $ cotisation ['date_debut ' ]) . '.pdf ' ;
383354
384355 $ pdf ->Output ($ pattern , 'D ' , true );
385356 } else {
@@ -446,18 +417,18 @@ private function formatFactureValue($value): string
446417 */
447418 public function envoyerFacture ($ id_cotisation , Mailer $ mailer , UserRepository $ userRepository )
448419 {
449- $ personne = $ this ->obtenir ($ id_cotisation, ' type_personne, id_personne ' );
420+ $ membership = $ this ->membershipFeeRepository -> get ($ id_cotisation );
450421
451- if ($ personne [ ' type_personne ' ] == MemberType::MemberCompany->value ) {
452- $ company = $ this ->companyMemberRepository ? $ this ->companyMemberRepository ->get ($ personne [ ' id_personne ' ] ) : null ;
422+ if ($ membership -> getUserType () == MemberType::MemberCompany->value ) {
423+ $ company = $ this ->companyMemberRepository ? $ this ->companyMemberRepository ->get ($ membership -> getUserId () ) : null ;
453424 Assert::notNull ($ company );
454425 $ contactPhysique = [
455426 'nom ' => $ company ->getLastName (),
456427 'prenom ' => $ company ->getFirstName (),
457428 'email ' => $ company ->getEmail (),
458429 ];
459430 } else {
460- $ user = $ userRepository ->get ($ personne [ ' id_personne ' ] );
431+ $ user = $ userRepository ->get ($ membership -> getUserId () );
461432 Assert::notNull ($ user );
462433 $ contactPhysique = [
463434 'nom ' => $ user ->getLastName (),
@@ -477,7 +448,7 @@ public function envoyerFacture($id_cotisation, Mailer $mailer, UserRepository $u
477448
478449 $ cheminFacture = AFUP_CHEMIN_RACINE . 'cache/fact ' . $ id_cotisation . '.pdf ' ;
479450 $ numeroFacture = $ this ->genererFacture ($ id_cotisation , $ cheminFacture );
480- $ cotisation = $ this ->getLastestByUserTypeAndId (MemberType:: from (( int ) $ personne [ ' type_personne ' ]), ( int ) $ personne [ ' id_personne ' ] );
451+ $ cotisation = $ this ->getLastestByUserTypeAndId ($ membership -> getUserType (), $ membership -> getUserId () );
481452 $ pattern = str_replace (' ' , '' , $ patternPrefix ) . '_ ' . $ numeroFacture . '_ ' . date ('dmY ' , $ cotisation ->getStartDate ()->getTimestamp ()) . '.pdf ' ;
482453
483454 $ message = new Message ('Facture AFUP ' , null , new MailUser (
0 commit comments