mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-06 03:02:34 +01:00
Validate invoices in a payment (#3397)
* Payment Scenarios: * Fixes for duplicate invoice-ids in a payment
This commit is contained in:
parent
08ce375fbc
commit
abe3376c48
@ -90,7 +90,7 @@ class StorePaymentRequest extends Request
|
|||||||
'amount' => [new PaymentAmountsBalanceRule(),new ValidCreditsPresentRule()],
|
'amount' => [new PaymentAmountsBalanceRule(),new ValidCreditsPresentRule()],
|
||||||
'date' => 'required',
|
'date' => 'required',
|
||||||
'client_id' => 'bail|required|exists:clients,id',
|
'client_id' => 'bail|required|exists:clients,id',
|
||||||
'invoices.*.invoice_id' => 'required|exists:invoices,id',
|
'invoices.*.invoice_id' => 'required|distinct|exists:invoices,id',
|
||||||
'invoices.*.invoice_id' => new ValidInvoicesRules($this->all()),
|
'invoices.*.invoice_id' => new ValidInvoicesRules($this->all()),
|
||||||
'invoices.*.amount' => 'required',
|
'invoices.*.amount' => 'required',
|
||||||
'credits.*.credit_id' => 'required|exists:credits,id',
|
'credits.*.credit_id' => 'required|exists:credits,id',
|
||||||
|
@ -53,26 +53,36 @@ class ValidInvoicesRules implements Rule
|
|||||||
{
|
{
|
||||||
|
|
||||||
if(!array_key_exists('client_id', $this->input)){
|
if(!array_key_exists('client_id', $this->input)){
|
||||||
\Log::error("Client id is required");
|
|
||||||
$this->error_msg = "Client id is required";
|
$this->error_msg = "Client id is required";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$unique_array = [];
|
||||||
|
|
||||||
foreach($this->input['invoices'] as $invoice)
|
foreach($this->input['invoices'] as $invoice)
|
||||||
{
|
{
|
||||||
$invoice = Invoice::whereId($invoice)->first();
|
$unique_array[] = $invoice['invoice_id'];
|
||||||
|
|
||||||
if(!$invoice){
|
$inv = Invoice::whereId($invoice['invoice_id'])->first();
|
||||||
|
|
||||||
|
if(!$inv){
|
||||||
$this->error_msg = "Invoice not found ";
|
$this->error_msg = "Invoice not found ";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($invoice->client_id != $this->input['client_id']){
|
if($inv->client_id != $this->input['client_id']){
|
||||||
$this->error_msg = "Selected invoices are not from a single client";
|
$this->error_msg = "Selected invoices are not from a single client";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!(array_unique($unique_array) == $unique_array))
|
||||||
|
{
|
||||||
|
$this->error_msg = "Duplicate invoices submitted.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1170,4 +1170,64 @@ class PaymentTest extends TestCase
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testPaymentWithSameInvoiceMultipleTimes()
|
||||||
|
{
|
||||||
|
|
||||||
|
$client1 = ClientFactory::create($this->company->id, $this->user->id);
|
||||||
|
$client1->save();
|
||||||
|
|
||||||
|
$invoice1 = InvoiceFactory::create($this->company->id,$this->user->id);//stub the company and user_id
|
||||||
|
$invoice1->client_id = $client1->id;
|
||||||
|
$invoice1->status_id = Invoice::STATUS_SENT;
|
||||||
|
|
||||||
|
$invoice1->line_items = $this->buildLineItems();
|
||||||
|
$invoice1->uses_inclusive_Taxes = false;
|
||||||
|
|
||||||
|
$invoice1->save();
|
||||||
|
|
||||||
|
$invoice_calc = new InvoiceSum($invoice1);
|
||||||
|
$invoice_calc->build();
|
||||||
|
|
||||||
|
$invoice1 = $invoice_calc->getInvoice();
|
||||||
|
$invoice1->save();
|
||||||
|
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'amount' => $invoice1->amount,
|
||||||
|
'client_id' => $client1->hashed_id,
|
||||||
|
'invoices' => [
|
||||||
|
[
|
||||||
|
'invoice_id' => $invoice1->hashed_id,
|
||||||
|
'amount' => 1
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'invoice_id' => $invoice1->hashed_id,
|
||||||
|
'amount' => 1
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'date' => '2020/12/12',
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
|
$response = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->post('/api/v1/payments?include=invoices', $data);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch(ValidationException $e) {
|
||||||
|
// \Log::error('in the validator');
|
||||||
|
$message = json_decode($e->validator->getMessageBag(),1);
|
||||||
|
\Log::error($message);
|
||||||
|
$this->assertNotNull($message);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertNull($response);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user