findCount($count) ?? abort(404); abort_unless($summary->isConfirmed, 404); $lines = $counts->lines($count); return response()->streamDownload(function () use ($lines): void { $out = fopen('php://output', 'w'); if ($out === false) { return; } fwrite($out, "\u{FEFF}"); fputcsv($out, array_map(fn (string $key): string => __('inventory.counts.csv.'.$key), [ 'item', 'code', 'group', 'unit', 'book', 'counted', 'difference', 'unit_cost', 'book_value', 'counted_value', 'difference_value', ]), ';', '"', ''); foreach ($lines as $line) { if (! $line->isCounted()) { continue; } $book = (int) $line->bookMilli; $counted = (int) $line->countedMilli; fputcsv($out, [ self::text($line->itemName), self::text((string) $line->code), self::text($line->groupName), __('inventory.units.'.$line->unit->value), StockQuantity::inputValue($book), StockQuantity::inputValue($counted), StockQuantity::inputValue($counted - $book), number_format($line->unitCostHundredths / 10_000, 4, ',', ''), (string) StockValuation::valueCents($book, $line->unitCostHundredths), (string) StockValuation::valueCents($counted, $line->unitCostHundredths), (string) StockValuation::valueCents($counted - $book, $line->unitCostHundredths), ], ';', '"', ''); } fclose($out); }, __('inventory.counts.csv.file', ['date' => $summary->countedOn]).'.csv', [ 'Content-Type' => 'text/csv; charset=UTF-8', 'Cache-Control' => 'no-store, private', ]); } private static function text(string $value): string { return preg_match('/^[=+\-@\t\r\n]/', $value) === 1 ? "'".$value : $value; } }