diff --git a/app/Listeners/Client/CreatedClient.php b/app/Listeners/Client/CreatedClient.php index 81901d1c8123..b4bb867bae2c 100644 --- a/app/Listeners/Client/CreatedClient.php +++ b/app/Listeners/Client/CreatedClient.php @@ -2,19 +2,22 @@ namespace App\Listeners\Client; -use Illuminate\Queue\InteractsWithQueue; +use App\Models\Activity; +use App\Repositories\ActivityRepository; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Queue\InteractsWithQueue; -class CreatedClient implements ShouldQueue +class CreatedClient { + protected $activityRepo; /** * Create the event listener. * * @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) { - // + + $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); } } diff --git a/app/Models/Activity.php b/app/Models/Activity.php index 2c39637f9a80..30bb6dafda5d 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -4,7 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; -class Activity extends Model +class Activity extends BaseModel { const CREATE_CLIENT=1; const ARCHIVE_CLIENT=2; @@ -54,7 +54,11 @@ class Activity extends Model const UPDATE_EXPENSE=47; - + public function backup() + { + return $this->hasOne(Backup::class); + } + /** * @return mixed */ diff --git a/app/Models/Backup.php b/app/Models/Backup.php new file mode 100644 index 000000000000..e61be4e7a86a --- /dev/null +++ b/app/Models/Backup.php @@ -0,0 +1,16 @@ +belongsTo(Activity::class); + } +} \ No newline at end of file diff --git a/app/Observers/ClientObserver.php b/app/Observers/ClientObserver.php index 28d646a869e4..c883b3318b2e 100644 --- a/app/Observers/ClientObserver.php +++ b/app/Observers/ClientObserver.php @@ -2,6 +2,7 @@ namespace App\Observers; +use App\Events\Client\ClientWasCreated; use App\Models\Client; class ClientObserver @@ -14,7 +15,7 @@ class ClientObserver */ public function created(Client $client) { - // + event(new ClientWasCreated($client)); } /** diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index e69b01c68106..47aa16f29597 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -23,7 +23,7 @@ class EventServiceProvider extends ServiceProvider // Clients ClientWasCreated::class => [ CreatedClient::class, - 'App\Listeners\SubscriptionListener@createdClient', + // 'App\Listeners\SubscriptionListener@createdClient', ], 'App\Events\ClientWasArchived' => [ 'App\Listeners\ActivityListener@archivedClient', diff --git a/app/Repositories/ActivityRepository.php b/app/Repositories/ActivityRepository.php new file mode 100644 index 000000000000..85a233097445 --- /dev/null +++ b/app/Repositories/ActivityRepository.php @@ -0,0 +1,37 @@ +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(); + } + +} \ No newline at end of file diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index 8eebc9ce6efc..0e6c07729dcf 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -590,18 +590,51 @@ class CreateUsersTable extends Migration Schema::create('activities', function ($table) { $table->increments('id'); - $table->unsignedInteger('user_id'); + $table->unsignedInteger('user_id')->nullable(); $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->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('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'); }); } - + /** * Reverse the migrations. *