mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-19 03:04:05 +00:00
Remove app/Space folder and extract model business logic into services
Relocate all 14 files from the catch-all app/Space namespace into proper locations: data providers to Support/Formatters, installation utilities to Services/Installation, PDF utils to Services/Pdf, module/update classes to Services/Module and Services/Update, SiteApi trait to Traits, and helpers to Support. Extract ~1,400 lines of business logic from 8 fat models (Invoice, Payment, Estimate, RecurringInvoice, Company, Customer, Expense, User) into 9 new service classes with constructor injection. Controllers now depend on services instead of calling static model methods. Shared item/tax creation logic consolidated into DocumentItemService.
This commit is contained in:
120
app/Services/CustomerService.php
Normal file
120
app/Services/CustomerService.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Customer;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class CustomerService
|
||||
{
|
||||
public function create(Request $request): Customer
|
||||
{
|
||||
$customer = Customer::create($request->getCustomerPayload());
|
||||
|
||||
if ($request->shipping) {
|
||||
if ($request->hasAddress($request->shipping)) {
|
||||
$customer->addresses()->create($request->getShippingAddress());
|
||||
}
|
||||
}
|
||||
|
||||
if ($request->billing) {
|
||||
if ($request->hasAddress($request->billing)) {
|
||||
$customer->addresses()->create($request->getBillingAddress());
|
||||
}
|
||||
}
|
||||
|
||||
$customFields = $request->customFields;
|
||||
|
||||
if ($customFields) {
|
||||
$customer->addCustomFields($customFields);
|
||||
}
|
||||
|
||||
return Customer::with('billingAddress', 'shippingAddress', 'fields')->find($customer->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function update(Request $request, Customer $customer): Customer
|
||||
{
|
||||
$condition = $customer->estimates()->exists() || $customer->invoices()->exists() || $customer->payments()->exists() || $customer->recurringInvoices()->exists();
|
||||
|
||||
if (($customer->currency_id !== $request->currency_id) && $condition) {
|
||||
throw ValidationException::withMessages([
|
||||
'currency_id' => ['you_cannot_edit_currency'],
|
||||
]);
|
||||
}
|
||||
|
||||
$customer->update($request->getCustomerPayload());
|
||||
|
||||
$customer->addresses()->delete();
|
||||
|
||||
if ($request->shipping) {
|
||||
if ($request->hasAddress($request->shipping)) {
|
||||
$customer->addresses()->create($request->getShippingAddress());
|
||||
}
|
||||
}
|
||||
|
||||
if ($request->billing) {
|
||||
if ($request->hasAddress($request->billing)) {
|
||||
$customer->addresses()->create($request->getBillingAddress());
|
||||
}
|
||||
}
|
||||
|
||||
$customFields = $request->customFields;
|
||||
|
||||
if ($customFields) {
|
||||
$customer->updateCustomFields($customFields);
|
||||
}
|
||||
|
||||
return Customer::with('billingAddress', 'shippingAddress', 'fields')->find($customer->id);
|
||||
}
|
||||
|
||||
public function delete(Collection $ids): bool
|
||||
{
|
||||
foreach ($ids as $id) {
|
||||
$customer = Customer::find($id);
|
||||
|
||||
if ($customer->estimates()->exists()) {
|
||||
$customer->estimates()->delete();
|
||||
}
|
||||
|
||||
if ($customer->invoices()->exists()) {
|
||||
$customer->invoices->map(function ($invoice) {
|
||||
if ($invoice->transactions()->exists()) {
|
||||
$invoice->transactions()->delete();
|
||||
}
|
||||
$invoice->delete();
|
||||
});
|
||||
}
|
||||
|
||||
if ($customer->payments()->exists()) {
|
||||
$customer->payments()->delete();
|
||||
}
|
||||
|
||||
if ($customer->addresses()->exists()) {
|
||||
$customer->addresses()->delete();
|
||||
}
|
||||
|
||||
if ($customer->expenses()->exists()) {
|
||||
$customer->expenses()->delete();
|
||||
}
|
||||
|
||||
if ($customer->recurringInvoices()->exists()) {
|
||||
foreach ($customer->recurringInvoices as $recurringInvoice) {
|
||||
if ($recurringInvoice->items()->exists()) {
|
||||
$recurringInvoice->items()->delete();
|
||||
}
|
||||
|
||||
$recurringInvoice->delete();
|
||||
}
|
||||
}
|
||||
|
||||
$customer->delete();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user