App\Ninja\Repositories\InvoiceRepository::findNeedingReminding PHP Method

findNeedingReminding() public method

public findNeedingReminding ( App\Models\Account $account ) : mixed
$account App\Models\Account
return mixed
    public function findNeedingReminding(Account $account)
    {
        $dates = [];
        for ($i = 1; $i <= 3; $i++) {
            if ($date = $account->getReminderDate($i)) {
                $field = $account->{"field_reminder{$i}"} == REMINDER_FIELD_DUE_DATE ? 'due_date' : 'invoice_date';
                $dates[] = "{$field} = '{$date}'";
            }
        }
        $sql = implode(' OR ', $dates);
        $invoices = Invoice::invoiceType(INVOICE_TYPE_STANDARD)->whereAccountId($account->id)->where('balance', '>', 0)->where('is_recurring', '=', false)->whereRaw('(' . $sql . ')')->get();
        return $invoices;
    }

Usage Example

 public function fire()
 {
     $this->info(date('Y-m-d') . ' Running SendReminders...');
     $accounts = $this->accountRepo->findWithReminders();
     $this->info(count($accounts) . ' accounts found');
     /** @var \App\Models\Account $account */
     foreach ($accounts as $account) {
         if (!$account->hasFeature(FEATURE_EMAIL_TEMPLATES_REMINDERS)) {
             continue;
         }
         $invoices = $this->invoiceRepo->findNeedingReminding($account);
         $this->info($account->name . ': ' . count($invoices) . ' invoices found');
         /** @var Invoice $invoice */
         foreach ($invoices as $invoice) {
             if ($reminder = $account->getInvoiceReminder($invoice)) {
                 $this->info('Send to ' . $invoice->id);
                 $this->mailer->sendInvoice($invoice, $reminder);
             }
         }
     }
     $this->info('Done');
 }