einvoice 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/einvoice.gemspec +2 -0
  3. data/lib/einvoice/client.rb +13 -0
  4. data/lib/einvoice/configuration.rb +1 -1
  5. data/lib/einvoice/connection.rb +7 -2
  6. data/lib/einvoice/neweb/model/base.rb +39 -0
  7. data/lib/einvoice/neweb/model/contact.rb +21 -0
  8. data/lib/einvoice/neweb/model/customer_defined.rb +19 -0
  9. data/lib/einvoice/neweb/model/invoice.rb +62 -0
  10. data/lib/einvoice/neweb/model/invoice_item.rb +27 -0
  11. data/lib/einvoice/neweb/provider.rb +35 -0
  12. data/lib/einvoice/neweb/validator/carrier_id1_validator.rb +13 -0
  13. data/lib/einvoice/neweb/validator/carrier_id2_validator.rb +13 -0
  14. data/lib/einvoice/neweb/validator/customs_clearance_mark_validator.rb +13 -0
  15. data/lib/einvoice/neweb/validator/print_mark_validator.rb +13 -0
  16. data/lib/einvoice/{api.rb → provider.rb} +2 -1
  17. data/lib/einvoice/response.rb +39 -0
  18. data/lib/einvoice/result.rb +17 -0
  19. data/lib/einvoice/utils.rb +23 -0
  20. data/lib/einvoice/version.rb +1 -1
  21. data/lib/einvoice.rb +9 -8
  22. data/lib/faraday/request/digest_neweb.rb +5 -3
  23. metadata +45 -14
  24. data/lib/einvoice/client/neweb.rb +0 -20
  25. data/lib/einvoice/model/base.rb +0 -16
  26. data/lib/einvoice/model/contact.rb +0 -18
  27. data/lib/einvoice/model/customer_defined.rb +0 -17
  28. data/lib/einvoice/model/invoice.rb +0 -54
  29. data/lib/einvoice/model/invoice_item.rb +0 -22
  30. data/lib/einvoice/validator/carrier_id1_validator.rb +0 -7
  31. data/lib/einvoice/validator/carrier_id2_validator.rb +0 -7
  32. data/lib/einvoice/validator/customs_clearance_mark_validator.rb +0 -7
  33. data/lib/einvoice/validator/print_mark_validator.rb +0 -7
  34. data/lib/faraday/request/encode_xml.rb +0 -52
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fbc9820ea32a3c3e23284e5b25de73215a5d660c
4
- data.tar.gz: a1a78e1b0f0d7438392dd1b1724eba9fa451701d
3
+ metadata.gz: d40d662bce05b0f05bff5b9b980a27abb5a1bb47
4
+ data.tar.gz: 43edf9033179b4475711025ba4a240e8fe2b116f
5
5
  SHA512:
6
- metadata.gz: 48f85fd5e4a2437bf88e951f62af15e8bd24e7b1aabe86e88efec919d3334424bb78566d38d7a77151fadc619d08ff0669477d2769bd1739836e3da6075fbdfa
7
- data.tar.gz: cac3cc64d46ebe7d3a879cc9603acb6d56a4f09bffb268083899b8a99971775427784f64c449a44e700c4f8465b0b33ebbe345cc1c11f5b0e4bbde1839b733c1
6
+ metadata.gz: 80e76b8fd5a68e4c8c28547751d1f67a2c372b07912bba3ec2768143b73988a88024d17b30e3c9b2d41ed6d383542eb56d36b83ed395e9c49c6706917f79bbad
7
+ data.tar.gz: 283626dbf581277a1993eef18c6ffad7e666901219df18cd4cb4b031a93ecbb2b8087a48fe1f1488dccae634f254d9ec7b98f25f05f1384c65d04e74eaff0cb1
data/einvoice.gemspec CHANGED
@@ -23,10 +23,12 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency "faraday_middleware"
24
24
  spec.add_dependency "multi_xml"
