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

class Admin::PriceDocumentsController < ApplicationController
  layout "admin"
  before_action :auth_admin, :except => :print

  before_action :admin_space, :except => :print
  
  def admin_space
    @admin_space = "ventes"
  end
  
  def search_to_affect
    @p_customer = PCustomer.where(:id => params[:p_customer_id]).first
  end
  
  def index
     
    if params[:price_document_type_id]
      params[:price_document_type_ids] = [params[:price_document_type_id]]
    else
      params[:price_document_type_ids] = params[:price_document_type_ids] || ["4", "7"]
    end
    
    if @p_customer
      @price_documents = @p_customer.price_documents
    else
      @price_documents = PriceDocument
    end
    @price_documents = @price_documents.order("created_at DESC")
    
    @price_documents = @price_documents.where(:price_document_type_id => params[:price_document_type_ids]) if params[:price_document_type_ids].size > 0

    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
    
    
    @price_documents = @price_documents.where("created_at >= ?", @start) if @start

    @price_documents = @price_documents.where("created_at <= ?", @stop.end_of_day) if @stop
    
    params[:per_page] = 100000 if params[:print]
    per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 50
    page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    @price_documents = @price_documents.page(page).per(per_page)
    
    
    @price_documents = @price_documents.all
    

  end

  def show
    @price_document = PriceDocument.find(params[:id])

  end

  def new
    @price_document = PriceDocument.new
    @price_document.price_line_block = PriceLineBlock.new
  
  end

  def edit
    @price_document = PriceDocument.find(params[:id])
    
  end

  def create
    @price_document = PriceDocument.new(params.require(:price_document).permit!)

    if @price_document.save
      redirect_to action: "index"
    else
      render action: "new"
 
    end

  end


  def update
    @price_document = PriceDocument.find(params[:id])


    if @price_document.update_attributes(params.require(:price_document).permit!)
      redirect_to action: "index"
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @price_document = PriceDocument.find(params[:id])
    @price_document.destroy
    
  end
  
  
  def print
    
  

    @price_document = PriceDocument.find_by_token(params[:id])
    @element = @price_document.ref_element
    
    doc_number = @price_document.d_number
    url = print_admin_price_document_path(:id => @price_document.token, :html => true)
  
  
  
    
    params[:inline] = true
    
    if !params[:html] # and !Rails.env.development?
      
      @data_to_send = File.open(@price_document.generate_pdf).read
      
      
      
      
      
      send_data @data_to_send, :filename =>"#{doc_number}.pdf" , :type => 'application/pdf',:disposition => (params[:inline] ? 'inline' : "attachment")
      #render :inline => "y"
    
    else
      render :layout => false
    end
    
  end
  
  
  
end
