1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Run 'bill later' once daily

This commit is contained in:
Hillel Coren 2018-09-23 09:08:48 +03:00
parent da50fc400c
commit 21f287ca00
2 changed files with 38 additions and 38 deletions

View File

@ -7,7 +7,6 @@ use App\Models\Invoice;
use App\Models\RecurringExpense;
use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Repositories\RecurringExpenseRepository;
use App\Services\PaymentService;
use App\Jobs\SendInvoiceEmail;
use DateTime;
use Illuminate\Console\Command;
@ -36,23 +35,16 @@ class SendRecurringInvoices extends Command
*/
protected $invoiceRepo;
/**
* @var PaymentService
*/
protected $paymentService;
/**
* SendRecurringInvoices constructor.
*
* @param InvoiceRepository $invoiceRepo
* @param PaymentService $paymentService
*/
public function __construct(InvoiceRepository $invoiceRepo, PaymentService $paymentService, RecurringExpenseRepository $recurringExpenseRepo)
public function __construct(InvoiceRepository $invoiceRepo, RecurringExpenseRepository $recurringExpenseRepo)
{
parent::__construct();
$this->invoiceRepo = $invoiceRepo;
$this->paymentService = $paymentService;
$this->recurringExpenseRepo = $recurringExpenseRepo;
}
@ -66,7 +58,6 @@ class SendRecurringInvoices extends Command
$this->resetCounters();
$this->createInvoices();
$this->billInvoices();
$this->createExpenses();
$this->info(date('r') . ' Done');
@ -123,33 +114,6 @@ class SendRecurringInvoices extends Command
}
}
private function billInvoices()
{
$today = new DateTime();
$delayedAutoBillInvoices = Invoice::with('account.timezone', 'recurring_invoice', 'invoice_items', 'client', 'user')
->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS FALSE AND is_public IS TRUE
AND balance > 0 AND due_date = ? AND recurring_invoice_id IS NOT NULL',
[$today->format('Y-m-d')])
->orderBy('invoices.id', 'asc')
->get();
$this->info(date('r ') . $delayedAutoBillInvoices->count() . ' due recurring invoice instance(s) found');
/** @var Invoice $invoice */
foreach ($delayedAutoBillInvoices as $invoice) {
if ($invoice->isPaid()) {
continue;
}
if ($invoice->getAutoBillEnabled() && $invoice->client->autoBillLater()) {
$this->info(date('r') . ' Processing Autobill-delayed Invoice: ' . $invoice->id);
Auth::loginUsingId($invoice->activeUser()->id);
$this->paymentService->autoBillInvoice($invoice);
Auth::logout();
}
}
}
private function createExpenses()
{
$today = new DateTime();

View File

@ -8,6 +8,7 @@ use Str;
use Cache;
use Utils;
use Exception;
use DateTime;
use App\Jobs\SendInvoiceEmail;
use App\Models\Invoice;
use App\Models\Currency;
@ -15,6 +16,7 @@ use App\Ninja\Mailers\UserMailer;
use App\Ninja\Repositories\AccountRepository;
use App\Ninja\Repositories\InvoiceRepository;
use App\Models\ScheduledReport;
use App\Services\PaymentService;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use App\Jobs\ExportReportResults;
@ -45,6 +47,11 @@ class SendReminders extends Command
*/
protected $accountRepo;
/**
* @var PaymentService
*/
protected $paymentService;
/**
* SendReminders constructor.
*
@ -52,10 +59,11 @@ class SendReminders extends Command
* @param InvoiceRepository $invoiceRepo
* @param accountRepository $accountRepo
*/
public function __construct(InvoiceRepository $invoiceRepo, AccountRepository $accountRepo, UserMailer $userMailer)
public function __construct(InvoiceRepository $invoiceRepo, PaymentService $paymentService, AccountRepository $accountRepo, UserMailer $userMailer)
{
parent::__construct();
$this->paymentService = $paymentService;
$this->invoiceRepo = $invoiceRepo;
$this->accountRepo = $accountRepo;
$this->userMailer = $userMailer;
@ -69,6 +77,7 @@ class SendReminders extends Command
config(['database.default' => $database]);
}
$this->billInvoices();
$this->chargeLateFees();
$this->sendReminderEmails();
$this->sendScheduledReports();
@ -85,6 +94,33 @@ class SendReminders extends Command
}
}
private function billInvoices()
{
$today = new DateTime();
$delayedAutoBillInvoices = Invoice::with('account.timezone', 'recurring_invoice', 'invoice_items', 'client', 'user')
->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS FALSE AND is_public IS TRUE
AND balance > 0 AND due_date = ? AND recurring_invoice_id IS NOT NULL',
[$today->format('Y-m-d')])
->orderBy('invoices.id', 'asc')
->get();
$this->info(date('r ') . $delayedAutoBillInvoices->count() . ' due recurring invoice instance(s) found');
/** @var Invoice $invoice */
foreach ($delayedAutoBillInvoices as $invoice) {
if ($invoice->isPaid()) {
continue;
}
if ($invoice->getAutoBillEnabled() && $invoice->client->autoBillLater()) {
$this->info(date('r') . ' Processing Autobill-delayed Invoice: ' . $invoice->id);
Auth::loginUsingId($invoice->activeUser()->id);
$this->paymentService->autoBillInvoice($invoice);
Auth::logout();
}
}
}
private function chargeLateFees()
{
$accounts = $this->accountRepo->findWithFees();