einvoicing-connect 0.2.0 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3d4d4580d842a03b65af1d21a25c888685ad7fa8f69e3ee2b8b537c51bab015
4
- data.tar.gz: bc35697fb4f88aa875b9460a106bffc2dc2578a57b0d8a19b32065a71bfb1c30
3
+ metadata.gz: c1d5afb37eec4ff165c07ca7f35ca30470385c3c9820fe012f69bdb2b663d347
4
+ data.tar.gz: 79882e661a1683484e4a5ffdcc53640916e70b0b5e997a85b0a58ee14e74dfb2
5
5
  SHA512:
6
- metadata.gz: '080192ef85f10fe22030b187abadcebcb44efc726c9438af82235d6e6e81a9df38c932ccbbf22bcc43941bad9165103c29620d8ef5f297bb545aa38a37ab90a7'
7
- data.tar.gz: eafe755d1d350e52efb9096170030a417e4db99d6dd420d0f773cbd8ace143482e561669bf2fe13791d47c910b307ab7229fb67862b4eb9cf713b8d1f3b39bc4
6
+ metadata.gz: c8f426765e61378bbbb5a8e7c7ea0860b93c9e73d0be38287b7d690731a095277ee62ed4d97570c4181f3086f6eae5ceacc1e706b15cbb470a1be779b524c71d
7
+ data.tar.gz: cec80f177749ff036fb921d011fa2f4b909647c1dfa29af808e49984a334cfe1ca4cb1ae0308eb20be6e0a0516212eb2bdb11f03d1f44b914e79ef94dcf34b9c
@@ -3,6 +3,7 @@
3
3
  require "net/http"
4
4
  require "json"
5
5
  require "uri"
6
+ require "cgi"
6
7
  require "securerandom"
7
8
 
8
9
  module Einvoicing
@@ -36,6 +37,46 @@ module Einvoicing
36
37
  get("/customer_invoices/#{id}")
37
38
  end
38
39
 
40
+ # Find a customer by the caller's own identifier.
41
+ #
42
+ # Pennylane does not match a customer from the invoice itself: a
43
+ # document imported without +customer_id+ lands as "incomplete" with
44
+ # customer nil, whatever SIRET or VAT number the Factur-X carries.
45
+ # +external_reference+ is the caller's key into Pennylane's own
46
+ # numbering, which is what makes resolution repeatable.
47
+ #
48
+ # @return [Hash, nil] the customer, or nil when none carries that reference
49
+ def customer_by_reference(reference)
50
+ filter = JSON.generate([ { field: "external_reference", operator: "eq", value: reference } ])
51
+ get("/customers?filter=#{CGI.escape(filter)}")["items"]&.first
52
+ end
53
+
54
+ # Create a company customer (an organisation, invoiced under its own name).
55
+ #
56
+ # @param attributes [Hash] at least +name+ and +billing_address+;
57
+ # +reg_no+ carries the SIRET, +external_reference+ the caller's key.
58
+ def create_company_customer(attributes)
59
+ post("/company_customers", attributes)
60
+ end
61
+
62
+ # Create an individual customer (a private person).
63
+ def create_individual_customer(attributes)
64
+ post("/individual_customers", attributes)
65
+ end
66
+
67
+ # The customer for +reference+, created from +attributes+ if Pennylane
68
+ # does not know it yet. Idempotent as long as the caller keeps the
69
+ # reference stable, which is the point of it.
70
+ #
71
+ # @param company [Boolean] an organisation rather than a private person
72
+ def find_or_create_customer(reference:, attributes:, company: true)
73
+ existing = customer_by_reference(reference)
74
+ return existing if existing
75
+
76
+ payload = attributes.merge(external_reference: reference)
77
+ company ? create_company_customer(payload) : create_individual_customer(payload)
78
+ end
79
+
39
80
  private
40
81
 
41
82
  def current_token
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Einvoicing
4
4
  module Connect
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: einvoicing-connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Le Ray