File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/AcademyParameterController.php
<?php
namespace App\Http\Controllers;
use App\AcademyParameter;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
class AcademyParameterController extends Controller
{
private $activeServices;
private $periods;
private $_MESSAGE;
private $_SCHEDULE;
private $ENROLLMENT_FIELDS = ['ciiu', 'athlete_ocupation', 'advisor_property_name'];
private $FULL_ENROLLMENT_FIELDS = ['eps', 'codigo_del_estudiante'];
private $paymentSchedules;
public function __construct()
{
$this->activeServices = array(
array(
'id' => 'none',
'name' => 'Ninguno'
),
array(
'id' => 'push',
'name' => 'Push'
),
array(
'id' => 'sms',
'name' => 'SMS'
),
array(
'id' => 'all',
'name' => 'Todos'
),
);
$this->periods = array(
array(
'id' => '1',
'name' => 'Cada día'
),
array(
'id' => '2',
'name' => 'Cada dos días'
),
array(
'id' => '3',
'name' => 'Cada tres días'
),
array(
'id' => '7',
'name' => 'Cada semana'
),
array(
'id' => '15',
'name' => 'Cada quince días'
),
array(
'id' => '30',
'name' => 'Cada mes'
),
array(
'id' => '365',
'name' => 'Cada año'
),
);
$this->paymentSchedules = array(
array(
'id' => '1',
'name' => 'Pagos anticipados'
),
array(
'id' => '2',
'name' => 'Pagos según fecha'
),
);
}
public function academyParameters($type_academy = 'children')
{
$parameters = AcademyParameter::where('type_academy', $type_academy)->get()->toArray();
$parametesrsAsArray = array_column($parameters, null, 'key');
$selectedEnrollmentFields = [];
if (array_key_exists('enrollment_fields', $parametesrsAsArray)) {
$selectedEnrollmentFields = explode(',', $parametesrsAsArray['enrollment_fields']['value']);
}
$selectedFullEnrollmentFields = [];
if (array_key_exists('full_enrollment_fields', $parametesrsAsArray)) {
$selectedFullEnrollmentFields = explode(',', $parametesrsAsArray['full_enrollment_fields']['value']);
}
return view('academy.parameters')
->with('parameters', $parameters)
->with('activeServices', $this->activeServices)
->with('type_academy', $type_academy)
->with('periods', $this->periods)
->with('enrollment_fields', $this->ENROLLMENT_FIELDS)
->with('selectedEnrollmentFields', $selectedEnrollmentFields)
->with('full_enrollment_fields', $this->FULL_ENROLLMENT_FIELDS)
->with('selectedFullEnrollmentFields', $selectedFullEnrollmentFields)
->with('paymentSchedules', $this->paymentSchedules);
}
public function saveParameters(Request $request)
{
$this->createOrUpdateParam($request, 'payment_frequency');
$this->createOrUpdateParam($request, 'school_grades');
$this->createOrUpdateParam($request, 'blood_type');
$this->createOrUpdateParam($request, 'ocupation');
$this->createOrUpdateParam($request, 'relationship');
$this->createOrUpdateParam($request, 'enrollment_title');
$this->createOrUpdateParam($request, 'enrollment_button_text');
$this->createOrUpdateParam($request, 'enrollment_alert_text');
$this->createOrUpdateParam($request, 'department_text');
$this->createOrUpdateParam($request, 'city_text');
$this->createOrUpdateParam($request, 'app_components');
$this->createOrUpdateParam($request, 'academy_places');
$this->createOrUpdateParam($request, 'tshirt_sizes');
//$this->createOrUpdateParam($request, 'shorts_sizes');
$this->createOrUpdateParam($request, 'uniform_types');
$this->createOrUpdateParam($request, 'full_enrollment_title');
$this->createOrUpdateParam($request, 'full_enrollment_button_text');
$this->createOrUpdateParam($request, 'full_enrollment_alert_text');
$this->createOrUpdateParam($request, 'documents_upload_title');
$this->createOrUpdateParam($request, 'documents_upload_button_text');
$this->createOrUpdateParam($request, 'documents_upload_alert_text');
$this->createOrUpdateParam($request, 'documents_sign_title');
$this->createOrUpdateParam($request, 'documents_sign_button_text');
$this->createOrUpdateParam($request, 'documents_sign_alert_text');
$this->createOrUpdateParam($request, 'help_information_responsable');
$this->createOrUpdateParam($request, 'help_information_contact');
$this->createOrUpdateParam($request, 'last_inscription_year');
$this->createOrUpdateParam($request, 'enable_collection_advance_payments');
$this->createOrUpdateParam($request, 'days_collection_advance_payments');
$this->createOrUpdateParam($request, 'enable_modify_payment_button');
$this->createOrUpdateParam($request, 'enable_attach_support_button');
$this->createOrUpdateParam($request, 'change_status_message');
$this->createOrUpdateParam($request, 'change_status_active_service');
$this->createOrUpdateParam($request, 'confirm_revision_message');
$this->createOrUpdateParam($request, 'confirm_revision_active_service');
$this->createOrUpdateParam($request, 'notify_payment_enable_message');
$this->createOrUpdateParam($request, 'notify_payment_enable_active_service');
$this->createOrUpdateParam($request, 'notify_payment_enable_periodicity');
$this->createOrUpdateParam($request, 'payment_activation_notification_schedule');
$this->createOrUpdateParam($request, 'notify_payment_due_message');
$this->createOrUpdateParam($request, 'notify_payment_due_active_service');
$this->createOrUpdateParam($request, 'notify_payment_due_periodicity');
$this->createOrUpdateParam($request, 'notify_payment_soon_due_days');
$this->createOrUpdateParam($request, 'notify_payment_soon_due_message');
$this->createOrUpdateParam($request, 'notify_payment_soon_due_active_service');
$this->createOrUpdateParam($request, 'notify_payment_soon_due_periodicity');
$this->createOrUpdateParam($request, 'notify_payment_pending_enrollment_message');
$this->createOrUpdateParam($request, 'notify_payment_pending_enrollment_active_service');
$this->createOrUpdateParam($request, 'notify_payment_pending_enrollment_due_periodicity');
$this->createOrUpdateParam($request, 'students_without_payments_message');
$this->createOrUpdateParam($request, 'students_without_payments_active_service');
$this->createOrUpdateParam($request, 'students_without_payments_due_periodicity');
// notificaciones de tipo matricular
$this->createOrUpdateParam($request, 'notify_rigistration_renewal_message');
$this->createOrUpdateParam($request, 'notify_rigistration_renewal_active_service');
$this->createOrUpdateParam($request, 'notify_rigistration_renewal_periodicity');
$this->createOrUpdateParam($request, 'enable_registration_renewal');
$this->createOrUpdateParam($request, 'enable_send_email_registration_renewal');
$this->createOrUpdateParam($request, 'registration_start_date');
$this->createOrUpdateParam($request, 'registration_end_date');
$this->createOrUpdateParam($request, 'maximum_payment_day_month');
$this->createOrUpdateParam($request, 'enable_first_month_renewal_payment_for_children');
$this->createOrUpdateParam($request, 'payment_schedule');
$this->createOrUpdateParam($request, 'payment_activation_date_in_app');
// Certificado medico app
$this->createOrUpdateParam($request, 'medical_certificate_app');
// Texto de terminos y condiciones
$this->createOrUpdateParam($request, 'terms_and_conditions_text');
$this->createOrUpdateParam($request, 'notify_birthday_message');
$this->createOrUpdateParam($request, 'notify_enrollment_confirmation');
// Habilitar franquicias
$this->createOrUpdateParam($request, 'enable_franchises');
$this->createOrUpdateParam($request, 'enrollment_fields');
$this->createOrUpdateParam($request, 'full_enrollment_fields');
$this->createOrUpdateParam($request, 'nit_text');
$this->createOrUpdateParam($request, 'show_payment_term');
$this->createOrUpdateParam($request, 'show_courtesy_button');
$this->createOrUpdateParam($request, 'show_debt_button');
$this->createOrUpdateParam($request, 'notify_unconfirmed_payments');
$this->registerLog(Auth::user()->id, 'Guardar parametros academia', json_encode($request->all()), "Update", $this->getModule($request));
if ($request->file('terms')) {
$this->saveFileParameter($request, 'terms', 'academy_terms_'. $request['type_academy']);
}
if ($request->file('playbook')) {
$this->saveFileParameter($request, 'playbook', 'playbook_'. $request['type_academy']);
}
return array('r' => true, 'm' => trans('messages.academy_parameters_labels.success_save_parameters'));
}
private function saveFileParameter(Request $request, $key, $filename)
{
try {
$extension = $request->file($key)->getClientOriginalExtension();
$filenametostore = $filename . '.' . $extension;
Storage::disk('s3')->put(config('s3.academy_documents') . $filenametostore, fopen($request->file($key), 'r+'), 'public');
$url = config('filesystems.disks.s3.url') . '/academy/academy_documents/' . $filenametostore;
AcademyParameter::updateOrCreate(
['key' => $key, 'type_academy' => $request['type_academy']],
['key' => $key, 'value' => $url, 'type_academy' => $request['type_academy']]
);
} catch (\Throwable $th) {
return array('r' => false, 'm' => $th->getMessage());
}
}
public function createOrUpdateParam(Request $request, $key)
{
if ($request[$key]) {
AcademyParameter::updateOrCreate(
['key' => $key, 'type_academy' => $request['type_academy']],
['key' => $key, 'value' => $request[$key], 'type_academy' => $request['type_academy']]
);
} else {
AcademyParameter::where('key', $key)
->where('type_academy', $request['type_academy'])
->delete();
}
}
public function createOrUpdateParamWithValue($value, $key)
{
if ($value) {
AcademyParameter::updateOrCreate(['key' => $key], ['key' => $key, 'value' => $value]);
}
}
public function getParametersValue($key)
{
$parameter = AcademyParameter::where('key', $key)->first();
return $parameter ? $parameter->value : '';
}
public function getParametersMessage($key, $reset = false)
{
if (!$this->_MESSAGE || $reset)
$this->_MESSAGE = AcademyParameter::where('key', $key)->first()->value;
return $this->_MESSAGE;
}
public function getParametersSchedule($key)
{
if (!$this->_SCHEDULE)
$this->_SCHEDULE = AcademyParameter::where('key', $key)->first()->value;
return $this->_SCHEDULE;
}
public function getSchedule($schedule)
{
if ($schedule) {
$date = Carbon::now()->toDateString() . " " . $schedule;
$dateTime = new \Carbon\Carbon($date);
$response = $dateTime->minute . ' ' . $dateTime->hour . ' * * *';
return $response;
}
return "0 0 * * *";
}
public function replaceWildcardsMessage($message, $term_type, $term, $student, $appName)
{
if ($message) {
$message = preg_replace('/:term_type/', $term_type, $message);
$message = preg_replace('/:term/', $term, $message);
$message = preg_replace('/:student/', $student, $message);
$message = preg_replace('/:appName/', $appName, $message);
}
return $message;
}
public function validatePeriodicity($date, $periodicity)
{
if ($date && $periodicity) {
$lastDate = Carbon::parse($date)->addDays(intval($periodicity));
$now = Carbon::now();
return $lastDate < $now;
}
return true;
}
public function validateEnableCollectionAdvancePayments($typeAcademy = 'children')
{
if ($this->getParametersValueByTypeAcademy('enable_collection_advance_payments', $typeAcademy) == 'true') {
return intval($this->getParametersValueByTypeAcademy('days_collection_advance_payments', $typeAcademy));
}
return false;
}
public function validateRegistrationStartDate($date = null, $typeAcademy = 'children')
{
if (!$date)
$date = Carbon::now();
$registrationStartDate = $this->registrationStartDate($typeAcademy);
$registrationEndDate = $this->registrationEndDate($typeAcademy);
$validRegistration = $date->between($registrationStartDate, $registrationEndDate) || $date->year == $registrationStartDate->year;
return !$validRegistration;
}
public function registrationStartDate($typeAcademy = 'children')
{
$registrationStartDate = $this->getParametersValueByTypeAcademy('registration_start_date', $typeAcademy);
return $registrationStartDate ? Carbon::parse($registrationStartDate) : Carbon::now()->startOfYear();
}
public function registrationEndDate($typeAcademy = 'children')
{
$registrationEndDate = $this->getParametersValueByTypeAcademy('registration_end_date', $typeAcademy);
return $registrationEndDate ? Carbon::parse($registrationEndDate) : Carbon::now()->endOfYear();
}
public function getParametersValueByTypeAcademy($key, $typeAcademy = 'children')
{
$parameter = AcademyParameter::where([['key', $key], ['type_academy', $typeAcademy]])->first();
return $parameter ? $parameter->value : '';
}
}