From 536a0fb1686a67653e4004ae23023f4587eb134b Mon Sep 17 00:00:00 2001 From: Adrian Hinz Date: Thu, 21 Sep 2017 15:58:39 +0200 Subject: [PATCH] Signed-off-by: Adrian Hinz --- app/assets/javascripts/application.js | 1 + app/controllers/invoices_controller.rb | 2 +- app/models/invoice.rb | 16 +++++++++ app/views/invoices/_form.html.erb | 20 +++-------- .../invoices/_invoice_product_fields.html.erb | 17 +++++++++ app/views/invoices/_show.html.erb | 35 +++++++++++++------ config/environment.rb | 1 + config/locales/pl.yml | 2 ++ 8 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 app/views/invoices/_invoice_product_fields.html.erb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 911d01c..569bd70 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -17,3 +17,4 @@ //= require bootstrap-datetimepicker //= require turbolinks //= require_tree . +//= require cocoon diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index c08b90f..9359280 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -6,7 +6,7 @@ class InvoicesController < ApplicationController def show @invoice = Invoice.find(params[:id]) - + @vats = Vat.all respond_to do |format| format.html {@inv_format = 'html'} format.pdf do diff --git a/app/models/invoice.rb b/app/models/invoice.rb index e3311da..19670ef 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -9,4 +9,20 @@ class Invoice < ApplicationRecord 0.0 end + def products_with_tax(vat_id) + self.invoice_products.joins(:product => :vat).where('vats.id = ?',vat_id) + end + + def products_with_tax_sum_arr(vat_id) + sum = 0.0 + vat = 0.0 + total = 0.0 + for invoice_product in self.invoice_products.joins(:product => :vat).where('vats.id = ?',vat_id) + sum += invoice_product.netto_price * invoice_product.qty + vat += (invoice_product.netto_price * (invoice_product.product.vat.tax_rate/100)) * invoice_product.qty + total += (invoice_product.netto_price * (invoice_product.product.vat.tax_rate/100 + 1)) * invoice_product.qty + end + return [sum,vat,total] + end + end diff --git a/app/views/invoices/_form.html.erb b/app/views/invoices/_form.html.erb index 2cefb80..b6be19b 100644 --- a/app/views/invoices/_form.html.erb +++ b/app/views/invoices/_form.html.erb @@ -48,23 +48,11 @@
Produkty/Usługi -
- <%= f.fields_for :invoice_products do |invoice_products_form| %> -
-
- <%= invoice_products_form.select :product_id, Product.where(user_id: current_user.id).collect {|p| [ p.name, p.id ] }, {include_blank: true}, {class: "form-control"} %> -
-
- <%= invoice_products_form.text_field :qty, class: "form-control", placeholder: 'Ilość' %> -
-
- <%= invoice_products_form.text_field :netto_price, class: "form-control", placeholder: 'Wartość netto' %> -
-
- <%= invoice_products_form.check_box :_destroy%> -
-
+
+ <%= f.fields_for :invoice_products do |invoice_products_form| %> + <%= render :partial => '/invoices/invoice_product_fields', :locals => {:f => invoice_products_form} %> <% end %> + <%= link_to_add_association raw(' '+ t('add_position')), f, :invoice_products, {:class => 'btn btn-info btn-xs shiny'} %>
diff --git a/app/views/invoices/_invoice_product_fields.html.erb b/app/views/invoices/_invoice_product_fields.html.erb new file mode 100644 index 0000000..45ebe99 --- /dev/null +++ b/app/views/invoices/_invoice_product_fields.html.erb @@ -0,0 +1,17 @@ +
+
+
+ <%= f.select :product_id, Product.where(user_id: current_user.id).collect {|p| [ p.name, p.id ] }, {include_blank: true}, {class: "form-control"} %> +
+
+ <%= f.text_field :qty, class: "form-control", placeholder: 'Ilość' %> +
+
+ <%= f.text_field :netto_price, class: "form-control", placeholder: 'Wartość netto' %> +
+
+ <%= f.check_box :_destroy%> + <%= t('del_position') %> +
+
+
diff --git a/app/views/invoices/_show.html.erb b/app/views/invoices/_show.html.erb index 59ec68c..c64f0aa 100644 --- a/app/views/invoices/_show.html.erb +++ b/app/views/invoices/_show.html.erb @@ -127,17 +127,30 @@ - - Podatek VAT 23% - 30 000,00 - 6 900,00 - 36 900,00 - + <% + total_arr = [0.0,0.0,0.0] + for vat in @vats + if @invoice.products_with_tax(vat.id) + sum_arr = @invoice.products_with_tax_sum_arr(vat.id) + total_arr[0] += sum_arr[0] + total_arr[1] += sum_arr[1] + total_arr[2] += sum_arr[2] + %> + + Podatek VAT <%= vat.name %> + <%= number_with_precision(sum_arr[0], delimiter: " ", separator: ",", precision: 2) %> + <%= number_with_precision(sum_arr[1], delimiter: " ", separator: ",", precision: 2) %> + <%= number_with_precision(sum_arr[2], delimiter: " ", separator: ",", precision: 2) %> + + <% + end + end + %> Razem: - 30 000,00 - 6 900,00 - 36 900,00 + <%= number_with_precision(total_arr[0], delimiter: " ", separator: ",", precision: 2) %> + <%= number_with_precision(total_arr[1], delimiter: " ", separator: ",", precision: 2) %> + <%= number_with_precision(total_arr[2], delimiter: " ", separator: ",", precision: 2) %> @@ -150,10 +163,10 @@
- + - + diff --git a/config/environment.rb b/config/environment.rb index 27dc1be..23a9958 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -5,4 +5,5 @@ require_relative 'application' Rails.application.initialize! # haslo_do_API +SMS_API_URL = '' SMS_API_PASS_MD5 = '798caa5d3415cb6a74188fe6c5478c85' diff --git a/config/locales/pl.yml b/config/locales/pl.yml index f782c1d..72fe78d 100644 --- a/config/locales/pl.yml +++ b/config/locales/pl.yml @@ -7,6 +7,8 @@ pl: forgot_your_password: 'Zapmniałeś hasła?' didnt_receive_confirmation_instructions: 'Nie otrzymałeś instrukcji potwierdzenia?' didnt_receive_unlock_instructions: 'Nie otrzymałeś instrukcji odblokowania?' + del_position: 'Usuń pozycję' + add_position: 'Dodaj pozycję' menu: dashboard: 'Dashboard' customers: 'Klienci'
Kwota do zapłaty: 36 900,00 PLNKwota do zapłaty: <%= number_with_precision(total_arr[2], delimiter: " ", separator: ",", precision: 2) %> PLN
Słownie do zapłaty: <%= decimal_to_word(36900.0) %>Słownie do zapłaty: <%= decimal_to_word(total_arr[2]) %>
Forma płatności: Przelew