1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-19 16:01:34 +02:00

Endless reminders

This commit is contained in:
Hillel Coren 2017-12-26 12:03:23 +02:00
parent e2d4c0a22b
commit d6c8191b7e
7 changed files with 87 additions and 11 deletions

View File

@ -126,6 +126,7 @@ class SendReminders extends Command
continue;
}
// standard reminders
$invoices = $this->invoiceRepo->findNeedingReminding($account);
$this->info($account->name . ': ' . count($invoices) . ' invoices found');
@ -135,6 +136,15 @@ class SendReminders extends Command
$this->mailer->sendInvoice($invoice, $reminder);
}
}
// endless reminders
$invoices = $this->invoiceRepo->findNeedingEndlessReminding($account);
$this->info($account->name . ': ' . count($invoices) . ' endless invoices found');
foreach ($invoices as $invoice) {
$this->info('Send email: ' . $invoice->id);
$this->mailer->sendInvoice($invoice, 'reminder4');
}
}
}

View File

@ -6,6 +6,7 @@ use Illuminate\Queue\Events\JobExceptionOccurred;
use App\Events\InvoiceInvitationWasViewed;
use App\Events\InvoiceWasCreated;
use App\Events\InvoiceWasUpdated;
use App\Events\InvoiceWasEmailed;
use App\Events\PaymentFailed;
use App\Events\PaymentWasCreated;
use App\Events\PaymentWasDeleted;
@ -60,6 +61,16 @@ class InvoiceListener
$invitation->markViewed();
}
/**
* @param InvoiceWasEmailed $event
*/
public function emailedInvoice(InvoiceWasEmailed $event)
{
$invoice = $event->invoice;
$invoice->last_sent_date = date('Y-m-d');
$invoice->save();
}
/**
* @param PaymentWasCreated $event
*/

View File

@ -697,7 +697,7 @@ class AccountRepository
public function findWithReminders()
{
return Account::whereRaw('enable_reminder1 = 1 OR enable_reminder2 = 1 OR enable_reminder3 = 1')->get();
return Account::whereRaw('enable_reminder1 = 1 OR enable_reminder2 = 1 OR enable_reminder3 = 1 OR enable_reminder4 = 1')->get();
}
public function findWithFees()

View File

@ -1204,6 +1204,40 @@ class InvoiceRepository extends BaseRepository
return $invoices;
}
public function findNeedingEndlessReminding(Account $account)
{
$frequencyId = $account->account_email_settings->frequency_id_reminder4;
$frequency = Utils::getFromCache($frequencyId, 'frequencies');
$lastSentDate = date_create();
$lastSentDate->sub(date_interval_create_from_date_string($frequency->date_interval));
$invoices = Invoice::invoiceType(INVOICE_TYPE_STANDARD)
->with('client', 'invoice_items')
->whereHas('client', function ($query) {
$query->whereSendReminders(true);
})
->whereAccountId($account->id)
->where('balance', '>', 0)
->where('is_recurring', '=', false)
->whereIsPublic(true)
->where('last_sent_date', '<', $lastSentDate);
for ($i=1; $i<=3; $i++) {
if (!$account->{"enable_reminder{$i}"}) {
continue;
}
$field = $account->{"field_reminder{$i}"} == REMINDER_FIELD_DUE_DATE ? 'due_date' : 'invoice_date';
$date = date_create();
if ($account->{"direction_reminder{$i}"} == REMINDER_DIRECTION_AFTER) {
$date->sub(date_interval_create_from_date_string($account->{"num_days_reminder{$i}"} . ' days'));
}
$invoices->where($field, '<', $date);
}
return $invoices->get();
}
public function clearGatewayFee($invoice)
{
$account = $invoice->account;

View File

@ -56,6 +56,7 @@ class EventServiceProvider extends ServiceProvider
],
'App\Events\InvoiceWasEmailed' => [
'App\Listeners\NotificationListener@emailedInvoice',
'App\Listeners\InvoiceListener@emailedInvoice',
],
'App\Events\InvoiceInvitationWasEmailed' => [
'App\Listeners\ActivityListener@emailedInvoice',

View File

@ -69,6 +69,20 @@ class AddRemember2faToken extends Migration
$table->unsignedInteger('frequency_id_reminder4')->nullable();
});
Schema::table('frequencies', function ($table) {
$table->string('date_interval')->nullable();
});
DB::statement("update invoices, (
select max(created_at) created_at, invoice_id
from activities
where activity_type_id = 6
group by invoice_id
) as activities
set invoices.last_sent_date = activities.created_at
where invoices.id = activities.invoice_id
and invoices.is_recurring = 0
and invoices.invoice_type_id = 1");
}
/**
@ -117,5 +131,10 @@ class AddRemember2faToken extends Migration
$table->dropColumn('email_template_reminder4');
$table->dropColumn('frequency_id_reminder4');
});
Schema::table('frequencies', function ($table) {
$table->dropColumn('date_interval');
});
}
}

View File

@ -9,21 +9,22 @@ class FrequencySeeder extends Seeder
Eloquent::unguard();
$frequencies = [
['name' => 'Weekly'],
['name' => 'Two weeks'],
['name' => 'Four weeks'],
['name' => 'Monthly'],
['name' => 'Two months'],
['name' => 'Three months'],
['name' => 'Four months'],
['name' => 'Six months'],
['name' => 'Annually'],
['name' => 'Weekly', 'date_interval' => '1 week'],
['name' => 'Two weeks', 'date_interval' => '2 weeks'],
['name' => 'Four weeks', 'date_interval' => '4 weeks'],
['name' => 'Monthly', 'date_interval' => '1 month'],
['name' => 'Two months', 'date_interval' => '2 months'],
['name' => 'Three months', 'date_interval' => '3 months'],
['name' => 'Four months', 'date_interval' => '4 months'],
['name' => 'Six months', 'date_interval' => '6 months'],
['name' => 'Annually', 'date_interval' => '1 year'],
];
foreach ($frequencies as $frequency) {
$record = Frequency::whereName($frequency['name'])->first();
if ($record) {
//$record->save();
$record->date_interval = $frequency['date_interval'];
$record->save();
} else {
Frequency::create($frequency);
}