Class: Shipment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/shipment.rb

Overview

Schema Information

Table name: shipments

id                 :integer(4)      not null, primary key
order_id           :integer(4)
shipping_method_id :integer(4)      not null
address_id         :integer(4)      not null
tracking           :string(255)
number             :string(255)     not null
state              :string(255)     not null
shipped_at         :datetime
active             :boolean(1)      default(TRUE), not null
created_at         :datetime
updated_at         :datetime

Constant Summary

CHARACTERS_SEED =
20
NUMBER_SEED =
2002002002000

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (none) create_shipments_with_items(order)

called when creating the shipment >>>

  • Creates a shipment object

  • Adds all the order items to the shipment

  • transitions the shipment state to ready_to_ship

Parameters:

Returns:

  • (none)


96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/shipment.rb', line 96

def self.create_shipments_with_items(order)
  order.order_items.group_by(&:shipping_method_id).each do |shipping_method_id, order_items|
    shipment = Shipment.new(:shipping_method_id => shipping_method_id,
                            :address_id         => order.ship_address_id,
                            :order_id           => order.id
                            )
    order_items.each do |item|
      shipment.order_items.push(item)
    end
    shipment.prepare!
  end
end

+ (Shipment) find_by_number(num)

finds the Shipment from the shipments number. Is more optimal than the normal rails find by id

because if calculates the shipment's id which is indexed

Parameters:

  • represents (String)

    the shipment.number

Returns:



138
139
140
# File 'app/models/shipment.rb', line 138

def self.find_by_number(num)
  find(id_from_number(num))##  now we can search by id which should be much faster
end

+ (Shipment) find_fulfillment_shipment(id)

finds the Shipment in the admin area (includes to prevent N + 1 queries)

Parameters:

  • shipment.id (Integer)

Returns:



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

def self.find_fulfillment_shipment(id)
  includes([{:order => {:user => :shipping_addresses}} , :address ]).find(id)
end

+ (Integer) id_from_number(num)

determines the shipment id from the shipment.number

Parameters:

  • represents (String)

    the shipment.number

Returns:

  • (Integer)

    id of the shipment to find



129
130
131
# File 'app/models/shipment.rb', line 129

def self.id_from_number(num)
  num.to_i(CHARACTERS_SEED) - NUMBER_SEED
end

Instance Method Details

- (String) display_shipped_at(format = I18n.translate('time.formats.us_date'))

when the order has been shipped the inventory must be updated

Parameters:

  • format (Optional String) (defaults to: I18n.translate('time.formats.us_date'))

    of the date string returned

Returns:

  • (String)


85
86
87
# File 'app/models/shipment.rb', line 85

def display_shipped_at(format = I18n.translate('time.formats.us_date'))
  shipped_at ? shipped_at.strftime(format) : 'Not Shipped.'
end

- (Boolean) has_items?

determines if shipment has order items

Parameters:

  • (none)

Returns:

  • (Boolean)


60
61
62
# File 'app/models/shipment.rb', line 60

def has_items?
  order_items.size > 0
end

- (none) mark_order_as_shipped

mark the order as shipped when the item ships

Parameters:

  • (none)

Returns:

  • (none)


77
78
79
# File 'app/models/shipment.rb', line 77

def mark_order_as_shipped
  order.update_attributes(:shipped => true)
end

- (none) set_to_shipped

sets the shipped at time to the current time

Parameters:

  • (none)

Returns:

  • (none)


52
53
54
# File 'app/models/shipment.rb', line 52

def set_to_shipped
  self.shipped_at = Time.zone.now
end

- (Boolean) ship_inventory

when the order has been shipped the inventory must be updated

Parameters:

  • (none)

Returns:

  • (Boolean)


68
69
70
71
# File 'app/models/shipment.rb', line 68

def ship_inventory
  order_items.each{ |item| item.variant.subtract_pending_to_customer(1) }
  order_items.each{ |item| item.variant.subtract_count_on_hand(1) }
end

- (Array [Address]) shipping_addresses

Addresses that the user has to ship to

Parameters:

  • (none)

Returns:

  • (Array [Address])

    all user addresses



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

def shipping_addresses
  order.user.shipping_addresses
end