1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Recurring Expense Cron

This commit is contained in:
= 2021-08-24 21:24:03 +10:00
parent 7fe6dea1d3
commit 4bd3f382cb
2 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,62 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Factory;
use App\Models\Expense;
use App\Models\RecurringExpense;
class RecurringExpenseToExpenseFactory
{
public static function create(RecurringExpense $recurring_expense) :Expense
{
$expense = new Expense();
$expense->user_id = $recurring_expense->user_id;
$expense->assigned_user_id = $recurring_expense->assigned_user_id;
$expense->vendor_id = $recurring_expense->vendor_id;
$expense->invoice_id = $recurring_expense->invoice_id;
$expense->currency_id = $recurring_expense->currency_id;
$expense->company_id = $recurring_expense->company_id;
$expense->bank_id = $recurring_expense->bank_id;
$expense->exchange_rate = $recurring_expense->exchange_rate;
$expense->is_deleted = false;
$expense->should_be_invoiced = $recurring_expense->should_be_invoiced;
$expense->tax_name1 = $recurring_expense->tax_name1;
$expense->tax_rate1 = $recurring_expense->tax_rate1;
$expense->tax_name2 = $recurring_expense->tax_name2;
$expense->tax_rate2 = $recurring_expense->tax_rate2;
$expense->tax_name3 = $recurring_expense->tax_name3;
$expense->tax_rate3 = $recurring_expense->tax_rate3;
$expense->date = now()->format('Y-m-d');
$expense->payment_date = $recurring_expense->payment_date;
$expense->amount = $recurring_expense->amount;
$expense->foreign_amount = $recurring_expense->foreign_amount;
$expense->private_notes = $recurring_expense->private_notes;
$expense->public_notes = $recurring_expense->public_notes;
$expense->transaction_reference = $recurring_expense->transaction_reference;
$expense->custom_value1 = $recurring_expense->custom_value1;
$expense->custom_value2 = $recurring_expense->custom_value2;
$expense->custom_value3 = $recurring_expense->custom_value3;
$expense->custom_value4 = $recurring_expense->custom_value4;
$expense->transaction_id = $recurring_expense->transaction_id;
$expense->category_id = $recurring_expense->category_id;
$expense->payment_type_id = $recurring_expense->payment_type_id;
$expense->project_id = $recurring_expense->project_id;
$expense->invoice_documents = $recurring_expense->invoice_documents;
$expense->tax_amount1 = $recurring_expense->tax_amount1;
$expense->tax_amount2 = $recurring_expense->tax_amount2;
$expense->tax_amount3 = $recurring_expense->tax_amount3;
$expense->uses_inclusive_taxes = $recurring_expense->uses_inclusive_taxes;
$expense->calculate_tax_by_amount = $recurring_expense->calculate_tax_by_amount;
return $expense;
}
}

View File

@ -0,0 +1,88 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Cron;
use App\Jobs\RecurringInvoice\SendRecurring;
use App\Libraries\MultiDB;
use App\Models\RecurringExpense;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Carbon;
class RecurringExpensesCron
{
use Dispatchable;
public $tries = 1;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle() : void
{
/* Get all expenses where the send date is less than NOW + 30 minutes() */
nlog("Sending recurring expenses ".Carbon::now()->format('Y-m-d h:i:s'));
if (! config('ninja.db.multi_db_enabled')) {
$this->getRecurringExpenses();
} else {
//multiDB environment, need to
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
$this->getRecurringExpenses();
}
}
}
private function getRecurringExpenses()
{
$recurring_expenses = RecurringExpense::where('next_send_date', '<=', now()->toDateTimeString())
->whereNotNull('next_send_date')
->whereNull('deleted_at')
->where('remaining_cycles', '!=', '0')
// ->whereHas('client', function ($query) {
// $query->where('is_deleted',0)
// ->where('deleted_at', NULL);
// })
->with('company')
->cursor();
nlog(now()->format('Y-m-d') . ' Generating Recurring Expenses. Count = '.$recurring_expenses->count());
$recurring_expenses->each(function ($recurring_expense, $key) {
nlog("Current date = " . now()->format("Y-m-d") . " Recurring date = " .$recurring_expense->next_send_date);
if (!$recurring_expense->company->is_disabled) {
$this->generateExpense($recurring_expense);
}
});
}
private function generateExpense(RecurringExpense $recurring_expense)
{
}
}