class LucaDeal::Contract

Public Class Methods

asof(year, month, day) { |contract| ... } click to toggle source

returns active contracts on specified date.

# 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
new(date = nil) click to toggle source
# File lib/luca_deal/contract.rb, line 14
def initialize(date = nil)
  @date = date ? Date.parse(date) : Date.today
  @pjdir = Pathname(Dir.pwd)
end

Public Instance Methods

active() { |compact| ... } click to toggle source

collect active contracts

# 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) click to toggle source
# 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) click to toggle source
# 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') click to toggle source
# 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

Private Instance Methods

monthly_template() click to toggle source
# File lib/luca_deal/contract.rb, line 82
def monthly_template
  {}.tap do |obj|
    obj['items'] = [{
                      'name' => '_ITEM_NAME_FOR_INVOICE_',
                      'qty' => 1,
                      'price' => 0
                    }]
    obj['terms'] = { 'billing_cycle' => 'monthly' }
  end
end
salesfee_template() click to toggle source
# File lib/luca_deal/contract.rb, line 93
def salesfee_template
  {}.tap do |obj|
    obj['rate'] = {
      'default' => '0.2',
      'initial' => '0.2'
    }
    obj['terms'] = { 'category' => 'sales_fee' }
  end
end