Skip to content

Commit 3ca66df

Browse files
authored
Merge pull request #119 from dnd-side-project/fix/#118-week_total
주간결제 계산 방식 수정
2 parents 313db9a + f9cc635 commit 3ca66df

File tree

2 files changed

+65
-29
lines changed

2 files changed

+65
-29
lines changed

src/main/java/com/dnd/sub/domain/product/repository/ProductRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public interface ProductRepository extends JpaRepository<Product, Long> {
3030
SELECT p
3131
FROM Product p
3232
WHERE p.category = :category
33+
AND p.isAdminWritten = true
3334
AND p.id NOT IN (
3435
SELECT s.product.id
3536
FROM Subscription s

src/main/java/com/dnd/sub/domain/subscription/repository/SubscriptionQueryRepositoryImpl.java

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.dnd.sub.domain.subscription.dto.GetMySubscriptionDto;
1010
import com.dnd.sub.domain.subscription.dto.GetPaymentSoonDto;
1111
import com.dnd.sub.domain.subscription.dto.response.GetPaymentTotalResponse;
12+
import com.dnd.sub.domain.subscription.entity.PayCycleUnitType;
1213
import com.dnd.sub.domain.subscription.entity.QSubscription;
1314
import com.dnd.sub.domain.subscription.entity.Subscription;
1415
import com.querydsl.core.BooleanBuilder;
@@ -121,54 +122,88 @@ public GetPaymentTotalResponse findPaymentTotal(Long memberId) {
121122
Subscription sub = tuple.get(s);
122123
Integer price = tuple.get(pp.price);
123124

124-
if(sub.getStartedAt() == null){
125+
if (sub.getStartedAt() == null) {
125126
continue;
126127
}
127128
int personalPrice = price / sub.getParticipantCount();
128-
LocalDate prevPayDay = sub.getPreviousPaymentDay();
129-
LocalDate nextPayDay = sub.getNextPaymentDay();
130-
131-
// 이전 결제일이 이번달인 경우
132-
boolean isPrevThisMonth = false;
133-
if (prevPayDay != null) {
134-
isPrevThisMonth = !prevPayDay.isBefore(startDay) &&
135-
!prevPayDay.isAfter(endDay);
129+
PayCycleUnitType cycleUnit = sub.getPayCycleUnit();
130+
131+
if (cycleUnit == PayCycleUnitType.WEEK) {
132+
LocalDate prevPayDay = sub.getPreviousPaymentDay();
133+
134+
if (prevPayDay != null) {
135+
LocalDate currentPaymentDay = prevPayDay;
136+
137+
// 과거 방향으로 결제한 금액
138+
while (!currentPaymentDay.isBefore(startDay)) {
139+
if (!currentPaymentDay.isAfter(endDay) && !currentPaymentDay.isAfter(today)) {
140+
totalAmount += personalPrice;
141+
usedAmount += personalPrice;
142+
}
143+
currentPaymentDay = currentPaymentDay.minusWeeks(1);
144+
}
145+
146+
// 미래 방향으로 결제할 금액
147+
LocalDate nextPayDay = sub.getNextPaymentDay();
148+
if (nextPayDay != null) {
149+
currentPaymentDay = nextPayDay;
150+
while (!currentPaymentDay.isAfter(endDay)) {
151+
if (!currentPaymentDay.isBefore(startDay) && currentPaymentDay.isAfter(today)) {
152+
totalAmount += personalPrice;
153+
remainingAmount += personalPrice;
154+
}
155+
currentPaymentDay = currentPaymentDay.plusWeeks(1);
156+
}
157+
}
158+
}
136159
}
137-
// 다음 결제일이 이번달인 경우
138-
boolean isNextThisMonth = false;
139-
if (nextPayDay != null) {
140-
isNextThisMonth = !nextPayDay.isBefore(startDay) &&
160+
else {
161+
LocalDate prevPayDay = sub.getPreviousPaymentDay();
162+
LocalDate nextPayDay = sub.getNextPaymentDay();
163+
164+
// 이전 결제일이 이번달인 경우
165+
boolean isPrevThisMonth = false;
166+
if (prevPayDay != null) {
167+
isPrevThisMonth = !prevPayDay.isBefore(startDay) &&
168+
!prevPayDay.isAfter(endDay);
169+
}
170+
// 다음 결제일이 이번달인 경우
171+
boolean isNextThisMonth = false;
172+
if (nextPayDay != null) {
173+
isNextThisMonth = !nextPayDay.isBefore(startDay) &&
141174
!nextPayDay.isAfter(endDay) &&
142175
nextPayDay.isAfter(today);
143-
}
176+
}
144177

145-
boolean isPrevPay = isPrevThisMonth && !prevPayDay.isAfter(today);
178+
boolean isPrevPay = isPrevThisMonth && !prevPayDay.isAfter(today);
146179

147-
if (isPrevThisMonth || isNextThisMonth) {
148-
totalAmount += personalPrice;
149-
}
180+
if (isPrevThisMonth || isNextThisMonth) {
181+
totalAmount += personalPrice;
182+
}
150183

151-
if (isPrevPay) {
152-
usedAmount += personalPrice;
153-
}
184+
if (isPrevPay) {
185+
usedAmount += personalPrice;
186+
}
154187

155-
if (isNextThisMonth) {
156-
remainingAmount += personalPrice;
188+
if (isNextThisMonth) {
189+
remainingAmount += personalPrice;
190+
}
157191
}
158192
}
159193

160-
int progressPercentage = 0;
161-
if (totalAmount > 0) {
162-
progressPercentage = (100 * usedAmount) / totalAmount;
163-
}
194+
int progressPercentage = 0;
195+
if (totalAmount > 0) {
196+
progressPercentage = (100 * usedAmount) / totalAmount;
197+
}
164198

165-
return new GetPaymentTotalResponse(
199+
return new GetPaymentTotalResponse(
166200
tuples.get(0).get(m.name),
167201
totalAmount,
168202
remainingAmount,
169203
progressPercentage,
170204
subCount
171-
);
205+
);
206+
172207
}
173208

174209
private List<GetMySubscriptionDto> findSubscriptions(BooleanBuilder builder,

0 commit comments

Comments
 (0)