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
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-04-21 14:24:26 +02:00
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
|
|
use App\Models\Activity;
|
|
|
|
use App\Models\Backup;
|
2019-11-06 23:57:09 +01:00
|
|
|
use App\Models\Client;
|
2019-11-20 06:41:49 +01:00
|
|
|
use App\Models\Invoice;
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
public function save($fields, $entity)
|
|
|
|
{
|
|
|
|
$activity = new Activity();
|
|
|
|
|
|
|
|
$activity->is_system = app()->runningInConsole();
|
2019-04-21 14:24:26 +02:00
|
|
|
$activity->ip = request()->getClientIp();
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
$activity->{$key} = $value;
|
2019-04-21 14:24:26 +02:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// if(get_class($entity) == Client::class)
|
|
|
|
// $settings = $entity->getMergedSettings();
|
|
|
|
// else
|
|
|
|
// $settings = $entity->client->getMergedSettings();
|
|
|
|
// $entity->clientMergedDettings = $settings;
|
|
|
|
|
|
|
|
if (get_class($entity) == Client::class) {
|
|
|
|
$entity->load('company');
|
|
|
|
} else {
|
|
|
|
$entity->load('company', 'client');
|
|
|
|
}
|
2019-11-20 06:41:49 +01:00
|
|
|
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (get_class($entity) == Invoice::class && ($activity->activity_type_id == Activity::MARK_SENT_INVOICE || $activity->activity_type_id == Activity::PAID_INVOICE)) {
|
|
|
|
$backup->html_backup = $this->generateInvoiceHtml($entity->design(), $entity);
|
|
|
|
}
|
2019-11-20 06:41:49 +01:00
|
|
|
|
2019-04-21 14:24:26 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$backup->activity_id = $activity->id;
|
|
|
|
$backup->json_backup = $entity->toJson();
|
|
|
|
$backup->save();
|
|
|
|
}
|
|
|
|
}
|