move automatic invoices to plugin
changes on deposit calculation tiny changes on group order invoice pdf
This commit is contained in:
parent
42a1773a87
commit
e78d1ad072
67 changed files with 5579 additions and 69 deletions
|
|
@ -0,0 +1,7 @@
|
|||
require 'factory_bot'
|
||||
|
||||
FactoryBot.define do
|
||||
factory :group_order_invoice do
|
||||
group_order { create :group_order }
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
require_relative '../spec_helper'
|
||||
|
||||
feature GroupOrderInvoice, js: true do
|
||||
let(:admin) { create :user, groups: [create(:workgroup, role_finance: true)] }
|
||||
let(:user) { create :user, groups: [create(:ordergroup)] }
|
||||
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(:go) { create :group_order, order: order, ordergroup: user.ordergroup}
|
||||
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 }
|
||||
|
||||
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_automatic_invoices: 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 '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')
|
||||
go.reload
|
||||
order.reload
|
||||
visit finance_order_index_path
|
||||
expect(page).to have_selector(:link_or_button, I18n.t('activerecord.attributes.group_order_invoice.links.generate'))
|
||||
click_link_or_button I18n.t('activerecord.attributes.group_order_invoice.links.generate')
|
||||
expect(GroupOrderInvoice.all.count).to eq(1)
|
||||
end
|
||||
|
||||
it 'generates multiple Group Order Invoice for order 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
|
||||
expect(page).to have_selector(:link_or_button, I18n.t('activerecord.attributes.group_order_invoice.links.generate_with_date'))
|
||||
click_link_or_button I18n.t('activerecord.attributes.group_order_invoice.links.generate_with_date')
|
||||
expect(GroupOrderInvoice.all.count).to eq(1)
|
||||
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
|
||||
end
|
||||
168
plugins/automatic_invoices/spec/models/article_spec.rb
Normal file
168
plugins/automatic_invoices/spec/models/article_spec.rb
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
require_relative '../spec_helper'
|
||||
|
||||
describe Article do
|
||||
let(:supplier) { create(:supplier) }
|
||||
let(:article) { create(:article, supplier: supplier) }
|
||||
|
||||
it 'has a unique name' do
|
||||
article2 = build(:article, supplier: supplier, name: article.name)
|
||||
expect(article2).to be_invalid
|
||||
end
|
||||
|
||||
it 'can be deleted' do
|
||||
expect(article).not_to be_deleted
|
||||
article.mark_as_deleted
|
||||
expect(article).to be_deleted
|
||||
end
|
||||
|
||||
describe 'convert units' do
|
||||
it 'returns nil when equal' do
|
||||
expect(article.convert_units(article)).to be_nil
|
||||
end
|
||||
|
||||
it 'returns false when invalid unit' do
|
||||
article1 = build(:article, supplier: supplier, unit: 'invalid')
|
||||
expect(article.convert_units(article1)).to be false
|
||||
end
|
||||
|
||||
it 'returns false if unit = 0' do
|
||||
article1 = build(:article, supplier: supplier, unit: '1kg', price: 2, unit_quantity: 1)
|
||||
article2 = build(:article, supplier: supplier, unit: '0kg', price: 2, unit_quantity: 1)
|
||||
expect(article1.convert_units(article2)).to be false
|
||||
end
|
||||
|
||||
it 'returns false if unit becomes zero because of , symbol in unit format' do
|
||||
article1 = build(:article, supplier: supplier, unit: '0,8kg', price: 2, unit_quantity: 1)
|
||||
article2 = build(:article, supplier: supplier, unit: '0,9kg', price: 2, unit_quantity: 1)
|
||||
expect(article1.convert_units(article2)).to be false
|
||||
end
|
||||
|
||||
it 'converts from ST to KI (german foodcoops legacy)' do
|
||||
article1 = build(:article, supplier: supplier, unit: 'ST')
|
||||
article2 = build(:article, supplier: supplier, name: 'banana 10-12 St', price: 12.34, unit: 'KI')
|
||||
new_price, new_unit_quantity = article1.convert_units(article2)
|
||||
expect(new_unit_quantity).to eq 10
|
||||
expect(new_price).to eq 1.23
|
||||
end
|
||||
|
||||
it 'converts from g to kg' do
|
||||
article1 = build(:article, supplier: supplier, unit: 'kg')
|
||||
article2 = build(:article, supplier: supplier, unit: 'g', price: 0.12, unit_quantity: 1500)
|
||||
new_price, new_unit_quantity = article1.convert_units(article2)
|
||||
expect(new_unit_quantity).to eq 1.5
|
||||
expect(new_price).to eq 120
|
||||
end
|
||||
end
|
||||
|
||||
it 'computes changed article attributes' do
|
||||
article2 = build(:article, supplier: supplier, name: 'banana')
|
||||
expect(article.unequal_attributes(article2)[:name]).to eq 'banana'
|
||||
end
|
||||
|
||||
it 'computes the gross price correctly' do
|
||||
article.deposit = 0
|
||||
article.tax = 12
|
||||
expect(article.gross_price).to eq((article.price * 1.12).round(2))
|
||||
article.deposit = 1.20
|
||||
if FoodsoftConfig[:group_order_invoices]&.[](:separate_deposits)
|
||||
expect(article.gross_price_without_deposit).to eq((article.price * 1.12 + 1.20).round(2))
|
||||
expect(article.gross_price).to eq(((article.price + 1.20) * 1.12).round(2))
|
||||
end
|
||||
end
|
||||
|
||||
it 'gross price >= net price' do
|
||||
expect(article.gross_price).to be >= article.price
|
||||
end
|
||||
|
||||
[[nil, 1],
|
||||
[0, 1],
|
||||
[5, 1.05],
|
||||
[42, 1.42],
|
||||
[100, 2]].each do |price_markup, percent|
|
||||
it "computes the fc price with price_markup #{price_markup} correctly" do
|
||||
FoodsoftConfig.config['price_markup'] = price_markup
|
||||
expect(article.fc_price).to eq((article.gross_price * percent).round(2))
|
||||
end
|
||||
end
|
||||
it 'knows when it is deleted' do
|
||||
expect(supplier.deleted?).to be false
|
||||
supplier.mark_as_deleted
|
||||
expect(supplier.deleted?).to be true
|
||||
end
|
||||
|
||||
it 'keeps a price history' do
|
||||
expect(article.article_prices.map(&:price)).to eq([article.price])
|
||||
oldprice = article.price
|
||||
sleep 1 # so that the new price really has a later creation time
|
||||
article.price += 1
|
||||
article.save!
|
||||
expect(article.article_prices.reload.map(&:price)).to eq([article.price, oldprice])
|
||||
end
|
||||
|
||||
it 'is not in an open order by default' do
|
||||
expect(article.in_open_order).to be_nil
|
||||
end
|
||||
|
||||
it 'is knows its open order' do
|
||||
order = create(:order, supplier: supplier, article_ids: [article.id])
|
||||
expect(article.in_open_order).to eq(order)
|
||||
end
|
||||
|
||||
it 'has no shared article by default' do
|
||||
expect(article.shared_article).to be_nil
|
||||
end
|
||||
|
||||
describe 'connected to a shared database', type: :feature do
|
||||
let(:shared_article) { create(:shared_article) }
|
||||
let(:supplier) { create(:supplier, shared_supplier_id: shared_article.supplier_id) }
|
||||
let(:article) { create(:article, supplier: supplier, order_number: shared_article.order_number) }
|
||||
|
||||
it 'can be found in the shared database' do
|
||||
expect(article.shared_article).not_to be_nil
|
||||
end
|
||||
|
||||
it 'can find updates' do
|
||||
changed = article.shared_article_changed?
|
||||
expect(changed).not_to be_falsey
|
||||
expect(changed.length).to be > 1
|
||||
end
|
||||
|
||||
it 'can be synchronised' do
|
||||
# TODO: move article sync from supplier to article
|
||||
article # need to reference for it to exist when syncing
|
||||
updated_article = supplier.sync_all[0].select { |s| s[0].id == article.id }.first[0]
|
||||
article.update(updated_article.attributes.reject { |k, _v| %w[id type].include?(k) })
|
||||
expect(article.name).to eq(shared_article.name)
|
||||
# now synchronising shouldn't change anything anymore
|
||||
expect(article.shared_article_changed?).to be_falsey
|
||||
end
|
||||
|
||||
it 'does not need to synchronise an imported article' do
|
||||
article = shared_article.build_new_article(supplier)
|
||||
article.article_category = create :article_category
|
||||
expect(article.shared_article_changed?).to be_falsey
|
||||
end
|
||||
|
||||
it 'adapts to foodcoop units when synchronising' do
|
||||
shared_article.unit = '1kg'
|
||||
shared_article.unit_quantity = 1
|
||||
shared_article.save!
|
||||
article = shared_article.build_new_article(supplier)
|
||||
article.article_category = create :article_category
|
||||
article.unit = '200g'
|
||||
article.shared_updated_on -= 1 # to make update do something
|
||||
article.save!
|
||||
# TODO: get sync functionality in article
|
||||
updated_article = supplier.sync_all[0].select { |s| s[0].id == article.id }.first[0]
|
||||
article.update!(updated_article.attributes.reject { |k, _v| %w[id type].include?(k) })
|
||||
expect(article.unit).to eq '200g'
|
||||
expect(article.unit_quantity).to eq 5
|
||||
expect(article.price).to be_within(0.005).of(shared_article.price / 5)
|
||||
end
|
||||
|
||||
it 'does not synchronise when it has no order number' do
|
||||
article.update(order_number: nil)
|
||||
expect(supplier.sync_all).to eq [[], [], []]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
require_relative '../spec_helper'
|
||||
|
||||
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 }
|
||||
|
||||
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, /.*/)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'valid group order invoice' do
|
||||
before do
|
||||
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(:user2) { create :user, groups: [create(:ordergroup)] }
|
||||
|
||||
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_order2) { create :group_order, order: order, ordergroup: user2.ordergroup }
|
||||
|
||||
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(goi1).to be_valid
|
||||
end
|
||||
|
||||
it 'sets invoice_number according to date' do
|
||||
number = Time.now.strftime("%Y%m%d") + '0001'
|
||||
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(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(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
|
||||
goi1
|
||||
expect { goi4 }.to raise_error(ActiveRecord::RecordInvalid)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue