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

Working on recurring expenses

This commit is contained in:
Hillel Coren 2017-06-26 15:57:40 +03:00
parent 1baaa76a00
commit df6ec9af70
3 changed files with 55 additions and 16 deletions

View File

@ -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();
*/
}
}

View File

@ -38,6 +38,7 @@ class PurgeAccountData extends Job
'credits',
'expense_categories',
'expenses',
'recurring_expenses',
'invoice_items',
'payments',
'invoices',

View File

@ -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;
}
}