-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/tw automatic group order invoice generation #907
base: master
Are you sure you want to change the base?
Changes from 1 commit
0fa696c
817c680
50017fe
eadfbd5
298a476
72fa7c1
06eb56a
f592b27
f428450
303f902
0967981
bafa163
1de377c
bddaebe
1fc5823
81daddf
bb69c35
81a5194
07b058a
bb3e049
b2882d8
76be8fd
3683758
5a817a9
3149e00
d37a752
aab1dca
f3914f3
59fd8da
0539edc
b6854bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you check already for the |
||
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')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end | ||
end | ||
|
||
def destroy | ||
goi = GroupOrderInvoice.find(params[:id]) | ||
@order = goi.group_order.order | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the purpose of |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the purpose of @order here? |
||
goi = GroupOrderInvoice.find_or_create_by!(group_order_id: go.id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why |
||
respond_to do |format| | ||
format.js | ||
end | ||
redirect_back fallback_location: root_path | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you redirect here to |
||
rescue => error | ||
redirect_back fallback_location: root_path, notice: 'Something went wrong', :alert => I18n.t('errors.general_msg', :msg => error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the comments form line 13 apply here too |
||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a newline at the end of the file |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
else | ||
generate_invoice_number(count.to_i + 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end | ||
end | ||
|
||
def tax_number_set | ||
if FoodsoftConfig[:contact][:tax_number].blank? | ||
errors.add(:group_order_invoice, "Keine Steuernummer in FoodsoftConfig :contact gesetzt") | ||
end | ||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method does not access the database and therefore should be removed from the model and move to a better location (BTW: there are user visible string which are not translated) |
||
end | ||
|
||
def init | ||
self.invoice_number = generate_invoice_number(1) unless self.invoice_number | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use a |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is the payment method stored in |
||
end | ||
|
||
def name | ||
I18n.t('activerecord.attributes.group_order_invoice.name') + "_#{invoice_number}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the translation should be moved from the model to a controller, since the model should be language independent |
||
end | ||
|
||
def load_data_for_invoice | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you copy everything here? usually working with a |
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why can't we show an invoice without an tax number?