Skip to content

Commit

Permalink
FEAT:fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
rivopelu committed Dec 1, 2024
1 parent 48753b4 commit 812dd0b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/java/com/pos/app/controller/AnalyticsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.pos.app.annotations.BaseController;
import com.pos.app.model.response.BaseResponse;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

Expand Down Expand Up @@ -33,4 +34,7 @@ BaseResponse getReportSales(
@RequestParam(value = "start-date", required = false) Date startDate,
@RequestParam(value = "end-date", required = false) Date endDate
);

@GetMapping("v1/download-report")
ResponseEntity<byte[]> downloadReport();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.pos.app.utils.ResponseHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;

import java.util.Date;

Expand Down Expand Up @@ -36,4 +37,10 @@ public BaseResponse getAnalyticsChartRevenue(Date startDate, Date endDate) {
public BaseResponse getReportSales(Pageable pageable, Date startDate, Date endDate) {
return ResponseHelper.createBaseResponse(analyticsService.getReportSales(pageable, startDate, endDate));
}

@Override
public ResponseEntity<byte[]> downloadReport() {

return analyticsService.downloadReport();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
@JsonSerialize
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class ResponseSalesReport {

private String productName;
private String productId;
private String orderId;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/pos/app/repositories/OrderProductRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,15 @@ public interface OrderProductRepository extends JpaRepository<OrderProduct, Stri
"join Transaction as t on t.order.id = o.id " +
"order by op.createdDate desc ")
Page<Object[]> getSalesReport(Pageable pageable);

@Query(value = "select p.id, p.name," +
" o.id, op.qty, op.pricePerQty," +
" op.totalPrice, t.totalTransaction, " +
"t.taxPercentage, op.createdDate" +
" from OrderProduct as op " +
"join Product as p on op.product.id = p.id " +
"join Order as o on o.id = op.order.id " +
"join Transaction as t on t.order.id = o.id " +
"order by op.createdDate desc ")
List<Object[]> getSalesReport();
}
4 changes: 4 additions & 0 deletions src/main/java/com/pos/app/service/AnalyticsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.pos.app.model.response.ResponseSalesReport;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;

import java.util.Date;
import java.util.List;
Expand All @@ -18,4 +19,7 @@ public interface AnalyticsService {
List<ResponseChartOrder> getAnalyticsChartRevenue(Date startDate, Date endDate);

Page getReportSales(Pageable pageable, Date startDate, Date endDate);

ResponseEntity<byte[]> downloadReport();

}
43 changes: 43 additions & 0 deletions src/main/java/com/pos/app/service/impl/AnalyticsServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -111,4 +117,41 @@ public Page getReportSales(Pageable pageable, Date startDate, Date endDate) {
throw new SystemErrorException(e);
}
}

@Override
public ResponseEntity<byte[]> downloadReport() {
List<Object[]> orderProductPage = orderProductRepository.getSalesReport();
try {
String[] headers = {"Product Name", "Product ID", "order ID", "qty", "price per qty", "total price", "total transaction", "tax", "date"};
String[][] data = new String[orderProductPage.size()][headers.length];

int index = 0;
for (Object[] obj : orderProductPage) {
String[] objData = new String[obj.length];
for (int i = 0; i < obj.length; i++) {
objData[i] = obj[i] != null ? obj[i].toString() : "";
}
data[index] = objData;
index++;
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(byteArrayOutputStream));

writer.println(String.join(",", headers));

for (String[] row : data) {
writer.println(String.join(",", row));
}

writer.flush();
writer.close();

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Disposition", "attachment; filename=data.csv");

return new ResponseEntity<>(byteArrayOutputStream.toByteArray(), httpHeaders, HttpStatus.OK);
} catch (Exception e) {
throw new SystemErrorException(e);
}
}
}

0 comments on commit 812dd0b

Please sign in to comment.