From a1abbf532978d5efd403b06cfb4dfdac11de9de8 Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Sun, 21 Jun 2020 15:50:27 +0200 Subject: [PATCH 1/2] Implement Supplier AutoSupplierPayment. closes #100 --- .../cronjob/AutoSupplierPayment.java | 77 +++++++++++++++++++ .../controller/cronjob/CronjobController.java | 27 ++++--- .../controller/cronjob/DashboardCronjob.java | 44 +++++++---- .../accounting/AccountingController.java | 16 ++-- .../entities/booking/BookingReason.java | 8 ++ .../entities/cron/BackgroundJob.java | 1 + 6 files changed, 143 insertions(+), 30 deletions(-) create mode 100644 prototype/src/main/java/org/hso/ecommerce/controller/cronjob/AutoSupplierPayment.java diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/AutoSupplierPayment.java b/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/AutoSupplierPayment.java new file mode 100644 index 0000000..9aca595 --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/AutoSupplierPayment.java @@ -0,0 +1,77 @@ +package org.hso.ecommerce.controller.cronjob; + +import org.hso.ecommerce.action.booking.CreateBookingAction; +import org.hso.ecommerce.entities.booking.Booking; +import org.hso.ecommerce.entities.booking.BookingAccountEntry; +import org.hso.ecommerce.entities.booking.BookingReason; +import org.hso.ecommerce.entities.supplier.Supplier; +import org.hso.ecommerce.repos.booking.BookingAccountEntryRepository; +import org.hso.ecommerce.repos.booking.BookingRepository; +import org.hso.ecommerce.repos.supplier.SupplierRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Calendar; + +@Component +public class AutoSupplierPayment implements ICronjob { + + @Autowired + private SupplierRepository supplierRepository = null; + + @Autowired + private BookingAccountEntryRepository bookingAccountEntryRepository = null; + + @Autowired + private BookingRepository bookingRepository = null; + + @Override + public Calendar nextExecution(Calendar reference) { + if (reference.get(Calendar.HOUR_OF_DAY) >= 10) { + reference.add(Calendar.DAY_OF_MONTH, 1); + } + reference.set(Calendar.HOUR_OF_DAY, 10); + reference.set(Calendar.MINUTE, 0); + reference.set(Calendar.SECOND, 0); + reference.set(Calendar.MILLISECOND, 0); + return reference; + } + + @Override + public Calendar previousExecution(Calendar reference) { + if (reference.get(Calendar.HOUR_OF_DAY) < 10) { + reference.add(Calendar.DAY_OF_MONTH, -1); + } + reference.set(Calendar.HOUR_OF_DAY, 10); + reference.set(Calendar.MINUTE, 0); + reference.set(Calendar.SECOND, 0); + reference.set(Calendar.MILLISECOND, 0); + return reference; + } + + @Override + public void executeAt(Calendar time, CronjobController controller) { + + for (Supplier supplier : supplierRepository.findAll()) { + int debts = bookingAccountEntryRepository + .getBySupplier(supplier.id) + .map(entry -> entry.newSumCent).orElse(0); + + if (debts > 0) { + Booking booking = new CreateBookingAction( + bookingAccountEntryRepository.getBySupplier(supplier.id).orElseGet(() -> BookingAccountEntry.newSupplier(supplier)), + null, + new BookingReason(supplier), + debts + ).finish(); + + bookingRepository.save(booking); + } + } + } + + @Override + public String getDisplayName() { + return "Supplier Auto Payment"; + } +} diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java b/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java index f86703b..441d2c4 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java @@ -22,8 +22,6 @@ import org.hso.ecommerce.repos.shop.CustomerOrderRepository; import org.hso.ecommerce.repos.supplier.ArticleOfferRepository; import org.hso.ecommerce.repos.supplier.SupplierOrderRepository; import org.hso.ecommerce.repos.supplier.SupplierRepository; -import org.hso.ecommerce.repos.user.UserRepository; -import org.hso.ecommerce.repos.warehouse.SlotRepository; import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -236,14 +234,11 @@ class ScheduledCronjob { class CronjobController { private static final Logger log = LoggerFactory.getLogger(CronjobController.class); - private static final Map cronjobs = getCronjobs(); + private Map cronjobs; @Autowired private final BackgroundJobRepository cronjobRepository = null; - @Autowired - final SlotRepository slotRepository = null; - @Autowired final ArticleRepository articleRepository = null; @@ -272,14 +267,26 @@ class CronjobController { final SupplierOrderRepository supplierOrderRepository = null; @Autowired - final UserRepository userRepository = null; + final Reorder reorderJob = null; - private static Map getCronjobs() { + @Autowired + final DashboardCronjob dashboardCronjob = null; + + @Autowired + final AutoSupplierPayment AutoSupplierPaymentJob = null; + + @PostConstruct + public void init() { + cronjobs = getCronjobs(); + } + + private Map getCronjobs() { HashMap map = new HashMap<>(); // Register all existing cronjobs - map.put(BackgroundJob.JOB_REORDER, new Reorder()); - map.put(BackgroundJob.JOB_DASHBOARD, new DashboardCronjob()); + map.put(BackgroundJob.JOB_REORDER, reorderJob); + map.put(BackgroundJob.JOB_DASHBOARD, dashboardCronjob); + map.put(BackgroundJob.JOB_SUPPLIER_AUTO_PAYMENT, AutoSupplierPaymentJob); return Collections.unmodifiableMap(map); } diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/DashboardCronjob.java b/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/DashboardCronjob.java index 8d1e9b7..6da40a5 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/DashboardCronjob.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/DashboardCronjob.java @@ -3,6 +3,11 @@ package org.hso.ecommerce.controller.cronjob; import org.hso.ecommerce.action.warehouse.CalculateWarehouseStatsAction; import org.hso.ecommerce.entities.dashboard.DashboardSummary; import org.hso.ecommerce.entities.warehouse.WarehouseBookingPositionSlotEntry; +import org.hso.ecommerce.repos.shop.CustomerOrderRepository; +import org.hso.ecommerce.repos.user.UserRepository; +import org.hso.ecommerce.repos.warehouse.SlotRepository; +import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.sql.Timestamp; @@ -13,6 +18,18 @@ import java.util.stream.Collectors; @Component public class DashboardCronjob implements ICronjob { + @Autowired + private SlotRepository slotRepository = null; + + @Autowired + private UserRepository userRepository = null; + + @Autowired + private WarehouseBookingPositionSlotEntryRepository warehouseBookingPositionSlotEntryRepository = null; + + @Autowired + private CustomerOrderRepository customerOrderRepository = null; + @Override public Calendar nextExecution(Calendar reference) { reference.add(Calendar.DAY_OF_MONTH, 1); @@ -40,19 +57,19 @@ public class DashboardCronjob implements ICronjob { DashboardSummary dashboardSummary = new DashboardSummary(); - List entries = controller.slotRepository.findAll().stream().map( - s -> controller.warehouseBookingPositionSlotEntryRepository + List entries = slotRepository.findAll().stream().map( + s -> warehouseBookingPositionSlotEntryRepository .getBySlotNum(s.slotNum) .orElseGet(() -> WarehouseBookingPositionSlotEntry.empty(null, s)) ).collect(Collectors.toList()); - CalculateWarehouseStatsAction.WarehouseStats warehouseStats = new CalculateWarehouseStatsAction(entries).finish(); + CalculateWarehouseStatsAction.WarehouseStats warehouseStats = new CalculateWarehouseStatsAction(entries).finish(); dashboardSummary.created = new java.sql.Date(time.getTimeInMillis()); - dashboardSummary.todaysCustomersOrders = nullToZero(getSales(oneDayBefore, time, controller)); - dashboardSummary.todaysNewCustomers = nullToZero(getNewUsers(oneDayBefore, time, controller)); + dashboardSummary.todaysCustomersOrders = nullToZero(getSales(oneDayBefore, time)); + dashboardSummary.todaysNewCustomers = nullToZero(getNewUsers(oneDayBefore, time)); dashboardSummary.todaysWarehouseCapacity = warehouseStats.efficiency; dashboardSummary.currentWarehouseCapacity = warehouseStats.ratioUsedSlots; - dashboardSummary.todaysSalesCent = nullToZero(getTurnover(oneDayBefore, time, controller)); + dashboardSummary.todaysSalesCent = nullToZero(getTurnover(oneDayBefore, time)); controller.dashboardSummaryRepository.save(dashboardSummary); } @@ -62,21 +79,18 @@ public class DashboardCronjob implements ICronjob { return "Dashboard refresh"; } - private Integer getSales (Calendar begin, Calendar end, CronjobController controller) - { - return controller.customerOrderRepository.countOrdersInTimespan( + private Integer getSales(Calendar begin, Calendar end) { + return customerOrderRepository.countOrdersInTimespan( new Timestamp(begin.getTimeInMillis()), new Timestamp(end.getTimeInMillis())); } - private Integer getTurnover (Calendar begin, Calendar end, CronjobController controller) - { - return controller.customerOrderRepository.countTurnoverInTimespan( + private Integer getTurnover(Calendar begin, Calendar end) { + return customerOrderRepository.countTurnoverInTimespan( new Timestamp(begin.getTimeInMillis()), new Timestamp(end.getTimeInMillis())); } - private Integer getNewUsers (Calendar begin, Calendar end, CronjobController controller) - { - return controller.userRepository.countUsersInTimespan( + private Integer getNewUsers(Calendar begin, Calendar end) { + return userRepository.countUsersInTimespan( new Timestamp(begin.getTimeInMillis()), new Timestamp(end.getTimeInMillis())); } diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/AccountingController.java b/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/AccountingController.java index c7f9b75..25a2c7f 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/AccountingController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/intern/accounting/AccountingController.java @@ -1,9 +1,5 @@ package org.hso.ecommerce.controller.intern.accounting; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.List; - import org.hso.ecommerce.entities.booking.Booking; import org.hso.ecommerce.entities.booking.BookingAccountEntry; import org.hso.ecommerce.entities.booking.BookingReason; @@ -13,6 +9,10 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + @Controller public class AccountingController { @@ -153,7 +153,9 @@ public class AccountingController { } else if (reason.customerPayment != null) { return "Bezahlung mit Kreditkarte " + reason.customerPayment.payment.creditCardNumber; } else if (reason.supplierOrder != null) { - return "Lieferanten-Bestellung " + reason.supplierOrder.id; + return "Lieferanten-Bestellung " + reason.supplierOrder.supplier.name; + } else if (reason.supplierPayment != null) { + return "Lieferanten-Zahlung " + reason.supplierPayment.name; } else { return "-"; } @@ -162,6 +164,10 @@ public class AccountingController { private String linkToReference(BookingReason reason) { if (reason.customerOrder != null) { return "/intern/customerOrders/" + reason.customerOrder.id; + } else if (reason.supplierPayment != null) { + return "/intern/suppliers/#q=" + reason.supplierPayment.id; + } else if (reason.supplierOrder != null) { + return "/intern/supplierOrders/#q=" + reason.supplierOrder.id; } else { return null; } diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/booking/BookingReason.java b/prototype/src/main/java/org/hso/ecommerce/entities/booking/BookingReason.java index 8d6572f..1de949c 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/booking/BookingReason.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/booking/BookingReason.java @@ -1,6 +1,7 @@ package org.hso.ecommerce.entities.booking; import org.hso.ecommerce.entities.shop.CustomerOrder; +import org.hso.ecommerce.entities.supplier.Supplier; import org.hso.ecommerce.entities.supplier.SupplierOrder; import javax.persistence.*; @@ -28,6 +29,9 @@ public class BookingReason { @ManyToOne(optional = true) public SupplierOrder supplierOrder; + @ManyToOne(optional = true) + public Supplier supplierPayment; + // Default Constructor is needed for construction by ORM public BookingReason() { } @@ -40,6 +44,10 @@ public class BookingReason { this.customerPayment = customerPayment; } + public BookingReason(Supplier supplierPayment) { + this.supplierPayment = supplierPayment; + } + public BookingReason(SupplierOrder supplierOrder) { this.supplierOrder = supplierOrder; } diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/cron/BackgroundJob.java b/prototype/src/main/java/org/hso/ecommerce/entities/cron/BackgroundJob.java index 7b0e09e..82b19ca 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/cron/BackgroundJob.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/cron/BackgroundJob.java @@ -9,6 +9,7 @@ public class BackgroundJob { public static final String JOB_DASHBOARD = "Dashboard"; public static final String JOB_REORDER = "SupplierOrder"; + public static final String JOB_SUPPLIER_AUTO_PAYMENT = "SupplierAutoPayment"; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) -- 2.44.0 From 7fafd14715b1ea04526c4fdbfc7422d858dc8bbd Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Sun, 21 Jun 2020 19:01:25 +0200 Subject: [PATCH 2/2] Fix variable name --- .../hso/ecommerce/controller/cronjob/CronjobController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java b/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java index 441d2c4..815565c 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java @@ -273,7 +273,7 @@ class CronjobController { final DashboardCronjob dashboardCronjob = null; @Autowired - final AutoSupplierPayment AutoSupplierPaymentJob = null; + final AutoSupplierPayment autoSupplierPaymentJob = null; @PostConstruct public void init() { @@ -286,7 +286,7 @@ class CronjobController { // Register all existing cronjobs map.put(BackgroundJob.JOB_REORDER, reorderJob); map.put(BackgroundJob.JOB_DASHBOARD, dashboardCronjob); - map.put(BackgroundJob.JOB_SUPPLIER_AUTO_PAYMENT, AutoSupplierPaymentJob); + map.put(BackgroundJob.JOB_SUPPLIER_AUTO_PAYMENT, autoSupplierPaymentJob); return Collections.unmodifiableMap(map); } -- 2.44.0