unova_factur_x 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 996d6179a21f9d65c42b237554a9107c0acb063cbf0c9ac6119ede6a73c429bd
4
+ data.tar.gz: 7767c329f0a3e09c314d52a81880111e96059af0223841fb07f9daa524e3c382
5
+ SHA512:
6
+ metadata.gz: 8eb85a7a4c6e7919a761b0c32c54e06bc3ec46ca0bf32595a3ad3999cf161e1808739f9acf11cd318be36fab01d41e7130ab3a588bedb4dd917329d5eeda999b
7
+ data.tar.gz: 4ecc28ab0f7a1d9253bc8d211e1fd360b5c77096deccf3eed6fb014199029bd3543a6bc8965814e62118fb5e1287bfc851a22e7255e78b55fd812db80c449b57
data/.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
data/.idea/misc.xml ADDED
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="asdf: 3.1.1" project-jdk-type="RUBY_SDK" />
4
+ </project>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/unova_factur_x.iml" filepath="$PROJECT_DIR$/.idea/unova_factur_x.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,35 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$">
8
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
+ </content>
12
+ <orderEntry type="inheritedJdk" />
13
+ <orderEntry type="sourceFolder" forTests="false" />
14
+ <orderEntry type="library" scope="PROVIDED" name="ast (v2.4.2, asdf: 3.1.1) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.5.22, asdf: 3.1.1) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="io-console (v0.7.2, asdf: 3.1.1) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="irb (v1.14.1, asdf: 3.1.1) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="json (v2.7.4, asdf: 3.1.1) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="language_server-protocol (v3.17.0.3, asdf: 3.1.1) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="parallel (v1.26.3, asdf: 3.1.1) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="parser (v3.3.5.0, asdf: 3.1.1) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="psych (v5.1.2, asdf: 3.1.1) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="racc (v1.8.1, asdf: 3.1.1) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="rainbow (v3.1.1, asdf: 3.1.1) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="rake (v13.2.1, asdf: 3.1.1) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="rdoc (v6.7.0, asdf: 3.1.1) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="regexp_parser (v2.9.2, asdf: 3.1.1) [gem]" level="application" />
28
+ <orderEntry type="library" scope="PROVIDED" name="reline (v0.5.10, asdf: 3.1.1) [gem]" level="application" />
29
+ <orderEntry type="library" scope="PROVIDED" name="rubocop (v1.67.0, asdf: 3.1.1) [gem]" level="application" />
30
+ <orderEntry type="library" scope="PROVIDED" name="rubocop-ast (v1.32.3, asdf: 3.1.1) [gem]" level="application" />
31
+ <orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.13.0, asdf: 3.1.1) [gem]" level="application" />
32
+ <orderEntry type="library" scope="PROVIDED" name="stringio (v3.1.1, asdf: 3.1.1) [gem]" level="application" />
33
+ <orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v2.6.0, asdf: 3.1.1) [gem]" level="application" />
34
+ </component>
35
+ </module>
data/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-06-04
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Rodolphe Limousin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # UnovaFacturX
2
+
3
+ Gem permettant la génération de factures et d'avoirs au format Factur-X.
4
+
5
+ ## Installation
6
+
7
+ Ajouter la gem au gemfile et faire un ```bundle install```
8
+ ```ruby
9
+ gem "unova_factur_x", git: "git@gitlab.unova.fr:unova-factur-x/unova-factur-x.git"
10
+ ```
11
+
12
+ ## Utilisation
13
+
14
+ Il suffit d'appeler la fonction generate de la gem à l'envoi du PDF par le controller avec les paramètres suivants :
15
+ - pdf: Le fichier PDF de la facture/avoir à transformer en Factur-X. Prends en charge ces deux formes :
16
+ - Depuis un fichier :
17
+ ```ruby
18
+ path = ActiveStorage::Blob.service.send(:path_for, @invoice.file.key)
19
+ pdf = File.open(path, 'rb')
20
+ ```
21
+ - Depuis une génération Prawn :
22
+ ```ruby
23
+ pdf = DocumentPdf.new(**options).render
24
+ ```
25
+ - document_hash: Le hash d'entrée pour la génération du XML, voir ci-après pour plus de détails.
26
+ - [optionnel] type: Le type de document (:invoice par défaut):
27
+ - :invoice pour une facture,
28
+ - :credit pour un avoir,
29
+ - [optionnel] with_validations: true ou false, si à true, va essayer de valider les données du hash fourni pour Factur-X /!\ Nécessite Java, à désactiver si Java non présent /!\ (true par défaut)
30
+ - [optionnel] devise: pour configurer la monnaie utilisée sur la facture/l'avoir (Euros 'EUR' par défaut).
31
+ ```ruby
32
+ # Exemple d'utilisation :
33
+ send_data UnovaFacturX.generate(pdf: pdf, document_hash: document_hash, type: :invoice, with_validations: true, devise: "USD"),
34
+ filename: "Factur-X.pdf",
35
+ type: 'application/pdf',
36
+ disposition: 'attachment'
37
+ ```
38
+
39
+ Pour le hash du document attendu :
40
+ - Les montants fournis doivent être arithmétiquement cohérents, aucune correction automatique n’est effectuée.
41
+ - Tous les attributs de la facture/du crédit sont attendus en String.
42
+ - Respecter la forme ci-dessous :
43
+ ```ruby
44
+ # Exemple de hash pour une facture (Même chose pour un avoir /!\ Ne pas mettre les valeurs de l'avoir en négatif /!\) :
45
+ document_hash = {
46
+ id: "Numéro unique de facture (BT-1) [OBLIGATOIRE]",
47
+ issue_date: "Date d'émission format YYYYMMDD (BT-2) [OBLIGATOIRE]",
48
+
49
+ seller: {
50
+ name: "Nom légal du vendeur (BT-27) [OBLIGATOIRE]",
51
+ legal_id: "Identifiant légal (SIREN/SIRET) (BT-30) [OPTIONNEL]",
52
+ vat_number: "Numéro TVA avec préfixe pays acheteur (ex: FR123...) (BT-31) [OPTIONNEL]",
53
+ address: {
54
+ line1: "Rue (BT-35) [OBLIGATOIRE]",
55
+ line2: "Complément adresse [OPTIONNEL]",
56
+ postcode: "Code postal (BT-38) [OBLIGATOIRE]",
57
+ city: "Ville (BT-37) [OBLIGATOIRE]",
58
+ country: "Code pays ISO 3166-1 alpha-2 (BT-40) [OBLIGATOIRE]",
59
+ }
60
+ },
61
+
62
+ # [BLOC OBLIGATOIRE]
63
+ buyer: {
64
+ id: "Identifiant interne client (BT-46) [OPTIONNEL]",
65
+ name: "Nom légal du client (BT-44) [OBLIGATOIRE]",
66
+ vat_number: "Numéro TVA avec préfixe pays acheteur (ex: FR123...) (BT-48) [OPTIONNEL]",
67
+ contact: { # [OPTIONNEL]
68
+ name: "Nom du contact client (BT-56) [OPTIONNEL]",
69
+ },
70
+ address: {
71
+ line1: "Rue (BT-50) [OBLIGATOIRE]",
72
+ line2: "Complément adresse [OPTIONNEL]",
73
+ postcode: "Code postal (BT-53) [OBLIGATOIRE]",
74
+ city: "Ville (BT-52) [OBLIGATOIRE]",
75
+ country: "Code pays ISO 3166-1 alpha-2 (BT-55) [OBLIGATOIRE]",
76
+ }
77
+ },
78
+
79
+ # [BLOC OPTIONNEL]
80
+ delivery: {
81
+ gln: "Identifiant GLN (schemeID 0088) (BT-71) [OPTIONNEL]",
82
+ gln_scheme: "0088: GLN (GS1), 0002: SIRENE (France), 9906: SIRET, 9915: TVA intracom FR, 0060: DUNS [OPTIONNEL | OBLIGATOIRE SI GLN]",
83
+ date: "Date réelle de livraison format YYYYMMDD (BT-72) [OPTIONNEL]",
84
+ address: {
85
+ line1: "Rue livraison (BT-75) [OPTIONNEL]",
86
+ line2: "Complément adresse livraison [OPTIONNEL]",
87
+ postcode: "Code postal livraison (BT-75) [OPTIONNEL]",
88
+ city: "Ville livraison (BT-74) [OPTIONNEL]",
89
+ country: "Code pays ISO 3166-1 alpha-2 (BT-76) [OPTIONNEL]",
90
+ }
91
+ },
92
+
93
+ # [BLOC OBLIGATOIRE] (minimum 1 item)
94
+ items: [
95
+ {
96
+ line_id: "Numéro de ligne (BT-126) [OBLIGATOIRE]",
97
+ seller_assigned_id: "Identifiant interne produit (BT-155) [OPTIONNEL]",
98
+ name: "Désignation produit/service (BT-153) [OBLIGATOIRE]",
99
+ quantity: "Quantité (BT-129) [OBLIGATOIRE]",
100
+ unit_code: "Code unité UN/ECE Rec20 (ex: H87, C62, DAY) (BT-130) [OBLIGATOIRE]",
101
+ price_ht: "Prix unitaire net HT (BT-146) [OBLIGATOIRE]",
102
+ vat_rate: "Taux TVA (BT-152) [OBLIGATOIRE]",
103
+ vat_category: "Catégorie TVA (S, Z, E, AE...) (BT-151) [OBLIGATOIRE]",
104
+ discount: { # [OPTIONNEL]
105
+ total_amount: "Montant de la remise applicable à la ligne de facture (BT-136) [OPTIONNEL sauf si discount]",
106
+ percentage: "Pourcentage de remise applicable à la ligne de facture (BT-138) [OPTIONNEL sauf si discount]",
107
+ # reason OU reason_code [OBLIGATOIRE] si bloc présent
108
+ reason: "Motif de la remise applicable à la ligne de facture (BT-139) [OPTIONNEL sauf si discount]",
109
+ reason_code: "Code de motif de la remise applicable à la ligne de facture (BT-140) [OPTIONNEL sauf si discount]"
110
+ },
111
+ line_total: "Montant net de la ligne HT = Quantité × Prix unitaire net (BT-131)"
112
+ }
113
+ ],
114
+
115
+ # [BLOC OBLIGATOIRE]
116
+ payment_means: {
117
+ type_code: "Code UNCL 4461 (30 = virement) (BT-81) [OBLIGATOIRE]",
118
+ iban: "IBAN bénéficiaire (BT-84) [OBLIGATOIRE si virement]",
119
+ },
120
+
121
+ # [BLOC OBLIGATOIRE]
122
+ vat_breakdown: [
123
+ {
124
+ vat_category: "Catégorie TVA (BT-118) [OBLIGATOIRE]",
125
+ vat_rate: "Taux TVA % (BT-119) [OBLIGATOIRE]",
126
+ taxable_amount: "Base HT pour ce taux (BT-116) [OBLIGATOIRE]",
127
+ tax_amount: "Montant TVA pour ce taux (BT-117) [OBLIGATOIRE]",
128
+ # exemption_reason OU exemption_reason_code [OBLIGATOIRE] si vat_category = "E" (Exempt)
129
+ exemption_reason: "Motif d'exonération de la TVA (BT-120)",
130
+ exemption_reason_code: "Code de motif d'exonération de la TVA (BT-121)"
131
+ }
132
+ ],
133
+
134
+ # [BLOC OPTIONNEL]
135
+ discount: [ # Ce bloc est un tableau avec un item par taux de TVA d'item. Il doit donc avoir la même longueur que le bloc vat_breakdown
136
+ {
137
+ vat_category: "Catégorie TVA (BT-118) [OBLIGATOIRE si le bloc est présent]",
138
+ vat_rate: "Taux TVA % (BT-119) [OBLIGATOIRE si le bloc est présent]",
139
+ total_amount: "Montant total de la remise pour le taux de TVA [OBLIGATOIRE si percentage présent]",
140
+ percentage: "% de remise au niveau du document si la remise est en % (BT-94) [OPTIONNEL]",
141
+ # reason OU reason_code [OBLIGATOIRE] si bloc présent
142
+ reason: "Motif de la remise au niveau du document (BT-97)",
143
+ reason_code: "Code de motif de la remise au niveau du document (BT-98)",
144
+ }
145
+ ],
146
+
147
+ # [BLOC OBLIGATOIRE]
148
+ totals: {
149
+ line_total_ht: "Total HT lignes (BT-106) [OBLIGATOIRE]",
150
+ total_discount: "Somme des remises au niveau du document (BT-107) [OBLIGATOIRE si bloc discount présent]",
151
+ tax_basis_total_ht: "Total bases taxables (BT-109) [OBLIGATOIRE]",
152
+ tax_total: "Total TVA (BT-110) [OBLIGATOIRE]",
153
+ grand_total_ttc: "Total TTC (BT-112) [OBLIGATOIRE]",
154
+ amount_due: "Montant à payer (BT-115) [OPTIONNEL]",
155
+ # due_date OU description [OBLIGATOIRE] si amount_due est défini et positif
156
+ due_date: "Date due du paiement format YYYYMMDD (BT-9)",
157
+ description: "Termes du paiement (BT-20)"
158
+ }
159
+ }
160
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,106 @@
1
+ module UnovaFacturX
2
+ class FacturXGenerator
3
+ def initialize(pdf:, xml:)
4
+ @pdf = pdf
5
+ @xml = xml
6
+ end
7
+
8
+ def call
9
+ # Override règles de base HexaPDF pour pas qu'il force la version en 2.0
10
+ begin
11
+ ::HexaPDF::Type::Catalog.send(:remove_field, :AF)
12
+ rescue StandardError
13
+ nil
14
+ end
15
+ ::HexaPDF::Type::Catalog.define_field :AF, type: ::HexaPDF::PDFArray
16
+
17
+ # Génération du XML grace au services (hash + xml)
18
+ xml_doc = @xml
19
+ xml_string = xml_doc.to_xml
20
+ xml_io = StringIO.new(xml_string)
21
+
22
+ # Transformation du PDF en StringIO
23
+ pdf_io = @pdf.is_a?(File) ? @pdf : StringIO.new(@pdf)
24
+ pdf_io.rewind
25
+
26
+ # Création d'un nouveau PDF avec HexaPDF
27
+ doc = ::HexaPDF::Document.new(io: pdf_io)
28
+ doc.task(:pdfa, level: "3b") # PDF format A/3
29
+
30
+ # Ajout du XML au PDF
31
+ file_spec = doc.files.add(
32
+ xml_io,
33
+ name: "factur-x.xml",
34
+ description: 'Factur-X XML',
35
+ mime_type: 'text/xml',
36
+ )
37
+ file_spec[:AFRelationship] = :Alternative
38
+
39
+ doc.catalog[:AF] ||= []
40
+ doc.catalog[:AF] << file_spec
41
+
42
+ # Réécriture des Metadata pour correspondre à Factur-X
43
+ doc.metadata.custom_metadata(metadata_xml)
44
+
45
+ # Renvoi du PDF en StringIO
46
+ doc.write_to_string
47
+ end
48
+
49
+ private
50
+
51
+ def metadata_xml # TODO : Utiliser les bonnes valeurs
52
+ <<~XML
53
+ <rdf:Description rdf:about=""
54
+ xmlns:pdfaExtension="http://www.aiim.org/pdfa/ns/extension/"
55
+ xmlns:pdfaSchema="http://www.aiim.org/pdfa/ns/schema#"
56
+ xmlns:pdfaProperty="http://www.aiim.org/pdfa/ns/property#">
57
+ <pdfaExtension:schemas>
58
+ <rdf:Bag>
59
+ <rdf:li rdf:parseType="Resource">
60
+ <pdfaSchema:schema>Factur-X PDFA Extension Schema</pdfaSchema:schema>
61
+ <pdfaSchema:namespaceURI>urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#</pdfaSchema:namespaceURI>
62
+ <pdfaSchema:prefix>fx</pdfaSchema:prefix>
63
+ <pdfaSchema:property>
64
+ <rdf:Seq>
65
+ <rdf:li rdf:parseType="Resource">
66
+ <pdfaProperty:name>DocumentType</pdfaProperty:name>
67
+ <pdfaProperty:valueType>Text</pdfaProperty:valueType>
68
+ <pdfaProperty:category>external</pdfaProperty:category>
69
+ <pdfaProperty:description>The type of the document</pdfaProperty:description>
70
+ </rdf:li>
71
+ <rdf:li rdf:parseType="Resource">
72
+ <pdfaProperty:name>DocumentFileName</pdfaProperty:name>
73
+ <pdfaProperty:valueType>Text</pdfaProperty:valueType>
74
+ <pdfaProperty:category>external</pdfaProperty:category>
75
+ <pdfaProperty:description>The name of the embedded XML file</pdfaProperty:description>
76
+ </rdf:li>
77
+ <rdf:li rdf:parseType="Resource">
78
+ <pdfaProperty:name>Version</pdfaProperty:name>
79
+ <pdfaProperty:valueType>Text</pdfaProperty:valueType>
80
+ <pdfaProperty:category>external</pdfaProperty:category>
81
+ <pdfaProperty:description>The version of the Factur-X specification</pdfaProperty:description>
82
+ </rdf:li>
83
+ <rdf:li rdf:parseType="Resource">
84
+ <pdfaProperty:name>ConformanceLevel</pdfaProperty:name>
85
+ <pdfaProperty:valueType>Text</pdfaProperty:valueType>
86
+ <pdfaProperty:category>external</pdfaProperty:category>
87
+ <pdfaProperty:description>The conformance level of the Factur-X data</pdfaProperty:description>
88
+ </rdf:li>
89
+ </rdf:Seq>
90
+ </pdfaSchema:property>
91
+ </rdf:li>
92
+ </rdf:Bag>
93
+ </pdfaExtension:schemas>
94
+ </rdf:Description>
95
+
96
+ <rdf:Description rdf:about=""
97
+ xmlns:fx="urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#">
98
+ <fx:DocumentType>INVOICE</fx:DocumentType>
99
+ <fx:DocumentFileName>factur-x.xml</fx:DocumentFileName>
100
+ <fx:Version>1.0</fx:Version>
101
+ <fx:ConformanceLevel>EN 16931</fx:ConformanceLevel>
102
+ </rdf:Description>
103
+ XML
104
+ end
105
+ end
106
+ end