File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Http/Controllers/ExperiencePaymentController.php
<?php
namespace App\Http\Controllers;
use App\Core\CorporateIdentity\Application\CorporateIdentityService;
use \Excel;
use Carbon\Carbon;
use App\Models\Experience\Experience;
use App\Models\Experience\ExperiencePayment;
use App\Http\Controllers\Exports\ExperiencesExport;
use App\PaymentTransaction;
use App\Repositories\ERPLogRepository;
use App\Repositories\PaymentTransactionRepository;
use App\Services\ExperienceService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;
class ExperiencePaymentController extends Controller implements PaymentTransactionRepository, ERPLogRepository
{
private $util;
private $experienceService;
public function __construct()
{
$this->util = new UtilController;
$this->experienceService = new ExperienceService;
}
public function index($experience_type = 'children')
{
$experiences = Experience::select('id', 'name')->get();
$states = PaymentTransaction::groupBy('state')->orderBy('state', 'ASC')->pluck('state');
$corporateIdentity = CorporateIdentityService::get();
return view('experiences.monitor.list', compact('experience_type', 'experiences', 'states', 'corporateIdentity'));
}
public function tableFilter(Request $request)
{
$obj = $this->tableFilterQuery($request);
DB::enableQueryLog();
$dataTable = DataTables::of($obj);
$this->tableFilterCustomColumns($dataTable);
$response = $dataTable->make(true);
$data = $response->getData();
$data = json_decode(json_encode($data), true);
$queries = \DB::getQueryLog();
$data['queries'] = $queries;
return $data;
}
public function validateExport(Request $request)
{
if ($request['query']) {
$results = $this->util->getGenericData($request["query"], $request["bindings"]);
if (count($results) > 0) {
$name = 'ReporteMonitorExperiencias' . time() . '.xlsx';
Excel::store(new ExperiencesExport($results), $name, 'public');
return response()->json(['success' => true, 'message' => 'Validación OK', 'data' => $name]);
}
}
return response()->json(['success' => false, 'message' => 'No existen datos a exportar']);
}
public function export($name)
{
return $this->util->export($name);
}
public function validatePayment(Request $request, $id)
{
$experiencePayment = ExperiencePayment::where('id', $id)->with('payment_transaction')->first();
$paymentTransactionController = new PaymentTransactionController;
$paymentTransactionController->validatePayment($experiencePayment->payment_transaction);
$this->registerLog(Auth::user()->id, 'Validar pago de experiencia en pasarela', json_encode($experiencePayment), "Update", $this->getModule($request));
return array(
'r' => true,
'data' => null,
'm' => 'Se validó el pago en la pasarela de pagos'
);
}
public function finishPayment($paymentTransaction)
{
$this->experienceService->finishPayment($paymentTransaction);
}
public function saveERPLog($data, $message, $action) {}
public function retrySyncERP($data, $action) {}
public function syncERP($order, $action = null) {}
private function tableFilterQuery($request)
{
$states = $request->states;
$experiences = $request->experiences;
$startPayment = $request->startPayment;
$endPayment = $request->endPayment;
$experienceType = $request->experienceType;
$obj = ExperiencePayment::select(
'experience_payments.id',
'experience_payments.payment_identifier',
'experience_payments.status AS experience_status',
'experience_payments.amount',
'experience_payments.raffle',
'experience_payments.referral_code',
'experience_payments.payment_due_date AS experience_date',
'experience_payments.expires_at AS experience_expires_at',
'experience_payments.billing_first_name',
'experience_payments.billing_last_name',
'billing_document_type.name AS billing_document_type',
'experience_payments.billing_document',
'experience_payments.billing_email',
'experience_payments.billing_phone',
'addresses.direction AS billing_address',
'experience_users.first_name',
'experience_users.last_name',
'document_types.name AS document_type',
'experience_users.document',
'experience_users.email',
'experience_users.phone',
'experience_users.birthdate',
'experience_users.identification_file',
'experience_users.insurance_file',
'experience_users.guardian_first_name',
'experience_users.guardian_last_name',
'experience_users.guardian_document',
'experience_users.guardian_email',
'experience_users.guardian_phone',
'experiences.name AS experience',
'experience_plans.name AS experience_plan',
DB::raw('IFNULL(payment_transactions.state, "PENDING") AS payment_state'),
'experience_payments.total AS payment_total',
DB::raw('IFNULL(payment_transactions.reference, "") AS payment_reference'),
DB::raw('IFNULL(payment_transactions.gateway_transaction_id, "") AS payment_transaction'),
DB::raw('IFNULL(payment_transactions.payment_date, "") AS payment_date'),
)->join('experience_users', 'experience_users.id', '=', 'experience_payments.experience_user_id')
->join('experience_plan_prices', 'experience_plan_prices.id', '=', 'experience_payments.experience_plan_price_id')
->join('experience_plans', 'experience_plans.id', '=', 'experience_plan_prices.experience_plan_id')
->join('experiences', 'experiences.id', '=', 'experience_plans.experience_id')
->join('document_types', 'document_types.id', '=', 'experience_users.document_type_id')
->leftjoin('payment_transactions', 'payment_transactions.id', '=', 'experience_payments.payment_transaction_id')
->leftJoin('document_types AS billing_document_type', 'billing_document_type.id', '=', 'experience_payments.billing_document_type_id')
->leftJoin('addresses', 'addresses.id', '=', 'experience_payments.billing_address')
->where('experience_users.experience_type', $experienceType)
->groupBy('experience_payments.id')
->orderBy('experience_payments.id', 'DESC');
if ($states && $states != 'null') {
$obj->whereIn('payment_transactions.state', $states);
}
if ($experiences && $experiences != 'null') {
$obj->whereIn('experiences.id', $experiences);
}
if ($startPayment && $startPayment != 'null') {
$obj->whereDate('payment_transactions.payment_date', '>=', $startPayment);
}
if ($endPayment && $endPayment != 'null') {
$obj->whereDate('payment_transactions.payment_date', '<=', $endPayment);
}
return $obj;
}
private function tableFilterCustomColumns($dataTable)
{
$dataTable->addColumn('actions', function ($obj) {
if (
$this->validateRolePermissions('experience', 'administrator') ||
$this->validateRolePermissions('experience', 'supervisor')
) {
$actions = '';
if ($obj->payment_state && $obj->payment_state != 'CONFIRMED')
$actions .= '<i class="fa fa-credit-card iconMini" onClick="clickValidatePaymentInGateway(' . $obj->id . ')" data-id="' . $obj->id . '" title="Validar en pasarela"></i>';
return $actions;
} else {
return '';
}
})
->editColumn('experience_status', function ($obj) {
return '<span class="label ' . ($obj->experience_status == 'CONFIRMED' ? 'label-success' : 'label-danger') . '">' . $obj->experience_status . '</span>';
})
->editColumn('experience_date', function ($obj) {
return $obj->experience_date ? Carbon::parse($obj->experience_date)->format('Y-m-d h:i:s A') : '';
})
->editColumn('experience_expires_at', function ($obj) {
return $obj->experience_expires_at ? Carbon::parse($obj->experience_expires_at)->format('Y-m-d h:i:s A') : '';
})
->editColumn('payment_state', function ($obj) {
return '<span class="label ' . ($obj->payment_state == 'CONFIRMED' ? 'label-success' : 'label-danger') . '">' . $obj->payment_state . '</span>';
})
->editColumn('payment_date', function ($obj) {
return $obj->payment_date ? Carbon::parse($obj->payment_date)->format('Y-m-d h:i:s A') : '';
})
->rawColumns(['actions', 'experience_status', 'experience_date', 'experience_expires_at', 'payment_state', 'payment_date']);
}
}