Created
August 23, 2011 10:16
-
-
Save gamov/1164794 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_deliver_partially | |
@purchase_order = PurchaseOrder.find(params[:id], :include => {:purchased_items => {:item_variant => :item_family}}) | |
# authorize! :update | |
@purchase_order.attributes = params[:purchase_order] | |
@stock_receipt = @purchase_order.stock_receipts.to_a.find{|sr| sr.new_record?} | |
#logger.debug @stock_receipt.inspect | |
# @purchased_items.each {|pi| p "in controller #{pi.wh_alloc_hash.inspect}"} | |
# @purchased_items.each {|pi| p pi.delivering_quantity} | |
setup_whs | |
@purchased_items = @purchase_order.purchased_items.reject{|pi| pi.balance == 0} #if call .deliverable, it is reloaded from DB | can also check delivering_quantity? | |
# p @purchase_order.valid? | |
# p @purchase_order.errors.inspect | |
respond_to do |format| | |
begin | |
unless @purchase_order.valid? # validates the associated pos | |
logger.warn "Purchase Order validation failed: #{@purchase_order.errors.full_messages}" | |
format.html { render :action => "deliver_partially" } | |
# return | |
end | |
PurchaseOrder.transaction do | |
raise 'Some allocations don\'t tally, please adjust accordingly' unless @purchase_order.purchased_items.all?{|pi| pi.valid?} #we make sure all are valid before trying to transfer to stock | |
#This comes before the delivery some because the workflow event seems to remove the pis wh_alloc.... Maybe because of the nested transaction... try without it | |
@stock_receipt.record_delivering_pis(@purchase_order.purchased_items) | |
@stock_receipt.executed = true | |
raise 'Receipt information not valid, see below.' unless @stock_receipt.valid? | |
@purchase_order.deliver_some | |
@purchase_order.save! | |
logger.debug 'All OK' | |
end | |
logger.debug "SR >#{@stock_receipt.valid?}<" | |
#flash[:notice] = 'Purchased items were transferred to stock and marked as delivered.' | |
format.html { redirect_to([@purchase_order, @stock_receipt], :notice => 'Purchased items were transferred to stock and marked as delivered.') } | |
#format.html {redirect_to @purchase_order} | |
#format.html { redirect_to( purchase_order_stock_receipt_path(@purchase_order, @stock_receipt), :notice => 'Purchased items were transferred to stock and marked as delivered.') } | |
format.xml { head :ok } | |
rescue Exception => e | |
raise e if DEBUG_FLAG | |
#We need to adjust the delivering quantities of the already processed items... () | |
#@purchased_items.each{|pi| pi.delivering_quantity= pi.sum_alloc unless pi.delivering_quantity == nil} | |
logger.error "Error applying partial delivery changes: "+ e.message | |
flash.now[:error] = "Error applying partial deliver changes: #{e.message}." #if @purchase_order.errors.empty? | |
format.html { render :action => "deliver_partially" } | |
format.xml { render :xml => @purchase_order.errors, :status => :unprocessable_entity } | |
# raise e if DEBUG_FLAG | |
end | |
end #respond_to | |
logger.debug 'End of update' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment