use of com.axelor.apps.account.db.Invoice in project axelor-open-suite by axelor.
the class StockMoveMultiInvoiceServiceImpl method areFieldsConflictedToGenerateSupplierInvoice.
@Override
public Map<String, Object> areFieldsConflictedToGenerateSupplierInvoice(List<StockMove> stockMoveList) throws AxelorException {
Map<String, Object> mapResult = new HashMap<>();
boolean paymentConditionToCheck = false;
boolean paymentModeToCheck = false;
boolean contactPartnerToCheck = false;
checkForAlreadyInvoicedStockMove(stockMoveList);
List<Invoice> dummyInvoiceList = stockMoveList.stream().map(this::createDummyInInvoice).collect(Collectors.toList());
checkInStockMoveRequiredFieldsAreTheSame(dummyInvoiceList);
if (!dummyInvoiceList.isEmpty()) {
PaymentCondition firstPaymentCondition = dummyInvoiceList.get(0).getPaymentCondition();
PaymentMode firstPaymentMode = dummyInvoiceList.get(0).getPaymentMode();
Partner firstContactPartner = dummyInvoiceList.get(0).getContactPartner();
paymentConditionToCheck = !dummyInvoiceList.stream().map(Invoice::getPaymentCondition).allMatch(paymentCondition -> Objects.equals(paymentCondition, firstPaymentCondition));
paymentModeToCheck = !dummyInvoiceList.stream().map(Invoice::getPaymentMode).allMatch(paymentMode -> Objects.equals(paymentMode, firstPaymentMode));
contactPartnerToCheck = !dummyInvoiceList.stream().map(Invoice::getContactPartner).allMatch(contactPartner -> Objects.equals(contactPartner, firstContactPartner));
mapResult.put("paymentCondition", firstPaymentCondition);
mapResult.put("paymentMode", firstPaymentMode);
mapResult.put("contactPartner", firstContactPartner);
}
mapResult.put("paymentConditionToCheck", paymentConditionToCheck);
mapResult.put("paymentModeToCheck", paymentModeToCheck);
mapResult.put("contactPartnerToCheck", contactPartnerToCheck);
return mapResult;
}
use of com.axelor.apps.account.db.Invoice in project axelor-open-suite by axelor.
the class StockMoveMultiInvoiceServiceImpl method createDummyInInvoice.
/**
* Create a dummy invoice to hold fields used to generate the invoice which will be saved.
*
* @param stockMove an in stock move.
* @return the created dummy invoice.
*/
protected Invoice createDummyInInvoice(StockMove stockMove) {
Invoice dummyInvoice = new Invoice();
if (stockMove.getOriginId() != null && StockMoveRepository.ORIGIN_PURCHASE_ORDER.equals(stockMove.getOriginTypeSelect())) {
PurchaseOrder purchaseOrder = purchaseOrderRepository.find(stockMove.getOriginId());
dummyInvoice.setCurrency(purchaseOrder.getCurrency());
dummyInvoice.setPartner(purchaseOrder.getSupplierPartner());
dummyInvoice.setCompany(purchaseOrder.getCompany());
dummyInvoice.setTradingName(purchaseOrder.getTradingName());
dummyInvoice.setPaymentCondition(purchaseOrder.getPaymentCondition());
dummyInvoice.setPaymentMode(purchaseOrder.getPaymentMode());
dummyInvoice.setContactPartner(purchaseOrder.getContactPartner());
dummyInvoice.setPriceList(purchaseOrder.getPriceList());
dummyInvoice.setInAti(purchaseOrder.getInAti());
} else {
dummyInvoice.setCurrency(stockMove.getCompany().getCurrency());
dummyInvoice.setPartner(stockMove.getPartner());
dummyInvoice.setCompany(stockMove.getCompany());
dummyInvoice.setTradingName(stockMove.getTradingName());
dummyInvoice.setAddress(stockMove.getFromAddress());
dummyInvoice.setAddressStr(stockMove.getFromAddressStr());
}
return dummyInvoice;
}
use of com.axelor.apps.account.db.Invoice in project axelor-open-suite by axelor.
the class StockMoveMultiInvoiceServiceImpl method createInvoiceFromMultiIncomingStockMove.
@Override
@Transactional(rollbackOn = { Exception.class })
public Optional<Invoice> createInvoiceFromMultiIncomingStockMove(List<StockMove> stockMoveList) throws AxelorException {
if (stockMoveList == null || stockMoveList.isEmpty()) {
return Optional.empty();
}
// create dummy invoice from the first stock move
Invoice dummyInvoice = createDummyInInvoice(stockMoveList.get(0));
// Check if field constraints are respected
for (StockMove stockMove : stockMoveList) {
completeInvoiceInMultiIncomingStockMove(dummyInvoice, stockMove);
}
/* check if some other fields are different and assign a default value */
if (dummyInvoice.getAddress() == null) {
dummyInvoice.setAddress(Beans.get(PartnerService.class).getInvoicingAddress(dummyInvoice.getPartner()));
dummyInvoice.setAddressStr(Beans.get(AddressService.class).computeAddressStr(dummyInvoice.getAddress()));
}
fillReferenceInvoiceFromMultiInStockMove(stockMoveList, dummyInvoice);
InvoiceGenerator invoiceGenerator = new InvoiceGenerator(InvoiceRepository.OPERATION_TYPE_SUPPLIER_PURCHASE, dummyInvoice.getCompany(), dummyInvoice.getPaymentCondition(), dummyInvoice.getPaymentMode(), dummyInvoice.getAddress(), dummyInvoice.getPartner(), dummyInvoice.getContactPartner(), dummyInvoice.getCurrency(), dummyInvoice.getPriceList(), dummyInvoice.getInternalReference(), dummyInvoice.getExternalReference(), dummyInvoice.getInAti(), null, dummyInvoice.getTradingName(), null) {
@Override
public Invoice generate() throws AxelorException {
return super.createInvoiceHeader();
}
};
Invoice invoice = invoiceGenerator.generate();
invoice.setAddressStr(dummyInvoice.getAddressStr());
List<InvoiceLine> invoiceLineList = new ArrayList<>();
for (StockMove stockMoveLocal : stockMoveList) {
List<InvoiceLine> createdInvoiceLines = stockMoveInvoiceService.createInvoiceLines(invoice, stockMoveLocal, stockMoveLocal.getStockMoveLineList(), null);
if (stockMoveLocal.getTypeSelect() == StockMoveRepository.TYPE_OUTGOING) {
createdInvoiceLines.forEach(this::negateInvoiceLinePrice);
}
invoiceLineList.addAll(createdInvoiceLines);
}
invoiceGenerator.populate(invoice, invoiceLineList);
invoiceRepository.save(invoice);
invoice = toPositivePriceInvoice(invoice);
if (invoice.getExTaxTotal().signum() == 0 && stockMoveList.stream().allMatch(StockMove::getIsReversion)) {
invoice.setOperationTypeSelect(InvoiceRepository.OPERATION_TYPE_SUPPLIER_REFUND);
}
stockMoveList.forEach(invoice::addStockMoveSetItem);
return Optional.of(invoice);
}
use of com.axelor.apps.account.db.Invoice in project axelor-open-suite by axelor.
the class StockMoveMultiInvoiceServiceImpl method createDummyOutInvoice.
/**
* Create a dummy invoice to hold fields used to generate the invoice which will be saved.
*
* @param stockMove an out stock move.
* @return the created dummy invoice.
*/
protected Invoice createDummyOutInvoice(StockMove stockMove) {
Invoice dummyInvoice = new Invoice();
if (stockMove.getOriginId() != null && StockMoveRepository.ORIGIN_SALE_ORDER.equals(stockMove.getOriginTypeSelect())) {
SaleOrder saleOrder = saleOrderRepository.find(stockMove.getOriginId());
dummyInvoice.setCurrency(saleOrder.getCurrency());
dummyInvoice.setPartner(saleOrder.getClientPartner());
dummyInvoice.setCompany(saleOrder.getCompany());
dummyInvoice.setTradingName(saleOrder.getTradingName());
dummyInvoice.setPaymentCondition(saleOrder.getPaymentCondition());
dummyInvoice.setPaymentMode(saleOrder.getPaymentMode());
dummyInvoice.setAddress(saleOrder.getMainInvoicingAddress());
dummyInvoice.setAddressStr(saleOrder.getMainInvoicingAddressStr());
dummyInvoice.setContactPartner(saleOrder.getContactPartner());
dummyInvoice.setPriceList(saleOrder.getPriceList());
dummyInvoice.setInAti(saleOrder.getInAti());
dummyInvoice.setGroupProductsOnPrintings(saleOrder.getGroupProductsOnPrintings());
} else {
dummyInvoice.setCurrency(stockMove.getCompany().getCurrency());
dummyInvoice.setPartner(stockMove.getPartner());
dummyInvoice.setCompany(stockMove.getCompany());
dummyInvoice.setTradingName(stockMove.getTradingName());
dummyInvoice.setAddress(stockMove.getToAddress());
dummyInvoice.setAddressStr(stockMove.getToAddressStr());
dummyInvoice.setGroupProductsOnPrintings(stockMove.getGroupProductsOnPrintings());
}
return dummyInvoice;
}
use of com.axelor.apps.account.db.Invoice in project axelor-open-suite by axelor.
the class StockMoveMultiInvoiceServiceImpl method toPositivePriceInvoice.
/**
* For any invoice, if ex tax total is negative, returns the corresponding refund invoice.
*/
protected Invoice toPositivePriceInvoice(Invoice invoice) throws AxelorException {
if (invoice.getExTaxTotal().signum() < 0) {
Invoice refund = transformToRefund(invoice);
invoiceRepository.remove(invoice);
return refund;
} else {
return invoice;
}
}
Aggregations