-content_for :header do
  .qi_header
    .right
      -if current_admin.has_permission?("products-c")
        = link_to ic(:plus)+' Ajouter un produit', new_admin_p_product_path(), :class => "btn btn-primary bgbd-stocks", :remote => false
    %h1
      Achats
      %span
        Produits


%table
  -PProduct.where(:s_brand_id => nil).each do |p_product|

    -p_product.code
  

    - ref_p_product = PProduct.where("s_brand_id is not NULL").where("code LIKE ?", "%"+p_product.code.sub("6KG", "").sub("RSTO", "").sub("RST", "").to_s+"%").first
    
    -if ref_p_product
    
      
      
      
      -if p_product_ref = ref_p_product.p_product_refs.first
      
      
        -pprf = p_product.p_product_refs.first
      
        - pprf.uv = p_product_ref.uv if pprf and p_product_ref.uv
        - pprf.save
        
      - p_product.uv = ref_p_product.uv if ref_p_product.uv
      - p_product.s_brand_id = ref_p_product.s_brand_id if ref_p_product.s_brand_id
      - p_product.country = ref_p_product.country if ref_p_product.country
      - p_product.sub_country = ref_p_product.sub_country if ref_p_product.sub_country
      - p_product.domaine = ref_p_product.domaine if ref_p_product.domaine
      
      - p_product.save


.qi_search_row
  
  =form_tag "", :method => "get",  :onsubmit => "" do
    
    =hidden_field_tag :column, params[:column]
    =hidden_field_tag :direction, params[:direction]
    
    %table
      %tr
        %td
          Actif :
          =select_tag "search[enabled]", options_for_select([[""], "Oui", "Non"], params[:search][:enabled])
        
          
        %td=text_field_tag :code, params[:code],:class => "form-control", :placeholder => "Code"
        
        %td=text_field_tag :name, params[:name],:class => "form-control", :placeholder => "Nom"
       

    %table
      %tr
        %td
          Catégorie :
          =select_tag "search[p_product_cat_id]", options_for_select([["",""]]+PProductCat.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:p_product_cat_id])
        
        %td
          Catégorie client :
          =select_tag "search[p_customer_cat_id]", options_for_select([["",""]]+PCustomerCat.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:p_customer_cat_id])
        
        %td
          Marque :
          =select_tag "search[s_brand_id]", options_for_select([["",""]]+SBrand.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:s_brand_id])
        
      
      
    =render :partial => "qi/qi_ordered_table_search_footer", :locals => {:collection_object => @p_products}


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




      
    