From 368375818e5a62363d0794536d494411cda5802c Mon Sep 17 00:00:00 2001 From: viehlieb Date: Wed, 30 Mar 2022 11:45:40 +0200 Subject: [PATCH] wip on generating invoices with date --- .../group_order_invoices_controller.rb | 23 +++++++------------ app/models/group_order_invoice.rb | 14 ++++++++--- .../group_order_invoices/_links.html.haml | 15 ++++++++---- config/locales/de.yml | 5 +++- config/locales/en.yml | 4 +++- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/app/controllers/group_order_invoices_controller.rb b/app/controllers/group_order_invoices_controller.rb index b444462a..8228005e 100644 --- a/app/controllers/group_order_invoices_controller.rb +++ b/app/controllers/group_order_invoices_controller.rb @@ -29,21 +29,13 @@ class GroupOrderInvoicesController < ApplicationController def create_multiple invoice_date = params[:group_order_invoice][:invoice_date] order_id = params[:group_order_invoice][:order_id] - puts " - " + "______________" + " - " + "______________" + " - " + "______________" + " - " + "#{invoice_date}" + " - " + "______________"+ " - " + "______________"+ " - " + "______________" - raise "#{invoice_date}" - gos = GroupOrder.find_by(order_id: params[:order_id]) - for go in gos - GroupOrderInvoice.find_or_create_by!(group_order_id: go.id) - respond_to do |format| - format.js - end + @order = Order.find(order_id) + gos = GroupOrder.where("order_id = ?", order_id) + gos.each do |go| + goi = GroupOrderInvoice.find_or_create_by!(group_order_id: go.id) + goi.invoice_date = invoice_date + goi.invoice_number = goi.generate_invoice_number(1) + goi.save! end redirect_back fallback_location: root_path rescue => error @@ -52,6 +44,7 @@ class GroupOrderInvoicesController < ApplicationController def create go = GroupOrder.find(params[:group_order]) + @order = go.order GroupOrderInvoice.find_or_create_by!(group_order_id: go.id) respond_to do |format| format.js diff --git a/app/models/group_order_invoice.rb b/app/models/group_order_invoice.rb index 3e3d809b..a4c1cf1c 100644 --- a/app/models/group_order_invoice.rb +++ b/app/models/group_order_invoice.rb @@ -7,10 +7,18 @@ class GroupOrderInvoice < ApplicationRecord def generate_invoice_number(count) trailing_number = count.to_s.rjust(4, '0') - if GroupOrderInvoice.find_by(invoice_number: Time.now.strftime("%Y%m%d") + trailing_number) + puts " + " + "______________" + " + " + "______________" + " + " + "______________" + " + " + "#{self.invoice_date}" + " + " + "______________"+ " + " + "______________"+ " + " + "______________" + if GroupOrderInvoice.find_by(invoice_number: self.invoice_date.strftime("%Y%m%d") + trailing_number) generate_invoice_number(count.to_i + 1) else - Time.now.strftime("%Y%m%d") + trailing_number + self.invoice_date.strftime("%Y%m%d") + trailing_number end end @@ -21,8 +29,8 @@ class GroupOrderInvoice < ApplicationRecord end def init + self.invoice_date = Time.now unless invoice_date 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 diff --git a/app/views/group_order_invoices/_links.html.haml b/app/views/group_order_invoices/_links.html.haml index 4c69e766..b79b0f64 100644 --- a/app/views/group_order_invoices/_links.html.haml +++ b/app/views/group_order_invoices/_links.html.haml @@ -1,8 +1,15 @@ .row - = form_for :group_order_invoice, url: url_for('group_order_invoice#create_multiple') do |f| - = f.date_field :invoice_date, {} - = f.hidden_field :order_id, value: order.id - = f.submit 'Alle Rechnungen mit Datum erzeugen', class: 'btn btn small' + .column.small-12 + - show_generate_with_date = true + - order.group_orders.each do |go| + - if go.group_order_invoice.present? + - show_generate_with_date = false + - if show_generate_with_date + = form_for :group_order_invoice, url: url_for('group_order_invoice#create_multiple') do |f| + = f.label :invoice_date, I18n.t('activerecord.attributes.group_order_invoice.links.invoice_date') + = f.date_field :invoice_date, {} + = f.hidden_field :order_id, value: order.id + = f.submit I18n.t('activerecord.attributes.group_order_invoice.links.generate_with_date'), class: 'btn btn small' - order.group_orders.includes([:group_order_invoice, :ordergroup]).each do |go| .row diff --git a/config/locales/de.yml b/config/locales/de.yml index 15699757..753db3aa 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -94,8 +94,11 @@ de: name: Bestellgruppenrechnung links: delete: Rechnung löschen - generate: Rechnung erzeugen download: Rechnung herunterladen + generate: Rechnung erzeugen + invoice_date: Datum der Bestellgruppenrechnung + generate_with_date: setzen & erzeugen + payment_method: Guthaben tax_number_not_set: Steuernummer in den Einstellungen nicht gesetzt invoice: diff --git a/config/locales/en.yml b/config/locales/en.yml index b2f76fcb..c4678ea0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -94,8 +94,10 @@ en: name: Group order invoice links: delete: delete invoice - generate: generate invoice download: download invoice + invoice_date: date of group order invoice + generate: generate invoice + generate_with_date: set & generate payment_method: Credit tax_number_not_set: Tax number not set in configs invoice: