Class: LucaDeal::Contract
- Inherits:
-
LucaRecord::Base
- Object
- LucaRecord::Base
- LucaDeal::Contract
- Defined in:
- lib/luca_deal/contract.rb
Class Method Summary collapse
-
.asof(year, month, day) ⇒ Object
returns active contracts on specified date.
Instance Method Summary collapse
-
#active ⇒ Object
collect active contracts.
- #active_period?(dat) ⇒ Boolean
- #describe(id) ⇒ Object
- #generate!(customer_id, mode = 'subscription') ⇒ Object
-
#initialize(date = nil) ⇒ Contract
constructor
A new instance of Contract.
Constructor Details
#initialize(date = nil) ⇒ Contract
Returns a new instance of Contract.
14 15 16 17 |
# File 'lib/luca_deal/contract.rb', line 14 def initialize(date = nil) @date = date ? Date.parse(date) : Date.today @pjdir = Pathname(Dir.pwd) end |
Class Method Details
.asof(year, month, day) ⇒ Object
returns active contracts on specified date.
21 22 23 24 25 26 27 |
# File 'lib/luca_deal/contract.rb', line 21 def self.asof(year, month, day) return enum_for(:asof, year, month, day) unless block_given? new("#{year}-#{month}-#{day}").active do |contract| yield contract end end |
Instance Method Details
#active ⇒ Object
collect active contracts
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/luca_deal/contract.rb', line 32 def active return enum_for(:active) unless block_given? self.class.all do |data| next if !active_period?(data.dig('terms')) contract = parse_current(data) contract['items'] = contract['items']&.map { |item| parse_current(item) } # TODO: handle sales_fee rate change contract['rate'] = contract['rate'] yield contract.compact end end |
#active_period?(dat) ⇒ Boolean
71 72 73 74 75 76 77 78 |
# File 'lib/luca_deal/contract.rb', line 71 def active_period?(dat) unless dat['defunct'].nil? defunct = dat['defunct'].respond_to?(:year) ? dat['defunct'] : Date.parse(dat['defunct']) return false if @date > defunct end effective = dat['effective'].respond_to?(:year) ? dat['effective'] : Date.parse(dat['effective']) @date >= effective end |
#describe(id) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/luca_deal/contract.rb', line 46 def describe(id) contract = parse_current(self.class.find(id)) if contract['products'] contract['products'] = contract['products'].map do |product| Product.find(product['id']) end end readable(contract) end |
#generate!(customer_id, mode = 'subscription') ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/luca_deal/contract.rb', line 56 def generate!(customer_id, mode = 'subscription') Customer.find(customer_id) do |customer| current_customer = parse_current(customer) if mode == 'sales_fee' obj = salesfee_template else obj = monthly_template end obj.merge!({ 'customer_id' => current_customer['id'], 'customer_name' => current_customer['name'] }) obj['terms'] ||= {} obj['terms']['effective'] = @date self.class.create(obj) end end |