2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.intern.suppliers ;
2020-04-28 22:41:29 +02:00
2020-06-12 19:22:36 +02:00
import org.hso.ecommerce.action.warehouse.SupplierOrderArrivedAction ;
import org.hso.ecommerce.entities.shop.Article ;
2020-05-30 23:32:01 +02:00
import org.hso.ecommerce.entities.supplier.SupplierOrder ;
2020-06-12 19:22:36 +02:00
import org.hso.ecommerce.entities.warehouse.WarehouseBookingPositionSlotEntry ;
import org.hso.ecommerce.repos.shop.ArticleRepository ;
2020-05-30 23:32:01 +02:00
import org.hso.ecommerce.repos.supplier.SupplierOrderRepository ;
2020-06-12 19:22:36 +02:00
import org.hso.ecommerce.repos.warehouse.SlotRepository ;
import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository ;
import org.hso.ecommerce.repos.warehouse.WarehouseBookingRepository ;
2020-05-30 23:32:01 +02:00
import org.springframework.beans.factory.annotation.Autowired ;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller ;
2020-05-30 23:32:01 +02:00
import org.springframework.ui.Model ;
import org.springframework.web.bind.annotation.GetMapping ;
2020-06-05 17:37:19 +02:00
import org.springframework.web.bind.annotation.PathVariable ;
import org.springframework.web.bind.annotation.PostMapping ;
2020-05-30 23:32:01 +02:00
import org.springframework.web.bind.annotation.RequestMapping ;
2020-06-12 19:22:36 +02:00
import javax.servlet.http.HttpServletResponse ;
import java.text.SimpleDateFormat ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.stream.Collectors ;
2020-04-28 22:41:29 +02:00
@Controller
2020-05-30 23:32:01 +02:00
@RequestMapping ( " /intern/ " )
2020-05-01 10:48:12 +02:00
public class SupplierOrderController {
2020-05-30 23:32:01 +02:00
@Autowired
private final SupplierOrderRepository supplierOrderRepository = null ;
2020-06-12 19:22:36 +02:00
@Autowired
private final ArticleRepository articleRepository = null ;
@Autowired
private final WarehouseBookingPositionSlotEntryRepository warehouseBookingPositionSlotEntryRepository = null ;
@Autowired
private final WarehouseBookingRepository warehouseBookingRepository = null ;
@Autowired
private final SlotRepository slotRepository = null ;
2020-05-30 23:32:01 +02:00
@GetMapping ( " supplierOrders " )
public String listSuppliers ( Model model ) {
2020-06-05 17:37:19 +02:00
2020-05-30 23:32:01 +02:00
List < UImodelSupplierOrder > totals = new ArrayList < UImodelSupplierOrder > ( ) ;
2020-06-24 02:15:31 +02:00
for ( SupplierOrder order : supplierOrderRepository . findAllDesc ( ) ) {
2020-06-12 19:22:36 +02:00
final Article article = articleRepository . findArticleByArticleOffer ( order . ordered ) . orElse ( null ) ;
totals . add ( new UImodelSupplierOrder ( order , article ) ) ;
2020-05-30 23:32:01 +02:00
}
model . addAttribute ( " orders " , totals ) ;
return " intern/supplierOrders/index " ;
}
2020-06-05 17:37:19 +02:00
@PostMapping ( " /supplierOrders/store/{id} " )
2020-06-12 19:22:36 +02:00
public String storeOrder ( @PathVariable ( " id " ) Long supplierOrderID , Model model , HttpServletResponse response ) {
SupplierOrder order = supplierOrderRepository . findById ( supplierOrderID ) . orElse ( null ) ;
if ( order = = null ) {
model . addAttribute ( " error " , " Die ausgewählte Lieferung konnte nicht gefunden werden. " ) ;
response . setStatus ( HttpServletResponse . SC_EXPECTATION_FAILED ) ;
return listSuppliers ( model ) ;
}
if ( order . wasDelivered ( ) ) {
model . addAttribute ( " error " , " Die ausgewählte Lieferung wurde schon zugestellt. " ) ;
response . setStatus ( HttpServletResponse . SC_EXPECTATION_FAILED ) ;
return listSuppliers ( model ) ;
}
2020-06-05 17:37:19 +02:00
2020-06-05 18:05:10 +02:00
2020-06-12 19:22:36 +02:00
final Article article = articleRepository . findArticleByArticleOffer ( order . ordered ) . orElse ( null ) ;
2020-06-13 11:54:25 +02:00
if ( article = = null ) {
2020-06-12 19:22:36 +02:00
model . addAttribute ( " error " , " Der bestellte Artikel wurde nicht angelegt, er hätte nicht bestellt werden dürfen. " ) ;
response . setStatus ( HttpServletResponse . SC_EXPECTATION_FAILED ) ;
return listSuppliers ( model ) ;
}
2020-06-05 17:37:19 +02:00
2020-06-12 19:22:36 +02:00
// Hard to do efficiently, this should be fine.
List < WarehouseBookingPositionSlotEntry > candidates = slotRepository
. findAll ( )
. stream ( )
. map ( slot - >
warehouseBookingPositionSlotEntryRepository . getBySlotNum ( slot . slotNum ) . orElseGet ( ( ) - >
WarehouseBookingPositionSlotEntry . empty ( article , slot )
)
)
2020-06-12 19:48:21 +02:00
. filter ( entry - > entry . article . id = = article . id | | entry . newSumSlot = = 0 )
2020-06-12 19:22:36 +02:00
. collect ( Collectors . toList ( ) ) ;
SupplierOrderArrivedAction action = new SupplierOrderArrivedAction ( candidates , order , article ) ;
try {
SupplierOrderArrivedAction . Result result = action . finish ( ) ;
supplierOrderRepository . save ( result . getOrder ( ) ) ;
warehouseBookingRepository . save ( result . getBooking ( ) ) ;
} catch ( SupplierOrderArrivedAction . NoSpaceInWarehouseException e ) {
e . printStackTrace ( ) ;
2020-06-05 17:37:19 +02:00
}
2020-06-12 19:22:36 +02:00
return " redirect:/intern/warehouse/todo " ;
2020-06-05 17:37:19 +02:00
}
2020-05-30 23:32:01 +02:00
public class UImodelSupplierOrder {
2020-06-05 18:05:10 +02:00
public long id ;
public String dateOrder ;
public String supplierName ;
public String articleName ;
public long articleId ;
public String priceNet ;
public String quantity ;
public String priceTotal ;
public boolean arrived ;
2020-06-24 00:37:58 +02:00
public String carrier ;
public String trackingId ;
public String estimatedArrival ;
2020-06-05 17:37:19 +02:00
2020-06-12 19:22:36 +02:00
public UImodelSupplierOrder ( SupplierOrder order , Article article ) {
2020-05-30 23:32:01 +02:00
this . id = order . id ;
this . supplierName = order . supplier . name ;
2020-06-12 19:22:36 +02:00
this . articleName = article ! = null ? article . title : " error " ;
this . articleId = article ! = null ? article . id : 0 ;
2020-06-05 18:05:10 +02:00
this . priceNet = String . format ( " %.2f " , ( ( float ) order . pricePerUnitNetCent / 100 ) ) ;
2020-05-30 23:32:01 +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 23:32:01 +02:00
2020-06-24 00:37:58 +02:00
this . carrier = order . carrier ! = null ? order . carrier : " - " ;
this . trackingId = order . trackingId ! = null ? order . trackingId : " - " ;
this . estimatedArrival = order . estimatedArrival ! = null
? new SimpleDateFormat ( " yyyy.MM.dd HH:00 " ) . format ( order . estimatedArrival ) + " Uhr "
: " - " ;
this . dateOrder = new SimpleDateFormat ( " yyyy.MM.dd " ) . format ( order . created ) ;
2020-05-30 23:32:01 +02:00
2020-06-12 19:22:36 +02:00
arrived = order . delivered ! = null ;
2020-05-30 23:32:01 +02:00
}
}
2020-04-28 22:41:29 +02:00
}