mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 09:14:08 +00:00
V1/Admin -> Company (company-scoped controllers) V1/SuperAdmin -> Admin (platform-wide admin controllers) V1/Customer -> CustomerPortal (customer-facing portal) V1/Installation -> Setup (installation wizard) V1/PDF -> Pdf (consistent casing) V1/Modules -> Modules (drop V1 prefix) V1/Webhook -> Webhook (drop V1 prefix) The V1 prefix served no purpose - API versioning is in the route prefix (/api/v1/), not the controller namespace. "Admin" was misleading for company-scoped controllers. "SuperAdmin" is now simply "Admin" for platform administration.
107 lines
2.7 KiB
PHP
107 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Company\Customer;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests;
|
|
use App\Http\Requests\DeleteCustomersRequest;
|
|
use App\Http\Resources\CustomerResource;
|
|
use App\Models\Customer;
|
|
use App\Services\CustomerService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CustomersController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly CustomerService $customerService,
|
|
) {}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorize('viewAny', Customer::class);
|
|
|
|
$limit = $request->has('limit') ? $request->limit : 10;
|
|
|
|
$customers = Customer::with('creator')
|
|
->whereCompany()
|
|
->applyFilters($request->all())
|
|
->withSum('invoices as base_due_amount', 'base_due_amount')
|
|
->withSum('invoices as due_amount', 'due_amount')
|
|
->paginateData($limit);
|
|
|
|
return CustomerResource::collection($customers)
|
|
->additional(['meta' => [
|
|
'customer_total_count' => Customer::whereCompany()->count(),
|
|
]]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function store(Requests\CustomerRequest $request)
|
|
{
|
|
$this->authorize('create', Customer::class);
|
|
|
|
$customer = $this->customerService->create($request);
|
|
|
|
return new CustomerResource($customer);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
public function show(Customer $customer)
|
|
{
|
|
$this->authorize('view', $customer);
|
|
|
|
return new CustomerResource($customer);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function update(Requests\CustomerRequest $request, Customer $customer)
|
|
{
|
|
$this->authorize('update', $customer);
|
|
|
|
$customer = $this->customerService->update($request, $customer);
|
|
|
|
return new CustomerResource($customer);
|
|
}
|
|
|
|
/**
|
|
* Remove a list of Customers along side all their resources (ie. Estimates, Invoices, Payments and Addresses)
|
|
*
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function delete(DeleteCustomersRequest $request)
|
|
{
|
|
$this->authorize('delete multiple customers');
|
|
|
|
$ids = Customer::whereCompany()
|
|
->whereIn('id', $request->ids)
|
|
->pluck('id');
|
|
|
|
$this->customerService->delete($ids);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
]);
|
|
}
|
|
}
|