akaunting/app/Console/Commands/RecurringCheck.php

169 lines
4.2 KiB
PHP
Raw Normal View History

2018-04-27 19:02:42 +03:00
<?php
namespace App\Console\Commands;
2019-12-31 15:49:09 +03:00
use App\Events\Purchase\BillCreated;
use App\Events\Purchase\BillRecurring;
use App\Events\Sale\InvoiceCreated;
use App\Events\Sale\InvoiceRecurring;
2018-06-10 02:48:51 +03:00
use App\Models\Common\Company;
2019-12-31 16:03:20 +03:00
use App\Traits\Sales;
2018-04-27 19:02:42 +03:00
use App\Utilities\Overrider;
2019-11-16 10:21:14 +03:00
use Carbon\Carbon;
2018-04-27 19:02:42 +03:00
use Date;
use Illuminate\Console\Command;
class RecurringCheck extends Command
{
2019-12-31 16:03:20 +03:00
use Sales;
2018-04-27 19:02:42 +03:00
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'recurring:check';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check for recurring';
2019-11-16 10:21:14 +03:00
/**
* The current day.
*
* @var Carbon
*/
protected $today;
2018-04-27 19:02:42 +03:00
/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// Get all companies
2019-11-16 10:21:14 +03:00
$companies = Company::enabled()->cursor();
2018-04-27 19:02:42 +03:00
foreach ($companies as $company) {
2019-11-16 10:21:14 +03:00
$this->info('Creating recurring records for ' . $company->name . ' company.');
2018-04-27 19:02:42 +03:00
// Set company id
session(['company_id' => $company->id]);
// Override settings and currencies
Overrider::load('settings');
Overrider::load('currencies');
2019-11-16 10:21:14 +03:00
$this->today = Date::today();
2018-04-27 19:02:42 +03:00
2018-10-29 15:13:18 +03:00
foreach ($company->recurring as $recurring) {
2019-11-16 10:21:14 +03:00
foreach ($recurring->schedule() as $schedule) {
$this->recur($recurring, $schedule);
2018-04-27 19:02:42 +03:00
}
}
}
// Unset company_id
session()->forget('company_id');
}
2019-11-16 10:21:14 +03:00
protected function recur($recurring, $schedule)
2018-04-27 19:02:42 +03:00
{
2019-11-16 10:21:14 +03:00
$schedule_date = Date::parse($schedule->getStart()->format('Y-m-d'));
2018-05-01 19:00:33 +03:00
2019-11-16 10:21:14 +03:00
// Check if should recur today
if ($this->today->ne($schedule_date)) {
return;
}
2018-04-27 19:02:42 +03:00
2019-11-16 10:21:14 +03:00
if (!$model = $recurring->recurable) {
return;
}
2018-05-01 19:00:33 +03:00
2019-11-16 10:21:14 +03:00
switch ($recurring->recurable_type) {
2019-12-31 15:49:09 +03:00
case 'App\Models\Purchase\Bill':
2019-11-16 10:21:14 +03:00
if (!$clone = $this->getDocumentClone($model, 'billed_at')) {
break;
}
2018-04-27 22:16:35 +03:00
2019-11-16 10:21:14 +03:00
event(new BillCreated($clone));
2018-04-27 22:16:35 +03:00
2019-11-16 10:21:14 +03:00
event(new BillRecurring($clone));
2018-04-27 19:02:42 +03:00
2019-11-16 10:21:14 +03:00
break;
2019-12-31 15:49:09 +03:00
case 'App\Models\Sale\Invoice':
2019-11-16 10:21:14 +03:00
if (!$clone = $this->getDocumentClone($model, 'invoiced_at')) {
break;
}
2018-04-27 19:02:42 +03:00
2019-11-16 10:21:14 +03:00
event(new InvoiceCreated($clone));
event(new InvoiceRecurring($clone));
break;
case 'App\Models\Banking\Transaction':
// Skip model created on the same day, but scheduler hasn't run yet
if ($this->today->eq(Date::parse($model->paid_at->format('Y-m-d')))) {
break;
}
$model->cloneable_relations = [];
2018-04-27 19:02:42 +03:00
2019-11-16 10:21:14 +03:00
// Create new record
$clone = $model->duplicate();
$clone->parent_id = $model->id;
$clone->paid_at = $this->today->format('Y-m-d');
$clone->save();
break;
}
2018-04-27 19:02:42 +03:00
}
2019-11-16 10:21:14 +03:00
/**
* Clone the document and return it.
*
* @param $model
* @param $date_field
*
* @return boolean|object
*/
protected function getDocumentClone($model, $date_field)
2018-04-27 19:02:42 +03:00
{
2019-11-16 10:21:14 +03:00
// Skip model created on the same day, but scheduler hasn't run yet
if ($this->today->eq(Date::parse($model->$date_field->format('Y-m-d')))) {
return false;
}
2018-05-01 19:00:33 +03:00
$model->cloneable_relations = ['items', 'totals'];
2018-04-27 22:16:35 +03:00
// Create new record
2018-04-27 19:02:42 +03:00
$clone = $model->duplicate();
2019-11-16 10:21:14 +03:00
// Set original model id
2018-05-01 19:00:33 +03:00
$clone->parent_id = $model->id;
2019-11-16 10:21:14 +03:00
// Days between issued and due date
$diff_days = Date::parse($clone->due_at)->diffInDays(Date::parse($clone->$date_field));
2018-04-27 22:16:35 +03:00
// Update dates
2019-11-16 10:21:14 +03:00
$clone->$date_field = $this->today->format('Y-m-d');
$clone->due_at = $this->today->copy()->addDays($diff_days)->format('Y-m-d');
2018-04-27 22:16:35 +03:00
$clone->save();
2019-11-16 10:21:14 +03:00
return $clone;
2018-04-27 19:02:42 +03:00
}
}