Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Here is the part where the data is being parsed in Controller:
- trip_rows = Trip.trips_report(@start_date, @end_date,
- @driver ? @driver.cnf_id : -1,
- @conductor ? @conductor.cnf_id : -1,
- @bus ? @bus.cnf_id : -1,
- @trip ? @trip.route : -1)
- trip_rows.each do |t|
- # HERE COMES THE SLOWEST PART (fix numeric fields):
- t_cnf_ticket_id = t.cnf_ticket_id.to_i
- t_trip_id = t.trip_id.to_i
- t_count = t.count.to_i
- t_vat = t.vat.to_f
- t_discount = t.discount.to_f
- t_price = t.price.to_f
- t_cancelled_count = t.cancelled_count.to_i
- t_cancelled_price = t.cancelled_price.to_f
- t_driver_cnf_id = t.driver_cnf_id.to_i
- t_conductor_cnf_id = t.conductor_cnf_id.to_i
- t_bus_cnf_id = t.bus_cnf_id.to_i
- t_ticket_number = t.ticket_number.to_i
- t_paym_type = t.paym_type.to_i
- t_route = t.route
- # STOP SLOWEST PART
- # The model method trips_report() collects data for the controller:
- # trips report
- #
- # Report of trips matching filter. Select trips between dates
- # filtering by zero or more of driver, conductor, bus and trip.
- #
- # args: (start date, end date, driver, conductor, bus, trip)
- def self.trips_report(s, e, d, c, b, t)
- args = Hash.new
- args[:trips] = Hash.new
- args[:trips][:start_dt] = s..e+1.day
- args[:route] = t if t > -1
- args[:s] = Hash.new
- args[:s][:driver_cnf_id] = d if d > -1
- args[:s][:conductor_cnf_id] = c if c > -1
- args[:s][:bus_cnf_id] = b if b > -1
- args[:c] = Hash.new
- args[:c][:is_test] = false
- order(:start_dt, :route) \
- .joins("LEFT OUTER JOIN shifts s ON s.id=trips.shift_id") \
- .joins("LEFT OUTER JOIN tickets ti ON ti.trip_id=trips.id") \
- .joins("LEFT OUTER JOIN cnf_tickets ct ON ct.id=ti.cnf_ticket_id") \
- .joins("LEFT OUTER JOIN cnf_buses cb ON cb.cnf_id=s.bus_cnf_id") \
- .joins("LEFT OUTER JOIN clients c ON c.serial=s.serial") \
- .where(args) \
- .group("ti.paym_type" \
- ",ct.id" \
- ",ct.title" \
- ",ct.comment" \
- ",trips.id" \
- ",trips.route" \
- ",trips.name" \
- ",trips.start_dt" \
- ",s.driver_cnf_id" \
- ",s.conductor_cnf_id" \
- ",s.bus_cnf_id" \
- ",s.serial" \
- ",cb.number" \
- ",ti.number" \
- ",ti.cancelled") \
- .select("trips.id AS trip_id" \
- ",ct.id AS cnf_ticket_id" \
- ",trips.route" \
- ",trips.name" \
- ",trips.start_dt" \
- ",s.driver_cnf_id" \
- ",s.conductor_cnf_id" \
- ",s.bus_cnf_id" \
- ",s.serial" \
- ",ct.title" \
- ",ct.comment" \
- ",ti.paym_type" \
- ",CASE WHEN ti.cancelled IS NULL THEN COUNT(ti) END AS count" \
- ",CASE WHEN ti.cancelled IS NULL THEN SUM(ti.vat) END AS vat" \
- ",CASE WHEN ti.cancelled IS NULL THEN SUM(ti.discount) END AS discount" \
- ",CASE WHEN ti.cancelled IS NULL THEN SUM(ti.price) END AS price" \
- ",CASE WHEN ti.cancelled IS NOT NULL THEN COUNT(ti) END AS cancelled_count" \
- ",CASE WHEN ti.cancelled IS NOT NULL THEN SUM(ti.price) END AS cancelled_price" \
- ",(SELECT p2.name FROM cnf_personnels p2 WHERE p2.cnf_id=conductor_cnf_id) AS c_name"\
- ",(SELECT p2.name FROM cnf_personnels p2 WHERE p2.cnf_id=driver_cnf_id) AS d_name" \
- ",ti.number AS ticket_number" \
- ",cb.number")
- end
Advertisement
Add Comment
Please, Sign In to add comment