feat: add partner store analytics, history logs, and global SRP validation
This commit is contained in:
@@ -128,19 +128,23 @@ class AdminPartnerStoreController extends ApiController
|
||||
|
||||
public function recordSale(Request $request, PartnerStore $store): JsonResponse
|
||||
{
|
||||
$globalPrice = config('qr.default_retail_price_per_code_centavos', 1000);
|
||||
|
||||
$data = $request->validate([
|
||||
'household_id' => ['required', 'integer', 'exists:households,id'],
|
||||
'quantity' => ['required', 'integer', 'min:1', 'max:1000'],
|
||||
'retail_price_per_code_centavos' => ['required', 'integer', 'min:0'],
|
||||
'retail_price_per_code_centavos' => ['nullable', 'integer', 'in:' . $globalPrice],
|
||||
]);
|
||||
|
||||
$retailPrice = (int) ($data['retail_price_per_code_centavos'] ?? $globalPrice);
|
||||
|
||||
try {
|
||||
$household = Household::findOrFail($data['household_id']);
|
||||
$sale = $this->ops->sellToHousehold(
|
||||
$store,
|
||||
$household,
|
||||
(int) $data['quantity'],
|
||||
(int) $data['retail_price_per_code_centavos'],
|
||||
$retailPrice,
|
||||
);
|
||||
} catch (\DomainException $e) {
|
||||
return $this->fail($e->getMessage(), null, 422);
|
||||
@@ -153,4 +157,73 @@ class AdminPartnerStoreController extends ApiController
|
||||
'commission_centavos' => $sale->commission_centavos,
|
||||
], 'Sale recorded');
|
||||
}
|
||||
|
||||
public function purchases(Request $request, PartnerStore $store): JsonResponse
|
||||
{
|
||||
$purchases = $store->purchases()
|
||||
->with('batch')
|
||||
->latest()
|
||||
->paginate(15);
|
||||
|
||||
return $this->ok(
|
||||
$purchases->map(fn ($p) => [
|
||||
'id' => $p->id,
|
||||
'batch_number' => $p->batch?->batch_number ?? '—',
|
||||
'quantity' => $p->quantity,
|
||||
'wholesale_price_pesos' => number_format($p->wholesale_price_centavos / 100, 2),
|
||||
'paid_at' => $p->paid_at?->toIso8601String() ?? $p->created_at?->toIso8601String(),
|
||||
]),
|
||||
null,
|
||||
[
|
||||
'page' => $purchases->currentPage(),
|
||||
'per_page' => $purchases->perPage(),
|
||||
'total' => $purchases->total(),
|
||||
'last_page' => $purchases->lastPage(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function salesHistory(Request $request, PartnerStore $store): JsonResponse
|
||||
{
|
||||
$sales = $store->sales()
|
||||
->with(['household.head'])
|
||||
->latest()
|
||||
->paginate(15);
|
||||
|
||||
return $this->ok(
|
||||
$sales->map(fn ($s) => [
|
||||
'id' => $s->id,
|
||||
'household_name' => $s->household?->head?->full_name ?? '—',
|
||||
'household_email' => $s->household?->head?->email ?? '—',
|
||||
'quantity' => $s->quantity,
|
||||
'retail_price_pesos' => number_format($s->retail_price_centavos / 100, 2),
|
||||
'commission_pesos' => number_format($s->commission_centavos / 100, 2),
|
||||
'sold_at' => $s->sold_at?->toIso8601String() ?? $s->created_at?->toIso8601String(),
|
||||
]),
|
||||
null,
|
||||
[
|
||||
'page' => $sales->currentPage(),
|
||||
'per_page' => $sales->perPage(),
|
||||
'total' => $sales->total(),
|
||||
'last_page' => $sales->lastPage(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function analytics(PartnerStore $store): JsonResponse
|
||||
{
|
||||
$totalWholesaleCost = (int) $store->purchases()->sum('wholesale_price_centavos');
|
||||
$totalRetailRevenue = (int) $store->sales()->sum('retail_price_centavos');
|
||||
$totalCommission = (int) $store->sales()->sum('commission_centavos');
|
||||
$grossProfit = $totalRetailRevenue - $totalWholesaleCost;
|
||||
|
||||
return $this->ok([
|
||||
'total_wholesale_cost_pesos' => number_format($totalWholesaleCost / 100, 2),
|
||||
'total_retail_revenue_pesos' => number_format($totalRetailRevenue / 100, 2),
|
||||
'total_commission_pesos' => number_format($totalCommission / 100, 2),
|
||||
'gross_profit_pesos' => number_format($grossProfit / 100, 2),
|
||||
'total_issued_codes' => (int) $store->purchases()->sum('quantity'),
|
||||
'total_sold_codes' => (int) $store->sales()->sum('quantity'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user