1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/Transformers/CompanyTransformer.php

500 lines
19 KiB
PHP
Raw Normal View History

2019-03-27 05:50:13 +01: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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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-03-27 05:50:13 +01:00
2019-03-28 03:36:36 +01:00
namespace App\Transformers;
2019-03-27 05:50:13 +01:00
2023-10-26 04:57:44 +02:00
use App\Models\Account;
use App\Models\Activity;
use App\Models\BankIntegration;
use App\Models\BankTransaction;
use App\Models\BankTransactionRule;
2019-03-28 03:36:36 +01:00
use App\Models\Client;
2023-10-26 04:57:44 +02:00
use App\Models\Company;
use App\Models\CompanyGateway;
use App\Models\CompanyLedger;
use App\Models\CompanyToken;
use App\Models\CompanyUser;
use App\Models\Credit;
use App\Models\Design;
2023-10-26 04:57:44 +02:00
use App\Models\Document;
use App\Models\Expense;
2023-10-26 04:57:44 +02:00
use App\Models\ExpenseCategory;
use App\Models\GroupSetting;
2023-04-28 04:10:53 +02:00
use App\Models\Invoice;
use App\Models\Payment;
2023-10-26 04:57:44 +02:00
use App\Models\PaymentTerm;
use App\Models\Product;
2020-01-21 01:32:34 +01:00
use App\Models\Project;
2023-10-26 04:57:44 +02:00
use App\Models\PurchaseOrder;
use App\Models\Quote;
use App\Models\RecurringExpense;
use App\Models\RecurringInvoice;
2023-01-16 22:31:07 +01:00
use App\Models\Scheduler;
2023-10-26 04:57:44 +02:00
use App\Models\Subscription;
2020-08-24 04:45:53 +02:00
use App\Models\SystemLog;
2023-10-26 04:57:44 +02:00
use App\Models\Task;
2020-10-25 21:56:02 +01:00
use App\Models\TaskStatus;
2023-10-26 04:57:44 +02:00
use App\Models\TaxRate;
use App\Models\User;
use App\Models\Vendor;
use App\Models\Webhook;
2019-03-27 05:50:13 +01:00
use App\Utils\Traits\MakesHash;
2023-10-26 04:57:44 +02:00
use stdClass;
2019-03-27 05:50:13 +01:00
/**
* Class CompanyTransformer.
2019-03-27 05:50:13 +01:00
*/
class CompanyTransformer extends EntityTransformer
{
2019-04-03 03:17:21 +02:00
use MakesHash;
2019-03-27 05:50:13 +01:00
/**
* @var array
*/
2023-08-21 03:29:31 +02:00
protected array $defaultIncludes = [
2020-08-06 05:04:09 +02:00
'documents',
2019-03-27 05:50:13 +01:00
];
/**
* @var array
*/
2023-08-21 03:29:31 +02:00
protected array $availableIncludes = [
2020-08-06 05:04:09 +02:00
'documents',
2019-03-27 05:50:13 +01:00
'users',
'designs',
2019-03-27 05:50:13 +01:00
'account',
'clients',
'contacts',
'invoices',
2020-09-10 03:05:42 +02:00
'recurring_invoices',
2019-03-27 05:50:13 +01:00
'tax_rates',
'products',
'country',
'timezone',
'language',
'expenses',
'vendors',
2019-03-27 05:50:13 +01:00
'payments',
'payment_terms',
2019-09-11 01:31:55 +02:00
'company_user',
2019-10-17 13:49:09 +02:00
'groups',
'company_gateways',
'activities',
'quotes',
'credits',
2020-01-21 01:32:34 +01:00
'projects',
'tasks',
'ledger',
2020-07-07 00:35:28 +02:00
'webhooks',
'tokens',
2020-08-24 04:45:53 +02:00
'tokens_hashed',
'system_logs',
2020-10-25 21:56:02 +01:00
'expense_categories',
'task_statuses',
2021-03-29 04:14:55 +02:00
'subscriptions',
2021-08-29 12:34:53 +02:00
'recurring_expenses',
2022-06-14 14:18:20 +02:00
'purchase_orders',
'bank_integrations',
2022-09-14 08:48:56 +02:00
'bank_transactions',
2022-11-21 11:49:33 +01:00
'bank_transaction_rules',
2023-01-16 22:31:07 +01:00
'task_schedulers',
'schedulers',
2019-03-27 05:50:13 +01:00
];
/**
* @param Company $company
*
* @return array
*/
public function transform(Company $company)
{
2024-01-14 05:05:00 +01:00
$std = new stdClass();
2019-03-27 05:50:13 +01:00
return [
'id' => (string) $this->encodePrimaryKey($company->id),
'company_key' => (string) $company->company_key ?: '',
'update_products' => (bool) $company->update_products,
'fill_products' => (bool) $company->fill_products,
'convert_products' => (bool) $company->convert_products,
'custom_surcharge_taxes1' => (bool) $company->custom_surcharge_taxes1,
'custom_surcharge_taxes2' => (bool) $company->custom_surcharge_taxes2,
'custom_surcharge_taxes3' => (bool) $company->custom_surcharge_taxes3,
'custom_surcharge_taxes4' => (bool) $company->custom_surcharge_taxes4,
'show_product_cost' => (bool) $company->show_product_cost,
'enable_product_cost' => (bool) $company->enable_product_cost,
'show_product_details' => (bool) $company->show_product_details,
'enable_product_quantity' => (bool) $company->enable_product_quantity,
'default_quantity' => (bool) $company->default_quantity,
2023-12-15 18:15:55 +01:00
'custom_fields' => (object) $company->custom_fields ?? $std,
2019-09-27 00:14:02 +02:00
'size_id' => (string) $company->size_id ?: '',
'industry_id' => (string) $company->industry_id ?: '',
2023-11-21 22:52:10 +01:00
'first_month_of_year' => (string) $company->first_month_of_year ?: '1',
'first_day_of_week' => (string) $company->first_day_of_week ?: '',
'subdomain' => (string) $company->subdomain ?: '',
'portal_mode' => (string) $company->portal_mode ?: '',
'portal_domain' => (string) $company->portal_domain ?: '',
2023-04-28 04:10:53 +02:00
'settings' => $company->settings ?? '',
'enabled_tax_rates' => (int) $company->enabled_tax_rates,
'enabled_modules' => (int) $company->enabled_modules,
'updated_at' => (int) $company->updated_at,
'archived_at' => (int) $company->deleted_at,
2023-12-15 18:15:55 +01:00
'created_at' => (int) $company->created_at,
'slack_webhook_url' => (string) $company->slack_webhook_url,
'google_analytics_url' => (string) $company->google_analytics_key, //@deprecate 1-2-2021
'google_analytics_key' => (string) $company->google_analytics_key,
2022-12-07 08:39:05 +01:00
'matomo_url' => (string) $company->matomo_url,
2022-12-08 01:39:43 +01:00
'matomo_id' => (string) $company->matomo_id ?: '',
2020-06-28 12:28:35 +02:00
'enabled_item_tax_rates' => (int) $company->enabled_item_tax_rates,
'client_can_register' => (bool) $company->client_can_register,
2023-04-19 05:47:47 +02:00
// 'is_large' => (bool) $company->is_large,
'is_large' => (bool) $this->isLarge($company),
2020-11-01 06:18:40 +01:00
'is_disabled' => (bool) $company->is_disabled,
'enable_shop_api' => (bool) $company->enable_shop_api,
2023-12-15 18:15:55 +01:00
'mark_expenses_invoiceable' => (bool) $company->mark_expenses_invoiceable,
2020-10-14 23:25:40 +02:00
'mark_expenses_paid' => (bool) $company->mark_expenses_paid,
2020-10-22 23:00:49 +02:00
'invoice_expense_documents' => (bool) $company->invoice_expense_documents,
'invoice_task_timelog' => (bool) $company->invoice_task_timelog,
'auto_start_tasks' => (bool) $company->auto_start_tasks,
2020-10-26 20:10:04 +01:00
'invoice_task_documents' => (bool) $company->invoice_task_documents,
2020-10-29 00:14:09 +01:00
'show_tasks_table' => (bool) $company->show_tasks_table,
'use_credits_payment' => 'always', // @deprecate 1-2-2021
'default_task_is_date_based' => (bool) $company->default_task_is_date_based,
'enable_product_discount' => (bool) $company->enable_product_discount,
2023-12-15 18:15:55 +01:00
'calculate_expense_tax_by_amount' => (bool) $company->calculate_expense_tax_by_amount,
'hide_empty_columns_on_pdf' => false, // @deprecate 1-2-2021
'expense_inclusive_taxes' => (bool) $company->expense_inclusive_taxes,
2023-12-15 18:15:55 +01:00
'expense_amount_is_pretax' => (bool) true, //@deprecate 1-2-2021
'oauth_password_required' => (bool) $company->oauth_password_required,
'session_timeout' => (int) $company->session_timeout,
2021-03-09 11:34:39 +01:00
'default_password_timeout' => (int) $company->default_password_timeout,
'invoice_task_datelog' => (bool) $company->invoice_task_datelog,
2021-05-06 23:12:07 +02:00
'show_task_end_date' => (bool) $company->show_task_end_date,
'markdown_enabled' => (bool) $company->markdown_enabled,
2021-08-19 00:12:39 +02:00
'use_comma_as_decimal_place' => (bool) $company->use_comma_as_decimal_place,
2021-08-25 11:41:03 +02:00
'report_include_drafts' => (bool) $company->report_include_drafts,
2021-10-08 11:51:55 +02:00
'client_registration_fields' => (array) $company->client_registration_fields,
2022-02-07 09:01:05 +01:00
'convert_rate_to_client' => (bool) $company->convert_rate_to_client,
2022-03-29 10:09:46 +02:00
'markdown_email_enabled' => (bool) $company->markdown_email_enabled,
'stop_on_unpaid_recurring' => (bool) $company->stop_on_unpaid_recurring,
'use_quote_terms_on_conversion' => (bool) $company->use_quote_terms_on_conversion,
2022-06-03 12:50:19 +02:00
'stock_notification' => (bool) $company->stock_notification,
'inventory_notification_threshold' => (int) $company->inventory_notification_threshold,
'track_inventory' => (bool) $company->track_inventory,
'enable_applying_payments' => (bool) $company->enable_applying_payments,
2022-07-29 11:14:18 +02:00
'enabled_expense_tax_rates' => (int) $company->enabled_expense_tax_rates,
'invoice_task_project' => (bool) $company->invoice_task_project,
'report_include_deleted' => (bool) $company->report_include_deleted,
2022-11-23 00:01:37 +01:00
'invoice_task_lock' => (bool) $company->invoice_task_lock,
'convert_payment_currency' => (bool) $company->convert_payment_currency,
'convert_expense_currency' => (bool) $company->convert_expense_currency,
'notify_vendor_when_paid' => (bool) $company->notify_vendor_when_paid,
'invoice_task_hours' => (bool) $company->invoice_task_hours,
'calculate_taxes' => (bool) $company->calculate_taxes,
2024-01-14 05:05:00 +01:00
'tax_data' => $company->tax_data ?: new \stdClass(),
2023-05-15 13:20:47 +02:00
'has_e_invoice_certificate' => $company->e_invoice_certificate ? true : false,
'has_e_invoice_certificate_passphrase' => $company->e_invoice_certificate_passphrase ? true : false,
'invoice_task_project_header' => (bool) $company->invoice_task_project_header,
'invoice_task_item_description' => (bool) $company->invoice_task_item_description,
2023-06-29 06:46:48 +02:00
'origin_tax_data' => $company->origin_tax_data ?: new \stdClass,
'expense_mailbox' => (bool) $company->expense_mailbox,
'expense_mailbox_active' => (bool) $company->expense_mailbox_active,
'inbound_mailbox_allow_company_users' => (bool) $company->inbound_mailbox_allow_company_users,
'inbound_mailbox_allow_vendors' => (bool) $company->inbound_mailbox_allow_vendors,
'inbound_mailbox_allow_clients' => (bool) $company->inbound_mailbox_allow_clients,
'inbound_mailbox_allow_unknown' => (bool) $company->inbound_mailbox_allow_unknown,
'inbound_mailbox_blacklist_domains' => $company->inbound_mailbox_blacklist_domains,
'inbound_mailbox_blacklist_senders' => $company->inbound_mailbox_blacklist_senders,
'inbound_mailbox_whitelist_domains' => $company->inbound_mailbox_whitelist_domains,
'inbound_mailbox_whitelist_senders' => $company->inbound_mailbox_whitelist_senders,
'smtp_host' => (string) $company->smtp_host ?? '',
'smtp_port' => (int) $company->smtp_port ?? 25,
'smtp_encryption' => (string) $company->smtp_encryption ?? 'tls',
'smtp_username' => $company->smtp_username ? '********' : '',
'smtp_password' => $company->smtp_password ? '********' : '',
'smtp_local_domain' => (string) $company->smtp_local_domain ?? '',
'smtp_verify_peer' => (bool) $company->smtp_verify_peer,
2019-03-27 05:50:13 +01:00
];
}
2023-04-19 05:47:47 +02:00
private function isLarge(Company $company): bool
{
2023-04-28 04:10:53 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-04-19 05:47:47 +02:00
//if the user is attached to more than one company AND they are not an admin across all companies
2023-10-26 04:57:44 +02:00
if ($company->is_large || ($user->company_users()->count() > 1 && ($user->company_users()->where('is_admin', 1)->count() != $user->company_users()->count()))) {
2023-04-19 05:47:47 +02:00
return true;
}
return false;
}
2020-10-25 21:56:02 +01:00
public function includeExpenseCategories(Company $company)
{
$transformer = new ExpenseCategoryTransformer($this->serializer);
return $this->includeCollection($company->expense_categories, $transformer, ExpenseCategory::class);
}
public function includeTaskStatuses(Company $company)
{
$transformer = new TaskStatusTransformer($this->serializer);
return $this->includeCollection($company->task_statuses, $transformer, TaskStatus::class);
}
2020-08-06 05:04:09 +02:00
public function includeDocuments(Company $company)
{
$transformer = new DocumentTransformer($this->serializer);
return $this->includeCollection($company->documents, $transformer, Document::class);
}
public function includeCompanyUser(Company $company)
{
$transformer = new CompanyUserTransformer($this->serializer);
return $this->includeItem($company->company_users->where('user_id', auth()->user()->id)->first(), $transformer, CompanyUser::class);
}
2019-03-27 05:50:13 +01:00
2020-07-07 00:35:28 +02:00
public function includeTokens(Company $company)
{
$transformer = new CompanyTokenTransformer($this->serializer);
return $this->includeCollection($company->tokens, $transformer, CompanyToken::class);
}
2023-01-16 22:31:07 +01:00
public function includeBankTransactions(Company $company)
2022-09-14 08:21:40 +02:00
{
$transformer = new BankTransactionTransformer($this->serializer);
2022-09-14 08:48:56 +02:00
return $this->includeCollection($company->bank_transactions, $transformer, BankTransaction::class);
}
2022-11-21 11:49:33 +01:00
2023-01-16 22:31:07 +01:00
public function includeTaskSchedulers(Company $company)
{
$transformer = new SchedulerTransformer($this->serializer);
return $this->includeCollection($company->schedulers, $transformer, Scheduler::class);
}
public function includeSchedulers(Company $company)
{
$transformer = new SchedulerTransformer($this->serializer);
return $this->includeCollection($company->schedulers, $transformer, Scheduler::class);
}
2023-01-16 22:31:07 +01:00
2022-11-21 11:49:33 +01:00
public function includeBankTransactionRules(Company $company)
{
$transformer = new BankTransactionRuleTransformer($this->serializer);
return $this->includeCollection($company->bank_transaction_rules, $transformer, BankTransactionRule::class);
}
2022-09-14 08:29:00 +02:00
public function includeBankIntegrations(Company $company)
{
$transformer = new BankIntegrationTransformer($this->serializer);
return $this->includeCollection($company->bank_integrations, $transformer, BankIntegration::class);
}
public function includeTokensHashed(Company $company)
{
$transformer = new CompanyTokenHashedTransformer($this->serializer);
return $this->includeCollection($company->tokens, $transformer, CompanyToken::class);
}
2020-07-07 00:35:28 +02:00
public function includeWebhooks(Company $company)
{
$transformer = new WebhookTransformer($this->serializer);
return $this->includeCollection($company->webhooks, $transformer, Webhook::class);
}
public function includeActivities(Company $company)
{
$transformer = new ActivityTransformer($this->serializer);
return $this->includeCollection($company->activities, $transformer, Activity::class);
}
2019-03-28 03:36:36 +01:00
public function includeUsers(Company $company)
{
$transformer = new UserTransformer($this->serializer);
2021-01-04 13:38:00 +01:00
$users = $company->users->map(function ($user) use ($company) {
$user->company_id = $company->id;
2021-01-04 13:38:00 +01:00
return $user;
});
2020-12-26 09:03:24 +01:00
return $this->includeCollection($users, $transformer, User::class);
2019-03-28 03:36:36 +01:00
}
2019-10-17 13:49:09 +02:00
public function includeCompanyGateways(Company $company)
{
$transformer = new CompanyGatewayTransformer($this->serializer);
return $this->includeCollection($company->company_gateways, $transformer, CompanyGateway::class);
}
2019-03-28 03:36:36 +01:00
public function includeClients(Company $company)
{
$transformer = new ClientTransformer($this->serializer);
return $this->includeCollection($company->clients, $transformer, Client::class);
}
2020-01-21 01:32:34 +01:00
public function includeProjects(Company $company)
{
$transformer = new ProjectTransformer($this->serializer);
return $this->includeCollection($company->projects, $transformer, Project::class);
}
public function includeTasks(Company $company)
2020-01-21 01:32:34 +01:00
{
$transformer = new TaskTransformer($this->serializer);
return $this->includeCollection($company->tasks, $transformer, Task::class);
}
public function includeExpenses(Company $company)
{
$transformer = new ExpenseTransformer($this->serializer);
return $this->includeCollection($company->expenses, $transformer, Expense::class);
}
public function includeVendors(Company $company)
{
$transformer = new VendorTransformer($this->serializer);
return $this->includeCollection($company->vendors, $transformer, Vendor::class);
}
public function includeGroups(Company $company)
{
$transformer = new GroupSettingTransformer($this->serializer);
return $this->includeCollection($company->groups, $transformer, GroupSetting::class);
}
2019-03-28 03:36:36 +01:00
public function includeInvoices(Company $company)
{
$transformer = new InvoiceTransformer($this->serializer);
return $this->includeCollection($company->invoices, $transformer, Invoice::class);
}
2020-09-10 03:05:42 +02:00
public function includeRecurringInvoices(Company $company)
{
$transformer = new RecurringInvoiceTransformer($this->serializer);
return $this->includeCollection($company->recurring_invoices, $transformer, RecurringInvoice::class);
}
2021-08-29 12:34:53 +02:00
public function includeRecurringExpenses(Company $company)
{
$transformer = new RecurringExpenseTransformer($this->serializer);
return $this->includeCollection($company->recurring_expenses, $transformer, RecurringExpense::class);
}
public function includeQuotes(Company $company)
{
$transformer = new QuoteTransformer($this->serializer);
return $this->includeCollection($company->quotes, $transformer, Quote::class);
}
public function includeCredits(Company $company)
{
$transformer = new CreditTransformer($this->serializer);
return $this->includeCollection($company->credits, $transformer, Credit::class);
}
2019-03-28 03:36:36 +01:00
public function includeAccount(Company $company)
{
$transformer = new AccountTransformer($this->serializer);
return $this->includeItem($company->account, $transformer, Account::class);
}
public function includeTaxRates(Company $company)
{
$transformer = new TaxRateTransformer($this->serializer);
return $this->includeCollection($company->tax_rates, $transformer, TaxRate::class);
}
public function includeProducts(Company $company)
{
$transformer = new ProductTransformer($this->serializer);
return $this->includeCollection($company->products, $transformer, Product::class);
}
public function includePayments(Company $company)
{
$transformer = new PaymentTransformer($this->serializer);
return $this->includeCollection($company->payments, $transformer, Payment::class);
}
public function includeDesigns(Company $company)
{
$transformer = new DesignTransformer($this->serializer);
return $this->includeCollection($company->designs()->get(), $transformer, Design::class);
}
public function includeLedger(Company $company)
{
$transformer = new CompanyLedgerTransformer($this->serializer);
return $this->includeCollection($company->ledger, $transformer, CompanyLedger::class);
}
public function includePaymentTerms(Company $company)
{
$transformer = new PaymentTermTransformer($this->serializer);
return $this->includeCollection($company->payment_terms()->get(), $transformer, PaymentTerm::class);
}
2020-08-24 04:45:53 +02:00
public function includeSystemLogs(Company $company)
{
$transformer = new SystemLogTransformer($this->serializer);
return $this->includeCollection($company->system_logs, $transformer, SystemLog::class);
}
2021-03-10 01:08:58 +01:00
public function includeSubscriptions(Company $company)
2021-03-10 01:08:58 +01:00
{
$transformer = new SubscriptionTransformer($this->serializer);
2021-03-10 01:08:58 +01:00
2021-03-29 04:14:55 +02:00
return $this->includeCollection($company->subscriptions, $transformer, Subscription::class);
2021-03-10 01:08:58 +01:00
}
2022-06-14 14:18:20 +02:00
public function includePurchaseOrders(Company $company)
{
$transformer = new PurchaseOrderTransformer($this->serializer);
return $this->includeCollection($company->purchase_orders, $transformer, PurchaseOrder::class);
}
2021-01-04 13:38:00 +01:00
}