Fixes orders.

This commit is contained in:
benni 2012-10-30 00:20:47 +01:00
parent 8f7e21b918
commit 16de9124fe
8 changed files with 78 additions and 106 deletions

View File

@ -109,7 +109,6 @@ function highlightRow(checkbox) {
} else {
row.removeClass('selected');
}
}
// Use with auto_complete to set a unique id,

View File

@ -69,6 +69,10 @@ table {
td.odd {
background-color: @tableBackgroundAccent;
}
tr.selected td {
background-color: @successBackground;
}
}
// Tasks ..

View File

@ -64,6 +64,7 @@ class OrdersController < ApplicationController
flash[:notice] = "Die Bestellung wurde erstellt."
redirect_to @order
else
logger.debug "[debug] order errors: #{@order.errors.messages}"
render :action => 'new'
end
end

View File

@ -20,7 +20,8 @@ class Order < ActiveRecord::Base
# Callbacks
after_update :update_price_of_group_orders
after_save :save_order_articles
# Finders
scope :open, where(state: 'open').order('ends DESC')
scope :finished, where("state = 'finished' OR state = 'closed'").order('ends DESC')
@ -47,25 +48,13 @@ class Order < ActiveRecord::Base
end
end
# Fetch last orders from same supplier, to generate an article selection proposal
def templates
if stockit?
Order.stockit :limit => 5
else
supplier.orders.finished :limit => 5
end
# Save ids, and create/delete order_articles after successfully saved the order
def article_ids=(ids)
@article_ids = ids
end
# Create or destroy OrderArticle associations on create/update
def article_ids=(ids)
# fetch selected articles
articles_list = Article.find(ids)
# create new order_articles
(articles_list - articles).each { |article| order_articles.build(:article => article) }
# delete old order_articles
articles.reject { |article| articles_list.include?(article) }.each do |article|
order_articles.detect { |order_article| order_article.article_id == article.id }.destroy
end
def article_ids
@article_ids ||= order_articles.map(&:article_id)
end
def open?
@ -83,12 +72,12 @@ class Order < ActiveRecord::Base
def expired?
!ends.nil? && ends < Time.now
end
# search GroupOrder of given Ordergroup
def group_order(ordergroup)
group_orders.where(:ordergroup_id => ordergroup.id).first
end
# Returns OrderArticles in a nested Array, grouped by category and ordered by article name.
# The array has the following form:
# e.g: [["drugs",[teethpaste, toiletpaper]], ["fruits" => [apple, banana, lemon]]]
@ -104,7 +93,7 @@ class Order < ActiveRecord::Base
a.article.article_category.name <=> b.article.article_category.name
end
end
# Returns the defecit/benefit for the foodcoop
# Requires a valid invoice, belonging to this order
#FIXME: Consider order.foodcoop_result
@ -115,7 +104,7 @@ class Order < ActiveRecord::Base
groups_sum - invoice.net_amount
end
end
# Returns the all round price of a finished order
# :groups returns the sum of all GroupOrders
# :clear returns the price without tax, deposit and markup
@ -182,7 +171,7 @@ class Order < ActiveRecord::Base
end
end
end
# Sets order.status to 'close' and updates all Ordergroup.account_balances
def close!(user)
raise "Bestellung wurde schon abgerechnet" if closed?
@ -221,11 +210,15 @@ class Order < ActiveRecord::Base
end
def include_articles
errors.add(:order_articles, "Es muss mindestens ein Artikel ausgewählt sein") if order_articles.empty?
errors.add(:articles, "Es muss mindestens ein Artikel ausgewählt sein") if article_ids.empty?
end
def save_order_articles
self.articles = Article.find(article_ids)
end
private
# Updates the "price" attribute of GroupOrders or GroupOrderResults
# This will be either the maximum value of a current order or the actual order value of a finished order.
def update_price_of_group_orders

View File

@ -1,63 +1,54 @@
= simple_form_for @order do |f|
.single_column
.box_title
%h2 Bestellung für #{@order.name}
.column_content
= f.hidden_field :supplier_id
= f.input :note
= f.input :starts
= f.input :ends
= f.hidden_field :supplier_id
= f.input :note, input_html: {rows: 8}
= f.input :starts, input_html: {class: 'input-small'}
= f.input :ends, input_html: {class: 'input-small'}
.box_title
%h2 Artikel
.column_content
- if (@template_orders && !@template_orders.empty?)
%p
%label{:for => 'template'} Benutze Artikelauswahl von
%select{:name => "template_id", :onchange => "useTemplate(this[this.selectedIndex].value)"}
%option{:value => "-1", :selected => "selected"} Bestellung auswählen...
- i = -1
- for order in @template_orders
%option{:value => (i += 1)}= "#{h(order.name)} bis #{order.ends.strftime('%d. %b')}"
%table.list
%tr
%th= check_box_tag 'checkall', "1", false, { 'data-check-all' => 'form.order' }
%th Name
%th Notiz
%h2 Artikel
- if @order.errors.has_key?(:articles)
.alert.alert-error
= @order.errors.get(:articles).join(" ")
%table.table.table-hover#articleList
%tr
%th= check_box_tag 'checkall', "1", false, { 'data-check-all' => '#articleList' }
%th Name
%th Notiz
- if @order.stockit?
%th Verfügbar
- else
%th Herkunft
%th Hersteller
%th Gebinde
%th Preis (netto/FC)
- for category_name, articles in @order.articles_for_ordering
%tr.article-category
%td
%td{:colspan => "6", :style => "text-align:left"}
= category_name
%i.icon-tag
- for article in articles
/ check if the article is selected
- included = @order.article_ids.include?(article.id)
- included_class = included ? ' selected' : ''
%tr{:class => included_class, :id => article.id.to_s }
%td= check_box_tag "order[article_ids][]", article.id, included, :id => "checkbox_#{article.id}"
%td.click-me{'data-check-this' => "#checkbox_#{article.id}"}= article.name
%td=h truncate article.note, :length => 25
- if @order.stockit?
%th Verfügbar
%td= "#{article.quantity_available} * #{article.unit}"
- else
%th Herkunft
%th Hersteller
%th Gebinde
%th Preis (netto/FC)
- for category_name, articles in @order.articles_for_ordering
%tr{:style => "background-color:#EFEFEF"}
%td
%td{:colspan => "6", :style => "text-align:left"}
%b=h category_name
- for article in articles
/ check if the article is selected
- included = @order.order_articles.detect { |order_article| order_article.article_id == article.id }
- included_class = included ? ' selected' : ''
%tr{:class => cycle('even', 'odd') + included_class, :id => article.id.to_s }
%td= check_box_tag "order[article_ids][]", article.id, included, :id => "checkbox_#{article.id}"
%td.click-me{'data-check-this' => "#checkbox_#{article.id}"}= article.name
%td=h truncate article.note, :length => 25
- if @order.stockit?
%td= "#{article.quantity_available} * #{article.unit}"
- else
%td=h truncate article.origin, :length => 15
%td=h truncate article.manufacturer, :length => 15
%td= "#{article.unit_quantity} x #{article.unit}"
%td= "#{number_to_currency(article.price)} / #{number_to_currency(article.fc_price)}"
%tr
%td{:colspan => "6"}
= check_box_tag 'checkall', "1", false, { 'data-check-all' => 'form.order' }
Alle auswählen
%td=h truncate article.origin, :length => 15
%td=h truncate article.manufacturer, :length => 15
%td= "#{article.unit_quantity} x #{article.unit}"
%td= "#{number_to_currency(article.price)} / #{number_to_currency(article.fc_price)}"
%tr
%td
= check_box_tag 'checkall', "1", false, { 'data-check-all' => '#articleList' }
%td{:colspan => "6"} Alle auswählen
- if (@template_orders && !@template_orders.empty?)
= render :partial => 'template_orders_script'
= f.submit
= link_to "oder abbrechen", orders_path
.form-actions
= f.submit class: 'btn'
= link_to "oder abbrechen", orders_path

View File

@ -1,17 +0,0 @@
:javascript
//
// Preset selected order articles per template order:
var template = new Array();
<haml:silent> current_article_ids = @order.supplier.articles.available.map(&:id)
</haml:silent><haml:silent> i = -1; for order in @template_orders
</haml:silent><haml:block> template[#{i += 1}] = new Array(#{current_article_ids.collect { |id| order.article_ids.include?(id) }.join(', ')});
</haml:block> // Call with index into template-array to select order articles from template.
function useTemplate(id) {
if (id >= 0 && id < template.length) {
<haml:silent> i = -1; for article_id in current_article_ids
</haml:silent><haml:block> var status = template[id][#{i += 1}]
$('checkbox_#{article_id}').checked = status;
highlightRow('#{article_id}',status);
</haml:block> }
}
//

View File

@ -1,13 +1,14 @@
- title "Bestellungen verwalten"
%p
- form_tag do
Neue Bestellung anlegen für
= select_tag :switch_supplier,
options_for_suppliers_to_select,
style: "font-size: 0.9em;margin-left:1em;",
'data-redirect-to' => true
.well
.btn-group.pull-right
= link_to '#', data: {toggle: 'dropdown'}, class: 'btn btn-primary dropdown-toggle' do
Neue Bestellung für ..
%span.caret
%ul.dropdown-menu
- Supplier.all.each do |supplier|
%li= link_to supplier.name, new_order_path(supplier_id: supplier.id), tabindex: -1
%h2 Laufende Bestellungen
- unless @open_orders.empty?
%table.table.table-striped

View File

@ -1,3 +1,3 @@
- title "Neue Bestellung anlegen"
= render :partial => 'form'
= render 'form'