Skip to content

Commit

Permalink
Merge pull request #190 from in27sung/ordermodify
Browse files Browse the repository at this point in the history
수주, 발주 가용재고 관련 코드 수정 및 추가
  • Loading branch information
freehyeon authored Dec 18, 2024
2 parents e702c50 + ac1b02e commit 0981927
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Map<String, String> orderRegisterPOST(@RequestBody OrderVO order, HttpSer
for (OrderItemVO item : order.getOrderItems()) {
if (order.getOrderType() == OrderType.INBOUND) {
// 수주의 경우 가용 재고 체크
if (!orderService.checkAvailableStock(item)) {
if (!orderService.checkAvailableStock(item,order.getOrderType())) {
throw new IllegalArgumentException(
String.format("재고 부족 - StockId: %d, 요청수량: %d",
item.getStockId(), item.getQuantity())
Expand Down
7 changes: 4 additions & 3 deletions stockMate/src/main/java/com/stockm8/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import com.stockm8.domain.vo.OrderItemVO;
import com.stockm8.domain.vo.OrderVO;
import com.stockm8.domain.vo.ProductVO;
import com.stockm8.domain.vo.StockVO;
import com.stockm8.domain.vo.StockVO;
import com.stockm8.domain.vo.OrderVO.OrderType;

public interface OrderService {

Expand All @@ -31,8 +32,8 @@ public interface OrderService {
// 주문 상세 항목 목록 조회
public List<OrderItemVO> getOrderItemsByOrderId(int orderId) throws Exception;

// 가용 재고 체크 미사용
public boolean checkAvailableStock(OrderItemVO item) throws Exception;
// 가용 재고 체크
public boolean checkAvailableStock(OrderItemVO item, OrderType orderType) throws Exception;

// 전체 주문 개수 조회 (페이징 계산)
public int getTotalOrderCount(int businessId);
Expand Down
14 changes: 11 additions & 3 deletions stockMate/src/main/java/com/stockm8/service/OrderServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ private void processOrderByType(OrderVO order) throws Exception {
// 수주(출고) 처리
for (OrderItemVO item : order.getOrderItems()) {
// 재고 감소
if (!checkAvailableStock(item, OrderType.OUTBOUND)) {
throw new Exception("수주 처리 중 오류: 가용 재고가 부족합니다.");
}
updateStockQuantity(item.getStockId(), item.getQuantity());
// 수주(출고)일 때는 예약수량을 양수로 처리해야 함 (+)
}
Expand Down Expand Up @@ -134,9 +137,14 @@ public List<OrderItemVO> getOrderItemsByOrderId(int orderId) throws Exception {

// 가용재고 체크
@Override
public boolean checkAvailableStock(OrderItemVO item) throws Exception {
StockVO stock = odao.getStockById(item.getStockId());
return stock != null && stock.getAvailableStock() >= item.getQuantity();
public boolean checkAvailableStock(OrderItemVO item, OrderType orderType) throws Exception {
// 발주(INBOUND)인 경우 체크하지 않음
if (orderType == OrderType.INBOUND) {
return true;
}
// 수주(OUTBOUND)인 경우만 재고 체크
StockVO stock = odao.getStockById(item.getStockId());
return stock != null && stock.getAvailableStock() >= item.getQuantity();
}

// 전체 주문 개수 조회 (페이징 계산)
Expand Down
41 changes: 28 additions & 13 deletions stockMate/src/main/webapp/WEB-INF/views/order/register.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
<label class="form-label required-label">주문 유형</label>
<div class="form-check">
수주
<input class="form-check-input" type="radio" name="orderType" id="inbound" value="INBOUND" required>
<label class="form-check-label" for="inbound">
<input class="form-check-input" type="radio" name="orderType" id="outbound" value="OUTBOUND" required>
<label class="form-check-label" for="outbound">
</label>
발주
<label class="form-check-label" for="outbound">
<input class="form-check-input" type="radio" name="orderType" id="outbound" value="OUTBOUND" required>
<label class="form-check-label" for="inbound">
<input class="form-check-input" type="radio" name="orderType" id="inbound" value="INBOUND" required>
</label>
</div>
</div>
Expand Down Expand Up @@ -479,7 +479,7 @@
// 주문 수량 최대값 설정(가용 재고량으로 설정)
const quantityInput = card.find('.order-quantity');
quantityInput.attr('max', stock.availableStock); // HTML input의 max 속성 설정
validateQuantity(quantityInput); // 현재 입력된 수량의 유효성 검사 실행
(quantityInput); // 현재 입력된 수량의 유효성 검사 실행
closeModal();
}
Expand All @@ -488,6 +488,8 @@
function validateQuantity(input) {
// 현재 수량 입력이 속한 카드 요소 찾기
const card = input.closest('.stock-info-card');
// 주문 유형 가져오기
const orderType = $('input[name="orderType"]:checked').val();
// 입력된 수량과 가용 재고를 정수로 변환 (값이 없으면 0으로 설정)
const quantity = parseInt(input.val()) || 0; // 입력된 주문 수량 반환실패 시 null 대신 0 값 들어감
const available = parseInt(card.find('.available-stock').val()) || 0; // 가용 재고량
Expand All @@ -496,7 +498,7 @@
if (quantity < 1) { // 최소 주문 수량(1개) 미만인 경우
alert('주문 수량은 1 이상이어야 합니다.');
input.val(1); // 수량을 1로 재설정
} else if (quantity > available) { // 가용 재고보다 많은 경우
} else if (orderType === 'OUTBOUND' && quantity > available) { // 가용 재고보다 많은 경우 수주의 경우에만 실행
alert('주문 수량이 가용 재고를 초과할 수 없습니다.');
input.val(available); // 수량을 가용 재고량으로 재설정
}
Expand Down Expand Up @@ -545,29 +547,42 @@
buttons.prop('disabled', buttons.length <= 1);
}
// 폼 유효성 검사
function validateForm() {
let isValid = true;
let hasUnselectedItems = false;
// 주문 유형 체크(수주/ 발주)
if (!$('input[name="orderType"]:checked').val()){
alert('주문 유형을 선택 해주세요.');
return false;
// 주문 유형 체크(수주/발주)
const orderType = $('input[name="orderType"]:checked').val();
if (!orderType) {
alert('주문 유형을 선택해주세요.');
return false;
}
// 각 주문 항목 검증
$('.stock-info-card:visible').each(function() {
const card = $(this);
// 재고 선택 여부 체크
if (!card.find('.stock-id').val()) {
hasUnselectedItems = true;
return false;
return false; // each 루프 중단
}
// 수량 유효성 체크
const quantity = parseInt(card.find('.order-quantity').val());
const available = parseInt(card.find('.available-stock').val());
if (!quantity || quantity < 1) {
alert('모든 주문의 수량은 1 이상이어야 합니다.');
isValid = false;
return false;
return false; // each 루프 중단
}
// 수주(OUTBOUND)인 경우에만 가용 재고 체크
if (orderType === 'OUTBOUND' && quantity > available) {
alert('수주 수량이 가용 재고를 초과할 수 없습니다.');
isValid = false;
return false; // each 루프 중단
}
});
Expand Down

0 comments on commit 0981927

Please sign in to comment.