lucadeal 0.2.13 → 0.2.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a2f79f88f7a7047f07f4c2137888f43e29cec70129e0b8d79544d4c8137e2f4
4
- data.tar.gz: 8d3d2dbf4908f7e5851f64003eaf39fd455f34c1e8f51c07dff7685fa913e036
3
+ metadata.gz: e1b2a723a3fc191616f5a602a58428797afe69b19f9ee7e8d78d7dd187ae9b73
4
+ data.tar.gz: b45c547d86312b94b702c68b94999be576c4f14fb55258b7961aa218ff4f74ef
5
5
  SHA512:
6
- metadata.gz: feb4db73cf4f5d59a1caafb5e18c7e00460e9903244ba9a43ac1781b8a2ffd7b6465d95a309211e98e487b5b77daa5e1463c4647313f001b80f779c2e23354ad
7
- data.tar.gz: 6204831c6bb54534a3527c7280a5c761bfb422a52831d1dd4d26b484fc221ddc78a979d5b66e42263c1a6d76ebeb140fefcb54c63a203d8b292e6ce40e5e2e05
6
+ metadata.gz: 9daff536435c5bcbcc9f6694b4068df22f108d667283eb48754fdda02121f3724312ca0257d7029ec09f27034112af72123e734b2262c6f91c3b93f56a099b4e
7
+ data.tar.gz: bdd40230688a1080fee49472adc22e8a98e53988aa2822688efc9e8537a0095de7919ec6c2c4a810da8f82e315fa714b2507d1e417dc07140fa0f1f897a31b0e
data/README.md CHANGED
@@ -75,6 +75,8 @@ Fields for subscription customers are as bellows:
75
75
  | terms | | | | |
76
76
  | | billing_cycle | optional | | If 'monthly', invoices are generated on each month. |
77
77
  | | category | optional | | Default: 'subscription' |
78
+ | products | | | | Array of products. |
79
+ | | id | | | reference for Product |
78
80
  | items | | | | Array of items. |
79
81
  | | name | | x | Item name. |
80
82
  | | price | | x | Item price. |
@@ -115,6 +117,7 @@ Invoice is basically auto generated from Customer and Contract objects.
115
117
  | | price | Item price. |
116
118
  | | qty | quantity. Default: 1. |
117
119
  | | type | |
120
+ | | product_id | refrence for Product |
118
121
  | subtotal | | Array of subtotal by tax category. |
119
122
  | | items | amount of items |
120
123
  | | tax | amount of tax |
@@ -9,6 +9,7 @@ require 'luca_record'
9
9
  module LucaDeal
10
10
  class Contract < LucaRecord::Base
11
11
  @dirname = 'contracts'
12
+ @required = ['customer_id', 'terms']
12
13
 
13
14
  def initialize(date = nil)
14
15
  @date = date ? Date.parse(date) : Date.today
@@ -10,6 +10,7 @@ require 'luca_record'
10
10
  module LucaDeal
11
11
  class Customer < LucaRecord::Base
12
12
  @dirname = 'customers'
13
+ @required = ['name']
13
14
 
14
15
  def initialize(pjdir = nil)
15
16
  @date = Date.today
@@ -12,6 +12,7 @@ require 'luca_record'
12
12
  module LucaDeal
13
13
  class Invoice < LucaRecord::Base
14
14
  @dirname = 'invoices'
15
+ @required = ['issue_date', 'customer', 'items', 'subtotal']
15
16
 
16
17
  def initialize(date = nil)
17
18
  @date = issue_date(date)
@@ -113,16 +114,12 @@ module LucaDeal
113
114
  invoice['due_date'] = due_date(@date)
114
115
  invoice['issue_date'] = @date
115
116
  invoice['sales_fee'] = contract.dig('sales_fee')
116
- invoice['items'] = contract.dig('items').map do |item|
117
- next if item.dig('type') == 'initial' && subsequent_month?(contract.dig('terms', 'effective'))
118
-
119
- {}.tap do |h|
120
- h['name'] = item.dig('name')
121
- h['price'] = item.dig('price')
122
- h['qty'] = item.dig('qty') || 1
123
- h['type'] = item.dig('type') if item.dig('type')
124
- end
125
- end.compact
117
+ invoice['items'] = get_products(contract['products'])
118
+ .concat(contract['items']&.map { |i| i['qty'] ||= 1; i } || [])
119
+ .compact
120
+ invoice['items'].reject! do |item|
121
+ item.dig('type') == 'initial' && subsequent_month?(contract.dig('terms', 'effective'))
122
+ end
126
123
  invoice['subtotal'] = subtotal(invoice['items'])
127
124
  .map { |k, v| v.tap { |dat| dat['rate'] = k } }
128
125
  gen_invoice!(invoice)
@@ -153,6 +150,20 @@ module LucaDeal
153
150
  end
154
151
  end
155
152
 
153
+ def get_products(products)
154
+ return [] if products.nil?
155
+
156
+ [].tap do |res|
157
+ products.each do |product|
158
+ LucaDeal::Product.find(product['id'])['items'].each do |item|
159
+ item['product_id'] = product['id']
160
+ item['qty'] ||= 1
161
+ res << item
162
+ end
163
+ end
164
+ end
165
+ end
166
+
156
167
  def gen_invoice!(invoice)
157
168
  id = invoice.dig('contract_id')
158
169
  self.class.create_record!(invoice, @date, Array(id))
@@ -190,8 +201,9 @@ module LucaDeal
190
201
  {}.tap do |subtotal|
191
202
  items.each do |i|
192
203
  rate = i.dig('tax') || 'default'
204
+ qty = i['qty'] || 1
193
205
  subtotal[rate] = { 'items' => 0, 'tax' => 0 } if subtotal.dig(rate).nil?
194
- subtotal[rate]['items'] += i['qty'] * i['price']
206
+ subtotal[rate]['items'] += qty * i['price']
195
207
  end
196
208
  subtotal.each do |rate, amount|
197
209
  amount['tax'] = (amount['items'] * load_tax_rate(rate)).to_i
@@ -10,32 +10,40 @@ require 'luca_record'
10
10
  module LucaDeal
11
11
  class Product < LucaRecord::Base
12
12
  @dirname = 'products'
13
+ @required = ['items']
13
14
 
14
15
  def list_name
15
16
  list = self.class.all.map { |dat| parse_current(dat) }
16
17
  YAML.dump(list).tap { |l| puts l }
17
18
  end
18
19
 
20
+ # Save data with hash in Product format. Simple format is also available as bellows:
21
+ # {
22
+ # name: 'item_name(required)', price: 'item_price', qty: 'item_qty',
23
+ # initial: { name: 'item_name', price: 'item_price', qty: 'item_qty' }
24
+ # }
19
25
  def self.create(obj)
20
- raise ':name is required' if obj[:name].nil?
21
-
22
- items = [{
23
- 'name' => obj[:name],
24
- 'price' => obj[:price] || 0,
25
- 'qty' => obj[:qty] || 1
26
- }]
27
- if obj[:initial]
28
- items << {
29
- 'name' => obj.dig(:initial, :name),
30
- 'price' => obj.dig(:initial, :price) || 0,
31
- 'qty' => obj.dig(:initial, :qty) || 1,
32
- 'type' => 'initial'
26
+ if obj[:name].nil?
27
+ h = obj
28
+ else
29
+ items = [{
30
+ 'name' => obj[:name],
31
+ 'price' => obj[:price] || 0,
32
+ 'qty' => obj[:qty] || 1
33
+ }]
34
+ if obj[:initial]
35
+ items << {
36
+ 'name' => obj.dig(:initial, :name),
37
+ 'price' => obj.dig(:initial, :price) || 0,
38
+ 'qty' => obj.dig(:initial, :qty) || 1,
39
+ 'type' => 'initial'
40
+ }
41
+ end
42
+ h = {
43
+ 'name' => obj[:name],
44
+ 'items' => items
33
45
  }
34
46
  end
35
- h = {
36
- 'name' => obj[:name],
37
- 'items' => items
38
- }
39
47
  super(h)
40
48
  end
41
49
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LucaDeal
4
- VERSION = '0.2.13'
4
+ VERSION = '0.2.14'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lucadeal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.2.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuma Takahiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-05 00:00:00.000000000 Z
11
+ date: 2020-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lucarecord