Quick sales of a period by cash day, product, seller and VAT rate, with credits, open cash days flagged, a chart by day or month and CSV. VAT is taken out of each day's total of a rate, as on the cash day balance. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
97 lines
4.1 KiB
PHP
97 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Sales\Infrastructure\Persistence\Repositories;
|
|
|
|
use App\Modules\Sales\Application\Contracts\QuickSaleReportQueries;
|
|
use App\Modules\Sales\Application\DTOs\QuickSalesInPeriod;
|
|
use DateTimeImmutable;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Sums quick sales and their lines in the database.
|
|
*/
|
|
class EloquentQuickSaleReportQueries implements QuickSaleReportQueries
|
|
{
|
|
public function between(string $from, string $to): QuickSalesInPeriod
|
|
{
|
|
$until = (new DateTimeImmutable($to))->modify('+1 day')->format('Y-m-d');
|
|
$closedDays = DB::table('cash_days')
|
|
->whereNotNull('closed_at')
|
|
->where('day', '>=', $from)
|
|
->where('day', '<', $until)
|
|
->pluck('day')
|
|
->map(fn ($day): string => substr((string) $day, 0, 10))
|
|
->flip();
|
|
|
|
$days = DB::table('quick_sales')
|
|
->where('sold_on', '>=', $from)
|
|
->where('sold_on', '<', $until)
|
|
->groupBy('sold_on')
|
|
->orderBy('sold_on')
|
|
->select('sold_on')
|
|
->selectRaw('COUNT(*) as sale_count')
|
|
->selectRaw("SUM(CASE WHEN payment_method = 'cash' THEN total_cents ELSE 0 END) as cash_cents")
|
|
->selectRaw("SUM(CASE WHEN payment_method = 'cash' THEN 0 ELSE total_cents END) as card_cents")
|
|
->get()
|
|
->map(function (object $row) use ($closedDays): array {
|
|
$day = substr((string) $row->sold_on, 0, 10);
|
|
|
|
return [
|
|
'day' => $day,
|
|
'count' => (int) $row->sale_count,
|
|
'cashCents' => (int) $row->cash_cents,
|
|
'cardCents' => (int) $row->card_cents,
|
|
'isClosed' => $closedDays->has($day),
|
|
];
|
|
})
|
|
->all();
|
|
|
|
$products = $this->lines($from, $until)
|
|
->groupBy('quick_sale_lines.product_name')
|
|
->select('quick_sale_lines.product_name')
|
|
->selectRaw('SUM(quick_sale_lines.quantity) as quantity')
|
|
->selectRaw('SUM(quick_sale_lines.quantity * quick_sale_lines.unit_price_cents) as gross_cents')
|
|
->orderByDesc('gross_cents')
|
|
->orderBy('quick_sale_lines.product_name')
|
|
->get()
|
|
->map(fn (object $row): array => ['name' => (string) $row->product_name, 'quantity' => (int) $row->quantity, 'grossCents' => (int) $row->gross_cents])
|
|
->all();
|
|
|
|
$sellers = DB::table('quick_sales')
|
|
->where('sold_on', '>=', $from)
|
|
->where('sold_on', '<', $until)
|
|
->groupBy('user_id')
|
|
->select('user_id')
|
|
->selectRaw('COUNT(*) as sale_count')
|
|
->selectRaw('SUM(total_cents) as gross_cents')
|
|
->get()
|
|
->map(fn (object $row): array => ['sellerId' => (int) $row->user_id, 'count' => (int) $row->sale_count, 'grossCents' => (int) $row->gross_cents])
|
|
->all();
|
|
|
|
$dayVatTotals = $this->lines($from, $until)
|
|
->groupBy('quick_sales.sold_on', 'quick_sale_lines.vat_basis_points')
|
|
->orderBy('quick_sales.sold_on')
|
|
->orderBy('quick_sale_lines.vat_basis_points')
|
|
->select('quick_sales.sold_on', 'quick_sale_lines.vat_basis_points')
|
|
->selectRaw('SUM(quick_sale_lines.quantity * quick_sale_lines.unit_price_cents) as gross_cents')
|
|
->get()
|
|
->map(fn (object $row): array => [
|
|
'day' => substr((string) $row->sold_on, 0, 10),
|
|
'vatBasisPoints' => (int) $row->vat_basis_points,
|
|
'grossCents' => (int) $row->gross_cents,
|
|
])
|
|
->all();
|
|
|
|
return new QuickSalesInPeriod(array_values($days), array_values($products), array_values($sellers), array_values($dayVatTotals));
|
|
}
|
|
|
|
private function lines(string $from, string $until): Builder
|
|
{
|
|
return DB::table('quick_sale_lines')
|
|
->join('quick_sales', 'quick_sales.id', '=', 'quick_sale_lines.quick_sale_id')
|
|
->where('quick_sales.sold_on', '>=', $from)
|
|
->where('quick_sales.sold_on', '<', $until);
|
|
}
|
|
}
|