diff --git a/app/assets/javascripts/darkswarm/controllers/distributor_node_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/distributor_node_controller.js.coffee index a260f81951..7c373326f2 100644 --- a/app/assets/javascripts/darkswarm/controllers/distributor_node_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/distributor_node_controller.js.coffee @@ -1,8 +1,9 @@ Darkswarm.controller "DistributorNodeCtrl", ($scope, HashNavigation, $anchorScroll) -> $scope.toggle = -> - HashNavigation.toggle $scope.distributor - $scope.open = -> - HashNavigation.active($scope.distributor) + HashNavigation.toggle $scope.distributor_id + $scope.open = -> + HashNavigation.active($scope.distributor_id) + if $scope.open() $anchorScroll() diff --git a/app/assets/javascripts/darkswarm/services/orders.js.coffee b/app/assets/javascripts/darkswarm/services/orders.js.coffee index dbfcf207e3..549e4f8a38 100644 --- a/app/assets/javascripts/darkswarm/services/orders.js.coffee +++ b/app/assets/javascripts/darkswarm/services/orders.js.coffee @@ -1,14 +1,19 @@ Darkswarm.factory 'Orders', (orders, CurrentHub, Taxons, Dereferencer, visibleFilter, Matcher, Geo, $rootScope)-> new class Orders orders_by_distributor: {} - distributors = [] + distributor_names_by_id: {} constructor: -> # Populate Orders.orders from json in page. @orders = orders # Organise orders by distributor. for order in orders - if order.distributor?.id - @orders_by_distributor[order.distributor.name] = order - # Can we guarantee order of keys in js? - @distributors = Object.keys(@orders_by_distributor) + if order.distributor? + if @orders_by_distributor[order.distributor.id]? then @orders_by_distributor[order.distributor.id].push order else @orders_by_distributor[order.distributor.id] = [order] + if !@distributor_names_by_id[order.distributor.id] then @distributor_names_by_id[order.distributor.id] = {name: order.distributor.name, balance: 0} + for id in Object.keys(@distributor_names_by_id) + @distributor_names_by_id[id].balance = @orders_by_distributor[id].reduce(((x,y) -> + x + Number(y.outstanding_balance)), 0).toFixed(2) + console.log @distributor_names_by_id + + # Sorting by most orders (most recent/frequent?) diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index 129c9aeed2..0354bcdeab 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -52,7 +52,8 @@ module InjectionHelper end def inject_orders_for_user - inject_json_ams "orders", spree_current_user.orders, Api::OrderSerializer + # Convert ActiveRecord::Relation to array for serialization + inject_json_ams "orders", spree_current_user.orders.where(state: :complete).to_a, Api::OrderSerializer end def inject_json(name, partial, opts = {}) diff --git a/app/serializers/api/order_serializer.rb b/app/serializers/api/order_serializer.rb index 6648cb7cea..d7fb8e6b6d 100644 --- a/app/serializers/api/order_serializer.rb +++ b/app/serializers/api/order_serializer.rb @@ -1,10 +1,36 @@ class Api::OrderSerializer < ActiveModel::Serializer - attributes :id, :completed_at, :total, :state, :shipment_state, :outstanding_balance + attributes :id, :completed_at, :total, :state, :shipment_state, :payment_state, :outstanding_balance, :total_money, :balance_money has_one :distributor, serializer: Api::IdNameSerializer + def completed_at object.completed_at.blank? ? "" : object.completed_at.strftime("%F %T") end + def total_money + to_money(object.total) + end + + def shipment_state + object.shipment_state ? object.shipment_state.humanize : nil # Or a call to t() here? + end + + def payment_state + object.payment_state ? object.payment_state.humanize : nil # Or a call to t() here? + end + + def state + object.state ? object.state.humanize : nil # Or a call to t() here? + end + + def balance_money + to_money(object.outstanding_balance) + end + + private + + def to_money(amount) + {currency_symbol:amount.to_money.currency_symbol, amount:amount.to_money.to_s} + end end diff --git a/app/views/spree/users/_fat.html.haml b/app/views/spree/users/_fat.html.haml index ce16555492..d3e2030408 100644 --- a/app/views/spree/users/_fat.html.haml +++ b/app/views/spree/users/_fat.html.haml @@ -1,3 +1,20 @@ -.row.active_table_row +.row.active_table_row{"ng-click" => "toggle($event)", "ng-class" => "{'closed' : !open()}"} + .container{"bo-text" => "orders[0].distributor.name"} + %span {{orders[0].total_money.currency_symbol}} {{Orders.distributor_names_by_id[distributor_id].balance}} .columns.small-12.medium-7.large-7.fat - %span{"bo-text" => "distributor"} + %table + %th + %tr + %td Transaction + %td Date + %td Payment Status + %td Shipping Status + %td Value + %td Balance + %tr{"ng-repeat" => "order in orders"} + %td {{order.id}} + %td {{order.completed_at}} + %td {{order.payment_state}} + %td {{order.shipment_state}} + %td {{order.total_money.currency_symbol}} {{order.total_money.amount}} + %td {{order.balance_money.currency_symbol}} {{order.balance_money.amount}} diff --git a/app/views/spree/users/show.html.haml b/app/views/spree/users/show.html.haml index 733c7aa6aa..55ccf737a0 100644 --- a/app/views/spree/users/show.html.haml +++ b/app/views/spree/users/show.html.haml @@ -14,9 +14,10 @@ .row{bindonce: true} .small-12.columns .active_table - %distributor.active_table_node.row.animate-repeat{id: "{{distributor}}", - "ng-repeat" => "distributor in Orders.distributors", + %distributor.active_table_node.row.animate-repeat{"ng-repeat" => "(distributor_id, orders) in Orders.orders_by_distributor", "ng-controller" => "DistributorNodeCtrl", - "ng-class" => "{'closed' : !open(), 'open' : open()}"} + "ng-class" => "{'closed' : !open(), 'open' : open(), 'inactive' : !distributor.active}", + id: "{{distributor_id}}"} .small-12.columns = render partial: "spree/users/fat" + -# TODO: Skinny partial