Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creación módulo Ventas: Crear, ver, eliminar #86

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions app/Http/Controllers/DetailSaleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace App\Http\Controllers;

use App\Models\DetailSale;
use Illuminate\Http\Request;

/**
* Class DetailSaleController
* @package App\Http\Controllers
*/
class DetailSaleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$detailSales = DetailSale::paginate();

return view('detail-sale.index', compact('detailSales'))
->with('i', (request()->input('page', 1) - 1) * $detailSales->perPage());
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$detailSale = new DetailSale();
return view('detail-sale.create', compact('detailSale'));
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
request()->validate(DetailSale::$rules);

$detailSale = DetailSale::create($request->all());

return redirect()->route('detail-sales.index')
->with('success', 'DetailSale created successfully.');
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$detailSale = DetailSale::find($id);

return view('detail-sale.show', compact('detailSale'));
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$detailSale = DetailSale::find($id);

return view('detail-sale.edit', compact('detailSale'));
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param DetailSale $detailSale
* @return \Illuminate\Http\Response
*/
public function update(Request $request, DetailSale $detailSale)
{
request()->validate(DetailSale::$rules);

$detailSale->update($request->all());

return redirect()->route('detail-sales.index')
->with('success', 'DetailSale updated successfully');
}

/**
* @param int $id
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
public function destroy($id)
{
$detailSale = DetailSale::find($id)->delete();

return redirect()->route('detail-sales.index')
->with('success', 'DetailSale deleted successfully');
}
}
122 changes: 122 additions & 0 deletions app/Http/Controllers/SaleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\Sale;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\PDF; // Importamos librería para exportación de PDF de Venta

class SaleController extends Controller
{
public function index()
{
$sales = Sale::paginate();

return view('Admin.sale.index', compact('sales'))
->with('i', (request()->input('page', 1) - 1) * $sales->perPage());
}

public function create()
{
$products = Product::all();
$sale = new Sale();

return view('Admin.Sale.create', compact('sale', 'products'));
}

public function store(Request $request)
{
$request->validate([
'date' => 'required',
'product_id.*' => 'required|distinct',
'quantity.*' => 'required|numeric',
'price.*' => 'required|numeric',
]);

$sale = Sale::create([
'date' => $request->input('date'),
]);

$productData = [];
$productIds = $request->input('product_id');
$quantities = $request->input('quantity');
$prices = $request->input('price');

$total = 0;

foreach ($productIds as $key => $productId) {
$quantity = $quantities[$key];
$price = $prices[$key];

$subtotal = $quantity * $price;
$total += $subtotal;

$productData[$productId] = [
'quantity' => $quantity,
'price' => $price,
];
}

$sale->total = $total;
$sale->save();

$sale->products()->sync($productData);

return redirect()->route('sales.index')->with('created', 'Venta creada con éxito');
}

public function show($id)
{
$sale = Sale::find($id);
$products = $sale->products;

return view('Admin.sale.show', compact('sale', 'products'));
}

public function edit($id)
{
$sale = Sale::find($id);
$products = $sale->products;

return view('Admin.sale.edit', compact('sale', 'products'));
}

public function update(Request $request, $id)
{
$request->validate([
'date' => 'required',
]);

$sale = Sale::find($id);

$productData = [];
$productIds = $request->input('product_id');
$quantities = $request->input('quantity');
$prices = $request->input('price');

$total = 0;

foreach ($productIds as $key => $productId) {
$productData[$productId] = [
'quantity' => $quantities[$key],
'price' => $prices[$key],
];

$subtotal = $quantities[$key] * $prices[$key];
$total += $subtotal;
}

$sale->products()->sync($productData);
$sale->total = $total;
$sale->save();

return redirect()->route('sales.index')->with('updated', 'Venta actualizada con éxito');
}

public function destroy($id)
{
$sale = Sale::find($id)->delete();

return redirect()->route('sales.index')->with('deleted', 'Venta eliminada con éxito');
}
}
58 changes: 58 additions & 0 deletions app/Models/DetailSale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
* Class DetailSale
*
* @property $id
* @property $sale_id
* @property $product_id
* @property $quantity
* @property $price
* @property $created_at
* @property $updated_at
*
* @property Product $product
* @property Sale $sale
* @package App
* @mixin \Illuminate\Database\Eloquent\Builder
*/
class DetailSale extends Model
{

static $rules = [
'sale_id' => 'required',
'product_id' => 'required',
'quantity' => 'required',
'price' => 'required',
];

protected $perPage = 20;

/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['sale_id','product_id','quantity','price'];


/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function product()
{
return $this->belongsTo(Product::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function sale()
{
return $this->hasOne('App\Models\Sale', 'id', 'sale_id');
}
}
50 changes: 50 additions & 0 deletions app/Models/Sale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
* Class Sale
*
* @property $id
* @property $date
* @property $total
* @property $created_at
* @property $updated_at
*
* @property DetailSale[] $detailSales
* @package App
* @mixin \Illuminate\Database\Eloquent\Builder
*/
class Sale extends Model
{

static $rules = [
'date' => 'required',
'total' => 'required',
];

protected $perPage = 20;

/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['date','total'];


/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function detailSales()
{
return $this->hasMany('App\Models\DetailSale', 'sale_id', 'id'); // Un detalle de venta tiene muchos productos y está ligado a una venta
}

public function products()
{
return $this->belongsToMany(Product::class, 'detail_sale')->withPivot('quantity', 'price');
}
}
29 changes: 29 additions & 0 deletions database/migrations/2023_12_05_155131_sales.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('sales', function (Blueprint $table) {
$table->id();
$table->date("date");
$table->decimal("total")->default(0.0);
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
Loading