Skip to content

Commit

Permalink
Improved calculate_duration method on model Order to properly handle …
Browse files Browse the repository at this point in the history
…missing dates
  • Loading branch information
elshaka committed Apr 28, 2012
1 parent d210d82 commit 4af1bab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
23 changes: 11 additions & 12 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,19 @@ def calculate_duration
start_date = Batch.where(:order_id=>self.id).minimum('created_at')
last_batch = Batch.find(:first, :conditions => ["number = ? and order_id = ?", Batch.where(:order_id=>self.id).maximum('number'), self.id])
end_date = BatchHopperLot.where(:batch_id=>last_batch.id).maximum('created_at')
unless end_date.nil?
return {
'start_date' => start_date.strftime("%d/%m/%Y %H:%M:%S"),
'end_date' => end_date.strftime("%d/%m/%Y %H:%M:%S"),
'duration' => (end_date.to_i - start_date.to_i) / 60.0
}
else
return {
'start_date' => start_date.strftime("%d/%m/%Y %H:%M:%S"),
'end_date' => "??/??/???? ??:??:??",
'duration' => 0
}

start_date_string = start_date.strftime("%d/%m/%Y %H:%M:%S") rescue "??/??/???? ??:??:??"
end_date_string = end_date.strftime("%d/%m/%Y %H:%M:%S") rescue "??/??/???? ??:??:??"
duration_value = 0
unless start_date.nil? or end_date.nil?
duration_value = (end_date.to_i - start_date.to_i) / 60.0
end

return {
'start_date' => start_date_string,
'end_date' => end_date_string,
'duration' => duration_value
}
end

def get_real_batches
Expand Down
31 changes: 14 additions & 17 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
t.integer "recipe_id"
t.integer "client_id"
t.integer "user_id"
t.integer "product_id"
t.integer "product_lot_id"
t.integer "prog_batches", :null => false
t.integer "real_batches"
t.string "code", :null => false
Expand All @@ -129,7 +129,7 @@
end

add_index "orders", ["client_id"], :name => "fk_orders_client_id"
add_index "orders", ["product_id"], :name => "fk_orders_product_id"
add_index "orders", ["product_lot_id"], :name => "fk_orders_product_lot_id"
add_index "orders", ["recipe_id"], :name => "fk_orders_recipe_id"
add_index "orders", ["user_id"], :name => "fk_orders_user_id"

Expand Down Expand Up @@ -164,12 +164,15 @@
end

create_table "products_lots", :force => true do |t|
t.integer "order_id"
t.string "number", :null => false
t.integer "product_id"
t.string "code", :null => false
t.date "date"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "products_lots", ["product_id"], :name => "fk_products_lots_product_id"

create_table "recipes", :force => true do |t|
t.string "code"
t.string "name", :null => false
Expand All @@ -188,13 +191,6 @@
t.datetime "updated_at"
end

create_table "roles_users", :force => true do |t|
t.integer "role_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "schedules", :force => true do |t|
t.string "name"
t.time "start_hour"
Expand All @@ -212,11 +208,12 @@
end

create_table "transactions", :force => true do |t|
t.integer "transaction_type_id"
t.integer "warehouse_id"
t.integer "user_id"
t.datetime "date"
t.float "amount"
t.integer "transaction_type_id", :null => false
t.integer "warehouse_id", :null => false
t.integer "user_id", :null => false
t.string "code", :null => false
t.date "date", :null => false
t.float "amount", :null => false
t.string "comment"
t.datetime "created_at"
t.datetime "updated_at"
Expand All @@ -236,7 +233,7 @@
end

create_table "warehouses", :force => true do |t|
t.integer "warehouse_type_id", :null => false
t.integer "warehouse_type_id"
t.integer "content_id", :null => false
t.string "code", :null => false
t.string "location", :null => false
Expand Down

0 comments on commit 4af1bab

Please sign in to comment.