Continue adding product variants

This commit is contained in:
2015-04-13 17:57:06 +02:00
parent 6746eaa383
commit 2b036368cf
32 changed files with 169 additions and 12224 deletions
+15 -7
View File
@@ -274,16 +274,24 @@ class List
state == 'active'
end
def place_order(products: {}, user: nil, employee: nil)
return false unless products.any?
def place_order(product_orders: [], user: nil, employee: nil)
return false unless product_orders.any?
order = Order.create list: self, supplier: supplier, user: user, employee: employee, section_id: section_id, table_id: table_id
return unless order.id
orders_placed_count = supplier.increment_orders_placed_count!
loaded_products = self.class.database.load_document products.keys
products.each do |product_id, quantity|
product = loaded_products.find{|p| p.id == product_id} # to get the price
if quantity.to_i > 0
ProductOrder.create(order: order, product_id: product_id, quantity: quantity, price: product.price, product_name: product.name)
loaded_products = self.class.database.load_document product_orders.map{|po| po['product_id']}
product_orders.each do |product_order|
next unless product = loaded_products.find{|p| p.id == product_order['product_id']} # to get the price
quantity = product_order['quantity'].to_i
if quantity > 0
ProductOrder.create(
order: order,
product_id: product.id,
quantity: quantity,
price: product.price,
product_name: product.name,
product_variant: product_order['product_variant']
)
end
end
set_price
+1 -1
View File
@@ -15,7 +15,7 @@ class Product
#has_and_belongs_to_many :product_categories, storing_keys: false
belongs_to :supplier # direct! category is an aid
has_many :product_orders
has_many :product_variants
has_many :product_variants, dependent: :destroy
attr_protected :supplier_id
+3 -1
View File
@@ -5,6 +5,7 @@ class ProductOrder
property :quantity, type: Fixnum
property :price, type: Float
property :product_name
property :product_variant
belongs_to :product
belongs_to :order
@@ -14,7 +15,8 @@ class ProductOrder
# Getter for product name. If a supplier deletes a product, that has product_orders, the product
# will become nil. This method should handle this case.
alias_method :direct_product_name, :product_name
def product_name
product.try(:name) || '[deleted]'
direct_product_name.presence || product.try(:name) || '[deleted]'
end
end
+1
View File
@@ -4,4 +4,5 @@ class ProductVariant
property :name
property :position, type: Fixnum, default: 0
belongs_to :product
belongs_to :supplier
end
+1
View File
@@ -35,6 +35,7 @@ class Supplier
#has_many :orders, through: :lists
has_many :products, dependent: :destroy
has_many :product_variants
has_many :product_categories, dependent: :destroy
has_many :tables, dependent: :destroy
has_many :lists, dependent: :destroy