From e4f3aae7c08f9b147ea9a761cd8c659b41b6c104 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Fri, 2 Oct 2020 17:18:21 +0100 Subject: [PATCH] Add customer balance to customer list --- app/serializers/api/admin/customer_serializer.rb | 6 +++++- app/views/admin/customers/index.html.haml | 4 ++++ config/locales/en.yml | 1 + lib/open_food_network/column_preference_defaults.rb | 3 ++- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/serializers/api/admin/customer_serializer.rb b/app/serializers/api/admin/customer_serializer.rb index 7648104600..be04c2d9b9 100644 --- a/app/serializers/api/admin/customer_serializer.rb +++ b/app/serializers/api/admin/customer_serializer.rb @@ -1,6 +1,6 @@ class Api::Admin::CustomerSerializer < ActiveModel::Serializer attributes :id, :email, :enterprise_id, :user_id, :code, :tags, :tag_list, :name, - :allow_charges, :default_card_present? + :allow_charges, :default_card_present?, :balance has_one :ship_address, serializer: Api::AddressSerializer has_one :bill_address, serializer: Api::AddressSerializer @@ -13,6 +13,10 @@ class Api::Admin::CustomerSerializer < ActiveModel::Serializer object.name.presence || object.bill_address.andand.full_name end + def balance + OpenFoodNetwork::UserBalanceCalculator.new(object.email, object.enterprise).balance + end + def tags customer_tag_list.map do |tag| tag_rule_map = options[:tag_rule_mapping].andand[tag] diff --git a/app/views/admin/customers/index.html.haml b/app/views/admin/customers/index.html.haml index abf39f47d5..a6d77b2961 100644 --- a/app/views/admin/customers/index.html.haml +++ b/app/views/admin/customers/index.html.haml @@ -55,6 +55,7 @@ %col.tags{ width: "20%", 'ng-show' => 'columns.tags.visible' } %col.bill_address{ width: "10%", 'ng-show' => 'columns.bill_address.visible' } %col.ship_address{ width: "10%", 'ng-show' => 'columns.ship_address.visible' } + %col.balance{ width: "10%", 'ng-show' => 'columns.balance.visible' } %col.actions{ width: "10%"} %thead %tr{ ng: { controller: "ColumnsCtrl" } } @@ -69,6 +70,7 @@ %th.tags{ 'ng-show' => 'columns.tags.visible' }=t('admin.tags') %th.bill_address{ 'ng-show' => 'columns.bill_address.visible' }=t('admin.customers.index.bill_address') %th.ship_address{ 'ng-show' => 'columns.ship_address.visible' }=t('admin.customers.index.ship_address') + %th.balance{ 'ng-show' => 'columns.balance.visible' }=t('admin.customers.index.balance') %th.actions Ask?  %input{ :type => 'checkbox', 'ng-model' => "confirmDelete" } @@ -92,6 +94,8 @@ %a{ id: 'bill-address-link', href: 'javascript:void(0)', "ng-bind" => "customer.bill_address ? customer.bill_address.address1 : '#{t('admin.customers.index.edit')}' | limitTo: 15", 'edit-address-dialog' => true } %td.ship_address{ 'ng-show' => 'columns.ship_address.visible' } %a{ id: 'ship-address-link', href: 'javascript:void(0)', "ng-bind" => "customer.ship_address ? customer.ship_address.address1 : '#{t('admin.customers.index.edit')}' | limitTo: 15", 'edit-address-dialog' => true } + %td.balance{ 'ng-show' => 'columns.balance.visible'} + %span{ 'ng-bind' => '::customer.balance' } %td.actions %a{ 'ng-click' => "deleteCustomer(customer)", :class => "delete-customer icon-trash no-text" } diff --git a/config/locales/en.yml b/config/locales/en.yml index a684672f2f..226e5cf38c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -444,6 +444,7 @@ en: duplicate_code: "This code is used already." bill_address: "Billing Address" ship_address: "Shipping Address" + balance: "Balance" update_address_success: 'Address updated successfully.' update_address_error: 'Sorry! Please input all of the required fields!' edit_bill_address: 'Edit Billing Address' diff --git a/lib/open_food_network/column_preference_defaults.rb b/lib/open_food_network/column_preference_defaults.rb index 273d8d03df..a79cfdd4c7 100644 --- a/lib/open_food_network/column_preference_defaults.rb +++ b/lib/open_food_network/column_preference_defaults.rb @@ -32,7 +32,8 @@ module OpenFoodNetwork code: { name: I18n.t("#{node}.code"), visible: true }, tags: { name: I18n.t("admin.tags"), visible: true }, bill_address: { name: I18n.t("#{node}.bill_address"), visible: true }, - ship_address: { name: I18n.t("#{node}.ship_address"), visible: true } + ship_address: { name: I18n.t("#{node}.ship_address"), visible: true }, + balance: { name: I18n.t("#{node}.balance"), visible: true } } end