
.qi_header
  %h1
    Achats
    %span
      Cuve :
      =@p_tank.name



-@p_tank.p_product_brut_stocks.group(:p_brut_product_id).each do |p_brut_product_cont|
  -p_brut_product = p_brut_product_cont.p_brut_product
  .qi_row.padding{:style => "margin-bottom:0;padding-bottom:0"}
    %h3{:style => "margin-bottom:0;"}= p_brut_product_cont.p_brut_product.name
  
  .qi_row
  
    .qi_pannel.qi_plain.padding
    
      Stocks à arrivés:
      =@p_tank.p_product_brut_stocks.where(:p_brut_product_id => p_brut_product.id).not_oks.sum(:qte)
      %table.table
        %tr
          %th Matière
          %th Volume
          %th Valeur
          %th Libélé
  

        %tbody#p_tanks_rows
          -@p_tank.p_product_brut_stocks.where(:p_brut_product_id => p_brut_product.id).not_oks.each do |p_product_brut_stock|
            %tr
              %td=p_product_brut_stock.p_brut_product.name
              %td=p_product_brut_stock.qte
              %td=p_product_brut_stock.price
              %td
                -if p_product_brut_stock.p_fournisseur_order
                  =link_to "Commande fournisseur", edit_admin_p_fournisseur_order_path(p_product_brut_stock.p_fournisseur_order), :target => "_blank", :remote => true
  
    .qi_pannel.qi_plain.padding
      %table.table
        %tr
          %td
            Stocks restants :
            =@p_tank.p_product_brut_stocks.where(:p_brut_product_id => p_brut_product.id).oks.sum(:qte_restant)
          %td
            Stocks :
            =@p_tank.p_product_brut_stocks.where(:p_brut_product_id => p_brut_product.id).oks.sum(:ok_qte)
      %table.table
        %tr
          %th Date
          %th Matière
          %th Volume
          
          %th Valeur
          %th Libélé
          %th Volume restant
  

        %tbody#p_tanks_rows
          -@p_tank.p_product_brut_stocks.where(:p_brut_product_id => p_brut_product.id).oks.each do |p_product_brut_stock|
            %tr{:style => "font-weight:bold;"}
              %td=l p_product_brut_stock.ok_at, :format => :date if p_product_brut_stock.ok_at
              %td=p_product_brut_stock.p_brut_product.name
              %td=p_product_brut_stock.ok_qte
              
              %td=number_to_currency p_product_brut_stock.ok_price, :precision => 5
              %td
                -if p_product_brut_stock.p_fournisseur_order
                  =link_to "Commande fournisseur", edit_admin_p_fournisseur_order_path(p_product_brut_stock.p_fournisseur_order), :target => "_blank", :remote => true
              
              %td=p_product_brut_stock.qte_restant
              %td=p_product_brut_stock.ok_qte.to_f - p_product_brut_stock.qte_used_calc
            
            -p_product_brut_stock.p_sheet_line_stocks.joins(:p_sheet_line).order("p_sheet_lines.ok_at DESC").each do |p_sheet_line_stock|
              %tr
                %td=l p_sheet_line_stock.p_sheet_line.ok_at, :format => :date
                %td=p_sheet_line_stock.p_sheet_line.p_product.name if p_sheet_line_stock.p_sheet_line.p_product
                %td=p_sheet_line_stock.qte_ok
                %td=number_to_currency p_sheet_line_stock.p_sheet_line.ok_price, :precision => 5
                
                %td{:colspan => 2}
                  =link_to "##{p_sheet_line_stock.p_sheet_line.p_customer_sheet.id} #{p_sheet_line_stock.p_sheet_line.p_customer_sheet.p_customer.show_name}", [:admin,p_sheet_line_stock.p_sheet_line.p_customer_sheet]