25
25
  spec.add_dependency "activemodel"
26
+ spec.add_dependency "activesupport"
26
27
  spec.add_dependency "gyoku"
27
28
 
28
29
  spec.add_development_dependency "bundler", "~> 1.11"
29
30
  spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "pry"
30
32
  spec.add_development_dependency "rspec"
31
33
  spec.add_development_dependency "factory_girl"
32
34
  spec.add_development_dependency "shoulda-matchers"
@@ -0,0 +1,13 @@
1
+ module Einvoice
2
+ class Client
3
+ attr_accessor :provider
4
+
5
+ def initialize(provider)
6
+ @provider = provider
7
+ end
8
+
9
+ def issue(payload)
10
+ provider.issue(payload)
11
+ end
12
+ end
13
+ end
@@ -12,7 +12,7 @@ module Einvoice
12
12
  DEFAULT_CLIENT_ID = nil
13
13
  DEFAULT_CLIENT_SECRET = nil
14
14
  DEFAULT_ENDPOINT = "".freeze
15
- DEFAULT_FORMAT = :xml
15
+ DEFAULT_FORMAT = ""
16
16
 
17
17
  attr_accessor *VALID_OPTIONS_KEYS
18
18
 
@@ -1,4 +1,5 @@
1
1
  require "faraday_middleware"
2
+ require "faraday/request/digest_neweb"
2
3
 
3
4
  module Einvoice
4
5
  module Connection
@@ -11,9 +12,13 @@ module Einvoice
11
12
  }
12
13
 
13
14
  ::Faraday::Connection.new(options) do |connection|
15
+ connection.request :digest, client_secret
16
+ connection.request :url_encoded
17
+
18
+ # Parser
14
19
  case format.to_s.downcase
15
- when "xml" then connection.use Faraday::Response::ParseXml
16
- when "json" then connection.use Faraday::Response::ParseJson
20
+ when "xml" then connection.response :xml
21
+ when "json" then connection.response :json
17
22
  end
18
23
 
19
24
  connection.adapter Faraday.default_adapter
