Guest User

Untitled

a guest
Oct 25th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 7.19 KB | None | 0 0
  1. # Here is the part where the data is being parsed in Controller:
  2.  
  3.     trip_rows = Trip.trips_report(@start_date, @end_date,
  4.                                   @driver    ? @driver.cnf_id : -1,
  5.                                   @conductor ? @conductor.cnf_id : -1,
  6.                                   @bus       ? @bus.cnf_id : -1,
  7.                                   @trip      ? @trip.route : -1)
  8.  
  9.     trip_rows.each do |t|
  10.  
  11.       # HERE COMES THE SLOWEST PART (fix numeric fields):
  12.  
  13.       t_cnf_ticket_id    = t.cnf_ticket_id.to_i
  14.       t_trip_id          = t.trip_id.to_i
  15.       t_count            = t.count.to_i
  16.       t_vat              = t.vat.to_f
  17.       t_discount         = t.discount.to_f
  18.       t_price            = t.price.to_f
  19.       t_cancelled_count  = t.cancelled_count.to_i
  20.       t_cancelled_price  = t.cancelled_price.to_f
  21.       t_driver_cnf_id    = t.driver_cnf_id.to_i
  22.       t_conductor_cnf_id = t.conductor_cnf_id.to_i
  23.       t_bus_cnf_id       = t.bus_cnf_id.to_i
  24.       t_ticket_number    = t.ticket_number.to_i
  25.       t_paym_type        = t.paym_type.to_i
  26.       t_route            = t.route
  27.  
  28.       # STOP SLOWEST PART
  29.  
  30. # The model method trips_report() collects data for the controller:
  31.  
  32.   # trips report                                                                                                                                                                                                                              
  33.   #                                                                                                                                                                                                                                            
  34.   # Report of trips matching filter. Select trips between dates                                                                                                                                                                                
  35.   # filtering by zero or more of driver, conductor, bus and trip.                                                                                                                                                                              
  36.   #                                                                                                                                                                                                                                            
  37.   # args: (start date, end date, driver, conductor, bus, trip)                                                                                                                                                                                
  38.   def self.trips_report(s, e, d, c, b, t)
  39.     args = Hash.new
  40.  
  41.     args[:trips] = Hash.new
  42.     args[:trips][:start_dt] = s..e+1.day
  43.  
  44.     args[:route] = t if t > -1
  45.  
  46.     args[:s] = Hash.new
  47.     args[:s][:driver_cnf_id] = d if d > -1
  48.     args[:s][:conductor_cnf_id] = c if c > -1
  49.     args[:s][:bus_cnf_id] = b if b > -1
  50.  
  51.     args[:c] = Hash.new
  52.     args[:c][:is_test] = false
  53.  
  54.  
  55.     order(:start_dt, :route)                                                                    \
  56.     .joins("LEFT OUTER JOIN shifts s ON s.id=trips.shift_id")                                   \
  57.     .joins("LEFT OUTER JOIN tickets ti ON ti.trip_id=trips.id")                                 \
  58.     .joins("LEFT OUTER JOIN cnf_tickets ct ON ct.id=ti.cnf_ticket_id")                          \
  59.     .joins("LEFT OUTER JOIN cnf_buses cb ON cb.cnf_id=s.bus_cnf_id")                            \
  60.     .joins("LEFT OUTER JOIN clients c ON c.serial=s.serial")                                    \
  61.     .where(args)                                                                                \
  62.     .group("ti.paym_type"                                                                       \
  63.            ",ct.id"                                                                             \
  64.            ",ct.title"                                                                          \
  65.            ",ct.comment"                                                                        \
  66.            ",trips.id"                                                                          \
  67.            ",trips.route"                                                                       \
  68.            ",trips.name"                                                                        \
  69.            ",trips.start_dt"                                                                    \
  70.            ",s.driver_cnf_id"                                                                   \
  71.            ",s.conductor_cnf_id"                                                                \
  72.            ",s.bus_cnf_id"                                                                      \
  73.            ",s.serial"                                                                          \
  74.            ",cb.number"                                                                         \
  75.            ",ti.number"                                                                         \
  76.            ",ti.cancelled")                                                                     \
  77.     .select("trips.id AS trip_id"                                                               \
  78.             ",ct.id AS cnf_ticket_id"                                                           \
  79.             ",trips.route"                                                                      \
  80.             ",trips.name"                                                                       \
  81.             ",trips.start_dt"                                                                   \
  82.             ",s.driver_cnf_id"                                                                  \
  83.             ",s.conductor_cnf_id"                                                               \
  84.             ",s.bus_cnf_id"                                                                     \
  85.             ",s.serial"                                                                         \
  86.             ",ct.title"                                                                         \
  87.             ",ct.comment"                                                                       \
  88.             ",ti.paym_type"                                                                     \
  89.             ",CASE WHEN ti.cancelled IS NULL THEN COUNT(ti) END AS count"                       \
  90.             ",CASE WHEN ti.cancelled IS NULL THEN SUM(ti.vat) END AS vat"                       \
  91.             ",CASE WHEN ti.cancelled IS NULL THEN SUM(ti.discount) END AS discount"             \
  92.             ",CASE WHEN ti.cancelled IS NULL THEN SUM(ti.price) END AS price"                   \
  93.             ",CASE WHEN ti.cancelled IS NOT NULL THEN COUNT(ti) END AS cancelled_count"         \
  94.             ",CASE WHEN ti.cancelled IS NOT NULL THEN SUM(ti.price) END AS cancelled_price"     \
  95.             ",(SELECT p2.name FROM cnf_personnels p2 WHERE p2.cnf_id=conductor_cnf_id) AS c_name"\
  96.             ",(SELECT p2.name FROM cnf_personnels p2 WHERE p2.cnf_id=driver_cnf_id) AS d_name"  \
  97.             ",ti.number AS ticket_number"                                                       \
  98.             ",cb.number")
  99.   end
Advertisement
Add Comment
Please, Sign In to add comment