Refactor tests to remove modifications of existing invoices
This commit is contained in:
parent
bf8041ab7c
commit
a141ca1549
|
|
@ -37,29 +37,32 @@ class CanGenerateModificationInvoice implements ValidationRule
|
|||
|
||||
/** For verifactu, we do not allow restores of deleted invoices */
|
||||
if (!$company->verifactuEnabled())
|
||||
$fail("Verifactu is not enabled for this company");
|
||||
$fail("Verifactu no está habilitado para esta empresa"); // Verifactu is not enabled for this company
|
||||
|
||||
$invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($value));
|
||||
|
||||
if (is_null($invoice)) {
|
||||
$fail("Invoice not found.");
|
||||
}elseif($invoice->is_deleted) {
|
||||
$fail("Cannot create a modification invoice for a deleted invoice.");
|
||||
$fail("Factura no encontrada."); // Invoice not found
|
||||
} elseif($invoice->is_deleted) {
|
||||
$fail("No se puede crear una factura de rectificación para una factura eliminada."); // Cannot create a rectification invoice for a deleted invoice
|
||||
} elseif($invoice->backup->document_type !== 'F1') {
|
||||
$fail("Solo las facturas originales F1 pueden ser rectificadas."); // Only original F1 invoices can be rectified
|
||||
} elseif($invoice->status_id === Invoice::STATUS_DRAFT){
|
||||
$fail("Cannot create a modification invoice for a draft invoice.");
|
||||
$fail("No se puede crear una factura de rectificación para una factura en borrador."); // Cannot create a rectification invoice for a draft invoice
|
||||
} elseif(in_array($invoice->status_id, [Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID])) {
|
||||
$fail("Cannot create a modification invoice where a payment has been made.");
|
||||
$fail("No se puede crear una factura de rectificación cuando se ha realizado un pago."); // Cannot create a rectification invoice where a payment has been made
|
||||
} elseif($invoice->status_id === Invoice::STATUS_CANCELLED ) {
|
||||
$fail("Cannot create a modification invoice for a cancelled invoice.");
|
||||
$fail("No se puede crear una factura de rectificación para una factura cancelada."); // Cannot create a rectification invoice for a cancelled invoice
|
||||
} elseif($invoice->status_id === Invoice::STATUS_REPLACED) {
|
||||
$fail("Cannot create a modification invoice for a replaced invoice.");
|
||||
$fail("No se puede crear una factura de rectificación para una factura reemplazada."); // Cannot create a rectification invoice for a replaced invoice
|
||||
} elseif($invoice->status_id === Invoice::STATUS_REVERSED) {
|
||||
$fail("Cannot create a modification invoice for a reversed invoice.");
|
||||
$fail("No se puede crear una factura de rectificación para una factura revertida."); // Cannot create a rectification invoice for a reversed invoice
|
||||
}
|
||||
// } elseif ($invoice->status_id !== Invoice::STATUS_SENT) {
|
||||
// $fail("Cannot create a modification invoice.");
|
||||
// } elseif($invoice->amount <= 0){
|
||||
// $fail("Cannot create a modification invoice for an invoice with an amount less than 0.");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ class RestoreDisabledRule implements ValidationRule
|
|||
$delete_query = clone $base_query;
|
||||
|
||||
$mutated_query = $delete_query->where(function ($q){
|
||||
$q->where('backup->document_type', '!=', 'R2')->orWhere('backup->child_invoice_ids', '!=', '[]');
|
||||
$q->where('backup->document_type', 'F1')->where('backup->child_invoice_ids', '!=', '[]');
|
||||
});
|
||||
|
||||
/** For verifactu, we do not allow restores of deleted invoices */
|
||||
|
|
@ -51,7 +51,6 @@ class RestoreDisabledRule implements ValidationRule
|
|||
$fail(ctrans('texts.restore_disabled_verifactu'));
|
||||
}
|
||||
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'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,7 +84,10 @@ class VerifactuAmountCheck implements ValidationRule
|
|||
|
||||
$total = $items->sum() - $total_discount;
|
||||
|
||||
if($total < 0 && !$invoice) {
|
||||
if($total > 0) {
|
||||
$fail("Only negative amounts allowed for rectification {$total}");
|
||||
}
|
||||
elseif($total < 0 && !$invoice) {
|
||||
$fail("Negative invoices {$total} can only be linked to existing invoices");
|
||||
}
|
||||
elseif($invoice && ($total + $child_invoice_totals + $invoice->amount) < 0) {
|
||||
|
|
|
|||
|
|
@ -62,22 +62,14 @@ class Verifactu extends AbstractService
|
|||
|
||||
$i_logs = $this->invoice->verifactu_logs;
|
||||
|
||||
// if($i_logs->count() >= 1){
|
||||
// $document = (new RegistroAlta($this->invoice))->run()->setRectification()->getInvoice();
|
||||
// }
|
||||
// else{
|
||||
$document = (new RegistroAlta($this->invoice))->run();
|
||||
|
||||
if($this->invoice->amount < 0) {
|
||||
$document = $document->setRectification();
|
||||
}
|
||||
elseif($this->invoice->backup->document_type === 'F3') {
|
||||
$document = $document->setModification();
|
||||
}
|
||||
|
||||
$document = $document->getInvoice();
|
||||
// }
|
||||
|
||||
$document = (new RegistroAlta($this->invoice))->run();
|
||||
|
||||
if($this->invoice->amount < 0) {
|
||||
$document = $document->setRectification();
|
||||
}
|
||||
|
||||
$document = $document->getInvoice();
|
||||
|
||||
//keep this state for logging later on successful send
|
||||
$this->_document = $document;
|
||||
|
||||
|
|
|
|||
|
|
@ -84,6 +84,8 @@ class VerifactuApiTest extends TestCase
|
|||
'footer' => '',
|
||||
]);
|
||||
|
||||
$invoice->backup->document_type = 'F1';
|
||||
|
||||
$repo = new InvoiceRepository();
|
||||
$invoice = $repo->save([], $invoice);
|
||||
|
||||
|
|
@ -185,8 +187,11 @@ class VerifactuApiTest extends TestCase
|
|||
$this->company->save();
|
||||
|
||||
$invoice = $this->buildData();
|
||||
$invoice->service()->markSent()->save();
|
||||
$invoice = $invoice->service()->markSent()->save();
|
||||
|
||||
$this->assertEquals('F1', $invoice->backup->document_type);
|
||||
$this->assertFalse($invoice->is_deleted);
|
||||
|
||||
$data = [
|
||||
'action' => 'delete',
|
||||
'ids' => [$invoice->hashed_id]
|
||||
|
|
@ -311,7 +316,7 @@ class VerifactuApiTest extends TestCase
|
|||
'X-API-TOKEN' => $this->token,
|
||||
])->postJson('/api/v1/invoices', $data);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertStatus(422);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -551,7 +556,7 @@ class VerifactuApiTest extends TestCase
|
|||
'X-API-TOKEN' => $this->token,
|
||||
])->postJson('/api/v1/invoices', $data);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertStatus(422);
|
||||
}
|
||||
|
||||
public function test_verifactu_amount_check()
|
||||
|
|
@ -620,39 +625,8 @@ class VerifactuApiTest extends TestCase
|
|||
'X-API-TOKEN' => $this->token,
|
||||
])->postJson('/api/v1/invoices', $data);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertStatus(422);
|
||||
|
||||
$arr = $response->json();
|
||||
|
||||
$this->assertEquals($arr['data']['status_id'], Invoice::STATUS_SENT);
|
||||
$this->assertEquals($arr['data']['amount'], 242);
|
||||
$this->assertEquals($arr['data']['balance'], 242);
|
||||
$this->assertEquals($arr['data']['backup']['parent_invoice_id'], $invoice->hashed_id);
|
||||
|
||||
$invoice = $invoice->fresh();
|
||||
|
||||
$this->assertEquals(Invoice::STATUS_REPLACED, $invoice->status_id);
|
||||
$this->assertTrue($invoice->backup->child_invoice_ids->contains($arr['data']['id']));
|
||||
|
||||
$this->assertEquals(615, $this->client->fresh()->balance);
|
||||
|
||||
//now create another modification invoice reducing the amounts
|
||||
|
||||
$data = $invoice2->toArray();
|
||||
$data['verifactu_modified'] = true;
|
||||
$data['modified_invoice_id'] = $arr['data']['id'];
|
||||
$data['number'] = null;
|
||||
$data['client_id'] = $this->client->hashed_id;
|
||||
$data['line_items'] = $invoice2->line_items;
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])->postJson('/api/v1/invoices', $data);
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
$this->assertEquals(494, $this->client->fresh()->balance);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -734,7 +708,7 @@ class VerifactuApiTest extends TestCase
|
|||
'X-API-TOKEN' => $this->token,
|
||||
])->postJson('/api/v1/invoices', $data);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertStatus(422);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue