add specs
This commit is contained in:
parent
e902aa0d5a
commit
45db0575b1
46 changed files with 714 additions and 238 deletions
34
app/models/concerns/invoice_common.rb
Normal file
34
app/models/concerns/invoice_common.rb
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
# app/models/concerns/invoice_common.rb
|
||||
module InvoiceCommon
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
include InvoiceHelper
|
||||
|
||||
validates_presence_of :invoice_number
|
||||
validates_uniqueness_of :invoice_number
|
||||
validate :tax_number_set
|
||||
|
||||
after_initialize :init, unless: :persisted?
|
||||
end
|
||||
|
||||
def mark_sepa_downloaded
|
||||
self.sepa_downloaded = true
|
||||
save
|
||||
end
|
||||
|
||||
def unmark_sepa_downloaded
|
||||
self.sepa_downloaded = false
|
||||
save
|
||||
end
|
||||
|
||||
def name
|
||||
I18n.t("activerecord.attributes.#{self.class.name.underscore}.name") + "_#{invoice_number}"
|
||||
end
|
||||
|
||||
def tax_number_set
|
||||
return if FoodsoftConfig[:contact][:tax_number].present?
|
||||
errors.add(:base, "Keine Steuernummer in FoodsoftConfig :contact gesetzt")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -11,7 +11,7 @@ class GroupOrder < ApplicationRecord
|
|||
has_one :financial_transaction
|
||||
has_one :group_order_invoice
|
||||
belongs_to :ordergroup_invoice, optional: true
|
||||
belongs_to :multi_group_order, optional: true, dependent: :destroy
|
||||
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'
|
||||
|
||||
|
|
|
|||
|
|
@ -1,34 +1,9 @@
|
|||
class GroupOrderInvoice < ApplicationRecord
|
||||
include InvoiceHelper
|
||||
include InvoiceCommon
|
||||
|
||||
belongs_to :group_order
|
||||
validates_presence_of :group_order
|
||||
validates_uniqueness_of :invoice_number
|
||||
validate :tax_number_set
|
||||
after_initialize :init, unless: :persisted?
|
||||
|
||||
enum sequence_type: {
|
||||
FRST: "Erst-Lastschrift",
|
||||
RCUR: "Folge-Lastschrift",
|
||||
OOFF: "Einmalige Lastschrift",
|
||||
FNAL: "Letztmalige Lastschrift"
|
||||
}
|
||||
|
||||
def tax_number_set
|
||||
return unless FoodsoftConfig[:contact][:tax_number].blank?
|
||||
|
||||
errors.add(:group_order_invoice, "Keine Steuernummer in FoodsoftConfig :contact gesetzt")
|
||||
end
|
||||
|
||||
def mark_sepa_downloaded
|
||||
self.sepa_downloaded = true
|
||||
self.save
|
||||
end
|
||||
|
||||
def unmark_sepa_downloaded
|
||||
self.sepa_downloaded = false
|
||||
self.save
|
||||
end
|
||||
validates_uniqueness_of :group_order_id
|
||||
|
||||
def init
|
||||
self.invoice_date = Time.now unless invoice_date
|
||||
|
|
@ -36,10 +11,6 @@ class GroupOrderInvoice < ApplicationRecord
|
|||
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
|
||||
|
||||
def name
|
||||
I18n.t('activerecord.attributes.group_order_invoice.name') + "_#{invoice_number}"
|
||||
end
|
||||
|
||||
def load_data_for_invoice
|
||||
invoice_data = {}
|
||||
order = group_order.order
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
class MultiGroupOrder < ApplicationRecord
|
||||
belongs_to :multi_order
|
||||
has_many :group_orders
|
||||
belongs_to :multi_order, optional: false
|
||||
has_many :group_orders, dependent: :nullify
|
||||
has_one :ordergroup_invoice, dependent: :destroy
|
||||
|
||||
validates :multi_order, presence: true
|
||||
|
||||
def ordergroup
|
||||
group_orders.first&.ordergroup
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,11 +6,10 @@ class MultiOrder < ApplicationRecord
|
|||
#TODO: diese association lösen
|
||||
has_many :group_orders, through: :multi_group_orders
|
||||
# has_many :ordergroups, through: :group_orders
|
||||
has_many :ordergroup_invoices, through: :group_orders
|
||||
has_many :ordergroup_invoices, through: :multi_group_orders
|
||||
|
||||
#make sure order has no multi_order_id
|
||||
#make sure orders are not in a multi_order
|
||||
before_create :check_orders
|
||||
validate :check_orders
|
||||
after_create :create_multi_group_orders
|
||||
|
||||
def name
|
||||
orders.map(&:name).join(', ')
|
||||
|
|
@ -40,15 +39,38 @@ class MultiOrder < ApplicationRecord
|
|||
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?
|
||||
unless orders.present?
|
||||
errors.add(:base, "No orders selected")
|
||||
return
|
||||
end
|
||||
if errors.any?
|
||||
errors.add(:base, "Cannot create multi order with unfinished orders")
|
||||
raise ActiveRecord::Rollback
|
||||
orders.each do |order|
|
||||
if order.group_orders.blank?
|
||||
errors.add(:base, "Order #{order.name} has no group orders")
|
||||
end
|
||||
unless order.closed?
|
||||
errors.add(:base, "Order #{order.name} is not closed")
|
||||
end
|
||||
if order.group_orders.any? { |go| go.group_order_invoice.present? }
|
||||
errors.add(:base, "Order #{order.name} has group order invoices")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def create_multi_group_orders
|
||||
return if orders.empty?
|
||||
all_group_orders = orders.flat_map(&:group_orders)
|
||||
grouped_by_ordergroup = all_group_orders.group_by(&:ordergroup_id)
|
||||
|
||||
grouped_by_ordergroup.each do |ordergroup_id, group_orders|
|
||||
multi_group_order = MultiGroupOrder.create!(
|
||||
multi_order: self, group_orders: group_orders
|
||||
)
|
||||
# Now, associate each group_order with the new multi_group_order
|
||||
group_orders.each do |group_order|
|
||||
group_order.update!(multi_group_order: multi_group_order)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,25 +1,10 @@
|
|||
class OrdergroupInvoice < ApplicationRecord
|
||||
include InvoiceHelper
|
||||
include InvoiceCommon
|
||||
|
||||
belongs_to :multi_group_order, optional: true
|
||||
belongs_to :multi_group_order
|
||||
|
||||
# has_many :group_orders, through: :multi_group_order, dependent: :nullify
|
||||
|
||||
validates_presence_of :invoice_number
|
||||
validates_uniqueness_of :invoice_number
|
||||
validate :tax_number_set
|
||||
after_initialize :init, unless: :persisted?
|
||||
|
||||
# accepts_nested_attributes_for :group_orders, :multi_group_order
|
||||
|
||||
|
||||
enum sequence_type: {
|
||||
FRST: "Erst-Lastschrift",
|
||||
RCUR: "Folge-Lastschrift",
|
||||
OOFF: "Einmalige Lastschrift",
|
||||
FNAL: "Letztmalige Lastschrift"
|
||||
}
|
||||
|
||||
def init
|
||||
self.invoice_date = Time.now unless invoice_date
|
||||
self.invoice_number = generate_invoice_number(self, 1) unless self.invoice_number
|
||||
|
|
@ -32,24 +17,8 @@ class OrdergroupInvoice < ApplicationRecord
|
|||
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 mark_sepa_downloaded
|
||||
self.sepa_downloaded = true
|
||||
self.save
|
||||
end
|
||||
|
||||
def unmark_sepa_downloaded
|
||||
self.sepa_downloaded = false
|
||||
self.save
|
||||
end
|
||||
|
||||
def name
|
||||
I18n.t('activerecord.attributes.ordergroup_invoice.name') + "_#{invoice_number}"
|
||||
def send_invoice
|
||||
NotifyOrdergroupInvoiceJob.perform_now(self)
|
||||
end
|
||||
|
||||
def load_data_for_invoice
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue