feat(API): 부실채권, 재고, 입고 기능 개선
- BadDebt 컨트롤러/서비스 기능 확장 - StockService 재고 조회 로직 개선 - ProcessReceivingRequest 검증 규칙 수정 - Item, Order, CommonCode, Shipment 모델 업데이트 - TodayIssueObserverService 개선 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,50 +5,69 @@
|
||||
use App\Models\BadDebts\BadDebt;
|
||||
use App\Models\BadDebts\BadDebtDocument;
|
||||
use App\Models\BadDebts\BadDebtMemo;
|
||||
use App\Models\Orders\Client;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class BadDebtService extends Service
|
||||
{
|
||||
/**
|
||||
* 악성채권 목록 조회
|
||||
* 악성채권 목록 조회 (거래처 기준)
|
||||
*
|
||||
* 거래처별로 is_active=true인 악성채권을 집계하여 조회
|
||||
*/
|
||||
public function index(array $params): LengthAwarePaginator
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
$query = BadDebt::query()
|
||||
$query = Client::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->with(['client:id,name,client_code', 'assignedUser:id,name']);
|
||||
// is_active=true인 악성채권이 있는 거래처만
|
||||
->whereHas('badDebts', function ($q) {
|
||||
$q->where('is_active', true);
|
||||
})
|
||||
// 활성 악성채권 eager loading (추심중/법적조치 + is_active=true)
|
||||
->with(['activeBadDebts' => function ($q) {
|
||||
$q->with('assignedUser:id,name')
|
||||
->orderBy('created_at', 'desc');
|
||||
}])
|
||||
// 집계: 총 미수금액 (is_active=true인 건만)
|
||||
->withSum(['badDebts as total_debt_amount' => function ($q) {
|
||||
$q->where('is_active', true);
|
||||
}], 'debt_amount')
|
||||
// 집계: 최대 연체일수 (is_active=true인 건만)
|
||||
->withMax(['badDebts as max_overdue_days' => function ($q) {
|
||||
$q->where('is_active', true);
|
||||
}], 'overdue_days')
|
||||
// 집계: 악성채권 건수 (is_active=true인 건만)
|
||||
->withCount(['badDebts as active_bad_debt_count' => function ($q) {
|
||||
$q->where('is_active', true);
|
||||
}]);
|
||||
|
||||
// 거래처 필터
|
||||
if (! empty($params['client_id'])) {
|
||||
$query->where('client_id', $params['client_id']);
|
||||
$query->where('id', $params['client_id']);
|
||||
}
|
||||
|
||||
// 상태 필터
|
||||
// 상태 필터 (해당 상태의 악성채권이 있는 거래처만)
|
||||
if (! empty($params['status'])) {
|
||||
$query->where('status', $params['status']);
|
||||
}
|
||||
|
||||
// 활성화 필터
|
||||
if (isset($params['is_active'])) {
|
||||
$query->where('is_active', $params['is_active']);
|
||||
$query->whereHas('badDebts', function ($q) use ($params) {
|
||||
$q->where('is_active', true)
|
||||
->where('status', $params['status']);
|
||||
});
|
||||
}
|
||||
|
||||
// 검색어 필터
|
||||
if (! empty($params['search'])) {
|
||||
$search = $params['search'];
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->whereHas('client', function ($q) use ($search) {
|
||||
$q->where('name', 'like', "%{$search}%")
|
||||
->orWhere('client_code', 'like', "%{$search}%");
|
||||
});
|
||||
$q->where('name', 'like', "%{$search}%")
|
||||
->orWhere('client_code', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
// 정렬
|
||||
$sortBy = $params['sort_by'] ?? 'created_at';
|
||||
$sortBy = $params['sort_by'] ?? 'total_debt_amount';
|
||||
$sortDir = $params['sort_dir'] ?? 'desc';
|
||||
$query->orderBy($sortBy, $sortDir);
|
||||
|
||||
@@ -59,14 +78,16 @@ public function index(array $params): LengthAwarePaginator
|
||||
}
|
||||
|
||||
/**
|
||||
* 악성채권 요약 통계
|
||||
* 악성채권 요약 통계 (is_active=true인 건만)
|
||||
*/
|
||||
public function summary(array $params = []): array
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
// is_active=true인 악성채권만 통계
|
||||
$query = BadDebt::query()
|
||||
->where('tenant_id', $tenantId);
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('is_active', true);
|
||||
|
||||
// 거래처 필터
|
||||
if (! empty($params['client_id'])) {
|
||||
@@ -82,12 +103,20 @@ public function summary(array $params = []): array
|
||||
$recoveredAmount = (clone $query)->recovered()->sum('debt_amount');
|
||||
$badDebtAmount = (clone $query)->badDebt()->sum('debt_amount');
|
||||
|
||||
// 거래처 수 (is_active=true인 악성채권이 있는)
|
||||
$clientCount = BadDebt::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('is_active', true)
|
||||
->distinct('client_id')
|
||||
->count('client_id');
|
||||
|
||||
return [
|
||||
'total_amount' => (float) $totalAmount,
|
||||
'collecting_amount' => (float) $collectingAmount,
|
||||
'legal_action_amount' => (float) $legalActionAmount,
|
||||
'recovered_amount' => (float) $recoveredAmount,
|
||||
'bad_debt_amount' => (float) $badDebtAmount,
|
||||
'client_count' => $clientCount,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user