Agregado es solidaio y eliminado proveedor

This commit is contained in:
Alejandro Tasistro 2025-05-15 01:13:38 -03:00
parent 2faea3b007
commit eb3e4730bf
4 changed files with 100 additions and 27 deletions

View file

@ -15,21 +15,15 @@ use Illuminate\Support\Facades\DB;
class Producto extends Model
{
public $timestamps = false;
protected $fillable = ["nombre", "precio", "presentacion", "stock", "categoria"];
protected $fillable = ["nombre", "precio", "categoria"];
static int $paginarPorDefecto = 10;
public function subpedidos(): BelongsToMany
{
return $this->belongsToMany('App\Subpedido', 'productos_subpedidos')->withPivot(["cantidad", "notas"]);
return $this->belongsToMany(Subpedido::class, 'productos_subpedidos')->withPivot(["cantidad", "notas"]);
}
public function proveedor(): BelongsTo
{
return $this->belongsTo('App\Proveedor');
}
//Este método permite que se apliquen los filtros al hacer una request (por ejemplo, de búsqueda)
// Este método permite que se apliquen los filtros al hacer una request (por ejemplo, de búsqueda)
public function scopeFiltrar($query, FiltroDeProducto $filtros): Builder
{
return $filtros->aplicar($query);

View file

@ -1,18 +0,0 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Proveedor extends Model
{
public $timestamps = false;
protected $fillable = [ "nombre","direccion","telefono","correo","comentario" ];
protected $table = 'proveedores';
public function productos(): HasMany
{
return $this->hasMany('App\Producto');
}
}

View file

@ -0,0 +1,38 @@
<?php
use App\Producto;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AgregarEsSolidario extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('productos', function (Blueprint $table) {
$table->boolean('es_solidario')->default(false);
});
foreach (Producto::all() as $producto) {
$producto->es_solidario = $producto->proveedor_id != null;
$producto->save();
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('productos', function (Blueprint $table) {
$table->dropColumn('es_solidario');
});
}
}

View file

@ -0,0 +1,59 @@
<?php
use App\Producto;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class EliminarProveedor extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('productos', function (Blueprint $table) {
$table->dropColumn('proveedor_id');
});
Schema::dropIfExists('proveedores');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::create('proveedores', function (Blueprint $table) {
$table->id();
$table->string('nombre');
$table->string('direccion')->nullable();
$table->string('telefono')->nullable();
$table->string('correo')->nullable();
$table->boolean('economia_solidaria')->nullable();
$table->boolean('nacional')->nullable();
$table->text('detalles_de_pago')->nullable();
$table->text('comentario')->nullable();
$table->timestamps();
});
Schema::table('productos', function (Blueprint $table) {
$table->foreignId('proveedor_id')->nullable();
});
$proveedor_id = DB::table('proveedores')->insertGetId([
['nombre' => 'Proveedor de economía solidaria',
'economia_solidaria' => 1,
'nacional' => 1]
]);
foreach (Producto::all() as $producto) {
$producto->proveedor_id = $producto->es_solidario ? $proveedor_id : null;
$producto->save();
}
}
}