# -*- encoding : utf-8 -*-

class Admin::PPaymentsController < ApplicationController
  layout "admin"
  before_action :auth_admin

  before_action :admin_space
  
  def relances
    params[:day] = 0 if params[:day].to_s == ""
    
    params[:day] =  params[:day].to_i
    params[:importeds] = "0" if params[:importeds].to_s == ""
    
    

    
    
    
    @relances = true
    
    @p_documents = PDocument.where(:p_document_type_id => 4, :paid => false, :i_archive => false).where("echeance_date <= ?", (Time.now - params[:day].day).end_of_day).order("DATEDIFF(NOW(), echeance_date) ASC,created_at DESC")
    
    if params[:importeds] == "0"
      @p_documents = @p_documents.where(:imported => false)
    end
    
    @p_document_bruts = @p_documents
    
		per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 50
		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
		@p_documents = @p_documents.page(page).per(per_page)
    
  end
  
  
  
  def import_lettrage
    if true
      require 'csv'    

      csv_text = File.read("#{Rails.root}/import_csv/lettrage.csv", :encoding => 'UTF-8')
      @csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
      
      
    end
  end
  
  def import
    if false
      if params[:csv_file]
        @file = params[:csv_file]
    
        require 'csv'    

        csv_text = @file.tempfile.read.force_encoding('ISO-8859-1').encode('utf-8')
      
        @csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
      end
      
    end
    
  end
  
  
  def import_arrondis
    
    if false
      
      
      @file = params[:csv_file]
    
      require 'csv'    

      csv_text = File.read("#{Rails.root}/import_csv/reglements3.csv", :encoding => 'UTF-8')
      @csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
    end
    
  end
  
  
  
  def admin_space
    @admin_space = "payments"
  end
  
  def index
    @p_payments = PPayment.order("paid_at DESC").all
    
    params[:remise] = 0 if !params[:remise]
    
    #params[:p_payment_type_ids] = ["4", "41", "42"] if !params[:p_payment_type_ids]
    
    params[:p_payment_type_ids] = params[:p_payment_type_ids] || [] 
    if params[:p_payment_type_ids].size > 0
      @p_payments = @p_payments.where(:p_payment_type_id => params[:p_payment_type_ids])
    end
    
    if params[:amount_min].to_s != ""
      @p_payments = @p_payments.where("amount >= ?", params[:amount_min])
    end
    
    if params[:amount_max].to_s != ""
      @p_payments = @p_payments.where("amount <= ?", params[:amount_max])
    end
    
    if params[:affected].to_s != ""
      @p_payments = @p_payments.where("affected = ?", params[:affected].to_i)
    end
    
    if params[:remise].to_s != ""
      @p_payments = @p_payments.where("remise = ?", params[:remise].to_i)
      if params[:remise].to_s == "0"
        @p_payments = @p_payments.where(:imported => false)
      end
    end
    
    date_regex = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/i 
    
    
    #params[:start] = Date.today.beginning_of_month.strftime('%d/%m/%Y') if !params[:start]
    
    if params[:start] and params[:start] =~ date_regex
      #fsfds = sdfsfd
      @start = Date.parse(params[:start]).beginning_of_day 
      params[:start]= @start.strftime('%d/%m/%Y')
    else
      @start = nil
    end
    
    if true
      #params[:stop] = Date.today.end_of_month.strftime('%d/%m/%Y') if !params[:stop]
     
    
      if params[:stop].to_s != "" # and params[:stop] =~ date_regex
        @stop = Date.parse(params[:stop]).end_of_day 
        
        params[:stop]= @stop.strftime('%d/%m/%Y')
        
        
      else
         @stop = nil
      end
    
    end
    
    @p_payments = @p_payments.where("paid_at >= ?", @start) if @start
    if @stop
      @p_payments = @p_payments.where("paid_at <= ?", @stop.end_of_day) 
    end
    
    
		per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 50
		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    @all_p_payments = @p_payments
		@p_payments = @p_payments.page(page).per(per_page)
   
  end

  def show
    @p_payment = PPayment.find(params[:id])

  end

  def new
    
    
		@p_payment = PPayment.new(:p_payment_type_id => 41)
    
    @p_payment.p_customer = PCustomer.find(params[:p_customer_id]) if params[:p_customer_id]

  end

 

  def edit
  
    
    @p_payment = PPayment.find(params[:id])
  end

  def create
    @p_payment = PPayment.new(params.require(:p_payment).permit!)


    if @p_payment.save
     #@p_payments = PPayment.all
      
    else
      render action: "new"
 
    end

  end


  def update
    @p_payment = PPayment.find(params[:id])


    if @p_payment.update_attributes(params.require(:p_payment).permit!)
      
       #@p_payments = PPayment.order("created_at ASC").all
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @p_payment = PPayment.find(params[:id])
    @p_payment.destroy
    
    
  end
end
