company->db); App::forgetInstance('translator'); App::setLocale($this->company->locale()); $t = app('translator'); $t->replace(Ninja::transformTranslations($this->company->settings)); $this->csv = Writer::createFromString(); \League\Csv\CharsetConverter::addTo($this->csv, 'UTF-8', 'UTF-8'); $query = Invoice::query() ->withTrashed() ->where('company_id', $this->company->id) ->where('is_deleted', 0) ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID]); $query = $this->addDateRange($query, 'invoices'); $query = $this->filterByClients($query); $this->csv->insertOne([ctrans('texts.user_sales_report_header', ['client' => $this->client_description, 'start_date' => $this->start_date, 'end_date' => $this->end_date])]); if (count($this->input['report_keys']) == 0) { $this->input['report_keys'] = $this->report_keys; } $this->csv->insertOne($this->buildHeader()); $users = $this->company->users; $report = $users->map(function ($user) use ($query) { $new_query = $query; $new_query->where('user_id', $user->id); return [ $user->present()->name(), $new_query->count(), Number::formatMoney($new_query->sum('amount'), $this->company), Number::formatMoney($new_query->sum('total_taxes'), $this->company), ]; })->toArray(); $key_values = array_column($report, 1); array_multisort($key_values, SORT_DESC, $report); $this->csv->insertAll($report); return $this->csv->toString(); } public function buildHeader(): array { $header = []; foreach ($this->input['report_keys'] as $value) { $header[] = ctrans("texts.{$value}"); } return $header; } }