This repository has been archived on 2020-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
e-commerce/prototype/src/main/java/org/hso/ecommerce/controller/intern/suppliers/SupplierIndexController.java

166 lines
5.2 KiB
Java
Raw Normal View History

2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.intern.suppliers;
2020-04-28 22:41:29 +02:00
2020-05-30 20:16:27 +02:00
import java.text.SimpleDateFormat;
2020-05-30 16:16:14 +02:00
import java.util.ArrayList;
2020-05-30 20:16:27 +02:00
import java.util.Date;
2020-05-30 16:16:14 +02:00
import java.util.List;
2020-06-05 13:10:16 +02:00
import java.util.Optional;
2020-05-30 16:16:14 +02:00
2020-06-05 15:43:46 +02:00
import org.hso.ecommerce.entities.booking.Booking;
2020-06-05 13:10:16 +02:00
import org.hso.ecommerce.entities.booking.BookingAccountEntry;
2020-05-30 16:16:14 +02:00
import org.hso.ecommerce.entities.supplier.Supplier;
2020-05-30 20:16:27 +02:00
import org.hso.ecommerce.entities.supplier.SupplierOrder;
2020-06-05 13:10:16 +02:00
import org.hso.ecommerce.repos.booking.BookingAccountEntryRepository;
2020-06-05 15:43:46 +02:00
import org.hso.ecommerce.repos.booking.BookingRepository;
2020-05-30 20:16:27 +02:00
import org.hso.ecommerce.repos.supplier.SupplierOrderRepository;
2020-05-30 16:16:14 +02:00
import org.hso.ecommerce.repos.supplier.SupplierRepository;
import org.springframework.beans.factory.annotation.Autowired;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller;
2020-05-30 16:16:14 +02:00
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
2020-05-30 16:43:07 +02:00
import org.springframework.web.bind.annotation.PathVariable;
2020-05-30 16:16:14 +02:00
import org.springframework.web.bind.annotation.RequestMapping;
2020-04-28 22:41:29 +02:00
@Controller
2020-05-30 16:16:14 +02:00
@RequestMapping("/intern/")
2020-05-01 10:48:12 +02:00
public class SupplierIndexController {
2020-05-30 16:16:14 +02:00
@Autowired
private final SupplierRepository supplierRepository = null;
2020-05-30 20:16:27 +02:00
@Autowired
private final SupplierOrderRepository supplierOrderRepository = null;
2020-06-05 13:10:16 +02:00
@Autowired
private final BookingAccountEntryRepository bookingAccountEntryRepository = null;
2020-06-05 15:43:46 +02:00
@Autowired
private final BookingRepository bookingRepository = null;
2020-05-30 16:16:14 +02:00
@GetMapping("suppliers")
public String listSuppliers(Model model) {
List<UImodelSuppliers> totals = new ArrayList<UImodelSuppliers>();
for (Supplier supplier : supplierRepository.findAll()) {
UImodelSuppliers tmp = new UImodelSuppliers(supplier.id, supplier.name);
totals.add(tmp);
}
model.addAttribute("suppliers", totals);
return "intern/suppliers/index";
}
2020-05-30 16:43:07 +02:00
@GetMapping("/suppliers/{id}")
2020-05-30 20:16:27 +02:00
public String supplierDetail(Model model, @PathVariable String id) {
2020-05-30 21:13:25 +02:00
2020-06-05 15:43:46 +02:00
long supplierId = Long.parseLong(id);
2020-05-30 16:43:07 +02:00
2020-06-05 15:43:46 +02:00
// add orders from supplier to UImodel
2020-05-30 20:16:27 +02:00
List<UImodelSupplierDetailOrders> orders = new ArrayList<UImodelSupplierDetailOrders>();
for (SupplierOrder supplierOrder : supplierOrderRepository.findOrderBySupplierID(supplierId)) {
orders.add(new UImodelSupplierDetailOrders(supplierOrder));
}
2020-05-30 16:43:07 +02:00
2020-06-05 13:10:16 +02:00
// get latest supplier booking
Optional<BookingAccountEntry> supplierBooking = bookingAccountEntryRepository.getBySupplier(supplierId);
2020-06-05 15:43:46 +02:00
// get account balance
String supplierBalance = ((supplierBooking.isPresent())
? String.format("%.2f", ((float) supplierBooking.get().newSumCent / 100))
: "0,00");
// add bookings from supplier to UImodel
List<UImodelSupplierDetailBookings> bookings = new ArrayList<UImodelSupplierDetailBookings>();
for (Booking booking : bookingRepository.getBySupplier(supplierId)) {
bookings.add(new UImodelSupplierDetailBookings(booking));
}
2020-05-30 21:13:25 +02:00
UImodelSupplierDetail total = new UImodelSupplierDetail(supplierRepository.findSupplierById(supplierId).name,
2020-06-05 15:43:46 +02:00
supplierBalance, orders, bookings);
2020-05-30 16:43:07 +02:00
model.addAttribute("SupplierDetail", total);
return "intern/suppliers/id";
}
2020-05-30 16:16:14 +02:00
public class UImodelSuppliers {
2020-06-05 18:05:10 +02:00
public long id;
public String name;
2020-05-30 16:16:14 +02:00
public UImodelSuppliers(long id, String name) {
this.id = id;
this.name = name;
}
}
2020-05-30 16:43:07 +02:00
public class UImodelSupplierDetail {
2020-06-05 18:05:10 +02:00
public String name;
public String balance;
public List<UImodelSupplierDetailOrders> orders;
public List<UImodelSupplierDetailBookings> bookings;
2020-06-05 15:43:46 +02:00
public UImodelSupplierDetail(String name, String balance, List<UImodelSupplierDetailOrders> orders,
List<UImodelSupplierDetailBookings> bookings) {
2020-05-30 16:43:07 +02:00
this.name = name;
this.balance = balance;
this.orders = orders;
2020-06-05 15:43:46 +02:00
this.bookings = bookings;
2020-05-30 16:43:07 +02:00
}
}
public class UImodelSupplierDetailOrders {
2020-06-05 18:05:10 +02:00
public long id;
public String dateOrder;
public String articleName;
public long articleId;
public String priceNet;
public String quantity;
public String priceTotal;
public boolean arrived;
2020-05-30 21:13:25 +02:00
public UImodelSupplierDetailOrders(SupplierOrder order) {
2020-05-30 20:16:27 +02:00
this.id = order.id;
this.articleName = order.ordered.title;
this.articleId = order.ordered.id;
2020-06-05 18:05:10 +02:00
this.priceNet = String.format("%.2f", ((float) order.pricePerUnitNetCent / 100));
2020-05-30 20:16:27 +02:00
this.quantity = String.valueOf(order.numberOfUnits);
2020-06-05 18:05:10 +02:00
this.priceTotal = String.format("%.2f", ((float) order.totalPriceNet / 100));
2020-05-30 21:13:25 +02:00
2020-05-30 20:16:27 +02:00
Date date = new Date();
date.setTime(order.created.getTime());
this.dateOrder = new SimpleDateFormat("dd.MM.yyyy").format(date);
2020-05-30 16:43:07 +02:00
2020-05-30 21:13:25 +02:00
if (order.delivered != null) {
arrived = true;
} else {
arrived = false;
}
}
2020-05-30 16:43:07 +02:00
}
2020-06-05 15:43:46 +02:00
public class UImodelSupplierDetailBookings {
2020-06-05 18:05:10 +02:00
public String dateBooking;
public String price;
public String srcName;
public String balance;
public String reason;
public long orderID;
2020-06-05 15:43:46 +02:00
public UImodelSupplierDetailBookings(Booking booking) {
Date date = new Date();
date.setTime(booking.reason.supplierOrder.created.getTime());
this.dateBooking = new SimpleDateFormat("dd.MM.yyyy").format(date);
this.price = String.format("%.2f", ((float) booking.amountCent / 100));
this.srcName = ((booking.source.isMainAccount) ? "Hauptkonto" : booking.source.supplierAccount.name);
this.balance = String.format("%.2f", ((float) booking.destination.newSumCent / 100));
this.reason = booking.reason.comment;
this.orderID = booking.reason.supplierOrder.id;
}
}
2020-04-28 22:41:29 +02:00
}