1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00

Fixes for Payments (#3194)

* Set payment number on completed payment

* Fix for paymentables not returning

* Do not set invoice status to paid if only a partial amount of the invoice has been paid
This commit is contained in:
David Bomba 2020-01-04 13:27:51 +11:00 committed by GitHub
parent b92a6a78c3
commit ad06de25f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 76 additions and 10 deletions

View File

@ -316,6 +316,7 @@ class CreateTestData extends Command
$payment->transaction_reference = rand(0, 500);
$payment->type_id = PaymentType::CREDIT_CARD_OTHER;
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->number = $client->getNextPaymentNumber($client);
$payment->save();
$payment->invoices()->save($invoice);

View File

@ -39,7 +39,8 @@ class StorePaymentRequest extends Request
$input['client_id'] = $this->decodePrimaryKey($input['client_id']);
}
if (isset($input['invoices'])) {
if (isset($input['invoices']) && is_array($input['invoices']) !== false) {
foreach ($input['invoices'] as $key => $value) {
$input['invoices'][$key]['id'] = $this->decodePrimaryKey($value['id']);
}

View File

@ -91,13 +91,18 @@ class ApplyInvoicePayment implements ShouldQueue
$this->invoice->setStatus(Invoice::STATUS_PARTIAL);
$this->invoice->updateBalance($this->amount*-1);
}
} elseif ($this->invoice->amount == $this->invoice->balance) { //total invoice paid.
} elseif ($this->amount == $this->invoice->balance) { //total invoice paid.
$this->invoice->clearPartial();
$this->invoice->setDueDate();
//$this->invoice->setDueDate();
$this->invoice->setStatus(Invoice::STATUS_PAID);
$this->invoice->updateBalance($this->amount*-1);
} elseif($this->amount < $this->invoice->balance) { //partial invoice payment made
$this->invoice->clearPartial();
$this->invoice->updateBalance($this->amount*-1);
}
$this->invoice->save();
/* Update Payment Applied Amount*/
$this->payment->applied += $this->amount;
$this->payment->save();

19
app/Models/Creditable.php Normal file
View File

@ -0,0 +1,19 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class Creditable extends Pivot
{
protected $table = 'creditables';
}

View File

@ -15,9 +15,5 @@ use Illuminate\Database\Eloquent\Relations\Pivot;
class Paymentable extends Pivot
{
// protected $guarded = ['id'];
// public $incrementing = true;
protected $table = 'paymentables';
}

View File

@ -54,6 +54,8 @@ class PaymentRepository extends BaseRepository
$payment->fill($request->input());
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->number = $payment->client->getNextPaymentNumber($payment->client);
$payment->save();
if ($request->input('invoices')) {

View File

@ -35,7 +35,7 @@ class PaymentableTransformer extends EntityTransformer
return [
'id' => $this->encodePrimaryKey($paymentable->id),
'invoice_id' => $this->encodePrimaryKey($paymentable->paymentable_id),
'amount' => $paymentable->amount,
'amount' => (float)$paymentable->amount,
];
}
}

View File

@ -14,6 +14,7 @@ namespace App\Utils\Traits;
use App\Models\Client;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Quote;
use App\Models\RecurringInvoice;
use App\Models\Timezone;
@ -127,10 +128,10 @@ trait GeneratesCounter
return $quote_number;
}
public function getNextRecurringInvoiceNumber()
public function getNextRecurringInvoiceNumber(Client $client)
{
//Reset counters if enabled
//Reset counters if enabled
$this->resetCounters($client);
$is_client_counter = false;
@ -162,6 +163,44 @@ trait GeneratesCounter
return $invoice_number;
}
/**
* Payment Number Generator
* @return string The payment number
*/
public function getNextPaymentNumber(Client $client) :string
{
//Reset counters if enabled
$this->resetCounters($client);
$is_client_counter = false;
//todo handle if we have specific client patterns in the future
$pattern = $client->company->settings->payment_number_pattern;
//Determine if we are using client_counters
if (strpos($pattern, 'client_counter') === false) {
$counter = $client->company->settings->payment_number_counter;
} else {
$counter = $client->settings->payment_number_counter;
$is_client_counter = true;
}
//Return a valid counter
$pattern = '';
$padding = $client->getSetting('counter_padding');
$payment_number = $this->checkEntityNumber(Payment::class, $client, $counter, $padding, $pattern);
//increment the correct invoice_number Counter (company vs client)
if ($is_client_counter) {
$this->incrementCounter($client, 'payment_number_counter');
} else {
$this->incrementCounter($client->company, 'payment_number_counter');
}
return (string)$payment_number;
}
/**
* Gets the next client number.
*
@ -225,9 +264,12 @@ trait GeneratesCounter
$check = $class::whereCompanyId($client->company_id)->whereNumber($number)->withTrashed()->first();
} elseif ($class == Quote::class) {
$check = $class::whereCompanyId($client->company_id)->whereNumber($number)->withTrashed()->first();
} elseif ($class == Payment::class) {
$check = $class::whereCompanyId($client->company_id)->whereNumber($number)->withTrashed()->first();
}
$counter++;
} while ($check);