.qi_header
  .right
    =link_to ic(:plus)+" Demande de commande", admin_p_customers_path(:offre => true), :class => "btn btn-primary btn-ap-add"
    
  %h1
    Ventes
    %span
      Demandes de commande


.qi_search_row
  =form_tag "", :method => "get",  :onsubmit => "" do
    -params[:search] =params[:search] || {}

    
    %table
      %tr
    
        -if false #!current_admin.p_commercial
          %td
            Commercial :
            =select_tag "search[p_commercial_id]", options_for_select([["",""],["Aucun","null"]]+PCommercial.order(:name).all.map{|a| [a.member_label, a.id]}, params[:search][:p_commercial_id])
          
        %td
          Statut :
          =select_tag "search[state]", options_for_select([["",""]]+PCustomerSheet.group(:state).all.map{|a| [a.state, a.state]}, params[:search][:state])
  
        %td
          Numéro d'offre :
        %td
          =text_field_tag "search[cc_com_counter]", params[:search][:cc_com_counter],:class => "form-control"
    %table
      %tr
        %td Date de commande
        %td
          Début
        %td
          .input-group
            =text_field_tag :start, params[:start],:class => "form-control datepicker", :placeholder => "Début"
            %span.input-group-addon.btn{:onclick => "$(this).prev('input').val('');"}
              =ic(:times)
        
        %td
          Fin
        %td
          .input-group
            =text_field_tag :stop, params[:stop],:class => "form-control datepicker", :placeholder => "Fin"
            %span.input-group-addon.btn{:onclick => "$(this).prev('input').val('');"}
              =ic(:times)
              

      %tr
        %td{:style =>"padding-right:10px;"} Date de livraison souhaitée
        %td
          Début
        %td
          .input-group
            =text_field_tag :wish_date_start, params[:wish_date_start],:class => "form-control datepicker", :placeholder => "Début"
            %span.input-group-addon.btn{:onclick => "$(this).prev('input').val('');"}
              =ic(:times)
        
        %td
          Fin
        %td
          .input-group
            =text_field_tag :wish_date_stop, params[:wish_date_stop],:class => "form-control datepicker", :placeholder => "Fin"
            %span.input-group-addon.btn{:onclick => "$(this).prev('input').val('');"}
              =ic(:times)
      
          


      
      
  
    =render :partial => "qi/qi_ordered_table_search_footer", :locals => {:collection_object => @p_customer_sheets}
.qi_kpi
  -params[:active_kpi] = [] if params[:active_kpi].to_s == ""

  
  
  -[["brouillon", "Brouillon", "#ffc107"], ["commande", "Commande", "#fd7e14"], ["en livraison", "En livraison", "#28a745"], ["facturée", "Facturée", "#3B7DDD"]].each do |state| #,["refusée", "Refusée", "#dc3545"]
    -p_customer_sheets = @p_customer_sheets.where(:state => state[0])
    
    -if p_customer_sheets.count > 0
      =link_to params.permit!.merge({:active_kpi => (["commandes-#{state[0]}"]).uniq,:p_customer_sheet_ids => p_customer_sheets.ids}), :class => ("active" if params[:active_kpi].include?("commandes-#{state[0]}")) do
        .kpi{:style => "background:#{state[2]};"}
          .number
            -if params[:kg]
              =number_with_delimiter(p_customer_sheets.sum(:cc_weight_tot).to_i, :delimiter => " ", :separator => ",")      
              Kg
        
            -else
              =number_to_currency (p_customer_sheets.sum(:cc_tot_amount_ht))

          .label

          .desc
            =p_customer_sheets.count
            commandes avec le statut
            ='"'+state[1].to_s+'"'
  
  
    
  -price_documents = PriceDocument.where(:price_document_type => [4]).where(:ref_element_type => "PCustomerSheet", :ref_element_id => @all_p_customer_sheets.ids).where(:cc_solded => false)
  
  
  =link_to admin_price_documents_path(:start => Date.today.beginning_of_year.strftime('%d/%m/%Y'), :stop => Date.today.end_of_year.strftime('%d/%m/%Y'), :price_document_type_ids => [4], :search => {:cc_solded => "Non"})  do
    .kpi{:style => "background:#f6c143;"}
    
      .number
        -if params[:kg]
          =number_with_delimiter(price_documents.sum(:cc_weight_tot).to_i, :delimiter => " ", :separator => ",")      
          Kg
        
        -else
          =number_to_currency (price_documents.sum(:cc_tot_amount_ht))
      

      .label

      .desc
        =price_documents.count
        factures restantes à payer
   
  
  -price_documents = PriceDocument.where(:price_document_type => [4]).where(:ref_element_type => "PCustomerSheet", :ref_element_id => @all_p_customer_sheets.ids).where(:cc_solded => false).where("cc_payment_end_at < ?", Date.today)
  
   
  =link_to admin_price_documents_path(:start => Date.today.beginning_of_year.strftime('%d/%m/%Y'), :stop => Date.today.end_of_year.strftime('%d/%m/%Y'), :price_document_type_ids => [4], :stop_cc_payment_end_at => (Date.today - 1.day).strftime('%d/%m/%Y'), :search => {:cc_solded => "Non"})  do
   
    .kpi.no_left_margin{:style => "background:#dd6b5a;"}
    
      .number
        -if params[:kg]
          =number_with_delimiter(price_documents.sum(:cc_weight_tot).to_i, :delimiter => " ", :separator => ",")     
          Kg
        
        -else
          =number_to_currency (price_documents.sum(:cc_tot_amount_ht))

      .label

      .desc
        dont
        =price_documents.count
        factures en retard de paiement
  
  -price_documents = PriceDocument.where(:price_document_type => [4]).where(:ref_element_type => "PCustomerSheet", :ref_element_id => @all_p_customer_sheets.ids).where(:cc_solded => true).where("cc_payment_end_at < ?", Date.today)
  
   
  =link_to admin_price_documents_path(:start => Date.today.beginning_of_year.strftime('%d/%m/%Y'), :stop => Date.today.end_of_year.strftime('%d/%m/%Y'), :price_document_type_ids => [4], :stop_cc_payment_end_at => (Date.today - 1.day).strftime('%d/%m/%Y'), :search => {:cc_solded => "Oui"})   do
   
    .kpi.no_left_margin{:style => "background:#57a36d;"}
    
      .number
        -if params[:kg]
          =number_with_delimiter(price_documents.sum(:cc_weight_tot).to_i, :delimiter => " ", :separator => ",")      
          Kg
        
        -else
          =number_to_currency (price_documents.sum(:cc_tot_amount_ht))

      .label

      .desc
        =price_documents.count
        factures réglées
        
        
  .clear
  
  -if params[:p_customer_sheet_ids].to_s != ""
    .custom_customers
      Affichage des résultats spécifiques pour 
      =params[:p_customer_sheet_ids].size
      commande(s)
    
      ="-"
      =link_to ic(:times)+" Supprimer ce filtre", params.permit!.merge({ :active_kpi => nil, :p_customer_sheet_ids => nil})


=render :partial => "qi/qi_ordered_table", :locals => {:qi_ordered_table_collection => @p_customer_sheets}
