mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Data organisation in angular, basic table structure and balance calculation.
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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?)
|
||||
|
||||
@@ -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 = {})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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}}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user