/**
  * Try to register a failed transaction.
  *
  * @param Transaction $t             Transaction
  * @param array       $report        Transactions to be reported.
  * @param int         $registeredCnt Number of registered transactions.
  * @param int         $expiredCnt    Number of expired transactions.
  * @param int         $failedCnt     Number of failed transactions.
  * @param User        $user          User object.
  *
  * @return boolean success
  */
 protected function processTransaction($t, &$report, &$registeredCnt, &$expiredCnt, &$failedCnt, &$user)
 {
     $this->msg("  Registering transaction id {$t->id} / {$t->transaction_id}");
     // Check if the transaction has not been registered for too long
     $now = new \DateTime();
     $paid_time = new \DateTime($t->paid);
     $diff = $now->diff($paid_time);
     $diffHours = $diff->days * 24 + $diff->h;
     if ($diffHours > $this->expireHours) {
         if (!isset($report[$t->driver])) {
             $report[$t->driver] = 0;
         }
         $report[$t->driver]++;
         $expiredCnt++;
         if (!$this->transactionTable->setTransactionReported($t->transaction_id)) {
             $this->err('    Failed to update transaction ' . $t->transaction_id . 'as reported');
         }
         $t->complete = Transaction::STATUS_REGISTRATION_EXPIRED;
         if (!$t->save()) {
             $this->err('    Failed to update transaction ' . $t->transaction_id . ' as expired.');
         } else {
             $this->msg('    Transaction ' . $t->transaction_id . ' expired.');
             return true;
         }
     } else {
         if ($user === false || $t->user_id != $user->id) {
             $user = $this->userTable->getById($t->user_id);
         }
         $catUsername = $patron = null;
         foreach ($user->getLibraryCards() as $card) {
             $card = $user->getLibraryCard($card['id']);
             if ($card['cat_username'] == $t->cat_username) {
                 try {
                     $patron = $this->catalog->patronLogin($card['cat_username'], $card['cat_password']);
                     if ($patron) {
                         break;
                     }
                 } catch (\Exception $e) {
                     $this->err('Patron login error: ' . $e->getMessage());
                 }
             }
         }
         if (!$patron) {
             $this->warn("Catalog login failed for user {$user->username}" . " (id {$user->id}), card {$card->cat_username}" . " (id {$card->id})");
             $failedCnt++;
             return false;
         }
         try {
             $this->catalog->markFeesAsPaid($patron, $t->amount);
             if (!$this->transactionTable->setTransactionRegistered($t->transaction_id)) {
                 $this->err('    Failed to update transaction ' . $t->transaction_id . 'as registered');
             }
             $registeredCnt++;
             return true;
         } catch (\Exception $e) {
             $this->err('    Registration of transaction ' . $t->transaction_id . ' failed');
             $this->err('      ' . $e->getMessage());
             if ($this->transactionTable->setTransactionRegistrationFailed($t->transaction_id, $e->getMessage())) {
                 $this->err('Error updating transaction ' . $t->transaction_id . ' status: ' . 'registering failed');
             }
             $failedCnt++;
             return false;
         }
     }
 }
 /**
  * Run service.
  *
  * @param array $arguments Command line arguments.
  *
  * @return boolean success
  */
 public function run($arguments)
 {
     $this->msg('Sending due date reminders');
     if (count($arguments) < 2) {
         $this->msg($this->getUsage());
         return false;
     } else {
         $this->collectScriptArguments($arguments);
     }
     $users = $this->userTable->getUsersWithDueDateReminders();
     $this->msg('Processing ' . count($users) . ' users');
     foreach ($users as $user) {
         $remindLoans = $this->getReminders($user);
         if ($remindCnt = count($remindLoans)) {
             $this->msg("{$remindCnt} new loans to remind for user {$user->username}" . " (id {$user->id})");
             $this->sendReminder($user, $remindLoans);
         } else {
             $this->msg("No loans to remind for user {$user->username} (id {$user->id})");
         }
     }
     return true;
 }