feat: add purchase
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
use App\Models\OldPurchase;
|
||||
use App\Models\PackageName;
|
||||
use App\Models\Product;
|
||||
use App\Models\Transaction;
|
||||
use App\Models\User;
|
||||
use App\Rules\MobileNumber;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -30,6 +31,45 @@ public function users(Request $request)
|
||||
return $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* List all subscription purchases (transactions) across users, with
|
||||
* filters for email, mobile (phone), package name and payment source
|
||||
* (gateway). Returns paginated results with the related user, product
|
||||
* and package eager-loaded for display in the admin panel.
|
||||
*/
|
||||
public function purchases(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'per_page' => 'integer',
|
||||
'email' => 'string|nullable',
|
||||
'mobile' => 'string|nullable',
|
||||
'package_name' => 'string|nullable',
|
||||
// payment source: accepts a gateway name (asanpardakht, zarinpal,
|
||||
// digipay, cafe, myket) or its numeric code
|
||||
'gateway' => 'string|nullable',
|
||||
'status' => 'integer|nullable',
|
||||
]);
|
||||
|
||||
// Resolve the payment source to its stored integer code
|
||||
$gateway = null;
|
||||
if (!empty($data['gateway'])) {
|
||||
$gateway = Transaction::GATEWAYS[$data['gateway']]
|
||||
?? (is_numeric($data['gateway']) ? (int) $data['gateway'] : null);
|
||||
}
|
||||
|
||||
$purchases = Transaction::with(['user', 'product.packageName'])
|
||||
->when(!empty($data['email']), fn ($q) => $q->whereHas('user', fn ($u) => $u->where('email', 'like', '%' . $data['email'] . '%')))
|
||||
->when(!empty($data['mobile']), fn ($q) => $q->whereHas('user', fn ($u) => $u->where('mobile', 'like', '%' . $data['mobile'] . '%')))
|
||||
->when(!empty($data['package_name']), fn ($q) => $q->whereHas('product.packageName', fn ($p) => $p->where('name', $data['package_name'])))
|
||||
->when(!is_null($gateway), fn ($q) => $q->where('gateway', $gateway))
|
||||
->when(isset($data['status']), fn ($q) => $q->where('status', $data['status']))
|
||||
->latest()
|
||||
->paginate($data['per_page'] ?? 30)
|
||||
->withQueryString();
|
||||
|
||||
return $purchases;
|
||||
}
|
||||
|
||||
public function getUserTransactions(Request $request, $identifier)
|
||||
{
|
||||
$data = $request->validate([
|
||||
|
||||
Reference in New Issue
Block a user