@@ -0,0 +1,39 @@
1
+ require "active_model"
2
+
3
+ require "einvoice/neweb/validator/carrier_id1_validator"
4
+ require "einvoice/neweb/validator/carrier_id2_validator"
5
+ require "einvoice/neweb/validator/customs_clearance_mark_validator"
6
+ require "einvoice/neweb/validator/print_mark_validator"
7
+
8
+ module Einvoice
9
+ module Neweb
10
+ module Model
11
+ class Base
12
+ include ActiveModel::Model
13
+ include ActiveModel::Validations
14
+ include ActiveModel::Serialization
15
+ include ActiveModel::Serializers::JSON
16
+
17
+ def attributes=(hash)
18
+ @invoice_item ||= []
19
+ hash.each do |key, value|
20
+ case key.to_sym
21
+ when :invoice_item
22
+ value.each { |v| @invoice_item << InvoiceItem.new(v) }
23
+ when :contact
24
+ @contact = Contact.new(value)
25
+ when :customer_defined
26
+ @customer_defined = CustomerDefined.new(value)
27
+ else
28
+ send("#{key}=", value)
29
+ end
30
+ end
31
+ end
32
+
33
+ def attributes
34
+ instance_values
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ module Einvoice
2
+ module Neweb
3
+ module Model
4
+ class Contact < Base
5
+ VALID_OPTIONS_KEYS = [
6
+ :name,
7
+ :address,
8
+ :t_e_l,
9
+ :email
10
+ ].freeze
11
+
12
+ attr_accessor *VALID_OPTIONS_KEYS
13
+
14
+ validates :name, presence: true, length: { maximum: 64 }
15
+ validates :address, presence: true, length: { maximum: 128 }
16
+ validates :t_e_l, length: { maximum: 64 }
17
+ validates :email, length: { maximum: 512 }
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module Einvoice
2
+ module Neweb
3
+ module Model
4
+ class CustomerDefined < Base
5
+ VALID_OPTIONS_KEYS = [
6
+ :project_no,
7
+ :purchase_no,
8
+ :stamp_duty_flag
9
+ ].freeze
10
+
11
+ attr_accessor *VALID_OPTIONS_KEYS
12
+
13
+ validates :project_no, length: { maximum: 64 }
14
+ validates :purchase_no, length: { maximum: 64 }
15
+ validates :stamp_duty_flag, length: { maximum: 1 }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,62 @@
1
+ module Einvoice
2
+ module Neweb
3
+ module Model
4
+ class Invoice < Base
5
+ include Einvoice::Validator::Neweb
6
+
7
+ VALID_OPTIONS_KEYS = [
8
+ :data_number,
9
+ :data_date,
10
+ :seller_id,
11
+ :buyer_name,
12
+ :buyer_id,
13
+ :customs_clearance_mark,
14
+ :invoice_type,
15
+ :donate_mark,
16
+ :carrier_type,
17
+ :carrier_id1,
18
+ :carrier_id2,
19
+ :print_mark,
20
+ :n_p_o_b_a_n,
21
+ :random_number,
22
+ :invoice_item,
23
+ :sales_amount,
24
+ :free_tax_sales_amount,
25
+ :zero_tax_sales_amount,
26
+ :tax_type,
27
+ :tax_rate,
28
+ :tax_amount,
29
+ :total_amount,
30
+ :contact,
31
+ :customer_defined
32
+ ].freeze
33
+
34
+ attr_accessor *VALID_OPTIONS_KEYS
35
+
36
+ validates :data_number, presence: true, length: { maximum: 20 }
37
+ validates :data_date, presence: true, length: { maximum: 10 }, format: { with: /\A\d{4}\/\d{2}\/\d{2}\z/ }
38
+ validates :seller_id, presence: true, length: { maximum: 10 }
39
+ validates :buyer_name, presence: true, length: { maximum: 60 }
40
+ validates :buyer_id, presence: true, length: { maximum: 10 }
41
+ validates :customs_clearance_mark, length: { maximum: 1 }, format: { with: /[01]/ }, customs_clearance_mark: true, allow_blank: true
42
+ validates :invoice_type, presence: true, length: { maximum: 2 }, format: { with: /07|08/ }
43
+ validates :donate_mark, presence: true, length: { maximum: 1 }, format: { with: /[01]/ }
44
+ validates :carrier_type, length: { maximum: 6 }, format: { with: /[a-zA-Z]{2}\d{4}/ }, allow_blank: true
45
+ validates :carrier_id1, length: { maximum: 64 }, carrier_id1: true, allow_blank: true
46
+ validates :carrier_id2, length: { maximum: 64 }, carrier_id2: true, allow_blank: true
47
+ validates :print_mark, presence: true, length: { maximum: 1 }, format: { with: /[YN]/ }, print_mark: true
48
+ validates :n_p_o_b_a_n, length: { maximum: 10 }
49
+ validates :random_number, length: { maximum: 4 }
50
+ validates :invoice_item, presence: true
51
+ validates :sales_amount, presence: true, length: { maximum: 12 }, numericality: { greater_than_or_equal_to: 0 }
52
+ validates :free_tax_sales_amount, presence: true, length: { maximum: 12 }, numericality: { greater_than_or_equal_to: 0 }
53
+ validates :zero_tax_sales_amount, presence: true, length: { maximum: 12 }, numericality: { greater_than_or_equal_to: 0 }
54
+ validates :tax_type, presence: true, length: { maximum: 1 }
55
+ validates :tax_rate, presence: true, length: { maximum: 6 }
56
+ validates :tax_amount, presence: true, length: { maximum: 12 }, numericality: { greater_than_or_equal_to: 0 }
57
+ validates :total_amount, presence: true, length: { maximum: 12 }, numericality: { greater_than_or_equal_to: 0 }
58
+ validates :contact, presence: true
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,27 @@
1
+ module Einvoice
2
+ module Neweb
3
+ module Model
4
+ class InvoiceItem < Base
5
+ VALID_OPTIONS_KEYS = [
6
+ :description,
7
+ :quantity,
8
+ :unit,
9
+ :unit_price,
10
+ :amount,
11
+ :sequence_number,
12
+ :remark
13
+ ].freeze
14
+
15
+ attr_accessor *VALID_OPTIONS_KEYS
16
+
17
+ validates :description, presence: true, length: { maximum: 256 }
18
+ validates :quantity, presence: true, length: { maximum: 17 }
19
+ validates :unit, length: { maximum: 6 }
20
+ validates :unit_price, presence: true, length: { maximum: 17 }
21
+ validates :amount, presence: true, length: { maximum: 17 }
22
+ validates :sequence_number, presence: true, length: { maximum: 3 }
23
+ validates :remark, length: { maximum: 40 }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ require "einvoice/utils"
2
+
3
+ require "einvoice/neweb/model/base"
4
+ require "einvoice/neweb/model/contact"
5
+ require "einvoice/neweb/model/customer_defined"
6
+ require "einvoice/neweb/model/invoice_item"
7
+ require "einvoice/neweb/model/invoice"
8
+
9
+ module Einvoice
10
+ module Neweb
11
+ class Provider < Einvoice::Provider
12
+ include Einvoice::Utils
13
+
14
+ def issue(payload)
15
+ invoice = Einvoice::Neweb::Model::Invoice.new
16
+ invoice.from_json(payload.to_json)
17
+
18
+ if invoice.valid?
19
+ action = "IN_PreInvoiceS.action"
20
+ response = connection.post do |request|
21
+ request.url endpoint + action
22
+ request.body = {
23
+ storecode: client_id,
24
+ xmldata: encode_xml(wrap(serialize(invoice)))
25
+ }
26
+ end.body
27
+
28
+ Einvoice::Result.new(Einvoice::NewebResponse.new(response))
29
+ else
30
+ Einvoice::Result.new(Einvoice::NewebResponse.new(response))
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ module Einvoice
2
+ module Validator
3
+ module Neweb
4
+ class CarrierId1Validator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, value)
6
+ if (record.print_mark.to_s == "Y" && value.present?) || (record.print_mark.to_s == "N" && value.blank?)
7
+ record.errors.add attribute, options[:message] || :invalid
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Einvoice
2
+ module Validator
3
+ module Neweb
4
+ class CarrierId2Validator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, value)
6
+ if (record.print_mark.to_s == "Y" && value.present?) || (record.print_mark.to_s == "N" && value.blank?)
7
+ record.errors.add attribute, options[:message] || :invalid
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Einvoice
2
+ module Validator
3
+ module Neweb
4
+ class CustomsClearanceMarkValidator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, value)
6
+ if record.tax_rate.present? && record.tax_rate.to_i.zero? && value.blank?
7
+ record.errors.add attribute, options[:message] || :invalid
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Einvoice
2
+ module Validator
3
+ module Neweb
4
+ class PrintMarkValidator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, value)
6
+ if value == "Y" && (record.carrier_id1.present? || record.carrier_id2.present? || record.donate_mark == "Y")
7
+ record.errors.add attribute, options[:message] || :invalid
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,6 +1,7 @@
1
1
  module Einvoice
2
- class API
2
+ class Provider
3
3
  attr_accessor *Configuration::VALID_OPTIONS_KEYS
4
+ attr_accessor :provider
4
5
 
5
6
  def initialize(options={})
6
7
  options = Einvoice.options.merge(options)
@@ -0,0 +1,39 @@
1
+ module Einvoice
2
+ class Response
3
+ attr_reader :response
4
+
5
+ def initialize(response = nil)
6
+ @response = response
7
+ end
8
+
9
+ def errors
10
+ raise NotImplementedError, 'You must initialize one of Einvoice::Response subclasses then use it.'
11
+ end
12
+
13
+ def success?
14
+ raise NotImplementedError, 'You must initialize one of Einvoice::Response subclasses then use it.'
15
+ end
16
+ end
17
+
18
+ class NewebResponse < Response
19
+ def errors
20
+ if response.is_a? ActiveModel::Errors
21
+ response.full_messages.join('; ')
22
+ else
23
+ response && response.fetch("Result", {}) do |data|
24
+ "#{data["statcode"]}: #{data["statdesc"]}"
25
+ end
26
+ end
27
+ end
28
+
29
+ def success?
30
+ if response.is_a? ActiveModel::Errors
31
+ false
32
+ elsif response && response.fetch("Result", {})["statdesc"] == "0000"
33
+ true
34
+ else
35
+ false
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,17 @@
1
+ module Einvoice
2
+ class Result
3
+ attr_reader :response
4
+
5
+ def initialize(response = nil)
6
+ @response = response
7
+ end
8
+
9
+ def errors
10
+ response.errors unless success?
11
+ end
12
+
13
+ def success?
14
+ response.success?
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ require "gyoku"
2
+
3
+ module Einvoice
4
+ module Utils
5
+ def serialize(object)
6
+ object.serializable_hash(except: [:errors, :validation_context], include: [:invoice_item, :contact, :customer_defined])
7
+ end
8
+
9
+ def wrap(hash)
10
+ { invoice_root:
11
+ { invoice: hash }
12
+ }
13
+ end
14
+
15
+ def camelize(hash)
16
+ hash.deep_transform_keys { |k| k.to_s.camelize }
17
+ end
18
+
19
+ def encode_xml(hash)
20
+ ::Gyoku.xml(hash, key_converter: :none)
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Einvoice
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/einvoice.rb CHANGED
@@ -1,14 +1,15 @@
1
- require "einvoice/version"
1
+ require "active_support"
2
+
3
+ require "einvoice/client"
2
4
  require "einvoice/configuration"
