use of eu.ggnet.dwoss.mandator.api.service.WarrantyService in project dwoss by gg-net.
the class RedTapeCloserOperation method poluteReporting.
/**
* Actually creating reportLines from the reportable documents.
* For each position of a {@link Document} a {@link ReportLine} is created with all information supplied.
* <p>
* Exceptions are:
* <ul>
* <li>A {@link Document} with a {@link Condition#CANCELED} which is silently ignored</li>
* <li>A {@link Document} with a {@link DocumentType#COMPLAINT} which sets all prices on the {@link ReportLine} to 0 and
* <ul>
* <li>If the {@link Document} has the {@link Condition#WITHDRAWN} or {@link Condition#REJECTED}, set {@link ReportLine#workflowStatus} to
* {@link ReportLine.WorkflowStatus#DISCHARGED}</li>
* <li>If the {@link Document} has the {@link Condition#ACCEPTED}, set {@link ReportLine#workflowStatus} to
* {@link ReportLine.WorkflowStatus#CHARGED}</li>
* <li>Otherwise set {@link ReportLine#workflowStatus} to {@link ReportLine.WorkflowStatus#UNDER_PROGRESS}</li>
* </ul>
* </li>
* <li>A {@link Document} with a {@link DocumentType#CREDIT_MEMO} gets its prices inverted</li>
* </ul>
* <p/>
* @param reportable the documents to create lines from
* @param monitor a optional monitor.
* @return the amount of created lines.
*/
private int poluteReporting(Set<Document> reportable, Date reporting, IMonitor monitor) {
WarrantyService warrantyService = null;
if (!warrantyServiceInstance.isUnsatisfied()) {
warrantyService = warrantyServiceInstance.get();
}
SubMonitor m = SubMonitor.convert(monitor, reportable.size() + 10);
m.start();
ReportLineEao reportLineEao = new ReportLineEao(reportEm);
UniqueUnitEao uniqueUnitEao = new UniqueUnitEao(uuEm);
ProductEao productEao = new ProductEao(uuEm);
int amountCreate = 0;
List<ReportLine> newLines = new ArrayList<>(reportable.size());
for (Document document : reportable) {
m.worked(1, "reported " + document.getIdentifier());
// A canceled document must be closed, but must not create a reportline.
if (document.getConditions().contains(Condition.CANCELED))
continue;
ReportLine l;
for (Position position : document.getPositions().values()) {
amountCreate++;
l = new ReportLine();
l.setActual(document.getActual());
l.setAmount(position.getAmount());
l.setBookingAccount(position.getBookingAccount().map(Ledger::getValue).orElse(-1));
l.setCustomerId(document.getDossier().getCustomerId());
l.setDescription(normalizeSpace(position.getDescription()));
l.setDocumentId(document.getId());
l.setDocumentIdentifier(document.getIdentifier());
l.setDocumentType(document.getType());
l.setDossierId(document.getDossier().getId());
l.setDossierIdentifier(document.getDossier().getIdentifier());
// TODO: We could use something else for a separator, but keep in mind that we want to avoid name, , , something.
l.setInvoiceAddress(normalizeSpace(document.getInvoiceAddress().getDescription()));
l.setName(normalizeSpace(position.getName()));
l.setPositionType(position.getType());
l.setPrice(position.getPrice());
l.setReportingDate(reporting);
l.setTax(position.getTax());
// Set via Report afterwards
l.setMarginPercentage(0);
// Set via Report afterwards
l.setPurchasePrice(0);
UiCustomer c = customerService.asUiCustomer(document.getDossier().getCustomerId());
if (c != null) {
l.setCustomerCompany(c.getCompany());
l.setCustomerName(c.toTitleNameLine());
l.setCustomerEmail(c.getEmail());
}
// A Credit Memo gets its prices inverted
if (document.getType() == DocumentType.CREDIT_MEMO) {
l.setPrice(position.getPrice() * (-1));
}
// Special handling of complaints.
if (document.getType() == DocumentType.COMPLAINT) {
// A Complaint position has "tagging" effect, but shall never result in a plus or minus.
l.setPrice(0);
if (document.getConditions().contains(Condition.REJECTED) || document.getConditions().contains(Condition.WITHDRAWN)) {
l.setWorkflowStatus(ReportLine.WorkflowStatus.DISCHARGED);
} else if (document.getConditions().contains(Condition.ACCEPTED)) {
l.setWorkflowStatus(ReportLine.WorkflowStatus.CHARGED);
} else {
l.setWorkflowStatus(ReportLine.WorkflowStatus.UNDER_PROGRESS);
}
}
// Extra information for Type Position
if (position.getType() == PositionType.UNIT || position.getType() == PositionType.UNIT_ANNEX) {
UniqueUnit uu = Objects.requireNonNull(uniqueUnitEao.findById(position.getUniqueUnitId()), "No UniqueUnit with id=" + position.getUniqueUnitId());
Product p = uu.getProduct();
if (uu.getContractor() == p.getTradeName().getManufacturer()) {
l.setContractorPartNo(p.getPartNo());
l.setContractorReferencePrice(p.getPrice(MANUFACTURER_COST));
} else {
l.setContractorPartNo(p.getAdditionalPartNo(uu.getContractor()));
l.setContractorReferencePrice(p.getPrice(CONTRACTOR_REFERENCE));
}
l.setManufacturerCostPrice(p.getPrice(MANUFACTURER_COST));
l.setContractor(uu.getContractor());
l.setContractorReferencePrice(p.getPrice(CONTRACTOR_REFERENCE));
if (Math.abs(l.getContractorReferencePrice()) < 0.001)
l.setContractorReferencePrice(p.getPrice(MANUFACTURER_COST));
l.setMfgDate(uu.getMfgDate());
l.setRefurbishId(uu.getRefurbishId());
l.setSerial(uu.getSerial());
l.setUniqueUnitId(uu.getId());
l.setSalesChannel(uu.getSalesChannel());
l.setPartNo(p.getPartNo());
l.setProductBrand(p.getTradeName());
l.setProductName(p.getName());
l.setProductGroup(p.getGroup());
l.setProductId(p.getId());
l.setGtin(p.getGtin());
// Extra Information for Type Product Batch
} else if (position.getType() == PositionType.PRODUCT_BATCH) {
Product p = Objects.requireNonNull(productEao.findById(position.getUniqueUnitProductId()), "No Product for id=" + position.getUniqueUnitProductId());
l.setPartNo(p.getPartNo());
l.setProductBrand(p.getTradeName());
l.setProductGroup(p.getGroup());
l.setProductName(p.getName());
l.setProductId(p.getId());
l.setGtin(p.getGtin());
l.setUniqueUnitId(position.getUniqueUnitId());
l.setSerial(position.getSerial());
l.setRefurbishId(position.getRefurbishedId());
if (warrantyService != null) {
// If this is no warranty Partno, this will return null ;-)
l.setContractor(warrantyService.warrantyContractor(p.getPartNo()));
}
}
reportEm.persist(l);
newLines.add(l);
}
}
reportEm.flush();
m.message("Updateing References");
for (ReportLine newLine : newLines) {
// Not Refs for Product_Batches or Versandkosten jet.
if (newLine.getUniqueUnitId() < 1)
continue;
if (newLine.getPositionType() == PositionType.PRODUCT_BATCH) {
// TODO: also evaluate the productId.
newLine.addAll(reportLineEao.findBySerialAndPositionTypeAndDossierId(newLine.getSerial(), newLine.getPositionType(), newLine.getDossierId()));
} else {
newLine.addAll(reportLineEao.findUnitsAlike(newLine.getUniqueUnitId(), newLine.getDossierId()));
}
}
updateSingleReferences(newLines);
m.worked(5);
m.finish();
return amountCreate;
}
Aggregations