Class: SpreeCmCommissioner::VariantAvailability::PermanentStockQuery
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::VariantAvailability::PermanentStockQuery
- Defined in:
- app/queries/spree_cm_commissioner/variant_availability/permanent_stock_query.rb
Instance Attribute Summary collapse
-
#except_line_item_id ⇒ Object
readonly
Returns the value of attribute except_line_item_id.
-
#from_date ⇒ Object
readonly
Returns the value of attribute from_date.
-
#to_date ⇒ Object
readonly
Returns the value of attribute to_date.
-
#variant ⇒ Object
readonly
Returns the value of attribute variant.
Instance Method Summary collapse
- #available?(quantity) ⇒ Boolean
- #booked_date_quantity ⇒ Object
-
#date_interval ⇒ Object
override this for product that have different date time interval than 1 day.
-
#initialize(variant:, from_date:, to_date:, except_line_item_id: nil) ⇒ PermanentStockQuery
constructor
A new instance of PermanentStockQuery.
Constructor Details
#initialize(variant:, from_date:, to_date:, except_line_item_id: nil) ⇒ PermanentStockQuery
6 7 8 9 10 11 |
# File 'app/queries/spree_cm_commissioner/variant_availability/permanent_stock_query.rb', line 6 def initialize(variant:, from_date:, to_date:, except_line_item_id: nil) @variant = variant @from_date = from_date @to_date = to_date @except_line_item_id = except_line_item_id end |
Instance Attribute Details
#except_line_item_id ⇒ Object (readonly)
Returns the value of attribute except_line_item_id.
4 5 6 |
# File 'app/queries/spree_cm_commissioner/variant_availability/permanent_stock_query.rb', line 4 def except_line_item_id @except_line_item_id end |
#from_date ⇒ Object (readonly)
Returns the value of attribute from_date.
4 5 6 |
# File 'app/queries/spree_cm_commissioner/variant_availability/permanent_stock_query.rb', line 4 def from_date @from_date end |
#to_date ⇒ Object (readonly)
Returns the value of attribute to_date.
4 5 6 |
# File 'app/queries/spree_cm_commissioner/variant_availability/permanent_stock_query.rb', line 4 def to_date @to_date end |
#variant ⇒ Object (readonly)
Returns the value of attribute variant.
4 5 6 |
# File 'app/queries/spree_cm_commissioner/variant_availability/permanent_stock_query.rb', line 4 def variant @variant end |
Instance Method Details
#available?(quantity) ⇒ Boolean
13 14 15 16 17 18 |
# File 'app/queries/spree_cm_commissioner/variant_availability/permanent_stock_query.rb', line 13 def available?(quantity) (from_date..from_date).all? do |booking_date| booked_date_quantity[booking_date].nil? || (booked_date_quantity[booking_date] - quantity) >= 0 end end |
#booked_date_quantity ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/queries/spree_cm_commissioner/variant_availability/permanent_stock_query.rb', line 20 def booked_date_quantity dates_sql = ApplicationRecord.sanitize_sql_array( [ 'SELECT date FROM generate_series(?::date, ?::date, ?) AS date', from_date, to_date, date_interval ] ) @booked_variants ||= Spree::LineItem .complete .select( '(SUM(DISTINCT spree_stock_items.count_on_hand) - SUM(spree_line_items.quantity)) AS available_quantity', 'dates.date AS reservation_date' ) .joins(variant: :stock_items) .where(variant_id: variant.id) .where.not(id: except_line_item_id) .joins("INNER JOIN (#{dates_sql}) dates ON dates.date >= spree_line_items.from_date AND dates.date < spree_line_items.to_date") .group(:variant_id, :reservation_date) .order(:reservation_date) .each_with_object({}) do |record, hash| hash[record.reservation_date.to_date] = record.available_quantity end end |
#date_interval ⇒ Object
override this for product that have different date time interval than 1 day.
50 51 52 |
# File 'app/queries/spree_cm_commissioner/variant_availability/permanent_stock_query.rb', line 50 def date_interval '1 day' end |