mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-17 02:04:03 +00:00
Remove dead model methods now handled by services
Remove createItem/updateItem from Item, createTransaction/ completeTransaction/failedTransaction from Transaction, createCustomField/updateCustomField from CustomField, all business methods from ExchangeRateProvider (CRUD + API checks + URL helpers), and validateCredentials/createDisk/updateDisk/updateDefaultDisks/ setAsDefaultDisk from FileDisk. All logic now lives in their respective service classes.
This commit is contained in:
@@ -104,23 +104,4 @@ class CustomField extends Model
|
||||
{
|
||||
$query->where('custom_fields.model_type', $type);
|
||||
}
|
||||
|
||||
public static function createCustomField($request)
|
||||
{
|
||||
$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 updateCustomField($request)
|
||||
{
|
||||
$data = $request->validated();
|
||||
$data[getCustomFieldValueKey($request->type)] = $request->default_answer;
|
||||
$this->update($data);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,9 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Http\Requests\ExchangeRateProviderRequest;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class ExchangeRateProvider extends Model
|
||||
{
|
||||
@@ -44,127 +42,4 @@ class ExchangeRateProvider extends Model
|
||||
{
|
||||
$query->where('exchange_rate_providers.company_id', request()->header('company'));
|
||||
}
|
||||
|
||||
public static function createFromRequest(ExchangeRateProviderRequest $request)
|
||||
{
|
||||
$exchangeRateProvider = self::create($request->getExchangeRateProviderPayload());
|
||||
|
||||
return $exchangeRateProvider;
|
||||
}
|
||||
|
||||
public function updateFromRequest(ExchangeRateProviderRequest $request)
|
||||
{
|
||||
$this->update($request->getExchangeRateProviderPayload());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function checkActiveCurrencies($request)
|
||||
{
|
||||
$query = ExchangeRateProvider::whereJsonContains('currencies', $request->currencies)
|
||||
->where('active', true)
|
||||
->get();
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function checkUpdateActiveCurrencies($request)
|
||||
{
|
||||
$query = ExchangeRateProvider::where('active', $request->active)
|
||||
->where('id', '<>', $this->id)
|
||||
->whereJsonContains('currencies', $request->currencies)
|
||||
->get();
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public static function checkExchangeRateProviderStatus($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);
|
||||
|
||||
break;
|
||||
|
||||
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);
|
||||
|
||||
break;
|
||||
|
||||
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);
|
||||
|
||||
break;
|
||||
|
||||
case 'currency_converter':
|
||||
$url = self::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);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getCurrencyConverterUrl($data)
|
||||
{
|
||||
switch ($data['type']) {
|
||||
case 'PREMIUM':
|
||||
return 'https://api.currconv.com';
|
||||
|
||||
break;
|
||||
|
||||
case 'PREPAID':
|
||||
return 'https://prepaid.currconv.com';
|
||||
|
||||
break;
|
||||
|
||||
case 'FREE':
|
||||
return 'https://free.currconv.com';
|
||||
|
||||
break;
|
||||
|
||||
case 'DEDICATED':
|
||||
return $data['url'];
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,92 +111,6 @@ class FileDisk extends Model
|
||||
config(['filesystems.disks.'.$prefix.$driver => $disks]);
|
||||
}
|
||||
|
||||
public static function validateCredentials($credentials, $disk)
|
||||
{
|
||||
$exists = false;
|
||||
|
||||
self::setFilesystem(collect($credentials), $disk);
|
||||
|
||||
$prefix = env('DYNAMIC_DISK_PREFIX', 'temp_');
|
||||
|
||||
try {
|
||||
$root = '';
|
||||
if ($disk == 'dropbox') {
|
||||
$root = $credentials['root'].'/';
|
||||
}
|
||||
\Storage::disk($prefix.$disk)->put($root.'invoiceshelf_temp.text', 'Check Credentials');
|
||||
|
||||
if (\Storage::disk($prefix.$disk)->exists($root.'invoiceshelf_temp.text')) {
|
||||
$exists = true;
|
||||
\Storage::disk($prefix.$disk)->delete($root.'invoiceshelf_temp.text');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$exists = false;
|
||||
}
|
||||
|
||||
return $exists;
|
||||
}
|
||||
|
||||
public static function createDisk($request)
|
||||
{
|
||||
if ($request->set_as_default) {
|
||||
self::updateDefaultDisks();
|
||||
}
|
||||
|
||||
$disk = self::create([
|
||||
'credentials' => $request->credentials,
|
||||
'name' => $request->name,
|
||||
'driver' => $request->driver,
|
||||
'set_as_default' => $request->set_as_default,
|
||||
'company_id' => $request->header('company'),
|
||||
]);
|
||||
|
||||
return $disk;
|
||||
}
|
||||
|
||||
public static function updateDefaultDisks()
|
||||
{
|
||||
$disks = self::get();
|
||||
|
||||
foreach ($disks as $disk) {
|
||||
$disk->set_as_default = false;
|
||||
$disk->save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateDisk($request)
|
||||
{
|
||||
$data = [
|
||||
'credentials' => $request->credentials,
|
||||
'name' => $request->name,
|
||||
'driver' => $request->driver,
|
||||
];
|
||||
|
||||
if (! $this->setAsDefault()) {
|
||||
if ($request->set_as_default) {
|
||||
self::updateDefaultDisks();
|
||||
}
|
||||
|
||||
$data['set_as_default'] = $request->set_as_default;
|
||||
}
|
||||
|
||||
$this->update($data);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAsDefaultDisk()
|
||||
{
|
||||
self::updateDefaultDisks();
|
||||
|
||||
$this->set_as_default = true;
|
||||
$this->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isSystem()
|
||||
{
|
||||
return $this->type === self::DISK_TYPE_SYSTEM;
|
||||
|
||||
@@ -7,7 +7,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class Item extends Model
|
||||
{
|
||||
@@ -135,45 +134,4 @@ class Item extends Model
|
||||
{
|
||||
return $this->hasMany(EstimateItem::class);
|
||||
}
|
||||
|
||||
public static function createItem($request)
|
||||
{
|
||||
$data = $request->validated();
|
||||
$data['company_id'] = $request->header('company');
|
||||
$data['creator_id'] = Auth::id();
|
||||
$company_currency = CompanySetting::getSetting('currency', $request->header('company'));
|
||||
$data['currency_id'] = $company_currency;
|
||||
$item = self::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);
|
||||
}
|
||||
}
|
||||
|
||||
$item = self::with('taxes')->find($item->id);
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public function updateItem($request)
|
||||
{
|
||||
$this->update($request->validated());
|
||||
|
||||
$this->taxes()->delete();
|
||||
|
||||
if ($request->has('taxes')) {
|
||||
foreach ($request->taxes as $tax) {
|
||||
$this->tax_per_item = true;
|
||||
$this->save();
|
||||
$tax['company_id'] = $request->header('company');
|
||||
$this->taxes()->create($tax);
|
||||
}
|
||||
}
|
||||
|
||||
return Item::with('taxes')->find($this->id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Facades\Hashids;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -42,27 +41,6 @@ class Transaction extends Model
|
||||
return $this->belongsTo(Company::class);
|
||||
}
|
||||
|
||||
public function completeTransaction()
|
||||
{
|
||||
$this->status = self::SUCCESS;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public function failedTransaction()
|
||||
{
|
||||
$this->status = self::FAILED;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public static function createTransaction($data)
|
||||
{
|
||||
$transaction = self::create($data);
|
||||
$transaction->unique_hash = Hashids::connection(Transaction::class)->encode($transaction->id);
|
||||
$transaction->save();
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
public function isExpired()
|
||||
{
|
||||
$linkExpiryDays = (int) CompanySetting::getSetting('link_expiry_days', $this->company_id);
|
||||
|
||||
Reference in New Issue
Block a user