foodsoft/app/models/multi_order.rb
2025-05-08 11:59:35 +02:00

48 lines
1.3 KiB
Ruby

class MultiOrder < ApplicationRecord
has_many :orders, dependent: :nullify
has_many :order_articles, through: :orders
has_many :multi_group_orders, dependent: :destroy
has_many :group_orders, through: :multi_group_orders
has_many :ordergroups, through: :group_orders
#TODO: wenn groupOrderInvoice existiert, dann fehler schmeißen..
has_many :ordergroup_invoices, through: :group_orders
#make sure order has no multi_order_id
#make sure orders are not in a multi_order
before_create :check_orders
def name
orders.map(&:name).join(', ')
end
def closed?
orders.all?(&:closed?)
end
def stockit?
orders.all?(&:stockit?)
end
def updated_by
orders.map(&:updated_by).compact.first
end
def updated_at
orders.map(&:updated_at).compact.first
end
def foodcoop_result
orders.map(&:foodcoop_result).compact_blank.sum
end
private
def check_orders
orders.each do |order|
errors.add(:base, "Order #{order.name} is already in a multi order") unless order.multi_order_id.nil?
#closed==abgerechnet
errors.add(:base, "Order #{order.name} not closed") unless order.closed?
end
if errors.any?
errors.add(:base, "Cannot create multi order with unfinished orders")
raise ActiveRecord::Rollback
end
end
end