dorsale 3.14.4 → 3.14.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +3 -0
  3. data/features/access.feature +17 -0
  4. data/features/billing_machine_invoices.feature +261 -0
  5. data/features/billing_machine_multiple_vat.feature +53 -0
  6. data/features/billing_machine_payment_terms.feature +19 -0
  7. data/features/billing_machine_quotations.feature +193 -0
  8. data/features/customer_vault_activity_types.feature +19 -0
  9. data/features/customer_vault_corporations.feature +95 -0
  10. data/features/customer_vault_filters.feature +14 -0
  11. data/features/customer_vault_individuals.feature +35 -0
  12. data/features/customer_vault_invoices.feature +10 -0
  13. data/features/customer_vault_links.feature +54 -0
  14. data/features/customer_vault_origins.feature +19 -0
  15. data/features/customer_vault_people.feature +25 -0
  16. data/features/customer_vault_search.feature +22 -0
  17. data/features/customer_vault_tasks.feature +35 -0
  18. data/features/expense_gun_categories.feature +19 -0
  19. data/features/expense_gun_expenses.feature +60 -0
  20. data/features/flyboy_task_comments.feature +21 -0
  21. data/features/flyboy_tasks.feature +122 -0
  22. data/features/flyboy_tasks_summary.feature +22 -0
  23. data/features/step_definitions/access_steps.rb +15 -0
  24. data/features/step_definitions/alexandrie_steps.rb +25 -0
  25. data/features/step_definitions/billing_machine_invoices_steps.rb +377 -0
  26. data/features/step_definitions/billing_machine_multiple_vat.rb +19 -0
  27. data/features/step_definitions/billing_machine_payment_terms_steps.rb +42 -0
  28. data/features/step_definitions/billing_machine_quotations_steps.rb +248 -0
  29. data/features/step_definitions/common_steps.rb +91 -0
  30. data/features/step_definitions/customer_vault_activity_types_steps.rb +42 -0
  31. data/features/step_definitions/customer_vault_corporations_steps.rb +218 -0
  32. data/features/step_definitions/customer_vault_filters_steps.rb +37 -0
  33. data/features/step_definitions/customer_vault_individuals_steps.rb +67 -0
  34. data/features/step_definitions/customer_vault_invoices_steps.rb +7 -0
  35. data/features/step_definitions/customer_vault_links_steps.rb +73 -0
  36. data/features/step_definitions/customer_vault_origins_steps.rb +42 -0
  37. data/features/step_definitions/customer_vault_people_steps.rb +26 -0
  38. data/features/step_definitions/customer_vault_search_steps.rb +48 -0
  39. data/features/step_definitions/customer_vault_tasks_steps.rb +34 -0
  40. data/features/step_definitions/expense_gun_categories_steps.rb +50 -0
  41. data/features/step_definitions/expense_gun_expenses_steps.rb +135 -0
  42. data/features/step_definitions/flyboy_task_comments_steps.rb +10 -0
  43. data/features/step_definitions/flyboy_tasks_steps.rb +196 -0
  44. data/features/step_definitions/tasks_summary_steps.rb +30 -0
  45. data/features/step_definitions/users_management_step.rb +29 -0
  46. data/features/support/env.rb +8 -0
  47. data/features/users_management.feature +18 -0
  48. data/lib/dorsale/version.rb +1 -1
  49. data/spec/controllers/dorsale/alexandrie/attachments_controller_spec.rb +145 -0
  50. data/spec/controllers/dorsale/billing_machine/invoices_controller_spec.rb +153 -0
  51. data/spec/controllers/dorsale/billing_machine/quotations_controller_spec.rb +24 -0
  52. data/spec/controllers/dorsale/comments_controller_spec.rb +50 -0
  53. data/spec/controllers/dorsale/customer_vault/events_controller_spec.rb +42 -0
  54. data/spec/controllers/dorsale/customer_vault/people_controller_spec.rb +129 -0
  55. data/spec/controllers/dorsale/expense_gun/expenses_controller_spec.rb +55 -0
  56. data/spec/controllers/dorsale/flyboy/task_comments_controller_spec.rb +40 -0
  57. data/spec/controllers/dorsale/flyboy/tasks_controller_spec.rb +349 -0
  58. data/spec/factories/alexandrie_attachment_types.rb +5 -0
  59. data/spec/factories/alexandrie_attachments.rb +16 -0
  60. data/spec/factories/billing_machine_invoice_lines.rb +9 -0
  61. data/spec/factories/billing_machine_invoices.rb +9 -0
  62. data/spec/factories/billing_machine_payment_terms.rb +5 -0
  63. data/spec/factories/billing_machine_quotation_lines.rb +9 -0
  64. data/spec/factories/billing_machine_quotations.rb +9 -0
  65. data/spec/factories/customer_vault_activity_types.rb +5 -0
  66. data/spec/factories/customer_vault_corporations.rb +13 -0
  67. data/spec/factories/customer_vault_event.rb +13 -0
  68. data/spec/factories/customer_vault_individuals.rb +17 -0
  69. data/spec/factories/customer_vault_links.rb +7 -0
  70. data/spec/factories/customer_vault_origins.rb +5 -0
  71. data/spec/factories/dorsale_addresses.rb +9 -0
  72. data/spec/factories/dorsale_comments.rb +7 -0
  73. data/spec/factories/expense_gun_categories.rb +7 -0
  74. data/spec/factories/expense_gun_expense_lines.rb +11 -0
  75. data/spec/factories/expense_gun_expenses.rb +13 -0
  76. data/spec/factories/flyboy_task_comments.rb +9 -0
  77. data/spec/factories/flyboy_tasks.rb +8 -0
  78. data/spec/files/avatar.png +0 -0
  79. data/spec/files/pdf.pdf +0 -0
  80. data/spec/mailers/user_mailer_spec.rb +21 -0
  81. data/spec/models/dorsale/address_spec.rb +46 -0
  82. data/spec/models/dorsale/alexandrie/attachment_spec.rb +36 -0
  83. data/spec/models/dorsale/alexandrie/attachment_type_spec.rb +11 -0
  84. data/spec/models/dorsale/billing_machine/invoice_line_spec.rb +52 -0
  85. data/spec/models/dorsale/billing_machine/invoice_spec.rb +258 -0
  86. data/spec/models/dorsale/billing_machine/quotation_line_spec.rb +51 -0
  87. data/spec/models/dorsale/billing_machine/quotation_spec.rb +213 -0
  88. data/spec/models/dorsale/billing_machine_spec.rb +57 -0
  89. data/spec/models/dorsale/comment_spec.rb +24 -0
  90. data/spec/models/dorsale/customer_vault/activity_type_spec.rb +10 -0
  91. data/spec/models/dorsale/customer_vault/corporation_spec.rb +33 -0
  92. data/spec/models/dorsale/customer_vault/event_spec.rb +22 -0
  93. data/spec/models/dorsale/customer_vault/individual_spec.rb +44 -0
  94. data/spec/models/dorsale/customer_vault/link_spec.rb +14 -0
  95. data/spec/models/dorsale/customer_vault/origin_spec.rb +8 -0
  96. data/spec/models/dorsale/customer_vault/person_spec.rb +112 -0
  97. data/spec/models/dorsale/email_spec.rb +39 -0
  98. data/spec/models/dorsale/expense_gun/category_spec.rb +9 -0
  99. data/spec/models/dorsale/expense_gun/expense_line_spec.rb +53 -0
  100. data/spec/models/dorsale/expense_gun/expense_spec.rb +176 -0
  101. data/spec/models/dorsale/flyboy/task_comment_spec.rb +54 -0
  102. data/spec/models/dorsale/flyboy/task_spec.rb +254 -0
  103. data/spec/models/dorsale/i18n_spec.rb +30 -0
  104. data/spec/models/dorsale/users_spec.rb +65 -0
  105. data/spec/pdfs/dorsale/billing_machine/invoice_multiple_vat_pdf_spec.rb +42 -0
  106. data/spec/pdfs/dorsale/billing_machine/invoice_single_vat_pdf_spec.rb +42 -0
  107. data/spec/pdfs/dorsale/billing_machine/quotation_multiple_vat_pdf_spec.rb +42 -0
  108. data/spec/pdfs/dorsale/billing_machine/quotation_single_vat_pdf_spec.rb +55 -0
  109. data/spec/pdfs/dorsale/expense_gun/expense_pdf_spec.rb +11 -0
  110. data/spec/rails_helper.rb +57 -0
  111. data/spec/routing/dorsale/alexandrie/attachments_routing_spec.rb +32 -0
  112. data/spec/routing/dorsale/billing_machine/invoices_routing_spec.rb +67 -0
  113. data/spec/routing/dorsale/billing_machine/quotations_routing_spec.rb +67 -0
  114. data/spec/routing/dorsale/comments_routing_spec.rb +23 -0
  115. data/spec/routing/dorsale/customer_vault/activity_types_routing_spec.rb +40 -0
  116. data/spec/routing/dorsale/customer_vault/events_routing_spec.rb +12 -0
  117. data/spec/routing/dorsale/customer_vault/origins_routing_spec.rb +40 -0
  118. data/spec/routing/dorsale/customer_vault/people_routing_spec.rb +109 -0
  119. data/spec/routing/dorsale/expense_gun/expenses_routing_spec.rb +62 -0
  120. data/spec/routing/dorsale/flyboy/task_comments_routing_spec.rb +11 -0
  121. data/spec/routing/dorsale/flyboy/tasks_routing_spec.rb +51 -0
  122. data/spec/routing/dorsale/users_routing_spec.rb +38 -0
  123. data/spec/services/dorsale/billing_machine/quotation/copy_spec.rb +55 -0
  124. data/spec/services/dorsale/billing_machine/quotation/statistics.rb +23 -0
  125. data/spec/services/dorsale/billing_machine/quotation/to_invoice_spec.rb +46 -0
  126. data/spec/services/dorsale/expense_gun/expense/copy_spec.rb +83 -0
  127. data/spec/services/dorsale/flyboy/task/copy_spec.rb +24 -0
  128. data/spec/services/dorsale/flyboy/task/snoozer_spec.rb +117 -0
  129. data/spec/spec_helper.rb +86 -0
  130. data/spec/support/devise.rb +3 -0
  131. data/spec/support/message_delivery.rb +5 -0
  132. metadata +258 -2
