diff --git a/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php b/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php index a5a4bc9f93..09e96cac63 100644 --- a/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php +++ b/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php @@ -43,15 +43,15 @@ class RestoreDisabledRule implements ValidationRule $delete_query = clone $base_query; $mutated_query = $delete_query->where(function ($q){ - $q->whereNotNull('backup->parent_invoice_id')->orWhere('backup->child_invoice_ids', '!=', []); + $q->whereNotNull('backup->parent_invoice_id')->orWhere('backup->child_invoice_ids', '!=', '[]'); }); /** For verifactu, we do not allow restores of deleted invoices */ if($value == 'restore' && $restore_query->where('is_deleted', true)->exists()) { $fail(ctrans('texts.restore_disabled_verifactu')); } - elseif(in_array($value, ['delete', 'cancel']) && $delete_query->exists()) { - nlog($delete_query->pluck('backup')->toArray()); // any verifactu invoices that have a parent can NEVER be deleted. The parent can also NEVER be deleted + elseif(in_array($value, ['delete', 'cancel']) && $mutated_query->exists()) { + // any verifactu invoices that have a parent can NEVER be deleted. The parent can also NEVER be deleted $fail(ctrans('texts.delete_disabled_verifactu')); } } diff --git a/tests/Feature/EInvoice/Verifactu/VerifactuApiTest.php b/tests/Feature/EInvoice/Verifactu/VerifactuApiTest.php index 9375f2b203..d94403603e 100644 --- a/tests/Feature/EInvoice/Verifactu/VerifactuApiTest.php +++ b/tests/Feature/EInvoice/Verifactu/VerifactuApiTest.php @@ -91,6 +91,129 @@ class VerifactuApiTest extends TestCase } + public function test_archive_invoice_with_no_parent() + { + + $settings = $this->company->settings; + $settings->e_invoice_type = 'verifactu'; + $settings->is_locked = 'when_sent'; + + $this->company->settings = $settings; + $this->company->save(); + + $invoice = $this->buildData(); + $invoice->service()->markSent()->save(); + + $data = [ + 'action' => 'archive', + 'ids' => [$invoice->hashed_id] + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/invoices/bulk', $data); + + $response->assertStatus(200); + + + $data = [ + 'action' => 'restore', + 'ids' => [$invoice->hashed_id] + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/invoices/bulk', $data); + + $response->assertStatus(200); + } + + public function test_delete_invoice_with_parent() + { + + $settings = $this->company->settings; + $settings->e_invoice_type = 'verifactu'; + + $this->company->settings = $settings; + $this->company->save(); + + $invoice = $this->buildData(); + $invoice->service()->markSent()->save(); + + $this->assertEquals(121, $invoice->amount); + + $data = $invoice->toArray(); + unset($data['client']); + unset($data['invitations']); + $data['client_id'] = $this->client->hashed_id; + $data['verifactu_modified'] = true; + $data['modified_invoice_id'] = $invoice->hashed_id; + $data['number'] = null; + $data['discount'] = 121; + $data['is_amount_discount'] = true; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/invoices', $data); + + $response->assertStatus(200); + + $data = [ + 'action' => 'delete', + 'ids' => [$invoice->hashed_id] + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/invoices/bulk', $data); + + $response->assertStatus(422); + + } + + public function test_delete_invoice_with_no_parent() + { + + $settings = $this->company->settings; + $settings->e_invoice_type = 'verifactu'; + + $this->company->settings = $settings; + $this->company->save(); + + $invoice = $this->buildData(); + $invoice->service()->markSent()->save(); + + $data = [ + 'action' => 'delete', + 'ids' => [$invoice->hashed_id] + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/invoices/bulk', $data); + + $response->assertStatus(200); + + + $data = [ + 'action' => 'restore', + 'ids' => [$invoice->hashed_id] + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/invoices/bulk', $data); + + $response->assertStatus(422); + } + + public function test_credits_never_exceed_original_invoice9() {