mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-05 04:34:36 -04:00
Wiring up Activities table and events
This commit is contained in:
parent
9790ed17f5
commit
2545935d31
@ -2,19 +2,22 @@
|
|||||||
|
|
||||||
namespace App\Listeners\Client;
|
namespace App\Listeners\Client;
|
||||||
|
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use App\Models\Activity;
|
||||||
|
use App\Repositories\ActivityRepository;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
|
||||||
class CreatedClient implements ShouldQueue
|
class CreatedClient
|
||||||
{
|
{
|
||||||
|
protected $activityRepo;
|
||||||
/**
|
/**
|
||||||
* Create the event listener.
|
* Create the event listener.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct(ActivityRepository $activityRepo)
|
||||||
{
|
{
|
||||||
//
|
$this->activityRepo = $activityRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,6 +28,14 @@ class CreatedClient implements ShouldQueue
|
|||||||
*/
|
*/
|
||||||
public function handle($event)
|
public function handle($event)
|
||||||
{
|
{
|
||||||
//
|
|
||||||
|
$fields = new \stdClass;
|
||||||
|
|
||||||
|
$fields->client_id = $event->client->id;
|
||||||
|
$fields->user_id = $event->client->user_id;
|
||||||
|
$fields->company_id = $event->client->company_id;
|
||||||
|
$fields->activity_type_id = Activity::CREATE_CLIENT;
|
||||||
|
|
||||||
|
$this->activityRepo->save($fields, $event->client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ namespace App\Models;
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
class Activity extends Model
|
class Activity extends BaseModel
|
||||||
{
|
{
|
||||||
const CREATE_CLIENT=1;
|
const CREATE_CLIENT=1;
|
||||||
const ARCHIVE_CLIENT=2;
|
const ARCHIVE_CLIENT=2;
|
||||||
@ -54,6 +54,10 @@ class Activity extends Model
|
|||||||
const UPDATE_EXPENSE=47;
|
const UPDATE_EXPENSE=47;
|
||||||
|
|
||||||
|
|
||||||
|
public function backup()
|
||||||
|
{
|
||||||
|
return $this->hasOne(Backup::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
16
app/Models/Backup.php
Normal file
16
app/Models/Backup.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Utils\Traits\MakesHash;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use Laracasts\Presenter\PresentableTrait;
|
||||||
|
|
||||||
|
class Backup extends BaseModel
|
||||||
|
{
|
||||||
|
public function activity()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Activity::class);
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Observers;
|
namespace App\Observers;
|
||||||
|
|
||||||
|
use App\Events\Client\ClientWasCreated;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
|
|
||||||
class ClientObserver
|
class ClientObserver
|
||||||
@ -14,7 +15,7 @@ class ClientObserver
|
|||||||
*/
|
*/
|
||||||
public function created(Client $client)
|
public function created(Client $client)
|
||||||
{
|
{
|
||||||
//
|
event(new ClientWasCreated($client));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,7 +23,7 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
// Clients
|
// Clients
|
||||||
ClientWasCreated::class => [
|
ClientWasCreated::class => [
|
||||||
CreatedClient::class,
|
CreatedClient::class,
|
||||||
'App\Listeners\SubscriptionListener@createdClient',
|
// 'App\Listeners\SubscriptionListener@createdClient',
|
||||||
],
|
],
|
||||||
'App\Events\ClientWasArchived' => [
|
'App\Events\ClientWasArchived' => [
|
||||||
'App\Listeners\ActivityListener@archivedClient',
|
'App\Listeners\ActivityListener@archivedClient',
|
||||||
|
37
app/Repositories/ActivityRepository.php
Normal file
37
app/Repositories/ActivityRepository.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Repositories;
|
||||||
|
|
||||||
|
use App\Models\Activity;
|
||||||
|
use App\Models\Backup;
|
||||||
|
|
||||||
|
class ActivityRepository extends BaseRepository
|
||||||
|
{
|
||||||
|
|
||||||
|
public function save($fields, $entity)
|
||||||
|
{
|
||||||
|
$activity = new Activity();
|
||||||
|
|
||||||
|
$activity->is_system = app()->runningInConsole();
|
||||||
|
$activity->ip = request()->getClientIp();
|
||||||
|
|
||||||
|
foreach($fields as $key => $value) {
|
||||||
|
|
||||||
|
$activity->{$key} = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$activity->save();
|
||||||
|
|
||||||
|
$this->createBackup($entity, $activity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createBackup($entity, $activity)
|
||||||
|
{
|
||||||
|
$backup = new Backup();
|
||||||
|
|
||||||
|
$backup->activity_id = $activity->id;
|
||||||
|
$backup->json_backup = $entity->toJson();
|
||||||
|
$backup->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -590,13 +590,46 @@ class CreateUsersTable extends Migration
|
|||||||
|
|
||||||
Schema::create('activities', function ($table) {
|
Schema::create('activities', function ($table) {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->unsignedInteger('user_id');
|
$table->unsignedInteger('user_id')->nullable();
|
||||||
$table->unsignedInteger('company_id');
|
$table->unsignedInteger('company_id');
|
||||||
$table->unsignedInteger('activity_type_id');
|
$table->unsignedInteger('client_id')->nullable();
|
||||||
|
$table->unsignedInteger('client_contact_id')->nullable();
|
||||||
|
$table->unsignedInteger('account_id')->nullable();
|
||||||
|
$table->unsignedInteger('payment_id')->nullable();
|
||||||
|
$table->unsignedInteger('invoice_id')->nullable();
|
||||||
|
$table->unsignedInteger('invitation_id')->nullable();
|
||||||
|
$table->unsignedInteger('task_id')->nullable();
|
||||||
|
$table->unsignedInteger('expense_id')->nullable();
|
||||||
|
$table->unsignedInteger('activity_type_id')->nullable();
|
||||||
|
$table->decimal('adjustment', 13, 2)->nullable();
|
||||||
|
$table->decimal('balance', 13, 2)->nullable();
|
||||||
|
$table->string('ip');
|
||||||
|
$table->boolean('is_system')->default(0);
|
||||||
|
|
||||||
|
$table->text('notes');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->index(['user_id', 'company_id']);
|
||||||
|
$table->index(['client_id', 'company_id']);
|
||||||
|
$table->index(['payment_id', 'company_id']);
|
||||||
|
$table->index(['invoice_id', 'company_id']);
|
||||||
|
$table->index(['invitation_id', 'company_id']);
|
||||||
|
$table->index(['task_id', 'company_id']);
|
||||||
|
$table->index(['expense_id', 'company_id']);
|
||||||
|
$table->index(['client_contact_id', 'company_id']);
|
||||||
|
|
||||||
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
|
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
|
||||||
|
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('backups', function ($table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->unsignedInteger('activity_id');
|
||||||
|
$table->text('json_backup');
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->foreign('activity_id')->references('id')->on('activities')->onDelete('cascade');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user