1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Export/CSV/BaseExport.php

1224 lines
46 KiB
PHP
Raw Normal View History

2022-04-27 05:07:29 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @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)
2022-04-27 05:07:29 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Export\CSV;
2023-03-01 13:02:48 +01:00
use App\Models\Client;
2023-10-26 04:57:44 +02:00
use App\Models\ClientContact;
use App\Models\Company;
2023-10-26 04:57:44 +02:00
use App\Models\Credit;
use App\Models\Document;
2023-07-15 01:34:27 +02:00
use App\Models\Expense;
2023-04-24 06:55:56 +02:00
use App\Models\Invoice;
2023-07-05 08:33:03 +02:00
use App\Models\Payment;
2023-10-26 04:57:44 +02:00
use App\Models\Product;
2023-09-12 03:01:14 +02:00
use App\Models\PurchaseOrder;
2023-10-26 04:57:44 +02:00
use App\Models\Quote;
2023-09-12 03:22:59 +02:00
use App\Models\RecurringInvoice;
2023-10-26 04:57:44 +02:00
use App\Models\Task;
use App\Models\Vendor;
2023-07-05 04:56:37 +02:00
use App\Transformers\PaymentTransformer;
2023-10-26 04:57:44 +02:00
use App\Transformers\TaskTransformer;
use App\Utils\Helpers;
use App\Utils\Traits\MakesHash;
2023-04-24 06:55:56 +02:00
use Illuminate\Database\Eloquent\Builder;
2023-10-26 04:57:44 +02:00
use Illuminate\Support\Carbon;
use League\Fractal\Manager;
2023-07-05 04:56:37 +02:00
use League\Fractal\Serializer\ArraySerializer;
2022-04-27 05:07:29 +02:00
class BaseExport
{
2023-03-01 12:59:24 +01:00
use MakesHash;
2023-08-24 05:22:35 +02:00
public Company $company;
2023-03-01 12:59:24 +01:00
public array $input;
public string $date_key = '';
public array $entity_keys = [];
public string $start_date = '';
public string $end_date = '';
2023-03-01 13:02:48 +01:00
public string $client_description = 'All Clients';
2023-07-13 07:23:57 +02:00
protected array $vendor_report_keys = [
'address1' => 'vendor.address1',
'address2' => 'vendor.address2',
'city' => 'vendor.city',
'country' => 'vendor.country_id',
'custom_value1' => 'vendor.custom_value1',
'custom_value2' => 'vendor.custom_value2',
'custom_value3' => 'vendor.custom_value3',
'custom_value4' => 'vendor.custom_value4',
'id_number' => 'vendor.id_number',
'name' => 'vendor.name',
'number' => 'vendor.number',
2023-09-12 09:23:52 +02:00
'phone' => 'vendor.phone',
2023-07-13 07:23:57 +02:00
'postal_code' => 'vendor.postal_code',
'private_notes' => 'vendor.private_notes',
'public_notes' => 'vendor.public_notes',
'state' => 'vendor.state',
'vat_number' => 'vendor.vat_number',
'website' => 'vendor.website',
'currency' => 'vendor.currency',
'first_name' => 'vendor_contact.first_name',
'last_name' => 'vendor_contact.last_name',
'contact_phone' => 'vendor_contact.phone',
'contact_custom_value1' => 'vendor_contact.custom_value1',
'contact_custom_value2' => 'vendor_contact.custom_value2',
'contact_custom_value3' => 'vendor_contact.custom_value3',
'contact_custom_value4' => 'vendor_contact.custom_value4',
'email' => 'vendor_contact.email',
'status' => 'vendor.status',
2023-09-28 22:48:50 +02:00
'classification' => 'vendor.classification',
2023-07-13 07:23:57 +02:00
];
2023-07-05 04:56:37 +02:00
protected array $client_report_keys = [
"name" => "client.name",
2023-10-20 22:50:58 +02:00
"number" => "client.number",
2023-07-14 11:08:55 +02:00
"user" => "client.user",
"assigned_user" => "client.assigned_user",
2023-07-05 04:56:37 +02:00
"balance" => "client.balance",
"paid_to_date" => "client.paid_to_date",
"currency" => "client.currency_id",
"website" => "client.website",
"private_notes" => "client.private_notes",
"industry" => "client.industry_id",
"size" => "client.size_id",
2023-07-05 08:07:36 +02:00
"work_phone" => "client.phone",
2023-07-05 04:56:37 +02:00
"address1" => "client.address1",
"address2" => "client.address2",
"city" => "client.city",
"state" => "client.state",
"postal_code" => "client.postal_code",
"country" => "client.country_id",
"shipping_address1" => "client.shipping_address1",
"shipping_address2" => "client.shipping_address2",
"shipping_city" => "client.shipping_city",
"shipping_state" => "client.shipping_state",
"shipping_postal_code" => "client.shipping_postal_code",
"shipping_country" => "client.shipping_country_id",
"payment_terms" => "client.payment_terms",
"vat_number" => "client.vat_number",
"id_number" => "client.id_number",
"public_notes" => "client.public_notes",
"phone" => "contact.phone",
"first_name" => "contact.first_name",
"last_name" => "contact.last_name",
"email" => "contact.email",
2023-08-07 12:47:05 +02:00
'custom_value1' => 'client.custom_value1',
'custom_value2' => 'client.custom_value2',
'custom_value3' => 'client.custom_value3',
'custom_value4' => 'client.custom_value4',
"contact_custom_value1" => "contact.custom_value1",
"contact_custom_value2" => "contact.custom_value2",
"contact_custom_value3" => "contact.custom_value3",
"contact_custom_value4" => "contact.custom_value4",
2023-09-28 22:39:28 +02:00
'payment_balance' => 'client.payment_balance',
'credit_balance' => 'client.credit_balance',
2023-09-28 22:48:50 +02:00
'classification' => 'client.classification',
2023-07-05 04:56:37 +02:00
];
protected array $invoice_report_keys = [
2023-10-03 10:31:17 +02:00
'name' => 'client.name',
2023-07-05 04:56:37 +02:00
"invoice_number" => "invoice.number",
"amount" => "invoice.amount",
"balance" => "invoice.balance",
"paid_to_date" => "invoice.paid_to_date",
"po_number" => "invoice.po_number",
"date" => "invoice.date",
"due_date" => "invoice.due_date",
"terms" => "invoice.terms",
"footer" => "invoice.footer",
"status" => "invoice.status",
"public_notes" => "invoice.public_notes",
"private_notes" => "invoice.private_notes",
"uses_inclusive_taxes" => "invoice.uses_inclusive_taxes",
"is_amount_discount" => "invoice.is_amount_discount",
2023-08-28 07:13:55 +02:00
"discount" => "invoice.discount",
2023-07-05 04:56:37 +02:00
"partial" => "invoice.partial",
"partial_due_date" => "invoice.partial_due_date",
"surcharge1" => "invoice.custom_surcharge1",
"surcharge2" => "invoice.custom_surcharge2",
"surcharge3" => "invoice.custom_surcharge3",
"surcharge4" => "invoice.custom_surcharge4",
"exchange_rate" => "invoice.exchange_rate",
"tax_amount" => "invoice.total_taxes",
2023-07-05 08:56:07 +02:00
"assigned_user" => "invoice.assigned_user_id",
"user" => "invoice.user_id",
2023-08-28 07:13:55 +02:00
"custom_value1" => "invoice.custom_value1",
"custom_value2" => "invoice.custom_value2",
"custom_value3" => "invoice.custom_value3",
"custom_value4" => "invoice.custom_value4",
'tax_name1' => 'invoice.tax_name1',
'tax_name2' => 'invoice.tax_name2',
'tax_name3' => 'invoice.tax_name3',
'tax_rate1' => 'invoice.tax_rate1',
'tax_rate2' => 'invoice.tax_rate2',
'tax_rate3' => 'invoice.tax_rate3',
2023-10-17 12:38:35 +02:00
'recurring_invoice' => 'invoice.recurring_id',
2023-11-06 22:23:51 +01:00
'auto_bill' => 'invoice.auto_bill_enabled',
2023-07-05 08:56:07 +02:00
];
2023-10-26 04:57:44 +02:00
protected array $recurring_invoice_report_keys = [
2023-07-18 07:11:45 +02:00
"invoice_number" => "recurring_invoice.number",
"amount" => "recurring_invoice.amount",
"balance" => "recurring_invoice.balance",
"paid_to_date" => "recurring_invoice.paid_to_date",
"po_number" => "recurring_invoice.po_number",
"date" => "recurring_invoice.date",
"due_date" => "recurring_invoice.due_date",
"terms" => "recurring_invoice.terms",
"footer" => "recurring_invoice.footer",
"status" => "recurring_invoice.status",
"public_notes" => "recurring_invoice.public_notes",
"private_notes" => "recurring_invoice.private_notes",
"uses_inclusive_taxes" => "recurring_invoice.uses_inclusive_taxes",
"is_amount_discount" => "recurring_invoice.is_amount_discount",
2023-08-28 07:13:55 +02:00
"discount" => "recurring_invoice.discount",
2023-07-18 07:11:45 +02:00
"partial" => "recurring_invoice.partial",
"partial_due_date" => "recurring_invoice.partial_due_date",
"surcharge1" => "recurring_invoice.custom_surcharge1",
"surcharge2" => "recurring_invoice.custom_surcharge2",
"surcharge3" => "recurring_invoice.custom_surcharge3",
"surcharge4" => "recurring_invoice.custom_surcharge4",
"exchange_rate" => "recurring_invoice.exchange_rate",
"tax_amount" => "recurring_invoice.total_taxes",
"assigned_user" => "recurring_invoice.assigned_user_id",
"user" => "recurring_invoice.user_id",
"frequency_id" => "recurring_invoice.frequency_id",
2023-08-28 07:13:55 +02:00
"next_send_date" => "recurring_invoice.next_send_date",
"custom_value1" => "recurring_invoice.custom_value1",
"custom_value2" => "recurring_invoice.custom_value2",
"custom_value3" => "recurring_invoice.custom_value3",
"custom_value4" => "recurring_invoice.custom_value4",
'tax_name1' => 'recurring_invoice.tax_name1',
'tax_name2' => 'recurring_invoice.tax_name2',
'tax_name3' => 'recurring_invoice.tax_name3',
'tax_rate1' => 'recurring_invoice.tax_rate1',
'tax_rate2' => 'recurring_invoice.tax_rate2',
'tax_rate3' => 'recurring_invoice.tax_rate3',
2023-11-06 22:23:51 +01:00
'auto_bill' => 'recurring_invoice.auto_bill',
'auto_bill_enabled' => 'recurring_invoice.auto_bill_enabled',
2023-07-18 07:11:45 +02:00
];
2023-07-13 07:23:57 +02:00
protected array $purchase_order_report_keys = [
'amount' => 'purchase_order.amount',
'balance' => 'purchase_order.balance',
'vendor' => 'purchase_order.vendor_id',
'custom_value1' => 'purchase_order.custom_value1',
'custom_value2' => 'purchase_order.custom_value2',
'custom_value3' => 'purchase_order.custom_value3',
'custom_value4' => 'purchase_order.custom_value4',
'date' => 'purchase_order.date',
'discount' => 'purchase_order.discount',
'due_date' => 'purchase_order.due_date',
'exchange_rate' => 'purchase_order.exchange_rate',
'footer' => 'purchase_order.footer',
'number' => 'purchase_order.number',
'paid_to_date' => 'purchase_order.paid_to_date',
'partial' => 'purchase_order.partial',
'partial_due_date' => 'purchase_order.partial_due_date',
'po_number' => 'purchase_order.po_number',
'private_notes' => 'purchase_order.private_notes',
'public_notes' => 'purchase_order.public_notes',
2023-10-18 06:22:38 +02:00
'status' => 'purchase_order.status',
2023-07-13 07:23:57 +02:00
'tax_name1' => 'purchase_order.tax_name1',
'tax_name2' => 'purchase_order.tax_name2',
'tax_name3' => 'purchase_order.tax_name3',
'tax_rate1' => 'purchase_order.tax_rate1',
'tax_rate2' => 'purchase_order.tax_rate2',
'tax_rate3' => 'purchase_order.tax_rate3',
'terms' => 'purchase_order.terms',
'total_taxes' => 'purchase_order.total_taxes',
'currency_id' => 'purchase_order.currency_id',
];
2023-08-29 11:15:36 +02:00
protected array $product_report_keys = [
2023-09-12 03:55:11 +02:00
// 'project' => 'project_id',
// 'vendor' => 'vendor_id',
2023-08-29 11:15:36 +02:00
'custom_value1' => 'custom_value1',
'custom_value2' => 'custom_value2',
'custom_value3' => 'custom_value3',
'custom_value4' => 'custom_value4',
'product_key' => 'product_key',
'notes' => 'notes',
'cost' => 'cost',
'price' => 'price',
'quantity' => 'quantity',
'tax_rate1' => 'tax_rate1',
'tax_rate2' => 'tax_rate2',
'tax_rate3' => 'tax_rate3',
'tax_name1' => 'tax_name1',
'tax_name2' => 'tax_name2',
'tax_name3' => 'tax_name3',
2023-09-12 03:55:11 +02:00
'image' => 'product_image',
'tax_category' => 'tax_id',
'max_quantity' => 'max_quantity',
'in_stock_quantity' => 'in_stock_quantity',
2023-08-29 11:15:36 +02:00
];
2023-07-05 08:56:07 +02:00
protected array $item_report_keys = [
2023-07-05 04:56:37 +02:00
"quantity" => "item.quantity",
"cost" => "item.cost",
"product_key" => "item.product_key",
"notes" => "item.notes",
2023-08-28 14:44:04 +02:00
"tax_name1" => "item.tax_name1",
"tax_rate1" => "item.tax_rate1",
"tax_name2" => "item.tax_name2",
"tax_rate2" => "item.tax_rate2",
"tax_name3" => "item.tax_name3",
"tax_rate3" => "item.tax_rate3",
2023-07-05 08:56:07 +02:00
"custom_value1" => "item.custom_value1",
"custom_value2" => "item.custom_value2",
"custom_value3" => "item.custom_value3",
"custom_value4" => "item.custom_value4",
"discount" => "item.discount",
2023-07-05 04:56:37 +02:00
"type" => "item.type_id",
"tax_category" => "item.tax_id",
2023-08-28 14:44:04 +02:00
'is_amount_discount' => 'item.is_amount_discount',
'line_total' => 'item.line_total',
'gross_line_total' => 'item.gross_line_total',
2023-11-06 22:23:51 +01:00
'tax_amount' => 'item.tax_amount',
2023-07-05 04:56:37 +02:00
];
protected array $quote_report_keys = [
2023-07-16 12:51:21 +02:00
'custom_value1' => 'quote.custom_value1',
'custom_value2' => 'quote.custom_value2',
'custom_value3' => 'quote.custom_value3',
'custom_value4' => 'quote.custom_value4',
2023-07-16 12:17:43 +02:00
"number" => "quote.number",
2023-07-05 04:56:37 +02:00
"amount" => "quote.amount",
"balance" => "quote.balance",
"paid_to_date" => "quote.paid_to_date",
"po_number" => "quote.po_number",
"date" => "quote.date",
2023-07-16 12:17:43 +02:00
"valid_until" => "quote.due_date",
2023-07-05 04:56:37 +02:00
"terms" => "quote.terms",
"footer" => "quote.footer",
"status" => "quote.status",
"public_notes" => "quote.public_notes",
"private_notes" => "quote.private_notes",
"uses_inclusive_taxes" => "quote.uses_inclusive_taxes",
"is_amount_discount" => "quote.is_amount_discount",
2023-08-29 14:56:36 +02:00
"discount" => "quote.discount",
2023-07-05 04:56:37 +02:00
"partial" => "quote.partial",
"partial_due_date" => "quote.partial_due_date",
"surcharge1" => "quote.custom_surcharge1",
"surcharge2" => "quote.custom_surcharge2",
"surcharge3" => "quote.custom_surcharge3",
"surcharge4" => "quote.custom_surcharge4",
"exchange_rate" => "quote.exchange_rate",
"tax_amount" => "quote.total_taxes",
"assigned_user" => "quote.assigned_user_id",
"user" => "quote.user_id",
2023-08-28 07:13:55 +02:00
'tax_name1' => 'quote.tax_name1',
'tax_name2' => 'quote.tax_name2',
'tax_name3' => 'quote.tax_name3',
'tax_rate1' => 'quote.tax_rate1',
'tax_rate2' => 'quote.tax_rate2',
'tax_rate3' => 'quote.tax_rate3',
2023-07-05 04:56:37 +02:00
];
protected array $credit_report_keys = [
"credit_number" => "credit.number",
"amount" => "credit.amount",
"balance" => "credit.balance",
"paid_to_date" => "credit.paid_to_date",
"po_number" => "credit.po_number",
"date" => "credit.date",
"due_date" => "credit.due_date",
"terms" => "credit.terms",
2023-08-24 05:22:35 +02:00
"discount" => "credit.discount",
2023-07-05 04:56:37 +02:00
"footer" => "credit.footer",
"status" => "credit.status",
"public_notes" => "credit.public_notes",
"private_notes" => "credit.private_notes",
"uses_inclusive_taxes" => "credit.uses_inclusive_taxes",
"is_amount_discount" => "credit.is_amount_discount",
"partial" => "credit.partial",
"partial_due_date" => "credit.partial_due_date",
"surcharge1" => "credit.custom_surcharge1",
"surcharge2" => "credit.custom_surcharge2",
"surcharge3" => "credit.custom_surcharge3",
"surcharge4" => "credit.custom_surcharge4",
2023-08-24 05:22:35 +02:00
"custom_value1" => "credit.custom_value1",
"custom_value2" => "credit.custom_value2",
"custom_value3" => "credit.custom_value3",
"custom_value4" => "credit.custom_value4",
2023-07-05 04:56:37 +02:00
"exchange_rate" => "credit.exchange_rate",
"tax_amount" => "credit.total_taxes",
"assigned_user" => "credit.assigned_user_id",
"user" => "credit.user_id",
];
protected array $payment_report_keys = [
"date" => "payment.date",
"amount" => "payment.amount",
"refunded" => "payment.refunded",
"applied" => "payment.applied",
"transaction_reference" => "payment.transaction_reference",
"currency" => "payment.currency",
"exchange_rate" => "payment.exchange_rate",
"number" => "payment.number",
"method" => "payment.method",
"status" => "payment.status",
"private_notes" => "payment.private_notes",
"custom_value1" => "payment.custom_value1",
"custom_value2" => "payment.custom_value2",
"custom_value3" => "payment.custom_value3",
"custom_value4" => "payment.custom_value4",
"user" => "payment.user_id",
"assigned_user" => "payment.assigned_user_id",
2023-10-18 06:38:45 +02:00
2023-07-05 04:56:37 +02:00
];
2023-07-13 13:10:54 +02:00
protected array $expense_report_keys = [
'amount' => 'expense.amount',
'category' => 'expense.category_id',
2023-09-14 06:35:43 +02:00
// 'client' => 'expense.client_id',
2023-07-13 13:10:54 +02:00
'custom_value1' => 'expense.custom_value1',
'custom_value2' => 'expense.custom_value2',
'custom_value3' => 'expense.custom_value3',
'custom_value4' => 'expense.custom_value4',
'currency' => 'expense.currency_id',
'date' => 'expense.date',
'exchange_rate' => 'expense.exchange_rate',
'converted_amount' => 'expense.foreign_amount',
'invoice_currency_id' => 'expense.invoice_currency_id',
'payment_date' => 'expense.payment_date',
'number' => 'expense.number',
'payment_type_id' => 'expense.payment_type_id',
'private_notes' => 'expense.private_notes',
'project' => 'expense.project_id',
'public_notes' => 'expense.public_notes',
'tax_amount1' => 'expense.tax_amount1',
'tax_amount2' => 'expense.tax_amount2',
'tax_amount3' => 'expense.tax_amount3',
'tax_name1' => 'expense.tax_name1',
'tax_name2' => 'expense.tax_name2',
'tax_name3' => 'expense.tax_name3',
'tax_rate1' => 'expense.tax_rate1',
'tax_rate2' => 'expense.tax_rate2',
'tax_rate3' => 'expense.tax_rate3',
'transaction_reference' => 'expense.transaction_reference',
'vendor' => 'expense.vendor_id',
'invoice' => 'expense.invoice_id',
'user' => 'expense.user',
'assigned_user' => 'expense.assigned_user',
];
protected array $task_report_keys = [
'start_date' => 'task.start_date',
'end_date' => 'task.end_date',
'duration' => 'task.duration',
'rate' => 'task.rate',
'number' => 'task.number',
'description' => 'task.description',
'custom_value1' => 'task.custom_value1',
'custom_value2' => 'task.custom_value2',
'custom_value3' => 'task.custom_value3',
'custom_value4' => 'task.custom_value4',
'status' => 'task.status_id',
'project' => 'task.project_id',
];
2023-10-18 06:22:38 +02:00
protected array $forced_client_fields = [
2023-10-18 06:38:45 +02:00
"client.name",
2023-10-18 06:22:38 +02:00
];
protected array $forced_vendor_fields = [
2023-10-18 06:38:45 +02:00
"vendor.name",
2023-10-18 06:22:38 +02:00
];
2023-03-01 12:59:24 +01:00
protected function filterByClients($query)
{
2023-03-01 13:06:46 +01:00
if (isset($this->input['client_id']) && $this->input['client_id'] != 'all') {
2023-03-01 13:02:48 +01:00
$client = Client::withTrashed()->find($this->input['client_id']);
$this->client_description = $client->present()->name;
return $query->where('client_id', $this->input['client_id']);
2023-10-26 04:57:44 +02:00
} elseif(isset($this->input['clients']) && count($this->input['clients']) > 0) {
2023-04-14 02:27:00 +02:00
2023-04-13 07:39:12 +02:00
$this->client_description = 'Multiple Clients';
return $query->whereIn('client_id', $this->input['clients']);
}
2023-03-01 12:59:24 +01:00
return $query;
}
2023-07-05 04:56:37 +02:00
protected function resolveKey($key, $entity, $transformer) :string
{
$parts = explode(".", $key);
2023-10-26 04:57:44 +02:00
if(!is_array($parts) || count($parts) < 2) {
2023-07-05 04:56:37 +02:00
return '';
2023-10-26 04:57:44 +02:00
}
2023-07-05 04:56:37 +02:00
2023-07-24 13:39:30 +02:00
$value = '';
2023-07-05 04:56:37 +02:00
match($parts[0]) {
2023-07-05 08:07:36 +02:00
'contact' => $value = $this->resolveClientContactKey($parts[1], $entity, $transformer),
2023-07-05 04:56:37 +02:00
'client' => $value = $this->resolveClientKey($parts[1], $entity, $transformer),
2023-07-13 13:10:54 +02:00
'expense' => $value = $this->resolveExpenseKey($parts[1], $entity, $transformer),
2023-07-13 07:23:57 +02:00
'vendor' => $value = $this->resolveVendorKey($parts[1], $entity, $transformer),
'vendor_contact' => $value = $this->resolveVendorContactKey($parts[1], $entity, $transformer),
2023-07-05 04:56:37 +02:00
'invoice' => $value = $this->resolveInvoiceKey($parts[1], $entity, $transformer),
2023-07-18 07:11:45 +02:00
'recurring_invoice' => $value = $this->resolveInvoiceKey($parts[1], $entity, $transformer),
2023-07-16 12:17:43 +02:00
'quote' => $value = $this->resolveQuoteKey($parts[1], $entity, $transformer),
2023-07-13 07:23:57 +02:00
'purchase_order' => $value = $this->resolvePurchaseOrderKey($parts[1], $entity, $transformer),
2023-07-05 04:56:37 +02:00
'payment' => $value = $this->resolvePaymentKey($parts[1], $entity, $transformer),
2023-07-18 10:21:12 +02:00
'task' => $value = $this->resolveTaskKey($parts[1], $entity, $transformer),
2023-07-24 13:39:30 +02:00
default => $value = '',
2023-07-05 04:56:37 +02:00
};
return $value;
}
2023-07-05 08:07:36 +02:00
private function resolveClientContactKey($column, $entity, $transformer)
{
2023-07-13 13:52:39 +02:00
2023-07-13 13:10:54 +02:00
if(!$entity->client) {
return "";
}
2023-07-05 08:07:36 +02:00
$primary_contact = $entity->client->primary_contact()->first() ?? $entity->client->contacts()->first();
2023-07-13 13:10:54 +02:00
return $primary_contact ? $primary_contact?->{$column} ?? '' : '';
2023-07-05 08:07:36 +02:00
}
2023-07-13 07:23:57 +02:00
private function resolveVendorContactKey($column, $entity, $transformer)
{
2023-10-26 04:57:44 +02:00
if(!$entity->vendor) {
2023-07-13 13:10:54 +02:00
return "";
2023-10-26 04:57:44 +02:00
}
2023-07-13 07:23:57 +02:00
$primary_contact = $entity->vendor->primary_contact()->first() ?? $entity->vendor->contacts()->first();
2023-07-13 13:10:54 +02:00
return $primary_contact ? $primary_contact?->{$column} ?? '' : '';
}
private function resolveExpenseKey($column, $entity, $transformer)
{
2023-07-15 01:34:27 +02:00
2023-10-26 04:57:44 +02:00
if($column == 'user' && $entity?->expense?->user) {
2023-07-13 13:10:54 +02:00
return $entity->expense->user->present()->name() ?? ' ';
2023-10-26 04:57:44 +02:00
}
2023-07-13 13:10:54 +02:00
2023-10-26 04:57:44 +02:00
if($column == 'assigned_user' && $entity?->expense?->assigned_user) {
2023-07-13 13:10:54 +02:00
return $entity->expense->assigned_user->present()->name() ?? ' ';
2023-10-26 04:57:44 +02:00
}
2023-07-13 13:10:54 +02:00
if($column == 'category' && $entity->expense) {
return $entity->expense->category?->name ?? ' ';
}
2023-10-26 04:57:44 +02:00
if($entity instanceof Expense) {
2023-07-15 01:34:27 +02:00
return '';
2023-10-26 04:57:44 +02:00
}
2023-07-15 01:37:33 +02:00
2023-07-15 01:34:27 +02:00
$transformed_entity = $transformer->includeExpense($entity);
$manager = new Manager();
$manager->setSerializer(new ArraySerializer());
$transformed_entity = $manager->createData($transformed_entity)->toArray();
2023-10-26 04:57:44 +02:00
if(array_key_exists($column, $transformed_entity)) {
return $transformed_entity[$column];
}
2023-07-13 13:52:39 +02:00
2023-10-26 04:57:44 +02:00
if(property_exists($entity, $column)) {
2023-07-13 13:10:54 +02:00
return $entity?->{$column} ?? '';
2023-10-26 04:57:44 +02:00
}
2023-07-13 13:10:54 +02:00
nlog("export: Could not resolve expense key: {$column}");
return '';
2023-07-13 07:23:57 +02:00
}
2023-07-18 10:21:12 +02:00
private function resolveTaskKey($column, $entity, $transformer)
{
2023-07-27 03:25:05 +02:00
// nlog("searching for {$column}");
2023-07-18 10:21:12 +02:00
$transformed_entity = $transformer->transform($entity);
if(array_key_exists($column, $transformed_entity)) {
return $transformed_entity[$column];
}
return '';
}
2023-07-13 07:23:57 +02:00
private function resolveVendorKey($column, $entity, $transformer)
{
2023-07-13 13:10:54 +02:00
2023-10-26 04:57:44 +02:00
if(!$entity->vendor) {
2023-07-13 13:10:54 +02:00
return '';
2023-10-26 04:57:44 +02:00
}
2023-07-13 13:10:54 +02:00
2023-07-13 07:23:57 +02:00
$transformed_entity = $transformer->includeVendor($entity);
$manager = new Manager();
$manager->setSerializer(new ArraySerializer());
$transformed_entity = $manager->createData($transformed_entity)->toArray();
2023-10-26 04:57:44 +02:00
if($column == 'name') {
2023-07-13 13:10:54 +02:00
return $entity->vendor->present()->name() ?: '';
2023-10-26 04:57:44 +02:00
}
2023-07-13 07:23:57 +02:00
2023-10-26 04:57:44 +02:00
if($column == 'user_id') {
2023-07-13 13:10:54 +02:00
return $entity->vendor->user->present()->name() ?: '';
2023-10-26 04:57:44 +02:00
}
2023-07-13 07:23:57 +02:00
2023-10-26 04:57:44 +02:00
if($column == 'country_id') {
2023-07-13 07:23:57 +02:00
return $entity->vendor->country ? ctrans("texts.country_{$entity->vendor->country->name}") : '';
2023-10-26 04:57:44 +02:00
}
2023-07-13 07:23:57 +02:00
if ($column == 'currency_id') {
return $entity->vendor->currency() ? $entity->vendor->currency()->code : $entity->company->currency()->code;
}
2023-10-26 04:57:44 +02:00
if($column == 'status') {
2023-07-13 13:10:54 +02:00
return $entity->stringStatus($entity->status_id) ?: '';
2023-10-26 04:57:44 +02:00
}
2023-07-13 07:23:57 +02:00
2023-10-26 04:57:44 +02:00
if(array_key_exists($column, $transformed_entity)) {
2023-07-13 07:23:57 +02:00
return $transformed_entity[$column];
2023-10-26 04:57:44 +02:00
}
2023-07-13 07:23:57 +02:00
2023-07-27 03:25:05 +02:00
// nlog("export: Could not resolve vendor key: {$column}");
2023-07-13 07:23:57 +02:00
return '';
}
2023-07-05 04:56:37 +02:00
private function resolveClientKey($column, $entity, $transformer)
{
2023-07-13 13:10:54 +02:00
2023-10-26 04:57:44 +02:00
if(!$entity->client) {
2023-07-13 13:10:54 +02:00
return '';
2023-10-26 04:57:44 +02:00
}
2023-07-13 13:10:54 +02:00
2023-07-05 04:56:37 +02:00
$transformed_client = $transformer->includeClient($entity);
$manager = new Manager();
$manager->setSerializer(new ArraySerializer());
$transformed_client = $manager->createData($transformed_client)->toArray();
2023-10-26 04:57:44 +02:00
if(in_array($column, ['client.name', 'name'])) {
2023-07-05 04:56:37 +02:00
return $transformed_client['display_name'];
2023-10-26 04:57:44 +02:00
}
2023-07-05 04:56:37 +02:00
2023-10-26 04:57:44 +02:00
if(in_array($column, ['client.user_id', 'user_id'])) {
2023-11-22 07:52:02 +01:00
return $entity->client->user ? $entity->client->user->present()->name() : '';
2023-10-26 04:57:44 +02:00
}
2023-07-05 04:56:37 +02:00
2023-10-26 04:57:44 +02:00
if(in_array($column, ['client.assigned_user_id', 'assigned_user_id'])) {
2023-11-22 07:52:02 +01:00
return $entity->client->assigned_user ? $entity->client->assigned_user->present()->name() : '';
2023-10-26 04:57:44 +02:00
}
2023-09-14 06:35:43 +02:00
2023-10-26 04:57:44 +02:00
if(in_array($column, ['client.country_id', 'country_id'])) {
2023-07-05 08:07:36 +02:00
return $entity->client->country ? ctrans("texts.country_{$entity->client->country->name}") : '';
2023-10-26 04:57:44 +02:00
}
2023-07-05 08:07:36 +02:00
2023-10-26 04:57:44 +02:00
if(in_array($column, ['client.shipping_country_id', 'shipping_country_id'])) {
2023-07-05 08:07:36 +02:00
return $entity->client->shipping_country ? ctrans("texts.country_{$entity->client->shipping_country->name}") : '';
2023-10-26 04:57:44 +02:00
}
2023-07-05 08:07:36 +02:00
2023-10-26 04:57:44 +02:00
if(in_array($column, ['client.size_id', 'size_id'])) {
2023-07-05 08:07:36 +02:00
return $entity->client->size?->name ?? '';
2023-10-26 04:57:44 +02:00
}
2023-07-05 04:56:37 +02:00
2023-10-26 04:57:44 +02:00
if(in_array($column, ['client.industry_id', 'industry_id'])) {
2023-07-05 08:07:36 +02:00
return $entity->client->industry?->name ?? '';
2023-10-26 04:57:44 +02:00
}
2023-07-05 08:07:36 +02:00
2023-10-26 04:57:44 +02:00
if (in_array($column, ['client.currency_id', 'currency_id'])) {
2023-07-05 08:07:36 +02:00
return $entity->client->currency() ? $entity->client->currency()->code : $entity->company->currency()->code;
2023-10-26 04:57:44 +02:00
}
2023-09-14 06:35:43 +02:00
2023-10-26 04:57:44 +02:00
if(in_array($column, ['payment_terms', 'client.payment_terms'])) {
2023-07-05 08:33:03 +02:00
return $entity->client->getSetting('payment_terms');
2023-10-26 04:57:44 +02:00
}
2023-09-14 06:35:43 +02:00
2023-07-05 08:07:36 +02:00
2023-10-26 04:57:44 +02:00
if(array_key_exists($column, $transformed_client)) {
2023-07-05 08:07:36 +02:00
return $transformed_client[$column];
2023-10-26 04:57:44 +02:00
}
2023-07-05 04:56:37 +02:00
2023-07-27 03:25:05 +02:00
// nlog("export: Could not resolve client key: {$column}");
2023-07-05 08:33:03 +02:00
2023-07-05 04:56:37 +02:00
return '';
}
2023-07-13 07:23:57 +02:00
private function resolvePurchaseOrderKey($column, $entity, $transformer)
{
2023-07-27 03:25:05 +02:00
// nlog("searching for {$column}");
2023-07-13 07:23:57 +02:00
$transformed_entity = $transformer->transform($entity);
2023-10-26 04:57:44 +02:00
if($column == 'status') {
2023-07-13 07:23:57 +02:00
return $entity->stringStatus($entity->status_id);
2023-10-26 04:57:44 +02:00
}
2023-07-13 07:23:57 +02:00
return '';
}
2023-07-16 12:17:43 +02:00
private function resolveQuoteKey($column, $entity, $transformer)
{
2023-07-27 03:25:05 +02:00
// nlog("searching for {$column}");
2023-07-16 12:17:43 +02:00
$transformed_entity = $transformer->transform($entity);
if(array_key_exists($column, $transformed_entity)) {
return $transformed_entity[$column];
}
return '';
}
2023-07-05 04:56:37 +02:00
private function resolveInvoiceKey($column, $entity, $transformer)
{
2023-07-27 03:25:05 +02:00
// nlog("searching for {$column}");
2023-07-24 13:39:30 +02:00
$transformed_invoice = false;
2023-07-05 08:56:07 +02:00
2023-09-12 09:23:52 +02:00
if($transformer instanceof PaymentTransformer && ($entity->invoices ?? false)) {
2023-07-05 04:56:37 +02:00
$transformed_invoices = $transformer->includeInvoices($entity);
2023-07-05 08:33:03 +02:00
2023-07-05 08:56:07 +02:00
$manager = new Manager();
$manager->setSerializer(new ArraySerializer());
$transformed_invoices = $manager->createData($transformed_invoices)->toArray();
2023-10-26 04:57:44 +02:00
if(!isset($transformed_invoices['App\\Models\\Invoice'])) {
return '';
2023-10-26 04:57:44 +02:00
}
$transformed_invoices = $transformed_invoices['App\\Models\\Invoice'];
2023-10-26 04:57:44 +02:00
if(count($transformed_invoices) == 1 && array_key_exists($column, $transformed_invoices[0])) {
return $transformed_invoices[0][$column];
2023-10-26 04:57:44 +02:00
}
2023-10-26 04:57:44 +02:00
if(count($transformed_invoices) > 1 && array_key_exists($column, $transformed_invoices[0])) {
return implode(', ', array_column($transformed_invoices, $column));
2023-10-26 04:57:44 +02:00
}
return "";
2023-07-05 08:56:07 +02:00
}
2023-07-05 08:33:03 +02:00
2023-09-12 05:55:37 +02:00
if($transformer instanceof TaskTransformer && ($entity->invoice ?? false)) {
2023-07-18 10:21:12 +02:00
$transformed_invoice = $transformer->includeInvoice($entity);
2023-10-26 04:57:44 +02:00
if(!$transformed_invoice) {
2023-07-18 10:21:12 +02:00
return '';
2023-10-26 04:57:44 +02:00
}
2023-07-18 10:21:12 +02:00
$manager = new Manager();
$manager->setSerializer(new ArraySerializer());
$transformed_invoice = $manager->createData($transformed_invoice)->toArray();
}
2023-07-24 13:39:30 +02:00
if($transformed_invoice && array_key_exists($column, $transformed_invoice)) {
2023-07-18 10:21:12 +02:00
return $transformed_invoice[$column];
2023-07-24 13:39:30 +02:00
} elseif ($transformed_invoice && array_key_exists(str_replace("invoice.", "", $column), $transformed_invoice)) {
2023-07-18 10:21:12 +02:00
return $transformed_invoice[$column];
}
2023-07-05 08:56:07 +02:00
return '';
2023-07-05 04:56:37 +02:00
}
private function resolvePaymentKey($column, $entity, $transformer)
{
2023-07-12 02:24:19 +02:00
2023-10-26 04:57:44 +02:00
if($entity instanceof Payment) {
2023-07-05 08:33:03 +02:00
$transformed_payment = $transformer->transform($entity);
if(array_key_exists($column, $transformed_payment)) {
return $transformed_payment[$column];
} elseif (array_key_exists(str_replace("payment.", "", $column), $transformed_payment)) {
return $transformed_payment[$column];
2023-11-26 08:41:42 +01:00
}
2023-07-05 08:33:03 +02:00
2023-07-27 03:25:05 +02:00
// nlog("export: Could not resolve payment key: {$column}");
2023-07-05 08:33:03 +02:00
return '';
}
2023-10-26 04:57:44 +02:00
if($column == 'amount') {
return $entity->payments()->exists() ? $entity->payments()->withoutTrashed()->sum('paymentables.amount') : ctrans('texts.unpaid');
2023-10-26 04:57:44 +02:00
}
2023-07-05 04:56:37 +02:00
if($column == 'refunded') {
return $entity->payments()->exists() ? $entity->payments()->withoutTrashed()->sum('paymentables.refunded') : '';
2023-07-05 04:56:37 +02:00
}
if($column == 'applied') {
$refunded = $entity->payments()->withoutTrashed()->sum('paymentables.refunded');
$amount = $entity->payments()->withoutTrashed()->sum('paymentables.amount');
2023-07-05 04:56:37 +02:00
return $entity->payments()->withoutTrashed()->exists() ? ($amount - $refunded) : '';
2023-07-05 04:56:37 +02:00
}
$payment = $entity->payments()->withoutTrashed()->first();
2023-07-05 04:56:37 +02:00
2023-10-26 04:57:44 +02:00
if(!$payment) {
2023-07-05 04:56:37 +02:00
return '';
2023-10-26 04:57:44 +02:00
}
2023-07-05 04:56:37 +02:00
2023-10-26 04:57:44 +02:00
if($column == 'method') {
2023-07-05 08:07:36 +02:00
return $payment->translatedType();
2023-10-26 04:57:44 +02:00
}
2023-07-05 08:07:36 +02:00
2023-10-26 04:57:44 +02:00
if($column == 'currency') {
2023-07-05 08:07:36 +02:00
return $payment?->currency?->code ?? '';
2023-10-26 04:57:44 +02:00
}
2023-07-05 08:07:36 +02:00
2023-07-05 04:56:37 +02:00
$payment_transformer = new PaymentTransformer();
$transformed_payment = $payment_transformer->transform($payment);
2023-10-26 04:57:44 +02:00
if($column == 'status') {
2023-07-05 04:56:37 +02:00
return $payment->stringStatus($transformed_payment['status_id']);
}
2023-10-26 04:57:44 +02:00
if(array_key_exists($column, $transformed_payment)) {
2023-07-05 04:56:37 +02:00
return $transformed_payment[$column];
2023-10-26 04:57:44 +02:00
}
2023-07-05 04:56:37 +02:00
return '';
}
2023-10-30 00:24:36 +01:00
public function applyFilters(Builder $query): Builder
{
if(isset($this->input['product_key'])) {
$products = explode(",", $this->input['product_key']);
$query->where(function ($q) use ($products) {
foreach($products as $product) {
$q->orWhereJsonContains('line_items', ['product_key' => $product]);
}
});
}
return $query;
}
2023-04-24 06:55:56 +02:00
protected function addInvoiceStatusFilter($query, $status): Builder
{
$status_parameters = explode(',', $status);
2023-10-26 04:57:44 +02:00
if(in_array('all', $status_parameters)) {
2023-04-24 06:55:56 +02:00
return $query;
2023-10-26 04:57:44 +02:00
}
2023-04-24 06:55:56 +02:00
$query->where(function ($nested) use ($status_parameters) {
$invoice_filters = [];
if (in_array('draft', $status_parameters)) {
$invoice_filters[] = Invoice::STATUS_DRAFT;
}
if (in_array('sent', $status_parameters)) {
$invoice_filters[] = Invoice::STATUS_SENT;
}
if (in_array('paid', $status_parameters)) {
$invoice_filters[] = Invoice::STATUS_PAID;
}
if (in_array('unpaid', $status_parameters)) {
$invoice_filters[] = Invoice::STATUS_SENT;
$invoice_filters[] = Invoice::STATUS_PARTIAL;
}
if (count($invoice_filters) > 0) {
$nested->whereIn('status_id', $invoice_filters);
}
if (in_array('overdue', $status_parameters)) {
$nested->orWhereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('due_date', '<', Carbon::now())
->orWhere('partial_due_date', '<', Carbon::now());
}
2023-10-26 04:57:44 +02:00
if(in_array('viewed', $status_parameters)) {
2023-04-24 06:55:56 +02:00
2023-10-26 04:57:44 +02:00
$nested->whereHas('invitations', function ($q) {
2023-04-24 06:55:56 +02:00
$q->whereNotNull('viewed_date')->whereNotNull('deleted_at');
});
}
});
return $query;
}
2022-04-27 07:17:45 +02:00
protected function addDateRange($query)
2022-04-27 05:07:29 +02:00
{
2023-10-30 00:24:36 +01:00
$query = $this->applyFilters($query);
2022-04-27 05:07:29 +02:00
$date_range = $this->input['date_range'];
if (array_key_exists('date_key', $this->input) && strlen($this->input['date_key']) > 1) {
2022-04-27 05:07:29 +02:00
$this->date_key = $this->input['date_key'];
}
try {
$custom_start_date = Carbon::parse($this->input['start_date']);
$custom_end_date = Carbon::parse($this->input['end_date']);
} catch (\Exception $e) {
2022-04-27 05:07:29 +02:00
$custom_start_date = now()->startOfYear();
$custom_end_date = now();
}
2022-04-27 05:07:29 +02:00
switch ($date_range) {
case 'all':
2023-03-01 12:59:24 +01:00
$this->start_date = 'All available data';
$this->end_date = 'All available data';
2022-04-27 05:07:29 +02:00
return $query;
case 'last7':
2023-03-01 12:59:24 +01:00
$this->start_date = now()->subDays(7)->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->subDays(7), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'last30':
2023-03-01 12:59:24 +01:00
$this->start_date = now()->subDays(30)->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->subDays(30), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'this_month':
2023-03-01 12:59:24 +01:00
$this->start_date = now()->startOfMonth()->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->startOfMonth(), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'last_month':
2023-03-01 12:59:24 +01:00
$this->start_date = now()->startOfMonth()->subMonth()->format('Y-m-d');
$this->end_date = now()->startOfMonth()->subMonth()->endOfMonth()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->startOfMonth()->subMonth(), now()->startOfMonth()->subMonth()->endOfMonth()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'this_quarter':
2023-03-01 12:59:24 +01:00
$this->start_date = (new \Carbon\Carbon('-3 months'))->firstOfQuarter()->format('Y-m-d');
$this->end_date = (new \Carbon\Carbon('-3 months'))->lastOfQuarter()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [(new \Carbon\Carbon('-3 months'))->firstOfQuarter(), (new \Carbon\Carbon('-3 months'))->lastOfQuarter()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'last_quarter':
2023-03-01 12:59:24 +01:00
$this->start_date = (new \Carbon\Carbon('-6 months'))->firstOfQuarter()->format('Y-m-d');
$this->end_date = (new \Carbon\Carbon('-6 months'))->lastOfQuarter()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [(new \Carbon\Carbon('-6 months'))->firstOfQuarter(), (new \Carbon\Carbon('-6 months'))->lastOfQuarter()])->orderBy($this->date_key, 'ASC');
2023-04-14 03:18:50 +02:00
case 'last365_days':
$this->start_date = now()->startOfDay()->subDays(365)->format('Y-m-d');
$this->end_date = now()->startOfDay()->format('Y-m-d');
return $query->whereBetween($this->date_key, [now()->subDays(365), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'this_year':
$first_month_of_year = $this->company->getSetting('first_month_of_year') ?? 1;
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
2023-10-26 04:57:44 +02:00
if(now()->lt($fin_year_start)) {
$fin_year_start->subYearNoOverflow();
2023-10-26 04:57:44 +02:00
}
2023-07-17 10:43:56 +02:00
$this->start_date = $fin_year_start->format('Y-m-d');
$this->end_date = $fin_year_start->copy()->addYear()->subDay()->format('Y-m-d');
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC');
case 'last_year':
$first_month_of_year = $this->company->getSetting('first_month_of_year') ?? 1;
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
$fin_year_start->subYearNoOverflow();
2023-10-26 04:57:44 +02:00
if(now()->subYear()->lt($fin_year_start)) {
2023-07-17 10:43:56 +02:00
$fin_year_start->subYearNoOverflow();
2023-10-26 04:57:44 +02:00
}
2023-07-17 10:43:56 +02:00
$this->start_date = $fin_year_start->format('Y-m-d');
$this->end_date = $fin_year_start->copy()->addYear()->subDay()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'custom':
2023-03-01 12:59:24 +01:00
$this->start_date = $custom_start_date->format('Y-m-d');
$this->end_date = $custom_end_date->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [$custom_start_date, $custom_end_date])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
default:
2023-03-01 12:59:24 +01:00
$this->start_date = now()->startOfYear()->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
}
}
2023-08-28 14:44:04 +02:00
/**
2023-10-26 04:57:44 +02:00
* Returns the merged array of
2023-08-28 14:44:04 +02:00
* the entity with the matching
* item report keys
*
* @param string $entity_report_keys
* @return array
*/
public function mergeItemsKeys(string $entity_report_keys): array
{
return array_merge($this->{$entity_report_keys}, $this->item_report_keys);
}
2022-04-27 05:07:29 +02:00
2023-04-14 05:04:28 +02:00
public function buildHeader() :array
2022-04-27 10:05:54 +02:00
{
2023-08-24 05:22:35 +02:00
$helper = new Helpers();
2022-04-27 10:05:54 +02:00
$header = [];
2023-09-14 07:34:02 +02:00
// nlog("header");
2023-08-24 05:22:35 +02:00
foreach ($this->input['report_keys'] as $value) {
2023-08-29 11:15:36 +02:00
$key = array_search($value, $this->entity_keys);
2023-08-24 09:09:39 +02:00
$original_key = $key;
2023-08-29 11:15:36 +02:00
2023-09-14 07:34:02 +02:00
// nlog("{$key} => {$value}");
2023-07-05 13:28:15 +02:00
$prefix = '';
2022-05-10 05:39:08 +02:00
2023-07-05 04:56:37 +02:00
if(!$key) {
2023-07-15 02:06:00 +02:00
$prefix = stripos($value, 'client.') !== false ? ctrans('texts.client')." " : ctrans('texts.contact')." ";
2023-07-05 04:56:37 +02:00
$key = array_search($value, $this->client_report_keys);
}
if(!$key) {
2023-07-15 02:06:00 +02:00
$prefix = ctrans('texts.invoice')." ";
2023-07-05 04:56:37 +02:00
$key = array_search($value, $this->invoice_report_keys);
}
2023-07-18 07:11:45 +02:00
if(!$key) {
$prefix = ctrans('texts.recurring_invoice')." ";
$key = array_search($value, $this->recurring_invoice_report_keys);
}
2023-07-05 08:56:07 +02:00
if(!$key) {
2023-07-15 02:06:00 +02:00
$prefix = ctrans('texts.payment')." ";
2023-07-05 08:56:07 +02:00
$key = array_search($value, $this->payment_report_keys);
}
2023-07-05 04:56:37 +02:00
if(!$key) {
2023-07-15 02:06:00 +02:00
$prefix = ctrans('texts.quote')." ";
2023-07-05 04:56:37 +02:00
$key = array_search($value, $this->quote_report_keys);
}
if(!$key) {
2023-07-15 02:06:00 +02:00
$prefix = ctrans('texts.credit')." ";
2023-07-05 04:56:37 +02:00
$key = array_search($value, $this->credit_report_keys);
}
if(!$key) {
2023-07-15 02:06:00 +02:00
$prefix = ctrans('texts.item')." ";
2023-07-05 08:56:07 +02:00
$key = array_search($value, $this->item_report_keys);
}
2023-07-13 13:10:54 +02:00
if(!$key) {
2023-07-15 02:06:00 +02:00
$prefix = ctrans('texts.expense')." ";
2023-07-13 13:10:54 +02:00
$key = array_search($value, $this->expense_report_keys);
2023-09-14 06:44:44 +02:00
2023-10-26 04:57:44 +02:00
if(!$key && $value == 'expense.category') {
$key = 'category';
}
2023-07-13 13:10:54 +02:00
}
if(!$key) {
2023-07-15 02:06:00 +02:00
$prefix = ctrans('texts.task')." ";
2023-07-13 13:10:54 +02:00
$key = array_search($value, $this->task_report_keys);
}
if(!$key) {
2023-07-15 02:06:00 +02:00
$prefix = ctrans('texts.vendor')." ";
2023-07-13 13:10:54 +02:00
$key = array_search($value, $this->vendor_report_keys);
}
if(!$key) {
2023-07-15 02:06:00 +02:00
$prefix = ctrans('texts.purchase_order')." ";
2023-07-13 13:10:54 +02:00
$key = array_search($value, $this->purchase_order_report_keys);
}
2023-08-29 11:15:36 +02:00
if(!$key) {
$prefix = '';
$key = array_search($value, $this->product_report_keys);
}
2023-07-05 08:56:07 +02:00
if(!$key) {
$prefix = '';
2023-07-05 04:56:37 +02:00
}
2023-09-14 07:34:02 +02:00
// nlog("key => {$key}");
2023-09-14 06:44:44 +02:00
$key = str_replace('item.', '', $key);
2023-07-13 07:23:57 +02:00
$key = str_replace('recurring_invoice.', '', $key);
2023-07-18 06:39:04 +02:00
$key = str_replace('purchase_order.', '', $key);
$key = str_replace('invoice.', '', $key);
2023-07-13 07:23:57 +02:00
$key = str_replace('quote.', '', $key);
$key = str_replace('credit.', '', $key);
$key = str_replace('task.', '', $key);
$key = str_replace('client.', '', $key);
2023-07-13 07:23:57 +02:00
$key = str_replace('vendor.', '', $key);
$key = str_replace('contact.', '', $key);
2023-07-05 04:56:37 +02:00
$key = str_replace('payment.', '', $key);
2023-07-13 13:10:54 +02:00
$key = str_replace('expense.', '', $key);
2023-08-29 11:15:36 +02:00
$key = str_replace('product.', '', $key);
2023-09-12 03:55:11 +02:00
$key = str_replace('task.', '', $key);
2023-08-24 05:22:35 +02:00
2023-10-26 04:57:44 +02:00
if(stripos($value, 'custom_value') !== false) {
2023-08-24 05:22:35 +02:00
$parts = explode(".", $value);
2023-10-26 04:57:44 +02:00
if(count($parts) == 2 && in_array($parts[0], ['credit','quote','invoice','purchase_order','recurring_invoice'])) {
2023-08-24 05:22:35 +02:00
$entity = "invoice".substr($parts[1], -1);
2023-08-24 09:09:39 +02:00
$prefix = ctrans("texts.".$parts[0]);
$fallback = "custom_value".substr($parts[1], -1);
$custom_field_label = $helper->makeCustomField($this->company->custom_fields, $entity);
2023-10-26 04:57:44 +02:00
if(strlen($custom_field_label) > 1) {
2023-08-24 09:09:39 +02:00
$header[] = $custom_field_label;
2023-10-26 04:57:44 +02:00
} else {
2023-08-24 09:09:39 +02:00
$header[] = $prefix . " ". ctrans("texts.{$fallback}");
}
2023-10-26 04:57:44 +02:00
} elseif(count($parts) == 2 && (stripos($parts[0], 'vendor_contact') !== false || stripos($parts[0], 'contact') !== false)) {
2023-09-14 08:00:41 +02:00
$parts[0] = str_replace('vendor_contact', 'contact', $parts[0]);
2023-08-24 05:51:48 +02:00
$entity = "contact".substr($parts[1], -1);
$custom_field_string = strlen($helper->makeCustomField($this->company->custom_fields, $entity)) > 1 ? $helper->makeCustomField($this->company->custom_fields, $entity) : ctrans("texts.{$parts[1]}");
$header[] = ctrans("texts.{$parts[0]}") . " " . $custom_field_string;
2023-09-14 08:00:41 +02:00
2023-10-26 04:57:44 +02:00
} elseif(count($parts) == 2 && in_array(substr($original_key, 0, -1), ['credit','quote','invoice','purchase_order','recurring_invoice','task'])) {
$custom_field_string = strlen($helper->makeCustomField($this->company->custom_fields, "product".substr($original_key, -1))) > 1 ? $helper->makeCustomField($this->company->custom_fields, "product".substr($original_key, -1)) : ctrans("texts.{$parts[1]}");
2023-08-24 09:09:39 +02:00
$header[] = ctrans("texts.{$parts[0]}") . " " . $custom_field_string;
2023-10-26 04:57:44 +02:00
} else {
2023-08-24 05:51:48 +02:00
$header[] = "{$prefix}" . ctrans("texts.{$key}");
}
2023-10-26 04:57:44 +02:00
} else {
2023-07-18 06:26:52 +02:00
$header[] = "{$prefix}" . ctrans("texts.{$key}");
}
2022-04-27 10:05:54 +02:00
}
2023-08-24 12:41:06 +02:00
2023-09-14 07:34:02 +02:00
// nlog($header);
2023-08-24 12:41:06 +02:00
2022-04-27 10:05:54 +02:00
return $header;
}
2023-09-11 12:33:56 +02:00
public function processMetaData(array $row, $resource): array
{
$class = get_class($resource);
2023-09-12 03:01:14 +02:00
$entity = '';
2023-09-12 03:22:59 +02:00
2023-09-11 12:33:56 +02:00
match ($class) {
Invoice::class => $entity = 'invoice',
2023-09-12 03:22:59 +02:00
RecurringInvoice::class => $entity = 'recurring_invoice',
Quote::class => $entity = 'quote',
Credit::class => $entity = 'credit',
2023-09-11 12:33:56 +02:00
Expense::class => $entity = 'expense',
2023-09-12 03:01:14 +02:00
Document::class => $entity = 'document',
2023-09-11 12:33:56 +02:00
ClientContact::class => $entity = 'contact',
2023-09-12 03:01:14 +02:00
PurchaseOrder::class => $entity = 'purchase_order',
2023-09-12 03:55:11 +02:00
Payment::class => $entity = 'payment',
Product::class => $entity = 'product',
Task::class => $entity = 'task',
2023-09-12 05:55:37 +02:00
Vendor::class => $entity = 'vendor',
2023-09-11 12:33:56 +02:00
default => $entity = 'invoice',
};
$clean_row = [];
foreach (array_values($this->input['report_keys']) as $key => $value) {
$report_keys = explode(".", $value);
$column_key = $value;
2023-09-14 08:00:41 +02:00
2023-09-12 03:55:11 +02:00
if($value == 'product_image') {
$column_key = 'image';
$value = 'image';
}
2023-10-26 04:57:44 +02:00
if($value == 'tax_id') {
2023-09-12 03:55:11 +02:00
$column_key = 'tax_category';
$value = 'tax_category';
}
2023-09-11 12:33:56 +02:00
$clean_row[$key]['entity'] = $report_keys[0];
$clean_row[$key]['id'] = $report_keys[1] ?? $report_keys[0];
$clean_row[$key]['hashed_id'] = $report_keys[0] == $entity ? null : $resource->{$report_keys[0]}->hashed_id ?? null;
$clean_row[$key]['value'] = $row[$column_key];
$clean_row[$key]['identifier'] = $value;
$clean_row[$key]['display_value'] = $row[$column_key];
}
return $clean_row;
2023-10-26 04:57:44 +02:00
}
2023-09-11 12:33:56 +02:00
2023-09-12 03:22:59 +02:00
public function processItemMetaData(array $row, $resource): array
{
$class = get_class($resource);
$entity = '';
match ($class) {
Invoice::class => $entity = 'invoice',
Quote::class => $entity = 'quote',
Credit::class => $entity = 'credit',
Expense::class => $entity = 'expense',
Document::class => $entity = 'document',
ClientContact::class => $entity = 'contact',
PurchaseOrder::class => $entity = 'purchase_order',
default => $entity = 'invoice',
};
$clean_row = [];
foreach (array_values($this->input['report_keys']) as $key => $value) {
$report_keys = explode(".", $value);
$column_key = $value;
2023-10-26 04:57:44 +02:00
if($value == 'type_id' || $value == 'item.type_id') {
2023-09-12 03:22:59 +02:00
$column_key = 'type';
2023-10-26 04:57:44 +02:00
}
2023-09-12 03:22:59 +02:00
2023-10-26 04:57:44 +02:00
if($value == 'tax_id' || $value == 'item.tax_id') {
2023-09-12 03:22:59 +02:00
$column_key = 'tax_category';
2023-10-26 04:57:44 +02:00
}
2023-09-12 03:22:59 +02:00
$clean_row[$key]['entity'] = $report_keys[0];
$clean_row[$key]['id'] = $report_keys[1] ?? $report_keys[0];
$clean_row[$key]['hashed_id'] = $report_keys[0] == $entity ? null : $resource->{$report_keys[0]}->hashed_id ?? null;
2023-10-20 01:34:17 +02:00
$clean_row[$key]['value'] = isset($row[$column_key]) ? $row[$column_key] : $row[$value];
2023-09-12 03:22:59 +02:00
$clean_row[$key]['identifier'] = $value;
2023-10-20 01:34:17 +02:00
$clean_row[$key]['display_value'] = isset($row[$column_key]) ? $row[$column_key] : $row[$value];
2023-09-12 03:22:59 +02:00
}
return $clean_row;
2023-10-26 04:57:44 +02:00
}
2023-09-12 03:22:59 +02:00
}