.qi_header
  %h1
    ODR
    %span
      
      Statistiques
    %span
      =@m_odr.p_customer.show_name
      ="-"
      =@m_odr.name


.qi_row
  .qi_pannel.qi_plain.padding
    
    %div{:style => "overflow:auto;"}
      -@m_odr_reps = @m_odr.m_odr_reps
    
      %table.table.table-striped.table-hover.table-bordered
        %tr
          %th
          %th{:rowspan => 2} Nombre de participation
          -@m_odr_reps.group(:state).each do |state|
            %th{:style => "font-weight:normal", :colspan => 5+(@m_odr.m_odr_brands.count*2)+(@m_odr.m_odr_products.count*2)}
              Etat :
              =state_helper state.state
      
        %tr
          %th
      
          -@m_odr_reps.group(:state).each do |state|
            %th Nbr participation
            %th Nbr pneus
            
            -@m_odr_reps.group(:qte).order(:qte).each do |qte|
              %th 
                Nbr part. avec
                =qte.qte
                pneus
          
            -@m_odr.m_odr_brands.each do |m_odr_brand|
              %th 
                =m_odr_brand.name
                Nbr part.
              %th 
                =m_odr_brand.name
                Nbr pneus
                
              
              -m_odr_brand.m_odr_products.each do |m_odr_product|
                %th 
                  =m_odr_product.name
                  Nbr part.
                %th 
                  =m_odr_product.name
                  Nbr pneus
          
          
          
            %th Total remises
        -m_odr_reps = @m_odr_reps
        %tr
          %td Général
          %td=m_odr_reps.count
          -@m_odr_reps.group(:state).each do |state|
            %td
              =m_odr_reps.where(:state => state.state).count
            %td
              =m_odr_reps.where(:state => state.state).sum(:qte)
            
            -@m_odr_reps.group(:qte).order(:qte).each do |qte|
              %th 
                =m_odr_reps.where(:state => state.state, :qte => qte.qte).count
            
            
            -@m_odr.m_odr_brands.each do |m_odr_brand|
              %td=m_odr_reps.where(:state => state.state, :m_odr_product_id => m_odr_brand.m_odr_products.ids).count
              %td=m_odr_reps.where(:state => state.state, :m_odr_product_id => m_odr_brand.m_odr_products.ids).sum(:qte)
              
              -m_odr_brand.m_odr_products.each do |m_odr_product|
                %td=m_odr_reps.where(:state => state.state, :m_odr_product_id => m_odr_product.id).count
                %td=m_odr_reps.where(:state => state.state, :m_odr_product_id => m_odr_product.id).sum(:qte)
            
            %td.numeraire{:style => ("color:red;" if total = m_odr_reps.where(:state => state.state).sum(:cc_remise) and total == 0.0)}
              =number_to_currency total
        
        -nbr_participations_for_places = []
        -nbr_pneus_for_places = []
        
        -@m_odr_reps.group(:m_odr_place_id).joins(:m_odr_place).order("m_odr_places.name").each do |place|
          -m_odr_reps = @m_odr_reps.where(:m_odr_place_id => place.m_odr_place_id)
        
          %tr
            %td=place.m_odr_place.name
            %td=m_odr_reps.count
            -@m_odr_reps.group(:state).each do |state|
              %td
                =p = m_odr_reps.where(:state => state.state).count
                -nbr_participations_for_places << p if p > 0
              %td
                =q = m_odr_reps.where(:state => state.state).sum(:qte)
                -nbr_pneus_for_places << q if p > 0
              -@m_odr_reps.group(:qte).order(:qte).each do |qte|
                %th 
                  =m_odr_reps.where(:state => state.state, :qte => qte.qte).count
              
              -@m_odr.m_odr_brands.each do |m_odr_brand|
                %td=m_odr_reps.where(:state => state.state, :m_odr_product_id => m_odr_brand.m_odr_products.ids).count
                %td=m_odr_reps.where(:state => state.state, :m_odr_product_id => m_odr_brand.m_odr_products.ids).sum(:qte)
                
                -m_odr_brand.m_odr_products.each do |m_odr_product|
                  %td=m_odr_reps.where(:state => state.state, :m_odr_product_id => m_odr_product.id).count
                  %td=m_odr_reps.where(:state => state.state, :m_odr_product_id => m_odr_product.id).sum(:qte)
              
              %td.numeraire{:style => ("color:red;" if total = m_odr_reps.where(:state => state.state).sum(:cc_remise) and total == 0.0)}
                =number_to_currency total
                
                

    
    %p
      Moyenne participation / point de vente avec participation :
      =(nbr_participations_for_places.inject{ |sum, el| sum + el }.to_f / nbr_participations_for_places.size).round(2)
    %p
      Moyenne nbr pneus / point de vente avec participation :
      =(nbr_pneus_for_places.inject{ |sum, el| sum + el }.to_f / nbr_pneus_for_places.size).round(2)