Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.pochta.abon.ui.window;
- import com.vaadin.ui.*;
- import org.apache.commons.lang3.exception.ExceptionUtils;
- import org.apache.log4j.Logger;
- import org.springframework.context.MessageSource;
- import ru.pochta.abon.container.Container;
- import ru.pochta.abon.library.dto.abonClient.dto.*;
- import ru.pochta.abon.library.dto.abonClient.enumeration.ClientTypeEnum;
- import ru.pochta.abon.library.dto.abonClient.enumeration.PaymentMethodEnum;
- import ru.pochta.abon.library.dto.abonClient.enumeration.PaymentStatusEnum;
- import ru.pochta.abon.library.dto.abonClient.enumeration.PaymentTypeEnum;
- import ru.pochta.abon.library.dto.abonClient.util.ClaimUtil;
- import ru.pochta.abon.library.service.abonClient.ClaimService;
- import ru.pochta.abon.library.service.abonClient.ServiceManager;
- import ru.pochta.abon.library.service.abonClient.exception.RestException;
- import ru.pochta.abon.library.service.integration.tarificator.TarificatorService;
- import ru.pochta.abon.library.service.reporting.PrintableForm;
- import ru.pochta.abon.library.service.reporting.ReportTypes;
- import ru.pochta.abon.library.util.CustomStringUtils;
- import ru.pochta.abon.ui.component.ClaimIndividualInfoForm;
- import ru.pochta.abon.ui.component.ClaimInfoForm;
- import ru.pochta.abon.ui.component.ClaimLegalInfoForm;
- import ru.pochta.abon.util.ExceptionNotificationUtil;
- import java.math.BigDecimal;
- import java.time.ZonedDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.*;
- import static ru.pochta.abon.library.service.abonClient.RussianRegionService.RUSSIAN_REGION_REL;
- import static ru.pochta.abon.payments.PaymentProcessor.createPayment;
- import static ru.pochta.abon.payments.PaymentProcessor.sendToEAS;
- public class ClaimInfoWindow extends Window {
- private static final Logger LOG = Logger.getLogger(ClaimInfoWindow.class);
- private Locale locale = Locale.forLanguageTag("ru");
- private Container container;
- private ServiceManager serviceManager;
- private MessageSource messageSource;
- private ClaimInfoForm claimInfoInnerForm;
- public ClaimInfoWindow(Claim claim, MessageSource messageSource,
- ServiceManager serviceManager, Container container, boolean isShowClaimInfoButtons, PrintableForm printableForm) {
- super();
- this.container = container;
- this.serviceManager = serviceManager;
- this.messageSource = messageSource;
- setCaption(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.title", new Object[]{claim.getEntityId()}, locale));
- center();
- setModal(true);
- setResizable(false);
- VerticalLayout infoLayout = new VerticalLayout();
- infoLayout.setSizeUndefined();
- infoLayout.setMargin(true);
- this.setContent(infoLayout);
- VerticalLayout claimInfoLayout = new VerticalLayout();
- try {
- Claim claimFull = this.serviceManager.getClaimService().getOneByLink(claim.getLink(ClaimService.SELF_LINK));
- if (claimFull != null && claim.getClient() != null) {
- if (claimFull.getClient().getClientType().getEntityId().equals(ClientTypeEnum.ORGANIZATION.getId())) {
- claimInfoInnerForm = new ClaimLegalInfoForm(container, serviceManager, messageSource, claimFull);
- } else {
- claimInfoInnerForm = new ClaimIndividualInfoForm(container, serviceManager, messageSource, claimFull);
- }
- }
- claimInfoLayout.addComponent(null != claimInfoInnerForm ? claimInfoInnerForm : new FormLayout());
- isShowClaimInfoButtons = false;
- HorizontalLayout buttonsLayout = new HorizontalLayout();
- buttonsLayout.setSpacing(true);
- if (isShowClaimInfoButtons) {
- Button printClaim = new Button((this.messageSource.getMessage("button.printClaim", null, locale)));
- printClaim.addClickListener(click -> {
- printableForm.print(claimFull, ReportTypes.CLAIM);
- });
- buttonsLayout.addComponent(printClaim);
- Button printInvoice = new Button(this.messageSource.getMessage("button.printInvoice", null, locale));
- printInvoice.addClickListener(clickEvent -> {
- printableForm.print(claimFull, ReportTypes.PAYMENT_BILL);
- }
- );
- buttonsLayout.addComponent(printInvoice);
- } else {
- setCaption(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refusalTitle", new Object[]{claimFull.getEntityId()}, locale));
- ZonedDateTime refusalDate = ZonedDateTime.
- now().
- withZoneSameInstant(container.getZoneId());
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
- TextField refusalDateField = new TextField(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refuseDate", null, locale));
- refusalDateField.setValue(refusalDate.format(formatter));
- refusalDateField.setEnabled(false);
- TextField endDate = new TextField(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.terminationDate", null, locale));
- ZonedDateTime endRefusalDate = ClaimUtil.getEndRefusalDate(container.getZoneId());
- endDate.setValue(
- endRefusalDate.format(formatter));
- endDate.setEnabled(false);
- TextField refundAmount = new TextField(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refundAmount", null, locale));
- boolean isProlongate = null != claimFull.getParent();
- BigDecimal refundValue = null;
- try {
- org.springframework.hateoas.Link link = container.getEmployee().getCurrentPostOffice().getLink(RUSSIAN_REGION_REL);
- RussianRegion region = serviceManager.getRussianRegionService().getOneByLink(link);
- refundValue = TarificatorService.getClaimRefusalSum(
- claimFull,
- region.getEntityId(),
- isProlongate);
- } catch (Exception e) {
- LOG.error(ExceptionUtils.getStackTrace(e));
- Notification.show(
- this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refundAmountError", null, getLocale()),
- Notification.Type.TRAY_NOTIFICATION);
- }
- refundAmount.setValue(CustomStringUtils.formatBigDecimal(refundValue));
- refundAmount.setEnabled(false);
- Button printRefuseButton = new Button(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.printRefuseButton", null, locale));
- printRefuseButton.setEnabled(true);
- printRefuseButton.addClickListener(clickEvent -> {
- close();
- });
- Button refuseServiceButton = new Button(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.completeRefuseService", null, locale));
- refuseServiceButton.setEnabled(true);
- refuseServiceButton.addClickListener(clickEvent -> {
- ConfirmationDialog dialogRefusal = new ConfirmationDialog(
- this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refusalTitle", new Object[]{claimFull.getEntityId()}, locale),
- this.messageSource.getMessage("claimsTab.ClaimInfoWindow.reallyRefuse", null, locale),
- this.messageSource.getMessage("button.Ok", null, locale),
- this.messageSource.getMessage("button.cancel", null, locale)
- );
- dialogRefusal.setClosable(false);
- dialogRefusal.setOkButtonListener(event -> {
- // First of all we check for the current claim if there is
- // no payment with negative amount and status Paid or Awaiting Confirmation
- List<Payment> payments = serviceManager.getPaymentService()
- .findByClaim(claim);
- Optional<Payment> failedPayment = payments
- .stream()
- .filter(payment -> (payment.getAmount().compareTo(BigDecimal.ZERO) < 0) &&
- (payment.getPaymentStatus().getEntityId().equals(PaymentStatusEnum.PAID.getId()) ||
- payment.getPaymentStatus().getEntityId().equals(PaymentStatusEnum.AWAITING_CONFIRMATION.getId())))
- .findFirst();
- if (failedPayment.isPresent()) {
- Notification notification = ExceptionNotificationUtil
- .getNotification(new RestException(
- this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refundPaymentAlreadyCreated", new Object[]{failedPayment.get().getEntityId(), failedPayment.get().getPaymentStatus().getStatus()}, locale)
- ), this.messageSource, locale);
- LOG.debug("found a refund payment [" + failedPayment.get().getEntityId() + "] for the claim [" + claim.getEntityId() + "]");
- dialogRefusal.close();
- notification.show(getUI().getPage());
- return;
- }
- PaymentType paymentType = serviceManager.getPaymentService().getLastClaimPaymentType(claimFull, PaymentStatusEnum.PAID);
- PaymentMethod paymentMethod = null;
- if (container.getLoggedViaEASOPS()) {
- paymentMethod = container.getPaymentMethods().get((int) (PaymentMethodEnum.CASH.getId() - 1));
- } else {
- container.getPaymentMethods().get((int) (PaymentMethodEnum.BILL.getId() - 1));
- }
- if (paymentType != null) {
- Payment payment = null;
- try {
- payment = createPayment(
- claimFull,
- paymentMethod,
- paymentType,
- true,
- container, serviceManager);
- } catch (Exception e) {
- LOG.error(ExceptionUtils.getStackTrace(e));
- Notification.show(
- this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refundAmountError", null, getLocale()),
- Notification.Type.TRAY_NOTIFICATION);
- }
- if (payment != null && payment.getEntityId() != null) {
- claimFull.setRefusalDate(endRefusalDate);
- claimFull.setUpdateDate(refusalDate);
- ClaimService claimService = serviceManager.getClaimService();
- try {
- claimService.update(claimFull);
- } catch (RestException e) {
- LOG.error(ExceptionUtils.getStackTrace(e));
- }
- PaymentType easPaymentType = container.getPaymentType(PaymentTypeEnum.EAS);
- if (paymentType.equals(easPaymentType)) {
- sendToEAS(claimFull, payment);
- } else {
- payment.setPaymentStatus(container.getPaymentStatus(PaymentStatusEnum.PAID));
- payment.setResolutionDate(ZonedDateTime.now().
- withZoneSameInstant(container.getZoneId()));
- payment.setPaymentMethod(container.getPaymentMethod(PaymentMethodEnum.CASHLESS));
- payment.setDescription("Отказ безнал");
- payment.setReceiptNumber("Отказ");
- try {
- serviceManager.getPaymentService().update(payment);
- } catch (RestException e) {
- LOG.error(ExceptionUtils.getStackTrace(e));
- }
- }
- }
- }
- dialogRefusal.close();
- this.close();
- });
- dialogRefusal.setCancelButtonListener(event -> {
- dialogRefusal.close();
- });
- getUI().addWindow(dialogRefusal);
- }
- );
- Button cancelRefuseButton = new Button(this.messageSource.getMessage("button.cancel", null, locale));
- cancelRefuseButton.setEnabled(true);
- cancelRefuseButton.addClickListener(clickEvent -> {
- close();
- });
- if (null != claimInfoInnerForm) {
- //balance columns
- //if column num is even (they are initially balanced inside) then append to the left. else to the right
- List<Component> refusalComponents = new ArrayList<>(Arrays.asList(
- refusalDateField,
- endDate,
- refundAmount));
- final int[] left = {countFields(claimInfoInnerForm.getLeftCol())};
- final int[] right = {countFields(claimInfoInnerForm.getRightCol())};
- refusalComponents.forEach(component -> {
- if (left[0] <= right[0]) {
- claimInfoInnerForm.getLeftCol().addComponent(component);
- left[0]++;
- } else {
- claimInfoInnerForm.getRightCol().addComponent(component);
- right[0]++;
- }
- });
- }
- buttonsLayout.addComponent(printRefuseButton);
- buttonsLayout.addComponent(refuseServiceButton);
- buttonsLayout.addComponent(cancelRefuseButton);
- }
- claimInfoLayout.addComponent(buttonsLayout);
- } catch (RestException e) {
- LOG.error(ExceptionUtils.getStackTrace(e));
- }
- infoLayout.addComponent(claimInfoLayout);
- }
- private int countFields(FormLayout layout) {
- int i = 0;
- for (Component c : layout) i++;
- return i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment