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 23:32:01 +02:00
import java.text.SimpleDateFormat ;
import java.util.ArrayList ;
import java.util.Date ;
import java.util.List ;
2020-06-05 17:37:19 +02:00
import java.util.Optional ;
2020-05-30 23:32:01 +02:00
import org.hso.ecommerce.entities.supplier.SupplierOrder ;
import org.hso.ecommerce.repos.supplier.SupplierOrderRepository ;
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-05 17:37:19 +02:00
import org.springframework.web.servlet.view.RedirectView ;
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 ;
@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 > ( ) ;
for ( SupplierOrder orders : supplierOrderRepository . findAll ( ) ) {
totals . add ( new UImodelSupplierOrder ( orders ) ) ;
}
model . addAttribute ( " orders " , totals ) ;
return " intern/supplierOrders/index " ;
}
2020-06-05 17:37:19 +02:00
@PostMapping ( " /supplierOrders/store/{id} " )
public RedirectView storeOrder ( @PathVariable ( required = true ) String id ) {
long supplierOrderID = Long . parseLong ( id ) ;
Optional < SupplierOrder > order = supplierOrderRepository . findById ( supplierOrderID ) ;
if ( order . isPresent ( ) ) {
2020-06-05 18:05:10 +02:00
// TODO call action
2020-06-05 17:37:19 +02:00
System . out . println ( " Order is present \ n " ) ;
}
return new RedirectView ( " ../../supplierOrders/ " ) ;
}
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-05 17:37:19 +02:00
2020-05-30 23:32:01 +02:00
public UImodelSupplierOrder ( SupplierOrder order ) {
this . id = order . id ;
this . supplierName = order . supplier . name ;
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 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
Date date = new Date ( ) ;
date . setTime ( order . created . getTime ( ) ) ;
this . dateOrder = new SimpleDateFormat ( " dd.MM.yyyy " ) . format ( date ) ;
if ( order . delivered ! = null ) {
arrived = true ;
} else {
arrived = false ;
}
}
}
2020-04-28 22:41:29 +02:00
}