# == Schema Information # # Table name: orders # # id :bigint(8) not null, primary key # order_number :integer # cart_id :bigint(8) not null # shipping_id :integer # payment_method_id :integer # terms_of_service_acceptance :boolean # email :string(255) # first_name :string(80) # last_name :string(80) # phone :string(15) # street :string(255) # city :string(80) # zip_code :string(10) # voivodeship :string(255) # country :string(255) # message_from_client :text(65535) # status :integer default(0), not null # created_at :datetime # updated_at :datetime # updated_by :bigint(8) # # 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', 7 => 'Anulowane' }.freeze ORDER_STATUSES_HTML = { 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', 7 => 'Anulowane' }.freeze VOIVOIDSHIPS = ['dolnośląskie', 'kujawsko-pomorskie', 'lubelskie', 'lubuskie', 'łódzkie', 'małopolskie', 'mazowieckie', 'opolskie', 'podkarpackie', 'podlaskie', 'pomorskie', 'śląskie', 'świętokrzyskie', 'warmińsko-mazurskie', 'wielkopolskie', 'zachodniopomorskie'].freeze def order_summary order_value + shipping.price end def cart_summary cart.cart_value + shipping.price end def beauty_id if order_number.nil? '---------' else order_number.to_s.rjust(8, '0') end 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"\ "woj. #{voivodeship}\n#{country}\n\ntel: #{phone}" end def order_value order_products.sum('order_products.price * order_products.quantity') end end