diff --git a/app/controllers/deliveries_controller.rb b/app/controllers/deliveries_controller.rb index dd9f6f87..393d3792 100644 --- a/app/controllers/deliveries_controller.rb +++ b/app/controllers/deliveries_controller.rb @@ -2,8 +2,6 @@ class DeliveriesController < ApplicationController before_filter :find_supplier - # GET /deliveries - # GET /deliveries.xml def index @deliveries = @supplier.deliveries.find(:all) @@ -13,8 +11,6 @@ class DeliveriesController < ApplicationController end end - # GET /deliveries/1 - # GET /deliveries/1.xml def show @delivery = Delivery.find(params[:id]) @@ -24,24 +20,19 @@ class DeliveriesController < ApplicationController end end - # GET /deliveries/new - # GET /deliveries/new.xml def new @delivery = @supplier.deliveries.build - + 3.times { @delivery.stock_changes.build } respond_to do |format| format.html # new.html.erb format.xml { render :xml => @delivery } end end - # GET /deliveries/1/edit def edit @delivery = Delivery.find(params[:id]) end - # POST /deliveries - # POST /deliveries.xml def create @delivery = Delivery.new(params[:delivery]) @@ -74,8 +65,6 @@ class DeliveriesController < ApplicationController end end - # DELETE /deliveries/1 - # DELETE /deliveries/1.xml def destroy @delivery = Delivery.find(params[:id]) @delivery.destroy @@ -86,6 +75,15 @@ class DeliveriesController < ApplicationController end end + def drop_stock_change + stock_change = StockChange.find(params[:stock_change_id]) + stock_change.destroy + + render :update do |page| + page.visual_effect(:DropOut, "stock_change_#{stock_change.id}") + end + end + protected def find_supplier diff --git a/app/helpers/deliveries_helper.rb b/app/helpers/deliveries_helper.rb new file mode 100644 index 00000000..03c5a703 --- /dev/null +++ b/app/helpers/deliveries_helper.rb @@ -0,0 +1,12 @@ +module DeliveriesHelper + def articles_for_select(supplier) + supplier.articles.find(:all, :limit => 10).collect { |a| [truncate(a.name), a.id] } + end + + def add_article_link + link_to_function "Artikel hinzufügen", nil, { :accesskey => 'n', :title => "ALT + SHIFT + N" } do |page| + page.insert_html :bottom, :stock_changes, :partial => 'stock_change', :object => StockChange.new + end + end + +end diff --git a/app/models/article.rb b/app/models/article.rb index 0c43c16d..09b480a9 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,5 +1,5 @@ # == Schema Information -# Schema version: 20090113111624 +# Schema version: 20090115232435 # # Table name: articles # @@ -21,32 +21,9 @@ # order_number :string(255) # created_at :datetime # updated_at :datetime +# quantity :decimal(6, 2) default(0.0) # -# == Schema Information -# Schema version: 20090102171850 -# -# Table name: articles -# -# id :integer(4) not null, primary key -# name :string(255) default(""), not null -# supplier_id :integer(4) default(0), not null -# article_category_id :integer(4) default(0), not null -# unit :string(255) default(""), not null -# note :string(255) -# availability :boolean(1) default(TRUE), not null -# manufacturer :string(255) -# origin :string(255) -# shared_updated_on :datetime -# net_price :decimal(8, 2) -# gross_price :decimal(8, 2) default(0.0), not null -# tax :float -# deposit :decimal(8, 2) default(0.0) -# unit_quantity :integer(4) default(1), not null -# order_number :string(255) -# created_at :datetime -# updated_at :datetime -# class Article < ActiveRecord::Base belongs_to :supplier belongs_to :article_category @@ -230,4 +207,9 @@ class Article < ActiveRecord::Base # it could be so easy ... but that doesn't work for empty category-ids... # articles.group_by {|a| a.article_category}.sort {|a, b| a[0].name <=> b[0].name} end + + def update_quantity(amount) + update_attribute :quantity, quantity + amount + end + end diff --git a/app/models/delivery.rb b/app/models/delivery.rb index 883b939f..30046378 100644 --- a/app/models/delivery.rb +++ b/app/models/delivery.rb @@ -12,6 +12,14 @@ class Delivery < ActiveRecord::Base belongs_to :supplier + has_many :stock_changes validates_presence_of :supplier_id + + def stock_change_attributes=(stock_change_attributes) + for attributes in stock_change_attributes + stock_changes.build(attributes) unless attributes[:quantity] == 0.0 or attributes[:quantity].blank? + end + end + end diff --git a/app/models/mailer.rb b/app/models/mailer.rb index 65c4d3c9..422ab958 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -21,10 +21,10 @@ class Mailer < ActionMailer::Base from (message.system_message? ? "FoodSoft <#{APP_CONFIG[:email_sender]}>" : "#{message.sender.nick} <#{message.sender.email}>") body :body => message.body, :sender => (message.system_message? ? 'Foodsoft' : message.sender.nick), :recipients => message.recipients, - :reply => url_for(:host => request.host, reply_message_path(message), - :profile => url_for(:host => request.host, my_profile_path), - :link => url_for(:host => request.host, message_path(message), - :foodsoftUrl => url_for(:host => request.host, :controller => "index") + :reply => url_for(:host => request.host, :controller => "messages", :action => "reply", :id => message), + :profile => url_for(:host => request.host, :controller => "home", :action => "profile"), + :link => url_for(:host => request.host, :controller => "messages", :action => "show", :id => message), + :foodsoftUrl => url_for(:host => request.host, :controller => "/") end # Sends an invite email. diff --git a/app/models/message.rb b/app/models/message.rb index 7a6d8222..5304afb8 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -1,11 +1,11 @@ # == Schema Information -# Schema version: 20090115123421 +# Schema version: 20090115232435 # # Table name: messages # # id :integer(4) not null, primary key # sender_id :integer(4) -# recipients_ids :text default(""), not null +# recipients_ids :text # subject :string(255) not null # body :text # email_state :integer(4) default(0), not null diff --git a/app/models/stock_change.rb b/app/models/stock_change.rb new file mode 100644 index 00000000..087d0044 --- /dev/null +++ b/app/models/stock_change.rb @@ -0,0 +1,34 @@ +# == Schema Information +# Schema version: 20090115232435 +# +# Table name: stock_changes +# +# id :integer(4) not null, primary key +# delivery_id :integer(4) +# order_id :integer(4) +# article_id :integer(4) +# quantity :decimal(6, 2) default(0.0) +# created_at :datetime +# + +class StockChange < ActiveRecord::Base + belongs_to :delivery + belongs_to :order + belongs_to :article + + validates_presence_of :article_id, :quantity + validates_numericality_of :quantity + + after_save :update_article_quantity + after_destroy :remove_added_quantity + + protected + + def update_article_quantity + article.update_quantity(quantity) + end + + def remove_added_quantity + article.update_quantity(quantity * -1) + end +end diff --git a/app/views/deliveries/_form.html.haml b/app/views/deliveries/_form.html.haml new file mode 100644 index 00000000..5a813fa0 --- /dev/null +++ b/app/views/deliveries/_form.html.haml @@ -0,0 +1,16 @@ +- form_for([@supplier,@delivery]) do |f| + = f.error_messages + = f.hidden_field :supplier_id + %p + %b= f.label :delivered_on + = f.date_select :delivered_on + + %h2 Artikel + #stock_changes + = yield + %br/ + = add_article_link + %p + = f.submit "Speichern" + += link_to 'Zurück', supplier_deliveries_path(@supplier) \ No newline at end of file diff --git a/app/views/deliveries/_stock_change.html.haml b/app/views/deliveries/_stock_change.html.haml new file mode 100644 index 00000000..b698797e --- /dev/null +++ b/app/views/deliveries/_stock_change.html.haml @@ -0,0 +1,6 @@ +%p + - fields_for "delivery[stock_change_attributes][]", stock_change do |form| + = form.select :article_id, articles_for_select(@supplier), :prompt => " -- Artikel auswählen -- " + Menge + = form.text_field :quantity, :size => 5, :autocomplete => 'off' + = link_to_function "Löschen", "$(this).up('p').remove()" \ No newline at end of file diff --git a/app/views/deliveries/edit.html.erb b/app/views/deliveries/edit.html.erb deleted file mode 100644 index cad38f80..00000000 --- a/app/views/deliveries/edit.html.erb +++ /dev/null @@ -1,17 +0,0 @@ -

Editing delivery

- -<% form_for([@supplier,@delivery]) do |f| %> - <%= f.error_messages %> - <%= f.hidden_field :supplier_id %> - -

- <%= f.label :delivered_on %>
- <%= f.date_select :delivered_on %> -

-

- <%= f.submit "Update" %> -

-<% end %> - -<%= link_to 'Show', [@supplier,@delivery] %> | -<%= link_to 'Back', supplier_deliveries_path(@supplier) %> diff --git a/app/views/deliveries/edit.html.haml b/app/views/deliveries/edit.html.haml new file mode 100644 index 00000000..47c418ad --- /dev/null +++ b/app/views/deliveries/edit.html.haml @@ -0,0 +1,16 @@ +- title "Lieferung bearbeiten" + +%p + %b Lieferant: + = @supplier.name + +- render :layout => 'form' do + %table{:style => "width:40em"} + - for stock_change in @delivery.stock_changes + %tr[stock_change] + %td= stock_change.article.name + %td= stock_change.quantity + %td + = link_to_remote "Artikel entfernen", | + :url => drop_stock_change_supplier_delivery_path(@supplier, @delivery, :stock_change_id => stock_change), | + :method => :post | diff --git a/app/views/deliveries/index.html.erb b/app/views/deliveries/index.html.erb deleted file mode 100644 index 8bf63994..00000000 --- a/app/views/deliveries/index.html.erb +++ /dev/null @@ -1,22 +0,0 @@ -<% title "#{@supplier.name}/deliveries" %> - - - - - - - -<% for delivery in @deliveries %> - - - - - - - -<% end %> -
Delivered onInvoice
<%=h delivery.delivered_on %><%=h "invoice ..." %><%= link_to 'Show', [@supplier, delivery] %><%= link_to 'Edit', edit_supplier_delivery_path(@supplier,delivery) %><%= link_to 'Destroy', [@supplier,delivery], :confirm => 'Are you sure?', :method => :delete %>
- -
- -<%= link_to 'New delivery', new_supplier_delivery_path(@supplier) %> diff --git a/app/views/deliveries/index.html.haml b/app/views/deliveries/index.html.haml new file mode 100644 index 00000000..95111e11 --- /dev/null +++ b/app/views/deliveries/index.html.haml @@ -0,0 +1,16 @@ +- title "#{@supplier.name}/deliveries" + +%table{:style => "width:50em"} + %tr + %th Delivered on + %th Invoice + - for delivery in @deliveries + %tr + %td=h delivery.delivered_on + %td=h "invoice ..." + %td= link_to 'Show', [@supplier, delivery] + %td= link_to 'Edit', edit_supplier_delivery_path(@supplier,delivery) + %td= link_to 'Destroy', [@supplier,delivery], :confirm => 'Are you sure?', :method => :delete + +%br/ += link_to 'New delivery', new_supplier_delivery_path(@supplier) diff --git a/app/views/deliveries/new.html.erb b/app/views/deliveries/new.html.erb deleted file mode 100644 index 60388294..00000000 --- a/app/views/deliveries/new.html.erb +++ /dev/null @@ -1,16 +0,0 @@ -

