class CashOutsController < ApplicationController
  ##--- Abilities
  load_and_authorize_resource

  ##--- Breadcrum_rails
  add_breadcrumb I18n.t("breadcrumbs." + controller_name), :cash_outs_path
  add_breadcrumb "Nueva Corte de caja ", :new_cash_out_path, only: :new
  add_breadcrumb "Detalle del corte de caja ", :cash_out_path, only: :show
  add_breadcrumb "Editar corte de caja ", :edit_cash_out_path, only: :edit
  add_breadcrumb "Cajas abiertas ", :opened_cash_registers_path, only: :opened_cash_registers

  before_action :set_cash_out, only: [:show, :edit, :update, :destroy]
  before_action :set_data, only: [:new, :create]
  before_action :get_filters, only: [:index, :show, :edit, :new]
  # GET /cash_outs.json
  def index
    case current_user.usertype
    when "A"
      @cash_outs = CashOut.all.includes(:open_cash_register, :user, :received_by).order('cash_outs.created_at desc')
    when "G"
      @cash_outs = Pointsale.find(current_user.pointsale_id).cash_outs.includes(:open_cash_register, :user, :received_by).order('cash_outs.created_at desc')
    when "C"
      @cash_outs = Pointsale.find(current_user.pointsale_id).cash_outs.includes(:open_cash_register, :user, :received_by).where("cash_outs.user_id = ?", current_user.id).order('cash_outs.created_at desc')
    end
  end

  # GET /cash_outs/1
  # GET /cash_outs/1.json
  def show
    @incomings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1", @cash_out.open_cash_register.id).order('created_at')
    @outgoings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '0' and status = 1", @cash_out.open_cash_register.id).order('created_at')

    @sales_total = @incomings.sum(:quantity)
    @expenses_total = @outgoings.sum(:quantity)
  end

  # GET /cash_outs/new
  def new
    @cash_out = CashOut.new
    @cash_out.cash_out_details.new
    @cash_out.received_cash = nil
    @cash_out.physical_cash = nil
    @cash_out.cash_fund = nil
  end

  # GET /cash_outs/1/edit
  def edit; end

  # POST /cash_outs
  # POST /cash_outs.json
  def create
    final_cash = 0
    @cash_out = CashOut.new(cash_out_params)
    open_cash_register = OpenCashRegister.find(params[:open_cash_register_id])
    @cash_out.amount_in = 0
    @cash_out.amount_out = 0
    @cash_out.open_cash_register_id = open_cash_register.id
    @cash_out.user_id = current_user.id
    @cash_out.cash_out_details.each do |detail|
      @cash_out.amount_in = @cash_out.amount_in + detail.incoming
      @cash_out.amount_out = @cash_out.amount_out + detail.outgoing
      if detail.payment_method_id == @cash_payment_method_id
        final_cash = detail.adjustment
      end
    end
    open_cash_register.expenses.update_all(status: 2) # registered
    respond_to do |format|
      message = "Corte de caja de #{@cash_out.open_cash_register.cash_register.name} realizado correctamente"
      @cash_out.audit_comment = message
      if @cash_out.save
        # eliminar el id del open cash de la sesion
        session.delete(:open_cash_register_id)
        open_cash_register.update_attributes(status: 'closed', final_cash: final_cash)
        format.js { flash[:success] = message }
      else
        format.js
        format.json { render json: @cash_out.errors, status: :unprocessable_entity }
      end
    end
  end

  def get_open_cash_registers
    respond_to do |format|
      format.js
    end
  end

  def opened_cash_registers
    @opened_cash_registers = OpenCashRegister.abiertas
  end

  def select_open_cash_to_close
    @open_cash_register_id = params[:open_cash_register_id]
    respond_to do |format|
      format.js
    end
  end

  def find_cash_outs_by_date
    if params[:begin_date] != "null" && params[:end_date] != "null"
      start_date = params[:begin_date].in_time_zone(Time.zone).beginning_of_day + 1.days
      end_date = params[:end_date].in_time_zone(Time.zone).end_of_day + 1.days
    else
      start_date = DateTime.now
      end_date = DateTime.now
    end

    @cash_outs = params[:pointsale_id] != "null" ? Pointsale.find(params[:pointsale_id]).cash_outs.includes(:open_cash_register, :user, :received_by).where(created_at: start_date..end_date).order(" id DESC ") : CashOut.all.includes(:open_cash_register, :user, :received_by).where(created_at: start_date..end_date).order('cash_outs.created_at desc')
    respond_to do |format|
      format.js
    end
  end

  def print_receipt
    # ticket para la venta
    respond_to do |format|
      @cash_out = CashOut.find(params[:cash_out_id])
      products_ids = @cash_out.open_cash_register.sales_details.joins(:sale).where("sales.status != 1").pluck(:product_id).join(",")
      @pointsale = OpenCashRegister.get_pointsale(@cash_out.open_cash_register_id, "open_cash_register")
      @initial_cash = @cash_out.open_cash_register.initial_cash
      @details = @cash_out.open_cash_register.products.present? ? ActiveRecord::Base.connection.exec_query("SELECT SUM(sales_details.quantity) as quantity, SUM(sales_details.total) as total, sales_details.product_id, sub.name as product_name, sub.category, sub.parent FROM sales_details INNER JOIN sales ON sales_details.sale_id = sales.id INNER JOIN (SELECT products. ID as product_id, category_id, categories.category, products. NAME, categories.parent_id, parents.category as parent FROM products INNER JOIN categories_products ON categories_products.product_id = products. ID INNER JOIN categories ON categories. ID = categories_products.category_id LEFT JOIN categories as parents ON parents.ID = categories.parent_id WHERE (products. ID IN (#{products_ids}))) sub ON (sales_details.product_id = sub.product_id) WHERE sales.open_cash_register_id = #{@cash_out.open_cash_register_id} and sales.status != 1 GROUP BY sales_details.product_id, sub.category, sub.name, sub.parent ORDER BY sub.category, sub.parent") : Array.new

      all_sales = Sale.where("open_cash_register_id = (?) and status != 1", @cash_out.open_cash_register_id)
      @cash_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and sale_id IN (?)", @cash_out.open_cash_register_id, all_sales.where(saletype: 1).pluck(:id)).sum(:quantity)
      @credit_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and concept = 3", @cash_out.open_cash_register_id).sum(:quantity)
      @reserved_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and sale_id IN (?)", @cash_out.open_cash_register_id, all_sales.where(saletype: 2).pluck(:id)).sum(:quantity)
      @details.each do |detail|
        detail["price_sale"] = Product.find(detail['product_id']).get_price_sale(@pointsale.id)

        detail["total_without_discount"] = detail["price_sale"] * detail['quantity'].to_f

        detail["discount"] = detail["total_without_discount"] - detail["total"].to_f
      end

      @expenses = Expense.where(open_cash_register_id: @cash_out.open_cash_register_id).activos

      format.pdf do
        render pdf: "ticket_corte_#{@cash_out.id}", template: "cash_outs/receipt.pdf.erb", layout: 'receipt.html.erb', locals: { cash_out: @cash_out, details: @details, pointsale: @pointsale, expenses: @expenses, initial_cash: @initial_cash, sales_total: @cash_sales, credit_sales: @credit_sales, reserved_sales: @reserved_sales }, show_as_html: params.key?('debug'), page_width: '80mm', page_height: '300mm'
      end
    end
  end

  private

  def set_data
    @cash_out.cash_out_details.destroy_all
    @cash_payment_method_id = PaymentMethod.find_by(isCash: 1).id
    @opened_cash_register = (params[:open_cash_register_id].blank? ? OpenCashRegister.find(session[:open_cash_register_id]) : OpenCashRegister.find(params[:open_cash_register_id]))
    if @opened_cash_register.present?
      @initial_cash = @opened_cash_register.initial_cash
      @incomings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1", @opened_cash_register.id).order('created_at')
      @outgoings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '0' and status = 1", @opened_cash_register.id).order('created_at')

      # all_sales = Sale.where("open_cash_register_id = (?) and status != 1", @opened_cash_register.id)
      sales_ids = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1", @opened_cash_register.id).pluck("DISTINCT sale_id")
      all_sales = Sale.activas.where("id IN (?)", sales_ids)

      @cash_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and sale_id IN (?)", @opened_cash_register.id, all_sales.where(saletype: 1).pluck(:id)).sum(:quantity)
      @credit_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and concept = 3", @opened_cash_register.id).sum(:quantity)
      @reserved_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and sale_id IN (?)", @opened_cash_register.id, all_sales.where(saletype: 2).pluck(:id)).sum(:quantity)
      @expenses_total = @outgoings.sum(:quantity)
    end

    @payments = ActiveRecord::Base.connection.exec_query("SELECT DISTINCT(crm.payment_method_id), pm.method, i.total as incoming, o.total as outgoing, SUM(crm.quantity) AS total from cash_registers_moves as crm LEFT JOIN (SELECT DISTINCT(payment_method_id), SUM(quantity) as total FROM cash_registers_moves WHERE move_type='1' and status = 1 and open_cash_register_id=#{@opened_cash_register.id}  group by payment_method_id) as i ON (i.payment_method_id=crm.payment_method_id) LEFT JOIN (SELECT DISTINCT(payment_method_id), SUM(quantity) as total FROM cash_registers_moves WHERE move_type='0' and status = 1 and open_cash_register_id=#{@opened_cash_register.id} group by payment_method_id) as o ON (o.payment_method_id=crm.payment_method_id), payment_methods as pm WHERE pm.id=crm.payment_method_id and crm.open_cash_register_id=#{@opened_cash_register.id} group by crm.payment_method_id, pm.method, i.total, o.total")
    @payments.each do |payment|
      payment["incoming"] = "0" if payment["incoming"].nil?
      payment["outgoing"] = "0" if payment["outgoing"].nil?
      payment["total"] = payment["incoming"].to_f - payment["outgoing"].to_f

      # si es el efectivo sumarle el fondo
      if payment["payment_method_id"].to_i == @cash_payment_method_id
        payment["total"] += @initial_cash
      end

      @cash_out.cash_out_details.build
    end
  end

  # Use callbacks to share common setup or constraints between actions.
  def set_cash_out
    @cash_out = CashOut.find(params[:id])
  end

  def get_filters
    @current_page = params[:current_page].blank? ? 1 : params[:current_page]
    @filter = params[:filter]
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def cash_out_params
    params.require(:cash_out).permit(:received_by_id, :received_cash, :cash_fund, :physical_cash, :observations, cash_out_details_attributes: [:payment_method_id, :observations, :incoming, :outgoing, :total, :adjustment])
  end
end
