Class: Subscription

Inherits:
Ekylibre::Record::Base show all
Includes:
Customizable
Defined in:
app/models/subscription.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2016 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: subscriptions

address_id     :integer
created_at     :datetime         not null
creator_id     :integer
custom_fields  :jsonb
description    :text
id             :integer          not null, primary key
lock_version   :integer          default(0), not null
nature_id      :integer
number         :string
parent_id      :integer
quantity       :integer          not null
sale_item_id   :integer
started_on     :date             not null
stopped_on     :date             not null
subscriber_id  :integer
suspended      :boolean          default(FALSE), not null
swim_lane_uuid :uuid             not null
updated_at     :datetime         not null
updater_id     :integer

Instance Method Summary collapse

Methods included from Customizable

#custom_value, #set_custom_value, #validate_custom_fields

Methods inherited from Ekylibre::Record::Base

#already_updated?, attr_readonly_with_conditions, #check_if_destroyable?, #check_if_updateable?, columns_definition, complex_scopes, customizable?, #customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, human_attribute_name_with_id, nomenclature_reflections, #old_record, #others, refers_to, scope_with_registration, simple_scopes, #updateable?

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

114
115
116
117
# File 'app/models/subscription.rb', line 114

def active?(instant = nil)
  instant ||= Time.zone.today
  self.started_on <= instant && instant <= stopped_on
end

#disabled?Boolean

Returns:

  • (Boolean)

173
174
175
# File 'app/models/subscription.rb', line 173

def disabled?
  past? || suspended
end

#following_attributesObject


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/subscription.rb', line 143

def following_attributes
  attributes = {
    nature_id: nature_id,
    address_id: self.address_id,
    subscriber_id: subscriber_id
  }
  last_subscription = subscriber.last_subscription(self.nature)
  if last_subscription
    attributes[:parent_id] = last_subscription.id
    attributes[:started_on] = last_subscription.stopped_on + 1
  else
    attributes[:started_on] = Time.zone.today
  end
  product_nature = self.product_nature || last_subscription.product_nature
  attributes[:stopped_on] = if product_nature
                              product_nature.subscription_stopped_on(attributes[:started_on])
                            else
                              attributes[:started_on] + 1.year - 1.day
                            end
  attributes
end

#future?Boolean

Returns:

  • (Boolean)

177
178
179
# File 'app/models/subscription.rb', line 177

def future?
  self.started_on > Time.zone.today
end

#past?Boolean

Returns:

  • (Boolean)

181
182
183
# File 'app/models/subscription.rb', line 181

def past?
  stopped_on < Time.zone.today
end

#renew!(attributes = {}) ⇒ Object

Create a Sale, a SaleItem and a Subscription linked to current subscription Inspired by Sale#duplicate


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/subscription.rb', line 125

def renew!(attributes = {})
  hash = {
    client_id: sale.client_id,
    nature_id: sale.nature_id,
    letter_format: false
  }
  # Items
  attrs = [
    :variant_id, :quantity, :amount, :label, :pretax_amount, :annotation,
    :reduction_percentage, :tax_id, :unit_amount, :unit_pretax_amount
  ].each_with_object({}) do |field, h|
    h[field] = sale_item.send(field)
  end
  attrs[:subscription_attributes] = following_attributes
  hash[:items_attributes] = { '0' => attrs }
  Sale.create!(hash.with_indifferent_access.deep_merge(attributes))
end

#renewable?Boolean

Returns:

  • (Boolean)

119
120
121
# File 'app/models/subscription.rb', line 119

def renewable?
  sale_item && children.empty?
end

#subscriber_nameObject


110
111
112
# File 'app/models/subscription.rb', line 110

def subscriber_name
  address.mail_line_1
end

#suspendObject


185
186
187
# File 'app/models/subscription.rb', line 185

def suspend
  update_attribute(:suspended, true)
end

#suspendable?Boolean

Returns:

  • (Boolean)

165
166
167
# File 'app/models/subscription.rb', line 165

def suspendable?
  !suspended
end

#takeoverObject


189
190
191
# File 'app/models/subscription.rb', line 189

def takeover
  update_attribute(:suspended, false)
end