add groupo order invoice and relation to group order
This commit is contained in:
parent
1f6598c144
commit
0fa696cfdd
4 changed files with 112 additions and 0 deletions
38
app/controllers/group_order_invoices_controller.rb
Normal file
38
app/controllers/group_order_invoices_controller.rb
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
class GroupOrderInvoicesController < ApplicationController
|
||||||
|
include Concerns::SendGroupOrderInvoicePdf
|
||||||
|
|
||||||
|
def show
|
||||||
|
@group_order_invoice = GroupOrderInvoice.find(params[:id])
|
||||||
|
if FoodsoftConfig[:contact][:tax_number]
|
||||||
|
respond_to do |format|
|
||||||
|
format.pdf do
|
||||||
|
send_group_order_invoice_pdf @group_order_invoice if FoodsoftConfig[:contact][:tax_number]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else raise RecordInvalid
|
||||||
|
redirect_back fallback_location: root_path, notice: 'Something went wrong', :alert => I18n.t('errors.general_msg', :msg => "#{error} " + I18n.t('errors.check_tax_number'))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
goi = GroupOrderInvoice.find(params[:id])
|
||||||
|
@order = goi.group_order.order
|
||||||
|
goi.destroy
|
||||||
|
respond_to do |format|
|
||||||
|
format.js
|
||||||
|
format.json { head :no_content }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
go = GroupOrder.find(params[:group_order])
|
||||||
|
@order = go.order
|
||||||
|
goi = GroupOrderInvoice.find_or_create_by!(group_order_id: go.id)
|
||||||
|
respond_to do |format|
|
||||||
|
format.js
|
||||||
|
end
|
||||||
|
redirect_back fallback_location: root_path
|
||||||
|
rescue => error
|
||||||
|
redirect_back fallback_location: root_path, notice: 'Something went wrong', :alert => I18n.t('errors.general_msg', :msg => error)
|
||||||
|
end
|
||||||
|
end
|
|
@ -9,6 +9,7 @@ class GroupOrder < ApplicationRecord
|
||||||
has_many :group_order_articles, :dependent => :destroy
|
has_many :group_order_articles, :dependent => :destroy
|
||||||
has_many :order_articles, :through => :group_order_articles
|
has_many :order_articles, :through => :group_order_articles
|
||||||
has_one :financial_transaction
|
has_one :financial_transaction
|
||||||
|
has_one :group_order_invoice
|
||||||
belongs_to :updated_by, optional: true, class_name: 'User', foreign_key: 'updated_by_user_id'
|
belongs_to :updated_by, optional: true, class_name: 'User', foreign_key: 'updated_by_user_id'
|
||||||
|
|
||||||
validates_presence_of :order_id
|
validates_presence_of :order_id
|
||||||
|
|
60
app/models/group_order_invoice.rb
Normal file
60
app/models/group_order_invoice.rb
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
class GroupOrderInvoice < ApplicationRecord
|
||||||
|
|
||||||
|
belongs_to :group_order
|
||||||
|
validates_presence_of :group_order
|
||||||
|
validates_uniqueness_of :invoice_number
|
||||||
|
validate :tax_number_set
|
||||||
|
after_initialize :init, unless: :persisted?
|
||||||
|
|
||||||
|
def generate_invoice_number(count)
|
||||||
|
trailing_number = count.to_s.rjust(4, '0')
|
||||||
|
unless GroupOrderInvoice.find_by(invoice_number: Time.now.strftime("%Y%m%d") + trailing_number)
|
||||||
|
Time.now.strftime("%Y%m%d") + trailing_number
|
||||||
|
else
|
||||||
|
generate_invoice_number(count.to_i + 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def tax_number_set
|
||||||
|
if FoodsoftConfig[:contact][:tax_number].blank?
|
||||||
|
errors.add(:group_order_invoice, "Keine Steuernummer in FoodsoftConfig :contact gesetzt")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def init
|
||||||
|
self.invoice_number = generate_invoice_number(1) unless self.invoice_number
|
||||||
|
self.invoice_date = Time.now unless self.invoice_date
|
||||||
|
self.payment_method = 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
|
||||||
|
invoice_data[:supplier] = order.supplier.name
|
||||||
|
invoice_data[:ordergroup] = group_order.ordergroup
|
||||||
|
invoice_data[:group_order] = group_order
|
||||||
|
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_order.order_articles.each do |order_article|
|
||||||
|
# Get the result of last time ordering, if possible
|
||||||
|
goa = group_order.group_order_articles.detect { |goa| 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
|
13
db/migrate/20211208142719_create_group_order_invoices.rb
Normal file
13
db/migrate/20211208142719_create_group_order_invoices.rb
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
class CreateGroupOrderInvoices < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_table :group_order_invoices do |t|
|
||||||
|
t.integer :group_order_id
|
||||||
|
t.bigint :invoice_number, unique: true, limit: 8
|
||||||
|
t.date :invoice_date
|
||||||
|
t.string :payment_method
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
add_index :group_order_invoices, :group_order_id, unique: true
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue