File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Services/TicketParametersService.php
<?php
namespace App\Services;
use App\Core\Ticket\QRTypesEnum;
use App\Core\Ticket\TicketOriginEnum;
use App\Http\Controllers\Controller;
use App\TicketParameter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
class TicketParametersService
{
private $controller;
public function __construct()
{
$this->controller = new Controller();
}
public function getParameterObject()
{
if (!Cache::get('parametersTickets')) {
$data = new \stdClass();
$parameters = TicketParameter::select('key', 'value')->get();
foreach ($parameters as $parameter) {
$key = $parameter->key;
if ($key != '') {
$value = $parameter->value;
$data->$key = $value;
if ($key == 'time_to_accept_complimentary_tickets') {
$data->time_to_accept_complimentary_tickets_hour = str_pad(floor($value / 60), 2, '0', STR_PAD_LEFT);
$data->time_to_accept_complimentary_tickets_minute = str_pad($value % 60, 2, '0', STR_PAD_LEFT);
}
}
}
Cache::put('parametersTickets', $data, config('cache.time'));
} else {
$data = Cache::get('parametersTickets');
}
return $data;
}
public function saveParameters(Request $request)
{
try {
// Conversión cm -> px
if ($request->has('qrcode_size')) {
$qrcodeSizeCm = $request->input('qrcode_size');
$qrcodeSizePx = $qrcodeSizeCm * 37.7952755906;
$request->merge(['qrcode_size' => round($qrcodeSizePx)]);
}
$this->createOrUpdateParam($request, 'number_transfers');
$this->createOrUpdateParam($request, 'send_mail_tickets_app_purchase');
$this->createOrUpdateParam($request, 'send_mail_tickets_web_purchase');
$this->createOrUpdateParam($request, 'enable_transfer_ticket');
$this->createOrUpdateParam($request, 'enable_download_button');
$this->createOrUpdateParam($request, 'enable_tyc');
$this->createOrUpdateParam($request, 'ticket_button');
$this->createOrUpdateParam($request, 'subscriber_button');
$this->createOrUpdateParam($request, 'enable_validate_doors');
$this->createOrUpdateParam($request, 'enable_electronic_invoice_creation');
$this->createOrUpdateParam($request, 'enable_electronic_invoice_service_charge');
$this->createOrUpdateParam($request, 'enable_electronic_courtesy_billing');
$this->createOrUpdateParam($request, 'iva_percentage_tickets');
$this->createOrUpdateParam($request, 'iva_percentage_service_charge');
$this->createOrUpdateParam($request, 'qrcode_type');
$this->createOrUpdateParam($request, 'qrcode_time');
$this->createOrUpdateParam($request, 'qrcode_size');
$this->createOrUpdateParam($request, 'time_to_accept_complimentary_tickets');
$this->createOrUpdateParam($request, 'enable_electronic_billing_by_mandate');
$this->createOrUpdateParam($request, 'electronic_billing_by_mandate_identification_client_organization');
$this->createOrUpdateParam($request, 'electronic_billing_by_mandate_identification_owner_organization');
$this->createOrUpdateParam($request, 'electronic_billing_by_mandate_commission');
$this->createOrUpdateParam($request, 'electronic_billing_by_mandate_iva_percentage');
if ($request->file('terms')) {
$extension = $request->file('terms')->getClientOriginalExtension();
$filenametostore = 'ticket_terms' . '.' . $extension;
Storage::disk('s3')->put(config('s3.default') . '/ticket/' . $filenametostore, fopen($request->file('terms'), 'r+'), 'public');
$url = config('filesystems.disks.s3.url') . '/ticket/' . $filenametostore;
TicketParameter::updateOrCreate(
['key' => 'terms'],
['key' => 'terms', 'value' => $url]
);
}
if ($request->file('app_ticketing_background')) {
$filenametostore = 'subscriber.png';
Storage::disk('s3')->put(config('s3.default') . '/ticket/' . $filenametostore, fopen($request->file('app_ticketing_background'), 'r+'), 'public');
$url = config('filesystems.disks.s3.url') . '/ticket/' . $filenametostore;
TicketParameter::updateOrCreate(
['key' => 'app_ticketing_background'],
['key' => 'app_ticketing_background', 'value' => $url]
);
}
if ($request->file('web_ticketing_background')) {
$filenametostore = 'webTicketingBackground.jpg';
Storage::disk('s3')->put(config('s3.default') . '/web_customization/' . $filenametostore, fopen($request->file('web_ticketing_background'), 'r+'), 'public');
$url = config('filesystems.disks.s3.url') . '/web_customization/' . $filenametostore;
TicketParameter::updateOrCreate(
['key' => 'web_ticketing_background'],
['key' => 'web_ticketing_background', 'value' => $url]
);
}
Cache::forget('parametersTickets');
$this->controller->registerLog(Auth::user()->id, 'Guardar parametros ticketing', json_encode($request->all()), "Update", $this->controller->getModule($request));
return array('r' => true, 'm' => __('messages.updated_successfully'));
} catch (\Throwable $th) {
return array('r' => false, 'm' => __('messages.error_updating'));
}
}
public function validateSendingEmailTickets($origin)
{
$sendMail = false;
if ($origin == TicketOriginEnum::APP) {
$sendMail = $this->getParametersValue('send_mail_tickets_app_purchase');
} else if ($origin == TicketOriginEnum::WEB) {
$sendMail = $this->getParametersValue('send_mail_tickets_web_purchase');
}
$sendMail = filter_var($sendMail, FILTER_VALIDATE_BOOLEAN);
return $sendMail;
}
public function validateElectronicInvoiceCreation()
{
$enableElectronicInvoiceCreation = $this->getParametersValue('enable_electronic_invoice_creation');
$enableElectronicInvoiceCreation = filter_var($enableElectronicInvoiceCreation, FILTER_VALIDATE_BOOLEAN);
return $enableElectronicInvoiceCreation;
}
public function getParametersValue($key)
{
$parameters = $this->getParameterObject();
return property_exists($parameters, $key) ? $parameters->$key : '';
}
public function ivaPercentageTickets()
{
$iva = $this->getParametersValue('iva_percentage_tickets') ?? 0;
return intval($iva);
}
public function ivaPercentageServiceCharge()
{
$iva = $this->getParametersValue('iva_percentage_service_charge') ?? 0;
return intval($iva);
}
public function enableElectronicInvoiceServiceCharge()
{
$enableElectronicInvoiceServiceCharge = $this->getParametersValue('enable_electronic_invoice_service_charge');
$enableElectronicInvoiceServiceCharge = filter_var($enableElectronicInvoiceServiceCharge, FILTER_VALIDATE_BOOLEAN);
return $enableElectronicInvoiceServiceCharge;
}
public function enableElectronicComplimentaryBilling()
{
$enableElectronicComplimentaryBilling = $this->getParametersValue('enable_electronic_courtesy_billing');
$enableElectronicComplimentaryBilling = filter_var($enableElectronicComplimentaryBilling, FILTER_VALIDATE_BOOLEAN);
return $enableElectronicComplimentaryBilling;
}
public function isStaticQRType()
{
$type = $this->getParametersValue('qrcode_type');
if (!$type) {
return false;
}
return $type == QRTypesEnum::STATIC;
}
public function isDinamicQRType()
{
$type = $this->getParametersValue('qrcode_type');
if (!$type) {
return false;
}
return $type == QRTypesEnum::WITH_TIMESTAMP;
}
public function timeToAcceptComplimentaryTickets()
{
$time = $this->getParametersValue('time_to_accept_complimentary_tickets');
$time = filter_var($time, FILTER_VALIDATE_INT);
if (!$time) {
return 30;
}
return $time;
}
public function qrCodeSize()
{
$size = $this->getParametersValue('qrcode_size');
$size = filter_var($size, FILTER_VALIDATE_INT);
if (!$size) {
return 200;
}
return $size;
}
public function enableElectronicBillingByMandate()
{
$enableElectronicBillingByMandate = $this->getParametersValue('enable_electronic_billing_by_mandate');
$enableElectronicBillingByMandate = filter_var($enableElectronicBillingByMandate, FILTER_VALIDATE_BOOLEAN);
return $enableElectronicBillingByMandate;
}
public function electronicBillingByMandateIdentificationClientOrganization()
{
$organization = $this->getParametersValue('electronic_billing_by_mandate_identification_client_organization') ?? 0;
return intval($organization);
}
public function electronicBillingByMandateIdentificationOwnerOrganization()
{
$organization = $this->getParametersValue('electronic_billing_by_mandate_identification_owner_organization') ?? 0;
return intval($organization);
}
public function electronicBillingByMandateCommission()
{
$commission = $this->getParametersValue('electronic_billing_by_mandate_commission') ?? 0;
return intval($commission);
}
public function electronicBillingByMandateIvaPercentage()
{
$iva = $this->getParametersValue('electronic_billing_by_mandate_iva_percentage') ?? 0;
return intval($iva);
}
public function getTicketParametersData()
{
$data = $this->getParameterObject();
return [
'qrcode_type' => $data->qrcode_type != QRTypesEnum::WITH_TIMESTAMP ? QRTypesEnum::STATIC : QRTypesEnum::DYNAMIC,
'qrcode_time' => $data->qrcode_time,
'qrcode_time_unit' => $data->qrcode_time_unit ?? 'seconds',
];
}
private function createOrUpdateParam(Request $request, $key)
{
if ($request[$key] != 'undefined') {
TicketParameter::updateOrCreate(
['key' => $key],
['key' => $key, 'value' => $request[$key]]
);
}
}
}