diff --git a/app/controllers/admin/bank_transactions_controller.rb b/app/controllers/admin/bank_transactions_controller.rb index 1aaf452a8..7fbdb6ad1 100644 --- a/app/controllers/admin/bank_transactions_controller.rb +++ b/app/controllers/admin/bank_transactions_controller.rb @@ -7,6 +7,7 @@ class Admin::BankTransactionsController < AdminController end def create + comma_support_for(:bank_transaction, :sum) @bank_transaction = BankTransaction.new( bank_transaction_params.merge(bank_statement_id: params[:bank_statement_id]) ) @@ -21,6 +22,7 @@ class Admin::BankTransactionsController < AdminController end def update + comma_support_for(:bank_transaction, :sum) if @bank_transaction.update(bank_transaction_params) flash[:notice] = I18n.t('record_updated') redirect_to [:admin, @bank_transaction] diff --git a/app/controllers/admin/pricelists_controller.rb b/app/controllers/admin/pricelists_controller.rb index 8037f57f6..ce1431ab0 100644 --- a/app/controllers/admin/pricelists_controller.rb +++ b/app/controllers/admin/pricelists_controller.rb @@ -17,6 +17,7 @@ class Admin::PricelistsController < AdminController end def create + comma_support_for(:pricelist, :price) @pricelist = Pricelist.new(pricelist_params) if @pricelist.save @@ -27,6 +28,7 @@ class Admin::PricelistsController < AdminController end def update + comma_support_for(:pricelist, :price) if @pricelist.update_attributes(pricelist_params) redirect_to admin_pricelists_url else @@ -49,4 +51,5 @@ class Admin::PricelistsController < AdminController params.require(:pricelist).permit(:operation_category, :category, :price_category, :duration, :price, :valid_from, :valid_to) end + end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 87099d6fa..318923e3d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -70,4 +70,10 @@ class ApplicationController < ActionController::Base return 'public' if user.nil? "#{user.id}-#{user.class}: #{user.username}" end + + def comma_support_for(parent_key, key) + return if params[parent_key].blank? + return if params[parent_key][key].blank? + params[parent_key][key].sub!(/,/, '.') + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7397ec7cb..4efb6f2ee 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -45,4 +45,9 @@ module ApplicationHelper # can be api user or some other user link_to(model.updator, ['admin', model.updator]) end + + def currency(amount) + amount ||= 0 + ("%01.2f" % amount.round(2)).sub(/\./, ',') + end end diff --git a/app/models/deposit.rb b/app/models/deposit.rb index f9f887af1..23045196a 100644 --- a/app/models/deposit.rb +++ b/app/models/deposit.rb @@ -23,6 +23,10 @@ class Deposit false end + def amount + BigDecimal.new(@amount.to_s.sub(/,/, '.')) + end + def issue_prepayment_invoice valid? && registrar.issue_prepayment_invoice(amount, description) end diff --git a/app/views/admin/bank_statements/show.haml b/app/views/admin/bank_statements/show.haml index db8d64004..9797c59bf 100644 --- a/app/views/admin/bank_statements/show.haml +++ b/app/views/admin/bank_statements/show.haml @@ -63,7 +63,7 @@ %tr %td= link_to(l(x.paid_at, format: :date_long), [:admin, x]) %td= x.buyer_name - %td= x.sum + %td= currency(x.sum) %td= x.currency - c = x.binded? ? 'text-success' : 'text-danger' %td{class: c}= x.binded? ? t(:binded) : t(:not_binded) diff --git a/app/views/admin/bank_transactions/_form.haml b/app/views/admin/bank_transactions/_form.haml index 244284fda..b8c63c166 100644 --- a/app/views/admin/bank_transactions/_form.haml +++ b/app/views/admin/bank_transactions/_form.haml @@ -18,7 +18,7 @@ .form-group = f.label :sum, class: 'col-md-4 control-label required' .col-md-8 - = f.text_field(:sum, class: 'form-control', required: true) + = f.text_field(:sum, value: currency(f.object.sum), class: 'form-control', required: true) .form-group = f.label :reference_no, class: 'col-md-4 control-label required' diff --git a/app/views/admin/bank_transactions/show.haml b/app/views/admin/bank_transactions/show.haml index 7bcd8cfa1..4133386e5 100644 --- a/app/views/admin/bank_transactions/show.haml +++ b/app/views/admin/bank_transactions/show.haml @@ -36,7 +36,7 @@ %dd= @bank_transaction.description %dt= t(:sum) - %dd= @bank_transaction.sum + %dd= currency(@bank_transaction.sum) %dt= t(:currency) %dd= @bank_transaction.currency diff --git a/app/views/admin/pricelists/_form.haml b/app/views/admin/pricelists/_form.haml index 9e1715e72..a751825ca 100644 --- a/app/views/admin/pricelists/_form.haml +++ b/app/views/admin/pricelists/_form.haml @@ -15,7 +15,7 @@ .form-group = f.label :price .input-group - = f.text_field(:price, class: 'form-control') + = f.text_field(:price, value: currency(f.object.price), class: 'form-control') %span.input-group-addon= Money.default_currency .form-group.input-daterange = f.label :valid_from, t(:valid) diff --git a/app/views/admin/pricelists/index.haml b/app/views/admin/pricelists/index.haml index 2f51a4329..50edcff2e 100644 --- a/app/views/admin/pricelists/index.haml +++ b/app/views/admin/pricelists/index.haml @@ -33,7 +33,7 @@ %td= pricelist.category %td= pricelist.duration %td= pricelist.operation_category - %td= pricelist.price + %td= currency(pricelist.price) %td= l(pricelist.valid_from, format: :ydate) %td= l(pricelist.valid_to, format: :ydate) %td= link_to(t(:edit), edit_admin_pricelist_path(pricelist), class: 'btn btn-xs btn-primary') diff --git a/app/views/registrar/invoices/partials/_items.haml b/app/views/registrar/invoices/partials/_items.haml index 784fe4412..6ed5144a8 100644 --- a/app/views/registrar/invoices/partials/_items.haml +++ b/app/views/registrar/invoices/partials/_items.haml @@ -14,19 +14,19 @@ %tr %td= t(x.description) %td= x.unit - %td= x.amount - %td= x.price - %td= x.item_sum_without_vat + %td= currency(x.amount) + %td= currency(x.price) + %td= currency(x.item_sum_without_vat) %tfoot %tr %th{colspan: 3} %th= t(:total_without_vat) - %td= @invoice.sum_without_vat + %td= currency(@invoice.sum_without_vat) %tr %th.no-border{colspan: 3} %th= t('vat', vat_prc: (@invoice.vat_prc * 100).round) - %td= @invoice.vat + %td= currency(@invoice.vat) %tr %th.no-border{colspan: 3} %th= t(:total) - %td= @invoice.sum + %td= currency(@invoice.sum) diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index ae67ac4e3..d90c24fec 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -229,22 +229,22 @@ %tr %td= t(x.description) %td= x.unit - %td= x.amount - %td= x.price - %td= "#{x.item_sum_without_vat} #{@invoice.currency}" + %td= currency(x.amount) + %td= currency(x.price) + %td= "#{currency(x.item_sum_without_vat)} #{@invoice.currency}" %tfoot %tr %th{colspan: 3} %th= t(:total_without_vat) - %td= "#{@invoice.sum_without_vat} #{@invoice.currency}" + %td= "#{currency(@invoice.sum_without_vat)} #{@invoice.currency}" %tr %th.no-border{colspan: 3} %th= t('vat', vat_prc: (@invoice.vat_prc * 100).round) - %td= "#{@invoice.vat} #{@invoice.currency}" + %td= "#{currency(@invoice.vat)} #{@invoice.currency}" %tr %th.no-border{colspan: 3} %th= t(:total) - %td= "#{@invoice.sum} #{@invoice.currency}" + %td= "#{currency(@invoice.sum)} #{@invoice.currency}" #footer %hr diff --git a/spec/features/admin/bank_statement_spec.rb b/spec/features/admin/bank_statement_spec.rb index bca3136fb..b6ac8dcd2 100644 --- a/spec/features/admin/bank_statement_spec.rb +++ b/spec/features/admin/bank_statement_spec.rb @@ -46,7 +46,7 @@ feature 'BankStatement', type: :feature do click_link 'Back to bank statement' - page.should have_content('120.0') + page.should have_content('120,00') page.should have_content('Test buyer') end end diff --git a/spec/features/admin/invoice_spec.rb b/spec/features/admin/invoice_spec.rb index e036b5f1c..1e10f066b 100644 --- a/spec/features/admin/invoice_spec.rb +++ b/spec/features/admin/invoice_spec.rb @@ -37,7 +37,7 @@ feature 'Invoice', type: :feature do page.should have_content('Record created') page.should have_content('Invoice no.') page.should have_content('Prepayment') - page.should have_content('120.0') + page.should have_content('120,00') page.should have_content(r.name) end @@ -107,7 +107,7 @@ feature 'Invoice', type: :feature do page.should have_content('689') page.should have_content('EE557700771000598731') page.should have_content('Not binded', count: 2) - page.should have_content(invoice.sum.to_s) + page.should have_content('240,00') page.should have_content('EUR') click_link 'Bind invoices' @@ -120,7 +120,7 @@ feature 'Invoice', type: :feature do page.should have_content('Binded') page.should have_content(invoice.to_s) - page.should have_content(invoice.sum.to_s) + page.should have_content('240,00') page.should have_content(invoice.reference_no) page.should have_content(I18n.l(paid_at, format: :date_long)) diff --git a/spec/models/deposit_spec.rb b/spec/models/deposit_spec.rb new file mode 100644 index 000000000..ff77dbd98 --- /dev/null +++ b/spec/models/deposit_spec.rb @@ -0,0 +1,39 @@ +require 'rails_helper' + +describe Deposit do + context 'with invalid attribute' do + before :all do + @deposit = Deposit.new + end + + it 'should not be valid' do + @deposit.valid? + @deposit.errors.full_messages.should match_array([ + "Registrar is missing" + ]) + end + + it 'should have 0 amount' do + @deposit.amount.should == 0 + end + + it 'should not be presisted' do + @deposit.persisted?.should == false + end + + it 'should replace comma with point for 0' do + @deposit.amount = '0,0' + @deposit.amount.should == 0.0 + end + + it 'should replace comma with points' do + @deposit.amount = '10,11' + @deposit.amount.should == 10.11 + end + + it 'should work with float as well' do + @deposit.amount = 0.123 + @deposit.amount.should == 0.123 + end + end +end