github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/core/documents/documents.go (about) 1 // Ce package regroupe les fonctions liées à l'édition et au partage (mail, etc) 2 // des documents (au sens fichier, et non rawdata.Document) 3 // Ce package repose sur les utilitaires pdf et excel notamment 4 package documents 5 6 import ( 7 "bytes" 8 "fmt" 9 "io/ioutil" 10 11 dm "github.com/benoitkugler/goACVE/server/core/datamodel" 12 13 rd "github.com/benoitkugler/goACVE/server/core/rawdata" 14 "github.com/benoitkugler/goACVE/server/core/utils/pdf" 15 ) 16 17 const PathRIB = "ressources/RIB.pdf" 18 19 var HeadersSuiviParticipants = []rd.Header{ 20 {Field: dm.FinancesPNomPrenom, Label: "Participant"}, 21 {Field: dm.FinancesPPrixBase, Label: "Prix de base (€)"}, 22 {Field: dm.FinancesPPrixNet, Label: "Montant attendu (€)"}, 23 {Field: dm.FinancesPTotalAides, Label: "Dont aides (€)"}, 24 {Field: dm.FinancesPEtatPaiement, Label: "Etat du paiement"}, 25 } 26 27 // -------------------------- Documents à générer -------------------------- 28 29 type DynamicDocument interface { 30 Generate() ([]byte, error) 31 FileName() string 32 } 33 34 // RGPD - On préfère ne pas mettre les tels 35 var fieldsParticipants = []pdf.ColumnMetas{ 36 {Field: dm.PersonneNomPrenom, Label: "Participant", Width: 0.25}, 37 {Field: dm.ParticipantRespoNomPrenom, Label: "Responsable", Width: 0.22}, 38 {Field: dm.ParticipantRespoMail, Label: "Mail", Width: 0.28}, 39 {Field: dm.ParticipantRespoCommune, Label: "Communne", Width: 0.25}, 40 } 41 42 type ListeParticipants struct { 43 Liste rd.Table 44 LabelCamp string 45 } 46 47 func NewListeParticipants(ac dm.AccesCamp) ListeParticipants { 48 inscrits, _ := ac.GetListes(false, "", true, true) 49 return ListeParticipants{Liste: inscrits, LabelCamp: ac.RawData().Label().String()} 50 } 51 52 func (l ListeParticipants) Generate() ([]byte, error) { 53 out := new(bytes.Buffer) 54 err := pdf.ListeParticipants(fieldsParticipants, l.Liste, l.LabelCamp, out) 55 if err != nil { 56 return nil, err 57 } 58 return out.Bytes(), nil 59 } 60 61 func (l ListeParticipants) FileName() string { 62 return fmt.Sprintf("Participants - %s.pdf", l.LabelCamp) 63 } 64 65 type ListeVetements struct { 66 Liste rd.Table 67 Complement string 68 LabelCamp string 69 } 70 71 func NewListeVetements(camp rd.Camp) ListeVetements { 72 lv := ListeVetements{LabelCamp: camp.Label().String(), Complement: camp.ListeVetements.Complement} 73 for _, vet := range camp.ListeVetements.Liste { 74 lv.Liste = append(lv.Liste, vet.AsItem()) 75 } 76 return lv 77 } 78 79 func (l ListeVetements) Generate() ([]byte, error) { 80 out := new(bytes.Buffer) 81 err := pdf.ListeVetements(l.Liste, l.Complement, l.LabelCamp, out) 82 if err != nil { 83 return nil, err 84 } 85 return out.Bytes(), nil 86 } 87 88 func (l ListeVetements) FileName() string { 89 return fmt.Sprintf("Liste de vêtements - %s.pdf", l.LabelCamp) 90 } 91 92 // Facture définit les champs nécessaire 93 // à la création d'une facture au format pdf. 94 type Facture struct { 95 Destinataire rd.Destinataire 96 Bilan dm.BilanFacture 97 } 98 99 func (p Facture) Generate() ([]byte, error) { 100 out := new(bytes.Buffer) 101 102 if err := pdf.Facture(p.Destinataire, p.Bilan, out); err != nil { 103 return nil, err 104 } 105 return out.Bytes(), nil 106 } 107 108 func (p Facture) FileName() string { 109 if p.Bilan.IsAcquitte() { 110 return "Facture acquittée.pdf" 111 } 112 return "Facture.pdf" 113 } 114 115 type Presence struct { 116 Destinataire rd.Destinataire 117 Participants []dm.AccesParticipant 118 } 119 120 func (p Presence) Generate() ([]byte, error) { 121 out := new(bytes.Buffer) 122 if err := pdf.AttestationPresence(p.Destinataire, p.Participants, out); err != nil { 123 return nil, err 124 } 125 return out.Bytes(), nil 126 } 127 128 func (p Presence) FileName() string { 129 return "Attestation de présence.pdf" 130 } 131 132 // ------------------------- RIB --------------------------------- 133 134 type RIB struct{} 135 136 func (r RIB) Generate() ([]byte, error) { 137 return ioutil.ReadFile(PathRIB) 138 } 139 140 func (r RIB) FileName() string { 141 return "RIB_ACVE.pdf" 142 } 143 144 func TestRIB() error { 145 _, err := RIB{}.Generate() 146 if err != nil { 147 return fmt.Errorf("RIB illisible : %s", err) 148 } 149 return nil 150 }