Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.85 KB | None | 0 0
  1. package org.gopos.erp.application.commandHandler.handler.sale;
  2.  
  3. import appStarter.security.domain.user.User;
  4. import library.commandHandler.CommandHandlerAnnotation;
  5. import org.gopos.client.domain.client.ClientVenue;
  6. import org.gopos.client.domain.client.repository.ClientVenueRepository;
  7. import org.gopos.erp.application.commandHandler.command.sale.GenerateSaleCommand;
  8. import org.gopos.erp.application.services.ReportService;
  9. import org.gopos.erp.domain.employee.Employee;
  10. import org.gopos.erp.domain.employee.repository.EmployeeRepository;
  11. import org.gopos.erp.domain.permission.VenueRole;
  12. import org.gopos.erp.domain.permission.repository.VenueRoleRepository;
  13. import org.gopos.erp.domain.product.Product;
  14. import org.gopos.erp.domain.product.ProductVariant;
  15. import org.gopos.erp.domain.product.repository.ProductRepository;
  16. import org.gopos.erp.domain.product.repository.ProductVariantRepository;
  17. import org.gopos.erp.domain.room.PointOfSale;
  18. import org.gopos.erp.domain.room.repository.PointOfSaleRepository;
  19. import org.gopos.erp.domain.sale.bill.*;
  20. import org.gopos.erp.domain.sale.bill.repository.BillRepository;
  21. import org.gopos.erp.domain.sale.report.ReportDrawer;
  22. import org.gopos.erp.domain.sale.report.ReportShiftWork;
  23. import org.gopos.erp.domain.sale.report.ReportStatus;
  24. import org.gopos.erp.domain.sale.report.repository.ReportRepository;
  25. import org.gopos.erp.domain.sale.waiter.Waiter;
  26. import org.gopos.erp.domain.settings.payment.PaymentMethod;
  27. import org.gopos.erp.domain.settings.payment.repository.PaymentMethodRepository;
  28. import org.gopos.erp.domain.venue.Venue;
  29. import org.gopos.erp.domain.venue.VenueTerminal;
  30. import org.gopos.erp.domain.venue.repository.VenueTerminalRepository;
  31. import org.gopos.messaging.venue.BaseEventCommandHandler;
  32. import org.gopos.share.domain.common.Money;
  33. import org.gopos.share.domain.common.TaxCopy;
  34. import org.gopos.share.domain.common.Terminal;
  35. import org.springframework.beans.factory.annotation.Autowired;
  36. import org.springframework.transaction.TransactionStatus;
  37. import org.springframework.transaction.support.TransactionCallbackWithoutResult;
  38. import org.springframework.transaction.support.TransactionTemplate;
  39.  
  40. import java.math.BigDecimal;
  41. import java.time.LocalDate;
  42. import java.time.LocalDateTime;
  43. import java.util.*;
  44. import java.util.stream.Collectors;
  45.  
  46. import static java.time.temporal.ChronoUnit.DAYS;
  47.  
  48. @CommandHandlerAnnotation
  49. public class GenerateSaleHandleer extends BaseEventCommandHandler<GenerateSaleCommand, Void> {
  50.  
  51. private static final int MAXIMUM_PRODUCTS_AMOUNT_PER_BILL = 7;
  52. private static final int MINIMUM_BILLS_PER_DAY = 50;
  53. private static final int MAXIMUM_BILLS_PER_DAY = 100;
  54.  
  55. @Autowired
  56. private ClientVenueRepository clientVenueRepository;
  57.  
  58. @Autowired
  59. private EmployeeRepository employeeRepository;
  60.  
  61. @Autowired
  62. private ProductVariantRepository productVariantRepository;
  63.  
  64. @Autowired
  65. private ProductRepository productRepository;
  66.  
  67. @Autowired
  68. private VenueRoleRepository venueRoleRepository;
  69.  
  70. @Autowired
  71. private VenueTerminalRepository venueTerminalRepository;
  72.  
  73. @Autowired
  74. private PointOfSaleRepository pointOfSaleRepository;
  75.  
  76. @Autowired
  77. private PaymentMethodRepository paymentMethodRepository;
  78.  
  79. @Autowired
  80. private TransactionTemplate transactionTemplate;
  81.  
  82. @Autowired
  83. private BillRepository billRepository;
  84.  
  85. @Autowired
  86. private ReportRepository reportRepository;
  87.  
  88. @Autowired
  89. private ReportService reportService;
  90.  
  91. @Override
  92. public Void handle(GenerateSaleCommand command) {
  93. Venue venue = command.getVenue();
  94. LocalDate dateFrom = command.getFromDate();
  95. LocalDate dateTo = command.getToDate();
  96. long daysBetween = DAYS.between(dateFrom, dateTo);
  97.  
  98. Terminal terminal = venueTerminalCreator(venue);
  99.  
  100. transactionTemplate.execute(new TransactionCallbackWithoutResult() {
  101. @Override
  102. protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
  103. createBills(venue, daysBetween, dateFrom, terminal);
  104. }
  105. });
  106. return null;
  107. }
  108.  
  109. private void createBills(Venue venue, long daysAmount, LocalDate dateFrom, Terminal terminal) {
  110. List<Waiter> waiters = getWaiters(venue);
  111. Waiter waiter = waiters.get(randomInt(0, waiters.size()));
  112. LocalDateTime localDateTime = randomizeOpeningDateTimeHours(dateFrom.atStartOfDay().plusHours(9));
  113. List<Bill> bills = new ArrayList<>();
  114. for(int i=0; i<daysAmount; ++i){
  115. ReportShiftWork reportShiftWork = new ReportShiftWork(venue, UUID.randomUUID().toString());
  116. reportRepository.save(reportShiftWork);
  117.  
  118. ReportDrawer reportDrawer = new ReportDrawer();
  119. reportDrawer.setReportShiftWork(reportShiftWork);
  120. reportRepository.save(reportDrawer);
  121. int billsPerDayAmount = randomInt(MINIMUM_BILLS_PER_DAY, MAXIMUM_BILLS_PER_DAY);
  122. for(int j=0; j<billsPerDayAmount; ++j){
  123. LocalDateTime billTime = randomizeBillHours(localDateTime.plusDays(i));
  124. Bill bill = billCreator(venue, billTime, waiter, terminal, reportDrawer);
  125. bills.add(bill);
  126. billRepository.save(bill);
  127. }
  128. Money income = bills.stream().map(Bill::getTotalPrice).reduce(new Money(0), Money::add);
  129. reportDrawer.update(new Money(0), income, "---", terminal, LocalDateTime.now(), LocalDateTime.now().plusSeconds(1), LocalDateTime.now(), ReportStatus.OPENED, waiter);
  130. reportShiftWork.close(income);
  131. reportRepository.save(reportDrawer);
  132. reportRepository.save(reportShiftWork);
  133. bills.clear();
  134. }
  135. }
  136.  
  137. private Bill billCreator(Venue venue, LocalDateTime billTime, Waiter waiter, Terminal terminal, ReportDrawer reportDrawer){
  138. BillPointOfSale billPointOfSale = getRandomBillPointOfSale(venue);
  139. Bill bill = new Bill(venue, UUID.randomUUID().toString());
  140. List<BillLine> billLines = getBillLines(bill, venue, waiter, billPointOfSale);
  141. ClientVenue clientVenue = getRandomClient(venue);
  142. BillClient billClient = getRandomBillClient(clientVenue);
  143. LocalDateTime closeDate = billTime.plusSeconds(20);
  144. Payment payment = createPayment(venue, waiter, closeDate, billLines);
  145. bill.update("---", "...", billTime, closeDate, closeDate, new Money(0), new Money(0), new Money(0), new Money(0), terminal, terminal.getUniqueId(), terminal.getUniqueId(), false, BillStatus.OPENED, billClient, null, waiter, null, waiter, true, 1, billPointOfSale, payment, reportDrawer.getReportShiftWork().getUniqueId(), reportDrawer.getUniqueId(), 1, billLines, Collections.emptyList());
  146. bill.calculateTotalPrices();
  147. Money money = bill.getBillLines().stream().map(BaseBillLine::getDefaultCost).reduce(new Money(0), Money::add);
  148. bill.updateCost(money, closeDate);
  149. bill.transferBill(terminal);
  150. return bill;
  151. }
  152.  
  153. private Payment createPayment(Venue venue, Waiter waiter, LocalDateTime closeDate, List<BillLine> billLines) {
  154. PaymentDetailMethod paymentDetailMethod = getPaymentDetailMethod(venue);
  155. Money totalCost = billLines.stream().map(BillLine::getTotalPrice).reduce(new Money(0), Money::add);
  156. Payment payment = new Payment(UUID.randomUUID().toString(), totalCost, new Money(0), closeDate, waiter);
  157. PaymentDetail paymentDetail = new PaymentDetail(payment, totalCost, totalCost, new Money(0), new Money(0), null, closeDate, paymentDetailMethod, PaymentDetailStatus.PAID);
  158. payment.addPaymentDetail(paymentDetail);
  159. return payment;
  160. }
  161.  
  162. private List<BillLine> getBillLines(Bill bill, Venue venue, Waiter waiter, BillPointOfSale billPointOfSale) {
  163. List<BillLine> billLines = new ArrayList<>();
  164. List<ProductVariant> productVariants = getRandomProducts(venue);
  165. BillLineDirection billLineDirection = getBillLineDirection(billPointOfSale);
  166. for(ProductVariant productVariant: productVariants){
  167. TaxCopy taxCopy = TaxCopy.build(productVariant.getProduct().getTax());
  168. BillLineProduct billLineProduct = new BillLineProduct(productVariant.getProduct().getUniqueId(), productVariant.getProduct().getProductCategory().getUniqueId(), productVariant.getUniqueId(), productVariant.getFullName(), productVariant.getName(), productVariant.getProduct().getProductType());
  169. BillLine billLine = new BillLine(UUID.randomUUID().toString(), bill, productVariant.getFullName(), null, new BigDecimal("1"), productVariant.getPrice(), productVariant.getPrice(), null, productVariant.getProduct().getDefaultPrice(), productVariant.getPrice(), productVariant.getPrice(), 0.0, 0.0, LocalDateTime.now(), 0, billLineProduct, billLineDirection, taxCopy, waiter, 0,0, null);
  170. billLines.add(billLine);
  171. }
  172. return billLines;
  173. }
  174.  
  175. private PaymentDetailMethod getPaymentDetailMethod(Venue venue){
  176. PaymentMethod paymentMethod = getRandomPaymentMethod(venue);
  177. PaymentDetailMethod paymentDetailMethod = new PaymentDetailMethod(paymentMethod.getUniqueId(), paymentMethod.isPrintReceipt(), paymentMethod.getName(), paymentMethod.getPaymentOutflowType(), paymentMethod.getPaymentReceiptType());
  178. return paymentDetailMethod;
  179. }
  180.  
  181. private PaymentMethod getRandomPaymentMethod(Venue venue){
  182. List<PaymentMethod> paymentMethodList = paymentMethodRepository.findByVenueId(venue.getId());
  183. PaymentMethod paymentMethod = paymentMethodList.get(randomInt(0, paymentMethodList.size()));
  184. return paymentMethod;
  185. }
  186.  
  187. private BillLineDirection getBillLineDirection(BillPointOfSale billPointOfSale){
  188. BillLineDirection billLineDirection = new BillLineDirection(billPointOfSale.getName(), billPointOfSale.getPointOfSaleUniqueId());
  189. return billLineDirection;
  190. }
  191.  
  192. private BillPointOfSale getRandomBillPointOfSale(Venue venue){
  193. List<PointOfSale> pointOfSales = pointOfSaleRepository.findByVenueId(venue.getId());
  194. PointOfSale pointOfSale = pointOfSales.get(randomInt(0, pointOfSales.size()));
  195. BillPointOfSale billPointOfSale = new BillPointOfSale(pointOfSale.getName(), pointOfSale.getUniqueId());
  196. return billPointOfSale;
  197. }
  198.  
  199. private Terminal venueTerminalCreator(Venue venue){
  200. VenueTerminal venueTerminal = new VenueTerminal(venue, UUID.randomUUID().toString(), 1, "Galaxy S5", "1.2.33.60492118");
  201. Terminal terminal = new Terminal(venueTerminal.getTerminalUniqueId(), venueTerminal.getName());
  202. venueTerminalRepository.save(venueTerminal);
  203. return terminal;
  204. }
  205.  
  206. private List<ProductVariant> getRandomProducts(Venue venue) {
  207. List<Product> products = productRepository.findByVenueId(venue.getId());
  208. List<String> uniqueIds = products.stream()
  209. .flatMap(x -> x.getProductVariants().stream().map(ProductVariant::getUniqueId))
  210. .collect(Collectors.toList());
  211. List<ProductVariant> productVariants = productVariantRepository.findByVenueIdAndUniqueIds(venue.getId(), uniqueIds);
  212. Collections.shuffle(productVariants);
  213. List<ProductVariant> randomProducts = productVariants.stream().limit(randomInt(2, MAXIMUM_PRODUCTS_AMOUNT_PER_BILL)).collect(Collectors.toList());
  214. return randomProducts;
  215. }
  216.  
  217. private List<Waiter> getWaiters(Venue venue){
  218. // VenueRole venueRole = venueRoleRepository.findByUniqueIdAndVenueId("WAITER", venue.getId());
  219. List<Employee> employees = employeeRepository.findByVenueId(venue.getId());
  220. List<Waiter> waiters = new ArrayList<>();
  221. for(Employee employee: employees){
  222. waiters.add(new Waiter(employee.getUniqueId(), employee.getName(), employee.getSurname()));
  223. }
  224. return waiters;
  225. }
  226.  
  227. private BillClient getRandomBillClient(ClientVenue clientVenue){
  228. BillClient billClient = new BillClient(clientVenue.getName(), clientVenue.getUniqueId(), clientVenue.getClientGroup().getUniqueId(), "");
  229. return billClient;
  230. }
  231.  
  232. private ClientVenue getRandomClient(Venue venue){
  233. List<ClientVenue> clientVenues = clientVenueRepository.findByVenueId(venue.getId(), LocalDateTime.MIN);
  234. return clientVenues.get(randomInt(0, clientVenues.size()));
  235. }
  236.  
  237. private LocalDateTime randomizeOpeningDateTimeHours(LocalDateTime localDateTime){
  238. return localDateTime.plusHours(randomInt(-1, 1)).plusMinutes(randomInt(0, 59));
  239. }
  240.  
  241. private LocalDateTime randomizeBillHours(LocalDateTime localDateTime){
  242. return localDateTime.plusHours(randomInt(0, 13)).plusMinutes(randomInt(0, 59)).plusSeconds(randomInt(0, 59));
  243. }
  244.  
  245. private static int randomInt(int low, int high) {
  246. return (int) (Math.random() * (high - low)) + low;
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement