Skip to content

Instantly share code, notes, and snippets.

@petros
Created July 13, 2010 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petros/473780 to your computer and use it in GitHub Desktop.
Save petros/473780 to your computer and use it in GitHub Desktop.
Search conditions for a form_tag
def list_by_filter
@filterinfo = []
sql = "select i.* from installations i"
sqlconditions = ""
if params[:code] != nil
@code = params[:code]
sqlconditions += "code = '#{@code}'"
@filterinfo << "με κωδικό #{@code}"
end
if params[:customer_name] != nil
@customer_name = CGI::unescape(params[:customer_name])
if sqlconditions.length > 0
sqlconditions += " and "
end
sqlconditions += "CONCAT(last_name, ' ', first_name) like '#{@customer_name}' "
@filterinfo << "με επώνυμο και όνομα #{@customer_name}"
end
if params[:payment_type] != nil
@payment_type = CGI::unescape(params[:payment_type])
if sqlconditions.length > 0
sqlconditions += " and "
end
sqlconditions += "payment_type = '#{@payment_type}'"
@filterinfo << "με τρόπο πληρωμής #{@payment_type}"
end
if params[:commandstatus] != nil
@commandstatus = CGI::unescape(params[:commandstatus])
if sqlconditions.length > 0
sqlconditions += " and "
end
if @commandstatus == "Εκκρεμείς"
sqlconditions += "((select count(*) from commands where installation_id = i.id and status = 1) > 0 or (select count(*) from commands where installation_id = i.id) = 0)"
@filterinfo << "να έχει τουλάχιστον μια εντολή σε εκκρεμότητα"
elsif @commandstatus == "Ολοκληρωμένες"
sqlconditions += "(select status from commands where installation_id = i.id order by id DESC LIMIT 1) = 2"
@filterinfo << "με την τελευταία εντολή να είναι ολοκληρωμένη"
elsif @commandstatus == "Ακυρωμένες"
sqlconditions += "(select status from commands where installation_id = i.id order by id DESC LIMIT 1) = 3"
@filterinfo << "με την τελευταία εντολή να είναι ακυρωμένη"
end
end
if params[:commandtype] != nil
@commandtype = CGI::unescape(params[:commandtype])
if sqlconditions.length > 0
sqlconditions += " and "
end
if @commandtype == "Εγκατάσταση"
commandtype = 1
@filterinfo << "με την τελευταία εντολή να είναι εγκατάσταση"
elsif @commandtype == "Συντήρηση"
commandtype = 2
@filterinfo << "με την τελευταία εντολή να είναι συντήρηση"
else
commandtype = 3
@filterinfo << "με την τελευταία εντολή να είναι βλάβη"
end
sqlconditions += "(select command_type from commands where installation_id = i.id order by id desc limit 1) = #{commandtype.to_s}"
end
if params[:guarantee] != nil
@guarantee = CGI::unescape(params[:guarantee])
if sqlconditions.length > 0
sqlconditions += " and "
end
if @guarantee == "Εντός"
guarantee = 1
@filterinfo << "με την τελευταία εντολή να είναι εντός εγγύησης"
else
guarantee = 2
@filterinfo << "με την τελευταία εντολή να είναι εκτός εγγύησης"
end
sqlconditions += "(select guarantee from commands where installation_id = i.id order by id desc limit 1) = #{guarantee.to_s}"
end
if params[:ispaid] != nil
@ispaid = CGI::unescape(params[:ispaid])
if sqlconditions.length > 0
sqlconditions += " and "
end
if @ispaid == "Οχι"
ispaid = 0
@filterinfo << "με τουλάχιστον μια εκκρεμή ή ολοκληρωμένη εντολή που δεν έχει εξοφληθεί"
else
ispaid = 1
@filterinfo << "με τουλάχιστον μια εκκρεμή ή ολοκληρωμένη εντολή που έχει εξοφληθεί"
end
sqlconditions += "(select count(*) from commands where installation_id = i.id and is_paid = #{ispaid.to_s} and status <> 3) > 0"
end
if params[:store] != nil
@store = CGI::unescape(params[:store])
if sqlconditions.length > 0
sqlconditions += " and "
end
if @store == "[Μόνο από κεντρικά]"
sqlconditions += "store_id is null"
@filterinfo << "καταχωρήσεις που δημιουργήθηκαν απευθείας στα κεντρικά"
elsif @store == "[Μόνο από κατάστημα]"
sqlconditions += "store_id is not null"
@filterinfo << "καταχωρήσεις που δημιουργήθηκαν μέσω κάποιου καταστήματος"
else
sqlconditions += "store_id = #{@store}"
@filterinfo << "καταχωρήσεις που δημιουργήθηκαν μέσω του καταστήματος #{Store.find(@store).description}"
end
end
if params[:partner] != nil
@partner = CGI::unescape(params[:partner])
if sqlconditions.length > 0
sqlconditions += " and "
end
if @partner == "[Με εντολές για κεντρικά]"
sqlconditions += "(select count(*) from commands where commands.installation_id = i.id and partner_id is null) > 0"
@filterinfo << "με τουλάχιστον μια εντολή που ανατέθηκε στα κεντρικά"
else
sqlconditions += "(select count(*) from commands where commands.installation_id = i.id and partner_id = #{@partner}) > 0"
@filterinfo << "με τουλάχιστον μια εντολή που να ανατέθηκε στον συνεργάτη #{Partner.find(@partner).name}"
end
end
if params[:year] != nil
@year = params[:year]
if sqlconditions.length > 0
sqlconditions += " and "
end
sqlconditions += "YEAR(created_at) = #{@year}"
@filterinfo << "Το έτος #{@year}"
end
if params[:month] != nil
@month = CGI::unescape(params[:month])
if @month == "Ιανουάριος"
month = 1
altmonth = "Ιανουάριο"
elsif @month == "Φεβρουάριος"
month = 2
altmonth = "Φεβρουάριο"
elsif @month == "Μάρτιος"
month = 3
altmonth = "Μάρτιο"
elsif @month == "Απρίλιος"
month = 4
altmonth = "Απρίλιο"
elsif @month == "Μάιος"
month = 5
altmonth = "Μάιο"
elsif @month == "Ιούνιος"
month = 6
altmonth = "Ιούνιο"
elsif @month == "Ιούλιος"
month = 7
altmonth = "Ιούλιο"
elsif @month == "Αύγουστος"
month = 8
altmonth = "Αύγουστο"
elsif @month == "Σεπτέμβριος"
month = 9
altmonth = "Σεπτέμβριο"
elsif @month == "Οκτώβριος"
month = 10
altmonth = "Οκτώβριο"
elsif @month == "Νοέμβριος"
month = 11
altmonth = "Νοέμβριο"
elsif @month == "Δεκέμβριος"
month = 12
altmonth = "Δεκέμβριο"
end
if sqlconditions.length > 0
sqlconditions += " and "
end
sqlconditions += "MONTH(created_at) = #{month.to_s}"
@filterinfo << "Το μήνα #{altmonth}"
end
if params[:dateFrom] != nil
@dateFrom = params[:dateFrom]
if sqlconditions.length > 0
sqlconditions += " and "
end
sqlconditions += "created_at >= '#{@dateFrom}'"
@filterinfo << "με ημερομηνία δημιουργίας από #{@dateFrom}"
end
if params[:dateTo] != nil
@dateTo = params[:dateTo]
if sqlconditions.length > 0
sqlconditions += " and "
end
sqlconditions += "created_at <= '#{@dateTo}'"
@filterinfo << "με ημερομηνία δημιουργίας έως #{@dateTo}"
end
if sqlconditions.length > 0
sql += " where #{sqlconditions} order by id DESC"
@installations = Installation.find_by_sql(sql)
@total = @installations.length
@dopaginate = false
@sql=sql
else
list
end
render_action 'list'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment