mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-27 11:41:09 -04:00
Fix currency settings error (#821)
* Fixed issue with currency error on change after transactions * organized imports
This commit is contained in:
@ -6,6 +6,7 @@ use Crater\Http\Controllers\Controller;
|
||||
use Crater\Http\Requests\UpdateSettingsRequest;
|
||||
use Crater\Models\Company;
|
||||
use Crater\Models\CompanySetting;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class UpdateCompanySettingsController extends Controller
|
||||
{
|
||||
@ -20,10 +21,13 @@ class UpdateCompanySettingsController extends Controller
|
||||
$company = Company::find($request->header('company'));
|
||||
$this->authorize('manage company', $company);
|
||||
|
||||
$companyCurrency = CompanySetting::getSetting('currency', $request->header('company'));
|
||||
$data = $request->settings;
|
||||
|
||||
if ($companyCurrency !== $data['currency'] && $company->hasTransactions()) {
|
||||
if (
|
||||
Arr::exists($data, 'currency') &&
|
||||
(CompanySetting::getSetting('currency', $company->id) !== $data['currency']) &&
|
||||
$company->hasTransactions()
|
||||
) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Cannot update company currency after transactions are created.'
|
||||
|
||||
@ -3,6 +3,9 @@
|
||||
use Crater\Http\Controllers\V1\Admin\Settings\CompanyController;
|
||||
use Crater\Http\Requests\CompanyRequest;
|
||||
use Crater\Http\Requests\ProfileRequest;
|
||||
use Crater\Models\Invoice;
|
||||
use Crater\Models\InvoiceItem;
|
||||
use Crater\Models\Tax;
|
||||
use Crater\Models\User;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
@ -120,6 +123,64 @@ test('update settings', function () {
|
||||
}
|
||||
});
|
||||
|
||||
test('update settings without currency setting', function () {
|
||||
|
||||
$settings = [
|
||||
'notification_email' => 'noreply@crater.in',
|
||||
];
|
||||
|
||||
$response = postJson('/api/v1/company/settings', ['settings' => $settings]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
foreach ($settings as $key => $value) {
|
||||
$this->assertDatabaseHas('company_settings', [
|
||||
'option' => $key,
|
||||
'value' => $value,
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
test('update currency settings after company has currency and transactions is not allowed', function () {
|
||||
$settings = [
|
||||
'currency' => 1,
|
||||
];
|
||||
|
||||
$response = postJson('/api/v1/company/settings', ['settings' => $settings]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
Invoice::factory()
|
||||
->raw([
|
||||
'taxes' => [Tax::factory()->raw()],
|
||||
'items' => [InvoiceItem::factory()->raw()],
|
||||
]);
|
||||
|
||||
$settings = [
|
||||
'currency' => 2,
|
||||
];
|
||||
|
||||
$response = postJson('/api/v1/company/settings', ['settings' => $settings]);
|
||||
|
||||
$response->assertOK()
|
||||
->assertJson([
|
||||
'success' => false,
|
||||
'message' => 'Cannot update company currency after transactions are created.'
|
||||
]);
|
||||
|
||||
|
||||
$this->assertDatabaseHas('company_settings', [
|
||||
'option' => 'currency',
|
||||
'value' => 1,
|
||||
]);
|
||||
});
|
||||
|
||||
test('get notification email settings', function () {
|
||||
$data['settings'] = [
|
||||
'currency',
|
||||
|
||||
Reference in New Issue
Block a user