2023-08-14 09:23:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
|
|
2023-08-14 17:48:26 +00:00
|
|
|
use App\Enums\Invoices\InvoiceStatusEnum;
|
2023-08-14 09:23:39 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Http\Requests\Bo\Invoices\StoreInvoiceRequest;
|
|
|
|
|
use App\Http\Requests\Bo\Invoices\UpdateInvoiceRequest;
|
|
|
|
|
use App\Models\Company;
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
|
use App\Models\Package;
|
|
|
|
|
use App\Models\Site;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
class InvoiceController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
|
|
|
|
public function index(Request $request): \Illuminate\Contracts\View\View
|
|
|
|
|
{
|
|
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder $invoiceModels */
|
2024-06-03 16:43:27 +02:00
|
|
|
$invoiceModels = Invoice::query()->orderByDesc('created_at');
|
2023-08-14 17:48:26 +00:00
|
|
|
$status = intval($request->status);
|
|
|
|
|
$invoiceModels = $invoiceModels->when($status, function ($query) use ($status) {
|
|
|
|
|
$query->where('status', $status);
|
|
|
|
|
});
|
2023-08-14 09:23:39 +00:00
|
|
|
|
|
|
|
|
/** @var string $search Search field */
|
|
|
|
|
$search = $request->search;
|
|
|
|
|
if ($search) {
|
|
|
|
|
$this->searchQuery(
|
|
|
|
|
$invoiceModels,
|
|
|
|
|
$search,
|
|
|
|
|
null,
|
|
|
|
|
'firstname',
|
|
|
|
|
'lastname',
|
2023-11-14 09:45:16 +01:00
|
|
|
'ref',
|
2024-06-03 16:57:47 +02:00
|
|
|
'site_id',
|
2023-08-14 09:23:39 +00:00
|
|
|
);
|
|
|
|
|
}
|
2024-06-03 16:57:47 +02:00
|
|
|
$searchFields = trans('Nom, Prénom, Référence, Site');
|
2023-08-14 09:23:39 +00:00
|
|
|
|
|
|
|
|
/** Sort columns with a query */
|
|
|
|
|
$this->sortQuery($invoiceModels);
|
|
|
|
|
|
|
|
|
|
/** Custom pagination */
|
|
|
|
|
$invoiceModels = $this->customPaginate($invoiceModels, $request->pagination);
|
|
|
|
|
|
2023-08-14 17:48:26 +00:00
|
|
|
return view('bo.pages.invoices.index', compact('invoiceModels', 'search', 'searchFields', 'status'));
|
2023-08-14 09:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show model action.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\Invoice $invoice
|
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
|
|
|
|
public function show(Invoice $invoice): \Illuminate\Contracts\View\View
|
|
|
|
|
{
|
|
|
|
|
return view('bo.pages.invoices.show', ['invoiceModel' => $invoice]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\Invoice $invoice
|
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
|
|
|
|
public function create(Invoice $invoice): \Illuminate\Contracts\View\View
|
|
|
|
|
{
|
|
|
|
|
$packageModels = Package::get();
|
|
|
|
|
$taskRowModel = Invoice::getTaskStructure();
|
|
|
|
|
$siteModels = Site::orderBy('name', 'ASC')->get();
|
|
|
|
|
$companyModels = Company::get();
|
|
|
|
|
|
|
|
|
|
return view('bo.pages.invoices.create', [
|
|
|
|
|
'invoiceModel' => $invoice,
|
|
|
|
|
'taskRowModel' => $taskRowModel,
|
|
|
|
|
'packageModels' => $packageModels,
|
|
|
|
|
'siteModels' => $siteModels,
|
|
|
|
|
'companyModels' => $companyModels,
|
2023-08-14 17:48:26 +00:00
|
|
|
'invoiceStatusArray' => $invoice->getInvoiceStatusArray($invoice),
|
2023-08-14 09:23:39 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Http\Requests\Bo\Invoices\StoreInvoiceRequest $request
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
public function store(StoreInvoiceRequest $request): \Illuminate\Http\RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
return DB::transaction(function () use ($request) {
|
|
|
|
|
$invoice = new Invoice();
|
|
|
|
|
$invoice->fill($request->validated());
|
|
|
|
|
|
|
|
|
|
if ($invoice->saveOrFail()) {
|
|
|
|
|
return redirect()
|
|
|
|
|
->route('bo.invoices.edit', $invoice)
|
|
|
|
|
->with('success', trans(
|
|
|
|
|
'crud.messages.has_been_created',
|
|
|
|
|
['model' => Str::singular(trans('models.classes.invoices'))]
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
return redirect()
|
|
|
|
|
->back()
|
|
|
|
|
->with('error', trans(
|
|
|
|
|
'crud.messages.cannot_be_created',
|
|
|
|
|
['model' => Str::singular(trans('models.classes.invoices'))]
|
|
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Edit model action.
|
|
|
|
|
*
|
|
|
|
|
* @param Invoice $invoice
|
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
|
|
|
|
public function edit(Invoice $invoice): \Illuminate\Contracts\View\View
|
|
|
|
|
{
|
|
|
|
|
$packageModels = Package::get();
|
|
|
|
|
$taskRowModel = Invoice::getTaskStructure();
|
|
|
|
|
$siteModels = Site::orderBy('name', 'ASC')->get();
|
|
|
|
|
$companyModels = Company::get();
|
|
|
|
|
|
|
|
|
|
return view('bo.pages.invoices.edit', [
|
|
|
|
|
'invoiceModel' => $invoice,
|
|
|
|
|
'taskRowModel' => $taskRowModel,
|
|
|
|
|
'packageModels' => $packageModels,
|
|
|
|
|
'siteModels' => $siteModels,
|
|
|
|
|
'companyModels' => $companyModels,
|
2023-08-14 17:48:26 +00:00
|
|
|
'invoiceStatusArray' => $invoice->getInvoiceStatusArray($invoice),
|
2023-08-14 09:23:39 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Http\Requests\Bo\Invoices\UpdateInvoiceRequest $request
|
|
|
|
|
* @param \App\Models\Invoice $invoice
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
public function update(UpdateInvoiceRequest $request, Invoice $invoice): \Illuminate\Http\RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
return DB::transaction(function () use ($request, $invoice) {
|
2023-08-14 17:48:26 +00:00
|
|
|
$invoice->fill(($invoice->status->value === InvoiceStatusEnum::opened->value) ? $request->validated() : []);
|
2023-08-14 09:23:39 +00:00
|
|
|
if ($invoice->saveOrFail()) {
|
|
|
|
|
return redirect()
|
|
|
|
|
->route('bo.invoices.edit', $invoice)
|
|
|
|
|
->with('success', __(
|
|
|
|
|
'crud.messages.has_been_updated',
|
2023-08-17 16:52:17 +02:00
|
|
|
['model' => __('models.classes.invoices')]
|
2023-08-14 09:23:39 +00:00
|
|
|
));
|
|
|
|
|
} //end if
|
|
|
|
|
return redirect()
|
|
|
|
|
->back()
|
|
|
|
|
->with('error', __(
|
|
|
|
|
'crud.messages.cannot_be_updated',
|
2023-08-17 16:52:17 +02:00
|
|
|
['model' => __('models.classes.invoices')]
|
2023-08-14 09:23:39 +00:00
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Download Invoice.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\Invoice $invoice
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\Response
|
|
|
|
|
*/
|
|
|
|
|
public function download(Invoice $invoice)
|
|
|
|
|
{
|
|
|
|
|
if (Gate::allows('isAdmin')) {
|
|
|
|
|
return $invoice->generateInvoice()
|
|
|
|
|
->stream(Str::slug(trans('Devis :appname :date - ref :ref', [
|
|
|
|
|
'appname' => config('app.name'),
|
2024-07-10 09:06:38 +00:00
|
|
|
'date' =>
|
|
|
|
|
$invoice->created_at !== $invoice->updated_at ?
|
|
|
|
|
$invoice->updated_at->isoFormat('YYYY-MM-DD') :
|
|
|
|
|
$invoice->created_at->isoFormat('YYYY-MM-DD'),
|
2023-08-14 09:23:39 +00:00
|
|
|
'ref' => $invoice->ref,
|
|
|
|
|
])) . '.pdf');
|
|
|
|
|
}
|
|
|
|
|
return redirect()->route('bo.invoices.index')
|
|
|
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
|
|
|
'model' => __('models.classes.invoices')
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a paginate resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return Illuminate\Http\JsonResponse
|
|
|
|
|
*/
|
|
|
|
|
public function jsonPaginate(Request $request): \Illuminate\Http\JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$model = $request->targetModel;
|
|
|
|
|
return DB::transaction(function () use ($model) {
|
2024-07-10 09:06:38 +00:00
|
|
|
$models = $model::query()->orderBy('name', 'ASC')->paginate(12);
|
2023-08-14 09:23:39 +00:00
|
|
|
return \response()->json($models ?? 500);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|