3
5
  require "einvoice/connection"
4
- require "einvoice/api"
5
- require "einvoice/model/base"
6
+ require "einvoice/provider"
7
+ require "einvoice/response"
8
+ require "einvoice/result"
9
+ require "einvoice/utils"
10
+ require "einvoice/version"
6
11
 
7
- require "einvoice/client/neweb"
8
- require "einvoice/model/invoice"
9
- require "einvoice/model/invoice_item"
10
- require "einvoice/model/contact"
11
- require "einvoice/model/customer_defined"
12
+ require "einvoice/neweb/provider"
12
13
 
13
14
  module Einvoice
14
15
  extend Configuration
@@ -1,7 +1,7 @@
1
1
  require 'faraday'
2
2
 
3
- module FaradayMiddleware
4
- class DigestNeweb < Faraday::Middleware
3
+ module Faraday
4
+ class Request::DigestNeweb < Faraday::Middleware
5
5
  dependency do
6
6
  require 'digest' unless defined?(::Digest)
7
7
  end
@@ -13,8 +13,10 @@ module FaradayMiddleware
13
13
 
14
14
  def call(env)
15
15
  xmldata = env[:body][:xmldata]
16
- env[:body][:hash] = Digest::MD5.digest([xmldata, @secret])
16
+ env[:body][:hash] = Digest::MD5.hexdigest(xmldata + @secret)
17
17
  @app.call env
18
18
  end
19
19
  end
20
20
  end
21
+
22
+ Faraday::Request.register_middleware digest: Faraday::Request::DigestNeweb
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: einvoice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Yun
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-04 00:00:00.000000000 Z
11
+ date: 2016-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: gyoku
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +122,20 @@ dependencies:
108
122
  - - "~>"
109
123
  - !ruby/object:Gem::Version
110
124
  version: '10.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
111
139
  - !ruby/object:Gem::Dependency
112
140
  name: rspec
113
141
  requirement: !ruby/object:Gem::Requirement
