diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index cd811137c38f..6fc1595c03a7 100755 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -315,9 +315,9 @@ class AccountController extends \BaseController { } else if ($section == ACCOUNT_ADVANCED_SETTINGS) { - if ($subSection == ACCOUNT_CUSTOM_FIELDS) + if ($subSection == ACCOUNT_INVOICE_SETTINGS) { - return AccountController::saveCustomFields(); + return AccountController::saveInvoiceSettings(); } else if ($subSection == ACCOUNT_INVOICE_DESIGN) { @@ -342,11 +342,12 @@ class AccountController extends \BaseController { return Redirect::to('company/products'); } - private function saveCustomFields() + private function saveInvoiceSettings() { if (Auth::user()->account->isPro()) { $account = Auth::user()->account; + $account->custom_label1 = trim(Input::get('custom_label1')); $account->custom_value1 = trim(Input::get('custom_value1')); $account->custom_label2 = trim(Input::get('custom_label2')); @@ -357,12 +358,26 @@ class AccountController extends \BaseController { $account->custom_invoice_label2 = trim(Input::get('custom_invoice_label2')); $account->custom_invoice_taxes1 = Input::get('custom_invoice_taxes1') ? true : false; $account->custom_invoice_taxes2 = Input::get('custom_invoice_taxes2') ? true : false; - $account->save(); - Session::flash('message', trans('texts.updated_settings')); + $account->invoice_number_prefix = Input::get('invoice_number_prefix'); + $account->invoice_number_counter = Input::get('invoice_number_counter'); + $account->quote_number_prefix = Input::get('quote_number_prefix'); + $account->share_counter = Input::get('share_counter') ? true : false; + + if (!$account->share_counter) { + $account->quote_number_counter = Input::get('quote_number_counter'); + } + + if (!$account->share_counter && $account->invoice_number_prefix == $account->quote_number_prefix) { + Session::flash('error', trans('texts.invalid_counter')); + return Redirect::to('company/advanced_settings/invoice_settings')->withInput(); + } else { + $account->save(); + Session::flash('message', trans('texts.updated_settings')); + } } - return Redirect::to('company/advanced_settings/custom_fields'); + return Redirect::to('company/advanced_settings/invoice_settings'); } private function saveInvoiceDesign() diff --git a/app/controllers/InvoiceController.php b/app/controllers/InvoiceController.php index 590d7dca7e88..18062f31a420 100755 --- a/app/controllers/InvoiceController.php +++ b/app/controllers/InvoiceController.php @@ -151,7 +151,7 @@ class InvoiceController extends \BaseController { if ($clone) { $invoice->id = null; - $invoice->invoice_number = Auth::user()->account->getNextInvoiceNumber(); + $invoice->invoice_number = Auth::user()->account->getNextInvoiceNumber($invoice->is_quote); $invoice->balance = $invoice->amount; $method = 'POST'; $url = "{$entityType}s"; diff --git a/app/controllers/QuoteController.php b/app/controllers/QuoteController.php index 3dee961af837..683cc0bf00ec 100644 --- a/app/controllers/QuoteController.php +++ b/app/controllers/QuoteController.php @@ -60,7 +60,7 @@ class QuoteController extends \BaseController { } $client = null; - $invoiceNumber = Auth::user()->account->getNextInvoiceNumber(); + $invoiceNumber = Auth::user()->account->getNextInvoiceNumber(true); $account = Account::with('country')->findOrFail(Auth::user()->account_id); if ($clientPublicId) diff --git a/app/database/migrations/2014_10_13_054100_add_invoice_number_settings.php b/app/database/migrations/2014_10_13_054100_add_invoice_number_settings.php new file mode 100644 index 000000000000..27abab49d1d8 --- /dev/null +++ b/app/database/migrations/2014_10_13_054100_add_invoice_number_settings.php @@ -0,0 +1,63 @@ +text('invoice_number_prefix')->nullable(); + $table->integer('invoice_number_counter')->default(1)->nullable(); + + $table->text('quote_number_prefix')->nullable(); + $table->integer('quote_number_counter')->default(1)->nullable(); + + $table->boolean('share_counter')->default(true); + }); + + // set initial counter value for accounts with invoices + $accounts = DB::table('accounts')->lists('id'); + + foreach ($accounts as $accountId) { + + $invoiceNumbers = DB::table('invoices')->where('account_id', $accountId)->lists('invoice_number'); + $max = 0; + + foreach ($invoiceNumbers as $invoiceNumber) { + $number = intval(preg_replace('/[^0-9]/', '', $invoiceNumber)); + $max = max($max, $number); + } + + DB::table('accounts')->where('id', $accountId)->update(['invoice_number_counter' => ++$max]); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('accounts', function($table) + { + $table->dropColumn('invoice_number_prefix'); + $table->dropColumn('invoice_number_counter'); + + $table->dropColumn('quote_number_prefix'); + $table->dropColumn('quote_number_counter'); + + $table->dropColumn('share_counter'); + }); + + } + +} diff --git a/app/lang/de/texts.php b/app/lang/de/texts.php index f7ad286a6072..eafe2549cfa3 100644 --- a/app/lang/de/texts.php +++ b/app/lang/de/texts.php @@ -424,5 +424,14 @@ return array( 'sample_data' => 'Sample data shown', 'hide' => 'Hide', 'new_version_available' => 'A new version of :releases_link is available. You\'re running v:user_version, the latest is v:latest_version', + + 'invoice_settings' => 'Invoice Settings', + 'invoice_number_prefix' => 'Invoice Number Prefix', + 'invoice_number_counter' => 'Invoice Number Counter', + 'quote_number_prefix' => 'Quote Number Prefix', + 'quote_number_counter' => 'Quote Number Counter', + 'share_invoice_counter' => 'Share invoice counter', + 'invoice_issued_to' => 'Invoice issued to', + 'invalid_counter' => 'To prevent a possible conflict please set either an invoice or quote number prefix', ); diff --git a/app/lang/en/texts.php b/app/lang/en/texts.php index a6d3b9f495e5..2b60a0101375 100644 --- a/app/lang/en/texts.php +++ b/app/lang/en/texts.php @@ -433,4 +433,14 @@ return array( 'hide' => 'Hide', 'new_version_available' => 'A new version of :releases_link is available. You\'re running v:user_version, the latest is v:latest_version', + 'invoice_settings' => 'Invoice Settings', + 'invoice_number_prefix' => 'Invoice Number Prefix', + 'invoice_number_counter' => 'Invoice Number Counter', + 'quote_number_prefix' => 'Quote Number Prefix', + 'quote_number_counter' => 'Quote Number Counter', + 'share_invoice_counter' => 'Share invoice counter', + 'invoice_issued_to' => 'Invoice issued to', + 'invalid_counter' => 'To prevent a possible conflict please set either an invoice or quote number prefix', + + ); \ No newline at end of file diff --git a/app/lang/es/texts.php b/app/lang/es/texts.php index 668e2abb590b..30fd20b6c0c6 100644 --- a/app/lang/es/texts.php +++ b/app/lang/es/texts.php @@ -422,5 +422,14 @@ return array( 'sample_data' => 'Sample data shown', 'hide' => 'Hide', 'new_version_available' => 'A new version of :releases_link is available. You\'re running v:user_version, the latest is v:latest_version', + + 'invoice_settings' => 'Invoice Settings', + 'invoice_number_prefix' => 'Invoice Number Prefix', + 'invoice_number_counter' => 'Invoice Number Counter', + 'quote_number_prefix' => 'Quote Number Prefix', + 'quote_number_counter' => 'Quote Number Counter', + 'share_invoice_counter' => 'Share invoice counter', + 'invoice_issued_to' => 'Invoice issued to', + 'invalid_counter' => 'To prevent a possible conflict please set either an invoice or quote number prefix', ); diff --git a/app/lang/fr/texts.php b/app/lang/fr/texts.php index e5cc815f7093..f0bacd42185e 100644 --- a/app/lang/fr/texts.php +++ b/app/lang/fr/texts.php @@ -425,5 +425,14 @@ return array( 'hide' => 'Hide', 'new_version_available' => 'A new version of :releases_link is available. You\'re running v:user_version, the latest is v:latest_version', + + 'invoice_settings' => 'Invoice Settings', + 'invoice_number_prefix' => 'Invoice Number Prefix', + 'invoice_number_counter' => 'Invoice Number Counter', + 'quote_number_prefix' => 'Quote Number Prefix', + 'quote_number_counter' => 'Quote Number Counter', + 'share_invoice_counter' => 'Share invoice counter', + 'invoice_issued_to' => 'Invoice issued to', + 'invalid_counter' => 'To prevent a possible conflict please set either an invoice or quote number prefix', ); diff --git a/app/lang/it/texts.php b/app/lang/it/texts.php index 8a6df978935c..1d7890a569fd 100644 --- a/app/lang/it/texts.php +++ b/app/lang/it/texts.php @@ -425,5 +425,14 @@ return array( 'hide' => 'Hide', 'new_version_available' => 'A new version of :releases_link is available. You\'re running v:user_version, the latest is v:latest_version', + 'invoice_settings' => 'Invoice Settings', + 'invoice_number_prefix' => 'Invoice Number Prefix', + 'invoice_number_counter' => 'Invoice Number Counter', + 'quote_number_prefix' => 'Quote Number Prefix', + 'quote_number_counter' => 'Quote Number Counter', + 'share_invoice_counter' => 'Share invoice counter', + 'invoice_issued_to' => 'Invoice issued to', + 'invalid_counter' => 'To prevent a possible conflict please set either an invoice or quote number prefix', + ); diff --git a/app/lang/lt/texts.php b/app/lang/lt/texts.php index f345dd6dd993..9b6056163ebb 100644 --- a/app/lang/lt/texts.php +++ b/app/lang/lt/texts.php @@ -433,6 +433,15 @@ return array( 'hide' => 'Hide', 'new_version_available' => 'A new version of :releases_link is available. You\'re running v:user_version, the latest is v:latest_version', + 'invoice_settings' => 'Invoice Settings', + 'invoice_number_prefix' => 'Invoice Number Prefix', + 'invoice_number_counter' => 'Invoice Number Counter', + 'quote_number_prefix' => 'Quote Number Prefix', + 'quote_number_counter' => 'Quote Number Counter', + 'share_invoice_counter' => 'Share invoice counter', + 'invoice_issued_to' => 'Invoice issued to', + 'invalid_counter' => 'To prevent a possible conflict please set either an invoice or quote number prefix', + ); diff --git a/app/lang/nb_NO/texts.php b/app/lang/nb_NO/texts.php index 44b5e0bbc354..7bcba4ebcb47 100644 --- a/app/lang/nb_NO/texts.php +++ b/app/lang/nb_NO/texts.php @@ -433,5 +433,14 @@ return array( 'hide' => 'Hide', 'new_version_available' => 'A new version of :releases_link is available. You\'re running v:user_version, the latest is v:latest_version', + 'invoice_settings' => 'Invoice Settings', + 'invoice_number_prefix' => 'Invoice Number Prefix', + 'invoice_number_counter' => 'Invoice Number Counter', + 'quote_number_prefix' => 'Quote Number Prefix', + 'quote_number_counter' => 'Quote Number Counter', + 'share_invoice_counter' => 'Share invoice counter', + 'invoice_issued_to' => 'Invoice issued to', + 'invalid_counter' => 'To prevent a possible conflict please set either an invoice or quote number prefix', + ); \ No newline at end of file diff --git a/app/lang/nl/texts.php b/app/lang/nl/texts.php index 319c7ccfea21..eab50cef2afa 100644 --- a/app/lang/nl/texts.php +++ b/app/lang/nl/texts.php @@ -425,6 +425,15 @@ return array( 'sample_data' => 'Sample data shown', 'hide' => 'Hide', 'new_version_available' => 'A new version of :releases_link is available. You\'re running v:user_version, the latest is v:latest_version', + + 'invoice_settings' => 'Invoice Settings', + 'invoice_number_prefix' => 'Invoice Number Prefix', + 'invoice_number_counter' => 'Invoice Number Counter', + 'quote_number_prefix' => 'Quote Number Prefix', + 'quote_number_counter' => 'Quote Number Counter', + 'share_invoice_counter' => 'Share invoice counter', + 'invoice_issued_to' => 'Invoice issued to', + 'invalid_counter' => 'To prevent a possible conflict please set either an invoice or quote number prefix', ); diff --git a/app/lang/pt_BR/texts.php b/app/lang/pt_BR/texts.php index 0c8b6fd58886..fccd9b2871c2 100644 --- a/app/lang/pt_BR/texts.php +++ b/app/lang/pt_BR/texts.php @@ -413,6 +413,15 @@ return array( 'sample_data' => 'Sample data shown', 'hide' => 'Hide', 'new_version_available' => 'A new version of :releases_link is available. You\'re running v:user_version, the latest is v:latest_version', + + 'invoice_settings' => 'Invoice Settings', + 'invoice_number_prefix' => 'Invoice Number Prefix', + 'invoice_number_counter' => 'Invoice Number Counter', + 'quote_number_prefix' => 'Quote Number Prefix', + 'quote_number_counter' => 'Quote Number Counter', + 'share_invoice_counter' => 'Share invoice counter', + 'invoice_issued_to' => 'Invoice issued to', + 'invalid_counter' => 'To prevent a possible conflict please set either an invoice or quote number prefix', ); diff --git a/app/models/Account.php b/app/models/Account.php index 5cfdb4365a9c..cf80925b018b 100755 --- a/app/models/Account.php +++ b/app/models/Account.php @@ -141,28 +141,25 @@ class Account extends Eloquent return $height; } - public function getNextInvoiceNumber() - { - $invoices = Invoice::withTrashed()->scope(false, $this->id)->get(['invoice_number']); + public function getNextInvoiceNumber($isQuote = false) + { + $counter = $isQuote && !$this->share_counter ? $this->quote_number_counter : $this->invoice_number_counter; + $prefix = $isQuote ? $this->quote_number_prefix : $this->invoice_number_prefix; - $max = 0; - - foreach ($invoices as $invoice) - { - $number = intval(preg_replace("/[^0-9]/", "", $invoice->invoice_number)); - $max = max($max, $number); - } - - if ($max > 0) - { - return str_pad($max+1, 4, "0", STR_PAD_LEFT); - } - else - { - return DEFAULT_INVOICE_NUMBER; - } + return $prefix . str_pad($counter, 4, "0", STR_PAD_LEFT); } + public function incrementCounter($isQuote = false) + { + if ($isQuote && !$this->share_counter) { + $this->quote_number_counter += 1; + } else { + $this->invoice_number_counter += 1; + } + + $this->save(); + } + public function getLocale() { $language = Language::remember(DEFAULT_QUERY_CACHE)->where('id', '=', $this->account->language_id)->first(); @@ -208,6 +205,7 @@ class Account extends Eloquent 'quote_date', 'quote_number', 'total', + 'invoice_issued_to', ]; foreach ($fields as $field) diff --git a/app/models/Invoice.php b/app/models/Invoice.php index 9ae5c8e6ce7a..4438fef6b979 100755 --- a/app/models/Invoice.php +++ b/app/models/Invoice.php @@ -217,6 +217,7 @@ class Invoice extends EntityModel Invoice::created(function($invoice) { + $invoice->account->incrementCounter($invoice->is_quote); Activity::createInvoice($invoice); }); diff --git a/app/ninja/repositories/InvoiceRepository.php b/app/ninja/repositories/InvoiceRepository.php index dad05fbd6fa4..b462f2dff230 100755 --- a/app/ninja/repositories/InvoiceRepository.php +++ b/app/ninja/repositories/InvoiceRepository.php @@ -359,12 +359,11 @@ class InvoiceRepository $clone = Invoice::createNew($invoice); $clone->balance = $invoice->amount; - $clone->invoice_number = $invoice->account->getNextInvoiceNumber(); + $clone->invoice_number = $invoice->account->getNextInvoiceNumber($invoice->is_quote); foreach ([ 'client_id', 'discount', - //'shipping', 'invoice_date', 'po_number', 'due_date', @@ -378,7 +377,11 @@ class InvoiceRepository 'tax_name', 'tax_rate', 'amount', - 'is_quote'] as $field) + 'is_quote', + 'custom_value1', + 'custom_value2', + 'custom_taxes1', + 'custom_taxes2'] as $field) { $clone->$field = $invoice->$field; } diff --git a/app/routes.php b/app/routes.php index 9f75562d1e58..84874dfcc920 100755 --- a/app/routes.php +++ b/app/routes.php @@ -1,5 +1,6 @@ hide_quantity)) }} {{ Former::populateField('hide_paid_to_date', intval($account->hide_paid_to_date)) }} - {{ Former::legend('invoice_options') }} - {{ Former::checkbox('hide_quantity')->text(trans('texts.hide_quantity_help')) }} - {{ Former::checkbox('hide_paid_to_date')->text(trans('texts.hide_paid_to_date_help')) }} - -

 

-

 

- {{ Former::legend('invoice_design') }} {{ Former::select('invoice_design_id')->style('display:inline;width:120px') ->fromQuery($invoiceDesigns, 'name', 'id') }} @@ -80,6 +73,13 @@

 

 

+ {{ Former::legend('invoice_options') }} + {{ Former::checkbox('hide_quantity')->text(trans('texts.hide_quantity_help')) }} + {{ Former::checkbox('hide_paid_to_date')->text(trans('texts.hide_paid_to_date_help')) }} + +

 

+

 

+ @if (Auth::user()->isPro()) {{ Former::actions( Button::lg_success_submit(trans('texts.save'))->append_with_icon('floppy-disk') ) }} @else diff --git a/app/views/accounts/custom_fields.blade.php b/app/views/accounts/invoice_settings.blade.php similarity index 57% rename from app/views/accounts/custom_fields.blade.php rename to app/views/accounts/invoice_settings.blade.php index b856a5337995..bf789be9be32 100644 --- a/app/views/accounts/custom_fields.blade.php +++ b/app/views/accounts/invoice_settings.blade.php @@ -8,6 +8,8 @@ {{ Former::populate($account) }} {{ Former::populateField('custom_invoice_taxes1', intval($account->custom_invoice_taxes1)) }} {{ Former::populateField('custom_invoice_taxes2', intval($account->custom_invoice_taxes2)) }} + {{ Former::populateField('share_counter', intval($account->share_counter)) }} + {{ Former::legend('invoice_fields') }} {{ Former::text('custom_invoice_label1')->label(trans('texts.field_label')) @@ -27,17 +29,45 @@

 

{{ Former::text('custom_label2')->label(trans('texts.field_label')) }} {{ Former::text('custom_value2')->label(trans('texts.field_value')) }} +

 

+ + {{ Former::legend('invoice_number') }} + {{ Former::text('invoice_number_prefix')->label(trans('texts.invoice_number_prefix')) }} + {{ Former::text('invoice_number_counter')->label(trans('texts.invoice_number_counter')) }} +

 

+ + {{ Former::legend('quote_number') }} + {{ Former::text('quote_number_prefix')->label(trans('texts.quote_number_prefix')) }} + {{ Former::text('quote_number_counter')->label(trans('texts.quote_number_counter')) + ->append(Former::checkbox('share_counter')->raw()->onclick('setQuoteNumberEnabled()') . ' ' . trans('texts.share_invoice_counter')) }} +

 

@if (Auth::user()->isPro()) {{ Former::actions( Button::lg_success_submit(trans('texts.save'))->append_with_icon('floppy-disk') ) }} @else @endif {{ Former::close() }} + + + + @stop \ No newline at end of file diff --git a/app/views/accounts/nav.blade.php b/app/views/accounts/nav.blade.php index 627c0cf8ab03..66fca0dcc43b 100755 --- a/app/views/accounts/nav.blade.php +++ b/app/views/accounts/nav.blade.php @@ -8,7 +8,7 @@ {{ HTML::nav_link('company/products', 'product_library') }} {{ HTML::nav_link('company/notifications', 'notifications') }} {{ HTML::nav_link('company/import_export', 'import_export', 'company/import_map') }} - {{ HTML::nav_link('company/advanced_settings/custom_fields', 'advanced_settings', '*/advanced_settings/*') }} + {{ HTML::nav_link('company/advanced_settings/invoice_settings', 'advanced_settings', '*/advanced_settings/*') }}
diff --git a/app/views/accounts/nav_advanced.blade.php b/app/views/accounts/nav_advanced.blade.php index 498a28d43b26..e24ea16f09fb 100644 --- a/app/views/accounts/nav_advanced.blade.php +++ b/app/views/accounts/nav_advanced.blade.php @@ -1,5 +1,5 @@