akaunting 3.0 (the last dance)

This commit is contained in:
Burak Civan
2022-06-01 10:15:55 +03:00
parent cead09f6d4
commit d9c0764572
3812 changed files with 126831 additions and 102949 deletions

View File

@ -8,6 +8,7 @@ use App\Models\Document\Document;
use App\Notifications\Purchase\Bill as Notification;
use App\Utilities\Date;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
class BillReminder extends Command
{
@ -35,22 +36,29 @@ class BillReminder extends Command
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
$today = Date::today();
$start_date = $today->copy()->subWeek()->toDateString() . ' 00:00:00';
$end_date = $today->copy()->addMonth()->toDateString() . ' 23:59:59';
// Get all companies
$companies = Company::enabled()->with('bills')->cursor();
$companies = Company::whereHas('bills', function (Builder $query) use ($start_date, $end_date) {
$query->allCompanies();
$query->whereBetween('due_at', [$start_date, $end_date]);
$query->accrued();
$query->notPaid();
})
->enabled()
->cursor();
foreach ($companies as $company) {
// Has company bills
if (!$company->bills->count()) {
continue;
}
$this->info('Sending bill reminders for ' . $company->name . ' company.');
// Set company
$company->makeCurrent();
// Don't send reminders if disabled
if (!setting('schedule.send_bill_reminder')) {
if (! setting('schedule.send_bill_reminder')) {
$this->info('Bill reminders disabled by ' . $company->name . '.');
continue;
@ -74,9 +82,11 @@ class BillReminder extends Command
$date = Date::today()->addDays($day)->toDateString();
// Get upcoming bills
$bills = Document::bill()->with('contact')->accrued()->notPaid()->due($date)->cursor();
$bills = Document::with('contact')->bill()->accrued()->notPaid()->due($date)->cursor();
foreach ($bills as $bill) {
$this->info($bill->document_number . ' bill reminded.');
try {
event(new DocumentReminded($bill, Notification::class));
} catch (\Throwable $e) {

View File

@ -2,9 +2,10 @@
namespace App\Console\Commands;
use Session;
use App\Utilities\Installer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
class Install extends Command
{
@ -74,15 +75,17 @@ class Install extends Command
Installer::createDefaultEnvFile();
$this->line('Creating database tables');
if (!$this->createDatabaseTables()) {
if (! $this->createDatabaseTables()) {
return self::CMD_ERROR;
}
$this->line('Creating company');
Installer::createCompany($this->company_name, $this->company_email, $this->locale);
DB::transaction(function () {
$this->line('Creating company');
Installer::createCompany($this->company_name, $this->company_email, $this->locale);
$this->line('Creating admin');
Installer::createUser($this->admin_email, $this->admin_password, $this->locale);
$this->line('Creating admin');
Installer::createUser($this->admin_email, $this->admin_password, $this->locale);
});
$this->line('Applying the final touches');
Installer::finalTouches();

View File

@ -8,6 +8,7 @@ use App\Models\Document\Document;
use App\Notifications\Sale\Invoice as Notification;
use App\Utilities\Date;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
class InvoiceReminder extends Command
{
@ -35,22 +36,29 @@ class InvoiceReminder extends Command
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
$today = Date::today();
$start_date = $today->copy()->subMonth()->toDateString() . ' 00:00:00';
$end_date = $today->copy()->addWeek()->toDateString() . ' 23:59:59';
// Get all companies
$companies = Company::enabled()->with('invoices')->cursor();
$companies = Company::whereHas('invoices', function (Builder $query) use ($start_date, $end_date) {
$query->allCompanies();
$query->whereBetween('due_at', [$start_date, $end_date]);
$query->accrued();
$query->notPaid();
})
->enabled()
->cursor();
foreach ($companies as $company) {
// Has company invoices
if (!$company->invoices->count()) {
continue;
}
$this->info('Sending invoice reminders for ' . $company->name . ' company.');
// Set company
$company->makeCurrent();
// Don't send reminders if disabled
if (!setting('schedule.send_invoice_reminder')) {
if (! setting('schedule.send_invoice_reminder')) {
$this->info('Invoice reminders disabled by ' . $company->name . '.');
continue;
@ -74,9 +82,11 @@ class InvoiceReminder extends Command
$date = Date::today()->subDays($day)->toDateString();
// Get upcoming invoices
$invoices = Document::invoice()->with('contact')->accrued()->notPaid()->due($date)->cursor();
$invoices = Document::with('contact')->invoice()->accrued()->notPaid()->due($date)->cursor();
foreach ($invoices as $invoice) {
$this->info($invoice->document_number . ' invoice reminded.');
try {
event(new DocumentReminded($invoice, Notification::class));
} catch (\Throwable $e) {

View File

@ -12,6 +12,9 @@ use App\Models\Common\Recurring;
use App\Models\Document\Document;
use App\Utilities\Date;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class RecurringCheck extends Command
{
@ -36,25 +39,40 @@ class RecurringCheck extends Command
*/
public function handle()
{
// Bind to container
app()->instance(static::class, $this);
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
// Get all recurring
$recurring = Recurring::allCompanies()->with('company')->get();
$recurring = Recurring::with('company')
/*->whereHas('recurable', function (Builder $query) {
$query->allCompanies();
})*/
->active()
->allCompanies()
->cursor();
$this->info('Creating recurring records ' . $recurring->count());
//$this->info('Total recurring: ' . $recurring->count());
$today = Date::today();
foreach ($recurring as $recur) {
if (empty($recur->company)) {
$this->info('Missing company.');
$recur->delete();
continue;
}
$this->info('Creating records for ' . $recur->id . ' recurring...');
$company_name = !empty($recur->company->name) ? $recur->company->name : 'Missing Company Name : ' . $recur->company->id;
$this->info('Creating recurring records for ' . $company_name . ' company...');
// Check if company is disabled
if (!$recur->company->enabled) {
if (! $recur->company->enabled) {
$this->info($company_name . ' company is disabled. Skipping...');
if (Date::parse($recur->company->updated_at)->format('Y-m-d') > Date::now()->subMonth(3)->format('Y-m-d')) {
@ -75,7 +93,7 @@ class RecurringCheck extends Command
}
}
if (!$has_active_users) {
if (! $has_active_users) {
$this->info('No active users for ' . $company_name . ' company. Skipping...');
$recur->delete();
@ -85,31 +103,46 @@ class RecurringCheck extends Command
company($recur->company_id)->makeCurrent();
$today = Date::today();
if (! $model = $recur->recurable) {
$this->info('Missing model.');
$recur->delete();
if (!$model = $recur->recurable) {
continue;
}
$schedules = $recur->getRecurringSchedule();
$children_count = $this->getChildrenCount($model);
$schedules = $recur->getRecurringSchedule();
$schedule_count = $schedules->count();
// All recurring created, including today
if ($children_count > ($schedule_count - 1)) {
$this->info('All recurring created.');
$recur->update(['status' => Recurring::COMPLETE_STATUS]);
continue;
}
// Recur only today
if ($children_count == ($schedule_count - 1)) {
$this->info('Recur only today.');
$this->recur($model, $recur->recurable_type, $today);
$recur->update(['status' => Recurring::COMPLETE_STATUS]);
continue;
}
// Recur all schedules, previously failed
// Don't create records for the future
$schedules = $schedules->endsBefore($recur->getRecurringRuleTomorrowDate());
// Recur all schedules, including the previously failed ones
foreach ($schedules as $schedule) {
$this->info('Recur all schedules.');
$schedule_date = Date::parse($schedule->getStart()->format('Y-m-d'));
$this->recur($model, $recur->recurable_type, $schedule_date);
@ -117,13 +150,16 @@ class RecurringCheck extends Command
}
Company::forgetCurrent();
// Remove from container
app()->forgetInstance(static::class);
}
protected function recur($model, $type, $schedule_date)
{
\DB::transaction(function () use ($model, $type, $schedule_date) {
/** @var Document $clone */
if (!$clone = $this->getClone($model, $schedule_date)) {
DB::transaction(function () use ($model, $type, $schedule_date) {
/** @var Document|Transaction $clone */
if (! $clone = $this->getClone($model, $schedule_date)) {
return;
}
@ -185,13 +221,12 @@ class RecurringCheck extends Command
$clone = $model->duplicate();
$date_field = $this->getDateField($model);
// Days between issued and due date
$diff_days = Date::parse($clone->due_at)->diffInDays(Date::parse($clone->$date_field));
$diff_days = Date::parse($model->due_at)->diffInDays(Date::parse($model->issued_at));
$clone->type = $this->getRealType($clone->type);
$clone->parent_id = $model->id;
$clone->$date_field = $schedule_date->format('Y-m-d');
$clone->issued_at = $schedule_date->format('Y-m-d');
$clone->due_at = $schedule_date->copy()->addDays($diff_days)->format('Y-m-d');
$clone->created_from = 'core::recurring';
$clone->save();
@ -213,6 +248,7 @@ class RecurringCheck extends Command
$clone = $model->duplicate();
$clone->type = $this->getRealType($clone->type);
$clone->parent_id = $model->id;
$clone->paid_at = $schedule_date->format('Y-m-d');
$clone->created_from = 'core::recurring';
@ -230,9 +266,7 @@ class RecurringCheck extends Command
return true;
}
$table = $this->getTable($model);
$already_cloned = \DB::table($table)
$already_cloned = DB::table($model->getTable())
->where('parent_id', $model->id)
->whereDate($date_field, $schedule_date)
->value('id');
@ -247,9 +281,7 @@ class RecurringCheck extends Command
protected function getChildrenCount($model)
{
$table = $this->getTable($model);
return \DB::table($table)
return DB::table($model->getTable())
->where('parent_id', $model->id)
->count();
}
@ -265,14 +297,8 @@ class RecurringCheck extends Command
}
}
protected function getTable($model)
public function getRealType(string $recurring_type): string
{
if ($model instanceof Transaction) {
return 'transactions';
}
if ($model instanceof Document) {
return 'documents';
}
return Str::replace('-recurring', '', $recurring_type);
}
}

View File

@ -1,83 +0,0 @@
<?php
namespace App\Console\Commands;
use App\Models\Common\Company;
use App\Models\Common\Report;
use App\Utilities\Reports as Utility;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
class ReportCache extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'report:cache';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Calculate and cache reports';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
// Get all companies
$companies = Company::enabled()->withCount('reports')->cursor();
foreach ($companies as $company) {
// Has company reports
if (!$company->reports_count) {
continue;
}
$this->info('Calculating reports for ' . $company->name . ' company.');
// Set company
$company->makeCurrent();
$this->cacheReportsOfCurrentCompany();
}
Company::forgetCurrent();
}
protected function cacheReportsOfCurrentCompany()
{
$reports = Report::orderBy('name')->get();
foreach ($reports as $report) {
try {
$class = Utility::getClassInstance($report, false);
if (empty($class)) {
continue;
}
$ttl = 3600 * 6; // 6 hours
Cache::forget('reports.totals.' . $report->id);
Cache::remember('reports.totals.' . $report->id, $ttl, function () use ($class) {
return $class->getGrandTotal();
});
} catch (\Throwable $e) {
$this->error($e->getMessage());
report($e);
}
}
}
}

2
app/Console/Commands/SampleData.php Executable file → Normal file
View File

@ -12,7 +12,7 @@ class SampleData extends Command
*
* @var string
*/
protected $signature = 'sample-data:seed {--count=100 : total records for each item}';
protected $signature = 'sample-data:seed {--count=100 : total records for each item} {--company=1 : the company id}';
/**
* The console command description.

View File

@ -0,0 +1,43 @@
<?php
namespace App\Console\Commands;
use App\Events\Install\UpdateFinished;
use Illuminate\Console\Command;
class UpdateDb extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update:db {alias=core} {company=1} {new=3.0.0} {old=2.1.36}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Allows to update Akaunting database manually';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$alias = $this->argument('alias');
$company_id = $this->argument('company');
$new = $this->argument('new');
$old = $this->argument('old');
company($company_id)->makeCurrent();
// Disable model cache during update
config(['laravel-model-caching.enabled' => false]);
event(new UpdateFinished($alias, $new, $old));
}
}