diff --git a/.gitignore b/.gitignore index d4e2efd56..d78195dcf 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ .env .env.cypress.local .phpunit.result.cache +.php_cs.cache .php-cs-fixer.cache Homestead.json Homestead.yaml diff --git a/app/Services/Company/Adminland/Expense/ConvertAmountFromOneCurrencyToCompanyCurrency.php b/app/Services/Company/Adminland/Expense/ConvertAmountFromOneCurrencyToCompanyCurrency.php index 5acd53979..2466e9171 100644 --- a/app/Services/Company/Adminland/Expense/ConvertAmountFromOneCurrencyToCompanyCurrency.php +++ b/app/Services/Company/Adminland/Expense/ConvertAmountFromOneCurrencyToCompanyCurrency.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use ErrorException; use Illuminate\Support\Str; +use App\Helpers\MoneyHelper; use App\Services\BaseService; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Http; @@ -118,7 +119,7 @@ private function getConversionRate(): void private function convert(): void { - $this->convertedAmount = $this->amount / $this->rate; + $this->convertedAmount = MoneyHelper::parseInput((string) ($this->amount / $this->rate), $this->companyCurrency); } /** diff --git a/database/migrations/2020_06_30_211833_create_expenses_table.php b/database/migrations/2020_06_30_211833_create_expenses_table.php index 78a04a50f..dded46077 100644 --- a/database/migrations/2020_06_30_211833_create_expenses_table.php +++ b/database/migrations/2020_06_30_211833_create_expenses_table.php @@ -22,9 +22,9 @@ public function up(): void $table->unsignedBigInteger('expense_category_id')->nullable(); $table->string('status')->default('created'); $table->string('title'); - $table->integer('amount'); + $table->unsignedBigInteger('amount'); $table->string('currency'); - $table->double('converted_amount')->nullable(); + $table->unsignedBigInteger('converted_amount')->nullable(); $table->string('converted_to_currency')->nullable(); $table->datetime('converted_at')->nullable(); $table->double('exchange_rate')->nullable(); diff --git a/database/migrations/2021_07_13_164157_fix_expense_converted_amount.php b/database/migrations/2021_07_13_164157_fix_expense_converted_amount.php new file mode 100644 index 000000000..84253e010 --- /dev/null +++ b/database/migrations/2021_07_13_164157_fix_expense_converted_amount.php @@ -0,0 +1,34 @@ +chunk(100, function ($expenses) { + foreach ($expenses as $expense) { + $expense->converted_amount = MoneyHelper::parseInput((string) $expense->converted_amount, $expense->converted_to_currency); + $expense->save(); + } + }); + + /** @var \Illuminate\Database\Connection $connection */ + $connection = DB::connection(); + if ($connection->getDriverName() !== 'sqlite') { + Schema::table('expenses', function (Blueprint $table) { + $table->unsignedBigInteger('amount')->change(); + $table->unsignedBigInteger('converted_amount')->change(); + }); + } + } +} diff --git a/tests/Unit/Services/Company/Adminland/Expense/ConvertAmountFromOneCurrencyToCompanyCurrencyTest.php b/tests/Unit/Services/Company/Adminland/Expense/ConvertAmountFromOneCurrencyToCompanyCurrencyTest.php index 4ab97392e..c12e8f452 100644 --- a/tests/Unit/Services/Company/Adminland/Expense/ConvertAmountFromOneCurrencyToCompanyCurrencyTest.php +++ b/tests/Unit/Services/Company/Adminland/Expense/ConvertAmountFromOneCurrencyToCompanyCurrencyTest.php @@ -45,7 +45,7 @@ public function it_converts_an_amount_from_eur_to_cad_and_store_rate_in_cache(): $this->assertEquals( [ 'exchange_rate' => 0.847968, - 'converted_amount' => 11792.897845201705, + 'converted_amount' => 1179290, 'converted_to_currency' => 'USD', 'converted_at' => '2018-01-01 00:00:00', ],