Class: Delivery

Inherits:
Ekylibre::Record::Base show all
Includes:
Attachable, Customizable
Defined in:
app/models/delivery.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: deliveries

annotation              :text
created_at              :datetime         not null
creator_id              :integer
custom_fields           :jsonb
driver_id               :integer
id                      :integer          not null, primary key
lock_version            :integer          default(0), not null
mode                    :string
number                  :string
reference_number        :string
responsible_id          :integer
started_at              :datetime
state                   :string           not null
stopped_at              :datetime
transporter_id          :integer
transporter_purchase_id :integer
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

#all_parcels_almost_prepared?Boolean

Returns:

  • (Boolean)

166
167
168
# File 'app/models/delivery.rb', line 166

def all_parcels_almost_prepared?
  parcels.all? { |p| p.prepared? || p.in_preparation? }
end

#all_parcels_prepared?Boolean

Returns:

  • (Boolean)

170
171
172
# File 'app/models/delivery.rb', line 170

def all_parcels_prepared?
  parcels.all? { |p| p.prepared? || p.given? }
end

#available_parcelsObject


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

def available_parcels
  Parcel.where('(delivery_id = ?) OR ((delivery_id IS ?) AND (state != ?))', id, nil, :given).order(:number)
end

#available_shipmentsObject


121
122
123
# File 'app/models/delivery.rb', line 121

def available_shipments
  available_parcels.where(type: 'Shipment')
end

#checkObject


141
142
143
144
145
146
147
148
# File 'app/models/delivery.rb', line 141

def check
  return false unless can_check?
  parcels.find_each do |parcel|
    # parcel.prepare if parcel.can_prepare?
    parcel.check if parcel.can_check?
  end
  super
end

#finishObject


155
156
157
158
159
160
161
162
163
164
# File 'app/models/delivery.rb', line 155

def finish
  start if can_start?
  return false unless can_finish?
  update_column(:stopped_at, Time.zone.now)
  parcels.each do |parcel|
    parcel.check if parcel.in_preparation?
    parcel.give if parcel.prepared?
  end
  super
end

#human_delivery_modeObject


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

def human_delivery_mode
  mode.text
end

#orderObject


125
126
127
128
129
130
131
# File 'app/models/delivery.rb', line 125

def order
  return false unless can_order?
  parcels.each do |parcel|
    parcel.order if parcel.draft?
  end
  super
end

#prepareObject


133
134
135
136
137
138
139
# File 'app/models/delivery.rb', line 133

def prepare
  return false unless can_prepare?
  parcels.each do |parcel|
    parcel.prepare if parcel.ordered?
  end
  super
end

#startObject


150
151
152
153
# File 'app/models/delivery.rb', line 150

def start
  update_column(:started_at, Time.zone.now)
  super
end

#statusObject


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

def status
  draft? ? :stop : finished? ? :go : :caution
end