supplier improvements
This commit is contained in:
+9
-2
@@ -23,8 +23,7 @@ class List
|
||||
validates :supplier_id, presence: true
|
||||
|
||||
view :by_supplier_id_and_id, key: [:supplier_id, :_id]
|
||||
view :by_supplier_id, key: :supplier_id
|
||||
view :for_supplier_view, key: [:supplier_id, :created_at]
|
||||
view :for_supplier_view, key: [:supplier_id, :state, :created_at]
|
||||
|
||||
view :active_by_table_id_view, type: :custom, map_function: %|function(doc){
|
||||
if(doc.ruby_class == 'List' && doc.state == 'active'){
|
||||
@@ -75,6 +74,14 @@ class List
|
||||
list
|
||||
end
|
||||
|
||||
def self.for_supplier(supplier, options = {})
|
||||
total_entries = database.view(for_supplier_view({startkey: ["#{supplier.id}\u9999"], endkey: [supplier.id], include_docs: false, reduce: true, descending: true}))
|
||||
options[:total_entries] = total_entries
|
||||
with_pagination_options(options) do |options|
|
||||
database.view(for_supplier_view({startkey: ["#{supplier.id}\u9999"], endkey: [supplier.id], include_docs: true, reduce: false, descending: true}.merge(options)))
|
||||
end
|
||||
end
|
||||
|
||||
def self.active_for_supplier(supplier_id, options = {})
|
||||
database.view(active_by_supplier_id_view(key: supplier_id, reduce: false, include_docs: true))
|
||||
end
|
||||
|
||||
@@ -44,6 +44,7 @@ class ProductCategory
|
||||
@week_days = typecasted_value
|
||||
end
|
||||
|
||||
#TODO I am uuuuggggllyyyyy
|
||||
def self.for_user(user, options = {})
|
||||
table = options[:table]
|
||||
raise "ProductCategory.for_user requires a table" unless table.present?
|
||||
@@ -54,19 +55,17 @@ class ProductCategory
|
||||
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 }
|
||||
#products = supplier.products
|
||||
|
||||
# Append other category if not defined by supplier
|
||||
other = product_categories.find(&:other?) || (product_categories << self.other).last # Container for non categorized products
|
||||
#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: 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) }
|
||||
#(products - product_categories.map(&:products).flatten).each{ |p| other.add_product(p) }
|
||||
|
||||
h[:categories] = product_categories.map{|pc| {name: pc.name, products: pc.products.to_a.map{|p| p.as_json}}}.select{|pc| pc && pc[:products].present?}
|
||||
h[:categories] = product_categories.map(&:to_client_format).select(&:present?)
|
||||
h
|
||||
end
|
||||
|
||||
@@ -81,7 +80,15 @@ class ProductCategory
|
||||
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]])))
|
||||
product_categories = database.view(by_supplier_id_and_week_time(view_options.merge(keys: [[supplier.id, week_day], [supplier.id, week_day, minute]])))
|
||||
product_categories.sort_by!{|pc| (pc.position || 90000).to_i }
|
||||
# sort categories
|
||||
product_categories
|
||||
end
|
||||
|
||||
def to_client_format
|
||||
return {} unless products.present?
|
||||
{name: name, products: products.to_a.map{|p| p.as_json}}
|
||||
end
|
||||
|
||||
def other?
|
||||
|
||||
@@ -116,6 +116,10 @@ class Supplier
|
||||
def send_creation_notifications
|
||||
SupplierMailer.creation(self).deliver
|
||||
end
|
||||
|
||||
def week_starts_on_monday?
|
||||
true
|
||||
end
|
||||
private
|
||||
|
||||
def add_section_on_create
|
||||
|
||||
Reference in New Issue
Block a user