96 lines
2.7 KiB
PHP
96 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Invoice;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class InvoiceController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index($from = null, $end = null): JsonResponse
|
|
{
|
|
$from = $from . ' 00:00:00';
|
|
$end = $end . ' 23:59:59';
|
|
return response()->json(Invoice::whereBetween('created_at', [$from, $end])->with(['address', 'customer'])->orderBy('created_at', 'desc')->get());
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource that have a status "sent".
|
|
*/
|
|
public function open(): JsonResponse
|
|
{
|
|
return response()->json(Invoice::where('status', '=', 'sent')->orderBy('created_at', 'desc')->with(['customer', 'address', 'payments'])->get());
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$invoiceData = $request->validate([
|
|
'customer_id' => 'required|integer|exists:customers,id',
|
|
'address_id' => 'required|integer|exists:addresses,id',
|
|
'delivery_id' => 'nullable|integer|exists:addresses,id',
|
|
'tax' => 'required|numeric',
|
|
'sum' => 'required|numeric',
|
|
]);
|
|
$invoiceData['user_id'] = auth()->id();
|
|
$invoiceData['type'] = '380';
|
|
$invoiceData['status'] = 'created';
|
|
$invoiceData['invoice_number'] = Invoice::whereYear('created_at', now()->year)->count() + 1;
|
|
|
|
|
|
|
|
$invoice = new Invoice($invoiceData);
|
|
$invoice->save();
|
|
|
|
return response()->json($invoice);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Invoice $invoice): JsonResponse
|
|
{
|
|
$invoiceData = $request->validate([
|
|
'customer_id' => 'required|integer|exists:customers,id',
|
|
'address_id' => 'required|integer|exists:addresses,id',
|
|
'delivery_id' => 'nullable|integer|exists:addresses,id',
|
|
'tax' => 'required|numeric',
|
|
'sum' => 'required|numeric',
|
|
]);
|
|
|
|
$invoice->update($invoiceData);
|
|
$invoice->items()->delete();
|
|
|
|
return response()->json($invoice);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource's status in storage.
|
|
*/
|
|
public function state(Request $request, Invoice $invoice): JsonResponse
|
|
{
|
|
$invoiceData = $request->validate([
|
|
'status' => 'required|string'
|
|
]);
|
|
|
|
$invoice->update($invoiceData);
|
|
|
|
return response()->json($invoice);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Invoice $invoice)
|
|
{
|
|
//
|
|
}
|
|
}
|