mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 01:04:03 +00:00
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.
60 lines
1.8 KiB
PHP
60 lines
1.8 KiB
PHP
<?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;
|
|
}
|
|
}
|