84 lines
2.3 KiB
Ruby
84 lines
2.3 KiB
Ruby
# order model
|
|
class Order < ApplicationRecord
|
|
belongs_to :cart
|
|
belongs_to :shipping
|
|
belongs_to :payment_method
|
|
has_many :order_products, -> { joins(:product).order('products.name ASC') }
|
|
has_many :order_histories
|
|
validates :email, format:
|
|
{ with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i },
|
|
allow_nil: true
|
|
validates :street, length: { minimum: 4 }, allow_nil: true
|
|
validates :first_name, length: { minimum: 2 }, allow_nil: true
|
|
validates :last_name, length: { minimum: 2 }, allow_nil: true
|
|
validates :phone, length: { minimum: 9 }, allow_nil: true
|
|
validates :city, length: { minimum: 2 }, allow_nil: true
|
|
validates :zip_code, length: { minimum: 5 }, allow_nil: true
|
|
validates :terms_of_service_acceptance, acceptance: { accept: true },
|
|
allow_nil: true
|
|
|
|
scope :complete, -> { where('status != 0') }
|
|
after_create :a_create
|
|
after_update :a_update
|
|
|
|
ORDER_STATUSES = {
|
|
0 => 'W trakcie składania',
|
|
1 => 'Nowe',
|
|
2 => 'W trakcie realizacji',
|
|
3 => 'Oczekuje na płatność',
|
|
4 => 'Zapłacone',
|
|
5 => 'Wysłane',
|
|
6 => 'Zakończone'
|
|
}.freeze
|
|
|
|
ORDER_STATUSES_HTML = {
|
|
0 => '<span class="badge bg-yellow">W trakcie składania</span>',
|
|
1 => '<span class="badge bg-green">Nowe</span>',
|
|
2 => '<span class="badge bg-blue">W trakcie realizacji</span>',
|
|
3 => '<span class="badge bg-maroon">Oczekuje na płatność</span>',
|
|
4 => '<span class="badge bg-light-blue">Zapłacone</span>',
|
|
5 => '<span class="badge bg-fuchsia">Wysłane</span>',
|
|
6 => '<span class="badge bg-navy">Zakończone</span>'
|
|
}.freeze
|
|
|
|
def order_summary
|
|
order_value + shipping.price
|
|
end
|
|
|
|
def cart_summary
|
|
cart.cart_value + shipping.price
|
|
end
|
|
|
|
def beauty_id
|
|
id.to_s.rjust(8, '0')
|
|
end
|
|
|
|
def date_checkout
|
|
created_at.to_date
|
|
end
|
|
|
|
def a_create
|
|
OrderHistory.create(order_id: id, status: status)
|
|
end
|
|
|
|
def a_update
|
|
OrderHistory.create(order_id: id, status: status) if status_changed?
|
|
end
|
|
|
|
def anonimize_data
|
|
# TODO; anonimizacja danych
|
|
end
|
|
|
|
def full_name
|
|
"#{first_name} #{last_name}"
|
|
end
|
|
|
|
def address
|
|
"#{street}\n#{zip_code} #{city}\n\ntel: #{phone}"
|
|
end
|
|
|
|
def order_value
|
|
order_products.sum('order_products.price * order_products.quantity')
|
|
end
|
|
end
|