diff --git a/app/controllers/registrar/deposits_controller.rb b/app/controllers/registrar/deposits_controller.rb index 6cf33f1c1..1f1bafd00 100644 --- a/app/controllers/registrar/deposits_controller.rb +++ b/app/controllers/registrar/deposits_controller.rb @@ -9,7 +9,7 @@ class Registrar::DepositsController < RegistrarController @deposit = Deposit.new(deposit_params.merge(registrar: current_user.registrar)) @invoice = @deposit.issue_prepayment_invoice - if @invoice + if @invoice.persisted? flash[:notice] = t('please_pay_the_following_invoice') redirect_to [:registrar, @invoice] else diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 9c1f1484b..405fa9873 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -13,6 +13,23 @@ class Invoice < ActiveRecord::Base validates :invoice_type, :due_date, :currency, :seller_name, :seller_iban, :buyer_name, :invoice_items, :vat_prc, presence: true + before_save :set_invoice_number + def set_invoice_number + last_no = Invoice.order(number: :desc).where('number IS NOT NULL').limit(1).pluck(:number).first + + if last_no + self.number = last_no + 1 + else + self.number = Setting.invoice_number_min.to_i + end + + return if number <= Setting.invoice_number_max.to_i + + errors.add(:base, I18n.t('failed_to_generate_invoice')) + logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE') + false + end + def binded? account_activity.present? end @@ -25,11 +42,6 @@ class Invoice < ActiveRecord::Base I18n.t('invoice_no', no: number) end - def number - # TODO: Real invoice numbers here - id - end - def seller_address [seller_street, seller_city, seller_state, seller_zip].reject(&:blank?).compact.join(', ') end diff --git a/app/views/admin/invoices/index.haml b/app/views/admin/invoices/index.haml index 8a53da5cb..e1c571d59 100644 --- a/app/views/admin/invoices/index.haml +++ b/app/views/admin/invoices/index.haml @@ -19,7 +19,7 @@ %tbody - @invoices.each do |x| %tr - %td= link_to(t('invoice_no', no: x.id), [:admin, x]) + %td= link_to(x, [:admin, x]) %td= link_to(x.buyer_name, admin_registrar_path(x.buyer_id)) %td= l(x.due_date) - if x.binded? diff --git a/app/views/admin/invoices/show.haml b/app/views/admin/invoices/show.haml index 9959dd88d..6f683c15d 100644 --- a/app/views/admin/invoices/show.haml +++ b/app/views/admin/invoices/show.haml @@ -1,7 +1,7 @@ .row .col-sm-6 %h1.text-center-xs - = t('invoice_no', no: @invoice.id) + = @invoice .col-sm-6 %h1.text-right.text-center-xs = link_to(t('back'), :back, class: 'btn btn-default') diff --git a/app/views/registrar/invoices/index.haml b/app/views/registrar/invoices/index.haml index 206f554ca..91673e13c 100644 --- a/app/views/registrar/invoices/index.haml +++ b/app/views/registrar/invoices/index.haml @@ -3,8 +3,8 @@ = link_to(t(:account_activity), registrar_account_activities_path, class: 'btn btn-default') = render 'shared/title', name: t(:your_account) -= t(:your_current_account_balance_is, - balance: current_user.registrar.cash_account.balance, += t(:your_current_account_balance_is, + balance: current_user.registrar.cash_account.balance, currency: current_user.registrar.cash_account.currency) %h1= t(:invoices) @@ -22,7 +22,7 @@ %tbody - @invoices.each do |x| %tr - %td= link_to(t(:invoice_no, no: x.id), [:registrar, x]) + %td= link_to(x, [:registrar, x]) - if x.receipt_date %td= l(x.receipt_date) - else diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index 486c38283..0f812b6a2 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -137,7 +137,7 @@ .col-sm-6.left #header-content %h1 - = t(:invoice_no, no: @invoice.id) + = @invoice .col-sm-6.right %img{src: "#{Rails.root}/public/eis-logo-black-et.png"} .clear diff --git a/app/views/registrar/invoices/show.haml b/app/views/registrar/invoices/show.haml index 6646d3d99..4b1943a45 100644 --- a/app/views/registrar/invoices/show.haml +++ b/app/views/registrar/invoices/show.haml @@ -2,7 +2,7 @@ = link_to(t(:download), download_pdf_registrar_invoice_path(@invoice), class: 'btn btn-default') = link_to(t(:forward_invoice), forward_registrar_invoice_path(@invoice), class: 'btn btn-default') = link_to(t(:back), :back, class: 'btn btn-default') -= render 'shared/title', name: t(:invoice_no, no: @invoice.id) += render 'shared/title', name: @invoice.to_s .row .col-md-6= render 'registrar/invoices/partials/details' diff --git a/config/initializers/initial_settings.rb b/config/initializers/initial_settings.rb index a09c83424..9b1322ddf 100644 --- a/config/initializers/initial_settings.rb +++ b/config/initializers/initial_settings.rb @@ -28,6 +28,8 @@ if con.present? && con.table_exists?('settings') Setting.save_default(:eis_bank, 'LHV Pank') Setting.save_default(:eis_swift, 'LHVBEE22') Setting.save_default(:eis_invoice_contact, 'Martti Õigus') + Setting.save_default(:invoice_number_min, '131050') + Setting.save_default(:invoice_number_max, '149999') end # dev only setting diff --git a/db/migrate/20150423083308_add_number_to_invoices.rb b/db/migrate/20150423083308_add_number_to_invoices.rb new file mode 100644 index 000000000..6e0f5ed4a --- /dev/null +++ b/db/migrate/20150423083308_add_number_to_invoices.rb @@ -0,0 +1,6 @@ +class AddNumberToInvoices < ActiveRecord::Migration + def change + add_column :invoices, :number, :integer + Invoice.all.each(&:save) + end +end diff --git a/db/schema.rb b/db/schema.rb index 0668ebb76..755d6eecc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150422132631) do +ActiveRecord::Schema.define(version: 20150423083308) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -367,6 +367,7 @@ ActiveRecord::Schema.define(version: 20150422132631) do t.string "buyer_email" t.string "creator_str" t.string "updator_str" + t.integer "number" end add_index "invoices", ["buyer_id"], name: "index_invoices_on_buyer_id", using: :btree