Ladies_Man

claim info window

Mar 31st, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.87 KB | None | 0 0
  1. package ru.pochta.abon.ui.window;
  2.  
  3. import com.vaadin.ui.*;
  4. import org.apache.commons.lang3.exception.ExceptionUtils;
  5. import org.apache.log4j.Logger;
  6. import org.springframework.context.MessageSource;
  7. import ru.pochta.abon.container.Container;
  8. import ru.pochta.abon.library.dto.abonClient.dto.*;
  9. import ru.pochta.abon.library.dto.abonClient.enumeration.ClientTypeEnum;
  10. import ru.pochta.abon.library.dto.abonClient.enumeration.PaymentMethodEnum;
  11. import ru.pochta.abon.library.dto.abonClient.enumeration.PaymentStatusEnum;
  12. import ru.pochta.abon.library.dto.abonClient.enumeration.PaymentTypeEnum;
  13. import ru.pochta.abon.library.dto.abonClient.util.ClaimUtil;
  14. import ru.pochta.abon.library.service.abonClient.ClaimService;
  15. import ru.pochta.abon.library.service.abonClient.ServiceManager;
  16. import ru.pochta.abon.library.service.abonClient.exception.RestException;
  17. import ru.pochta.abon.library.service.integration.tarificator.TarificatorService;
  18. import ru.pochta.abon.library.service.reporting.PrintableForm;
  19. import ru.pochta.abon.library.service.reporting.ReportTypes;
  20. import ru.pochta.abon.library.util.CustomStringUtils;
  21. import ru.pochta.abon.ui.component.ClaimIndividualInfoForm;
  22. import ru.pochta.abon.ui.component.ClaimInfoForm;
  23. import ru.pochta.abon.ui.component.ClaimLegalInfoForm;
  24. import ru.pochta.abon.util.ExceptionNotificationUtil;
  25.  
  26. import java.math.BigDecimal;
  27. import java.time.ZonedDateTime;
  28. import java.time.format.DateTimeFormatter;
  29. import java.util.*;
  30.  
  31. import static ru.pochta.abon.library.service.abonClient.RussianRegionService.RUSSIAN_REGION_REL;
  32. import static ru.pochta.abon.payments.PaymentProcessor.createPayment;
  33. import static ru.pochta.abon.payments.PaymentProcessor.sendToEAS;
  34.  
  35. public class ClaimInfoWindow extends Window {
  36.     private static final Logger LOG = Logger.getLogger(ClaimInfoWindow.class);
  37.     private Locale locale = Locale.forLanguageTag("ru");
  38.  
  39.     private Container container;
  40.     private ServiceManager serviceManager;
  41.     private  MessageSource messageSource;
  42.  
  43.     private ClaimInfoForm claimInfoInnerForm;
  44.  
  45.     public ClaimInfoWindow(Claim claim, MessageSource messageSource,
  46.                            ServiceManager serviceManager, Container container, boolean isShowClaimInfoButtons, PrintableForm printableForm) {
  47.         super();
  48.         this.container = container;
  49.         this.serviceManager = serviceManager;
  50.         this.messageSource = messageSource;
  51.  
  52.  
  53.         setCaption(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.title", new Object[]{claim.getEntityId()}, locale));
  54.  
  55.         center();
  56.         setModal(true);
  57.         setResizable(false);
  58.  
  59.         VerticalLayout infoLayout = new VerticalLayout();
  60.         infoLayout.setSizeUndefined();
  61.         infoLayout.setMargin(true);
  62.  
  63.         this.setContent(infoLayout);
  64.  
  65.  
  66.         VerticalLayout claimInfoLayout = new VerticalLayout();
  67.  
  68.         try {
  69.  
  70.             Claim claimFull = this.serviceManager.getClaimService().getOneByLink(claim.getLink(ClaimService.SELF_LINK));
  71.  
  72.             if (claimFull != null && claim.getClient() != null) {
  73.                 if (claimFull.getClient().getClientType().getEntityId().equals(ClientTypeEnum.ORGANIZATION.getId())) {
  74.                     claimInfoInnerForm = new ClaimLegalInfoForm(container, serviceManager, messageSource, claimFull);
  75.                 } else {
  76.                     claimInfoInnerForm = new ClaimIndividualInfoForm(container, serviceManager, messageSource, claimFull);
  77.                 }
  78.             }
  79.  
  80.             claimInfoLayout.addComponent(null != claimInfoInnerForm ? claimInfoInnerForm : new FormLayout());
  81.  
  82.             isShowClaimInfoButtons = false;
  83.  
  84.             HorizontalLayout buttonsLayout = new HorizontalLayout();
  85.             buttonsLayout.setSpacing(true);
  86.  
  87.             if (isShowClaimInfoButtons) {
  88.                 Button printClaim = new Button((this.messageSource.getMessage("button.printClaim", null, locale)));
  89.                 printClaim.addClickListener(click -> {
  90.                     printableForm.print(claimFull, ReportTypes.CLAIM);
  91.                 });
  92.                 buttonsLayout.addComponent(printClaim);
  93.  
  94.                 Button printInvoice = new Button(this.messageSource.getMessage("button.printInvoice", null, locale));
  95.                 printInvoice.addClickListener(clickEvent -> {
  96.                     printableForm.print(claimFull, ReportTypes.PAYMENT_BILL);
  97.                         }
  98.                 );
  99.                 buttonsLayout.addComponent(printInvoice);
  100.             } else {
  101.                 setCaption(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refusalTitle", new Object[]{claimFull.getEntityId()}, locale));
  102.  
  103.                 ZonedDateTime refusalDate = ZonedDateTime.
  104.                         now().
  105.                         withZoneSameInstant(container.getZoneId());
  106.  
  107.                 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
  108.  
  109.                 TextField refusalDateField = new TextField(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refuseDate", null, locale));
  110.                 refusalDateField.setValue(refusalDate.format(formatter));
  111.                 refusalDateField.setEnabled(false);
  112.  
  113.                 TextField endDate = new TextField(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.terminationDate", null, locale));
  114.                 ZonedDateTime endRefusalDate = ClaimUtil.getEndRefusalDate(container.getZoneId());
  115.                 endDate.setValue(
  116.                         endRefusalDate.format(formatter));
  117.                 endDate.setEnabled(false);
  118.  
  119.                 TextField refundAmount = new TextField(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refundAmount", null, locale));
  120.  
  121.                 boolean isProlongate = null != claimFull.getParent();
  122.                 BigDecimal refundValue = null;
  123.                 try {
  124.                     org.springframework.hateoas.Link link = container.getEmployee().getCurrentPostOffice().getLink(RUSSIAN_REGION_REL);
  125.                     RussianRegion region = serviceManager.getRussianRegionService().getOneByLink(link);
  126.  
  127.                     refundValue = TarificatorService.getClaimRefusalSum(
  128.                             claimFull,
  129.                             region.getEntityId(),
  130.                             isProlongate);
  131.  
  132.                 } catch (Exception e) {
  133.                     LOG.error(ExceptionUtils.getStackTrace(e));
  134.                     Notification.show(
  135.                             this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refundAmountError", null, getLocale()),
  136.                             Notification.Type.TRAY_NOTIFICATION);
  137.                 }
  138.                 refundAmount.setValue(CustomStringUtils.formatBigDecimal(refundValue));
  139.                 refundAmount.setEnabled(false);
  140.  
  141.  
  142.                 Button printRefuseButton = new Button(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.printRefuseButton", null, locale));
  143.                 printRefuseButton.setEnabled(true);
  144.                 printRefuseButton.addClickListener(clickEvent -> {
  145.                     close();
  146.                 });
  147.  
  148.  
  149.                 Button refuseServiceButton = new Button(this.messageSource.getMessage("claimsTab.ClaimInfoWindow.completeRefuseService", null, locale));
  150.                 refuseServiceButton.setEnabled(true);
  151.                 refuseServiceButton.addClickListener(clickEvent -> {
  152.                             ConfirmationDialog dialogRefusal = new ConfirmationDialog(
  153.                                     this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refusalTitle", new Object[]{claimFull.getEntityId()}, locale),
  154.                                     this.messageSource.getMessage("claimsTab.ClaimInfoWindow.reallyRefuse", null, locale),
  155.                                     this.messageSource.getMessage("button.Ok", null, locale),
  156.                                     this.messageSource.getMessage("button.cancel", null, locale)
  157.                             );
  158.                             dialogRefusal.setClosable(false);
  159.  
  160.                             dialogRefusal.setOkButtonListener(event -> {
  161.                                 // First of all we check for the current claim if there is
  162.                                 // no payment with negative amount and status Paid or Awaiting Confirmation
  163.                                 List<Payment> payments = serviceManager.getPaymentService()
  164.                                         .findByClaim(claim);
  165.                                 Optional<Payment> failedPayment = payments
  166.                                         .stream()
  167.                                         .filter(payment -> (payment.getAmount().compareTo(BigDecimal.ZERO) < 0) &&
  168.                                                 (payment.getPaymentStatus().getEntityId().equals(PaymentStatusEnum.PAID.getId()) ||
  169.                                                         payment.getPaymentStatus().getEntityId().equals(PaymentStatusEnum.AWAITING_CONFIRMATION.getId())))
  170.                                         .findFirst();
  171.                                 if (failedPayment.isPresent()) {
  172.                                     Notification notification = ExceptionNotificationUtil
  173.                                             .getNotification(new RestException(
  174.                                                     this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refundPaymentAlreadyCreated", new Object[]{failedPayment.get().getEntityId(), failedPayment.get().getPaymentStatus().getStatus()}, locale)
  175.                                             ), this.messageSource, locale);
  176.                                     LOG.debug("found a refund payment [" + failedPayment.get().getEntityId() + "] for the claim [" + claim.getEntityId() + "]");
  177.                                     dialogRefusal.close();
  178.                                     notification.show(getUI().getPage());
  179.                                     return;
  180.                                 }
  181.  
  182.  
  183.                                 PaymentType paymentType = serviceManager.getPaymentService().getLastClaimPaymentType(claimFull, PaymentStatusEnum.PAID);
  184.                                 PaymentMethod paymentMethod = null;
  185.                                 if (container.getLoggedViaEASOPS()) {
  186.                                     paymentMethod = container.getPaymentMethods().get((int) (PaymentMethodEnum.CASH.getId() - 1));
  187.                                 } else {
  188.                                     container.getPaymentMethods().get((int) (PaymentMethodEnum.BILL.getId() - 1));
  189.                                 }
  190.                                 if (paymentType != null) {
  191.                                     Payment payment = null;
  192.                                     try {
  193.                                         payment = createPayment(
  194.                                                 claimFull,
  195.                                                 paymentMethod,
  196.                                                 paymentType,
  197.                                                 true,
  198.                                                 container, serviceManager);
  199.                                     } catch (Exception e) {
  200.                                         LOG.error(ExceptionUtils.getStackTrace(e));
  201.                                         Notification.show(
  202.                                                 this.messageSource.getMessage("claimsTab.ClaimInfoWindow.refundAmountError", null, getLocale()),
  203.                                                 Notification.Type.TRAY_NOTIFICATION);
  204.                                     }
  205.                                     if (payment != null && payment.getEntityId() != null) {
  206.                                         claimFull.setRefusalDate(endRefusalDate);
  207.                                         claimFull.setUpdateDate(refusalDate);
  208.                                         ClaimService claimService = serviceManager.getClaimService();
  209.                                         try {
  210.                                             claimService.update(claimFull);
  211.                                         } catch (RestException e) {
  212.                                             LOG.error(ExceptionUtils.getStackTrace(e));
  213.                                         }
  214.                                         PaymentType easPaymentType = container.getPaymentType(PaymentTypeEnum.EAS);
  215.                                         if (paymentType.equals(easPaymentType)) {
  216.                                             sendToEAS(claimFull, payment);
  217.                                         } else {
  218.                                             payment.setPaymentStatus(container.getPaymentStatus(PaymentStatusEnum.PAID));
  219.                                             payment.setResolutionDate(ZonedDateTime.now().
  220.                                                     withZoneSameInstant(container.getZoneId()));
  221.                                             payment.setPaymentMethod(container.getPaymentMethod(PaymentMethodEnum.CASHLESS));
  222.                                             payment.setDescription("Отказ безнал");
  223.                                             payment.setReceiptNumber("Отказ");
  224.                                             try {
  225.                                                 serviceManager.getPaymentService().update(payment);
  226.                                             } catch (RestException e) {
  227.                                                 LOG.error(ExceptionUtils.getStackTrace(e));
  228.                                             }
  229.                                         }
  230.                                     }
  231.                                 }
  232.                                 dialogRefusal.close();
  233.                                 this.close();
  234.                             });
  235.  
  236.                             dialogRefusal.setCancelButtonListener(event -> {
  237.                                 dialogRefusal.close();
  238.                             });
  239.                             getUI().addWindow(dialogRefusal);
  240.                         }
  241.  
  242.                 );
  243.  
  244.                 Button cancelRefuseButton = new Button(this.messageSource.getMessage("button.cancel", null, locale));
  245.                 cancelRefuseButton.setEnabled(true);
  246.                 cancelRefuseButton.addClickListener(clickEvent -> {
  247.                     close();
  248.                 });
  249.  
  250.  
  251.                 if (null != claimInfoInnerForm) {
  252.                     //balance columns
  253.                     //if column num is even (they are initially balanced inside) then append to the left. else to the right
  254.                     List<Component> refusalComponents = new ArrayList<>(Arrays.asList(
  255.                             refusalDateField,
  256.                             endDate,
  257.                             refundAmount));
  258.  
  259.                     final int[] left = {countFields(claimInfoInnerForm.getLeftCol())};
  260.                     final int[] right = {countFields(claimInfoInnerForm.getRightCol())};
  261.  
  262.                     refusalComponents.forEach(component -> {
  263.                         if (left[0] <= right[0]) {
  264.                             claimInfoInnerForm.getLeftCol().addComponent(component);
  265.                             left[0]++;
  266.                         } else {
  267.                             claimInfoInnerForm.getRightCol().addComponent(component);
  268.                             right[0]++;
  269.                         }
  270.                     });
  271.                 }
  272.  
  273.                 buttonsLayout.addComponent(printRefuseButton);
  274.                 buttonsLayout.addComponent(refuseServiceButton);
  275.                 buttonsLayout.addComponent(cancelRefuseButton);
  276.  
  277.             }
  278.             claimInfoLayout.addComponent(buttonsLayout);
  279.  
  280.         } catch (RestException e) {
  281.             LOG.error(ExceptionUtils.getStackTrace(e));
  282.         }
  283.  
  284.         infoLayout.addComponent(claimInfoLayout);
  285.     }
  286.  
  287.     private int countFields(FormLayout layout) {
  288.         int i = 0;
  289.         for (Component c : layout) i++;
  290.         return i;
  291.     }
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment