move receive screen to orders

This commit is contained in:
wvengen 2013-12-18 21:06:05 +01:00
parent 94b4454a1b
commit d299fa4870
10 changed files with 75 additions and 79 deletions

View File

@ -1,56 +0,0 @@
class Finance::ReceiveController < Finance::BaseController
def edit
@order = Order.find(params[:id])
@order_articles = @order.order_articles.ordered.includes(:article)
end
def update
# where to leave remainder during redistribution
rest_to = []
rest_to << :tolerance if params[:rest_to_tolerance]
rest_to << :stock if params[:rest_to_stock]
rest_to << nil
# count what happens to the articles
counts = [0] * (rest_to.length+2)
cunits = [0] * (rest_to.length+2)
OrderArticle.transaction do
params[:order_articles].each do |oa_id, oa_params|
unless oa_params.blank?
oa = OrderArticle.find(oa_id)
# update attributes; don't use update_attribute because it calls save
# which makes received_changed? not work anymore
oa.attributes = oa_params
counts[0] += 1 if oa.units_received_changed?
cunits[0] += oa.units_received * oa.article.unit_quantity
unless oa.units_received.blank?
oacounts = oa.redistribute oa.units_received * oa.price.unit_quantity, rest_to
oacounts.each_with_index {|c,i| cunits[i+1]+=c; counts[i+1]+=1 if c>0 }
end
oa.save!
end
end
#flash[:notice] = I18n.t('finance.receive.update.notice')
notice = "Order received:"
notice += " #{counts.shift} articles (#{cunits.shift} units) updated"
notice += ", #{counts.shift} (#{cunits.shift}) using tolerance" if params[:rest_to_tolerance]
notice += ", #{counts.shift} (#{cunits.shift}) go to stock if foodsoft would support that" if params[:rest_to_stock]
notice += ", #{counts.shift} (#{cunits.shift}) left over"
flash[:notice] = notice
redirect_to finance_order_index_path
end
end
# ajax add article
def add_article
@order = Order.find(params[:receive_id])
@order_article = @order.order_articles.where(:article_id => params[:article_id]).includes(:article).first
# we need to create the order article if it's not part of the current order
if @order_article.nil?
@order_article = @order.order_articles.build({order: @order, article_id: params[:article_id]})
@order_article.save!
end
end
end

View File

@ -106,6 +106,27 @@ class OrdersController < ApplicationController
redirect_to orders_url, alert: I18n.t('errors.general_msg', :msg => error.message)
end
# ajax add article
def add_article
@order = Order.find(params[:id])
@order_article = @order.order_articles.where(:article_id => params[:article_id]).includes(:article).first
# we need to create the order article if it's not part of the current order
if @order_article.nil?
@order_article = @order.order_articles.build({order: @order, article_id: params[:article_id]})
@order_article.save!
end
end
def receive
@order = Order.find(params[:id])
unless request.post?
@order_articles = @order.order_articles.ordered.includes(:article)
else
flash[:notice] = "Order received: " + update_order_amounts
redirect_to @order
end
end
protected
# Renders the fax-text-file
@ -132,4 +153,37 @@ class OrdersController < ApplicationController
end
text
end
def update_order_amounts
# where to leave remainder during redistribution
rest_to = []
rest_to << :tolerance if params[:rest_to_tolerance]
rest_to << :stock if params[:rest_to_stock]
rest_to << nil
# count what happens to the articles
counts = [0] * (rest_to.length+2)
cunits = [0] * (rest_to.length+2)
OrderArticle.transaction do
params[:order_articles].each do |oa_id, oa_params|
unless oa_params.blank?
oa = OrderArticle.find(oa_id)
# update attributes; don't use update_attribute because it calls save
# which makes received_changed? not work anymore
oa.attributes = oa_params
counts[0] += 1 if oa.units_received_changed?
cunits[0] += oa.units_received * oa.article.unit_quantity
unless oa.units_received.blank?
oacounts = oa.redistribute oa.units_received * oa.price.unit_quantity, rest_to
oacounts.each_with_index {|c,i| cunits[i+1]+=c; counts[i+1]+=1 if c>0 }
end
oa.save!
end
end
end
notice = " #{counts.shift} articles (#{cunits.shift} units) updated"
notice += ", #{counts.shift} (#{cunits.shift}) using tolerance" if params[:rest_to_tolerance]
notice += ", #{counts.shift} (#{cunits.shift}) go to stock if foodsoft would support that" if params[:rest_to_stock]
notice += ", #{counts.shift} (#{cunits.shift}) left over"
end
end

View File

