From 09cfc055ea85121a73d5209d0a2933711352446e Mon Sep 17 00:00:00 2001 From: Adrian Hinz Date: Tue, 4 Oct 2016 23:47:06 +0200 Subject: [PATCH] Signed-off-by: Adrian Hinz --- .gitignore | 1 + Gemfile | 3 +- app/assets/javascripts/application.js | 3 ++ app/assets/stylesheets/application.css | 1 + app/controllers/invoices_controller.rb | 49 +++++++++++++++++ app/controllers/products_controller.rb | 4 ++ app/helpers/application_helper.rb | 2 +- app/models/invoice.rb | 11 ++++ app/models/invoice_product.rb | 4 ++ app/models/product.rb | 1 + app/models/user.rb | 1 + app/models/user_firm.rb | 1 + app/views/customers/new.html.erb | 8 +-- app/views/invoices/_form.html.erb | 52 +++++++++++++++++++ app/views/invoices/_show.html.erb | 17 ++++++ app/views/invoices/edit.html.erb | 12 +++++ app/views/invoices/index.html.erb | 34 +++++++++--- app/views/invoices/new.html.erb | 11 ++++ app/views/invoices/show.html.erb | 12 +++++ app/views/products/_form.html.erb | 35 +++++++++++++ app/views/products/edit.html.erb | 12 +++++ app/views/products/index.html.erb | 32 +++++++++--- app/views/products/new.html.erb | 11 ++++ config/routes.rb | 2 +- db/migrate/20161004195824_create_invoices.rb | 14 +++++ .../20161004200042_create_invoice_products.rb | 12 +++++ db/schema.rb | 32 +++++++++++- test/fixtures/invoice_products.yml | 13 +++++ test/fixtures/invoices.yml | 17 ++++++ test/models/invoice_product_test.rb | 7 +++ test/models/invoice_test.rb | 7 +++ 31 files changed, 399 insertions(+), 22 deletions(-) create mode 100644 app/models/invoice.rb create mode 100644 app/models/invoice_product.rb create mode 100644 app/views/invoices/_form.html.erb create mode 100644 app/views/invoices/_show.html.erb create mode 100644 app/views/invoices/edit.html.erb create mode 100644 app/views/invoices/new.html.erb create mode 100644 app/views/invoices/show.html.erb create mode 100644 app/views/products/_form.html.erb create mode 100644 app/views/products/edit.html.erb create mode 100644 app/views/products/new.html.erb create mode 100644 db/migrate/20161004195824_create_invoices.rb create mode 100644 db/migrate/20161004200042_create_invoice_products.rb create mode 100644 test/fixtures/invoice_products.yml create mode 100644 test/fixtures/invoices.yml create mode 100644 test/models/invoice_product_test.rb create mode 100644 test/models/invoice_test.rb diff --git a/.gitignore b/.gitignore index bab620d..5f9e0d3 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ # Ignore Byebug command history file. .byebug_history +Gemfile.lock diff --git a/Gemfile b/Gemfile index 10e5de1..5e7fb07 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,8 @@ source 'https://rubygems.org' gem 'rails', '~> 5.0.0', '>= 5.0.0.1' # Use sqlite3 as the database for Active Record # gem 'sqlite3' - +gem 'momentjs-rails', '>= 2.9.0' +gem 'bootstrap3-datetimepicker-rails', '~> 4.14.30' gem 'devise' gem 'mysql2' diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index b12018d..911d01c 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -12,5 +12,8 @@ // //= require jquery //= require jquery_ujs +//= require moment +//= require moment/pl +//= require bootstrap-datetimepicker //= require turbolinks //= require_tree . diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 0ebd7fe..bfa3896 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -11,5 +11,6 @@ * It is generally better to create a new file per style scope. * *= require_tree . + *= require bootstrap-datetimepicker *= require_self */ diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index e3be1c6..e2ab588 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -1,4 +1,53 @@ class InvoicesController < ApplicationController def index + @invoices = Invoice.where(user_id: current_user.id) + end + + def show + @invoice = Invoice.find(params[:id]) + end + + def new + @invoice = Invoice.new(date: Date.today) + @user_firms = current_user.user_firms + @customers = current_user.customers + end + + def create + @invoice = Invoice.new(invoice_params) + @invoice.user_id = current_user.id + if @invoice.save + redirect_to @invoice + else + @user_firms = current_user.user_firms + @customers = current_user.customers + render :new + end + end + + def edit + @invoice = Invoice.find(params[:id]) + end + + def update + @invoice = Invoice.find(params[:id]) + if @invoice.update_attributes(invoice_params) + redirect_to @invoice + else + @user_firms = current_user.user_firms + @customers = current_user.customers + render :edit + end + end + + def destroy + @invoice = Invoice.find(params[:id]) + @invoice.destroy unless @invoice.blank? + redirect_to invoices_path + end + + private + def invoice_params + params.require(:invoice).permit(:number, :user_firm_id, :customer_id, :date, :date_of_payment) end end diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 1ae4d4b..4c1ae9a 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -6,6 +6,7 @@ class ProductsController < ApplicationController def new @product = Product.new + @vats = Vat.all end def create @@ -14,12 +15,14 @@ class ProductsController < ApplicationController if @product.save redirect_to products_path else + @vats = Vat.all render :new end end def edit @product = Product.find(params[:id]) + @vats = Vat.all end def update @@ -27,6 +30,7 @@ class ProductsController < ApplicationController if @product.update_attributes(product_params) redirect_to products_path else + @vats = Vat.all render :edit end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0bbafea..aab0ab5 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -4,7 +4,7 @@ module ApplicationHelper def menu ret = "" ret += create_menu_li("glyphicon glyphicon-home","Dashboard","/","welcome") - ret += create_menu_li("glyphicon glyphicon-list-alt","Faktury","/invoices/index","invoices") + ret += create_menu_li("glyphicon glyphicon-list-alt","Faktury","/invoices","invoices") ret += create_menu_li("glyphicon glyphicon-book","Klienci","/customers","customers") ret += create_menu_li("glyphicon glyphicon-shopping-cart","Produkty","/products","products") ret += create_menu_li("glyphicon glyphicon-cog","Ustawienia","/settings/index","settings") diff --git a/app/models/invoice.rb b/app/models/invoice.rb new file mode 100644 index 0000000..96179e3 --- /dev/null +++ b/app/models/invoice.rb @@ -0,0 +1,11 @@ +class Invoice < ApplicationRecord + belongs_to :user + belongs_to :user_firm + belongs_to :customer + has_many :invoice_products + + def netto_amount + 0.0 + end + +end diff --git a/app/models/invoice_product.rb b/app/models/invoice_product.rb new file mode 100644 index 0000000..3d0b649 --- /dev/null +++ b/app/models/invoice_product.rb @@ -0,0 +1,4 @@ +class InvoiceProduct < ApplicationRecord + belongs_to :invoice + belongs_to :product +end diff --git a/app/models/product.rb b/app/models/product.rb index f00671a..644491b 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -1,4 +1,5 @@ class Product < ApplicationRecord belongs_to :vat belongs_to :user + has_many :invoice_products end diff --git a/app/models/user.rb b/app/models/user.rb index 8fb58bd..d847145 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,4 +7,5 @@ class User < ApplicationRecord has_many :user_firms has_many :products has_many :customers + has_many :invoices end diff --git a/app/models/user_firm.rb b/app/models/user_firm.rb index 44501ae..a31d486 100644 --- a/app/models/user_firm.rb +++ b/app/models/user_firm.rb @@ -1,3 +1,4 @@ class UserFirm < ApplicationRecord belongs_to :user + has_many :invoices end diff --git a/app/views/customers/new.html.erb b/app/views/customers/new.html.erb index 8c7f8ab..947b460 100644 --- a/app/views/customers/new.html.erb +++ b/app/views/customers/new.html.erb @@ -1,9 +1,9 @@
-
Nowy klient
-
- <%= link_to raw(" " + I18n.t('back')), customers_path, title: I18n.t('back'), class: "text-primary" %> -
+
Nowy klient
+
+ <%= link_to raw(" " + I18n.t('back')), customers_path, title: I18n.t('back'), class: "text-primary" %> +
<%= render 'form' %> diff --git a/app/views/invoices/_form.html.erb b/app/views/invoices/_form.html.erb new file mode 100644 index 0000000..8fb31aa --- /dev/null +++ b/app/views/invoices/_form.html.erb @@ -0,0 +1,52 @@ + +<%= form_for @invoice, html: {class: "form-horizontal"} do |f| %> +<% if @invoice.errors.any? %> + <%= raw errors_to_html(@invoice.errors) %> +<% end %> +
+ <%= f.label :number, class: "col-sm-2 control-label" %> +
+ <%= f.text_field :number, class: "form-control", placeholder: 'Numer faktury' %> +
+
+
+ <%= f.label :user_firm, class: "col-sm-2 control-label" %> +
+ <%= f.select :user_firm_id, @user_firms.collect {|uf| [ uf.name, uf.id ] }, {}, {class: "form-control"} %> +
+
+
+ <%= f.label :customer, class: "col-sm-2 control-label" %> +
+ <%= f.select :customer_id, @customers.collect {|c| [ c.name, c.id ] }, {include_blank: true}, {class: "form-control"} %> +
+
+
+ <%= f.label :date, class: "col-sm-2 control-label" %> +
+
+ <%= f.text_field :date, class: "form-control datepicker", placeholder: 'Data wystawienia' %> + + + + +
+
+
+ +
+
+ <%= f.submit 'Zapisz', class: "btn btn-primary" %> +
+
+ +<% end %> diff --git a/app/views/invoices/_show.html.erb b/app/views/invoices/_show.html.erb new file mode 100644 index 0000000..b09cef5 --- /dev/null +++ b/app/views/invoices/_show.html.erb @@ -0,0 +1,17 @@ +
+
+
+
+
Faktura VAT
+ +
+
+ Ut tristique adipiscing mauris, sit amet suscipit metus porta quis. Donec dictum tincidunt erat, eu blandit ligula. Nam sit amet dolor sapien. Quisque velit erat, congue sed suscipit vel, feugiat sit amet enim. Suspendisse interdum enim at mi tempor commodo. Sed tincidunt sed tortor eu scelerisque. Donec luctus malesuada vulputate. Nunc vel auctor metus, vel adipiscing odio. Aliquam aliquet rhoncus libero, at varius nisi pulvinar nec. Aliquam erat volutpat. Donec ut neque mi. Praesent enim nisl, bibendum vitae ante et, placerat pharetra magna. Donec facilisis nisl turpis, eget facilisis turpis semper non. Maecenas luctus ligula tincidunt iasdsd vitae ante et, +

+ Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque sed consectetur erat. Maecenas in elementum libero. Sed consequat pellentesque ultricies. Ut laoreet vehicula nisl sed placerat. Duis posuere lectus n, eros et hendrerit pellentesque, ante magna condimentum sapien, eget ultrices eros libero non orci. Etiam varius diam lectus. +

+
+
+
+ +
diff --git a/app/views/invoices/edit.html.erb b/app/views/invoices/edit.html.erb new file mode 100644 index 0000000..3d7c76b --- /dev/null +++ b/app/views/invoices/edit.html.erb @@ -0,0 +1,12 @@ +
+
+
Edycja faktury
+
+ <%= link_to raw(" " + I18n.t('back')), invoices_path, title: I18n.t('back'), class: "text-primary" %> +
+
+
+ <%= render 'form' %> +
+
+
diff --git a/app/views/invoices/index.html.erb b/app/views/invoices/index.html.erb index 2db3d72..9ccb0ca 100644 --- a/app/views/invoices/index.html.erb +++ b/app/views/invoices/index.html.erb @@ -1,15 +1,35 @@ -
-
-
+
+
Faktury
- + <%= link_to raw(' Nowa faktura'), new_invoice_path, class: "text-primary" %>
-
- +
+ + + + + + + + + + <% @invoices.each do |invoice| %> + + + + + + + + + <% end %> +
NumerKlientKwota nettoData wystawieniaTermin płatnościAkcje
<%= invoice.number %><%= invoice.customer.name %><%= number_with_precision(invoice.netto_amount, precision: 2) %><%= invoice.date %><%= invoice.date_of_payment %> + <%= link_to raw(' Edycja'), edit_invoice_path(invoice), class: "btn btn-primary btn-xs" %> + <%= link_to raw(' Usuń'), invoice_path(invoice), class: "btn btn-danger btn-xs", method: :delete, data: { confirm: 'Czy na pewno usunąć?' } %> +
-
diff --git a/app/views/invoices/new.html.erb b/app/views/invoices/new.html.erb new file mode 100644 index 0000000..904c62b --- /dev/null +++ b/app/views/invoices/new.html.erb @@ -0,0 +1,11 @@ +
+
+
Nowa faktura
+
+ <%= link_to raw(" " + I18n.t('back')), invoices_path, title: I18n.t('back'), class: "text-primary" %> +
+
+
+ <%= render 'form' %> +
+
diff --git a/app/views/invoices/show.html.erb b/app/views/invoices/show.html.erb new file mode 100644 index 0000000..941e44a --- /dev/null +++ b/app/views/invoices/show.html.erb @@ -0,0 +1,12 @@ +
+
+
Faktura: <%= @invoice.number %>
+
+ <%= link_to raw(" " + I18n.t('back')), invoices_path, title: I18n.t('back'), class: "text-primary" %> +
+
+
+ <%= render 'show' %> +
+
+
diff --git a/app/views/products/_form.html.erb b/app/views/products/_form.html.erb new file mode 100644 index 0000000..9a98331 --- /dev/null +++ b/app/views/products/_form.html.erb @@ -0,0 +1,35 @@ + +<%= form_for @product, html: {class: "form-horizontal"} do |f| %> +<% if @product.errors.any? %> + <%= raw errors_to_html(@product.errors) %> +<% end %> +
+ <%= f.label :name, class: "col-sm-2 control-label" %> +
+ <%= f.text_field :name, class: "form-control", placeholder: 'Nazwa' %> +
+
+
+ <%= f.label :netto_price, class: "col-sm-2 control-label" %> +
+ <%= f.text_field :netto_price, class: "form-control", placeholder: 'Wartość netto' %> +
+
+
+ <%= f.label :qnt_name, class: "col-sm-2 control-label" %> +
+ <%= f.text_field :qnt_name, class: "form-control", placeholder: 'Jednostka miary' %> +
+
+
+ <%= f.label :vat, class: "col-sm-2 control-label" %> +
+ <%= f.select :vat_id, @vats.collect {|v| [ v.name, v.id ] }, {}, {class: "form-control"} %> +
+
+
+
+ <%= f.submit 'Zapisz', class: "btn btn-primary" %> +
+
+<% end %> diff --git a/app/views/products/edit.html.erb b/app/views/products/edit.html.erb new file mode 100644 index 0000000..ee6f830 --- /dev/null +++ b/app/views/products/edit.html.erb @@ -0,0 +1,12 @@ +
+
+
Edycja produktu
+
+ <%= link_to raw(" " + I18n.t('back')), products_path, title: I18n.t('back'), class: "text-primary" %> +
+
+
+ <%= render 'form' %> +
+
+
diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index 6115cb1..613b289 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -1,15 +1,33 @@ -
-
-
+
+
Produkty
- + <%= link_to raw(' Nowy produkt'), new_product_path, class: "text-primary" %>
-
- +
+ + + + + + + + + <% @products.each do |product| %> + + + + + + + + <% end %> +
NazwaWartość nettoVATJednostka miaryAkcje
<%= product.name %><%= number_with_precision(product.netto_price, precision: 2) %><%= product.vat.name %><%= product.qnt_name %> + <%= link_to raw(' Edycja'), edit_product_path(product), class: "btn btn-primary btn-xs" %> + <%= link_to raw(' Usuń'), product_path(product), class: "btn btn-danger btn-xs", method: :delete, data: { confirm: 'Czy na pewno usunąć?' } %> +
-
diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb new file mode 100644 index 0000000..0e0d345 --- /dev/null +++ b/app/views/products/new.html.erb @@ -0,0 +1,11 @@ +
+
+
Nowy produkt
+
+ <%= link_to raw(" " + I18n.t('back')), products_path, title: I18n.t('back'), class: "text-primary" %> +
+
+
+ <%= render 'form' %> +
+
diff --git a/config/routes.rb b/config/routes.rb index 5dc5e2d..4e2991c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,10 +1,10 @@ Rails.application.routes.draw do - get 'invoices/index' get 'settings/index' devise_for :users resources :products + resources :invoices resources :customers diff --git a/db/migrate/20161004195824_create_invoices.rb b/db/migrate/20161004195824_create_invoices.rb new file mode 100644 index 0000000..f760e22 --- /dev/null +++ b/db/migrate/20161004195824_create_invoices.rb @@ -0,0 +1,14 @@ +class CreateInvoices < ActiveRecord::Migration[5.0] + def change + create_table :invoices do |t| + t.references :user, foreign_key: true + t.references :user_firm, foreign_key: true + t.references :customer, foreign_key: true + t.string :number + t.date :date + t.date :date_of_payment + + t.timestamps + end + end +end diff --git a/db/migrate/20161004200042_create_invoice_products.rb b/db/migrate/20161004200042_create_invoice_products.rb new file mode 100644 index 0000000..a80e0ec --- /dev/null +++ b/db/migrate/20161004200042_create_invoice_products.rb @@ -0,0 +1,12 @@ +class CreateInvoiceProducts < ActiveRecord::Migration[5.0] + def change + create_table :invoice_products do |t| + t.references :invoice, foreign_key: true + t.references :product, foreign_key: true + t.decimal :netto_price, { precision: 18, scale: 2 } + t.decimal :qty, { precision: 18, scale: 3 } + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index d86cee1..c09f18e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161003103325) do +ActiveRecord::Schema.define(version: 20161004200042) do create_table "customers", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci" do |t| t.string "name" @@ -25,6 +25,31 @@ ActiveRecord::Schema.define(version: 20161003103325) do t.index ["user_id"], name: "index_customers_on_user_id", using: :btree end + create_table "invoice_products", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci" do |t| + t.integer "invoice_id" + t.integer "product_id" + t.decimal "netto_price", precision: 18, scale: 2 + t.decimal "qty", precision: 18, scale: 3 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["invoice_id"], name: "index_invoice_products_on_invoice_id", using: :btree + t.index ["product_id"], name: "index_invoice_products_on_product_id", using: :btree + end + + create_table "invoices", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci" do |t| + t.integer "user_id" + t.integer "user_firm_id" + t.integer "customer_id" + t.string "number" + t.date "date" + t.date "date_of_payment" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["customer_id"], name: "index_invoices_on_customer_id", using: :btree + t.index ["user_firm_id"], name: "index_invoices_on_user_firm_id", using: :btree + t.index ["user_id"], name: "index_invoices_on_user_id", using: :btree + end + create_table "products", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci" do |t| t.string "name" t.integer "vat_id" @@ -80,6 +105,11 @@ ActiveRecord::Schema.define(version: 20161003103325) do end add_foreign_key "customers", "users" + add_foreign_key "invoice_products", "invoices" + add_foreign_key "invoice_products", "products" + add_foreign_key "invoices", "customers" + add_foreign_key "invoices", "user_firms" + add_foreign_key "invoices", "users" add_foreign_key "products", "users" add_foreign_key "products", "vats" add_foreign_key "user_firms", "users" diff --git a/test/fixtures/invoice_products.yml b/test/fixtures/invoice_products.yml new file mode 100644 index 0000000..4f8cdbb --- /dev/null +++ b/test/fixtures/invoice_products.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + invoice: one + product: one + netto_price: + qty: + +two: + invoice: two + product: two + netto_price: + qty: diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml new file mode 100644 index 0000000..0a780ea --- /dev/null +++ b/test/fixtures/invoices.yml @@ -0,0 +1,17 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + user: one + user_firm: one + customer: one + number: MyString + date: 2016-10-04 + date_of_payment: 2016-10-04 + +two: + user: two + user_firm: two + customer: two + number: MyString + date: 2016-10-04 + date_of_payment: 2016-10-04 diff --git a/test/models/invoice_product_test.rb b/test/models/invoice_product_test.rb new file mode 100644 index 0000000..55ab5e1 --- /dev/null +++ b/test/models/invoice_product_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class InvoiceProductTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb new file mode 100644 index 0000000..25dd32c --- /dev/null +++ b/test/models/invoice_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class InvoiceTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end