diff --git a/app/Console/Commands/SendRecurringInvoices.php b/app/Console/Commands/SendRecurringInvoices.php index f637a43145c5..ebb38060322b 100644 --- a/app/Console/Commands/SendRecurringInvoices.php +++ b/app/Console/Commands/SendRecurringInvoices.php @@ -4,8 +4,10 @@ namespace App\Console\Commands; use App\Models\Account; use App\Models\Invoice; +use App\Models\RecurringExpense; use App\Ninja\Mailers\ContactMailer as Mailer; use App\Ninja\Repositories\InvoiceRepository; +use App\Ninja\Repositories\RecurringExpenseRepository; use App\Services\PaymentService; use DateTime; use Illuminate\Console\Command; @@ -49,13 +51,14 @@ class SendRecurringInvoices extends Command * @param InvoiceRepository $invoiceRepo * @param PaymentService $paymentService */ - public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, PaymentService $paymentService) + public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, PaymentService $paymentService, RecurringExpenseRepository $recurringExpenseRepo) { parent::__construct(); $this->mailer = $mailer; $this->invoiceRepo = $invoiceRepo; $this->paymentService = $paymentService; + $this->recurringExpenseRepo = $recurringExpenseRepo; } public function fire() @@ -148,7 +151,7 @@ class SendRecurringInvoices extends Command { $today = new DateTime(); - $expenses = Expense::with('client') + $expenses = RecurringExpense::with('client') ->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND start_date <= ? AND (end_date IS NULL OR end_date >= ?)', [$today, $today]) ->orderBy('id', 'asc') ->get(); @@ -162,21 +165,7 @@ class SendRecurringInvoices extends Command } $this->info('Processing Expense: '. $expense->id); - $this->recurringExpenseRepo->createRecurringExpense($expense); - - /* - $account = $expense->account; - //$account->loadLocalizationSettings($recurInvoice->client); - //Auth::loginUsingId($recurInvoice->user_id); - $invoice = $this->invoiceRepo->createRecurringInvoice($recurInvoice); - - if ($invoice && ! $invoice->isPaid()) { - $this->info('Sending Invoice'); - $this->mailer->sendInvoice($invoice); - } - Auth::logout(); - */ } } diff --git a/app/Jobs/PurgeAccountData.php b/app/Jobs/PurgeAccountData.php index daad1f1efb97..157a6c89e44f 100644 --- a/app/Jobs/PurgeAccountData.php +++ b/app/Jobs/PurgeAccountData.php @@ -38,6 +38,7 @@ class PurgeAccountData extends Job 'credits', 'expense_categories', 'expenses', + 'recurring_expenses', 'invoice_items', 'payments', 'invoices', diff --git a/app/Ninja/Repositories/RecurringExpenseRepository.php b/app/Ninja/Repositories/RecurringExpenseRepository.php index 5b299f9eed98..a83ab1717eb5 100644 --- a/app/Ninja/Repositories/RecurringExpenseRepository.php +++ b/app/Ninja/Repositories/RecurringExpenseRepository.php @@ -3,6 +3,7 @@ namespace App\Ninja\Repositories; use App\Models\RecurringExpense; +use App\Models\Expense; use App\Models\Vendor; use Auth; use DB; @@ -138,4 +139,52 @@ class RecurringExpenseRepository extends BaseRepository return $expense; } + + public function createRecurringExpense(RecurringExpense $recurringExpense) + { + if ($recurringExpense->client && $recurringExpense->client->deleted_at) { + return false; + } + + if (! $recurringExpense->user->confirmed) { + return false; + } + + if (! $recurringExpense->shouldSendToday()) { + return false; + } + + $account = $recurringExpense->account; + $expense = Expense::createNew($recurringExpense); + + $fields = [ + 'vendor_id', + 'client_id', + 'amount', + 'public_notes', + 'private_notes', + 'invoice_currency_id', + 'expense_currency_id', + 'should_be_invoiced', + 'expense_category_id', + 'tax_name1', + 'tax_rate1', + 'tax_name2', + 'tax_rate2', + ]; + + foreach ($fields as $field) { + $expense->$field = $recurringExpense->$field; + } + + $expense->expense_date = $account->getDateTime()->format('Y-m-d'); + $expense->exchange_rate = 1; + $expense->invoice_currency_id = $recurringExpense->expense_currency_id; + $expense->save(); + + $recurringExpense->last_sent_date = $account->getDateTime()->format('Y-m-d'); + $recurringExpense->save(); + + return $expense; + } }