
=debug PriceDocument.update_average_marge


-if true
  -PriceLineBlock.where(:imported => true).update_all(:cost_ok => true)
  -PriceDocument.where(:imported => true).update_all(:cost_ok => true)

-if true

  %table.table.table-stripped.table-bordered
    -OrderHist.where(:p_customer_id => nil).order("past_code_client").find_each do |oh|
      %tr
        %td
          =oh.numerofacture
        
        -a = []
      
        %td
          =oh.order_hist_lines.first.interlocuteur
      
        %td
          =a << oh.past_code_client
        
      
      
        
        %td
          =a << oh.order_hist_lines.first.past_code_client
        
        %td
          =a << oh.order_hist_lines.first.past_code_client.to_s[0..6]
      
        %td
          =a << oh.order_hist_lines.first.new_code_client.to_s[0..6]
        
        %td
          -if p_customer = PCustomer.where("code in(?) or past_code in(?)",a,a).first
            =p_customer.show_name
            -oh.p_customer = p_customer
            -oh.order_hist_lines.update_all(:p_customer_id => p_customer.id)
            -#oh.save
            
            -PriceDocument.where(:d_number => oh.numerofacture).each do |pd|
              -pd.p_customer = p_customer
              -pd.price_line_block.p_customer = p_customer
              -pd.save
              
          
        
        
        
        

-if false
  -if true #params[:force]
    -PriceDocument.where(:imported => true).destroy_all 

  -if true and current_admin.id == 1 #and params[:update]
    %table.table.table-stripped.table-bordered
      -OrderHist.find_each do |oh|
        -if PriceDocument.where(:d_number => oh.numerofacture).count == 0
        
          -first_line = oh.order_hist_lines.first
          -payment_delails = 0
          -if first_line and first_line.date_echeance
            -payment_delails =  ((first_line.date_echeance.to_time - oh.date_facture.to_time) / 1.day.seconds ).to_i
    
          -@price_document = PriceDocument.new(:ac_payment_delais => payment_delails, :ac_payment_end_at => (first_line.date_echeance if first_line), :ac_solded_at => (first_line.date_echeance if first_line), :cc_solded_at => (first_line.date_echeance if first_line) , :stock_ok => true, :ac_to_paid_ht => 0.0, :ac_to_paid_ttc => 0.0, :ac_to_paid_tva => 0.0, :d_number => oh.numerofacture, :imported => true, :date => oh.date_facture, :p_payment_type_id => oh.p_payment_type_id, :p_customer_id => oh.p_customer_id, :p_commercial_id => oh.p_commercial_id, :cc_solded => 0, :ac_solded => 0)
          -@price_document.price_document_type = PriceDocumentType.where(:label => "Facture").first
          -@price_document.price_line_block = PriceLineBlock.new(:ct_tot_fdp_ht => 0.0, :wish_date => oh.date_facture ,:imported => true, :p_payment_type_id => oh.p_payment_type_id, :p_customer_id => oh.p_customer_id, :p_commercial_id => oh.p_commercial_id ,:cost_ok => true)
          -@price_document.ac_solded = true
          
     
          -oh.order_hist_lines.find_all do |ohl|
            -@price_document.price_line_block.price_lines << PriceLine.new(:imported => true, :p_product_ref_id => ohl.p_product_ref_id, :qte => ohl.qte, :ac_uv => 1, :ac_weight_u => 1, :ct_u_price_ht => ohl.prix_u, :ac_tva_account_value => ohl.taux_tva, :ac_tot_line_ht => ohl.montant_ht ,:ac_tot_amount_ht => ohl.montant_ht_remise, :ct_tot_amount_ht => ohl.montant_ht_remise, :ac_cost_ht => (ohl.cc_cout_achat_ht.to_f*1.0))

          %tr
            %td
              =oh.numerofacture
  
            %td
              -if @price_document.save
                -@price_document.ac_solded = true
                -@price_document.save
              
              -else
                =debug @price_document.errors.messages
      
            %td
              =number_to_currency @price_document.tot_amount_ht
  
            %td
              =number_to_currency oh.montant_ht_remise
        