@@ -0,0 +1,9 @@
1
+ FactoryBot.define do
2
+ factory :billing_machine_quotation, class: ::Dorsale::BillingMachine::Quotation do
3
+ customer { create(:customer_vault_corporation) }
4
+ payment_term { create(:billing_machine_payment_term) }
5
+ date { "2014-02-19" }
6
+ label { "Software service" }
7
+ comments { "This is the quotation for the software your companie want" }
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ FactoryBot.define do
2
+ factory :customer_vault_activity_type, class: ::Dorsale::CustomerVault::ActivityType do
3
+ name { Faker::Lorem.word }
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ FactoryBot.define do
2
+ factory :customer_vault_corporation, class: ::Dorsale::CustomerVault::Corporation do
3
+ activity_type { create(:customer_vault_activity_type) }
4
+ origin { create(:customer_vault_origin) }
5
+ name { "#{Faker::Company.name} #{Faker::Company.suffix}" }
6
+ short_name { "SN" }
7
+ email { Faker::Internet.email }
8
+ phone { "06 xx xx xx xx" }
9
+ fax { "09 xx xx xx xx" }
10
+ www { Faker::Internet.url }
11
+ address { build(:dorsale_address) }
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ FactoryBot.define do
2
+ factory :customer_vault_event, class: ::Dorsale::CustomerVault::Event do
3
+ author { build(:user) }
4
+ person { build(:customer_vault_corporation) }
5
+ action { "create" }
6
+ text { "Création de la personne" }
7
+ end
8
+
9
+ factory :customer_vault_event_comment, parent: :customer_vault_event do
10
+ action { "comment" }
11
+ text { "Je suis un commentaire" }
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ FactoryBot.define do
2
+ factory :customer_vault_individual, class: ::Dorsale::CustomerVault::Individual do
3
+ origin { create(:customer_vault_origin) }
4
+ first_name { Faker::Name.first_name }
5
+ last_name { Faker::Name.last_name }
6
+ short_name { "SN" }
7
+ email { Faker::Internet.email("#{first_name} #{last_name}") }
8
+ title { "Individual-Title" }
9
+ twitter { "#{first_name}#{last_name}" }
10
+ www { Faker::Internet.url }
11
+ context { "Individual-Context" }
12
+ phone { "01 23 xx xx xx" }
13
+ fax { "09 xx xx xx xx" }
14
+ mobile { "06 xx xx xx xx" }
15
+ address { build(:dorsale_address) }
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ FactoryBot.define do
2
+ factory :customer_vault_link, class: ::Dorsale::CustomerVault::Link do
3
+ association :alice, factory: :customer_vault_individual
4
+ association :bob, factory: :customer_vault_corporation
5
+ title { "Manager" }
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ FactoryBot.define do
2
+ factory :customer_vault_origin, class: ::Dorsale::CustomerVault::Origin do
3
+ name { Faker::Lorem.word }
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ FactoryBot.define do
2
+ factory :dorsale_address, class: ::Dorsale::Address do
3
+ street { "3 Rue Marx Dormoy" }
4
+ street_bis { "1er étage" }
5
+ city { "Marseille" }
6
+ zip { "13004" }
7
+ country { "France" }
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ FactoryBot.define do
2
+ factory :dorsale_comment, class: ::Dorsale::Comment do
3
+ commentable { create(:customer_vault_corporation) }
4
+ author { create(:user) }
5
+ text { Faker::Lorem.paragraph }
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ FactoryBot.define do
2
+ factory :expense_gun_category, class: ::Dorsale::ExpenseGun::Category do
3
+ name { Faker::Lorem.word }
4
+ code { Faker::Number.number(4) }
5
+ vat_deductible { [true, false].sample }
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ FactoryBot.define do
2
+ factory :expense_gun_expense_line, class: ::Dorsale::ExpenseGun::ExpenseLine do
3
+ expense { build(:expense_gun_expense) }
4
+ name { Faker::Lorem.sentence(3) }
5
+ category { build(:expense_gun_category) }
6
+ date { Faker::Date.backward(30) }
7
+ total_all_taxes { rand(100..1000) }
8
+ vat { rand(1..(total_all_taxes/5)) }
9
+ company_part { [25, 50, 75, 100].sample }
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ FactoryBot.define do
2
+ factory :expense_gun_expense, class: ::Dorsale::ExpenseGun::Expense do
3
+ name { Faker::Lorem.sentence(3) }
4
+ date { Date.current }
5
+ user { create(:user) }
6
+
7
+ after(:create) { |expense|
8
+ rand(2..5).times {
9
+ create(:expense_gun_expense_line, expense: expense)
10
+ }
11
+ }
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ FactoryBot.define do
2
+ factory :flyboy_task_comment, class: ::Dorsale::Flyboy::TaskComment do
3
+ task { create(:flyboy_task) }
4
+ date { Time.zone.now }
5
+ progress { 95 }
6
+ description { "I-am-a-task-comment" }
7
+ author { create(:user) }
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ FactoryBot.define do
2
+ factory :flyboy_task, class: ::Dorsale::Flyboy::Task do
3
+ name { "I-am-a-task#{Kernel.rand(1_000..9_999)}" }
4
+ description { "I-am-the-task-description_#{Kernel.rand(1_000..9_999)}" }
5
+ progress { Kernel.rand(0..99) }
6
+ done { false }
7
+ end
8
+ end
Binary file
Binary file
@@ -0,0 +1,21 @@
1
+ require "rails_helper"
2
+
3
+ describe ::Dorsale::UserMailer do
4
+ describe "New Account" do
5
+ let(:user) { create(:user) }
6
+ let(:email) { ::Dorsale::UserMailer.new_account(user, user.password) }
7
+
8
+ it "should send to the right person" do
9
+ expect(email.to).to eq([user.email])
10
+ end
11
+
12
+ it "should have the right sender" do
13
+ expect(email.from).to eq(["example@example.org"])
14
+ end
15
+
16
+ it "should contain user_type, login and password" do
17
+ expect(email.body).to include user.email
18
+ expect(email.body).to include user.password
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,46 @@
1
+ require "rails_helper"
2
+
3
+ module Dorsale
4
+ RSpec.describe Address, :type => :model do
5
+ it { should belong_to :addressable }
6
+ it { should validate_presence_of :addressable }
7
+
8
+ describe "#one_line" do
9
+ it "should build a one line address" do
10
+ address = Address.new(
11
+ street: "3 Rue Marx Dormoy",
12
+ street_bis: "L'atelier",
13
+ zip: "13004",
14
+ city: "Marseille",
15
+ country: "France",
16
+ )
17
+
18
+ expect(address.one_line).to eq("3 Rue Marx Dormoy, L'atelier, 13004 Marseille, France")
19
+ end
20
+
21
+ it "should remove useless commas" do
22
+ address = Address.new(
23
+ street: "3 Rue Marx Dormoy",
24
+ street_bis: "",
25
+ zip: "13004",
26
+ city: "Marseille",
27
+ country: "",
28
+ )
29
+
30
+ expect(address.one_line).to eq("3 Rue Marx Dormoy, 13004 Marseille")
31
+ end
32
+
33
+ it "should remove useless commas" do
34
+ address = Address.new(
35
+ street: "3 Rue Marx Dormoy",
36
+ street_bis: "",
37
+ zip: "",
38
+ city: "Marseille",
39
+ country: "",
40
+ )
41
+
42
+ expect(address.one_line).to eq("3 Rue Marx Dormoy, Marseille")
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,36 @@
1
+ require "rails_helper"
2
+
3
+ describe ::Dorsale::Alexandrie::Attachment, type: :model do
4
+ it { is_expected.to belong_to :attachable }
5
+ it { is_expected.to belong_to :sender }
6
+ it { is_expected.to belong_to :attachment_type }
7
+
8
+ it { is_expected.to validate_presence_of :attachable }
9
+ it { is_expected.to validate_presence_of :file }
10
+
11
+ it { is_expected.to_not validate_presence_of :attachment_type }
12
+ it { is_expected.to_not validate_presence_of :sender }
13
+
14
+ it "factory should be valid" do
15
+ expect(create(:alexandrie_attachment)).to be_valid
16
+ end
17
+
18
+ it "should assign default name" do
19
+ attachment = create(:alexandrie_attachment, name: nil)
20
+ expect(attachment.name).to eq "pdf.pdf"
21
+ end
22
+
23
+ it "download_filename should be valid" do
24
+ attachment = create(:alexandrie_attachment)
25
+ expect(attachment.download_filename).to eq "pdf.pdf"
26
+
27
+ attachment = create(:alexandrie_attachment, name: "hello")
28
+ expect(attachment.download_filename).to eq "hello.pdf"
29
+
30
+ attachment = create(:alexandrie_attachment, name: "hello.pdf")
31
+ expect(attachment.download_filename).to eq "hello.pdf"
32
+
33
+ attachment = create(:alexandrie_attachment, name: "hello.xls")
34
+ expect(attachment.download_filename).to eq "hello-xls.pdf"
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ require "rails_helper"
2
+
3
+ describe ::Dorsale::Alexandrie::AttachmentType, type: :model do
4
+ it { is_expected.to have_many(:attachments).dependent(:nullify) }
5
+
6
+ it { is_expected.to validate_presence_of :name }
7
+
8
+ it "factory should be valid" do
9
+ expect(create(:alexandrie_attachment_type)).to be_valid
10
+ end
11
+ end
@@ -0,0 +1,52 @@
1
+ require "rails_helper"
2
+
3
+ describe Dorsale::BillingMachine::InvoiceLine, type: :model do
4
+ it { is_expected.to belong_to :invoice }
5
+ it { is_expected.to validate_presence_of :invoice }
6
+
7
+ it { is_expected.to respond_to :quantity }
8
+ it { is_expected.to respond_to :label }
9
+ it { is_expected.to respond_to :vat_rate }
10
+ it { is_expected.to respond_to :total }
11
+ it { is_expected.to respond_to :unit }
12
+ it { is_expected.to respond_to :unit_price }
13
+
14
+ it "should have a valid factory" do
15
+ expect(build(:billing_machine_invoice_line)).to be_valid
16
+ end
17
+
18
+ describe "default values" do
19
+ it "quantity should be 0" do
20
+ expect(described_class.new.quantity).to eq 0
21
+ end
22
+
23
+ it "unit_price should be 0" do
24
+ expect(described_class.new.unit_price).to eq 0
25
+ end
26
+
27
+ it "vat_rate should be 0" do
28
+ expect(described_class.new.vat_rate).to eq Dorsale::BillingMachine.default_vat_rate
29
+ end
30
+ end
31
+
32
+ it "should be sorted by created_at" do
33
+ line1 = create(:billing_machine_invoice_line, :created_at => Time.zone.now + 1.minute)
34
+ line2 = create(:billing_machine_invoice_line, :created_at => Time.zone.now + 2.minutes)
35
+ line3 = create(:billing_machine_invoice_line, :created_at => Time.zone.now + 3.minutes)
36
+ line4 = create(:billing_machine_invoice_line, :created_at => Time.zone.now + 4.minutes)
37
+ line3.update!(:created_at => Time.zone.now + 5.minutes)
38
+
39
+ lines = described_class.all
40
+ expect(lines).to eq [line1, line2, line4, line3]
41
+ end
42
+
43
+ it "should update the total upon save" do
44
+ line = create(:billing_machine_invoice_line, quantity: 12.34, unit_price: 12.34, total: 0)
45
+ expect(line.total.to_f).to eq 152.28
46
+ end
47
+
48
+ it "should update the total gracefully with invalid data" do
49
+ line = create(:billing_machine_invoice_line, quantity: nil, unit_price: nil, total: 0)
50
+ expect(line.total.to_f).to eq 0
51
+ end
52
+ end
@@ -0,0 +1,258 @@
1
+ require "rails_helper"
2
+
3
+ describe Dorsale::BillingMachine::Invoice, type: :model do
4
+ # Reset to default mode before each test
5
+ before :each do
6
+ ::Dorsale::BillingMachine.vat_mode = :single
7
+ end
8
+
9
+ it { is_expected.to belong_to :customer }
10
+ it { is_expected.to belong_to :payment_term }
11
+ it { is_expected.to have_many(:lines).dependent(:destroy) }
12
+
13
+ it { is_expected.to validate_presence_of :date }
14
+
15
+ it { is_expected.to respond_to :date }
16
+ it { is_expected.to respond_to :label }
17
+ it { is_expected.to respond_to :vat_amount }
18
+ it { is_expected.to respond_to :unique_index }
19
+ it { is_expected.to respond_to :commercial_discount }
20
+
21
+ it { is_expected.to respond_to :total_excluding_taxes }
22
+ it { is_expected.to respond_to :total_including_taxes }
23
+
24
+ it { is_expected.to respond_to :advance }
25
+
26
+ it "should have a valid factory" do
27
+ expect(create(:billing_machine_invoice)).to be_valid
28
+ end
29
+
30
+ it "should return document_type" do
31
+ expect(described_class.new.document_type).to eq :invoice
32
+ end
33
+
34
+ describe "unique_index" do
35
+ context "when unique index is 69" do
36
+ it "should be assigned upon creation" do
37
+ invoice1 = create(:billing_machine_invoice, date: "2014-02-01", unique_index: 69)
38
+ invoice2 = create(:billing_machine_invoice, date: "2014-02-01")
39
+ expect(invoice2.unique_index).to eq(70)
40
+ end
41
+ end
42
+
43
+ context "when unique index is nil" do
44
+ it "should be assigned upon creation" do
45
+ described_class.destroy_all
46
+ invoice1 = create(:billing_machine_invoice, date: "2014-02-01")
47
+ expect(invoice1.unique_index).to eq(1)
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "tracking_id" do
53
+ it "should return correct tracking_id" do
54
+ invoice = create(:billing_machine_invoice, date: "2014-02-01")
55
+ expect(invoice.tracking_id).to eq("2014-01")
56
+ end
57
+ end
58
+
59
+ describe "payment_status" do
60
+ it "should be pending if nothing special" do
61
+ invoice = create(:billing_machine_invoice,
62
+ :due_date => Date.current,
63
+ :date => Date.current,
64
+ :paid => false,
65
+ )
66
+ expect(invoice.payment_status).to eq(:pending)
67
+ end
68
+
69
+ it "should be late if a bit late" do
70
+ invoice = create(:billing_machine_invoice,
71
+ :due_date => Date.current-1,
72
+ :date => Date.current-1,
73
+ :paid => false,
74
+ )
75
+ expect(invoice.payment_status).to eq(:late)
76
+ end
77
+
78
+ it "should be on_alert if a too late" do
79
+ invoice = create(:billing_machine_invoice,
80
+ :due_date => Date.current-16,
81
+ :date => Date.current-16,
82
+ :paid => false,
83
+ )
84
+ expect(invoice.payment_status).to eq(:on_alert)
85
+ end
86
+
87
+ it "should be paid if paid" do
88
+ invoice = create(:billing_machine_invoice,
89
+ :due_date => Date.current-16,
90
+ :date => Date.current-16,
91
+ :paid => true,
92
+ )
93
+ expect(invoice.payment_status).to eq(:paid)
94
+ end
95
+
96
+ it "should be on_alert if no due date is defined" do
97
+ invoice = create(:billing_machine_invoice, due_date: nil, date: Date.current, paid: false)
98
+ expect(invoice.payment_status).to eq(:on_alert)
99
+ end
100
+
101
+ it "should be on_alert if no due date is defined" do
102
+ invoice = create(:billing_machine_invoice, due_date: nil, date: Date.current, paid: true)
103
+ expect(invoice.payment_status).to eq(:paid)
104
+ end
105
+
106
+ it "should work fine upon creation" do
107
+ invoice = build(:billing_machine_invoice)
108
+ invoice.lines << ::Dorsale::BillingMachine::InvoiceLine.new(quantity: 1, unit_price: 10)
109
+ invoice.lines << ::Dorsale::BillingMachine::InvoiceLine.new(quantity: 1, unit_price: 10)
110
+ invoice.save!
111
+ end
112
+ end
113
+
114
+ describe "paid" do
115
+ it "should be false by default" do
116
+ invoice = create(:billing_machine_invoice)
117
+ expect(invoice.paid).to eq(false)
118
+ end
119
+ end
120
+
121
+ describe "vat rate" do
122
+ it "default vat rate should be 20" do
123
+ expect(described_class.new.vat_rate).to eq ::Dorsale::BillingMachine.default_vat_rate
124
+ end
125
+
126
+ it "it should be specified vat rate" do
127
+ expect(build(:billing_machine_invoice, vat_rate: 12).vat_rate).to eq 12
128
+ end
129
+
130
+ it "it should be first line vat rate" do
131
+ invoice = create(:billing_machine_invoice)
132
+ line1 = create(:billing_machine_invoice_line, invoice: invoice, vat_rate: 10)
133
+ line2 = create(:billing_machine_invoice_line, invoice: invoice, vat_rate: 10)
134
+ expect(invoice.vat_rate).to eq 10
135
+ end
136
+
137
+ it "it should raise if multiple vat_rates" do
138
+ invoice = create(:billing_machine_invoice)
139
+ line1 = create(:billing_machine_invoice_line, invoice: invoice, vat_rate: 10)
140
+
141
+ expect {
142
+ line2 = create(:billing_machine_invoice_line, invoice: invoice, vat_rate: 15)
143
+ }.to raise_error(RuntimeError)
144
+ end
145
+
146
+ it "it should raise when vat mode is multiple" do
147
+ ::Dorsale::BillingMachine.vat_mode = :multiple
148
+ invoice = build(:billing_machine_invoice)
149
+ expect { invoice.vat_rate }.to raise_error(RuntimeError)
150
+ end
151
+ end
152
+
153
+ describe "totals" do
154
+ it "should be calculated upon saving with advance" do
155
+ invoice = create(:billing_machine_invoice,
156
+ :commercial_discount => 10,
157
+ :advance => 40,
158
+ )
159
+
160
+ create(:billing_machine_invoice_line,
161
+ :quantity => 10,
162
+ :unit_price => 5,
163
+ :vat_rate => 20,
164
+ :invoice => invoice,
165
+ ) # total 50
166
+
167
+ create(:billing_machine_invoice_line,
168
+ :quantity => 10,
169
+ :unit_price => 5,
170
+ :vat_rate => 20,
171
+ :invoice => invoice,
172
+ ) # total 50
173
+
174
+ expect(invoice.total_excluding_taxes).to eq(90.0)
175
+ expect(invoice.vat_amount).to eq(18)
176
+ expect(invoice.total_including_taxes).to eq(108)
177
+ expect(invoice.balance).to eq(68)
178
+ end
179
+
180
+ it "should be calculated upon saving without advance" do
181
+ invoice = create(:billing_machine_invoice,
182
+ :commercial_discount => 10,
183
+ )
184
+
185
+ create(:billing_machine_invoice_line,
186
+ :quantity => 10,
187
+ :unit_price => 5,
188
+ :vat_rate => 20,
189
+ :invoice => invoice,
190
+ ) # total 50
191
+
192
+ create(:billing_machine_invoice_line,
193
+ :quantity => 10,
194
+ :unit_price => 5,
195
+ :vat_rate => 20,
196
+ :invoice => invoice,
197
+ ) # total 50
198
+
199
+ expect(invoice.total_excluding_taxes).to eq(90.0)
200
+ expect(invoice.vat_amount).to eq(18)
201
+ expect(invoice.total_including_taxes).to eq(108)
202
+ expect(invoice.balance).to eq(108)
203
+ end
204
+
205
+ describe "VAT round" do
206
+ let(:invoice) {
207
+ invoice = create(:billing_machine_invoice,
208
+ :commercial_discount => 0,
209
+ )
210
+
211
+ create(:billing_machine_invoice_line,
212
+ :quantity => 12.34,
213
+ :unit_price => 12.34,
214
+ :vat_rate => 20,
215
+ :invoice => invoice,
216
+ ) # vat 30.45512 / total 152.2756
217
+
218
+ create(:billing_machine_invoice_line,
219
+ :quantity => 12.34,
220
+ :unit_price => 12.34,
221
+ :vat_rate => 20,
222
+ :invoice => invoice,
223
+ ) # vat 30.45512 / total 152.2756
224
+
225
+ invoice
226
+ }
227
+
228
+ after { Dorsale::BillingMachine.vat_round_by_line = nil }
229
+
230
+ it "should round VAT by line" do
231
+ Dorsale::BillingMachine.vat_round_by_line = true
232
+ expect(invoice.total_excluding_taxes.to_f).to eq(304.56)
233
+ expect(invoice.vat_amount.to_f).to eq(60.92)
234
+ expect(invoice.total_including_taxes.to_f).to eq(365.48)
235
+ expect(invoice.balance.to_f).to eq(365.48)
236
+ end
237
+
238
+ it "should round VAT globally" do
239
+ Dorsale::BillingMachine.vat_round_by_line = false
240
+ expect(invoice.total_excluding_taxes.to_f).to eq(304.56)
241
+ expect(invoice.vat_amount.to_f).to eq(60.91)
242
+ expect(invoice.total_including_taxes.to_f).to eq(365.47)
243
+ expect(invoice.balance.to_f).to eq(365.47)
244
+ end
245
+ end # describe "VAT round"
246
+
247
+ it "should work fine even with empty lines" do
248
+ invoice = create(:billing_machine_invoice)
249
+
250
+ create(:billing_machine_invoice_line, invoice: invoice, quantity: nil, unit_price: nil)
251
+
252
+ expect(invoice.total_excluding_taxes).to eq(0.0)
253
+ expect(invoice.vat_amount).to eq(0)
254
+ expect(invoice.total_including_taxes).to eq(0)
255
+ expect(invoice.balance).to eq(0)
256
+ end
257
+ end
258
+ end