mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-08 20:22:42 +01:00
Working on custom invoice numbers
This commit is contained in:
parent
87dad69d57
commit
a0ec032f69
@ -87,7 +87,7 @@ class InvoiceApiController extends Controller
|
||||
|
||||
// check if the invoice number is set and unique
|
||||
if (!isset($data['invoice_number']) && !isset($data['id'])) {
|
||||
$data['invoice_number'] = Auth::user()->account->getNextInvoiceNumber(false, '', $client);
|
||||
$data['invoice_number'] = Auth::user()->account->getNextInvoiceNumber(false, '', $client, Auth::user());
|
||||
} else if (isset($data['invoice_number'])) {
|
||||
$invoice = Invoice::scope()->where('invoice_number', '=', $data['invoice_number'])->first();
|
||||
if ($invoice) {
|
||||
|
@ -248,7 +248,7 @@ class InvoiceController extends BaseController
|
||||
|
||||
if ($clone) {
|
||||
$invoice->id = null;
|
||||
$invoice->invoice_number = $account->getNextInvoiceNumber($invoice->is_quote, '', $invoice->client);
|
||||
$invoice->invoice_number = $account->getNextInvoiceNumber($invoice->is_quote, '', $invoice->client, Auth::user());
|
||||
$invoice->balance = $invoice->amount;
|
||||
$invoice->invoice_status_id = 0;
|
||||
$invoice->invoice_date = date_create()->format('Y-m-d');
|
||||
@ -350,7 +350,11 @@ class InvoiceController extends BaseController
|
||||
public function create($clientPublicId = 0, $isRecurring = false)
|
||||
{
|
||||
$client = null;
|
||||
$invoiceNumber = $isRecurring ? microtime(true) : Auth::user()->account->getDefaultInvoiceNumber();
|
||||
if ($isRecurring) {
|
||||
$invoiceNumber = microtime(true);
|
||||
} else {
|
||||
$invoiceNumber = Auth::user()->account->getDefaultInvoiceNumber(false, false, false, Auth::user());
|
||||
}
|
||||
|
||||
if ($clientPublicId) {
|
||||
$client = Client::scope($clientPublicId)->firstOrFail();
|
||||
|
@ -248,7 +248,14 @@ class Account extends Eloquent
|
||||
return $isQuote ? ($this->quote_number_pattern ? true : false) : ($this->invoice_number_pattern ? true : false);
|
||||
}
|
||||
|
||||
public function getNumberPattern($isQuote, $client = null)
|
||||
public function hasClientNumberPattern($isQuote)
|
||||
{
|
||||
$pattern = $this->getNumberPattern($isQuote);
|
||||
|
||||
return strstr($pattern, '$custom');
|
||||
}
|
||||
|
||||
public function getNumberPattern($isQuote, $client = null, $user = null)
|
||||
{
|
||||
$pattern = $isQuote ? $this->quote_number_pattern : $this->invoice_number_pattern;
|
||||
|
||||
@ -262,6 +269,11 @@ class Account extends Eloquent
|
||||
$search[] = '{$counter}';
|
||||
$replace[] = str_pad($this->getCounter($isQuote), 4, '0', STR_PAD_LEFT);
|
||||
|
||||
if ($user) {
|
||||
$search[] = '{$userId}';
|
||||
$replace[] = str_pad($user->public_id, 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
$matches = false;
|
||||
preg_match('/{\$date:(.*?)}/', $pattern, $matches);
|
||||
if (count($matches) > 1) {
|
||||
@ -287,14 +299,12 @@ class Account extends Eloquent
|
||||
|
||||
$search = [
|
||||
//'{$clientId}',
|
||||
'{$userId}',
|
||||
'{$custom1}',
|
||||
'{$custom2}',
|
||||
];
|
||||
|
||||
$replace = [
|
||||
//str_pad($client->public_id, 3, '0', STR_PAD_LEFT),
|
||||
str_pad($client->user->public_id, 2, '0', STR_PAD_LEFT),
|
||||
$client->custom_value1,
|
||||
$client->custom_value2,
|
||||
];
|
||||
@ -304,13 +314,13 @@ class Account extends Eloquent
|
||||
|
||||
// if we're using a pattern we don't know the next number until a client
|
||||
// is selected, to support this the default value is blank
|
||||
public function getDefaultInvoiceNumber($isQuote = false, $prefix = '')
|
||||
public function getDefaultInvoiceNumber($isQuote = false, $prefix = '', $client = null, $user = null)
|
||||
{
|
||||
if ($this->getNumberPattern($isQuote)) {
|
||||
if ($this->hasClientNumberPattern($isQuote)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->getNextInvoiceNumber($isQuote = false, $prefix = '');
|
||||
return $this->getNextInvoiceNumber($isQuote = false, $prefix = '', $client, $user);
|
||||
}
|
||||
|
||||
public function getCounter($isQuote)
|
||||
@ -318,10 +328,10 @@ class Account extends Eloquent
|
||||
return $isQuote && !$this->share_counter ? $this->quote_number_counter : $this->invoice_number_counter;
|
||||
}
|
||||
|
||||
public function getNextInvoiceNumber($isQuote = false, $prefix = '', $client = null)
|
||||
public function getNextInvoiceNumber($isQuote = false, $prefix = '', $client = null, $user = null)
|
||||
{
|
||||
if ($this->hasNumberPattern($isQuote)) {
|
||||
return $this->getNumberPattern($isQuote, $client);
|
||||
return $this->getNumberPattern($isQuote, $client, $user);
|
||||
}
|
||||
|
||||
$counter = $this->getCounter($isQuote);
|
||||
|
@ -478,7 +478,7 @@ class InvoiceRepository
|
||||
}
|
||||
$clone->invoice_number = $account->invoice_number_prefix.$invoiceNumber;
|
||||
} else {
|
||||
$clone->invoice_number = $account->getNextInvoiceNumber($invoice->is_quote, '', $invoice->client);
|
||||
$clone->invoice_number = $account->getNextInvoiceNumber($invoice->is_quote, '', $invoice->client, Auth::user());
|
||||
}
|
||||
|
||||
foreach ([
|
||||
@ -631,7 +631,7 @@ class InvoiceRepository
|
||||
$invoice = Invoice::createNew($recurInvoice);
|
||||
$invoice->client_id = $recurInvoice->client_id;
|
||||
$invoice->recurring_invoice_id = $recurInvoice->id;
|
||||
$invoice->invoice_number = $recurInvoice->account->getNextInvoiceNumber(false, 'R', $recurInvoice->client);
|
||||
$invoice->invoice_number = $recurInvoice->account->getNextInvoiceNumber(false, 'R', $recurInvoice->client, $recurInvoice->user);
|
||||
$invoice->amount = $recurInvoice->amount;
|
||||
$invoice->balance = $recurInvoice->amount;
|
||||
$invoice->invoice_date = date_create()->format('Y-m-d');
|
||||
|
@ -644,7 +644,7 @@
|
||||
// we enable searching by contact but the selection must be the client
|
||||
$('.client-input').val(getClientDisplayName(selected));
|
||||
// if there's an invoice number pattern we'll apply it now
|
||||
@if ($account->hasNumberPattern($entityType == ENTITY_QUOTE))
|
||||
@if ($account->hasClientNumberPattern($entityType == ENTITY_QUOTE))
|
||||
setInvoiceNumber(selected);
|
||||
@endif
|
||||
} else {
|
||||
@ -1033,11 +1033,11 @@
|
||||
@if ($invoice)
|
||||
return;
|
||||
@endif
|
||||
var number = '{{ $account->getNumberPattern($entityType == ENTITY_QUOTE) }}';
|
||||
number = number.replace('{$clientId}', client.public_id < 0 ? '???' : padToThree(client.public_id));
|
||||
var number = '{{ $account->getNumberPattern($entityType == ENTITY_QUOTE, false, Auth::user()) }}';
|
||||
number = number.replace('{$custom1}', client.custom_value1);
|
||||
number = number.replace('{$custom2}', client.custom_value1);
|
||||
number = number.replace('{$counter}', padToFour(client.{{ $entityType }}_number_counter));
|
||||
//number = number.replace('{$clientId}', client.public_id < 0 ? '???' : padToThree(client.public_id));
|
||||
//number = number.replace('{$counter}', padToFour(client.{{ $entityType }}_number_counter));
|
||||
model.invoice().invoice_number(number);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user