<% if current_user.manager_id @records = ActiveRecord::Base.connection.execute("select r.date, m.name, count(rpt.id) as points_count, sum(case when docs.count > 0 then 1 else 0 end) as visited_points from template_route_points rpt inner join template_routes rt on rt.id = rpt.template_route_id AND rt.manager_id = #{current_user.manager_id} inner join shipping_addresses sa on rpt.shipping_address_id = sa.id left join routes r on r.date = '#{Date.today.strftime("%Y-%m-%d")}' AND rt.manager_id = r.manager_id AND date_part('isodow', r.date) = rt.day_of_week left join route_points rp on r.id = rp.route_id and rp.shipping_address_id = rpt.shipping_address_id left join managers m on rt.manager_id = m.id left join (select route_point_id, sum(count) as count from (select route_point_id, count(*) as count from orders o group by route_point_id union all select route_point_id, count(*) as count from audit_documents a group by route_point_id union all select route_point_id, count(*) as count from refunds r group by route_point_id union all select route_point_id, count(*) as count from route_point_photos rph group by route_point_id ) data group by route_point_id) docs on rp.id = docs.route_point_id where rpt.validity = true AND NOT r.date is null group by r.date, m.name order by r.date") end %>

<% if @records and @records.first %> <%= @records.first['visited_points'].to_f >0 ? (@records.first['visited_points'].to_f / @records.first['points_count'].to_f * 100).round(2) : 0 %> % <%else%> 0% <%end%>