pai-sklep/app/Http/Controllers/OrderController.php

145 lines
4.3 KiB
PHP
Raw Normal View History

2021-11-14 14:51:49 +01:00
<?php
namespace App\Http\Controllers;
2021-12-07 12:22:46 +01:00
use App\Models\Address;
2021-11-14 14:51:49 +01:00
use App\Models\Order;
use App\Models\Product;
2021-12-07 12:22:46 +01:00
use App\Models\User;
2021-11-14 14:51:49 +01:00
use Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class OrderController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
2021-12-07 12:22:46 +01:00
$user = Auth::user();
if (!$user) {
throw new AccessDeniedHttpException("Not logged in");
}
return view("order/index", ["orders" => $user->orders]);
2021-11-14 14:51:49 +01:00
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
2021-12-07 12:22:46 +01:00
public function create(Request $request)
2021-11-14 14:51:49 +01:00
{
2021-12-07 12:22:46 +01:00
$validated = $request->validate([
'products' => 'required|array',
2021-12-14 18:03:22 +01:00
'products.*' => 'exists:products,uuid',
'product_quantities' => 'required|array',
'product_quantities.*' => 'numeric|min:1',
2021-12-07 12:22:46 +01:00
]);
2021-12-14 18:03:22 +01:00
$products_quantities = array_map(fn ($v, $q) => [Product::query()->where("uuid", $v)->first(), $q], $validated["products"], $validated["product_quantities"]);
return view("order/create", ["products_with_quantities" => $products_quantities]);
2021-11-14 14:51:49 +01:00
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$user = Auth::user();
if (!$user) {
throw new AccessDeniedHttpException("Not logged in");
}
$validated = $request->validate([
'products' => 'required|array',
2021-12-07 12:22:46 +01:00
'products.*' => 'exists:products,uuid',
2021-12-14 18:03:22 +01:00
'product_quantities' => 'required|array',
'product_quantities.*' => 'numeric|min:1',
2021-12-07 12:22:46 +01:00
'address' => 'required',
2021-11-14 14:51:49 +01:00
]);
2021-12-14 18:03:22 +01:00
$products_quantities = array_map(fn ($v, $q) => [Product::query()->where("uuid", $v)->first(), $q], $validated["products"], $validated["product_quantities"]);
2021-11-14 14:51:49 +01:00
2021-12-14 18:03:22 +01:00
DB::transaction(function () use ($validated, $user, $products_quantities) {
2021-12-07 12:22:46 +01:00
$address = new Address($validated);
$address->user()->associate($user);
$address->save();
$order = new Order();
$order->address()->associate($address);
2021-11-14 14:51:49 +01:00
$order->user()->associate($user);
2021-12-14 18:03:22 +01:00
$order->cost = array_reduce($products_quantities, fn ($c, $i) => bcadd($c,bcmul($i[0]->price,$i[1])), 0);
2021-11-14 14:51:49 +01:00
2021-12-07 12:22:46 +01:00
$order->save();
2021-12-14 18:03:22 +01:00
foreach($products_quantities as $product_quantity) {
$product = $product_quantity[0];
$quantity = $product_quantity[1];
2021-12-07 12:22:46 +01:00
$order->products()->attach($product);
2021-12-14 18:03:22 +01:00
$order->products()->find($product->uuid)->pivot->quantity = $quantity;
2021-12-07 12:22:46 +01:00
}
2021-11-14 14:51:49 +01:00
});
2021-12-14 18:03:22 +01:00
foreach($products_quantities as $product) {
$user->cart()->detach($product[0]);
2021-12-07 12:22:46 +01:00
}
2021-12-14 18:03:22 +01:00
return redirect()->route("order.index");
2021-11-14 14:51:49 +01:00
}
/**
* Display the specified resource.
*
* @return \Illuminate\Http\Response
*/
public function show(Order $order)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @return \Illuminate\Http\Response
*/
public function edit(Order $order)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Order $order)
{
$validated = $request->validate([
'address' => 'string',
]);
2021-11-24 11:34:46 +01:00
if (isset($validated["address"])) {
$address = $order->address()->withDefault(fn () => new Addres($validated));
2021-11-14 14:51:49 +01:00
$address->address = $validated["address"];
$order->save();
}
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\Response
*/
public function destroy(Order $order)
{
throw new Error("TODO");
//
}
}