From 1b1d7df53c815e37b1a58894f7991d76e6a3878b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 18 Jun 2023 13:35:51 +1000 Subject: [PATCH] Fixes for profit and loss reports --- app/Models/Account.php | 4 ++-- app/Repositories/CompanyRepository.php | 4 +++- app/Services/Report/ProfitLoss.php | 25 ++++++++++++------------- app/Utils/Ninja.php | 6 ++++-- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/app/Models/Account.php b/app/Models/Account.php index 4e18792f8a62..e6a5701f558c 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -527,11 +527,11 @@ class Account extends BaseModel public function emailsSent() { - if (is_null(Cache::get($this->key))) { + if (is_null(Cache::get("email_quota".$this->key))) { return 0; } - return Cache::get($this->key); + return Cache::get("email_quota".$this->key); } public function emailQuotaExceeded() :bool diff --git a/app/Repositories/CompanyRepository.php b/app/Repositories/CompanyRepository.php index 3d29e6da907b..542b30ead9c0 100644 --- a/app/Repositories/CompanyRepository.php +++ b/app/Repositories/CompanyRepository.php @@ -40,7 +40,9 @@ class CompanyRepository extends BaseRepository $company->fill($data); /** Only required to handle v4 migration workloads */ - if(Ninja::isHosted() && $company->isDirty('is_disabled') && !$company->is_disabled) { + // if(Ninja::isHosted() && $company->isDirty('is_disabled') && !$company->is_disabled) { + if($company->isDirty('is_disabled') && !$company->is_disabled) { + nlog("trigger"); Ninja::triggerForwarding($company->company_key, $company->owner()->email); } diff --git a/app/Services/Report/ProfitLoss.php b/app/Services/Report/ProfitLoss.php index 45e53a20d8f0..4bdbac75bbfa 100644 --- a/app/Services/Report/ProfitLoss.php +++ b/app/Services/Report/ProfitLoss.php @@ -11,18 +11,19 @@ namespace App\Services\Report; -use App\Libraries\Currency\Conversion\CurrencyApi; -use App\Libraries\MultiDB; -use App\Models\Company; -use App\Models\Currency; -use App\Models\Expense; -use App\Models\Payment; use App\Utils\Ninja; use App\Utils\Number; +use League\Csv\Writer; +use App\Models\Company; +use App\Models\Expense; +use App\Models\Invoice; +use App\Models\Payment; +use App\Models\Currency; +use App\Libraries\MultiDB; +use Illuminate\Support\Str; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\App; -use Illuminate\Support\Str; -use League\Csv\Writer; +use App\Libraries\Currency\Conversion\CurrencyApi; class ProfitLoss { @@ -279,8 +280,6 @@ class ProfitLoss ->with(['company', 'client']) ->cursor() ->each(function ($payment) { - $company = $payment->company; - $client = $payment->client; $map = new \stdClass; $amount_payment_paid = 0; @@ -293,8 +292,8 @@ class ProfitLoss $tax_amount_credit_converted = $tax_amount_credit_converted = 0; foreach ($payment->paymentables as $pivot) { - if ($pivot->paymentable instanceof \App\Models\Invoice) { - $invoice = $pivot->paymentable; + if ($pivot->paymentable_type == 'invoices') { + $invoice = Invoice::withTrashed()->find($pivot->paymentable_id); $amount_payment_paid += $pivot->amount - $pivot->refunded; $amount_payment_paid_converted += $amount_payment_paid / ($payment->exchange_rate ?: 1); @@ -303,7 +302,7 @@ class ProfitLoss $tax_amount_converted += (($amount_payment_paid / $invoice->amount) * $invoice->total_taxes) / $payment->exchange_rate; } - if ($pivot->paymentable instanceof \App\Models\Credit) { + if ($pivot->paymentable_type == 'credits') { $amount_credit_paid += $pivot->amount - $pivot->refunded; $amount_credit_paid_converted += $amount_payment_paid / ($payment->exchange_rate ?: 1); diff --git a/app/Utils/Ninja.php b/app/Utils/Ninja.php index c09039ccd91a..16f357d4df0b 100644 --- a/app/Utils/Ninja.php +++ b/app/Utils/Ninja.php @@ -156,15 +156,17 @@ class Ninja public static function triggerForwarding(string $company_key, string $email) { try { - Http::withHeaders([ + $x = Http::withHeaders([ 'X-API-HOSTED-SECRET' => config('ninja.ninja_hosted_secret'), ])->post(config('ninja.license_url').'/api/v1/enable_forwarding', [ 'account_key' => $company_key, 'email' => $email, ]); + + nlog($x->body()); } catch (\Exception $e) { - nlog("attempt forwarding for{$email} - {$company_key}"); + nlog("Attempt forwarding for {$email} - {$company_key} Failed"); } }