Fix: Correcciones panel admin y API frontend
## Panel de Administración ### FormBuilder personalizado (Laravel 11) - Creado app/Support/FormBuilder.php como reemplazo de laravelcollective/html - Creado app/Support/Facades/Form.php para el facade - Registrado en AppServiceProvider y config/app.php - Soporta: text, email, password, file, textarea, select, checkbox, radio, etc. - Manejo de valores null en todos los parámetros ### Configuración de sesión - Cambiado same_site de "none" a "lax" para compatibilidad HTTP - Corrige error 419 Page Expired en login ### Status CRUD - Agregado campo en_name al formulario (español/inglés) - Actualizado StatusController create/update para manejar en_name ### Dependencias - Instalado spatie/laravel-google-cloud-storage para driver GCS ## API Frontend ### Validaciones de perfil de proveedor Agregadas validaciones en endpoints que requieren perfil de proveedor: - SupplierController::getpostulation - Retorna error 400 si no hay perfil - SupplierController::getcontractedpostulation - Retorna error 400 si no hay perfil - PostulationController::postulate - Retorna error 400 si no hay perfil - PostulationController::getfinishedpostulations - Retorna error 400 si no hay perfil - ContractController::startcontract - Retorna error 400 si no hay perfil ### Null safety en contratos - ContractController::getcurrentcontracts - Manejo seguro de supplier/category null - ContractController::getfinishedcontracts - Manejo seguro de supplier/category/status null Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -639,12 +639,12 @@ class ContractController extends Controller
|
||||
$day_limit = Carbon::parse($ccontract->created_at);
|
||||
$currentcontractinfo = array(
|
||||
'id' => $ccontract->id,
|
||||
'phone' => $supplier->user->phone,
|
||||
'category' => $category->name,
|
||||
'en_category' => $category->en_name,
|
||||
'phone' => $supplier ? ($supplier->user ? $supplier->user->phone : null) : null,
|
||||
'category' => $category ? $category->name : null,
|
||||
'en_category' => $category ? $category->en_name : null,
|
||||
'address' => $ccontract->address,
|
||||
'date' => $ccontract->appointment,
|
||||
'supplier' => $supplier->company_name,
|
||||
'supplier' => $supplier ? $supplier->company_name : 'Proveedor no disponible',
|
||||
'status' => $ccontract->status_id,
|
||||
'amount' => $ccontract->amount,
|
||||
'code' => $ccontract->code,
|
||||
@@ -778,6 +778,14 @@ class ContractController extends Controller
|
||||
|
||||
$user = Auth::user();
|
||||
$supplier = $user->suppliers;
|
||||
|
||||
if (!$supplier) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'No tienes un perfil de proveedor registrado'
|
||||
], 400);
|
||||
}
|
||||
|
||||
$ccontract = CurrentContracts::where('code', $request->contract_pin)->where('supplier_id', $supplier->id)->first();
|
||||
|
||||
if($ccontract) {
|
||||
@@ -969,16 +977,16 @@ class ContractController extends Controller
|
||||
$day_limit = Carbon::parse($fcontract->created_at);
|
||||
$finishedcontractinfo = array(
|
||||
'id' => $fcontract->id,
|
||||
'category' => $category->name,
|
||||
'en_category' => $category->en_name,
|
||||
'category' => $category ? $category->name : null,
|
||||
'en_category' => $category ? $category->en_name : null,
|
||||
'address' => $fcontract->address,
|
||||
'date' => $fcontract->appointment,
|
||||
'date_difference' => $time_limit->diff(Carbon::now(), false)->days,
|
||||
'supplier' => $supplier->company_name,
|
||||
'supplier' => $supplier ? $supplier->company_name : 'Proveedor no disponible',
|
||||
'amount' => $fcontract->amount,
|
||||
'scored' => $fcontract->scored_at,
|
||||
'parent' => $fcontract->parent_contract_id,
|
||||
'status' => $fcontract->status->name
|
||||
'status' => $fcontract->status ? $fcontract->status->name : null
|
||||
);
|
||||
$finishedcontracts[] = $finishedcontractinfo;
|
||||
}
|
||||
|
||||
@@ -218,6 +218,13 @@ class PostulationController extends Controller
|
||||
$time_limit = (9900 - Carbon::now()->diffInMinutes($time_created));
|
||||
$supplier = Suppliers::where('user_id', $user->id)->first();
|
||||
|
||||
if (!$supplier) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'No tienes un perfil de proveedor registrado'
|
||||
], 400);
|
||||
}
|
||||
|
||||
if ($time_limit > 0) {
|
||||
if (in_array($postulation->category_id, $supplier->categories->pluck('id')->toArray())) {
|
||||
/*if($supplier->membership == 1) {
|
||||
@@ -282,6 +289,14 @@ class PostulationController extends Controller
|
||||
|
||||
public function getfinishedpostulations(Request $request) {
|
||||
$user = Auth::user();
|
||||
|
||||
if (!$user->suppliers) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'No tienes un perfil de proveedor registrado'
|
||||
], 400);
|
||||
}
|
||||
|
||||
$postulations = FinishedContracts::where('supplier_id', $user->suppliers->id)->orderBy('created_at', 'DESC')->get();
|
||||
$finishedpostulations = array();
|
||||
|
||||
|
||||
@@ -53,10 +53,12 @@ class StatusController extends Controller
|
||||
|
||||
$rules = [
|
||||
'name' => 'required|string',
|
||||
'en_name' => 'required|string',
|
||||
];
|
||||
|
||||
$messages = [
|
||||
'name.required' => 'Se requiere el nombre del status',
|
||||
'en_name.required' => 'Se requiere el nombre del status en inglés',
|
||||
];
|
||||
|
||||
$validator = Validator::make($request->all(), $rules, $messages);
|
||||
@@ -66,6 +68,7 @@ class StatusController extends Controller
|
||||
|
||||
$status = new Status();
|
||||
$status->name = strip_tags($request->name);
|
||||
$status->en_name = strip_tags($request->en_name);
|
||||
$status->save();
|
||||
|
||||
return redirect('status');
|
||||
@@ -119,10 +122,12 @@ class StatusController extends Controller
|
||||
|
||||
$rules = [
|
||||
'name' => 'required|string',
|
||||
'en_name' => 'required|string',
|
||||
];
|
||||
|
||||
$messages = [
|
||||
'name.required' => 'Se requiere el nombre del status',
|
||||
'en_name.required' => 'Se requiere el nombre del status en inglés',
|
||||
];
|
||||
|
||||
$validator = Validator::make($request->all(), $rules, $messages);
|
||||
@@ -132,6 +137,7 @@ class StatusController extends Controller
|
||||
|
||||
$status = Status::find($id);
|
||||
$status->name = strip_tags($request->name);
|
||||
$status->en_name = strip_tags($request->en_name);
|
||||
$status->save();
|
||||
|
||||
return redirect('status');
|
||||
|
||||
@@ -1211,6 +1211,14 @@ class SupplierController extends Controller
|
||||
|
||||
$user = Auth::user();
|
||||
$supplier = Suppliers::where('user_id', $user->id)->first();
|
||||
|
||||
if (!$supplier) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'No tienes un perfil de proveedor registrado'
|
||||
], 400);
|
||||
}
|
||||
|
||||
$distance = 0.5;
|
||||
$postulations = Postulations::distance('location', $supplier->location, $distance)->orderBy('created_at', 'DESC')->get();
|
||||
|
||||
@@ -1246,6 +1254,14 @@ class SupplierController extends Controller
|
||||
|
||||
$user = Auth::user();
|
||||
$supplier = Suppliers::where('user_id', $user->id)->first();
|
||||
|
||||
if (!$supplier) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'No tienes un perfil de proveedor registrado'
|
||||
], 400);
|
||||
}
|
||||
|
||||
$contracts = CurrentContracts::where('supplier_id', $supplier->id)->orderBy('created_at', 'DESC')->get();
|
||||
|
||||
$contractsinfo = array();
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Services\SocialUserResolver;
|
||||
use App\Support\FormBuilder;
|
||||
use Coderello\SocialGrant\Resolvers\SocialUserResolverInterface;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
@@ -25,7 +26,9 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
$this->app->singleton('form', function ($app) {
|
||||
return new FormBuilder();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
13
app/Support/Facades/Form.php
Normal file
13
app/Support/Facades/Form.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
class Form extends Facade
|
||||
{
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return 'form';
|
||||
}
|
||||
}
|
||||
265
app/Support/FormBuilder.php
Normal file
265
app/Support/FormBuilder.php
Normal file
@@ -0,0 +1,265 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Illuminate\Support\HtmlString;
|
||||
|
||||
class FormBuilder
|
||||
{
|
||||
protected $model;
|
||||
|
||||
public function model($model, $options = [])
|
||||
{
|
||||
$this->model = $model;
|
||||
return $this->open($options ?? []);
|
||||
}
|
||||
|
||||
public function open($options = [])
|
||||
{
|
||||
$options = $options ?? [];
|
||||
$method = strtoupper($options['method'] ?? 'POST');
|
||||
$action = $options['url'] ?? ($options['action'] ?? '');
|
||||
$files = isset($options['files']) && $options['files'] ? ' enctype="multipart/form-data"' : '';
|
||||
$class = isset($options['class']) ? ' class="' . e($options['class']) . '"' : '';
|
||||
$id = isset($options['id']) ? ' id="' . e($options['id']) . '"' : '';
|
||||
|
||||
$html = '<form method="' . ($method === 'GET' ? 'GET' : 'POST') . '" action="' . e($action) . '"' . $files . $class . $id . '>';
|
||||
$html .= csrf_field();
|
||||
|
||||
if (!in_array($method, ['GET', 'POST'])) {
|
||||
$html .= method_field($method);
|
||||
}
|
||||
|
||||
return new HtmlString($html);
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
$this->model = null;
|
||||
return new HtmlString('</form>');
|
||||
}
|
||||
|
||||
public function token()
|
||||
{
|
||||
return csrf_field();
|
||||
}
|
||||
|
||||
public function label($name, $value = null, $options = [], $escapeHtml = true)
|
||||
{
|
||||
$options = $options ?? [];
|
||||
$value = $value ?? ucfirst(str_replace('_', ' ', $name));
|
||||
$for = ' for="' . e($name) . '"';
|
||||
$attributes = $this->attributes($options);
|
||||
|
||||
return new HtmlString('<label' . $for . $attributes . '>' . ($escapeHtml ? e($value) : $value) . '</label>');
|
||||
}
|
||||
|
||||
public function text($name, $value = null, $options = [])
|
||||
{
|
||||
return $this->input('text', $name, $value, $options ?? []);
|
||||
}
|
||||
|
||||
public function email($name, $value = null, $options = [])
|
||||
{
|
||||
return $this->input('email', $name, $value, $options ?? []);
|
||||
}
|
||||
|
||||
public function password($name, $options = [])
|
||||
{
|
||||
return $this->input('password', $name, null, $options ?? []);
|
||||
}
|
||||
|
||||
public function hidden($name, $value = null, $options = [])
|
||||
{
|
||||
return $this->input('hidden', $name, $value, $options ?? []);
|
||||
}
|
||||
|
||||
public function number($name, $value = null, $options = [])
|
||||
{
|
||||
return $this->input('number', $name, $value, $options ?? []);
|
||||
}
|
||||
|
||||
public function file($name, $options = [])
|
||||
{
|
||||
return $this->input('file', $name, null, $options ?? []);
|
||||
}
|
||||
|
||||
public function textarea($name, $value = null, $options = [])
|
||||
{
|
||||
$options = $options ?? [];
|
||||
$value = $this->getValueAttribute($name, $value);
|
||||
$attributes = $this->attributes(array_merge(['name' => $name, 'id' => $name], $options));
|
||||
|
||||
return new HtmlString('<textarea' . $attributes . '>' . e($value ?? '') . '</textarea>');
|
||||
}
|
||||
|
||||
public function select($name, $list = [], $selected = null, $options = [], $optionsAttributes = [], $optgroupsAttributes = [])
|
||||
{
|
||||
$options = $options ?? [];
|
||||
$list = $list ?? [];
|
||||
$selected = $this->getValueAttribute($name, $selected);
|
||||
$attributes = $this->attributes(array_merge(['name' => $name, 'id' => $name], $options));
|
||||
|
||||
$html = '<select' . $attributes . '>';
|
||||
|
||||
foreach ($list as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$html .= '<optgroup label="' . e($key) . '">';
|
||||
foreach ($value as $optKey => $optValue) {
|
||||
$html .= $this->option($optKey, $optValue, $selected);
|
||||
}
|
||||
$html .= '</optgroup>';
|
||||
} else {
|
||||
$html .= $this->option($key, $value, $selected);
|
||||
}
|
||||
}
|
||||
|
||||
$html .= '</select>';
|
||||
|
||||
return new HtmlString($html);
|
||||
}
|
||||
|
||||
protected function option($key, $value, $selected)
|
||||
{
|
||||
$isSelected = $this->isSelected($key, $selected) ? ' selected' : '';
|
||||
return '<option value="' . e($key) . '"' . $isSelected . '>' . e($value) . '</option>';
|
||||
}
|
||||
|
||||
protected function isSelected($key, $selected)
|
||||
{
|
||||
if (is_array($selected)) {
|
||||
return in_array($key, $selected);
|
||||
}
|
||||
return (string) $key === (string) $selected;
|
||||
}
|
||||
|
||||
public function checkbox($name, $value = 1, $checked = null, $options = [])
|
||||
{
|
||||
$options = $options ?? [];
|
||||
$checked = $this->getCheckedState($name, $value, $checked) ? ' checked' : '';
|
||||
$attributes = $this->attributes(array_merge([
|
||||
'name' => $name,
|
||||
'id' => $options['id'] ?? $name,
|
||||
'value' => $value,
|
||||
'type' => 'checkbox'
|
||||
], $options));
|
||||
|
||||
return new HtmlString('<input' . $attributes . $checked . '>');
|
||||
}
|
||||
|
||||
public function radio($name, $value = null, $checked = null, $options = [])
|
||||
{
|
||||
$options = $options ?? [];
|
||||
$checked = $this->getCheckedState($name, $value, $checked) ? ' checked' : '';
|
||||
$attributes = $this->attributes(array_merge([
|
||||
'name' => $name,
|
||||
'id' => $options['id'] ?? $name . '_' . $value,
|
||||
'value' => $value,
|
||||
'type' => 'radio'
|
||||
], $options));
|
||||
|
||||
return new HtmlString('<input' . $attributes . $checked . '>');
|
||||
}
|
||||
|
||||
public function submit($value = null, $options = [])
|
||||
{
|
||||
return $this->input('submit', null, $value, $options ?? []);
|
||||
}
|
||||
|
||||
public function button($value = null, $options = [])
|
||||
{
|
||||
$options = $options ?? [];
|
||||
$attributes = $this->attributes(array_merge(['type' => 'button'], $options));
|
||||
return new HtmlString('<button' . $attributes . '>' . e($value ?? '') . '</button>');
|
||||
}
|
||||
|
||||
public function input($type, $name, $value = null, $options = [])
|
||||
{
|
||||
$options = $options ?? [];
|
||||
|
||||
if ($type !== 'password' && $type !== 'file') {
|
||||
$value = $this->getValueAttribute($name, $value);
|
||||
}
|
||||
|
||||
$attributes = $this->attributes(array_merge([
|
||||
'type' => $type,
|
||||
'name' => $name,
|
||||
'id' => $options['id'] ?? $name,
|
||||
'value' => $value
|
||||
], $options));
|
||||
|
||||
return new HtmlString('<input' . $attributes . '>');
|
||||
}
|
||||
|
||||
public function date($name, $value = null, $options = [])
|
||||
{
|
||||
return $this->input('date', $name, $value, $options ?? []);
|
||||
}
|
||||
|
||||
public function time($name, $value = null, $options = [])
|
||||
{
|
||||
return $this->input('time', $name, $value, $options ?? []);
|
||||
}
|
||||
|
||||
public function datetime($name, $value = null, $options = [])
|
||||
{
|
||||
return $this->input('datetime-local', $name, $value, $options ?? []);
|
||||
}
|
||||
|
||||
protected function getValueAttribute($name, $value = null)
|
||||
{
|
||||
if (is_null($name)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$old = old($name);
|
||||
if (!is_null($old)) {
|
||||
return $old;
|
||||
}
|
||||
|
||||
if (!is_null($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ($this->model && isset($this->model->{$name})) {
|
||||
return $this->model->{$name};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function getCheckedState($name, $value, $checked)
|
||||
{
|
||||
$old = old($name);
|
||||
if (!is_null($old)) {
|
||||
return $old == $value;
|
||||
}
|
||||
|
||||
if (!is_null($checked)) {
|
||||
return $checked;
|
||||
}
|
||||
|
||||
if ($this->model && isset($this->model->{$name})) {
|
||||
return $this->model->{$name} == $value;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function attributes($attributes)
|
||||
{
|
||||
$attributes = $attributes ?? [];
|
||||
$html = '';
|
||||
foreach ($attributes as $key => $value) {
|
||||
if (is_null($value)) {
|
||||
continue;
|
||||
}
|
||||
if (is_numeric($key)) {
|
||||
$html .= ' ' . $value;
|
||||
} else {
|
||||
$html .= ' ' . $key . '="' . e($value) . '"';
|
||||
}
|
||||
}
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user