- Changed view extends from "admin.main" to "administrator.layout.master" across multiple files. - Updated route names in admin.php and administrator.php to reflect new structure for user, order, invoice, and other management functionalities. - Adjusted view includes to point to the new directory structure for blocks and components. - Ensured all permission checks remain intact while refactoring the views.
100 lines
2.8 KiB
PHP
100 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\InvoiceRequest;
|
|
use App\Repositories\Invoice\InvoiceRepositoryInterface;
|
|
use App\Repositories\Order\OrderRepositoryInterface;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class InvoiceController extends Controller
|
|
{
|
|
public function __construct(
|
|
public InvoiceRepositoryInterface $invoiceRepository,
|
|
public OrderRepositoryInterface $orderRepository
|
|
) {}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
|
|
$invoices = $this->invoiceRepository->filter(
|
|
$request->merge(['page' => $request->get('page') ?? 1])->all(),
|
|
[
|
|
'payment',
|
|
'customer'
|
|
]
|
|
);
|
|
$title = 'List all invoices';
|
|
|
|
return view('administrator.pages.invoice.index', compact('title', 'invoices'));
|
|
}
|
|
|
|
public function edit(int $id = 0)
|
|
{
|
|
$invoice = $this->invoiceRepository->findWithRelationships($id, ['payment', 'invoiceItem', 'customer']);
|
|
|
|
if (!$invoice) {
|
|
return back()->withErrors("Can't find Invoice by ID #" . $id);
|
|
}
|
|
|
|
$title = "View invoice #" . $id;
|
|
|
|
return view("administrator.pages.invoice.edit", compact('title', 'invoice'));
|
|
}
|
|
|
|
public function update(InvoiceRequest $request, int $id = 0)
|
|
{
|
|
|
|
$invoice = $this->invoiceRepository->find($id);
|
|
if (!$invoice) {
|
|
return back()->withErrors("Invalid request");
|
|
}
|
|
$request->validated();
|
|
|
|
$status = match (strtolower($request->get('action'))) {
|
|
"paid" => 1,
|
|
"cancel" => 2,
|
|
default => 0
|
|
};
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
|
|
$this->invoiceRepository->update($id, [
|
|
'status' => $status,
|
|
'date_paid' => $status === 1 ? time() : null
|
|
]);
|
|
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
return back()->with('success', "Updated Successfully");
|
|
}
|
|
|
|
public function destroy(int $id = 0)
|
|
{
|
|
|
|
$invoice = $this->invoiceRepository->find($id);
|
|
if (!$invoice) {
|
|
return back()->withErrors("Invalid request");
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
|
|
$this->orderRepository->updateBy('invoice_id', $id, ['invoice_id' => 0]);
|
|
$this->invoiceRepository->delete($id);
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
|
|
return redirect()->route('administrator.pages.invoice.index')->with('success', 'Invoice deleted successfully');
|
|
}
|
|
}
|