diff --git a/app/controllers/group_order_invoices_controller.rb b/app/controllers/group_order_invoices_controller.rb index bf6d559f..82c411da 100644 --- a/app/controllers/group_order_invoices_controller.rb +++ b/app/controllers/group_order_invoices_controller.rb @@ -9,9 +9,11 @@ class GroupOrderInvoicesController < ApplicationController 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')) + else + raise RecordInvalid end + rescue => error + 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 def destroy @@ -27,7 +29,7 @@ class GroupOrderInvoicesController < ApplicationController def create go = GroupOrder.find(params[:group_order]) @order = go.order - goi = GroupOrderInvoice.find_or_create_by!(group_order_id: go.id) + GroupOrderInvoice.find_or_create_by!(group_order_id: go.id) respond_to do |format| format.js end @@ -35,4 +37,4 @@ class GroupOrderInvoicesController < ApplicationController rescue => error redirect_back fallback_location: root_path, notice: 'Something went wrong', :alert => I18n.t('errors.general_msg', :msg => error) end -end \ No newline at end of file +end diff --git a/app/documents/group_order_invoice_pdf.rb b/app/documents/group_order_invoice_pdf.rb index 13856330..76bf107f 100644 --- a/app/documents/group_order_invoice_pdf.rb +++ b/app/documents/group_order_invoice_pdf.rb @@ -12,8 +12,8 @@ class GroupOrderInvoicePdf < RenderPDF ordergroup = @options[:ordergroup] # From paragraph - bounding_box [margin_box.right - 200, margin_box.top-20], width: 200 do - text I18n.t('documents.group_order_invoice_pdf.invoicer') + bounding_box [margin_box.right - 200, margin_box.top - 20], width: 200 do + text I18n.t('documents.group_order_invoice_pdf.invoicer') move_down 7 text FoodsoftConfig[:name], size: fontsize(9), align: :left move_down 5 @@ -32,24 +32,23 @@ class GroupOrderInvoicePdf < RenderPDF text I18n.t('documents.group_order_invoice_pdf.tax_number', :number => @options[:tax_number]), size: fontsize(9), align: :left end - # Receiving Ordergroup bounding_box [margin_box.left, margin_box.top - 20], width: 200 do - text I18n.t('documents.group_order_invoice_pdf.invoicee') + text I18n.t('documents.group_order_invoice_pdf.invoicee') move_down 7 - text I18n.t('documents.group_order_invoice_pdf.ordergroup.name', ordergroup: ordergroup.name.to_s ), size: fontsize(9) + text I18n.t('documents.group_order_invoice_pdf.ordergroup.name', ordergroup: ordergroup.name.to_s), size: fontsize(9) move_down 5 if ordergroup.contact_address - text I18n.t('documents.group_order_invoice_pdf.ordergroup.contact_address', contact_address: ordergroup.contact_address.to_s ), size: fontsize(9) + text I18n.t('documents.group_order_invoice_pdf.ordergroup.contact_address', contact_address: ordergroup.contact_address.to_s), size: fontsize(9) move_down 5 end if ordergroup.contact_phone - text I18n.t('documents.group_order_invoice_pdf.ordergroup.contact_phone', contact_phone: ordergroup.contact_phone.to_s ), size: fontsize(9) + text I18n.t('documents.group_order_invoice_pdf.ordergroup.contact_phone', contact_phone: ordergroup.contact_phone.to_s), size: fontsize(9) move_down 5 end end - #invoice Date and nnvoice number + # invoice Date and nnvoice number bounding_box [margin_box.right - 200, margin_box.top - 150], width: 200 do text I18n.t('documents.group_order_invoice_pdf.invoice_date', invoice_date: @options[:invoice_date].strftime(I18n.t('date.formats.default'))), align: :left move_down 5 @@ -69,36 +68,37 @@ class GroupOrderInvoicePdf < RenderPDF total_net = 0 # Articles - tax_hash_net = Hash.new(0) #for summing up article net prices grouped into vat percentage - tax_hash_gross = Hash.new(0) #same here with gross prices + tax_hash_net = Hash.new(0) # for summing up article net prices grouped into vat percentage + tax_hash_gross = Hash.new(0) # same here with gross prices group_order = GroupOrder.find(@options[:group_order].id) marge = FoodsoftConfig[:price_markup] # data table looks different when price_markup > 0 - if marge == 0 - data = [I18n.t('documents.group_order_invoice_pdf.no_price_markup_rows')] - else - data = [I18n.t('documents.group_order_invoice_pdf.price_markup_rows', marge: marge)] - end - goa_tax_hash = GroupOrderArticle.where(group_order_id: group_order.id).find_each.group_by {|oat| oat.order_article.price.tax} + data = if marge == 0 + [I18n.t('documents.group_order_invoice_pdf.no_price_markup_rows')] + else + [I18n.t('documents.group_order_invoice_pdf.price_markup_rows', marge: marge)] + end + goa_tax_hash = GroupOrderArticle.where(group_order_id: group_order.id).find_each.group_by { |oat| oat.order_article.price.tax } goa_tax_hash.each do |tax, group_order_articles| group_order_articles.each do |goa| # if no unit is received, nothing is to be charged next if goa.result.to_i == 0 + order_article = goa.order_article goa_total_net = goa.result * order_article.price.price goa_total_gross = goa.result * order_article.price.gross_price data << [order_article.article.name, - goa.result.to_i, - number_to_currency(order_article.price.price), - number_to_currency(goa_total_net), - tax.to_s + '%', - number_to_currency(goa.total_price)] - tax_hash_net[tax.to_i] += goa_total_net - tax_hash_gross[tax.to_i] += goa_total_gross - total_net += goa_total_net - total_gross += goa_total_gross + goa.result.to_i, + number_to_currency(order_article.price.price), + number_to_currency(goa_total_net), + tax.to_s + '%', + number_to_currency(goa.total_price)] + tax_hash_net[tax.to_i] += goa_total_net + tax_hash_gross[tax.to_i] += goa_total_gross + total_net += goa_total_net + total_gross += goa_total_gross end end @@ -106,7 +106,7 @@ class GroupOrderInvoicePdf < RenderPDF # article information + data table data, cell_style: { size: fontsize(8), overflow: :shrink_to_fit } do |table| table.header = true - table.position= :center + table.position = :center table.cells.border_width = 1 table.cells.border_color = '666666' @@ -118,23 +118,22 @@ class GroupOrderInvoicePdf < RenderPDF sum = [] sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.sum_to_pay_net'), number_to_currency(total_net)] - tax_hash_net.keys.each do |tax| - sum << [nil,nil,nil,nil, I18n.t('documents.group_order_invoice_pdf.tax_included', tax: tax), number_to_currency(tax_hash_gross[tax] - tax_hash_net[tax])] + tax_hash_net.each_key.each do |tax| + sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.tax_included', tax: tax), number_to_currency(tax_hash_gross[tax] - tax_hash_net[tax])] end unless marge == 0 - sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.markup_included', marge: marge), number_to_currency(total_gross * marge/100.0)] + sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.markup_included', marge: marge), number_to_currency(total_gross * marge / 100.0)] end - end_sum = total_gross * (1 + marge/100.0) + end_sum = total_gross * (1 + marge / 100.0) sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.sum_to_pay_gross'), number_to_currency(end_sum)] # table for sum - table sum, position: :right, cell_style: { size: fontsize(8), overflow: :shrink_to_fit } do |table| + table sum, position: :right, cell_style: { size: fontsize(8), overflow: :shrink_to_fit } do |table| sum.length.times do |count| table.row(count).columns(0..5).borders = [] end - table.row(sum.length-1).columns(0..4).borders = [] - table.row(sum.length-1).border_bottom_width = 2 - table.row(sum.length-1).columns(5).borders = [:bottom] - + table.row(sum.length - 1).columns(0..4).borders = [] + table.row(sum.length - 1).border_bottom_width = 2 + table.row(sum.length - 1).columns(5).borders = [:bottom] end move_down 15 diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index ae99e08c..48367f17 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -63,7 +63,6 @@ class Mailer < ActionMailer::Base subject: I18n.t('mailer.group_order_invoice.subject', group: @group.name, supplier: @supplier) end - # Sends order result for specific Ordergroup def order_result(user, group_order) @order = group_order.order diff --git a/app/models/group_order_invoice.rb b/app/models/group_order_invoice.rb index f39b4b2a..3e3d809b 100644 --- a/app/models/group_order_invoice.rb +++ b/app/models/group_order_invoice.rb @@ -1,5 +1,4 @@ class GroupOrderInvoice < ApplicationRecord - belongs_to :group_order validates_presence_of :group_order validates_uniqueness_of :invoice_number @@ -8,10 +7,10 @@ class GroupOrderInvoice < ApplicationRecord 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 + if GroupOrderInvoice.find_by(invoice_number: Time.now.strftime("%Y%m%d") + trailing_number) generate_invoice_number(count.to_i + 1) + else + Time.now.strftime("%Y%m%d") + trailing_number end end @@ -44,7 +43,7 @@ class GroupOrderInvoice < ApplicationRecord 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 } + goa = group_order.group_order_articles.detect { |tmp_goa| tmp_goa.order_article_id == order_article.id } # Build hash with relevant data invoice_data[:order_articles][order_article.id] = { @@ -55,6 +54,5 @@ class GroupOrderInvoice < ApplicationRecord } end invoice_data - end end diff --git a/config/routes.rb b/config/routes.rb index b0f5c300..8b166ea3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -172,7 +172,6 @@ Rails.application.routes.draw do get :unpaid, on: :collection end - resources :links, controller: 'financial_links', only: [:create, :show] do collection do get :incomplete diff --git a/spec/factories/group_order_invoice.rb b/spec/factories/group_order_invoice.rb index 036a2815..89723873 100644 --- a/spec/factories/group_order_invoice.rb +++ b/spec/factories/group_order_invoice.rb @@ -2,6 +2,6 @@ require 'factory_bot' FactoryBot.define do factory :group_order_invoice do - group_order{ create :group_order} + group_order { create :group_order } end end diff --git a/spec/integration/group_order_invoices_spec.rb b/spec/integration/group_order_invoices_spec.rb index 05535d76..f2a76847 100644 --- a/spec/integration/group_order_invoices_spec.rb +++ b/spec/integration/group_order_invoices_spec.rb @@ -3,59 +3,55 @@ require_relative '../spec_helper' feature GroupOrderInvoice, js: true do let(:admin) { create :user, groups: [create(:workgroup, role_finance: true)] } let(:article) { create :article, unit_quantity: 1 } - let(:order) { create :order, supplier: article.supplier, article_ids: [article.id], ends: Time.now } # need to ref article + let(:order) { create :order, supplier: article.supplier, article_ids: [article.id], ends: Time.now } # need to ref article let(:go) { create :group_order, order: order } let(:oa) { order.order_articles.find_by_article_id(article.id) } let(:ftt) { create :financial_transaction_type } let(:goa) { create :group_order_article, group_order: go, order_article: oa } + include ActiveJob::TestHelper + before { login admin } - describe 'trigger process' do + after { clear_enqueued_jobs } - include ActiveJob::TestHelper - - before { login admin } - after { clear_enqueued_jobs } - - it 'does not enqueue MailerJob when order is settled if tax_number or options not set' do - goa.update_quantities 2, 0 - oa.update_results! - visit confirm_finance_order_path(id: order.id) - click_link_or_button I18n.t('finance.balancing.confirm.clear') - expect(NotifyGroupOrderInvoiceJob).not_to have_been_enqueued - end - - it 'enqueues MailerJob when order is settled if tax_number or options are set' do - goa.update_quantities 2, 0 - oa.update_results! - order.reload - FoodsoftConfig[:group_order_invoices] = { use: true } - FoodsoftConfig[:contact][:tax_number] = 12345678 - visit confirm_finance_order_path(id: order.id, type: ftt) - expect(page).to have_selector(:link_or_button, I18n.t('finance.balancing.confirm.clear')) - click_link_or_button I18n.t('finance.balancing.confirm.clear') - expect(NotifyGroupOrderInvoiceJob).to have_been_enqueued - end - - it 'does not generate Group Order Invoice when order is closed if tax_number not set' do - goa.update_quantities 2, 0 - oa.update_results! - order.update!(state: 'closed') - order.reload - visit finance_order_index_path - expect(page).to have_content(I18n.t('activerecord.attributes.group_order_invoice.tax_number_not_set')) - end - - it 'generates Group Order Invoice when order is closed if tax_number is set' do - goa.update_quantities 2, 0 - oa.update_results! - FoodsoftConfig[:contact][:tax_number] = 12345678 - order.update!(state: 'closed') - order.reload - visit finance_order_index_path - click_link_or_button I18n.t('activerecord.attributes.group_order_invoice.links.generate') - expect(GroupOrderInvoice.all.count).to eq(1) - end + it 'does not enqueue MailerJob when order is settled if tax_number or options not set' do + goa.update_quantities 2, 0 + oa.update_results! + visit confirm_finance_order_path(id: order.id) + click_link_or_button I18n.t('finance.balancing.confirm.clear') + expect(NotifyGroupOrderInvoiceJob).not_to have_been_enqueued end -end \ No newline at end of file + + it 'enqueues MailerJob when order is settled if tax_number or options are set' do + goa.update_quantities 2, 0 + oa.update_results! + order.reload + FoodsoftConfig[:group_order_invoices] = { use: true } + FoodsoftConfig[:contact][:tax_number] = 12_345_678 + visit confirm_finance_order_path(id: order.id, type: ftt) + expect(page).to have_selector(:link_or_button, I18n.t('finance.balancing.confirm.clear')) + click_link_or_button I18n.t('finance.balancing.confirm.clear') + expect(NotifyGroupOrderInvoiceJob).to have_been_enqueued + end + + it 'does not generate Group Order Invoice when order is closed if tax_number not set' do + goa.update_quantities 2, 0 + oa.update_results! + order.update!(state: 'closed') + order.reload + visit finance_order_index_path + expect(page).to have_content(I18n.t('activerecord.attributes.group_order_invoice.tax_number_not_set')) + end + + it 'generates Group Order Invoice when order is closed if tax_number is set' do + goa.update_quantities 2, 0 + oa.update_results! + FoodsoftConfig[:contact][:tax_number] = 12_345_678 + order.update!(state: 'closed') + order.reload + visit finance_order_index_path + click_link_or_button I18n.t('activerecord.attributes.group_order_invoice.links.generate') + expect(GroupOrderInvoice.all.count).to eq(1) + end +end diff --git a/spec/models/group_order_invoice_spec.rb b/spec/models/group_order_invoice_spec.rb index 1f3fc559..2da767e4 100644 --- a/spec/models/group_order_invoice_spec.rb +++ b/spec/models/group_order_invoice_spec.rb @@ -4,58 +4,57 @@ describe GroupOrderInvoice do let(:user) { create :user, groups: [create(:ordergroup)] } let(:supplier) { create :supplier } let(:article) { create :article, supplier: supplier } - let(:order){ create :order } - let(:group_order) {create :group_order, order: order, ordergroup: user.ordergroup } + let(:order) { create :order } + let(:group_order) { create :group_order, order: order, ordergroup: user.ordergroup } describe 'erroneous group order invoice' do let(:goi) { create :group_order_invoice, group_order_id: group_order.id } it 'does not create group order invoice if tax_number not set' do - expect{goi}.to raise_error(ActiveRecord::RecordInvalid) + expect { goi }.to raise_error(ActiveRecord::RecordInvalid) end - end describe 'valid group order invoice' do before do - FoodsoftConfig[:contact][:tax_number] = 12345678 + FoodsoftConfig[:contact][:tax_number] = 123_457_8 end invoice_number1 = Time.now.strftime("%Y%m%d") + '0001' invoice_number2 = Time.now.strftime("%Y%m%d") + '0002' - let(:user_2) { create :user, groups: [create(:ordergroup)] } + let(:user2) { create :user, groups: [create(:ordergroup)] } - let(:goi_1) { create :group_order_invoice, group_order_id: group_order.id } - let(:goi_2) { create :group_order_invoice, group_order_id: group_order.id } + let(:goi1) { create :group_order_invoice, group_order_id: group_order.id } + let(:goi2) { create :group_order_invoice, group_order_id: group_order.id } - let(:group_order_2) {create :group_order, order: order, ordergroup: user_2.ordergroup } + let(:group_order2) { create :group_order, order: order, ordergroup: user2.ordergroup } - let(:goi_3) { create :group_order_invoice, group_order_id: group_order_2.id } - let(:goi_4) { create :group_order_invoice, group_order_id: group_order_2.id, invoice_number: invoice_number1 } + let(:goi3) { create :group_order_invoice, group_order_id: group_order2.id } + let(:goi4) { create :group_order_invoice, group_order_id: group_order2.id, invoice_number: invoice_number1 } it 'creates group order invoice if tax_number is set' do - expect(goi_1).to be_valid + expect(goi1).to be_valid end it 'sets invoice_number according to date' do number = Time.now.strftime("%Y%m%d") + '0001' - expect(goi_1.invoice_number).to eq(number.to_i) + expect(goi1.invoice_number).to eq(number.to_i) end it 'fails to create if group_order_id is used multiple times for creation' do - expect(goi_1.group_order.id).to eq(group_order.id) - expect{goi_2}.to raise_error(ActiveRecord::RecordNotUnique) + expect(goi1.group_order.id).to eq(group_order.id) + expect { goi2 }.to raise_error(ActiveRecord::RecordNotUnique) end it 'creates two different group order invoice with different invoice_numbers' do - expect(goi_1.invoice_number).to eq(invoice_number1.to_i) - expect(goi_3.invoice_number).to eq(invoice_number2.to_i) + expect(goi1.invoice_number).to eq(invoice_number1.to_i) + expect(goi3.invoice_number).to eq(invoice_number2.to_i) end it 'fails to create two different group order invoice with same invoice_numbers' do - goi_1 - expect{goi_4}.to raise_error(ActiveRecord::RecordInvalid) + goi1 + expect { goi4 }.to raise_error(ActiveRecord::RecordInvalid) end end -end \ No newline at end of file +end