diff --git a/app/Export/CSV/ExpenseExport.php b/app/Export/CSV/ExpenseExport.php new file mode 100644 index 000000000000..4249a685839d --- /dev/null +++ b/app/Export/CSV/ExpenseExport.php @@ -0,0 +1,164 @@ + 'amount', + 'category' => 'category_id', + 'client' => 'client_id', + 'custom_value1' => 'custom_value1', + 'custom_value2' => 'custom_value2', + 'custom_value3' => 'custom_value3', + 'custom_value4' => 'custom_value4', + 'currency' => 'currency_id', + 'date' => 'date', + 'exchange_rate' => 'exchange_rate', + 'converted_amount' => 'foreign_amount', + 'invoice_currency_id' => 'invoice_currency_id', + 'payment_date' => 'payment_date', + 'number' => 'number', + 'payment_type_id' => 'payment_type_id', + 'private_notes' => 'private_notes', + 'project' => 'project_id', + 'public_notes' => 'public_notes', + 'tax_amount1' => 'tax_amount1', + 'tax_amount2' => 'tax_amount2', + 'tax_amount3' => 'tax_amount3', + 'tax_name1' => 'tax_name1', + 'tax_name2' => 'tax_name2', + 'tax_name3' => 'tax_name3', + 'tax_rate1' => 'tax_rate1', + 'tax_rate2' => 'tax_rate2', + 'tax_rate3' => 'tax_rate3', + 'transaction_reference' => 'transaction_reference', + 'vendor' => 'vendor_id', + 'invoice' => 'invoice_id', + ]; + + private array $decorate_keys = [ + 'client', + 'currency', + 'invoice', + 'category', + 'vendor', + 'project', + 'payment_type_id', + ]; + + public function __construct(Company $company, array $report_keys) + { + $this->company = $company; + $this->report_keys = $report_keys; + $this->expense_transformer = new ExpenseTransformer(); + } + + public function run() + { + + MultiDB::setDb($this->company->db); + App::forgetInstance('translator'); + App::setLocale($this->company->locale()); + $t = app('translator'); + $t->replace(Ninja::transformTranslations($this->company->settings)); + + //load the CSV document from a string + $this->csv = Writer::createFromString(); + + //insert the header + $this->csv->insertOne($this->buildHeader()); + + Expense::with('client')->where('company_id', $this->company->id) + ->where('is_deleted',0) + ->cursor() + ->each(function ($expense){ + + $this->csv->insertOne($this->buildRow($expense)); + + }); + + + return $this->csv->toString(); + + } + + private function buildHeader() :array + { + + $header = []; + + foreach(array_keys($this->report_keys) as $key) + $header[] = ctrans("texts.{$key}"); + + return $header; + } + + private function buildRow(Expense $expense) :array + { + + $transformed_expense = $this->expense_transformer->transform($expense); + + $entity = []; + + foreach(array_values($this->report_keys) as $key){ + + $entity[$key] = $transformed_expense[$key]; + } + + return $this->decorateAdvancedFields($expense, $entity); + + } + + private function decorateAdvancedFields(Expense $expense, array $entity) :array + { + if(array_key_exists('currency_id', $entity)) + $entity['currency_id'] = $expense->currency ? $expense->currency->code : ""; + + if(array_key_exists('client_id', $entity)) + $entity['client_id'] = $expense->client ? $expense->client->present()->name() : ""; + + if(array_key_exists('invoice_id', $entity)) + $entity['invoice_id'] = $expense->invoice ? $expense->invoice->number : ""; + + if(array_key_exists('category_id', $entity)) + $entity['category_id'] = $expense->category ? $expense->category->name : ""; + + if(array_key_exists('vendor_id', $entity)) + $entity['vendor_id'] = $expense->vendor ? $expense->vendor->name : ""; + + if(array_key_exists('payment_type_id', $entity)) + $entity['payment_type_id'] = $expense->payment_type ? $expense->payment_type->name : ""; + + if(array_key_exists('project_id', $entity)) + $entity['project_id'] = $expense->project ? $expense->project->name : ""; + + + return $entity; + } + +} diff --git a/app/Models/Expense.php b/app/Models/Expense.php index 27f1273113d1..c730654e680a 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -106,4 +106,24 @@ class Expense extends BaseModel { return ctrans('texts.expense'); } + + public function currency() + { + return $this->belongsTo(Currency::class); + } + + public function category() + { + return $this->belongsTo(ExpenseCategory::class); + } + + public function payment_type() + { + return $this->belongsTo(PaymentType::class); + } + + public function project() + { + return $this->belongsTo(Project::class); + } }