@@ -168,22 +196,25 @@ files:
168
196
  - bin/setup
169
197
  - einvoice.gemspec
170
198
  - lib/einvoice.rb
171
- - lib/einvoice/api.rb
172
- - lib/einvoice/client/neweb.rb
199
+ - lib/einvoice/client.rb
173
200
  - lib/einvoice/configuration.rb
174
201
  - lib/einvoice/connection.rb
175
- - lib/einvoice/model/base.rb
176
- - lib/einvoice/model/contact.rb
177
- - lib/einvoice/model/customer_defined.rb
178
- - lib/einvoice/model/invoice.rb
179
- - lib/einvoice/model/invoice_item.rb
180
- - lib/einvoice/validator/carrier_id1_validator.rb
181
- - lib/einvoice/validator/carrier_id2_validator.rb
182
- - lib/einvoice/validator/customs_clearance_mark_validator.rb
183
- - lib/einvoice/validator/print_mark_validator.rb
202
+ - lib/einvoice/neweb/model/base.rb
203
+ - lib/einvoice/neweb/model/contact.rb
204
+ - lib/einvoice/neweb/model/customer_defined.rb
205
+ - lib/einvoice/neweb/model/invoice.rb
206
+ - lib/einvoice/neweb/model/invoice_item.rb
207
+ - lib/einvoice/neweb/provider.rb
208
+ - lib/einvoice/neweb/validator/carrier_id1_validator.rb
209
+ - lib/einvoice/neweb/validator/carrier_id2_validator.rb
210
+ - lib/einvoice/neweb/validator/customs_clearance_mark_validator.rb
211
+ - lib/einvoice/neweb/validator/print_mark_validator.rb
212
+ - lib/einvoice/provider.rb
213
+ - lib/einvoice/response.rb
214
+ - lib/einvoice/result.rb
215
+ - lib/einvoice/utils.rb
184
216
  - lib/einvoice/version.rb
185
217
  - lib/faraday/request/digest_neweb.rb
186
- - lib/faraday/request/encode_xml.rb
187
218
  homepage: https://github.com/abookyun/einvoice
188
219
  licenses:
189
220
  - MIT
