mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Fixes for payments with invoices from different clients (#3396)
This commit is contained in:
parent
177c756dd8
commit
08ce375fbc
@ -12,9 +12,10 @@
|
||||
namespace App\Http\Requests\Payment;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use App\Http\ValidationRules\ValidPayableInvoicesRule;
|
||||
use App\Http\ValidationRules\PaymentAmountsBalanceRule;
|
||||
use App\Http\ValidationRules\Payment\ValidInvoicesRules;
|
||||
use App\Http\ValidationRules\ValidCreditsPresentRule;
|
||||
use App\Http\ValidationRules\ValidPayableInvoicesRule;
|
||||
use App\Models\Payment;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
|
||||
@ -88,8 +89,9 @@ class StorePaymentRequest extends Request
|
||||
'amount' => 'numeric|required',
|
||||
'amount' => [new PaymentAmountsBalanceRule(),new ValidCreditsPresentRule()],
|
||||
'date' => 'required',
|
||||
'client_id' => 'required|exists:clients,id',
|
||||
'client_id' => 'bail|required|exists:clients,id',
|
||||
'invoices.*.invoice_id' => 'required|exists:invoices,id',
|
||||
'invoices.*.invoice_id' => new ValidInvoicesRules($this->all()),
|
||||
'invoices.*.amount' => 'required',
|
||||
'credits.*.credit_id' => 'required|exists:credits,id',
|
||||
'credits.*.amount' => 'required',
|
||||
|
90
app/Http/ValidationRules/Payment/ValidInvoicesRules.php
Normal file
90
app/Http/ValidationRules/Payment/ValidInvoicesRules.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com)
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Http\ValidationRules\Payment;
|
||||
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Models\Credit;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Payment;
|
||||
use App\Models\User;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
/**
|
||||
* Class ValidInvoicesRules
|
||||
* @package App\Http\ValidationRules\Payment
|
||||
*/
|
||||
class ValidInvoicesRules implements Rule
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
private $error_msg;
|
||||
|
||||
private $input;
|
||||
|
||||
|
||||
public function __construct($input)
|
||||
{
|
||||
$this->input = $input;
|
||||
}
|
||||
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
|
||||
return $this->checkInvoicesAreHomogenous();
|
||||
|
||||
}
|
||||
|
||||
private function checkInvoicesAreHomogenous()
|
||||
{
|
||||
|
||||
if(!array_key_exists('client_id', $this->input)){
|
||||
\Log::error("Client id is required");
|
||||
$this->error_msg = "Client id is required";
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach($this->input['invoices'] as $invoice)
|
||||
{
|
||||
$invoice = Invoice::whereId($invoice)->first();
|
||||
|
||||
if(!$invoice){
|
||||
$this->error_msg = "Invoice not found ";
|
||||
return false;
|
||||
}
|
||||
|
||||
if($invoice->client_id != $this->input['client_id']){
|
||||
$this->error_msg = "Selected invoices are not from a single client";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return $this->error_msg;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -396,7 +396,6 @@ class PaymentTest extends TestCase
|
||||
$this->invoice->is_deleted = false;
|
||||
$this->invoice->save();
|
||||
|
||||
|
||||
$data = [
|
||||
'amount' => 6.0,
|
||||
'client_id' => $client->hashed_id,
|
||||
@ -420,13 +419,13 @@ class PaymentTest extends TestCase
|
||||
catch(ValidationException $e) {
|
||||
|
||||
$message = json_decode($e->validator->getMessageBag(),1);
|
||||
|
||||
\Log::error($message);
|
||||
\Log::error('errrr');
|
||||
}
|
||||
|
||||
$arr = $response->json();
|
||||
$response->assertStatus(200);
|
||||
|
||||
|
||||
$payment_id = $arr['data']['id'];
|
||||
|
||||
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
||||
@ -1097,4 +1096,78 @@ class PaymentTest extends TestCase
|
||||
$this->assertEquals(round($payment->applied,2), $this->invoice->amount);
|
||||
}
|
||||
|
||||
public function testPaymentForInvoicesFromDifferentClients()
|
||||
{
|
||||
|
||||
$client1 = ClientFactory::create($this->company->id, $this->user->id);
|
||||
$client1->save();
|
||||
|
||||
$client2 = ClientFactory::create($this->company->id, $this->user->id);
|
||||
$client2->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();
|
||||
|
||||
$invoice2 = InvoiceFactory::create($this->company->id,$this->user->id);//stub the company and user_id
|
||||
$invoice2->client_id = $client2->id;
|
||||
$invoice2->status_id = Invoice::STATUS_SENT;
|
||||
|
||||
$invoice2->line_items = $this->buildLineItems();
|
||||
$invoice2->uses_inclusive_Taxes = false;
|
||||
|
||||
$invoice2->save();
|
||||
|
||||
$invoice_calc = new InvoiceSum($invoice2);
|
||||
$invoice_calc->build();
|
||||
|
||||
$invoice2 = $invoice_calc->getInvoice();
|
||||
$invoice2->save();
|
||||
|
||||
$data = [
|
||||
'amount' => $invoice1->amount + $invoice2->amount,
|
||||
'client_id' => $client1->hashed_id,
|
||||
'invoices' => [
|
||||
[
|
||||
'invoice_id' => $invoice1->hashed_id,
|
||||
'amount' => $invoice1->amount
|
||||
],
|
||||
[
|
||||
'invoice_id' => $invoice2->hashed_id,
|
||||
'amount' => $invoice2->amount
|
||||
]
|
||||
],
|
||||
'date' => '2020/12/12',
|
||||
|
||||
];
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user