mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-23 20:00:33 -04:00
Working on recurring expenses
This commit is contained in:
parent
1baaa76a00
commit
df6ec9af70
@ -4,8 +4,10 @@ namespace App\Console\Commands;
|
|||||||
|
|
||||||
use App\Models\Account;
|
use App\Models\Account;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
|
use App\Models\RecurringExpense;
|
||||||
use App\Ninja\Mailers\ContactMailer as Mailer;
|
use App\Ninja\Mailers\ContactMailer as Mailer;
|
||||||
use App\Ninja\Repositories\InvoiceRepository;
|
use App\Ninja\Repositories\InvoiceRepository;
|
||||||
|
use App\Ninja\Repositories\RecurringExpenseRepository;
|
||||||
use App\Services\PaymentService;
|
use App\Services\PaymentService;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
@ -49,13 +51,14 @@ class SendRecurringInvoices extends Command
|
|||||||
* @param InvoiceRepository $invoiceRepo
|
* @param InvoiceRepository $invoiceRepo
|
||||||
* @param PaymentService $paymentService
|
* @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();
|
parent::__construct();
|
||||||
|
|
||||||
$this->mailer = $mailer;
|
$this->mailer = $mailer;
|
||||||
$this->invoiceRepo = $invoiceRepo;
|
$this->invoiceRepo = $invoiceRepo;
|
||||||
$this->paymentService = $paymentService;
|
$this->paymentService = $paymentService;
|
||||||
|
$this->recurringExpenseRepo = $recurringExpenseRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fire()
|
public function fire()
|
||||||
@ -148,7 +151,7 @@ class SendRecurringInvoices extends Command
|
|||||||
{
|
{
|
||||||
$today = new DateTime();
|
$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])
|
->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')
|
->orderBy('id', 'asc')
|
||||||
->get();
|
->get();
|
||||||
@ -162,21 +165,7 @@ class SendRecurringInvoices extends Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->info('Processing Expense: '. $expense->id);
|
$this->info('Processing Expense: '. $expense->id);
|
||||||
|
|
||||||
$this->recurringExpenseRepo->createRecurringExpense($expense);
|
$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();
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +38,7 @@ class PurgeAccountData extends Job
|
|||||||
'credits',
|
'credits',
|
||||||
'expense_categories',
|
'expense_categories',
|
||||||
'expenses',
|
'expenses',
|
||||||
|
'recurring_expenses',
|
||||||
'invoice_items',
|
'invoice_items',
|
||||||
'payments',
|
'payments',
|
||||||
'invoices',
|
'invoices',
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace App\Ninja\Repositories;
|
namespace App\Ninja\Repositories;
|
||||||
|
|
||||||
use App\Models\RecurringExpense;
|
use App\Models\RecurringExpense;
|
||||||
|
use App\Models\Expense;
|
||||||
use App\Models\Vendor;
|
use App\Models\Vendor;
|
||||||
use Auth;
|
use Auth;
|
||||||
use DB;
|
use DB;
|
||||||
@ -138,4 +139,52 @@ class RecurringExpenseRepository extends BaseRepository
|
|||||||
|
|
||||||
return $expense;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user