Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[26808] stockService - add stock to article return & disposal method #765

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public Result<IBilled> bill(IBillable billable, IEncounter encounter, double amo

if (billable instanceof IArticle) {
IStatus status = stockService.performSingleDisposal((IArticle) billable, doubleToInt(amount),
contextService.getActiveMandator().map(m -> m.getId()).orElse(null));
contextService.getActiveMandator().map(m -> m.getId()).orElse(null), null);
if (!status.isOK()) {
StatusUtil.logStatus(logger, status, true);
}
Expand Down Expand Up @@ -247,7 +247,7 @@ public Result<?> removeBilled(IBilled billed, IEncounter encounter) {
// TODO stock return via event
IArticle article = (IArticle) billable;
String mandatorId = contextService.getActiveMandator().map(m -> m.getId()).orElse(null);
stockService.performSingleReturn(article, (int) billed.getAmount(), mandatorId);
stockService.performSingleReturn(article, (int) billed.getAmount(), mandatorId, null);

// TODO prescription via event
Object prescId = billed.getExtInfo(Constants.FLD_EXT_PRESC_ID);
Expand Down Expand Up @@ -369,10 +369,10 @@ public void changeAmount(IBilled billed, double newAmount) {
String mandatorId = contextService.getActiveMandator().map(m -> m.getId()).orElse(null);
double difference = newAmount - oldAmount;
if (difference > 0) {
stockService.performSingleDisposal(art, (int) difference, mandatorId);
stockService.performSingleDisposal(art, (int) difference, mandatorId, null);
} else if (difference < 0) {
difference *= -1;
stockService.performSingleReturn(art, (int) difference, mandatorId);
stockService.performSingleReturn(art, (int) difference, mandatorId, null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,21 @@ public Long getCumulatedStockForArticle(IArticle article) {

public void performSingleDisposal(IArticle article, int count) {
Optional<IMandator> mandator = ContextServiceHolder.get().getActiveMandator();
performSingleDisposal(article, count, (mandator.isPresent()) ? mandator.get().getId() : null);
performSingleDisposal(article, count, (mandator.isPresent()) ? mandator.get().getId() : null, null);
}

@Override
public IStatus performSingleDisposal(IArticle article, int count, String mandatorId) {
public IStatus performSingleDisposal(IArticle article, int count, String mandatorId, IStock stock) {
if (count < 0) {
throw new IllegalArgumentException();
}
if (article == null) {
return new Status(Status.ERROR, "ch.elexis.core.services", "Article is null");
}

IStockEntry se = findPreferredStockEntryForArticle(StoreToStringServiceHolder.getStoreToString(article),
mandatorId);

IStockEntry se = (stock == null)
? findPreferredStockEntryForArticle(StoreToStringServiceHolder.getStoreToString(article), mandatorId)
: this.findStockEntryForArticleInStock(stock, article);
if (se == null) {
return new Status(Status.WARNING, "ch.elexis.core.services", "No stock entry for article found");
}
Expand Down Expand Up @@ -141,20 +142,17 @@ public IStatus performSingleDisposal(IArticle article, int count, String mandato
}

@Override
public IStatus performSingleReturn(IArticle article, int count, String mandatorId) {
public IStatus performSingleReturn(IArticle article, int count, String mandatorId, IStock stock) {
if (count < 0) {
throw new IllegalArgumentException();
}
if (article == null) {
return new Status(Status.ERROR, "ch.elexis.core.services", "Article is null");
}

IStockEntry se = findPreferredStockEntryForArticle(StoreToStringServiceHolder.getStoreToString(article),
mandatorId);
if (se == null) {
return new Status(Status.WARNING, "ch.elexis.core.services", "No stock entry for article found");
}

IStockEntry se = (stock == null)
? findPreferredStockEntryForArticle(StoreToStringServiceHolder.getStoreToString(article), mandatorId)
: this.findStockEntryForArticleInStock(stock, article);
if (se.getStock().isCommissioningSystem()) {
// updates must happen via manual inputs in the machine
return Status.OK_STATUS;
Expand Down Expand Up @@ -412,7 +410,7 @@ public List<IStockEntry> findAllStockEntriesForStock(IStock stock) {
public IStatus performSingleDisposal(String articleStoreToString, int count, String mandatorId) {
Optional<Identifiable> article = StoreToStringServiceHolder.get().loadFromString(articleStoreToString);
if (article.isPresent()) {
return performSingleDisposal((IArticle) article.get(), count, mandatorId);
return performSingleDisposal((IArticle) article.get(), count, mandatorId, null);
}
return new Status(Status.WARNING, "ch.elexis.core.services", "No article found [" + articleStoreToString + "]");
}
Expand All @@ -421,7 +419,7 @@ public IStatus performSingleDisposal(String articleStoreToString, int count, Str
public IStatus performSingleReturn(String articleStoreToString, int count, String mandatorId) {
Optional<Identifiable> article = StoreToStringServiceHolder.get().loadFromString(articleStoreToString);
if (article.isPresent()) {
return performSingleReturn((IArticle) article.get(), count, mandatorId);
return performSingleReturn((IArticle) article.get(), count, mandatorId, null);
}
return new Status(Status.WARNING, "ch.elexis.core.services", "No article found [" + articleStoreToString + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.eclipse.core.runtime.IStatus;

import ch.elexis.core.jdt.Nullable;
import ch.elexis.core.model.IArticle;
import ch.elexis.core.model.IPatient;
import ch.elexis.core.model.IStock;
Expand Down Expand Up @@ -131,14 +132,16 @@ public enum Availability {

/**
* Perform a single disposal of an article. The article will be withdrawn from
* the Stock with the highest priority owning this article (if multiple).
* the Stock with the highest priority owning this article (if multiple) or from
* the given stock.
*
* @param article
* @param mandatorId may be <code>null</code> to not consider the mandator
* @param count
* @param stock
* @return
*/
public IStatus performSingleDisposal(IArticle article, int count, String mandatorId);
public IStatus performSingleDisposal(IArticle article, int count, String mandatorId, @Nullable IStock stock);

/**
* Perform a single disposal of an article. Use this method if only the store to
Expand All @@ -159,9 +162,10 @@ public enum Availability {
* @param article
* @param count
* @param mandatorId
* @param stock
* @return
*/
public IStatus performSingleReturn(IArticle article, int count, String mandatorId);
public IStatus performSingleReturn(IArticle article, int count, String mandatorId, @Nullable IStock stock);

/**
* Perform a single return of an article. Use this method if only the store to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import ch.elexis.core.model.IArticle;
import ch.elexis.core.model.IPatient;
import ch.elexis.core.model.IPerson;
import ch.elexis.core.model.IStock;
import ch.elexis.core.model.IStockEntry;
import ch.elexis.core.model.builder.IArticleBuilder;
Expand All @@ -22,6 +23,7 @@
import ch.elexis.core.types.ArticleTyp;
import ch.elexis.core.types.Gender;
import ch.elexis.core.utils.OsgiServiceUtil;
import ch.rgw.tools.TimeTool;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class IStockServiceTest extends AbstractServiceTest {
Expand All @@ -30,14 +32,18 @@ public class IStockServiceTest extends AbstractServiceTest {
private static IStockService service;
private static IPatient patient;
private static IArticle article;
private static IPerson person;

@BeforeClass
public static void beforeClass() {
TimeTool timeTool = new TimeTool();
service = OsgiServiceUtil.getService(IStockService.class).get();
patient = new IContactBuilder.PatientBuilder(coreModelService, "test", "patient", LocalDate.of(2000, 1, 1),
Gender.MALE).buildAndSave();
article = new IArticleBuilder(coreModelService, "test medication article", "1234567", ArticleTyp.ARTIKELSTAMM)
.buildAndSave();
person = new IContactBuilder.PersonBuilder(coreModelService, "mandator1 " + timeTool.toString(),
"Anton" + timeTool.toString(), timeTool.toLocalDate(), Gender.MALE).mandator().buildAndSave();
}

@Test
Expand Down Expand Up @@ -72,4 +78,28 @@ public void b_deletePatientStock() {
List<IStockEntry> entries = service.findAllStockEntriesForStock(patientStock);
assertTrue(entries.isEmpty());
}

@Test
public void performSingleDisposalWithProvidedStock() {
IStock stock = coreModelService.create(IStock.class);
stock.setOwner(person);
IStockEntry stockEntry = service.storeArticleInStock(stock, article);
stockEntry.setCurrentStock(2);
coreModelService.save(stockEntry);

service.performSingleDisposal(article, 1, person.getId(), null);
assertEquals(1, service.findStockEntryForArticleInStock(stock, article).getCurrentStock());
}

@Test
public void performSingleReturn() {
IStock stock = coreModelService.create(IStock.class);
IStockEntry stockEntry = service.storeArticleInStock(stock, article);
stockEntry.setCurrentStock(1);
coreModelService.save(stockEntry);

service.performSingleReturn(article, 1, person.getId(), stock);
assertEquals(2, service.findStockEntryForArticleInStock(stock, article).getCurrentStock());

}
}