2019-04-21 14:24:26 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-04-21 14:24:26 +02:00
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2020-06-09 10:59:38 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2019-04-21 14:24:26 +02:00
|
|
|
use App\Models\Activity;
|
|
|
|
use App\Models\Backup;
|
2019-11-06 23:57:09 +01:00
|
|
|
use App\Models\Client;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Models\CompanyToken;
|
2020-08-12 00:59:28 +02:00
|
|
|
use App\Models\Credit;
|
2019-11-20 06:41:49 +01:00
|
|
|
use App\Models\Invoice;
|
2020-08-12 00:59:28 +02:00
|
|
|
use App\Models\Quote;
|
2020-03-25 03:50:08 +01:00
|
|
|
use App\Models\User;
|
2019-11-20 06:41:49 +01:00
|
|
|
use App\Utils\Traits\MakesInvoiceHtml;
|
2019-07-08 07:54:46 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-04-21 14:24:26 +02:00
|
|
|
|
2019-05-13 08:18:46 +02:00
|
|
|
/**
|
|
|
|
* Class for activity repository.
|
|
|
|
*/
|
2019-04-21 14:24:26 +02:00
|
|
|
class ActivityRepository extends BaseRepository
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
use MakesInvoiceHtml;
|
|
|
|
/**
|
|
|
|
* Save the Activity
|
|
|
|
*
|
|
|
|
* @param stdClass $fields The fields
|
|
|
|
* @param Collection $entity The entity that you wish to have backed up (typically Invoice, Quote etc etc rather than Payment)
|
|
|
|
*/
|
2020-07-08 14:02:16 +02:00
|
|
|
public function save($fields, $entity, $event_vars)
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2020-06-09 10:59:38 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$activity = new Activity();
|
|
|
|
|
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
$activity->{$key} = $value;
|
2019-04-21 14:24:26 +02:00
|
|
|
}
|
|
|
|
|
2020-07-08 14:02:16 +02:00
|
|
|
if($token_id = $this->getTokenId($event_vars)){
|
|
|
|
$fields->token_id = $token_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields->ip = $event_vars['ip'];
|
|
|
|
$fields->is_system = $event_vars['is_system'];
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$activity->save();
|
|
|
|
|
|
|
|
$this->createBackup($entity, $activity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a backup.
|
|
|
|
*
|
|
|
|
* @param Collection $entity The entity
|
|
|
|
* @param Collection $activity The activity
|
|
|
|
*/
|
|
|
|
public function createBackup($entity, $activity)
|
|
|
|
{
|
|
|
|
$backup = new Backup();
|
|
|
|
|
2020-08-13 02:57:06 +02:00
|
|
|
if (get_class($entity) == Invoice::class || get_class($entity) == Quote::class || get_class($entity) == Credit::class){
|
|
|
|
$contact = $entity->client->primary_contact()->first();
|
|
|
|
$backup->html_backup = $this->generateEntityHtml($entity->getEntityDesigner(), $entity, $contact);
|
|
|
|
}
|
2019-11-20 06:41:49 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$backup->activity_id = $activity->id;
|
2020-08-11 03:33:43 +02:00
|
|
|
$backup->json_backup = '';
|
|
|
|
//$backup->json_backup = $entity->toJson();
|
2019-12-30 22:59:12 +01:00
|
|
|
$backup->save();
|
|
|
|
}
|
2020-07-08 14:02:16 +02:00
|
|
|
|
|
|
|
public function getTokenId(array $event_vars)
|
|
|
|
{
|
|
|
|
|
|
|
|
if($event_vars['token'])
|
|
|
|
{
|
|
|
|
|
|
|
|
$company_token = CompanyToken::whereRaw("BINARY `token`= ?", [$event_vars['token']])->first();
|
|
|
|
|
|
|
|
if($company_token)
|
|
|
|
return $company_token->id;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|