mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-08 20:22:42 +01:00
Refactored number genrating code
This commit is contained in:
parent
c37f6cd55b
commit
7a58108cd0
@ -100,7 +100,7 @@ class InvoiceController extends BaseController
|
||||
if ($clone) {
|
||||
$invoice->id = $invoice->public_id = null;
|
||||
$invoice->is_public = false;
|
||||
$invoice->invoice_number = $account->getNextInvoiceNumber($invoice);
|
||||
$invoice->invoice_number = $account->getNextNumber($invoice);
|
||||
$invoice->balance = $invoice->amount;
|
||||
$invoice->invoice_status_id = 0;
|
||||
$invoice->invoice_date = Utils::today();
|
||||
|
@ -864,7 +864,7 @@ class Account extends Eloquent
|
||||
if ($this->hasClientNumberPattern($invoice) && !$clientId) {
|
||||
// do nothing, we don't yet know the value
|
||||
} elseif ( ! $invoice->invoice_number) {
|
||||
$invoice->invoice_number = $this->getNextInvoiceNumber($invoice);
|
||||
$invoice->invoice_number = $this->getNextNumber($invoice);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -535,7 +535,7 @@ class Client extends EntityModel
|
||||
|
||||
Client::creating(function ($client) {
|
||||
$client->setNullValues();
|
||||
$client->account->incrementClientCounter();
|
||||
$client->account->incrementCounter($client);
|
||||
});
|
||||
|
||||
Client::updating(function ($client) {
|
||||
|
@ -11,43 +11,48 @@ use App\Models\Client;
|
||||
trait GeneratesNumbers
|
||||
{
|
||||
/**
|
||||
* @param $invoice
|
||||
* @param bool $validateUnique
|
||||
* @param $entity
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getNextInvoiceNumber($invoice, $validateUnique = true)
|
||||
public function getNextNumber($entity = false)
|
||||
{
|
||||
if ($this->hasNumberPattern($invoice->invoice_type_id)) {
|
||||
$number = $this->getNumberPattern($invoice);
|
||||
} else {
|
||||
$counter = $this->getCounter($invoice->invoice_type_id);
|
||||
$prefix = $this->getNumberPrefix($invoice->invoice_type_id);
|
||||
$counterOffset = 0;
|
||||
$check = false;
|
||||
$entity = $entity ?: new Client();
|
||||
$entityType = $entity->getEntityType();
|
||||
|
||||
// confirm the invoice number isn't already taken
|
||||
do {
|
||||
$counter = $this->getCounter($entityType);
|
||||
$prefix = $this->getNumberPrefix($entityType);
|
||||
$counterOffset = 0;
|
||||
$check = false;
|
||||
|
||||
// confirm the invoice number isn't already taken
|
||||
do {
|
||||
if ($this->hasNumberPattern($entityType)) {
|
||||
$number = $this->applyNumberPattern($entity, $counter);
|
||||
} else {
|
||||
$number = $prefix . str_pad($counter, $this->invoice_number_padding, '0', STR_PAD_LEFT);
|
||||
if ($validateUnique) {
|
||||
$check = Invoice::scope(false, $this->id)->whereInvoiceNumber($number)->withTrashed()->first();
|
||||
$counter++;
|
||||
$counterOffset++;
|
||||
}
|
||||
} while ($check);
|
||||
|
||||
// update the invoice counter to be caught up
|
||||
if ($counterOffset > 1) {
|
||||
if ($invoice->isType(INVOICE_TYPE_QUOTE) && !$this->share_counter) {
|
||||
$this->quote_number_counter += $counterOffset - 1;
|
||||
} else {
|
||||
$this->invoice_number_counter += $counterOffset - 1;
|
||||
}
|
||||
|
||||
$this->save();
|
||||
}
|
||||
|
||||
if ($entity->isEntityType(ENTITY_CLIENT)) {
|
||||
$check = Client::scope(false, $this->id)->whereIdNumber($number)->withTrashed()->first();
|
||||
} else {
|
||||
$check = Invoice::scope(false, $this->id)->whereInvoiceNumber($number)->withTrashed()->first();
|
||||
}
|
||||
$counter++;
|
||||
$counterOffset++;
|
||||
} while ($check);
|
||||
|
||||
// update the counter to be caught up
|
||||
if ($counterOffset > 1) {
|
||||
if ($entity->isType(INVOICE_TYPE_QUOTE) && !$this->share_counter) {
|
||||
$this->quote_number_counter += $counterOffset - 1;
|
||||
} else {
|
||||
$this->{$entityType.'_number_counter'} += $counterOffset - 1;
|
||||
}
|
||||
|
||||
$this->save();
|
||||
}
|
||||
|
||||
if ($invoice->recurring_invoice_id) {
|
||||
if ($entity->recurring_invoice_id) {
|
||||
$number = $this->recurring_invoice_number_prefix . $number;
|
||||
}
|
||||
|
||||
@ -55,33 +60,44 @@ trait GeneratesNumbers
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $invoice_type_id
|
||||
* @param $entityType
|
||||
* @return string
|
||||
*/
|
||||
public function getNumberPrefix($invoice_type_id)
|
||||
public function getNumberPrefix($entityType)
|
||||
{
|
||||
if ( ! $this->hasFeature(FEATURE_INVOICE_SETTINGS)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return ($invoice_type_id == INVOICE_TYPE_QUOTE ? $this->quote_number_prefix : $this->invoice_number_prefix) ?: '';
|
||||
$field = "{$entityType}_number_prefix";
|
||||
return $this->$field ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $invoice_type_id
|
||||
* @param $entityType
|
||||
* @return bool
|
||||
*/
|
||||
public function hasNumberPattern($invoice_type_id)
|
||||
public function getNumberPattern($entityType)
|
||||
{
|
||||
if ( ! $this->hasFeature(FEATURE_INVOICE_SETTINGS)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $invoice_type_id == INVOICE_TYPE_QUOTE ? ($this->quote_number_pattern ? true : false) : ($this->invoice_number_pattern ? true : false);
|
||||
$field = "{$entityType}_number_pattern";
|
||||
return $this->$field;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $invoice
|
||||
* @param $entityType
|
||||
* @return bool
|
||||
*/
|
||||
public function hasNumberPattern($entityType)
|
||||
{
|
||||
return $this->getNumberPattern($entityType) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $entityType
|
||||
* @return string
|
||||
*/
|
||||
public function hasClientNumberPattern($invoice)
|
||||
@ -92,12 +108,14 @@ trait GeneratesNumbers
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $invoice
|
||||
* @param $entity
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function getNumberPattern($invoice)
|
||||
public function applyNumberPattern($entity, $counter = 0)
|
||||
{
|
||||
$pattern = $invoice->invoice_type_id == INVOICE_TYPE_QUOTE ? $this->quote_number_pattern : $this->invoice_number_pattern;
|
||||
$entityType = $entity->getEntityType();
|
||||
$counter = $counter ?: $this->getCounter($entityType);
|
||||
$pattern = $this->getNumberPattern($entityType);
|
||||
|
||||
if (!$pattern) {
|
||||
return false;
|
||||
@ -107,11 +125,11 @@ trait GeneratesNumbers
|
||||
$replace = [date('Y')];
|
||||
|
||||
$search[] = '{$counter}';
|
||||
$replace[] = str_pad($this->getCounter($invoice->invoice_type_id), $this->invoice_number_padding, '0', STR_PAD_LEFT);
|
||||
$replace[] = str_pad($counter, $this->invoice_number_padding, '0', STR_PAD_LEFT);
|
||||
|
||||
if (strstr($pattern, '{$userId}')) {
|
||||
$search[] = '{$userId}';
|
||||
$replace[] = str_pad(($invoice->user->public_id + 1), 2, '0', STR_PAD_LEFT);
|
||||
$replace[] = str_pad(($entity->user->public_id + 1), 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
$matches = false;
|
||||
@ -125,8 +143,8 @@ trait GeneratesNumbers
|
||||
|
||||
$pattern = str_replace($search, $replace, $pattern);
|
||||
|
||||
if ($invoice->client_id) {
|
||||
$pattern = $this->getClientInvoiceNumber($pattern, $invoice);
|
||||
if ($entity->client_id) {
|
||||
$pattern = $this->getClientInvoiceNumber($pattern, $entity);
|
||||
}
|
||||
|
||||
return $pattern;
|
||||
@ -157,12 +175,18 @@ trait GeneratesNumbers
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $invoice_type_id
|
||||
* @param $entityType
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCounter($invoice_type_id)
|
||||
public function getCounter($entityType)
|
||||
{
|
||||
return $invoice_type_id == INVOICE_TYPE_QUOTE && !$this->share_counter ? $this->quote_number_counter : $this->invoice_number_counter;
|
||||
if ($entityType == ENTITY_CLIENT) {
|
||||
return $this->client_number_counter;
|
||||
} elseif ($entityType == ENTITY_QUOTE && ! $this->share_counter) {
|
||||
return $this->quote_number_counter;
|
||||
} else {
|
||||
return $this->invoice_number_counter;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -172,20 +196,17 @@ trait GeneratesNumbers
|
||||
public function previewNextInvoiceNumber($entityType = ENTITY_INVOICE)
|
||||
{
|
||||
$invoice = $this->createInvoice($entityType);
|
||||
return $this->getNextInvoiceNumber($invoice);
|
||||
return $this->getNextNumber($invoice);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $invoice
|
||||
* @param $entity
|
||||
*/
|
||||
public function incrementCounter($invoice)
|
||||
public function incrementCounter($entity)
|
||||
{
|
||||
// if they didn't use the counter don't increment it
|
||||
if ($invoice->invoice_number != $this->getNextInvoiceNumber($invoice, false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($invoice->isType(INVOICE_TYPE_QUOTE) && !$this->share_counter) {
|
||||
if ($entity->isEntityType(ENTITY_CLIENT)) {
|
||||
$this->client_number_counter += 1;
|
||||
} elseif ($entity->isType(INVOICE_TYPE_QUOTE) && ! $this->share_counter) {
|
||||
$this->quote_number_counter += 1;
|
||||
} else {
|
||||
$this->invoice_number_counter += 1;
|
||||
@ -194,79 +215,7 @@ trait GeneratesNumbers
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public function getNextClientNumber()
|
||||
{
|
||||
if ( ! $this->hasFeature(FEATURE_INVOICE_SETTINGS)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$counter = $this->client_number_counter;
|
||||
$prefix = $this->client_number_prefix;
|
||||
$counterOffset = 0;
|
||||
$check = false;
|
||||
|
||||
// confirm the invoice number isn't already taken
|
||||
do {
|
||||
if ($this->client_number_pattern) {
|
||||
$number = $this->getClientNumberPattern($counter);
|
||||
} else {
|
||||
$number = $prefix . str_pad($counter, $this->invoice_number_padding, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
$check = Client::scope(false, $this->id)->whereIdNumber($number)->withTrashed()->first();
|
||||
$counter++;
|
||||
$counterOffset++;
|
||||
} while ($check);
|
||||
|
||||
// update the invoice counter to be caught up
|
||||
if ($counterOffset > 1) {
|
||||
$this->client_number_counter += $counterOffset - 1;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
return $number;
|
||||
}
|
||||
|
||||
private function getClientNumberPattern($counter)
|
||||
{
|
||||
$pattern = $this->client_number_pattern;
|
||||
|
||||
if ( ! $pattern) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$search = ['{$year}'];
|
||||
$replace = [date('Y')];
|
||||
|
||||
$search[] = '{$counter}';
|
||||
$replace[] = str_pad($counter, $this->invoice_number_padding, '0', STR_PAD_LEFT);
|
||||
|
||||
if (strstr($pattern, '{$userId}') && Auth::check()) {
|
||||
$search[] = '{$userId}';
|
||||
$replace[] = str_pad((Auth::user()->public_id + 1), 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
$matches = false;
|
||||
preg_match('/{\$date:(.*?)}/', $pattern, $matches);
|
||||
if (count($matches) > 1) {
|
||||
$format = $matches[1];
|
||||
$search[] = $matches[0];
|
||||
$date = Carbon::now(session(SESSION_TIMEZONE, DEFAULT_TIMEZONE))->format($format);
|
||||
$replace[] = str_replace($format, $date, $matches[1]);
|
||||
}
|
||||
|
||||
return str_replace($search, $replace, $pattern);
|
||||
}
|
||||
|
||||
public function incrementClientCounter()
|
||||
{
|
||||
if ($this->client_number_counter) {
|
||||
$this->client_number_counter += 1;
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
||||
public function useClientNumbers()
|
||||
public function clientNumbersEnabled()
|
||||
{
|
||||
return $this->hasFeature(FEATURE_INVOICE_SETTINGS) && $this->client_number_counter;
|
||||
}
|
||||
|
@ -278,7 +278,7 @@ class AccountRepository
|
||||
$invoice->user_id = $account->users()->first()->id;
|
||||
$invoice->public_id = $publicId;
|
||||
$invoice->client_id = $client->id;
|
||||
$invoice->invoice_number = $account->getNextInvoiceNumber($invoice);
|
||||
$invoice->invoice_number = $account->getNextNumber($invoice);
|
||||
$invoice->invoice_date = $renewalDate->format('Y-m-d');
|
||||
$invoice->amount = $invoice->balance = $plan_cost - $credit;
|
||||
$invoice->invoice_type_id = INVOICE_TYPE_STANDARD;
|
||||
|
@ -79,7 +79,7 @@ class ClientRepository extends BaseRepository
|
||||
} elseif (!$publicId || $publicId == '-1') {
|
||||
$client = Client::createNew();
|
||||
if (Auth::check() && Auth::user()->account->client_number_counter && empty($data['id_number'])) {
|
||||
$data['id_number'] = Auth::user()->account->getNextClientNumber();
|
||||
$data['id_number'] = Auth::user()->account->getNextNumber();
|
||||
}
|
||||
} else {
|
||||
$client = Client::scope($publicId)->with('contacts')->firstOrFail();
|
||||
|
@ -659,7 +659,7 @@ class InvoiceRepository extends BaseRepository
|
||||
$invoiceNumber = false;
|
||||
}
|
||||
}
|
||||
$clone->invoice_number = $invoiceNumber ?: $account->getNextInvoiceNumber($clone);
|
||||
$clone->invoice_number = $invoiceNumber ?: $account->getNextNumber($clone);
|
||||
$clone->invoice_date = Utils::today();
|
||||
|
||||
foreach ([
|
||||
@ -859,7 +859,7 @@ class InvoiceRepository extends BaseRepository
|
||||
$invoice->invoice_type_id = INVOICE_TYPE_STANDARD;
|
||||
$invoice->client_id = $recurInvoice->client_id;
|
||||
$invoice->recurring_invoice_id = $recurInvoice->id;
|
||||
$invoice->invoice_number = $recurInvoice->account->getNextInvoiceNumber($invoice);
|
||||
$invoice->invoice_number = $recurInvoice->account->getNextNumber($invoice);
|
||||
$invoice->amount = $recurInvoice->amount;
|
||||
$invoice->balance = $recurInvoice->amount;
|
||||
$invoice->invoice_date = Utils::today();
|
||||
|
@ -318,7 +318,7 @@ class ImportService
|
||||
if ($entityType == ENTITY_INVOICE && !$data['invoice_number']) {
|
||||
$account = Auth::user()->account;
|
||||
$invoice = Invoice::createNew();
|
||||
$data['invoice_number'] = $account->getNextInvoiceNumber($invoice);
|
||||
$data['invoice_number'] = $account->getNextNumber($invoice);
|
||||
}
|
||||
|
||||
if (EntityModel::validate($data, $entityType) !== true) {
|
||||
|
@ -135,7 +135,7 @@
|
||||
->label(trans('texts.counter'))
|
||||
->addGroupClass('pad-checkbox')
|
||||
->help(trans('texts.client_number_help') . ' ' .
|
||||
trans('texts.next_client_number', ['number' => $account->getNextClientNumber()])) !!}
|
||||
trans('texts.next_client_number', ['number' => $account->getNextNumber()])) !!}
|
||||
|
||||
@if ( ! $account->client_number_counter)
|
||||
</div>
|
||||
|
@ -25,7 +25,7 @@
|
||||
{!! Former::populate($client) !!}
|
||||
{!! Former::hidden('public_id') !!}
|
||||
@elseif ($account->client_number_counter)
|
||||
{!! Former::populateField('id_number', $account->getNextClientNumber()) !!}
|
||||
{!! Former::populateField('id_number', $account->getNextNumber()) !!}
|
||||
@endif
|
||||
|
||||
<div class="row">
|
||||
@ -39,7 +39,7 @@
|
||||
<div class="panel-body">
|
||||
|
||||
{!! Former::text('name')->data_bind("attr { placeholder: placeholderName }") !!}
|
||||
{!! Former::text('id_number')->placeholder($account->useClientNumbers() ? $account->getNextClientNumber() : ' ') !!}
|
||||
{!! Former::text('id_number')->placeholder($account->clientNumbersEnabled() ? $account->getNextNumber() : ' ') !!}
|
||||
{!! Former::text('vat_number') !!}
|
||||
{!! Former::text('website') !!}
|
||||
{!! Former::text('work_phone') !!}
|
||||
|
@ -622,7 +622,7 @@
|
||||
|
||||
{!! Former::text('client[id_number]')
|
||||
->label('id_number')
|
||||
->placeholder($account->useClientNumbers() ? $account->getNextClientNumber() : ' ')
|
||||
->placeholder($account->clientNumbersEnabled() ? $account->getNextNumber() : ' ')
|
||||
->data_bind("value: id_number, valueUpdate: 'afterkeydown'") !!}
|
||||
|
||||
@if ( ! $account->client_number_counter)
|
||||
@ -1598,7 +1598,7 @@
|
||||
@if ($invoice->id || !$account->hasClientNumberPattern($invoice))
|
||||
return;
|
||||
@endif
|
||||
var number = '{{ $account->getNumberPattern($invoice) }}';
|
||||
var number = '{{ $account->applyNumberPattern($invoice) }}';
|
||||
number = number.replace('{$custom1}', client.custom_value1 ? client.custom_value1 : '');
|
||||
number = number.replace('{$custom2}', client.custom_value2 ? client.custom_value1 : '');
|
||||
model.invoice().invoice_number(number);
|
||||
|
@ -155,7 +155,7 @@ function InvoiceModel(data) {
|
||||
var clientModel = false;
|
||||
} else {
|
||||
var clientModel = new ClientModel();
|
||||
clientModel.id_number("{{ $account->getNextClientNumber() }}");
|
||||
clientModel.id_number("{{ $account->getNextNumber() }}");
|
||||
}
|
||||
|
||||
var self = this;
|
||||
|
Loading…
Reference in New Issue
Block a user