Moved to _dev
This commit is contained in:
213
crater/app/Traits/ExchangeRateProvidersTrait.php
Normal file
213
crater/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
crater/app/Traits/GeneratesMenuTrait.php
Normal file
25
crater/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;
|
||||
}
|
||||
}
|
||||
184
crater/app/Traits/GeneratesPdfTrait.php
Normal file
184
crater/app/Traits/GeneratesPdfTrait.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Traits;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Crater\Models\Address;
|
||||
use Crater\Models\CompanySetting;
|
||||
use Crater\Models\FileDisk;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
||||
trait GeneratesPdfTrait
|
||||
{
|
||||
public function getGeneratedPDFOrStream($collection_name)
|
||||
{
|
||||
$pdf = $this->getGeneratedPDF($collection_name);
|
||||
if ($pdf && file_exists($pdf['path'])) {
|
||||
return response()->make(file_get_contents($pdf['path']), 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="'.$pdf['file_name'].'"',
|
||||
]);
|
||||
}
|
||||
|
||||
$locale = CompanySetting::getSetting('language', $this->company_id);
|
||||
|
||||
App::setLocale($locale);
|
||||
|
||||
$pdf = $this->getPDFData();
|
||||
|
||||
return response()->make($pdf->stream(), 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="'.$this[$collection_name.'_number'].'.pdf"',
|
||||
]);
|
||||
}
|
||||
|
||||
public function getGeneratedPDF($collection_name)
|
||||
{
|
||||
try {
|
||||
$media = $this->getMedia($collection_name)->first();
|
||||
|
||||
if ($media) {
|
||||
$file_disk = FileDisk::find($media->custom_properties['file_disk_id']);
|
||||
|
||||
if (! $file_disk) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$file_disk->setConfig();
|
||||
|
||||
$path = null;
|
||||
|
||||
if ($file_disk->driver == 'local') {
|
||||
$path = $media->getPath();
|
||||
} else {
|
||||
$path = $media->getTemporaryUrl(Carbon::now()->addMinutes(5));
|
||||
}
|
||||
|
||||
return collect([
|
||||
'path' => $path,
|
||||
'file_name' => $media->file_name,
|
||||
]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function generatePDF($collection_name, $file_name, $deleteExistingFile = false)
|
||||
{
|
||||
$save_pdf_to_disk = CompanySetting::getSetting('save_pdf_to_disk', $this->company_id);
|
||||
|
||||
if ($save_pdf_to_disk == 'NO') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$locale = CompanySetting::getSetting('language', $this->company_id);
|
||||
|
||||
App::setLocale($locale);
|
||||
|
||||
$pdf = $this->getPDFData();
|
||||
|
||||
\Storage::disk('local')->put('temp/'.$collection_name.'/'.$this->id.'/temp.pdf', $pdf->output());
|
||||
|
||||
if ($deleteExistingFile) {
|
||||
$this->clearMediaCollection($this->id);
|
||||
}
|
||||
|
||||
$file_disk = FileDisk::whereSetAsDefault(true)->first();
|
||||
|
||||
if ($file_disk) {
|
||||
$file_disk->setConfig();
|
||||
}
|
||||
|
||||
$media = \Storage::disk('local')->path('temp/'.$collection_name.'/'.$this->id.'/temp.pdf');
|
||||
|
||||
try {
|
||||
$this->addMedia($media)
|
||||
->withCustomProperties(['file_disk_id' => $file_disk->id])
|
||||
->usingFileName($file_name.'.pdf')
|
||||
->toMediaCollection($collection_name, config('filesystems.default'));
|
||||
|
||||
\Storage::disk('local')->deleteDirectory('temp/'.$collection_name.'/'.$this->id);
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function getFieldsArray()
|
||||
{
|
||||
$customer = $this->customer;
|
||||
$shippingAddress = $customer->shippingAddress ?? new Address();
|
||||
$billingAddress = $customer->billingAddress ?? new Address();
|
||||
$companyAddress = $this->company->address ?? new Address();
|
||||
|
||||
$fields = [
|
||||
'{SHIPPING_ADDRESS_NAME}' => $shippingAddress->name,
|
||||
'{SHIPPING_COUNTRY}' => $shippingAddress->country_name,
|
||||
'{SHIPPING_STATE}' => $shippingAddress->state,
|
||||
'{SHIPPING_CITY}' => $shippingAddress->city,
|
||||
'{SHIPPING_ADDRESS_STREET_1}' => $shippingAddress->address_street_1,
|
||||
'{SHIPPING_ADDRESS_STREET_2}' => $shippingAddress->address_street_2,
|
||||
'{SHIPPING_PHONE}' => $shippingAddress->phone,
|
||||
'{SHIPPING_ZIP_CODE}' => $shippingAddress->zip,
|
||||
'{BILLING_ADDRESS_NAME}' => $billingAddress->name,
|
||||
'{BILLING_COUNTRY}' => $billingAddress->country_name,
|
||||
'{BILLING_STATE}' => $billingAddress->state,
|
||||
'{BILLING_CITY}' => $billingAddress->city,
|
||||
'{BILLING_ADDRESS_STREET_1}' => $billingAddress->address_street_1,
|
||||
'{BILLING_ADDRESS_STREET_2}' => $billingAddress->address_street_2,
|
||||
'{BILLING_PHONE}' => $billingAddress->phone,
|
||||
'{BILLING_ZIP_CODE}' => $billingAddress->zip,
|
||||
'{COMPANY_NAME}' => $this->company->name,
|
||||
'{COMPANY_COUNTRY}' => $companyAddress->country_name,
|
||||
'{COMPANY_STATE}' => $companyAddress->state,
|
||||
'{COMPANY_CITY}' => $companyAddress->city,
|
||||
'{COMPANY_ADDRESS_STREET_1}' => $companyAddress->address_street_1,
|
||||
'{COMPANY_ADDRESS_STREET_2}' => $companyAddress->address_street_2,
|
||||
'{COMPANY_PHONE}' => $companyAddress->phone,
|
||||
'{COMPANY_ZIP_CODE}' => $companyAddress->zip,
|
||||
'{CONTACT_DISPLAY_NAME}' => $customer->name,
|
||||
'{PRIMARY_CONTACT_NAME}' => $customer->contact_name,
|
||||
'{CONTACT_EMAIL}' => $customer->email,
|
||||
'{CONTACT_PHONE}' => $customer->phone,
|
||||
'{CONTACT_WEBSITE}' => $customer->website,
|
||||
];
|
||||
|
||||
$customFields = $this->fields;
|
||||
$customerCustomFields = $this->customer->fields;
|
||||
|
||||
foreach ($customFields as $customField) {
|
||||
$fields['{'.$customField->customField->slug.'}'] = $customField->defaultAnswer;
|
||||
}
|
||||
|
||||
foreach ($customerCustomFields as $customField) {
|
||||
$fields['{'.$customField->customField->slug.'}'] = $customField->defaultAnswer;
|
||||
}
|
||||
|
||||
foreach ($fields as $key => $field) {
|
||||
$fields[$key] = htmlspecialchars($field, ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function getFormattedString($format)
|
||||
{
|
||||
$values = array_merge($this->getFieldsArray(), $this->getExtraFields());
|
||||
|
||||
$str = nl2br(strtr($format, $values));
|
||||
|
||||
$str = preg_replace('/{(.*?)}/', '', $str);
|
||||
|
||||
$str = preg_replace("/<[^\/>]*>([\s]?)*<\/[^>]*>/", '', $str);
|
||||
|
||||
$str = str_replace("<p>", "", $str);
|
||||
|
||||
$str = str_replace("</p>", "</br>", $str);
|
||||
|
||||
return $str;
|
||||
}
|
||||
}
|
||||
81
crater/app/Traits/HasCustomFieldsTrait.php
Normal file
81
crater/app/Traits/HasCustomFieldsTrait.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Traits;
|
||||
|
||||
use Crater\Models\CustomField;
|
||||
|
||||
trait HasCustomFieldsTrait
|
||||
{
|
||||
public function fields()
|
||||
{
|
||||
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 = [
|
||||
'type' => $customField->type,
|
||||
'custom_field_id' => $customField->id,
|
||||
'company_id' => $customField->company_id,
|
||||
getCustomFieldValueKey($customField->type) => $field['value'],
|
||||
];
|
||||
|
||||
$this->fields()->create($customFieldValue);
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
'type' => $customField->type,
|
||||
'company_id' => $this->company_id,
|
||||
]);
|
||||
|
||||
$type = getCustomFieldValueKey($customField->type);
|
||||
$customFieldValue->$type = $field['value'];
|
||||
$customFieldValue->save();
|
||||
}
|
||||
}
|
||||
|
||||
public function getCustomFieldBySlug($slug)
|
||||
{
|
||||
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