class Ability
  include CanCan::Ability

  def initialize(user)
    # Define abilities for the passed in user here. For example:
    #
    #   user ||= User.new # guest user (not logged in)
    #   if user.admin?
    #     can :manage, :all
    #   else
    #     can :read, :all
    #   end
    #
    # The first argument to `can` is the action you are giving the user
    # permission to do.
    # If you pass :manage it will apply to every action. Other common actions
    # here are :read, :create, :update and :destroy.
    #
    # The second argument is the resource the user can perform the action on.
    # If you pass :all it will apply to every resource. Otherwise pass a Ruby
    # class of the resource.
    #
    # The third argument is an optional hash of conditions to further filter the
    # objects.
    # For example, here the user can only update published articles.
    #
    #   can :update, Article, :published => true
    #
    # See the wiki for details:
    # https://github.com/ryanb/cancan/wiki/Defining-Abilities
    alias_action :create, :read, :update, to: :cru

    user ||= User.new

    if user.usertype == "A" || user.usertype == "SS"
      # Cajas registradoras
      can :read, [AvailableProduct, CashOut, CashRegister, PaymentMethod, ProductsReturn]
      # Categorias
      can :manage, [BillingInformation, Category, Commission, Customer, Expense, Expensesconcept, Pointsale, PosConfig, Product, Promotion, ProductWaste, Purchase, Sale, Seller, Sellerscommission, SpecialPrice, Supplier, Transfer, Unit, User, Warehouse]
      can [:opened_cash_registers, :find_cash_outs_by_date, :print_receipt], CashOut
      can :sales_reserved, Sale
      cannot [:delete, :liquidate_reserve], Sale
      cannot :create, [ProductWaste, Sale]
      cannot [:debtors, :customer_sales], Customer
    elsif user.usertype == "G"
      # Cajas registradoras
      can :manage, [CashOut, CashRegister, Category, Credit, CreditPayment, Commission, Customer, Expense, OpenCashRegister, PreTransfer, PrePurchase, Product, Promotion, ProductsReturn, ProductWaste, Purchase, Sale, Seller, Sellerscommission, Supplier, Transfer]
      # Categorias
      can :read, [AvailableProduct, Credit, CreditPayment, Expensesconcept, PosConfig, SpecialPrice, Unit]
      # Clientes
      can :cru, [BillingInformation, Commission, Credit, CreditPayment, Customer, Sellerscommission, User, Warehouse]

      cannot :opened_cash_registers, CashOut
      cannot :sales_per_month_report, Sale
      cannot :product_track, Product

    elsif user.usertype == "C"
      # Cajas registradoras
      can :read, [AvailableProduct, BillingInformation, Credit, CreditPayment, Customer, SpecialPrice, Product, Promotion]
      # ventas
      can :cru, [Credit, CreditPayment]

      can :manage, [CashOut, Credit, CreditPayment, CashRegister, Customer, Expense, OpenCashRegister, PreSale, PreTransfer, ProductsReturn, ProductWaste, Sale, Transfer]

      cannot :opened_cash_registers, CashOut
      cannot :sales_per_month_report, Sale
      cannot :index, [CashRegister, Product]

    elsif user.usertype == "S"
      can :read, [AvailableProduct, BillingInformation, CashRegister, PosConfig, Product, Promotion]
      can :manage, [PreTransfer, ProductWaste, Transfer]

      cannot :index, [CashRegister, Product]
    end
  end
end
