2020-06-01 20:25:51 +02:00
package org.hso.ecommerce.controller.intern.warehouse ;
2020-06-03 16:55:36 +02:00
import org.hso.ecommerce.action.shop.EnableTrackingAction ;
2020-06-01 20:25:51 +02:00
import org.hso.ecommerce.entities.warehouse.WarehouseBooking ;
import org.hso.ecommerce.repos.warehouse.WarehouseBookingRepository ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Controller ;
2020-06-08 09:14:11 +02:00
import org.springframework.ui.Model ;
2020-06-01 20:25:51 +02:00
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.PathVariable ;
import org.springframework.web.bind.annotation.PostMapping ;
import org.springframework.web.bind.annotation.RequestMapping ;
2020-06-13 20:05:22 +02:00
import org.springframework.web.client.ResourceAccessException ;
2020-06-01 20:25:51 +02:00
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import java.util.Optional ;
@Controller
@RequestMapping ( " /intern/warehouse/ " )
public class TodoController {
@Autowired
private final WarehouseBookingRepository warehouseBookingRepository = null ;
@GetMapping ( " todo " )
public String accountingWarehouseTodo (
2020-06-08 09:14:11 +02:00
Model model
2020-06-01 20:25:51 +02:00
) {
2020-06-08 09:14:11 +02:00
model . addAttribute ( " bookings " , warehouseBookingRepository . findNotDone ( ) ) ;
2020-06-01 20:25:51 +02:00
return " intern/warehouse/todo " ;
}
@PostMapping ( " progress/{id} " )
public String postProgressId (
2020-06-08 09:14:11 +02:00
Model model ,
2020-06-01 20:25:51 +02:00
HttpServletRequest request ,
HttpServletResponse response ,
@PathVariable ( " id " ) Long id
) {
Optional < WarehouseBooking > booking = warehouseBookingRepository . findById ( id ) ;
if ( ! booking . isPresent ( ) ) {
2020-06-08 09:14:11 +02:00
model . addAttribute ( " error " , " Die Buchung wurde nicht gefunden. " ) ;
2020-06-01 20:25:51 +02:00
response . setStatus ( HttpServletResponse . SC_BAD_REQUEST ) ;
return " error/404 " ;
}
if ( booking . get ( ) . isInProgress ) {
response . setStatus ( 409 ) ;
return " intern/warehouse/error_progress_failed " ;
}
booking . get ( ) . isInProgress = true ;
warehouseBookingRepository . save ( booking . get ( ) ) ;
return " redirect:/intern/warehouse/progress/ " + id ;
}
@PostMapping ( " progress/{id}/finish " )
public String postProgressIdFinish (
2020-06-08 09:14:11 +02:00
Model model ,
2020-06-01 20:25:51 +02:00
HttpServletRequest request ,
HttpServletResponse response ,
@PathVariable ( " id " ) Long id
) {
Optional < WarehouseBooking > booking = warehouseBookingRepository . findById ( id ) ;
if ( ! booking . isPresent ( ) ) {
2020-06-08 09:14:11 +02:00
model . addAttribute ( " error " , " Die Buchung wurde nicht gefunden. " ) ;
2020-06-01 20:25:51 +02:00
response . setStatus ( HttpServletResponse . SC_BAD_REQUEST ) ;
return " error/404 " ;
}
booking . get ( ) . isInProgress = true ;
booking . get ( ) . isDone = true ;
2020-06-03 16:55:36 +02:00
// Update Delivery Date
if ( booking . get ( ) . reason . customerOrder ! = null ) {
2020-06-13 20:05:22 +02:00
try {
EnableTrackingAction . addTrackingInfo ( booking . get ( ) . reason . customerOrder ) ;
}
catch ( ResourceAccessException e )
{
return " error/500 " ;
}
2020-06-03 16:55:36 +02:00
}
2020-06-01 20:25:51 +02:00
warehouseBookingRepository . save ( booking . get ( ) ) ;
return " redirect:/intern/warehouse/todo " ;
}
@GetMapping ( " progress/{id} " )
2020-06-08 09:14:11 +02:00
public String getProgressId ( Model model ,
HttpServletRequest request ,
2020-06-01 20:25:51 +02:00
HttpServletResponse response ,
@PathVariable ( " id " ) Long id ) {
Optional < WarehouseBooking > booking = warehouseBookingRepository . findById ( id ) ;
if ( ! booking . isPresent ( ) ) {
2020-06-08 09:14:11 +02:00
model . addAttribute ( " error " , " Die Buchung wurde nicht gefunden. " ) ;
2020-06-01 20:25:51 +02:00
response . setStatus ( HttpServletResponse . SC_BAD_REQUEST ) ;
return " error/404 " ;
}
if ( booking . get ( ) . isDone ) {
2020-06-08 09:14:11 +02:00
model . addAttribute ( " info " , " Die Buchung wurde schon abgeschlossen. " ) ;
2020-06-01 20:25:51 +02:00
}
if ( ! booking . get ( ) . isInProgress ) {
// Only reachable if path is manipulated.
2020-06-08 09:14:11 +02:00
model . addAttribute ( " error " , " Die Buchung wurde noch nicht zugewiesen! " ) ;
2020-06-01 20:25:51 +02:00
}
2020-06-08 09:14:11 +02:00
model . addAttribute ( " booking " , booking . get ( ) ) ;
2020-06-01 20:25:51 +02:00
return " intern/warehouse/id_progress " ;
}
}