# -*- 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.order("created_at DESC").all
    
		per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 50
		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
		@p_customer_sheets = @p_customer_sheets.page(page).per(per_page)
   
  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_sheet = PCustomerSheet.new(:wish_start => Date.today, :wish_stop => ((Date.today + 7.day).end_of_week)-2.day)
    
    @p_customer_sheet.p_customer = PCustomer.find(params[:p_customer_id]) if params[:p_customer_id]
    

  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_bc
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_bc
    
    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
