From eaa6ba1d3971fc042f3cd63dc16487b340967688 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 11 May 2022 16:29:56 +1000 Subject: [PATCH] Fixes for ACH notification with WePay --- app/Console/Commands/DemoMode.php | 1 + .../Ninja/WePayFailureNotification.php | 2 +- app/Repositories/ExpenseRepository.php | 29 ++++++++++ app/Services/Report/ProfitLoss.php | 56 ++++++++++++++++++- 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/DemoMode.php b/app/Console/Commands/DemoMode.php index 7652568d18b7..bbb24e2a89db 100644 --- a/app/Console/Commands/DemoMode.php +++ b/app/Console/Commands/DemoMode.php @@ -131,6 +131,7 @@ class DemoMode extends Command 'enabled_modules' => 32767, 'company_key' => 'KEY', 'enable_shop_api' => true, + 'markdown_email_enabled' => false, ]); $settings = $company->settings; diff --git a/app/Notifications/Ninja/WePayFailureNotification.php b/app/Notifications/Ninja/WePayFailureNotification.php index 08e96cb856c0..011020492d6e 100644 --- a/app/Notifications/Ninja/WePayFailureNotification.php +++ b/app/Notifications/Ninja/WePayFailureNotification.php @@ -73,7 +73,7 @@ class WePayFailureNotification extends Notification public function toSlack($notifiable) { - (new SlackMessage) + return (new SlackMessage) ->success() ->from(ctrans('texts.notification_bot')) ->image('https://app.invoiceninja.com/favicon.png') diff --git a/app/Repositories/ExpenseRepository.php b/app/Repositories/ExpenseRepository.php index 9f8577683e69..4cba59bab284 100644 --- a/app/Repositories/ExpenseRepository.php +++ b/app/Repositories/ExpenseRepository.php @@ -12,8 +12,10 @@ namespace App\Repositories; use App\Factory\ExpenseFactory; +use App\Libraries\Currency\Conversion\CurrencyApi; use App\Models\Expense; use App\Utils\Traits\GeneratesCounter; +use Illuminate\Support\Carbon; /** * ExpenseRepository. @@ -34,6 +36,10 @@ class ExpenseRepository extends BaseRepository public function save(array $data, Expense $expense) : ?Expense { $expense->fill($data); + + if(!$expense->id) + $expense = $this->processExchangeRates($data, $expense); + $expense->number = empty($expense->number) ? $this->getNextExpenseNumber($expense) : $expense->number; $expense->save(); @@ -57,4 +63,27 @@ class ExpenseRepository extends BaseRepository ExpenseFactory::create(auth()->user()->company()->id, auth()->user()->id) ); } + + public function processExchangeRates($data, $expense) + { + + if(array_key_exists('exchange_rate', $data) && isset($data['exchange_rate']) && $data['exchange_rate'] != 1){ + return $expense; + } + + $expense_currency = $data['currency_id']; + $company_currency = $expense->company->settings->currency_id; + + if ($company_currency != $expense_currency) { + + $exchange_rate = new CurrencyApi(); + + $expense->exchange_rate = $exchange_rate->exchangeRate($expense_currency, $company_currency, Carbon::parse($expense->date)); + + return $expense; + } + + return $expense; + } + } diff --git a/app/Services/Report/ProfitLoss.php b/app/Services/Report/ProfitLoss.php index de6c2ab10e45..983ba68b8c71 100644 --- a/app/Services/Report/ProfitLoss.php +++ b/app/Services/Report/ProfitLoss.php @@ -11,7 +11,9 @@ namespace App\Services\Report; +use App\Libraries\Currency\Conversion\CurrencyApi; use App\Models\Company; +use App\Models\Expense; use Illuminate\Support\Carbon; class ProfitLoss @@ -26,6 +28,8 @@ class ProfitLoss private $end_date; + protected CurrencyApi $currency_api; + /* payload variables. @@ -51,8 +55,9 @@ class ProfitLoss protected Company $company; - public function __construct(Company $company, array $payload) + public function __construct(Company $company, array $payload, CurrencyApi $currency_api) { + $this->currency_api = $currency_api; $this->company = $company; @@ -148,6 +153,55 @@ class ProfitLoss } private function expenseCalc() + { + + $expenses = Expense::where('company_id', $this->company->id) + ->where('is_deleted', 0) + ->withTrashed() + ->whereBetween('date', [$this->start_date, $this->end_date]) + ->cursor(); + + + if($this->is_tax_included) + return $this->calculateExpensesWithTaxes($expenses); + + return $this->calculateExpensesWithoutTaxes($expenses); + + } + + private function calculateExpensesWithTaxes($expenses) + { + + foreach($expenses as $expense) + { + + if(!$expense->calculate_tax_by_amount && !$expense->uses_inclusive_taxes) + { + + } + + } + + } + + private function calculateExpensesWithoutTaxes($expenses) + { + $total = 0; + $converted_total = 0; + + foreach($expenses as $expense) + { + $total += $expense->amount; + $total += $this->getConvertedTotal($expense); + } + } + + private function getConvertedTotal($expense) + { + + } + + private function expenseCalcWithTax() { return \DB::select( \DB::raw("