App\Ninja\Repositories\AccountRepository::getNinjaAccount PHP Method

getNinjaAccount() public method

public getNinjaAccount ( )
    public function getNinjaAccount()
    {
        $account = Account::whereAccountKey(NINJA_ACCOUNT_KEY)->first();
        if ($account) {
            return $account;
        } else {
            $account = new Account();
            $account->name = 'Invoice Ninja';
            $account->work_email = '[email protected]';
            $account->work_phone = '(800) 763-1948';
            $account->account_key = NINJA_ACCOUNT_KEY;
            $account->save();
            $random = str_random(RANDOM_KEY_LENGTH);
            $user = new User();
            $user->registered = true;
            $user->confirmed = true;
            $user->email = '[email protected]';
            $user->password = $random;
            $user->username = $random;
            $user->first_name = 'Invoice';
            $user->last_name = 'Ninja';
            $user->notify_sent = true;
            $user->notify_paid = true;
            $account->users()->save($user);
            if ($config = env(NINJA_GATEWAY_CONFIG)) {
                $accountGateway = new AccountGateway();
                $accountGateway->user_id = $user->id;
                $accountGateway->gateway_id = NINJA_GATEWAY_ID;
                $accountGateway->public_id = 1;
                $accountGateway->setConfig(json_decode($config));
                $account->account_gateways()->save($accountGateway);
            }
        }
        return $account;
    }

Usage Example

 public function fire()
 {
     $this->info(date('Y-m-d') . ' ChargeRenewalInvoices...');
     $ninjaAccount = $this->accountRepo->getNinjaAccount();
     $invoices = Invoice::whereAccountId($ninjaAccount->id)->whereDueDate(date('Y-m-d'))->where('balance', '>', 0)->with('client')->orderBy('id')->get();
     $this->info(count($invoices) . ' invoices found');
     foreach ($invoices as $invoice) {
         // check if account has switched to free since the invoice was created
         $account = Account::find($invoice->client->public_id);
         if (!$account) {
             continue;
         }
         $company = $account->company;
         if (!$company->plan || $company->plan == PLAN_FREE) {
             continue;
         }
         try {
             $this->info("Charging invoice {$invoice->invoice_number}");
             $this->paymentService->autoBillInvoice($invoice);
         } catch (Exception $exception) {
             $this->info('Error: ' . $exception->getMessage());
         }
     }
     $this->info('Done');
 }
All Usage Examples Of App\Ninja\Repositories\AccountRepository::getNinjaAccount