-ok = ok || false


%table.table.sheet_line{:style => "width:100%;"}
  %tr.sheet_line_head
    =#%th{:style => "width:30px;"}
    %th 
      Etat
    %th
      Description
    
    
    %th
      Qte
    %th
      Prix U.
    %th 
      Prix Tot.
    %th
      Prix TTC
  
  
  -sheet_lines.each do |sheet_line|
    -p_sheet_line_lines = sheet_line.p_sheet_line_lines.where("qte > 0")
    -nbr_lines = p_sheet_line_lines.count
    -ok = true if sheet_line.shiped
    
    
    %tr
      %td
        
        - if sheet_line.shiped
          -if sheet_line.decr
            Stocks décrémentés
          -else
            Livrée
            =link_to "Décrémenter les stocks", decrement_admin_p_sheet_line_path(sheet_line), :data => {:confirm => "Attention : les stocks seront définitivement décrémenté, à ne faire que dans le cas d'un remboursement où la marchandise a été rachetée."}
          
          
        -elsif sheet_line.bl
          En cours de livraison
          %br
          -if sheet_line.p_product.p_product_cat_id != 6 or sheet_line.externe and @p_customer_sheet
            =link_to "Indiquer comme livré", set_sheet_line_ship_admin_p_customer_sheet_path(@p_customer_sheet, :p_sheet_line_ids => [sheet_line.id]), :remote => true
            
      %td{:colspan => 1, :style => "text-align:right;"}
        %table.size_table
          %tr
            %th{:style => "text-align:left !important;"}
              =sheet_line.product_name
              -if sheet_line.cust_ref?
                %strong{:style => "font-weight:normal;"}
                  ="-"
                  Réf client : 
                  =sheet_line.cust_ref
         
        
            -sheet_line.p_product.p_sizes.order("p_sizes.position ASC, p_sizes.id ASC").all.each do |psize|
              %th.p_size_td=psize.name
        
      
      %td{:colspan => 4}
    -if sheet_line.p_product.p_colors.count > 0 
      -sheet_line.p_product.p_colors.all.each do |pc|
        -qte_tot = 0.0
        %tr
          %td
          %td{:colspan => 1, :style => "text-align:right;"}
            %table.size_table
    
      
      
  
              %tr
                %td
                  =pc.name
      
                -sheet_line.p_product.p_sizes.order("p_sizes.position ASC, p_sizes.id ASC").all.each do |psize|
      
                  %td.p_size_td
                    -psl = p_sheet_line_lines.joins(:p_product_stock).where(:p_product_stocks => {:p_size_id => psize.id, :p_color_id => pc.id} ).first
                    -if psl
                      =psl.qte
                      -qte_tot += psl.qte
            

          %td{}
            =qte_tot.to_i
          %td
            =number_to_currency sheet_line.price.to_f
          %td
            =number_to_currency sheet_line.price.to_f * qte_tot.to_i
  
          %td
            =number_to_currency sheet_line.price.to_f * (sheet_line.tva.to_f+1) * qte_tot.to_i
    
    -elsif sheet_line.p_product.p_sizes.count > 0
      -qte_tot = 0.0
      %tr
        %td
        %td{:colspan => 1, :style => "text-align:right;"}
          %table.size_table
  
    
    

            %tr
              %td

    
              -sheet_line.p_product.p_sizes.order("p_sizes.position ASC, p_sizes.id ASC").all.each do |psize|
    
                %td.p_size_td
                  -psl = p_sheet_line_lines.joins(:p_product_stock).where(:p_product_stocks => {:p_size_id => psize.id} ).first
                  -if psl
                    =psl.qte
                    -qte_tot += psl.qte
          

        %td{}
          =qte_tot.to_i
        %td
          =number_to_currency sheet_line.price.to_f
        %td
          =number_to_currency sheet_line.price.to_f * qte_tot.to_i

        %td
          =number_to_currency sheet_line.price.to_f * (sheet_line.tva.to_f+1) * qte_tot.to_i
          
    -else
      %tr
        -qte_tot = 0.0
        %td
        %td{:colspan => 1, :style => "text-align:right;"}
          %table.size_table
  
    
    

            %tr
              %td

    
              -p_sheet_line_lines.joins(:p_product_stock).each do |psl|
                -qte_tot += psl.qte
          

        %td{}
          =qte_tot.to_i
        %td
          =number_to_currency sheet_line.price.to_f
        %td
          =number_to_currency sheet_line.price.to_f * qte_tot.to_i

        %td
          =number_to_currency sheet_line.price.to_f * (sheet_line.tva.to_f+1) * qte_tot.to_i
    
      
    
    
    
    
    
    
        
      
  
  -total_with_labels =  PSheetLine.total_with_labels(sheet_lines)
  

  
  -total_with_labels.each do |index, total|


    %tr.sheet_line_head
      %td{:style => "text-align:right;padding-right:10px;", :colspan =>5}
        %strong
          =total[:label]
      %td
        =number_to_currency total[:value]
  
  
:scss
  .sheet_line_head{
    td, th{
      border-color:rgba(0,0,0,0.5) !important;
    }
    
  }
  .marges{
    td{
      vertical-align:top;
    }
    
    
    
    
  }
  
  .size_table{
    width:97%;

    text-align:center;
    margin-right:3%;
    td,th{
      text-align:right;
      border:0;
      border-right:solid 1px rgba(0,0,0,0.1) !important;
      padding:0 3px;
    }
    .p_size_td{
      width:60px;
      text-align:center;
    }

  }