Fixes for duplicate payment numbers"

This commit is contained in:
David Bomba 2021-12-10 21:50:46 +11:00
parent 307e32e54d
commit b1a2403e35
3 changed files with 57 additions and 1 deletions

View File

@ -20,6 +20,7 @@ use App\Http\ValidationRules\ValidCreditsPresentRule;
use App\Http\ValidationRules\ValidPayableInvoicesRule;
use App\Models\Payment;
use App\Utils\Traits\MakesHash;
use Illuminate\Validation\Rule;
class StorePaymentRequest extends Request
{
@ -100,7 +101,8 @@ class StorePaymentRequest extends Request
'credits.*.credit_id' => new ValidCreditsRules($this->all()),
'credits.*.amount' => ['required', new CreditsSumRule($this->all())],
'invoices' => new ValidPayableInvoicesRule(),
'number' => 'bail|nullable|unique:payments,number,'.$this->id.',id,company_id,'.$this->company_id,
'number' => ['nullable', Rule::unique('payments')->where('company_id', auth()->user()->company()->id)],
];
if ($this->input('documents') && is_array($this->input('documents'))) {

View File

@ -17,6 +17,8 @@ use App\Models\CompanyGateway;
use App\Models\Invoice;
use App\Models\Payment;
use App\Services\AbstractService;
use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
class AddGatewayFee extends AbstractService
{
@ -72,6 +74,10 @@ class AddGatewayFee extends AbstractService
private function processGatewayFee($gateway_fee)
{
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->invoice->company->settings));
$invoice_item = new InvoiceItem;
$invoice_item->type_id = '3';
$invoice_item->product_key = ctrans('texts.surcharge');
@ -98,6 +104,10 @@ class AddGatewayFee extends AbstractService
private function processGatewayDiscount($gateway_fee)
{
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->invoice->company->settings));
$invoice_item = new InvoiceItem;
$invoice_item->type_id = '3';
$invoice_item->product_key = ctrans('texts.discount');

View File

@ -1480,5 +1480,49 @@ class PaymentTest extends TestCase
$this->assertEquals(10, $this->invoice->fresh()->balance);
$this->assertEquals(10, $this->invoice->fresh()->balance);
}
public function testUniquePaymentNumbers()
{
$data = [
'amount' => $this->invoice->amount,
'client_id' => $this->client->hashed_id,
'date' => '2020/12/12',
'number' => 'duplicate',
];
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($message);
}
$arr = $response->json();
$response->assertStatus(200);
$response = false;
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($message);
}
if($response)
$response->assertStatus(302);
}
}