attr = DS.attr
Qsupplier.App.Order = DS.Model.extend
  state: attr('string')
  list: DS.belongsTo('list')
  price: attr('number')
  section: DS.belongsTo('section')
  section_id: attr('string')
  product_orders: DS.hasMany('product_order')

  active: (-> @get('state') == 'active').property('state')
  delivered: (-> @get('state') == 'delivered').property('state')
  placed: (-> @get('state') == 'placed').property('state')
  needs_supplier_attention: (-> @get('state') == 'placed' || @get('state') == 'active').property('state')

  #display: (->
    #return '' unless @get('product_orders')
    #@get('product_orders').map((po)-> "#{po.product_name} (#{po.quantity})").join(',')
  #).property('product_orders')

  markClosed: ->
    @set 'state', 'closed'
  total: (->
    @get('product_orders').getEach('total').reduce(((sum, total) -> sum + total), 0)
  ).property('product_orders.@each.quantity', 'product_orders.@each.product.@each.price')

  display: (->
    @get('product_orders').map((po) -> "#{po.get('quantity')} x #{po.get('product.name')}").join(', ')
  ).property('product_orders.@each.quantity', 'product_orders.@each.product.@each.name')
