Commit 8f8270b1 authored by Marco Descher's avatar Marco Descher

[21017] Perform differential disposal/return for stock in billingservice

parent 61bb3f8f
Pipeline #16139 passed with stages
in 7 minutes and 37 seconds
......@@ -335,8 +335,13 @@ public class BillingService implements IBillingService {
if (billable instanceof IArticle) {
IArticle art = (IArticle) billable;
String mandatorId = contextService.getActiveMandator().map(m -> m.getId()).orElse(null);
stockService.performSingleReturn(art, (int) oldAmount, mandatorId);
stockService.performSingleDisposal(art, (int) newAmount, mandatorId);
double difference = newAmount - oldAmount;
if(difference > 0) {
stockService.performSingleDisposal(art, (int) difference, mandatorId);
} else if (difference < 0) {
difference *= -1;
stockService.performSingleReturn(art, (int) difference, mandatorId);
}
}
}
}
......@@ -69,6 +69,9 @@ public class StockService implements IStockService {
@Override
public IStatus performSingleDisposal(IArticle article, int count, String mandatorId){
if (count < 0) {
throw new IllegalArgumentException();
}
if (article == null) {
return new Status(Status.ERROR, "ch.elexis.core.services", "Article is null");
}
......@@ -144,6 +147,9 @@ public class StockService implements IStockService {
@Override
public IStatus performSingleReturn(IArticle article, int count, String mandatorId){
if (count < 0) {
throw new IllegalArgumentException();
}
if (article == null) {
return new Status(Status.ERROR, "ch.elexis.core.services", "Article is null");
}
......
package ch.elexis.core.services.holder;
import org.osgi.service.component.annotations.Reference;
import ch.elexis.core.services.IAccessControlService;
public class AccessControlServiceHolder {
private static IAccessControlService accessControlService;
@Reference
public void setContextService(IAccessControlService accessControlService){
AccessControlServiceHolder.accessControlService = accessControlService;
}
public static IAccessControlService get(){
if (accessControlService == null) {
throw new IllegalStateException("No IAccessControlService available");
}
return accessControlService;
}
}
package ch.elexis.core.ui.dialogs;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import ch.elexis.core.l10n.Messages;
public class StatusDialog {
public static void show(IStatus status){
if (!status.getMessage().isEmpty()) {
if (status.isOK()) {
showInfo(status);
} else {
if (isWarnining(status)) {
showWarninig(status);
} else {
showError(status);
}
}
}
}
private static boolean isWarnining(IStatus status){
return Status.WARNING == status.getSeverity();
}
private static void showError(IStatus status){
MessageDialog.openError(getShell(), Messages.ResultDialog_Error, getStatusMessage(status));
}
private static void showWarninig(IStatus status){
MessageDialog.openWarning(getShell(), Messages.ResultDialog_Warning,
getStatusMessage(status));
}
private static void showInfo(IStatus status){
MessageDialog.openInformation(getShell(), Messages.ResultDialog_Info,
getStatusMessage(status));
}
private static String getStatusMessage(IStatus status){
return status.getMessage();
}
private static Shell getShell(){
return Display.getDefault().getActiveShell();
}
}
......@@ -19,6 +19,7 @@ import java.util.List;
import javax.inject.Inject;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.ui.di.UIEventTopic;
import org.eclipse.jface.action.Action;
......@@ -72,11 +73,10 @@ import org.eclipse.ui.statushandlers.StatusManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.elexis.admin.AccessControlDefaults;
import ch.elexis.core.ac.AccessControlDefaults;
import ch.elexis.core.common.ElexisEventTopics;
import ch.elexis.core.constants.Preferences;
import ch.elexis.core.constants.StringConstants;
import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.data.events.ElexisEventDispatcher;
import ch.elexis.core.data.interfaces.IDiagnose;
import ch.elexis.core.data.interfaces.IVerrechenbar;
......@@ -94,6 +94,7 @@ import ch.elexis.core.model.IEncounter;
import ch.elexis.core.model.IService;
import ch.elexis.core.model.prescription.EntryType;
import ch.elexis.core.services.IBillingService;
import ch.elexis.core.services.holder.AccessControlServiceHolder;
import ch.elexis.core.services.holder.BillingServiceHolder;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.core.services.holder.CoreModelServiceHolder;
......@@ -102,6 +103,7 @@ import ch.elexis.core.ui.Hub;
import ch.elexis.core.ui.UiDesk;
import ch.elexis.core.ui.actions.CodeSelectorHandler;
import ch.elexis.core.ui.dialogs.ResultDialog;
import ch.elexis.core.ui.dialogs.StatusDialog;
import ch.elexis.core.ui.icons.Images;
import ch.elexis.core.ui.locks.AcquireLockUi;
import ch.elexis.core.ui.locks.IUnlockable;
......@@ -543,7 +545,7 @@ public class VerrechnungsDisplay extends Composite implements IUnlockable {
o = presc.getArtikel();
}
if (o instanceof IVerrechenbar) {
if (CoreHub.acl.request(AccessControlDefaults.LSTG_VERRECHNEN) == false) {
if (AccessControlServiceHolder.get().request(AccessControlDefaults.LSTG_VERRECHNEN) == false) {
SWTHelper.alert(Messages.VerrechnungsDisplay_missingRightsCaption, //$NON-NLS-1$
Messages.VerrechnungsDisplay_missingRightsBody); //$NON-NLS-1$
} else {
......@@ -1061,11 +1063,10 @@ public class VerrechnungsDisplay extends Composite implements IUnlockable {
"Wenn der Preis bereits geändert wurde, darf die Anzahl nur ganzzahlig sein.");
return;
}
double diff = changeAnzahl - billed.getAmount();
Result<?> result =
BillingServiceHolder.get().bill(billable, actEncounter, diff);
if(!result.isOK()) {
ResultDialog.show(result);
IStatus status = BillingServiceHolder.get().changeAmountValidated(billed, changeAnzahl);
if(!status.isOK()) {
StatusDialog.show(status);
return;
}
billed.setText(text);
......@@ -1094,6 +1095,6 @@ public class VerrechnungsDisplay extends Composite implements IUnlockable {
}
public void adaptMenus(){
table.getMenu().setEnabled(CoreHub.acl.request(AccessControlDefaults.LSTG_VERRECHNEN));
table.getMenu().setEnabled(AccessControlServiceHolder.get().request(AccessControlDefaults.LSTG_VERRECHNEN));
}
}
......@@ -40,6 +40,7 @@ public class IBillingServiceTest extends AbstractServiceTest {
private static ICustomService customService;
private static IArticle customArticle;
private static IEncounter encounter;
private static IStockEntry customArticleStockEntry;
@BeforeClass
public static void beforeClass(){
......@@ -56,6 +57,10 @@ public class IBillingServiceTest extends AbstractServiceTest {
customArticle.setTyp(ArticleTyp.EIGENARTIKEL);
coreModelService.save(customArticle);
customArticleStockEntry = StockServiceHolder.get()
.storeArticleInStock(StockServiceHolder.get().getDefaultStock(), customArticle);
assertEquals(1, customArticleStockEntry.getCurrentStock());
encounter = new IEncounterBuilder(CoreModelServiceHolder.get(),
AllServiceTests.getCoverage(), AllServiceTests.getMandator()).buildAndSave();
}
......@@ -67,6 +72,9 @@ public class IBillingServiceTest extends AbstractServiceTest {
@After
public void after(){
customArticleStockEntry.setCurrentStock(1);
coreModelService.save(customArticleStockEntry);
cleanup();
}
......@@ -99,6 +107,8 @@ public class IBillingServiceTest extends AbstractServiceTest {
assertEquals(1, billedList.size());
assertEquals(1, billedList.get(0).getAmount(), 0d);
assertEquals(1024, billedList.get(0).getPrice().getCents());
coreModelService.remove(billed.get());
}
@Test
......@@ -141,17 +151,34 @@ public class IBillingServiceTest extends AbstractServiceTest {
@Test
public void billArticleAndDecrementStock(){
IStockEntry stockEntry = StockServiceHolder.get()
.storeArticleInStock(StockServiceHolder.get().getDefaultStock(), customArticle);
assertEquals(1, stockEntry.getCurrentStock());
Result<IBilled> billed = billingService.bill(customArticle, encounter, 1.0);
assertTrue(billed.isOK());
CoreModelServiceHolder.get().remove(billed.get());
stockEntry = StockServiceHolder.get().findStockEntryForArticleInStock(
IStockEntry stockEntry = StockServiceHolder.get().findStockEntryForArticleInStock(
StockServiceHolder.get().getDefaultStock(), customArticle);
assertEquals(0, stockEntry.getCurrentStock());
}
@Test
public void changeAmountCorrectlyModifiesStock(){
customArticleStockEntry.setCurrentStock(8);
coreModelService.save(customArticleStockEntry);
Result<IBilled> billed = billingService.bill(customArticle, encounter, 1.0);
assertTrue(billed.isOK());
billingService.changeAmountValidated(billed.get(), 4);
IStockEntry stockEntry = StockServiceHolder.get().findStockEntryForArticleInStock(
StockServiceHolder.get().getDefaultStock(), customArticle);
assertEquals(4, stockEntry.getCurrentStock());
billingService.changeAmountValidated(billed.get(), 3);
stockEntry = StockServiceHolder.get().findStockEntryForArticleInStock(
StockServiceHolder.get().getDefaultStock(), customArticle);
assertEquals(5, stockEntry.getCurrentStock());
CoreModelServiceHolder.get().remove(billed.get());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment