Skip to content

Commit b04ce37

Browse files
committed
Merge branch 'dev' into product2
2 parents 52b3416 + 526a8b7 commit b04ce37

File tree

31 files changed

+3693
-798
lines changed

31 files changed

+3693
-798
lines changed

backend/src/main/java/com/smalltrend/dto/inventory/purchaseorder/GoodsReceiptRequest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import lombok.Data;
66
import lombok.NoArgsConstructor;
77

8+
import java.math.BigDecimal;
89
import java.util.List;
910

1011
@Data
@@ -13,6 +14,13 @@
1314
@AllArgsConstructor
1415
public class GoodsReceiptRequest {
1516
private String notes;
17+
private Integer supplierId;
18+
private Integer locationId;
19+
private BigDecimal taxPercent;
20+
private BigDecimal shippingFee;
21+
private BigDecimal subtotal;
22+
private BigDecimal taxAmount;
23+
private BigDecimal totalAmount;
1624
private List<GoodsReceiptItemRequest> items;
1725

1826
@Data
@@ -23,6 +31,7 @@ public static class GoodsReceiptItemRequest {
2331
private Integer itemId; // PurchaseOrderItem id
2432
private Integer variantId;
2533
private Integer receivedQuantity;
34+
private java.math.BigDecimal unitCost;
2635
private String notes; // Ghi chú kiểm kê
2736
}
2837
}

