.qi_header
  
  .right
    =raw @p_customer_sheet.state_html
    
    =#debug @p_customer_sheet.archive_now
    
  
    
    -if !@p_customer_sheet.archived and (current_admin.has_permission?("gestion-des-commandes") or @p_customer_sheet.demande_type.to_s == "Brouillon" or @p_customer_sheet.state == "commande")
    
      = link_to i(:pencil), edit_admin_p_customer_sheet_path(@p_customer_sheet), :remote => false
  
  
  %h1
    Ventes
    %span
      =link_to "Demande de commandes",admin_p_customer_sheets_path
    %span
      ="#{@p_customer_sheet.com_counter}"
  

.qi_row
  .qi_pannel.qi_plain.padding
    
    
    
    -if @bon_de_commande = @p_customer_sheet.price_documents.where(:cc_label => "Bon de commande").first
      -price_line_block = @bon_de_commande.price_line_block
      %h3
        Contenu du bon de commande 
        =@bon_de_commande.d_number
        =":"
    -else
      -price_line_block = @p_customer_sheet.price_line_block
      %h3
        Contenu de la demande de commande :
    
   
    
    =render price_line_block
    
    
    
        
            
    %hr
    -if @p_customer_sheet.demande_type?
      %p
        Demande lors de la saisie :
        %strong=@p_customer_sheet.demande_type
    .right{:style => "text-align:right;"}
      
      -if current_admin.has_permission?("gestion-des-commandes")
      
        -if @p_customer_sheet.state == "brouillon"
      
          %p
            -if @p_customer_sheet.price_blocked
              =link_to ic(:unlock)+" Débloquer les prix", block_price_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-default"
            -else
              =link_to ic(:lock)+" Bloquer les prix", block_price_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-default"
        
        
      
        -if @p_customer_sheet.state == "brouillon" or @p_customer_sheet.state == "offre"# or @p_customer_sheet.state == "commande"
          -if @p_customer_sheet.state == "brouillon"
            %p=link_to "Générer le devis", generate_d_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary"
          %p=#link_to "Générer la facture proforma", generate_fp_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary"  
          %p=link_to "Générer le bon de commande", generate_bc_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-success btn-ap-add"
        %p=link_to "Marquer l'offre comme refusée", reject_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary btn-ap-error "
    
        -if( @p_customer_sheet.state == "commande" or (@p_customer_sheet.state == "en livraison" and @p_customer_sheet.price_documents.where(:cc_label => "Bon de livraison", :cancelled => false).count == 0)) and @bon_de_commande
          -if price_line_block.nbr_ship
            %p
              -if @bon_de_commande.label == "Bon de commande" and @p_customer_sheet and @p_customer_sheet.price_documents.where(:cc_label => "Bon de livraison", :cancelled => false).count == 0
        
                = link_to ic(:pencil)+" Modifier le bon de commande", edit_admin_price_document_path(:id => @bon_de_commande.id, :public_edit => true), :class => "btn btn-default"
                =link_to "Générer le bon de livraison",  bl_admin_price_document_path(@bon_de_commande), :class => "btn btn-primary btn-ap-add"
            
          
          
              
          -else
            %p=link_to "Indiquer le nombre de colis à livrer", edit_ship_admin_price_document_path(@bon_de_commande), :class => "btn btn-default"
        
        
        -elsif @bon_de_commande
          -if (@bon_de_commande.price_line_block.price_lines.sum(:qte) > PriceLine.where(:price_line_block_id => PriceLineBlock.where(:price_lineable_type => "PriceDocument", :price_lineable_id => @p_customer_sheet.price_documents.where(:cancelled => false, :cc_label => "Bon de livraison").ids)).sum(:qte))
            =link_to "Générer un bon de livraison pour le reliquat",  bl_admin_price_document_path(@bon_de_commande), :class => "btn btn-primary btn-ap-add"
            

      
    .clear
    
  %h3 Documents liés à l'offre  
  #bills
    -params[:search][:per_page] = params[:search][:per_page] || 50
    -per_page = params[:search][:per_page]
    -page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    
    -@price_documents = @p_customer_sheet.price_documents.order("date DESC, created_at DESC")
    -if params[:price_document_ids]
      -@price_documents = @price_documents.where(:id => params[:price_document_ids])
    

    -@price_documents = sort_by_sorting(@price_documents, "created_at DESC")
    -@price_documents = @price_documents.page(page).per(per_page)
    %br
    


    .clear 
    =render :partial => "qi/qi_ordered_table", :locals => {:qi_ordered_table_collection => @price_documents}
           
    
 