diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index e4c743923c4c..f829d0931291 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -13,6 +13,7 @@ namespace App\Export\CSV; use App\Utils\Number; use App\Models\Client; +use App\Utils\Helpers; use App\Models\Company; use App\Models\Expense; use App\Models\Invoice; @@ -28,7 +29,7 @@ use League\Fractal\Serializer\ArraySerializer; class BaseExport { use MakesHash; - + public Company $company; public array $input; @@ -43,8 +44,6 @@ class BaseExport public string $client_description = 'All Clients'; - public array $forced_keys = []; - protected array $vendor_report_keys = [ 'address1' => 'vendor.address1', 'address2' => 'vendor.address2', @@ -271,6 +270,7 @@ class BaseExport "date" => "credit.date", "due_date" => "credit.due_date", "terms" => "credit.terms", + "discount" => "credit.discount", "footer" => "credit.footer", "status" => "credit.status", "public_notes" => "credit.public_notes", @@ -283,6 +283,10 @@ class BaseExport "surcharge2" => "credit.custom_surcharge2", "surcharge3" => "credit.custom_surcharge3", "surcharge4" => "credit.custom_surcharge4", + "custom_value1" => "credit.custom_value1", + "custom_value2" => "credit.custom_value2", + "custom_value3" => "credit.custom_value3", + "custom_value4" => "credit.custom_value4", "exchange_rate" => "credit.exchange_rate", "tax_amount" => "credit.total_taxes", "assigned_user" => "credit.assigned_user_id", @@ -832,11 +836,13 @@ class BaseExport public function buildHeader() :array { + $helper = new Helpers(); + $header = []; // nlog($this->input['report_keys']); - foreach (array_merge($this->input['report_keys'], $this->forced_keys) as $value) { + foreach ($this->input['report_keys'] as $value) { $key = array_search($value, $this->entity_keys); nlog("{$key} => {$value}"); @@ -913,11 +919,20 @@ class BaseExport $key = str_replace('contact.', '', $key); $key = str_replace('payment.', '', $key); $key = str_replace('expense.', '', $key); -// nlog($key); - if(in_array($key, ['quote1','quote2','quote3','quote4','credit1','credit2','credit3','credit4','purchase_order1','purchase_order2','purchase_order3','purchase_order4'])) + + // if( in_array($key, ['quote1','quote2','quote3','quote4','credit1','credit2','credit3','credit4','purchase_order1','purchase_order2','purchase_order3','purchase_order4'])) + if(stripos($value, 'custom_value') !== false) { - $number = substr($key, -1); - $header[] = ctrans('texts.item') . " ". ctrans("texts.custom_value{$number}"); + $parts = explode(".", $value); + + if(in_array($parts[0], ['credit','quote','invoice','purchase_order','recurring_invoice'])){ + $entity = "invoice".substr($parts[1], -1); + } + else { + $entity = $parts[0].substr($parts[1], -1); + } + + $header[] = $helper->makeCustomField($this->company->custom_fields, $entity); } else { @@ -925,8 +940,6 @@ class BaseExport } } - // nlog($header); - return $header; } } diff --git a/app/Export/CSV/ClientExport.php b/app/Export/CSV/ClientExport.php index 6e4dded88159..0335be61c467 100644 --- a/app/Export/CSV/ClientExport.php +++ b/app/Export/CSV/ClientExport.php @@ -79,9 +79,6 @@ class ClientExport extends BaseExport 'client.industry', ]; - public array $forced_keys = [ - ]; - public function __construct(Company $company, array $input) { $this->company = $company; diff --git a/app/Export/CSV/CreditExport.php b/app/Export/CSV/CreditExport.php index 2cf25cb56899..ffc979513949 100644 --- a/app/Export/CSV/CreditExport.php +++ b/app/Export/CSV/CreditExport.php @@ -30,51 +30,6 @@ class CreditExport extends BaseExport public Writer $csv; - public array $entity_keys = [ - 'amount' => 'amount', - 'balance' => 'balance', - 'client' => 'client_id', - 'country' => 'country_id', - 'custom_surcharge1' => 'custom_surcharge1', - 'custom_surcharge2' => 'custom_surcharge2', - 'custom_surcharge3' => 'custom_surcharge3', - 'custom_surcharge4' => 'custom_surcharge4', - 'currency' => 'currency', - 'custom_value1' => 'custom_value1', - 'custom_value2' => 'custom_value2', - 'custom_value3' => 'custom_value3', - 'custom_value4' => 'custom_value4', - 'date' => 'date', - 'discount' => 'discount', - 'due_date' => 'due_date', - 'exchange_rate' => 'exchange_rate', - 'footer' => 'footer', - 'invoice' => 'invoice_id', - 'number' => 'number', - 'paid_to_date' => 'paid_to_date', - 'partial' => 'partial', - 'partial_due_date' => 'partial_due_date', - 'po_number' => 'po_number', - 'private_notes' => 'private_notes', - 'public_notes' => 'public_notes', - 'status' => 'status_id', - 'tax_name1' => 'tax_name1', - 'tax_name2' => 'tax_name2', - 'tax_name3' => 'tax_name3', - 'tax_rate1' => 'tax_rate1', - 'tax_rate2' => 'tax_rate2', - 'tax_rate3' => 'tax_rate3', - 'terms' => 'terms', - 'total_taxes' => 'total_taxes', - ]; - - private array $decorate_keys = [ - 'country', - 'client', - 'invoice', - 'currency', - ]; - public function __construct(Company $company, array $input) { $this->company = $company; @@ -86,13 +41,13 @@ class CreditExport extends BaseExport { $query = $this->init(); - $headerdisplay = $this->buildHeader(); $header = []; + $headerdisplay = $this->buildHeader(); foreach ($this->input['report_keys'] as $key => $value) { $header[] = ['identifier' => $value, 'display_value' => $headerdisplay[$key]]; } - + $report = $query->cursor() ->map(function ($credit) { $row = $this->buildRow($credit); @@ -105,21 +60,18 @@ class CreditExport extends BaseExport private function processMetaData(array $row, Credit $credit): array { $clean_row = []; - - foreach ($this->input['report_keys'] as $key => $value) { - + foreach (array_values($this->input['report_keys']) as $key => $value) { + $report_keys = explode(".", $value); - $column_key = str_replace("credit.", "", $value); - $column_key = array_search($column_key, $this->entity_keys); - + $column_key = $value; $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] == 'credit' ? null : $credit->{$report_keys[0]}->hashed_id ?? null; $clean_row[$key]['value'] = $row[$column_key]; $clean_row[$key]['identifier'] = $value; - if(in_array($clean_row[$key]['id'], ['amount', 'balance', 'partial', 'refunded', 'applied','unit_cost','cost','price'])) + if(in_array($clean_row[$key]['id'], ['paid_to_date','total_taxes','amount', 'balance', 'partial', 'refunded', 'applied','unit_cost','cost','price'])) $clean_row[$key]['display_value'] = Number::formatMoney($row[$column_key], $credit->client); else $clean_row[$key]['display_value'] = $row[$column_key]; @@ -139,12 +91,13 @@ class CreditExport extends BaseExport $t->replace(Ninja::transformTranslations($this->company->settings)); if (count($this->input['report_keys']) == 0) { - $this->input['report_keys'] = array_values($this->entity_keys); + $this->input['report_keys'] = array_values($this->credit_report_keys); } $query = Credit::query() ->withTrashed() - ->with('client')->where('company_id', $this->company->id) + ->with('client') + ->where('company_id', $this->company->id) ->where('is_deleted', 0); $query = $this->addDateRange($query); @@ -160,11 +113,9 @@ class CreditExport extends BaseExport //insert the header $this->csv->insertOne($this->buildHeader()); - // nlog($this->input['report_keys']); $query->cursor() ->each(function ($credit) { - // nlog($this->buildRow($credit)); $this->csv->insertOne($this->buildRow($credit)); }); @@ -178,22 +129,22 @@ class CreditExport extends BaseExport $entity = []; foreach (array_values($this->input['report_keys']) as $key) { - $keyval = array_search($key, $this->entity_keys); - - if(!$keyval) - $keyval = array_search(str_replace("credit.", "", $key), $this->entity_keys) ?? $key; - - if(!$keyval) - $keyval = $key; + + $keyval = $key; + $credit_key = str_replace("credit.", "", $key); + $searched_credit_key = array_search(str_replace("credit.", "", $key), $this->credit_report_keys) ?? $key; - if (array_key_exists($key, $transformed_credit)) { - $entity[$keyval] = $transformed_credit[$key]; - } elseif (array_key_exists($keyval, $transformed_credit)) { + if (isset($transformed_credit[$credit_key])) { + $entity[$keyval] = $transformed_credit[$credit_key]; + } elseif (isset($transformed_credit[$keyval])) { $entity[$keyval] = $transformed_credit[$keyval]; + } elseif(isset($transformed_credit[$searched_credit_key])){ + $entity[$keyval] = $transformed_credit[$searched_credit_key]; } else { $entity[$keyval] = $this->resolveKey($keyval, $credit, $this->credit_transformer); } + } return $this->decorateAdvancedFields($credit, $entity); diff --git a/app/Export/CSV/VendorExport.php b/app/Export/CSV/VendorExport.php index efc8bf32134e..011484b054a3 100644 --- a/app/Export/CSV/VendorExport.php +++ b/app/Export/CSV/VendorExport.php @@ -67,10 +67,6 @@ class VendorExport extends BaseExport 'vendor.currency', ]; - public array $forced_keys = [ - // 'vendor.status' - ]; - public function __construct(Company $company, array $input) { $this->company = $company;