mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-18 18:54:07 +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:
@@ -135,114 +135,6 @@ class Customer extends Authenticatable implements HasMedia
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function deleteCustomers($ids)
|
||||
{
|
||||
foreach ($ids as $id) {
|
||||
$customer = self::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;
|
||||
}
|
||||
|
||||
public static function createCustomer($request)
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
$customer = Customer::with('billingAddress', 'shippingAddress', 'fields')->find($customer->id);
|
||||
|
||||
return $customer;
|
||||
}
|
||||
|
||||
public static function updateCustomer($request, $customer)
|
||||
{
|
||||
$condition = $customer->estimates()->exists() || $customer->invoices()->exists() || $customer->payments()->exists() || $customer->recurringInvoices()->exists();
|
||||
|
||||
if (($customer->currency_id !== $request->currency_id) && $condition) {
|
||||
return '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);
|
||||
}
|
||||
|
||||
$customer = Customer::with('billingAddress', 'shippingAddress', 'fields')->find($customer->id);
|
||||
|
||||
return $customer;
|
||||
}
|
||||
|
||||
public function scopePaginateData($query, $limit)
|
||||
{
|
||||
if ($limit == 'all') {
|
||||
|
||||
Reference in New Issue
Block a user