2018-10-25 16:51:20 +02:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
no-return-assign,
|
|
|
|
no-undef,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2018-10-25 16:50:16 +02:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS205: Consider reworking code to avoid use of IIFEs
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2018-11-21 11:08:53 +01:00
|
|
|
'use strict';
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Controller used in the prices edition page
|
|
|
|
*/
|
2020-05-25 15:16:40 +02:00
|
|
|
Application.Controllers.controller('EditPricingController', ['$scope', '$state', '$uibModal', '$filter', 'TrainingsPricing', 'Credit', 'Pricing', 'Plan', 'Coupon', 'plans', 'groups', 'growl', 'machinesPricesPromise', 'Price', 'dialogs', 'trainingsPricingsPromise', 'trainingsPromise', 'machineCreditsPromise', 'machinesPromise', 'trainingCreditsPromise', 'couponsPromise', 'spacesPromise', 'spacesPricesPromise', 'spacesCreditsPromise', 'slotDurationPromise', '_t', 'Member', 'uiTourService',
|
|
|
|
function ($scope, $state, $uibModal, $filter, TrainingsPricing, Credit, Pricing, Plan, Coupon, plans, groups, growl, machinesPricesPromise, Price, dialogs, trainingsPricingsPromise, trainingsPromise, machineCreditsPromise, machinesPromise, trainingCreditsPromise, couponsPromise, spacesPromise, spacesPricesPromise, spacesCreditsPromise, slotDurationPromise, _t, Member, uiTourService) {
|
2020-02-24 15:02:45 +01:00
|
|
|
/* PUBLIC SCOPE */
|
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// List of machines prices (not considering any plan)
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.machinesPrices = machinesPricesPromise;
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// List of trainings pricing
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.trainingsPricings = trainingsPricingsPromise;
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// List of available subscriptions plans (eg. student/month, PME/year ...)
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.plans = plans;
|
|
|
|
$scope.enabledPlans = plans.filter(function (p) { return !p.disabled; });
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// List of groups (eg. normal, student ...)
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.groups = groups.filter(function (g) { return g.slug !== 'admins'; });
|
|
|
|
$scope.enabledGroups = groups.filter(function (g) { return (g.slug !== 'admins') && !g.disabled; });
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Associate free machine hours with subscriptions
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.machineCredits = machineCreditsPromise;
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Array of associations (plan <-> training)
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.trainingCredits = trainingCreditsPromise;
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Associate a plan with all its trainings ids
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.trainingCreditsGroups = {};
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// List of trainings
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.trainings = trainingsPromise.filter(function (t) { return !t.disabled; });
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// List of machines
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.machines = machinesPromise;
|
|
|
|
$scope.enabledMachines = machinesPromise.filter(function (m) { return !m.disabled; });
|
2016-08-04 14:26:07 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// List of coupons
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.coupons = couponsPromise;
|
2019-04-04 18:00:19 +02:00
|
|
|
$scope.couponsPage = 1;
|
2017-02-14 15:25:28 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// List of spaces
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.spaces = spacesPromise;
|
|
|
|
$scope.enabledSpaces = spacesPromise.filter(function (s) { return !s.disabled; });
|
2017-02-14 17:29:52 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Associate free space hours with subscriptions
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.spaceCredits = spacesCreditsPromise;
|
2017-02-14 15:25:28 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// List of spaces prices (not considering any plan)
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.spacesPrices = spacesPricesPromise;
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// The plans list ordering. Default: by group
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.orderPlans = 'group_id';
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Status of the drop-down menu in Credits tab
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.status =
|
2018-11-21 11:08:53 +01:00
|
|
|
{ isopen: false };
|
2017-10-05 15:32:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Default: we show only enabled plans
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.planFiltering = 'enabled';
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2020-05-25 15:16:40 +02:00
|
|
|
// Default duration for the slots
|
|
|
|
$scope.slotDuration = parseInt(slotDurationPromise.setting.value, 10);
|
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Available options for filtering plans by status
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.filterDisabled = [
|
|
|
|
'enabled',
|
|
|
|
'disabled',
|
|
|
|
'all'
|
2018-11-21 11:08:53 +01:00
|
|
|
];
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2019-04-04 18:00:19 +02:00
|
|
|
// Default: we do not filter coupons
|
|
|
|
$scope.filter = {
|
2020-02-24 15:02:45 +01:00
|
|
|
coupon: 'all'
|
2019-04-04 18:00:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Available status for filtering coupons
|
|
|
|
$scope.couponStatus = [
|
|
|
|
'all',
|
|
|
|
'disabled',
|
|
|
|
'expired',
|
|
|
|
'sold_out',
|
|
|
|
'active'
|
|
|
|
];
|
|
|
|
|
2020-02-24 15:02:45 +01:00
|
|
|
// default tab: plans list
|
|
|
|
$scope.tabs = { active: 0 };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a training price from all the trainings prices
|
|
|
|
* @param trainingsPricings {Array<Object>} all trainings prices
|
|
|
|
* @param trainingId {number}
|
|
|
|
* @param groupId {number}
|
|
|
|
* @returns {float}
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.findTrainingsPricing = function (trainingsPricings, trainingId, groupId) {
|
|
|
|
for (let trainingsPricing of Array.from(trainingsPricings)) {
|
|
|
|
if ((trainingsPricing.training_id === trainingId) && (trainingsPricing.group_id === groupId)) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return trainingsPricing;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2020-02-24 15:02:45 +01:00
|
|
|
/**
|
|
|
|
* Update the price of a training for the given parameters
|
|
|
|
* @param data {float} the new price
|
|
|
|
* @param trainingsPricing {Object} the training pricing to update
|
|
|
|
* @returns {Promise|string}
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.updateTrainingsPricing = function (data, trainingsPricing) {
|
|
|
|
if (data != null) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return TrainingsPricing.update({ id: trainingsPricing.id }, { trainings_pricing: { amount: data } }).$promise;
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2019-12-17 12:40:36 +01:00
|
|
|
return _t('app.admin.pricing.please_specify_a_number');
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Retrieve a plan from its given identifier and returns it
|
|
|
|
* @param id {number} plan ID
|
|
|
|
* @returns {Object} Plan, inherits from $resource
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.getPlanFromId = function (id) {
|
|
|
|
for (let plan of Array.from($scope.plans)) {
|
|
|
|
if (plan.id === parseInt(id)) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return plan;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Retrieve a group from its given identifier and returns it
|
|
|
|
* @param id {number} group ID
|
|
|
|
* @returns {Object} Group, inherits from $resource
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.getGroupFromId = function (groups, id) {
|
|
|
|
for (let group of Array.from(groups)) {
|
|
|
|
if (group.id === parseInt(id)) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return group;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Returns a human readable string of named trainings, according to the provided array.
|
|
|
|
* $scope.trainings may contains the full list of training. The returned string will only contains the trainings
|
|
|
|
* whom ID are given in the provided parameter
|
|
|
|
* @param trainings {Array<number>} trainings IDs array
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.showTrainings = function (trainings) {
|
|
|
|
if (!angular.isArray(trainings) || !(trainings.length > 0)) {
|
2019-12-17 12:40:36 +01:00
|
|
|
return _t('app.admin.pricing.none');
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-21 11:08:53 +01:00
|
|
|
const selected = [];
|
2018-10-25 16:51:20 +02:00
|
|
|
angular.forEach($scope.trainings, function (t) {
|
|
|
|
if (trainings.indexOf(t.id) >= 0) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return selected.push(t.name);
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
2019-12-17 12:40:36 +01:00
|
|
|
if (selected.length) { return selected.join(' | '); } else { return _t('app.admin.pricing.none'); }
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Validation callback when editing training's credits. Save the changes.
|
|
|
|
* @param newdata {Object} training and associated plans
|
|
|
|
* @param planId {number|string} plan id
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.saveTrainingCredits = function (newdata, planId) {
|
2018-10-25 16:50:16 +02:00
|
|
|
// save the number of credits
|
2018-11-20 12:26:06 +01:00
|
|
|
Plan.update(
|
|
|
|
{ id: planId },
|
2018-10-25 16:51:20 +02:00
|
|
|
{ training_credit_nb: newdata.training_credits }
|
|
|
|
, angular.noop() // do nothing in case of success
|
2018-11-21 10:59:07 +01:00
|
|
|
, function (error) {
|
2019-12-17 12:40:36 +01:00
|
|
|
growl.error(_t('app.admin.pricing.an_error_occurred_while_saving_the_number_of_credits'));
|
2018-11-21 11:08:53 +01:00
|
|
|
console.error(error);
|
2018-11-20 12:26:06 +01:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
);
|
2018-10-25 16:51:20 +02:00
|
|
|
|
|
|
|
// save the associated trainings
|
|
|
|
return angular.forEach($scope.trainingCreditsGroups, function (original, key) {
|
|
|
|
if (parseInt(key) === parseInt(planId)) { // we've got the original data
|
|
|
|
if (original.join('_') !== newdata.training_ids.join('_')) { // if any changes
|
2018-10-25 16:50:16 +02:00
|
|
|
// iterate through the previous credits to remove
|
2018-10-25 16:51:20 +02:00
|
|
|
angular.forEach(original, function (oldTrainingId) {
|
|
|
|
if (newdata.training_ids.indexOf(oldTrainingId) === -1) {
|
2018-11-21 11:08:53 +01:00
|
|
|
const tc = findTrainingCredit(oldTrainingId, planId);
|
2018-10-25 16:51:20 +02:00
|
|
|
if (tc) {
|
|
|
|
return tc.$delete({}
|
|
|
|
, function () {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.trainingCredits.splice($scope.trainingCredits.indexOf(tc), 1);
|
|
|
|
return $scope.trainingCreditsGroups[planId].splice($scope.trainingCreditsGroups[planId].indexOf(tc.id), 1);
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-20 12:26:06 +01:00
|
|
|
, function (error) {
|
2019-12-17 12:40:36 +01:00
|
|
|
growl.error(_t('app.admin.pricing.an_error_occurred_while_deleting_credit_with_the_TRAINING', { TRAINING: tc.creditable.name }));
|
2018-11-21 11:08:53 +01:00
|
|
|
console.error(error);
|
|
|
|
});
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2019-12-17 12:40:36 +01:00
|
|
|
return growl.error(_t('app.admin.pricing.an_error_occurred_unable_to_find_the_credit_to_revoke'));
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
2018-10-25 16:51:20 +02:00
|
|
|
|
|
|
|
// iterate through the new credits to add
|
|
|
|
return angular.forEach(newdata.training_ids, function (newTrainingId) {
|
|
|
|
if (original.indexOf(newTrainingId) === -1) {
|
|
|
|
return Credit.save({
|
|
|
|
credit: {
|
|
|
|
creditable_id: newTrainingId,
|
|
|
|
creditable_type: 'Training',
|
|
|
|
plan_id: planId
|
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-10-25 16:51:20 +02:00
|
|
|
, function (newTc) { // success
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.trainingCredits.push(newTc);
|
|
|
|
return $scope.trainingCreditsGroups[newTc.plan_id].push(newTc.creditable_id);
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
|
|
|
, function (error) { // failed
|
2018-11-21 11:08:53 +01:00
|
|
|
const training = getTrainingFromId(newTrainingId);
|
2019-12-17 12:40:36 +01:00
|
|
|
growl.error(_t('app.admin.pricing.an_error_occurred_while_creating_credit_with_the_TRAINING', { TRAINING: training.name }));
|
2018-11-21 11:08:53 +01:00
|
|
|
return console.error(error);
|
|
|
|
});
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Cancel the current training credit modification
|
|
|
|
* @param rowform {Object} see http://vitalets.github.io/angular-xeditable/
|
|
|
|
*/
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.cancelTrainingCredit = function (rowform) { rowform.$cancel(); };
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Create a new empty entry in the $scope.machineCredits array
|
|
|
|
* @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.addMachineCredit = function (e) {
|
2018-11-21 11:08:53 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.inserted =
|
2018-11-21 11:08:53 +01:00
|
|
|
{ creditable_type: 'Machine' };
|
|
|
|
$scope.machineCredits.push($scope.inserted);
|
|
|
|
return $scope.status.isopen = !$scope.status.isopen;
|
|
|
|
};
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* In the Credits tab, return the name of the machine/space associated with the given credit
|
|
|
|
* @param credit {Object} credit object, inherited from $resource
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.showCreditableName = function (credit) {
|
2019-12-17 12:40:36 +01:00
|
|
|
let selected = _t('app.admin.pricing.not_set');
|
2018-10-25 16:51:20 +02:00
|
|
|
if (credit && credit.creditable_id) {
|
2018-11-21 11:08:53 +01:00
|
|
|
const object = $scope.getCreditable(credit);
|
|
|
|
selected = object.name;
|
2018-10-25 16:51:20 +02:00
|
|
|
if (credit.creditable_type === 'Machine') {
|
2018-11-21 11:08:53 +01:00
|
|
|
selected += ` ( id. ${object.id} )`;
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
return selected;
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* In the Credits tab, return the machine/space associated with the given credit
|
|
|
|
* @param credit {Object} credit object, inherited from $resource
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.getCreditable = function (credit) {
|
2018-11-21 11:08:53 +01:00
|
|
|
let selected;
|
2018-10-25 16:51:20 +02:00
|
|
|
if (credit && credit.creditable_id) {
|
|
|
|
if (credit.creditable_type === 'Machine') {
|
|
|
|
angular.forEach($scope.machines, function (m) {
|
|
|
|
if (m.id === credit.creditable_id) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return selected = m;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
2018-10-25 16:51:20 +02:00
|
|
|
} else if (credit.creditable_type === 'Space') {
|
|
|
|
angular.forEach($scope.spaces, function (s) {
|
|
|
|
if (s.id === credit.creditable_id) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return selected = s;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
return selected;
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Validation callback when editing machine's credits. Save the changes.
|
|
|
|
* This will prevent the creation of two credits associating the same machine and plan.
|
|
|
|
* @param data {Object} machine, associated plan and number of credit hours.
|
|
|
|
* @param [id] {number} credit id for edition, create a new credit object if not provided
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.saveMachineCredit = function (data, id) {
|
|
|
|
for (let mc of Array.from($scope.machineCredits)) {
|
|
|
|
if ((mc.plan_id === data.plan_id) && (mc.creditable_id === data.creditable_id) && ((id === null) || (mc.id !== id))) {
|
2019-12-17 12:40:36 +01:00
|
|
|
growl.error(_t('app.admin.pricing.error_a_credit_linking_this_machine_with_that_subscription_already_exists'));
|
2018-10-25 16:51:20 +02:00
|
|
|
if (!id) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.machineCredits.pop();
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
return false;
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
if (id != null) {
|
2019-12-17 12:40:36 +01:00
|
|
|
return Credit.update({ id }, { credit: data }, function () { growl.success(_t('app.admin.pricing.changes_have_been_successfully_saved')); });
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2018-11-21 11:08:53 +01:00
|
|
|
data.creditable_type = 'Machine';
|
2018-10-25 16:51:20 +02:00
|
|
|
return Credit.save(
|
|
|
|
{ credit: data }
|
|
|
|
, function (resp) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.machineCredits[$scope.machineCredits.length - 1].id = resp.id;
|
2019-12-17 12:40:36 +01:00
|
|
|
return growl.success(_t('app.admin.pricing.credit_was_successfully_saved'));
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
|
|
|
, function (err) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.machineCredits.pop();
|
2019-12-17 12:40:36 +01:00
|
|
|
growl.error(_t('app.admin.pricing.error_creating_credit'));
|
2018-11-21 11:08:53 +01:00
|
|
|
console.error(err);
|
|
|
|
});
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Removes the newly inserted but not saved machine credit / Cancel the current machine credit modification
|
|
|
|
* @param rowform {Object} see http://vitalets.github.io/angular-xeditable/
|
|
|
|
* @param index {number} credit index in the $scope.machineCredits array
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.cancelMachineCredit = function (rowform, index) {
|
|
|
|
if ($scope.machineCredits[index].id != null) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return rowform.$cancel();
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.machineCredits.splice(index, 1);
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Deletes the machine credit at the specified index
|
|
|
|
* @param index {number} machine credit index in the $scope.machineCredits array
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.removeMachineCredit = function (index) {
|
2018-11-21 11:08:53 +01:00
|
|
|
Credit.delete($scope.machineCredits[index]);
|
|
|
|
$scope.machineCredits.splice(index, 1);
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Create a new empty entry in the $scope.spaceCredits array
|
|
|
|
* @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.addSpaceCredit = function (e) {
|
2018-11-21 11:08:53 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.inserted =
|
2018-11-21 11:08:53 +01:00
|
|
|
{ creditable_type: 'Space' };
|
|
|
|
$scope.spaceCredits.push($scope.inserted);
|
|
|
|
$scope.status.isopen = !$scope.status.isopen;
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Validation callback when editing space's credits. Save the changes.
|
|
|
|
* This will prevent the creation of two credits associated with the same space and plan.
|
|
|
|
* @param data {Object} space, associated plan and number of credit hours.
|
|
|
|
* @param [id] {number} credit id for edition, create a new credit object if not provided
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.saveSpaceCredit = function (data, id) {
|
|
|
|
for (let sc of Array.from($scope.spaceCredits)) {
|
|
|
|
if ((sc.plan_id === data.plan_id) && (sc.creditable_id === data.creditable_id) && ((id === null) || (sc.id !== id))) {
|
2019-12-17 12:40:36 +01:00
|
|
|
growl.error(_t('app.admin.pricing.error_a_credit_linking_this_space_with_that_subscription_already_exists'));
|
2018-10-25 16:51:20 +02:00
|
|
|
if (!id) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.spaceCredits.pop();
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
return false;
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
if (id != null) {
|
2019-12-17 12:40:36 +01:00
|
|
|
return Credit.update({ id }, { credit: data }, function () { growl.success(_t('app.admin.pricing.changes_have_been_successfully_saved')); });
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2018-11-21 11:08:53 +01:00
|
|
|
data.creditable_type = 'Space';
|
2018-10-25 16:51:20 +02:00
|
|
|
return Credit.save(
|
|
|
|
{ credit: data }
|
|
|
|
, function (resp) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.spaceCredits[$scope.spaceCredits.length - 1].id = resp.id;
|
2019-12-17 12:40:36 +01:00
|
|
|
return growl.success(_t('app.admin.pricing.credit_was_successfully_saved'));
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-10-25 16:51:20 +02:00
|
|
|
, function (err) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.spaceCredits.pop();
|
2019-12-17 12:40:36 +01:00
|
|
|
return growl.error(_t('app.admin.pricing.error_creating_credit'));
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Removes the newly inserted but not saved space credit / Cancel the current space credit modification
|
|
|
|
* @param rowform {Object} see http://vitalets.github.io/angular-xeditable/
|
|
|
|
* @param index {number} credit index in the $scope.spaceCredits array
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.cancelSpaceCredit = function (rowform, index) {
|
|
|
|
if ($scope.spaceCredits[index].id != null) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return rowform.$cancel();
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.spaceCredits.splice(index, 1);
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Deletes the space credit at the specified index
|
|
|
|
* @param index {number} space credit index in the $scope.spaceCredits array
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.removeSpaceCredit = function (index) {
|
2018-11-21 11:08:53 +01:00
|
|
|
Credit.delete($scope.spaceCredits[index]);
|
|
|
|
return $scope.spaceCredits.splice(index, 1);
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* If the plan does not have a type, return a default value for display purposes
|
|
|
|
* @param type {string|undefined|null} plan's type (eg. 'partner')
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.getPlanType = function (type) {
|
|
|
|
if (type === 'PartnerPlan') {
|
2019-12-17 12:40:36 +01:00
|
|
|
return _t('app.admin.pricing.partner');
|
|
|
|
} else { return _t('app.admin.pricing.standard'); }
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Change the plans ordering criterion to the one provided
|
|
|
|
* @param orderBy {string} ordering criterion
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.setOrderPlans = function (orderBy) {
|
|
|
|
if ($scope.orderPlans === orderBy) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.orderPlans = `-${orderBy}`;
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.orderPlans = orderBy;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Retrieve a price from prices array by a machineId and a groupId
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.findPriceBy = function (prices, machineId, groupId) {
|
|
|
|
for (let price of Array.from(prices)) {
|
|
|
|
if ((price.priceable_id === machineId) && (price.group_id === groupId)) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return price;
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* update a price for a machine and a group, not considering any plan
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.updatePrice = function (data, price) {
|
|
|
|
if (data != null) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return Price.update({ id: price.id }, { price: { amount: data } }).$promise;
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2019-12-17 12:40:36 +01:00
|
|
|
return _t('app.admin.pricing.please_specify_a_number');
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Delete the specified subcription plan
|
|
|
|
* @param id {number} plan id
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.deletePlan = function (plans, id) {
|
|
|
|
if (typeof id !== 'number') {
|
2018-11-21 11:08:53 +01:00
|
|
|
return console.error('[EditPricingController::deletePlan] Error: invalid id parameter');
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
|
|
|
// open a confirmation dialog
|
2018-11-20 12:26:06 +01:00
|
|
|
return dialogs.confirm(
|
|
|
|
{
|
|
|
|
resolve: {
|
|
|
|
object () {
|
|
|
|
return {
|
2019-12-17 12:40:36 +01:00
|
|
|
title: _t('app.admin.pricing.confirmation_required'),
|
|
|
|
msg: _t('app.admin.pricing.do_you_really_want_to_delete_this_subscription_plan')
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-20 12:26:06 +01:00
|
|
|
},
|
|
|
|
function () {
|
|
|
|
// the admin has confirmed, delete the plan
|
|
|
|
Plan.delete(
|
|
|
|
{ id },
|
|
|
|
function (res) {
|
2019-12-17 12:40:36 +01:00
|
|
|
growl.success(_t('app.admin.pricing.subscription_plan_was_successfully_deleted'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.plans.splice(findItemIdxById(plans, id), 1);
|
2018-11-20 12:26:06 +01:00
|
|
|
},
|
|
|
|
function (error) {
|
2018-11-21 11:08:53 +01:00
|
|
|
if (error.statusText) { console.error(`[EditPricingController::deletePlan] Error: ${error.statusText}`); }
|
2019-12-17 12:40:36 +01:00
|
|
|
growl.error(_t('app.admin.pricing.unable_to_delete_the_specified_subscription_an_error_occurred'));
|
2018-11-20 12:26:06 +01:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
);
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
);
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Generate a string identifying the given plan by literal humain-readable name
|
|
|
|
* @param plan {Object} Plan object, as recovered from GET /api/plan/:id
|
|
|
|
* @param groups {Array} List of Groups objects, as recovered from GET /api/groups
|
|
|
|
* @param short {boolean} If true, the generated name will contains the group slug, otherwise the group full name
|
|
|
|
* will be included.
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.humanReadablePlanName = function (plan, groups, short) { return `${$filter('humanReadablePlanName')(plan, groups, short)}`; };
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Delete a coupon from the server's database and, in case of success, from the list in memory
|
|
|
|
* @param coupons {Array<Object>} should be called with $scope.coupons
|
|
|
|
* @param id {number} ID of the coupon to delete
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.deleteCoupon = function (coupons, id) {
|
|
|
|
if (typeof id !== 'number') {
|
2018-11-21 11:08:53 +01:00
|
|
|
return console.error('[EditPricingController::deleteCoupon] Error: invalid id parameter');
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
|
|
|
// open a confirmation dialog
|
|
|
|
return dialogs.confirm({
|
|
|
|
resolve: {
|
|
|
|
object () {
|
|
|
|
return {
|
2019-12-17 12:40:36 +01:00
|
|
|
title: _t('app.admin.pricing.confirmation_required'),
|
|
|
|
msg: _t('app.admin.pricing.do_you_really_want_to_delete_this_coupon')
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-21 10:59:07 +01:00
|
|
|
, function () {
|
2018-10-25 16:51:20 +02:00
|
|
|
// the admin has confirmed, delete the coupon
|
|
|
|
Coupon.delete({ id }, function (res) {
|
2019-12-18 13:04:38 +01:00
|
|
|
growl.success(_t('app.admin.pricing.coupon_was_successfully_deleted'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.coupons.splice(findItemIdxById(coupons, id), 1);
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
, function (error) {
|
2018-11-21 11:08:53 +01:00
|
|
|
if (error.statusText) { console.error(`[EditPricingController::deleteCoupon] Error: ${error.statusText}`); }
|
2018-10-25 16:51:20 +02:00
|
|
|
if (error.status === 422) {
|
2019-12-17 12:40:36 +01:00
|
|
|
return growl.error(_t('app.admin.pricing.unable_to_delete_the_specified_coupon_already_in_use'));
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2019-12-17 12:40:36 +01:00
|
|
|
return growl.error(_t('app.admin.pricing.unable_to_delete_the_specified_coupon_an_unexpected_error_occurred'));
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
|
|
|
});
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Open a modal allowing to select an user and send him the details of the provided coupon
|
|
|
|
* @param coupon {Object} The coupon to send
|
|
|
|
*/
|
2018-11-21 10:59:07 +01:00
|
|
|
$scope.sendCouponToUser = function (coupon) {
|
2018-10-25 16:51:20 +02:00
|
|
|
$uibModal.open({
|
|
|
|
templateUrl: '<%= asset_path "admin/pricing/sendCoupon.html" %>',
|
|
|
|
resolve: {
|
2018-11-21 11:08:53 +01:00
|
|
|
coupon () { return coupon; }
|
2018-10-25 16:51:20 +02:00
|
|
|
},
|
|
|
|
size: 'md',
|
|
|
|
controller: ['$scope', '$uibModalInstance', 'Coupon', 'coupon', '_t', function ($scope, $uibModalInstance, Coupon, coupon, _t) {
|
2018-11-19 16:17:49 +01:00
|
|
|
// Member, receiver of the coupon
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.ctrl =
|
2018-11-21 11:08:53 +01:00
|
|
|
{ member: null };
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Details of the coupon to send
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.coupon = coupon;
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Callback to validate sending of the coupon
|
2018-11-21 10:59:07 +01:00
|
|
|
$scope.ok = function () {
|
2018-10-25 16:51:20 +02:00
|
|
|
Coupon.send({ coupon_code: coupon.code, user_id: $scope.ctrl.member.id }, function (res) {
|
2019-12-17 12:40:36 +01:00
|
|
|
growl.success(_t('app.admin.pricing.coupon_successfully_sent_to_USER', { USER: $scope.ctrl.member.name }));
|
2018-11-21 11:08:53 +01:00
|
|
|
return $uibModalInstance.close({ user_id: $scope.ctrl.member.id });
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 10:59:07 +01:00
|
|
|
, function (err) {
|
2019-12-17 12:40:36 +01:00
|
|
|
growl.error(_t('app.admin.pricing.an_error_occurred_unable_to_send_the_coupon'));
|
2018-11-21 11:08:53 +01:00
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
};
|
2018-11-19 16:17:49 +01:00
|
|
|
// Callback to close the modal and cancel the sending process
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.cancel = function () { $uibModalInstance.dismiss('cancel'); };
|
2018-11-20 12:26:06 +01:00
|
|
|
}]
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
|
|
|
};
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2019-04-04 18:00:19 +02:00
|
|
|
/**
|
|
|
|
* Load the next 10 coupons
|
|
|
|
*/
|
2019-04-04 17:28:29 +02:00
|
|
|
$scope.loadMore = function() {
|
|
|
|
$scope.couponsPage++;
|
2019-04-04 18:00:19 +02:00
|
|
|
Coupon.query({ page: $scope.couponsPage, filter: $scope.filter.coupon }, function (data) {
|
2019-04-04 17:28:29 +02:00
|
|
|
$scope.coupons = $scope.coupons.concat(data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-04-04 18:00:19 +02:00
|
|
|
/**
|
|
|
|
* Reset the list of coupons according to the newly selected filter
|
|
|
|
*/
|
|
|
|
$scope.updateCouponFilter = function() {
|
|
|
|
$scope.couponsPage = 1;
|
|
|
|
Coupon.query({ page: $scope.couponsPage, filter: $scope.filter.coupon }, function (data) {
|
|
|
|
$scope.coupons = data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-06 16:55:26 +02:00
|
|
|
/**
|
|
|
|
* Return the exemple price based on the configuration of the default slot duration.
|
|
|
|
* @param type {string} 'hourly_rate' | *
|
2020-05-25 15:16:40 +02:00
|
|
|
* @returns {number} price for "SLOT_DURATION" minutes.
|
2020-05-06 16:55:26 +02:00
|
|
|
*/
|
|
|
|
$scope.examplePrice = function(type) {
|
|
|
|
const hourlyRate = 10;
|
|
|
|
|
|
|
|
if (type === 'hourly_rate') {
|
|
|
|
return $filter('currency')(hourlyRate);
|
|
|
|
}
|
|
|
|
|
2020-05-25 15:16:40 +02:00
|
|
|
const price = (hourlyRate / 60) * $scope.slotDuration;
|
2020-05-06 16:55:26 +02:00
|
|
|
return $filter('currency')(price);
|
|
|
|
}
|
|
|
|
|
2020-02-24 15:02:45 +01:00
|
|
|
/**
|
|
|
|
* Setup the feature-tour for the admin/pricing page.
|
|
|
|
* This is intended as a contextual help (when pressing F1)
|
|
|
|
*/
|
|
|
|
$scope.setupPricingTour = function () {
|
|
|
|
// get the tour defined by the ui-tour directive
|
|
|
|
const uitour = uiTourService.getTourByName('pricing');
|
|
|
|
uitour.createStep({
|
|
|
|
selector: 'body',
|
|
|
|
stepId: 'welcome',
|
|
|
|
order: 0,
|
|
|
|
title: _t('app.admin.tour.pricing.welcome.title'),
|
|
|
|
content: _t('app.admin.tour.pricing.welcome.content'),
|
|
|
|
placement: 'bottom',
|
|
|
|
orphan: true
|
|
|
|
});
|
|
|
|
uitour.createStep({
|
|
|
|
selector: '.plans-pricing .new-plan-button',
|
|
|
|
stepId: 'new_plan',
|
|
|
|
order: 1,
|
|
|
|
title: _t('app.admin.tour.pricing.new_plan.title'),
|
|
|
|
content: _t('app.admin.tour.pricing.new_plan.content'),
|
|
|
|
placement: 'bottom'
|
|
|
|
});
|
|
|
|
uitour.createStep({
|
|
|
|
selector: '.plans-pricing .trainings-tab',
|
|
|
|
stepId: 'trainings',
|
|
|
|
order: 2,
|
|
|
|
title: _t('app.admin.tour.pricing.trainings.title'),
|
|
|
|
content: _t('app.admin.tour.pricing.trainings.content'),
|
|
|
|
placement: 'bottom'
|
|
|
|
});
|
|
|
|
uitour.createStep({
|
|
|
|
selector: '.plans-pricing .machines-tab',
|
|
|
|
stepId: 'machines',
|
|
|
|
order: 3,
|
|
|
|
title: _t('app.admin.tour.pricing.machines.title'),
|
|
|
|
content: _t('app.admin.tour.pricing.machines.content'),
|
|
|
|
placement: 'bottom'
|
|
|
|
});
|
2020-02-24 15:04:17 +01:00
|
|
|
if (!Fablab.withoutSpaces) {
|
|
|
|
uitour.createStep({
|
|
|
|
selector: '.plans-pricing .spaces-tab',
|
|
|
|
stepId: 'spaces',
|
|
|
|
order: 4,
|
|
|
|
title: _t('app.admin.tour.pricing.spaces.title'),
|
|
|
|
content: _t('app.admin.tour.pricing.spaces.content'),
|
|
|
|
placement: 'bottom'
|
|
|
|
});
|
|
|
|
}
|
2020-02-24 15:02:45 +01:00
|
|
|
uitour.createStep({
|
|
|
|
selector: '.plans-pricing .credits-tab',
|
|
|
|
stepId: 'credits',
|
|
|
|
order: 5,
|
|
|
|
title: _t('app.admin.tour.pricing.credits.title'),
|
|
|
|
content: _t('app.admin.tour.pricing.credits.content'),
|
|
|
|
placement: 'bottom'
|
|
|
|
});
|
|
|
|
uitour.createStep({
|
|
|
|
selector: '.plans-pricing .coupons-tab',
|
|
|
|
stepId: 'coupons',
|
|
|
|
order: 6,
|
|
|
|
title: _t('app.admin.tour.pricing.coupons.title'),
|
|
|
|
content: _t('app.admin.tour.pricing.coupons.content'),
|
2020-03-03 11:20:17 +01:00
|
|
|
placement: 'bottom',
|
|
|
|
popupClass: 'shift-left-50'
|
2020-02-24 15:02:45 +01:00
|
|
|
});
|
|
|
|
uitour.createStep({
|
|
|
|
selector: 'body',
|
|
|
|
stepId: 'conclusion',
|
|
|
|
order: 7,
|
|
|
|
title: _t('app.admin.tour.conclusion.title'),
|
|
|
|
content: _t('app.admin.tour.conclusion.content'),
|
|
|
|
placement: 'bottom',
|
|
|
|
orphan: true
|
|
|
|
});
|
|
|
|
// on step change, change the active tab if needed
|
|
|
|
uitour.on('stepChanged', function (nextStep) {
|
|
|
|
if (nextStep.stepId === 'new_plan') { $scope.tabs.active = 0; }
|
|
|
|
if (nextStep.stepId === 'trainings') { $scope.tabs.active = 1; }
|
|
|
|
if (nextStep.stepId === 'machines') { $scope.tabs.active = 2; }
|
|
|
|
if (nextStep.stepId === 'spaces') { $scope.tabs.active = 3; }
|
|
|
|
if (nextStep.stepId === 'credits') { $scope.tabs.active = 4; }
|
|
|
|
if (nextStep.stepId === 'coupons') { $scope.tabs.active = 5; }
|
|
|
|
});
|
|
|
|
// on tour end, save the status in database
|
|
|
|
uitour.on('ended', function () {
|
|
|
|
if (uitour.getStatus() === uitour.Status.ON && $scope.currentUser.profile.tours.indexOf('pricing') < 0) {
|
|
|
|
Member.completeTour({ id: $scope.currentUser.id }, { tour: 'pricing' }, function (res) {
|
|
|
|
$scope.currentUser.profile.tours = res.tours;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// if the user has never seen the tour, show him now
|
2020-03-02 11:17:28 +01:00
|
|
|
if (Fablab.featureTourDisplay !== 'manual' && $scope.currentUser.profile.tours.indexOf('pricing') < 0) {
|
2020-02-24 15:02:45 +01:00
|
|
|
uitour.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
/* PRIVATE SCOPE */
|
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Kind of constructor: these actions will be realized first when the controller is loaded
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
const initialize = function () {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.trainingCreditsGroups = groupCreditsByPlan($scope.trainingCredits);
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// adds empty array for plan which hasn't any credits yet
|
2018-11-21 10:59:07 +01:00
|
|
|
return (function () {
|
2018-11-21 11:08:53 +01:00
|
|
|
const result = [];
|
2018-10-25 16:51:20 +02:00
|
|
|
for (let plan of Array.from($scope.plans)) {
|
|
|
|
if ($scope.trainingCreditsGroups[plan.id] == null) {
|
2018-11-21 11:08:53 +01:00
|
|
|
result.push($scope.trainingCreditsGroups[plan.id] = []);
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2018-11-21 11:08:53 +01:00
|
|
|
result.push(undefined);
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
return result;
|
|
|
|
})();
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Retrieve an item index by its ID from the given array of objects
|
|
|
|
* @param items {Array<{id:number}>}
|
|
|
|
* @param id {number}
|
|
|
|
* @returns {number} item index in the provided array
|
|
|
|
*/
|
2018-11-20 12:26:06 +01:00
|
|
|
var findItemIdxById = function (items, id) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return (items.map(function (item) { return item.id; })).indexOf(id);
|
|
|
|
};
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Group the given credits array into a map associating the plan ID with its associated trainings/machines
|
|
|
|
* @return {Object} the association map
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
var groupCreditsByPlan = function (credits) {
|
2018-11-21 11:08:53 +01:00
|
|
|
const creditsMap = {};
|
2018-10-25 16:51:20 +02:00
|
|
|
angular.forEach(credits, function (c) {
|
|
|
|
if (!creditsMap[c.plan_id]) {
|
2018-11-21 11:08:53 +01:00
|
|
|
creditsMap[c.plan_id] = [];
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
return creditsMap[c.plan_id].push(c.creditable_id);
|
|
|
|
});
|
|
|
|
return creditsMap;
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Iterate through $scope.traininfCredits to find the credit matching the given criterion
|
|
|
|
* @param trainingId {number|string} training ID
|
|
|
|
* @param planId {number|string} plan ID
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
var findTrainingCredit = function (trainingId, planId) {
|
2018-11-21 11:08:53 +01:00
|
|
|
trainingId = parseInt(trainingId);
|
|
|
|
planId = parseInt(planId);
|
2018-10-25 16:51:20 +02:00
|
|
|
|
|
|
|
for (let credit of Array.from($scope.trainingCredits)) {
|
|
|
|
if ((credit.plan_id === planId) && (credit.creditable_id === trainingId)) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return credit;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Retrieve a training from its given identifier and returns it
|
|
|
|
* @param id {number} training ID
|
|
|
|
* @returns {Object} Training inherited from $resource
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
var getTrainingFromId = function (id) {
|
|
|
|
for (let training of Array.from($scope.trainings)) {
|
|
|
|
if (training.id === parseInt(id)) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return training;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// !!! MUST BE CALLED AT THE END of the controller
|
2018-11-21 11:08:53 +01:00
|
|
|
return initialize();
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
]);
|