@@ -1,20 +0,0 @@
1
- module Einvoice
2
- module Client
3
- class Neweb < API
4
- def initialize
5
- connection.use Faraday::Request::UrlEncoded
6
- connection.use Faraday::Request::DigestNeweb :neweb, client_secret
7
- end
8
-
9
- def create_invoice(invoice)
10
- connection.post do |request|
11
- request.url "IN_PreInvoiceS.action"
12
- request.body = {
13
- StoreCode: client_id,
14
- xmldata: invoice.to_json
15
- }
16
- end.body
17
- end
18
- end
19
- end
20
- end
@@ -1,16 +0,0 @@
1
- require "active_model"
2
-
3
- require "einvoice/validator/carrier_id1_validator"
4
- require "einvoice/validator/carrier_id2_validator"
5
- require "einvoice/validator/customs_clearance_mark_validator"
6
- require "einvoice/validator/print_mark_validator"
7
-
8
- module Einvoice
9
- module Model
10
- class Base
11
- include ActiveModel::Model
12
- include ActiveModel::Validations
13
- include ActiveModel::Serialization
14
- end
15
- end
16
- end
@@ -1,18 +0,0 @@
1
- module Einvoice
2
- module Model
3
- class Contact < Einvoice::Model::Base
4
- attr_accessor :name, :address, :t_e_l, :email
5
-
6
- validates :name, presence: true, length: { maximum: 64 }
7
- validates :address, presence: true, length: { maximum: 128 }
8
- validates :t_e_l, length: { maximum: 64 }
9
- validates :email, length: { maximum: 512 }
10
-
11
- def initialize(attributes={})
12
- attributes.each do |attribute, value|
13
- instance_variable_set("@#{attribute}", value) if respond_to?(attribute.to_sym)
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,17 +0,0 @@
1
- module Einvoice
2
- module Model
3
- class CustomerDefined < Einvoice::Model::Base
4
- attr_accessor :project_no, :purchase_no, :stamp_duty_flag
5
-
6
- validates :project_no, length: { maximum: 64 }
7
- validates :purchase_no, length: { maximum: 64 }
8
- validates :stamp_duty_flag, length: { maximum: 1 }
9
-
10
- def initialize(attributes={})
11
- attributes.each do |attribute, value|
12
- instance_variable_set("@#{attribute}", value) if respond_to?(attribute.to_sym)
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,54 +0,0 @@
1
- module Einvoice
2
- module Model
3
- class Invoice < Einvoice::Model::Base
4
- attr_accessor :data_number, :data_date, :seller_id, :buyer_name,
5
- :buyer_id, :customs_clearance_mark, :invoice_type,
6
- :donate_mark, :carrier_type, :carrier_id1, :carrier_id2,
7
- :print_mark, :n_p_o_b_a_n, :random_number, :invoice_items,
8
- :sales_amount, :free_tax_sales_amount,
9
- :zero_tax_sales_amount, :tax_type, :tax_rate, :tax_amount,
10
- :total_amount, :contact, :customer_defined
11
-
12
- validates :data_number, presence: true, length: { maximum: 20 }
13
- validates :data_date, presence: true, length: { maximum: 10 }, format: { with: /\A\d{4}\/\d{2}\/\d{2}\z/ }
14
- validates :seller_id, presence: true, length: { maximum: 10 }
15
- validates :buyer_name, presence: true, length: { maximum: 60 }
16
- validates :buyer_id, presence: true, length: { maximum: 10 }
17
- validates :customs_clearance_mark, length: { maximum: 1 }, format: { with: /[01]/ }, customs_clearance_mark: true
18
- validates :invoice_type, presence: true, length: { maximum: 2 }, format: { with: /07|08/ }
19
- validates :donate_mark, presence: true, length: { maximum: 1 }, format: { with: /[01]/ }
20
- validates :carrier_type, length: { maximum: 6 }, format: { with: /[a-zA-Z]{2}\d{4}/ }, allow_blank: true
21
- validates :carrier_id1, length: { maximum: 64 }, carrier_id1: true
22
- validates :carrier_id2, length: { maximum: 64 }, carrier_id2: true
23
- validates :print_mark, presence: true, length: { maximum: 1 }, format: { with: /[YN]/ }, print_mark: true
24
- validates :n_p_o_b_a_n, length: { maximum: 10 }
25
- validates :random_number, length: { maximum: 4 }
26
- validates :invoice_items, presence: true
27
- validates :sales_amount, presence: true, length: { maximum: 12 }, numericality: { greater_than_or_equal_to: 0 }
28
- validates :free_tax_sales_amount, presence: true, length: { maximum: 12 }, numericality: { greater_than_or_equal_to: 0 }
29
- validates :zero_tax_sales_amount, presence: true, length: { maximum: 12 }, numericality: { greater_than_or_equal_to: 0 }
30
- validates :tax_type, presence: true, length: { maximum: 1 }
31
- validates :tax_rate, presence: true, length: { maximum: 6 }
32
- validates :tax_amount, presence: true, length: { maximum: 12 }, numericality: { greater_than_or_equal_to: 0 }
33
- validates :total_amount, presence: true, length: { maximum: 12 }, numericality: { greater_than_or_equal_to: 0 }
34
- validates :contact, presence: true
35
- validates :customer_defined, presence: true
36
-
37
- def initialize(attributes={})
38
- invoice_items = []
39
- attributes.each do |attribute, value|
40
- case attribute.to_sym
41
- when :invoice_item
42
- invoice_items << InvoiceItem.new(value)
43
- when :contact
44
- contact = Contact.new(value)
45
- when :customer_defined
46
- customer_defined = CustomerDefined.new(value)
47
- else
48
- instance_variable_set("@#{attribute}", value) if respond_to?(attribute.to_sym)
49
- end
50
- end
51
- end
52
- end
53
- end
54
- end
@@ -1,22 +0,0 @@
1
- module Einvoice
2
- module Model
3
- class InvoiceItem < Einvoice::Model::Base
4
- attr_accessor :description, :quantity, :unit, :unit_price, :amount,
5
- :sequence_number, :remark
6
-
7
- validates :description, presence: true, length: { maximum: 256 }
8
- validates :quantity, presence: true, length: { maximum: 17 }
9
- validates :unit, length: { maximum: 6 }
10
- validates :unit_price, presence: true, length: { maximum: 17 }
11
- validates :amount, presence: true, length: { maximum: 17 }
12
- validates :sequence_number, presence: true, length: { maximum: 3 }
13
- validates :remark, length: { maximum: 40 }
14
-
15
- def initialize(attributes={})
16
- attributes.each do |attribute, value|
17
- instance_variable_set("@#{attribute}", value) if respond_to?(attribute.to_sym)
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,7 +0,0 @@
1
- class CarrierId1Validator < ActiveModel::EachValidator
2
- def validate_each(record, attribute, value)
3
- if (record.print_mark.to_s == "Y" && value.present?) || (record.print_mark.to_s == "N" && value.blank?)
4
- record.errors.add attribute, options[:message] || :invalid
5
- end
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- class CarrierId2Validator < ActiveModel::EachValidator
2
- def validate_each(record, attribute, value)
3
- if (record.print_mark.to_s == "Y" && value.present?) || (record.print_mark.to_s == "N" && value.blank?)
4
- record.errors.add attribute, options[:message] || :invalid
5
- end
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- class CustomsClearanceMarkValidator < ActiveModel::EachValidator
2
- def validate_each(record, attribute, value)
3
- if record.tax_rate.present? && record.tax_rate.to_i.zero? && value.blank?
4
- record.errors.add attribute, options[:message] || :invalid
5
- end
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- class PrintMarkValidator < ActiveModel::EachValidator
2
- def validate_each(record, attribute, value)
3
- if value == "Y" && (record.carrier_id1.present? || record.carrier_id2.present? || record.donate_mark == "Y")
4
- record.errors.add attribute, options[:message] || :invalid
5
- end
6
- end
7
- end
@@ -1,52 +0,0 @@
1
- require 'faraday'
2
-
3
- module FaradayMiddleware
4
- # Request middleware that encodes the body as XML.
5
- #
6
- # Processes only requests with matching Content-type or those without a type.
7
- # If a request doesn't have a type but has a body, it sets the Content-type
8
- # to XML MIME-type.
9
- #
10
- # Doesn't try to encode bodies that already are in string form.
11
- class EncodeXml < Faraday::Middleware
12
- CONTENT_TYPE = 'Content-Type'.freeze
13
- MIME_TYPE = 'application/xml'.freeze
14
-
15
- dependency do
16
- require 'gyoku' unless defined?(::Gyoku)
17
- end
18
-
19
- def call(env)
20
- match_content_type(env) do |data|
21
- env[:body] = encode data
22
- end
23
- @app.call env
24
- end
25
-
26
- def encode(data)
27
- ::Gyoku.xml(data, :key_converter => :none)
28
- end
29
-
30
- def match_content_type(env)
31
- if process_request?(env)
32
- env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
33
- yield env[:body] unless env[:body].respond_to?(:to_str)
34
- end
35
- end
36
-
37
- def process_request?(env)
38
- type = request_type(env)
39
- has_body?(env) and (type.empty? or type == MIME_TYPE)
40
- end
41
-
42
- def has_body?(env)
43
- body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
44
- end
45
-
46
- def request_type(env)
47
- type = env[:request_headers][CONTENT_TYPE].to_s
48
- type = type.split(';', 2).first if type.index(';')
49
- type
50
- end
51
- end
52
- end