wip multi orders
This commit is contained in:
parent
fd769509af
commit
f676497e43
29 changed files with 939 additions and 107 deletions
60
app/models/ordergroup_invoice.rb
Normal file
60
app/models/ordergroup_invoice.rb
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue