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

class Admin::PCustomerSheetsController < ApplicationController
  layout "admin"
  before_filter :auth_admin

  before_filter :admin_space
  
  def update_stocks
    @p_sheet_lines = PSheetLine.where("p_customer_sheet_id is not null").where(:externe => false, :stock_done => false, :shiped => true).order("ok_at DESC")
    @p_sheet_lines.each do |psl|
      psl.update_stock
      
    end
    redirect_to :back
  end
  
  def affects
    @p_sheet_lines = PSheetLine.where("p_customer_sheet_id is not null").where(:externe => false, :stock_done => false, :shiped => true)
  end
  
  def admin_space
    @admin_space = "ventes"
  end

  def index
    @p_customer_sheets = PCustomerSheet.all
    
    
    date_regex = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/i 
    

    
    if params[:search][:created_at_start] and params[:search][:created_at_start] =~ date_regex
      
      @start = Date.parse(params[:search][:created_at_start]).beginning_of_day 
      params[:search][:created_at_start]= @start.strftime('%d/%m/%Y')
      
      
    else
      @start = nil
    end
    if params[:search][:created_at_stop].to_s != "" # and params[:search][:created_at_stop] =~ date_regex
      @stop = Date.parse(params[:search][:created_at_stop]).end_of_day 
  
      params[:search][:created_at_stop]= @stop.strftime('%d/%m/%Y')
  
  
    else
       @stop = nil
    end

    @p_customer_sheets = @p_customer_sheets.where("p_customer_sheets.created_at >= ?", @start) if @start
    @p_customer_sheets = @p_customer_sheets.where("p_customer_sheets.created_at <= ?", @stop.end_of_day) if @stop
    

    if params[:search][:state].to_s != ""
      @p_customer_sheets = @p_customer_sheets.where(:state => params[:search][:state])
    end
    
    
    @p_customer_sheets = sort_by_sorting(@p_customer_sheets, "created_at DESC")
    respond_to do |format|
      format.html{
    
        params[:search][:per_page] = params[:search][:per_page] || 50
    		per_page = params[:search][:per_page]
    		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    		@p_customer_sheets = @p_customer_sheets.page(page).per(per_page)
    
      }
    end
   
  end

  def show
    @p_customer_sheet = PCustomerSheet.find(params[:id])

  end
  
  def set_sheet_line_ship
    @p_customer_sheet = PCustomerSheet.find(params[:id])

  end
  
  def set_sheet_line_ship_save
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    if @p_customer_sheet.update_attributes(params.require(:p_customer_sheet).permit!)
   
      
       @redirect_to_url = admin_p_customer_sheet_path(@p_customer_sheet)
       render :layout => false
    else
     render action: "set_sheet_line_ship" 
      
    end
  end

  def new
    
    @p_customer = PCustomer.find(params[:p_customer_id]) if params[:p_customer_id]
    
		@p_customer_sheet = PCustomerSheet.new(:p_customer => @p_customer, :wish_start => Date.today, :wish_stop => ((Date.today + 7.day).end_of_week)-2.day)
    
    
    

  end
  
  
  def dup
  
    
    @past_customer_sheet = PCustomerSheet.find(params[:id])
    
    
    @p_customer_sheet = PCustomerSheet.new(:p_customer => @past_customer_sheet.p_customer, :wish_start => Date.today, :wish_stop => ((Date.today + 7.day).end_of_week)-2.day)
    @p_customer_sheet.particular_bill_id = @past_customer_sheet.particular_bill_id
    @p_customer_sheet.particular_send_id = @past_customer_sheet.particular_send_id
    @p_customer_sheet.cust_ref = @past_customer_sheet.cust_ref
    @past_customer_sheet.p_sheet_lines.each do |p_sheet_line|
      
      new_p_sheet_line = p_sheet_line.dup
      p_sheet_line.p_sheet_line_lines.each do |p_sheet_line_line| 
        new_p_sheet_line.p_sheet_line_lines << p_sheet_line_line.dup
      end
      
      p_sheet_line.p_sheet_line_stocks.each do |p_sheet_line_stock| 
        new_p_sheet_line.p_sheet_line_stocks << p_sheet_line_stock.dup
      end
      
      
      
      
      
      @p_customer_sheet.p_sheet_lines << new_p_sheet_line
      
    end
    
    
    
    @p_customer_sheet.fdp_force_price = @past_customer_sheet.fdp_force_price
    @p_customer_sheet.obs_devis = @past_customer_sheet.obs_devis
    @p_customer_sheet.obs_bdc = @past_customer_sheet.obs_bdc
    @p_customer_sheet.obs_bdl = @past_customer_sheet.obs_bdl
    
    
    @p_customer_sheet.p_payment_type_id = @past_customer_sheet.p_payment_type_id
    @p_customer_sheet.comptant = @past_customer_sheet.comptant
    @p_customer_sheet.acompte = @past_customer_sheet.acompte
    @p_customer_sheet.acompte_percent = @past_customer_sheet.acompte_percent
    @p_customer_sheet.payment_delais = @past_customer_sheet.payment_delais
    @p_customer_sheet.payment_fin_de_mois = @past_customer_sheet.payment_fin_de_mois
    
    
    
    
    @p_customer_sheet.save
    
    
    
    redirect_to admin_p_customer_sheets_path()
  end

 

  def edit
  
    
    @p_customer_sheet = PCustomerSheet.find(params[:id])
  end

  def create
    @p_customer_sheet = PCustomerSheet.new(params.require(:p_customer_sheet).permit!)


    if @p_customer_sheet.save
     @p_customer_sheets = PCustomerSheet.order(:id).all
      redirect_to admin_p_customer_sheet_path(@p_customer_sheet)
    else
      render action: "new"
 
    end

  end


  def update
    @p_customer_sheet = PCustomerSheet.find(params[:id])

    
    if @p_customer_sheet.update_attributes(params.require(:p_customer_sheet).permit!)
      
       @p_customer_sheets = PCustomerSheet.order(:id).all
      
       redirect_to admin_p_customer_sheet_path(@p_customer_sheet)
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.destroy
    
    
  end
  
  
  def generate_d
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_d
    
    redirect_to :back
    
  end
  
  def generate_fp
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_fp
    
    redirect_to :back
    
  end
  
  def generate_fa
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_fa
    
    redirect_to :back
    
  end
  
  def generate_bc
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_bc
    @p_customer_sheet.customer_validation_at = Time.now
    @p_customer_sheet.save
    
    redirect_to :back
    
  end
  
  def generate_bl
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_bl
    
    redirect_to :back
    
  end
  
  def generate_f
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_f
    
    redirect_to :back
    
  end
  
  def reject
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.state = "refusée"
    @p_customer_sheet.save
    
    redirect_to :back
    
  end
  
  
  
  
end