mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-16 17:54:06 +00:00
Extract business logic from remaining models to services
New services: - ExchangeRateProviderService: CRUD, API status checks, currency converter URL resolution (extracted 122 lines from ExchangeRateProvider model) - FileDiskService: create, update, setAsDefault, validateCredentials (extracted 97 lines from FileDisk model) - ItemService: create/update with tax handling (extracted from Item model) - TransactionService: create/complete/fail (extracted from Transaction model) - CustomFieldService: create/update with slug generation (extracted from CustomField model) Controllers updated to use constructor-injected services: ExchangeRateProviderController, DiskController, ItemsController, CustomFieldsController.
This commit is contained in:
49
app/Services/ItemService.php
Normal file
49
app/Services/ItemService.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\CompanySetting;
|
||||
use App\Models\Item;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ItemService
|
||||
{
|
||||
public function create(Request $request): Item
|
||||
{
|
||||
$data = $request->validated();
|
||||
$data['company_id'] = $request->header('company');
|
||||
$data['creator_id'] = Auth::id();
|
||||
$data['currency_id'] = CompanySetting::getSetting('currency', $request->header('company'));
|
||||
$item = Item::create($data);
|
||||
|
||||
if ($request->has('taxes')) {
|
||||
foreach ($request->taxes as $tax) {
|
||||
$item->tax_per_item = true;
|
||||
$item->save();
|
||||
$tax['company_id'] = $request->header('company');
|
||||
$item->taxes()->create($tax);
|
||||
}
|
||||
}
|
||||
|
||||
return Item::with('taxes')->find($item->id);
|
||||
}
|
||||
|
||||
public function update(Item $item, Request $request): Item
|
||||
{
|
||||
$item->update($request->validated());
|
||||
|
||||
$item->taxes()->delete();
|
||||
|
||||
if ($request->has('taxes')) {
|
||||
foreach ($request->taxes as $tax) {
|
||||
$item->tax_per_item = true;
|
||||
$item->save();
|
||||
$tax['company_id'] = $request->header('company');
|
||||
$item->taxes()->create($tax);
|
||||
}
|
||||
}
|
||||
|
||||
return Item::with('taxes')->find($item->id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user