module Toolhound

  # A single charge for a rental item
  class RentalCharge < Base


    # self.table_name   = :rental
    # self.primary_key  = :int_rental_id

    def default_selects
      #{ }"SELECT tblEntity.intEntityID, tblEntity.varEntityID AS job_no, tblLocation.intLocationID, tblLocationText.varLocationName  FROM tblEntity INNER JOIN tblLocation ON tblLocation.intEntityID = tblEntity.intEntityID INNER JOIN tblLocationText ON tblLocationText.intLocationID = tblLocation.intLocationID WHERE varEntityID LIKE '%10526.00%'"
      {
        rental_charge:   [:int_rental_charge_id ]
        # rental_item_detail: [:var_status, :status_date, ],
        # rental_detail: [:int_rental_id, :int_inventory_id, :int_quantity, :dec_daily, :dec_weekly, :dec_monthly, :dec_selling_price],
      }
    end

    def default_joins
      arr = []
      # arr << "INNER JOIN tblRentalItemDetail ON (tblRentalItemDetail.intRentalItemID = tblRentalItem.intRentalItemID)"
      # arr << "INNER JOIN tblRentalDetail ON (tblRentalDetail.intRentalDetailID = tblRentalItem.intRentalDetailID)"

      # arr << "INNER JOIN tblRental ON (tblRental.intRentalID = tblRentalDetail.intRentalID)"
      # arr << "INNER JOIN tblLocationText ON (tblLocationText.intLocationID = tblLocation.intLocationID)"

      arr
    end

    # def find_by_entity_id(id)
    #   all(limit: 1, where: [{int_entity_id: id}]).first
    # end

    def find_first_and_last_charge_for_entity(entity_id)
      selects = {
        rental_charge: [
          {dte_start_date: {as: :max_date, agg: :max} },
          {dte_start_date: {as: :min_date, agg: :min} }
        ]
      }

      build_and_query(selects: selects, where: [{int_entity_id: entity_id}]).first
    end

    def find_by_rental_id(id)
      all(where: [{"rental_detail.int_rental_id" => id}])
    end

    def for_entity_over_period(entity_id, from, to)
      joins = []
      joins << "LEFT OUTER JOIN tblRentalItem ON tblRentalItem.intRentalItemID = tblRentalCharge.intRentalItemID"
      joins << "LEFT OUTER JOIN tblRentalDetail ON tblRentalDetail.intRentalDetailID = tblRentalItem.intRentalDetailID"
      joins << "LEFT OUTER JOIN tblRental ON tblRental.intRentalID = tblRentalDetail.intRentalID"
      joins << "INNER JOIN tblInventoryID ON tblInventoryID.intInventoryIdID = tblRentalCharge.intInventoryIDID"
      joins << "INNER JOIN tblInventory   ON tblInventoryID.intInventoryID = tblInventory.intInventoryID"
      joins << "INNER JOIN tblInventoryText ON tblInventory.intInventoryID = tblInventoryText.intInventoryID"

      wheres = [
        {"rental_charge.int_entity_id" => entity_id},
        {"inventory.bol_deleted" => 0},
        {"inventory.bol_is_active" => 1},
        {"inventory_text.var_locale_id" => locale},
        {dte_end_date: {value: [from, to], op: :between} }
      ]
      selects = {
        rental_charge: [
          :int_rental_charge_id, :int_entity_id, :int_qty, :dec_total, :var_type, :int_inventory_id_id,
          :dec_days, :dec_daily, :dec_weeks, :dec_weekly, :dec_months, :dec_monthly
        ],
        inventory_id:     [{var_inventory_id: :inventory_id_no}, :int_inventory_id],
        inventory:        [:int_category_id, :int_sub_category_id, :int_inventory_type_id],
        inventory_text:   [
          :var_part_no, :var_description, {var_user_field1: :gl_revenue}, {var_user_field2: :gl_cogs_code},
          {var_user_field3: :phase_code}
        ],
        rental:           [:var_rental_number]
      }

      build_and_query(selects: selects, where: wheres, joins: joins, order: "tblInventoryText.varPartNo")

    end


    # rename_attributes intLocationID: 'location_id'

  end
end