mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Implement Currency Conversion library. (#3643)
* Fixes for testS * Fixes for migration * Fixes for migratin * Query performance improvements * Check Data Script * Currency Conversion API * Implement currency conversion * Currency Conversions
This commit is contained in:
parent
272109f699
commit
aa690578e3
386
app/Console/Commands/CheckData.php
Normal file
386
app/Console/Commands/CheckData.php
Normal file
@ -0,0 +1,386 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App;
|
||||
use App\Libraries\CurlUtils;
|
||||
use App\Models\Account;
|
||||
use App\Models\ClientContact;
|
||||
use App\Models\Contact;
|
||||
use App\Models\Invitation;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\InvoiceInvitation;
|
||||
use App\Utils\Ninja;
|
||||
use Carbon;
|
||||
use DB;
|
||||
use Exception;
|
||||
use Illuminate\Console\Command;
|
||||
use Mail;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Utils;
|
||||
|
||||
/*
|
||||
|
||||
##################################################################
|
||||
WARNING: Please backup your database before running this script
|
||||
##################################################################
|
||||
|
||||
If you have any questions please email us at contact@invoiceninja.com
|
||||
|
||||
Usage:
|
||||
|
||||
php artisan ninja:check-data
|
||||
|
||||
Options:
|
||||
|
||||
--client_id:<value>
|
||||
|
||||
Limits the script to a single client
|
||||
|
||||
--fix=true
|
||||
|
||||
By default the script only checks for errors, adding this option
|
||||
makes the script apply the fixes.
|
||||
|
||||
--fast=true
|
||||
|
||||
Skip using phantomjs
|
||||
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class CheckData.
|
||||
*/
|
||||
class CheckData extends Command
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'ninja:check-data';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Check/fix data';
|
||||
|
||||
protected $log = '';
|
||||
protected $isValid = true;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$this->logMessage(date('Y-m-d h:i:s') . ' Running CheckData...');
|
||||
|
||||
if ($database = $this->option('database')) {
|
||||
config(['database.default' => $database]);
|
||||
}
|
||||
|
||||
if (! $this->option('client_id')) {
|
||||
$this->checkPaidToDate();
|
||||
}
|
||||
|
||||
$this->checkInvoiceBalances();
|
||||
$this->checkClientBalances();
|
||||
$this->checkContacts();
|
||||
//$this->checkLogoFiles();
|
||||
|
||||
if (! $this->option('client_id')) {
|
||||
$this->checkOAuth();
|
||||
//$this->checkInvitations();
|
||||
|
||||
$this->checkFailedJobs();
|
||||
}
|
||||
|
||||
$this->logMessage('Done: ' . strtoupper($this->isValid ? Account::RESULT_SUCCESS : Account::RESULT_FAILURE));
|
||||
$errorEmail = config('ninja.error_email');
|
||||
|
||||
if ($errorEmail) {
|
||||
Mail::raw($this->log, function ($message) use ($errorEmail, $database) {
|
||||
$message->to($errorEmail)
|
||||
->from(config('ninja.error_email'))
|
||||
->subject("Check-Data: " . strtoupper($this->isValid ? Account::RESULT_SUCCESS : Account::RESULT_FAILURE) . " [{$database}]");
|
||||
});
|
||||
} elseif (! $this->isValid) {
|
||||
throw new Exception("Check data failed!!\n" . $this->log);
|
||||
}
|
||||
}
|
||||
|
||||
private function logMessage($str)
|
||||
{
|
||||
$str = date('Y-m-d h:i:s') . ' ' . $str;
|
||||
$this->info($str);
|
||||
$this->log .= $str . "\n";
|
||||
}
|
||||
|
||||
private function checkOAuth()
|
||||
{
|
||||
// check for duplicate oauth ids
|
||||
$users = DB::table('users')
|
||||
->whereNotNull('oauth_user_id')
|
||||
->groupBy('users.oauth_user_id')
|
||||
->havingRaw('count(users.id) > 1')
|
||||
->get(['users.oauth_user_id']);
|
||||
|
||||
$this->logMessage($users->count() . ' users with duplicate oauth ids');
|
||||
|
||||
if ($users->count() > 0) {
|
||||
$this->isValid = false;
|
||||
}
|
||||
|
||||
if ($this->option('fix') == 'true') {
|
||||
foreach ($users as $user) {
|
||||
$first = true;
|
||||
$this->logMessage('checking ' . $user->oauth_user_id);
|
||||
$matches = DB::table('users')
|
||||
->where('oauth_user_id', '=', $user->oauth_user_id)
|
||||
->orderBy('id')
|
||||
->get(['id']);
|
||||
|
||||
foreach ($matches as $match) {
|
||||
if ($first) {
|
||||
$this->logMessage('skipping ' . $match->id);
|
||||
$first = false;
|
||||
continue;
|
||||
}
|
||||
$this->logMessage('updating ' . $match->id);
|
||||
|
||||
DB::table('users')
|
||||
->where('id', '=', $match->id)
|
||||
->where('oauth_user_id', '=', $user->oauth_user_id)
|
||||
->update([
|
||||
'oauth_user_id' => null,
|
||||
'oauth_provider_id' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function checkContacts()
|
||||
{
|
||||
// check for contacts with the contact_key value set
|
||||
$contacts = DB::table('client_contacts')
|
||||
->whereNull('contact_key')
|
||||
->orderBy('id')
|
||||
->get(['id']);
|
||||
$this->logMessage($contacts->count() . ' contacts without a contact_key');
|
||||
|
||||
if ($contacts->count() > 0) {
|
||||
$this->isValid = false;
|
||||
}
|
||||
|
||||
if ($this->option('fix') == 'true') {
|
||||
foreach ($contacts as $contact) {
|
||||
DB::table('client_contacts')
|
||||
->where('id', '=', $contact->id)
|
||||
->whereNull('contact_key')
|
||||
->update([
|
||||
'contact_key' => str_random(config('ninja.key_length')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// check for missing contacts
|
||||
$clients = DB::table('clients')
|
||||
->leftJoin('client_contacts', function($join) {
|
||||
$join->on('client_contacts.client_id', '=', 'clients.id')
|
||||
->whereNull('client_contacts.deleted_at');
|
||||
})
|
||||
->groupBy('clients.id', 'clients.user_id', 'clients.company_id')
|
||||
->havingRaw('count(client_contacts.id) = 0');
|
||||
|
||||
if ($this->option('client_id')) {
|
||||
$clients->where('clients.id', '=', $this->option('client_id'));
|
||||
}
|
||||
|
||||
$clients = $clients->get(['clients.id', 'clients.user_id', 'clients.company_id']);
|
||||
$this->logMessage($clients->count() . ' clients without any contacts');
|
||||
|
||||
if ($clients->count() > 0) {
|
||||
$this->isValid = false;
|
||||
}
|
||||
|
||||
if ($this->option('fix') == 'true') {
|
||||
foreach ($clients as $client) {
|
||||
$contact = new ClientContact();
|
||||
$contact->company_id = $client->company_id;
|
||||
$contact->user_id = $client->user_id;
|
||||
$contact->client_id = $client->id;
|
||||
$contact->is_primary = true;
|
||||
$contact->send_invoice = true;
|
||||
$contact->contact_key = str_random(config('ninja.key_length'));
|
||||
$contact->save();
|
||||
}
|
||||
}
|
||||
|
||||
// check for more than one primary contact
|
||||
$clients = DB::table('clients')
|
||||
->leftJoin('client_contacts', function($join) {
|
||||
$join->on('client_contacts.client_id', '=', 'clients.id')
|
||||
->where('client_contacts.is_primary', '=', true)
|
||||
->whereNull('client_contacts.deleted_at');
|
||||
})
|
||||
->groupBy('clients.id')
|
||||
->havingRaw('count(client_contacts.id) != 1');
|
||||
|
||||
if ($this->option('client_id')) {
|
||||
$clients->where('clients.id', '=', $this->option('client_id'));
|
||||
}
|
||||
|
||||
$clients = $clients->get(['clients.id', DB::raw('count(client_contacts.id)')]);
|
||||
$this->logMessage($clients->count() . ' clients without a single primary contact');
|
||||
|
||||
if ($clients->count() > 0) {
|
||||
$this->isValid = false;
|
||||
}
|
||||
}
|
||||
|
||||
private function checkFailedJobs()
|
||||
{
|
||||
if (config('ninja.testvars.travis')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$queueDB = config('queue.connections.database.connection');
|
||||
$count = DB::connection($queueDB)->table('failed_jobs')->count();
|
||||
|
||||
if ($count > 25) {
|
||||
$this->isValid = false;
|
||||
}
|
||||
|
||||
$this->logMessage($count . ' failed jobs');
|
||||
}
|
||||
|
||||
private function checkInvitations()
|
||||
{
|
||||
$invoices = DB::table('invoices')
|
||||
->leftJoin('invoice_invitations', function ($join) {
|
||||
$join->on('invoice_invitations.invoice_id', '=', 'invoices.id')
|
||||
->whereNull('invoice_invitations.deleted_at');
|
||||
})
|
||||
->groupBy('invoices.id', 'invoices.user_id', 'invoices.company_id', 'invoices.client_id')
|
||||
->havingRaw('count(invoice_invitations.id) = 0')
|
||||
->get(['invoices.id', 'invoices.user_id', 'invoices.company_id', 'invoices.client_id']);
|
||||
|
||||
$this->logMessage($invoices->count() . ' invoices without any invitations');
|
||||
|
||||
if ($invoices->count() > 0) {
|
||||
$this->isValid = false;
|
||||
}
|
||||
|
||||
if ($this->option('fix') == 'true') {
|
||||
foreach ($invoices as $invoice) {
|
||||
$invitation = new InvoiceInvitation();
|
||||
$invitation->company_id = $invoice->company_id;
|
||||
$invitation->user_id = $invoice->user_id;
|
||||
$invitation->invoice_id = $invoice->id;
|
||||
$invitation->contact_id = ClientContact::whereClientId($invoice->client_id)->whereIsPrimary(true)->first()->id;
|
||||
$invitation->invitation_key = str_random(config('ninja.key_length'));
|
||||
$invitation->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function checkPaidToDate()
|
||||
{
|
||||
//Check the client paid to date value matches the sum of payments by the client
|
||||
}
|
||||
|
||||
private function checkInvoiceBalances()
|
||||
{
|
||||
// $invoices = DB::table('invoices')
|
||||
// ->leftJoin('payments', function($join) {
|
||||
// $join->on('payments.invoice_id', '=', 'invoices.id')
|
||||
// ->where('payments.payment_status_id', '!=', 2)
|
||||
// ->where('payments.payment_status_id', '!=', 3)
|
||||
// ->where('payments.is_deleted', '=', 0);
|
||||
// })
|
||||
// ->where('invoices.updated_at', '>', '2017-10-01')
|
||||
// ->groupBy('invoices.id')
|
||||
// ->havingRaw('(invoices.amount - invoices.balance) != coalesce(sum(payments.amount - payments.refunded), 0)')
|
||||
// ->get(['invoices.id', 'invoices.amount', 'invoices.balance', DB::raw('coalesce(sum(payments.amount - payments.refunded), 0)')]);
|
||||
|
||||
// $this->logMessage($invoices->count() . ' invoices with incorrect balances');
|
||||
|
||||
// if ($invoices->count() > 0) {
|
||||
// $this->isValid = false;
|
||||
// }
|
||||
}
|
||||
|
||||
private function checkClientBalances()
|
||||
{
|
||||
// find all clients where the balance doesn't equal the sum of the outstanding invoices
|
||||
// $clients = DB::table('clients')
|
||||
// ->join('invoices', 'invoices.client_id', '=', 'clients.id')
|
||||
// ->join('accounts', 'accounts.id', '=', 'clients.company_id')
|
||||
// ->where('accounts.id', '!=', 20432)
|
||||
// ->where('clients.is_deleted', '=', 0)
|
||||
// ->where('invoices.is_deleted', '=', 0)
|
||||
// ->where('invoices.is_public', '=', 1)
|
||||
// ->where('invoices.invoice_type_id', '=', INVOICE_TYPE_STANDARD)
|
||||
// ->where('invoices.is_recurring', '=', 0)
|
||||
// ->havingRaw('abs(clients.balance - sum(invoices.balance)) > .01 and clients.balance != 999999999.9999');
|
||||
|
||||
// if ($this->option('client_id')) {
|
||||
// $clients->where('clients.id', '=', $this->option('client_id'));
|
||||
// }
|
||||
|
||||
// $clients = $clients->groupBy('clients.id', 'clients.balance')
|
||||
// ->orderBy('accounts.company_id', 'DESC')
|
||||
// ->get(['accounts.company_id', 'clients.company_id', 'clients.id', 'clients.balance', 'clients.paid_to_date', DB::raw('sum(invoices.balance) actual_balance')]);
|
||||
// $this->logMessage($clients->count() . ' clients with incorrect balance/activities');
|
||||
|
||||
// if ($clients->count() > 0) {
|
||||
// $this->isValid = false;
|
||||
// }
|
||||
|
||||
// foreach ($clients as $client) {
|
||||
// $this->logMessage("=== Company: {$client->company_id} Account:{$client->company_id} Client:{$client->id} Balance:{$client->balance} Actual Balance:{$client->actual_balance} ===");
|
||||
|
||||
//}
|
||||
}
|
||||
|
||||
private function checkLogoFiles()
|
||||
{
|
||||
// $accounts = DB::table('accounts')
|
||||
// ->where('logo', '!=', '')
|
||||
// ->orderBy('id')
|
||||
// ->get(['logo']);
|
||||
|
||||
// $countMissing = 0;
|
||||
|
||||
// foreach ($accounts as $account) {
|
||||
// $path = public_path('logo/' . $account->logo);
|
||||
// if (! file_exists($path)) {
|
||||
// $this->logMessage('Missing file: ' . $account->logo);
|
||||
// $countMissing++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if ($countMissing > 0) {
|
||||
// $this->isValid = false;
|
||||
// }
|
||||
|
||||
// $this->logMessage($countMissing . ' missing logo files');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getArguments()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return [
|
||||
['fix', null, InputOption::VALUE_OPTIONAL, 'Fix data', null],
|
||||
['fast', null, InputOption::VALUE_OPTIONAL, 'Fast', null],
|
||||
['client_id', null, InputOption::VALUE_OPTIONAL, 'Client id', null],
|
||||
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
|
||||
];
|
||||
}
|
||||
}
|
@ -107,8 +107,10 @@ class ImportMigrations extends Command
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$account->default_company_id = $company->id;
|
||||
$account->save();
|
||||
if(!$account->default_company_id){
|
||||
$account->default_company_id = $company->id;
|
||||
$account->save();
|
||||
}
|
||||
|
||||
return $company;
|
||||
}
|
||||
|
@ -26,7 +26,6 @@ class InvoiceEmail extends EmailBuilder
|
||||
|
||||
$body_template = $client->getSetting('email_template_' . $reminder_template);
|
||||
|
||||
|
||||
/* Use default translations if a custom message has not been set*/
|
||||
if (iconv_strlen($body_template) == 0) {
|
||||
$body_template = trans(
|
||||
|
@ -258,9 +258,13 @@ class BaseController extends Controller
|
||||
'company.company_gateways.gateway',
|
||||
'company.clients.contacts',
|
||||
'company.products',
|
||||
'company.invoices.invitations.contact',
|
||||
'company.invoices.invitations.company',
|
||||
'company.invoices.documents',
|
||||
'company.payments.paymentables',
|
||||
'company.quotes.invitations.contact',
|
||||
'company.quotes.invitations.company',
|
||||
'company.credits.invitations.contact',
|
||||
'company.credits.invitations.company',
|
||||
'company.vendors.contacts',
|
||||
'company.expenses',
|
||||
|
@ -694,10 +694,12 @@ class InvoiceController extends BaseController
|
||||
|
||||
$this->reminder_template = $invoice->calculateTemplate();
|
||||
|
||||
$invoice->invitations->each(function ($invitation) use ($invoice) {
|
||||
$invoice->invitations->load('contact.client.country','invoice.client.country','invoice.company')->each(function ($invitation) use ($invoice) {
|
||||
|
||||
$email_builder = (new InvoiceEmail())->build($invitation, $this->reminder_template);
|
||||
|
||||
EmailInvoice::dispatch($email_builder, $invitation, $invoice->company);
|
||||
|
||||
});
|
||||
|
||||
if ($invoice->invitations->count() > 0) {
|
||||
|
@ -181,6 +181,7 @@ class MigrationController extends BaseController
|
||||
public function startMigration(Request $request, Company $company)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
$existing_company = Company::where('company_key', $request->company_key)->first();
|
||||
|
||||
if ($request->company_key !== $company->company_key) {
|
||||
@ -198,7 +199,8 @@ class MigrationController extends BaseController
|
||||
return;
|
||||
}
|
||||
|
||||
$account = (new ImportMigrations())->getAccount();
|
||||
$account = auth()->user()->account;
|
||||
//$account = (new ImportMigrations())->getAccount();
|
||||
$company = (new ImportMigrations())->getCompany($account);
|
||||
|
||||
$company_token = new CompanyToken();
|
||||
|
@ -50,8 +50,9 @@ class QueryLogging
|
||||
|
||||
Log::info($request->method() . ' - ' . $request->url() . ": $count queries - " . $time);
|
||||
|
||||
if($count > 50)
|
||||
Log::info($queries);
|
||||
// if($count > 50)
|
||||
// Log::info($queries);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -79,7 +79,7 @@ class CreateCreditPdf implements ShouldQueue
|
||||
|
||||
$designer = new Designer($this->credit, $design, $this->credit->client->getSetting('pdf_variables'), 'credit');
|
||||
|
||||
$html = (new HtmlEngine($designer, $invitation, 'credit'))->build();
|
||||
$html = (new HtmlEngine($designer, $this->invitation, 'credit'))->build();
|
||||
|
||||
Storage::makeDirectory($path, 0755);
|
||||
|
||||
|
@ -55,7 +55,7 @@ class EmailInvoice implements ShouldQueue
|
||||
public function handle()
|
||||
{
|
||||
MultiDB::setDB($this->company->db);
|
||||
|
||||
|
||||
Mail::to($this->invoice_invitation->contact->email, $this->invoice_invitation->contact->present()->name())
|
||||
->send(
|
||||
new TemplateEmail(
|
||||
|
53
app/Libraries/Currency/Conversion/CurrencyApi.php
Normal file
53
app/Libraries/Currency/Conversion/CurrencyApi.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com)
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Libraries\Currency\Conversion;
|
||||
|
||||
use App\Models\Currency;
|
||||
use AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class CurrencyApi implements CurrencyConversionInterface
|
||||
{
|
||||
|
||||
public function convert($amount, $from_currency_id, $to_currency_id, $date = null)
|
||||
{
|
||||
|
||||
if(!$date)
|
||||
$date = Carbon::now();
|
||||
|
||||
$from_currency = Currency::find($from_currency_id)->code;
|
||||
|
||||
$to_currency = Currency::find($to_currency_id)->code;
|
||||
|
||||
$exchangeRates = new ExchangeRate();
|
||||
|
||||
return $exchangeRates->convert($amount, $from_currency, $to_currency, $date);
|
||||
|
||||
}
|
||||
|
||||
public function exchangeRate($from_currency_id, $to_currency_id, $date = null)
|
||||
{
|
||||
|
||||
if(!$date)
|
||||
$date = Carbon::now();
|
||||
|
||||
$from_currency = Currency::find($from_currency_id)->code;
|
||||
|
||||
$to_currency = Currency::find($to_currency_id)->code;
|
||||
|
||||
$exchangeRates = new ExchangeRate();
|
||||
|
||||
return $exchangeRates->exchangeRate($from_currency, $to_currency, $date);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com)
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Libraries\Currency\Conversion;
|
||||
|
||||
interface CurrencyConversionInterface
|
||||
{
|
||||
|
||||
public function convert($amount, $from_currency_id, $to_currency_id, $date = null);
|
||||
|
||||
public function exchangeRate($from_currency_id, $to_currency_id, $date = null);
|
||||
|
||||
}
|
@ -35,6 +35,6 @@ class CreateInvoicePdf implements ShouldQueue
|
||||
*/
|
||||
public function handle($event)
|
||||
{
|
||||
PdfCreator::dispatch($event->invoice, $event->company, $event->invoice->client->primary_contact()->first());
|
||||
PdfCreator::dispatch($invoice->invitations->first());
|
||||
}
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ class Client extends BaseModel implements HasLocalePreference
|
||||
|
||||
public function primary_contact()
|
||||
{
|
||||
return $this->hasMany(ClientContact::class)->whereIsPrimary(true);
|
||||
return $this->hasMany(ClientContact::class)->where('is_primary', true);
|
||||
}
|
||||
|
||||
public function company()
|
||||
@ -190,7 +190,13 @@ class Client extends BaseModel implements HasLocalePreference
|
||||
|
||||
public function language()
|
||||
{
|
||||
return Language::find($this->getSetting('language_id'));
|
||||
//return Language::find($this->getSetting('language_id'));
|
||||
|
||||
$languages = Cache::get('languages');
|
||||
|
||||
return $languages->filter(function ($item) {
|
||||
return $item->id == $this->getSetting('language_id');
|
||||
})->first();
|
||||
}
|
||||
|
||||
public function locale()
|
||||
@ -292,7 +298,9 @@ class Client extends BaseModel implements HasLocalePreference
|
||||
return $this->company->settings->{$setting};
|
||||
}
|
||||
|
||||
throw new \Exception("Settings corrupted", 1);
|
||||
return '';
|
||||
|
||||
// throw new \Exception("Settings corrupted", 1);
|
||||
}
|
||||
|
||||
public function getSettingEntity($setting)
|
||||
|
@ -138,15 +138,13 @@ class ClientContact extends Authenticatable implements HasLocalePreference
|
||||
|
||||
public function preferredLocale()
|
||||
{
|
||||
|
||||
$languages = Cache::get('languages');
|
||||
|
||||
return $languages->filter(function ($item) {
|
||||
return $item->id == $this->client->getSetting('language_id');
|
||||
})->first()->locale;
|
||||
|
||||
//$lang = Language::find($this->client->getSetting('language_id'));
|
||||
|
||||
//return $lang->locale;
|
||||
}
|
||||
|
||||
public function routeNotificationForMail($notification)
|
||||
|
@ -14,6 +14,7 @@ namespace App\Repositories;
|
||||
use App\Events\Payment\PaymentWasCreated;
|
||||
use App\Factory\CreditFactory;
|
||||
use App\Jobs\Credit\ApplyCreditPayment;
|
||||
use App\Libraries\Currency\Conversion\CurrencyApi;
|
||||
use App\Models\Client;
|
||||
use App\Models\Credit;
|
||||
use App\Models\Invoice;
|
||||
@ -21,6 +22,7 @@ use App\Models\Payment;
|
||||
use App\Repositories\CreditRepository;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* PaymentRepository
|
||||
@ -192,6 +194,7 @@ class PaymentRepository extends BaseRepository
|
||||
*/
|
||||
private function processExchangeRates($data, $payment)
|
||||
{
|
||||
|
||||
$client = Client::find($data['client_id']);
|
||||
|
||||
$client_currency = $client->getSetting('currency_id');
|
||||
@ -200,7 +203,9 @@ class PaymentRepository extends BaseRepository
|
||||
if ($company_currency != $client_currency) {
|
||||
$currency = $client->currency();
|
||||
|
||||
$payment->exchange_rate = $currency->exchange_rate;
|
||||
$exchange_rate = new CurrencyApi();
|
||||
|
||||
$payment->exchange_rate = $exchange_rate->exchangeRate($client_currency, $company_currency, Carbon::parse($payment->date));
|
||||
$payment->exchange_currency_id = $client_currency;
|
||||
}
|
||||
|
||||
|
@ -48,10 +48,6 @@ class CompanyUserTransformer extends EntityTransformer
|
||||
public function transform(CompanyUser $company_user)
|
||||
{
|
||||
return [
|
||||
// 'id' => $company_user->id,
|
||||
// 'account_id' => $company_user->account_id,
|
||||
// 'user_id' => $company_user->user_id,
|
||||
// 'company_id' => $company_user->company_id,
|
||||
'permissions' => $company_user->permissions ?: '',
|
||||
'notifications' => (object)$company_user->notifications,
|
||||
'settings' => (object)$company_user->settings,
|
||||
|
@ -59,8 +59,7 @@ class HtmlEngine
|
||||
}
|
||||
|
||||
public function build() :string
|
||||
{
|
||||
|
||||
{
|
||||
App::setLocale($this->client->preferredLocale());
|
||||
|
||||
$values_and_labels = $this->generateLabelsAndValues();
|
||||
|
@ -80,20 +80,38 @@ trait CompanyGatewayFeesAndLimitsSaver
|
||||
}
|
||||
}
|
||||
|
||||
// public function cleanFeesAndLimits($fees_and_limits)
|
||||
// {
|
||||
// $new_arr = [];
|
||||
|
||||
// foreach ($fees_and_limits as $key => $value) {
|
||||
// $fal = new FeesAndLimits;
|
||||
|
||||
// foreach ($value as $k => $v) {
|
||||
// $fal->{$k} = $v;
|
||||
// $fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
|
||||
// }
|
||||
|
||||
// $new_arr[$key] = (array)$fal;
|
||||
// }
|
||||
|
||||
// return $new_arr;
|
||||
// }
|
||||
//
|
||||
public function cleanFeesAndLimits($fees_and_limits)
|
||||
{
|
||||
$new_arr = [];
|
||||
|
||||
|
||||
foreach ($fees_and_limits as $key => $value) {
|
||||
$fal = new FeesAndLimits;
|
||||
|
||||
foreach ($value as $k => $v) {
|
||||
$fal->{$k} = $v;
|
||||
$fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
|
||||
}
|
||||
$fal->{$key} = $value;
|
||||
// foreach ($value as $k => $v) {
|
||||
// $fal->{$k} = $v;
|
||||
// $fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
|
||||
// }
|
||||
|
||||
$new_arr[$key] = (array)$fal;
|
||||
// $new_arr[$key] = (array)$fal;
|
||||
}
|
||||
|
||||
return $new_arr;
|
||||
|
@ -21,6 +21,7 @@
|
||||
"php": ">=7.3",
|
||||
"ext-json": "*",
|
||||
"asgrim/ofxparser": "^1.2",
|
||||
"ashallendesign/laravel-exchange-rates": "^2.1",
|
||||
"cleverit/ubl_invoice": "^1.3",
|
||||
"codedge/laravel-selfupdater": "2.5.1",
|
||||
"composer/composer": "^1.10",
|
||||
|
@ -1250,4 +1250,73 @@ class PaymentTest extends TestCase
|
||||
$this->assertEquals(1, $payment->invoices()->count());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function testStorePaymentExchangeRate()
|
||||
{
|
||||
$settings = ClientSettings::defaults();
|
||||
$settings->currency_id = "2";
|
||||
|
||||
$client = ClientFactory::create($this->company->id, $this->user->id);
|
||||
$client->settings = $settings;
|
||||
$client->save();
|
||||
|
||||
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id);//stub the company and user_id
|
||||
$this->invoice->client_id = $client->id;
|
||||
$this->invoice->status_id = Invoice::STATUS_SENT;
|
||||
|
||||
$this->invoice->line_items = $this->buildLineItems();
|
||||
$this->invoice->uses_inclusive_Taxes = false;
|
||||
|
||||
$this->invoice->save();
|
||||
|
||||
$this->invoice_calc = new InvoiceSum($this->invoice);
|
||||
$this->invoice_calc->build();
|
||||
|
||||
$this->invoice = $this->invoice_calc->getInvoice();
|
||||
$this->invoice->save();
|
||||
|
||||
$data = [
|
||||
'amount' => $this->invoice->amount,
|
||||
'client_id' => $client->hashed_id,
|
||||
'invoices' => [
|
||||
[
|
||||
'invoice_id' => $this->invoice->hashed_id,
|
||||
'amount' => $this->invoice->amount
|
||||
],
|
||||
],
|
||||
'date' => '2020/12/12',
|
||||
|
||||
];
|
||||
|
||||
$response = null;
|
||||
|
||||
try {
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])->post('/api/v1/payments?include=invoices', $data);
|
||||
} catch (ValidationException $e) {
|
||||
$message = json_decode($e->validator->getMessageBag(), 1);
|
||||
$this->assertNotNull($message);
|
||||
}
|
||||
|
||||
if ($response) {
|
||||
$arr = $response->json();
|
||||
$response->assertStatus(200);
|
||||
|
||||
$payment_id = $arr['data']['id'];
|
||||
|
||||
$payment = Payment::find($this->decodePrimaryKey($payment_id))->first();
|
||||
|
||||
info($payment);
|
||||
|
||||
$this->assertNotNull($payment);
|
||||
$this->assertNotNull($payment->invoices());
|
||||
$this->assertEquals(1, $payment->invoices()->count());
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -143,9 +143,11 @@ class DesignTest extends TestCase
|
||||
$this->credit->service()->createInvitations()->markSent()->save();
|
||||
$this->credit->fresh();
|
||||
$this->credit->load('invitations');
|
||||
|
||||
$invitation = $this->credit->invitations->first();
|
||||
$invitation->setRelation('credit', $this->credit);
|
||||
|
||||
|
||||
$this->client->settings = $settings;
|
||||
$this->client->save();
|
||||
|
||||
|
57
tests/Unit/CurrencyApiTest.php
Normal file
57
tests/Unit/CurrencyApiTest.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Libraries\Currency\Conversion\CurrencyApi;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @covers App\Libraries\Currency\Conversion\CurrencyApi
|
||||
*/
|
||||
class CurrencyApiTest extends TestCase
|
||||
{
|
||||
public function setUp() :void
|
||||
{
|
||||
|
||||
parent::setUp();
|
||||
|
||||
}
|
||||
|
||||
public function testCurrencyConversionWorking()
|
||||
{
|
||||
|
||||
$converter = new CurrencyApi();
|
||||
|
||||
$converted_amount = $converter->convert(100,1,2);
|
||||
|
||||
$this->assertIsFloat($converted_amount);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function testExchangeRate()
|
||||
{
|
||||
|
||||
$converter = new CurrencyApi();
|
||||
|
||||
$exchange_rate = $converter->exchangeRate(1, 2);
|
||||
|
||||
$this->assertIsNumeric($exchange_rate);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function testExchangeRateWithDate()
|
||||
{
|
||||
$date = Carbon::parse('2020-03-08');
|
||||
|
||||
$converter = new CurrencyApi();
|
||||
|
||||
$exchange_rate = $converter->exchangeRate(1, 2, $date);
|
||||
|
||||
$this->assertIsNumeric($exchange_rate);
|
||||
|
||||
}
|
||||
}
|
58
tests/Unit/Migration/FeesAndLimitsTest.php
Normal file
58
tests/Unit/Migration/FeesAndLimitsTest.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Migration;
|
||||
|
||||
use App\DataMapper\BaseSettings;
|
||||
use App\DataMapper\FeesAndLimits;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class FeesAndLimitsTest extends TestCase
|
||||
{
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testFeesAndLimitsFunctionWorks()
|
||||
{
|
||||
$data = [];
|
||||
$data['min_limit'] = 234;
|
||||
$data['max_limit'] = 65317;
|
||||
$data['fee_amount'] = 0.00;
|
||||
$data['fee_percent'] = 0.000;
|
||||
$data['tax_name1'] = '' ;
|
||||
$data['tax_rate1'] = '';
|
||||
$data['tax_name2'] = '';
|
||||
$data['tax_rate2'] = '';
|
||||
$data['tax_name3'] = '';
|
||||
$data['tax_rate3'] = 0;
|
||||
|
||||
$transformed = $this->cleanFeesAndLimits($data);
|
||||
|
||||
$this->assertTrue(is_array($transformed));
|
||||
}
|
||||
|
||||
|
||||
public function cleanFeesAndLimits($fees_and_limits)
|
||||
{
|
||||
$new_arr = [];
|
||||
|
||||
foreach ($fees_and_limits as $key => $value) {
|
||||
$fal = new FeesAndLimits;
|
||||
|
||||
$fal->{$key} = $value;
|
||||
// foreach ($value as $k => $v) {
|
||||
// $fal->{$k} = $v;
|
||||
// $fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
|
||||
// }
|
||||
|
||||
// $new_arr[$key] = (array)$fal;
|
||||
}
|
||||
|
||||
return $new_arr;
|
||||
}
|
||||
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user