feat: partner store financial settlements, qr replacements, and tenant resolution fix
This commit is contained in:
@@ -6,6 +6,8 @@ use App\Http\Controllers\Api\V1\ApiController;
|
||||
use App\Http\Resources\PartnerStoreResource;
|
||||
use App\Models\Household;
|
||||
use App\Models\PartnerStore;
|
||||
use App\Models\QrCode;
|
||||
use App\Models\StoreInventoryAdjustment;
|
||||
use App\Services\Store\StoreOperations;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -116,6 +118,8 @@ class AdminPartnerStoreController extends ApiController
|
||||
$store,
|
||||
(int) $data['quantity'],
|
||||
(int) $data['wholesale_price_centavos'],
|
||||
null,
|
||||
$request->user()
|
||||
);
|
||||
|
||||
return $this->created([
|
||||
@@ -158,6 +162,76 @@ class AdminPartnerStoreController extends ApiController
|
||||
], 'Sale recorded');
|
||||
}
|
||||
|
||||
public function reportIssue(Request $request, PartnerStore $store): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'serial' => ['required', 'string', 'exists:qr_codes,serial'],
|
||||
'reason' => ['required', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
try {
|
||||
$this->ops->reportDefective(
|
||||
$store,
|
||||
$data['serial'],
|
||||
$data['reason'],
|
||||
$request->user()->id
|
||||
);
|
||||
} catch (\DomainException $e) {
|
||||
return $this->fail($e->getMessage(), null, 422);
|
||||
}
|
||||
|
||||
return $this->ok([
|
||||
'inventory_balance' => $store->fresh()->load('inventory')->inventory?->current_code_balance ?? 0,
|
||||
], 'Defective QR reported and inventory adjusted');
|
||||
}
|
||||
|
||||
public function adjustInventory(Request $request, PartnerStore $store): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'quantity' => ['required', 'integer', 'not_in:0'],
|
||||
'reason' => ['required', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$this->ops->manualAdjustment(
|
||||
$store,
|
||||
(int) $data['quantity'],
|
||||
$data['reason'],
|
||||
$request->user()->id
|
||||
);
|
||||
|
||||
return $this->ok([
|
||||
'inventory_balance' => $store->fresh()->load('inventory')->inventory?->current_code_balance ?? 0,
|
||||
], 'Inventory adjusted manually');
|
||||
}
|
||||
|
||||
public function inventoryLog(Request $request, PartnerStore $store): JsonResponse
|
||||
{
|
||||
$logs = StoreInventoryAdjustment::query()
|
||||
->where('store_id', $store->id)
|
||||
->with(['user', 'qrCode'])
|
||||
->latest('id')
|
||||
->paginate(15);
|
||||
|
||||
return $this->ok(
|
||||
$logs->map(fn ($l) => [
|
||||
'id' => $l->id,
|
||||
'type' => $l->type,
|
||||
'quantity' => $l->quantity,
|
||||
'qr_serial' => $l->qrCode?->serial ?? '—',
|
||||
'reason' => $l->reason ?? '—',
|
||||
'adjusted_by' => $l->user?->full_name ?? 'System',
|
||||
'created_at' => $l->created_at->toIso8601String(),
|
||||
]),
|
||||
null,
|
||||
[
|
||||
'page' => $logs->currentPage(),
|
||||
'per_page' => $logs->perPage(),
|
||||
'total' => $logs->total(),
|
||||
'last_page' => $logs->lastPage(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function purchases(Request $request, PartnerStore $store): JsonResponse
|
||||
{
|
||||
$purchases = $store->purchases()
|
||||
@@ -210,18 +284,85 @@ class AdminPartnerStoreController extends ApiController
|
||||
);
|
||||
}
|
||||
|
||||
public function recordPayment(Request $request, PartnerStore $store): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'amount_pesos' => ['required', 'numeric', 'min:0.01'],
|
||||
'payment_method' => ['required', 'string', 'in:cash,gcash,maya,card'],
|
||||
'reference_number' => ['nullable', 'string', 'max:100'],
|
||||
'notes' => ['nullable', 'string', 'max:500'],
|
||||
]);
|
||||
|
||||
$settlement = $this->ops->recordPayment(
|
||||
$store,
|
||||
(int) round($data['amount_pesos'] * 100),
|
||||
$data['payment_method'],
|
||||
$data['reference_number'] ?? null,
|
||||
$data['notes'] ?? null,
|
||||
$request->user()->id
|
||||
);
|
||||
|
||||
return $this->ok([
|
||||
'id' => $settlement->id,
|
||||
'balance_due_pesos' => number_format($this->ops->calculateBalanceDue($store) / 100, 2),
|
||||
], 'Payment recorded successfully');
|
||||
}
|
||||
|
||||
public function settlementHistory(Request $request, PartnerStore $store): JsonResponse
|
||||
{
|
||||
$history = $store->settlements()
|
||||
->with('recorder')
|
||||
->latest('settled_at')
|
||||
->paginate(15);
|
||||
|
||||
return $this->ok(
|
||||
$history->map(fn ($s) => [
|
||||
'id' => $s->id,
|
||||
'amount_pesos' => number_format($s->amount_centavos / 100, 2),
|
||||
'method' => strtoupper($s->payment_method),
|
||||
'reference' => $s->reference_number ?? '—',
|
||||
'recorded_by' => $s->recorder?->full_name ?? '—',
|
||||
'settled_at' => $s->settled_at->toIso8601String(),
|
||||
]),
|
||||
null,
|
||||
[
|
||||
'page' => $history->currentPage(),
|
||||
'per_page' => $history->perPage(),
|
||||
'total' => $history->total(),
|
||||
'last_page' => $history->lastPage(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function printReplacement(Request $request, PartnerStore $store, QrCode $qrCode): JsonResponse
|
||||
{
|
||||
// Safety check: ensure the QR belongs to this store and is a replacement
|
||||
if ($qrCode->assigned_to_store_id !== $store->id) {
|
||||
return $this->fail('Unauthorized access to this QR code', null, 403);
|
||||
}
|
||||
|
||||
// In a real app, this might return a PDF or a signed URL to a PDF.
|
||||
// For now, we'll return the data needed to render a printable QR.
|
||||
return $this->ok([
|
||||
'serial' => $qrCode->serial,
|
||||
'qr_data' => route('qr.verify', ['serial' => $qrCode->serial]),
|
||||
]);
|
||||
}
|
||||
|
||||
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;
|
||||
$totalSettled = (int) $store->settlements()->sum('amount_centavos');
|
||||
$balanceDue = $this->ops->calculateBalanceDue($store);
|
||||
|
||||
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_settled_pesos' => number_format($totalSettled / 100, 2),
|
||||
'balance_due_pesos' => number_format($balanceDue / 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