split stock move line

This commit is contained in:
2023-01-07 18:20:07 +01:00
parent ad90a43479
commit 71e8bdab59
3 changed files with 90 additions and 0 deletions

View File

@@ -154,6 +154,12 @@ public interface StockMoveService {
StockMove originalStockMove, List<StockMoveLine> modifiedStockMoveLines) StockMove originalStockMove, List<StockMoveLine> modifiedStockMoveLines)
throws AxelorException; throws AxelorException;
public StockMove splitInto2SameMove(
StockMove originalStockMove, List<StockMoveLine> modifiedStockMoveLines)
throws AxelorException;
public List<Map<String, Object>> getStockPerDate( public List<Map<String, Object>> getStockPerDate(
Long locationId, Long productId, LocalDate fromDate, LocalDate toDate); Long locationId, Long productId, LocalDate fromDate, LocalDate toDate);

View File

@@ -51,6 +51,7 @@ import com.axelor.apps.stock.db.repo.StockMoveRepository;
import com.axelor.apps.stock.exception.IExceptionMessage; import com.axelor.apps.stock.exception.IExceptionMessage;
import com.axelor.apps.stock.report.IReport; import com.axelor.apps.stock.report.IReport;
import com.axelor.common.ObjectUtils; import com.axelor.common.ObjectUtils;
import com.axelor.db.JPA;
import com.axelor.exception.AxelorException; import com.axelor.exception.AxelorException;
import com.axelor.exception.db.repo.TraceBackRepository; import com.axelor.exception.db.repo.TraceBackRepository;
import com.axelor.i18n.I18n; import com.axelor.i18n.I18n;
@@ -70,6 +71,9 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.persistence.Query;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -1317,4 +1321,65 @@ public class StockMoveServiceImpl implements StockMoveService {
} }
} }
} }
// same move
@Override
@Transactional(rollbackOn = {Exception.class})
public StockMove splitInto2SameMove(
StockMove originalStockMove, List<StockMoveLine> modifiedStockMoveLines)
throws AxelorException {
modifiedStockMoveLines =
modifiedStockMoveLines
.stream()
.filter(stockMoveLine -> stockMoveLine.getQty().compareTo(BigDecimal.ZERO) != 0)
.collect(Collectors.toList());
for (StockMoveLine moveLine : modifiedStockMoveLines) {
StockMoveLine newStockMoveLine = new StockMoveLine();
// Set quantity in new stock move line
newStockMoveLine = stockMoveLineRepo.copy(moveLine, false);
newStockMoveLine.setQty(moveLine.getQty());
newStockMoveLine.setRealQty(moveLine.getQty());
newStockMoveLine.setProductTypeSelect(moveLine.getProductTypeSelect());
// add stock move line
originalStockMove.addStockMoveLineListItem(newStockMoveLine);
// find the original move line to update it
Optional<StockMoveLine> correspondingMoveLine =
originalStockMove
.getStockMoveLineList()
.stream()
.filter(stockMoveLine -> stockMoveLine.getId().equals(moveLine.getId()))
.findFirst();
if (BigDecimal.ZERO.compareTo(moveLine.getQty()) > 0
|| (correspondingMoveLine.isPresent()
&& moveLine.getQty().compareTo(correspondingMoveLine.get().getRealQty()) > 0)) {
throw new AxelorException(
TraceBackRepository.CATEGORY_INCONSISTENCY,
I18n.get(IExceptionMessage.STOCK_MOVE_16),
originalStockMove);
}
if (correspondingMoveLine.isPresent()) {
// Update quantity in original stock move.
// If the remaining quantity is 0, remove the stock move line
BigDecimal remainingQty = correspondingMoveLine.get().getQty().subtract(moveLine.getQty());
if (BigDecimal.ZERO.compareTo(remainingQty) == 0) {
// Remove the stock move line
originalStockMove.removeStockMoveLineListItem(correspondingMoveLine.get());
} else {
correspondingMoveLine.get().setQty(remainingQty);
correspondingMoveLine.get().setRealQty(remainingQty);
}
}
}
if (!originalStockMove.getStockMoveLineList().isEmpty()) {
return stockMoveRepo.save(originalStockMove);
} else {
return null;
}
}
} }

View File

@@ -410,6 +410,25 @@ public class StockMoveController {
} }
} }
public void splitInto2Same(ActionRequest request, ActionResponse response) {
StockMove stockMove = request.getContext().asType(StockMove.class);
List<StockMoveLine> modifiedStockMoveLineList = stockMove.getStockMoveLineList();
stockMove = Beans.get(StockMoveRepository.class).find(stockMove.getId());
try {
StockMove newStockMove =
Beans.get(StockMoveService.class).splitInto2SameMove(stockMove, modifiedStockMoveLineList);
if (newStockMove == null) {
response.setFlash(I18n.get(IExceptionMessage.STOCK_MOVE_SPLIT_NOT_GENERATED));
} else {
response.setReload(true);
}
} catch (Exception e) {
TraceBackService.trace(response, e);
}
}
public void changeConformityStockMove(ActionRequest request, ActionResponse response) { public void changeConformityStockMove(ActionRequest request, ActionResponse response) {
StockMove stockMove = request.getContext().asType(StockMove.class); StockMove stockMove = request.getContext().asType(StockMove.class);