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'); $this->csv->insertOne([]); $this->csv->insertOne([]); $this->csv->insertOne([]); $this->csv->insertOne([]); $this->csv->insertOne([ctrans('texts.aged_receivable_detailed_report')]); $this->csv->insertOne([ctrans('texts.created_on'),' ',$this->translateDate(now()->format('Y-m-d'), $this->company->date_format(), $this->company->locale())]); if (count($this->input['report_keys']) == 0) { $this->input['report_keys'] = $this->report_keys; } $this->csv->insertOne($this->buildHeader()); $query = Invoice::query() ->whereIn('invoices.status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) ->withTrashed() ->whereHas('client', function ($query) { $query->where('is_deleted', 0); }) ->where('invoices.company_id', $this->company->id) ->where('invoices.is_deleted', 0) ->where('invoices.balance', '>', 0) ->orderBy('invoices.due_date', 'ASC'); $query = $this->addDateRange($query, 'invoices'); $query = $this->filterByClients($query); $query->cursor() ->each(function ($invoice) { $this->csv->insertOne($this->buildRow($invoice)); }); return $this->csv->toString(); } private function buildRow(Invoice $invoice): array { $client = $invoice->client; return [ $this->translateDate($invoice->date, $this->company->date_format(), $this->company->locale()), $this->translateDate($invoice->due_date, $this->company->date_format(), $this->company->locale()), $invoice->number, $invoice->stringStatus($invoice->status_id), $client->present()->name(), $client->number, $client->id_number, intval(abs(Carbon::parse($invoice->due_date)->diffInDays(now()))), Number::formatMoney($invoice->amount, $this->company), Number::formatMoney($invoice->balance, $this->company), ]; } public function buildHeader(): array { $header = []; foreach ($this->input['report_keys'] as $value) { $header[] = ctrans("texts.{$value}"); } return $header; } }