Continue adding product variants
This commit is contained in:
@@ -45,6 +45,11 @@ App.MenuProductComponent = Ember.Component.extend
|
||||
product_variant.rollback()
|
||||
@get('product').rollback()
|
||||
@set 'editMode', false
|
||||
addProductVariant: ->
|
||||
product_variant = @get('targetObject.store').createRecord('product_variant')
|
||||
@get('product.product_variants').addObject product_variant
|
||||
removeProductVariant: (product_variant)->
|
||||
product_variant.destroyRecord()
|
||||
didInsertElement: ->
|
||||
@set 'editMode', true if @get('product.isNew')
|
||||
namePlaceholder: (-> t "attributes.product.name").property()
|
||||
|
||||
@@ -23,9 +23,15 @@ if editMode
|
||||
img src=product.image_src
|
||||
each product_variant in product.product_variants
|
||||
.row
|
||||
.small-3.medium-2.large-1.columns plus
|
||||
.small-9.medium-5.large-4.columns.end
|
||||
= input value=product_variant.name
|
||||
.small-1.columns
|
||||
.small-8.medium-5.large-4.columns= input value=product_variant.name
|
||||
.small-2.medium-6.large-7.columns
|
||||
button.remove-product-variant{action "removeProductVariant" product_variant}: span
|
||||
.row
|
||||
.small-12.columns
|
||||
button.add-product-variant{ action "addProductVariant"}
|
||||
span.icon
|
||||
= t 'product_variant.add_product_variant'
|
||||
else
|
||||
if showProduct
|
||||
.row
|
||||
|
||||
@@ -13,4 +13,4 @@ form.form-horizontal
|
||||
.form-field= number-field numericValue=number_end
|
||||
hr
|
||||
button.modal-close{action "close"}=t 'section.add_tables.modal.close_button'
|
||||
button.modal-confirm.right{action "addTables"}=t 'section.add_tables.modal.add_button'
|
||||
button.modal-confirm.right{action "ok"}=t 'section.add_tables.modal.add_button'
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
@ttry = (path, vars={})->
|
||||
@t(path, $.extend(vars, emptyWhenNotFound: true))
|
||||
|
||||
|
||||
# return translation in the form
|
||||
# <span data-t="models.table">Tafel</span>
|
||||
@tspan = (path, vars={}) -> "<span data-t='#{path}' class='translation' data-t-attributes='#{JSON.stringify(vars)}'>#{t(path, vars)}</span>"
|
||||
|
||||
@@ -4,11 +4,13 @@ App.MenuProductComponent = Ember.Component.extend
|
||||
specific_id: (-> "order-product-#{@get('product.id')}").property('product.id')
|
||||
orderProducts: false
|
||||
target: -> @get('parentView.targetObject')
|
||||
showDescriptionIcon: Ember.computed.or 'product.description', 'product.product_variants.length'
|
||||
|
||||
actions:
|
||||
addProduct: (product)->
|
||||
if existing = @target().store.all('product_order').find((po)-> po.get('product') == product and not po.get('order'))
|
||||
existing.increment()
|
||||
if product.get('product_variants.length')
|
||||
@target().modal 'product_variant_select', model: product
|
||||
else
|
||||
@target().store.createRecord 'product_order', product: product, price: product.get('price')
|
||||
product.addOrderItem()
|
||||
showProductDescription: (product)->
|
||||
@target().modal 'product_info', model: product, title: product.get('name')
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
@App.modals.ProductVariantSelectController = @App.modals.BaseController.extend
|
||||
actions:
|
||||
chooseProductVariant: (product_variant)->
|
||||
@get('model').addOrderItem(product_variant: product_variant.get('name'))
|
||||
@send 'close'
|
||||
@@ -31,10 +31,17 @@ App.ProductOrdersController = Ember.ArrayController.extend
|
||||
|
||||
#orders = @store.all('product_order').toArray()
|
||||
#data = @get('product_orders').map( (po)->po.serialize() )
|
||||
dataObject = order: {table_id: @get('controllers.table.model.id')}
|
||||
@get('product_orders').forEach (product_order)-> dataObject['order'][product_order.get('product.id')] = product_order.get('quantity')
|
||||
Ember.$.post "#{$data_host}/user/orders.json", dataObject, (response) =>
|
||||
@store.pushPayload('order', response) if response.order
|
||||
dataObject = {table_id: @get('controllers.table.model.id')}
|
||||
dataObject.product_orders = @get('product_orders').map( (po) -> po.serialize()).toArray()
|
||||
#@get('product_orders').forEach (product_order)-> dataObject['order'][product_order.get('product.id')] = product_order.get('quantity')
|
||||
Ember.$.ajax
|
||||
type: 'POST'
|
||||
dataType: 'json'
|
||||
contentType: 'application/json'
|
||||
url: "#{$data_host}/user/orders.json"
|
||||
data: JSON.stringify(dataObject)
|
||||
success: (response) =>
|
||||
@store.pushPayload('order', response) if response.order
|
||||
@transitionToRoute 'active_list'
|
||||
@get('product_orders').invoke 'unloadRecord'
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
attr = DS.attr
|
||||
App.ProductVariant = DS.Model.extend
|
||||
name: attr 'string'
|
||||
product: DS.belongsTo 'product'
|
||||
position: attr 'number', defaultValue: 0
|
||||
@@ -6,5 +6,26 @@ App.Product = DS.Model.extend
|
||||
position: attr('number', defaultValue: 0)
|
||||
active: attr 'boolean', defaultValue: true
|
||||
image: attr()
|
||||
product_category: DS.belongsTo('product_category')
|
||||
product_orders: DS.hasMany('product_order')
|
||||
product_category: DS.belongsTo('product-category')
|
||||
product_orders: DS.hasMany('product-order')
|
||||
product_variants: DS.hasMany 'product-variant'
|
||||
sorted_product_variants: Ember.computed 'product_variants.@each.name', 'product_variants.@each.position', ->
|
||||
@get('product_variants').sortBy('position')
|
||||
|
||||
variantsDisplay: Ember.computed 'sorted_product_variants', ->
|
||||
@get('sorted_product_variants').mapBy('name').join(', ')
|
||||
|
||||
addOrderItem: (options = {})->
|
||||
#if existing = @store.all('product_order').find((po)-> po.get('product') == product and not po.get('order'))
|
||||
if options.product_variant
|
||||
existing = @get('product_orders').find( (po)-> !po.get('order') and po.get('product_variant') is options.product_variant )
|
||||
else
|
||||
existing = @get('product_orders').find( (po)-> !po.get('order') )
|
||||
|
||||
if existing
|
||||
existing.increment()
|
||||
else
|
||||
@store.createRecord 'product_order',
|
||||
product: this
|
||||
price: @get('price')
|
||||
product_variant: options.product_variant
|
||||
|
||||
@@ -4,12 +4,17 @@ App.ProductOrder = DS.Model.extend
|
||||
price: attr 'number'
|
||||
product_name: attr('string')
|
||||
product: DS.belongsTo('product')
|
||||
product_variant: attr('string')
|
||||
order: DS.belongsTo('order')
|
||||
placed: attr('boolean', defaultValue: false) # virtual attribute for new orders to be placed, should be more elegant
|
||||
increment: ->
|
||||
@set('quantity', @get('quantity') + 1)
|
||||
total: (-> @get('quantity') * @get('price')).property('quantity', 'price')
|
||||
display: (-> "#{@get('quantity')} x #{@get('product.name')}").property('quantity', 'product.name')
|
||||
display: Ember.computed 'quantity', 'product_variant', 'product.name', ->
|
||||
disp = "#{@get('quantity')} x #{@get('product.name')}"
|
||||
if variant = @get('product_variant')
|
||||
disp = "#{disp} (#{variant})"
|
||||
disp.htmlSafe()
|
||||
setOrder: (order)->
|
||||
@set('placed', true)
|
||||
@set('order', order)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
if product.description
|
||||
if showDescriptionIcon
|
||||
button.show-product-description{action "showProductDescription" product}
|
||||
span
|
||||
else
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
if image
|
||||
.right: img src=image.small alt=""
|
||||
p==description
|
||||
if product_variants
|
||||
h4= t 'models.plural.product_variant'
|
||||
ul
|
||||
each product_variant in product_variants
|
||||
li= product_variant.name
|
||||
hr
|
||||
button{action "close"}= t 'modal.info.close'
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
if model.image
|
||||
.right: img src=model.image.small alt=""
|
||||
each product_variant in product_variants
|
||||
.row
|
||||
.small-8.columns: a{action "chooseProductVariant" product_variant}= product_variant.name
|
||||
.small-4.columns: a.choose-product-variant-button{action "chooseProductVariant" product_variant}= t 'product_variant.choose'
|
||||
hr
|
||||
button.modal-close{action "close"}= t 'modal.info.close'
|
||||
@@ -90,6 +90,15 @@
|
||||
@extend .fa
|
||||
@extend .fa-lg
|
||||
@extend .fa-trash
|
||||
.remove-product-variant
|
||||
+push-button($bg: $alert-color, $padding: 5px)
|
||||
span
|
||||
@extend .fa, .fa-trash
|
||||
.add-product-variant
|
||||
+button($bg: $success-color, $padding: $button-tny)
|
||||
.icon
|
||||
@extend .fa, .fa-plus
|
||||
margin-right: 6px
|
||||
.error
|
||||
color: $alert-color
|
||||
input
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
.modal
|
||||
margin: auto
|
||||
margin-top: 90px
|
||||
width: 300px
|
||||
width: 90%
|
||||
max-width: 756px
|
||||
background-color: #fff
|
||||
padding: 1em
|
||||
z-index: 9085
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
.choose-product-variant-button
|
||||
+button($padding: $button-tny)
|
||||
margin: 0
|
||||
margin-bottom: 6px
|
||||
@@ -24,6 +24,13 @@ module Suppliers
|
||||
# POST /product_variants
|
||||
# POST /product_variants.json
|
||||
def create
|
||||
@product_variant = ProductVariant.new(product_variant_params)
|
||||
@product_variant.supplier = current_supplier
|
||||
if @product_variant.save
|
||||
render json: @product_variant
|
||||
else
|
||||
render json: {errors: @product_variant.errors}, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
# PUT /product_variants/1
|
||||
@@ -44,13 +51,10 @@ module Suppliers
|
||||
# DELETE /product_variants/1
|
||||
# DELETE /product_variants/1.json
|
||||
def destroy
|
||||
@product_variant = ProductVariant.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
|
||||
#@product_variant = ProductVariant.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
|
||||
@product_variant = ProductVariant.find(params[:id])
|
||||
@product_variant.destroy
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to suppliers_product_variants_url, notice: t('action.destroy.successfull', model: ProductVariant.model_name.human) }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
render json: {}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -6,9 +6,6 @@ module Users
|
||||
# POST /user/orders
|
||||
def create
|
||||
# render json: {}, status: :unprocessable_entity and return unless params[:order].present? && params[:order][:product_orders].present?
|
||||
# converted_order = params[:order][:product_orders].each_with_object({}){|po, o| o[po[:product_id]] = po[:quantity] }
|
||||
converted_order = params[:order]
|
||||
table_id = params[:order].delete('table_id')
|
||||
if list = current_user.active_list
|
||||
render json: {}, status: :not_acceptable and return unless list.supplier.open?
|
||||
else
|
||||
@@ -16,8 +13,8 @@ module Users
|
||||
#NOTE: security bug here!!!!!!
|
||||
# - supplier.open?
|
||||
# - etc....
|
||||
render json: {}, status: :unprocessable_entity and return unless table_id.present?
|
||||
table = Table.find(table_id)
|
||||
render json: {}, status: :unprocessable_entity and return unless params[:table_id].present?
|
||||
table = Table.find(params[:table_id])
|
||||
render json: {}, status: :not_acceptable and return unless table.supplier.open?
|
||||
|
||||
if table.occupied?
|
||||
@@ -27,8 +24,8 @@ module Users
|
||||
|
||||
list = List.from_table( table, current_user )
|
||||
end
|
||||
order = list.place_order products: converted_order, user: current_user
|
||||
render json: order, serializer: OrderSerializer
|
||||
order = list.place_order product_orders: params[:product_orders], user: current_user
|
||||
render json: order, serializer: OrderSerializer
|
||||
#render nothing: true
|
||||
end
|
||||
end
|
||||
|
||||
+15
-7
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4,4 +4,5 @@ class ProductVariant
|
||||
property :name
|
||||
property :position, type: Fixnum, default: 0
|
||||
belongs_to :product
|
||||
belongs_to :supplier
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Used for user ember1
|
||||
class ProductOrderSerializer < Qwaiter::Serializer
|
||||
attributes :order_id, :product_id, :quantity, :price, :product_name
|
||||
attributes :order_id, :product_id, :quantity, :price, :product_name, :product_variant
|
||||
end
|
||||
|
||||
@@ -78,3 +78,5 @@ en:
|
||||
selected_products:
|
||||
clear: Clear
|
||||
order: Order
|
||||
product_variant:
|
||||
add_product_variant: Add ${models.product_variant}
|
||||
|
||||
@@ -9,6 +9,7 @@ en:
|
||||
product: Product
|
||||
order: Order
|
||||
product_category: Product category
|
||||
product_variant: Variant
|
||||
section: Section
|
||||
join_request: Join request
|
||||
user_feedback: User feedback
|
||||
@@ -26,6 +27,7 @@ en:
|
||||
product: Products
|
||||
order: Orders
|
||||
product_category: Product categories
|
||||
product_variant: Variants
|
||||
section: Sections
|
||||
join_request: Join requests
|
||||
user_feedback: User feedbacks
|
||||
@@ -52,6 +54,8 @@ en:
|
||||
visible: Visible?
|
||||
created_at: Created
|
||||
image: Image
|
||||
product_variant:
|
||||
name: Name
|
||||
list:
|
||||
created_at: Created
|
||||
state: Status
|
||||
|
||||
@@ -9,6 +9,7 @@ nl:
|
||||
product: Product
|
||||
order: Bestelling
|
||||
product_category: Product categorie
|
||||
product_variant: Variant
|
||||
section: Afdeling
|
||||
join_request: Deelname verzoek
|
||||
employee: Werknemer
|
||||
@@ -25,6 +26,7 @@ nl:
|
||||
product: Producten
|
||||
order: Bestellingen
|
||||
product_category: Product categorieen
|
||||
product_variant: Varianten
|
||||
section: Afdelingen
|
||||
join_request: Deelname verzoeken
|
||||
employee: Werknemers
|
||||
@@ -50,6 +52,8 @@ nl:
|
||||
active: "Actief?"
|
||||
created_at: Aangemaakt
|
||||
image: Afbeelding
|
||||
product_variant:
|
||||
name: Naam
|
||||
list:
|
||||
created_at: Aangemaakt
|
||||
state: Status
|
||||
|
||||
@@ -76,6 +76,8 @@ nl:
|
||||
selected_products:
|
||||
clear: Leegmaken
|
||||
order: Bestellen
|
||||
product_variant:
|
||||
add_product_variant: ${models.product_variant} toevoegen
|
||||
views:
|
||||
pagination:
|
||||
first: "« Eerste"
|
||||
|
||||
-12183
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,13 @@
|
||||
moduleForModel 'product', 'Product model',
|
||||
needs: [
|
||||
'model:product_variant',
|
||||
'model:product_category',
|
||||
'model:section_area',
|
||||
'model:product_order',
|
||||
'model:order',
|
||||
'model:supplier'
|
||||
]
|
||||
|
||||
#test "Nothing yet", ->
|
||||
#element = @subject()
|
||||
#assert 2, 2
|
||||
@@ -1,5 +1,5 @@
|
||||
moduleForModel 'section-element', 'Section element model',
|
||||
needs: ['model:section', 'model:table']
|
||||
needs: ['model:section', 'model:table', 'model:section-area']
|
||||
|
||||
test "box_size", ->
|
||||
element = @subject
|
||||
|
||||
Reference in New Issue
Block a user