Skip to content

Commit

Permalink
Merge pull request #1 from hotuankietgit/nhan
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
hotuankietgit authored Dec 26, 2023
2 parents a8fc032 + 8bc6174 commit aff9b5e
Show file tree
Hide file tree
Showing 35 changed files with 1,039 additions and 536 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class AdminController {

@Autowired
private RoleService roleService;

@GetMapping("/report")
public String reportPage( ) {
return "report/report";
}

@GetMapping("/home")
public ModelAndView adminHomePage(Principal principal){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public class AgentController {
public String getHomePAage(){
return "home";
}

// @GetMapping("/")
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.PointOfSale.controller;

import com.example.PointOfSale.model.*;
import com.example.PointOfSale.service.AccountService;
import com.example.PointOfSale.service.CustomerService;
import com.example.PointOfSale.service.OrderService;
import com.example.PointOfSale.service.productService;
Expand All @@ -27,6 +28,8 @@ public class CheckOutController {
private productService productService1;
@Autowired
private OrderService orderService;
@Autowired AccountService accountService;


@PostMapping("/checkout")
public View handleCheckout(@RequestBody CheckoutData data, @AuthenticationPrincipal UserDetails userDetails) {
Expand Down Expand Up @@ -63,6 +66,7 @@ public View handleCheckout(@RequestBody CheckoutData data, @AuthenticationPrinci
orders.setTotalAmount(totalAmount);
orders.setBalance(balance);
orders.setPaidAmount(paidAmount);


List<OrderItems> itemsList = new ArrayList<>();
for (ProductData i : productData) {
Expand All @@ -83,12 +87,15 @@ public View handleCheckout(@RequestBody CheckoutData data, @AuthenticationPrinci
orderItems.setQuantity(quantity);
orderItems.setProduct(product);
orderItems.setOrders(orders);
Account account = accountService.getAccountByUsername(data.getEmployeeId());


System.out.println("Test OrderItem has a right Product: " + orderItems.getProduct());
itemsList.add(orderItems);
}

orders.setOrderItems(itemsList);
orders.setAccount(accountService.getAccountByUsername(data.getEmployeeId()));
Date currentDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class CustomerController {
@Autowired private OrderItemService orderItemService;

@Autowired private productService productService;


// , @Autowired OrderItemService orderItemService
public CustomerController(@Autowired CustomerService customerService, @Autowired OrderService orderService, @Autowired OrderItemService orderItemService){
this.customerService = customerService;
Expand Down
140 changes: 110 additions & 30 deletions src/main/java/com/example/PointOfSale/controller/productController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

import com.example.PointOfSale.model.Category;
import com.example.PointOfSale.model.Product;
import com.example.PointOfSale.model.ProductProfit;
import com.example.PointOfSale.model.ProductProfitByDate;
import com.example.PointOfSale.service.OrderItemService;
import com.example.PointOfSale.service.categoryService;
import com.example.PointOfSale.service.productService;
import com.example.PointOfSale.utils.uploadImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -17,7 +23,9 @@
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.awt.print.Printable;
import java.io.IOException;
import java.sql.Date;
import java.util.List;
import java.util.Optional;

Expand All @@ -26,39 +34,30 @@
public class productController {

@Autowired
private productService productService;
private categoryService categoryService;
@Autowired
private categoryService categoryService;



public productController(@Autowired productService productService){
this.productService = productService;
}
private productService productService;
@Autowired OrderItemService orderItemService;

@GetMapping("")
public String showProducts(Model model, @AuthenticationPrincipal UserDetails userDetails){
public String showProducts(Model model){

return listByPage(model, 1, userDetails);
return listByPage(model, 1);
}

@GetMapping("/page/{pageNumber}")
public String listByPage(Model model, @PathVariable("pageNumber") int currentPage, @AuthenticationPrincipal UserDetails userDetails){
public String listByPage(Model model, @PathVariable("pageNumber") int currentPage){
Page<Product> page = productService.getAll(currentPage);
long totalItems = page.getTotalElements();
int totalPages = page.getTotalPages();

List<String> roles = userDetails.getAuthorities().stream().map(GrantedAuthority::getAuthority).toList();
//getAuthories = [object { role.getName } , object{ role.getName } ] //object co kieu GrantedAuthority
// List<String> roles = ["Admin", "Agent"]

List<Product> productList = page.getContent();
model.addAttribute("currentPage", currentPage);
model.addAttribute("totalItems", totalItems);
model.addAttribute("totalPages", totalPages);
model.addAttribute("products", productList);
model.addAttribute("present", productList.size());
model.addAttribute("roles", roles);

return "index";
}

Expand All @@ -75,20 +74,73 @@ public String addProductForm(Model model){
}


// @PostMapping("/add")
// public String addProduct(Product product, @RequestParam("fileImage") MultipartFile multipartFile, Model model) throws IOException {
// if(!multipartFile.isEmpty()){
// String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename());
// product.setImage(fileName);
//
//// Optional<Product> optionalProduct = productService.findByBarcode(product.getBarcode());
//// if(optionalProduct.isPresent()){
//// model.addAttribute("Error", "Please enter another barcode");
//// return "add";
////// return "redirect:/products/add";
//// }
// Product addProduct = productService.add(product);
//
//
// String upload = "images";
// uploadImage.saveFile(upload, fileName, multipartFile);
// }
// else {
// if(product.getImage().isEmpty()){
// product.setImage(null);
// productService.add(product);
//
// }
//
// }
//
// productService.add(product);
// return "redirect:/products";
// }
//
//
// @GetMapping("/edit/{id}")
// public String edit(@PathVariable("id") int id, Model model){
// try {
// Optional<Product> result = productService.findById(id);
//
// if(result.isPresent()){
// Product product = result.get();
// String previousImage = product.getImage();
//
//
// model.addAttribute("pageTitle", "Edit Product");
// model.addAttribute("previousImage", previousImage);
//
// List<Category> categories = categoryService.getCategory();
// model.addAttribute("categories", categories);
// model.addAttribute("product", product);
// }
// else{
// System.out.println("Don't see product");
// }
// return "add";
// }catch (Exception e){
// e.printStackTrace();
//
// return "redirect:/products";
// }
//
// }
@PostMapping("/add")
public String addProduct(Product product, @RequestParam("fileImage") MultipartFile multipartFile, Model model) throws IOException {
public String addProduct(Product product, @RequestParam("fileImage") MultipartFile multipartFile) throws IOException {
if(!multipartFile.isEmpty()){
String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename());
product.setImage(fileName);

Optional<Product> optionalProduct = productService.findByBarcode(product.getBarcode());
if(optionalProduct.isPresent()){
model.addAttribute("Error", "Please enter another barcode");
// return "add";
return "redirect:/products/add";
}
Product addProduct = productService.add(product);

String upload = "images";
uploadImage.saveFile(upload, fileName, multipartFile);
}
Expand All @@ -115,10 +167,6 @@ public String edit(@PathVariable("id") int id, Model model, RedirectAttributes r
Product product = result.get();
String previousImage = product.getImage();

// System.out.println(product.getCreationDate() );
// if (product.getCreationDate() == null) {
// product.setCreationDate(LocalDate.now()); // Set it to the current date
// }

model.addAttribute("pageTitle", "Edit Product");
model.addAttribute("previousImage", previousImage);
Expand All @@ -140,18 +188,50 @@ public String edit(@PathVariable("id") int id, Model model, RedirectAttributes r

}


@PostMapping("/remove")
public String removePost(@RequestParam(value="options[]", required = false) String[] options){
if (options == null){
return "redirect:/products";
}


for (String option : options) {

int productId = Integer.valueOf(option);

if(orderItemService.checkProductExist(productId)){
//
return "redirect:/products?orderItemError=true";
}
productService.deleteById(productId);
}
return "redirect:/products";
}

@GetMapping("/profits")
public ResponseEntity<List<ProductProfit>> getProfitData(
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") String startDate,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") String endDate) {

// Convert String to java.sql.Date
Date startDateSql = Date.valueOf(startDate);
Date endDateSql = Date.valueOf(endDate);

List<ProductProfit> productProfits = productService.getProductProfits(startDateSql, endDateSql);

return new ResponseEntity<>(productProfits, HttpStatus.OK);
}
@GetMapping("/profits-by-date")
public ResponseEntity<List<ProductProfitByDate>> getProfitDataByDate(
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") String startDate,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") String endDate) {

// Convert String to java.sql.Date
Date startDateSql = Date.valueOf(startDate);
Date endDateSql = Date.valueOf(endDate);

List<ProductProfitByDate> productProfits = productService.getTotalProfitsByDate(startDateSql, endDateSql);

return new ResponseEntity<>(productProfits, HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.example.PointOfSale.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -13,7 +16,11 @@
import org.springframework.web.bind.annotation.RequestParam;
import com.example.PointOfSale.service.productService;

import jakarta.persistence.CollectionTable;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

Expand All @@ -38,8 +45,16 @@ public transactionController(@Autowired productService productService, @Autowire


@GetMapping("")
public String viewCart(Model model){

public String viewCart(Model model, Principal principal){

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String username = auth.getName();
Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();
String role = authorities.toArray()[0].toString(); // role duy nhat

model.addAttribute("username", username);
model.addAttribute("role", role);


model.addAttribute("cart", cart);
int total = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import com.example.PointOfSale.model.OrderItems;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.List;


@Repository
public interface OrderItemsRepository extends JpaRepository<OrderItems, Integer> {
List<OrderItems> findByOrders_OrderId(String orderId);

boolean existsByProductId(int productId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import com.example.PointOfSale.model.Orders;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.List;

@Repository
public interface OrderRepository extends JpaRepository<Orders, String> {

List<Orders> findByCustomer_CustomerID(int id);


}
Loading

0 comments on commit aff9b5e

Please sign in to comment.