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-2019 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?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

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

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

#destroyable_by_user?Boolean

Returns:

  • (Boolean)

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

def destroyable_by_user?
  !sale_item
end

#disabled?Boolean

Returns:

  • (Boolean)

183
184
185
# File 'app/models/subscription.rb', line 183

def disabled?
  past? || suspended
end

#following_attributesObject


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/subscription.rb', line 153

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)

187
188
189
# File 'app/models/subscription.rb', line 187

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

#past?Boolean

Returns:

  • (Boolean)

191
192
193
# File 'app/models/subscription.rb', line 191

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


149
150
151
# File 'app/models/subscription.rb', line 149

def renew!(attributes = {})
  Sale.create!(renew_attributes(attributes))
end

#renew_attributes(attributes = {}) ⇒ Object

Returns a hash to create a Sale with a SaleItem and a Subscription linked to current subscription


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/subscription.rb', line 128

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

#renewable?Boolean

Returns:

  • (Boolean)

122
123
124
# File 'app/models/subscription.rb', line 122

def renewable?
  sale_item && children.empty?
end

#subscriber_nameObject


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

def subscriber_name
  address.mail_line_1
end

#suspendObject


195
196
197
# File 'app/models/subscription.rb', line 195

def suspend
  update_attribute(:suspended, true)
end

#suspendable?Boolean

Returns:

  • (Boolean)

175
176
177
# File 'app/models/subscription.rb', line 175

def suspendable?
  !suspended && active?
end

#takeoverObject


199
200
201
# File 'app/models/subscription.rb', line 199

def takeover
  update_attribute(:suspended, false)
end