wip multi orders

This commit is contained in:
viehlieb 2025-05-08 11:59:35 +02:00
parent fd769509af
commit f676497e43
29 changed files with 939 additions and 107 deletions

View file

@ -10,6 +10,9 @@ class GroupOrder < ApplicationRecord
has_many :order_articles, through: :group_order_articles
has_one :financial_transaction
has_one :group_order_invoice
belongs_to :ordergroup_invoice, optional: true
belongs_to :multi_group_order, optional: true
belongs_to :multi_order, optional: true
belongs_to :updated_by, optional: true, class_name: 'User', foreign_key: 'updated_by_user_id'
validates :order_id, presence: true

View file

@ -1,4 +1,5 @@
class GroupOrderInvoice < ApplicationRecord
include InvoiceHelper
belongs_to :group_order
validates_presence_of :group_order
@ -13,19 +14,10 @@ class GroupOrderInvoice < ApplicationRecord
FNAL: "Letztmalige Lastschrift"
}
def generate_invoice_number(count)
trailing_number = count.to_s.rjust(4, '0')
if GroupOrderInvoice.find_by(invoice_number: self.invoice_date.strftime("%Y%m%d") + trailing_number)
generate_invoice_number(count.to_i + 1)
else
self.invoice_date.strftime("%Y%m%d") + trailing_number
end
end
def tax_number_set
if FoodsoftConfig[:contact][:tax_number].blank?
errors.add(:group_order_invoice, "Keine Steuernummer in FoodsoftConfig :contact gesetzt")
end
return unless FoodsoftConfig[:contact][:tax_number].blank?
errors.add(:group_order_invoice, "Keine Steuernummer in FoodsoftConfig :contact gesetzt")
end
def mark_sepa_downloaded
@ -40,7 +32,7 @@ class GroupOrderInvoice < ApplicationRecord
def init
self.invoice_date = Time.now unless invoice_date
self.invoice_number = generate_invoice_number(1) unless self.invoice_number
self.invoice_number = generate_invoice_number(self, 1) unless self.invoice_number
self.payment_method = group_order&.financial_transaction&.financial_transaction_type&.name || FoodsoftConfig[:group_order_invoices]&.[](:payment_method) || I18n.t('activerecord.attributes.group_order_invoice.payment_method') unless self.payment_method
end
@ -54,7 +46,7 @@ class GroupOrderInvoice < ApplicationRecord
invoice_data[:pickup] = order.pickup
invoice_data[:supplier] = order.supplier&.name
invoice_data[:ordergroup] = group_order.ordergroup
invoice_data[:group_order] = group_order
invoice_data[:group_order_ids] = [group_order.id]
invoice_data[:invoice_number] = invoice_number
invoice_data[:invoice_date] = invoice_date
invoice_data[:tax_number] = FoodsoftConfig[:contact][:tax_number]

View file

@ -0,0 +1,21 @@
class MultiGroupOrder < ApplicationRecord
belongs_to :multi_order
has_many :group_orders, dependent: :nullify
after_destroy :delete_ordergroup_invoices
def ordergroup_invoice
#TODO: delete if deleted
group_orders.joins(:ordergroup_invoice).first&.ordergroup_invoice
end
def ordergroup
ordergroup_invoice&.ordergroup
end
private
def delete_ordergroup_invoices
ordergroup_invoice&.destroy
end
end

48
app/models/multi_order.rb Normal file
View file

@ -0,0 +1,48 @@
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

View file

@ -10,6 +10,7 @@ class Order < ApplicationRecord
has_many :comments, -> { order('created_at') }, class_name: 'OrderComment'
has_many :stock_changes
belongs_to :invoice, optional: true
belongs_to :multi_order, optional: true, inverse_of: :orders
belongs_to :supplier, optional: true
belongs_to :updated_by, class_name: 'User', foreign_key: 'updated_by_user_id'
belongs_to :created_by, class_name: 'User', foreign_key: 'created_by_user_id'
@ -44,6 +45,8 @@ class Order < ApplicationRecord
scope :finished, -> { where(state: %w[finished received closed]).order(ends: :desc) }
scope :finished_not_closed, -> { where(state: %w[finished received]).order(ends: :desc) }
scope :non_multi_order, -> { where(multi_order_id: nil) }
# Allow separate inputs for date and time
# with workaround for https://github.com/einzige/date_time_attribute/issues/14
include DateTimeAttributeValidate

View file

@ -0,0 +1,60 @@
class OrdergroupInvoice < ApplicationRecord
include InvoiceHelper
belongs_to :multi_group_order, optional: true
has_many :group_orders, dependent: :nullify
validates_presence_of :invoice_number
validates_uniqueness_of :invoice_number
validate :tax_number_set
after_initialize :init, unless: :persisted?
def init
self.invoice_date = Time.now unless invoice_date
self.invoice_number = generate_invoice_number(self, 1) unless self.invoice_number
self.payment_method = group_orders.first&.financial_transaction&.financial_transaction_type&.name || FoodsoftConfig[:group_order_invoices]&.[](:payment_method) || I18n.t('activerecord.attributes.group_order_invoice.payment_method') unless self.payment_method
end
def ordergroup
return if group_orders.empty?
group_orders.first.ordergroup
end
def tax_number_set
return if FoodsoftConfig[:contact][:tax_number].present?
errors.add(:cumulative_invoice, "Keine Steuernummer in FoodsoftConfig :contact gesetzt")
end
def name
I18n.t('activerecord.attributes.group_order_invoice.name') + "_#{invoice_number}"
end
def load_data_for_invoice
invoice_data = {}
order = group_orders.map(&:order).first
invoice_data[:pickup] = order.pickup
invoice_data[:supplier] = order.supplier&.name
invoice_data[:ordergroup] = group_orders.first.ordergroup
invoice_data[:group_order_ids] = group_orders.pluck(:id)
invoice_data[:invoice_number] = invoice_number
invoice_data[:invoice_date] = invoice_date
invoice_data[:tax_number] = FoodsoftConfig[:contact][:tax_number]
invoice_data[:payment_method] = payment_method
invoice_data[:order_articles] = {}
group_orders.map(&:order_articles).flatten.each do |order_article|
# Get the result of last time ordering, if possible
# goa = group_orders.group_order_articles.detect { |tmp_goa| tmp_goa.order_article_id == order_article.id }
goa = group_orders.map(&:group_order_articles).flatten.detect { |tmp_goa| tmp_goa.order_article_id == order_article.id }
# Build hash with relevant data
invoice_data[:order_articles][order_article.id] = {
:price => order_article.article.fc_price,
:quantity => (goa ? goa.quantity : 0),
:total_price => (goa ? goa.total_price : 0),
:tax => order_article.article.tax
}
end
invoice_data
end
end