mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-27 19:51:09 -04:00
v5.0.0 update
This commit is contained in:
213
app/Traits/ExchangeRateProvidersTrait.php
Normal file
213
app/Traits/ExchangeRateProvidersTrait.php
Normal file
@ -0,0 +1,213 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Traits;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
trait ExchangeRateProvidersTrait
|
||||
{
|
||||
public function getExchangeRate($filter, $baseCurrencyCode, $currencyCode)
|
||||
{
|
||||
switch ($filter['driver']) {
|
||||
case 'currency_freak':
|
||||
$url = "https://api.currencyfreaks.com/latest?apikey=".$filter['key'];
|
||||
|
||||
$url = $url."&symbols={$currencyCode}"."&base={$baseCurrencyCode}";
|
||||
$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=".$filter['key']."&source={$baseCurrencyCode}¤cies={$currencyCode}";
|
||||
$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=".$filter['key']."&base={$baseCurrencyCode}&symbols={$currencyCode}";
|
||||
$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 = $this->getCurrencyConverterUrl($filter['driver_config']);
|
||||
$url = $url."/api/v7/convert?apiKey=".$filter['key'];
|
||||
|
||||
$query = "{$baseCurrencyCode}_{$currencyCode}";
|
||||
$url = $url."&q={$query}"."&compact=y";
|
||||
$response = Http::get($url)->json();
|
||||
|
||||
return response()->json([
|
||||
'exchangeRate' => array_values($response[$query]),
|
||||
], 200);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public 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;
|
||||
}
|
||||
}
|
||||
|
||||
public function getSupportedCurrencies($request)
|
||||
{
|
||||
$message = 'Please Enter Valid Provider Key.';
|
||||
$error = 'invalid_key';
|
||||
|
||||
$server_message = 'Server not responding';
|
||||
$error_message = 'server_error';
|
||||
|
||||
switch ($request->driver) {
|
||||
case 'currency_freak':
|
||||
$url = "https://api.currencyfreaks.com/currency-symbols";
|
||||
$response = Http::get($url)->json();
|
||||
$checkKey = $this->getUrl($request);
|
||||
|
||||
if ($response == null || $checkKey == null) {
|
||||
return respondJson($error_message, $server_message);
|
||||
}
|
||||
|
||||
if (array_key_exists('success', $checkKey) && array_key_exists('error', $checkKey)) {
|
||||
if ($checkKey['error']['status'] == 404) {
|
||||
return respondJson($error, $message);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json(['supportedCurrencies' => array_keys($response)]);
|
||||
|
||||
break;
|
||||
|
||||
case 'currency_layer':
|
||||
$url = "http://api.currencylayer.com/list?access_key=".$request->key;
|
||||
$response = Http::get($url)->json();
|
||||
|
||||
if ($response == null) {
|
||||
return respondJson($error_message, $server_message);
|
||||
}
|
||||
|
||||
if (array_key_exists('currencies', $response)) {
|
||||
return response()->json(['supportedCurrencies' => array_keys($response['currencies'])]);
|
||||
}
|
||||
|
||||
return respondJson($error, $message);
|
||||
|
||||
break;
|
||||
|
||||
case 'open_exchange_rate':
|
||||
$url = "https://openexchangerates.org/api/currencies.json";
|
||||
$response = Http::get($url)->json();
|
||||
$checkKey = $this->getUrl($request);
|
||||
|
||||
if ($response == null || $checkKey == null) {
|
||||
return respondJson($error_message, $server_message);
|
||||
}
|
||||
|
||||
if (array_key_exists('error', $checkKey)) {
|
||||
if ($checkKey['status'] == 401) {
|
||||
return respondJson($error, $message);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json(['supportedCurrencies' => array_keys($response)]);
|
||||
|
||||
break;
|
||||
|
||||
case 'currency_converter':
|
||||
$response = $this->getUrl($request);
|
||||
|
||||
if ($response == null) {
|
||||
return respondJson($error_message, $server_message);
|
||||
}
|
||||
|
||||
if (array_key_exists('results', $response)) {
|
||||
return response()->json(['supportedCurrencies' => array_keys($response['results'])]);
|
||||
}
|
||||
|
||||
return respondJson($error, $message);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public function getUrl($request)
|
||||
{
|
||||
switch ($request->driver) {
|
||||
case 'currency_freak':
|
||||
$url = "https://api.currencyfreaks.com/latest?apikey=".$request->key."&symbols=INR&base=USD";
|
||||
|
||||
return Http::get($url)->json();
|
||||
|
||||
break;
|
||||
|
||||
case 'currency_layer':
|
||||
$url = "http://api.currencylayer.com/live?access_key=".$request->key."&source=INR¤cies=USD";
|
||||
|
||||
return Http::get($url)->json();
|
||||
|
||||
break;
|
||||
|
||||
case 'open_exchange_rate':
|
||||
$url = "https://openexchangerates.org/api/latest.json?app_id=".$request->key."&base=INR&symbols=USD";
|
||||
|
||||
return Http::get($url)->json();
|
||||
|
||||
break;
|
||||
|
||||
case 'currency_converter':
|
||||
$url = $this->getCurrencyConverterUrl($request)."/api/v7/currencies?apiKey=".$request->key;
|
||||
|
||||
return Http::get($url)->json();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
app/Traits/GeneratesMenuTrait.php
Normal file
25
app/Traits/GeneratesMenuTrait.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Traits;
|
||||
|
||||
trait GeneratesMenuTrait
|
||||
{
|
||||
public function generateMenu($key, $user)
|
||||
{
|
||||
$menu = [];
|
||||
|
||||
foreach (\Menu::get($key)->items->toArray() as $data) {
|
||||
if ($user->checkAccess($data)) {
|
||||
$menu[] = [
|
||||
'title' => $data->title,
|
||||
'link' => $data->link->path['url'],
|
||||
'icon' => $data->data['icon'],
|
||||
'name' => $data->data['name'],
|
||||
'group' => $data->data['group'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $menu;
|
||||
}
|
||||
}
|
||||
@ -110,7 +110,7 @@ trait GeneratesPdfTrait
|
||||
|
||||
public function getFieldsArray()
|
||||
{
|
||||
$customer = $this->user;
|
||||
$customer = $this->customer;
|
||||
$shippingAddress = $customer->shippingAddress ?? new Address();
|
||||
$billingAddress = $customer->billingAddress ?? new Address();
|
||||
$companyAddress = $this->company->address ?? new Address();
|
||||
@ -148,7 +148,7 @@ trait GeneratesPdfTrait
|
||||
];
|
||||
|
||||
$customFields = $this->fields;
|
||||
$customerCustomFields = $this->user->fields;
|
||||
$customerCustomFields = $this->customer->fields;
|
||||
|
||||
foreach ($customFields as $customField) {
|
||||
$fields['{'.$customField->customField->slug.'}'] = $customField->defaultAnswer;
|
||||
|
||||
@ -11,9 +11,21 @@ trait HasCustomFieldsTrait
|
||||
return $this->morphMany('Crater\Models\CustomFieldValue', 'custom_field_valuable');
|
||||
}
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::deleting(function ($data) {
|
||||
if ($data->fields()->exists()) {
|
||||
$data->fields()->delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function addCustomFields($customFields)
|
||||
{
|
||||
foreach ($customFields as $field) {
|
||||
if (! is_array($field)) {
|
||||
$field = (array)$field;
|
||||
}
|
||||
$customField = CustomField::find($field['id']);
|
||||
|
||||
$customFieldValue = [
|
||||
@ -30,6 +42,10 @@ trait HasCustomFieldsTrait
|
||||
public function updateCustomFields($customFields)
|
||||
{
|
||||
foreach ($customFields as $field) {
|
||||
if (! is_array($field)) {
|
||||
$field = (array)$field;
|
||||
}
|
||||
|
||||
$customField = CustomField::find($field['id']);
|
||||
$customFieldValue = $this->fields()->firstOrCreate([
|
||||
'custom_field_id' => $customField->id,
|
||||
@ -45,8 +61,21 @@ trait HasCustomFieldsTrait
|
||||
|
||||
public function getCustomFieldBySlug($slug)
|
||||
{
|
||||
return $this->fields()->with('customField')->whereHas('customField', function ($query) use ($slug) {
|
||||
$query->where('slug', $slug);
|
||||
})->first();
|
||||
return $this->fields()
|
||||
->with('customField')
|
||||
->whereHas('customField', function ($query) use ($slug) {
|
||||
$query->where('slug', $slug);
|
||||
})->first();
|
||||
}
|
||||
|
||||
public function getCustomFieldValueBySlug($slug)
|
||||
{
|
||||
$value = $this->getCustomFieldBySlug($slug);
|
||||
|
||||
if ($value) {
|
||||
return $value->defaultAnswer;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user