mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-18 02:34:08 +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:
59
app/Services/ExpenseService.php
Normal file
59
app/Services/ExpenseService.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\CompanySetting;
|
||||
use App\Models\ExchangeRateLog;
|
||||
use App\Models\Expense;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ExpenseService
|
||||
{
|
||||
public function create(Request $request): Expense
|
||||
{
|
||||
$expense = Expense::create($request->getExpensePayload());
|
||||
|
||||
$companyCurrency = CompanySetting::getSetting('currency', $request->header('company'));
|
||||
|
||||
if ((string) $expense['currency_id'] !== $companyCurrency) {
|
||||
ExchangeRateLog::addExchangeRateLog($expense);
|
||||
}
|
||||
|
||||
if ($request->hasFile('attachment_receipt')) {
|
||||
$expense->addMediaFromRequest('attachment_receipt')->toMediaCollection('receipts');
|
||||
}
|
||||
|
||||
if ($request->customFields) {
|
||||
$expense->addCustomFields(json_decode($request->customFields));
|
||||
}
|
||||
|
||||
return $expense;
|
||||
}
|
||||
|
||||
public function update(Expense $expense, Request $request): bool
|
||||
{
|
||||
$data = $request->getExpensePayload();
|
||||
|
||||
$expense->update($data);
|
||||
|
||||
$companyCurrency = CompanySetting::getSetting('currency', $request->header('company'));
|
||||
|
||||
if ((string) $data['currency_id'] !== $companyCurrency) {
|
||||
ExchangeRateLog::addExchangeRateLog($expense);
|
||||
}
|
||||
|
||||
if (isset($request->is_attachment_receipt_removed) && (bool) $request->is_attachment_receipt_removed) {
|
||||
$expense->clearMediaCollection('receipts');
|
||||
}
|
||||
if ($request->hasFile('attachment_receipt')) {
|
||||
$expense->clearMediaCollection('receipts');
|
||||
$expense->addMediaFromRequest('attachment_receipt')->toMediaCollection('receipts');
|
||||
}
|
||||
|
||||
if ($request->customFields) {
|
||||
$expense->updateCustomFields(json_decode($request->customFields));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user