2018-04-27 19:02:42 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
use App\Events\Banking\TransactionCreated;
|
|
|
|
use App\Events\Banking\TransactionRecurring;
|
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;
|
2020-09-27 00:33:06 +03:00
|
|
|
use App\Models\Banking\Transaction;
|
2018-06-10 02:48:51 +03:00
|
|
|
use App\Models\Common\Company;
|
2020-09-27 00:33:06 +03:00
|
|
|
use App\Models\Sale\Invoice;
|
2020-09-30 17:01:35 +03:00
|
|
|
use App\Utilities\Date;
|
2018-04-27 19:02:42 +03:00
|
|
|
use App\Utilities\Overrider;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
class RecurringCheck extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2018-04-27 19:02:42 +03:00
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2020-03-13 12:44:05 +03:00
|
|
|
// Disable model cache
|
|
|
|
config(['laravel-model-caching.enabled' => false]);
|
|
|
|
|
2018-04-27 19:02:42 +03:00
|
|
|
// 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');
|
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
$today = Date::today();
|
2018-04-27 19:02:42 +03:00
|
|
|
|
2018-10-29 15:13:18 +03:00
|
|
|
foreach ($company->recurring as $recurring) {
|
2020-09-27 00:33:06 +03:00
|
|
|
if (!$model = $recurring->recurable) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
$schedules = $recurring->getRecurringSchedule();
|
|
|
|
|
|
|
|
$children_count = $this->getChildrenCount($model);
|
|
|
|
$schedule_count = $schedules->count();
|
|
|
|
|
|
|
|
// All recurring created, including today
|
|
|
|
if ($children_count > ($schedule_count - 1)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recur only today
|
|
|
|
if ($children_count == ($schedule_count - 1)) {
|
|
|
|
$this->recur($model, $recurring->recurable_type, $today);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recur all schedules, previously failed
|
|
|
|
foreach ($schedules as $schedule) {
|
2020-09-27 00:33:06 +03:00
|
|
|
$schedule_date = Date::parse($schedule->getStart()->format('Y-m-d'));
|
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
$this->recur($model, $recurring->recurable_type, $schedule_date);
|
2018-04-27 19:02:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unset company_id
|
|
|
|
session()->forget('company_id');
|
2020-02-26 10:37:24 +03:00
|
|
|
setting()->forgetAll();
|
2018-04-27 19:02:42 +03:00
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
protected function recur($model, $type, $schedule_date)
|
2018-04-27 19:02:42 +03:00
|
|
|
{
|
2020-09-30 17:01:35 +03:00
|
|
|
\DB::transaction(function () use ($model, $type, $schedule_date) {
|
|
|
|
if (!$clone = $this->getClone($model, $schedule_date)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-01 19:00:33 +03:00
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
switch ($type) {
|
|
|
|
case 'App\Models\Purchase\Bill':
|
|
|
|
event(new BillCreated($clone));
|
2018-04-27 22:16:35 +03:00
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
event(new BillRecurring($clone));
|
2018-04-27 19:02:42 +03:00
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
break;
|
|
|
|
case 'App\Models\Sale\Invoice':
|
|
|
|
event(new InvoiceCreated($clone));
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
event(new InvoiceRecurring($clone));
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
break;
|
|
|
|
case 'App\Models\Banking\Transaction':
|
|
|
|
event(new TransactionCreated($clone));
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
event(new TransactionRecurring($clone));
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2018-04-27 19:02:42 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
/**
|
2020-09-27 00:33:06 +03:00
|
|
|
* Clone the model and return it.
|
2019-11-16 10:21:14 +03:00
|
|
|
*
|
|
|
|
* @param $model
|
2020-09-27 00:33:06 +03:00
|
|
|
* @param $schedule_date
|
2019-11-16 10:21:14 +03:00
|
|
|
*
|
|
|
|
* @return boolean|object
|
|
|
|
*/
|
2020-09-27 00:33:06 +03:00
|
|
|
protected function getClone($model, $schedule_date)
|
2018-04-27 19:02:42 +03:00
|
|
|
{
|
2020-09-27 00:33:06 +03:00
|
|
|
if ($this->skipThisClone($model, $schedule_date)) {
|
2019-11-16 10:21:14 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
$function = ($model instanceof Transaction) ? 'getTransactionClone' : 'getDocumentClone';
|
2018-05-01 19:00:33 +03:00
|
|
|
|
2020-07-10 13:37:48 +03:00
|
|
|
try {
|
2020-09-27 00:33:06 +03:00
|
|
|
return $this->$function($model, $schedule_date);
|
|
|
|
} catch (\Exception | \Throwable | \Swift_RfcComplianceException| \Swift_TransportException | \Illuminate\Database\QueryException $e) {
|
2020-07-10 13:37:48 +03:00
|
|
|
$this->error($e->getMessage());
|
|
|
|
|
|
|
|
logger('Recurring check:: ' . $e->getMessage());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-27 00:33:06 +03:00
|
|
|
}
|
2018-04-27 19:02:42 +03:00
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
/**
|
|
|
|
* Clone the document and return it.
|
|
|
|
*
|
|
|
|
* @param $model
|
|
|
|
* @param $schedule_date
|
|
|
|
*
|
|
|
|
* @return boolean|object
|
|
|
|
*/
|
|
|
|
protected function getDocumentClone($model, $schedule_date)
|
|
|
|
{
|
|
|
|
$model->cloneable_relations = ['items', 'totals'];
|
|
|
|
|
|
|
|
$clone = $model->duplicate();
|
|
|
|
|
|
|
|
$date_field = $this->getDateField($model);
|
2018-05-01 19:00:33 +03:00
|
|
|
|
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
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
$clone->parent_id = $model->id;
|
|
|
|
$clone->$date_field = $schedule_date->format('Y-m-d');
|
|
|
|
$clone->due_at = $schedule_date->copy()->addDays($diff_days)->format('Y-m-d');
|
2018-04-27 22:16:35 +03:00
|
|
|
$clone->save();
|
2020-02-21 11:12:52 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return $clone;
|
2018-04-27 19:02:42 +03:00
|
|
|
}
|
2020-09-27 00:33:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone the transaction and return it.
|
|
|
|
*
|
|
|
|
* @param $model
|
|
|
|
* @param $schedule_date
|
|
|
|
*
|
|
|
|
* @return boolean|object
|
|
|
|
*/
|
|
|
|
protected function getTransactionClone($model, $schedule_date)
|
|
|
|
{
|
|
|
|
$model->cloneable_relations = [];
|
|
|
|
|
|
|
|
$clone = $model->duplicate();
|
|
|
|
|
|
|
|
$clone->parent_id = $model->id;
|
|
|
|
$clone->paid_at = $schedule_date->format('Y-m-d');
|
|
|
|
$clone->save();
|
|
|
|
|
|
|
|
return $clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function skipThisClone($model, $schedule_date)
|
|
|
|
{
|
|
|
|
$date_field = $this->getDateField($model);
|
|
|
|
|
|
|
|
// Skip model created on the same day, but scheduler hasn't run yet
|
|
|
|
if ($schedule_date->equalTo(Date::parse($model->$date_field->format('Y-m-d')))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$table = $this->getTable($model);
|
|
|
|
|
|
|
|
$already_cloned = \DB::table($table)
|
|
|
|
->where('parent_id', $model->id)
|
|
|
|
->whereDate($date_field, $schedule_date)
|
|
|
|
->value('id');
|
|
|
|
|
|
|
|
// Skip if already cloned
|
|
|
|
if ($already_cloned) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
protected function getChildrenCount($model)
|
|
|
|
{
|
|
|
|
$table = $this->getTable($model);
|
|
|
|
|
|
|
|
return \DB::table($table)
|
|
|
|
->where('parent_id', $model->id)
|
|
|
|
->count();
|
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
protected function getDateField($model)
|
|
|
|
{
|
|
|
|
if ($model instanceof Transaction) {
|
|
|
|
return 'paid_at';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($model instanceof Invoice) {
|
|
|
|
return 'invoiced_at';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'billed_at';
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTable($model)
|
|
|
|
{
|
|
|
|
if ($model instanceof Transaction) {
|
|
|
|
return 'transactions';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($model instanceof Invoice) {
|
|
|
|
return 'invoices';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'bills';
|
|
|
|
}
|
2018-04-27 19:02:42 +03:00
|
|
|
}
|