@@ -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 ) {
0 commit comments