Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.gopos.erp.application.commandHandler.handler.sale;
- import appStarter.security.domain.user.User;
- import library.commandHandler.CommandHandlerAnnotation;
- import org.gopos.client.domain.client.ClientVenue;
- import org.gopos.client.domain.client.repository.ClientVenueRepository;
- import org.gopos.erp.application.commandHandler.command.sale.GenerateSaleCommand;
- import org.gopos.erp.application.services.ReportService;
- import org.gopos.erp.domain.employee.Employee;
- import org.gopos.erp.domain.employee.repository.EmployeeRepository;
- import org.gopos.erp.domain.permission.VenueRole;
- import org.gopos.erp.domain.permission.repository.VenueRoleRepository;
- import org.gopos.erp.domain.product.Product;
- import org.gopos.erp.domain.product.ProductVariant;
- import org.gopos.erp.domain.product.repository.ProductRepository;
- import org.gopos.erp.domain.product.repository.ProductVariantRepository;
- import org.gopos.erp.domain.room.PointOfSale;
- import org.gopos.erp.domain.room.repository.PointOfSaleRepository;
- import org.gopos.erp.domain.sale.bill.*;
- import org.gopos.erp.domain.sale.bill.repository.BillRepository;
- import org.gopos.erp.domain.sale.report.ReportDrawer;
- import org.gopos.erp.domain.sale.report.ReportShiftWork;
- import org.gopos.erp.domain.sale.report.ReportStatus;
- import org.gopos.erp.domain.sale.report.repository.ReportRepository;
- import org.gopos.erp.domain.sale.waiter.Waiter;
- import org.gopos.erp.domain.settings.payment.PaymentMethod;
- import org.gopos.erp.domain.settings.payment.repository.PaymentMethodRepository;
- import org.gopos.erp.domain.venue.Venue;
- import org.gopos.erp.domain.venue.VenueTerminal;
- import org.gopos.erp.domain.venue.repository.VenueTerminalRepository;
- import org.gopos.messaging.venue.BaseEventCommandHandler;
- import org.gopos.share.domain.common.Money;
- import org.gopos.share.domain.common.TaxCopy;
- import org.gopos.share.domain.common.Terminal;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.transaction.TransactionStatus;
- import org.springframework.transaction.support.TransactionCallbackWithoutResult;
- import org.springframework.transaction.support.TransactionTemplate;
- import java.math.BigDecimal;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.stream.Collectors;
- import static java.time.temporal.ChronoUnit.DAYS;
- @CommandHandlerAnnotation
- public class GenerateSaleHandleer extends BaseEventCommandHandler<GenerateSaleCommand, Void> {
- private static final int MAXIMUM_PRODUCTS_AMOUNT_PER_BILL = 7;
- private static final int MINIMUM_BILLS_PER_DAY = 50;
- private static final int MAXIMUM_BILLS_PER_DAY = 100;
- @Autowired
- private ClientVenueRepository clientVenueRepository;
- @Autowired
- private EmployeeRepository employeeRepository;
- @Autowired
- private ProductVariantRepository productVariantRepository;
- @Autowired
- private ProductRepository productRepository;
- @Autowired
- private VenueRoleRepository venueRoleRepository;
- @Autowired
- private VenueTerminalRepository venueTerminalRepository;
- @Autowired
- private PointOfSaleRepository pointOfSaleRepository;
- @Autowired
- private PaymentMethodRepository paymentMethodRepository;
- @Autowired
- private TransactionTemplate transactionTemplate;
- @Autowired
- private BillRepository billRepository;
- @Autowired
- private ReportRepository reportRepository;
- @Autowired
- private ReportService reportService;
- @Override
- public Void handle(GenerateSaleCommand command) {
- Venue venue = command.getVenue();
- LocalDate dateFrom = command.getFromDate();
- LocalDate dateTo = command.getToDate();
- long daysBetween = DAYS.between(dateFrom, dateTo);
- Terminal terminal = venueTerminalCreator(venue);
- transactionTemplate.execute(new TransactionCallbackWithoutResult() {
- @Override
- protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
- createBills(venue, daysBetween, dateFrom, terminal);
- }
- });
- return null;
- }
- private void createBills(Venue venue, long daysAmount, LocalDate dateFrom, Terminal terminal) {
- List<Waiter> waiters = getWaiters(venue);
- Waiter waiter = waiters.get(randomInt(0, waiters.size()));
- LocalDateTime localDateTime = randomizeOpeningDateTimeHours(dateFrom.atStartOfDay().plusHours(9));
- List<Bill> bills = new ArrayList<>();
- for(int i=0; i<daysAmount; ++i){
- ReportShiftWork reportShiftWork = new ReportShiftWork(venue, UUID.randomUUID().toString());
- reportRepository.save(reportShiftWork);
- ReportDrawer reportDrawer = new ReportDrawer();
- reportDrawer.setReportShiftWork(reportShiftWork);
- reportRepository.save(reportDrawer);
- int billsPerDayAmount = randomInt(MINIMUM_BILLS_PER_DAY, MAXIMUM_BILLS_PER_DAY);
- for(int j=0; j<billsPerDayAmount; ++j){
- LocalDateTime billTime = randomizeBillHours(localDateTime.plusDays(i));
- Bill bill = billCreator(venue, billTime, waiter, terminal, reportDrawer);
- bills.add(bill);
- billRepository.save(bill);
- }
- Money income = bills.stream().map(Bill::getTotalPrice).reduce(new Money(0), Money::add);
- reportDrawer.update(new Money(0), income, "---", terminal, LocalDateTime.now(), LocalDateTime.now().plusSeconds(1), LocalDateTime.now(), ReportStatus.OPENED, waiter);
- reportShiftWork.close(income);
- reportRepository.save(reportDrawer);
- reportRepository.save(reportShiftWork);
- bills.clear();
- }
- }
- private Bill billCreator(Venue venue, LocalDateTime billTime, Waiter waiter, Terminal terminal, ReportDrawer reportDrawer){
- BillPointOfSale billPointOfSale = getRandomBillPointOfSale(venue);
- Bill bill = new Bill(venue, UUID.randomUUID().toString());
- List<BillLine> billLines = getBillLines(bill, venue, waiter, billPointOfSale);
- ClientVenue clientVenue = getRandomClient(venue);
- BillClient billClient = getRandomBillClient(clientVenue);
- LocalDateTime closeDate = billTime.plusSeconds(20);
- Payment payment = createPayment(venue, waiter, closeDate, billLines);
- 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());
- bill.calculateTotalPrices();
- Money money = bill.getBillLines().stream().map(BaseBillLine::getDefaultCost).reduce(new Money(0), Money::add);
- bill.updateCost(money, closeDate);
- bill.transferBill(terminal);
- return bill;
- }
- private Payment createPayment(Venue venue, Waiter waiter, LocalDateTime closeDate, List<BillLine> billLines) {
- PaymentDetailMethod paymentDetailMethod = getPaymentDetailMethod(venue);
- Money totalCost = billLines.stream().map(BillLine::getTotalPrice).reduce(new Money(0), Money::add);
- Payment payment = new Payment(UUID.randomUUID().toString(), totalCost, new Money(0), closeDate, waiter);
- PaymentDetail paymentDetail = new PaymentDetail(payment, totalCost, totalCost, new Money(0), new Money(0), null, closeDate, paymentDetailMethod, PaymentDetailStatus.PAID);
- payment.addPaymentDetail(paymentDetail);
- return payment;
- }
- private List<BillLine> getBillLines(Bill bill, Venue venue, Waiter waiter, BillPointOfSale billPointOfSale) {
- List<BillLine> billLines = new ArrayList<>();
- List<ProductVariant> productVariants = getRandomProducts(venue);
- BillLineDirection billLineDirection = getBillLineDirection(billPointOfSale);
- for(ProductVariant productVariant: productVariants){
- TaxCopy taxCopy = TaxCopy.build(productVariant.getProduct().getTax());
- BillLineProduct billLineProduct = new BillLineProduct(productVariant.getProduct().getUniqueId(), productVariant.getProduct().getProductCategory().getUniqueId(), productVariant.getUniqueId(), productVariant.getFullName(), productVariant.getName(), productVariant.getProduct().getProductType());
- 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);
- billLines.add(billLine);
- }
- return billLines;
- }
- private PaymentDetailMethod getPaymentDetailMethod(Venue venue){
- PaymentMethod paymentMethod = getRandomPaymentMethod(venue);
- PaymentDetailMethod paymentDetailMethod = new PaymentDetailMethod(paymentMethod.getUniqueId(), paymentMethod.isPrintReceipt(), paymentMethod.getName(), paymentMethod.getPaymentOutflowType(), paymentMethod.getPaymentReceiptType());
- return paymentDetailMethod;
- }
- private PaymentMethod getRandomPaymentMethod(Venue venue){
- List<PaymentMethod> paymentMethodList = paymentMethodRepository.findByVenueId(venue.getId());
- PaymentMethod paymentMethod = paymentMethodList.get(randomInt(0, paymentMethodList.size()));
- return paymentMethod;
- }
- private BillLineDirection getBillLineDirection(BillPointOfSale billPointOfSale){
- BillLineDirection billLineDirection = new BillLineDirection(billPointOfSale.getName(), billPointOfSale.getPointOfSaleUniqueId());
- return billLineDirection;
- }
- private BillPointOfSale getRandomBillPointOfSale(Venue venue){
- List<PointOfSale> pointOfSales = pointOfSaleRepository.findByVenueId(venue.getId());
- PointOfSale pointOfSale = pointOfSales.get(randomInt(0, pointOfSales.size()));
- BillPointOfSale billPointOfSale = new BillPointOfSale(pointOfSale.getName(), pointOfSale.getUniqueId());
- return billPointOfSale;
- }
- private Terminal venueTerminalCreator(Venue venue){
- VenueTerminal venueTerminal = new VenueTerminal(venue, UUID.randomUUID().toString(), 1, "Galaxy S5", "1.2.33.60492118");
- Terminal terminal = new Terminal(venueTerminal.getTerminalUniqueId(), venueTerminal.getName());
- venueTerminalRepository.save(venueTerminal);
- return terminal;
- }
- private List<ProductVariant> getRandomProducts(Venue venue) {
- List<Product> products = productRepository.findByVenueId(venue.getId());
- List<String> uniqueIds = products.stream()
- .flatMap(x -> x.getProductVariants().stream().map(ProductVariant::getUniqueId))
- .collect(Collectors.toList());
- List<ProductVariant> productVariants = productVariantRepository.findByVenueIdAndUniqueIds(venue.getId(), uniqueIds);
- Collections.shuffle(productVariants);
- List<ProductVariant> randomProducts = productVariants.stream().limit(randomInt(2, MAXIMUM_PRODUCTS_AMOUNT_PER_BILL)).collect(Collectors.toList());
- return randomProducts;
- }
- private List<Waiter> getWaiters(Venue venue){
- // VenueRole venueRole = venueRoleRepository.findByUniqueIdAndVenueId("WAITER", venue.getId());
- List<Employee> employees = employeeRepository.findByVenueId(venue.getId());
- List<Waiter> waiters = new ArrayList<>();
- for(Employee employee: employees){
- waiters.add(new Waiter(employee.getUniqueId(), employee.getName(), employee.getSurname()));
- }
- return waiters;
- }
- private BillClient getRandomBillClient(ClientVenue clientVenue){
- BillClient billClient = new BillClient(clientVenue.getName(), clientVenue.getUniqueId(), clientVenue.getClientGroup().getUniqueId(), "");
- return billClient;
- }
- private ClientVenue getRandomClient(Venue venue){
- List<ClientVenue> clientVenues = clientVenueRepository.findByVenueId(venue.getId(), LocalDateTime.MIN);
- return clientVenues.get(randomInt(0, clientVenues.size()));
- }
- private LocalDateTime randomizeOpeningDateTimeHours(LocalDateTime localDateTime){
- return localDateTime.plusHours(randomInt(-1, 1)).plusMinutes(randomInt(0, 59));
- }
- private LocalDateTime randomizeBillHours(LocalDateTime localDateTime){
- return localDateTime.plusHours(randomInt(0, 13)).plusMinutes(randomInt(0, 59)).plusSeconds(randomInt(0, 59));
- }
- private static int randomInt(int low, int high) {
- return (int) (Math.random() * (high - low)) + low;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement