Working on custom invoice numbers

This commit is contained in:
Hillel Coren 2015-10-23 11:06:46 +03:00
parent 87dad69d57
commit a0ec032f69
5 changed files with 31 additions and 17 deletions

View File

@ -87,7 +87,7 @@ class InvoiceApiController extends Controller
// check if the invoice number is set and unique // check if the invoice number is set and unique
if (!isset($data['invoice_number']) && !isset($data['id'])) { 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'])) { } else if (isset($data['invoice_number'])) {
$invoice = Invoice::scope()->where('invoice_number', '=', $data['invoice_number'])->first(); $invoice = Invoice::scope()->where('invoice_number', '=', $data['invoice_number'])->first();
if ($invoice) { if ($invoice) {

View File

@ -248,7 +248,7 @@ class InvoiceController extends BaseController
if ($clone) { if ($clone) {
$invoice->id = null; $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->balance = $invoice->amount;
$invoice->invoice_status_id = 0; $invoice->invoice_status_id = 0;
$invoice->invoice_date = date_create()->format('Y-m-d'); $invoice->invoice_date = date_create()->format('Y-m-d');
@ -350,7 +350,11 @@ class InvoiceController extends BaseController
public function create($clientPublicId = 0, $isRecurring = false) public function create($clientPublicId = 0, $isRecurring = false)
{ {
$client = null; $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) { if ($clientPublicId) {
$client = Client::scope($clientPublicId)->firstOrFail(); $client = Client::scope($clientPublicId)->firstOrFail();

View File

@ -248,7 +248,14 @@ class Account extends Eloquent
return $isQuote ? ($this->quote_number_pattern ? true : false) : ($this->invoice_number_pattern ? true : false); 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; $pattern = $isQuote ? $this->quote_number_pattern : $this->invoice_number_pattern;
@ -262,6 +269,11 @@ class Account extends Eloquent
$search[] = '{$counter}'; $search[] = '{$counter}';
$replace[] = str_pad($this->getCounter($isQuote), 4, '0', STR_PAD_LEFT); $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; $matches = false;
preg_match('/{\$date:(.*?)}/', $pattern, $matches); preg_match('/{\$date:(.*?)}/', $pattern, $matches);
if (count($matches) > 1) { if (count($matches) > 1) {
@ -287,14 +299,12 @@ class Account extends Eloquent
$search = [ $search = [
//'{$clientId}', //'{$clientId}',
'{$userId}',
'{$custom1}', '{$custom1}',
'{$custom2}', '{$custom2}',
]; ];
$replace = [ $replace = [
//str_pad($client->public_id, 3, '0', STR_PAD_LEFT), //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_value1,
$client->custom_value2, $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 // 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 // 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 false;
} }
return $this->getNextInvoiceNumber($isQuote = false, $prefix = ''); return $this->getNextInvoiceNumber($isQuote = false, $prefix = '', $client, $user);
} }
public function getCounter($isQuote) 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; 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)) { if ($this->hasNumberPattern($isQuote)) {
return $this->getNumberPattern($isQuote, $client); return $this->getNumberPattern($isQuote, $client, $user);
} }
$counter = $this->getCounter($isQuote); $counter = $this->getCounter($isQuote);

View File

@ -478,7 +478,7 @@ class InvoiceRepository
} }
$clone->invoice_number = $account->invoice_number_prefix.$invoiceNumber; $clone->invoice_number = $account->invoice_number_prefix.$invoiceNumber;
} else { } else {
$clone->invoice_number = $account->getNextInvoiceNumber($invoice->is_quote, '', $invoice->client); $clone->invoice_number = $account->getNextInvoiceNumber($invoice->is_quote, '', $invoice->client, Auth::user());
} }
foreach ([ foreach ([
@ -631,7 +631,7 @@ class InvoiceRepository
$invoice = Invoice::createNew($recurInvoice); $invoice = Invoice::createNew($recurInvoice);
$invoice->client_id = $recurInvoice->client_id; $invoice->client_id = $recurInvoice->client_id;
$invoice->recurring_invoice_id = $recurInvoice->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->amount = $recurInvoice->amount;
$invoice->balance = $recurInvoice->amount; $invoice->balance = $recurInvoice->amount;
$invoice->invoice_date = date_create()->format('Y-m-d'); $invoice->invoice_date = date_create()->format('Y-m-d');

View File

@ -644,7 +644,7 @@
// we enable searching by contact but the selection must be the client // we enable searching by contact but the selection must be the client
$('.client-input').val(getClientDisplayName(selected)); $('.client-input').val(getClientDisplayName(selected));
// if there's an invoice number pattern we'll apply it now // 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); setInvoiceNumber(selected);
@endif @endif
} else { } else {
@ -1033,11 +1033,11 @@
@if ($invoice) @if ($invoice)
return; return;
@endif @endif
var number = '{{ $account->getNumberPattern($entityType == ENTITY_QUOTE) }}'; var number = '{{ $account->getNumberPattern($entityType == ENTITY_QUOTE, false, Auth::user()) }}';
number = number.replace('{$clientId}', client.public_id < 0 ? '???' : padToThree(client.public_id));
number = number.replace('{$custom1}', client.custom_value1); number = number.replace('{$custom1}', client.custom_value1);
number = number.replace('{$custom2}', 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); model.invoice().invoice_number(number);
} }