2019-04-21 14:24:26 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2019-04-21 14:24:26 +02:00
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2023-08-30 04:21:22 +02:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Quote;
|
2019-04-21 14:24:26 +02:00
|
|
|
use App\Models\Backup;
|
2020-08-12 00:59:28 +02:00
|
|
|
use App\Models\Credit;
|
2020-10-28 00:21:53 +01:00
|
|
|
use App\Models\Design;
|
2019-11-20 06:41:49 +01:00
|
|
|
use App\Models\Invoice;
|
2023-08-30 04:21:22 +02:00
|
|
|
use App\Models\Activity;
|
|
|
|
use App\Utils\HtmlEngine;
|
|
|
|
use App\Models\CompanyToken;
|
|
|
|
use App\Models\PurchaseOrder;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use App\Utils\VendorHtmlEngine;
|
2020-10-28 00:21:53 +01:00
|
|
|
use App\Models\RecurringInvoice;
|
2023-08-30 04:21:22 +02:00
|
|
|
use App\Utils\Traits\MakesInvoiceHtml;
|
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;
|
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;
|
2020-10-28 00:21:53 +01:00
|
|
|
use MakesHash;
|
2020-10-28 11:10:49 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Save the Activity.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2023-08-04 08:40:44 +02:00
|
|
|
* @param \stdClass $fields The fields
|
|
|
|
* @param \App\Models\Invoice | \App\Models\Quote | \App\Models\Credit | \App\Models\PurchaseOrder $entity
|
|
|
|
* @param array $event_vars
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
2020-07-08 14:02:16 +02:00
|
|
|
public function save($fields, $entity, $event_vars)
|
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
|
|
|
}
|
|
|
|
|
2023-07-14 06:23:01 +02:00
|
|
|
if($entity->company)
|
|
|
|
$activity->account_id = $entity->company->account_id;
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($token_id = $this->getTokenId($event_vars)) {
|
2021-07-26 14:11:53 +02:00
|
|
|
$activity->token_id = $token_id;
|
2020-07-08 14:02:16 +02:00
|
|
|
}
|
|
|
|
|
2022-02-01 00:03:51 +01:00
|
|
|
$activity->ip = $event_vars['ip'] ?: ' ';
|
2021-07-26 14:11:53 +02:00
|
|
|
$activity->is_system = $event_vars['is_system'];
|
2020-07-08 14:02:16 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$activity->save();
|
|
|
|
|
2022-10-21 23:23:36 +02:00
|
|
|
//rate limiter
|
2023-02-16 02:36:09 +01:00
|
|
|
$this->createBackup($entity, $activity);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a backup.
|
|
|
|
*
|
2023-08-04 08:40:44 +02:00
|
|
|
* @param \App\Models\Invoice | \App\Models\Quote | \App\Models\Credit | \App\Models\PurchaseOrder $entity
|
|
|
|
* @param \App\Models\Activity $activity The activity
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
|
|
|
public function createBackup($entity, $activity)
|
|
|
|
{
|
2022-11-07 00:33:39 +01:00
|
|
|
if ($entity instanceof User || $entity->company->is_disabled || $entity->company?->account->isFreeHostedClient()) {
|
2020-11-23 13:55:04 +01:00
|
|
|
return;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-10-08 06:00:17 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (get_class($entity) == Invoice::class
|
|
|
|
|| get_class($entity) == Quote::class
|
|
|
|
|| get_class($entity) == Credit::class
|
2021-05-10 07:59:23 +02:00
|
|
|
|| get_class($entity) == RecurringInvoice::class
|
|
|
|
) {
|
2021-10-20 05:05:46 +02:00
|
|
|
$backup = new Backup();
|
2021-10-13 06:47:56 +02:00
|
|
|
$entity->load('client');
|
2020-08-19 04:44:25 +02:00
|
|
|
$backup->amount = $entity->amount;
|
2021-10-20 05:05:46 +02:00
|
|
|
$backup->activity_id = $activity->id;
|
|
|
|
$backup->json_backup = '';
|
|
|
|
$backup->save();
|
|
|
|
|
|
|
|
$backup->storeRemotely($this->generateHtml($entity), $entity->client);
|
2023-08-30 04:21:22 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(get_class($entity) == PurchaseOrder::class)
|
|
|
|
{
|
|
|
|
|
|
|
|
$backup = new Backup();
|
|
|
|
$entity->load('client');
|
|
|
|
$backup->amount = $entity->amount;
|
|
|
|
$backup->activity_id = $activity->id;
|
|
|
|
$backup->json_backup = '';
|
|
|
|
$backup->save();
|
|
|
|
|
|
|
|
$backup->storeRemotely($this->generateVendorHtml($entity), $entity->vendor);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
2020-08-13 02:57:06 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-07-08 14:02:16 +02:00
|
|
|
|
|
|
|
public function getTokenId(array $event_vars)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($event_vars['token']) {
|
2023-08-04 08:40:44 +02:00
|
|
|
/** @var \App\Models\CompanyToken $company_token **/
|
2023-08-06 09:35:19 +02:00
|
|
|
$company_token = CompanyToken::query()->where('token', $event_vars['token'])->first();
|
2020-07-08 14:02:16 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($company_token) {
|
2020-07-08 14:02:16 +02:00
|
|
|
return $company_token->id;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-07-08 14:02:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-28 00:21:53 +01:00
|
|
|
|
2023-08-30 04:21:22 +02:00
|
|
|
private function generateVendorHtml($entity)
|
|
|
|
{
|
|
|
|
$entity_design_id = $entity->design_id ? $entity->design_id : $this->decodePrimaryKey($entity->vendor->getSetting('purchase_order_design_id'));
|
|
|
|
|
|
|
|
$design = Design::withTrashed()->find($entity_design_id);
|
|
|
|
|
|
|
|
if (! $entity->invitations()->exists() || ! $design) {
|
|
|
|
nlog("No invitations for entity {$entity->id} - {$entity->number}");
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$entity->load('vendor.company', 'invitations');
|
|
|
|
|
|
|
|
$html = new VendorHtmlEngine($entity->invitations->first()->load('purchase_order', 'contact'));
|
|
|
|
|
|
|
|
if ($design->is_custom) {
|
|
|
|
$options = [
|
|
|
|
'custom_partials' => json_decode(json_encode($design->design), true),
|
|
|
|
];
|
|
|
|
$template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
|
|
|
|
} else {
|
|
|
|
$template = new PdfMakerDesign(strtolower($design->name));
|
|
|
|
}
|
|
|
|
|
|
|
|
$state = [
|
|
|
|
'template' => $template->elements([
|
|
|
|
'vendor' => $entity->vendor,
|
|
|
|
'entity' => $entity,
|
|
|
|
'pdf_variables' => (array) $entity->company->settings->pdf_variables,
|
|
|
|
'$product' => $design->design->product,
|
|
|
|
]),
|
|
|
|
'variables' => $html->generateLabelsAndValues(),
|
|
|
|
'options' => [
|
|
|
|
'all_pages_header' => $entity->vendor->getSetting('all_pages_header'),
|
|
|
|
'all_pages_footer' => $entity->vendor->getSetting('all_pages_footer'),
|
|
|
|
],
|
|
|
|
'process_markdown' => $entity->vendor->company->markdown_enabled,
|
|
|
|
];
|
|
|
|
|
|
|
|
$maker = new PdfMakerService($state);
|
|
|
|
|
|
|
|
$html = $maker->design($template)
|
|
|
|
->build()
|
|
|
|
->getCompiledHTML(true);
|
|
|
|
|
|
|
|
$maker = null;
|
|
|
|
$state = null;
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-28 00:21:53 +01:00
|
|
|
private function generateHtml($entity)
|
|
|
|
{
|
|
|
|
$entity_design_id = '';
|
2022-04-20 03:55:33 +02:00
|
|
|
$entity_type = '';
|
2020-10-28 00:21:53 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($entity instanceof Invoice) {
|
2022-04-20 03:55:33 +02:00
|
|
|
$entity_type = 'invoice';
|
2020-10-28 00:21:53 +01:00
|
|
|
$entity_design_id = 'invoice_design_id';
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif ($entity instanceof RecurringInvoice) {
|
2022-04-20 03:55:33 +02:00
|
|
|
$entity_type = 'recurring_invoice';
|
|
|
|
$entity_design_id = 'invoice_design_id';
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif ($entity instanceof Quote) {
|
2022-04-20 03:55:33 +02:00
|
|
|
$entity_type = '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) {
|
2022-04-20 03:55:33 +02:00
|
|
|
$entity_type = '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));
|
|
|
|
|
2023-04-29 13:01:02 +02:00
|
|
|
$design = Design::withTrashed()->find($entity_design_id);
|
2021-01-19 22:30:04 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $entity->invitations()->exists() || ! $design) {
|
2021-01-19 22:30:04 +01:00
|
|
|
nlog("No invitations for entity {$entity->id} - {$entity->number}");
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-20 05:05:46 +02:00
|
|
|
return '';
|
2021-01-19 22:30:04 +01:00
|
|
|
}
|
|
|
|
|
2021-10-13 06:47:56 +02:00
|
|
|
$entity->load('client.company', 'invitations');
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$html = new HtmlEngine($entity->invitations->first()->load($entity_type, 'contact'));
|
2020-10-28 00:21:53 +01:00
|
|
|
|
|
|
|
if ($design->is_custom) {
|
2020-11-25 15:19:52 +01:00
|
|
|
$options = [
|
2022-06-21 11:57:17 +02: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,
|
2020-11-04 14:56:08 +01:00
|
|
|
'$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'),
|
|
|
|
],
|
2021-08-09 14:31:31 +02:00
|
|
|
'process_markdown' => $entity->client->company->markdown_enabled,
|
2020-10-28 00:21:53 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$maker = new PdfMakerService($state);
|
|
|
|
|
2022-11-01 11:20:28 +01:00
|
|
|
$html = $maker->design($template)
|
2020-10-28 00:21:53 +01:00
|
|
|
->build()
|
|
|
|
->getCompiledHTML(true);
|
2022-11-01 11:20:28 +01:00
|
|
|
|
|
|
|
$maker = null;
|
|
|
|
$state = null;
|
|
|
|
|
|
|
|
return $html;
|
2020-10-28 00:21:53 +01:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|