Working on custom invoice numbers

This commit is contained in:
Hillel Coren 2015-10-23 14:55:18 +03:00
parent a0ec032f69
commit 17f853f907
12 changed files with 136 additions and 107 deletions

View File

@ -59,7 +59,7 @@ class CreateRandomData extends Command {
}
$invoice = Invoice::createNew($user);
$invoice->invoice_number = $user->account->getNextInvoiceNumber();
$invoice->invoice_number = $user->account->getNextInvoiceNumber($invoice);
$invoice->amount = $invoice->balance = $price;
$invoice->created_at = date('Y-m-d', strtotime(date("Y-m-d") . ' - ' . rand(1, 100) . ' days'));
$client->invoices()->save($invoice);

View File

@ -308,7 +308,7 @@ class AccountController extends BaseController
$client->work_phone = '';
$client->work_email = '';
$invoice->invoice_number = $account->getNextInvoiceNumber();
$invoice->invoice_number = '0000';
$invoice->invoice_date = Utils::fromSqlDate(date('Y-m-d'));
$invoice->account = json_decode($account->toJson());
$invoice->amount = $invoice->balance = 100;

View File

@ -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, Auth::user());
// do nothing... invoice number will be set automatically
} else if (isset($data['invoice_number'])) {
$invoice = Invoice::scope()->where('invoice_number', '=', $data['invoice_number'])->first();
if ($invoice) {

View File

@ -248,7 +248,7 @@ class InvoiceController extends BaseController
if ($clone) {
$invoice->id = null;
$invoice->invoice_number = $account->getNextInvoiceNumber($invoice->is_quote, '', $invoice->client, Auth::user());
$invoice->invoice_number = $account->getNextInvoiceNumber($invoice);
$invoice->balance = $invoice->amount;
$invoice->invoice_status_id = 0;
$invoice->invoice_date = date_create()->format('Y-m-d');
@ -350,26 +350,23 @@ class InvoiceController extends BaseController
public function create($clientPublicId = 0, $isRecurring = false)
{
$client = null;
if ($isRecurring) {
$invoiceNumber = microtime(true);
} else {
$invoiceNumber = Auth::user()->account->getDefaultInvoiceNumber(false, false, false, Auth::user());
}
if ($clientPublicId) {
$client = Client::scope($clientPublicId)->firstOrFail();
}
$data = array(
'entityType' => ENTITY_INVOICE,
'invoice' => null,
'data' => Input::old('data'),
'invoiceNumber' => $invoiceNumber,
'method' => 'POST',
'url' => 'invoices',
'title' => trans('texts.new_invoice'),
'isRecurring' => $isRecurring,
'client' => $client);
$invoice = Invoice::createNew();
$invoice->client = $client;
$invoice->is_recurring = $isRecurring;
$invoice->initialize();
$data = [
'entityType' => $invoice->getEntityType(),
'invoice' => $invoice,
'data' => Input::old('data'),
'method' => 'POST',
'url' => 'invoices',
'title' => trans('texts.new_invoice'),
];
$data = array_merge($data, self::getViewModel());
return View::make('invoices.edit', $data);

View File

@ -82,24 +82,25 @@ class QuoteController extends BaseController
}
$client = null;
$invoiceNumber = Auth::user()->account->getDefaultInvoiceNumber(true);
$account = Account::with('country')->findOrFail(Auth::user()->account_id);
if ($clientPublicId) {
$client = Client::scope($clientPublicId)->firstOrFail();
}
$data = array(
'account' => $account,
'invoice' => null,
'data' => Input::old('data'),
'invoiceNumber' => $invoiceNumber,
'method' => 'POST',
'url' => 'invoices',
'title' => trans('texts.new_quote'),
'client' => $client, );
$invoice = Invoice::createNew();
$invoice->client = $client;
$invoice->is_quote = true;
$invoice->initialize();
$data = [
'entityType' => $invoice->getEntityType(),
'invoice' => $invoice,
'data' => Input::old('data'),
'method' => 'POST',
'url' => 'invoices',
'title' => trans('texts.new_quote'),
];
$data = array_merge($data, self::getViewModel());
return View::make('invoices.edit', $data);
}

View File

@ -248,16 +248,16 @@ class Account extends Eloquent
return $isQuote ? ($this->quote_number_pattern ? true : false) : ($this->invoice_number_pattern ? true : false);
}
public function hasClientNumberPattern($isQuote)
public function hasClientNumberPattern($invoice)
{
$pattern = $this->getNumberPattern($isQuote);
$pattern = $invoice->is_quote ? $this->quote_number_pattern : $this->invoice_number_pattern;
return strstr($pattern, '$custom');
}
public function getNumberPattern($isQuote, $client = null, $user = null)
public function getNumberPattern($invoice)
{
$pattern = $isQuote ? $this->quote_number_pattern : $this->invoice_number_pattern;
$pattern = $invoice->is_quote ? $this->quote_number_pattern : $this->invoice_number_pattern;
if (!$pattern) {
return false;
@ -267,11 +267,11 @@ class Account extends Eloquent
$replace = [date('Y')];
$search[] = '{$counter}';
$replace[] = str_pad($this->getCounter($isQuote), 4, '0', STR_PAD_LEFT);
$replace[] = str_pad($this->getCounter($invoice->is_quote), 4, '0', STR_PAD_LEFT);
if ($user) {
if (strstr($pattern, '{$userId}')) {
$search[] = '{$userId}';
$replace[] = str_pad($user->public_id, 2, '0', STR_PAD_LEFT);
$replace[] = str_pad($invoice->user->public_id, 2, '0', STR_PAD_LEFT);
}
$matches = false;
@ -284,16 +284,16 @@ class Account extends Eloquent
$pattern = str_replace($search, $replace, $pattern);
if ($client) {
$pattern = $this->getClientInvoiceNumber($pattern, $isQuote, $client);
if ($invoice->client->id) {
$pattern = $this->getClientInvoiceNumber($pattern, $invoice);
}
return $pattern;
}
private function getClientInvoiceNumber($pattern, $isQuote, $client)
private function getClientInvoiceNumber($pattern, $invoice)
{
if (!$client) {
if (!$invoice->client) {
return $pattern;
}
@ -305,8 +305,8 @@ class Account extends Eloquent
$replace = [
//str_pad($client->public_id, 3, '0', STR_PAD_LEFT),
$client->custom_value1,
$client->custom_value2,
$invoice->client->custom_value1,
$invoice->client->custom_value2,
];
return str_replace($search, $replace, $pattern);
@ -314,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 = '', $client = null, $user = null)
public function getDefaultInvoiceNumber($invoice = false, $prefix = '')
{
if ($this->hasClientNumberPattern($isQuote)) {
if ($this->hasClientNumberPattern($invoice)) {
return false;
}
return $this->getNextInvoiceNumber($isQuote = false, $prefix = '', $client, $user);
return $this->getNextInvoiceNumber($invoice, $prefix = '');
}
public function getCounter($isQuote)
@ -328,14 +328,14 @@ 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, $user = null)
public function getNextInvoiceNumber($invoice, $prefix = '')
{
if ($this->hasNumberPattern($isQuote)) {
return $this->getNumberPattern($isQuote, $client, $user);
if ($this->hasNumberPattern($invoice->is_quote)) {
return $this->getNumberPattern($invoice);
}
$counter = $this->getCounter($isQuote);
$prefix .= $isQuote ? $this->quote_number_prefix : $this->invoice_number_prefix;
$counter = $this->getCounter($invoice->is_quote);
$prefix .= $invoice->is_quote ? $this->quote_number_prefix : $this->invoice_number_prefix;
$counterOffset = 0;
// confirm the invoice number isn't already taken
@ -348,7 +348,7 @@ class Account extends Eloquent
// update the invoice counter to be caught up
if ($counterOffset > 1) {
if ($isQuote && !$this->share_counter) {
if ($invoice->is_quote && !$this->share_counter) {
$this->quote_number_counter += $counterOffset - 1;
} else {
$this->invoice_number_counter += $counterOffset - 1;

View File

@ -20,11 +20,47 @@ class Invoice extends EntityModel
'custom1',
'custom2',
'userId',
//'clientId', // need to update after saving
'year',
'date:',
];
public function initialize()
{
$account = $this->account;
$this->invoice_date = Utils::today();
$this->start_date = Utils::today();
$this->invoice_design_id = $account->invoice_design_id;
$this->terms = $account->invoice_terms;
$this->invoice_footer = $account->invoice_footer;
if (!$this->invoice_number) {
if ($this->is_recurring) {
$this->invoice_number = microtime(true);
} else {
if ($account->hasClientNumberPattern($this) && !$this->client) {
// do nothing, we don't yet know the value
} else {
$this->invoice_number = $account->getNextInvoiceNumber($this);
}
}
}
if (!$this->client) {
$this->client = Client::createNew($this);
$this->client->public_id = 0;
}
}
public function isTrashed()
{
if ($this->client && $this->client->isTrashed()) {
return true;
}
return parent::isTrashed();
}
public function account()
{
return $this->belongsTo('App\Models\Account');

View File

@ -139,7 +139,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_number = $account->getNextInvoiceNumber($invoice);
$invoice->invoice_date = date_create()->format('Y-m-d');
$invoice->amount = PRO_PLAN_PRICE;
$invoice->balance = PRO_PLAN_PRICE;

View File

@ -254,10 +254,12 @@ class InvoiceRepository
$invoice = Invoice::scope($publicId)->firstOrFail();
} else {
$invoice = Invoice::createNew();
$invoice->client_id = $data['client_id'];
$invoice->is_recurring = $data['is_recurring'] ? true : false;
if ($entityType == ENTITY_QUOTE) {
$invoice->is_quote = true;
}
$invoice->initialize();
}
$account = \Auth::user()->account;
@ -283,11 +285,6 @@ class InvoiceRepository
$invoice->invoice_date = isset($data['invoice_date_sql']) ? $data['invoice_date_sql'] : Utils::toSqlDate($data['invoice_date']);
$invoice->has_tasks = isset($data['has_tasks']) ? $data['has_tasks'] : false;
if (!$publicId) {
$invoice->client_id = $data['client_id'];
$invoice->is_recurring = $data['is_recurring'] ? true : false;
}
if ($invoice->is_recurring) {
if ($invoice->start_date && $invoice->start_date != Utils::toSqlDate($data['start_date'])) {
$invoice->last_sent_date = null;
@ -478,7 +475,7 @@ class InvoiceRepository
}
$clone->invoice_number = $account->invoice_number_prefix.$invoiceNumber;
} else {
$clone->invoice_number = $account->getNextInvoiceNumber($invoice->is_quote, '', $invoice->client, Auth::user());
$clone->invoice_number = $account->getNextInvoiceNumber($invoice);
}
foreach ([
@ -631,7 +628,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, $recurInvoice->user);
$invoice->invoice_number = $recurInvoice->account->getNextInvoiceNumber($recurInvoice, 'R');
$invoice->amount = $recurInvoice->amount;
$invoice->balance = $recurInvoice->amount;
$invoice->invoice_date = date_create()->format('Y-m-d');

View File

@ -8,9 +8,9 @@
@section('content')
@if ($invoice && $invoice->id)
@if ($invoice->id)
<ol class="breadcrumb">
@if ($isRecurring)
@if ($invoice->is_recurring)
<li>{!! link_to('invoices', trans('texts.recurring_invoice')) !!}</li>
@else
<li>{!! link_to(($entityType == ENTITY_QUOTE ? 'quotes' : 'invoices'), trans('texts.' . ($entityType == ENTITY_QUOTE ? 'quotes' : 'invoices'))) !!}</li>
@ -34,7 +34,7 @@
<div class="row" style="min-height:195px" onkeypress="formEnterClick(event)">
<div class="col-md-4" id="col_1">
@if ($invoice && $invoice->id)
@if ($invoice->id)
<div class="form-group">
<label for="client" class="control-label col-lg-4 col-sm-4">Client</label>
<div class="col-lg-8 col-sm-8">
@ -356,7 +356,7 @@
{!! Button::primary(trans('texts.download_pdf'))->withAttributes(array('onclick' => 'onDownloadClick()'))->appendIcon(Icon::create('download-alt')) !!}
@if (!$invoice || (!$invoice->trashed() && !$invoice->client->trashed()))
@if (!$invoice->trashed())
{!! Button::success(trans("texts.save_{$entityType}"))->withAttributes(array('id' => 'saveButton', 'onclick' => 'onSaveClick()'))->appendIcon(Icon::create('floppy-disk')) !!}
{!! Button::info(trans("texts.email_{$entityType}"))->withAttributes(array('id' => 'emailButton', 'onclick' => 'onEmailClick()'))->appendIcon(Icon::create('send')) !!}
@ -528,7 +528,6 @@
var invoiceDesigns = {!! $invoiceDesigns !!};
$(function() {
// create client dictionary
for (var i=0; i<clients.length; i++) {
var client = clients[i];
@ -559,9 +558,10 @@
model.addTaxRate({!! $taxRate !!});
@endforeach
@if ($invoice)
var invoice = {!! $invoice !!};
ko.mapping.fromJS(invoice, model.invoice().mapping, model.invoice);
var invoice = {!! $invoice !!};
ko.mapping.fromJS(invoice, model.invoice().mapping, model.invoice);
@if ($invoice->id)
var invitationContactIds = {!! json_encode($invitationContactIds) !!};
var client = clientMap[invoice.client.public_id];
if (client) { // in case it's deleted
@ -613,7 +613,7 @@
ko.applyBindings(model);
onItemChange();
$('#country_id').combobox().on('change', function(e) {
var countryId = $('input[name=country_id]').val();
@ -631,8 +631,8 @@
$('#invoice_date, #due_date, #start_date, #end_date, #last_sent_date').datepicker();
@if ($client && !$invoice)
$('input[name=client]').val({{ $client->public_id }});
@if ($invoice->client && !$invoice->id)
$('input[name=client]').val({{ $invoice->client->public_id }});
@endif
var $input = $('select#client');
@ -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->hasClientNumberPattern($entityType == ENTITY_QUOTE))
@if ($account->hasClientNumberPattern($invoice))
setInvoiceNumber(selected);
@endif
} else {
@ -681,14 +681,14 @@
})(field);
}
@if ($client || $invoice || count($clients) == 0)
@if ($invoice->id || count($clients) == 0)
$('#invoice_number').focus();
@else
$('.client_select input.form-control').focus();
$('.client_select input.form-control').focus();
@endif
$('#clientModal').on('shown.bs.modal', function () {
$('#name').focus();
$('#name').focus();
}).on('hidden.bs.modal', function () {
if (model.clientBackup) {
model.loadClient(model.clientBackup);
@ -704,7 +704,7 @@
applyComboboxListeners();
@if ($client)
@if ($invoice->client->id)
$input.trigger('change');
@else
refreshPDF(true);
@ -831,7 +831,7 @@
function onDownloadClick() {
trackEvent('/activity', '/download_pdf');
var invoice = createInvoiceModel();
var design = getDesignJavascript();
var design = getDesignJavascript();
if (!design) return;
var doc = generatePDF(invoice, design, true);
var type = invoice.is_quote ? '{{ trans('texts.'.ENTITY_QUOTE) }}' : '{{ trans('texts.'.ENTITY_INVOICE) }}';
@ -945,16 +945,16 @@
submitAction('convert');
}
@if ($client && $invoice)
function onPaymentClick() {
window.location = '{{ URL::to('payments/create/' . $client->public_id . '/' . $invoice->public_id ) }}';
}
function onCreditClick() {
window.location = '{{ URL::to('credits/create/' . $client->public_id . '/' . $invoice->public_id ) }}';
}
@endif
@if ($invoice->id)
function onPaymentClick() {
window.location = '{{ URL::to('payments/create/' . $invoice->client->public_id . '/' . $invoice->public_id ) }}';
}
function onCreditClick() {
window.location = '{{ URL::to('credits/create/' . $invoice->client->public_id . '/' . $invoice->public_id ) }}';
}
@endif
function onArchiveClick() {
submitAction('archive');
}
@ -1030,14 +1030,12 @@
}
function setInvoiceNumber(client) {
@if ($invoice)
@if ($invoice->id)
return;
@endif
var number = '{{ $account->getNumberPattern($entityType == ENTITY_QUOTE, false, Auth::user()) }}';
var number = '{{ $account->getNumberPattern($invoice) }}';
number = number.replace('{$custom1}', client.custom_value1);
number = number.replace('{$custom2}', client.custom_value1);
//number = number.replace('{$clientId}', client.public_id < 0 ? '???' : padToThree(client.public_id));
//number = number.replace('{$counter}', padToFour(client.{{ $entityType }}_number_counter));
number = number.replace('{$custom2}', client.custom_value2);
model.invoice().invoice_number(number);
}

View File

@ -194,8 +194,8 @@ function ViewModel(data) {
}
function InvoiceModel(data) {
var self = this;
this.client = ko.observable(data ? false : new ClientModel());
var self = this;
this.client = ko.observable(data ? false : new ClientModel());
self.account = {!! $account !!};
self.id = ko.observable('');
self.discount = ko.observable('');
@ -209,24 +209,24 @@ function InvoiceModel(data) {
self.default_footer = ko.observable(account.invoice_footer);
self.footer_placeholder = ko.observable({{ !$invoice && $account->invoice_footer ? 'account.invoice_footer' : false}});
self.set_default_footer = ko.observable(false);
self.public_notes = ko.observable('');
self.public_notes = ko.observable('');
self.po_number = ko.observable('');
self.invoice_date = ko.observable('{{ Utils::today() }}');
self.invoice_number = ko.observable('{{ isset($invoiceNumber) ? $invoiceNumber : '' }}');
self.invoice_date = ko.observable('');
self.invoice_number = ko.observable('');
self.due_date = ko.observable('');
self.start_date = ko.observable('{{ Utils::today() }}');
self.start_date = ko.observable('');
self.end_date = ko.observable('');
self.last_sent_date = ko.observable('');
self.tax_name = ko.observable();
self.tax_rate = ko.observable();
self.is_recurring = ko.observable({{ $isRecurring ? 'true' : 'false' }});
self.is_recurring = ko.observable(false);
self.auto_bill = ko.observable();
self.invoice_status_id = ko.observable(0);
self.invoice_items = ko.observableArray();
self.amount = ko.observable(0);
self.balance = ko.observable(0);
self.invoice_design_id = ko.observable({{ $account->invoice_design_id }});
self.partial = ko.observable(0);
self.invoice_design_id = ko.observable(1);
self.partial = ko.observable(0);
self.has_tasks = ko.observable(false);
self.custom_value1 = ko.observable(0);

View File

@ -16,7 +16,7 @@ class InvoiceCest
$this->faker = Factory::create();
}
/*
public function createInvoice(AcceptanceTester $I)
{
$clientEmail = $this->faker->safeEmail;
@ -75,7 +75,7 @@ class InvoiceCest
$I->click('#lastSent');
$I->see($invoiceNumber);
}
*/
public function editInvoice(AcceptanceTester $I)
{
$I->wantTo('edit an invoice');