mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-19 03:04:05 +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:
28
app/Services/CustomFieldService.php
Normal file
28
app/Services/CustomFieldService.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\CustomField;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CustomFieldService
|
||||
{
|
||||
public function create(Request $request): CustomField
|
||||
{
|
||||
$data = $request->validated();
|
||||
$data[getCustomFieldValueKey($request->type)] = $request->default_answer;
|
||||
$data['company_id'] = $request->header('company');
|
||||
$data['slug'] = clean_slug($request->model_type, $request->name);
|
||||
|
||||
return CustomField::create($data);
|
||||
}
|
||||
|
||||
public function update(CustomField $customField, Request $request): CustomField
|
||||
{
|
||||
$data = $request->validated();
|
||||
$data[getCustomFieldValueKey($request->type)] = $request->default_answer;
|
||||
$customField->update($data);
|
||||
|
||||
return $customField;
|
||||
}
|
||||
}
|
||||
116
app/Services/ExchangeRateProviderService.php
Normal file
116
app/Services/ExchangeRateProviderService.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Http\Requests\ExchangeRateProviderRequest;
|
||||
use App\Models\CompanySetting;
|
||||
use App\Models\ExchangeRateLog;
|
||||
use App\Models\ExchangeRateProvider;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class ExchangeRateProviderService
|
||||
{
|
||||
public function create(ExchangeRateProviderRequest $request): ExchangeRateProvider
|
||||
{
|
||||
return ExchangeRateProvider::create($request->getExchangeRateProviderPayload());
|
||||
}
|
||||
|
||||
public function update(ExchangeRateProvider $provider, ExchangeRateProviderRequest $request): ExchangeRateProvider
|
||||
{
|
||||
$provider->update($request->getExchangeRateProviderPayload());
|
||||
|
||||
return $provider;
|
||||
}
|
||||
|
||||
public function checkActiveCurrencies($request)
|
||||
{
|
||||
return ExchangeRateProvider::whereJsonContains('currencies', $request->currencies)
|
||||
->where('active', true)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function checkUpdateActiveCurrencies(ExchangeRateProvider $provider, $request)
|
||||
{
|
||||
return ExchangeRateProvider::where('active', $request->active)
|
||||
->where('id', '<>', $provider->id)
|
||||
->whereJsonContains('currencies', $request->currencies)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function checkProviderStatus($request)
|
||||
{
|
||||
switch ($request['driver']) {
|
||||
case 'currency_freak':
|
||||
$url = 'https://api.currencyfreaks.com/latest?apikey='.$request['key'].'&symbols=INR&base=USD';
|
||||
$response = Http::get($url)->json();
|
||||
|
||||
if (array_key_exists('success', $response)) {
|
||||
if ($response['success'] == false) {
|
||||
return respondJson($response['error']['message'], $response['error']['message']);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'exchangeRate' => array_values($response['rates']),
|
||||
], 200);
|
||||
|
||||
case 'currency_layer':
|
||||
$url = 'http://api.currencylayer.com/live?access_key='.$request['key'].'&source=INR¤cies=USD';
|
||||
$response = Http::get($url)->json();
|
||||
|
||||
if (array_key_exists('success', $response)) {
|
||||
if ($response['success'] == false) {
|
||||
return respondJson($response['error']['info'], $response['error']['info']);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'exchangeRate' => array_values($response['quotes']),
|
||||
], 200);
|
||||
|
||||
case 'open_exchange_rate':
|
||||
$url = 'https://openexchangerates.org/api/latest.json?app_id='.$request['key'].'&base=INR&symbols=USD';
|
||||
$response = Http::get($url)->json();
|
||||
|
||||
if (array_key_exists('error', $response)) {
|
||||
return respondJson($response['message'], $response['description']);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'exchangeRate' => array_values($response['rates']),
|
||||
], 200);
|
||||
|
||||
case 'currency_converter':
|
||||
$url = $this->getCurrencyConverterUrl($request['driver_config']);
|
||||
$url = $url.'/api/v7/convert?apiKey='.$request['key'];
|
||||
|
||||
$query = 'INR_USD';
|
||||
$url = $url."&q={$query}".'&compact=y';
|
||||
$response = Http::get($url)->json();
|
||||
|
||||
return response()->json([
|
||||
'exchangeRate' => array_values($response[$query]),
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
|
||||
public function addExchangeRateLog($model): ExchangeRateLog
|
||||
{
|
||||
return ExchangeRateLog::create([
|
||||
'exchange_rate' => $model->exchange_rate,
|
||||
'company_id' => $model->company_id,
|
||||
'base_currency_id' => $model->currency_id,
|
||||
'currency_id' => CompanySetting::getSetting('currency', $model->company_id),
|
||||
]);
|
||||
}
|
||||
|
||||
private function getCurrencyConverterUrl($data): string
|
||||
{
|
||||
return match ($data['type']) {
|
||||
'PREMIUM' => 'https://api.currconv.com',
|
||||
'PREPAID' => 'https://prepaid.currconv.com',
|
||||
'FREE' => 'https://free.currconv.com',
|
||||
'DEDICATED' => $data['url'],
|
||||
};
|
||||
}
|
||||
}
|
||||
85
app/Services/FileDiskService.php
Normal file
85
app/Services/FileDiskService.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\FileDisk;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class FileDiskService
|
||||
{
|
||||
public function create(Request $request): FileDisk
|
||||
{
|
||||
if ($request->set_as_default) {
|
||||
$this->clearDefaults();
|
||||
}
|
||||
|
||||
return FileDisk::create([
|
||||
'credentials' => $request->credentials,
|
||||
'name' => $request->name,
|
||||
'driver' => $request->driver,
|
||||
'set_as_default' => $request->set_as_default,
|
||||
'company_id' => $request->header('company'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(FileDisk $disk, Request $request): FileDisk
|
||||
{
|
||||
$data = [
|
||||
'credentials' => $request->credentials,
|
||||
'name' => $request->name,
|
||||
'driver' => $request->driver,
|
||||
];
|
||||
|
||||
if (! $disk->set_as_default) {
|
||||
if ($request->set_as_default) {
|
||||
$this->clearDefaults();
|
||||
}
|
||||
|
||||
$data['set_as_default'] = $request->set_as_default;
|
||||
}
|
||||
|
||||
$disk->update($data);
|
||||
|
||||
return $disk;
|
||||
}
|
||||
|
||||
public function setAsDefault(FileDisk $disk): FileDisk
|
||||
{
|
||||
$this->clearDefaults();
|
||||
|
||||
$disk->set_as_default = true;
|
||||
$disk->save();
|
||||
|
||||
return $disk;
|
||||
}
|
||||
|
||||
public function validateCredentials(array $credentials, string $driver): bool
|
||||
{
|
||||
FileDisk::setFilesystem(collect($credentials), $driver);
|
||||
|
||||
$prefix = env('DYNAMIC_DISK_PREFIX', 'temp_');
|
||||
|
||||
try {
|
||||
$root = '';
|
||||
if ($driver == 'dropbox') {
|
||||
$root = $credentials['root'].'/';
|
||||
}
|
||||
\Storage::disk($prefix.$driver)->put($root.'invoiceshelf_temp.text', 'Check Credentials');
|
||||
|
||||
if (\Storage::disk($prefix.$driver)->exists($root.'invoiceshelf_temp.text')) {
|
||||
\Storage::disk($prefix.$driver)->delete($root.'invoiceshelf_temp.text');
|
||||
|
||||
return true;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function clearDefaults(): void
|
||||
{
|
||||
FileDisk::query()->update(['set_as_default' => false]);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
30
app/Services/TransactionService.php
Normal file
30
app/Services/TransactionService.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Facades\Hashids;
|
||||
use App\Models\Transaction;
|
||||
|
||||
class TransactionService
|
||||
{
|
||||
public function create(array $data): Transaction
|
||||
{
|
||||
$transaction = Transaction::create($data);
|
||||
$transaction->unique_hash = Hashids::connection(Transaction::class)->encode($transaction->id);
|
||||
$transaction->save();
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
public function complete(Transaction $transaction): void
|
||||
{
|
||||
$transaction->status = Transaction::SUCCESS;
|
||||
$transaction->save();
|
||||
}
|
||||
|
||||
public function fail(Transaction $transaction): void
|
||||
{
|
||||
$transaction->status = Transaction::FAILED;
|
||||
$transaction->save();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user