backend/src/main/java/com/smalltrend/service/inventory/InventoryDashboardService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public List<DashboardProductResponse> getAllProductsForDashboard() {
3737
List<Product> products = productRepository.findAll();
3838
List<InventoryStock> allStocks = inventoryStockRepository.findAll();
3939

40-
log.debug("📦 Dashboard: {} products, {} inventory stock rows", products.size(), allStocks.size());
40+
log.debug(" Dashboard: {} products, {} inventory stock rows", products.size(), allStocks.size());
4141
allStocks.forEach(s -> log.debug(" Stock: variant={}, batch={}, qty={}",
4242
s.getVariant() != null ? s.getVariant().getId() : "null",
4343
s.getBatch() != null ? s.getBatch().getId() : "null",
@@ -170,7 +170,7 @@ public DashboardSummaryResponse getDashboardSummary() {
170170
// Need action
171171
int needActionCount = lowStockCount + expiredCount;
172172

173-
log.debug("📊 Summary: totalProducts={}, totalValue={}, lowStock={}, expired={}, expiring={}",
173+
log.debug(" Summary: totalProducts={}, totalValue={}, lowStock={}, expired={}, expiring={}",
174174
totalProducts, totalValue, lowStockCount, expiredCount, expiringSoonCount);
175175

176176
return DashboardSummaryResponse.builder()

backend/src/main/java/com/smalltrend/service/inventory/PurchaseOrderService.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,37 @@ public PurchaseOrderResponse receiveGoods(Integer orderId, GoodsReceiptRequest r
184184
throw new RuntimeException("Chỉ có thể nhập kho phiếu đang kiểm kê.");
185185
}
186186

187+
if (receiptRequest.getSupplierId() == null) {
188+
throw new RuntimeException("Nhà cung cấp là bắt buộc khi xác nhận nhập kho.");
189+
}
190+
if (receiptRequest.getLocationId() == null) {
191+
throw new RuntimeException("Vị trí nhập kho là bắt buộc khi xác nhận nhập kho.");
192+
}
193+
if (receiptRequest.getTaxPercent() == null) {
194+
throw new RuntimeException("Thuế VAT (%) là bắt buộc khi xác nhận nhập kho.");
195+
}
196+
if (receiptRequest.getShippingFee() == null) {
197+
throw new RuntimeException("Phí vận chuyển là bắt buộc khi xác nhận nhập kho.");
198+
}
199+
if (receiptRequest.getTaxPercent().compareTo(BigDecimal.ZERO) < 0) {
200+
throw new RuntimeException("Thuế VAT (%) không được âm.");
201+
}
202+
if (receiptRequest.getShippingFee().compareTo(BigDecimal.ZERO) < 0) {
203+
throw new RuntimeException("Phí vận chuyển không được âm.");
204+
}
205+
206+
// Cập nhật receivedQuantity cho từng item
207+
if (receiptRequest.getItems() != null) {
208+
for (GoodsReceiptRequest.GoodsReceiptItemRequest receiptItem : receiptRequest.getItems()) {
209+
if (receiptItem.getUnitCost() == null || receiptItem.getUnitCost().compareTo(BigDecimal.ZERO) <= 0) {
210+
throw new RuntimeException("Giá nhập từng sản phẩm là bắt buộc và phải lớn hơn 0.");
211+
}
212+
if (receiptItem.getReceivedQuantity() == null || receiptItem.getReceivedQuantity() < 0) {
213+
throw new RuntimeException("Số lượng thực nhận không hợp lệ.");
214+
}
215+
}
216+
}
217+
187218
// Cập nhật receivedQuantity cho từng item
188219
if (receiptRequest.getItems() != null) {
189220
for (GoodsReceiptRequest.GoodsReceiptItemRequest receiptItem : receiptRequest.getItems()) {
@@ -195,6 +226,11 @@ public PurchaseOrderResponse receiveGoods(Integer orderId, GoodsReceiptRequest r
195226
if (orderItem != null) {
196227
orderItem.setReceivedQuantity(receiptItem.getReceivedQuantity() != null
197228
? receiptItem.getReceivedQuantity() : 0);
229+
if (receiptItem.getUnitCost() != null) {
230+
orderItem.setUnitCost(receiptItem.getUnitCost());
231+
int receivedQty = orderItem.getReceivedQuantity() != null ? orderItem.getReceivedQuantity() : 0;
232+
orderItem.setTotalCost(receiptItem.getUnitCost().multiply(BigDecimal.valueOf(receivedQty)));
233+
}
198234
if (receiptItem.getNotes() != null) {
199235
orderItem.setNotes(receiptItem.getNotes());
200236
}
@@ -203,6 +239,29 @@ public PurchaseOrderResponse receiveGoods(Integer orderId, GoodsReceiptRequest r
203239
}
204240
}
205241

242+
Supplier supplier = supplierRepository.findById(receiptRequest.getSupplierId())
243+
.orElseThrow(() -> new RuntimeException("Nhà cung cấp không tồn tại."));
244+
order.setSupplier(supplier);
245+
order.setLocationId(receiptRequest.getLocationId());
246+
247+
BigDecimal subtotal = order.getItems().stream()
248+
.map(item -> {
249+
int receivedQty = item.getReceivedQuantity() != null ? item.getReceivedQuantity() : 0;
250+
BigDecimal unitCost = item.getUnitCost() != null ? item.getUnitCost() : BigDecimal.ZERO;
251+
return unitCost.multiply(BigDecimal.valueOf(receivedQty));
252+
})
253+
.reduce(BigDecimal.ZERO, BigDecimal::add);
254+
BigDecimal taxPercent = receiptRequest.getTaxPercent();
255+
BigDecimal taxAmount = subtotal.multiply(taxPercent).divide(BigDecimal.valueOf(100));
256+
BigDecimal shippingFee = receiptRequest.getShippingFee();
257+
BigDecimal totalAmount = subtotal.add(taxAmount).add(shippingFee);
258+
259+
order.setSubtotal(subtotal);
260+
order.setTaxPercent(taxPercent);
261+
order.setTaxAmount(taxAmount);
262+
order.setShippingFee(shippingFee);
263+
order.setTotalAmount(totalAmount);
264+
206265
order.setStatus(PurchaseOrderStatus.RECEIVED);
207266
order.setActualDeliveryDate(LocalDate.now());
208267
if (receiptRequest.getNotes() != null) {

backend/src/main/resources/data.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,7 @@ INSERT INTO advertisements (
12221222
NOW(), NOW()
12231223
);
12241224

1225+
12251226
-- =============================================================================
12261227
-- End of SmallTrend Combined Sample Data
12271228
-- =============================================================================

0 commit comments

Comments
 (0)