1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00

Support reseting counters

This commit is contained in:
Hillel Coren 2017-03-23 11:39:44 +02:00
parent 7f02493b6d
commit 1df20adca4
10 changed files with 112 additions and 9 deletions

View File

@ -2,6 +2,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Models\Account;
use App\Models\Invoice; use App\Models\Invoice;
use App\Ninja\Mailers\ContactMailer as Mailer; use App\Ninja\Mailers\ContactMailer as Mailer;
use App\Ninja\Repositories\InvoiceRepository; use App\Ninja\Repositories\InvoiceRepository;
@ -60,6 +61,15 @@ class SendRecurringInvoices extends Command
$this->info(date('Y-m-d').' Running SendRecurringInvoices...'); $this->info(date('Y-m-d').' Running SendRecurringInvoices...');
$today = new DateTime(); $today = new DateTime();
// check for counter resets
$accounts = Account::where('reset_counter_frequency_id', '>', 0)
->orderBy('id', 'asc')
->get();
foreach ($accounts as $account) {
$account->checkCounterReset();
}
$invoices = Invoice::with('account.timezone', 'invoice_items', 'client', 'user') $invoices = Invoice::with('account.timezone', 'invoice_items', 'client', 'user')
->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS TRUE AND is_public IS TRUE AND frequency_id > 0 AND start_date <= ? AND (end_date IS NULL OR end_date >= ?)', [$today, $today]) ->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS TRUE AND is_public IS TRUE AND frequency_id > 0 AND start_date <= ? AND (end_date IS NULL OR end_date >= ?)', [$today, $today])
->orderBy('id', 'asc') ->orderBy('id', 'asc')
@ -74,7 +84,8 @@ class SendRecurringInvoices extends Command
continue; continue;
} }
$recurInvoice->account->loadLocalizationSettings($recurInvoice->client); $account = $recurInvoice->account;
$account->loadLocalizationSettings($recurInvoice->client);
$invoice = $this->invoiceRepo->createRecurringInvoice($recurInvoice); $invoice = $this->invoiceRepo->createRecurringInvoice($recurInvoice);
if ($invoice && ! $invoice->isPaid()) { if ($invoice && ! $invoice->isPaid()) {

View File

@ -932,6 +932,8 @@ class AccountController extends BaseController
$account->client_number_prefix = trim(Input::get('client_number_prefix')); $account->client_number_prefix = trim(Input::get('client_number_prefix'));
$account->client_number_pattern = trim(Input::get('client_number_pattern')); $account->client_number_pattern = trim(Input::get('client_number_pattern'));
$account->client_number_counter = Input::get('client_number_counter'); $account->client_number_counter = Input::get('client_number_counter');
$account->reset_counter_frequency_id = Input::get('reset_counter_frequency_id');
$account->reset_counter_date = $account->reset_counter_frequency_id ? Utils::toSqlDate(Input::get('reset_counter_date')) : null;
if (Input::has('recurring_hour')) { if (Input::has('recurring_hour')) {
$account->recurring_hour = Input::get('recurring_hour'); $account->recurring_hour = Input::get('recurring_hour');

View File

@ -172,6 +172,7 @@ class Account extends Eloquent
'reset_counter_frequency_id', 'reset_counter_frequency_id',
'payment_type_id', 'payment_type_id',
'gateway_fee_location', 'gateway_fee_location',
'reset_counter_date',
]; ];
/** /**

View File

@ -252,4 +252,50 @@ trait GeneratesNumbers
{ {
return $this->hasFeature(FEATURE_INVOICE_SETTINGS) && $this->client_number_counter > 0; return $this->hasFeature(FEATURE_INVOICE_SETTINGS) && $this->client_number_counter > 0;
} }
public function checkCounterReset()
{
if (! $this->reset_counter_frequency_id || ! $this->reset_counter_date) {
return false;
}
$timezone = $this->getTimezone();
$resetDate = Carbon::parse($this->reset_counter_date, $timezone);
if (! $resetDate->isToday()) {
return false;
}
switch ($this->reset_counter_frequency_id) {
case FREQUENCY_WEEKLY:
$resetDate->addWeek();
break;
case FREQUENCY_TWO_WEEKS:
$resetDate->addWeeks(2);
break;
case FREQUENCY_FOUR_WEEKS:
$resetDate->addWeeks(4);
break;
case FREQUENCY_MONTHLY:
$resetDate->addMonth();
break;
case FREQUENCY_TWO_MONTHS:
$resetDate->addMonths(2);
break;
case FREQUENCY_THREE_MONTHS:
$resetDate->addMonths(3);
break;
case FREQUENCY_SIX_MONTHS:
$resetDate->addMonths(6);
break;
case FREQUENCY_ANNUALLY:
$resetDate->addYear();
break;
}
$this->reset_counter_date = $resetDate->format('Y-m-d');
$this->invoice_number_counter = 1;
$this->quote_number_counter = 1;
$this->save();
}
} }

View File

@ -264,6 +264,7 @@ class AccountTransformer extends EntityTransformer
'reset_counter_frequency_id' => (int) $account->reset_counter_frequency_id, 'reset_counter_frequency_id' => (int) $account->reset_counter_frequency_id,
'payment_type_id' => (int) $account->payment_type_id, 'payment_type_id' => (int) $account->payment_type_id,
'gateway_fee_location' => $account->gateway_fee_location, 'gateway_fee_location' => $account->gateway_fee_location,
'reset_counter_date' => $account->reset_counter_date,
]; ];
} }
} }

View File

@ -13,7 +13,10 @@ class AddGatewayFeeLocation extends Migration
public function up() public function up()
{ {
Schema::table('accounts', function ($table) { Schema::table('accounts', function ($table) {
$table->dropColumn('auto_wrap');
$table->dropColumn('utf8_invoices');
$table->enum('gateway_fee_location', [FEE_LOCATION_CHARGE1, FEE_LOCATION_CHARGE2, FEE_LOCATION_ITEM])->nullable(); $table->enum('gateway_fee_location', [FEE_LOCATION_CHARGE1, FEE_LOCATION_CHARGE2, FEE_LOCATION_ITEM])->nullable();
$table->date('reset_counter_date')->nullable();
}); });
} }
@ -26,6 +29,7 @@ class AddGatewayFeeLocation extends Migration
{ {
Schema::table('accounts', function ($table) { Schema::table('accounts', function ($table) {
$table->dropColumn('gateway_fee_location'); $table->dropColumn('gateway_fee_location');
$table->dropColumn('reset_counter_date');
}); });
} }
} }

File diff suppressed because one or more lines are too long

View File

@ -2430,6 +2430,9 @@ $LANG = array(
'data' => 'Data', 'data' => 'Data',
'imported_settings' => 'Successfully imported settings', 'imported_settings' => 'Successfully imported settings',
'lang_Greek' => 'Greek', 'lang_Greek' => 'Greek',
'reset_counter' => 'Reset Counter',
'next_reset' => 'Next Reset',
'reset_counter_help' => 'Automatically reset the invoice and quote counters.',
); );

View File

@ -47,6 +47,9 @@
<li role="presentation"> <li role="presentation">
<a href="#recurring_invoice_number" aria-controls="recurring_invoice_number" role="tab" data-toggle="tab">{{ trans('texts.recurring_invoice_number') }}</a> <a href="#recurring_invoice_number" aria-controls="recurring_invoice_number" role="tab" data-toggle="tab">{{ trans('texts.recurring_invoice_number') }}</a>
</li> </li>
<li role="presentation">
<a href="#reset_counter" aria-controls="reset_counter" role="tab" data-toggle="tab">{{ trans('texts.reset_counter') }}</a>
</li>
</ul> </ul>
</div> </div>
<div class="tab-content"> <div class="tab-content">
@ -73,7 +76,6 @@
->label(trans('texts.counter')) ->label(trans('texts.counter'))
->help(trans('texts.invoice_number_help') . ' ' . ->help(trans('texts.invoice_number_help') . ' ' .
trans('texts.next_invoice_number', ['number' => $account->previewNextInvoiceNumber()])) !!} trans('texts.next_invoice_number', ['number' => $account->previewNextInvoiceNumber()])) !!}
</div> </div>
</div> </div>
<div role="tabpanel" class="tab-pane" id="quote_number"> <div role="tabpanel" class="tab-pane" id="quote_number">
@ -150,6 +152,24 @@
</div> </div>
</div> </div>
<div role="tabpanel" class="tab-pane" id="reset_counter">
<div class="panel-body">
{!! Former::select('reset_counter_frequency_id')
->onchange('onResetFrequencyChange()')
->label('frequency')
->addOption(trans('texts.never'), '')
->options(\App\Models\Frequency::selectOptions())
->help('reset_counter_help') !!}
{!! Former::text('reset_counter_date')
->label('next_reset')
->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT, DEFAULT_DATE_PICKER_FORMAT))
->addGroupClass('reset_counter_date_group')
->append('<i class="glyphicon glyphicon-calendar"></i>') !!}
</div>
</div>
</div> </div>
</div> </div>
@ -410,6 +430,15 @@
} }
} }
function onResetFrequencyChange() {
var val = $('#reset_counter_frequency_id').val();
if (val) {
$('.reset_counter_date_group').show();
} else {
$('.reset_counter_date_group').hide();
}
}
$('.number-pattern .input-group-addon').click(function() { $('.number-pattern .input-group-addon').click(function() {
$('#patternHelpModal').modal('show'); $('#patternHelpModal').modal('show');
}); });
@ -420,6 +449,13 @@
onQuoteNumberTypeChange(); onQuoteNumberTypeChange();
onClientNumberTypeChange(); onClientNumberTypeChange();
onClientNumberEnabled(); onClientNumberEnabled();
onResetFrequencyChange();
$('#reset_counter_date').datepicker('update', '{{ Utils::fromSqlDate($account->reset_counter_date) ?: 'new Date()' }}');
$('.reset_counter_date_group .input-group-addon').click(function() {
toggleDatePicker('reset_counter_date');
});
}); });
</script> </script>

View File

@ -165,7 +165,7 @@
</div> </div>
@if ($entityType == ENTITY_INVOICE) @if ($entityType == ENTITY_INVOICE)
<div data-bind="visible: is_recurring" style="display: none"> <div data-bind="visible: is_recurring" style="display: none">
{!! Former::select('frequency_id')->options($frequencies)->data_bind("value: frequency_id") {!! Former::select('frequency_id')->label('frequency')->options($frequencies)->data_bind("value: frequency_id")
->appendIcon('question-sign')->addGroupClass('frequency_id')->onchange('onFrequencyChange()') !!} ->appendIcon('question-sign')->addGroupClass('frequency_id')->onchange('onFrequencyChange()') !!}
{!! Former::text('start_date')->data_bind("datePicker: start_date, valueUpdate: 'afterkeydown'") {!! Former::text('start_date')->data_bind("datePicker: start_date, valueUpdate: 'afterkeydown'")
->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT, DEFAULT_DATE_PICKER_FORMAT))->appendIcon('calendar')->addGroupClass('start_date') !!} ->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT, DEFAULT_DATE_PICKER_FORMAT))->appendIcon('calendar')->addGroupClass('start_date') !!}