Make time dependent product categories work
This commit is contained in:
@@ -4,6 +4,9 @@ class ProductCategory
|
||||
property :name
|
||||
property :position, type: Fixnum, default: 0
|
||||
property :week_days, type: Array, default: Array.new(7, 1)
|
||||
property :full_day, type: :boolean, default: true
|
||||
property :start_from, type: Fixnum, default: 0
|
||||
property :end_on, type: Fixnum, default: 2880 # Two days in minutes
|
||||
|
||||
belongs_to :supplier
|
||||
has_and_belongs_to_many :products, storing_keys: true
|
||||
@@ -13,7 +16,24 @@ class ProductCategory
|
||||
validates :name, presence: true
|
||||
validates :position, numericality: true
|
||||
validates :supplier_id, presence: true
|
||||
validates :end_on, numericality: {less_than: 2880}, presence: {unless: :full_day?}
|
||||
|
||||
view :by_supplier_id_and_id, key: [:supplier_id, :_id]
|
||||
view :by_supplier_id_and_week_time, type: :custom, map_function: "function(doc){
|
||||
if(doc.ruby_class == 'ProductCategory'){
|
||||
for(var i=0;i<7;i++){
|
||||
if(doc.full_day || !doc.end_on){
|
||||
if(doc.week_days[i]) emit([doc.supplier_id, i], 1);
|
||||
}else{
|
||||
for(var j=(doc.start_from || 0);j < doc.end_on;j++){
|
||||
if(doc.week_days[i]) emit([doc.supplier_id, i, j], 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}", reduce_function: :_sum
|
||||
|
||||
#start_key: [supplier_id, wday, minute], end_key: [supplier_id, wday, 2880, minute]
|
||||
|
||||
#alias orignal_week_days= week_days=
|
||||
def week_days=(ary)
|
||||
@@ -27,12 +47,14 @@ class ProductCategory
|
||||
def self.for_user(user, options = {})
|
||||
table = options[:table]
|
||||
raise "ProductCategory.for_user requires a table" unless table.present?
|
||||
list = options[:list] || table.active_list
|
||||
list = options[:list] || table.active_list # Performance option to specify it in stead of loading it
|
||||
supplier = options[:supplier] || table.supplier # same as list
|
||||
|
||||
# Get supplier objects
|
||||
products = table.supplier.products
|
||||
product_categories = table.supplier.product_categories || []
|
||||
product_categories = for_supplier_in_time(supplier, options[:time])
|
||||
|
||||
# The following part is creating the products hash. This should not be performed here!!!!!! #TODO fix this!!!@@!!@@!!@
|
||||
products = supplier.products
|
||||
# sort categories
|
||||
product_categories.sort_by!{|pc| (pc.position || 90000).to_i }
|
||||
|
||||
@@ -40,7 +62,7 @@ class ProductCategory
|
||||
other = product_categories.find(&:other?) || (product_categories << self.other).last # Container for non categorized products
|
||||
|
||||
# Initialize base return object
|
||||
h = {table_number: table.number, table_occupied: table.occupied?, supplier_name: table.supplier.name, my_list: list.try(:user_ids).to_a.include?(user.id)}
|
||||
h = {table_number: table.number, table_occupied: table.occupied?, supplier_name: supplier.name, my_list: list.try(:user_ids).to_a.include?(user.id)}
|
||||
|
||||
(products - product_categories.map(&:products).flatten).each{ |p| other.add_product(p) }
|
||||
|
||||
@@ -48,6 +70,20 @@ class ProductCategory
|
||||
h
|
||||
end
|
||||
|
||||
# time is not by default Time.now.utc since it can be specified as nil
|
||||
def self.for_supplier_in_time(supplier, time = nil)
|
||||
time ||= Time.now.utc
|
||||
minute = (time.seconds_since_midnight/60).round
|
||||
week_day = time.wday
|
||||
if minute < supplier.night_offset
|
||||
minute += 1440 # 1440 is 1 day in minutes 1.day/1.minute
|
||||
week_day -= 1
|
||||
week_day = 6 if week_day < 0 # Saturday
|
||||
end
|
||||
view_options = {reduce: false, include_docs: true}
|
||||
database.view(by_supplier_id_and_week_time(view_options.merge(keys: [[supplier.id, week_day], [supplier.id, week_day, minute]])))
|
||||
end
|
||||
|
||||
def other?
|
||||
%w[other overig].include?(name.to_s.downcase)
|
||||
end
|
||||
|
||||
@@ -7,7 +7,7 @@ class Supplier
|
||||
property :name
|
||||
property :open, type: :boolean, default: false
|
||||
property :time_zone, default: 'UTC'
|
||||
property :night_offset, type: Fixnum, default: 0 # Hours
|
||||
property :night_offset, type: Fixnum, default: 0 # Minutes
|
||||
|
||||
#LOCATION
|
||||
property :lat, type: Float, default: 52.08062426379751
|
||||
|
||||
Reference in New Issue
Block a user