Count stock and correct the differences on confirmation
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Presentation\Http\Controllers;
|
||||
|
||||
use App\Modules\Inventory\Application\Contracts\InventoryCountQueries;
|
||||
use App\Modules\Inventory\Domain\Services\StockValuation;
|
||||
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
/**
|
||||
* A confirmed stock count as a CSV for the bookkeeping (spec 19.3): item,
|
||||
* unit, book balance, counted quantity, difference, average cost and the
|
||||
* values in cents. Semicolons and a UTF-8 BOM for Excel; quantities with a
|
||||
* decimal comma; text that looks like a formula gets an apostrophe.
|
||||
*/
|
||||
class DownloadInventoryCount
|
||||
{
|
||||
public function __invoke(int $count, InventoryCountQueries $counts): StreamedResponse
|
||||
{
|
||||
Gate::authorize('inventory.manage-counts');
|
||||
|
||||
$summary = $counts->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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user