1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 11:12:43 +01:00
invoiceninja/app/Repositories/ActivityRepository.php

161 lines
4.7 KiB
PHP
Raw Normal View History

2019-04-21 14:24:26 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. 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;
use App\Models\Activity;
use App\Models\Backup;
2020-07-08 14:02:16 +02:00
use App\Models\CompanyToken;
use App\Models\Credit;
2020-10-28 00:21:53 +01:00
use App\Models\Design;
use App\Models\Invoice;
use App\Models\Quote;
2020-10-28 00:21:53 +01:00
use App\Models\RecurringInvoice;
2021-01-14 04:44:52 +01:00
use App\Models\User;
2020-10-28 00:21:53 +01:00
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
2020-11-25 15:19:52 +01:00
use App\Utils\HtmlEngine;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
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
{
use MakesInvoiceHtml;
2020-10-28 00:21:53 +01:00
use MakesHash;
2020-10-28 11:10:49 +01:00
/**
* Save the Activity.
*
2020-10-28 11:10:49 +01:00
* @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)
* @param $event_vars
*/
2020-07-08 14:02:16 +02:00
public function save($fields, $entity, $event_vars)
{
$activity = new Activity();
foreach ($fields as $key => $value) {
$activity->{$key} = $value;
2019-04-21 14:24:26 +02:00
}
if ($token_id = $this->getTokenId($event_vars)) {
2020-07-08 14:02:16 +02:00
$fields->token_id = $token_id;
}
$fields->ip = $event_vars['ip'];
$fields->is_system = $event_vars['is_system'];
$activity->save();
$this->createBackup($entity, $activity);
}
/**
* Creates a backup.
*
* @param Collection $entity The entity
* @param Collection $activity The activity
*/
public function createBackup($entity, $activity)
{
2021-01-14 04:44:52 +01:00
if($entity instanceof User){
}
else if ($entity->company->is_disabled) {
2020-11-23 13:55:04 +01:00
return;
2020-11-25 15:19:52 +01:00
}
2020-11-23 13:55:04 +01:00
$backup = new Backup();
if (get_class($entity) == Invoice::class || get_class($entity) == Quote::class || get_class($entity) == Credit::class) {
2020-08-13 02:57:06 +02:00
$contact = $entity->client->primary_contact()->first();
2020-10-28 00:21:53 +01:00
$backup->html_backup = $this->generateHtml($entity);
2020-08-19 04:44:25 +02:00
$backup->amount = $entity->amount;
2020-08-13 02:57:06 +02:00
}
$backup->activity_id = $activity->id;
2020-08-11 03:33:43 +02:00
$backup->json_backup = '';
//$backup->json_backup = $entity->toJson();
$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();
2020-07-08 14:02:16 +02:00
if ($company_token) {
2020-07-08 14:02:16 +02:00
return $company_token->id;
}
2020-07-08 14:02:16 +02:00
}
return false;
}
2020-10-28 00:21:53 +01:00
private function generateHtml($entity)
{
$entity_design_id = '';
2020-11-25 15:19:52 +01:00
if ($entity instanceof Invoice || $entity instanceof RecurringInvoice) {
2020-10-28 00:21:53 +01:00
$entity_design_id = 'invoice_design_id';
2020-11-25 15:19:52 +01:00
} elseif ($entity instanceof Quote) {
2020-10-28 00:21:53 +01:00
$entity_design_id = 'quote_design_id';
2020-11-25 15:19:52 +01:00
} elseif ($entity instanceof Credit) {
2020-10-28 00:21:53 +01:00
$entity_design_id = 'credit_design_id';
}
$entity_design_id = $entity->design_id ? $entity->design_id : $this->decodePrimaryKey($entity->client->getSetting($entity_design_id));
$design = Design::find($entity_design_id);
2021-01-19 22:30:04 +01:00
if(!$entity->invitations()->exists()){
nlog("No invitations for entity {$entity->id} - {$entity->number}");
return;
}
2020-10-28 00:29:54 +01:00
$html = new HtmlEngine($entity->invitations->first());
2020-10-28 00:21:53 +01:00
if ($design->is_custom) {
2020-11-25 15:19:52 +01:00
$options = [
2020-10-28 00:21:53 +01:00
'custom_partials' => json_decode(json_encode($design->design), true)
];
2020-11-25 15:19:52 +01:00
$template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
2020-10-28 00:21:53 +01:00
} else {
2020-11-25 15:19:52 +01:00
$template = new PdfMakerDesign(strtolower($design->name));
2020-10-28 00:21:53 +01:00
}
$state = [
'template' => $template->elements([
'client' => $entity->client,
'entity' => $entity,
'pdf_variables' => (array) $entity->company->settings->pdf_variables,
'$product' => $design->design->product,
2020-10-28 00:21:53 +01:00
]),
'variables' => $html->generateLabelsAndValues(),
'options' => [
'all_pages_header' => $entity->client->getSetting('all_pages_header'),
'all_pages_footer' => $entity->client->getSetting('all_pages_footer'),
],
];
$maker = new PdfMakerService($state);
return $maker->design($template)
->build()
->getCompiledHTML(true);
}
}