Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sac points tracking system:tm: #1015

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ signing-key.pem
/yarn-error.log
yarn-debug.log*
.yarn-integrity
yarn.lock

# generated translations
app/javascript/src/translations.js
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/admin/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
class Admin::ActivitiesController < ApplicationController
impressionist actions: [:update, :destroy]

def load_sac_categories
ConstipatedKoala::Application.config.sac_categories.map { |c| [c[:name], c[:id]] }
end

def index
@activities = Activity.study_year(params['year']).order(start_date: :desc)
@years = (Activity.take(1).first.start_date.year..Date.today.study_year).map do |year|
["#{ year }-#{ year + 1 }", year]
end.reverse

@activity = Activity.new
@sac_categories = load_sac_categories
end

def show
Expand All @@ -18,6 +23,7 @@ def show
@recipients = @activity.payment_mail_recipients
@attendees = @activity.ordered_attendees
@reservists = @activity.ordered_reservists
@sac_categories = load_sac_categories
end

def create
Expand Down Expand Up @@ -100,6 +106,7 @@ def activity_post_params
:is_seniors,
:participant_limit,
:show_participants,
:sac_category,
:_destroy)
end
end
31 changes: 31 additions & 0 deletions app/controllers/admin/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,43 @@ def show
# Pagination for checkout transactions
@limit = params[:limit] ? params[:limit].to_i : 10

@sac_points = @member.activities
.filter { |ac| ac.sac_category or ac.participants.any?(&:sac_points) }
.map do |ac|
# if the participant has a custom number of points
category = ConstipatedKoala::Application.config.sac_categories.find { |c| c[:id] == ac.sac_category } # Find the category in the list
custom_points = ac.participants.where(member: @member).first.sac_points
p ac.sac_category
{ points: (custom_points or category[:points]), activity: ac }
end
@sac_points_total = @sac_points.reduce(0) { |ac, record| ac + record[:points] }

@pagination, @transactions = pagy(CheckoutTransaction
.where(checkout_balance: CheckoutBalance
.find_by(member_id: params[:id]))
.order(created_at: :desc), items: 10)
end

def sac
data = "name;category;points;date;activity"
member = Member.find(params[:member_id])

# Find all activities a members participated in with sac points
sac_eligible = member.activities.filter do |ac|
(ac.sac_category? and ac.sac_category > 0) or ac.participants.where(member: member).first.sac_points?
end

sac_categories = ConstipatedKoala::Application.config.sac_categories
# Create csv rows for every activity
sac_eligible.each do |ac|
category = ((sac_categories.find { |c| c[:id] == ac.sac_category }) or { name: "" })
custom_points = ac.participants.where(member: member).first.sac_points
data += "\n#{ member.name };#{ category[:name] };#{ custom_points or category[:points] };#{ ac.start_date };#{ ac.name }"
end

send_data(data, { filename: "data.csv" })
end

def new
@member = Member.new

Expand Down
4 changes: 4 additions & 0 deletions app/controllers/admin/participants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def update

message = 'price'
@participant.update(price: params[:price])
elsif params[:sac_points].present?
raise('negative sac points') unless params[:sac_points].to_i >= 0

@participant.update(sac_points: params[:sac_points])
end

if @participant.save
Expand Down
139 changes: 75 additions & 64 deletions app/javascript/src/admin/activities.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ function bind_activities() {

// Admin updates participant's price
// [PATCH] participants
$("#participants").find("input.price").on("change", participant.updatePrice);
$("#participants")
.find("input.price")
.on("change", participant.updateValue("price"));
$("#participants")
.find("input.sac")
.on("change", participant.updateValue("sac_points"));
}

/*
Expand Down Expand Up @@ -275,73 +280,79 @@ var participant = {
},

//Admin updates participant's price
updatePrice: function () {
var row = $(this).closest("tr");
var token = encodeURIComponent(
$(this).closest(".page").attr("data-authenticity-token")
);
var price = $(this).val().replace(",", ".");

// If left blank assume 0
if (!price) {
price = 0;
$(this).val(0);
}

// Make it a bit more pretty
if (!isNaN(price)) $(this).val(parseFloat(price).toFixed(2));

$.ajax({
url:
"/activities/" +
row.attr("data-activities-id") +
"/participants/" +
row.attr("data-id"),
type: "PATCH",
data: {
authenticity_token: token,
price: price,
},
})
.done(function (data) {
$(row)
.find("button.unpaid")
.empty()
.addClass("paid btn-warning")
.removeClass("d-none unpaid btn-primary")
.append('<i class="fa fa-fw fa-times"></i>');
$(row).find("button.paid").removeClass("d-none");
$(row).removeClass("in-debt");

if (price > 0) {
$(row).addClass("in-debt");

$("#mail").trigger("recipient_unpayed", [
$(row).attr("data-id"),
$(row).find("a").html(),
$(row).attr("data-email"),
]);
} else {
$(row).find("button.paid").addClass("d-none");
updateValue: (field) =>
function () {
var row = $(this).closest("tr");
var token = encodeURIComponent(
$(this).closest(".page").attr("data-authenticity-token")
);
var value = $(this).val().replace(",", ".");

$("#mail").trigger("recipient_payed", [
$(row).attr("data-id"),
$(row).find("a").html(),
$(row).attr("data-email"),
]);
}
// If left blank assume 0
if (!value) {
value = 0;
$(this).val(0);
}

participant.update_debt_header(
data.activity.paid_sum,
data.activity.price_sum
);
// Make it a bit more pretty
if (field === "price" && !isNaN(value))
$(this).val(parseFloat(value).toFixed(2));

toastr.success(I18n.t("admin.activities.info.price_changed"));
$.ajax({
url:
"/activities/" +
row.attr("data-activities-id") +
"/participants/" +
row.attr("data-id"),
type: "PATCH",
data: {
authenticity_token: token,
[field]: value,
},
})
.fail(function () {
toastr.error(I18n.t("admin.activities.info.price_error"));
});
},
.done(function (data) {
if (field === "price") {
$(row)
.find("button.unpaid")
.empty()
.addClass("paid btn-warning")
.removeClass("d-none unpaid btn-primary")
.append('<i class="fa fa-fw fa-times"></i>');
$(row).find("button.paid").removeClass("d-none");
$(row).removeClass("in-debt");

if (value > 0) {
$(row).addClass("in-debt");

$("#mail").trigger("recipient_unpayed", [
$(row).attr("data-id"),
$(row).find("a").html(),
$(row).attr("data-email"),
]);
} else {
$(row).find("button.paid").addClass("d-none");

$("#mail").trigger("recipient_payed", [
$(row).attr("data-id"),
$(row).find("a").html(),
$(row).attr("data-email"),
]);
}

participant.update_debt_header(
data.activity.paid_sum,
data.activity.price_sum
);

toastr.success(I18n.t("admin.activities.info.price_changed"));
} else {
toastr.success(I18n.t("admin.activities.info.sac_changed"));
}
})
.fail(function () {
toastr.error(I18n.t("admin.activities.info.price_error"));
});
},
};

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

%td.price-input-td{:style => 'padding: 0px; min-width: 10%; width: 10%; text-align:left;' }
%input.price{ :type => 'text', :value => number_to_currency(participant.currency, :unit => ''), :autocomplete=>'off'}
%td.sac-input-td{:style => 'padding: 0px; min-width: 10%; width: 10%; text-align:left;' }
- if @activity.sac_category
- category = ConstipatedKoala::Application.config.sac_categories.find { |c| c[:id] == @activity.sac_category }
%input.sac{ :type => 'text', :value => (participant.sac_points or (category[:points] if category) or 0), :autocomplete=>'off'}
%td.notes-td
- if participant.notes
= participant.notes
Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/activities/partials/_edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@
.col-lg-6
= f.label :participant_limit
= f.number_field :participant_limit, min: 0, data: {original: @activity.participant_limit}, id: "participant_limit"
.col-lg-6
= f.label :sac_category
= f.select :sac_category, [["None", 0]] + @sac_categories
%hr.my-4
.row
.col
%hr
Expand Down
9 changes: 9 additions & 0 deletions app/views/admin/activities/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
= I18n.t('activerecord.attributes.activity.name')
%td{ :style => 'padding: 0px; min-width: 10%; width: 10%; text-align:left;' }
= I18n.t('activerecord.attributes.activity.price')
- if @activity.sac_category
%td{ :style => 'padding: 0px; min-width: 10%; width: 10%; text-align:left;' }
= I18n.t('admin.sac.title')
- if !@is_summarized.nil?
%td
= I18n.t('activerecord.attributes.activity.notes')
Expand All @@ -60,6 +63,8 @@

%td.price-input-td{:style => 'padding: 0px; min-width: 10%; width: 10%; text-align:left;' }
%input.price{ :type => 'text', :value => number_to_currency(participant.currency, :unit => ''), :autocomplete=>'off'}
%td.sac-input-td{:style => 'padding: 0px; min-width: 10%; width: 10%; text-align:left;' }
%input.sac{ :type => 'text', :value => (participant.sac_points or @activity.sac_points or 0), :autocomplete=>'off'}
%td.notes-td
%td.buttons
.btn-group
Expand All @@ -79,6 +84,10 @@
%td{ :style => 'text-align: left;' }
- if [email protected]?
%span= number_to_currency(@activity.price, :unit => '€')
%td{ :style => 'text-align: left;' }
- if @activity.sac_category
- category = ConstipatedKoala::Application.config.sac_categories.find { |c| c[:id] == @activity.sac_category }
%span= (category[:points] if category) or 0
%td
%td

Expand Down
23 changes: 23 additions & 0 deletions app/views/admin/members/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,29 @@
%button.btn.btn-default.destroy
%i.fa.fa-fw.fa-trash

.card
.card-header
= I18n.t('admin.sac.title')
= link_to I18n.t("admin.sac.download"), member_sac_path(@member.id)
%table.table.table-striped#sac
%thead
%tr
%th= I18n.t('admin.activities.single')
%th= I18n.t('admin.sac.category')
%th= I18n.t('admin.sac.title')
%tbody
- @sac_points.each do |record|
- points, sac_ac = record.values_at(:points, :activity)
- categories = ConstipatedKoala::Application.config.sac_categories
%tr
- category = categories.find {|c| c[:id] == sac_ac.sac_category }
%td= link_to sac_ac.name, sac_ac
%td= category[:name] if sac_ac.sac_category?
%td= points
%tr
%td= I18n.t('admin.sac.total')
%td
%td= @sac_points_total

.card
.card-header
Expand Down
12 changes: 12 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,17 @@ class Application < Rails::Application

# Generate translations.json
config.middleware.use(I18n::JS::Middleware)

# Sac categories
# Expected format: { id: <int>, name: <string>, points: <int> }
config.sac_categories = [
{ id: 1, name: "Studiereis", points: 4 },
{ id: 2, name: "Inhouse dag", points: 2 },
{ id: 3, name: "Consultancy day", points: 2 },
{ id: 4, name: "Lezing / workshop", points: 1 },
{ id: 5, name: "Bedrijvendiner", points: 2 },
{ id: 6, name: "Bedrijvenborrel", points: 2 },
{ id: 7, name: "Traders evenement", points: 3 }
].freeze
end
end
6 changes: 6 additions & 0 deletions config/locales/admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ en:
already_added: This participant was already added
price_changed: The participants fee has changed
price_error: No connection or not a number
sac_changed: The number of sac points has been changed
new: New activity
remove_participant: Do you want to remove %{user} as participant?
save: Save activity?
Expand Down Expand Up @@ -140,6 +141,11 @@ en:
status_text: Status
tags: Tags
title: Title
sac:
category: Category
download: Download report
title: Sac points
total: Total
settings:
action: Action
address: IP address
Expand Down
6 changes: 6 additions & 0 deletions config/locales/admin.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ nl:
already_added: Deze persoon is al toegevoegd
price_changed: Het deelname bedrag is veranderd
price_error: Geen verbinding of geen nummer
sac_changed: Het aantal sac punten is veranderd
new: Nieuwe activiteit
remove_participant: Deelname van %{user} verwijderen?
save: Activiteit opslaan?
Expand Down Expand Up @@ -140,6 +141,11 @@ nl:
status_text: Status
tags: Labels
title: Titel
sac:
category: Categorie
download: Download rapport
title: Sac punten
total: Totaal
settings:
action: Actie
address: IP-adres
Expand Down
Loading
Loading