ifnex/04_Laravel/app/Http/Controllers/Api/CustomerFinancialController.php
Kazem Alghasi 02c29db696 feat(core): implement order approval flow, credit system, and import invoicing
Introduce a comprehensive set of commercial features including a multi-step
order approval workflow, customer credit management, and specialized
import service invoicing.

Key changes:
- Implement `pending_approval` and `approved` shipment statuses to allow
  staff verification before customer payment.
- Add a credit system to `User` model with `credit_limit` and `credit_used`
  to manage customer balances and debts.
- Develop a new `importInvoice` PDF generation service following the
  "Sheet ENG Invoice" specification for import services.
- Add Filament resources for managing Audit Logs, Commitment Forms,
  Customer Credits, and Shipment Checklists.
- Implement staff-specific APIs for order approval/rejection and
  customer financial status monitoring.
- Integrate Kavenegar SMS service for mobile verification and notifications.
- Add bulk tracking import functionality via CSV/Excel.
- Update WordPress bridge assets (CSS/JS) to support the new multi-step
  order form UI and updated redirection logic.
- Update deployment configurations and documentation to reflect new
  production domains and feature sets.
2026-09-03 06:04:20 +03:30

136 lines
4.8 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Shipment;
use App\Models\WalletTransaction;
use App\Enums\ShipmentStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class CustomerFinancialController extends Controller
{
/**
* جستجوی مشتری بر اساس نام یا ایمیل
* GET /api/v1/staff/customers/search?q=search_term
*/
public function search(Request $request): JsonResponse
{
$validated = $request->validate([
'q' => ['required', 'string', 'min:2'],
]);
$query = User::query()
->where(function ($q) use ($validated) {
$q->where('name', 'LIKE', "%{$validated['q']}%")
->orWhere('email', 'LIKE', "%{$validated['q']}%")
->orWhere('phone', 'LIKE', "%{$validated['q']}%");
})
->with('wallet');
$customers = $query->take(20)->get()->map(function ($customer) {
return [
'id' => $customer->id,
'name' => $customer->name,
'email' => $customer->email,
'phone' => $customer->phone,
'wallet_balance' => $customer->wallet ? $customer->wallet->balance : 0,
];
});
return response()->json([
'success' => true,
'data' => $customers,
]);
}
/**
* دریافت وضعیت مالی کامل مشتری
* GET /api/v1/staff/customers/{customer}/financial-status
*/
public function financialStatus(User $customer): JsonResponse
{
// دریافت سفارشات در انتظار پرداخت (تأیید شده)
$pending_orders = Shipment::where('user_id', $customer->id)
->where('status', ShipmentStatus::Approved)
->with(['fromCountry', 'toCountry'])
->orderBy('created_at', 'desc')
->get();
// محاسبه بدهی به هر ارز
$debts_by_currency = $pending_orders->groupBy(function ($order) {
return $order->fromCountry?->iso_code ?? 'unknown';
})->map(function ($orders, $currency) {
return [
'currency' => $currency,
'total' => $orders->sum('total_fee'),
'count' => $orders->count(),
];
});
// دریافت تراکنش‌های اخیر
$recent_transactions = WalletTransaction::where('user_id', $customer->id)
->orderBy('created_at', 'desc')
->take(10)
->get()
->map(fn ($tx) => [
'id' => $tx->id,
'type' => $tx->type,
'amount' => $tx->amount,
'description' => $tx->description,
'created_at' => $tx->created_at->format('Y-m-d H:i'),
]);
// دریافت سفارشات اخیر
$recent_orders = Shipment::where('user_id', $customer->id)
->with(['fromCountry', 'toCountry'])
->orderBy('created_at', 'desc')
->take(10)
->get()
->map(fn ($order) => [
'id' => $order->id,
'awb_no' => $order->awb_no,
'status' => [
'value' => $order->status?->value,
'label' => $order->status?->label(),
],
'direction' => $order->direction?->value,
'total_fee' => $order->total_fee,
'from_country' => $order->fromCountry?->name,
'to_country' => $order->toCountry?->name,
'created_at' => $order->created_at->format('Y-m-d H:i'),
]);
// محاسبه کل بدهی
$total_debt = $pending_orders->sum('total_fee');
// موجودی کیف پول
$wallet_balance = $customer->wallet ? $customer->wallet->balance : 0;
// مانده حساب
$account_balance = $wallet_balance - $total_debt;
return response()->json([
'success' => true,
'customer' => [
'id' => $customer->id,
'name' => $customer->name,
'email' => $customer->email,
'phone' => $customer->phone,
],
'financial' => [
'wallet_balance' => $wallet_balance,
'total_debt' => $total_debt,
'account_balance' => $account_balance,
'debts_by_currency' => array_values($debts_by_currency->toArray()),
'pending_orders_count' => $pending_orders->count(),
],
'recent_transactions' => $recent_transactions,
'recent_orders' => $recent_orders,
]);
}
}