@ -40,7 +40,7 @@
function init_add_article(sel) {
$(sel).removeAttr('disabled').select2({
placeholder: '#{t '.add_article'}',
placeholder: '#{t 'orders.add_article.title'}',
data: new_article_data,
// TODO implement adding a new article, like in deliveries
}).on('change', function(e) {
@ -50,7 +50,7 @@
}
$.ajax({
url: '#{finance_receive_add_article_path(@order)}',
url: '#{add_article_order_path(@order)}',
type: 'get',
data: {article_id: selectedArticle.id},
contentType: 'application/json; charset=UTF-8'
@ -71,8 +71,8 @@
%table.ordered-articles.table.table-striped.stupidtable
%thead
%tr
%th.sort{:data => {:sort => 'string'}}= t('.number')
%th.sort{:data => {:sort => 'string'}}= t('.article')
%th.sort{:data => {:sort => 'string'}}= heading_helper Article, :order_number, short: true
%th.sort{:data => {:sort => 'string'}}= heading_helper Article, :name
%th= heading_helper GroupOrderArticle, :units
%th Members
%th Ordered
@ -81,7 +81,7 @@
%th
%tbody#result_table
- @order_articles.each do |order_article|
= render :partial => 'edit_article', :locals => {:order_article => order_article}
= render :partial => 'edit_amount', :locals => {:order_article => order_article}
%tfoot
%tr
%th{:colspan => 8}

View File

@ -54,7 +54,7 @@
%td= truncate(order.note)
%td
-# TODO btn-success class only if not received before
= link_to t('.receive'), edit_finance_receive_path(order), class: 'btn btn-small btn-success'
= link_to t('.receive'), receive_order_path(order), class: 'btn btn-small btn-success'
%td
= link_to t('ui.edit'), '#', class: 'btn btn-small disabled'

View File

@ -1,8 +1,8 @@
- title "Receiving #{@order.name}"
= form_tag(finance_receive_path(@order), :method => :put) do
= form_tag(receive_order_path(@order)) do
%section#results
= render 'edit_articles'
= render 'edit_amounts'
.form-actions
.pull-left
@ -18,6 +18,6 @@
.pull-right
= submit_tag t('.submit'), class: 'btn btn-primary'
= link_to t('ui.or_cancel'), finance_order_index_path
= link_to t('ui.or_cancel'), order_path(@order)
%p= link_to_top

View File

@ -31,6 +31,9 @@
= link_to t('ui.edit'), edit_order_path(@order), class: 'btn'
= link_to t('.action_end'), finish_order_path(@order), method: :post, class: 'btn btn-success',
confirm: t('.confirm_end', order: @order.name)
- elsif not @order.closed?
-# TODO btn-success class only if not received before
= link_to t('orders.index.receive'), receive_order_path(@order), class: 'btn btn-success'
- unless @order.closed?
= link_to t('ui.delete'), @order, confirm: t('.confirm_delete'), method: :delete,
class: 'btn btn-danger'

View File

@ -541,7 +541,6 @@ en:
last_edited_by: Last edited by
name: Supplier
no_closed_orders: At the moment there are no closed orders.
receive: receive
state: State
summary:
changed: Data was changed!
@ -625,15 +624,6 @@ en:
ordergroups:
account_statement: Account statement
new_transaction: New transaction
receive:
add_article:
notice: Article "%{name}" was added to the order.
edit:
submit: Receive order
edit_articles:
add_article: Add article
update:
notice: Updated received article amounts for order
update:
notice: Invoice was updated
foodcoop:
@ -1093,6 +1083,9 @@ en:
error_single_group: ! '%{user} is already a member of another ordergroup'
invalid_balance: is not a valid number
orders:
add_article:
title: Add article
notice: Article "%{name}" was added to the order.
articles:
article_count: ! 'Ordered articles:'
prices: Net/gross price
@ -1138,6 +1131,8 @@ en:
warning_ordered_stock: ! 'Warning: Articles marked red have already been ordered/purchased within this open stock order. If you uncheck them here, all existing orders/purchases of these articles will be deleted and it will not be accounted for them.'
new:
title: Create new order
receive:
submit: Receive order
show:
action_end: Close!
amounts: ! 'Net/gross sum:'

View File

@ -38,6 +38,10 @@ Foodsoft::Application.routes.draw do
member do
post :finish
post :add_comment
get :receive
post :receive
get :add_article
end
end
@ -146,10 +150,6 @@ Foodsoft::Application.routes.draw do
resources :order_articles
end
resources :receive do
get :add_article
end
resources :group_order_articles do
member do
put :update_result