-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added VAT rate column to treasurer tab * change to layout of treasurer tab * Created Sales export tab, which can export sales per month to csv file (replaces the Treasurer tab) * Rename Salesexport to SalesExport --------- Co-authored-by: root <[email protected]>
- Loading branch information
1 parent
40983e9
commit d81dd86
Showing
12 changed files
with
555 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
src/main/java/ch/wisv/events/admin/controller/DashboardSalesExportController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package ch.wisv.events.admin.controller; | ||
|
||
import ch.wisv.events.core.model.order.PaymentMethod; | ||
import ch.wisv.events.core.admin.TreasurerData; | ||
import ch.wisv.events.core.admin.SalesExportSubmission; | ||
import ch.wisv.events.utils.LdapGroup; | ||
|
||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.*; | ||
import java.io.InputStream; | ||
import java.io.ByteArrayInputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import com.google.common.collect.Lists; | ||
|
||
import ch.wisv.events.core.repository.OrderRepository; | ||
import org.apache.commons.lang3.tuple.ImmutableTriple; | ||
import org.apache.commons.lang3.tuple.Triple; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.javatuples.Septet; | ||
|
||
import org.springframework.core.io.InputStreamResource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
|
||
|
||
/** | ||
* DashboardSalesExportController class. | ||
*/ | ||
@Controller | ||
@RequestMapping("/administrator/salesexport") | ||
@PreAuthorize("hasRole('ADMIN')") | ||
public class DashboardSalesExportController extends DashboardController { | ||
|
||
/** TreasurerRepository */ | ||
private final OrderRepository orderRepository; | ||
|
||
/** | ||
* DashboardSalesExportController constructor. | ||
* | ||
* @param orderRepository of type OrderRepository | ||
*/ | ||
@Autowired | ||
public DashboardSalesExportController(OrderRepository orderRepository) { | ||
this.orderRepository = orderRepository; | ||
} | ||
|
||
/** | ||
* Index of vendor [GET "/"]. | ||
* | ||
* @param model String model | ||
* | ||
* @return path to Thymeleaf template location | ||
*/ | ||
@GetMapping | ||
public String index(Model model) { | ||
// model.addAttribute("productMap", this.generateProductMap()); | ||
model.addAttribute("SalesExportSubmission", new SalesExportSubmission()); | ||
|
||
return "admin/salesexport/index"; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Exports sales of month to csv | ||
* | ||
*/ | ||
@GetMapping(value="/csv", produces="text/csv") | ||
public HttpEntity<? extends Object> csvExport(@ModelAttribute SalesExportSubmission SalesExportSubmission, Model model) { | ||
model.addAttribute("SalesExportSubmission", SalesExportSubmission); | ||
|
||
// Convert payment methods to integers | ||
List<Integer> paymentMethods = new ArrayList<>(); | ||
SalesExportSubmission.getIncludedPaymentMethods().forEach( (m) -> paymentMethods.add(m.toInt()) ); | ||
|
||
List<TreasurerData> treasurerData = orderRepository.findallPaymentsByMonth(SalesExportSubmission.getMonth(), SalesExportSubmission.getYear(), paymentMethods, SalesExportSubmission.isFreeProductsIncluded()); | ||
|
||
ListIterator<TreasurerData> listIterator = treasurerData.listIterator(treasurerData.size()); | ||
|
||
// Loop through all orders in TreasurerData and add orders of the same product together | ||
|
||
// Key: Product ID, Value: Septet with: event title, organized by, product title, total income, amount, vatRate, price | ||
Map<Integer, Septet<String, Integer, String, Double, Integer, String, Double>> map = new HashMap<>(); | ||
|
||
while (listIterator.hasPrevious()) { | ||
TreasurerData data = listIterator.previous(); | ||
|
||
if (!map.containsKey(data.getProductId())) { | ||
map.put( | ||
data.getProductId(), | ||
Septet.with( | ||
data.getEventTitle(), | ||
data.getOrganizedBy(), | ||
data.getProductTitle(), | ||
data.getPrice(), | ||
data.getAmount(), | ||
data.getVatRate(), | ||
data.getPrice())); | ||
} else { | ||
Double totalIncome = map.get(data.getProductId()).getValue3(); | ||
Integer totalAmount = map.get(data.getProductId()).getValue4(); | ||
map.put( | ||
data.getProductId(), | ||
Septet.with( | ||
data.getEventTitle(), | ||
data.getOrganizedBy(), | ||
data.getProductTitle(), | ||
totalIncome + data.getPrice(), | ||
totalAmount + data.getAmount(), | ||
data.getVatRate(), | ||
data.getPrice())); | ||
} | ||
} | ||
|
||
// String csvContent = "Options:\n" + SalesExportSubmission.toString() + "\n\n"; | ||
// csvContent += "Event;Organized by;Product;Total income;Total amount;VAT rate\n"; | ||
String csvContent = "Event;Organized by;Product;Total income;Total amount;VAT rate;price\n"; | ||
for (Map.Entry<Integer, Septet<String, Integer, String, Double, Integer, String, Double>> entry : map.entrySet()) { | ||
csvContent += entry.getValue().getValue0() // event title | ||
+ ";" + LdapGroup.intToString(entry.getValue().getValue1()) // organized by | ||
+ ";" + entry.getValue().getValue2() // product title | ||
+ ";" + entry.getValue().getValue3() // total income | ||
+ ";" + entry.getValue().getValue4() // total amount | ||
+ ";" + entry.getValue().getValue5() // vat rate | ||
+ ";" + entry.getValue().getValue6() + "\n"; // price | ||
} | ||
|
||
InputStream bufferedInputStream = new ByteArrayInputStream(csvContent.getBytes(StandardCharsets.UTF_8)); | ||
InputStreamResource fileInputStream = new InputStreamResource(bufferedInputStream); | ||
|
||
String filename = "Sales_overview_"+SalesExportSubmission.getYear()+"-"+SalesExportSubmission.getMonth()+"_export.csv"; | ||
|
||
// setting HTTP headers | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename); | ||
// defining the custom Content-Type | ||
headers.set(HttpHeaders.CONTENT_TYPE, "text/csv"); | ||
|
||
return new ResponseEntity<>( | ||
fileInputStream, | ||
headers, | ||
HttpStatus.OK | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/ch/wisv/events/core/admin/SalesExportSubmission.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package ch.wisv.events.core.admin; | ||
|
||
import ch.wisv.events.core.model.order.PaymentMethod; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import java.util.*; | ||
import java.time.LocalDate; | ||
import java.time.LocalDate; | ||
import com.google.common.collect.Lists; | ||
|
||
|
||
/** | ||
* Class that contains the settings for the salesexport query, | ||
* which can be specified in on the form on the Sales Export tab. | ||
*/ | ||
@Getter | ||
@Setter | ||
public class SalesExportSubmission { | ||
|
||
/** | ||
* Year of query | ||
*/ | ||
private int year; | ||
|
||
/** | ||
* Month of query. | ||
*/ | ||
private int month; | ||
|
||
/** | ||
* Payment methods that should be contained in query. | ||
*/ | ||
private List<PaymentMethod> includedPaymentMethods; | ||
|
||
private boolean freeProductsIncluded; | ||
|
||
/** | ||
* Default constructor. | ||
*/ | ||
public SalesExportSubmission() { | ||
|
||
// default: previous month | ||
if (LocalDate.now().getMonthValue() == 1) { | ||
this.year = LocalDate.now().getYear()-1; | ||
this.month = 12; | ||
} | ||
else { | ||
this.year = LocalDate.now().getYear(); | ||
this.month = LocalDate.now().getMonthValue()-1; | ||
} | ||
|
||
this.includedPaymentMethods = Lists.newArrayList(PaymentMethod.IDEAL, PaymentMethod.SOFORT); | ||
|
||
this.freeProductsIncluded = false; | ||
|
||
} | ||
|
||
/** | ||
* Return all options in a string. | ||
*/ | ||
public String toString() { | ||
String settings = "Year: " + this.year + ", Month: " + this.month + ", Free products included: " + this.freeProductsIncluded; | ||
for (PaymentMethod paymentMethod : this.includedPaymentMethods) { | ||
settings += ", Payment method: " + paymentMethod.getName(); | ||
} | ||
return settings; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.