New delivery

- -<% form_for([@supplier,@delivery]) do |f| %> - <%= f.error_messages %> - <%= f.hidden_field :supplier_id %> - -

- <%= f.label :delivered_on %>
- <%= f.date_select :delivered_on %> -

-

- <%= f.submit "Create" %> -

-<% end %> - -<%= link_to 'Back', supplier_deliveries_path(@supplier) %> diff --git a/app/views/deliveries/new.html.haml b/app/views/deliveries/new.html.haml new file mode 100644 index 00000000..7533a96b --- /dev/null +++ b/app/views/deliveries/new.html.haml @@ -0,0 +1,4 @@ +- title "Neue Lieferung von #{@supplier.name}" + +- render :layout => 'form' do + = render :partial => 'stock_change', :collection => @delivery.stock_changes \ No newline at end of file diff --git a/app/views/deliveries/show.html.erb b/app/views/deliveries/show.html.erb deleted file mode 100644 index d9ca7317..00000000 --- a/app/views/deliveries/show.html.erb +++ /dev/null @@ -1,13 +0,0 @@ -

- Supplier: - <%=h @delivery.supplier_id %> -

- -

- Delivered on: - <%=h @delivery.delivered_on %> -

- - -<%= link_to 'Edit', edit_supplier_delivery_path(@supplier,@delivery) %> | -<%= link_to 'Back', supplier_deliveries_path(@supplier) %> diff --git a/app/views/deliveries/show.html.haml b/app/views/deliveries/show.html.haml new file mode 100644 index 00000000..4b68cac7 --- /dev/null +++ b/app/views/deliveries/show.html.haml @@ -0,0 +1,25 @@ +- title "Lieferung anzeigen" + +%p + %b Lieferant: + =h @delivery.supplier.name +%p + %b Delivered on: + =h @delivery.delivered_on + +%h2 Artikel +%table.list{:style => "width:30em"} + %tr + %th Artikel + %th Einheit + %th Menge + - for stock_change in @delivery.stock_changes.find :all, :include => :article + %tr + %td= stock_change.article.name + %td= stock_change.article.unit + %td= stock_change.quantity + +%br/ += link_to 'Edit', edit_supplier_delivery_path(@supplier,@delivery) +| += link_to 'Back', supplier_deliveries_path(@supplier) \ No newline at end of file diff --git a/app/views/layouts/_main_tabnav.html.erb b/app/views/layouts/_main_tabnav.html.erb index 40fe2dd9..889d0357 100644 --- a/app/views/layouts/_main_tabnav.html.erb +++ b/app/views/layouts/_main_tabnav.html.erb @@ -23,7 +23,7 @@ { :name => "Manage orders", :url => "/orders", :access? => (u.role_orders?) } ] }, - { :name => "Articles", :url => "/articles", :active => ["articles", "suppliers"], + { :name => "Articles", :url => "/articles", :active => ["articles", "suppliers", "deliveries"], :access? => (u.role_article_meta? || u.role_suppliers?), :subnav => [ { :name => "Show articles", :url => "/articles/list" }, diff --git a/app/views/messages/_messages.html.haml b/app/views/messages/_messages.html.haml index f5155a55..b7738161 100644 --- a/app/views/messages/_messages.html.haml +++ b/app/views/messages/_messages.html.haml @@ -3,9 +3,8 @@ %tbody - for message in @messages %tr{:class => cycle('even','odd', :name => 'messages')} - %td - %b= h(message.sender_name) %td= format_subject(message, subject_length) + %td= h(message.sender_name) %td= format_time(message.created_at) %td= link_to('Antworten', reply_message_path(message)) \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index f06f33bc..88a82efa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,9 +17,9 @@ ActionController::Routing::Routes.draw do |map| finance.connect 'balancing/:action/:id', :controller => 'balancing' end - map.resources :suppliers, - :has_many => [:deliveries], - :collection => { :shared_suppliers => :get } + map.resources :suppliers, :collection => { :shared_suppliers => :get } do |suppliers| + suppliers.resources :deliveries, :member => { :drop_stock_change => :post } + end map.root :controller => 'home', :action => 'index' diff --git a/db/migrate/20090115232435_create_stock_changes.rb b/db/migrate/20090115232435_create_stock_changes.rb new file mode 100644 index 00000000..8264cd30 --- /dev/null +++ b/db/migrate/20090115232435_create_stock_changes.rb @@ -0,0 +1,17 @@ +class CreateStockChanges < ActiveRecord::Migration + def self.up + create_table :stock_changes do |t| + t.references :delivery + t.references :order + t.references :article + t.decimal :quantity, :precision => 6, :scale => 2, :default => 0.0 + t.datetime :created_at + end + + add_column :articles, :quantity, :decimal, :precision => 6, :scale => 2, :default => 0.0 + end + + def self.down + drop_table :stock_changes + end +end diff --git a/db/schema.rb b/db/schema.rb index ee909c10..16f9b97f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20090114101610) do +ActiveRecord::Schema.define(:version => 20090115232435) do create_table "article_categories", :force => true do |t| t.string "name", :default => "", :null => false @@ -36,6 +36,7 @@ ActiveRecord::Schema.define(:version => 20090114101610) do t.string "order_number" t.datetime "created_at" t.datetime "updated_at" + t.decimal "quantity", :precision => 6, :scale => 2, :default => 0.0 end add_index "articles", ["name", "supplier_id"], :name => "index_articles_on_name_and_supplier_id" @@ -184,7 +185,7 @@ ActiveRecord::Schema.define(:version => 20090114101610) do create_table "messages", :force => true do |t| t.integer "sender_id" - t.text "recipients_ids", :null => false + t.text "recipients_ids" t.string "subject", :null => false t.text "body" t.integer "email_state", :default => 0, :null => false @@ -241,6 +242,14 @@ ActiveRecord::Schema.define(:version => 20090114101610) do add_index "orders", ["finished"], :name => "index_orders_on_finished" add_index "orders", ["starts"], :name => "index_orders_on_starts" + create_table "stock_changes", :force => true do |t| + t.integer "delivery_id" + t.integer "order_id" + t.integer "article_id" + t.decimal "quantity", :precision => 6, :scale => 2, :default => 0.0 + t.datetime "created_at" + end + create_table "suppliers", :force => true do |t| t.string "name", :default => "", :null => false t.string "address", :default => "", :null => false diff --git a/test/fixtures/articles.yml b/test/fixtures/articles.yml index a7b92343..ab1fb097 100644 --- a/test/fixtures/articles.yml +++ b/test/fixtures/articles.yml @@ -1,5 +1,5 @@ # == Schema Information -# Schema version: 20090115123421 +# Schema version: 20090115232435 # # Table name: articles # diff --git a/test/fixtures/messages.yml b/test/fixtures/messages.yml index 14846ad0..508040a4 100644 --- a/test/fixtures/messages.yml +++ b/test/fixtures/messages.yml @@ -1,11 +1,11 @@ # == Schema Information -# Schema version: 20090115123421 +# Schema version: 20090115232435 # # Table name: messages # # id :integer(4) not null, primary key # sender_id :integer(4) -# recipients_ids :text default(""), not null +# recipients_ids :text # subject :string(255) not null # body :text # email_state :integer(4) default(0), not null diff --git a/test/fixtures/stock_changes.yml b/test/fixtures/stock_changes.yml new file mode 100644 index 00000000..0633241b --- /dev/null +++ b/test/fixtures/stock_changes.yml @@ -0,0 +1,28 @@ +# == Schema Information +# Schema version: 20090115232435 +# +# Table name: stock_changes +# +# id :integer(4) not null, primary key +# delivery_id :integer(4) +# order_id :integer(4) +# article_id :integer(4) +# quantity :decimal(6, 2) default(0.0) +# created_at :datetime +# + +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + delivery: + order: + article: + quantity: 1 + created_at: 2009-01-16 00:24:35 + +two: + delivery: + order: + article: + quantity: 1 + created_at: 2009-01-16 00:24:35 diff --git a/test/fixtures/suppliers.yml b/test/fixtures/suppliers.yml index 822a477c..b10f55e3 100644 --- a/test/fixtures/suppliers.yml +++ b/test/fixtures/suppliers.yml @@ -1,5 +1,5 @@ # == Schema Information -# Schema version: 20090115123421 +# Schema version: 20090115232435 # # Table name: suppliers # diff --git a/test/unit/stock_change_test.rb b/test/unit/stock_change_test.rb new file mode 100644 index 00000000..748fd103 --- /dev/null +++ b/test/unit/stock_change_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class StockChangeTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end